2 * USB 3.0 DRD Controller
4 * (C) Copyright 2012-2014
5 * Texas Instruments Incorporated, <www.ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/arch/psc_defs.h>
15 #include <linux/usb/dwc3.h>
16 #include <asm/arch/xhci-keystone.h>
17 #include <asm-generic/errno.h>
18 #include <linux/list.h>
21 struct kdwc3_irq_regs {
22 u32 revision; /* 0x000 */
24 u32 sysconfig; /* 0x010 */
36 struct keystone_xhci {
37 struct xhci_hccr *hcd;
38 struct dwc3 *dwc3_reg;
39 struct xhci_hcor *hcor;
40 struct kdwc3_irq_regs *usbss;
41 struct keystone_xhci_phy *phy;
44 struct keystone_xhci keystone;
46 static void keystone_xhci_phy_set(struct keystone_xhci_phy *phy)
51 * VBUSVLDEXTSEL has a default value of 1 in BootCfg but shouldn't.
52 * It should always be cleared because our USB PHY has an onchip VBUS
55 val = readl(&phy->phy_clock);
56 /* quit selecting the vbusvldextsel by default! */
57 val &= ~USB3_PHY_OTG_VBUSVLDECTSEL;
58 writel(val, &phy->phy_clock);
61 static void keystone_xhci_phy_unset(struct keystone_xhci_phy *phy)
65 /* Disable the PHY REFCLK clock gate */
66 val = readl(&phy->phy_clock);
67 val &= ~USB3_PHY_REF_SSP_EN;
68 writel(val, &phy->phy_clock);
71 static int keystone_xhci_core_init(struct dwc3 *dwc3_reg)
75 ret = dwc3_core_init(dwc3_reg);
77 debug("failed to initialize core\n");
81 /* We are hard-coding DWC3 core to Host Mode */
82 dwc3_set_mode(dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
87 int xhci_hcd_init(int index,
88 struct xhci_hccr **ret_hccr, struct xhci_hcor **ret_hcor)
92 struct xhci_hccr *hcd;
93 struct xhci_hcor *hcor;
94 struct kdwc3_irq_regs *usbss;
95 struct keystone_xhci_phy *phy;
97 usbss = (struct kdwc3_irq_regs *)CONFIG_USB_SS_BASE;
98 phy = (struct keystone_xhci_phy *)CONFIG_DEV_USB_PHY_BASE;
100 /* Enable the PHY REFCLK clock gate with phy_ref_ssp_en = 1 */
101 val = readl(&(phy->phy_clock));
102 val |= USB3_PHY_REF_SSP_EN;
103 writel(val, &phy->phy_clock);
107 /* Release USB from reset */
108 ret = psc_enable_module(KS2_LPSC_USB);
110 puts("Cannot enable USB module");
116 /* Initialize usb phy */
117 keystone_xhci_phy_set(phy);
119 /* soft reset usbss */
120 writel(1, &usbss->sysconfig);
121 while (readl(&usbss->sysconfig) & 1)
124 val = readl(&usbss->revision);
125 debug("usbss revision %x\n", val);
127 /* Initialize usb core */
128 hcd = (struct xhci_hccr *)CONFIG_USB_HOST_XHCI_BASE;
129 keystone.dwc3_reg = (struct dwc3 *)(CONFIG_USB_HOST_XHCI_BASE +
132 keystone_xhci_core_init(keystone.dwc3_reg);
134 /* set register addresses */
135 hcor = (struct xhci_hcor *)((uint32_t)hcd +
136 HC_LENGTH(readl(&hcd->cr_capbase)));
138 debug("Keystone2-xhci: init hccr %08x and hcor %08x hc_length %d\n",
140 (u32)HC_LENGTH(xhci_readl(&hcd->cr_capbase)));
142 keystone.usbss = usbss;
145 keystone.hcor = hcor;
153 static int keystone_xhci_phy_suspend(void)
156 struct xhci_hcor *hcor;
157 uint32_t *portsc_1 = NULL;
158 uint32_t *portsc_2 = NULL;
159 u32 val, usb2_pls, usb3_pls, event_q;
160 struct dwc3 *dwc3_reg = keystone.dwc3_reg;
162 /* set register addresses */
163 hcor = keystone.hcor;
165 /* Bypass Scrambling and Set Shorter Training sequence for simulation */
166 val = DWC3_GCTL_PWRDNSCALE(0x4b0) | DWC3_GCTL_PRTCAPDIR(0x2);
167 writel(val, &dwc3_reg->g_ctl);
170 val = readl(&dwc3_reg->g_usb2phycfg[0]);
172 /* assert bit 6 (SusPhy) */
173 val |= DWC3_GUSB2PHYCFG_SUSPHY;
174 writel(val, &dwc3_reg->g_usb2phycfg[0]);
177 val = readl(&dwc3_reg->g_usb3pipectl[0]);
180 * assert bit 29 to allow PHY to go to suspend when idle
181 * and cause the USB3 SS PHY to enter suspend mode
183 val |= (BIT(29) | DWC3_GUSB3PIPECTL_SUSPHY);
184 writel(val, &dwc3_reg->g_usb3pipectl[0]);
187 * Steps necessary to allow controller to suspend even when
189 * - Init DCFG[2:0] (DevSpd) to: 1=FS
190 * - Init GEVNTADR0 to point to an eventQ
191 * - Init GEVNTSIZ0 to 0x0100 to specify the size of the eventQ
192 * - Init DCTL::Run_nStop = 1
194 writel(0x00020001, &dwc3_reg->d_cfg);
195 /* TODO: local2global( (Uint32) eventQ )? */
196 writel((u32)&event_q, &dwc3_reg->g_evnt_buf[0].g_evntadrlo);
197 writel(0, &dwc3_reg->g_evnt_buf[0].g_evntadrhi);
198 writel(0x4, &dwc3_reg->g_evnt_buf[0].g_evntsiz);
200 writel(DWC3_DCTL_RUN_STOP, &dwc3_reg->d_ctl);
204 /* Wait for USB2 & USB3 PORTSC::PortLinkState to indicate suspend */
205 portsc_1 = (uint32_t *)(&hcor->portregs[0].or_portsc);
206 portsc_2 = (uint32_t *)(&hcor->portregs[1].or_portsc);
211 usb2_pls = (readl(portsc_1) & PORT_PLS_MASK) >> 5;
212 usb3_pls = (readl(portsc_2) & PORT_PLS_MASK) >> 5;
213 } while (((usb2_pls != 0x4) || (usb3_pls != 0x4)) && loop_cnt < 1000);
215 if (usb2_pls != 0x4 || usb3_pls != 0x4) {
216 debug("USB suspend failed - PLS USB2=%02x, USB3=%02x\n",
221 debug("USB2 and USB3 PLS - Disabled, loop_cnt=%d\n", loop_cnt);
225 void xhci_hcd_stop(int index)
228 if (keystone_xhci_phy_suspend())
231 if (psc_disable_module(KS2_LPSC_USB)) {
232 debug("PSC disable module USB failed!\n");
237 keystone_xhci_phy_unset(keystone.phy);
239 /* memset(&keystone, 0, sizeof(struct keystone_xhci)); */
240 debug("xhci_hcd_stop OK.\n");