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 | |
Samuel Holland | 49fb540 | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 10 | #include <clk.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 <generic-phy.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Samuel Holland | 49fb540 | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 14 | #include <reset.h> |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 15 | #include <usb.h> |
Jean-Jacques Hiblot | 3de978a | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 16 | #include <dwc3-uboot.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 18 | |
Jean-Jacques Hiblot | ad4142b | 2019-09-11 11:33:46 +0200 | [diff] [blame] | 19 | #include <usb/xhci.h> |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 20 | #include <asm/io.h> |
| 21 | #include <linux/usb/dwc3.h> |
Patrice Chotard | 17b0887 | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 22 | #include <linux/usb/otg.h> |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 23 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 24 | struct xhci_dwc3_plat { |
Samuel Holland | 49fb540 | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 25 | struct clk_bulk clks; |
developer | b72ae70 | 2020-05-14 13:55:11 +0800 | [diff] [blame] | 26 | struct phy_bulk phys; |
Samuel Holland | 49fb540 | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 27 | struct reset_ctl_bulk resets; |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 30 | void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode) |
| 31 | { |
| 32 | clrsetbits_le32(&dwc3_reg->g_ctl, |
| 33 | DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG), |
| 34 | DWC3_GCTL_PRTCAPDIR(mode)); |
| 35 | } |
| 36 | |
Masahiro Yamada | 6d8e433 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 37 | static void dwc3_phy_reset(struct dwc3 *dwc3_reg) |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 38 | { |
| 39 | /* Assert USB3 PHY reset */ |
| 40 | setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST); |
| 41 | |
| 42 | /* Assert USB2 PHY reset */ |
| 43 | setbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST); |
| 44 | |
| 45 | mdelay(100); |
| 46 | |
| 47 | /* Clear USB3 PHY reset */ |
| 48 | clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST); |
| 49 | |
| 50 | /* Clear USB2 PHY reset */ |
| 51 | clrbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST); |
| 52 | } |
| 53 | |
| 54 | void dwc3_core_soft_reset(struct dwc3 *dwc3_reg) |
| 55 | { |
| 56 | /* Before Resetting PHY, put Core in Reset */ |
| 57 | setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET); |
| 58 | |
| 59 | /* reset USB3 phy - if required */ |
| 60 | dwc3_phy_reset(dwc3_reg); |
| 61 | |
Rajesh Bhagat | 295d027 | 2015-12-02 11:44:27 +0530 | [diff] [blame] | 62 | mdelay(100); |
| 63 | |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 64 | /* After PHYs are stable we can take Core out of reset state */ |
| 65 | clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET); |
| 66 | } |
| 67 | |
| 68 | int dwc3_core_init(struct dwc3 *dwc3_reg) |
| 69 | { |
| 70 | u32 reg; |
| 71 | u32 revision; |
| 72 | unsigned int dwc3_hwparams1; |
| 73 | |
| 74 | revision = readl(&dwc3_reg->g_snpsid); |
| 75 | /* This should read as U3 followed by revision number */ |
Mark Kettenis | f0a916d | 2021-09-16 16:00:09 +0200 | [diff] [blame] | 76 | if ((revision & DWC3_GSNPSID_MASK) != 0x55330000 && |
| 77 | (revision & DWC3_GSNPSID_MASK) != 0x33310000) { |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 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 Badola | 807babb | 2015-06-23 09:17:49 +0530 | [diff] [blame] | 110 | |
| 111 | void 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 Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 116 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 117 | #if CONFIG_IS_ENABLED(DM_USB) |
Samuel Holland | 49fb540 | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 118 | static 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 | |
| 138 | static 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 | |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 158 | static int xhci_dwc3_probe(struct udevice *dev) |
| 159 | { |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 160 | struct xhci_hcor *hcor; |
| 161 | struct xhci_hccr *hccr; |
| 162 | struct dwc3 *dwc3_reg; |
| 163 | enum usb_dr_mode dr_mode; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 164 | struct xhci_dwc3_plat *plat = dev_get_plat(dev); |
Mark Kettenis | b2bb622 | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 165 | const char *phy; |
| 166 | u32 reg; |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 167 | int ret; |
| 168 | |
Samuel Holland | 49fb540 | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 169 | 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 Roese | cf35437 | 2020-08-24 13:04:36 +0200 | [diff] [blame] | 177 | hccr = (struct xhci_hccr *)((uintptr_t)dev_remap_addr(dev)); |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 178 | hcor = (struct xhci_hcor *)((uintptr_t)hccr + |
| 179 | HC_LENGTH(xhci_readl(&(hccr)->cr_capbase))); |
| 180 | |
developer | b72ae70 | 2020-05-14 13:55:11 +0800 | [diff] [blame] | 181 | ret = dwc3_setup_phy(dev, &plat->phys); |
Jean-Jacques Hiblot | 3de978a | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 182 | if (ret && (ret != -ENOTSUPP)) |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 183 | return ret; |
Vignesh R | c85d7a9 | 2018-03-07 14:50:10 +0530 | [diff] [blame] | 184 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 185 | dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET); |
| 186 | |
| 187 | dwc3_core_init(dwc3_reg); |
| 188 | |
Mark Kettenis | b2bb622 | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 189 | /* 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 | |
Jonas Karlman | 96a0473 | 2024-03-02 13:09:49 +0000 | [diff] [blame] | 199 | if (dev_read_bool(dev, "snps,dis_enblslpm_quirk")) |
Mark Kettenis | b2bb622 | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 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 Armstrong | 8ef7530 | 2019-09-09 18:52:39 +0000 | [diff] [blame] | 205 | if (dev_read_bool(dev, "snps,dis_u2_susphy_quirk")) |
| 206 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
| 207 | |
Mark Kettenis | b2bb622 | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 208 | writel(reg, &dwc3_reg->g_usb2phycfg[0]); |
| 209 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 210 | dr_mode = usb_get_dr_mode(dev_ofnode(dev)); |
Mark Kettenis | 5732eae | 2022-04-19 21:06:33 +0200 | [diff] [blame] | 211 | if (dr_mode == USB_DR_MODE_OTG && |
| 212 | dev_read_bool(dev, "usb-role-switch")) { |
| 213 | dr_mode = usb_get_role_switch_default_mode(dev_ofnode(dev)); |
| 214 | if (dr_mode == USB_DR_MODE_UNKNOWN) |
| 215 | dr_mode = USB_DR_MODE_OTG; |
| 216 | } |
Patrice Chotard | 17b0887 | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 217 | if (dr_mode == USB_DR_MODE_UNKNOWN) |
| 218 | /* by default set dual role mode to HOST */ |
| 219 | dr_mode = USB_DR_MODE_HOST; |
| 220 | |
| 221 | dwc3_set_mode(dwc3_reg, dr_mode); |
| 222 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 223 | return xhci_register(dev, hccr, hcor); |
| 224 | } |
| 225 | |
| 226 | static int xhci_dwc3_remove(struct udevice *dev) |
| 227 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 228 | struct xhci_dwc3_plat *plat = dev_get_plat(dev); |
Jean-Jacques Hiblot | 3de978a | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 229 | |
developer | b72ae70 | 2020-05-14 13:55:11 +0800 | [diff] [blame] | 230 | dwc3_shutdown_phy(dev, &plat->phys); |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 231 | |
Samuel Holland | 49fb540 | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 232 | clk_release_bulk(&plat->clks); |
| 233 | |
| 234 | reset_release_bulk(&plat->resets); |
| 235 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 236 | return xhci_deregister(dev); |
| 237 | } |
| 238 | |
| 239 | static const struct udevice_id xhci_dwc3_ids[] = { |
| 240 | { .compatible = "snps,dwc3" }, |
| 241 | { } |
| 242 | }; |
| 243 | |
| 244 | U_BOOT_DRIVER(xhci_dwc3) = { |
| 245 | .name = "xhci-dwc3", |
| 246 | .id = UCLASS_USB, |
| 247 | .of_match = xhci_dwc3_ids, |
| 248 | .probe = xhci_dwc3_probe, |
| 249 | .remove = xhci_dwc3_remove, |
| 250 | .ops = &xhci_usb_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 251 | .priv_auto = sizeof(struct xhci_ctrl), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 252 | .plat_auto = sizeof(struct xhci_dwc3_plat), |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 253 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 254 | }; |
Patrice Chotard | a3d03ea | 2017-07-24 17:07:03 +0200 | [diff] [blame] | 255 | #endif |