Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 1 | /* |
| 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 Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 12 | #include <dm.h> |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 13 | #include <fdtdec.h> |
| 14 | #include <generic-phy.h> |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 15 | #include <usb.h> |
| 16 | |
| 17 | #include "xhci.h" |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | #include <linux/usb/dwc3.h> |
Patrice Chotard | 17b0887 | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 20 | #include <linux/usb/otg.h> |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 21 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 24 | struct xhci_dwc3_platdata { |
| 25 | struct phy usb_phy; |
| 26 | }; |
| 27 | |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 28 | void 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 Yamada | 6d8e433 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 35 | static void dwc3_phy_reset(struct dwc3 *dwc3_reg) |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 36 | { |
| 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 | |
| 52 | void 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 Bhagat | 295d027 | 2015-12-02 11:44:27 +0530 | [diff] [blame] | 60 | mdelay(100); |
| 61 | |
Ramneek Mehresh | 3dad08d | 2015-05-29 14:47:15 +0530 | [diff] [blame] | 62 | /* After PHYs are stable we can take Core out of reset state */ |
| 63 | clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET); |
| 64 | } |
| 65 | |
| 66 | int 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 Badola | 807babb | 2015-06-23 09:17:49 +0530 | [diff] [blame] | 107 | |
| 108 | void 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 Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 113 | |
Patrice Chotard | a3d03ea | 2017-07-24 17:07:03 +0200 | [diff] [blame] | 114 | #ifdef CONFIG_DM_USB |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame^] | 115 | static int xhci_dwc3_setup_phy(struct udevice *dev, int index, struct phy *phy) |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 116 | { |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame^] | 117 | int ret = 0; |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 118 | |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame^] | 119 | ret = generic_phy_get_by_index(dev, index, phy); |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 120 | if (ret) { |
| 121 | if (ret != -ENOENT) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 122 | pr_err("Failed to get USB PHY for %s\n", dev->name); |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 123 | return ret; |
| 124 | } |
| 125 | } else { |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame^] | 126 | ret = generic_phy_init(phy); |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 127 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 128 | pr_err("Can't init USB PHY for %s\n", dev->name); |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 129 | return ret; |
| 130 | } |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame^] | 131 | ret = generic_phy_power_on(phy); |
Vignesh R | 7ec414e | 2018-03-07 14:50:08 +0530 | [diff] [blame] | 132 | if (ret) { |
| 133 | pr_err("Can't power on USB PHY for %s\n", dev->name); |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame^] | 134 | generic_phy_exit(phy); |
Vignesh R | 7ec414e | 2018-03-07 14:50:08 +0530 | [diff] [blame] | 135 | return ret; |
| 136 | } |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame^] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int xhci_dwc3_shutdown_phy(struct phy *phy) |
| 143 | { |
| 144 | int ret = 0; |
| 145 | |
| 146 | if (generic_phy_valid(phy)) { |
| 147 | ret = generic_phy_power_off(phy); |
| 148 | if (ret) |
| 149 | return ret; |
| 150 | |
| 151 | ret = generic_phy_exit(phy); |
| 152 | if (ret) |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int xhci_dwc3_probe(struct udevice *dev) |
| 160 | { |
| 161 | struct xhci_dwc3_platdata *plat = dev_get_platdata(dev); |
| 162 | struct xhci_hcor *hcor; |
| 163 | struct xhci_hccr *hccr; |
| 164 | struct dwc3 *dwc3_reg; |
| 165 | enum usb_dr_mode dr_mode; |
| 166 | int ret; |
| 167 | |
| 168 | hccr = (struct xhci_hccr *)((uintptr_t)dev_read_addr(dev)); |
| 169 | hcor = (struct xhci_hcor *)((uintptr_t)hccr + |
| 170 | HC_LENGTH(xhci_readl(&(hccr)->cr_capbase))); |
| 171 | |
| 172 | ret = xhci_dwc3_setup_phy(dev, 0, &plat->usb_phy); |
| 173 | if (ret) { |
| 174 | pr_err("Failed to setup USB PHY for %s\n", dev->name); |
| 175 | return ret; |
| 176 | } |
| 177 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 178 | dwc3_reg = (struct dwc3 *)((char *)(hccr) + DWC3_REG_OFFSET); |
| 179 | |
| 180 | dwc3_core_init(dwc3_reg); |
| 181 | |
Patrice Chotard | 17b0887 | 2017-07-18 11:38:41 +0200 | [diff] [blame] | 182 | dr_mode = usb_get_dr_mode(dev_of_offset(dev)); |
| 183 | if (dr_mode == USB_DR_MODE_UNKNOWN) |
| 184 | /* by default set dual role mode to HOST */ |
| 185 | dr_mode = USB_DR_MODE_HOST; |
| 186 | |
| 187 | dwc3_set_mode(dwc3_reg, dr_mode); |
| 188 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 189 | return xhci_register(dev, hccr, hcor); |
| 190 | } |
| 191 | |
| 192 | static int xhci_dwc3_remove(struct udevice *dev) |
| 193 | { |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 194 | struct xhci_dwc3_platdata *plat = dev_get_platdata(dev); |
| 195 | int ret; |
| 196 | |
Vignesh R | d52f8a3e9 | 2018-03-07 14:50:09 +0530 | [diff] [blame^] | 197 | ret = xhci_dwc3_shutdown_phy(&plat->usb_phy); |
| 198 | if (ret) |
| 199 | pr_err("Can't shutdown USB PHY for %s\n", dev->name); |
Vignesh R | 7ec414e | 2018-03-07 14:50:08 +0530 | [diff] [blame] | 200 | |
Patrice Chotard | ecfafba | 2017-07-18 11:38:44 +0200 | [diff] [blame] | 201 | |
Patrice Chotard | c7eadfc | 2017-07-18 11:38:40 +0200 | [diff] [blame] | 202 | return xhci_deregister(dev); |
| 203 | } |
| 204 | |
| 205 | static const struct udevice_id xhci_dwc3_ids[] = { |
| 206 | { .compatible = "snps,dwc3" }, |
| 207 | { } |
| 208 | }; |
| 209 | |
| 210 | U_BOOT_DRIVER(xhci_dwc3) = { |
| 211 | .name = "xhci-dwc3", |
| 212 | .id = UCLASS_USB, |
| 213 | .of_match = xhci_dwc3_ids, |
| 214 | .probe = xhci_dwc3_probe, |
| 215 | .remove = xhci_dwc3_remove, |
| 216 | .ops = &xhci_usb_ops, |
| 217 | .priv_auto_alloc_size = sizeof(struct xhci_ctrl), |
| 218 | .platdata_auto_alloc_size = sizeof(struct xhci_dwc3_platdata), |
| 219 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 220 | }; |
Patrice Chotard | a3d03ea | 2017-07-24 17:07:03 +0200 | [diff] [blame] | 221 | #endif |