Chinaunix首页 | 论坛 | 博客
  • 博客访问: 479525
  • 博文数量: 100
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-21 09:30
文章分类

全部博文(100)

文章存档

2017年(1)

2016年(16)

2015年(83)

我的朋友

分类: 嵌入式

2016-01-17 21:44:25

参考http://www.arm9home.net/read.php?tid-80476.html
http://www.arm9home.net/read.php?tid-25944.html
https://github.com/kangear/tiny210v2-uboot

4412的u-boot:
https://github.com/friendlyarm/uboot_tiny4412

cmd_usbd.c :

点击(此处)折叠或打开

  1. /*
  2.  * common/cmd_usbd.c
  3.  *
  4.  * $Id: cmd_usbd.c,v 1.2 2009/01/28 00:11:42 dark0351 Exp $
  5.  *
  6.  * (C) Copyright 2007
  7.  * Byungjae Lee, Samsung Erectronics, bjlee@samsung.com.
  8.  *    - support for S3C2412, S3C2443 and S3C6400
  9.  *
  10.  * (C) Copyright SAMSUNG Electronics
  11.  * SW.LEE <hitchcar@samsung.com>
  12.  * - add USB device fo S3C2440A, S3C24A0A
  13.  *
  14.  * (C) Copyright 2000
  15.  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  16.  *
  17.  * See file CREDITS for list of people who contributed to this
  18.  * project.
  19.  *
  20.  * This program is free software; you can redistribute it and/or
  21.  * modify it under the terms of the GNU General Public License as
  22.  * published by the Free Software Foundation; either version 2 of
  23.  * the License, or (at your option) any later version.
  24.  */

  25. /*
  26.  * Memory Functions
  27.  *
  28.  * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
  29.  */

  30. #include <common.h>
  31. #include <command.h>

  32. #ifdef CONFIG_S3C_USBD

  33. #include <regs.h>

  34. #if defined(CONFIG_S3C2412) || defined(CONFIG_S3C2442)
  35. #include "../cpu/s3c24xx/usbd-fs.h"
  36. #elif defined(CONFIG_S3C2443) || defined(CONFIG_S3C2450) || defined(CONFIG_S3C2416)
  37. #include "../cpu/s3c24xx/usbd-hs.h"
  38. #elif defined(CONFIG_S3C6400) || defined(CONFIG_S3C6410) || defined(CONFIG_S3C6430)
  39. #include "../cpu/s3c64xx/usbd-otg-hs.h"
  40. #elif defined(CONFIG_S5PC100)
  41. #include "../cpu/s5pc1xx/usbd-otg-hs.h"
  42. #elif defined(CONFIG_S5PC110)
  43. #include "../cpu/s5pc11x/usbd-otg-hs.h"
  44. #elif defined(CONFIG_S5P6440)
  45. #include "../cpu/s5p64xx/usbd-otg-hs.h"
  46. #elif defined(CONFIG_S5P6442)
  47. #include "../cpu/s5p644x/usbd-otg-hs.h"
  48. #else
  49. #error "* CFG_ERROR : you have to setup right Samsung CPU configuration"
  50. #endif

  51. #undef    CMD_USBD_DEBUG
  52. #ifdef    CMD_USBD_DEBUG
  53. #define    PRINTF(fmt,args...)    printf (fmt ,##args)
  54. #else
  55. #define PRINTF(fmt,args...)
  56. #endif

  57. static const char pszMe[] = "usbd: ";

  58. int do_usbd_dnw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  59. {

  60.     if (argv[0][0] == 'u') {
  61.         DNW = 0;
  62.     }
  63.     else {
  64.         DNW = 1;
  65.         s3c_got_header = 0;
  66.     }

  67.     switch (argc) {
  68.     case 1 :
  69.         s3c_usbd_dn_addr = USBD_DOWN_ADDR;    /* Default Address */
  70.         break;
  71.     case 2 :
  72.         s3c_usbd_dn_addr = simple_strtoul(argv[1], NULL, 16);
  73.         break;
  74.     default:
  75.         printf ("Usage:\n%s\n", cmdtp->usage);
  76.         return 1;
  77.     }

  78.     s3c_receive_done = 0;

  79.     s3c_usbctl_init(0);
  80.     s3c_usbc_activate();

  81.     PRINTF("Download address 0x%08x\n", s3c_usbd_dn_addr);
  82.     printf("Now, Waiting for DNW to transmit data\n");

  83.     while (1) {
  84.         if (S3C_USBD_DETECT_IRQ()) {
  85.             s3c_udc_int_hndlr();
  86.             S3C_USBD_CLEAR_IRQ();
  87.         }

  88.         if (s3c_receive_done)
  89.             break;

  90.         if (serial_tstc()) {
  91.             serial_getc();
  92.             break;
  93.         }
  94.     }

  95.     /* when operation is done, usbd must be stopped */
  96.     s3c_usb_stop();

  97.     return 0;
  98. }

  99. #if 0 /* ud command not support yet */
  100. U_BOOT_CMD(
  101.     ud, 3, 0, do_usbd_dnw,
  102.     "ud - initialize USB device and ready to receive for LINUX server (specific)\n",
  103.     "[download address]\n"
  104. );
  105. #endif

  106. U_BOOT_CMD(
  107.     dnw, 3, 0, do_usbd_dnw,
  108.     "dnw - initialize USB device and ready to receive for Windows server (specific)\n",
  109.     "[download address]\n"
  110. );

  111. #endif    /* CONFIG_S3C_USBD */
Usbd-otg-hs.c (cpu\s5pc11x)

