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 void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
73 clrsetbits_le32(&dwc3_reg->g_ctl,
74 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
75 DWC3_GCTL_PRTCAPDIR(mode));
78 static void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
80 /* Before Resetting PHY, put Core in Reset */
81 setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
83 /* Assert USB3 PHY reset */
84 setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
86 /* Assert USB2 PHY reset */
87 setbits_le32(&dwc3_reg->g_usb2phycfg[0], DWC3_GUSB2PHYCFG_PHYSOFTRST);
91 /* Clear USB3 PHY reset */
92 clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
94 /* Clear USB2 PHY reset */
95 clrbits_le32(&dwc3_reg->g_usb2phycfg[0], DWC3_GUSB2PHYCFG_PHYSOFTRST);
97 /* After PHYs are stable we can take Core out of reset state */
98 clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
101 static int dwc3_core_init(struct dwc3 *dwc3_reg)
105 unsigned int dwc3_hwparams1;
107 revision = readl(&dwc3_reg->g_snpsid);
108 /* This should read as U3 followed by revision number */
109 if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
110 puts("this is not a DesignWare USB3 DRD Core\n");
114 /* issue device SoftReset too */
115 writel(DWC3_DCTL_CSFTRST, &dwc3_reg->d_ctl);
117 t_rst = get_timer(0);
119 val = readl(&dwc3_reg->d_ctl);
120 if (!(val & DWC3_DCTL_CSFTRST))
123 } while (get_timer(t_rst) < 500);
125 if (val & DWC3_DCTL_CSFTRST) {
126 debug("Reset timed out\n");
130 dwc3_core_soft_reset(dwc3_reg);
132 dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
134 val = readl(&dwc3_reg->g_ctl);
135 val &= ~DWC3_GCTL_SCALEDOWN_MASK;
136 val &= ~DWC3_GCTL_DISSCRAMBLE;
137 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
138 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
139 val &= ~DWC3_GCTL_DSBLCLKGTNG;
142 printf("No power optimization available\n");
146 * WORKAROUND: DWC3 revisions <1.90a have a bug
147 * where the device can fail to connect at SuperSpeed
148 * and falls back to high-speed mode which causes
149 * the device to enter a Connect/Disconnect loop
151 if ((revision & DWC3_REVISION_MASK) < 0x190a)
152 val |= DWC3_GCTL_U2RSTECN;
154 writel(val, &dwc3_reg->g_ctl);
159 static int keystone_xhci_core_init(struct dwc3 *dwc3_reg)
163 ret = dwc3_core_init(dwc3_reg);
165 debug("failed to initialize core\n");
169 /* We are hard-coding DWC3 core to Host Mode */
170 dwc3_set_mode(dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
175 int xhci_hcd_init(int index,
176 struct xhci_hccr **ret_hccr, struct xhci_hcor **ret_hcor)
180 struct xhci_hccr *hcd;
181 struct xhci_hcor *hcor;
182 struct kdwc3_irq_regs *usbss;
183 struct keystone_xhci_phy *phy;
185 usbss = (struct kdwc3_irq_regs *)CONFIG_USB_SS_BASE;
186 phy = (struct keystone_xhci_phy *)CONFIG_DEV_USB_PHY_BASE;
188 /* Enable the PHY REFCLK clock gate with phy_ref_ssp_en = 1 */
189 val = readl(&(phy->phy_clock));
190 val |= USB3_PHY_REF_SSP_EN;
191 writel(val, &phy->phy_clock);
195 /* Release USB from reset */
196 ret = psc_enable_module(KS2_LPSC_USB);
198 puts("Cannot enable USB module");
204 /* Initialize usb phy */
205 keystone_xhci_phy_set(phy);
207 /* soft reset usbss */
208 writel(1, &usbss->sysconfig);
209 while (readl(&usbss->sysconfig) & 1)
212 val = readl(&usbss->revision);
213 debug("usbss revision %x\n", val);
215 /* Initialize usb core */
216 hcd = (struct xhci_hccr *)CONFIG_USB_HOST_XHCI_BASE;
217 keystone.dwc3_reg = (struct dwc3 *)(CONFIG_USB_HOST_XHCI_BASE +
220 keystone_xhci_core_init(keystone.dwc3_reg);
222 /* set register addresses */
223 hcor = (struct xhci_hcor *)((uint32_t)hcd +
224 HC_LENGTH(readl(&hcd->cr_capbase)));
226 debug("Keystone2-xhci: init hccr %08x and hcor %08x hc_length %d\n",
228 (u32)HC_LENGTH(xhci_readl(&hcd->cr_capbase)));
230 keystone.usbss = usbss;
233 keystone.hcor = hcor;
241 static int keystone_xhci_phy_suspend(void)
244 struct xhci_hcor *hcor;
245 uint32_t *portsc_1 = NULL;
246 uint32_t *portsc_2 = NULL;
247 u32 val, usb2_pls, usb3_pls, event_q;
248 struct dwc3 *dwc3_reg = keystone.dwc3_reg;
250 /* set register addresses */
251 hcor = keystone.hcor;
253 /* Bypass Scrambling and Set Shorter Training sequence for simulation */
254 val = DWC3_GCTL_PWRDNSCALE(0x4b0) | DWC3_GCTL_PRTCAPDIR(0x2);
255 writel(val, &dwc3_reg->g_ctl);
258 val = readl(&dwc3_reg->g_usb2phycfg[0]);
260 /* assert bit 6 (SusPhy) */
261 val |= DWC3_GUSB2PHYCFG_SUSPHY;
262 writel(val, &dwc3_reg->g_usb2phycfg[0]);
265 val = readl(&dwc3_reg->g_usb3pipectl[0]);
268 * assert bit 29 to allow PHY to go to suspend when idle
269 * and cause the USB3 SS PHY to enter suspend mode
271 val |= (BIT(29) | DWC3_GUSB3PIPECTL_SUSPHY);
272 writel(val, &dwc3_reg->g_usb3pipectl[0]);
275 * Steps necessary to allow controller to suspend even when
277 * - Init DCFG[2:0] (DevSpd) to: 1=FS
278 * - Init GEVNTADR0 to point to an eventQ
279 * - Init GEVNTSIZ0 to 0x0100 to specify the size of the eventQ
280 * - Init DCTL::Run_nStop = 1
282 writel(0x00020001, &dwc3_reg->d_cfg);
283 /* TODO: local2global( (Uint32) eventQ )? */
284 writel((u32)&event_q, &dwc3_reg->g_evnt_buf[0].g_evntadrlo);
285 writel(0, &dwc3_reg->g_evnt_buf[0].g_evntadrhi);
286 writel(0x4, &dwc3_reg->g_evnt_buf[0].g_evntsiz);
288 writel(DWC3_DCTL_RUN_STOP, &dwc3_reg->d_ctl);
292 /* Wait for USB2 & USB3 PORTSC::PortLinkState to indicate suspend */
293 portsc_1 = (uint32_t *)(&hcor->portregs[0].or_portsc);
294 portsc_2 = (uint32_t *)(&hcor->portregs[1].or_portsc);
299 usb2_pls = (readl(portsc_1) & PORT_PLS_MASK) >> 5;
300 usb3_pls = (readl(portsc_2) & PORT_PLS_MASK) >> 5;
301 } while (((usb2_pls != 0x4) || (usb3_pls != 0x4)) && loop_cnt < 1000);
303 if (usb2_pls != 0x4 || usb3_pls != 0x4) {
304 debug("USB suspend failed - PLS USB2=%02x, USB3=%02x\n",
309 debug("USB2 and USB3 PLS - Disabled, loop_cnt=%d\n", loop_cnt);
313 void xhci_hcd_stop(int index)
316 if (keystone_xhci_phy_suspend())
319 if (psc_disable_module(KS2_LPSC_USB)) {
320 debug("PSC disable module USB failed!\n");
325 keystone_xhci_phy_unset(keystone.phy);
327 /* memset(&keystone, 0, sizeof(struct keystone_xhci)); */
328 debug("xhci_hcd_stop OK.\n");