blob: 1022dd5512418fe90b5a399ad6ea5735202789cb [file] [log] [blame]
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +05301/*
2 * Copyright 2015 Freescale Semiconductor, Inc.
3 *
4 * DWC3 controller driver
5 *
6 * Author: Ramneek Mehresh<ramneek.mehresh@freescale.com>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <common.h>
Patrice Chotardc7eadfc2017-07-18 11:38:40 +020012#include <dm.h>
Patrice Chotardecfafba2017-07-18 11:38:44 +020013#include <fdtdec.h>
14#include <generic-phy.h>
Patrice Chotardc7eadfc2017-07-18 11:38:40 +020015#include <usb.h>
16
17#include "xhci.h"
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +053018#include <asm/io.h>
19#include <linux/usb/dwc3.h>
Patrice Chotard17b08872017-07-18 11:38:41 +020020#include <linux/usb/otg.h>
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +053021
Patrice Chotardc7eadfc2017-07-18 11:38:40 +020022DECLARE_GLOBAL_DATA_PTR;
23
Patrice Chotardecfafba2017-07-18 11:38:44 +020024struct xhci_dwc3_platdata {
25 struct phy usb_phy;
Vignesh Rc85d7a92018-03-07 14:50:10 +053026 struct phy usb3_phy;
Patrice Chotardecfafba2017-07-18 11:38:44 +020027};
28
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +053029void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
30{
31 clrsetbits_le32(&dwc3_reg->g_ctl,
32 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
33 DWC3_GCTL_PRTCAPDIR(mode));
34}
35
Masahiro Yamada6d8e4332017-06-22 16:35:14 +090036static void dwc3_phy_reset(struct dwc3 *dwc3_reg)
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +053037{
38 /* Assert USB3 PHY reset */
39 setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
40
41 /* Assert USB2 PHY reset */
42 setbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
43
44 mdelay(100);
45
46 /* Clear USB3 PHY reset */
47 clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
48
49 /* Clear USB2 PHY reset */
50 clrbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
51}
52
53void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
54{
55 /* Before Resetting PHY, put Core in Reset */
56 setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
57
58 /* reset USB3 phy - if required */
59 dwc3_phy_reset(dwc3_reg);
60
Rajesh Bhagat295d0272015-12-02 11:44:27 +053061 mdelay(100);
62
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +053063 /* After PHYs are stable we can take Core out of reset state */
64 clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
65}
66
67int dwc3_core_init(struct dwc3 *dwc3_reg)
68{
69 u32 reg;
70 u32 revision;
71 unsigned int dwc3_hwparams1;
72
73 revision = readl(&dwc3_reg->g_snpsid);
74 /* This should read as U3 followed by revision number */
75 if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
76 puts("this is not a DesignWare USB3 DRD Core\n");
77 return -1;
78 }
79
80 dwc3_core_soft_reset(dwc3_reg);
81
82 dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
83
84 reg = readl(&dwc3_reg->g_ctl);
85 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
86 reg &= ~DWC3_GCTL_DISSCRAMBLE;
87 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
88 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
89 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
90 break;
91 default:
92 debug("No power optimization available\n");
93 }
94
95 /*
96 * WORKAROUND: DWC3 revisions <1.90a have a bug
97 * where the device can fail to connect at SuperSpeed
98 * and falls back to high-speed mode which causes
99 * the device to enter a Connect/Disconnect loop
100 */
101 if ((revision & DWC3_REVISION_MASK) < 0x190a)
102 reg |= DWC3_GCTL_U2RSTECN;
103
104 writel(reg, &dwc3_reg->g_ctl);
105
106 return 0;
107}
Nikhil Badola807babb2015-06-23 09:17:49 +0530108
109void dwc3_set_fladj(struct dwc3 *dwc3_reg, u32 val)
110{
111 setbits_le32(&dwc3_reg->g_fladj, GFLADJ_30MHZ_REG_SEL |
112 GFLADJ_30MHZ(val));
113}
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200114
Patrice Chotarda3d03ea2017-07-24 17:07:03 +0200115#ifdef CONFIG_DM_USB
Vignesh Rd52f8a3e92018-03-07 14:50:09 +0530116static int xhci_dwc3_setup_phy(struct udevice *dev, int index, struct phy *phy)
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200117{
Vignesh Rd52f8a3e92018-03-07 14:50:09 +0530118 int ret = 0;
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200119
Vignesh Rd52f8a3e92018-03-07 14:50:09 +0530120 ret = generic_phy_get_by_index(dev, index, phy);
Patrice Chotardecfafba2017-07-18 11:38:44 +0200121 if (ret) {
122 if (ret != -ENOENT) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900123 pr_err("Failed to get USB PHY for %s\n", dev->name);
Patrice Chotardecfafba2017-07-18 11:38:44 +0200124 return ret;
125 }
126 } else {
Vignesh Rd52f8a3e92018-03-07 14:50:09 +0530127 ret = generic_phy_init(phy);
Patrice Chotardecfafba2017-07-18 11:38:44 +0200128 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900129 pr_err("Can't init USB PHY for %s\n", dev->name);
Patrice Chotardecfafba2017-07-18 11:38:44 +0200130 return ret;
131 }
Vignesh Rd52f8a3e92018-03-07 14:50:09 +0530132 ret = generic_phy_power_on(phy);
Vignesh R7ec414e2018-03-07 14:50:08 +0530133 if (ret) {
134 pr_err("Can't power on USB PHY for %s\n", dev->name);
Vignesh Rd52f8a3e92018-03-07 14:50:09 +0530135 generic_phy_exit(phy);
Vignesh R7ec414e2018-03-07 14:50:08 +0530136 return ret;
137 }
Patrice Chotardecfafba2017-07-18 11:38:44 +0200138 }
139
Vignesh Rd52f8a3e92018-03-07 14:50:09 +0530140 return 0;
141}
142
143static int xhci_dwc3_shutdown_phy(struct phy *phy)
144{
145 int ret = 0;
146
147 if (generic_phy_valid(phy)) {
148 ret = generic_phy_power_off(phy);
149 if (ret)
150 return ret;
151
152 ret = generic_phy_exit(phy);
153 if (ret)
154 return ret;
155 }
156
157 return 0;
158}
159
160static int xhci_dwc3_probe(struct udevice *dev)
161{
162 struct xhci_dwc3_platdata *plat = dev_get_platdata(dev);
163 struct xhci_hcor *hcor;
164 struct xhci_hccr *hccr;
165 struct dwc3 *dwc3_reg;
166 enum usb_dr_mode dr_mode;
167 int ret;
168
169 hccr = (struct xhci_hccr *)((uintptr_t)dev_read_addr(dev));
170 hcor = (struct xhci_hcor *)((uintptr_t)hccr +
171 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
172
173 ret = xhci_dwc3_setup_phy(dev, 0, &plat->usb_phy);
174 if (ret) {
175 pr_err("Failed to setup USB PHY for %s\n", dev->name);
176 return ret;
177 }
178
Vignesh Rc85d7a92018-03-07 14:50:10 +0530179 ret = xhci_dwc3_setup_phy(dev, 1, &plat->usb3_phy);
180 if (ret) {
181 pr_err("Failed to setup USB3 PHY for %s\n", dev->name);
182 xhci_dwc3_shutdown_phy(&plat->usb_phy);
183 return ret;
184 }
185
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200186 dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET);
187
188 dwc3_core_init(dwc3_reg);
189
Patrice Chotard17b08872017-07-18 11:38:41 +0200190 dr_mode = usb_get_dr_mode(dev_of_offset(dev));
191 if (dr_mode == USB_DR_MODE_UNKNOWN)
192 /* by default set dual role mode to HOST */
193 dr_mode = USB_DR_MODE_HOST;
194
195 dwc3_set_mode(dwc3_reg, dr_mode);
196
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200197 return xhci_register(dev, hccr, hcor);
198}
199
200static int xhci_dwc3_remove(struct udevice *dev)
201{
Patrice Chotardecfafba2017-07-18 11:38:44 +0200202 struct xhci_dwc3_platdata *plat = dev_get_platdata(dev);
203 int ret;
204
Vignesh Rd52f8a3e92018-03-07 14:50:09 +0530205 ret = xhci_dwc3_shutdown_phy(&plat->usb_phy);
206 if (ret)
207 pr_err("Can't shutdown USB PHY for %s\n", dev->name);
Vignesh R7ec414e2018-03-07 14:50:08 +0530208
Vignesh Rc85d7a92018-03-07 14:50:10 +0530209 ret = xhci_dwc3_shutdown_phy(&plat->usb3_phy);
210 if (ret)
211 pr_err("Can't shutdown USB3 PHY for %s\n", dev->name);
Patrice Chotardecfafba2017-07-18 11:38:44 +0200212
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200213 return xhci_deregister(dev);
214}
215
216static const struct udevice_id xhci_dwc3_ids[] = {
217 { .compatible = "snps,dwc3" },
218 { }
219};
220
221U_BOOT_DRIVER(xhci_dwc3) = {
222 .name = "xhci-dwc3",
223 .id = UCLASS_USB,
224 .of_match = xhci_dwc3_ids,
225 .probe = xhci_dwc3_probe,
226 .remove = xhci_dwc3_remove,
227 .ops = &xhci_usb_ops,
228 .priv_auto_alloc_size = sizeof(struct xhci_ctrl),
229 .platdata_auto_alloc_size = sizeof(struct xhci_dwc3_platdata),
230 .flags = DM_FLAG_ALLOC_PRIV_DMA,
231};
Patrice Chotarda3d03ea2017-07-24 17:07:03 +0200232#endif