Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ramneek Mehresh | dc9cdf8 | 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 | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
Samuel Holland | 91ce84a | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 10 | #include <clk.h> |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 11 | #include <common.h> |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 12 | #include <dm.h> |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 13 | #include <generic-phy.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Samuel Holland | 91ce84a | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 15 | #include <reset.h> |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 16 | #include <usb.h> |
Jean-Jacques Hiblot | d648a50 | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 17 | #include <dwc3-uboot.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 19 | |
Jean-Jacques Hiblot | 1708a12 | 2019-09-11 11:33:46 +0200 | [diff] [blame] | 20 | #include <usb/xhci.h> |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | #include <linux/usb/dwc3.h> |
Patrice Chotard | 576e3cc | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 23 | #include <linux/usb/otg.h> |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 24 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 25 | struct xhci_dwc3_plat { |
Samuel Holland | 91ce84a | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 26 | struct clk_bulk clks; |
Chunfeng Yun | 58221d7 | 2020-05-14 13:55:11 +0800 | [diff] [blame] | 27 | struct phy_bulk phys; |
Samuel Holland | 91ce84a | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 28 | struct reset_ctl_bulk resets; |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 31 | void 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 Yamada | 121a4d1 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 38 | static void dwc3_phy_reset(struct dwc3 *dwc3_reg) |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 39 | { |
| 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 | |
| 55 | void 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 Bhagat | 5955bb9 | 2015-12-02 11:44:27 +0530 | [diff] [blame] | 63 | mdelay(100); |
| 64 | |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 65 | /* After PHYs are stable we can take Core out of reset state */ |
| 66 | clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET); |
| 67 | } |
| 68 | |
| 69 | int 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 */ |
Mark Kettenis | 53eed9a | 2021-09-16 16:00:09 +0200 | [diff] [blame] | 77 | if ((revision & DWC3_GSNPSID_MASK) != 0x55330000 && |
| 78 | (revision & DWC3_GSNPSID_MASK) != 0x33310000) { |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 79 | puts("this is not a DesignWare USB3 DRD Core\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | dwc3_core_soft_reset(dwc3_reg); |
| 84 | |
| 85 | dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1); |
| 86 | |
| 87 | reg = readl(&dwc3_reg->g_ctl); |
| 88 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
| 89 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
| 90 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) { |
| 91 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
| 92 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; |
| 93 | break; |
| 94 | default: |
| 95 | debug("No power optimization available\n"); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * WORKAROUND: DWC3 revisions <1.90a have a bug |
| 100 | * where the device can fail to connect at SuperSpeed |
| 101 | * and falls back to high-speed mode which causes |
| 102 | * the device to enter a Connect/Disconnect loop |
| 103 | */ |
| 104 | if ((revision & DWC3_REVISION_MASK) < 0x190a) |
| 105 | reg |= DWC3_GCTL_U2RSTECN; |
| 106 | |
| 107 | writel(reg, &dwc3_reg->g_ctl); |
| 108 | |
| 109 | return 0; |
| 110 | } |
Nikhil Badola | 667f4dd | 2015-06-23 09:17:49 +0530 | [diff] [blame] | 111 | |
| 112 | void dwc3_set_fladj(struct dwc3 *dwc3_reg, u32 val) |
| 113 | { |
| 114 | setbits_le32(&dwc3_reg->g_fladj, GFLADJ_30MHZ_REG_SEL | |
| 115 | GFLADJ_30MHZ(val)); |
| 116 | } |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 117 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 118 | #if CONFIG_IS_ENABLED(DM_USB) |
Samuel Holland | 91ce84a | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 119 | static int xhci_dwc3_reset_init(struct udevice *dev, |
| 120 | struct xhci_dwc3_plat *plat) |
| 121 | { |
| 122 | int ret; |
| 123 | |
| 124 | ret = reset_get_bulk(dev, &plat->resets); |
| 125 | if (ret == -ENOTSUPP || ret == -ENOENT) |
| 126 | return 0; |
| 127 | else if (ret) |
| 128 | return ret; |
| 129 | |
| 130 | ret = reset_deassert_bulk(&plat->resets); |
| 131 | if (ret) { |
| 132 | reset_release_bulk(&plat->resets); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int xhci_dwc3_clk_init(struct udevice *dev, |
| 140 | struct xhci_dwc3_plat *plat) |
| 141 | { |
| 142 | int ret; |
| 143 | |
| 144 | ret = clk_get_bulk(dev, &plat->clks); |
| 145 | if (ret == -ENOSYS || ret == -ENOENT) |
| 146 | return 0; |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | ret = clk_enable_bulk(&plat->clks); |
| 151 | if (ret) { |
| 152 | clk_release_bulk(&plat->clks); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 159 | static int xhci_dwc3_probe(struct udevice *dev) |
| 160 | { |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 161 | struct xhci_hcor *hcor; |
| 162 | struct xhci_hccr *hccr; |
| 163 | struct dwc3 *dwc3_reg; |
Patrice Chotard | 576e3cc | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 164 | enum usb_dr_mode dr_mode; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 165 | struct xhci_dwc3_plat *plat = dev_get_plat(dev); |
Mark Kettenis | 062790f | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 166 | const char *phy; |
| 167 | u32 reg; |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 168 | int ret; |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 169 | |
Samuel Holland | 91ce84a | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 170 | ret = xhci_dwc3_reset_init(dev, plat); |
| 171 | if (ret) |
| 172 | return ret; |
| 173 | |
| 174 | ret = xhci_dwc3_clk_init(dev, plat); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | |
Stefan Roese | 72a53ac | 2020-08-24 13:04:36 +0200 | [diff] [blame] | 178 | hccr = (struct xhci_hccr *)((uintptr_t)dev_remap_addr(dev)); |
Patrice Chotard | d38a8ea | 2017-07-25 13:24:44 +0200 | [diff] [blame] | 179 | hcor = (struct xhci_hcor *)((uintptr_t)hccr + |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 180 | HC_LENGTH(xhci_readl(&(hccr)->cr_capbase))); |
| 181 | |
Chunfeng Yun | 58221d7 | 2020-05-14 13:55:11 +0800 | [diff] [blame] | 182 | ret = dwc3_setup_phy(dev, &plat->phys); |
Jean-Jacques Hiblot | d648a50 | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 183 | if (ret && (ret != -ENOTSUPP)) |
Vignesh R | 3fc2635 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 184 | return ret; |
Vignesh R | 2fd4242 | 2018-03-07 14:50:10 +0530 | [diff] [blame] | 185 | |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 186 | dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET); |
| 187 | |
| 188 | dwc3_core_init(dwc3_reg); |
| 189 | |
Mark Kettenis | 062790f | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 190 | /* Set dwc3 usb2 phy config */ |
| 191 | reg = readl(&dwc3_reg->g_usb2phycfg[0]); |
| 192 | |
| 193 | phy = dev_read_string(dev, "phy_type"); |
| 194 | if (phy && strcmp(phy, "utmi_wide") == 0) { |
| 195 | reg |= DWC3_GUSB2PHYCFG_PHYIF; |
| 196 | reg &= ~DWC3_GUSB2PHYCFG_USBTRDTIM_MASK; |
| 197 | reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_16BIT; |
| 198 | } |
| 199 | |
| 200 | if (dev_read_bool(dev, "snps,dis_enblslpm-quirk")) |
| 201 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 202 | |
| 203 | if (dev_read_bool(dev, "snps,dis-u2-freeclk-exists-quirk")) |
| 204 | reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS; |
| 205 | |
Neil Armstrong | b35b807 | 2019-09-09 18:52:39 +0000 | [diff] [blame] | 206 | if (dev_read_bool(dev, "snps,dis_u2_susphy_quirk")) |
| 207 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
| 208 | |
Mark Kettenis | 062790f | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 209 | writel(reg, &dwc3_reg->g_usb2phycfg[0]); |
| 210 | |
Simon Glass | f10643c | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 211 | dr_mode = usb_get_dr_mode(dev_ofnode(dev)); |
Mark Kettenis | 6a6468f | 2022-04-19 21:06:33 +0200 | [diff] [blame] | 212 | if (dr_mode == USB_DR_MODE_OTG && |
| 213 | dev_read_bool(dev, "usb-role-switch")) { |
| 214 | dr_mode = usb_get_role_switch_default_mode(dev_ofnode(dev)); |
| 215 | if (dr_mode == USB_DR_MODE_UNKNOWN) |
| 216 | dr_mode = USB_DR_MODE_OTG; |
| 217 | } |
Patrice Chotard | 576e3cc | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 218 | if (dr_mode == USB_DR_MODE_UNKNOWN) |
| 219 | /* by default set dual role mode to HOST */ |
| 220 | dr_mode = USB_DR_MODE_HOST; |
| 221 | |
| 222 | dwc3_set_mode(dwc3_reg, dr_mode); |
| 223 | |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 224 | return xhci_register(dev, hccr, hcor); |
| 225 | } |
| 226 | |
| 227 | static int xhci_dwc3_remove(struct udevice *dev) |
| 228 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 229 | struct xhci_dwc3_plat *plat = dev_get_plat(dev); |
Jean-Jacques Hiblot | d648a50 | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 230 | |
Chunfeng Yun | 58221d7 | 2020-05-14 13:55:11 +0800 | [diff] [blame] | 231 | dwc3_shutdown_phy(dev, &plat->phys); |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 232 | |
Samuel Holland | 91ce84a | 2021-07-05 13:29:03 +0100 | [diff] [blame] | 233 | clk_release_bulk(&plat->clks); |
| 234 | |
| 235 | reset_release_bulk(&plat->resets); |
| 236 | |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 237 | return xhci_deregister(dev); |
| 238 | } |
| 239 | |
| 240 | static const struct udevice_id xhci_dwc3_ids[] = { |
| 241 | { .compatible = "snps,dwc3" }, |
| 242 | { } |
| 243 | }; |
| 244 | |
| 245 | U_BOOT_DRIVER(xhci_dwc3) = { |
| 246 | .name = "xhci-dwc3", |
| 247 | .id = UCLASS_USB, |
| 248 | .of_match = xhci_dwc3_ids, |
| 249 | .probe = xhci_dwc3_probe, |
| 250 | .remove = xhci_dwc3_remove, |
| 251 | .ops = &xhci_usb_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 252 | .priv_auto = sizeof(struct xhci_ctrl), |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 253 | .plat_auto = sizeof(struct xhci_dwc3_plat), |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 254 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 255 | }; |
Patrice Chotard | 623b7ac | 2017-07-24 17:07:03 +0200 | [diff] [blame] | 256 | #endif |