dwc2.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
  4. * Copyright (C) 2014 Marek Vasut <marex@denx.de>
  5. */
  6. #include <string.h>
  7. #include "amt630h.h"
  8. #include "typedef.h"
  9. #include "uart.h"
  10. #include "timer.h"
  11. #include "list.h"
  12. #include "scsi.h"
  13. #include "usb.h"
  14. #include "dwc2.h"
  15. #include "cp15.h"
  16. /* Use only HC channel 0. */
  17. #define DWC2_HC_CHANNEL 0
  18. #define DWC2_STATUS_BUF_SIZE 64
  19. #define DWC2_DATA_BUF_SIZE (1* 1024)//(CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
  20. #define MAX_DEVICE 6//16
  21. #define MAX_ENDPOINT 6//16
  22. #define CONFIG_USB_DWC2_REG_ADDR REGS_USB_BASE
  23. static unsigned long phys_to_bus(unsigned long phys)
  24. {
  25. return phys;
  26. }
  27. static void clrsetbits_le32(void *addr,unsigned int clear,unsigned int set)
  28. {
  29. unsigned int val = 0;
  30. val = *(unsigned int *)addr;
  31. val &= ~(clear);
  32. val |= set;
  33. *(unsigned int *)addr = val;
  34. }
  35. static void setbits_le32(void *addr,unsigned int set)
  36. {
  37. unsigned int val;
  38. val = *(unsigned int *)addr;
  39. val |=set;
  40. *(unsigned int *)addr = val;
  41. }
  42. static void clrbits_le32(void *addr,unsigned int clear)
  43. {
  44. unsigned int val = 0;
  45. val = *(unsigned int *)addr;
  46. val &= ~(clear);
  47. *(unsigned int *)addr = val;
  48. }
  49. static inline int wait_for_bit_le32(void *reg,\
  50. const unsigned int mask,\
  51. const bool set,\
  52. const unsigned int timeout_ms,\
  53. const bool breakable)
  54. {
  55. unsigned int val;
  56. unsigned long start = get_timer(0);
  57. while (1) {
  58. val = readl(reg);
  59. if (!set)
  60. val = ~val;
  61. if ((val & mask) == mask)
  62. return 0;
  63. if (get_timer(start) > timeout_ms)
  64. break;
  65. // if (breakable && ctrlc()) {
  66. // return -EINTR;
  67. // }
  68. udelay(1);
  69. }
  70. return -ETIMEDOUT;
  71. }
  72. struct dwc2_priv {
  73. uint8_t *aligned_buffer;
  74. uint8_t *status_buffer;
  75. u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
  76. u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
  77. struct dwc2_core_regs *regs;
  78. int root_hub_devnum;
  79. bool ext_vbus;
  80. /*
  81. * The hnp/srp capability must be disabled if the platform
  82. * does't support hnp/srp. Otherwise the force mode can't work.
  83. */
  84. bool hnp_srp_disable;
  85. bool oc_disable;
  86. };
  87. /* We need cacheline-aligned buffers for DMA transfers and dcache support */
  88. //DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
  89. // ARCH_DMA_MINALIGN);
  90. //DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
  91. // ARCH_DMA_MINALIGN);
  92. //uint8_t *aligned_buffer_addr;
  93. //uint8_t *status_buffer_addr;
  94. #pragma data_alignment=32
  95. uint8_t aligned_buffer_addr[DWC2_DATA_BUF_SIZE];
  96. #pragma data_alignment=32
  97. uint8_t status_buffer_addr[DWC2_STATUS_BUF_SIZE];
  98. static struct dwc2_priv local;
  99. /*
  100. * DWC2 IP interface
  101. */
  102. /*
  103. * Initializes the FSLSPClkSel field of the HCFG register
  104. * depending on the PHY type.
  105. */
  106. static void init_fslspclksel(struct dwc2_core_regs *regs)
  107. {
  108. uint32_t phyclk;
  109. /* High speed PHY running at full speed or high speed */
  110. phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
  111. clrsetbits_le32(&regs->host_regs.hcfg,
  112. DWC2_HCFG_FSLSPCLKSEL_MASK,
  113. phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
  114. }
  115. /*
  116. * Flush a Tx FIFO.
  117. *
  118. * @param regs Programming view of DWC_otg controller.
  119. * @param num Tx FIFO to flush.
  120. */
  121. static void dwc_otg_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
  122. {
  123. int ret;
  124. writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
  125. &regs->grstctl);
  126. ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_TXFFLSH,
  127. false, 1000, false);
  128. if (ret)
  129. SendUartString("dwc_otg_core_reset Timeout!\n");
  130. /* Wait for 3 PHY Clocks */
  131. udelay(1);
  132. }
  133. /*
  134. * Flush Rx FIFO.
  135. *
  136. * @param regs Programming view of DWC_otg controller.
  137. */
  138. static void dwc_otg_flush_rx_fifo(struct dwc2_core_regs *regs)
  139. {
  140. int ret;
  141. writel(DWC2_GRSTCTL_RXFFLSH, &regs->grstctl);
  142. ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_RXFFLSH,
  143. false, 1000, false);
  144. if (ret)
  145. SendUartString("dwc_otg_core_reset Timeout!\n");
  146. /* Wait for 3 PHY Clocks */
  147. udelay(1);
  148. }
  149. /*
  150. * Do core a soft reset of the core. Be careful with this because it
  151. * resets all the internal state machines of the core.
  152. */
  153. static void dwc_otg_core_reset(struct dwc2_core_regs *regs)
  154. {
  155. int ret;
  156. /* Wait for AHB master IDLE state. */
  157. ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_AHBIDLE,
  158. true, 1000, false);
  159. if (ret)
  160. SendUartString("dwc_otg_core_reset Timeout!\n");
  161. /* Core Soft Reset */
  162. writel(DWC2_GRSTCTL_CSFTRST, &regs->grstctl);
  163. ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_CSFTRST,
  164. false, 1000, false);
  165. if (ret)
  166. SendUartString("dwc_otg_core_reset Timeout!\n");
  167. /*
  168. * Wait for core to come out of reset.
  169. * NOTE: This long sleep is _very_ important, otherwise the core will
  170. * not stay in host mode after a connector ID change!
  171. */
  172. mdelay(100);
  173. }
  174. static int dwc_vbus_supply_init(struct udevice *dev)
  175. {
  176. return 0;
  177. }
  178. /*
  179. * This function initializes the DWC_otg controller registers for
  180. * host mode.
  181. *
  182. * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  183. * request queues. Host channels are reset to ensure that they are ready for
  184. * performing transfers.
  185. *
  186. * @param dev USB Device (NULL if driver model is not being used)
  187. * @param regs Programming view of DWC_otg controller
  188. *
  189. */
  190. static void dwc_otg_core_host_init(struct udevice *dev,
  191. struct dwc2_core_regs *regs)
  192. {
  193. uint32_t nptxfifosize = 0;
  194. uint32_t ptxfifosize = 0;
  195. uint32_t hprt0 = 0;
  196. int i, ret, num_channels;
  197. /* Restart the Phy Clock */
  198. writel(0, &regs->pcgcctl);
  199. /* Initialize Host Configuration Register */
  200. init_fslspclksel(regs);
  201. /* Configure data FIFO sizes */
  202. #ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
  203. if (readl(&regs->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
  204. /* Rx FIFO */
  205. writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, &regs->grxfsiz);
  206. /* Non-periodic Tx FIFO */
  207. nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
  208. DWC2_FIFOSIZE_DEPTH_OFFSET;
  209. nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
  210. DWC2_FIFOSIZE_STARTADDR_OFFSET;
  211. writel(nptxfifosize, &regs->gnptxfsiz);
  212. /* Periodic Tx FIFO */
  213. ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
  214. DWC2_FIFOSIZE_DEPTH_OFFSET;
  215. ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
  216. CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
  217. DWC2_FIFOSIZE_STARTADDR_OFFSET;
  218. writel(ptxfifosize, &regs->hptxfsiz);
  219. }
  220. #endif
  221. /* Clear Host Set HNP Enable in the OTG Control Register */
  222. clrbits_le32(&regs->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
  223. /* Make sure the FIFOs are flushed. */
  224. dwc_otg_flush_tx_fifo(regs, 0x10); /* All Tx FIFOs */
  225. dwc_otg_flush_rx_fifo(regs);
  226. /* Flush out any leftover queued requests. */
  227. num_channels = readl(&regs->ghwcfg2);
  228. num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
  229. num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
  230. num_channels += 1;
  231. for (i = 0; i < num_channels; i++)
  232. clrsetbits_le32(&regs->hc_regs[i].hcchar,
  233. DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
  234. DWC2_HCCHAR_CHDIS);
  235. /* Halt all channels to put them into a known state. */
  236. for (i = 0; i < num_channels; i++) {
  237. clrsetbits_le32(&regs->hc_regs[i].hcchar,
  238. DWC2_HCCHAR_EPDIR,
  239. DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
  240. ret = wait_for_bit_le32(&regs->hc_regs[i].hcchar,
  241. DWC2_HCCHAR_CHEN, false, 1000, false);
  242. if (ret)
  243. SendUartString("dwc_otg_core_host_init Timeout!\n");
  244. }
  245. /* Turn on the vbus power. */
  246. if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
  247. hprt0 = readl(&regs->hprt0);
  248. hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
  249. hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
  250. if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
  251. hprt0 |= DWC2_HPRT0_PRTPWR;
  252. writel(hprt0, &regs->hprt0);
  253. }
  254. }
  255. if (dev)
  256. dwc_vbus_supply_init(dev);
  257. }
  258. /*
  259. * This function initializes the DWC_otg controller registers and
  260. * prepares the core for device mode or host mode operation.
  261. *
  262. * @param regs Programming view of the DWC_otg controller
  263. */
  264. static void dwc_otg_core_init(struct dwc2_priv *priv)
  265. {
  266. struct dwc2_core_regs *regs = priv->regs;
  267. uint32_t ahbcfg = 0;
  268. uint32_t usbcfg = 0;
  269. uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
  270. /* Common Initialization */
  271. usbcfg = readl(&regs->gusbcfg);
  272. /* Program the ULPI External VBUS bit if needed */
  273. if (priv->ext_vbus) {
  274. usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
  275. if (!priv->oc_disable) {
  276. usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
  277. DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
  278. }
  279. } else {
  280. usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
  281. }
  282. usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
  283. writel(usbcfg, &regs->gusbcfg);
  284. /* Reset the Controller */
  285. dwc_otg_core_reset(regs);
  286. /*
  287. * HS PHY parameters. These parameters are preserved during
  288. * soft reset so only program the first time. Do a soft reset
  289. * immediately after setting phyif.
  290. */
  291. usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
  292. usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
  293. if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) {
  294. usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
  295. } else { /* UTMI+ interface */
  296. #if (CONFIG_DWC2_UTMI_WIDTH == 16)
  297. usbcfg |= DWC2_GUSBCFG_PHYIF;
  298. #endif
  299. }
  300. writel(usbcfg, &regs->gusbcfg);
  301. /* Reset after setting the PHY parameters */
  302. dwc_otg_core_reset(regs);
  303. usbcfg = readl(&regs->gusbcfg);
  304. usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
  305. /*
  306. #ifdef CONFIG_DWC2_ULPI_FS_LS
  307. uint32_t hwcfg2 = readl(&regs->ghwcfg2);
  308. uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
  309. DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
  310. uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
  311. DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
  312. if (hval == 2 && fval == 1) {
  313. usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
  314. usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
  315. }
  316. #endif
  317. */
  318. if (priv->hnp_srp_disable)
  319. usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
  320. writel(usbcfg, &regs->gusbcfg);
  321. /* Program the GAHBCFG Register. */
  322. switch (readl(&regs->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
  323. case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
  324. break;
  325. case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
  326. while (brst_sz > 1) {
  327. ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
  328. ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
  329. brst_sz >>= 1;
  330. }
  331. #ifdef CONFIG_DWC2_DMA_ENABLE
  332. ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
  333. #endif
  334. break;
  335. case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
  336. ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
  337. #ifdef CONFIG_DWC2_DMA_ENABLE
  338. ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
  339. #endif
  340. break;
  341. }
  342. writel(ahbcfg, &regs->gahbcfg);
  343. /* Program the capabilities in GUSBCFG Register */
  344. usbcfg = 0;
  345. if (!priv->hnp_srp_disable)
  346. usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
  347. setbits_le32(&regs->gusbcfg, usbcfg);
  348. }
  349. /*
  350. * Prepares a host channel for transferring packets to/from a specific
  351. * endpoint. The HCCHARn register is set up with the characteristics specified
  352. * in _hc. Host channel interrupts that may need to be serviced while this
  353. * transfer is in progress are enabled.
  354. *
  355. * @param regs Programming view of DWC_otg controller
  356. * @param hc Information needed to initialize the host channel
  357. */
  358. static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
  359. struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
  360. uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
  361. {
  362. struct dwc2_hc_regs *hc_regs = &regs->hc_regs[hc_num];
  363. uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
  364. (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
  365. (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
  366. (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
  367. (max_packet << DWC2_HCCHAR_MPS_OFFSET);
  368. if (dev->speed == USB_SPEED_LOW)
  369. hcchar |= DWC2_HCCHAR_LSPDDEV;
  370. /*
  371. * Program the HCCHARn register with the endpoint characteristics
  372. * for the current transfer.
  373. */
  374. writel(hcchar, &hc_regs->hcchar);
  375. /* Program the HCSPLIT register, default to no SPLIT */
  376. writel(0, &hc_regs->hcsplt);
  377. }
  378. static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
  379. uint8_t hub_devnum, uint8_t hub_port)
  380. {
  381. uint32_t hcsplt = 0;
  382. hcsplt = DWC2_HCSPLT_SPLTENA;
  383. hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
  384. hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
  385. /* Program the HCSPLIT register for SPLITs */
  386. writel(hcsplt, &hc_regs->hcsplt);
  387. }
  388. /*
  389. * DWC2 to USB API interface
  390. */
  391. /* Direction: In ; Request: Status */
  392. static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
  393. struct usb_device *dev, void *buffer,
  394. int txlen, struct devrequest *cmd)
  395. {
  396. uint32_t hprt0 = 0;
  397. uint32_t port_status = 0;
  398. uint32_t port_change = 0;
  399. int len = 0;
  400. int stat = 0;
  401. switch (cmd->requesttype & ~USB_DIR_IN) {
  402. case 0:
  403. *(uint16_t *)buffer = cpu_to_le16(1);
  404. len = 2;
  405. break;
  406. case USB_RECIP_INTERFACE:
  407. case USB_RECIP_ENDPOINT:
  408. *(uint16_t *)buffer = cpu_to_le16(0);
  409. len = 2;
  410. break;
  411. case USB_TYPE_CLASS:
  412. *(uint32_t *)buffer = cpu_to_le32(0);
  413. len = 4;
  414. break;
  415. case USB_RECIP_OTHER | USB_TYPE_CLASS:
  416. hprt0 = readl(&regs->hprt0);
  417. if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
  418. port_status |= USB_PORT_STAT_CONNECTION;
  419. if (hprt0 & DWC2_HPRT0_PRTENA)
  420. port_status |= USB_PORT_STAT_ENABLE;
  421. if (hprt0 & DWC2_HPRT0_PRTSUSP)
  422. port_status |= USB_PORT_STAT_SUSPEND;
  423. if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
  424. port_status |= USB_PORT_STAT_OVERCURRENT;
  425. if (hprt0 & DWC2_HPRT0_PRTRST)
  426. port_status |= USB_PORT_STAT_RESET;
  427. if (hprt0 & DWC2_HPRT0_PRTPWR)
  428. port_status |= USB_PORT_STAT_POWER;
  429. if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
  430. port_status |= USB_PORT_STAT_LOW_SPEED;
  431. else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
  432. DWC2_HPRT0_PRTSPD_HIGH)
  433. port_status |= USB_PORT_STAT_HIGH_SPEED;
  434. if (hprt0 & DWC2_HPRT0_PRTENCHNG)
  435. port_change |= USB_PORT_STAT_C_ENABLE;
  436. if (hprt0 & DWC2_HPRT0_PRTCONNDET)
  437. port_change |= USB_PORT_STAT_C_CONNECTION;
  438. if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
  439. port_change |= USB_PORT_STAT_C_OVERCURRENT;
  440. *(uint32_t *)buffer = cpu_to_le32(port_status |
  441. (port_change << 16));
  442. len = 4;
  443. break;
  444. default:
  445. //puts("unsupported root hub command\n");
  446. stat = USB_ST_STALLED;
  447. }
  448. dev->act_len = min(len, txlen);
  449. dev->status = stat;
  450. return stat;
  451. }
  452. /* Direction: In ; Request: Descriptor */
  453. static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
  454. void *buffer, int txlen,
  455. struct devrequest *cmd)
  456. {
  457. unsigned char data[32];
  458. uint32_t dsc;
  459. int len = 0;
  460. int stat = 0;
  461. uint16_t wValue = cpu_to_le16(cmd->value);
  462. uint16_t wLength = cpu_to_le16(cmd->length);
  463. switch (cmd->requesttype & ~USB_DIR_IN) {
  464. case 0:
  465. {
  466. switch (wValue & 0xff00) {
  467. case 0x0100: /* device descriptor */
  468. len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
  469. memcpy(buffer, root_hub_dev_des, len);
  470. break;
  471. case 0x0200: /* configuration descriptor */
  472. len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
  473. memcpy(buffer, root_hub_config_des, len);
  474. break;
  475. case 0x0300: /* string descriptors */
  476. switch (wValue & 0xff) {
  477. case 0x00:
  478. len = min3(txlen, (int)sizeof(root_hub_str_index0),
  479. (int)wLength);
  480. memcpy(buffer, root_hub_str_index0, len);
  481. break;
  482. case 0x01:
  483. len = min3(txlen, (int)sizeof(root_hub_str_index1),
  484. (int)wLength);
  485. memcpy(buffer, root_hub_str_index1, len);
  486. break;
  487. }
  488. break;
  489. default:
  490. stat = USB_ST_STALLED;
  491. }
  492. break;
  493. }
  494. case USB_TYPE_CLASS:
  495. /* Root port config, set 1 port and nothing else. */
  496. dsc = 0x00000001;
  497. data[0] = 9; /* min length; */
  498. data[1] = 0x29;
  499. data[2] = dsc & RH_A_NDP;
  500. data[3] = 0;
  501. if (dsc & RH_A_PSM)
  502. data[3] |= 0x1;
  503. if (dsc & RH_A_NOCP)
  504. data[3] |= 0x10;
  505. else if (dsc & RH_A_OCPM)
  506. data[3] |= 0x8;
  507. /* corresponds to data[4-7] */
  508. data[5] = (dsc & RH_A_POTPGT) >> 24;
  509. data[7] = dsc & RH_B_DR;
  510. if (data[2] < 7) {
  511. data[8] = 0xff;
  512. } else {
  513. data[0] += 2;
  514. data[8] = (dsc & RH_B_DR) >> 8;
  515. data[9] = 0xff;
  516. data[10] = data[9];
  517. }
  518. len = min3(txlen, (int)data[0], (int)wLength);
  519. memcpy(buffer, data, len);
  520. break;
  521. default:
  522. //puts("unsupported root hub command\n");
  523. stat = USB_ST_STALLED;
  524. }
  525. dev->act_len = min(len, txlen);
  526. dev->status = stat;
  527. return stat;
  528. }
  529. /* Direction: In ; Request: Configuration */
  530. static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
  531. void *buffer, int txlen,
  532. struct devrequest *cmd)
  533. {
  534. int len = 0;
  535. int stat = 0;
  536. switch (cmd->requesttype & ~USB_DIR_IN) {
  537. case 0:
  538. *(uint8_t *)buffer = 0x01;
  539. len = 1;
  540. break;
  541. default:
  542. //puts("unsupported root hub command\n");
  543. stat = USB_ST_STALLED;
  544. }
  545. dev->act_len = min(len, txlen);
  546. dev->status = stat;
  547. return stat;
  548. }
  549. /* Direction: In */
  550. static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
  551. struct usb_device *dev, void *buffer,
  552. int txlen, struct devrequest *cmd)
  553. {
  554. switch (cmd->request) {
  555. case USB_REQ_GET_STATUS:
  556. return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
  557. txlen, cmd);
  558. case USB_REQ_GET_DESCRIPTOR:
  559. return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
  560. txlen, cmd);
  561. case USB_REQ_GET_CONFIGURATION:
  562. return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
  563. txlen, cmd);
  564. default:
  565. //puts("unsupported root hub command\n");
  566. return USB_ST_STALLED;
  567. }
  568. }
  569. /* Direction: Out */
  570. static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
  571. struct usb_device *dev,
  572. void *buffer, int txlen,
  573. struct devrequest *cmd)
  574. {
  575. struct dwc2_core_regs *regs = priv->regs;
  576. int len = 0;
  577. int stat = 0;
  578. uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
  579. uint16_t wValue = cpu_to_le16(cmd->value);
  580. switch (bmrtype_breq & ~USB_DIR_IN) {
  581. case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
  582. case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
  583. break;
  584. case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
  585. switch (wValue) {
  586. case USB_PORT_FEAT_C_CONNECTION:
  587. setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
  588. break;
  589. }
  590. break;
  591. case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
  592. switch (wValue) {
  593. case USB_PORT_FEAT_SUSPEND:
  594. break;
  595. case USB_PORT_FEAT_RESET:
  596. clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
  597. DWC2_HPRT0_PRTCONNDET |
  598. DWC2_HPRT0_PRTENCHNG |
  599. DWC2_HPRT0_PRTOVRCURRCHNG,
  600. DWC2_HPRT0_PRTRST);
  601. mdelay(50);
  602. clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
  603. break;
  604. case USB_PORT_FEAT_POWER:
  605. clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
  606. DWC2_HPRT0_PRTCONNDET |
  607. DWC2_HPRT0_PRTENCHNG |
  608. DWC2_HPRT0_PRTOVRCURRCHNG,
  609. DWC2_HPRT0_PRTRST);
  610. break;
  611. case USB_PORT_FEAT_ENABLE:
  612. break;
  613. }
  614. break;
  615. case (USB_REQ_SET_ADDRESS << 8):
  616. priv->root_hub_devnum = wValue;
  617. break;
  618. case (USB_REQ_SET_CONFIGURATION << 8):
  619. break;
  620. default:
  621. //puts("unsupported root hub command\n");
  622. stat = USB_ST_STALLED;
  623. }
  624. len = min(len, txlen);
  625. dev->act_len = len;
  626. dev->status = stat;
  627. return stat;
  628. }
  629. static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
  630. unsigned long pipe, void *buffer, int txlen,
  631. struct devrequest *cmd)
  632. {
  633. int stat = 0;
  634. if (usb_pipeint(pipe)) {
  635. //puts("Root-Hub submit IRQ: NOT implemented\n");
  636. return 0;
  637. }
  638. if (cmd->requesttype & USB_DIR_IN)
  639. stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
  640. else
  641. stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
  642. mdelay(1);
  643. return stat;
  644. }
  645. int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
  646. {
  647. int ret;
  648. uint32_t hcint, hctsiz;
  649. ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
  650. 2000, false);
  651. if (ret)
  652. return ret;
  653. hcint = readl(&hc_regs->hcint);
  654. hctsiz = readl(&hc_regs->hctsiz);
  655. *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
  656. DWC2_HCTSIZ_XFERSIZE_OFFSET;
  657. *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
  658. if (hcint & DWC2_HCINT_XFERCOMP)
  659. return 0;
  660. if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
  661. return -EAGAIN;
  662. return -EINVAL;
  663. }
  664. static int dwc2_eptype[] = {
  665. DWC2_HCCHAR_EPTYPE_ISOC,
  666. DWC2_HCCHAR_EPTYPE_INTR,
  667. DWC2_HCCHAR_EPTYPE_CONTROL,
  668. DWC2_HCCHAR_EPTYPE_BULK,
  669. };
  670. static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
  671. u8 *pid, int in, void *buffer, int num_packets,
  672. int xfer_len, int *actual_len, int odd_frame)
  673. {
  674. int ret = 0;
  675. uint32_t sub;
  676. writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
  677. (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
  678. (*pid << DWC2_HCTSIZ_PID_OFFSET),
  679. &hc_regs->hctsiz);
  680. if (xfer_len) {
  681. if (in) {
  682. CP15_invalidate_dcache_for_dma(
  683. (uint32_t)aligned_buffer,
  684. (uint32_t)aligned_buffer + xfer_len);
  685. } else {
  686. memcpy(aligned_buffer, buffer, xfer_len);
  687. CP15_flush_dcache_for_dma(
  688. (uint32_t)aligned_buffer,
  689. (uint32_t)aligned_buffer + xfer_len);
  690. }
  691. }
  692. writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
  693. /* Clear old interrupt conditions for this host channel. */
  694. writel(0x3fff, &hc_regs->hcint);
  695. /* Set host channel enable after all other setup is complete. */
  696. clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
  697. DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
  698. DWC2_HCCHAR_ODDFRM,
  699. (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
  700. (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
  701. DWC2_HCCHAR_CHEN);
  702. ret = wait_for_chhltd(hc_regs, &sub, pid);
  703. if (ret < 0)
  704. return ret;
  705. if (in) {
  706. if (xfer_len >= sub)
  707. xfer_len -= sub;
  708. CP15_invalidate_dcache_for_dma((unsigned long)aligned_buffer,
  709. (unsigned long)aligned_buffer + xfer_len);
  710. memcpy(buffer, aligned_buffer, xfer_len);
  711. }
  712. *actual_len = xfer_len;
  713. return ret;
  714. }
  715. int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
  716. unsigned long pipe, u8 *pid, int in, void *buffer, int len)
  717. {
  718. struct dwc2_core_regs *regs = priv->regs;
  719. struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
  720. struct dwc2_host_regs *host_regs = &regs->host_regs;
  721. int devnum = usb_pipedevice(pipe);
  722. int ep = usb_pipeendpoint(pipe);
  723. int max = usb_maxpacket(dev, pipe);
  724. int eptype = dwc2_eptype[usb_pipetype(pipe)];
  725. int done = 0;
  726. int ret = 0;
  727. int do_split = 0;
  728. int complete_split = 0;
  729. uint32_t xfer_len;
  730. uint32_t num_packets;
  731. int stop_transfer = 0;
  732. uint32_t max_xfer_len;
  733. int ssplit_frame_num = 0;
  734. max_xfer_len = CONFIG_DWC2_MAX_PACKET_COUNT * max;
  735. if (max_xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
  736. max_xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE;
  737. if (max_xfer_len > DWC2_DATA_BUF_SIZE)
  738. max_xfer_len = DWC2_DATA_BUF_SIZE;
  739. /* Make sure that max_xfer_len is a multiple of max packet size. */
  740. num_packets = max_xfer_len / max;
  741. max_xfer_len = num_packets * max;
  742. /* Initialize channel */
  743. dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
  744. eptype, max);
  745. /* Check if the target is a FS/LS device behind a HS hub */
  746. if (dev->speed != USB_SPEED_HIGH) {
  747. uint8_t hub_addr;
  748. uint8_t hub_port;
  749. uint32_t hprt0 = readl(&regs->hprt0);
  750. if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
  751. DWC2_HPRT0_PRTSPD_HIGH) {
  752. usb_find_usb2_hub_address_port(dev, &hub_addr,
  753. &hub_port);
  754. dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
  755. do_split = 1;
  756. num_packets = 1;
  757. max_xfer_len = max;
  758. }
  759. }
  760. do {
  761. int actual_len = 0;
  762. uint32_t hcint;
  763. int odd_frame = 0;
  764. xfer_len = len - done;
  765. if (xfer_len > max_xfer_len)
  766. xfer_len = max_xfer_len;
  767. else if (xfer_len > max)
  768. num_packets = (xfer_len + max - 1) / max;
  769. else
  770. num_packets = 1;
  771. if (complete_split)
  772. setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
  773. else if (do_split)
  774. clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
  775. if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
  776. int uframe_num = readl(&host_regs->hfnum);
  777. if (!(uframe_num & 0x1))
  778. odd_frame = 1;
  779. }
  780. ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
  781. in, (char *)buffer + done, num_packets,
  782. xfer_len, &actual_len, odd_frame);
  783. hcint = readl(&hc_regs->hcint);
  784. if (complete_split) {
  785. stop_transfer = 0;
  786. if (hcint & DWC2_HCINT_NYET) {
  787. ret = 0;
  788. int frame_num = DWC2_HFNUM_MAX_FRNUM &
  789. readl(&host_regs->hfnum);
  790. if (((frame_num - ssplit_frame_num) &
  791. DWC2_HFNUM_MAX_FRNUM) > 4)
  792. ret = -EAGAIN;
  793. } else
  794. complete_split = 0;
  795. } else if (do_split) {
  796. if (hcint & DWC2_HCINT_ACK) {
  797. ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
  798. readl(&host_regs->hfnum);
  799. ret = 0;
  800. complete_split = 1;
  801. }
  802. }
  803. if (ret)
  804. break;
  805. if (actual_len < xfer_len)
  806. stop_transfer = 1;
  807. done += actual_len;
  808. /* Transactions are done when when either all data is transferred or
  809. * there is a short transfer. In case of a SPLIT make sure the CSPLIT
  810. * is executed.
  811. */
  812. } while (((done < len) && !stop_transfer) || complete_split);
  813. writel(0, &hc_regs->hcintmsk);
  814. writel(0xFFFFFFFF, &hc_regs->hcint);
  815. dev->status = 0;
  816. dev->act_len = done;
  817. return ret;
  818. }
  819. /* U-Boot USB transmission interface */
  820. int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
  821. unsigned long pipe, void *buffer, int len)
  822. {
  823. int devnum = usb_pipedevice(pipe);
  824. int ep = usb_pipeendpoint(pipe);
  825. u8* pid;
  826. if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
  827. dev->status = 0;
  828. return -EINVAL;
  829. }
  830. if (usb_pipein(pipe))
  831. pid = &priv->in_data_toggle[devnum][ep];
  832. else
  833. pid = &priv->out_data_toggle[devnum][ep];
  834. return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
  835. }
  836. static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
  837. unsigned long pipe, void *buffer, int len,
  838. struct devrequest *setup)
  839. {
  840. int devnum = usb_pipedevice(pipe);
  841. int ret, act_len;
  842. u8 pid;
  843. /* For CONTROL endpoint pid should start with DATA1 */
  844. int status_direction;
  845. if (devnum == priv->root_hub_devnum) {
  846. dev->status = 0;
  847. dev->speed = USB_SPEED_HIGH;
  848. return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
  849. setup);
  850. }
  851. /* SETUP stage */
  852. pid = DWC2_HC_PID_SETUP;
  853. do {
  854. ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
  855. } while (ret == -EAGAIN);
  856. if (ret)
  857. return ret;
  858. /* DATA stage */
  859. act_len = 0;
  860. if (buffer) {
  861. pid = DWC2_HC_PID_DATA1;
  862. unsigned char *p = (unsigned char *)buffer;
  863. do {
  864. ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
  865. (void*)p, len);
  866. act_len += dev->act_len;
  867. // buffer += dev->act_len;
  868. p += dev->act_len;
  869. len -= dev->act_len;
  870. } while (ret == -EAGAIN);
  871. if (ret)
  872. return ret;
  873. status_direction = usb_pipeout(pipe);
  874. } else {
  875. /* No-data CONTROL always ends with an IN transaction */
  876. status_direction = 1;
  877. }
  878. /* STATUS stage */
  879. pid = DWC2_HC_PID_DATA1;
  880. do {
  881. ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
  882. priv->status_buffer, 0);
  883. } while (ret == -EAGAIN);
  884. if (ret)
  885. return ret;
  886. dev->act_len = act_len;
  887. return 0;
  888. }
  889. static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
  890. {
  891. struct dwc2_core_regs *regs = priv->regs;
  892. uint32_t snpsid;
  893. int i, j;
  894. snpsid = readl(&regs->gsnpsid);
  895. if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
  896. (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
  897. return -ENODEV;
  898. }
  899. #ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
  900. priv->ext_vbus = 1;
  901. #else
  902. priv->ext_vbus = 0;
  903. #endif
  904. dwc_otg_core_init(priv);
  905. dwc_otg_core_host_init(dev, regs);
  906. /* clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
  907. DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
  908. DWC2_HPRT0_PRTOVRCURRCHNG,
  909. DWC2_HPRT0_PRTRST);
  910. mdelay(50);
  911. clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
  912. DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
  913. DWC2_HPRT0_PRTRST); */
  914. for (i = 0; i < MAX_DEVICE; i++) {
  915. for (j = 0; j < MAX_ENDPOINT; j++) {
  916. priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
  917. priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
  918. }
  919. }
  920. /*
  921. * Add a 1 second delay here. This gives the host controller
  922. * a bit time before the comminucation with the USB devices
  923. * is started (the bus is scanned) and fixes the USB detection
  924. * problems with some problematic USB keys.
  925. */
  926. if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
  927. mdelay(1000);
  928. return 0;
  929. }
  930. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  931. int len, struct devrequest *setup)
  932. {
  933. unsigned long timeout;
  934. int ret;
  935. /* FIXME: what is interval? */
  936. timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
  937. for (;;) {
  938. if (get_timer(0) > timeout) {
  939. //dev_err(dev, "Timeout poll on interrupt endpoint\n");
  940. return -ETIMEDOUT;
  941. }
  942. ret = _submit_control_msg(&local, dev, pipe, buffer, len, setup);
  943. if (ret != -EAGAIN)
  944. return ret;
  945. mdelay(10);
  946. }
  947. }
  948. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  949. int len)
  950. {
  951. unsigned long timeout;
  952. int ret;
  953. /* FIXME: what is interval? */
  954. timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
  955. for (;;) {
  956. if (get_timer(0) > timeout) {
  957. //dev_err(dev, "Timeout poll on interrupt endpoint\n");
  958. return -ETIMEDOUT;
  959. }
  960. ret = _submit_bulk_msg(&local, dev, pipe, buffer, len);
  961. if (ret != -EAGAIN)
  962. return ret;
  963. mdelay(10);
  964. }
  965. }
  966. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  967. int len, int interval)
  968. {
  969. unsigned long timeout;
  970. int ret;
  971. /* FIXME: what is interval? */
  972. timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
  973. for (;;) {
  974. if (get_timer(0) > timeout) {
  975. //dev_err(dev, "Timeout poll on interrupt endpoint\n");
  976. return -ETIMEDOUT;
  977. }
  978. ret = _submit_bulk_msg(&local, dev, pipe, buffer, len);
  979. if (ret != -EAGAIN)
  980. return ret;
  981. mdelay(10);
  982. }
  983. }
  984. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  985. {
  986. struct dwc2_priv *priv = &local;
  987. /* aligned_buffer_addr = (uint8_t *)malloc(DWC2_DATA_BUF_SIZE + ARCH_DMA_MINALIGN - 1);
  988. if (!aligned_buffer_addr)
  989. {
  990. SendUartString("malloc aligned_buffer error!/n");
  991. return (-ENOMEM);
  992. }
  993. status_buffer_addr = (uint8_t *)malloc(DWC2_STATUS_BUF_SIZE + ARCH_DMA_MINALIGN - 1);
  994. if (!status_buffer_addr)
  995. {
  996. SendUartString("malloc status_buffer error!/n");
  997. return (-ENOMEM);
  998. }*/
  999. memset(priv, '\0', sizeof(*priv));
  1000. priv->root_hub_devnum = 0;
  1001. priv->hnp_srp_disable = 1;
  1002. priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
  1003. priv->aligned_buffer = aligned_buffer_addr;
  1004. priv->status_buffer = status_buffer_addr;
  1005. return dwc2_init_common(NULL, priv);
  1006. }