Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2015 Freescale Semiconductor, Inc. |
| 4 | * |
| 5 | * DWC3 controller driver |
| 6 | * |
| 7 | * Author: Ramneek Mehresh<ramneek.mehresh@freescale.com> |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 11 | #include <dm.h> |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 12 | #include <fdtdec.h> |
| 13 | #include <generic-phy.h> |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 14 | #include <usb.h> |
| 15 | |
| 16 | #include "xhci.h" |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <linux/usb/dwc3.h> |
Patrice Chotard | 17b0887 | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 19 | #include <linux/usb/otg.h> |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 20 | |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 21 | struct xhci_dwc3_platdata { |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 22 | struct phy *usb_phys; |
| 23 | int num_phys; |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 26 | void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode) |
| 27 | { |
| 28 | clrsetbits_le32(&dwc3_reg->g_ctl, |
| 29 | DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG), |
| 30 | DWC3_GCTL_PRTCAPDIR(mode)); |
| 31 | } |
| 32 | |
Masahiro Yamada | 6d8e433 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 33 | static void dwc3_phy_reset(struct dwc3 *dwc3_reg) |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 34 | { |
| 35 | /* Assert USB3 PHY reset */ |
| 36 | setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST); |
| 37 | |
| 38 | /* Assert USB2 PHY reset */ |
| 39 | setbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST); |
| 40 | |
| 41 | mdelay(100); |
| 42 | |
| 43 | /* Clear USB3 PHY reset */ |
| 44 | clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST); |
| 45 | |
| 46 | /* Clear USB2 PHY reset */ |
| 47 | clrbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST); |
| 48 | } |
| 49 | |
| 50 | void dwc3_core_soft_reset(struct dwc3 *dwc3_reg) |
| 51 | { |
| 52 | /* Before Resetting PHY, put Core in Reset */ |
| 53 | setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET); |
| 54 | |
| 55 | /* reset USB3 phy - if required */ |
| 56 | dwc3_phy_reset(dwc3_reg); |
| 57 | |
Rajesh Bhagat | 295d027 | 2015-12-02 11:44:27 +0530 | [diff] [blame] | 58 | mdelay(100); |
| 59 | |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 60 | /* After PHYs are stable we can take Core out of reset state */ |
| 61 | clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET); |
| 62 | } |
| 63 | |
| 64 | int dwc3_core_init(struct dwc3 *dwc3_reg) |
| 65 | { |
| 66 | u32 reg; |
| 67 | u32 revision; |
| 68 | unsigned int dwc3_hwparams1; |
| 69 | |
| 70 | revision = readl(&dwc3_reg->g_snpsid); |
| 71 | /* This should read as U3 followed by revision number */ |
| 72 | if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) { |
| 73 | puts("this is not a DesignWare USB3 DRD Core\n"); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | dwc3_core_soft_reset(dwc3_reg); |
| 78 | |
| 79 | dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1); |
| 80 | |
| 81 | reg = readl(&dwc3_reg->g_ctl); |
| 82 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
| 83 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
| 84 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) { |
| 85 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
| 86 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; |
| 87 | break; |
| 88 | default: |
| 89 | debug("No power optimization available\n"); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * WORKAROUND: DWC3 revisions <1.90a have a bug |
| 94 | * where the device can fail to connect at SuperSpeed |
| 95 | * and falls back to high-speed mode which causes |
| 96 | * the device to enter a Connect/Disconnect loop |
| 97 | */ |
| 98 | if ((revision & DWC3_REVISION_MASK) < 0x190a) |
| 99 | reg |= DWC3_GCTL_U2RSTECN; |
| 100 | |
| 101 | writel(reg, &dwc3_reg->g_ctl); |
| 102 | |
| 103 | return 0; |
| 104 | } |
Nikhil Badola | 807babb | 2015-06-23 09:17:49 +0530 | [diff] [blame] | 105 | |
| 106 | void dwc3_set_fladj(struct dwc3 *dwc3_reg, u32 val) |
| 107 | { |
| 108 | setbits_le32(&dwc3_reg->g_fladj, GFLADJ_30MHZ_REG_SEL | |
| 109 | GFLADJ_30MHZ(val)); |
| 110 | } |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 111 | |
Patrice Chotard | a3d03ea | 2017-07-24 17:07:03 +0200 | [diff] [blame] | 112 | #ifdef CONFIG_DM_USB |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 113 | static int xhci_dwc3_setup_phy(struct udevice *dev) |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 114 | { |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 115 | struct xhci_dwc3_platdata *plat = dev_get_platdata(dev); |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 116 | int i, ret, count; |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 117 | |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 118 | /* Return if no phy declared */ |
| 119 | if (!dev_read_prop(dev, "phys", NULL)) |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 120 | return 0; |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 121 | |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 122 | count = dev_count_phandle_with_args(dev, "phys", "#phy-cells"); |
| 123 | if (count <= 0) |
| 124 | return count; |
| 125 | |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 126 | plat->usb_phys = devm_kcalloc(dev, count, sizeof(struct phy), |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 127 | GFP_KERNEL); |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 128 | if (!plat->usb_phys) |
| 129 | return -ENOMEM; |
| 130 | |
| 131 | for (i = 0; i < count; i++) { |
| 132 | ret = generic_phy_get_by_index(dev, i, &plat->usb_phys[i]); |
| 133 | if (ret && ret != -ENOENT) { |
| 134 | pr_err("Failed to get USB PHY%d for %s\n", |
| 135 | i, dev->name); |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 136 | return ret; |
| 137 | } |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 138 | |
| 139 | ++plat->num_phys; |
| 140 | } |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 141 | |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 142 | for (i = 0; i < plat->num_phys; i++) { |
| 143 | ret = generic_phy_init(&plat->usb_phys[i]); |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 144 | if (ret) { |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 145 | pr_err("Can't init USB PHY%d for %s\n", |
| 146 | i, dev->name); |
| 147 | goto phys_init_err; |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 148 | } |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 149 | } |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 150 | |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 151 | for (i = 0; i < plat->num_phys; i++) { |
| 152 | ret = generic_phy_power_on(&plat->usb_phys[i]); |
Vignesh R | 7ec414e | 2018-03-07 14:50:08 +0530 | [diff] [blame] | 153 | if (ret) { |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 154 | pr_err("Can't power USB PHY%d for %s\n", |
| 155 | i, dev->name); |
| 156 | goto phys_poweron_err; |
Vignesh R | 7ec414e | 2018-03-07 14:50:08 +0530 | [diff] [blame] | 157 | } |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 160 | return 0; |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 161 | |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 162 | phys_poweron_err: |
| 163 | for (; i >= 0; i--) |
| 164 | generic_phy_power_off(&plat->usb_phys[i]); |
| 165 | |
| 166 | for (i = 0; i < plat->num_phys; i++) |
| 167 | generic_phy_exit(&plat->usb_phys[i]); |
| 168 | |
| 169 | return ret; |
| 170 | |
| 171 | phys_init_err: |
| 172 | for (; i >= 0; i--) |
| 173 | generic_phy_exit(&plat->usb_phys[i]); |
| 174 | |
| 175 | return ret; |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 176 | } |
| 177 | |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 178 | static int xhci_dwc3_shutdown_phy(struct udevice *dev) |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 179 | { |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 180 | struct xhci_dwc3_platdata *plat = dev_get_platdata(dev); |
| 181 | int i, ret; |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 182 | |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 183 | for (i = 0; i < plat->num_phys; i++) { |
| 184 | if (!generic_phy_valid(&plat->usb_phys[i])) |
| 185 | continue; |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 186 | |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 187 | ret = generic_phy_power_off(&plat->usb_phys[i]); |
| 188 | ret |= generic_phy_exit(&plat->usb_phys[i]); |
| 189 | if (ret) { |
| 190 | pr_err("Can't shutdown USB PHY%d for %s\n", |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 191 | i, dev->name); |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 192 | } |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int xhci_dwc3_probe(struct udevice *dev) |
| 199 | { |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 200 | struct xhci_hcor *hcor; |
| 201 | struct xhci_hccr *hccr; |
| 202 | struct dwc3 *dwc3_reg; |
| 203 | enum usb_dr_mode dr_mode; |
| 204 | int ret; |
| 205 | |
| 206 | hccr = (struct xhci_hccr *)((uintptr_t)dev_read_addr(dev)); |
| 207 | hcor = (struct xhci_hcor *)((uintptr_t)hccr + |
| 208 | HC_LENGTH(xhci_readl(&(hccr)->cr_capbase))); |
| 209 | |
Neil Armstrong | 08de06f | 2018-04-25 11:39:08 +0200 | [diff] [blame] | 210 | ret = xhci_dwc3_setup_phy(dev); |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 211 | if (ret) |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 212 | return ret; |
Vignesh R | c85d7a9 | 2018-03-07 14:50:10 +0530 | [diff] [blame] | 213 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 214 | dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET); |
| 215 | |
| 216 | dwc3_core_init(dwc3_reg); |
| 217 | |
Patrice Chotard | 17b0887 | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 218 | dr_mode = usb_get_dr_mode(dev_of_offset(dev)); |
| 219 | if (dr_mode == USB_DR_MODE_UNKNOWN) |
| 220 | /* by default set dual role mode to HOST */ |
| 221 | dr_mode = USB_DR_MODE_HOST; |
| 222 | |
| 223 | dwc3_set_mode(dwc3_reg, dr_mode); |
| 224 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 225 | return xhci_register(dev, hccr, hcor); |
| 226 | } |
| 227 | |
| 228 | static int xhci_dwc3_remove(struct udevice *dev) |
| 229 | { |
Neil Armstrong | fcfb0fb | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 230 | xhci_dwc3_shutdown_phy(dev); |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 231 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 232 | return xhci_deregister(dev); |
| 233 | } |
| 234 | |
| 235 | static const struct udevice_id xhci_dwc3_ids[] = { |
| 236 | { .compatible = "snps,dwc3" }, |
| 237 | { } |
| 238 | }; |
| 239 | |
| 240 | U_BOOT_DRIVER(xhci_dwc3) = { |
| 241 | .name = "xhci-dwc3", |
| 242 | .id = UCLASS_USB, |
| 243 | .of_match = xhci_dwc3_ids, |
| 244 | .probe = xhci_dwc3_probe, |
| 245 | .remove = xhci_dwc3_remove, |
| 246 | .ops = &xhci_usb_ops, |
| 247 | .priv_auto_alloc_size = sizeof(struct xhci_ctrl), |
| 248 | .platdata_auto_alloc_size = sizeof(struct xhci_dwc3_platdata), |
| 249 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 250 | }; |
Patrice Chotard | a3d03ea | 2017-07-24 17:07:03 +0200 | [diff] [blame] | 251 | #endif |