blob: 4dba56993776541b80c9a22cf5375f9f3d44df64 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +05302/*
3 * Copyright 2015 Freescale Semiconductor, Inc.
4 *
5 * DWC3 controller driver
6 *
7 * Author: Ramneek Mehresh<ramneek.mehresh@freescale.com>
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +05308 */
9
10#include <common.h>
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +020011#include <dm.h>
Patrice Chotardf56db162017-07-18 11:38:44 +020012#include <generic-phy.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +020014#include <usb.h>
Jean-Jacques Hiblotd648a502018-11-29 10:52:45 +010015#include <dwc3-uboot.h>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +020017
Jean-Jacques Hiblot1708a122019-09-11 11:33:46 +020018#include <usb/xhci.h>
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053019#include <asm/io.h>
20#include <linux/usb/dwc3.h>
Patrice Chotard576e3cc2017-07-18 11:38:41 +020021#include <linux/usb/otg.h>
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053022
Patrice Chotardf56db162017-07-18 11:38:44 +020023struct xhci_dwc3_platdata {
Chunfeng Yun58221d72020-05-14 13:55:11 +080024 struct phy_bulk phys;
Patrice Chotardf56db162017-07-18 11:38:44 +020025};
26
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053027void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
28{
29 clrsetbits_le32(&dwc3_reg->g_ctl,
30 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
31 DWC3_GCTL_PRTCAPDIR(mode));
32}
33
Masahiro Yamada121a4d12017-06-22 16:35:14 +090034static void dwc3_phy_reset(struct dwc3 *dwc3_reg)
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053035{
36 /* Assert USB3 PHY reset */
37 setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
38
39 /* Assert USB2 PHY reset */
40 setbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
41
42 mdelay(100);
43
44 /* Clear USB3 PHY reset */
45 clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
46
47 /* Clear USB2 PHY reset */
48 clrbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
49}
50
51void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
52{
53 /* Before Resetting PHY, put Core in Reset */
54 setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
55
56 /* reset USB3 phy - if required */
57 dwc3_phy_reset(dwc3_reg);
58
Rajesh Bhagat5955bb92015-12-02 11:44:27 +053059 mdelay(100);
60
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053061 /* After PHYs are stable we can take Core out of reset state */
62 clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
63}
64
65int dwc3_core_init(struct dwc3 *dwc3_reg)
66{
67 u32 reg;
68 u32 revision;
69 unsigned int dwc3_hwparams1;
70
71 revision = readl(&dwc3_reg->g_snpsid);
72 /* This should read as U3 followed by revision number */
73 if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
74 puts("this is not a DesignWare USB3 DRD Core\n");
75 return -1;
76 }
77
78 dwc3_core_soft_reset(dwc3_reg);
79
80 dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
81
82 reg = readl(&dwc3_reg->g_ctl);
83 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
84 reg &= ~DWC3_GCTL_DISSCRAMBLE;
85 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
86 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
87 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
88 break;
89 default:
90 debug("No power optimization available\n");
91 }
92
93 /*
94 * WORKAROUND: DWC3 revisions <1.90a have a bug
95 * where the device can fail to connect at SuperSpeed
96 * and falls back to high-speed mode which causes
97 * the device to enter a Connect/Disconnect loop
98 */
99 if ((revision & DWC3_REVISION_MASK) < 0x190a)
100 reg |= DWC3_GCTL_U2RSTECN;
101
102 writel(reg, &dwc3_reg->g_ctl);
103
104 return 0;
105}
Nikhil Badola667f4dd2015-06-23 09:17:49 +0530106
107void dwc3_set_fladj(struct dwc3 *dwc3_reg, u32 val)
108{
109 setbits_le32(&dwc3_reg->g_fladj, GFLADJ_30MHZ_REG_SEL |
110 GFLADJ_30MHZ(val));
111}
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200112
Sven Schwermerfd09c202018-11-21 08:43:56 +0100113#if CONFIG_IS_ENABLED(DM_USB)
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200114static int xhci_dwc3_probe(struct udevice *dev)
115{
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200116 struct xhci_hcor *hcor;
117 struct xhci_hccr *hccr;
118 struct dwc3 *dwc3_reg;
Patrice Chotard576e3cc2017-07-18 11:38:41 +0200119 enum usb_dr_mode dr_mode;
Jean-Jacques Hiblotd648a502018-11-29 10:52:45 +0100120 struct xhci_dwc3_platdata *plat = dev_get_platdata(dev);
Mark Kettenis062790f2019-06-30 18:01:55 +0200121 const char *phy;
122 u32 reg;
Patrice Chotardf56db162017-07-18 11:38:44 +0200123 int ret;
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200124
Stefan Roese72a53ac2020-08-24 13:04:36 +0200125 hccr = (struct xhci_hccr *)((uintptr_t)dev_remap_addr(dev));
Patrice Chotardd38a8ea2017-07-25 13:24:44 +0200126 hcor = (struct xhci_hcor *)((uintptr_t)hccr +
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200127 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
128
Chunfeng Yun58221d72020-05-14 13:55:11 +0800129 ret = dwc3_setup_phy(dev, &plat->phys);
Jean-Jacques Hiblotd648a502018-11-29 10:52:45 +0100130 if (ret && (ret != -ENOTSUPP))
Vignesh R3fc26352018-03-07 14:50:09 +0530131 return ret;
Vignesh R2fd42422018-03-07 14:50:10 +0530132
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200133 dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET);
134
135 dwc3_core_init(dwc3_reg);
136
Mark Kettenis062790f2019-06-30 18:01:55 +0200137 /* Set dwc3 usb2 phy config */
138 reg = readl(&dwc3_reg->g_usb2phycfg[0]);
139
140 phy = dev_read_string(dev, "phy_type");
141 if (phy && strcmp(phy, "utmi_wide") == 0) {
142 reg |= DWC3_GUSB2PHYCFG_PHYIF;
143 reg &= ~DWC3_GUSB2PHYCFG_USBTRDTIM_MASK;
144 reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_16BIT;
145 }
146
147 if (dev_read_bool(dev, "snps,dis_enblslpm-quirk"))
148 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
149
150 if (dev_read_bool(dev, "snps,dis-u2-freeclk-exists-quirk"))
151 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
152
Neil Armstrongb35b8072019-09-09 18:52:39 +0000153 if (dev_read_bool(dev, "snps,dis_u2_susphy_quirk"))
154 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
155
Mark Kettenis062790f2019-06-30 18:01:55 +0200156 writel(reg, &dwc3_reg->g_usb2phycfg[0]);
157
Kever Yangac28e592020-03-04 08:59:50 +0800158 dr_mode = usb_get_dr_mode(dev->node);
Patrice Chotard576e3cc2017-07-18 11:38:41 +0200159 if (dr_mode == USB_DR_MODE_UNKNOWN)
160 /* by default set dual role mode to HOST */
161 dr_mode = USB_DR_MODE_HOST;
162
163 dwc3_set_mode(dwc3_reg, dr_mode);
164
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200165 return xhci_register(dev, hccr, hcor);
166}
167
168static int xhci_dwc3_remove(struct udevice *dev)
169{
Jean-Jacques Hiblotd648a502018-11-29 10:52:45 +0100170 struct xhci_dwc3_platdata *plat = dev_get_platdata(dev);
171
Chunfeng Yun58221d72020-05-14 13:55:11 +0800172 dwc3_shutdown_phy(dev, &plat->phys);
Patrice Chotardf56db162017-07-18 11:38:44 +0200173
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200174 return xhci_deregister(dev);
175}
176
177static const struct udevice_id xhci_dwc3_ids[] = {
178 { .compatible = "snps,dwc3" },
179 { }
180};
181
182U_BOOT_DRIVER(xhci_dwc3) = {
183 .name = "xhci-dwc3",
184 .id = UCLASS_USB,
185 .of_match = xhci_dwc3_ids,
186 .probe = xhci_dwc3_probe,
187 .remove = xhci_dwc3_remove,
188 .ops = &xhci_usb_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700189 .priv_auto = sizeof(struct xhci_ctrl),
190 .platdata_auto = sizeof(struct xhci_dwc3_platdata),
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200191 .flags = DM_FLAG_ALLOC_PRIV_DMA,
192};
Patrice Chotard623b7ac2017-07-24 17:07:03 +0200193#endif