dwc2.c 24 KB

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