blob: 5b12d1358e47ec9a90738389b72ed1ae8505cfec [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
Samuel Holland91ce84a2021-07-05 13:29:03 +010010#include <clk.h>
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053011#include <common.h>
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +020012#include <dm.h>
Patrice Chotardf56db162017-07-18 11:38:44 +020013#include <generic-phy.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Samuel Holland91ce84a2021-07-05 13:29:03 +010015#include <reset.h>
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +020016#include <usb.h>
Jean-Jacques Hiblotd648a502018-11-29 10:52:45 +010017#include <dwc3-uboot.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +020019
Jean-Jacques Hiblot1708a122019-09-11 11:33:46 +020020#include <usb/xhci.h>
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053021#include <asm/io.h>
22#include <linux/usb/dwc3.h>
Patrice Chotard576e3cc2017-07-18 11:38:41 +020023#include <linux/usb/otg.h>
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053024
Simon Glass8a8d24b2020-12-03 16:55:23 -070025struct xhci_dwc3_plat {
Samuel Holland91ce84a2021-07-05 13:29:03 +010026 struct clk_bulk clks;
Chunfeng Yun58221d72020-05-14 13:55:11 +080027 struct phy_bulk phys;
Samuel Holland91ce84a2021-07-05 13:29:03 +010028 struct reset_ctl_bulk resets;
Patrice Chotardf56db162017-07-18 11:38:44 +020029};
30
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053031void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
32{
33 clrsetbits_le32(&dwc3_reg->g_ctl,
34 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
35 DWC3_GCTL_PRTCAPDIR(mode));
36}
37
Masahiro Yamada121a4d12017-06-22 16:35:14 +090038static void dwc3_phy_reset(struct dwc3 *dwc3_reg)
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053039{
40 /* Assert USB3 PHY reset */
41 setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
42
43 /* Assert USB2 PHY reset */
44 setbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
45
46 mdelay(100);
47
48 /* Clear USB3 PHY reset */
49 clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
50
51 /* Clear USB2 PHY reset */
52 clrbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
53}
54
55void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
56{
57 /* Before Resetting PHY, put Core in Reset */
58 setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
59
60 /* reset USB3 phy - if required */
61 dwc3_phy_reset(dwc3_reg);
62
Rajesh Bhagat5955bb92015-12-02 11:44:27 +053063 mdelay(100);
64
Ramneek Mehreshdc9cdf82015-05-29 14:47:15 +053065 /* After PHYs are stable we can take Core out of reset state */
66 clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
67}
68
69int dwc3_core_init(struct dwc3 *dwc3_reg)
70{
71 u32 reg;
72 u32 revision;
73 unsigned int dwc3_hwparams1;
74
75 revision = readl(&dwc3_reg->g_snpsid);
76 /* This should read as U3 followed by revision number */
77 if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
78 puts("this is not a DesignWare USB3 DRD Core\n");
79 return -1;
80 }
81
82 dwc3_core_soft_reset(dwc3_reg);
83
84 dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
85
86 reg = readl(&dwc3_reg->g_ctl);
87 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
88 reg &= ~DWC3_GCTL_DISSCRAMBLE;
89 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
90 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
91 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
92 break;
93 default:
94 debug("No power optimization available\n");
95 }
96
97 /*
98 * WORKAROUND: DWC3 revisions <1.90a have a bug
99 * where the device can fail to connect at SuperSpeed
100 * and falls back to high-speed mode which causes
101 * the device to enter a Connect/Disconnect loop
102 */
103 if ((revision & DWC3_REVISION_MASK) < 0x190a)
104 reg |= DWC3_GCTL_U2RSTECN;
105
106 writel(reg, &dwc3_reg->g_ctl);
107
108 return 0;
109}
Nikhil Badola667f4dd2015-06-23 09:17:49 +0530110
111void dwc3_set_fladj(struct dwc3 *dwc3_reg, u32 val)
112{
113 setbits_le32(&dwc3_reg->g_fladj, GFLADJ_30MHZ_REG_SEL |
114 GFLADJ_30MHZ(val));
115}
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200116
Sven Schwermerfd09c202018-11-21 08:43:56 +0100117#if CONFIG_IS_ENABLED(DM_USB)
Samuel Holland91ce84a2021-07-05 13:29:03 +0100118static int xhci_dwc3_reset_init(struct udevice *dev,
119 struct xhci_dwc3_plat *plat)
120{
121 int ret;
122
123 ret = reset_get_bulk(dev, &plat->resets);
124 if (ret == -ENOTSUPP || ret == -ENOENT)
125 return 0;
126 else if (ret)
127 return ret;
128
129 ret = reset_deassert_bulk(&plat->resets);
130 if (ret) {
131 reset_release_bulk(&plat->resets);
132 return ret;
133 }
134
135 return 0;
136}
137
138static int xhci_dwc3_clk_init(struct udevice *dev,
139 struct xhci_dwc3_plat *plat)
140{
141 int ret;
142
143 ret = clk_get_bulk(dev, &plat->clks);
144 if (ret == -ENOSYS || ret == -ENOENT)
145 return 0;
146 if (ret)
147 return ret;
148
149 ret = clk_enable_bulk(&plat->clks);
150 if (ret) {
151 clk_release_bulk(&plat->clks);
152 return ret;
153 }
154
155 return 0;
156}
157
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200158static int xhci_dwc3_probe(struct udevice *dev)
159{
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200160 struct xhci_hcor *hcor;
161 struct xhci_hccr *hccr;
162 struct dwc3 *dwc3_reg;
Patrice Chotard576e3cc2017-07-18 11:38:41 +0200163 enum usb_dr_mode dr_mode;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700164 struct xhci_dwc3_plat *plat = dev_get_plat(dev);
Mark Kettenis062790f2019-06-30 18:01:55 +0200165 const char *phy;
166 u32 reg;
Patrice Chotardf56db162017-07-18 11:38:44 +0200167 int ret;
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200168
Samuel Holland91ce84a2021-07-05 13:29:03 +0100169 ret = xhci_dwc3_reset_init(dev, plat);
170 if (ret)
171 return ret;
172
173 ret = xhci_dwc3_clk_init(dev, plat);
174 if (ret)
175 return ret;
176
Stefan Roese72a53ac2020-08-24 13:04:36 +0200177 hccr = (struct xhci_hccr *)((uintptr_t)dev_remap_addr(dev));
Patrice Chotardd38a8ea2017-07-25 13:24:44 +0200178 hcor = (struct xhci_hcor *)((uintptr_t)hccr +
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200179 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
180
Chunfeng Yun58221d72020-05-14 13:55:11 +0800181 ret = dwc3_setup_phy(dev, &plat->phys);
Jean-Jacques Hiblotd648a502018-11-29 10:52:45 +0100182 if (ret && (ret != -ENOTSUPP))
Vignesh R3fc26352018-03-07 14:50:09 +0530183 return ret;
Vignesh R2fd42422018-03-07 14:50:10 +0530184
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200185 dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET);
186
187 dwc3_core_init(dwc3_reg);
188
Mark Kettenis062790f2019-06-30 18:01:55 +0200189 /* Set dwc3 usb2 phy config */
190 reg = readl(&dwc3_reg->g_usb2phycfg[0]);
191
192 phy = dev_read_string(dev, "phy_type");
193 if (phy && strcmp(phy, "utmi_wide") == 0) {
194 reg |= DWC3_GUSB2PHYCFG_PHYIF;
195 reg &= ~DWC3_GUSB2PHYCFG_USBTRDTIM_MASK;
196 reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_16BIT;
197 }
198
199 if (dev_read_bool(dev, "snps,dis_enblslpm-quirk"))
200 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
201
202 if (dev_read_bool(dev, "snps,dis-u2-freeclk-exists-quirk"))
203 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
204
Neil Armstrongb35b8072019-09-09 18:52:39 +0000205 if (dev_read_bool(dev, "snps,dis_u2_susphy_quirk"))
206 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
207
Mark Kettenis062790f2019-06-30 18:01:55 +0200208 writel(reg, &dwc3_reg->g_usb2phycfg[0]);
209
Simon Glassf10643c2020-12-19 10:40:14 -0700210 dr_mode = usb_get_dr_mode(dev_ofnode(dev));
Patrice Chotard576e3cc2017-07-18 11:38:41 +0200211 if (dr_mode == USB_DR_MODE_UNKNOWN)
212 /* by default set dual role mode to HOST */
213 dr_mode = USB_DR_MODE_HOST;
214
215 dwc3_set_mode(dwc3_reg, dr_mode);
216
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200217 return xhci_register(dev, hccr, hcor);
218}
219
220static int xhci_dwc3_remove(struct udevice *dev)
221{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700222 struct xhci_dwc3_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblotd648a502018-11-29 10:52:45 +0100223
Chunfeng Yun58221d72020-05-14 13:55:11 +0800224 dwc3_shutdown_phy(dev, &plat->phys);
Patrice Chotardf56db162017-07-18 11:38:44 +0200225
Samuel Holland91ce84a2021-07-05 13:29:03 +0100226 clk_release_bulk(&plat->clks);
227
228 reset_release_bulk(&plat->resets);
229
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200230 return xhci_deregister(dev);
231}
232
233static const struct udevice_id xhci_dwc3_ids[] = {
234 { .compatible = "snps,dwc3" },
235 { }
236};
237
238U_BOOT_DRIVER(xhci_dwc3) = {
239 .name = "xhci-dwc3",
240 .id = UCLASS_USB,
241 .of_match = xhci_dwc3_ids,
242 .probe = xhci_dwc3_probe,
243 .remove = xhci_dwc3_remove,
244 .ops = &xhci_usb_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700245 .priv_auto = sizeof(struct xhci_ctrl),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700246 .plat_auto = sizeof(struct xhci_dwc3_plat),
Patrice Chotardb7c1c7d2017-07-18 11:38:40 +0200247 .flags = DM_FLAG_ALLOC_PRIV_DMA,
248};
Patrice Chotard623b7ac2017-07-24 17:07:03 +0200249#endif