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 | |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 10 | #include <dm.h> |
Patrice Chotard | 94be152 | 2025-01-30 17:35:42 +0100 | [diff] [blame] | 11 | #include <reset.h> |
| 12 | #include <asm/gpio.h> |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 13 | #include <dm/lists.h> |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 14 | #include <linux/delay.h> |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 15 | #include <linux/usb/gadget.h> |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 16 | #include <power/regulator.h> |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 17 | #include <usb/xhci.h> |
Patrice Chotard | 94be152 | 2025-01-30 17:35:42 +0100 | [diff] [blame] | 18 | #include "core.h" |
Kunihiko Hayashi | 6cf357e | 2023-02-20 14:50:28 +0900 | [diff] [blame] | 19 | #include "dwc3-generic.h" |
Patrice Chotard | 94be152 | 2025-01-30 17:35:42 +0100 | [diff] [blame] | 20 | #include "gadget.h" |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 21 | |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 22 | struct dwc3_generic_plat { |
| 23 | fdt_addr_t base; |
| 24 | u32 maximum_speed; |
| 25 | enum usb_dr_mode dr_mode; |
| 26 | }; |
| 27 | |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 28 | struct dwc3_generic_priv { |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 29 | void *base; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 30 | struct dwc3 dwc3; |
developer | f8bced1 | 2020-05-02 11:35:13 +0200 | [diff] [blame] | 31 | struct phy_bulk phys; |
Venkatesh Yadav Abbarapu | 5f70b0a | 2023-01-13 10:42:02 +0530 | [diff] [blame] | 32 | struct gpio_desc *ulpi_reset; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 33 | }; |
| 34 | |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 35 | struct dwc3_generic_host_priv { |
| 36 | struct xhci_ctrl xhci_ctrl; |
| 37 | struct dwc3_generic_priv gen_priv; |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 38 | struct udevice *vbus_supply; |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 41 | static int dwc3_generic_probe(struct udevice *dev, |
Siddharth Vadapalli | b9ae082e | 2024-11-26 17:33:18 +0530 | [diff] [blame] | 42 | struct dwc3_generic_priv *priv, |
| 43 | enum usb_dr_mode mode) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 44 | { |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 45 | int rc; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 46 | struct dwc3_generic_plat *plat = dev_get_plat(dev); |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 47 | struct dwc3 *dwc3 = &priv->dwc3; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 48 | struct dwc3_glue_data *glue = dev_get_plat(dev->parent); |
Marek Vasut | aacbcb6 | 2022-11-27 15:31:52 +0100 | [diff] [blame] | 49 | int __maybe_unused index; |
| 50 | ofnode __maybe_unused node; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 51 | |
Jean-Jacques Hiblot | ce868d0 | 2019-09-11 11:33:52 +0200 | [diff] [blame] | 52 | dwc3->dev = dev; |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 53 | dwc3->maximum_speed = plat->maximum_speed; |
Siddharth Vadapalli | b9ae082e | 2024-11-26 17:33:18 +0530 | [diff] [blame] | 54 | dwc3->dr_mode = mode; |
Jean-Jacques Hiblot | ce868d0 | 2019-09-11 11:33:52 +0200 | [diff] [blame] | 55 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 56 | dwc3_of_parse(dwc3); |
Marek Vasut | aacbcb6 | 2022-11-27 15:31:52 +0100 | [diff] [blame] | 57 | |
Marek Vasut | 4d28572 | 2023-02-20 14:50:25 +0900 | [diff] [blame] | 58 | /* |
| 59 | * There are currently four disparate placement possibilities of DWC3 |
| 60 | * reference clock phandle in SoC DTs: |
| 61 | * - in top level glue node, with generic subnode without clock (ZynqMP) |
| 62 | * - in top level generic node, with no subnode (i.MX8MQ) |
| 63 | * - in generic subnode, with other clock in top level node (i.MX8MP) |
| 64 | * - in both top level node and generic subnode (Rockchip) |
| 65 | * Cover all the possibilities here by looking into both nodes, start |
| 66 | * with the top level node as that seems to be used in majority of DTs |
| 67 | * to reference the clock. |
| 68 | */ |
Marek Vasut | aacbcb6 | 2022-11-27 15:31:52 +0100 | [diff] [blame] | 69 | node = dev_ofnode(dev->parent); |
| 70 | index = ofnode_stringlist_search(node, "clock-names", "ref"); |
| 71 | if (index < 0) |
| 72 | index = ofnode_stringlist_search(node, "clock-names", "ref_clk"); |
Marek Vasut | 4d28572 | 2023-02-20 14:50:25 +0900 | [diff] [blame] | 73 | if (index < 0) { |
| 74 | node = dev_ofnode(dev); |
| 75 | index = ofnode_stringlist_search(node, "clock-names", "ref"); |
| 76 | if (index < 0) |
| 77 | index = ofnode_stringlist_search(node, "clock-names", "ref_clk"); |
| 78 | } |
Marek Vasut | aacbcb6 | 2022-11-27 15:31:52 +0100 | [diff] [blame] | 79 | if (index >= 0) |
| 80 | dwc3->ref_clk = &glue->clks.clks[index]; |
Jean-Jacques Hiblot | ce868d0 | 2019-09-11 11:33:52 +0200 | [diff] [blame] | 81 | #endif |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 82 | |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 83 | /* |
| 84 | * It must hold whole USB3.0 OTG controller in resetting to hold pipe |
| 85 | * power state in P2 before initializing TypeC PHY on RK3399 platform. |
| 86 | */ |
| 87 | if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) { |
| 88 | reset_assert_bulk(&glue->resets); |
| 89 | udelay(1); |
| 90 | } |
| 91 | |
developer | f8bced1 | 2020-05-02 11:35:13 +0200 | [diff] [blame] | 92 | rc = dwc3_setup_phy(dev, &priv->phys); |
Siva Durga Prasad Paladugu | c37f8f3 | 2020-10-21 14:17:31 +0200 | [diff] [blame] | 93 | if (rc && rc != -ENOTSUPP) |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 94 | return rc; |
| 95 | |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 96 | if (CONFIG_IS_ENABLED(DM_GPIO) && |
| 97 | device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) { |
Venkatesh Yadav Abbarapu | 5f70b0a | 2023-01-13 10:42:02 +0530 | [diff] [blame] | 98 | priv->ulpi_reset = devm_gpiod_get_optional(dev->parent, "reset", |
Peter Korsgaard | 686a0f0 | 2023-06-28 14:26:48 +0200 | [diff] [blame] | 99 | GPIOD_IS_OUT | GPIOD_ACTIVE_LOW); |
Venkatesh Yadav Abbarapu | 5f70b0a | 2023-01-13 10:42:02 +0530 | [diff] [blame] | 100 | /* property is optional, don't return error! */ |
| 101 | if (priv->ulpi_reset) { |
| 102 | /* Toggle ulpi to reset the phy. */ |
| 103 | rc = dm_gpio_set_value(priv->ulpi_reset, 1); |
| 104 | if (rc) |
| 105 | return rc; |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 106 | |
Venkatesh Yadav Abbarapu | 5f70b0a | 2023-01-13 10:42:02 +0530 | [diff] [blame] | 107 | mdelay(5); |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 108 | |
Venkatesh Yadav Abbarapu | 5f70b0a | 2023-01-13 10:42:02 +0530 | [diff] [blame] | 109 | rc = dm_gpio_set_value(priv->ulpi_reset, 0); |
| 110 | if (rc) |
| 111 | return rc; |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 112 | |
Venkatesh Yadav Abbarapu | 5f70b0a | 2023-01-13 10:42:02 +0530 | [diff] [blame] | 113 | mdelay(5); |
| 114 | } |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 117 | if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) |
| 118 | reset_deassert_bulk(&glue->resets); |
| 119 | |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 120 | priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE); |
| 121 | dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START; |
Jean-Jacques Hiblot | ce868d0 | 2019-09-11 11:33:52 +0200 | [diff] [blame] | 122 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 123 | rc = dwc3_init(dwc3); |
| 124 | if (rc) { |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 125 | unmap_physmem(priv->base, MAP_NOCACHE); |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 126 | return rc; |
| 127 | } |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 128 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 129 | return 0; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 132 | static int dwc3_generic_remove(struct udevice *dev, |
| 133 | struct dwc3_generic_priv *priv) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 134 | { |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 135 | struct dwc3 *dwc3 = &priv->dwc3; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 136 | |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 137 | if (CONFIG_IS_ENABLED(DM_GPIO) && |
Venkatesh Yadav Abbarapu | 63d5d86 | 2023-08-09 09:03:50 +0530 | [diff] [blame] | 138 | device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3") && |
| 139 | priv->ulpi_reset) { |
Venkatesh Yadav Abbarapu | 5f70b0a | 2023-01-13 10:42:02 +0530 | [diff] [blame] | 140 | struct gpio_desc *ulpi_reset = priv->ulpi_reset; |
T Karthik Reddy | f7adf89 | 2022-07-08 11:21:59 +0200 | [diff] [blame] | 141 | |
| 142 | dm_gpio_free(ulpi_reset->dev, ulpi_reset); |
| 143 | } |
| 144 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 145 | dwc3_remove(dwc3); |
developer | f8bced1 | 2020-05-02 11:35:13 +0200 | [diff] [blame] | 146 | dwc3_shutdown_phy(dev, &priv->phys); |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 147 | unmap_physmem(dwc3->regs, MAP_NOCACHE); |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 152 | static int dwc3_generic_of_to_plat(struct udevice *dev) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 153 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 154 | struct dwc3_generic_plat *plat = dev_get_plat(dev); |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 155 | ofnode node = dev_ofnode(dev); |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 156 | |
Angus Ainslie | 6e382a8 | 2022-02-02 15:08:54 -0800 | [diff] [blame] | 157 | if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) { |
| 158 | /* This is a leaf so check the parent */ |
| 159 | plat->base = dev_read_addr(dev->parent); |
| 160 | } else { |
| 161 | plat->base = dev_read_addr(dev); |
| 162 | } |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 163 | |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 164 | plat->maximum_speed = usb_get_maximum_speed(node); |
| 165 | if (plat->maximum_speed == USB_SPEED_UNKNOWN) { |
Jean-Jacques Hiblot | 547df0d | 2019-09-11 11:33:51 +0200 | [diff] [blame] | 166 | pr_info("No USB maximum speed specified. Using super speed\n"); |
| 167 | plat->maximum_speed = USB_SPEED_SUPER; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Jean-Jacques Hiblot | a33aa76 | 2019-09-11 11:33:48 +0200 | [diff] [blame] | 170 | plat->dr_mode = usb_get_dr_mode(node); |
| 171 | if (plat->dr_mode == USB_DR_MODE_UNKNOWN) { |
Angus Ainslie | 6e382a8 | 2022-02-02 15:08:54 -0800 | [diff] [blame] | 172 | /* might be a leaf so check the parent for mode */ |
| 173 | node = dev_ofnode(dev->parent); |
| 174 | plat->dr_mode = usb_get_dr_mode(node); |
| 175 | if (plat->dr_mode == USB_DR_MODE_UNKNOWN) { |
| 176 | pr_err("Invalid usb mode setup\n"); |
| 177 | return -ENODEV; |
| 178 | } |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 184 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
Marek Vasut | eab470b | 2024-06-14 02:51:19 +0200 | [diff] [blame] | 185 | static int dwc3_generic_peripheral_probe(struct udevice *dev) |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 186 | { |
| 187 | struct dwc3_generic_priv *priv = dev_get_priv(dev); |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 188 | |
Siddharth Vadapalli | b9ae082e | 2024-11-26 17:33:18 +0530 | [diff] [blame] | 189 | return dwc3_generic_probe(dev, priv, USB_DR_MODE_PERIPHERAL); |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Marek Vasut | eab470b | 2024-06-14 02:51:19 +0200 | [diff] [blame] | 192 | static int dwc3_generic_peripheral_remove(struct udevice *dev) |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 193 | { |
| 194 | struct dwc3_generic_priv *priv = dev_get_priv(dev); |
| 195 | |
Marek Vasut | eab470b | 2024-06-14 02:51:19 +0200 | [diff] [blame] | 196 | return dwc3_generic_remove(dev, priv); |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Marek Vasut | eab470b | 2024-06-14 02:51:19 +0200 | [diff] [blame] | 199 | static int dwc3_gadget_handle_interrupts(struct udevice *dev) |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 200 | { |
| 201 | struct dwc3_generic_priv *priv = dev_get_priv(dev); |
Marek Vasut | eab470b | 2024-06-14 02:51:19 +0200 | [diff] [blame] | 202 | struct dwc3 *dwc3 = &priv->dwc3; |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 203 | |
Marek Vasut | eab470b | 2024-06-14 02:51:19 +0200 | [diff] [blame] | 204 | dwc3_gadget_uboot_handle_interrupt(dwc3); |
| 205 | |
| 206 | return 0; |
Jean-Jacques Hiblot | 2bf2c35 | 2019-09-11 11:33:49 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Marek Vasut | eab470b | 2024-06-14 02:51:19 +0200 | [diff] [blame] | 209 | static const struct usb_gadget_generic_ops dwc3_gadget_ops = { |
| 210 | .handle_interrupts = dwc3_gadget_handle_interrupts, |
| 211 | }; |
| 212 | |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 213 | U_BOOT_DRIVER(dwc3_generic_peripheral) = { |
| 214 | .name = "dwc3-generic-peripheral", |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 215 | .id = UCLASS_USB_GADGET_GENERIC, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 216 | .of_to_plat = dwc3_generic_of_to_plat, |
Marek Vasut | eab470b | 2024-06-14 02:51:19 +0200 | [diff] [blame] | 217 | .ops = &dwc3_gadget_ops, |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 218 | .probe = dwc3_generic_peripheral_probe, |
| 219 | .remove = dwc3_generic_peripheral_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 220 | .priv_auto = sizeof(struct dwc3_generic_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 221 | .plat_auto = sizeof(struct dwc3_generic_plat), |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 222 | }; |
Jean-Jacques Hiblot | 44aaec7 | 2018-11-29 10:52:42 +0100 | [diff] [blame] | 223 | #endif |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 224 | |
Jonas Karlman | 5e8b61b | 2023-07-30 22:59:56 +0000 | [diff] [blame] | 225 | #if CONFIG_IS_ENABLED(USB_HOST) |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 226 | static int dwc3_generic_host_probe(struct udevice *dev) |
| 227 | { |
| 228 | struct xhci_hcor *hcor; |
| 229 | struct xhci_hccr *hccr; |
| 230 | struct dwc3_generic_host_priv *priv = dev_get_priv(dev); |
| 231 | int rc; |
| 232 | |
Siddharth Vadapalli | b9ae082e | 2024-11-26 17:33:18 +0530 | [diff] [blame] | 233 | rc = dwc3_generic_probe(dev, &priv->gen_priv, USB_DR_MODE_HOST); |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 234 | if (rc) |
| 235 | return rc; |
| 236 | |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 237 | rc = device_get_supply_regulator(dev, "vbus-supply", &priv->vbus_supply); |
Jan Kiszka | edc1e75 | 2024-08-08 10:51:33 +0200 | [diff] [blame] | 238 | if (rc && rc != -ENOSYS) |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 239 | debug("%s: No vbus regulator found: %d\n", dev->name, rc); |
| 240 | |
Jan Kiszka | edc1e75 | 2024-08-08 10:51:33 +0200 | [diff] [blame] | 241 | /* Does not return an error if regulator is invalid - but does so when DM_REGULATOR is disabled */ |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 242 | rc = regulator_set_enable_if_allowed(priv->vbus_supply, true); |
Jan Kiszka | edc1e75 | 2024-08-08 10:51:33 +0200 | [diff] [blame] | 243 | if (rc && rc != -ENOSYS) |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 244 | return rc; |
| 245 | |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 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 | |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 250 | rc = xhci_register(dev, hccr, hcor); |
| 251 | if (rc) |
| 252 | regulator_set_enable_if_allowed(priv->vbus_supply, false); |
| 253 | |
| 254 | return rc; |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static int dwc3_generic_host_remove(struct udevice *dev) |
| 258 | { |
| 259 | struct dwc3_generic_host_priv *priv = dev_get_priv(dev); |
| 260 | int rc; |
| 261 | |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 262 | /* This function always returns 0 */ |
| 263 | xhci_deregister(dev); |
| 264 | |
| 265 | rc = regulator_set_enable_if_allowed(priv->vbus_supply, false); |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 266 | if (rc) |
Caleb Connolly | c52bc90 | 2024-02-26 17:26:06 +0000 | [diff] [blame] | 267 | debug("%s: Failed to disable vbus regulator: %d\n", dev->name, rc); |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 268 | |
| 269 | return dwc3_generic_remove(dev, &priv->gen_priv); |
| 270 | } |
| 271 | |
| 272 | U_BOOT_DRIVER(dwc3_generic_host) = { |
| 273 | .name = "dwc3-generic-host", |
| 274 | .id = UCLASS_USB, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 275 | .of_to_plat = dwc3_generic_of_to_plat, |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 276 | .probe = dwc3_generic_host_probe, |
| 277 | .remove = dwc3_generic_host_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 278 | .priv_auto = sizeof(struct dwc3_generic_host_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 279 | .plat_auto = sizeof(struct dwc3_generic_plat), |
Jean-Jacques Hiblot | 175cd7c | 2019-09-11 11:33:50 +0200 | [diff] [blame] | 280 | .ops = &xhci_usb_ops, |
| 281 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 282 | }; |
| 283 | #endif |
| 284 | |
Marek Vasut | ae21934 | 2022-04-13 00:42:56 +0200 | [diff] [blame] | 285 | void dwc3_imx8mp_glue_configure(struct udevice *dev, int index, |
| 286 | enum usb_dr_mode mode) |
| 287 | { |
| 288 | /* USB glue registers */ |
| 289 | #define USB_CTRL0 0x00 |
| 290 | #define USB_CTRL1 0x04 |
| 291 | |
| 292 | #define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */ |
| 293 | #define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */ |
| 294 | #define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */ |
| 295 | |
| 296 | #define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */ |
| 297 | #define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */ |
| 298 | fdt_addr_t regs = dev_read_addr_index(dev, 1); |
| 299 | void *base = map_physmem(regs, 0x8, MAP_NOCACHE); |
| 300 | u32 value; |
| 301 | |
| 302 | value = readl(base + USB_CTRL0); |
| 303 | |
| 304 | if (dev_read_bool(dev, "fsl,permanently-attached")) |
| 305 | value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED); |
| 306 | else |
| 307 | value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED); |
| 308 | |
| 309 | if (dev_read_bool(dev, "fsl,disable-port-power-control")) |
| 310 | value &= ~(USB_CTRL0_PORTPWR_EN); |
| 311 | else |
| 312 | value |= USB_CTRL0_PORTPWR_EN; |
| 313 | |
| 314 | writel(value, base + USB_CTRL0); |
| 315 | |
| 316 | value = readl(base + USB_CTRL1); |
| 317 | if (dev_read_bool(dev, "fsl,over-current-active-low")) |
| 318 | value |= USB_CTRL1_OC_POLARITY; |
| 319 | else |
| 320 | value &= ~USB_CTRL1_OC_POLARITY; |
| 321 | |
| 322 | if (dev_read_bool(dev, "fsl,power-active-low")) |
| 323 | value |= USB_CTRL1_PWR_POLARITY; |
| 324 | else |
| 325 | value &= ~USB_CTRL1_PWR_POLARITY; |
| 326 | |
| 327 | writel(value, base + USB_CTRL1); |
| 328 | |
| 329 | unmap_physmem(base, MAP_NOCACHE); |
| 330 | } |
| 331 | |
| 332 | struct dwc3_glue_ops imx8mp_ops = { |
| 333 | .glue_configure = dwc3_imx8mp_glue_configure, |
| 334 | }; |
| 335 | |
Marek Vasut | 68c8656 | 2022-04-13 00:42:55 +0200 | [diff] [blame] | 336 | void dwc3_ti_glue_configure(struct udevice *dev, int index, |
Jean-Jacques Hiblot | 65596f1 | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 337 | enum usb_dr_mode mode) |
| 338 | { |
| 339 | #define USBOTGSS_UTMI_OTG_STATUS 0x0084 |
| 340 | #define USBOTGSS_UTMI_OTG_OFFSET 0x0480 |
| 341 | |
| 342 | /* UTMI_OTG_STATUS REGISTER */ |
| 343 | #define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31) |
| 344 | #define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9) |
| 345 | #define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8) |
| 346 | #define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4) |
| 347 | #define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3) |
| 348 | #define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2) |
| 349 | #define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1) |
| 350 | enum dwc3_omap_utmi_mode { |
| 351 | DWC3_OMAP_UTMI_MODE_UNKNOWN = 0, |
| 352 | DWC3_OMAP_UTMI_MODE_HW, |
| 353 | DWC3_OMAP_UTMI_MODE_SW, |
| 354 | }; |
| 355 | |
| 356 | u32 use_id_pin; |
| 357 | u32 host_mode; |
| 358 | u32 reg; |
| 359 | u32 utmi_mode; |
| 360 | u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS; |
| 361 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 362 | struct dwc3_glue_data *glue = dev_get_plat(dev); |
Jean-Jacques Hiblot | 65596f1 | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 363 | void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE); |
| 364 | |
| 365 | if (device_is_compatible(dev, "ti,am437x-dwc3")) |
| 366 | utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET; |
| 367 | |
| 368 | utmi_mode = dev_read_u32_default(dev, "utmi-mode", |
| 369 | DWC3_OMAP_UTMI_MODE_UNKNOWN); |
| 370 | if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) { |
| 371 | debug("%s: OTG is not supported. defaulting to PERIPHERAL\n", |
| 372 | dev->name); |
| 373 | mode = USB_DR_MODE_PERIPHERAL; |
| 374 | } |
| 375 | |
| 376 | switch (mode) { |
| 377 | case USB_DR_MODE_PERIPHERAL: |
| 378 | use_id_pin = 0; |
| 379 | host_mode = 0; |
| 380 | break; |
| 381 | case USB_DR_MODE_HOST: |
| 382 | use_id_pin = 0; |
| 383 | host_mode = 1; |
| 384 | break; |
| 385 | case USB_DR_MODE_OTG: |
| 386 | default: |
| 387 | use_id_pin = 1; |
| 388 | host_mode = 0; |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | reg = readl(base + utmi_status_offset); |
| 393 | |
| 394 | reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE); |
| 395 | if (!use_id_pin) |
| 396 | reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE; |
| 397 | |
| 398 | writel(reg, base + utmi_status_offset); |
| 399 | |
| 400 | reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND | |
| 401 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID | |
| 402 | USBOTGSS_UTMI_OTG_STATUS_IDDIG); |
| 403 | |
| 404 | reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID | |
| 405 | USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT; |
| 406 | |
| 407 | if (!host_mode) |
| 408 | reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG | |
| 409 | USBOTGSS_UTMI_OTG_STATUS_VBUSVALID; |
| 410 | |
| 411 | writel(reg, base + utmi_status_offset); |
| 412 | |
| 413 | unmap_physmem(base, MAP_NOCACHE); |
| 414 | } |
| 415 | |
| 416 | struct dwc3_glue_ops ti_ops = { |
Marek Vasut | 68c8656 | 2022-04-13 00:42:55 +0200 | [diff] [blame] | 417 | .glue_configure = dwc3_ti_glue_configure, |
Jean-Jacques Hiblot | 65596f1 | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 418 | }; |
| 419 | |
Caleb Connolly | 60d1fc2 | 2024-03-20 14:30:47 +0000 | [diff] [blame] | 420 | /* USB QSCRATCH Hardware registers */ |
| 421 | #define QSCRATCH_GENERAL_CFG 0x08 |
| 422 | #define PIPE_UTMI_CLK_SEL BIT(0) |
| 423 | #define PIPE3_PHYSTATUS_SW BIT(3) |
| 424 | #define PIPE_UTMI_CLK_DIS BIT(8) |
| 425 | |
| 426 | #define QSCRATCH_HS_PHY_CTRL 0x10 |
| 427 | #define UTMI_OTG_VBUS_VALID BIT(20) |
| 428 | #define SW_SESSVLD_SEL BIT(28) |
| 429 | |
| 430 | #define QSCRATCH_SS_PHY_CTRL 0x30 |
| 431 | #define LANE0_PWR_PRESENT BIT(24) |
| 432 | |
| 433 | #define PWR_EVNT_IRQ_STAT_REG 0x58 |
| 434 | #define PWR_EVNT_LPM_IN_L2_MASK BIT(4) |
| 435 | #define PWR_EVNT_LPM_OUT_L2_MASK BIT(5) |
| 436 | |
| 437 | #define SDM845_QSCRATCH_BASE_OFFSET 0xf8800 |
| 438 | #define SDM845_QSCRATCH_SIZE 0x400 |
| 439 | #define SDM845_DWC3_CORE_SIZE 0xcd00 |
| 440 | |
| 441 | static void dwc3_qcom_vbus_override_enable(void __iomem *qscratch_base, bool enable) |
| 442 | { |
| 443 | if (enable) { |
| 444 | setbits_le32(qscratch_base + QSCRATCH_SS_PHY_CTRL, |
| 445 | LANE0_PWR_PRESENT); |
| 446 | setbits_le32(qscratch_base + QSCRATCH_HS_PHY_CTRL, |
| 447 | UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL); |
| 448 | } else { |
| 449 | clrbits_le32(qscratch_base + QSCRATCH_SS_PHY_CTRL, |
| 450 | LANE0_PWR_PRESENT); |
| 451 | clrbits_le32(qscratch_base + QSCRATCH_HS_PHY_CTRL, |
| 452 | UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* For controllers running without superspeed PHYs */ |
| 457 | static void dwc3_qcom_select_utmi_clk(void __iomem *qscratch_base) |
| 458 | { |
| 459 | /* Configure dwc3 to use UTMI clock as PIPE clock not present */ |
| 460 | setbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG, |
| 461 | PIPE_UTMI_CLK_DIS); |
| 462 | |
| 463 | setbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG, |
| 464 | PIPE_UTMI_CLK_SEL | PIPE3_PHYSTATUS_SW); |
| 465 | |
| 466 | clrbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG, |
| 467 | PIPE_UTMI_CLK_DIS); |
| 468 | } |
| 469 | |
| 470 | static void dwc3_qcom_glue_configure(struct udevice *dev, int index, |
| 471 | enum usb_dr_mode mode) |
| 472 | { |
| 473 | struct dwc3_glue_data *glue = dev_get_plat(dev); |
| 474 | void __iomem *qscratch_base = map_physmem(glue->regs, 0x400, MAP_NOCACHE); |
| 475 | if (IS_ERR_OR_NULL(qscratch_base)) { |
| 476 | log_err("%s: Invalid qscratch base address\n", dev->name); |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | if (dev_read_bool(dev, "qcom,select-utmi-as-pipe-clk")) |
| 481 | dwc3_qcom_select_utmi_clk(qscratch_base); |
| 482 | |
| 483 | if (mode != USB_DR_MODE_HOST) |
| 484 | dwc3_qcom_vbus_override_enable(qscratch_base, true); |
| 485 | } |
| 486 | |
| 487 | struct dwc3_glue_ops qcom_ops = { |
| 488 | .glue_configure = dwc3_qcom_glue_configure, |
| 489 | }; |
| 490 | |
Jonas Karlman | 04c6ae8 | 2023-07-30 22:59:57 +0000 | [diff] [blame] | 491 | static int dwc3_rk_glue_get_ctrl_dev(struct udevice *dev, ofnode *node) |
| 492 | { |
| 493 | *node = dev_ofnode(dev); |
| 494 | if (!ofnode_valid(*node)) |
| 495 | return -EINVAL; |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | struct dwc3_glue_ops rk_ops = { |
| 501 | .glue_get_ctrl_dev = dwc3_rk_glue_get_ctrl_dev, |
| 502 | }; |
| 503 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 504 | static int dwc3_glue_bind_common(struct udevice *parent, ofnode node) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 505 | { |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 506 | const char *name = ofnode_get_name(node); |
Jonas Karlman | 5e8b61b | 2023-07-30 22:59:56 +0000 | [diff] [blame] | 507 | const char *driver; |
Angus Ainslie | 6e382a8 | 2022-02-02 15:08:54 -0800 | [diff] [blame] | 508 | enum usb_dr_mode dr_mode; |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 509 | struct udevice *dev; |
| 510 | int ret; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 511 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 512 | debug("%s: subnode name: %s\n", __func__, name); |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 513 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 514 | /* if the parent node doesn't have a mode check the leaf */ |
| 515 | dr_mode = usb_get_dr_mode(dev_ofnode(parent)); |
| 516 | if (!dr_mode) |
| 517 | dr_mode = usb_get_dr_mode(node); |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 518 | |
Jonas Karlman | 5e8b61b | 2023-07-30 22:59:56 +0000 | [diff] [blame] | 519 | if (CONFIG_IS_ENABLED(DM_USB_GADGET) && |
| 520 | (dr_mode == USB_DR_MODE_PERIPHERAL || dr_mode == USB_DR_MODE_OTG)) { |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 521 | debug("%s: dr_mode: OTG or Peripheral\n", __func__); |
| 522 | driver = "dwc3-generic-peripheral"; |
Jonas Karlman | 5e8b61b | 2023-07-30 22:59:56 +0000 | [diff] [blame] | 523 | } else if (CONFIG_IS_ENABLED(USB_HOST) && dr_mode == USB_DR_MODE_HOST) { |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 524 | debug("%s: dr_mode: HOST\n", __func__); |
| 525 | driver = "dwc3-generic-host"; |
Jonas Karlman | 5e8b61b | 2023-07-30 22:59:56 +0000 | [diff] [blame] | 526 | } else { |
| 527 | debug("%s: unsupported dr_mode %d\n", __func__, dr_mode); |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 528 | return -ENODEV; |
Jonas Karlman | 5e8b61b | 2023-07-30 22:59:56 +0000 | [diff] [blame] | 529 | } |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 530 | |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 531 | ret = device_bind_driver_to_node(parent, driver, name, |
| 532 | node, &dev); |
| 533 | if (ret) { |
| 534 | debug("%s: not able to bind usb device mode\n", |
| 535 | __func__); |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Kunihiko Hayashi | 6cf357e | 2023-02-20 14:50:28 +0900 | [diff] [blame] | 542 | int dwc3_glue_bind(struct udevice *parent) |
Kunihiko Hayashi | 8c42037 | 2023-02-20 14:50:26 +0900 | [diff] [blame] | 543 | { |
| 544 | struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent); |
| 545 | ofnode node; |
| 546 | int ret; |
| 547 | |
| 548 | if (ops && ops->glue_get_ctrl_dev) { |
| 549 | ret = ops->glue_get_ctrl_dev(parent, &node); |
| 550 | if (ret) |
| 551 | return ret; |
| 552 | |
| 553 | return dwc3_glue_bind_common(parent, node); |
| 554 | } |
| 555 | |
| 556 | ofnode_for_each_subnode(node, dev_ofnode(parent)) { |
| 557 | ret = dwc3_glue_bind_common(parent, node); |
| 558 | if (ret == -ENXIO) |
| 559 | continue; |
| 560 | if (ret) |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 561 | return ret; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 562 | } |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int dwc3_glue_reset_init(struct udevice *dev, |
| 568 | struct dwc3_glue_data *glue) |
| 569 | { |
| 570 | int ret; |
| 571 | |
| 572 | ret = reset_get_bulk(dev, &glue->resets); |
Vignesh Raghavendra | e9310fc | 2019-10-25 13:48:05 +0530 | [diff] [blame] | 573 | if (ret == -ENOTSUPP || ret == -ENOENT) |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 574 | return 0; |
| 575 | else if (ret) |
| 576 | return ret; |
| 577 | |
Caleb Connolly | 60d1fc2 | 2024-03-20 14:30:47 +0000 | [diff] [blame] | 578 | if (device_is_compatible(dev, "qcom,dwc3")) { |
| 579 | reset_assert_bulk(&glue->resets); |
| 580 | /* We should wait at least 6 sleep clock cycles, that's |
| 581 | * (6 / 32764) * 1000000 ~= 200us. But some platforms |
| 582 | * have slower sleep clocks so we'll play it safe. |
| 583 | */ |
| 584 | udelay(500); |
| 585 | } |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 586 | ret = reset_deassert_bulk(&glue->resets); |
| 587 | if (ret) { |
| 588 | reset_release_bulk(&glue->resets); |
| 589 | return ret; |
| 590 | } |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static int dwc3_glue_clk_init(struct udevice *dev, |
| 596 | struct dwc3_glue_data *glue) |
| 597 | { |
| 598 | int ret; |
| 599 | |
| 600 | ret = clk_get_bulk(dev, &glue->clks); |
Vignesh Raghavendra | e9310fc | 2019-10-25 13:48:05 +0530 | [diff] [blame] | 601 | if (ret == -ENOSYS || ret == -ENOENT) |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 602 | return 0; |
| 603 | if (ret) |
| 604 | return ret; |
| 605 | |
| 606 | #if CONFIG_IS_ENABLED(CLK) |
| 607 | ret = clk_enable_bulk(&glue->clks); |
| 608 | if (ret) { |
| 609 | clk_release_bulk(&glue->clks); |
| 610 | return ret; |
| 611 | } |
| 612 | #endif |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
Kunihiko Hayashi | 6cf357e | 2023-02-20 14:50:28 +0900 | [diff] [blame] | 617 | int dwc3_glue_probe(struct udevice *dev) |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 618 | { |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 619 | 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] | 620 | struct dwc3_glue_data *glue = dev_get_plat(dev); |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 621 | struct udevice *child = NULL; |
| 622 | int index = 0; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 623 | int ret; |
Michal Simek | bb19d62 | 2022-03-09 10:05:45 +0100 | [diff] [blame] | 624 | struct phy phy; |
| 625 | |
| 626 | ret = generic_phy_get_by_name(dev, "usb3-phy", &phy); |
| 627 | if (!ret) { |
| 628 | ret = generic_phy_init(&phy); |
| 629 | if (ret) |
| 630 | return ret; |
Jan Kiszka | 2fe2cf0 | 2022-04-25 13:26:45 +0200 | [diff] [blame] | 631 | } else if (ret != -ENOENT && ret != -ENODATA) { |
Michal Simek | bb19d62 | 2022-03-09 10:05:45 +0100 | [diff] [blame] | 632 | debug("could not get phy (err %d)\n", ret); |
| 633 | return ret; |
| 634 | } |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 635 | |
Kunihiko Hayashi | 54c277e | 2023-02-20 14:50:29 +0900 | [diff] [blame] | 636 | glue->regs = dev_read_addr_size_index(dev, 0, &glue->size); |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 637 | |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 638 | ret = dwc3_glue_clk_init(dev, glue); |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | |
| 642 | ret = dwc3_glue_reset_init(dev, glue); |
| 643 | if (ret) |
| 644 | return ret; |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 645 | |
Jonas Karlman | fd5c3c2 | 2023-08-31 22:16:36 +0000 | [diff] [blame] | 646 | if (generic_phy_valid(&phy)) { |
Michal Simek | bb19d62 | 2022-03-09 10:05:45 +0100 | [diff] [blame] | 647 | ret = generic_phy_power_on(&phy); |
| 648 | if (ret) |
| 649 | return ret; |
| 650 | } |
| 651 | |
Jonas Karlman | ee1e070 | 2023-07-30 22:59:55 +0000 | [diff] [blame] | 652 | device_find_first_child(dev, &child); |
| 653 | if (!child) |
| 654 | return 0; |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 655 | |
Kunihiko Hayashi | df0f5d3 | 2023-02-20 14:50:27 +0900 | [diff] [blame] | 656 | if (glue->clks.count == 0) { |
| 657 | ret = dwc3_glue_clk_init(child, glue); |
| 658 | if (ret) |
| 659 | return ret; |
| 660 | } |
| 661 | |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 662 | if (glue->resets.count == 0) { |
| 663 | ret = dwc3_glue_reset_init(child, glue); |
| 664 | if (ret) |
| 665 | return ret; |
| 666 | } |
| 667 | |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 668 | while (child) { |
| 669 | enum usb_dr_mode dr_mode; |
| 670 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 671 | dr_mode = usb_get_dr_mode(dev_ofnode(child)); |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 672 | device_find_next_child(&child); |
Marek Vasut | 68c8656 | 2022-04-13 00:42:55 +0200 | [diff] [blame] | 673 | if (ops && ops->glue_configure) |
| 674 | ops->glue_configure(dev, index, dr_mode); |
Jean-Jacques Hiblot | ae004d3 | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 675 | index++; |
| 676 | } |
| 677 | |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
Kunihiko Hayashi | 6cf357e | 2023-02-20 14:50:28 +0900 | [diff] [blame] | 681 | int dwc3_glue_remove(struct udevice *dev) |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 682 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 683 | struct dwc3_glue_data *glue = dev_get_plat(dev); |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 684 | |
| 685 | reset_release_bulk(&glue->resets); |
| 686 | |
| 687 | clk_release_bulk(&glue->clks); |
| 688 | |
Jean-Jacques Hiblot | 5a94557 | 2019-07-05 09:33:56 +0200 | [diff] [blame] | 689 | return 0; |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static const struct udevice_id dwc3_glue_ids[] = { |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 693 | { .compatible = "xlnx,zynqmp-dwc3" }, |
Siva Durga Prasad Paladugu | 1eb3c30 | 2020-05-12 08:36:01 +0200 | [diff] [blame] | 694 | { .compatible = "xlnx,versal-dwc3" }, |
Jean-Jacques Hiblot | 3e0684b | 2018-12-04 11:12:56 +0100 | [diff] [blame] | 695 | { .compatible = "ti,keystone-dwc3"}, |
Jean-Jacques Hiblot | 65596f1 | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 696 | { .compatible = "ti,dwc3", .data = (ulong)&ti_ops }, |
Jean-Jacques Hiblot | ca848df | 2018-12-04 11:30:50 +0100 | [diff] [blame] | 697 | { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops }, |
Vignesh Raghavendra | c628295 | 2019-12-09 10:37:29 +0530 | [diff] [blame] | 698 | { .compatible = "ti,am654-dwc3" }, |
Jagan Teki | e5b9341 | 2023-06-06 22:39:14 +0530 | [diff] [blame] | 699 | { .compatible = "rockchip,rk3328-dwc3", .data = (ulong)&rk_ops }, |
Frank Wang | f5a6c5b | 2020-05-26 11:34:31 +0800 | [diff] [blame] | 700 | { .compatible = "rockchip,rk3399-dwc3" }, |
Jonas Karlman | 04c6ae8 | 2023-07-30 22:59:57 +0000 | [diff] [blame] | 701 | { .compatible = "rockchip,rk3568-dwc3", .data = (ulong)&rk_ops }, |
Jonas Karlman | 39076d9 | 2023-11-12 15:25:25 +0000 | [diff] [blame] | 702 | { .compatible = "rockchip,rk3588-dwc3", .data = (ulong)&rk_ops }, |
Caleb Connolly | 60d1fc2 | 2024-03-20 14:30:47 +0000 | [diff] [blame] | 703 | { .compatible = "qcom,dwc3", .data = (ulong)&qcom_ops }, |
Marek Vasut | ae21934 | 2022-04-13 00:42:56 +0200 | [diff] [blame] | 704 | { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops }, |
Angus Ainslie | 6e382a8 | 2022-02-02 15:08:54 -0800 | [diff] [blame] | 705 | { .compatible = "fsl,imx8mq-dwc3" }, |
Andy Shevchenko | 221d7fa | 2020-12-03 19:45:01 +0200 | [diff] [blame] | 706 | { .compatible = "intel,tangier-dwc3" }, |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 707 | { } |
| 708 | }; |
| 709 | |
| 710 | U_BOOT_DRIVER(dwc3_generic_wrapper) = { |
| 711 | .name = "dwc3-generic-wrapper", |
Jean-Jacques Hiblot | b49b5c2 | 2019-07-05 09:33:58 +0200 | [diff] [blame] | 712 | .id = UCLASS_NOP, |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 713 | .of_match = dwc3_glue_ids, |
| 714 | .bind = dwc3_glue_bind, |
| 715 | .probe = dwc3_glue_probe, |
| 716 | .remove = dwc3_glue_remove, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 717 | .plat_auto = sizeof(struct dwc3_glue_data), |
Jean-Jacques Hiblot | aa866a0 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 718 | |
Michal Simek | 9d8cbbf | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 719 | }; |