点击(此处)折叠或打开

  1. /*
  2.  * cpu/s5pc1xx/usbd-otg-hs.c
  3.  *
  4.  * (C) Copyright 2007
  5.  * Byungjae Lee, Samsung Erectronics, bjlee@samsung.com.
  6.  *    - only support for S5PC100
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21.  * MA 02111-1307 USA
  22.  */

  23. #include <common.h>

  24. #if defined(CONFIG_S5PC110)
  25. #include <command.h>
  26. #include <asm/errno.h>
  27. #include <asm/io.h>
  28. #include <regs.h>
  29. #include "usbd-otg-hs.h"

  30. #undef USB_OTG_DEBUG_SETUP
  31. #ifdef USB_OTG_DEBUG_SETUP
  32. #define DBG_SETUP0(fmt, args...) printf("[%s:%d] " fmt, __FUNCTION__, __LINE__, ##args)
  33. #define DBG_SETUP1(fmt, args...) printf("\t" fmt, ##args)
  34. #define DBG_SETUP2(fmt, args...) printf(fmt, ##args)
  35. #else
  36. #define DBG_SETUP0(fmt, args...) do { } while (0)
  37. #define DBG_SETUP1(fmt, args...) do { } while (0)
  38. #define DBG_SETUP2(fmt, args...) do { } while (0)
  39. #endif

  40. #undef USB_OTG_DEBUG_BULK
  41. #ifdef USB_OTG_DEBUG_BULK
  42. #define DBG_BULK0(fmt, args...) printf("[%s:%d] " fmt, __FUNCTION__, __LINE__, ##args)
  43. #define DBG_BULK1(fmt, args...)    printf("\t" fmt, ##args)
  44. #else
  45. #define DBG_BULK0(fmt, args...) do { } while (0)
  46. #define DBG_BULK1(fmt, args...) do { } while (0)
  47. #endif

  48. #define USB_CHECKSUM_EN

  49. #define TRUE    1
  50. #define FALSE    0
  51. #define SUSPEND_RESUME_ON FALSE

  52. u32 s3c_usbd_dn_addr = 0;
  53. u32 s3c_usbd_dn_cnt = 0;
  54. u32 remode_wakeup;
  55. u16 config_value;

  56. int DNW;
  57. int s3c_receive_done = 0;
  58. int s3c_got_header = 0;
  59. int s3c_usb_phy_inited=0;
  60. int s3c_usb_connected_status= 0;/*0,none,1,usb connected,2,power connected,3,bad usb connected*/
  61. USB_OPMODE    op_mode = USB_CPU;
  62. USB_SPEED    speed = USB_HIGH;

  63. otg_dev_t    otg;
  64. get_status_t    get_status;
  65. get_intf_t    get_intf;

  66. enum EP_INDEX
  67. {
  68.     EP0, EP1, EP2, EP3, EP4
  69. };

  70. /*------------------------------------------------*/
  71. /* EP0 state */
  72. enum EP0_STATE
  73. {
  74.     EP0_STATE_INIT            = 0,
  75.     EP0_STATE_GD_DEV_0        = 11,
  76.     EP0_STATE_GD_DEV_1        = 12,
  77.     EP0_STATE_GD_DEV_2        = 13,
  78.     EP0_STATE_GD_CFG_0        = 21,
  79.     EP0_STATE_GD_CFG_1        = 22,
  80.     EP0_STATE_GD_CFG_2        = 23,
  81.     EP0_STATE_GD_CFG_3        = 24,
  82.     EP0_STATE_GD_CFG_4        = 25,
  83.     EP0_STATE_GD_STR_I0        = 30,
  84.     EP0_STATE_GD_STR_I1        = 31,
  85.     EP0_STATE_GD_STR_I2        = 32,
  86.     EP0_STATE_GD_DEV_QUALIFIER    = 33,
  87.     EP0_STATE_INTERFACE_GET        = 34,
  88.     EP0_STATE_GET_STATUS0        = 35,
  89.     EP0_STATE_GET_STATUS1        = 36,
  90.     EP0_STATE_GET_STATUS2        = 37,
  91.     EP0_STATE_GET_STATUS3        = 38,
  92.     EP0_STATE_GET_STATUS4        = 39,
  93.     EP0_STATE_GD_OTHER_SPEED    = 40,
  94.     EP0_STATE_GD_CFG_ONLY_0     = 41,
  95.     EP0_STATE_GD_CFG_ONLY_1     = 42,
  96.     EP0_STATE_GD_IF_ONLY_0        = 44,
  97.     EP0_STATE_GD_IF_ONLY_1        = 45,
  98.     EP0_STATE_GD_EP0_ONLY_0     = 46,
  99.     EP0_STATE_GD_EP1_ONLY_0     = 47,
  100.     EP0_STATE_GD_EP2_ONLY_0     = 48,
  101.     EP0_STATE_GD_EP3_ONLY_0     = 49,
  102.     EP0_STATE_GD_OTHER_SPEED_HIGH_1    = 51,
  103.     EP0_STATE_GD_OTHER_SPEED_HIGH_2    = 52,
  104.     EP0_STATE_GD_OTHER_SPEED_HIGH_3    = 53
  105. };

  106. /*definitions related to CSR setting */

  107. /* S5P_OTG_GOTGCTL*/
  108. #define B_SESSION_VALID        (0x1<<19)
  109. #define A_SESSION_VALID        (0x1<<18)

  110. /* S5P_OTG_GAHBCFG*/
  111. #define PTXFE_HALF        (0<<8)
  112. #define PTXFE_ZERO        (1<<8)
  113. #define NPTXFE_HALF        (0<<7)
  114. #define NPTXFE_ZERO        (1<<7)
  115. #define MODE_SLAVE        (0<<5)
  116. #define MODE_DMA        (1<<5)
  117. #define BURST_SINGLE        (0<<1)
  118. #define BURST_INCR        (1<<1)
  119. #define BURST_INCR4        (3<<1)
  120. #define BURST_INCR8        (5<<1)
  121. #define BURST_INCR16        (7<<1)
  122. #define GBL_INT_UNMASK        (1<<0)
  123. #define GBL_INT_MASK        (0<<0)

  124. /* S5P_OTG_GRSTCTL*/
  125. #define AHB_MASTER_IDLE        (1u<<31)
  126. #define CORE_SOFT_RESET        (0x1<<0)

  127. /* S5P_OTG_GINTSTS/S5P_OTG_GINTMSK core interrupt register */
  128. #define INT_RESUME        (1u<<31)
  129. #define INT_DISCONN        (0x1<<29)
  130. #define INT_CONN_ID_STS_CNG    (0x1<<28)
  131. #define INT_OUT_EP        (0x1<<19)
  132. #define INT_IN_EP        (0x1<<18)
  133. #define INT_ENUMDONE        (0x1<<13)
  134. #define INT_RESET        (0x1<<12)
  135. #define INT_SUSPEND        (0x1<<11)
  136. #define INT_TX_FIFO_EMPTY    (0x1<<5)
  137. #define INT_RX_FIFO_NOT_EMPTY    (0x1<<4)
  138. #define INT_SOF            (0x1<<3)
  139. #define INT_DEV_MODE        (0x0<<0)
  140. #define INT_HOST_MODE        (0x1<<1)

  141. /* S5P_OTG_GRXSTSP STATUS*/
  142. #define GLOBAL_OUT_NAK            (0x1<<17)
  143. #define OUT_PKT_RECEIVED        (0x2<<17)
  144. #define OUT_TRNASFER_COMPLETED        (0x3<<17)
  145. #define SETUP_TRANSACTION_COMPLETED    (0x4<<17)
  146. #define SETUP_PKT_RECEIVED        (0x6<<17)

  147. /* S5P_OTG_DCTL device control register */
  148. #define NORMAL_OPERATION        (0x1<<0)
  149. #define SOFT_DISCONNECT            (0x1<<1)
  150. #define    TEST_J_MODE            (TEST_J<<4)
  151. #define    TEST_K_MODE            (TEST_K<<4)
  152. #define    TEST_SE0_NAK_MODE        (TEST_SE0_NAK<<4)
  153. #define    TEST_PACKET_MODE        (TEST_PACKET<<4)
  154. #define    TEST_FORCE_ENABLE_MODE        (TEST_FORCE_ENABLE<<4)
  155. #define TEST_CONTROL_FIELD        (0x7<<4)

  156. /* S5P_OTG_DAINT device all endpoint interrupt register */
  157. #define INT_IN_EP0            (0x1<<0)
  158. #define INT_IN_EP1            (0x1<<1)
  159. #define INT_IN_EP3            (0x1<<3)
  160. #define INT_OUT_EP0            (0x1<<16)
  161. #define INT_OUT_EP2            (0x1<<18)
  162. #define INT_OUT_EP4            (0x1<<20)

  163. /* S5P_OTG_DIEPCTL0/S5P_OTG_DOEPCTL0 */
  164. #define DEPCTL_EPENA            (0x1<<31)
  165. #define DEPCTL_EPDIS            (0x1<<30)
  166. #define DEPCTL_SNAK            (0x1<<27)
  167. #define DEPCTL_CNAK            (0x1<<26)
  168. #define DEPCTL_CTRL_TYPE        (EP_TYPE_CONTROL<<18)
  169. #define DEPCTL_ISO_TYPE            (EP_TYPE_ISOCHRONOUS<<18)
  170. #define DEPCTL_BULK_TYPE        (EP_TYPE_BULK<<18)
  171. #define DEPCTL_INTR_TYPE        (EP_TYPE_INTERRUPT<<18)
  172. #define DEPCTL_USBACTEP            (0x1<<15)

  173. /*ep0 enable, clear nak, next ep0, max 64byte */
  174. #define EPEN_CNAK_EP0_64 (DEPCTL_EPENA|DEPCTL_CNAK|(CONTROL_EP<<11)|(0<<0))

  175. /*ep0 enable, clear nak, next ep0, 8byte */
  176. #define EPEN_CNAK_EP0_8 (DEPCTL_EPENA|DEPCTL_CNAK|(CONTROL_EP<<11)|(3<<0))

  177. /* DIEPCTLn/DOEPCTLn */
  178. #define BACK2BACK_SETUP_RECEIVED    (0x1<<6)
  179. #define INTKN_TXFEMP            (0x1<<4)
  180. #define NON_ISO_IN_EP_TIMEOUT        (0x1<<3)
  181. #define CTRL_OUT_EP_SETUP_PHASE_DONE    (0x1<<3)
  182. #define AHB_ERROR            (0x1<<2)
  183. #define TRANSFER_DONE            (0x1<<0)


  184. /* codes representing languages */
  185. const u8 string_desc0[] =
  186. {
  187.     4, STRING_DESCRIPTOR, LANGID_US_L, LANGID_US_H,
  188. };

  189. const u8 string_desc1[] = /* Manufacturer */
  190. {
  191.     (0x14+2), STRING_DESCRIPTOR,
  192.     'S', 0x0, 'y', 0x0, 's', 0x0, 't', 0x0, 'e', 0x0,
  193.     'm', 0x0, ' ', 0x0, 'M', 0x0, 'C', 0x0, 'U', 0x0,
  194. };

  195. const u8 string_desc2[] = /* Product */
  196. {
  197.     (0x2a+2), STRING_DESCRIPTOR,
  198.     'S', 0x0, 'E', 0x0, 'C', 0x0, ' ', 0x0, 'S', 0x0,
  199.     '3', 0x0, 'C', 0x0, '6', 0x0, '4', 0x0, '0', 0x0,
  200.     '0', 0x0, 'X', 0x0, ' ', 0x0, 'T', 0x0, 'e', 0x0,
  201.     's', 0x0, 't', 0x0, ' ', 0x0, 'B', 0x0, '/', 0x0,
  202.     'D', 0x0
  203. };

  204. /* setting the device qualifier descriptor and a string descriptor */
  205. const u8 qualifier_desc[] =
  206. {
  207.     0x0a,    /* 0 desc size */
  208.     0x06,    /* 1 desc type (DEVICE_QUALIFIER)*/
  209.     0x00,    /* 2 USB release */
  210.     0x02,    /* 3 => 2.00*/
  211.     0xFF,    /* 4 class */
  212.     0x00,    /* 5 subclass */
  213.     0x00,    /* 6 protocol */
  214.     64,    /* 7 max pack size */
  215.     0x01,    /* 8 number of other-speed configuration */
  216.     0x00,    /* 9 reserved */
  217. };

  218. const u8 config_full[] =
  219. {
  220.     0x09,    /* 0 desc size */
  221.     0x07,    /* 1 desc type (other speed)*/
  222.     0x20,    /* 2 Total length of data returned */
  223.     0x00,    /* 3 */
  224.     0x01,    /* 4 Number of interfaces supported by this speed configuration */
  225.     0x01,    /* 5 value to use to select configuration */
  226.     0x00,    /* 6 index of string desc */
  227.         /* 7 same as configuration desc */
  228.     CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED,
  229.     0x19,    /* 8 same as configuration desc */

  230. };

  231. const u8 config_full_total[] =
  232. {
  233.   0x09, 0x07 ,0x20 ,0x00 ,0x01 ,0x01 ,0x00 ,0xC0 ,0x19,
  234.   0x09 ,0x04 ,0x00 ,0x00 ,0x02 ,0xff ,0x00 ,0x00 ,0x00,
  235.   0x07 ,0x05 ,0x83 ,0x02 ,0x40 ,0x00 ,0x00,
  236.   0x07 ,0x05 ,0x04 ,0x02 ,0x40 ,0x00 ,0x00
  237. };

  238. const u8 config_high[] =
  239. {
  240.     0x09,    /* 0 desc size */
  241.     0x07,    /* 1 desc type (other speed)*/
  242.     0x20,    /* 2 Total length of data returned */
  243.     0x00,    /* 3 */
  244.     0x01,    /* 4 Number of interfaces supported by this speed configuration */
  245.     0x01,    /* 5 value to use to select configuration */
  246.     0x00,    /* 6 index of string desc */
  247.         /* 7 same as configuration desc */
  248.     CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED,
  249.     0x19,    /* 8 same as configuration desc */

  250. };

  251. const u8 config_high_total[] =
  252. {
  253.   0x09, 0x07 ,0x20 ,0x00 ,0x01 ,0x01 ,0x00 ,0xC0 ,0x19,
  254.   0x09 ,0x04 ,0x00 ,0x00 ,0x02 ,0xff ,0x00 ,0x00 ,0x00,
  255.   0x07 ,0x05 ,0x81 ,0x02 ,0x00 ,0x02 ,0x00,
  256.   0x07 ,0x05 ,0x02 ,0x02 ,0x00 ,0x02 ,0x00
  257. };

  258. /* Descriptor size */
  259. enum DESCRIPTOR_SIZE
  260. {
  261.     DEVICE_DESC_SIZE    = sizeof(device_desc_t),
  262.     STRING_DESC0_SIZE    = sizeof(string_desc0),
  263.     STRING_DESC1_SIZE    = sizeof(string_desc1),
  264.     STRING_DESC2_SIZE    = sizeof(string_desc2),
  265.     CONFIG_DESC_SIZE    = sizeof(config_desc_t),
  266.     INTERFACE_DESC_SIZE    = sizeof(intf_desc_t),
  267.     ENDPOINT_DESC_SIZE    = sizeof(ep_desc_t),
  268.     DEVICE_QUALIFIER_SIZE    = sizeof(qualifier_desc),
  269.     OTHER_SPEED_CFG_SIZE    = 9

  270. };

  271. /*32 <cfg desc>+<if desc>+<endp0 desc>+<endp1 desc>*/
  272. #define CONFIG_DESC_TOTAL_SIZE    \
  273.     (CONFIG_DESC_SIZE+INTERFACE_DESC_SIZE+ENDPOINT_DESC_SIZE*2)
  274. #define TEST_PKT_SIZE 53

  275. u8 test_pkt [TEST_PKT_SIZE] = {
  276.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*JKJKJKJK x 9*/
  277.     0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,    /*JJKKJJKK x 8*/
  278.     0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,    /*JJJJKKKK x 8*/
  279.     0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,    /*JJJJJJJKKKKKKK x8 - '1'*/
  280.     0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,        /*'1' + JJJJJJJK x 8*/
  281.     0xFC,0x7E,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0x7E    /*{JKKKKKKK x 10},JK*/
  282. };

  283. void s3c_usb_init_phy(void)
  284. {
  285.     writel(readl(USB_PHY_CONTROL)|(1<<0), USB_PHY_CONTROL);    /*USB PHY0 Enable */
  286.     
  287.     writel(0xa0, S5P_OTG_PHYPWR);
  288.     writel(0x3, S5P_OTG_PHYCLK);
  289.     writel(0x1, S5P_OTG_RSTCON);
  290.     udelay(10);
  291.     writel(0x0, S5P_OTG_RSTCON);
  292.     udelay(10);

  293. }

  294. /* OTG PHY Power Off */
  295. void s3c_usb_phy_off(void) {
  296.     writel(readl(S5P_OTG_PHYPWR)|(0x18), S5P_OTG_PHYPWR);
  297.     writel(readl(USB_PHY_CONTROL)&~(1<<0), USB_PHY_CONTROL);
  298.     s3c_usb_phy_inited=0;
  299. }

  300. void s3c_usb_core_soft_reset(void)
  301. {
  302.     u32 tmp;

  303.     writel(CORE_SOFT_RESET, S5P_OTG_GRSTCTL);

  304.     do
  305.     {
  306.         tmp = readl(S5P_OTG_GRSTCTL);
  307.     }while(!(tmp & AHB_MASTER_IDLE));

  308. }

  309. void s3c_usb_wait_cable_insert(void)
  310. {
  311.     u32 tmp;
  312.     int ucFirst=1;

  313.     do {
  314.         udelay(50);

  315.         tmp = readl(S5P_OTG_GOTGCTL);

  316.         if (tmp & (B_SESSION_VALID|A_SESSION_VALID)) {
  317.             printf("OTG cable Connected!\n");
  318.             break;
  319.         } else if(ucFirst == 1) {
  320.             printf("Insert a OTG cable into the connector!\n");
  321.             ucFirst = 0;
  322.         }
  323.     } while(1);
  324. }

  325. void s3c_usb_init_core(void)
  326. {
  327.     writel(PTXFE_HALF|NPTXFE_HALF|MODE_SLAVE|BURST_SINGLE|GBL_INT_UNMASK,
  328.         S5P_OTG_GAHBCFG);

  329.     writel( 0<<15        /* PHY Low Power Clock sel */
  330.         |1<<14        /* Non-Periodic TxFIFO Rewind Enable */
  331.         |0x5<<10    /* Turnaround time */
  332.         |0<<9        /* 0:HNP disable, 1:HNP enable */
  333.         |0<<8        /* 0:SRP disable, 1:SRP enable */
  334.         |0<<7        /* ULPI DDR sel */
  335.         |0<<6        /* 0: high speed utmi+, 1: full speed serial */
  336.         |0<<4        /* 0: utmi+, 1:ulpi */
  337.         |1<<3        /* phy i/f 0:8bit, 1:16bit */
  338.         |0x7<<0,    /* HS/FS Timeout**/
  339.         S5P_OTG_GUSBCFG );
  340. }

  341. void s3c_usb_check_current_mode(u8 *pucMode)
  342. {
  343.     u32 tmp;

  344.     tmp = readl(S5P_OTG_GINTSTS);
  345.     *pucMode = tmp & 0x1;
  346. }

  347. void s3c_usb_set_soft_disconnect(void)
  348. {
  349.     u32 tmp;

  350.     tmp = readl(S5P_OTG_DCTL);
  351.     tmp |= SOFT_DISCONNECT;
  352.     writel(tmp, S5P_OTG_DCTL);
  353. }

  354. void s3c_usb_clear_soft_disconnect(void)
  355. {
  356.     u32 tmp;

  357.     tmp = readl(S5P_OTG_DCTL);
  358.     tmp &= ~SOFT_DISCONNECT;
  359.     writel(tmp, S5P_OTG_DCTL);
  360. }

  361. void s3c_usb_init_device(void)
  362. {
  363.     writel(1<<18|otg.speed<<0, S5P_OTG_DCFG); /* [][1: full speed(30Mhz) 0:high speed]*/

  364.     writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|
  365.         INT_RESET|INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY,
  366.         S5P_OTG_GINTMSK);    /*gint unmask */
  367.     s3c_usb_phy_inited=1;
  368. }

  369. int s3c_usbctl_init(int ischeckstatus)/*diog, if not for check status, donot need wait cable insert*/
  370. {
  371.     u8 ucMode;

  372.     DBG_SETUP0("USB Control Init\n");
  373.     
  374.     otg.speed = speed;
  375.     otg.set_config = 0;
  376.     otg.ep0_state = EP0_STATE_INIT;
  377.     otg.ep0_substate = 0;
  378.     s3c_usb_init_phy();
  379.     s3c_usb_core_soft_reset();
  380.     if(!ischeckstatus)
  381.     s3c_usb_wait_cable_insert();
  382.     s3c_usb_init_core();
  383.     s3c_usb_check_current_mode(&ucMode);

  384.     if (ucMode == INT_DEV_MODE) {
  385.         s3c_usb_set_soft_disconnect();
  386.         udelay(10);
  387.         s3c_usb_clear_soft_disconnect();
  388.         s3c_usb_init_device();
  389.         return 0;
  390.     } else {
  391.         printf("Error : Current Mode is Host\n");
  392.         return 0;
  393.     }
  394. }

  395. int s3c_usbc_activate (void)
  396. {
  397.     /* dont used in usb high speed, but used in common file cmd_usbd.c */
  398.     return 0;
  399. }

  400. int s3c_usb_stop (void)
  401. {
  402.     /* dont used in usb high speed, but used in common file cmd_usbd.c */
  403.     s3c_usb_core_soft_reset();
  404.     s3c_usb_phy_off();
  405.     return 0;
  406. }

  407. void s3c_usb_print_pkt(u8 *pt, u8 count)
  408. {
  409.     int i;
  410.     printf("[s3c_usb_print_pkt:");

  411.     for(i=0;i<count;i++)
  412.         printf("%x,", pt[i]);

  413.     printf("]\n");
  414. }

  415. void s3c_usb_verify_checksum(void)
  416. {
  417.     u8 *cs_start, *cs_end;
  418.     u16 dnCS;
  419.     u16 checkSum;

  420.     printf("Checksum is being calculated.");

  421.     /* checksum calculation */
  422.     cs_start = (u8*)otg.dn_addr;
  423.     cs_end = (u8*)(otg.dn_addr+otg.dn_filesize-10);
  424.     checkSum = 0;
  425.     while(cs_start < cs_end) {
  426.         checkSum += *cs_start++;
  427.         if(((u32)cs_start&0xfffff)==0) printf(".");
  428.     }

  429.     /* fixed alignment fault in case when cs_end is odd. */
  430.     dnCS = (u16)((cs_end[1]<<8) + cs_end[0]);
  431.     //dnCS = *(u16 *)cs_end;

  432.     if (checkSum == dnCS)
  433.     {
  434.         printf("\nChecksum O.K.\n");
  435.     }
  436.     else
  437.     {
  438.         printf("\nChecksum Value => MEM:%x DNW:%x\n",checkSum,dnCS);
  439.         printf("Checksum failed.\n\n");
  440.     }

  441. }

  442. void s3c_usb_set_inep_xfersize(EP_TYPE type, u32 pktcnt, u32 xfersize)
  443. {
  444.     if(type == EP_TYPE_CONTROL)
  445.     {
  446.         writel((pktcnt<<19)|(xfersize<<0), S5P_OTG_DIEPTSIZ0);
  447.     }
  448.     else if(type == EP_TYPE_BULK)
  449.     {
  450.         writel((1<<29)|(pktcnt<<19)|(xfersize<<0), S5P_OTG_DIEPTSIZ_IN);
  451.     }
  452. }

  453. void s3c_usb_set_outep_xfersize(EP_TYPE type, u32 pktcnt, u32 xfersize)
  454. {
  455.     if(type == EP_TYPE_CONTROL)
  456.     {
  457.         writel((1<<29)|(pktcnt<<19)|(xfersize<<0), S5P_OTG_DOEPTSIZ0);
  458.     }
  459.     else if(type == EP_TYPE_BULK)
  460.     {
  461.         writel((pktcnt<<19)|(xfersize<<0), S5P_OTG_DOEPTSIZ_OUT);
  462.     }
  463. }

  464. void s3c_usb_write_ep0_fifo(u8 *buf, int num)
  465. {
  466.     int i;
  467.     u32 Wr_Data=0;

  468.     DBG_SETUP1("[s3c_usb_write_ep0_fifo:");

  469.     for(i=0;i<num;i+=4)
  470.     {
  471.         Wr_Data = ((*(buf+3))<<24)|((*(buf+2))<<16)|((*(buf+1))<<8)|*buf;
  472.         DBG_SETUP2(" 0x%08x,", Wr_Data);
  473.         writel(Wr_Data, S5P_OTG_EP0_FIFO);
  474.         buf += 4;
  475.     }

  476.     DBG_SETUP2("]\n");
  477. }


  478. void s3c_usb_write_in_fifo(u8 *buf, int num)
  479. {
  480.     int i;
  481.     u32 data=0;

  482.     for(i=0;i<num;i+=4)
  483.     {
  484.         data=((*(buf+3))<<24)|((*(buf+2))<<16)|((*(buf+1))<<8)|*buf;
  485.         writel(data, S5P_OTG_IN_FIFO);
  486.         buf += 4;
  487.     }
  488. }

  489. void s3c_usb_read_out_fifo(u8 *buf, int num)
  490. {
  491.     int i;
  492.     u32 data;

  493.     for (i=0;i<num;i+=4)
  494.     {
  495.         data = readl(S5P_OTG_OUT_FIFO);

  496.         buf[i] = (u8)data;
  497.         buf[i+1] = (u8)(data>>8);
  498.         buf[i+2] = (u8)(data>>16);
  499.         buf[i+3] = (u8)(data>>24);
  500.     }
  501. }

  502. void s3c_usb_get_desc(void)
  503. {
  504.     switch (otg.dev_req.wValue_H) {
  505.     case DEVICE_DESCRIPTOR:
  506.         otg.req_length = (u32)((otg.dev_req.wLength_H << 8) |
  507.             otg.dev_req.wLength_L);
  508.         DBG_SETUP1("DEVICE_DESCRIPTOR = 0x%x \n",otg.req_length);
  509.         otg.ep0_state = EP0_STATE_GD_DEV_0;
  510.         break;

  511.     case CONFIGURATION_DESCRIPTOR:
  512.         otg.req_length = (u32)((otg.dev_req.wLength_H << 8) |
  513.             otg.dev_req.wLength_L);
  514.         DBG_SETUP1("CONFIGURATION_DESCRIPTOR = 0x%x \n",otg.req_length);

  515.         /* GET_DESCRIPTOR:CONFIGURATION+INTERFACE+ENDPOINT0+ENDPOINT1 */
  516.         if (otg.req_length > CONFIG_DESC_SIZE){
  517.             otg.ep0_state = EP0_STATE_GD_CFG_0;
  518.         } else
  519.             otg.ep0_state = EP0_STATE_GD_CFG_ONLY_0;
  520.         break;

  521.     case STRING_DESCRIPTOR :
  522.         DBG_SETUP1("STRING_DESCRIPTOR \n");

  523.         switch(otg.dev_req.wValue_L) {
  524.         case 0:
  525.             otg.ep0_state = EP0_STATE_GD_STR_I0;
  526.             break;
  527.         case 1:
  528.             otg.ep0_state = EP0_STATE_GD_STR_I1;
  529.             break;
  530.         case 2:
  531.             otg.ep0_state = EP0_STATE_GD_STR_I2;
  532.             break;
  533.         default:
  534.             break;
  535.         }
  536.         break;

  537.     case ENDPOINT_DESCRIPTOR:
  538.         DBG_SETUP1("ENDPOINT_DESCRIPTOR \n");
  539.         switch(otg.dev_req.wValue_L&0xf) {
  540.         case 0:
  541.             otg.ep0_state=EP0_STATE_GD_EP0_ONLY_0;
  542.             break;
  543.         case 1:
  544.             otg.ep0_state=EP0_STATE_GD_EP1_ONLY_0;
  545.             break;
  546.         default:
  547.             break;
  548.         }
  549.         break;

  550.     case DEVICE_QUALIFIER:
  551.         otg.req_length = (u32)((otg.dev_req.wLength_H << 8) |
  552.             otg.dev_req.wLength_L);
  553.         DBG_SETUP1("DEVICE_QUALIFIER = 0x%x \n",otg.req_length);
  554.         otg.ep0_state = EP0_STATE_GD_DEV_QUALIFIER;
  555.         break;

  556.     case OTHER_SPEED_CONFIGURATION :
  557.         DBG_SETUP1("OTHER_SPEED_CONFIGURATION \n");
  558.         otg.req_length = (u32)((otg.dev_req.wLength_H << 8) |
  559.             otg.dev_req.wLength_L);
  560.         otg.ep0_state = EP0_STATE_GD_OTHER_SPEED;
  561.         break;

  562.     }
  563. }

  564. void s3c_usb_clear_feature(void)
  565. {
  566.     switch (otg.dev_req.bmRequestType) {
  567.     case DEVICE_RECIPIENT:
  568.         DBG_SETUP1("DEVICE_RECIPIENT \n");
  569.         if (otg.dev_req.wValue_L == 1)
  570.             remode_wakeup = FALSE;
  571.         break;

  572.     case ENDPOINT_RECIPIENT:
  573.         DBG_SETUP1("ENDPOINT_RECIPIENT \n");
  574.         if (otg.dev_req.wValue_L == 0) {
  575.             if ((otg.dev_req.wIndex_L & 0x7f) == CONTROL_EP)
  576.                 get_status.ep_ctrl= 0;

  577.             /* IN    Endpoint */
  578.             if ((otg.dev_req.wIndex_L & 0x7f) == BULK_IN_EP)
  579.                 get_status.ep_in= 0;

  580.             /* OUT Endpoint */
  581.             if ((otg.dev_req.wIndex_L & 0x7f) == BULK_OUT_EP)
  582.                 get_status.ep_out= 0;
  583.         }
  584.         break;

  585.     default:
  586.         DBG_SETUP1("\n");
  587.         break;
  588.     }
  589.     otg.ep0_state = EP0_STATE_INIT;

  590. }

  591. void s3c_usb_set_feature(void)
  592. {
  593.     u32 tmp;

  594.     switch (otg.dev_req.bmRequestType) {
  595.     case DEVICE_RECIPIENT:
  596.         DBG_SETUP1("DEVICE_RECIPIENT \n");
  597.         if (otg.dev_req.wValue_L == 1)
  598.             remode_wakeup = TRUE;
  599.             break;

  600.     case ENDPOINT_RECIPIENT:
  601.         DBG_SETUP1("ENDPOINT_RECIPIENT \n");
  602.         if (otg.dev_req.wValue_L == 0) {
  603.             if ((otg.dev_req.wIndex_L & 0x7f) == CONTROL_EP)
  604.                 get_status.ep_ctrl= 1;

  605.             if ((otg.dev_req.wIndex_L & 0x7f) == BULK_IN_EP)
  606.                 get_status.ep_in= 1;

  607.             if ((otg.dev_req.wIndex_L & 0x7f) == BULK_OUT_EP)
  608.                 get_status.ep_out= 1;
  609.         }
  610.         break;

  611.     default:
  612.         DBG_SETUP1("\n");
  613.         break;
  614.     }

  615.     switch (otg.dev_req.wValue_L) {
  616.     case EP_STALL:
  617.         /* TBD: additional processing if required */
  618.         break;

  619.     case TEST_MODE:
  620.         if ((0 != otg.dev_req.wIndex_L ) ||(0 != otg.dev_req.bmRequestType))
  621.             break;

  622.         /* Set TEST MODE*/
  623.         tmp = readl(S5P_OTG_DCTL);
  624.         tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_FORCE_ENABLE_MODE);
  625.         writel(tmp, S5P_OTG_DCTL);

  626.         switch(otg.dev_req.wIndex_H) {
  627.         case TEST_J:
  628.             /*Set Test J*/
  629.             tmp = readl(S5P_OTG_DCTL);
  630.             tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_J_MODE);
  631.             writel(tmp, S5P_OTG_DCTL);
  632.             break;

  633.         case TEST_K:
  634.             /*Set Test K*/
  635.             tmp = readl(S5P_OTG_DCTL);
  636.             tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_K_MODE);
  637.             writel(tmp, S5P_OTG_DCTL);
  638.             break;

  639.         case TEST_SE0_NAK:
  640.             /*Set Test SE0NAK*/
  641.             tmp = readl(S5P_OTG_DCTL);
  642.             tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_SE0_NAK_MODE);
  643.             writel(tmp, S5P_OTG_DCTL);
  644.             break;

  645.         case TEST_PACKET:
  646.             DBG_SETUP1 ("Test_packet\n");
  647.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  648.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, TEST_PKT_SIZE);
  649.             s3c_usb_write_ep0_fifo(test_pkt, TEST_PKT_SIZE);
  650.             tmp = readl(S5P_OTG_DCTL);
  651.             tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_PACKET_MODE);
  652.             writel(tmp, S5P_OTG_DCTL);
  653.             DBG_SETUP1 ("S5P_OTG_DCTL=0x%08x\n", tmp);
  654.             break;
  655.         }
  656.         break;

  657.     default:
  658.         break;
  659.     }
  660.     otg.ep0_state = EP0_STATE_INIT;

  661. }

  662. void s3c_usb_get_status(void)
  663. {
  664.     switch(otg.dev_req.bmRequestType) {
  665.     case (0x80):    /*device */
  666.         DBG_SETUP1("DEVICE\n");
  667.         get_status.Device=((u8)remode_wakeup<<1)|0x1; /* SelfPowered */
  668.         otg.ep0_state = EP0_STATE_GET_STATUS0;
  669.         break;

  670.     case (0x81):    /*interface */
  671.         DBG_SETUP1("INTERFACE\n");
  672.         get_status.Interface=0;
  673.         otg.ep0_state = EP0_STATE_GET_STATUS1;
  674.         break;

  675.     case (0x82):    /*endpoint */
  676.         DBG_SETUP1("ENDPOINT\n");
  677.         if ((otg.dev_req.wIndex_L & 0x7f) == CONTROL_EP)
  678.             otg.ep0_state = EP0_STATE_GET_STATUS2;

  679.         if ((otg.dev_req.wIndex_L & 0x7f) == BULK_IN_EP)
  680.             otg.ep0_state = EP0_STATE_GET_STATUS3;

  681.         if ((otg.dev_req.wIndex_L & 0x7f) == BULK_OUT_EP)
  682.             otg.ep0_state = EP0_STATE_GET_STATUS4;
  683.         break;

  684.     default:
  685.         DBG_SETUP1("\n");
  686.         break;
  687.     }

  688. }

  689. void s3c_usb_ep0_int_hndlr(void)
  690. {
  691.     u16 i;
  692.     u32 buf[2]={0x0000, };
  693.     u16 addr;

  694.     DBG_SETUP0("Event EP0\n");

  695.     if (otg.ep0_state == EP0_STATE_INIT) {

  696.         for(i=0;i<2;i++)
  697.             buf[i] = readl(S5P_OTG_EP0_FIFO);

  698.         otg.dev_req.bmRequestType = buf[0];
  699.         otg.dev_req.bRequest    = buf[0]>>8;
  700.         otg.dev_req.wValue_L    = buf[0]>>16;
  701.         otg.dev_req.wValue_H    = buf[0]>>24;
  702.         otg.dev_req.wIndex_L    = buf[1];
  703.         otg.dev_req.wIndex_H    = buf[1]>>8;
  704.         otg.dev_req.wLength_L    = buf[1]>>16;
  705.         otg.dev_req.wLength_H    = buf[1]>>24;

  706. #ifdef USB_OTG_DEBUG_SETUP
  707.         s3c_usb_print_pkt((u8 *)&otg.dev_req, 8);
  708. #endif

  709.         switch (otg.dev_req.bRequest) {
  710.         case STANDARD_SET_ADDRESS:
  711.             /* Set Address Update bit */
  712.             addr = (otg.dev_req.wValue_L);
  713.             writel(1<<18|addr<<4|otg.speed<<0, S5P_OTG_DCFG);
  714.             DBG_SETUP1("S5P_OTG_DCFG : %x, STANDARD_SET_ADDRESS : %d\n",
  715.                     readl(S5P_OTG_DCFG), addr);
  716.             otg.ep0_state = EP0_STATE_INIT;
  717.             break;

  718.         case STANDARD_SET_DESCRIPTOR:
  719.             DBG_SETUP1("STANDARD_SET_DESCRIPTOR \n");
  720.             break;

  721.         case STANDARD_SET_CONFIGURATION:
  722.             DBG_SETUP1("STANDARD_SET_CONFIGURATION \n");
  723.             /* Configuration value in configuration descriptor */
  724.             config_value = otg.dev_req.wValue_L;
  725.             otg.set_config = 1;
  726.             otg.ep0_state = EP0_STATE_INIT;
  727.             break;

  728.         case STANDARD_GET_CONFIGURATION:
  729.             DBG_SETUP1("STANDARD_GET_CONFIGURATION \n");
  730.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);

  731.             /*ep0 enable, clear nak, next ep0, 8byte */
  732.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  733.             writel(config_value, S5P_OTG_EP0_FIFO);
  734.             otg.ep0_state = EP0_STATE_INIT;
  735.             break;

  736.         case STANDARD_GET_DESCRIPTOR:
  737.             DBG_SETUP1("STANDARD_GET_DESCRIPTOR :");
  738.             s3c_usb_get_desc();
  739.             break;

  740.         case STANDARD_CLEAR_FEATURE:
  741.             DBG_SETUP1("STANDARD_CLEAR_FEATURE :");
  742.             s3c_usb_clear_feature();
  743.             break;

  744.         case STANDARD_SET_FEATURE:
  745.             DBG_SETUP1("STANDARD_SET_FEATURE :");
  746.             s3c_usb_set_feature();
  747.             break;

  748.         case STANDARD_GET_STATUS:
  749.             DBG_SETUP1("STANDARD_GET_STATUS :");
  750.             s3c_usb_get_status();
  751.             break;

  752.         case STANDARD_GET_INTERFACE:
  753.             DBG_SETUP1("STANDARD_GET_INTERFACE \n");
  754.             otg.ep0_state = EP0_STATE_INTERFACE_GET;
  755.             break;

  756.         case STANDARD_SET_INTERFACE:
  757.             DBG_SETUP1("STANDARD_SET_INTERFACE \n");
  758.             get_intf.AlternateSetting= otg.dev_req.wValue_L;
  759.             otg.ep0_state = EP0_STATE_INIT;
  760.             break;

  761.         case STANDARD_SYNCH_FRAME:
  762.             DBG_SETUP1("STANDARD_SYNCH_FRAME \n");
  763.             otg.ep0_state = EP0_STATE_INIT;
  764.             break;

  765.         default:
  766.             break;
  767.         }
  768.     }

  769.     s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.ctrl_max_pktsize);

  770.     if(otg.speed == USB_HIGH) {
  771.         /*clear nak, next ep0, 64byte */
  772.         writel(((1<<26)|(CONTROL_EP<<11)|(0<<0)), S5P_OTG_DIEPCTL0);
  773.     }
  774.     else {
  775.         /*clear nak, next ep0, 8byte */
  776.         writel(((1<<26)|(CONTROL_EP<<11)|(3<<0)), S5P_OTG_DIEPCTL0);
  777.     }

  778. }

  779. void s3c_usb_set_otherspeed_conf_desc(u32 length)
  780. {
  781.     /* Standard device descriptor */
  782.     if (otg.speed == USB_HIGH)
  783.     {
  784.      if (length ==9)
  785.      {
  786.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 9);
  787.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  788.             s3c_usb_write_ep0_fifo(((u8 *)&config_full)+0, 9);
  789.         }
  790.      else if(length ==32)
  791.         {
  792.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 32);
  793.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  794.             s3c_usb_write_ep0_fifo(((u8 *)&config_full_total)+0, 32);

  795.      }
  796.         otg.ep0_state = EP0_STATE_INIT;
  797.     }
  798.     else
  799.     {
  800.      if (length ==9)
  801.      {
  802.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  803.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  804.             s3c_usb_write_ep0_fifo(((u8 *)&config_high)+0, 8);
  805.         }
  806.      else if(length ==32)
  807.         {
  808.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  809.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  810.             s3c_usb_write_ep0_fifo(((u8 *)&config_high_total)+0, 8);
  811.      }
  812.         otg.ep0_state = EP0_STATE_GD_OTHER_SPEED_HIGH_1;
  813.     }
  814. }

  815. void s3c_usb_transfer_ep0(void)
  816. {

  817.     DBG_SETUP0("otg.ep0_state = %d\n", otg.ep0_state);

  818.     switch (otg.ep0_state) {
  819.     case EP0_STATE_INIT:
  820.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 0);

  821.         /*ep0 enable, clear nak, next ep0, 8byte */
  822.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  823.         DBG_SETUP1("EP0_STATE_INIT\n");
  824.         break;

  825.     /* GET_DESCRIPTOR:DEVICE */
  826.     case EP0_STATE_GD_DEV_0:
  827.         DBG_SETUP1("EP0_STATE_GD_DEV_0 :");
  828.         if (otg.speed == USB_HIGH) {
  829.             DBG_SETUP1("High Speed\n");

  830.             /*ep0 enable, clear nak, next ep0, max 64byte */
  831.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  832.             if (otg.req_length < DEVICE_DESC_SIZE) {
  833.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  834.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+0, otg.req_length);
  835.             } else {
  836.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, DEVICE_DESC_SIZE);
  837.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+0, DEVICE_DESC_SIZE);
  838.             }
  839.             otg.ep0_state = EP0_STATE_INIT;
  840.         } else {
  841.             DBG_SETUP1("Full Speed\n");
  842.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  843.             if(otg.req_length<DEVICE_DESC_SIZE) {
  844.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  845.             } else {
  846.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, DEVICE_DESC_SIZE);
  847.             }

  848.             if(otg.req_length<FS_CTRL_PKT_SIZE) {
  849.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+0, otg.req_length);
  850.                 otg.ep0_state = EP0_STATE_INIT;
  851.             } else {
  852.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+0, FS_CTRL_PKT_SIZE);
  853.                 otg.ep0_state = EP0_STATE_GD_DEV_1;
  854.             }
  855.         }
  856.         break;

  857.     case EP0_STATE_GD_DEV_1:
  858.         DBG_SETUP1("EP0_STATE_GD_DEV_1\n");
  859.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  860.         if(otg.req_length<(2*FS_CTRL_PKT_SIZE)) {
  861.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+FS_CTRL_PKT_SIZE,
  862.                         (otg.req_length-FS_CTRL_PKT_SIZE));
  863.             otg.ep0_state = EP0_STATE_INIT;
  864.         } else {
  865.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+FS_CTRL_PKT_SIZE,
  866.                         FS_CTRL_PKT_SIZE);
  867.             otg.ep0_state = EP0_STATE_GD_DEV_2;
  868.         }
  869.         break;

  870.     case EP0_STATE_GD_DEV_2:
  871.         DBG_SETUP1("EP0_STATE_GD_DEV_2\n");
  872.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  873.         if(otg.req_length<DEVICE_DESC_SIZE) {
  874.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+(2*FS_CTRL_PKT_SIZE),
  875.                         (otg.req_length-2*FS_CTRL_PKT_SIZE));
  876.         } else {
  877.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+(2*FS_CTRL_PKT_SIZE),
  878.                         (DEVICE_DESC_SIZE-2*FS_CTRL_PKT_SIZE));
  879.         }
  880.         otg.ep0_state = EP0_STATE_INIT;
  881.         break;

  882.     /* GET_DESCRIPTOR:CONFIGURATION+INTERFACE+ENDPOINT0+ENDPOINT1 */
  883.     case EP0_STATE_GD_CFG_0:
  884.         DBG_SETUP1("EP0_STATE_GD_CFG_0 :");
  885.         if (otg.speed == USB_HIGH)
  886.         {
  887.             DBG_SETUP1("High Speed\n");
  888.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  889.             if(otg.req_length<CONFIG_DESC_TOTAL_SIZE)
  890.             {
  891.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  892.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, otg.req_length);
  893.             }
  894.             else
  895.             {
  896.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, CONFIG_DESC_TOTAL_SIZE);
  897.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, CONFIG_DESC_TOTAL_SIZE);
  898.             }
  899.             otg.ep0_state = EP0_STATE_INIT;
  900.         }
  901.         else
  902.         {
  903.             DBG_SETUP1("Full Speed\n");
  904.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  905.             if(otg.req_length<CONFIG_DESC_TOTAL_SIZE)
  906.             {
  907.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  908.             }
  909.             else
  910.             {
  911.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, CONFIG_DESC_TOTAL_SIZE);
  912.             }
  913.             if(otg.req_length<FS_CTRL_PKT_SIZE)
  914.             {
  915.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, otg.req_length);
  916.                 otg.ep0_state = EP0_STATE_INIT;
  917.             }
  918.             else
  919.             {
  920.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, FS_CTRL_PKT_SIZE);
  921.                 otg.ep0_state = EP0_STATE_GD_CFG_1;
  922.             }
  923.         }
  924.         break;

  925.     case EP0_STATE_GD_CFG_1:
  926.         DBG_SETUP1("EP0_STATE_GD_CFG_1\n");
  927.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  928.         if(otg.req_length<(2*FS_CTRL_PKT_SIZE))
  929.         {
  930.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+FS_CTRL_PKT_SIZE,
  931.                         (otg.req_length-FS_CTRL_PKT_SIZE));
  932.             otg.ep0_state = EP0_STATE_INIT;
  933.         }
  934.         else
  935.         {
  936.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+FS_CTRL_PKT_SIZE,
  937.                         FS_CTRL_PKT_SIZE);
  938.             otg.ep0_state = EP0_STATE_GD_CFG_2;
  939.         }
  940.         break;

  941.     case EP0_STATE_GD_CFG_2:
  942.         DBG_SETUP1("EP0_STATE_GD_CFG_2\n");
  943.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  944.         if(otg.req_length<(3*FS_CTRL_PKT_SIZE))
  945.         {
  946.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+(2*FS_CTRL_PKT_SIZE),
  947.                         (otg.req_length-2*FS_CTRL_PKT_SIZE));
  948.             otg.ep0_state = EP0_STATE_INIT;
  949.         }
  950.         else
  951.         {
  952.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+(2*FS_CTRL_PKT_SIZE),
  953.                         FS_CTRL_PKT_SIZE);
  954.             otg.ep0_state = EP0_STATE_GD_CFG_3;
  955.         }
  956.         break;

  957.     case EP0_STATE_GD_CFG_3:
  958.         DBG_SETUP1("EP0_STATE_GD_CFG_3\n");
  959.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  960.         if(otg.req_length<(4*FS_CTRL_PKT_SIZE))
  961.         {
  962.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+(3*FS_CTRL_PKT_SIZE),
  963.                         (otg.req_length-3*FS_CTRL_PKT_SIZE));
  964.             otg.ep0_state = EP0_STATE_INIT;
  965.         }
  966.         else
  967.         {
  968.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+(3*FS_CTRL_PKT_SIZE),
  969.                         FS_CTRL_PKT_SIZE);
  970.             otg.ep0_state = EP0_STATE_GD_CFG_4;
  971.         }
  972.         break;

  973.     case EP0_STATE_GD_CFG_4:
  974.         DBG_SETUP1("EP0_STATE_GD_CFG_4\n");
  975.         otg.ep0_state = EP0_STATE_INIT;
  976.         break;

  977.     case EP0_STATE_GD_DEV_QUALIFIER:    /*only supported in USB 2.0*/
  978.         DBG_SETUP1("EP0_STATE_GD_DEV_QUALIFIER\n");
  979.         writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  980.         if(otg.req_length<10)
  981.         {
  982.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  983.             s3c_usb_write_ep0_fifo((u8 *)qualifier_desc+0, otg.req_length);
  984.         }
  985.         else
  986.         {
  987.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 10);
  988.             s3c_usb_write_ep0_fifo((u8 *)qualifier_desc+0, 10);
  989.         }
  990.         otg.ep0_state = EP0_STATE_INIT;
  991.         break;

  992.     case EP0_STATE_GD_OTHER_SPEED:
  993.             DBG_SETUP1("EP0_STATE_GD_OTHER_SPEED\n");
  994.             s3c_usb_set_otherspeed_conf_desc(otg.req_length);
  995.             break;

  996.     case EP0_STATE_GD_OTHER_SPEED_HIGH_1:
  997.         DBG_SETUP1("EP0_STATE_GD_OTHER_SPEED_HIGH_1\n");
  998.         if(otg.req_length==9)
  999.         {
  1000.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1001.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1002.                 s3c_usb_write_ep0_fifo(((u8 *)&config_high)+8, 1);
  1003.                 otg.ep0_state = EP0_STATE_INIT;
  1004.         }
  1005.         else
  1006.         {
  1007.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  1008.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1009.                 s3c_usb_write_ep0_fifo(((u8 *)&config_high)+8, 8);
  1010.                 otg.ep0_state = EP0_STATE_GD_OTHER_SPEED_HIGH_2;
  1011.         }
  1012.             break;

  1013.     case EP0_STATE_GD_OTHER_SPEED_HIGH_2:
  1014.         DBG_SETUP1("EP0_STATE_GD_OTHER_SPEED_HIGH_2\n");
  1015.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  1016.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1017.             s3c_usb_write_ep0_fifo(((u8 *)&config_high)+16, 8);
  1018.             otg.ep0_state = EP0_STATE_GD_OTHER_SPEED_HIGH_3;
  1019.             break;

  1020.     case EP0_STATE_GD_OTHER_SPEED_HIGH_3:
  1021.         DBG_SETUP1("EP0_STATE_GD_OTHER_SPEED_HIGH_3\n");
  1022.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  1023.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1024.             s3c_usb_write_ep0_fifo(((u8 *)&config_high)+24, 8);
  1025.             otg.ep0_state = EP0_STATE_INIT;
  1026.             break;

  1027.     /* GET_DESCRIPTOR:CONFIGURATION ONLY*/
  1028.     case EP0_STATE_GD_CFG_ONLY_0:
  1029.         DBG_SETUP1("EP0_STATE_GD_CFG_ONLY_0:");
  1030.         if (otg.speed == USB_HIGH)
  1031.         {
  1032.             DBG_SETUP1("High Speed\n");
  1033.             if(otg.req_length<CONFIG_DESC_SIZE)
  1034.             {
  1035.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  1036.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1037.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, otg.req_length);
  1038.             }
  1039.             else
  1040.             {
  1041.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, CONFIG_DESC_SIZE);
  1042.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1043.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0,
  1044.                             CONFIG_DESC_SIZE);
  1045.             }
  1046.             otg.ep0_state = EP0_STATE_INIT;
  1047.         }
  1048.         else
  1049.         {
  1050.             DBG_SETUP1("Full Speed\n");
  1051.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1052.             if(otg.req_length<CONFIG_DESC_SIZE)
  1053.             {
  1054.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  1055.             }
  1056.             else
  1057.             {
  1058.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, CONFIG_DESC_SIZE);
  1059.             }
  1060.             if(otg.req_length<FS_CTRL_PKT_SIZE)
  1061.             {
  1062.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, otg.req_length);
  1063.                 otg.ep0_state = EP0_STATE_INIT;
  1064.             }
  1065.             else
  1066.             {
  1067.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, FS_CTRL_PKT_SIZE);
  1068.                 otg.ep0_state = EP0_STATE_GD_CFG_ONLY_1;
  1069.             }
  1070.         }
  1071.         break;

  1072.     case EP0_STATE_GD_CFG_ONLY_1:
  1073.         DBG_SETUP1("EP0_STATE_GD_CFG_ONLY_1\n");
  1074.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1075.         s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+FS_CTRL_PKT_SIZE,
  1076.                     (CONFIG_DESC_SIZE-FS_CTRL_PKT_SIZE));
  1077.         otg.ep0_state = EP0_STATE_INIT;
  1078.         break;

  1079.     /* GET_DESCRIPTOR:INTERFACE ONLY */

  1080.     case EP0_STATE_GD_IF_ONLY_0:
  1081.         DBG_SETUP1("EP0_STATE_GD_IF_ONLY_0 :");
  1082.         if (otg.speed == USB_HIGH)
  1083.         {
  1084.             DBG_SETUP1("High Speed\n");
  1085.             if(otg.req_length<INTERFACE_DESC_SIZE)
  1086.             {
  1087.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  1088.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1089.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+0, otg.req_length);
  1090.             }
  1091.             else
  1092.             {
  1093.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, INTERFACE_DESC_SIZE);
  1094.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1095.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+0, INTERFACE_DESC_SIZE);
  1096.             }
  1097.             otg.ep0_state = EP0_STATE_INIT;
  1098.         }
  1099.         else
  1100.         {
  1101.             DBG_SETUP1("Full Speed\n");
  1102.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1103.             if(otg.req_length<INTERFACE_DESC_SIZE)
  1104.             {
  1105.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  1106.             }
  1107.             else
  1108.             {
  1109.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, INTERFACE_DESC_SIZE);
  1110.             }
  1111.             if(otg.req_length<FS_CTRL_PKT_SIZE)
  1112.             {
  1113.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+0, otg.req_length);
  1114.                 otg.ep0_state = EP0_STATE_INIT;
  1115.             }
  1116.             else
  1117.             {
  1118.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+0, FS_CTRL_PKT_SIZE);
  1119.                 otg.ep0_state = EP0_STATE_GD_IF_ONLY_1;
  1120.             }
  1121.         }
  1122.         break;

  1123.     case EP0_STATE_GD_IF_ONLY_1:
  1124.         DBG_SETUP1("EP0_STATE_GD_IF_ONLY_1\n");
  1125.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1126.         s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+FS_CTRL_PKT_SIZE,
  1127.                     (INTERFACE_DESC_SIZE-FS_CTRL_PKT_SIZE));
  1128.         otg.ep0_state = EP0_STATE_INIT;
  1129.         break;


  1130.     /* GET_DESCRIPTOR:ENDPOINT 1 ONLY */
  1131.     case EP0_STATE_GD_EP0_ONLY_0:
  1132.         DBG_SETUP1("EP0_STATE_GD_EP0_ONLY_0\n");
  1133.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1134.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, ENDPOINT_DESC_SIZE);
  1135.         s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.ep1))+0, ENDPOINT_DESC_SIZE);
  1136.         otg.ep0_state = EP0_STATE_INIT;
  1137.         break;

  1138.     /* GET_DESCRIPTOR:ENDPOINT 2 ONLY */
  1139.     case EP0_STATE_GD_EP1_ONLY_0:
  1140.         DBG_SETUP1("EP0_STATE_GD_EP1_ONLY_0\n");
  1141.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, ENDPOINT_DESC_SIZE);
  1142.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1143.         s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.ep2))+0, ENDPOINT_DESC_SIZE);
  1144.         otg.ep0_state = EP0_STATE_INIT;
  1145.         break;

  1146.     /* GET_DESCRIPTOR:STRING */
  1147.     case EP0_STATE_GD_STR_I0:
  1148.         DBG_SETUP1("EP0_STATE_GD_STR_I0\n");
  1149.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, STRING_DESC0_SIZE);
  1150.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1151.         s3c_usb_write_ep0_fifo((u8 *)string_desc0, STRING_DESC0_SIZE);
  1152.         otg.ep0_state = EP0_STATE_INIT;
  1153.         break;

  1154.     case EP0_STATE_GD_STR_I1:
  1155.         DBG_SETUP1("EP0_STATE_GD_STR_I1 %d\n", otg.ep0_substate);
  1156.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, sizeof(string_desc1));
  1157.         if ((otg.ep0_substate*otg.ctrl_max_pktsize+otg.ctrl_max_pktsize)
  1158.             < sizeof(string_desc1)) {

  1159.             if (otg.speed == USB_HIGH)
  1160.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1161.             else
  1162.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1163.             s3c_usb_write_ep0_fifo((u8 *)string_desc1+(otg.ep0_substate*otg.ctrl_max_pktsize),
  1164.                         otg.ctrl_max_pktsize);
  1165.             otg.ep0_state = EP0_STATE_GD_STR_I1;
  1166.             otg.ep0_substate++;
  1167.         } else {
  1168.             if (otg.speed == USB_HIGH)
  1169.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1170.             else
  1171.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1172.             s3c_usb_write_ep0_fifo((u8 *)string_desc1+(otg.ep0_substate*otg.ctrl_max_pktsize),
  1173.                         sizeof(string_desc1)-(otg.ep0_substate*otg.ctrl_max_pktsize));
  1174.             otg.ep0_state = EP0_STATE_INIT;
  1175.             otg.ep0_substate = 0;
  1176.         }
  1177.         break;

  1178.     case EP0_STATE_GD_STR_I2:
  1179.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, sizeof(string_desc2));
  1180.         if ((otg.ep0_substate*otg.ctrl_max_pktsize+otg.ctrl_max_pktsize)
  1181.             < sizeof(string_desc2)) {

  1182.             if (otg.speed == USB_HIGH)
  1183.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1184.             else
  1185.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1186.             s3c_usb_write_ep0_fifo((u8 *)string_desc2+(otg.ep0_substate*otg.ctrl_max_pktsize),
  1187.                         otg.ctrl_max_pktsize);
  1188.             otg.ep0_state = EP0_STATE_GD_STR_I2;
  1189.             otg.ep0_substate++;
  1190.         } else {
  1191.             if (otg.speed == USB_HIGH)
  1192.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1193.             else
  1194.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1195.             s3c_usb_write_ep0_fifo((u8 *)string_desc2+(otg.ep0_substate*otg.ctrl_max_pktsize),
  1196.                         sizeof(string_desc2)-(otg.ep0_substate*otg.ctrl_max_pktsize));
  1197.             otg.ep0_state = EP0_STATE_INIT;
  1198.             otg.ep0_substate = 0;
  1199.         }
  1200.         DBG_SETUP1("EP0_STATE_GD_STR_I2 %d", otg.ep0_substate);
  1201.         break;

  1202.     case EP0_STATE_INTERFACE_GET:
  1203.         DBG_SETUP1("EP0_STATE_INTERFACE_GET\n");
  1204.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1205.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1206.         s3c_usb_write_ep0_fifo((u8 *)&get_intf+0, 1);
  1207.         otg.ep0_state = EP0_STATE_INIT;
  1208.         break;


  1209.     case EP0_STATE_GET_STATUS0:
  1210.         DBG_SETUP1("EP0_STATE_GET_STATUS0\n");
  1211.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1212.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1213.         s3c_usb_write_ep0_fifo((u8 *)&get_status+0, 1);
  1214.         otg.ep0_state = EP0_STATE_INIT;
  1215.         break;

  1216.     case EP0_STATE_GET_STATUS1:
  1217.         DBG_SETUP1("EP0_STATE_GET_STATUS1\n");
  1218.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1219.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1220.         s3c_usb_write_ep0_fifo((u8 *)&get_status+1, 1);
  1221.         otg.ep0_state = EP0_STATE_INIT;
  1222.         break;

  1223.     case EP0_STATE_GET_STATUS2:
  1224.         DBG_SETUP1("EP0_STATE_GET_STATUS2\n");
  1225.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1226.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1227.         s3c_usb_write_ep0_fifo((u8 *)&get_status+2, 1);
  1228.         otg.ep0_state = EP0_STATE_INIT;
  1229.         break;

  1230.     case EP0_STATE_GET_STATUS3:
  1231.         DBG_SETUP1("EP0_STATE_GET_STATUS3\n");
  1232.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1233.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1234.         s3c_usb_write_ep0_fifo((u8 *)&get_status+3, 1);
  1235.         otg.ep0_state = EP0_STATE_INIT;
  1236.         break;

  1237.     case EP0_STATE_GET_STATUS4:
  1238.         DBG_SETUP1("EP0_STATE_GET_STATUS4\n");
  1239.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1240.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1241.         s3c_usb_write_ep0_fifo((u8 *)&get_status+4, 1);
  1242.         otg.ep0_state = EP0_STATE_INIT;
  1243.         break;

  1244.     default:
  1245.         break;
  1246.     }
  1247. }


  1248. void s3c_usb_int_bulkin(void)
  1249. {
  1250.     u8* bulkin_buf;
  1251.     u32 remain_cnt;

  1252.     DBG_BULK0("Bulk In Function\n");

  1253.     bulkin_buf = (u8*)otg.up_ptr;
  1254.     remain_cnt = otg.up_size- ((u32)otg.up_ptr - otg.up_addr);
  1255.     DBG_BULK1("bulkin_buf = 0x%x,remain_cnt = 0x%x \n", bulkin_buf, remain_cnt);

  1256.     if (remain_cnt > otg.bulkin_max_pktsize) {
  1257.         s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1, otg.bulkin_max_pktsize);

  1258.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1259.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkin_max_pktsize<<0,
  1260.             S5P_OTG_DIEPCTL_IN);

  1261.         s3c_usb_write_in_fifo(bulkin_buf, otg.bulkin_max_pktsize);

  1262.         otg.up_ptr += otg.bulkin_max_pktsize;

  1263.     } else if(remain_cnt > 0) {
  1264.         s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1, remain_cnt);

  1265.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1266.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkin_max_pktsize<<0,
  1267.             S5P_OTG_DIEPCTL_IN);

  1268.         s3c_usb_write_in_fifo(bulkin_buf, remain_cnt);

  1269.         otg.up_ptr += remain_cnt;

  1270.     } else { /*remain_cnt = 0*/
  1271.         writel((DEPCTL_SNAK|DEPCTL_BULK_TYPE), S5P_OTG_DIEPCTL_IN);
  1272.     }
  1273. }

  1274. void s3c_usb_upload_start(void)
  1275. {
  1276.     u8 tmp_buf[12];
  1277.     u32 check;

  1278.     s3c_usb_read_out_fifo((u8 *)tmp_buf, 10);
  1279.     check = *((u8 *)(tmp_buf+8)) + (*((u8 *)(tmp_buf+9))<<8);

  1280.     if (check==0x1) {
  1281.         otg.up_addr =
  1282.             *((u8 *)(tmp_buf+0))+
  1283.             (*((u8 *)(tmp_buf+1))<<8)+
  1284.             (*((u8 *)(tmp_buf+2))<<16)+
  1285.             (*((u8 *)(tmp_buf+3))<<24);

  1286.         otg.up_size =
  1287.             *((u8 *)(tmp_buf+4))+
  1288.             (*((u8 *)(tmp_buf+5))<<8)+
  1289.             (*((u8 *)(tmp_buf+6))<<16)+
  1290.             (*((u8 *)(tmp_buf+7))<<24);

  1291.         otg.up_ptr=(u8 *)otg.up_addr;
  1292.         DBG_BULK1("UploadAddress : 0x%x, UploadSize: %d\n",
  1293.             otg.up_addr, otg.up_size);

  1294.         if (otg.op_mode == USB_CPU) {
  1295.             if (otg.up_size > otg.bulkin_max_pktsize) {
  1296.                 s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1,
  1297.                     otg.bulkin_max_pktsize);
  1298.             } else {
  1299.                 s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1,
  1300.                     otg.up_size);
  1301.             }

  1302.             /*ep1 enable, clear nak, bulk, usb active, max pkt 64*/
  1303.             writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkin_max_pktsize<<0,
  1304.                 S5P_OTG_DIEPCTL_IN);
  1305.         } else if ((otg.op_mode == USB_DMA) && (otg.up_size > 0)) {
  1306.             u32 pktcnt, remainder;

  1307.             DBG_BULK1("Dma Start for IN PKT \n");

  1308.             writel(MODE_DMA|BURST_INCR4|GBL_INT_UNMASK,
  1309.                 S5P_OTG_GAHBCFG);
  1310.             writel(INT_RESUME|INT_OUT_EP|INT_IN_EP| INT_ENUMDONE|
  1311.                 INT_RESET|INT_SUSPEND, S5P_OTG_GINTMSK);

  1312.             writel((u32)otg.up_ptr, S5P_OTG_DIEPDMA_IN);

  1313.             pktcnt = (u32)(otg.up_size/otg.bulkin_max_pktsize);
  1314.             remainder = (u32)(otg.up_size%otg.bulkin_max_pktsize);
  1315.             if(remainder != 0) {
  1316.                 pktcnt += 1;
  1317.             }

  1318.             if (pktcnt > 1023) {
  1319.                 s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1023,
  1320.                     (otg.bulkin_max_pktsize*1023));
  1321.             } else {
  1322.                 s3c_usb_set_inep_xfersize(EP_TYPE_BULK, pktcnt,
  1323.                     otg.up_size);
  1324.             }

  1325.             /*ep1 enable, clear nak, bulk, usb active, next ep1, max pkt */
  1326.             writel(1u<<31|1<<26|2<<18|1<<15|BULK_IN_EP<<11|
  1327.                 otg.bulkin_max_pktsize<<0,
  1328.                 S5P_OTG_DIEPCTL_IN);
  1329.         }
  1330.     }
  1331.     otg.dn_filesize=0;
  1332. }

  1333. void s3c_usb_download_start(u32 fifo_cnt_byte)
  1334. {
  1335.     u8 tmp_buf[8];

  1336.     s3c_usb_read_out_fifo((u8 *)tmp_buf, 8);
  1337.     DBG_BULK1("downloadFileSize==0, 1'st BYTE_READ_CNT_REG : %x\n",
  1338.         fifo_cnt_byte);

  1339.     otg.dn_addr=s3c_usbd_dn_addr;
  1340.     otg.dn_filesize=
  1341.         *((u8 *)(tmp_buf+4))+
  1342.         (*((u8 *)(tmp_buf+5))<<8)+
  1343.         (*((u8 *)(tmp_buf+6))<<16)+
  1344.         (*((u8 *)(tmp_buf+7))<<24);

  1345.     otg.dn_ptr=(u8 *)otg.dn_addr;
  1346.     DBG_BULK1("downloadAddress : 0x%x, downloadFileSize: %x\n",
  1347.         otg.dn_addr, otg.dn_filesize);

  1348.     /* The first 8-bytes are deleted.*/
  1349.     s3c_usb_read_out_fifo((u8 *)otg.dn_ptr, fifo_cnt_byte-8);
  1350.     otg.dn_ptr += fifo_cnt_byte-8;

  1351.     if (otg.op_mode == USB_CPU) {
  1352.         s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1,
  1353.             otg.bulkout_max_pktsize);

  1354.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1355.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1356.         S5P_OTG_DOEPCTL_OUT);
  1357.     } else if (otg.dn_filesize>otg.bulkout_max_pktsize) {
  1358.         u32 pkt_cnt, remain_cnt;

  1359.         DBG_BULK1("downloadFileSize!=0, Dma Start for 2nd OUT PKT \n");
  1360.         writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|
  1361.             INT_RESET|INT_SUSPEND, S5P_OTG_GINTMSK); /*gint unmask */
  1362.         writel(MODE_DMA|BURST_INCR4|GBL_INT_UNMASK,
  1363.             S5P_OTG_GAHBCFG);
  1364.         writel((u32)otg.dn_ptr, S5P_OTG_DOEPDMA_OUT);
  1365.         pkt_cnt = (u32)(otg.dn_filesize-otg.bulkout_max_pktsize)/otg.bulkout_max_pktsize;
  1366.         remain_cnt = (u32)((otg.dn_filesize-otg.bulkout_max_pktsize)%otg.bulkout_max_pktsize);
  1367.         if(remain_cnt != 0) {
  1368.             pkt_cnt += 1;
  1369.         }

  1370.         if (pkt_cnt > 1023) {
  1371.             s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1023,
  1372.                 (otg.bulkout_max_pktsize*1023));
  1373.         } else {
  1374.             s3c_usb_set_outep_xfersize(EP_TYPE_BULK, pkt_cnt,
  1375.                 (otg.dn_filesize-otg.bulkout_max_pktsize));
  1376.         }

  1377.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1378.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1379.             S5P_OTG_DOEPCTL_OUT);
  1380.     }
  1381. }

  1382. void s3c_usb_download_continue(u32 fifo_cnt_byte)
  1383. {
  1384.     if (otg.op_mode == USB_CPU) {
  1385.         s3c_usb_read_out_fifo((u8 *)otg.dn_ptr, fifo_cnt_byte);
  1386.         otg.dn_ptr += fifo_cnt_byte;
  1387.         DBG_BULK1("downloadFileSize!=0, 2nd BYTE_READ_CNT_REG = 0x%x, m_pDownPt = 0x%x\n",
  1388.                 fifo_cnt_byte, otg.dn_ptr);

  1389.         s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1, otg.bulkout_max_pktsize);

  1390.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1391.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1392.             S5P_OTG_DOEPCTL_OUT);

  1393.         /* USB format : addr(4)+size(4)+data(n)+cs(2) */
  1394.         if (((u32)otg.dn_ptr - otg.dn_addr) >= (otg.dn_filesize - 8)) {
  1395.             printf("Download Done!! Download Address: 0x%x, Download Filesize:0x%x\n",
  1396.                 otg.dn_addr, (otg.dn_filesize-10));

  1397.             s3c_usbd_dn_cnt     = otg.dn_filesize-10;
  1398.             s3c_usbd_dn_addr    = otg.dn_addr;

  1399. #ifdef USB_CHECKSUM_EN
  1400.             s3c_usb_verify_checksum();
  1401. #endif
  1402.             s3c_receive_done = 1;
  1403.         }

  1404.     }\
  1405. }

  1406. void s3c_usb_int_bulkout(u32 fifo_cnt_byte)
  1407. {
  1408.     DBG_BULK0("Bulk Out Function : otg.dn_filesize=0x%x\n", otg.dn_filesize);
  1409.     if (otg.dn_filesize==0) {
  1410.         if (fifo_cnt_byte == 10) {
  1411.             s3c_usb_upload_start();
  1412.         } else {
  1413.             s3c_usb_download_start(fifo_cnt_byte);
  1414.         }
  1415.     } else {
  1416.         s3c_usb_download_continue(fifo_cnt_byte);
  1417.     }
  1418. }

  1419. void s3c_usb_dma_in_done(void)
  1420. {
  1421.     s32 remain_cnt;

  1422.     DBG_BULK0("DMA IN : Transfer Done\n");

  1423.     otg.up_ptr = (u8 *)readl(S5P_OTG_DIEPDMA_IN);
  1424.     remain_cnt = otg.up_size- ((u32)otg.up_ptr - otg.up_addr);

  1425.     if (remain_cnt>0) {
  1426.         u32 pktcnt, remainder;
  1427.         pktcnt = (u32)(remain_cnt/otg.bulkin_max_pktsize);
  1428.         remainder = (u32)(remain_cnt%otg.bulkin_max_pktsize);
  1429.         if(remainder != 0) {
  1430.             pktcnt += 1;
  1431.         }
  1432.         DBG_SETUP1("remain_cnt : %d \n", remain_cnt);
  1433.         if (pktcnt> 1023) {
  1434.             s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1023,
  1435.                 (otg.bulkin_max_pktsize*1023));
  1436.         } else {
  1437.             s3c_usb_set_inep_xfersize(EP_TYPE_BULK, pktcnt,
  1438.                 remain_cnt);
  1439.         }

  1440.         /*ep1 enable, clear nak, bulk, usb active, next ep1, max pkt */
  1441.         writel(1u<<31|1<<26|2<<18|1<<15|BULK_IN_EP<<11|otg.bulkin_max_pktsize<<0,
  1442.             S5P_OTG_DIEPCTL_IN);
  1443.     } else
  1444.         DBG_SETUP1("DMA IN : Transfer Complete\n");
  1445. }

  1446. void s3c_usb_dma_out_done(void)
  1447. {
  1448.     s32 remain_cnt;

  1449.     DBG_BULK1("DMA OUT : Transfer Done\n");
  1450.     otg.dn_ptr = (u8 *)readl(S5P_OTG_DOEPDMA_OUT);

  1451.     remain_cnt = otg.dn_filesize - ((u32)otg.dn_ptr - otg.dn_addr + 8);

  1452.     if (remain_cnt>0) {
  1453.         u32 pktcnt, remainder;
  1454.         pktcnt = (u32)(remain_cnt/otg.bulkout_max_pktsize);
  1455.         remainder = (u32)(remain_cnt%otg.bulkout_max_pktsize);
  1456.         if(remainder != 0) {
  1457.             pktcnt += 1;
  1458.         }
  1459.         DBG_BULK1("remain_cnt : %d \n", remain_cnt);
  1460.         if (pktcnt> 1023) {
  1461.             s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1023,
  1462.                 (otg.bulkout_max_pktsize*1023));
  1463.         } else {
  1464.             s3c_usb_set_outep_xfersize(EP_TYPE_BULK, pktcnt,
  1465.                 remain_cnt);
  1466.         }

  1467.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1468.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1469.             S5P_OTG_DOEPCTL_OUT);
  1470.     } else {
  1471.         DBG_BULK1("DMA OUT : Transfer Complete\n");
  1472.         udelay(500);        /*for FPGA ???*/
  1473.     }
  1474. }

  1475. void s3c_usb_set_all_outep_nak(void)
  1476. {
  1477.     u8 i;
  1478.     u32 tmp;

  1479.     for(i=0;i<16;i++)
  1480.     {
  1481.         tmp = readl(S5P_OTG_DOEPCTL0+0x20*i);
  1482.         tmp |= DEPCTL_SNAK;
  1483.         writel(tmp, S5P_OTG_DOEPCTL0+0x20*i);
  1484.     }
  1485. }

  1486. void s3c_usb_clear_all_outep_nak(void)
  1487. {
  1488.     u8 i;
  1489.     u32 tmp;

  1490.     for(i=0;i<16;i++)
  1491.     {
  1492.         tmp = readl(S5P_OTG_DOEPCTL0+0x20*i);
  1493.         tmp |= (DEPCTL_EPENA|DEPCTL_CNAK);
  1494.         writel(tmp, S5P_OTG_DOEPCTL0+0x20*i);
  1495.     }
  1496. }

  1497. void s3c_usb_set_max_pktsize(USB_SPEED speed)
  1498. {
  1499.     if (speed == USB_HIGH)
  1500.     {
  1501.         otg.speed = USB_HIGH;
  1502.         otg.ctrl_max_pktsize = HS_CTRL_PKT_SIZE;
  1503.         otg.bulkin_max_pktsize = HS_BULK_PKT_SIZE;
  1504.         otg.bulkout_max_pktsize = HS_BULK_PKT_SIZE;
  1505.     }
  1506.     else
  1507.     {
  1508.         otg.speed = USB_FULL;
  1509.         otg.ctrl_max_pktsize = FS_CTRL_PKT_SIZE;
  1510.         otg.bulkin_max_pktsize = FS_BULK_PKT_SIZE;
  1511.         otg.bulkout_max_pktsize = FS_BULK_PKT_SIZE;
  1512.     }
  1513. }

  1514. void s3c_usb_set_endpoint(void)
  1515. {
  1516.     /* Unmask S5P_OTG_DAINT source */
  1517.     writel(0xff, S5P_OTG_DIEPINT0);
  1518.     writel(0xff, S5P_OTG_DOEPINT0);
  1519.     writel(0xff, S5P_OTG_DIEPINT_IN);
  1520.     writel(0xff, S5P_OTG_DOEPINT_OUT);

  1521.     /* Init For Ep0*/
  1522.     if(otg.speed == USB_HIGH)
  1523.     {
  1524.         /*MPS:64bytes */
  1525.         writel(((1<<26)|(CONTROL_EP<<11)|(0<<0)), S5P_OTG_DIEPCTL0);
  1526.         /*ep0 enable, clear nak */
  1527.         writel((1u<<31)|(1<<26)|(0<<0), S5P_OTG_DOEPCTL0);
  1528.     }
  1529.     else
  1530.     {
  1531.         /*MPS:8bytes */
  1532.         writel(((1<<26)|(CONTROL_EP<<11)|(3<<0)), S5P_OTG_DIEPCTL0);
  1533.         /*ep0 enable, clear nak */
  1534.         writel((1u<<31)|(1<<26)|(3<<0), S5P_OTG_DOEPCTL0);
  1535.     }
  1536. }

  1537. void s3c_usb_set_descriptors(void)
  1538. {
  1539.     /* Standard device descriptor */
  1540.     otg.desc.dev.bLength=DEVICE_DESC_SIZE;    /*0x12*/
  1541.     otg.desc.dev.bDescriptorType=DEVICE_DESCRIPTOR;
  1542.     otg.desc.dev.bDeviceClass=0xFF; /* 0x0*/
  1543.     otg.desc.dev.bDeviceSubClass=0x0;
  1544.     otg.desc.dev.bDeviceProtocol=0x0;
  1545.     otg.desc.dev.bMaxPacketSize0=otg.ctrl_max_pktsize;
  1546.     otg.desc.dev.idVendorL=0xE8;    /*0x45;*/
  1547.     otg.desc.dev.idVendorH=0x04;    /*0x53;*/
  1548.     otg.desc.dev.idProductL=0x34; /*0x00*/
  1549.     otg.desc.dev.idProductH=0x12; /*0x64*/
  1550.     otg.desc.dev.bcdDeviceL=0x00;
  1551.     otg.desc.dev.bcdDeviceH=0x01;
  1552.     otg.desc.dev.iManufacturer=0x1; /* index of string descriptor */
  1553.     otg.desc.dev.iProduct=0x2;    /* index of string descriptor */
  1554.     otg.desc.dev.iSerialNumber=0x0;
  1555.     otg.desc.dev.bNumConfigurations=0x1;
  1556.     if (otg.speed == USB_FULL) {
  1557.         otg.desc.dev.bcdUSBL=0x10;
  1558.         otg.desc.dev.bcdUSBH=0x01;    /* Ver 1.10*/
  1559.     }
  1560.     else {
  1561.         otg.desc.dev.bcdUSBL=0x00;
  1562.         otg.desc.dev.bcdUSBH=0x02;    /* Ver 2.0*/
  1563.     }

  1564.     /* Standard configuration descriptor */
  1565.     otg.desc.config.bLength=CONFIG_DESC_SIZE; /* 0x9 bytes */
  1566.     otg.desc.config.bDescriptorType=CONFIGURATION_DESCRIPTOR;
  1567.     otg.desc.config.wTotalLengthL=CONFIG_DESC_TOTAL_SIZE;
  1568.     otg.desc.config.wTotalLengthH=0;
  1569.     otg.desc.config.bNumInterfaces=1;
  1570. /* dbg     descConf.bConfigurationValue=2; // why 2? There's no reason.*/
  1571.     otg.desc.config.bConfigurationValue=1;
  1572.     otg.desc.config.iConfiguration=0;
  1573.     otg.desc.config.bmAttributes=CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED; /* bus powered only.*/
  1574.     otg.desc.config.maxPower=25; /* draws 50mA current from the USB bus.*/

  1575.     /* Standard interface descriptor */
  1576.     otg.desc.intf.bLength=INTERFACE_DESC_SIZE; /* 9*/
  1577.     otg.desc.intf.bDescriptorType=INTERFACE_DESCRIPTOR;
  1578.     otg.desc.intf.bInterfaceNumber=0x0;
  1579.     otg.desc.intf.bAlternateSetting=0x0; /* ?*/
  1580.     otg.desc.intf.bNumEndpoints = 2;    /* # of endpoints except EP0*/
  1581.     otg.desc.intf.bInterfaceClass=0xff; /* 0x0 ?*/
  1582.     otg.desc.intf.bInterfaceSubClass=0x0;
  1583.     otg.desc.intf.bInterfaceProtocol=0x0;
  1584.     otg.desc.intf.iInterface=0x0;

  1585.     /* Standard endpoint0 descriptor */
  1586.     otg.desc.ep1.bLength=ENDPOINT_DESC_SIZE;
  1587.     otg.desc.ep1.bDescriptorType=ENDPOINT_DESCRIPTOR;
  1588.     otg.desc.ep1.bEndpointAddress=BULK_IN_EP|EP_ADDR_IN;
  1589.     otg.desc.ep1.bmAttributes=EP_ATTR_BULK;
  1590.     otg.desc.ep1.wMaxPacketSizeL=(u8)otg.bulkin_max_pktsize; /* 64*/
  1591.     otg.desc.ep1.wMaxPacketSizeH=(u8)(otg.bulkin_max_pktsize>>8);
  1592.     otg.desc.ep1.bInterval=0x0; /* not used */

  1593.     /* Standard endpoint1 descriptor */
  1594.     otg.desc.ep2.bLength=ENDPOINT_DESC_SIZE;
  1595.     otg.desc.ep2.bDescriptorType=ENDPOINT_DESCRIPTOR;
  1596.     otg.desc.ep2.bEndpointAddress=BULK_OUT_EP|EP_ADDR_OUT;
  1597.     otg.desc.ep2.bmAttributes=EP_ATTR_BULK;
  1598.     otg.desc.ep2.wMaxPacketSizeL=(u8)otg.bulkout_max_pktsize; /* 64*/
  1599.     otg.desc.ep2.wMaxPacketSizeH=(u8)(otg.bulkout_max_pktsize>>8);
  1600.     otg.desc.ep2.bInterval=0x0; /* not used */
  1601. }

  1602. void s3c_usb_check_speed(USB_SPEED *speed)
  1603. {
  1604.     u32 status;

  1605.     status = readl(S5P_OTG_DSTS); /* System status read */

  1606.     *speed = (USB_SPEED)((status&0x6) >>1);
  1607. }

  1608. void s3c_usb_clear_dnfile_info(void)
  1609. {
  1610.     otg.dn_addr = 0;
  1611.     otg.dn_filesize = 0;
  1612.     otg.dn_ptr = 0;
  1613. }

  1614. void s3c_usb_clear_upfile_info(void)
  1615. {
  1616.     otg.up_addr= 0;
  1617.     otg.up_size= 0;
  1618.     otg.up_ptr = 0;
  1619. }


  1620. int s3c_usb_check_setconf(void)
  1621. {
  1622.     if (otg.set_config == 0)
  1623.         return FALSE;
  1624.     else
  1625.         return TRUE;
  1626. }

  1627. void s3c_usb_set_opmode(USB_OPMODE mode)
  1628. {
  1629.     otg.op_mode = mode;

  1630.     writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|
  1631.         INT_RESET|INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY,
  1632.         S5P_OTG_GINTMSK); /*gint unmask */

  1633.     writel(MODE_SLAVE|BURST_SINGLE|GBL_INT_UNMASK, S5P_OTG_GAHBCFG);

  1634.     s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1, otg.bulkout_max_pktsize);
  1635.     s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1, 0);

  1636.     /*bulk out ep enable, clear nak, bulk, usb active, next ep3, max pkt */
  1637.     writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1638.         S5P_OTG_DOEPCTL_OUT);

  1639.     /*bulk in ep enable, clear nak, bulk, usb active, next ep1, max pkt */
  1640.     writel(0u<<31|1<<26|2<<18|1<<15|otg.bulkin_max_pktsize<<0,
  1641.         S5P_OTG_DIEPCTL_IN);
  1642. }

  1643. void s3c_usb_reset(void)
  1644. {
  1645.     s3c_usb_set_all_outep_nak();

  1646.     otg.ep0_state = EP0_STATE_INIT;
  1647.     writel(((1<<BULK_OUT_EP)|(1<<CONTROL_EP))<<16|((1<<BULK_IN_EP)|(1<<CONTROL_EP)),
  1648.         S5P_OTG_DAINTMSK);
  1649.     writel(CTRL_OUT_EP_SETUP_PHASE_DONE|AHB_ERROR|TRANSFER_DONE,
  1650.         S5P_OTG_DOEPMSK);
  1651.     writel(INTKN_TXFEMP|NON_ISO_IN_EP_TIMEOUT|AHB_ERROR|TRANSFER_DONE,
  1652.         S5P_OTG_DIEPMSK);

  1653.     /* Rx FIFO Size */
  1654.     writel(RX_FIFO_SIZE, S5P_OTG_GRXFSIZ);

  1655.     /* Non Periodic Tx FIFO Size */
  1656.     writel(NPTX_FIFO_SIZE<<16| NPTX_FIFO_START_ADDR<<0, S5P_OTG_GNPTXFSIZ);

  1657.     s3c_usb_clear_all_outep_nak();

  1658.     /*clear device address */
  1659.     writel(readl(S5P_OTG_DCFG)&~(0x7f<<4), S5P_OTG_DCFG);

  1660.     if(SUSPEND_RESUME_ON) {
  1661.         writel(readl(S5P_OTG_PCGCCTL)&~(1<<0), S5P_OTG_PCGCCTL);
  1662.     }
  1663. }
  1664. int s3c_usb_set_init(void)
  1665. {
  1666.     u32 status;

  1667.     status = readl(S5P_OTG_DSTS); /* System status read */

  1668.     /* Set if Device is High speed or Full speed */
  1669.     if (((status&0x6) >>1) == USB_HIGH) {
  1670.         DBG_SETUP1("High Speed Detection\n");
  1671.         s3c_usb_set_max_pktsize(USB_HIGH);
  1672.     }
  1673.     else if(((status&0x6) >>1) == USB_FULL) {
  1674.         DBG_SETUP1("Full Speed Detec tion\n");
  1675.         s3c_usb_set_max_pktsize(USB_FULL);
  1676.     }
  1677.     else {
  1678.         printf("**** Error:Neither High_Speed nor Full_Speed\n");
  1679.         return FALSE;
  1680.     }

  1681.     s3c_usb_set_endpoint();
  1682.     s3c_usb_set_descriptors();
  1683.     s3c_usb_clear_dnfile_info();
  1684.     s3c_usb_set_opmode(op_mode);

  1685.     return TRUE;
  1686. }

  1687. void s3c_usb_pkt_receive(void)
  1688. {
  1689.     u32 rx_status;
  1690.     u32 fifo_cnt_byte;

  1691.     rx_status = readl(S5P_OTG_GRXSTSP);
  1692.     DBG_SETUP0("S5P_OTG_GRXSTSP = 0x%x\n", rx_status);

  1693.     if ((rx_status & (0xf<<17)) == SETUP_PKT_RECEIVED) {
  1694.         DBG_SETUP1("SETUP_PKT_RECEIVED\n");
  1695.         s3c_usb_ep0_int_hndlr();

  1696.     } else if ((rx_status & (0xf<<17)) == OUT_PKT_RECEIVED) {
  1697.         fifo_cnt_byte = (rx_status & 0x7ff0)>>4;
  1698.         DBG_SETUP1("OUT_PKT_RECEIVED\n");

  1699.         if((rx_status & BULK_OUT_EP)&&(fifo_cnt_byte)) {
  1700.             s3c_usb_int_bulkout(fifo_cnt_byte);
  1701.             if( otg.op_mode == USB_CPU )
  1702.                 writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|
  1703.                     INT_ENUMDONE|INT_RESET|INT_SUSPEND|
  1704.                     INT_RX_FIFO_NOT_EMPTY,
  1705.                     S5P_OTG_GINTMSK);
  1706.             return;
  1707.         }

  1708.     } else if ((rx_status & (0xf<<17)) == GLOBAL_OUT_NAK) {
  1709.         DBG_SETUP1("GLOBAL_OUT_NAK\n");

  1710.     } else if ((rx_status & (0xf<<17)) == OUT_TRNASFER_COMPLETED) {
  1711.         DBG_SETUP1("OUT_TRNASFER_COMPLETED\n");

  1712.     } else if ((rx_status & (0xf<<17)) == SETUP_TRANSACTION_COMPLETED) {
  1713.         DBG_SETUP1("SETUP_TRANSACTION_COMPLETED\n");

  1714.     } else {
  1715.         DBG_SETUP1("Reserved\n");
  1716.     }
  1717. }

  1718. void s3c_usb_transfer(void)
  1719. {
  1720.     u32 ep_int;
  1721.     u32 check_dma;
  1722.     u32 ep_int_status;

  1723.     ep_int = readl(S5P_OTG_DAINT);
  1724.     DBG_SETUP0("S5P_OTG_DAINT = 0x%x", ep_int);

  1725.     if (ep_int & (1<<CONTROL_EP)) {
  1726.         ep_int_status = readl(S5P_OTG_DIEPINT0);
  1727.         DBG_SETUP1("S5P_OTG_DIEPINT0 : %x \n", ep_int_status);

  1728.         if (ep_int_status & INTKN_TXFEMP) {
  1729.             u32 uNTxFifoSpace;
  1730.             do {
  1731.                 uNTxFifoSpace=readl(S5P_OTG_GNPTXSTS)&0xffff;
  1732.             }while(uNTxFifoSpace<otg.ctrl_max_pktsize);

  1733.             s3c_usb_transfer_ep0();
  1734.         }

  1735.         writel(ep_int_status, S5P_OTG_DIEPINT0); /* Interrupt Clear */
  1736.     }

  1737.     if (ep_int & ((1<<CONTROL_EP)<<16)) {
  1738.         ep_int_status = readl(S5P_OTG_DOEPINT0);
  1739.         DBG_SETUP1("S5P_OTG_DOEPINT0 : %x \n", ep_int_status);

  1740.         s3c_usb_set_outep_xfersize(EP_TYPE_CONTROL, 1, 8);
  1741.         writel(1u<<31|1<<26, S5P_OTG_DOEPCTL0); /*ep0 enable, clear nak */

  1742.         writel(ep_int_status, S5P_OTG_DOEPINT0); /* Interrupt Clear */
  1743.     }

  1744.     if(ep_int & (1<<BULK_IN_EP)) {
  1745.         ep_int_status = readl(S5P_OTG_DIEPINT_IN);
  1746.         DBG_BULK1("S5P_OTG_DIEPINT_IN : %x \n", ep_int_status);
  1747.         writel(ep_int_status, S5P_OTG_DIEPINT_IN); /* Interrupt Clear */

  1748.         if ( (ep_int_status&INTKN_TXFEMP) && otg.op_mode == USB_CPU)
  1749.             s3c_usb_int_bulkin();

  1750.         check_dma = readl(S5P_OTG_GAHBCFG);
  1751.         if ((check_dma&MODE_DMA)&&(ep_int_status&TRANSFER_DONE))
  1752.             s3c_usb_dma_in_done();
  1753.     }

  1754.     if (ep_int & ((1<<BULK_OUT_EP)<<16)) {
  1755.         ep_int_status = readl(S5P_OTG_DOEPINT_OUT);
  1756.         DBG_BULK1("S5P_OTG_DOEPINT_OUT : 0x%x\n", ep_int_status);
  1757.         writel(ep_int_status, S5P_OTG_DOEPINT_OUT); /* Interrupt Clear */

  1758.         check_dma = readl(S5P_OTG_GAHBCFG);
  1759.         if ((check_dma&MODE_DMA)&&(ep_int_status&TRANSFER_DONE)) {
  1760.             s3c_usb_dma_out_done();
  1761.         }
  1762.     }
  1763. }

  1764. void s3c_udc_int_hndlr(void)
  1765. {
  1766.     u32 int_status;
  1767.     int tmp;

  1768.     int_status = readl(S5P_OTG_GINTSTS); /* Core Interrupt Register */
  1769.     writel(int_status, S5P_OTG_GINTSTS); /* Interrupt Clear */
  1770.     DBG_SETUP0("*** USB OTG Interrupt(S5P_OTG_GINTSTS: 0x%08x) ****\n",
  1771.         int_status);

  1772.     if (int_status & INT_RESET) {
  1773.         DBG_SETUP1("INT_RESET\n");
  1774.         writel(INT_RESET, S5P_OTG_GINTSTS); /* Interrupt Clear */

  1775.         s3c_usb_reset();
  1776.     }

  1777.     if (int_status & INT_ENUMDONE) {
  1778.         DBG_SETUP1("INT_ENUMDONE :");
  1779.         writel(INT_ENUMDONE, S5P_OTG_GINTSTS); /* Interrupt Clear */
  1780.         s3c_usb_connected_status=3;
  1781.         tmp = s3c_usb_set_init();
  1782.         if (tmp == FALSE)
  1783.             return;

  1784.     }

  1785.     if (int_status & INT_RESUME) {
  1786.         DBG_SETUP1("INT_RESUME\n");
  1787.         writel(INT_RESUME, S5P_OTG_GINTSTS); /* Interrupt Clear */

  1788.         if(SUSPEND_RESUME_ON) {
  1789.             writel(readl(S5P_OTG_PCGCCTL)&~(1<<0), S5P_OTG_PCGCCTL);
  1790.             DBG_SETUP1("INT_RESUME\n");
  1791.         }
  1792.     }

  1793.     if (int_status & INT_SUSPEND) {
  1794.         DBG_SETUP1("INT_SUSPEND\n");
  1795.         writel(INT_SUSPEND, S5P_OTG_GINTSTS); /* Interrupt Clear */

  1796.         if(SUSPEND_RESUME_ON) {
  1797.             writel(readl(S5P_OTG_PCGCCTL)|(1<<0), S5P_OTG_PCGCCTL);
  1798.         }
  1799.     }

  1800.     if(int_status & INT_RX_FIFO_NOT_EMPTY) {
  1801.         DBG_SETUP1("INT_RX_FIFO_NOT_EMPTY\n");
  1802.         /* Read only register field */

  1803.         writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|
  1804.             INT_ENUMDONE|INT_RESET|INT_SUSPEND,
  1805.             S5P_OTG_GINTMSK);
  1806.         s3c_usb_pkt_receive();
  1807.         writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|
  1808.             INT_RESET |INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY,
  1809.             S5P_OTG_GINTMSK); /*gint unmask */
  1810.         s3c_usb_connected_status=1;
  1811.     }

  1812.     if ((int_status & INT_IN_EP) || (int_status & INT_OUT_EP)) {
  1813.         DBG_SETUP1("INT_IN or OUT_EP\n");
  1814.         /* Read only register field */

  1815.         s3c_usb_transfer();
  1816.     }
  1817. }

  1818. int s3c_udc_is_adc_cable()
  1819. {
  1820.     int count = 0;
  1821.     while(count<10)
  1822.         {
  1823.     if((readl(S5P_OTG_HPRT)&0xc00)==0xc00)
  1824.         count ++;
  1825.     else
  1826.         {
  1827.             count =0;
  1828.             return 0;
  1829.         }
  1830.     }
  1831.     return 1;
  1832. }
  1833. /*the following is for u-boot battery charge*/
  1834. int s3c_check_usb_connect_status()
  1835. {
  1836.     if(!s3c_usb_phy_inited)/*initalized at first*/
  1837.     {    
  1838.         s3c_usbctl_init(1);
  1839.     }
  1840.     /*then check current usb status*/
  1841.     unsigned long usec = (1000*1000*10);
  1842.     ulong tmo, tmp;

  1843.     if (usec >= 1000) {        /* if "big" number, spread normalization to seconds */
  1844.         tmo = usec / 1000;    /* start to normalize for usec to ticks per sec */
  1845.         tmo *= CFG_HZ;        /* find number of "ticks" to wait to achieve target */
  1846.         tmo /= 1000;        /* finish normalize. */
  1847.     }
  1848.     else {                /* else small number, don't kill it prior to HZ multiply */
  1849.         tmo = usec * CFG_HZ;
  1850.         tmo /= (1000 * 1000);
  1851.     }
  1852.     tmp = get_timer(0);        /* get current timestamp */
  1853.     if ((tmo + tmp + 1) < tmp)    /* if setting this fordward will roll time stamp */
  1854.     {        
  1855.         reset_timer_masked();    /* reset "advancing" timestamp to 0, set lastdec value */
  1856.     }
  1857.     else
  1858.         tmo += tmp;        /* else, set advancing stamp wake up time */

  1859.     while (get_timer_masked()<tmo)    
  1860.     {
  1861.         if (S3C_USBD_DETECT_IRQ()) {
  1862.             s3c_udc_int_hndlr();
  1863.             S3C_USBD_CLEAR_IRQ();
  1864.         }
  1865.         if(s3c_udc_is_adc_cable())
  1866.             s3c_usb_connected_status=2;
  1867.         if(s3c_usb_connected_status==2||s3c_usb_connected_status==1)
  1868.             break;
  1869.     }
  1870.     
  1871.     if(s3c_usb_phy_inited)
  1872.     {        
  1873.         s3c_usb_stop();
  1874.     }
  1875.     return s3c_usb_connected_status;
  1876.     
  1877. }
  1878. #endif


