xhci-keystone.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 <linux/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 int keystone_xhci_core_init(struct dwc3 *dwc3_reg)
  63. {
  64. int ret;
  65. ret = dwc3_core_init(dwc3_reg);
  66. if (ret) {
  67. debug("failed to initialize core\n");
  68. return -EINVAL;
  69. }
  70. /* We are hard-coding DWC3 core to Host Mode */
  71. dwc3_set_mode(dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
  72. return 0;
  73. }
  74. int xhci_hcd_init(int index,
  75. struct xhci_hccr **ret_hccr, struct xhci_hcor **ret_hcor)
  76. {
  77. u32 val;
  78. int ret;
  79. struct xhci_hccr *hcd;
  80. struct xhci_hcor *hcor;
  81. struct kdwc3_irq_regs *usbss;
  82. struct keystone_xhci_phy *phy;
  83. usbss = (struct kdwc3_irq_regs *)CONFIG_USB_SS_BASE;
  84. phy = (struct keystone_xhci_phy *)CONFIG_DEV_USB_PHY_BASE;
  85. /* Enable the PHY REFCLK clock gate with phy_ref_ssp_en = 1 */
  86. val = readl(&(phy->phy_clock));
  87. val |= USB3_PHY_REF_SSP_EN;
  88. writel(val, &phy->phy_clock);
  89. mdelay(100);
  90. /* Release USB from reset */
  91. ret = psc_enable_module(KS2_LPSC_USB);
  92. if (ret) {
  93. puts("Cannot enable USB module");
  94. return -1;
  95. }
  96. mdelay(100);
  97. /* Initialize usb phy */
  98. keystone_xhci_phy_set(phy);
  99. /* soft reset usbss */
  100. writel(1, &usbss->sysconfig);
  101. while (readl(&usbss->sysconfig) & 1)
  102. ;
  103. val = readl(&usbss->revision);
  104. debug("usbss revision %x\n", val);
  105. /* Initialize usb core */
  106. hcd = (struct xhci_hccr *)CONFIG_USB_HOST_XHCI_BASE;
  107. keystone.dwc3_reg = (struct dwc3 *)(CONFIG_USB_HOST_XHCI_BASE +
  108. DWC3_REG_OFFSET);
  109. keystone_xhci_core_init(keystone.dwc3_reg);
  110. /* set register addresses */
  111. hcor = (struct xhci_hcor *)((uint32_t)hcd +
  112. HC_LENGTH(readl(&hcd->cr_capbase)));
  113. debug("Keystone2-xhci: init hccr %08x and hcor %08x hc_length %d\n",
  114. (u32)hcd, (u32)hcor,
  115. (u32)HC_LENGTH(xhci_readl(&hcd->cr_capbase)));
  116. keystone.usbss = usbss;
  117. keystone.phy = phy;
  118. keystone.hcd = hcd;
  119. keystone.hcor = hcor;
  120. *ret_hccr = hcd;
  121. *ret_hcor = hcor;
  122. return 0;
  123. }
  124. static int keystone_xhci_phy_suspend(void)
  125. {
  126. int loop_cnt = 0;
  127. struct xhci_hcor *hcor;
  128. uint32_t *portsc_1 = NULL;
  129. uint32_t *portsc_2 = NULL;
  130. u32 val, usb2_pls, usb3_pls, event_q;
  131. struct dwc3 *dwc3_reg = keystone.dwc3_reg;
  132. /* set register addresses */
  133. hcor = keystone.hcor;
  134. /* Bypass Scrambling and Set Shorter Training sequence for simulation */
  135. val = DWC3_GCTL_PWRDNSCALE(0x4b0) | DWC3_GCTL_PRTCAPDIR(0x2);
  136. writel(val, &dwc3_reg->g_ctl);
  137. /* GUSB2PHYCFG */
  138. val = readl(&dwc3_reg->g_usb2phycfg[0]);
  139. /* assert bit 6 (SusPhy) */
  140. val |= DWC3_GUSB2PHYCFG_SUSPHY;
  141. writel(val, &dwc3_reg->g_usb2phycfg[0]);
  142. /* GUSB3PIPECTL */
  143. val = readl(&dwc3_reg->g_usb3pipectl[0]);
  144. /*
  145. * assert bit 29 to allow PHY to go to suspend when idle
  146. * and cause the USB3 SS PHY to enter suspend mode
  147. */
  148. val |= (BIT(29) | DWC3_GUSB3PIPECTL_SUSPHY);
  149. writel(val, &dwc3_reg->g_usb3pipectl[0]);
  150. /*
  151. * Steps necessary to allow controller to suspend even when
  152. * VBUS is HIGH:
  153. * - Init DCFG[2:0] (DevSpd) to: 1=FS
  154. * - Init GEVNTADR0 to point to an eventQ
  155. * - Init GEVNTSIZ0 to 0x0100 to specify the size of the eventQ
  156. * - Init DCTL::Run_nStop = 1
  157. */
  158. writel(0x00020001, &dwc3_reg->d_cfg);
  159. /* TODO: local2global( (Uint32) eventQ )? */
  160. writel((u32)&event_q, &dwc3_reg->g_evnt_buf[0].g_evntadrlo);
  161. writel(0, &dwc3_reg->g_evnt_buf[0].g_evntadrhi);
  162. writel(0x4, &dwc3_reg->g_evnt_buf[0].g_evntsiz);
  163. /* Run */
  164. writel(DWC3_DCTL_RUN_STOP, &dwc3_reg->d_ctl);
  165. mdelay(100);
  166. /* Wait for USB2 & USB3 PORTSC::PortLinkState to indicate suspend */
  167. portsc_1 = (uint32_t *)(&hcor->portregs[0].or_portsc);
  168. portsc_2 = (uint32_t *)(&hcor->portregs[1].or_portsc);
  169. usb2_pls = 0;
  170. usb3_pls = 0;
  171. do {
  172. ++loop_cnt;
  173. usb2_pls = (readl(portsc_1) & PORT_PLS_MASK) >> 5;
  174. usb3_pls = (readl(portsc_2) & PORT_PLS_MASK) >> 5;
  175. } while (((usb2_pls != 0x4) || (usb3_pls != 0x4)) && loop_cnt < 1000);
  176. if (usb2_pls != 0x4 || usb3_pls != 0x4) {
  177. debug("USB suspend failed - PLS USB2=%02x, USB3=%02x\n",
  178. usb2_pls, usb3_pls);
  179. return -1;
  180. }
  181. debug("USB2 and USB3 PLS - Disabled, loop_cnt=%d\n", loop_cnt);
  182. return 0;
  183. }
  184. void xhci_hcd_stop(int index)
  185. {
  186. /* Disable USB */
  187. if (keystone_xhci_phy_suspend())
  188. return;
  189. if (psc_disable_module(KS2_LPSC_USB)) {
  190. debug("PSC disable module USB failed!\n");
  191. return;
  192. }
  193. /* Disable PHY */
  194. keystone_xhci_phy_unset(keystone.phy);
  195. /* memset(&keystone, 0, sizeof(struct keystone_xhci)); */
  196. debug("xhci_hcd_stop OK.\n");
  197. }