Patrick Delaunay | d6e53c7 | 2018-10-26 09:02:52 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Generic DWC3 Glue layer |
| 4 | * |
| 5 | * Copyright (C) 2016 - 2018 Xilinx, Inc. |
| 6 | * |
| 7 | * Based on dwc3-omap.c. |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 13 | #include <dm.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/lists.h> |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 16 | #include <dwc3-uboot.h> |
Michal Simek | bb19d62 | 2022-03-09 10:05:45 +0100 | [diff] [blame] | 17 | #include <generic-phy.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 19 | #include <linux/delay.h> |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 20 | #include <linux/usb/ch9.h> |
| 21 | #include <linux/usb/gadget.h> |
| 22 | #include <malloc.h> |
| 23 | #include <usb.h> |
| 24 | #include "core.h" |
| 25 | #include "gadget.h" |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 26 | #include <reset.h> |
| 27 | #include <clk.h> |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 28 | #include <usb/xhci.h> |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 29 | #include <asm/gpio.h> |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 30 | |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 31 | struct dwc3_glue_data { |
| 32 | struct clk_bulk clks; |
| 33 | struct reset_ctl_bulk resets; |
| 34 | fdt_addr_t regs; |
| 35 | }; |
| 36 | |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 37 | struct dwc3_generic_plat { |
| 38 | fdt_addr_t base; |
| 39 | u32 maximum_speed; |
| 40 | enum usb_dr_mode dr_mode; |
| 41 | }; |
| 42 | |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 43 | struct dwc3_generic_priv { |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 44 | void *base; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 45 | struct dwc3 dwc3; |
developer | f8bced1 | 2020-05-02 11:35:13 +0200 | [diff] [blame] | 46 | struct phy_bulk phys; |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 47 | struct gpio_desc ulpi_reset; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 50 | struct dwc3_generic_host_priv { |
| 51 | struct xhci_ctrl xhci_ctrl; |
| 52 | struct dwc3_generic_priv gen_priv; |
| 53 | }; |
| 54 | |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 55 | static int dwc3_generic_probe(struct udevice *dev, |
| 56 | struct dwc3_generic_priv *priv) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 57 | { |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 58 | int rc; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 59 | struct dwc3_generic_plat *plat = dev_get_plat(dev); |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 60 | struct dwc3 *dwc3 = &priv->dwc3; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 61 | struct dwc3_glue_data *glue = dev_get_plat(dev->parent); |
Marek Vasut | aacbcb6 | 2022-11-27 15:31:52 +0100 | [diff] [blame] | 62 | int __maybe_unused index; |
| 63 | ofnode __maybe_unused node; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 64 | |
Jean-Jacques Hiblot | ce868d0 | 2019-09-11 11:33:52 +0200 | [diff] [blame] | 65 | dwc3->dev = dev; |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 66 | dwc3->maximum_speed = plat->maximum_speed; |
| 67 | dwc3->dr_mode = plat->dr_mode; |
Jean-Jacques Hiblot | ce868d0 | 2019-09-11 11:33:52 +0200 | [diff] [blame] | 68 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 69 | dwc3_of_parse(dwc3); |
Marek Vasut | aacbcb6 | 2022-11-27 15:31:52 +0100 | [diff] [blame] | 70 | |
Marek Vasut | 4d28572 | 2023-02-20 14:50:25 +0900 | [diff] [blame] | 71 | /* |
| 72 | * There are currently four disparate placement possibilities of DWC3 |
| 73 | * reference clock phandle in SoC DTs: |
| 74 | * - in top level glue node, with generic subnode without clock (ZynqMP) |
| 75 | * - in top level generic node, with no subnode (i.MX8MQ) |
| 76 | * - in generic subnode, with other clock in top level node (i.MX8MP) |
| 77 | * - in both top level node and generic subnode (Rockchip) |
| 78 | * Cover all the possibilities here by looking into both nodes, start |
| 79 | * with the top level node as that seems to be used in majority of DTs |
| 80 | * to reference the clock. |
| 81 | */ |
Marek Vasut | aacbcb6 | 2022-11-27 15:31:52 +0100 | [diff] [blame] | 82 | node = dev_ofnode(dev->parent); |
| 83 | index = ofnode_stringlist_search(node, "clock-names", "ref"); |
| 84 | if (index < 0) |
| 85 | index = ofnode_stringlist_search(node, "clock-names", "ref_clk"); |
Marek Vasut | 4d28572 | 2023-02-20 14:50:25 +0900 | [diff] [blame] | 86 | if (index < 0) { |
| 87 | node = dev_ofnode(dev); |
| 88 | index = ofnode_stringlist_search(node, "clock-names", "ref"); |
| 89 | if (index < 0) |
| 90 | index = ofnode_stringlist_search(node, "clock-names", "ref_clk"); |
| 91 | } |
Marek Vasut | aacbcb6 | 2022-11-27 15:31:52 +0100 | [diff] [blame] | 92 | if (index >= 0) |
| 93 | dwc3->ref_clk = &glue->clks.clks[index]; |
Jean-Jacques Hiblot | ce868d0 | 2019-09-11 11:33:52 +0200 | [diff] [blame] | 94 | #endif |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 95 | |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 96 | /* |
| 97 | * It must hold whole USB3.0 OTG controller in resetting to hold pipe |
| 98 | * power state in P2 before initializing TypeC PHY on RK3399 platform. |
| 99 | */ |
| 100 | if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) { |
| 101 | reset_assert_bulk(&glue->resets); |
| 102 | udelay(1); |
| 103 | } |
| 104 | |
developer | f8bced1 | 2020-05-02 11:35:13 +0200 | [diff] [blame] | 105 | rc = dwc3_setup_phy(dev, &priv->phys); |
Siva Durga Prasad Paladugu | c37f8f3 | 2020-10-21 14:17:31 +0200 | [diff] [blame] | 106 | if (rc && rc != -ENOTSUPP) |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 107 | return rc; |
| 108 | |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 109 | if (CONFIG_IS_ENABLED(DM_GPIO) && |
| 110 | device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) { |
| 111 | rc = gpio_request_by_name(dev->parent, "reset-gpios", 0, |
| 112 | &priv->ulpi_reset, GPIOD_ACTIVE_LOW); |
| 113 | if (rc) |
| 114 | return rc; |
| 115 | |
| 116 | /* Toggle ulpi to reset the phy. */ |
| 117 | rc = dm_gpio_set_value(&priv->ulpi_reset, 1); |
| 118 | if (rc) |
| 119 | return rc; |
| 120 | |
| 121 | mdelay(5); |
| 122 | |
| 123 | rc = dm_gpio_set_value(&priv->ulpi_reset, 0); |
| 124 | if (rc) |
| 125 | return rc; |
| 126 | |
| 127 | mdelay(5); |
| 128 | } |
| 129 | |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 130 | if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) |
| 131 | reset_deassert_bulk(&glue->resets); |
| 132 | |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 133 | priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE); |
| 134 | dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START; |
Jean-Jacques Hiblot | ce868d0 | 2019-09-11 11:33:52 +0200 | [diff] [blame] | 135 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 136 | |
| 137 | rc = dwc3_init(dwc3); |
| 138 | if (rc) { |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 139 | unmap_physmem(priv->base, MAP_NOCACHE); |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 140 | return rc; |
| 141 | } |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 142 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 143 | return 0; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 146 | static int dwc3_generic_remove(struct udevice *dev, |
| 147 | struct dwc3_generic_priv *priv) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 148 | { |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 149 | struct dwc3 *dwc3 = &priv->dwc3; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 150 | |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 151 | if (CONFIG_IS_ENABLED(DM_GPIO) && |
| 152 | device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) { |
| 153 | struct gpio_desc *ulpi_reset = &priv->ulpi_reset; |
| 154 | |
| 155 | dm_gpio_free(ulpi_reset->dev, ulpi_reset); |
| 156 | } |
| 157 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 158 | dwc3_remove(dwc3); |
developer | f8bced1 | 2020-05-02 11:35:13 +0200 | [diff] [blame] | 159 | dwc3_shutdown_phy(dev, &priv->phys); |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 160 | unmap_physmem(dwc3->regs, MAP_NOCACHE); |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 165 | static int dwc3_generic_of_to_plat(struct udevice *dev) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 166 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 167 | struct dwc3_generic_plat *plat = dev_get_plat(dev); |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 168 | ofnode node = dev_ofnode(dev); |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 169 | |
Angus Ainslie | 6e382a8 | 2022-02-02 15:08:54 -0800 | [diff] [blame] | 170 | if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) { |
| 171 | /* This is a leaf so check the parent */ |
| 172 | plat->base = dev_read_addr(dev->parent); |
| 173 | } else { |
| 174 | plat->base = dev_read_addr(dev); |
| 175 | } |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 176 | |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 177 | plat->maximum_speed = usb_get_maximum_speed(node); |
| 178 | if (plat->maximum_speed == USB_SPEED_UNKNOWN) { |
Jean-Jacques Hiblot | 547df0d | 2019-09-11 11:33:51 +0200 | [diff] [blame] | 179 | pr_info("No USB maximum speed specified. Using super speed\n"); |
| 180 | plat->maximum_speed = USB_SPEED_SUPER; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 183 | plat->dr_mode = usb_get_dr_mode(node); |
| 184 | if (plat->dr_mode == USB_DR_MODE_UNKNOWN) { |
Angus Ainslie | 6e382a8 | 2022-02-02 15:08:54 -0800 | [diff] [blame] | 185 | /* might be a leaf so check the parent for mode */ |
| 186 | node = dev_ofnode(dev->parent); |
| 187 | plat->dr_mode = usb_get_dr_mode(node); |
| 188 | if (plat->dr_mode == USB_DR_MODE_UNKNOWN) { |
| 189 | pr_err("Invalid usb mode setup\n"); |
| 190 | return -ENODEV; |
| 191 | } |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 197 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
| 198 | int dm_usb_gadget_handle_interrupts(struct udevice *dev) |
| 199 | { |
| 200 | struct dwc3_generic_priv *priv = dev_get_priv(dev); |
| 201 | struct dwc3 *dwc3 = &priv->dwc3; |
| 202 | |
| 203 | dwc3_gadget_uboot_handle_interrupt(dwc3); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int dwc3_generic_peripheral_probe(struct udevice *dev) |
| 209 | { |
| 210 | struct dwc3_generic_priv *priv = dev_get_priv(dev); |
| 211 | |
| 212 | return dwc3_generic_probe(dev, priv); |
| 213 | } |
| 214 | |
| 215 | static int dwc3_generic_peripheral_remove(struct udevice *dev) |
| 216 | { |
| 217 | struct dwc3_generic_priv *priv = dev_get_priv(dev); |
| 218 | |
| 219 | return dwc3_generic_remove(dev, priv); |
| 220 | } |
| 221 | |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 222 | U_BOOT_DRIVER(dwc3_generic_peripheral) = { |
| 223 | .name = "dwc3-generic-peripheral", |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 224 | .id = UCLASS_USB_GADGET_GENERIC, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 225 | .of_to_plat = dwc3_generic_of_to_plat, |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 226 | .probe = dwc3_generic_peripheral_probe, |
| 227 | .remove = dwc3_generic_peripheral_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 228 | .priv_auto = sizeof(struct dwc3_generic_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 229 | .plat_auto = sizeof(struct dwc3_generic_plat), |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 230 | }; |
Jean-Jacques Hiblot | 44aaec7 | 2018-11-29 10:52:42 +0100 | [diff] [blame] | 231 | #endif |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 232 | |
Simon Glass | 1f2440c | 2021-07-10 21:14:29 -0600 | [diff] [blame] | 233 | #if defined(CONFIG_SPL_USB_HOST) || \ |
Kunihiko Hayashi | b032aa6 | 2021-05-12 23:11:14 +0900 | [diff] [blame] | 234 | !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST) |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 235 | static int dwc3_generic_host_probe(struct udevice *dev) |
| 236 | { |
| 237 | struct xhci_hcor *hcor; |
| 238 | struct xhci_hccr *hccr; |
| 239 | struct dwc3_generic_host_priv *priv = dev_get_priv(dev); |
| 240 | int rc; |
| 241 | |
| 242 | rc = dwc3_generic_probe(dev, &priv->gen_priv); |
| 243 | if (rc) |
| 244 | return rc; |
| 245 | |
| 246 | hccr = (struct xhci_hccr *)priv->gen_priv.base; |
| 247 | hcor = (struct xhci_hcor *)(priv->gen_priv.base + |
| 248 | HC_LENGTH(xhci_readl(&(hccr)->cr_capbase))); |
| 249 | |
| 250 | return xhci_register(dev, hccr, hcor); |
| 251 | } |
| 252 | |
| 253 | static int dwc3_generic_host_remove(struct udevice *dev) |
| 254 | { |
| 255 | struct dwc3_generic_host_priv *priv = dev_get_priv(dev); |
| 256 | int rc; |
| 257 | |
| 258 | rc = xhci_deregister(dev); |
| 259 | if (rc) |
| 260 | return rc; |
| 261 | |
| 262 | return dwc3_generic_remove(dev, &priv->gen_priv); |
| 263 | } |
| 264 | |
| 265 | U_BOOT_DRIVER(dwc3_generic_host) = { |
| 266 | .name = "dwc3-generic-host", |
| 267 | .id = UCLASS_USB, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 268 | .of_to_plat = dwc3_generic_of_to_plat, |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 269 | .probe = dwc3_generic_host_probe, |
| 270 | .remove = dwc3_generic_host_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 271 | .priv_auto = sizeof(struct dwc3_generic_host_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 272 | .plat_auto = sizeof(struct dwc3_generic_plat), |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 273 | .ops = &xhci_usb_ops, |
| 274 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 275 | }; |
| 276 | #endif |
| 277 | |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 278 | struct dwc3_glue_ops { |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 279 | int (*glue_get_ctrl_dev)(struct udevice *parent, ofnode *node); |
Marek Vasut | 68c8656 | 2022-04-13 00:42:55 +0200 | [diff] [blame] | 280 | void (*glue_configure)(struct udevice *dev, int index, |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 281 | enum usb_dr_mode mode); |
| 282 | }; |
| 283 | |
Marek Vasut | ae21934 | 2022-04-13 00:42:56 +0200 | [diff] [blame] | 284 | void dwc3_imx8mp_glue_configure(struct udevice *dev, int index, |
| 285 | enum usb_dr_mode mode) |
| 286 | { |
| 287 | /* USB glue registers */ |
| 288 | #define USB_CTRL0 0x00 |
| 289 | #define USB_CTRL1 0x04 |
| 290 | |
| 291 | #define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */ |
| 292 | #define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */ |
| 293 | #define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */ |
| 294 | |
| 295 | #define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */ |
| 296 | #define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */ |
| 297 | fdt_addr_t regs = dev_read_addr_index(dev, 1); |
| 298 | void *base = map_physmem(regs, 0x8, MAP_NOCACHE); |
| 299 | u32 value; |
| 300 | |
| 301 | value = readl(base + USB_CTRL0); |
| 302 | |
| 303 | if (dev_read_bool(dev, "fsl,permanently-attached")) |
| 304 | value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED); |
| 305 | else |
| 306 | value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED); |
| 307 | |
| 308 | if (dev_read_bool(dev, "fsl,disable-port-power-control")) |
| 309 | value &= ~(USB_CTRL0_PORTPWR_EN); |
| 310 | else |
| 311 | value |= USB_CTRL0_PORTPWR_EN; |
| 312 | |
| 313 | writel(value, base + USB_CTRL0); |
| 314 | |
| 315 | value = readl(base + USB_CTRL1); |
| 316 | if (dev_read_bool(dev, "fsl,over-current-active-low")) |
| 317 | value |= USB_CTRL1_OC_POLARITY; |
| 318 | else |
| 319 | value &= ~USB_CTRL1_OC_POLARITY; |
| 320 | |
| 321 | if (dev_read_bool(dev, "fsl,power-active-low")) |
| 322 | value |= USB_CTRL1_PWR_POLARITY; |
| 323 | else |
| 324 | value &= ~USB_CTRL1_PWR_POLARITY; |
| 325 | |
| 326 | writel(value, base + USB_CTRL1); |
| 327 | |
| 328 | unmap_physmem(base, MAP_NOCACHE); |
| 329 | } |
| 330 | |
| 331 | struct dwc3_glue_ops imx8mp_ops = { |
| 332 | .glue_configure = dwc3_imx8mp_glue_configure, |
| 333 | }; |
| 334 | |
Marek Vasut | 68c8656 | 2022-04-13 00:42:55 +0200 | [diff] [blame] | 335 | void dwc3_ti_glue_configure(struct udevice *dev, int index, |
Jean-Jacques Hiblot | 65596f1 | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 336 | enum usb_dr_mode mode) |
| 337 | { |
| 338 | #define USBOTGSS_UTMI_OTG_STATUS 0x0084 |
| 339 | #define USBOTGSS_UTMI_OTG_OFFSET 0x0480 |
| 340 | |
| 341 | /* UTMI_OTG_STATUS REGISTER */ |
| 342 | #define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31) |
| 343 | #define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9) |
| 344 | #define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8) |
| 345 | #define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4) |
| 346 | #define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3) |
| 347 | #define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2) |
| 348 | #define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1) |
| 349 | enum dwc3_omap_utmi_mode { |
| 350 | DWC3_OMAP_UTMI_MODE_UNKNOWN = 0, |
| 351 | DWC3_OMAP_UTMI_MODE_HW, |
| 352 | DWC3_OMAP_UTMI_MODE_SW, |
| 353 | }; |
| 354 | |
| 355 | u32 use_id_pin; |
| 356 | u32 host_mode; |
| 357 | u32 reg; |
| 358 | u32 utmi_mode; |
| 359 | u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS; |
| 360 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 361 | struct dwc3_glue_data *glue = dev_get_plat(dev); |
Jean-Jacques Hiblot | 65596f1 | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 362 | void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE); |
| 363 | |
| 364 | if (device_is_compatible(dev, "ti,am437x-dwc3")) |
| 365 | utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET; |
| 366 | |
| 367 | utmi_mode = dev_read_u32_default(dev, "utmi-mode", |
| 368 | DWC3_OMAP_UTMI_MODE_UNKNOWN); |
| 369 | if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) { |
| 370 | debug("%s: OTG is not supported. defaulting to PERIPHERAL\n", |
| 371 | dev->name); |
| 372 | mode = USB_DR_MODE_PERIPHERAL; |
| 373 | } |
| 374 | |
| 375 | switch (mode) { |
| 376 | case USB_DR_MODE_PERIPHERAL: |
| 377 | use_id_pin = 0; |
| 378 | host_mode = 0; |
| 379 | break; |
| 380 | case USB_DR_MODE_HOST: |
| 381 | use_id_pin = 0; |
| 382 | host_mode = 1; |
| 383 | break; |
| 384 | case USB_DR_MODE_OTG: |
| 385 | default: |
| 386 | use_id_pin = 1; |
| 387 | host_mode = 0; |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | reg = readl(base + utmi_status_offset); |
| 392 | |
| 393 | reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE); |
| 394 | if (!use_id_pin) |
| 395 | reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE; |
| 396 | |
| 397 | writel(reg, base + utmi_status_offset); |
| 398 | |
| 399 | reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND | |
| 400 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID | |
| 401 | USBOTGSS_UTMI_OTG_STATUS_IDDIG); |
| 402 | |
| 403 | reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID | |
| 404 | USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT; |
| 405 | |
| 406 | if (!host_mode) |
| 407 | reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG | |
| 408 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID; |
| 409 | |
| 410 | writel(reg, base + utmi_status_offset); |
| 411 | |
| 412 | unmap_physmem(base, MAP_NOCACHE); |
| 413 | } |
| 414 | |
| 415 | struct dwc3_glue_ops ti_ops = { |
Marek Vasut | 68c8656 | 2022-04-13 00:42:55 +0200 | [diff] [blame] | 416 | .glue_configure = dwc3_ti_glue_configure, |
Jean-Jacques Hiblot | 65596f1 | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 417 | }; |
| 418 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 419 | static int dwc3_glue_bind_common(struct udevice *parent, ofnode node) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 420 | { |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 421 | const char *name = ofnode_get_name(node); |
| 422 | const char *driver = NULL; |
Angus Ainslie | 6e382a8 | 2022-02-02 15:08:54 -0800 | [diff] [blame] | 423 | enum usb_dr_mode dr_mode; |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 424 | struct udevice *dev; |
| 425 | int ret; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 426 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 427 | debug("%s: subnode name: %s\n", __func__, name); |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 428 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 429 | /* if the parent node doesn't have a mode check the leaf */ |
| 430 | dr_mode = usb_get_dr_mode(dev_ofnode(parent)); |
| 431 | if (!dr_mode) |
| 432 | dr_mode = usb_get_dr_mode(node); |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 433 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 434 | switch (dr_mode) { |
| 435 | case USB_DR_MODE_PERIPHERAL: |
| 436 | case USB_DR_MODE_OTG: |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 437 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 438 | debug("%s: dr_mode: OTG or Peripheral\n", __func__); |
| 439 | driver = "dwc3-generic-peripheral"; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 440 | #endif |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 441 | break; |
Simon Glass | 1f2440c | 2021-07-10 21:14:29 -0600 | [diff] [blame] | 442 | #if defined(CONFIG_SPL_USB_HOST) || !defined(CONFIG_SPL_BUILD) |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 443 | case USB_DR_MODE_HOST: |
| 444 | debug("%s: dr_mode: HOST\n", __func__); |
| 445 | driver = "dwc3-generic-host"; |
| 446 | break; |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 447 | #endif |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 448 | default: |
| 449 | debug("%s: unsupported dr_mode\n", __func__); |
| 450 | return -ENODEV; |
| 451 | }; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 452 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 453 | if (!driver) |
| 454 | return -ENXIO; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 455 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame^] | 456 | ret = device_bind_driver_to_node(parent, driver, name, |
| 457 | node, &dev); |
| 458 | if (ret) { |
| 459 | debug("%s: not able to bind usb device mode\n", |
| 460 | __func__); |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int dwc3_glue_bind(struct udevice *parent) |
| 468 | { |
| 469 | struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent); |
| 470 | ofnode node; |
| 471 | int ret; |
| 472 | |
| 473 | if (ops && ops->glue_get_ctrl_dev) { |
| 474 | ret = ops->glue_get_ctrl_dev(parent, &node); |
| 475 | if (ret) |
| 476 | return ret; |
| 477 | |
| 478 | return dwc3_glue_bind_common(parent, node); |
| 479 | } |
| 480 | |
| 481 | ofnode_for_each_subnode(node, dev_ofnode(parent)) { |
| 482 | ret = dwc3_glue_bind_common(parent, node); |
| 483 | if (ret == -ENXIO) |
| 484 | continue; |
| 485 | if (ret) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 486 | return ret; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 487 | } |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int dwc3_glue_reset_init(struct udevice *dev, |
| 493 | struct dwc3_glue_data *glue) |
| 494 | { |
| 495 | int ret; |
| 496 | |
| 497 | ret = reset_get_bulk(dev, &glue->resets); |
Vignesh Raghavendra | e9310fc | 2019-10-25 13:48:05 +0530 | [diff] [blame] | 498 | if (ret == -ENOTSUPP || ret == -ENOENT) |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 499 | return 0; |
| 500 | else if (ret) |
| 501 | return ret; |
| 502 | |
| 503 | ret = reset_deassert_bulk(&glue->resets); |
| 504 | if (ret) { |
| 505 | reset_release_bulk(&glue->resets); |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int dwc3_glue_clk_init(struct udevice *dev, |
| 513 | struct dwc3_glue_data *glue) |
| 514 | { |
| 515 | int ret; |
| 516 | |
| 517 | ret = clk_get_bulk(dev, &glue->clks); |
Vignesh Raghavendra | e9310fc | 2019-10-25 13:48:05 +0530 | [diff] [blame] | 518 | if (ret == -ENOSYS || ret == -ENOENT) |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 519 | return 0; |
| 520 | if (ret) |
| 521 | return ret; |
| 522 | |
| 523 | #if CONFIG_IS_ENABLED(CLK) |
| 524 | ret = clk_enable_bulk(&glue->clks); |
| 525 | if (ret) { |
| 526 | clk_release_bulk(&glue->clks); |
| 527 | return ret; |
| 528 | } |
| 529 | #endif |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static int dwc3_glue_probe(struct udevice *dev) |
| 535 | { |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 536 | struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 537 | struct dwc3_glue_data *glue = dev_get_plat(dev); |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 538 | struct udevice *child = NULL; |
| 539 | int index = 0; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 540 | int ret; |
Michal Simek | bb19d62 | 2022-03-09 10:05:45 +0100 | [diff] [blame] | 541 | struct phy phy; |
| 542 | |
| 543 | ret = generic_phy_get_by_name(dev, "usb3-phy", &phy); |
| 544 | if (!ret) { |
| 545 | ret = generic_phy_init(&phy); |
| 546 | if (ret) |
| 547 | return ret; |
Jan Kiszka | 2fe2cf0 | 2022-04-25 13:26:45 +0200 | [diff] [blame] | 548 | } else if (ret != -ENOENT && ret != -ENODATA) { |
Michal Simek | bb19d62 | 2022-03-09 10:05:45 +0100 | [diff] [blame] | 549 | debug("could not get phy (err %d)\n", ret); |
| 550 | return ret; |
Jan Kiszka | 2fe2cf0 | 2022-04-25 13:26:45 +0200 | [diff] [blame] | 551 | } else { |
| 552 | phy.dev = NULL; |
Michal Simek | bb19d62 | 2022-03-09 10:05:45 +0100 | [diff] [blame] | 553 | } |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 554 | |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 555 | glue->regs = dev_read_addr(dev); |
| 556 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 557 | ret = dwc3_glue_clk_init(dev, glue); |
| 558 | if (ret) |
| 559 | return ret; |
| 560 | |
| 561 | ret = dwc3_glue_reset_init(dev, glue); |
| 562 | if (ret) |
| 563 | return ret; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 564 | |
Michal Simek | bb19d62 | 2022-03-09 10:05:45 +0100 | [diff] [blame] | 565 | if (phy.dev) { |
| 566 | ret = generic_phy_power_on(&phy); |
| 567 | if (ret) |
| 568 | return ret; |
| 569 | } |
| 570 | |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 571 | ret = device_find_first_child(dev, &child); |
| 572 | if (ret) |
| 573 | return ret; |
| 574 | |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 575 | if (glue->resets.count == 0) { |
| 576 | ret = dwc3_glue_reset_init(child, glue); |
| 577 | if (ret) |
| 578 | return ret; |
| 579 | } |
| 580 | |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 581 | while (child) { |
| 582 | enum usb_dr_mode dr_mode; |
| 583 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 584 | dr_mode = usb_get_dr_mode(dev_ofnode(child)); |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 585 | device_find_next_child(&child); |
Marek Vasut | 68c8656 | 2022-04-13 00:42:55 +0200 | [diff] [blame] | 586 | if (ops && ops->glue_configure) |
| 587 | ops->glue_configure(dev, index, dr_mode); |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 588 | index++; |
| 589 | } |
| 590 | |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 594 | static int dwc3_glue_remove(struct udevice *dev) |
| 595 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 596 | struct dwc3_glue_data *glue = dev_get_plat(dev); |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 597 | |
| 598 | reset_release_bulk(&glue->resets); |
| 599 | |
| 600 | clk_release_bulk(&glue->clks); |
| 601 | |
Jean-Jacques Hiblot | 5a94557 | 2019-07-05 09:33:56 +0200 | [diff] [blame] | 602 | return 0; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static const struct udevice_id dwc3_glue_ids[] = { |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 606 | { .compatible = "xlnx,zynqmp-dwc3" }, |
Siva Durga Prasad Paladugu | 1eb3c30 | 2020-05-12 08:36:01 +0200 | [diff] [blame] | 607 | { .compatible = "xlnx,versal-dwc3" }, |
Jean-Jacques Hiblot | 3e0684b | 2018-12-04 11:12:56 +0100 | [diff] [blame] | 608 | { .compatible = "ti,keystone-dwc3"}, |
Jean-Jacques Hiblot | 65596f1 | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 609 | { .compatible = "ti,dwc3", .data = (ulong)&ti_ops }, |
Jean-Jacques Hiblot | ca848df | 2018-12-04 11:30:50 +0100 | [diff] [blame] | 610 | { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops }, |
Vignesh Raghavendra | c628295 | 2019-12-09 10:37:29 +0530 | [diff] [blame] | 611 | { .compatible = "ti,am654-dwc3" }, |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 612 | { .compatible = "rockchip,rk3328-dwc3" }, |
| 613 | { .compatible = "rockchip,rk3399-dwc3" }, |
Robert Marko | 746862b | 2020-09-10 16:00:05 +0200 | [diff] [blame] | 614 | { .compatible = "qcom,dwc3" }, |
Marek Vasut | ae21934 | 2022-04-13 00:42:56 +0200 | [diff] [blame] | 615 | { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops }, |
Angus Ainslie | 6e382a8 | 2022-02-02 15:08:54 -0800 | [diff] [blame] | 616 | { .compatible = "fsl,imx8mq-dwc3" }, |
Andy Shevchenko | 221d7fa | 2020-12-03 19:45:01 +0200 | [diff] [blame] | 617 | { .compatible = "intel,tangier-dwc3" }, |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 618 | { } |
| 619 | }; |
| 620 | |
| 621 | U_BOOT_DRIVER(dwc3_generic_wrapper) = { |
| 622 | .name = "dwc3-generic-wrapper", |
Jean-Jacques Hiblot | b49b5c2 | 2019-07-05 09:33:58 +0200 | [diff] [blame] | 623 | .id = UCLASS_NOP, |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 624 | .of_match = dwc3_glue_ids, |
| 625 | .bind = dwc3_glue_bind, |
| 626 | .probe = dwc3_glue_probe, |
| 627 | .remove = dwc3_glue_remove, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 628 | .plat_auto = sizeof(struct dwc3_glue_data), |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 629 | |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 630 | }; |