xhci-keystone.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * USB 3.0 DRD Controller
  3. *
  4. * (C) Copyright 2012-2014
  5. * Texas Instruments Incorporated, <www.ti.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <watchdog.h>
  11. #include <usb.h>
  12. #include <asm/arch/psc_defs.h>
  13. #include <asm/io.h>
  14. #include <linux/usb/dwc3.h>
  15. #include <asm/arch/xhci-keystone.h>
  16. #include <asm-generic/errno.h>
  17. #include <linux/list.h>
  18. #include "xhci.h"
  19. struct kdwc3_irq_regs {
  20. u32 revision; /* 0x000 */
  21. u32 rsvd0[3];
  22. u32 sysconfig; /* 0x010 */
  23. u32 rsvd1[1];
  24. u32 irq_eoi;
  25. u32 rsvd2[1];
  26. struct {
  27. u32 raw_status;
  28. u32 status;
  29. u32 enable_set;
  30. u32 enable_clr;
  31. } irqs[16];
  32. };
  33. struct keystone_xhci {
  34. struct xhci_hccr *hcd;
  35. struct dwc3 *dwc3_reg;
  36. struct xhci_hcor *hcor;
  37. struct kdwc3_irq_regs *usbss;
  38. struct keystone_xhci_phy *phy;
  39. };
  40. struct keystone_xhci keystone;
  41. static void keystone_xhci_phy_set(struct keystone_xhci_phy *phy)
  42. {
  43. u32 val;
  44. /*
  45. * VBUSVLDEXTSEL has a default value of 1 in BootCfg but shouldn't.
  46. * It should always be cleared because our USB PHY has an onchip VBUS
  47. * analog comparator.
  48. */
  49. val = readl(&phy->phy_clock);
  50. /* quit selecting the vbusvldextsel by default! */
  51. val &= ~USB3_PHY_OTG_VBUSVLDECTSEL;
  52. writel(val, &phy->phy_clock);
  53. }
  54. static void keystone_xhci_phy_unset(struct keystone_xhci_phy *phy)
  55. {
  56. u32 val;
  57. /* Disable the PHY REFCLK clock gate */
  58. val = readl(&phy->phy_clock);
  59. val &= ~USB3_PHY_REF_SSP_EN;
  60. writel(val, &phy->phy_clock);
  61. }
  62. static void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
  63. {
  64. clrsetbits_le32(&dwc3_reg->g_ctl,
  65. DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
  66. DWC3_GCTL_PRTCAPDIR(mode));
  67. }
  68. static void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
  69. {
  70. /* Before Resetting PHY, put Core in Reset */
  71. setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
  72. /* Assert USB3 PHY reset */
  73. setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
  74. /* Assert USB2 PHY reset */
  75. setbits_le32(&dwc3_reg->g_usb2phycfg[0], DWC3_GUSB2PHYCFG_PHYSOFTRST);
  76. mdelay(100);
  77. /* Clear USB3 PHY reset */
  78. clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
  79. /* Clear USB2 PHY reset */
  80. clrbits_le32(&dwc3_reg->g_usb2phycfg[0], DWC3_GUSB2PHYCFG_PHYSOFTRST);
  81. /* After PHYs are stable we can take Core out of reset state */
  82. clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
  83. }
  84. static int dwc3_core_init(struct dwc3 *dwc3_reg)
  85. {
  86. u32 revision, val;
  87. unsigned long t_rst;
  88. unsigned int dwc3_hwparams1;
  89. revision = readl(&dwc3_reg->g_snpsid);
  90. /* This should read as U3 followed by revision number */
  91. if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
  92. puts("this is not a DesignWare USB3 DRD Core\n");
  93. return -EINVAL;
  94. }
  95. /* issue device SoftReset too */
  96. writel(DWC3_DCTL_CSFTRST, &dwc3_reg->d_ctl);
  97. t_rst = get_timer(0);
  98. do {
  99. val = readl(&dwc3_reg->d_ctl);
  100. if (!(val & DWC3_DCTL_CSFTRST))
  101. break;
  102. WATCHDOG_RESET();
  103. } while (get_timer(t_rst) < 500);
  104. if (val & DWC3_DCTL_CSFTRST) {
  105. debug("Reset timed out\n");
  106. return -2;
  107. }
  108. dwc3_core_soft_reset(dwc3_reg);
  109. dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
  110. val = readl(&dwc3_reg->g_ctl);
  111. val &= ~DWC3_GCTL_SCALEDOWN_MASK;
  112. val &= ~DWC3_GCTL_DISSCRAMBLE;
  113. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
  114. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  115. val &= ~DWC3_GCTL_DSBLCLKGTNG;
  116. break;
  117. default:
  118. printf("No power optimization available\n");
  119. }
  120. /*
  121. * WORKAROUND: DWC3 revisions <1.90a have a bug
  122. * where the device can fail to connect at SuperSpeed
  123. * and falls back to high-speed mode which causes
  124. * the device to enter a Connect/Disconnect loop
  125. */
  126. if ((revision & DWC3_REVISION_MASK) < 0x190a)
  127. val |= DWC3_GCTL_U2RSTECN;
  128. writel(val, &dwc3_reg->g_ctl);
  129. return 0;
  130. }
  131. static int keystone_xhci_core_init(struct dwc3 *dwc3_reg)
  132. {
  133. int ret;
  134. ret = dwc3_core_init(dwc3_reg);
  135. if (ret) {
  136. debug("failed to initialize core\n");
  137. return -EINVAL;
  138. }
  139. /* We are hard-coding DWC3 core to Host Mode */
  140. dwc3_set_mode(dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
  141. return 0;
  142. }
  143. int xhci_hcd_init(int index,
  144. struct xhci_hccr **ret_hccr, struct xhci_hcor **ret_hcor)
  145. {
  146. u32 val;
  147. int ret;
  148. struct xhci_hccr *hcd;
  149. struct xhci_hcor *hcor;
  150. struct kdwc3_irq_regs *usbss;
  151. struct keystone_xhci_phy *phy;
  152. usbss = (struct kdwc3_irq_regs *)CONFIG_USB_SS_BASE;
  153. phy = (struct keystone_xhci_phy *)CONFIG_DEV_USB_PHY_BASE;
  154. /* Enable the PHY REFCLK clock gate with phy_ref_ssp_en = 1 */
  155. val = readl(&(phy->phy_clock));
  156. val |= USB3_PHY_REF_SSP_EN;
  157. writel(val, &phy->phy_clock);
  158. mdelay(100);
  159. /* Release USB from reset */
  160. ret = psc_enable_module(KS2_LPSC_USB);
  161. if (ret) {
  162. puts("Cannot enable USB module");
  163. return -1;
  164. }
  165. mdelay(100);
  166. /* Initialize usb phy */
  167. keystone_xhci_phy_set(phy);
  168. /* soft reset usbss */
  169. writel(1, &usbss->sysconfig);
  170. while (readl(&usbss->sysconfig) & 1)
  171. ;
  172. val = readl(&usbss->revision);
  173. debug("usbss revision %x\n", val);
  174. /* Initialize usb core */
  175. hcd = (struct xhci_hccr *)CONFIG_USB_HOST_XHCI_BASE;
  176. keystone.dwc3_reg = (struct dwc3 *)(CONFIG_USB_HOST_XHCI_BASE +
  177. DWC3_REG_OFFSET);
  178. keystone_xhci_core_init(keystone.dwc3_reg);
  179. /* set register addresses */
  180. hcor = (struct xhci_hcor *)((uint32_t)hcd +
  181. HC_LENGTH(readl(&hcd->cr_capbase)));
  182. debug("Keystone2-xhci: init hccr %08x and hcor %08x hc_length %d\n",
  183. (u32)hcd, (u32)hcor,
  184. (u32)HC_LENGTH(xhci_readl(&hcd->cr_capbase)));
  185. keystone.usbss = usbss;
  186. keystone.phy = phy;
  187. keystone.hcd = hcd;
  188. keystone.hcor = hcor;
  189. *ret_hccr = hcd;
  190. *ret_hcor = hcor;
  191. return 0;
  192. }
  193. static int keystone_xhci_phy_suspend(void)
  194. {
  195. int loop_cnt = 0;
  196. struct xhci_hcor *hcor;
  197. uint32_t *portsc_1 = NULL;
  198. uint32_t *portsc_2 = NULL;
  199. u32 val, usb2_pls, usb3_pls, event_q;
  200. struct dwc3 *dwc3_reg = keystone.dwc3_reg;
  201. /* set register addresses */
  202. hcor = keystone.hcor;
  203. /* Bypass Scrambling and Set Shorter Training sequence for simulation */
  204. val = DWC3_GCTL_PWRDNSCALE(0x4b0) | DWC3_GCTL_PRTCAPDIR(0x2);
  205. writel(val, &dwc3_reg->g_ctl);
  206. /* GUSB2PHYCFG */
  207. val = readl(&dwc3_reg->g_usb2phycfg[0]);
  208. /* assert bit 6 (SusPhy) */
  209. val |= DWC3_GUSB2PHYCFG_SUSPHY;
  210. writel(val, &dwc3_reg->g_usb2phycfg[0]);
  211. /* GUSB3PIPECTL */
  212. val = readl(&dwc3_reg->g_usb3pipectl[0]);
  213. /*
  214. * assert bit 29 to allow PHY to go to suspend when idle
  215. * and cause the USB3 SS PHY to enter suspend mode
  216. */
  217. val |= (BIT(29) | DWC3_GUSB3PIPECTL_SUSPHY);
  218. writel(val, &dwc3_reg->g_usb3pipectl[0]);
  219. /*
  220. * Steps necessary to allow controller to suspend even when
  221. * VBUS is HIGH:
  222. * - Init DCFG[2:0] (DevSpd) to: 1=FS
  223. * - Init GEVNTADR0 to point to an eventQ
  224. * - Init GEVNTSIZ0 to 0x0100 to specify the size of the eventQ
  225. * - Init DCTL::Run_nStop = 1
  226. */
  227. writel(0x00020001, &dwc3_reg->d_cfg);
  228. /* TODO: local2global( (Uint32) eventQ )? */
  229. writel((u32)&event_q, &dwc3_reg->g_evnt_buf[0].g_evntadrlo);
  230. writel(0, &dwc3_reg->g_evnt_buf[0].g_evntadrhi);
  231. writel(0x4, &dwc3_reg->g_evnt_buf[0].g_evntsiz);
  232. /* Run */
  233. writel(DWC3_DCTL_RUN_STOP, &dwc3_reg->d_ctl);
  234. mdelay(100);
  235. /* Wait for USB2 & USB3 PORTSC::PortLinkState to indicate suspend */
  236. portsc_1 = (uint32_t *)(&hcor->portregs[0].or_portsc);
  237. portsc_2 = (uint32_t *)(&hcor->portregs[1].or_portsc);
  238. usb2_pls = 0;
  239. usb3_pls = 0;
  240. do {
  241. ++loop_cnt;
  242. usb2_pls = (readl(portsc_1) & PORT_PLS_MASK) >> 5;
  243. usb3_pls = (readl(portsc_2) & PORT_PLS_MASK) >> 5;
  244. } while (((usb2_pls != 0x4) || (usb3_pls != 0x4)) && loop_cnt < 1000);
  245. if (usb2_pls != 0x4 || usb3_pls != 0x4) {
  246. debug("USB suspend failed - PLS USB2=%02x, USB3=%02x\n",
  247. usb2_pls, usb3_pls);
  248. return -1;
  249. }
  250. debug("USB2 and USB3 PLS - Disabled, loop_cnt=%d\n", loop_cnt);
  251. return 0;
  252. }
  253. void xhci_hcd_stop(int index)
  254. {
  255. /* Disable USB */
  256. if (keystone_xhci_phy_suspend())
  257. return;
  258. if (psc_disable_module(KS2_LPSC_USB)) {
  259. debug("PSC disable module USB failed!\n");
  260. return;
  261. }
  262. /* Disable PHY */
  263. keystone_xhci_phy_unset(keystone.phy);
  264. /* memset(&keystone, 0, sizeof(struct keystone_xhci)); */
  265. debug("xhci_hcd_stop OK.\n");
  266. }