dwc2.c 32 KB

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