点击(此处)折叠或打开

  1. /*
  2.  * cpu/s5pc1xx/usbd-otg-hs.c
  3.  *
  4.  * (C) Copyright 2007
  5.  * Byungjae Lee, Samsung Erectronics, bjlee@samsung.com.
  6.  *    - only support for S5PC100
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21.  * MA 02111-1307 USA
  22.  */

  23. #include <common.h>

  24. #if defined(CONFIG_S5PC110)
  25. #include <command.h>
  26. #include <asm/errno.h>
  27. #include <asm/io.h>
  28. #include <regs.h>
  29. #include "usbd-otg-hs.h"

  30. #undef USB_OTG_DEBUG_SETUP
  31. #ifdef USB_OTG_DEBUG_SETUP
  32. #define DBG_SETUP0(fmt, args...) printf("[%s:%d] " fmt, __FUNCTION__, __LINE__, ##args)
  33. #define DBG_SETUP1(fmt, args...) printf("\t" fmt, ##args)
  34. #define DBG_SETUP2(fmt, args...) printf(fmt, ##args)
  35. #else
  36. #define DBG_SETUP0(fmt, args...) do { } while (0)
  37. #define DBG_SETUP1(fmt, args...) do { } while (0)
  38. #define DBG_SETUP2(fmt, args...) do { } while (0)
  39. #endif

  40. #undef USB_OTG_DEBUG_BULK
  41. #ifdef USB_OTG_DEBUG_BULK
  42. #define DBG_BULK0(fmt, args...) printf("[%s:%d] " fmt, __FUNCTION__, __LINE__, ##args)
  43. #define DBG_BULK1(fmt, args...)    printf("\t" fmt, ##args)
  44. #else
  45. #define DBG_BULK0(fmt, args...) do { } while (0)
  46. #define DBG_BULK1(fmt, args...) do { } while (0)
  47. #endif

  48. #define USB_CHECKSUM_EN

  49. #define TRUE    1
  50. #define FALSE    0
  51. #define SUSPEND_RESUME_ON FALSE

  52. u32 s3c_usbd_dn_addr = 0;
  53. u32 s3c_usbd_dn_cnt = 0;
  54. u32 remode_wakeup;
  55. u16 config_value;

  56. int DNW;
  57. int s3c_receive_done = 0;
  58. int s3c_got_header = 0;
  59. int s3c_usb_phy_inited=0;
  60. int s3c_usb_connected_status= 0;/*0,none,1,usb connected,2,power connected,3,bad usb connected*/
  61. USB_OPMODE    op_mode = USB_CPU;
  62. USB_SPEED    speed = USB_HIGH;

  63. otg_dev_t    otg;
  64. get_status_t    get_status;
  65. get_intf_t    get_intf;

  66. enum EP_INDEX
  67. {
  68.     EP0, EP1, EP2, EP3, EP4
  69. };

  70. /*------------------------------------------------*/
  71. /* EP0 state */
  72. enum EP0_STATE
  73. {
  74.     EP0_STATE_INIT            = 0,
  75.     EP0_STATE_GD_DEV_0        = 11,
  76.     EP0_STATE_GD_DEV_1        = 12,
  77.     EP0_STATE_GD_DEV_2        = 13,
  78.     EP0_STATE_GD_CFG_0        = 21,
  79.     EP0_STATE_GD_CFG_1        = 22,
  80.     EP0_STATE_GD_CFG_2        = 23,
  81.     EP0_STATE_GD_CFG_3        = 24,
  82.     EP0_STATE_GD_CFG_4        = 25,
  83.     EP0_STATE_GD_STR_I0        = 30,
  84.     EP0_STATE_GD_STR_I1        = 31,
  85.     EP0_STATE_GD_STR_I2        = 32,
  86.     EP0_STATE_GD_DEV_QUALIFIER    = 33,
  87.     EP0_STATE_INTERFACE_GET        = 34,
  88.     EP0_STATE_GET_STATUS0        = 35,
  89.     EP0_STATE_GET_STATUS1        = 36,
  90.     EP0_STATE_GET_STATUS2        = 37,
  91.     EP0_STATE_GET_STATUS3        = 38,
  92.     EP0_STATE_GET_STATUS4        = 39,
  93.     EP0_STATE_GD_OTHER_SPEED    = 40,
  94.     EP0_STATE_GD_CFG_ONLY_0     = 41,
  95.     EP0_STATE_GD_CFG_ONLY_1     = 42,
  96.     EP0_STATE_GD_IF_ONLY_0        = 44,
  97.     EP0_STATE_GD_IF_ONLY_1        = 45,
  98.     EP0_STATE_GD_EP0_ONLY_0     = 46,
  99.     EP0_STATE_GD_EP1_ONLY_0     = 47,
  100.     EP0_STATE_GD_EP2_ONLY_0     = 48,
  101.     EP0_STATE_GD_EP3_ONLY_0     = 49,
  102.     EP0_STATE_GD_OTHER_SPEED_HIGH_1    = 51,
  103.     EP0_STATE_GD_OTHER_SPEED_HIGH_2    = 52,
  104.     EP0_STATE_GD_OTHER_SPEED_HIGH_3    = 53
  105. };

  106. /*definitions related to CSR setting */

  107. /* S5P_OTG_GOTGCTL*/
  108. #define B_SESSION_VALID        (0x1<<19)
  109. #define A_SESSION_VALID        (0x1<<18)

  110. /* S5P_OTG_GAHBCFG*/
  111. #define PTXFE_HALF        (0<<8)
  112. #define PTXFE_ZERO        (1<<8)
  113. #define NPTXFE_HALF        (0<<7)
  114. #define NPTXFE_ZERO        (1<<7)
  115. #define MODE_SLAVE        (0<<5)
  116. #define MODE_DMA        (1<<5)
  117. #define BURST_SINGLE        (0<<1)
  118. #define BURST_INCR        (1<<1)
  119. #define BURST_INCR4        (3<<1)
  120. #define BURST_INCR8        (5<<1)
  121. #define BURST_INCR16        (7<<1)
  122. #define GBL_INT_UNMASK        (1<<0)
  123. #define GBL_INT_MASK        (0<<0)

  124. /* S5P_OTG_GRSTCTL*/
  125. #define AHB_MASTER_IDLE        (1u<<31)
  126. #define CORE_SOFT_RESET        (0x1<<0)

  127. /* S5P_OTG_GINTSTS/S5P_OTG_GINTMSK core interrupt register */
  128. #define INT_RESUME        (1u<<31)
  129. #define INT_DISCONN        (0x1<<29)
  130. #define INT_CONN_ID_STS_CNG    (0x1<<28)
  131. #define INT_OUT_EP        (0x1<<19)
  132. #define INT_IN_EP        (0x1<<18)
  133. #define INT_ENUMDONE        (0x1<<13)
  134. #define INT_RESET        (0x1<<12)
  135. #define INT_SUSPEND        (0x1<<11)
  136. #define INT_TX_FIFO_EMPTY    (0x1<<5)
  137. #define INT_RX_FIFO_NOT_EMPTY    (0x1<<4)
  138. #define INT_SOF            (0x1<<3)
  139. #define INT_DEV_MODE        (0x0<<0)
  140. #define INT_HOST_MODE        (0x1<<1)

  141. /* S5P_OTG_GRXSTSP STATUS*/
  142. #define GLOBAL_OUT_NAK            (0x1<<17)
  143. #define OUT_PKT_RECEIVED        (0x2<<17)
  144. #define OUT_TRNASFER_COMPLETED        (0x3<<17)
  145. #define SETUP_TRANSACTION_COMPLETED    (0x4<<17)
  146. #define SETUP_PKT_RECEIVED        (0x6<<17)

  147. /* S5P_OTG_DCTL device control register */
  148. #define NORMAL_OPERATION        (0x1<<0)
  149. #define SOFT_DISCONNECT            (0x1<<1)
  150. #define    TEST_J_MODE            (TEST_J<<4)
  151. #define    TEST_K_MODE            (TEST_K<<4)
  152. #define    TEST_SE0_NAK_MODE        (TEST_SE0_NAK<<4)
  153. #define    TEST_PACKET_MODE        (TEST_PACKET<<4)
  154. #define    TEST_FORCE_ENABLE_MODE        (TEST_FORCE_ENABLE<<4)
  155. #define TEST_CONTROL_FIELD        (0x7<<4)

  156. /* S5P_OTG_DAINT device all endpoint interrupt register */
  157. #define INT_IN_EP0            (0x1<<0)
  158. #define INT_IN_EP1            (0x1<<1)
  159. #define INT_IN_EP3            (0x1<<3)
  160. #define INT_OUT_EP0            (0x1<<16)
  161. #define INT_OUT_EP2            (0x1<<18)
  162. #define INT_OUT_EP4            (0x1<<20)

  163. /* S5P_OTG_DIEPCTL0/S5P_OTG_DOEPCTL0 */
  164. #define DEPCTL_EPENA            (0x1<<31)
  165. #define DEPCTL_EPDIS            (0x1<<30)
  166. #define DEPCTL_SNAK            (0x1<<27)
  167. #define DEPCTL_CNAK            (0x1<<26)
  168. #define DEPCTL_CTRL_TYPE        (EP_TYPE_CONTROL<<18)
  169. #define DEPCTL_ISO_TYPE            (EP_TYPE_ISOCHRONOUS<<18)
  170. #define DEPCTL_BULK_TYPE        (EP_TYPE_BULK<<18)
  171. #define DEPCTL_INTR_TYPE        (EP_TYPE_INTERRUPT<<18)
  172. #define DEPCTL_USBACTEP            (0x1<<15)

  173. /*ep0 enable, clear nak, next ep0, max 64byte */
  174. #define EPEN_CNAK_EP0_64 (DEPCTL_EPENA|DEPCTL_CNAK|(CONTROL_EP<<11)|(0<<0))

  175. /*ep0 enable, clear nak, next ep0, 8byte */
  176. #define EPEN_CNAK_EP0_8 (DEPCTL_EPENA|DEPCTL_CNAK|(CONTROL_EP<<11)|(3<<0))

  177. /* DIEPCTLn/DOEPCTLn */
  178. #define BACK2BACK_SETUP_RECEIVED    (0x1<<6)
  179. #define INTKN_TXFEMP            (0x1<<4)
  180. #define NON_ISO_IN_EP_TIMEOUT        (0x1<<3)
  181. #define CTRL_OUT_EP_SETUP_PHASE_DONE    (0x1<<3)
  182. #define AHB_ERROR            (0x1<<2)
  183. #define TRANSFER_DONE            (0x1<<0)


  184. /* codes representing languages */
  185. const u8 string_desc0[] =
  186. {
  187.     4, STRING_DESCRIPTOR, LANGID_US_L, LANGID_US_H,
  188. };

  189. const u8 string_desc1[] = /* Manufacturer */
  190. {
  191.     (0x14+2), STRING_DESCRIPTOR,
  192.     'S', 0x0, 'y', 0x0, 's', 0x0, 't', 0x0, 'e', 0x0,
  193.     'm', 0x0, ' ', 0x0, 'M', 0x0, 'C', 0x0, 'U', 0x0,
  194. };

  195. const u8 string_desc2[] = /* Product */
  196. {
  197.     (0x2a+2), STRING_DESCRIPTOR,
  198.     'S', 0x0, 'E', 0x0, 'C', 0x0, ' ', 0x0, 'S', 0x0,
  199.     '3', 0x0, 'C', 0x0, '6', 0x0, '4', 0x0, '0', 0x0,
  200.     '0', 0x0, 'X', 0x0, ' ', 0x0, 'T', 0x0, 'e', 0x0,
  201.     's', 0x0, 't', 0x0, ' ', 0x0, 'B', 0x0, '/', 0x0,
  202.     'D', 0x0
  203. };

  204. /* setting the device qualifier descriptor and a string descriptor */
  205. const u8 qualifier_desc[] =
  206. {
  207.     0x0a,    /* 0 desc size */
  208.     0x06,    /* 1 desc type (DEVICE_QUALIFIER)*/
  209.     0x00,    /* 2 USB release */
  210.     0x02,    /* 3 => 2.00*/
  211.     0xFF,    /* 4 class */
  212.     0x00,    /* 5 subclass */
  213.     0x00,    /* 6 protocol */
  214.     64,    /* 7 max pack size */
  215.     0x01,    /* 8 number of other-speed configuration */
  216.     0x00,    /* 9 reserved */
  217. };

  218. const u8 config_full[] =
  219. {
  220.     0x09,    /* 0 desc size */
  221.     0x07,    /* 1 desc type (other speed)*/
  222.     0x20,    /* 2 Total length of data returned */
  223.     0x00,    /* 3 */
  224.     0x01,    /* 4 Number of interfaces supported by this speed configuration */
  225.     0x01,    /* 5 value to use to select configuration */
  226.     0x00,    /* 6 index of string desc */
  227.         /* 7 same as configuration desc */
  228.     CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED,
  229.     0x19,    /* 8 same as configuration desc */

  230. };

  231. const u8 config_full_total[] =
  232. {
  233.   0x09, 0x07 ,0x20 ,0x00 ,0x01 ,0x01 ,0x00 ,0xC0 ,0x19,
  234.   0x09 ,0x04 ,0x00 ,0x00 ,0x02 ,0xff ,0x00 ,0x00 ,0x00,
  235.   0x07 ,0x05 ,0x83 ,0x02 ,0x40 ,0x00 ,0x00,
  236.   0x07 ,0x05 ,0x04 ,0x02 ,0x40 ,0x00 ,0x00
  237. };

  238. const u8 config_high[] =
  239. {
  240.     0x09,    /* 0 desc size */
  241.     0x07,    /* 1 desc type (other speed)*/
  242.     0x20,    /* 2 Total length of data returned */
  243.     0x00,    /* 3 */
  244.     0x01,    /* 4 Number of interfaces supported by this speed configuration */
  245.     0x01,    /* 5 value to use to select configuration */
  246.     0x00,    /* 6 index of string desc */
  247.         /* 7 same as configuration desc */
  248.     CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED,
  249.     0x19,    /* 8 same as configuration desc */

  250. };

  251. const u8 config_high_total[] =
  252. {
  253.   0x09, 0x07 ,0x20 ,0x00 ,0x01 ,0x01 ,0x00 ,0xC0 ,0x19,
  254.   0x09 ,0x04 ,0x00 ,0x00 ,0x02 ,0xff ,0x00 ,0x00 ,0x00,
  255.   0x07 ,0x05 ,0x81 ,0x02 ,0x00 ,0x02 ,0x00,
  256.   0x07 ,0x05 ,0x02 ,0x02 ,0x00 ,0x02 ,0x00
  257. };

  258. /* Descriptor size */
  259. enum DESCRIPTOR_SIZE
  260. {
  261.     DEVICE_DESC_SIZE    = sizeof(device_desc_t),
  262.     STRING_DESC0_SIZE    = sizeof(string_desc0),
  263.     STRING_DESC1_SIZE    = sizeof(string_desc1),
  264.     STRING_DESC2_SIZE    = sizeof(string_desc2),
  265.     CONFIG_DESC_SIZE    = sizeof(config_desc_t),
  266.     INTERFACE_DESC_SIZE    = sizeof(intf_desc_t),
  267.     ENDPOINT_DESC_SIZE    = sizeof(ep_desc_t),
  268.     DEVICE_QUALIFIER_SIZE    = sizeof(qualifier_desc),
  269.     OTHER_SPEED_CFG_SIZE    = 9

  270. };

  271. /*32 <cfg desc>+<if desc>+<endp0 desc>+<endp1 desc>*/
  272. #define CONFIG_DESC_TOTAL_SIZE    \
  273.     (CONFIG_DESC_SIZE+INTERFACE_DESC_SIZE+ENDPOINT_DESC_SIZE*2)
  274. #define TEST_PKT_SIZE 53

  275. u8 test_pkt [TEST_PKT_SIZE] = {
  276.     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*JKJKJKJK x 9*/
  277.     0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,    /*JJKKJJKK x 8*/
  278.     0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,    /*JJJJKKKK x 8*/
  279.     0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,    /*JJJJJJJKKKKKKK x8 - '1'*/
  280.     0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,        /*'1' + JJJJJJJK x 8*/
  281.     0xFC,0x7E,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0x7E    /*{JKKKKKKK x 10},JK*/
  282. };

  283. void s3c_usb_init_phy(void)
  284. {
  285.     writel(readl(USB_PHY_CONTROL)|(1<<0), USB_PHY_CONTROL);    /*USB PHY0 Enable */
  286.     
  287.     writel(0xa0, S5P_OTG_PHYPWR);
  288.     writel(0x3, S5P_OTG_PHYCLK);
  289.     writel(0x1, S5P_OTG_RSTCON);
  290.     udelay(10);
  291.     writel(0x0, S5P_OTG_RSTCON);
  292.     udelay(10);

  293. }

  294. /* OTG PHY Power Off */
  295. void s3c_usb_phy_off(void) {
  296.     writel(readl(S5P_OTG_PHYPWR)|(0x18), S5P_OTG_PHYPWR);
  297.     writel(readl(USB_PHY_CONTROL)&~(1<<0), USB_PHY_CONTROL);
  298.     s3c_usb_phy_inited=0;
  299. }

  300. void s3c_usb_core_soft_reset(void)
  301. {
  302.     u32 tmp;

  303.     writel(CORE_SOFT_RESET, S5P_OTG_GRSTCTL);

  304.     do
  305.     {
  306.         tmp = readl(S5P_OTG_GRSTCTL);
  307.     }while(!(tmp & AHB_MASTER_IDLE));

  308. }

  309. void s3c_usb_wait_cable_insert(void)
  310. {
  311.     u32 tmp;
  312.     int ucFirst=1;

  313.     do {
  314.         udelay(50);

  315.         tmp = readl(S5P_OTG_GOTGCTL);

  316.         if (tmp & (B_SESSION_VALID|A_SESSION_VALID)) {
  317.             printf("OTG cable Connected!\n");
  318.             break;
  319.         } else if(ucFirst == 1) {
  320.             printf("Insert a OTG cable into the connector!\n");
  321.             ucFirst = 0;
  322.         }
  323.     } while(1);
  324. }

  325. void s3c_usb_init_core(void)
  326. {
  327.     writel(PTXFE_HALF|NPTXFE_HALF|MODE_SLAVE|BURST_SINGLE|GBL_INT_UNMASK,
  328.         S5P_OTG_GAHBCFG);

  329.     writel( 0<<15        /* PHY Low Power Clock sel */
  330.         |1<<14        /* Non-Periodic TxFIFO Rewind Enable */
  331.         |0x5<<10    /* Turnaround time */
  332.         |0<<9        /* 0:HNP disable, 1:HNP enable */
  333.         |0<<8        /* 0:SRP disable, 1:SRP enable */
  334.         |0<<7        /* ULPI DDR sel */
  335.         |0<<6        /* 0: high speed utmi+, 1: full speed serial */
  336.         |0<<4        /* 0: utmi+, 1:ulpi */
  337.         |1<<3        /* phy i/f 0:8bit, 1:16bit */
  338.         |0x7<<0,    /* HS/FS Timeout**/
  339.         S5P_OTG_GUSBCFG );
  340. }

  341. void s3c_usb_check_current_mode(u8 *pucMode)
  342. {
  343.     u32 tmp;

  344.     tmp = readl(S5P_OTG_GINTSTS);
  345.     *pucMode = tmp & 0x1;
  346. }

  347. void s3c_usb_set_soft_disconnect(void)
  348. {
  349.     u32 tmp;

  350.     tmp = readl(S5P_OTG_DCTL);
  351.     tmp |= SOFT_DISCONNECT;
  352.     writel(tmp, S5P_OTG_DCTL);
  353. }

  354. void s3c_usb_clear_soft_disconnect(void)
  355. {
  356.     u32 tmp;

  357.     tmp = readl(S5P_OTG_DCTL);
  358.     tmp &= ~SOFT_DISCONNECT;
  359.     writel(tmp, S5P_OTG_DCTL);
  360. }

  361. void s3c_usb_init_device(void)
  362. {
  363.     writel(1<<18|otg.speed<<0, S5P_OTG_DCFG); /* [][1: full speed(30Mhz) 0:high speed]*/

  364.     writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|
  365.         INT_RESET|INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY,
  366.         S5P_OTG_GINTMSK);    /*gint unmask */
  367.     s3c_usb_phy_inited=1;
  368. }

  369. int s3c_usbctl_init(int ischeckstatus)/*diog, if not for check status, donot need wait cable insert*/
  370. {
  371.     u8 ucMode;

  372.     DBG_SETUP0("USB Control Init\n");
  373.     
  374.     otg.speed = speed;
  375.     otg.set_config = 0;
  376.     otg.ep0_state = EP0_STATE_INIT;
  377.     otg.ep0_substate = 0;
  378.     s3c_usb_init_phy();
  379.     s3c_usb_core_soft_reset();
  380.     if(!ischeckstatus)
  381.     s3c_usb_wait_cable_insert();
  382.     s3c_usb_init_core();
  383.     s3c_usb_check_current_mode(&ucMode);

  384.     if (ucMode == INT_DEV_MODE) {
  385.         s3c_usb_set_soft_disconnect();
  386.         udelay(10);
  387.         s3c_usb_clear_soft_disconnect();
  388.         s3c_usb_init_device();
  389.         return 0;
  390.     } else {
  391.         printf("Error : Current Mode is Host\n");
  392.         return 0;
  393.     }
  394. }

  395. int s3c_usbc_activate (void)
  396. {
  397.     /* dont used in usb high speed, but used in common file cmd_usbd.c */
  398.     return 0;
  399. }

  400. int s3c_usb_stop (void)
  401. {
  402.     /* dont used in usb high speed, but used in common file cmd_usbd.c */
  403.     s3c_usb_core_soft_reset();
  404.     s3c_usb_phy_off();
  405.     return 0;
  406. }

  407. void s3c_usb_print_pkt(u8 *pt, u8 count)
  408. {
  409.     int i;
  410.     printf("[s3c_usb_print_pkt:");

  411.     for(i=0;i<count;i++)
  412.         printf("%x,", pt[i]);

  413.     printf("]\n");
  414. }

  415. void s3c_usb_verify_checksum(void)
  416. {
  417.     u8 *cs_start, *cs_end;
  418.     u16 dnCS;
  419.     u16 checkSum;

  420.     printf("Checksum is being calculated.");

  421.     /* checksum calculation */
  422.     cs_start = (u8*)otg.dn_addr;
  423.     cs_end = (u8*)(otg.dn_addr+otg.dn_filesize-10);
  424.     checkSum = 0;
  425.     while(cs_start < cs_end) {
  426.         checkSum += *cs_start++;
  427.         if(((u32)cs_start&0xfffff)==0) printf(".");
  428.     }

  429.     /* fixed alignment fault in case when cs_end is odd. */
  430.     dnCS = (u16)((cs_end[1]<<8) + cs_end[0]);
  431.     //dnCS = *(u16 *)cs_end;

  432.     if (checkSum == dnCS)
  433.     {
  434.         printf("\nChecksum O.K.\n");
  435.     }
  436.     else
  437.     {
  438.         printf("\nChecksum Value => MEM:%x DNW:%x\n",checkSum,dnCS);
  439.         printf("Checksum failed.\n\n");
  440.     }

  441. }

  442. void s3c_usb_set_inep_xfersize(EP_TYPE type, u32 pktcnt, u32 xfersize)
  443. {
  444.     if(type == EP_TYPE_CONTROL)
  445.     {
  446.         writel((pktcnt<<19)|(xfersize<<0), S5P_OTG_DIEPTSIZ0);
  447.     }
  448.     else if(type == EP_TYPE_BULK)
  449.     {
  450.         writel((1<<29)|(pktcnt<<19)|(xfersize<<0), S5P_OTG_DIEPTSIZ_IN);
  451.     }
  452. }

  453. void s3c_usb_set_outep_xfersize(EP_TYPE type, u32 pktcnt, u32 xfersize)
  454. {
  455.     if(type == EP_TYPE_CONTROL)
  456.     {
  457.         writel((1<<29)|(pktcnt<<19)|(xfersize<<0), S5P_OTG_DOEPTSIZ0);
  458.     }
  459.     else if(type == EP_TYPE_BULK)
  460.     {
  461.         writel((pktcnt<<19)|(xfersize<<0), S5P_OTG_DOEPTSIZ_OUT);
  462.     }
  463. }

  464. void s3c_usb_write_ep0_fifo(u8 *buf, int num)
  465. {
  466.     int i;
  467.     u32 Wr_Data=0;

  468.     DBG_SETUP1("[s3c_usb_write_ep0_fifo:");

  469.     for(i=0;i<num;i+=4)
  470.     {
  471.         Wr_Data = ((*(buf+3))<<24)|((*(buf+2))<<16)|((*(buf+1))<<8)|*buf;
  472.         DBG_SETUP2(" 0x%08x,", Wr_Data);
  473.         writel(Wr_Data, S5P_OTG_EP0_FIFO);
  474.         buf += 4;
  475.     }

  476.     DBG_SETUP2("]\n");
  477. }


  478. void s3c_usb_write_in_fifo(u8 *buf, int num)
  479. {
  480.     int i;
  481.     u32 data=0;

  482.     for(i=0;i<num;i+=4)
  483.     {
  484.         data=((*(buf+3))<<24)|((*(buf+2))<<16)|((*(buf+1))<<8)|*buf;
  485.         writel(data, S5P_OTG_IN_FIFO);
  486.         buf += 4;
  487.     }
  488. }

  489. void s3c_usb_read_out_fifo(u8 *buf, int num)
  490. {
  491.     int i;
  492.     u32 data;

  493.     for (i=0;i<num;i+=4)
  494.     {
  495.         data = readl(S5P_OTG_OUT_FIFO);

  496.         buf[i] = (u8)data;
  497.         buf[i+1] = (u8)(data>>8);
  498.         buf[i+2] = (u8)(data>>16);
  499.         buf[i+3] = (u8)(data>>24);
  500.     }
  501. }

  502. void s3c_usb_get_desc(void)
  503. {
  504.     switch (otg.dev_req.wValue_H) {
  505.     case DEVICE_DESCRIPTOR:
  506.         otg.req_length = (u32)((otg.dev_req.wLength_H << 8) |
  507.             otg.dev_req.wLength_L);
  508.         DBG_SETUP1("DEVICE_DESCRIPTOR = 0x%x \n",otg.req_length);
  509.         otg.ep0_state = EP0_STATE_GD_DEV_0;
  510.         break;

  511.     case CONFIGURATION_DESCRIPTOR:
  512.         otg.req_length = (u32)((otg.dev_req.wLength_H << 8) |
  513.             otg.dev_req.wLength_L);
  514.         DBG_SETUP1("CONFIGURATION_DESCRIPTOR = 0x%x \n",otg.req_length);

  515.         /* GET_DESCRIPTOR:CONFIGURATION+INTERFACE+ENDPOINT0+ENDPOINT1 */
  516.         if (otg.req_length > CONFIG_DESC_SIZE){
  517.             otg.ep0_state = EP0_STATE_GD_CFG_0;
  518.         } else
  519.             otg.ep0_state = EP0_STATE_GD_CFG_ONLY_0;
  520.         break;

  521.     case STRING_DESCRIPTOR :
  522.         DBG_SETUP1("STRING_DESCRIPTOR \n");

  523.         switch(otg.dev_req.wValue_L) {
  524.         case 0:
  525.             otg.ep0_state = EP0_STATE_GD_STR_I0;
  526.             break;
  527.         case 1:
  528.             otg.ep0_state = EP0_STATE_GD_STR_I1;
  529.             break;
  530.         case 2:
  531.             otg.ep0_state = EP0_STATE_GD_STR_I2;
  532.             break;
  533.         default:
  534.             break;
  535.         }
  536.         break;

  537.     case ENDPOINT_DESCRIPTOR:
  538.         DBG_SETUP1("ENDPOINT_DESCRIPTOR \n");
  539.         switch(otg.dev_req.wValue_L&0xf) {
  540.         case 0:
  541.             otg.ep0_state=EP0_STATE_GD_EP0_ONLY_0;
  542.             break;
  543.         case 1:
  544.             otg.ep0_state=EP0_STATE_GD_EP1_ONLY_0;
  545.             break;
  546.         default:
  547.             break;
  548.         }
  549.         break;

  550.     case DEVICE_QUALIFIER:
  551.         otg.req_length = (u32)((otg.dev_req.wLength_H << 8) |
  552.             otg.dev_req.wLength_L);
  553.         DBG_SETUP1("DEVICE_QUALIFIER = 0x%x \n",otg.req_length);
  554.         otg.ep0_state = EP0_STATE_GD_DEV_QUALIFIER;
  555.         break;

  556.     case OTHER_SPEED_CONFIGURATION :
  557.         DBG_SETUP1("OTHER_SPEED_CONFIGURATION \n");
  558.         otg.req_length = (u32)((otg.dev_req.wLength_H << 8) |
  559.             otg.dev_req.wLength_L);
  560.         otg.ep0_state = EP0_STATE_GD_OTHER_SPEED;
  561.         break;

  562.     }
  563. }

  564. void s3c_usb_clear_feature(void)
  565. {
  566.     switch (otg.dev_req.bmRequestType) {
  567.     case DEVICE_RECIPIENT:
  568.         DBG_SETUP1("DEVICE_RECIPIENT \n");
  569.         if (otg.dev_req.wValue_L == 1)
  570.             remode_wakeup = FALSE;
  571.         break;

  572.     case ENDPOINT_RECIPIENT:
  573.         DBG_SETUP1("ENDPOINT_RECIPIENT \n");
  574.         if (otg.dev_req.wValue_L == 0) {
  575.             if ((otg.dev_req.wIndex_L & 0x7f) == CONTROL_EP)
  576.                 get_status.ep_ctrl= 0;

  577.             /* IN    Endpoint */
  578.             if ((otg.dev_req.wIndex_L & 0x7f) == BULK_IN_EP)
  579.                 get_status.ep_in= 0;

  580.             /* OUT Endpoint */
  581.             if ((otg.dev_req.wIndex_L & 0x7f) == BULK_OUT_EP)
  582.                 get_status.ep_out= 0;
  583.         }
  584.         break;

  585.     default:
  586.         DBG_SETUP1("\n");
  587.         break;
  588.     }
  589.     otg.ep0_state = EP0_STATE_INIT;

  590. }

  591. void s3c_usb_set_feature(void)
  592. {
  593.     u32 tmp;

  594.     switch (otg.dev_req.bmRequestType) {
  595.     case DEVICE_RECIPIENT:
  596.         DBG_SETUP1("DEVICE_RECIPIENT \n");
  597.         if (otg.dev_req.wValue_L == 1)
  598.             remode_wakeup = TRUE;
  599.             break;

  600.     case ENDPOINT_RECIPIENT:
  601.         DBG_SETUP1("ENDPOINT_RECIPIENT \n");
  602.         if (otg.dev_req.wValue_L == 0) {
  603.             if ((otg.dev_req.wIndex_L & 0x7f) == CONTROL_EP)
  604.                 get_status.ep_ctrl= 1;

  605.             if ((otg.dev_req.wIndex_L & 0x7f) == BULK_IN_EP)
  606.                 get_status.ep_in= 1;

  607.             if ((otg.dev_req.wIndex_L & 0x7f) == BULK_OUT_EP)
  608.                 get_status.ep_out= 1;
  609.         }
  610.         break;

  611.     default:
  612.         DBG_SETUP1("\n");
  613.         break;
  614.     }

  615.     switch (otg.dev_req.wValue_L) {
  616.     case EP_STALL:
  617.         /* TBD: additional processing if required */
  618.         break;

  619.     case TEST_MODE:
  620.         if ((0 != otg.dev_req.wIndex_L ) ||(0 != otg.dev_req.bmRequestType))
  621.             break;

  622.         /* Set TEST MODE*/
  623.         tmp = readl(S5P_OTG_DCTL);
  624.         tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_FORCE_ENABLE_MODE);
  625.         writel(tmp, S5P_OTG_DCTL);

  626.         switch(otg.dev_req.wIndex_H) {
  627.         case TEST_J:
  628.             /*Set Test J*/
  629.             tmp = readl(S5P_OTG_DCTL);
  630.             tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_J_MODE);
  631.             writel(tmp, S5P_OTG_DCTL);
  632.             break;

  633.         case TEST_K:
  634.             /*Set Test K*/
  635.             tmp = readl(S5P_OTG_DCTL);
  636.             tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_K_MODE);
  637.             writel(tmp, S5P_OTG_DCTL);
  638.             break;

  639.         case TEST_SE0_NAK:
  640.             /*Set Test SE0NAK*/
  641.             tmp = readl(S5P_OTG_DCTL);
  642.             tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_SE0_NAK_MODE);
  643.             writel(tmp, S5P_OTG_DCTL);
  644.             break;

  645.         case TEST_PACKET:
  646.             DBG_SETUP1 ("Test_packet\n");
  647.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  648.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, TEST_PKT_SIZE);
  649.             s3c_usb_write_ep0_fifo(test_pkt, TEST_PKT_SIZE);
  650.             tmp = readl(S5P_OTG_DCTL);
  651.             tmp = (tmp & ~(TEST_CONTROL_FIELD)) | (TEST_PACKET_MODE);
  652.             writel(tmp, S5P_OTG_DCTL);
  653.             DBG_SETUP1 ("S5P_OTG_DCTL=0x%08x\n", tmp);
  654.             break;
  655.         }
  656.         break;

  657.     default:
  658.         break;
  659.     }
  660.     otg.ep0_state = EP0_STATE_INIT;

  661. }

  662. void s3c_usb_get_status(void)
  663. {
  664.     switch(otg.dev_req.bmRequestType) {
  665.     case (0x80):    /*device */
  666.         DBG_SETUP1("DEVICE\n");
  667.         get_status.Device=((u8)remode_wakeup<<1)|0x1; /* SelfPowered */
  668.         otg.ep0_state = EP0_STATE_GET_STATUS0;
  669.         break;

  670.     case (0x81):    /*interface */
  671.         DBG_SETUP1("INTERFACE\n");
  672.         get_status.Interface=0;
  673.         otg.ep0_state = EP0_STATE_GET_STATUS1;
  674.         break;

  675.     case (0x82):    /*endpoint */
  676.         DBG_SETUP1("ENDPOINT\n");
  677.         if ((otg.dev_req.wIndex_L & 0x7f) == CONTROL_EP)
  678.             otg.ep0_state = EP0_STATE_GET_STATUS2;

  679.         if ((otg.dev_req.wIndex_L & 0x7f) == BULK_IN_EP)
  680.             otg.ep0_state = EP0_STATE_GET_STATUS3;

  681.         if ((otg.dev_req.wIndex_L & 0x7f) == BULK_OUT_EP)
  682.             otg.ep0_state = EP0_STATE_GET_STATUS4;
  683.         break;

  684.     default:
  685.         DBG_SETUP1("\n");
  686.         break;
  687.     }

  688. }

  689. void s3c_usb_ep0_int_hndlr(void)
  690. {
  691.     u16 i;
  692.     u32 buf[2]={0x0000, };
  693.     u16 addr;

  694.     DBG_SETUP0("Event EP0\n");

  695.     if (otg.ep0_state == EP0_STATE_INIT) {

  696.         for(i=0;i<2;i++)
  697.             buf[i] = readl(S5P_OTG_EP0_FIFO);

  698.         otg.dev_req.bmRequestType = buf[0];
  699.         otg.dev_req.bRequest    = buf[0]>>8;
  700.         otg.dev_req.wValue_L    = buf[0]>>16;
  701.         otg.dev_req.wValue_H    = buf[0]>>24;
  702.         otg.dev_req.wIndex_L    = buf[1];
  703.         otg.dev_req.wIndex_H    = buf[1]>>8;
  704.         otg.dev_req.wLength_L    = buf[1]>>16;
  705.         otg.dev_req.wLength_H    = buf[1]>>24;

  706. #ifdef USB_OTG_DEBUG_SETUP
  707.         s3c_usb_print_pkt((u8 *)&otg.dev_req, 8);
  708. #endif

  709.         switch (otg.dev_req.bRequest) {
  710.         case STANDARD_SET_ADDRESS:
  711.             /* Set Address Update bit */
  712.             addr = (otg.dev_req.wValue_L);
  713.             writel(1<<18|addr<<4|otg.speed<<0, S5P_OTG_DCFG);
  714.             DBG_SETUP1("S5P_OTG_DCFG : %x, STANDARD_SET_ADDRESS : %d\n",
  715.                     readl(S5P_OTG_DCFG), addr);
  716.             otg.ep0_state = EP0_STATE_INIT;
  717.             break;

  718.         case STANDARD_SET_DESCRIPTOR:
  719.             DBG_SETUP1("STANDARD_SET_DESCRIPTOR \n");
  720.             break;

  721.         case STANDARD_SET_CONFIGURATION:
  722.             DBG_SETUP1("STANDARD_SET_CONFIGURATION \n");
  723.             /* Configuration value in configuration descriptor */
  724.             config_value = otg.dev_req.wValue_L;
  725.             otg.set_config = 1;
  726.             otg.ep0_state = EP0_STATE_INIT;
  727.             break;

  728.         case STANDARD_GET_CONFIGURATION:
  729.             DBG_SETUP1("STANDARD_GET_CONFIGURATION \n");
  730.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);

  731.             /*ep0 enable, clear nak, next ep0, 8byte */
  732.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  733.             writel(config_value, S5P_OTG_EP0_FIFO);
  734.             otg.ep0_state = EP0_STATE_INIT;
  735.             break;

  736.         case STANDARD_GET_DESCRIPTOR:
  737.             DBG_SETUP1("STANDARD_GET_DESCRIPTOR :");
  738.             s3c_usb_get_desc();
  739.             break;

  740.         case STANDARD_CLEAR_FEATURE:
  741.             DBG_SETUP1("STANDARD_CLEAR_FEATURE :");
  742.             s3c_usb_clear_feature();
  743.             break;

  744.         case STANDARD_SET_FEATURE:
  745.             DBG_SETUP1("STANDARD_SET_FEATURE :");
  746.             s3c_usb_set_feature();
  747.             break;

  748.         case STANDARD_GET_STATUS:
  749.             DBG_SETUP1("STANDARD_GET_STATUS :");
  750.             s3c_usb_get_status();
  751.             break;

  752.         case STANDARD_GET_INTERFACE:
  753.             DBG_SETUP1("STANDARD_GET_INTERFACE \n");
  754.             otg.ep0_state = EP0_STATE_INTERFACE_GET;
  755.             break;

  756.         case STANDARD_SET_INTERFACE:
  757.             DBG_SETUP1("STANDARD_SET_INTERFACE \n");
  758.             get_intf.AlternateSetting= otg.dev_req.wValue_L;
  759.             otg.ep0_state = EP0_STATE_INIT;
  760.             break;

  761.         case STANDARD_SYNCH_FRAME:
  762.             DBG_SETUP1("STANDARD_SYNCH_FRAME \n");
  763.             otg.ep0_state = EP0_STATE_INIT;
  764.             break;

  765.         default:
  766.             break;
  767.         }
  768.     }

  769.     s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.ctrl_max_pktsize);

  770.     if(otg.speed == USB_HIGH) {
  771.         /*clear nak, next ep0, 64byte */
  772.         writel(((1<<26)|(CONTROL_EP<<11)|(0<<0)), S5P_OTG_DIEPCTL0);
  773.     }
  774.     else {
  775.         /*clear nak, next ep0, 8byte */
  776.         writel(((1<<26)|(CONTROL_EP<<11)|(3<<0)), S5P_OTG_DIEPCTL0);
  777.     }

  778. }

  779. void s3c_usb_set_otherspeed_conf_desc(u32 length)
  780. {
  781.     /* Standard device descriptor */
  782.     if (otg.speed == USB_HIGH)
  783.     {
  784.      if (length ==9)
  785.      {
  786.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 9);
  787.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  788.             s3c_usb_write_ep0_fifo(((u8 *)&config_full)+0, 9);
  789.         }
  790.      else if(length ==32)
  791.         {
  792.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 32);
  793.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  794.             s3c_usb_write_ep0_fifo(((u8 *)&config_full_total)+0, 32);

  795.      }
  796.         otg.ep0_state = EP0_STATE_INIT;
  797.     }
  798.     else
  799.     {
  800.      if (length ==9)
  801.      {
  802.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  803.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  804.             s3c_usb_write_ep0_fifo(((u8 *)&config_high)+0, 8);
  805.         }
  806.      else if(length ==32)
  807.         {
  808.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  809.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  810.             s3c_usb_write_ep0_fifo(((u8 *)&config_high_total)+0, 8);
  811.      }
  812.         otg.ep0_state = EP0_STATE_GD_OTHER_SPEED_HIGH_1;
  813.     }
  814. }

  815. void s3c_usb_transfer_ep0(void)
  816. {

  817.     DBG_SETUP0("otg.ep0_state = %d\n", otg.ep0_state);

  818.     switch (otg.ep0_state) {
  819.     case EP0_STATE_INIT:
  820.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 0);

  821.         /*ep0 enable, clear nak, next ep0, 8byte */
  822.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  823.         DBG_SETUP1("EP0_STATE_INIT\n");
  824.         break;

  825.     /* GET_DESCRIPTOR:DEVICE */
  826.     case EP0_STATE_GD_DEV_0:
  827.         DBG_SETUP1("EP0_STATE_GD_DEV_0 :");
  828.         if (otg.speed == USB_HIGH) {
  829.             DBG_SETUP1("High Speed\n");

  830.             /*ep0 enable, clear nak, next ep0, max 64byte */
  831.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  832.             if (otg.req_length < DEVICE_DESC_SIZE) {
  833.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  834.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+0, otg.req_length);
  835.             } else {
  836.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, DEVICE_DESC_SIZE);
  837.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+0, DEVICE_DESC_SIZE);
  838.             }
  839.             otg.ep0_state = EP0_STATE_INIT;
  840.         } else {
  841.             DBG_SETUP1("Full Speed\n");
  842.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  843.             if(otg.req_length<DEVICE_DESC_SIZE) {
  844.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  845.             } else {
  846.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, DEVICE_DESC_SIZE);
  847.             }

  848.             if(otg.req_length<FS_CTRL_PKT_SIZE) {
  849.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+0, otg.req_length);
  850.                 otg.ep0_state = EP0_STATE_INIT;
  851.             } else {
  852.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+0, FS_CTRL_PKT_SIZE);
  853.                 otg.ep0_state = EP0_STATE_GD_DEV_1;
  854.             }
  855.         }
  856.         break;

  857.     case EP0_STATE_GD_DEV_1:
  858.         DBG_SETUP1("EP0_STATE_GD_DEV_1\n");
  859.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  860.         if(otg.req_length<(2*FS_CTRL_PKT_SIZE)) {
  861.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+FS_CTRL_PKT_SIZE,
  862.                         (otg.req_length-FS_CTRL_PKT_SIZE));
  863.             otg.ep0_state = EP0_STATE_INIT;
  864.         } else {
  865.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+FS_CTRL_PKT_SIZE,
  866.                         FS_CTRL_PKT_SIZE);
  867.             otg.ep0_state = EP0_STATE_GD_DEV_2;
  868.         }
  869.         break;

  870.     case EP0_STATE_GD_DEV_2:
  871.         DBG_SETUP1("EP0_STATE_GD_DEV_2\n");
  872.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  873.         if(otg.req_length<DEVICE_DESC_SIZE) {
  874.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+(2*FS_CTRL_PKT_SIZE),
  875.                         (otg.req_length-2*FS_CTRL_PKT_SIZE));
  876.         } else {
  877.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.dev))+(2*FS_CTRL_PKT_SIZE),
  878.                         (DEVICE_DESC_SIZE-2*FS_CTRL_PKT_SIZE));
  879.         }
  880.         otg.ep0_state = EP0_STATE_INIT;
  881.         break;

  882.     /* GET_DESCRIPTOR:CONFIGURATION+INTERFACE+ENDPOINT0+ENDPOINT1 */
  883.     case EP0_STATE_GD_CFG_0:
  884.         DBG_SETUP1("EP0_STATE_GD_CFG_0 :");
  885.         if (otg.speed == USB_HIGH)
  886.         {
  887.             DBG_SETUP1("High Speed\n");
  888.             writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  889.             if(otg.req_length<CONFIG_DESC_TOTAL_SIZE)
  890.             {
  891.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  892.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, otg.req_length);
  893.             }
  894.             else
  895.             {
  896.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, CONFIG_DESC_TOTAL_SIZE);
  897.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, CONFIG_DESC_TOTAL_SIZE);
  898.             }
  899.             otg.ep0_state = EP0_STATE_INIT;
  900.         }
  901.         else
  902.         {
  903.             DBG_SETUP1("Full Speed\n");
  904.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  905.             if(otg.req_length<CONFIG_DESC_TOTAL_SIZE)
  906.             {
  907.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  908.             }
  909.             else
  910.             {
  911.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, CONFIG_DESC_TOTAL_SIZE);
  912.             }
  913.             if(otg.req_length<FS_CTRL_PKT_SIZE)
  914.             {
  915.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, otg.req_length);
  916.                 otg.ep0_state = EP0_STATE_INIT;
  917.             }
  918.             else
  919.             {
  920.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, FS_CTRL_PKT_SIZE);
  921.                 otg.ep0_state = EP0_STATE_GD_CFG_1;
  922.             }
  923.         }
  924.         break;

  925.     case EP0_STATE_GD_CFG_1:
  926.         DBG_SETUP1("EP0_STATE_GD_CFG_1\n");
  927.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  928.         if(otg.req_length<(2*FS_CTRL_PKT_SIZE))
  929.         {
  930.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+FS_CTRL_PKT_SIZE,
  931.                         (otg.req_length-FS_CTRL_PKT_SIZE));
  932.             otg.ep0_state = EP0_STATE_INIT;
  933.         }
  934.         else
  935.         {
  936.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+FS_CTRL_PKT_SIZE,
  937.                         FS_CTRL_PKT_SIZE);
  938.             otg.ep0_state = EP0_STATE_GD_CFG_2;
  939.         }
  940.         break;

  941.     case EP0_STATE_GD_CFG_2:
  942.         DBG_SETUP1("EP0_STATE_GD_CFG_2\n");
  943.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  944.         if(otg.req_length<(3*FS_CTRL_PKT_SIZE))
  945.         {
  946.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+(2*FS_CTRL_PKT_SIZE),
  947.                         (otg.req_length-2*FS_CTRL_PKT_SIZE));
  948.             otg.ep0_state = EP0_STATE_INIT;
  949.         }
  950.         else
  951.         {
  952.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+(2*FS_CTRL_PKT_SIZE),
  953.                         FS_CTRL_PKT_SIZE);
  954.             otg.ep0_state = EP0_STATE_GD_CFG_3;
  955.         }
  956.         break;

  957.     case EP0_STATE_GD_CFG_3:
  958.         DBG_SETUP1("EP0_STATE_GD_CFG_3\n");
  959.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  960.         if(otg.req_length<(4*FS_CTRL_PKT_SIZE))
  961.         {
  962.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+(3*FS_CTRL_PKT_SIZE),
  963.                         (otg.req_length-3*FS_CTRL_PKT_SIZE));
  964.             otg.ep0_state = EP0_STATE_INIT;
  965.         }
  966.         else
  967.         {
  968.             s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+(3*FS_CTRL_PKT_SIZE),
  969.                         FS_CTRL_PKT_SIZE);
  970.             otg.ep0_state = EP0_STATE_GD_CFG_4;
  971.         }
  972.         break;

  973.     case EP0_STATE_GD_CFG_4:
  974.         DBG_SETUP1("EP0_STATE_GD_CFG_4\n");
  975.         otg.ep0_state = EP0_STATE_INIT;
  976.         break;

  977.     case EP0_STATE_GD_DEV_QUALIFIER:    /*only supported in USB 2.0*/
  978.         DBG_SETUP1("EP0_STATE_GD_DEV_QUALIFIER\n");
  979.         writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  980.         if(otg.req_length<10)
  981.         {
  982.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  983.             s3c_usb_write_ep0_fifo((u8 *)qualifier_desc+0, otg.req_length);
  984.         }
  985.         else
  986.         {
  987.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 10);
  988.             s3c_usb_write_ep0_fifo((u8 *)qualifier_desc+0, 10);
  989.         }
  990.         otg.ep0_state = EP0_STATE_INIT;
  991.         break;

  992.     case EP0_STATE_GD_OTHER_SPEED:
  993.             DBG_SETUP1("EP0_STATE_GD_OTHER_SPEED\n");
  994.             s3c_usb_set_otherspeed_conf_desc(otg.req_length);
  995.             break;

  996.     case EP0_STATE_GD_OTHER_SPEED_HIGH_1:
  997.         DBG_SETUP1("EP0_STATE_GD_OTHER_SPEED_HIGH_1\n");
  998.         if(otg.req_length==9)
  999.         {
  1000.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1001.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1002.                 s3c_usb_write_ep0_fifo(((u8 *)&config_high)+8, 1);
  1003.                 otg.ep0_state = EP0_STATE_INIT;
  1004.         }
  1005.         else
  1006.         {
  1007.             s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  1008.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1009.                 s3c_usb_write_ep0_fifo(((u8 *)&config_high)+8, 8);
  1010.                 otg.ep0_state = EP0_STATE_GD_OTHER_SPEED_HIGH_2;
  1011.         }
  1012.             break;

  1013.     case EP0_STATE_GD_OTHER_SPEED_HIGH_2:
  1014.         DBG_SETUP1("EP0_STATE_GD_OTHER_SPEED_HIGH_2\n");
  1015.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  1016.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1017.             s3c_usb_write_ep0_fifo(((u8 *)&config_high)+16, 8);
  1018.             otg.ep0_state = EP0_STATE_GD_OTHER_SPEED_HIGH_3;
  1019.             break;

  1020.     case EP0_STATE_GD_OTHER_SPEED_HIGH_3:
  1021.         DBG_SETUP1("EP0_STATE_GD_OTHER_SPEED_HIGH_3\n");
  1022.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 8);
  1023.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1024.             s3c_usb_write_ep0_fifo(((u8 *)&config_high)+24, 8);
  1025.             otg.ep0_state = EP0_STATE_INIT;
  1026.             break;

  1027.     /* GET_DESCRIPTOR:CONFIGURATION ONLY*/
  1028.     case EP0_STATE_GD_CFG_ONLY_0:
  1029.         DBG_SETUP1("EP0_STATE_GD_CFG_ONLY_0:");
  1030.         if (otg.speed == USB_HIGH)
  1031.         {
  1032.             DBG_SETUP1("High Speed\n");
  1033.             if(otg.req_length<CONFIG_DESC_SIZE)
  1034.             {
  1035.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  1036.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1037.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, otg.req_length);
  1038.             }
  1039.             else
  1040.             {
  1041.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, CONFIG_DESC_SIZE);
  1042.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1043.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0,
  1044.                             CONFIG_DESC_SIZE);
  1045.             }
  1046.             otg.ep0_state = EP0_STATE_INIT;
  1047.         }
  1048.         else
  1049.         {
  1050.             DBG_SETUP1("Full Speed\n");
  1051.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1052.             if(otg.req_length<CONFIG_DESC_SIZE)
  1053.             {
  1054.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  1055.             }
  1056.             else
  1057.             {
  1058.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, CONFIG_DESC_SIZE);
  1059.             }
  1060.             if(otg.req_length<FS_CTRL_PKT_SIZE)
  1061.             {
  1062.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, otg.req_length);
  1063.                 otg.ep0_state = EP0_STATE_INIT;
  1064.             }
  1065.             else
  1066.             {
  1067.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+0, FS_CTRL_PKT_SIZE);
  1068.                 otg.ep0_state = EP0_STATE_GD_CFG_ONLY_1;
  1069.             }
  1070.         }
  1071.         break;

  1072.     case EP0_STATE_GD_CFG_ONLY_1:
  1073.         DBG_SETUP1("EP0_STATE_GD_CFG_ONLY_1\n");
  1074.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1075.         s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.config))+FS_CTRL_PKT_SIZE,
  1076.                     (CONFIG_DESC_SIZE-FS_CTRL_PKT_SIZE));
  1077.         otg.ep0_state = EP0_STATE_INIT;
  1078.         break;

  1079.     /* GET_DESCRIPTOR:INTERFACE ONLY */

  1080.     case EP0_STATE_GD_IF_ONLY_0:
  1081.         DBG_SETUP1("EP0_STATE_GD_IF_ONLY_0 :");
  1082.         if (otg.speed == USB_HIGH)
  1083.         {
  1084.             DBG_SETUP1("High Speed\n");
  1085.             if(otg.req_length<INTERFACE_DESC_SIZE)
  1086.             {
  1087.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  1088.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1089.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+0, otg.req_length);
  1090.             }
  1091.             else
  1092.             {
  1093.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, INTERFACE_DESC_SIZE);
  1094.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1095.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+0, INTERFACE_DESC_SIZE);
  1096.             }
  1097.             otg.ep0_state = EP0_STATE_INIT;
  1098.         }
  1099.         else
  1100.         {
  1101.             DBG_SETUP1("Full Speed\n");
  1102.             writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1103.             if(otg.req_length<INTERFACE_DESC_SIZE)
  1104.             {
  1105.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, otg.req_length);
  1106.             }
  1107.             else
  1108.             {
  1109.                 s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, INTERFACE_DESC_SIZE);
  1110.             }
  1111.             if(otg.req_length<FS_CTRL_PKT_SIZE)
  1112.             {
  1113.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+0, otg.req_length);
  1114.                 otg.ep0_state = EP0_STATE_INIT;
  1115.             }
  1116.             else
  1117.             {
  1118.                 s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+0, FS_CTRL_PKT_SIZE);
  1119.                 otg.ep0_state = EP0_STATE_GD_IF_ONLY_1;
  1120.             }
  1121.         }
  1122.         break;

  1123.     case EP0_STATE_GD_IF_ONLY_1:
  1124.         DBG_SETUP1("EP0_STATE_GD_IF_ONLY_1\n");
  1125.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1126.         s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.intf))+FS_CTRL_PKT_SIZE,
  1127.                     (INTERFACE_DESC_SIZE-FS_CTRL_PKT_SIZE));
  1128.         otg.ep0_state = EP0_STATE_INIT;
  1129.         break;


  1130.     /* GET_DESCRIPTOR:ENDPOINT 1 ONLY */
  1131.     case EP0_STATE_GD_EP0_ONLY_0:
  1132.         DBG_SETUP1("EP0_STATE_GD_EP0_ONLY_0\n");
  1133.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1134.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, ENDPOINT_DESC_SIZE);
  1135.         s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.ep1))+0, ENDPOINT_DESC_SIZE);
  1136.         otg.ep0_state = EP0_STATE_INIT;
  1137.         break;

  1138.     /* GET_DESCRIPTOR:ENDPOINT 2 ONLY */
  1139.     case EP0_STATE_GD_EP1_ONLY_0:
  1140.         DBG_SETUP1("EP0_STATE_GD_EP1_ONLY_0\n");
  1141.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, ENDPOINT_DESC_SIZE);
  1142.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1143.         s3c_usb_write_ep0_fifo(((u8 *)&(otg.desc.ep2))+0, ENDPOINT_DESC_SIZE);
  1144.         otg.ep0_state = EP0_STATE_INIT;
  1145.         break;

  1146.     /* GET_DESCRIPTOR:STRING */
  1147.     case EP0_STATE_GD_STR_I0:
  1148.         DBG_SETUP1("EP0_STATE_GD_STR_I0\n");
  1149.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, STRING_DESC0_SIZE);
  1150.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1151.         s3c_usb_write_ep0_fifo((u8 *)string_desc0, STRING_DESC0_SIZE);
  1152.         otg.ep0_state = EP0_STATE_INIT;
  1153.         break;

  1154.     case EP0_STATE_GD_STR_I1:
  1155.         DBG_SETUP1("EP0_STATE_GD_STR_I1 %d\n", otg.ep0_substate);
  1156.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, sizeof(string_desc1));
  1157.         if ((otg.ep0_substate*otg.ctrl_max_pktsize+otg.ctrl_max_pktsize)
  1158.             < sizeof(string_desc1)) {

  1159.             if (otg.speed == USB_HIGH)
  1160.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1161.             else
  1162.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1163.             s3c_usb_write_ep0_fifo((u8 *)string_desc1+(otg.ep0_substate*otg.ctrl_max_pktsize),
  1164.                         otg.ctrl_max_pktsize);
  1165.             otg.ep0_state = EP0_STATE_GD_STR_I1;
  1166.             otg.ep0_substate++;
  1167.         } else {
  1168.             if (otg.speed == USB_HIGH)
  1169.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1170.             else
  1171.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1172.             s3c_usb_write_ep0_fifo((u8 *)string_desc1+(otg.ep0_substate*otg.ctrl_max_pktsize),
  1173.                         sizeof(string_desc1)-(otg.ep0_substate*otg.ctrl_max_pktsize));
  1174.             otg.ep0_state = EP0_STATE_INIT;
  1175.             otg.ep0_substate = 0;
  1176.         }
  1177.         break;

  1178.     case EP0_STATE_GD_STR_I2:
  1179.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, sizeof(string_desc2));
  1180.         if ((otg.ep0_substate*otg.ctrl_max_pktsize+otg.ctrl_max_pktsize)
  1181.             < sizeof(string_desc2)) {

  1182.             if (otg.speed == USB_HIGH)
  1183.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1184.             else
  1185.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1186.             s3c_usb_write_ep0_fifo((u8 *)string_desc2+(otg.ep0_substate*otg.ctrl_max_pktsize),
  1187.                         otg.ctrl_max_pktsize);
  1188.             otg.ep0_state = EP0_STATE_GD_STR_I2;
  1189.             otg.ep0_substate++;
  1190.         } else {
  1191.             if (otg.speed == USB_HIGH)
  1192.                 writel(EPEN_CNAK_EP0_64, S5P_OTG_DIEPCTL0);
  1193.             else
  1194.                 writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1195.             s3c_usb_write_ep0_fifo((u8 *)string_desc2+(otg.ep0_substate*otg.ctrl_max_pktsize),
  1196.                         sizeof(string_desc2)-(otg.ep0_substate*otg.ctrl_max_pktsize));
  1197.             otg.ep0_state = EP0_STATE_INIT;
  1198.             otg.ep0_substate = 0;
  1199.         }
  1200.         DBG_SETUP1("EP0_STATE_GD_STR_I2 %d", otg.ep0_substate);
  1201.         break;

  1202.     case EP0_STATE_INTERFACE_GET:
  1203.         DBG_SETUP1("EP0_STATE_INTERFACE_GET\n");
  1204.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1205.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1206.         s3c_usb_write_ep0_fifo((u8 *)&get_intf+0, 1);
  1207.         otg.ep0_state = EP0_STATE_INIT;
  1208.         break;


  1209.     case EP0_STATE_GET_STATUS0:
  1210.         DBG_SETUP1("EP0_STATE_GET_STATUS0\n");
  1211.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1212.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1213.         s3c_usb_write_ep0_fifo((u8 *)&get_status+0, 1);
  1214.         otg.ep0_state = EP0_STATE_INIT;
  1215.         break;

  1216.     case EP0_STATE_GET_STATUS1:
  1217.         DBG_SETUP1("EP0_STATE_GET_STATUS1\n");
  1218.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1219.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1220.         s3c_usb_write_ep0_fifo((u8 *)&get_status+1, 1);
  1221.         otg.ep0_state = EP0_STATE_INIT;
  1222.         break;

  1223.     case EP0_STATE_GET_STATUS2:
  1224.         DBG_SETUP1("EP0_STATE_GET_STATUS2\n");
  1225.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1226.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1227.         s3c_usb_write_ep0_fifo((u8 *)&get_status+2, 1);
  1228.         otg.ep0_state = EP0_STATE_INIT;
  1229.         break;

  1230.     case EP0_STATE_GET_STATUS3:
  1231.         DBG_SETUP1("EP0_STATE_GET_STATUS3\n");
  1232.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1233.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1234.         s3c_usb_write_ep0_fifo((u8 *)&get_status+3, 1);
  1235.         otg.ep0_state = EP0_STATE_INIT;
  1236.         break;

  1237.     case EP0_STATE_GET_STATUS4:
  1238.         DBG_SETUP1("EP0_STATE_GET_STATUS4\n");
  1239.         s3c_usb_set_inep_xfersize(EP_TYPE_CONTROL, 1, 1);
  1240.         writel(EPEN_CNAK_EP0_8, S5P_OTG_DIEPCTL0);
  1241.         s3c_usb_write_ep0_fifo((u8 *)&get_status+4, 1);
  1242.         otg.ep0_state = EP0_STATE_INIT;
  1243.         break;

  1244.     default:
  1245.         break;
  1246.     }
  1247. }


  1248. void s3c_usb_int_bulkin(void)
  1249. {
  1250.     u8* bulkin_buf;
  1251.     u32 remain_cnt;

  1252.     DBG_BULK0("Bulk In Function\n");

  1253.     bulkin_buf = (u8*)otg.up_ptr;
  1254.     remain_cnt = otg.up_size- ((u32)otg.up_ptr - otg.up_addr);
  1255.     DBG_BULK1("bulkin_buf = 0x%x,remain_cnt = 0x%x \n", bulkin_buf, remain_cnt);

  1256.     if (remain_cnt > otg.bulkin_max_pktsize) {
  1257.         s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1, otg.bulkin_max_pktsize);

  1258.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1259.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkin_max_pktsize<<0,
  1260.             S5P_OTG_DIEPCTL_IN);

  1261.         s3c_usb_write_in_fifo(bulkin_buf, otg.bulkin_max_pktsize);

  1262.         otg.up_ptr += otg.bulkin_max_pktsize;

  1263.     } else if(remain_cnt > 0) {
  1264.         s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1, remain_cnt);

  1265.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1266.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkin_max_pktsize<<0,
  1267.             S5P_OTG_DIEPCTL_IN);

  1268.         s3c_usb_write_in_fifo(bulkin_buf, remain_cnt);

  1269.         otg.up_ptr += remain_cnt;

  1270.     } else { /*remain_cnt = 0*/
  1271.         writel((DEPCTL_SNAK|DEPCTL_BULK_TYPE), S5P_OTG_DIEPCTL_IN);
  1272.     }
  1273. }

  1274. void s3c_usb_upload_start(void)
  1275. {
  1276.     u8 tmp_buf[12];
  1277.     u32 check;

  1278.     s3c_usb_read_out_fifo((u8 *)tmp_buf, 10);
  1279.     check = *((u8 *)(tmp_buf+8)) + (*((u8 *)(tmp_buf+9))<<8);

  1280.     if (check==0x1) {
  1281.         otg.up_addr =
  1282.             *((u8 *)(tmp_buf+0))+
  1283.             (*((u8 *)(tmp_buf+1))<<8)+
  1284.             (*((u8 *)(tmp_buf+2))<<16)+
  1285.             (*((u8 *)(tmp_buf+3))<<24);

  1286.         otg.up_size =
  1287.             *((u8 *)(tmp_buf+4))+
  1288.             (*((u8 *)(tmp_buf+5))<<8)+
  1289.             (*((u8 *)(tmp_buf+6))<<16)+
  1290.             (*((u8 *)(tmp_buf+7))<<24);

  1291.         otg.up_ptr=(u8 *)otg.up_addr;
  1292.         DBG_BULK1("UploadAddress : 0x%x, UploadSize: %d\n",
  1293.             otg.up_addr, otg.up_size);

  1294.         if (otg.op_mode == USB_CPU) {
  1295.             if (otg.up_size > otg.bulkin_max_pktsize) {
  1296.                 s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1,
  1297.                     otg.bulkin_max_pktsize);
  1298.             } else {
  1299.                 s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1,
  1300.                     otg.up_size);
  1301.             }

  1302.             /*ep1 enable, clear nak, bulk, usb active, max pkt 64*/
  1303.             writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkin_max_pktsize<<0,
  1304.                 S5P_OTG_DIEPCTL_IN);
  1305.         } else if ((otg.op_mode == USB_DMA) && (otg.up_size > 0)) {
  1306.             u32 pktcnt, remainder;

  1307.             DBG_BULK1("Dma Start for IN PKT \n");

  1308.             writel(MODE_DMA|BURST_INCR4|GBL_INT_UNMASK,
  1309.                 S5P_OTG_GAHBCFG);
  1310.             writel(INT_RESUME|INT_OUT_EP|INT_IN_EP| INT_ENUMDONE|
  1311.                 INT_RESET|INT_SUSPEND, S5P_OTG_GINTMSK);

  1312.             writel((u32)otg.up_ptr, S5P_OTG_DIEPDMA_IN);

  1313.             pktcnt = (u32)(otg.up_size/otg.bulkin_max_pktsize);
  1314.             remainder = (u32)(otg.up_size%otg.bulkin_max_pktsize);
  1315.             if(remainder != 0) {
  1316.                 pktcnt += 1;
  1317.             }

  1318.             if (pktcnt > 1023) {
  1319.                 s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1023,
  1320.                     (otg.bulkin_max_pktsize*1023));
  1321.             } else {
  1322.                 s3c_usb_set_inep_xfersize(EP_TYPE_BULK, pktcnt,
  1323.                     otg.up_size);
  1324.             }

  1325.             /*ep1 enable, clear nak, bulk, usb active, next ep1, max pkt */
  1326.             writel(1u<<31|1<<26|2<<18|1<<15|BULK_IN_EP<<11|
  1327.                 otg.bulkin_max_pktsize<<0,
  1328.                 S5P_OTG_DIEPCTL_IN);
  1329.         }
  1330.     }
  1331.     otg.dn_filesize=0;
  1332. }

  1333. void s3c_usb_download_start(u32 fifo_cnt_byte)
  1334. {
  1335.     u8 tmp_buf[8];

  1336.     s3c_usb_read_out_fifo((u8 *)tmp_buf, 8);
  1337.     DBG_BULK1("downloadFileSize==0, 1'st BYTE_READ_CNT_REG : %x\n",
  1338.         fifo_cnt_byte);

  1339.     otg.dn_addr=s3c_usbd_dn_addr;
  1340.     otg.dn_filesize=
  1341.         *((u8 *)(tmp_buf+4))+
  1342.         (*((u8 *)(tmp_buf+5))<<8)+
  1343.         (*((u8 *)(tmp_buf+6))<<16)+
  1344.         (*((u8 *)(tmp_buf+7))<<24);

  1345.     otg.dn_ptr=(u8 *)otg.dn_addr;
  1346.     DBG_BULK1("downloadAddress : 0x%x, downloadFileSize: %x\n",
  1347.         otg.dn_addr, otg.dn_filesize);

  1348.     /* The first 8-bytes are deleted.*/
  1349.     s3c_usb_read_out_fifo((u8 *)otg.dn_ptr, fifo_cnt_byte-8);
  1350.     otg.dn_ptr += fifo_cnt_byte-8;

  1351.     if (otg.op_mode == USB_CPU) {
  1352.         s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1,
  1353.             otg.bulkout_max_pktsize);

  1354.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1355.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1356.         S5P_OTG_DOEPCTL_OUT);
  1357.     } else if (otg.dn_filesize>otg.bulkout_max_pktsize) {
  1358.         u32 pkt_cnt, remain_cnt;

  1359.         DBG_BULK1("downloadFileSize!=0, Dma Start for 2nd OUT PKT \n");
  1360.         writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|
  1361.             INT_RESET|INT_SUSPEND, S5P_OTG_GINTMSK); /*gint unmask */
  1362.         writel(MODE_DMA|BURST_INCR4|GBL_INT_UNMASK,
  1363.             S5P_OTG_GAHBCFG);
  1364.         writel((u32)otg.dn_ptr, S5P_OTG_DOEPDMA_OUT);
  1365.         pkt_cnt = (u32)(otg.dn_filesize-otg.bulkout_max_pktsize)/otg.bulkout_max_pktsize;
  1366.         remain_cnt = (u32)((otg.dn_filesize-otg.bulkout_max_pktsize)%otg.bulkout_max_pktsize);
  1367.         if(remain_cnt != 0) {
  1368.             pkt_cnt += 1;
  1369.         }

  1370.         if (pkt_cnt > 1023) {
  1371.             s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1023,
  1372.                 (otg.bulkout_max_pktsize*1023));
  1373.         } else {
  1374.             s3c_usb_set_outep_xfersize(EP_TYPE_BULK, pkt_cnt,
  1375.                 (otg.dn_filesize-otg.bulkout_max_pktsize));
  1376.         }

  1377.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1378.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1379.             S5P_OTG_DOEPCTL_OUT);
  1380.     }
  1381. }

  1382. void s3c_usb_download_continue(u32 fifo_cnt_byte)
  1383. {
  1384.     if (otg.op_mode == USB_CPU) {
  1385.         s3c_usb_read_out_fifo((u8 *)otg.dn_ptr, fifo_cnt_byte);
  1386.         otg.dn_ptr += fifo_cnt_byte;
  1387.         DBG_BULK1("downloadFileSize!=0, 2nd BYTE_READ_CNT_REG = 0x%x, m_pDownPt = 0x%x\n",
  1388.                 fifo_cnt_byte, otg.dn_ptr);

  1389.         s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1, otg.bulkout_max_pktsize);

  1390.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1391.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1392.             S5P_OTG_DOEPCTL_OUT);

  1393.         /* USB format : addr(4)+size(4)+data(n)+cs(2) */
  1394.         if (((u32)otg.dn_ptr - otg.dn_addr) >= (otg.dn_filesize - 8)) {
  1395.             printf("Download Done!! Download Address: 0x%x, Download Filesize:0x%x\n",
  1396.                 otg.dn_addr, (otg.dn_filesize-10));

  1397.             s3c_usbd_dn_cnt     = otg.dn_filesize-10;
  1398.             s3c_usbd_dn_addr    = otg.dn_addr;

  1399. #ifdef USB_CHECKSUM_EN
  1400.             s3c_usb_verify_checksum();
  1401. #endif
  1402.             s3c_receive_done = 1;
  1403.         }

  1404.     }\
  1405. }

  1406. void s3c_usb_int_bulkout(u32 fifo_cnt_byte)
  1407. {
  1408.     DBG_BULK0("Bulk Out Function : otg.dn_filesize=0x%x\n", otg.dn_filesize);
  1409.     if (otg.dn_filesize==0) {
  1410.         if (fifo_cnt_byte == 10) {
  1411.             s3c_usb_upload_start();
  1412.         } else {
  1413.             s3c_usb_download_start(fifo_cnt_byte);
  1414.         }
  1415.     } else {
  1416.         s3c_usb_download_continue(fifo_cnt_byte);
  1417.     }
  1418. }

  1419. void s3c_usb_dma_in_done(void)
  1420. {
  1421.     s32 remain_cnt;

  1422.     DBG_BULK0("DMA IN : Transfer Done\n");

  1423.     otg.up_ptr = (u8 *)readl(S5P_OTG_DIEPDMA_IN);
  1424.     remain_cnt = otg.up_size- ((u32)otg.up_ptr - otg.up_addr);

  1425.     if (remain_cnt>0) {
  1426.         u32 pktcnt, remainder;
  1427.         pktcnt = (u32)(remain_cnt/otg.bulkin_max_pktsize);
  1428.         remainder = (u32)(remain_cnt%otg.bulkin_max_pktsize);
  1429.         if(remainder != 0) {
  1430.             pktcnt += 1;
  1431.         }
  1432.         DBG_SETUP1("remain_cnt : %d \n", remain_cnt);
  1433.         if (pktcnt> 1023) {
  1434.             s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1023,
  1435.                 (otg.bulkin_max_pktsize*1023));
  1436.         } else {
  1437.             s3c_usb_set_inep_xfersize(EP_TYPE_BULK, pktcnt,
  1438.                 remain_cnt);
  1439.         }

  1440.         /*ep1 enable, clear nak, bulk, usb active, next ep1, max pkt */
  1441.         writel(1u<<31|1<<26|2<<18|1<<15|BULK_IN_EP<<11|otg.bulkin_max_pktsize<<0,
  1442.             S5P_OTG_DIEPCTL_IN);
  1443.     } else
  1444.         DBG_SETUP1("DMA IN : Transfer Complete\n");
  1445. }

  1446. void s3c_usb_dma_out_done(void)
  1447. {
  1448.     s32 remain_cnt;

  1449.     DBG_BULK1("DMA OUT : Transfer Done\n");
  1450.     otg.dn_ptr = (u8 *)readl(S5P_OTG_DOEPDMA_OUT);

  1451.     remain_cnt = otg.dn_filesize - ((u32)otg.dn_ptr - otg.dn_addr + 8);

  1452.     if (remain_cnt>0) {
  1453.         u32 pktcnt, remainder;
  1454.         pktcnt = (u32)(remain_cnt/otg.bulkout_max_pktsize);
  1455.         remainder = (u32)(remain_cnt%otg.bulkout_max_pktsize);
  1456.         if(remainder != 0) {
  1457.             pktcnt += 1;
  1458.         }
  1459.         DBG_BULK1("remain_cnt : %d \n", remain_cnt);
  1460.         if (pktcnt> 1023) {
  1461.             s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1023,
  1462.                 (otg.bulkout_max_pktsize*1023));
  1463.         } else {
  1464.             s3c_usb_set_outep_xfersize(EP_TYPE_BULK, pktcnt,
  1465.                 remain_cnt);
  1466.         }

  1467.         /*ep3 enable, clear nak, bulk, usb active, next ep3, max pkt 64*/
  1468.         writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1469.             S5P_OTG_DOEPCTL_OUT);
  1470.     } else {
  1471.         DBG_BULK1("DMA OUT : Transfer Complete\n");
  1472.         udelay(500);        /*for FPGA ???*/
  1473.     }
  1474. }

  1475. void s3c_usb_set_all_outep_nak(void)
  1476. {
  1477.     u8 i;
  1478.     u32 tmp;

  1479.     for(i=0;i<16;i++)
  1480.     {
  1481.         tmp = readl(S5P_OTG_DOEPCTL0+0x20*i);
  1482.         tmp |= DEPCTL_SNAK;
  1483.         writel(tmp, S5P_OTG_DOEPCTL0+0x20*i);
  1484.     }
  1485. }

  1486. void s3c_usb_clear_all_outep_nak(void)
  1487. {
  1488.     u8 i;
  1489.     u32 tmp;

  1490.     for(i=0;i<16;i++)
  1491.     {
  1492.         tmp = readl(S5P_OTG_DOEPCTL0+0x20*i);
  1493.         tmp |= (DEPCTL_EPENA|DEPCTL_CNAK);
  1494.         writel(tmp, S5P_OTG_DOEPCTL0+0x20*i);
  1495.     }
  1496. }

  1497. void s3c_usb_set_max_pktsize(USB_SPEED speed)
  1498. {
  1499.     if (speed == USB_HIGH)
  1500.     {
  1501.         otg.speed = USB_HIGH;
  1502.         otg.ctrl_max_pktsize = HS_CTRL_PKT_SIZE;
  1503.         otg.bulkin_max_pktsize = HS_BULK_PKT_SIZE;
  1504.         otg.bulkout_max_pktsize = HS_BULK_PKT_SIZE;
  1505.     }
  1506.     else
  1507.     {
  1508.         otg.speed = USB_FULL;
  1509.         otg.ctrl_max_pktsize = FS_CTRL_PKT_SIZE;
  1510.         otg.bulkin_max_pktsize = FS_BULK_PKT_SIZE;
  1511.         otg.bulkout_max_pktsize = FS_BULK_PKT_SIZE;
  1512.     }
  1513. }

  1514. void s3c_usb_set_endpoint(void)
  1515. {
  1516.     /* Unmask S5P_OTG_DAINT source */
  1517.     writel(0xff, S5P_OTG_DIEPINT0);
  1518.     writel(0xff, S5P_OTG_DOEPINT0);
  1519.     writel(0xff, S5P_OTG_DIEPINT_IN);
  1520.     writel(0xff, S5P_OTG_DOEPINT_OUT);

  1521.     /* Init For Ep0*/
  1522.     if(otg.speed == USB_HIGH)
  1523.     {
  1524.         /*MPS:64bytes */
  1525.         writel(((1<<26)|(CONTROL_EP<<11)|(0<<0)), S5P_OTG_DIEPCTL0);
  1526.         /*ep0 enable, clear nak */
  1527.         writel((1u<<31)|(1<<26)|(0<<0), S5P_OTG_DOEPCTL0);
  1528.     }
  1529.     else
  1530.     {
  1531.         /*MPS:8bytes */
  1532.         writel(((1<<26)|(CONTROL_EP<<11)|(3<<0)), S5P_OTG_DIEPCTL0);
  1533.         /*ep0 enable, clear nak */
  1534.         writel((1u<<31)|(1<<26)|(3<<0), S5P_OTG_DOEPCTL0);
  1535.     }
  1536. }

  1537. void s3c_usb_set_descriptors(void)
  1538. {
  1539.     /* Standard device descriptor */
  1540.     otg.desc.dev.bLength=DEVICE_DESC_SIZE;    /*0x12*/
  1541.     otg.desc.dev.bDescriptorType=DEVICE_DESCRIPTOR;
  1542.     otg.desc.dev.bDeviceClass=0xFF; /* 0x0*/
  1543.     otg.desc.dev.bDeviceSubClass=0x0;
  1544.     otg.desc.dev.bDeviceProtocol=0x0;
  1545.     otg.desc.dev.bMaxPacketSize0=otg.ctrl_max_pktsize;
  1546.     otg.desc.dev.idVendorL=0xE8;    /*0x45;*/
  1547.     otg.desc.dev.idVendorH=0x04;    /*0x53;*/
  1548.     otg.desc.dev.idProductL=0x34; /*0x00*/
  1549.     otg.desc.dev.idProductH=0x12; /*0x64*/
  1550.     otg.desc.dev.bcdDeviceL=0x00;
  1551.     otg.desc.dev.bcdDeviceH=0x01;
  1552.     otg.desc.dev.iManufacturer=0x1; /* index of string descriptor */
  1553.     otg.desc.dev.iProduct=0x2;    /* index of string descriptor */
  1554.     otg.desc.dev.iSerialNumber=0x0;
  1555.     otg.desc.dev.bNumConfigurations=0x1;
  1556.     if (otg.speed == USB_FULL) {
  1557.         otg.desc.dev.bcdUSBL=0x10;
  1558.         otg.desc.dev.bcdUSBH=0x01;    /* Ver 1.10*/
  1559.     }
  1560.     else {
  1561.         otg.desc.dev.bcdUSBL=0x00;
  1562.         otg.desc.dev.bcdUSBH=0x02;    /* Ver 2.0*/
  1563.     }

  1564.     /* Standard configuration descriptor */
  1565.     otg.desc.config.bLength=CONFIG_DESC_SIZE; /* 0x9 bytes */
  1566.     otg.desc.config.bDescriptorType=CONFIGURATION_DESCRIPTOR;
  1567.     otg.desc.config.wTotalLengthL=CONFIG_DESC_TOTAL_SIZE;
  1568.     otg.desc.config.wTotalLengthH=0;
  1569.     otg.desc.config.bNumInterfaces=1;
  1570. /* dbg     descConf.bConfigurationValue=2; // why 2? There's no reason.*/
  1571.     otg.desc.config.bConfigurationValue=1;
  1572.     otg.desc.config.iConfiguration=0;
  1573.     otg.desc.config.bmAttributes=CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED; /* bus powered only.*/
  1574.     otg.desc.config.maxPower=25; /* draws 50mA current from the USB bus.*/

  1575.     /* Standard interface descriptor */
  1576.     otg.desc.intf.bLength=INTERFACE_DESC_SIZE; /* 9*/
  1577.     otg.desc.intf.bDescriptorType=INTERFACE_DESCRIPTOR;
  1578.     otg.desc.intf.bInterfaceNumber=0x0;
  1579.     otg.desc.intf.bAlternateSetting=0x0; /* ?*/
  1580.     otg.desc.intf.bNumEndpoints = 2;    /* # of endpoints except EP0*/
  1581.     otg.desc.intf.bInterfaceClass=0xff; /* 0x0 ?*/
  1582.     otg.desc.intf.bInterfaceSubClass=0x0;
  1583.     otg.desc.intf.bInterfaceProtocol=0x0;
  1584.     otg.desc.intf.iInterface=0x0;

  1585.     /* Standard endpoint0 descriptor */
  1586.     otg.desc.ep1.bLength=ENDPOINT_DESC_SIZE;
  1587.     otg.desc.ep1.bDescriptorType=ENDPOINT_DESCRIPTOR;
  1588.     otg.desc.ep1.bEndpointAddress=BULK_IN_EP|EP_ADDR_IN;
  1589.     otg.desc.ep1.bmAttributes=EP_ATTR_BULK;
  1590.     otg.desc.ep1.wMaxPacketSizeL=(u8)otg.bulkin_max_pktsize; /* 64*/
  1591.     otg.desc.ep1.wMaxPacketSizeH=(u8)(otg.bulkin_max_pktsize>>8);
  1592.     otg.desc.ep1.bInterval=0x0; /* not used */

  1593.     /* Standard endpoint1 descriptor */
  1594.     otg.desc.ep2.bLength=ENDPOINT_DESC_SIZE;
  1595.     otg.desc.ep2.bDescriptorType=ENDPOINT_DESCRIPTOR;
  1596.     otg.desc.ep2.bEndpointAddress=BULK_OUT_EP|EP_ADDR_OUT;
  1597.     otg.desc.ep2.bmAttributes=EP_ATTR_BULK;
  1598.     otg.desc.ep2.wMaxPacketSizeL=(u8)otg.bulkout_max_pktsize; /* 64*/
  1599.     otg.desc.ep2.wMaxPacketSizeH=(u8)(otg.bulkout_max_pktsize>>8);
  1600.     otg.desc.ep2.bInterval=0x0; /* not used */
  1601. }

  1602. void s3c_usb_check_speed(USB_SPEED *speed)
  1603. {
  1604.     u32 status;

  1605.     status = readl(S5P_OTG_DSTS); /* System status read */

  1606.     *speed = (USB_SPEED)((status&0x6) >>1);
  1607. }

  1608. void s3c_usb_clear_dnfile_info(void)
  1609. {
  1610.     otg.dn_addr = 0;
  1611.     otg.dn_filesize = 0;
  1612.     otg.dn_ptr = 0;
  1613. }

  1614. void s3c_usb_clear_upfile_info(void)
  1615. {
  1616.     otg.up_addr= 0;
  1617.     otg.up_size= 0;
  1618.     otg.up_ptr = 0;
  1619. }


  1620. int s3c_usb_check_setconf(void)
  1621. {
  1622.     if (otg.set_config == 0)
  1623.         return FALSE;
  1624.     else
  1625.         return TRUE;
  1626. }

  1627. void s3c_usb_set_opmode(USB_OPMODE mode)
  1628. {
  1629.     otg.op_mode = mode;

  1630.     writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|
  1631.         INT_RESET|INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY,
  1632.         S5P_OTG_GINTMSK); /*gint unmask */

  1633.     writel(MODE_SLAVE|BURST_SINGLE|GBL_INT_UNMASK, S5P_OTG_GAHBCFG);

  1634.     s3c_usb_set_outep_xfersize(EP_TYPE_BULK, 1, otg.bulkout_max_pktsize);
  1635.     s3c_usb_set_inep_xfersize(EP_TYPE_BULK, 1, 0);

  1636.     /*bulk out ep enable, clear nak, bulk, usb active, next ep3, max pkt */
  1637.     writel(1u<<31|1<<26|2<<18|1<<15|otg.bulkout_max_pktsize<<0,
  1638.         S5P_OTG_DOEPCTL_OUT);

  1639.     /*bulk in ep enable, clear nak, bulk, usb active, next ep1, max pkt */
  1640.     writel(0u<<31|1<<26|2<<18|1<<15|otg.bulkin_max_pktsize<<0,
  1641.         S5P_OTG_DIEPCTL_IN);
  1642. }

  1643. void s3c_usb_reset(void)
  1644. {
  1645.     s3c_usb_set_all_outep_nak();

  1646.     otg.ep0_state = EP0_STATE_INIT;
  1647.     writel(((1<<BULK_OUT_EP)|(1<<CONTROL_EP))<<16|((1<<BULK_IN_EP)|(1<<CONTROL_EP)),
  1648.         S5P_OTG_DAINTMSK);
  1649.     writel(CTRL_OUT_EP_SETUP_PHASE_DONE|AHB_ERROR|TRANSFER_DONE,
  1650.         S5P_OTG_DOEPMSK);
  1651.     writel(INTKN_TXFEMP|NON_ISO_IN_EP_TIMEOUT|AHB_ERROR|TRANSFER_DONE,
  1652.         S5P_OTG_DIEPMSK);

  1653.     /* Rx FIFO Size */
  1654.     writel(RX_FIFO_SIZE, S5P_OTG_GRXFSIZ);

  1655.     /* Non Periodic Tx FIFO Size */
  1656.     writel(NPTX_FIFO_SIZE<<16| NPTX_FIFO_START_ADDR<<0, S5P_OTG_GNPTXFSIZ);

  1657.     s3c_usb_clear_all_outep_nak();

  1658.     /*clear device address */
  1659.     writel(readl(S5P_OTG_DCFG)&~(0x7f<<4), S5P_OTG_DCFG);

  1660.     if(SUSPEND_RESUME_ON) {
  1661.         writel(readl(S5P_OTG_PCGCCTL)&~(1<<0), S5P_OTG_PCGCCTL);
  1662.     }
  1663. }
  1664. int s3c_usb_set_init(void)
  1665. {
  1666.     u32 status;

  1667.     status = readl(S5P_OTG_DSTS); /* System status read */

  1668.     /* Set if Device is High speed or Full speed */
  1669.     if (((status&0x6) >>1) == USB_HIGH) {
  1670.         DBG_SETUP1("High Speed Detection\n");
  1671.         s3c_usb_set_max_pktsize(USB_HIGH);
  1672.     }
  1673.     else if(((status&0x6) >>1) == USB_FULL) {
  1674.         DBG_SETUP1("Full Speed Detec tion\n");
  1675.         s3c_usb_set_max_pktsize(USB_FULL);
  1676.     }
  1677.     else {
  1678.         printf("**** Error:Neither High_Speed nor Full_Speed\n");
  1679.         return FALSE;
  1680.     }

  1681.     s3c_usb_set_endpoint();
  1682.     s3c_usb_set_descriptors();
  1683.     s3c_usb_clear_dnfile_info();
  1684.     s3c_usb_set_opmode(op_mode);

  1685.     return TRUE;
  1686. }

  1687. void s3c_usb_pkt_receive(void)
  1688. {
  1689.     u32 rx_status;
  1690.     u32 fifo_cnt_byte;

  1691.     rx_status = readl(S5P_OTG_GRXSTSP);
  1692.     DBG_SETUP0("S5P_OTG_GRXSTSP = 0x%x\n", rx_status);

  1693.     if ((rx_status & (0xf<<17)) == SETUP_PKT_RECEIVED) {
  1694.         DBG_SETUP1("SETUP_PKT_RECEIVED\n");
  1695.         s3c_usb_ep0_int_hndlr();

  1696.     } else if ((rx_status & (0xf<<17)) == OUT_PKT_RECEIVED) {
  1697.         fifo_cnt_byte = (rx_status & 0x7ff0)>>4;
  1698.         DBG_SETUP1("OUT_PKT_RECEIVED\n");

  1699.         if((rx_status & BULK_OUT_EP)&&(fifo_cnt_byte)) {
  1700.             s3c_usb_int_bulkout(fifo_cnt_byte);
  1701.             if( otg.op_mode == USB_CPU )
  1702.                 writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|
  1703.                     INT_ENUMDONE|INT_RESET|INT_SUSPEND|
  1704.                     INT_RX_FIFO_NOT_EMPTY,
  1705.                     S5P_OTG_GINTMSK);
  1706.             return;
  1707.         }

  1708.     } else if ((rx_status & (0xf<<17)) == GLOBAL_OUT_NAK) {
  1709.         DBG_SETUP1("GLOBAL_OUT_NAK\n");

  1710.     } else if ((rx_status & (0xf<<17)) == OUT_TRNASFER_COMPLETED) {
  1711.         DBG_SETUP1("OUT_TRNASFER_COMPLETED\n");

  1712.     } else if ((rx_status & (0xf<<17)) == SETUP_TRANSACTION_COMPLETED) {
  1713.         DBG_SETUP1("SETUP_TRANSACTION_COMPLETED\n");

  1714.     } else {
  1715.         DBG_SETUP1("Reserved\n");
  1716.     }
  1717. }

  1718. void s3c_usb_transfer(void)
  1719. {
  1720.     u32 ep_int;
  1721.     u32 check_dma;
  1722.     u32 ep_int_status;

  1723.     ep_int = readl(S5P_OTG_DAINT);
  1724.     DBG_SETUP0("S5P_OTG_DAINT = 0x%x", ep_int);

  1725.     if (ep_int & (1<<CONTROL_EP)) {
  1726.         ep_int_status = readl(S5P_OTG_DIEPINT0);
  1727.         DBG_SETUP1("S5P_OTG_DIEPINT0 : %x \n", ep_int_status);

  1728.         if (ep_int_status & INTKN_TXFEMP) {
  1729.             u32 uNTxFifoSpace;
  1730.             do {
  1731.                 uNTxFifoSpace=readl(S5P_OTG_GNPTXSTS)&0xffff;
  1732.             }while(uNTxFifoSpace<otg.ctrl_max_pktsize);

  1733.             s3c_usb_transfer_ep0();
  1734.         }

  1735.         writel(ep_int_status, S5P_OTG_DIEPINT0); /* Interrupt Clear */
  1736.     }

  1737.     if (ep_int & ((1<<CONTROL_EP)<<16)) {
  1738.         ep_int_status = readl(S5P_OTG_DOEPINT0);
  1739.         DBG_SETUP1("S5P_OTG_DOEPINT0 : %x \n", ep_int_status);

  1740.         s3c_usb_set_outep_xfersize(EP_TYPE_CONTROL, 1, 8);
  1741.         writel(1u<<31|1<<26, S5P_OTG_DOEPCTL0); /*ep0 enable, clear nak */

  1742.         writel(ep_int_status, S5P_OTG_DOEPINT0); /* Interrupt Clear */
  1743.     }

  1744.     if(ep_int & (1<<BULK_IN_EP)) {
  1745.         ep_int_status = readl(S5P_OTG_DIEPINT_IN);
  1746.         DBG_BULK1("S5P_OTG_DIEPINT_IN : %x \n", ep_int_status);
  1747.         writel(ep_int_status, S5P_OTG_DIEPINT_IN); /* Interrupt Clear */

  1748.         if ( (ep_int_status&INTKN_TXFEMP) && otg.op_mode == USB_CPU)
  1749.             s3c_usb_int_bulkin();

  1750.         check_dma = readl(S5P_OTG_GAHBCFG);
  1751.         if ((check_dma&MODE_DMA)&&(ep_int_status&TRANSFER_DONE))
  1752.             s3c_usb_dma_in_done();
  1753.     }

  1754.     if (ep_int & ((1<<BULK_OUT_EP)<<16)) {
  1755.         ep_int_status = readl(S5P_OTG_DOEPINT_OUT);
  1756.         DBG_BULK1("S5P_OTG_DOEPINT_OUT : 0x%x\n", ep_int_status);
  1757.         writel(ep_int_status, S5P_OTG_DOEPINT_OUT); /* Interrupt Clear */

  1758.         check_dma = readl(S5P_OTG_GAHBCFG);
  1759.         if ((check_dma&MODE_DMA)&&(ep_int_status&TRANSFER_DONE)) {
  1760.             s3c_usb_dma_out_done();
  1761.         }
  1762.     }
  1763. }

  1764. void s3c_udc_int_hndlr(void)
  1765. {
  1766.     u32 int_status;
  1767.     int tmp;

  1768.     int_status = readl(S5P_OTG_GINTSTS); /* Core Interrupt Register */
  1769.     writel(int_status, S5P_OTG_GINTSTS); /* Interrupt Clear */
  1770.     DBG_SETUP0("*** USB OTG Interrupt(S5P_OTG_GINTSTS: 0x%08x) ****\n",
  1771.         int_status);

  1772.     if (int_status & INT_RESET) {
  1773.         DBG_SETUP1("INT_RESET\n");
  1774.         writel(INT_RESET, S5P_OTG_GINTSTS); /* Interrupt Clear */

  1775.         s3c_usb_reset();
  1776.     }

  1777.     if (int_status & INT_ENUMDONE) {
  1778.         DBG_SETUP1("INT_ENUMDONE :");
  1779.         writel(INT_ENUMDONE, S5P_OTG_GINTSTS); /* Interrupt Clear */
  1780.         s3c_usb_connected_status=3;
  1781.         tmp = s3c_usb_set_init();
  1782.         if (tmp == FALSE)
  1783.             return;

  1784.     }

  1785.     if (int_status & INT_RESUME) {
  1786.         DBG_SETUP1("INT_RESUME\n");
  1787.         writel(INT_RESUME, S5P_OTG_GINTSTS); /* Interrupt Clear */

  1788.         if(SUSPEND_RESUME_ON) {
  1789.             writel(readl(S5P_OTG_PCGCCTL)&~(1<<0), S5P_OTG_PCGCCTL);
  1790.             DBG_SETUP1("INT_RESUME\n");
  1791.         }
  1792.     }

  1793.     if (int_status & INT_SUSPEND) {
  1794.         DBG_SETUP1("INT_SUSPEND\n");
  1795.         writel(INT_SUSPEND, S5P_OTG_GINTSTS); /* Interrupt Clear */

  1796.         if(SUSPEND_RESUME_ON) {
  1797.             writel(readl(S5P_OTG_PCGCCTL)|(1<<0), S5P_OTG_PCGCCTL);
  1798.         }
  1799.     }

  1800.     if(int_status & INT_RX_FIFO_NOT_EMPTY) {
  1801.         DBG_SETUP1("INT_RX_FIFO_NOT_EMPTY\n");
  1802.         /* Read only register field */

  1803.         writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|
  1804.             INT_ENUMDONE|INT_RESET|INT_SUSPEND,
  1805.             S5P_OTG_GINTMSK);
  1806.         s3c_usb_pkt_receive();
  1807.         writel(INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|
  1808.             INT_RESET |INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY,
  1809.             S5P_OTG_GINTMSK); /*gint unmask */
  1810.         s3c_usb_connected_status=1;
  1811.     }

  1812.     if ((int_status & INT_IN_EP) || (int_status & INT_OUT_EP)) {
  1813.         DBG_SETUP1("INT_IN or OUT_EP\n");
  1814.         /* Read only register field */

  1815.         s3c_usb_transfer();
  1816.     }
  1817. }

  1818. int s3c_udc_is_adc_cable()
  1819. {
  1820.     int count = 0;
  1821.     while(count<10)
  1822.         {
  1823.     if((readl(S5P_OTG_HPRT)&0xc00)==0xc00)
  1824.         count ++;
  1825.     else
  1826.         {
  1827.             count =0;
  1828.             return 0;
  1829.         }
  1830.     }
  1831.     return 1;
  1832. }
  1833. /*the following is for u-boot battery charge*/
  1834. int s3c_check_usb_connect_status()
  1835. {
  1836.     if(!s3c_usb_phy_inited)/*initalized at first*/
  1837.     {    
  1838.         s3c_usbctl_init(1);
  1839.     }
  1840.     /*then check current usb status*/
  1841.     unsigned long usec = (1000*1000*10);
  1842.     ulong tmo, tmp;

  1843.     if (usec >= 1000) {        /* if "big" number, spread normalization to seconds */
  1844.         tmo = usec / 1000;    /* start to normalize for usec to ticks per sec */
  1845.         tmo *= CFG_HZ;        /* find number of "ticks" to wait to achieve target */
  1846.         tmo /= 1000;        /* finish normalize. */
  1847.     }
  1848.     else {                /* else small number, don't kill it prior to HZ multiply */
  1849.         tmo = usec * CFG_HZ;
  1850.         tmo /= (1000 * 1000);
  1851.     }
  1852.     tmp = get_timer(0);        /* get current timestamp */
  1853.     if ((tmo + tmp + 1) < tmp)    /* if setting this fordward will roll time stamp */
  1854.     {        
  1855.         reset_timer_masked();    /* reset "advancing" timestamp to 0, set lastdec value */
  1856.     }
  1857.     else
  1858.         tmo += tmp;        /* else, set advancing stamp wake up time */

  1859.     while (get_timer_masked()<tmo)    
  1860.     {
  1861.         if (S3C_USBD_DETECT_IRQ()) {
  1862.             s3c_udc_int_hndlr();
  1863.             S3C_USBD_CLEAR_IRQ();
  1864.         }
  1865.         if(s3c_udc_is_adc_cable())
  1866.             s3c_usb_connected_status=2;
  1867.         if(s3c_usb_connected_status==2||s3c_usb_connected_status==1)
  1868.             break;
  1869.     }
  1870.     
  1871.     if(s3c_usb_phy_inited)
  1872.     {        
  1873.         s3c_usb_stop();
  1874.     }
  1875.     return s3c_usb_connected_status;
  1876.     
  1877. }
  1878. #endif

