dwc2.c 34 KB

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