blob: cf1986bebd07cc8cb047d8c1ff48024f3934f6f0 [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;
26};
27
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +053028void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
29{
30 clrsetbits_le32(&dwc3_reg->g_ctl,
31 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
32 DWC3_GCTL_PRTCAPDIR(mode));
33}
34
Masahiro Yamada6d8e4332017-06-22 16:35:14 +090035static void dwc3_phy_reset(struct dwc3 *dwc3_reg)
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +053036{
37 /* Assert USB3 PHY reset */
38 setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
39
40 /* Assert USB2 PHY reset */
41 setbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
42
43 mdelay(100);
44
45 /* Clear USB3 PHY reset */
46 clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
47
48 /* Clear USB2 PHY reset */
49 clrbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
50}
51
52void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
53{
54 /* Before Resetting PHY, put Core in Reset */
55 setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
56
57 /* reset USB3 phy - if required */
58 dwc3_phy_reset(dwc3_reg);
59
Rajesh Bhagat295d0272015-12-02 11:44:27 +053060 mdelay(100);
61
Ramneek Mehresh3dad08d2015-05-29 14:47:15 +053062 /* After PHYs are stable we can take Core out of reset state */
63 clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
64}
65
66int dwc3_core_init(struct dwc3 *dwc3_reg)
67{
68 u32 reg;
69 u32 revision;
70 unsigned int dwc3_hwparams1;
71
72 revision = readl(&dwc3_reg->g_snpsid);
73 /* This should read as U3 followed by revision number */
74 if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
75 puts("this is not a DesignWare USB3 DRD Core\n");
76 return -1;
77 }
78
79 dwc3_core_soft_reset(dwc3_reg);
80
81 dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
82
83 reg = readl(&dwc3_reg->g_ctl);
84 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
85 reg &= ~DWC3_GCTL_DISSCRAMBLE;
86 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
87 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
88 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
89 break;
90 default:
91 debug("No power optimization available\n");
92 }
93
94 /*
95 * WORKAROUND: DWC3 revisions <1.90a have a bug
96 * where the device can fail to connect at SuperSpeed
97 * and falls back to high-speed mode which causes
98 * the device to enter a Connect/Disconnect loop
99 */
100 if ((revision & DWC3_REVISION_MASK) < 0x190a)
101 reg |= DWC3_GCTL_U2RSTECN;
102
103 writel(reg, &dwc3_reg->g_ctl);
104
105 return 0;
106}
Nikhil Badola807babb2015-06-23 09:17:49 +0530107
108void dwc3_set_fladj(struct dwc3 *dwc3_reg, u32 val)
109{
110 setbits_le32(&dwc3_reg->g_fladj, GFLADJ_30MHZ_REG_SEL |
111 GFLADJ_30MHZ(val));
112}
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200113
Patrice Chotarda3d03ea2017-07-24 17:07:03 +0200114#ifdef CONFIG_DM_USB
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200115static int xhci_dwc3_probe(struct udevice *dev)
116{
117 struct xhci_dwc3_platdata *plat = dev_get_platdata(dev);
118 struct xhci_hcor *hcor;
119 struct xhci_hccr *hccr;
120 struct dwc3 *dwc3_reg;
Patrice Chotard17b08872017-07-18 11:38:41 +0200121 enum usb_dr_mode dr_mode;
Patrice Chotardecfafba2017-07-18 11:38:44 +0200122 int ret;
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200123
Patrice Chotard15b8d632017-07-25 13:24:44 +0200124 hccr = (struct xhci_hccr *)((uintptr_t)dev_read_addr(dev));
125 hcor = (struct xhci_hcor *)((uintptr_t)hccr +
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200126 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
127
Patrice Chotardecfafba2017-07-18 11:38:44 +0200128 ret = generic_phy_get_by_index(dev, 0, &plat->usb_phy);
129 if (ret) {
130 if (ret != -ENOENT) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900131 pr_err("Failed to get USB PHY for %s\n", dev->name);
Patrice Chotardecfafba2017-07-18 11:38:44 +0200132 return ret;
133 }
134 } else {
135 ret = generic_phy_init(&plat->usb_phy);
136 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900137 pr_err("Can't init USB PHY for %s\n", dev->name);
Patrice Chotardecfafba2017-07-18 11:38:44 +0200138 return ret;
139 }
Vignesh R7ec414e2018-03-07 14:50:08 +0530140
141 ret = generic_phy_power_on(&plat->usb_phy);
142 if (ret) {
143 pr_err("Can't power on USB PHY for %s\n", dev->name);
144 return ret;
145 }
Patrice Chotardecfafba2017-07-18 11:38:44 +0200146 }
147
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200148 dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET);
149
150 dwc3_core_init(dwc3_reg);
151
Patrice Chotard17b08872017-07-18 11:38:41 +0200152 dr_mode = usb_get_dr_mode(dev_of_offset(dev));
153 if (dr_mode == USB_DR_MODE_UNKNOWN)
154 /* by default set dual role mode to HOST */
155 dr_mode = USB_DR_MODE_HOST;
156
157 dwc3_set_mode(dwc3_reg, dr_mode);
158
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200159 return xhci_register(dev, hccr, hcor);
160}
161
162static int xhci_dwc3_remove(struct udevice *dev)
163{
Patrice Chotardecfafba2017-07-18 11:38:44 +0200164 struct xhci_dwc3_platdata *plat = dev_get_platdata(dev);
165 int ret;
166
167 if (generic_phy_valid(&plat->usb_phy)) {
Vignesh R7ec414e2018-03-07 14:50:08 +0530168 ret = generic_phy_power_off(&plat->usb_phy);
169 if (ret) {
170 pr_err("Can't poweroff USB PHY for %s\n", dev->name);
171 return ret;
172 }
173
Patrice Chotardecfafba2017-07-18 11:38:44 +0200174 ret = generic_phy_exit(&plat->usb_phy);
175 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900176 pr_err("Can't deinit USB PHY for %s\n", dev->name);
Patrice Chotardecfafba2017-07-18 11:38:44 +0200177 return ret;
178 }
179 }
180
Patrice Chotardc7eadfc2017-07-18 11:38:40 +0200181 return xhci_deregister(dev);
182}
183
184static const struct udevice_id xhci_dwc3_ids[] = {
185 { .compatible = "snps,dwc3" },
186 { }
187};
188
189U_BOOT_DRIVER(xhci_dwc3) = {
190 .name = "xhci-dwc3",
191 .id = UCLASS_USB,
192 .of_match = xhci_dwc3_ids,
193 .probe = xhci_dwc3_probe,
194 .remove = xhci_dwc3_remove,
195 .ops = &xhci_usb_ops,
196 .priv_auto_alloc_size = sizeof(struct xhci_ctrl),
197 .platdata_auto_alloc_size = sizeof(struct xhci_dwc3_platdata),
198 .flags = DM_FLAG_ALLOC_PRIV_DMA,
199};
Patrice Chotarda3d03ea2017-07-24 17:07:03 +0200200#endif