usbd-otg-hs.h

点击(此处)折叠或打开

  1. /*
  2.  * cpu/s5pc1xx/usbd-otg-hs.h
  3.  *
  4.  * (C) Copyright 2009
  5.  * Byungjae Lee, Samsung Erectronics, bjlee@samsung.com.
  6.  *    - only support for S5PC100
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21.  * MA 02111-1307 USA
  22.  */

  23. #ifndef __S3C_OTG_HS_H__
  24. #define __S3C_OTG_HS_H__

  25. #include <asm/byteorder.h>
  26. #include <asm/arch/hardware.h>
  27. #include <asm/io.h>

  28. #define S3C_USBD_DETECT_IRQ()    (readl(S5P_OTG_GINTSTS) & \
  29.                     (GINTSTS_WkUpInt|GINTSTS_OEPInt|GINTSTS_IEPInt| \
  30.                      GINTSTS_EnumDone|GINTSTS_USBRst|GINTSTS_USBSusp|GINTSTS_RXFLvl))
  31. #define S3C_USBD_CLEAR_IRQ()    do { \
  32.                     writel(BIT_ALLMSK, (S5P_OTG_GINTSTS)); \
  33.                 } while (0)

  34. #define CONTROL_EP        0
  35. #define BULK_IN_EP        1
  36. #define BULK_OUT_EP        2

  37. #define FS_CTRL_PKT_SIZE    8
  38. #define FS_BULK_PKT_SIZE    64

  39. #define HS_CTRL_PKT_SIZE    64
  40. #define HS_BULK_PKT_SIZE    512

  41. #define RX_FIFO_SIZE        512
  42. #define NPTX_FIFO_START_ADDR    RX_FIFO_SIZE
  43. #define NPTX_FIFO_SIZE        512
  44. #define PTX_FIFO_SIZE        512

  45. // string descriptor
  46. #define LANGID_US_L        (0x09)
  47. #define LANGID_US_H        (0x04)

  48. // Feature Selectors
  49. #define EP_STALL        0
  50. #define DEVICE_REMOTE_WAKEUP    1
  51. #define TEST_MODE        2

  52. /* Test Mode Selector*/
  53. #define TEST_J            1
  54. #define TEST_K            2
  55. #define TEST_SE0_NAK        3
  56. #define TEST_PACKET        4
  57. #define TEST_FORCE_ENABLE    5

  58. #define S5P_OTG_DIEPCTL_IN    (S5P_OTG_DIEPCTL0 + 0x20*BULK_IN_EP)
  59. #define S5P_OTG_DIEPINT_IN    (S5P_OTG_DIEPINT0 + 0x20*BULK_IN_EP)
  60. #define S5P_OTG_DIEPTSIZ_IN    (S5P_OTG_DIEPTSIZ0 + 0x20*BULK_IN_EP)
  61. #define S5P_OTG_DIEPDMA_IN    (S5P_OTG_DIEPDMA0 + 0x20*BULK_IN_EP)
  62. #define S5P_OTG_DOEPCTL_OUT    (S5P_OTG_DOEPCTL0 + 0x20*BULK_OUT_EP)
  63. #define S5P_OTG_DOEPINT_OUT    (S5P_OTG_DOEPINT0 + 0x20*BULK_OUT_EP)
  64. #define S5P_OTG_DOEPTSIZ_OUT    (S5P_OTG_DOEPTSIZ0 + 0x20*BULK_OUT_EP)
  65. #define S5P_OTG_DOEPDMA_OUT    (S5P_OTG_DOEPDMA0 + 0x20*BULK_OUT_EP)
  66. #define S5P_OTG_IN_FIFO        (S5P_OTG_EP0_FIFO + 0x1000*BULK_IN_EP)
  67. #define S5P_OTG_OUT_FIFO    (S5P_OTG_EP0_FIFO + 0x1000*BULK_OUT_EP)


  68. typedef struct
  69. {
  70.     u8 bLength;
  71.     u8 bDescriptorType;
  72.     u8 bcdUSBL;
  73.     u8 bcdUSBH;
  74.     u8 bDeviceClass;
  75.     u8 bDeviceSubClass;
  76.     u8 bDeviceProtocol;
  77.     u8 bMaxPacketSize0;
  78.     u8 idVendorL;
  79.     u8 idVendorH;
  80.     u8 idProductL;
  81.     u8 idProductH;
  82.     u8 bcdDeviceL;
  83.     u8 bcdDeviceH;
  84.     u8 iManufacturer;
  85.     u8 iProduct;
  86.     u8 iSerialNumber;
  87.     u8 bNumConfigurations;
  88. } __attribute__ ((packed)) device_desc_t;

  89. typedef struct
  90. {
  91.     u8 bLength;
  92.     u8 bDescriptorType;
  93.     u8 wTotalLengthL;
  94.     u8 wTotalLengthH;
  95.     u8 bNumInterfaces;
  96.     u8 bConfigurationValue;
  97.     u8 iConfiguration;
  98.     u8 bmAttributes;
  99.     u8 maxPower;
  100. } __attribute__ ((packed)) config_desc_t;

  101. typedef struct
  102. {
  103.     u8 bLength;
  104.     u8 bDescriptorType;
  105.     u8 bInterfaceNumber;
  106.     u8 bAlternateSetting;
  107.     u8 bNumEndpoints;
  108.     u8 bInterfaceClass;
  109.     u8 bInterfaceSubClass;
  110.     u8 bInterfaceProtocol;
  111.     u8 iInterface;
  112. } __attribute__ ((packed)) intf_desc_t;

  113. typedef struct
  114. {
  115.     u8 bLength;
  116.     u8 bDescriptorType;
  117.     u8 bEndpointAddress;
  118.     u8 bmAttributes;
  119.     u8 wMaxPacketSizeL;
  120.     u8 wMaxPacketSizeH;
  121.     u8 bInterval;
  122. } __attribute__ ((packed)) ep_desc_t;

  123. typedef struct
  124. {
  125.     u8 bmRequestType;
  126.     u8 bRequest;
  127.     u8 wValue_L;
  128.     u8 wValue_H;
  129.     u8 wIndex_L;
  130.     u8 wIndex_H;
  131.     u8 wLength_L;
  132.     u8 wLength_H;
  133. } __attribute__ ((packed)) device_req_t;

  134. typedef struct
  135. {
  136.     device_desc_t dev;
  137.     config_desc_t config;
  138.     intf_desc_t intf;
  139.     ep_desc_t ep1;
  140.     ep_desc_t ep2;
  141.     ep_desc_t ep3;
  142.     ep_desc_t ep4;
  143. } __attribute__ ((packed)) descriptors_t;

  144. typedef struct
  145. {
  146.     u8 Device;
  147.     u8 Interface;
  148.     u8 ep_ctrl;
  149.     u8 ep_in;
  150.     u8 ep_out;
  151. } __attribute__ ((packed)) get_status_t;

  152. typedef struct
  153. {
  154.     u8 AlternateSetting;
  155. } __attribute__ ((packed)) get_intf_t;


  156. typedef enum
  157. {
  158.     USB_CPU, USB_DMA
  159. } USB_OPMODE;

  160. typedef enum
  161. {
  162.     USB_HIGH, USB_FULL, USB_LOW
  163. } USB_SPEED;

  164. typedef enum
  165. {
  166.     EP_TYPE_CONTROL, EP_TYPE_ISOCHRONOUS, EP_TYPE_BULK, EP_TYPE_INTERRUPT
  167. } EP_TYPE;


  168. typedef struct
  169. {
  170.     descriptors_t desc;
  171.     device_req_t dev_req;

  172.     u32 ep0_state;
  173.     u32 ep0_substate;
  174.     USB_OPMODE op_mode;
  175.     USB_SPEED speed;
  176.     u32 ctrl_max_pktsize;
  177.     u32 bulkin_max_pktsize;
  178.     u32 bulkout_max_pktsize;
  179.     u32 dn_addr;
  180.     u32 dn_filesize;
  181.     u32 up_addr;
  182.     u32 up_size;
  183.     u8* dn_ptr;
  184.     u8* up_ptr;
  185.     u32 set_config;
  186.     u32 req_length;
  187. } __attribute__ ((packed)) otg_dev_t;

  188. // SPEC1.1

  189. // Standard bmRequestType (direction)
  190. enum DEV_REQUEST_DIRECTION
  191. {
  192.     HOST_TO_DEVICE                = 0x00,
  193.     DEVICE_TO_HOST                = 0x80
  194. };

  195. // Standard bmRequestType (Type)
  196. enum DEV_REQUEST_TYPE
  197. {
  198.     STANDARD_TYPE            = 0x00,
  199.     CLASS_TYPE            = 0x20,
  200.     VENDOR_TYPE            = 0x40,
  201.     RESERVED_TYPE            = 0x60
  202. };

  203. // Standard bmRequestType (Recipient)
  204. enum DEV_REQUEST_RECIPIENT
  205. {
  206.     DEVICE_RECIPIENT        = 0,
  207.     INTERFACE_RECIPIENT        = 1,
  208.     ENDPOINT_RECIPIENT        = 2,
  209.     OTHER_RECIPIENT            = 3
  210. };

  211. // Descriptor types
  212. enum DESCRIPTOR_TYPE
  213. {
  214.     DEVICE_DESCRIPTOR        = 1,
  215.     CONFIGURATION_DESCRIPTOR    = 2,
  216.     STRING_DESCRIPTOR        = 3,
  217.     INTERFACE_DESCRIPTOR        = 4,
  218.     ENDPOINT_DESCRIPTOR        = 5,
  219.     DEVICE_QUALIFIER        = 6,
  220.     OTHER_SPEED_CONFIGURATION    = 7,
  221.     INTERFACE_POWER            = 8
  222. };

  223. // configuration descriptor: bmAttributes
  224. enum CONFIG_ATTRIBUTES
  225. {
  226.     CONF_ATTR_DEFAULT        = 0x80,
  227.     CONF_ATTR_REMOTE_WAKEUP     = 0x20,
  228.     CONF_ATTR_SELFPOWERED        = 0x40
  229. };

  230. // endpoint descriptor
  231. enum ENDPOINT_ATTRIBUTES
  232. {
  233.     EP_ADDR_IN            = 0x80,
  234.     EP_ADDR_OUT            = 0x00,

  235.     EP_ATTR_CONTROL            = 0x0,
  236.     EP_ATTR_ISOCHRONOUS        = 0x1,
  237.     EP_ATTR_BULK            = 0x2,
  238.     EP_ATTR_INTERRUPT        = 0x3
  239. };

  240. // Standard bRequest codes
  241. enum STANDARD_REQUEST_CODE
  242. {
  243.     STANDARD_GET_STATUS        = 0,
  244.     STANDARD_CLEAR_FEATURE        = 1,
  245.     STANDARD_RESERVED_1        = 2,
  246.     STANDARD_SET_FEATURE        = 3,
  247.     STANDARD_RESERVED_2        = 4,
  248.     STANDARD_SET_ADDRESS        = 5,
  249.     STANDARD_GET_DESCRIPTOR        = 6,
  250.     STANDARD_SET_DESCRIPTOR        = 7,
  251.     STANDARD_GET_CONFIGURATION    = 8,
  252.     STANDARD_SET_CONFIGURATION    = 9,
  253.     STANDARD_GET_INTERFACE        = 10,
  254.     STANDARD_SET_INTERFACE        = 11,
  255.     STANDARD_SYNCH_FRAME        = 12
  256. };

  257. int s3c_usbctl_init(int ischeckstatus);
  258. int s3c_usbc_activate (void);
  259. int s3c_usb_stop( void );
  260. void s3c_udc_int_hndlr(void);

  261. /* in usbd-otg-hs.c */
  262. extern unsigned int s3c_usbd_dn_addr;
  263. extern unsigned int s3c_usbd_dn_cnt;
  264. extern int DNW;
  265. extern int s3c_got_header;
  266. extern int s3c_receive_done;

  267. #endif



阅读(3395) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~