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 | |
| 10 | #include <common.h> |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 11 | #include <dm.h> |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 12 | #include <generic-phy.h> |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 13 | #include <usb.h> |
Jean-Jacques Hiblot | d648a50 | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 14 | #include <dwc3-uboot.h> |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 15 | |
Jean-Jacques Hiblot | 1708a12 | 2019-09-11 11:33:46 +0200 | [diff] [blame] | 16 | #include <usb/xhci.h> |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <linux/usb/dwc3.h> |
Patrice Chotard | 576e3cc | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 19 | #include <linux/usb/otg.h> |
Ramneek Mehresh | dc9cdf8 | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 20 | |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 21 | struct xhci_dwc3_platdata { |
Neil Armstrong | 7c839ea | 2018-04-11 17:08:01 +0200 | [diff] [blame] | 22 | struct phy *usb_phys; |
| 23 | int num_phys; |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
Ramneek Mehresh | dc9cdf8 | 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 | 121a4d1 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 33 | static void dwc3_phy_reset(struct dwc3 *dwc3_reg) |
Ramneek Mehresh | dc9cdf8 | 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 | 5955bb9 | 2015-12-02 11:44:27 +0530 | [diff] [blame] | 58 | mdelay(100); |
| 59 | |
Ramneek Mehresh | dc9cdf8 | 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 | 667f4dd | 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 | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 111 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 112 | #if CONFIG_IS_ENABLED(DM_USB) |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 113 | static int xhci_dwc3_probe(struct udevice *dev) |
| 114 | { |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 115 | struct xhci_hcor *hcor; |
| 116 | struct xhci_hccr *hccr; |
| 117 | struct dwc3 *dwc3_reg; |
Patrice Chotard | 576e3cc | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 118 | enum usb_dr_mode dr_mode; |
Jean-Jacques Hiblot | d648a50 | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 119 | struct xhci_dwc3_platdata *plat = dev_get_platdata(dev); |
Mark Kettenis | 062790f | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 120 | const char *phy; |
| 121 | u32 reg; |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 122 | int ret; |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 123 | |
Patrice Chotard | d38a8ea | 2017-07-25 13:24:44 +0200 | [diff] [blame] | 124 | hccr = (struct xhci_hccr *)((uintptr_t)dev_read_addr(dev)); |
| 125 | hcor = (struct xhci_hcor *)((uintptr_t)hccr + |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 126 | HC_LENGTH(xhci_readl(&(hccr)->cr_capbase))); |
| 127 | |
Jean-Jacques Hiblot | d648a50 | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 128 | ret = dwc3_setup_phy(dev, &plat->usb_phys, &plat->num_phys); |
| 129 | if (ret && (ret != -ENOTSUPP)) |
Vignesh R | 3fc2635 | 2018-03-07 14:50:09 +0530 | [diff] [blame] | 130 | return ret; |
Vignesh R | 2fd4242 | 2018-03-07 14:50:10 +0530 | [diff] [blame] | 131 | |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 132 | dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET); |
| 133 | |
| 134 | dwc3_core_init(dwc3_reg); |
| 135 | |
Mark Kettenis | 062790f | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 136 | /* Set dwc3 usb2 phy config */ |
| 137 | reg = readl(&dwc3_reg->g_usb2phycfg[0]); |
| 138 | |
| 139 | phy = dev_read_string(dev, "phy_type"); |
| 140 | if (phy && strcmp(phy, "utmi_wide") == 0) { |
| 141 | reg |= DWC3_GUSB2PHYCFG_PHYIF; |
| 142 | reg &= ~DWC3_GUSB2PHYCFG_USBTRDTIM_MASK; |
| 143 | reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_16BIT; |
| 144 | } |
| 145 | |
| 146 | if (dev_read_bool(dev, "snps,dis_enblslpm-quirk")) |
| 147 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 148 | |
| 149 | if (dev_read_bool(dev, "snps,dis-u2-freeclk-exists-quirk")) |
| 150 | reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS; |
| 151 | |
Neil Armstrong | b35b807 | 2019-09-09 18:52:39 +0000 | [diff] [blame] | 152 | if (dev_read_bool(dev, "snps,dis_u2_susphy_quirk")) |
| 153 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
| 154 | |
Mark Kettenis | 062790f | 2019-06-30 18:01:55 +0200 | [diff] [blame] | 155 | writel(reg, &dwc3_reg->g_usb2phycfg[0]); |
| 156 | |
Kever Yang | ac28e59 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 157 | dr_mode = usb_get_dr_mode(dev->node); |
Patrice Chotard | 576e3cc | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 158 | if (dr_mode == USB_DR_MODE_UNKNOWN) |
| 159 | /* by default set dual role mode to HOST */ |
| 160 | dr_mode = USB_DR_MODE_HOST; |
| 161 | |
| 162 | dwc3_set_mode(dwc3_reg, dr_mode); |
| 163 | |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 164 | return xhci_register(dev, hccr, hcor); |
| 165 | } |
| 166 | |
| 167 | static int xhci_dwc3_remove(struct udevice *dev) |
| 168 | { |
Jean-Jacques Hiblot | d648a50 | 2018-11-29 10:52:45 +0100 | [diff] [blame] | 169 | struct xhci_dwc3_platdata *plat = dev_get_platdata(dev); |
| 170 | |
| 171 | dwc3_shutdown_phy(dev, plat->usb_phys, plat->num_phys); |
Patrice Chotard | f56db16 | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 172 | |
Patrice Chotard | b7c1c7d | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 173 | return xhci_deregister(dev); |
| 174 | } |
| 175 | |
| 176 | static const struct udevice_id xhci_dwc3_ids[] = { |
| 177 | { .compatible = "snps,dwc3" }, |
| 178 | { } |
| 179 | }; |
| 180 | |
| 181 | U_BOOT_DRIVER(xhci_dwc3) = { |
| 182 | .name = "xhci-dwc3", |
| 183 | .id = UCLASS_USB, |
| 184 | .of_match = xhci_dwc3_ids, |
| 185 | .probe = xhci_dwc3_probe, |
| 186 | .remove = xhci_dwc3_remove, |
| 187 | .ops = &xhci_usb_ops, |
| 188 | .priv_auto_alloc_size = sizeof(struct xhci_ctrl), |
| 189 | .platdata_auto_alloc_size = sizeof(struct xhci_dwc3_platdata), |
| 190 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 191 | }; |
Patrice Chotard | 623b7ac | 2017-07-24 17:07:03 +0200 | [diff] [blame] | 192 | #endif |