dwc2.c 31 KB

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