blob: 21452ad15693fa62c49bffa37f175905f5f29a54 [file] [log] [blame]
Patrick Delaunayd6e53c72018-10-26 09:02:52 +02001// SPDX-License-Identifier: GPL-2.0
Michal Simek9d8cbbf2018-05-18 13:15:06 +02002/*
3 * Generic DWC3 Glue layer
4 *
5 * Copyright (C) 2016 - 2018 Xilinx, Inc.
6 *
7 * Based on dwc3-omap.c.
8 */
9
Michal Simek9d8cbbf2018-05-18 13:15:06 +020010#include <dm.h>
Patrice Chotard94be1522025-01-30 17:35:42 +010011#include <reset.h>
12#include <asm/gpio.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020013#include <dm/lists.h>
Frank Wangf5a6c5b2020-05-26 11:34:31 +080014#include <linux/delay.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020015#include <linux/usb/gadget.h>
Caleb Connollyc52bc902024-02-26 17:26:06 +000016#include <power/regulator.h>
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020017#include <usb/xhci.h>
Patrice Chotard94be1522025-01-30 17:35:42 +010018#include "core.h"
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +090019#include "dwc3-generic.h"
Patrice Chotard94be1522025-01-30 17:35:42 +010020#include "gadget.h"
Frank Wangf5a6c5b2020-05-26 11:34:31 +080021
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020022struct dwc3_generic_plat {
23 fdt_addr_t base;
24 u32 maximum_speed;
25 enum usb_dr_mode dr_mode;
26};
27
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020028struct dwc3_generic_priv {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020029 void *base;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010030 struct dwc3 dwc3;
developerf8bced12020-05-02 11:35:13 +020031 struct phy_bulk phys;
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +053032 struct gpio_desc *ulpi_reset;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010033};
34
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020035struct dwc3_generic_host_priv {
36 struct xhci_ctrl xhci_ctrl;
37 struct dwc3_generic_priv gen_priv;
Caleb Connollyc52bc902024-02-26 17:26:06 +000038 struct udevice *vbus_supply;
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020039};
40
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020041static int dwc3_generic_probe(struct udevice *dev,
Siddharth Vadapallib9ae082e2024-11-26 17:33:18 +053042 struct dwc3_generic_priv *priv,
43 enum usb_dr_mode mode)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020044{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010045 int rc;
Simon Glassfa20e932020-12-03 16:55:20 -070046 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010047 struct dwc3 *dwc3 = &priv->dwc3;
Simon Glassfa20e932020-12-03 16:55:20 -070048 struct dwc3_glue_data *glue = dev_get_plat(dev->parent);
Marek Vasutaacbcb62022-11-27 15:31:52 +010049 int __maybe_unused index;
50 ofnode __maybe_unused node;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010051
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020052 dwc3->dev = dev;
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020053 dwc3->maximum_speed = plat->maximum_speed;
Siddharth Vadapallib9ae082e2024-11-26 17:33:18 +053054 dwc3->dr_mode = mode;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020055#if CONFIG_IS_ENABLED(OF_CONTROL)
56 dwc3_of_parse(dwc3);
Marek Vasutaacbcb62022-11-27 15:31:52 +010057
Marek Vasut4d285722023-02-20 14:50:25 +090058 /*
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 Vasutaacbcb62022-11-27 15:31:52 +010069 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 Vasut4d285722023-02-20 14:50:25 +090073 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 Vasutaacbcb62022-11-27 15:31:52 +010079 if (index >= 0)
80 dwc3->ref_clk = &glue->clks.clks[index];
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020081#endif
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020082
Frank Wangf5a6c5b2020-05-26 11:34:31 +080083 /*
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
developerf8bced12020-05-02 11:35:13 +020092 rc = dwc3_setup_phy(dev, &priv->phys);
Siva Durga Prasad Paladuguc37f8f32020-10-21 14:17:31 +020093 if (rc && rc != -ENOTSUPP)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010094 return rc;
95
T Karthik Reddyf7adf892022-07-08 11:21:59 +020096 if (CONFIG_IS_ENABLED(DM_GPIO) &&
97 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +053098 priv->ulpi_reset = devm_gpiod_get_optional(dev->parent, "reset",
Peter Korsgaard686a0f02023-06-28 14:26:48 +020099 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530100 /* 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 Reddyf7adf892022-07-08 11:21:59 +0200106
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530107 mdelay(5);
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200108
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530109 rc = dm_gpio_set_value(priv->ulpi_reset, 0);
110 if (rc)
111 return rc;
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200112
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530113 mdelay(5);
114 }
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200115 }
116
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800117 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3"))
118 reset_deassert_bulk(&glue->resets);
119
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200120 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
121 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +0200122
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100123 rc = dwc3_init(dwc3);
124 if (rc) {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200125 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100126 return rc;
127 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200128
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100129 return 0;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200130}
131
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200132static int dwc3_generic_remove(struct udevice *dev,
133 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200134{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100135 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200136
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200137 if (CONFIG_IS_ENABLED(DM_GPIO) &&
Venkatesh Yadav Abbarapu63d5d862023-08-09 09:03:50 +0530138 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3") &&
139 priv->ulpi_reset) {
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530140 struct gpio_desc *ulpi_reset = priv->ulpi_reset;
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200141
142 dm_gpio_free(ulpi_reset->dev, ulpi_reset);
143 }
144
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100145 dwc3_remove(dwc3);
developerf8bced12020-05-02 11:35:13 +0200146 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100147 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200148
149 return 0;
150}
151
Simon Glassaad29ae2020-12-03 16:55:21 -0700152static int dwc3_generic_of_to_plat(struct udevice *dev)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200153{
Simon Glassfa20e932020-12-03 16:55:20 -0700154 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Simon Glassa7ece582020-12-19 10:40:14 -0700155 ofnode node = dev_ofnode(dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200156
Angus Ainslie6e382a82022-02-02 15:08:54 -0800157 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 Simek9d8cbbf2018-05-18 13:15:06 +0200163
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200164 plat->maximum_speed = usb_get_maximum_speed(node);
165 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot547df0d2019-09-11 11:33:51 +0200166 pr_info("No USB maximum speed specified. Using super speed\n");
167 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200168 }
169
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200170 plat->dr_mode = usb_get_dr_mode(node);
171 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Angus Ainslie6e382a82022-02-02 15:08:54 -0800172 /* 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 Simek9d8cbbf2018-05-18 13:15:06 +0200179 }
180
181 return 0;
182}
183
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200184#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Marek Vasuteab470b2024-06-14 02:51:19 +0200185static int dwc3_generic_peripheral_probe(struct udevice *dev)
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200186{
187 struct dwc3_generic_priv *priv = dev_get_priv(dev);
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200188
Siddharth Vadapallib9ae082e2024-11-26 17:33:18 +0530189 return dwc3_generic_probe(dev, priv, USB_DR_MODE_PERIPHERAL);
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200190}
191
Marek Vasuteab470b2024-06-14 02:51:19 +0200192static int dwc3_generic_peripheral_remove(struct udevice *dev)
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200193{
194 struct dwc3_generic_priv *priv = dev_get_priv(dev);
195
Marek Vasuteab470b2024-06-14 02:51:19 +0200196 return dwc3_generic_remove(dev, priv);
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200197}
198
Marek Vasuteab470b2024-06-14 02:51:19 +0200199static int dwc3_gadget_handle_interrupts(struct udevice *dev)
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200200{
201 struct dwc3_generic_priv *priv = dev_get_priv(dev);
Marek Vasuteab470b2024-06-14 02:51:19 +0200202 struct dwc3 *dwc3 = &priv->dwc3;
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200203
Marek Vasuteab470b2024-06-14 02:51:19 +0200204 dwc3_gadget_uboot_handle_interrupt(dwc3);
205
206 return 0;
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200207}
208
Marek Vasuteab470b2024-06-14 02:51:19 +0200209static const struct usb_gadget_generic_ops dwc3_gadget_ops = {
210 .handle_interrupts = dwc3_gadget_handle_interrupts,
211};
212
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200213U_BOOT_DRIVER(dwc3_generic_peripheral) = {
214 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100215 .id = UCLASS_USB_GADGET_GENERIC,
Simon Glassaad29ae2020-12-03 16:55:21 -0700216 .of_to_plat = dwc3_generic_of_to_plat,
Marek Vasuteab470b2024-06-14 02:51:19 +0200217 .ops = &dwc3_gadget_ops,
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200218 .probe = dwc3_generic_peripheral_probe,
219 .remove = dwc3_generic_peripheral_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700220 .priv_auto = sizeof(struct dwc3_generic_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700221 .plat_auto = sizeof(struct dwc3_generic_plat),
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200222};
Jean-Jacques Hiblot44aaec72018-11-29 10:52:42 +0100223#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200224
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000225#if CONFIG_IS_ENABLED(USB_HOST)
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200226static 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 Vadapallib9ae082e2024-11-26 17:33:18 +0530233 rc = dwc3_generic_probe(dev, &priv->gen_priv, USB_DR_MODE_HOST);
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200234 if (rc)
235 return rc;
236
Caleb Connollyc52bc902024-02-26 17:26:06 +0000237 rc = device_get_supply_regulator(dev, "vbus-supply", &priv->vbus_supply);
Jan Kiszkaedc1e752024-08-08 10:51:33 +0200238 if (rc && rc != -ENOSYS)
Caleb Connollyc52bc902024-02-26 17:26:06 +0000239 debug("%s: No vbus regulator found: %d\n", dev->name, rc);
240
Jan Kiszkaedc1e752024-08-08 10:51:33 +0200241 /* Does not return an error if regulator is invalid - but does so when DM_REGULATOR is disabled */
Caleb Connollyc52bc902024-02-26 17:26:06 +0000242 rc = regulator_set_enable_if_allowed(priv->vbus_supply, true);
Jan Kiszkaedc1e752024-08-08 10:51:33 +0200243 if (rc && rc != -ENOSYS)
Caleb Connollyc52bc902024-02-26 17:26:06 +0000244 return rc;
245
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200246 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 Connollyc52bc902024-02-26 17:26:06 +0000250 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 Hiblot175cd7c2019-09-11 11:33:50 +0200255}
256
257static 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 Connollyc52bc902024-02-26 17:26:06 +0000262 /* This function always returns 0 */
263 xhci_deregister(dev);
264
265 rc = regulator_set_enable_if_allowed(priv->vbus_supply, false);
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200266 if (rc)
Caleb Connollyc52bc902024-02-26 17:26:06 +0000267 debug("%s: Failed to disable vbus regulator: %d\n", dev->name, rc);
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200268
269 return dwc3_generic_remove(dev, &priv->gen_priv);
270}
271
272U_BOOT_DRIVER(dwc3_generic_host) = {
273 .name = "dwc3-generic-host",
274 .id = UCLASS_USB,
Simon Glassaad29ae2020-12-03 16:55:21 -0700275 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200276 .probe = dwc3_generic_host_probe,
277 .remove = dwc3_generic_host_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700278 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700279 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200280 .ops = &xhci_usb_ops,
281 .flags = DM_FLAG_ALLOC_PRIV_DMA,
282};
283#endif
284
Marek Vasutae219342022-04-13 00:42:56 +0200285void 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
332struct dwc3_glue_ops imx8mp_ops = {
333 .glue_configure = dwc3_imx8mp_glue_configure,
334};
335
Marek Vasut68c86562022-04-13 00:42:55 +0200336void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100337 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)
350enum 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 Glassfa20e932020-12-03 16:55:20 -0700362 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100363 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
416struct dwc3_glue_ops ti_ops = {
Marek Vasut68c86562022-04-13 00:42:55 +0200417 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100418};
419
Caleb Connolly60d1fc22024-03-20 14:30:47 +0000420/* 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
441static 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 */
457static 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
470static 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
487struct dwc3_glue_ops qcom_ops = {
488 .glue_configure = dwc3_qcom_glue_configure,
489};
490
Jonas Karlman04c6ae82023-07-30 22:59:57 +0000491static 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
500struct dwc3_glue_ops rk_ops = {
501 .glue_get_ctrl_dev = dwc3_rk_glue_get_ctrl_dev,
502};
503
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900504static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200505{
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900506 const char *name = ofnode_get_name(node);
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000507 const char *driver;
Angus Ainslie6e382a82022-02-02 15:08:54 -0800508 enum usb_dr_mode dr_mode;
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900509 struct udevice *dev;
510 int ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200511
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900512 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200513
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900514 /* 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 Simek9d8cbbf2018-05-18 13:15:06 +0200518
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000519 if (CONFIG_IS_ENABLED(DM_USB_GADGET) &&
520 (dr_mode == USB_DR_MODE_PERIPHERAL || dr_mode == USB_DR_MODE_OTG)) {
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900521 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
522 driver = "dwc3-generic-peripheral";
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000523 } else if (CONFIG_IS_ENABLED(USB_HOST) && dr_mode == USB_DR_MODE_HOST) {
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900524 debug("%s: dr_mode: HOST\n", __func__);
525 driver = "dwc3-generic-host";
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000526 } else {
527 debug("%s: unsupported dr_mode %d\n", __func__, dr_mode);
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900528 return -ENODEV;
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000529 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100530
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900531 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 Hayashi6cf357e2023-02-20 14:50:28 +0900542int dwc3_glue_bind(struct udevice *parent)
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900543{
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 Simek9d8cbbf2018-05-18 13:15:06 +0200561 return ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200562 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100563
564 return 0;
565}
566
567static 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 Raghavendrae9310fc2019-10-25 13:48:05 +0530573 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100574 return 0;
575 else if (ret)
576 return ret;
577
Caleb Connolly60d1fc22024-03-20 14:30:47 +0000578 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 Hiblotaa866a02018-11-29 10:52:48 +0100586 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
595static 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 Raghavendrae9310fc2019-10-25 13:48:05 +0530601 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100602 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 Hayashi6cf357e2023-02-20 14:50:28 +0900617int dwc3_glue_probe(struct udevice *dev)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100618{
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100619 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700620 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100621 struct udevice *child = NULL;
622 int index = 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100623 int ret;
Michal Simekbb19d622022-03-09 10:05:45 +0100624 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 Kiszka2fe2cf02022-04-25 13:26:45 +0200631 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simekbb19d622022-03-09 10:05:45 +0100632 debug("could not get phy (err %d)\n", ret);
633 return ret;
634 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100635
Kunihiko Hayashi54c277e2023-02-20 14:50:29 +0900636 glue->regs = dev_read_addr_size_index(dev, 0, &glue->size);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100637
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100638 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 Simek9d8cbbf2018-05-18 13:15:06 +0200645
Jonas Karlmanfd5c3c22023-08-31 22:16:36 +0000646 if (generic_phy_valid(&phy)) {
Michal Simekbb19d622022-03-09 10:05:45 +0100647 ret = generic_phy_power_on(&phy);
648 if (ret)
649 return ret;
650 }
651
Jonas Karlmanee1e0702023-07-30 22:59:55 +0000652 device_find_first_child(dev, &child);
653 if (!child)
654 return 0;
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100655
Kunihiko Hayashidf0f5d32023-02-20 14:50:27 +0900656 if (glue->clks.count == 0) {
657 ret = dwc3_glue_clk_init(child, glue);
658 if (ret)
659 return ret;
660 }
661
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800662 if (glue->resets.count == 0) {
663 ret = dwc3_glue_reset_init(child, glue);
664 if (ret)
665 return ret;
666 }
667
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100668 while (child) {
669 enum usb_dr_mode dr_mode;
670
Simon Glassa7ece582020-12-19 10:40:14 -0700671 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100672 device_find_next_child(&child);
Marek Vasut68c86562022-04-13 00:42:55 +0200673 if (ops && ops->glue_configure)
674 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100675 index++;
676 }
677
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200678 return 0;
679}
680
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +0900681int dwc3_glue_remove(struct udevice *dev)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100682{
Simon Glassfa20e932020-12-03 16:55:20 -0700683 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100684
685 reset_release_bulk(&glue->resets);
686
687 clk_release_bulk(&glue->clks);
688
Jean-Jacques Hiblot5a945572019-07-05 09:33:56 +0200689 return 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100690}
691
692static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200693 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu1eb3c302020-05-12 08:36:01 +0200694 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot3e0684b2018-12-04 11:12:56 +0100695 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100696 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblotca848df2018-12-04 11:30:50 +0100697 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendrac6282952019-12-09 10:37:29 +0530698 { .compatible = "ti,am654-dwc3" },
Jagan Tekie5b93412023-06-06 22:39:14 +0530699 { .compatible = "rockchip,rk3328-dwc3", .data = (ulong)&rk_ops },
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800700 { .compatible = "rockchip,rk3399-dwc3" },
Jonas Karlman04c6ae82023-07-30 22:59:57 +0000701 { .compatible = "rockchip,rk3568-dwc3", .data = (ulong)&rk_ops },
Jonas Karlman39076d92023-11-12 15:25:25 +0000702 { .compatible = "rockchip,rk3588-dwc3", .data = (ulong)&rk_ops },
Caleb Connolly60d1fc22024-03-20 14:30:47 +0000703 { .compatible = "qcom,dwc3", .data = (ulong)&qcom_ops },
Marek Vasutae219342022-04-13 00:42:56 +0200704 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainslie6e382a82022-02-02 15:08:54 -0800705 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko221d7fa2020-12-03 19:45:01 +0200706 { .compatible = "intel,tangier-dwc3" },
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200707 { }
708};
709
710U_BOOT_DRIVER(dwc3_generic_wrapper) = {
711 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblotb49b5c22019-07-05 09:33:58 +0200712 .id = UCLASS_NOP,
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100713 .of_match = dwc3_glue_ids,
714 .bind = dwc3_glue_bind,
715 .probe = dwc3_glue_probe,
716 .remove = dwc3_glue_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -0700717 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100718
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200719};