blob: 8db678eb85d45da5ec7020312eaa6c06d1e24e7d [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
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020012#include <dm.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010015#include <dwc3-uboot.h>
Michal Simekbb19d622022-03-09 10:05:45 +010016#include <generic-phy.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Frank Wangf5a6c5b2020-05-26 11:34:31 +080018#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060019#include <linux/printk.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020020#include <linux/usb/ch9.h>
21#include <linux/usb/gadget.h>
22#include <malloc.h>
Caleb Connollyc52bc902024-02-26 17:26:06 +000023#include <power/regulator.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020024#include <usb.h>
25#include "core.h"
26#include "gadget.h"
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010027#include <reset.h>
28#include <clk.h>
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020029#include <usb/xhci.h>
T Karthik Reddyf7adf892022-07-08 11:21:59 +020030#include <asm/gpio.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020031
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +090032#include "dwc3-generic.h"
Frank Wangf5a6c5b2020-05-26 11:34:31 +080033
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020034struct dwc3_generic_plat {
35 fdt_addr_t base;
36 u32 maximum_speed;
37 enum usb_dr_mode dr_mode;
38};
39
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020040struct dwc3_generic_priv {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020041 void *base;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010042 struct dwc3 dwc3;
developerf8bced12020-05-02 11:35:13 +020043 struct phy_bulk phys;
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +053044 struct gpio_desc *ulpi_reset;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010045};
46
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020047struct dwc3_generic_host_priv {
48 struct xhci_ctrl xhci_ctrl;
49 struct dwc3_generic_priv gen_priv;
Caleb Connollyc52bc902024-02-26 17:26:06 +000050 struct udevice *vbus_supply;
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020051};
52
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020053static int dwc3_generic_probe(struct udevice *dev,
54 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020055{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010056 int rc;
Simon Glassfa20e932020-12-03 16:55:20 -070057 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010058 struct dwc3 *dwc3 = &priv->dwc3;
Simon Glassfa20e932020-12-03 16:55:20 -070059 struct dwc3_glue_data *glue = dev_get_plat(dev->parent);
Marek Vasutaacbcb62022-11-27 15:31:52 +010060 int __maybe_unused index;
61 ofnode __maybe_unused node;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010062
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020063 dwc3->dev = dev;
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020064 dwc3->maximum_speed = plat->maximum_speed;
65 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020066#if CONFIG_IS_ENABLED(OF_CONTROL)
67 dwc3_of_parse(dwc3);
Marek Vasutaacbcb62022-11-27 15:31:52 +010068
Marek Vasut4d285722023-02-20 14:50:25 +090069 /*
70 * There are currently four disparate placement possibilities of DWC3
71 * reference clock phandle in SoC DTs:
72 * - in top level glue node, with generic subnode without clock (ZynqMP)
73 * - in top level generic node, with no subnode (i.MX8MQ)
74 * - in generic subnode, with other clock in top level node (i.MX8MP)
75 * - in both top level node and generic subnode (Rockchip)
76 * Cover all the possibilities here by looking into both nodes, start
77 * with the top level node as that seems to be used in majority of DTs
78 * to reference the clock.
79 */
Marek Vasutaacbcb62022-11-27 15:31:52 +010080 node = dev_ofnode(dev->parent);
81 index = ofnode_stringlist_search(node, "clock-names", "ref");
82 if (index < 0)
83 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
Marek Vasut4d285722023-02-20 14:50:25 +090084 if (index < 0) {
85 node = dev_ofnode(dev);
86 index = ofnode_stringlist_search(node, "clock-names", "ref");
87 if (index < 0)
88 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
89 }
Marek Vasutaacbcb62022-11-27 15:31:52 +010090 if (index >= 0)
91 dwc3->ref_clk = &glue->clks.clks[index];
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020092#endif
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020093
Frank Wangf5a6c5b2020-05-26 11:34:31 +080094 /*
95 * It must hold whole USB3.0 OTG controller in resetting to hold pipe
96 * power state in P2 before initializing TypeC PHY on RK3399 platform.
97 */
98 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) {
99 reset_assert_bulk(&glue->resets);
100 udelay(1);
101 }
102
developerf8bced12020-05-02 11:35:13 +0200103 rc = dwc3_setup_phy(dev, &priv->phys);
Siva Durga Prasad Paladuguc37f8f32020-10-21 14:17:31 +0200104 if (rc && rc != -ENOTSUPP)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100105 return rc;
106
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200107 if (CONFIG_IS_ENABLED(DM_GPIO) &&
108 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530109 priv->ulpi_reset = devm_gpiod_get_optional(dev->parent, "reset",
Peter Korsgaard686a0f02023-06-28 14:26:48 +0200110 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530111 /* property is optional, don't return error! */
112 if (priv->ulpi_reset) {
113 /* Toggle ulpi to reset the phy. */
114 rc = dm_gpio_set_value(priv->ulpi_reset, 1);
115 if (rc)
116 return rc;
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200117
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530118 mdelay(5);
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200119
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530120 rc = dm_gpio_set_value(priv->ulpi_reset, 0);
121 if (rc)
122 return rc;
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200123
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530124 mdelay(5);
125 }
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200126 }
127
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800128 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3"))
129 reset_deassert_bulk(&glue->resets);
130
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200131 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
132 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +0200133
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100134
135 rc = dwc3_init(dwc3);
136 if (rc) {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200137 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100138 return rc;
139 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200140
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100141 return 0;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200142}
143
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200144static int dwc3_generic_remove(struct udevice *dev,
145 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200146{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100147 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200148
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200149 if (CONFIG_IS_ENABLED(DM_GPIO) &&
Venkatesh Yadav Abbarapu63d5d862023-08-09 09:03:50 +0530150 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3") &&
151 priv->ulpi_reset) {
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530152 struct gpio_desc *ulpi_reset = priv->ulpi_reset;
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200153
154 dm_gpio_free(ulpi_reset->dev, ulpi_reset);
155 }
156
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100157 dwc3_remove(dwc3);
developerf8bced12020-05-02 11:35:13 +0200158 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100159 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200160
161 return 0;
162}
163
Simon Glassaad29ae2020-12-03 16:55:21 -0700164static int dwc3_generic_of_to_plat(struct udevice *dev)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200165{
Simon Glassfa20e932020-12-03 16:55:20 -0700166 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Simon Glassa7ece582020-12-19 10:40:14 -0700167 ofnode node = dev_ofnode(dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200168
Angus Ainslie6e382a82022-02-02 15:08:54 -0800169 if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) {
170 /* This is a leaf so check the parent */
171 plat->base = dev_read_addr(dev->parent);
172 } else {
173 plat->base = dev_read_addr(dev);
174 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200175
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200176 plat->maximum_speed = usb_get_maximum_speed(node);
177 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot547df0d2019-09-11 11:33:51 +0200178 pr_info("No USB maximum speed specified. Using super speed\n");
179 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200180 }
181
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200182 plat->dr_mode = usb_get_dr_mode(node);
183 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Angus Ainslie6e382a82022-02-02 15:08:54 -0800184 /* might be a leaf so check the parent for mode */
185 node = dev_ofnode(dev->parent);
186 plat->dr_mode = usb_get_dr_mode(node);
187 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
188 pr_err("Invalid usb mode setup\n");
189 return -ENODEV;
190 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200191 }
192
193 return 0;
194}
195
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200196#if CONFIG_IS_ENABLED(DM_USB_GADGET)
197int dm_usb_gadget_handle_interrupts(struct udevice *dev)
198{
199 struct dwc3_generic_priv *priv = dev_get_priv(dev);
200 struct dwc3 *dwc3 = &priv->dwc3;
201
202 dwc3_gadget_uboot_handle_interrupt(dwc3);
203
204 return 0;
205}
206
207static int dwc3_generic_peripheral_probe(struct udevice *dev)
208{
209 struct dwc3_generic_priv *priv = dev_get_priv(dev);
210
211 return dwc3_generic_probe(dev, priv);
212}
213
214static int dwc3_generic_peripheral_remove(struct udevice *dev)
215{
216 struct dwc3_generic_priv *priv = dev_get_priv(dev);
217
218 return dwc3_generic_remove(dev, priv);
219}
220
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200221U_BOOT_DRIVER(dwc3_generic_peripheral) = {
222 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100223 .id = UCLASS_USB_GADGET_GENERIC,
Simon Glassaad29ae2020-12-03 16:55:21 -0700224 .of_to_plat = dwc3_generic_of_to_plat,
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200225 .probe = dwc3_generic_peripheral_probe,
226 .remove = dwc3_generic_peripheral_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700227 .priv_auto = sizeof(struct dwc3_generic_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700228 .plat_auto = sizeof(struct dwc3_generic_plat),
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200229};
Jean-Jacques Hiblot44aaec72018-11-29 10:52:42 +0100230#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200231
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000232#if CONFIG_IS_ENABLED(USB_HOST)
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200233static int dwc3_generic_host_probe(struct udevice *dev)
234{
235 struct xhci_hcor *hcor;
236 struct xhci_hccr *hccr;
237 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
238 int rc;
239
240 rc = dwc3_generic_probe(dev, &priv->gen_priv);
241 if (rc)
242 return rc;
243
Caleb Connollyc52bc902024-02-26 17:26:06 +0000244 rc = device_get_supply_regulator(dev, "vbus-supply", &priv->vbus_supply);
245 if (rc)
246 debug("%s: No vbus regulator found: %d\n", dev->name, rc);
247
248 /* Only returns an error if regulator is valid and failed to enable due to a driver issue */
249 rc = regulator_set_enable_if_allowed(priv->vbus_supply, true);
250 if (rc)
251 return rc;
252
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200253 hccr = (struct xhci_hccr *)priv->gen_priv.base;
254 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
255 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
256
Caleb Connollyc52bc902024-02-26 17:26:06 +0000257 rc = xhci_register(dev, hccr, hcor);
258 if (rc)
259 regulator_set_enable_if_allowed(priv->vbus_supply, false);
260
261 return rc;
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200262}
263
264static int dwc3_generic_host_remove(struct udevice *dev)
265{
266 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
267 int rc;
268
Caleb Connollyc52bc902024-02-26 17:26:06 +0000269 /* This function always returns 0 */
270 xhci_deregister(dev);
271
272 rc = regulator_set_enable_if_allowed(priv->vbus_supply, false);
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200273 if (rc)
Caleb Connollyc52bc902024-02-26 17:26:06 +0000274 debug("%s: Failed to disable vbus regulator: %d\n", dev->name, rc);
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200275
276 return dwc3_generic_remove(dev, &priv->gen_priv);
277}
278
279U_BOOT_DRIVER(dwc3_generic_host) = {
280 .name = "dwc3-generic-host",
281 .id = UCLASS_USB,
Simon Glassaad29ae2020-12-03 16:55:21 -0700282 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200283 .probe = dwc3_generic_host_probe,
284 .remove = dwc3_generic_host_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700285 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700286 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200287 .ops = &xhci_usb_ops,
288 .flags = DM_FLAG_ALLOC_PRIV_DMA,
289};
290#endif
291
Marek Vasutae219342022-04-13 00:42:56 +0200292void dwc3_imx8mp_glue_configure(struct udevice *dev, int index,
293 enum usb_dr_mode mode)
294{
295/* USB glue registers */
296#define USB_CTRL0 0x00
297#define USB_CTRL1 0x04
298
299#define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
300#define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
301#define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
302
303#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
304#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
305 fdt_addr_t regs = dev_read_addr_index(dev, 1);
306 void *base = map_physmem(regs, 0x8, MAP_NOCACHE);
307 u32 value;
308
309 value = readl(base + USB_CTRL0);
310
311 if (dev_read_bool(dev, "fsl,permanently-attached"))
312 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
313 else
314 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
315
316 if (dev_read_bool(dev, "fsl,disable-port-power-control"))
317 value &= ~(USB_CTRL0_PORTPWR_EN);
318 else
319 value |= USB_CTRL0_PORTPWR_EN;
320
321 writel(value, base + USB_CTRL0);
322
323 value = readl(base + USB_CTRL1);
324 if (dev_read_bool(dev, "fsl,over-current-active-low"))
325 value |= USB_CTRL1_OC_POLARITY;
326 else
327 value &= ~USB_CTRL1_OC_POLARITY;
328
329 if (dev_read_bool(dev, "fsl,power-active-low"))
330 value |= USB_CTRL1_PWR_POLARITY;
331 else
332 value &= ~USB_CTRL1_PWR_POLARITY;
333
334 writel(value, base + USB_CTRL1);
335
336 unmap_physmem(base, MAP_NOCACHE);
337}
338
339struct dwc3_glue_ops imx8mp_ops = {
340 .glue_configure = dwc3_imx8mp_glue_configure,
341};
342
Marek Vasut68c86562022-04-13 00:42:55 +0200343void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100344 enum usb_dr_mode mode)
345{
346#define USBOTGSS_UTMI_OTG_STATUS 0x0084
347#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
348
349/* UTMI_OTG_STATUS REGISTER */
350#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
351#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
352#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
353#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
354#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
355#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
356#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
357enum dwc3_omap_utmi_mode {
358 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
359 DWC3_OMAP_UTMI_MODE_HW,
360 DWC3_OMAP_UTMI_MODE_SW,
361};
362
363 u32 use_id_pin;
364 u32 host_mode;
365 u32 reg;
366 u32 utmi_mode;
367 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
368
Simon Glassfa20e932020-12-03 16:55:20 -0700369 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100370 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
371
372 if (device_is_compatible(dev, "ti,am437x-dwc3"))
373 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
374
375 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
376 DWC3_OMAP_UTMI_MODE_UNKNOWN);
377 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
378 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
379 dev->name);
380 mode = USB_DR_MODE_PERIPHERAL;
381 }
382
383 switch (mode) {
384 case USB_DR_MODE_PERIPHERAL:
385 use_id_pin = 0;
386 host_mode = 0;
387 break;
388 case USB_DR_MODE_HOST:
389 use_id_pin = 0;
390 host_mode = 1;
391 break;
392 case USB_DR_MODE_OTG:
393 default:
394 use_id_pin = 1;
395 host_mode = 0;
396 break;
397 }
398
399 reg = readl(base + utmi_status_offset);
400
401 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
402 if (!use_id_pin)
403 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
404
405 writel(reg, base + utmi_status_offset);
406
407 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
408 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
409 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
410
411 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
412 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
413
414 if (!host_mode)
415 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
416 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
417
418 writel(reg, base + utmi_status_offset);
419
420 unmap_physmem(base, MAP_NOCACHE);
421}
422
423struct dwc3_glue_ops ti_ops = {
Marek Vasut68c86562022-04-13 00:42:55 +0200424 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100425};
426
Caleb Connolly60d1fc22024-03-20 14:30:47 +0000427/* USB QSCRATCH Hardware registers */
428#define QSCRATCH_GENERAL_CFG 0x08
429#define PIPE_UTMI_CLK_SEL BIT(0)
430#define PIPE3_PHYSTATUS_SW BIT(3)
431#define PIPE_UTMI_CLK_DIS BIT(8)
432
433#define QSCRATCH_HS_PHY_CTRL 0x10
434#define UTMI_OTG_VBUS_VALID BIT(20)
435#define SW_SESSVLD_SEL BIT(28)
436
437#define QSCRATCH_SS_PHY_CTRL 0x30
438#define LANE0_PWR_PRESENT BIT(24)
439
440#define PWR_EVNT_IRQ_STAT_REG 0x58
441#define PWR_EVNT_LPM_IN_L2_MASK BIT(4)
442#define PWR_EVNT_LPM_OUT_L2_MASK BIT(5)
443
444#define SDM845_QSCRATCH_BASE_OFFSET 0xf8800
445#define SDM845_QSCRATCH_SIZE 0x400
446#define SDM845_DWC3_CORE_SIZE 0xcd00
447
448static void dwc3_qcom_vbus_override_enable(void __iomem *qscratch_base, bool enable)
449{
450 if (enable) {
451 setbits_le32(qscratch_base + QSCRATCH_SS_PHY_CTRL,
452 LANE0_PWR_PRESENT);
453 setbits_le32(qscratch_base + QSCRATCH_HS_PHY_CTRL,
454 UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
455 } else {
456 clrbits_le32(qscratch_base + QSCRATCH_SS_PHY_CTRL,
457 LANE0_PWR_PRESENT);
458 clrbits_le32(qscratch_base + QSCRATCH_HS_PHY_CTRL,
459 UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
460 }
461}
462
463/* For controllers running without superspeed PHYs */
464static void dwc3_qcom_select_utmi_clk(void __iomem *qscratch_base)
465{
466 /* Configure dwc3 to use UTMI clock as PIPE clock not present */
467 setbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG,
468 PIPE_UTMI_CLK_DIS);
469
470 setbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG,
471 PIPE_UTMI_CLK_SEL | PIPE3_PHYSTATUS_SW);
472
473 clrbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG,
474 PIPE_UTMI_CLK_DIS);
475}
476
477static void dwc3_qcom_glue_configure(struct udevice *dev, int index,
478 enum usb_dr_mode mode)
479{
480 struct dwc3_glue_data *glue = dev_get_plat(dev);
481 void __iomem *qscratch_base = map_physmem(glue->regs, 0x400, MAP_NOCACHE);
482 if (IS_ERR_OR_NULL(qscratch_base)) {
483 log_err("%s: Invalid qscratch base address\n", dev->name);
484 return;
485 }
486
487 if (dev_read_bool(dev, "qcom,select-utmi-as-pipe-clk"))
488 dwc3_qcom_select_utmi_clk(qscratch_base);
489
490 if (mode != USB_DR_MODE_HOST)
491 dwc3_qcom_vbus_override_enable(qscratch_base, true);
492}
493
494struct dwc3_glue_ops qcom_ops = {
495 .glue_configure = dwc3_qcom_glue_configure,
496};
497
Jonas Karlman04c6ae82023-07-30 22:59:57 +0000498static int dwc3_rk_glue_get_ctrl_dev(struct udevice *dev, ofnode *node)
499{
500 *node = dev_ofnode(dev);
501 if (!ofnode_valid(*node))
502 return -EINVAL;
503
504 return 0;
505}
506
507struct dwc3_glue_ops rk_ops = {
508 .glue_get_ctrl_dev = dwc3_rk_glue_get_ctrl_dev,
509};
510
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900511static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200512{
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900513 const char *name = ofnode_get_name(node);
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000514 const char *driver;
Angus Ainslie6e382a82022-02-02 15:08:54 -0800515 enum usb_dr_mode dr_mode;
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900516 struct udevice *dev;
517 int ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200518
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900519 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200520
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900521 /* if the parent node doesn't have a mode check the leaf */
522 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
523 if (!dr_mode)
524 dr_mode = usb_get_dr_mode(node);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200525
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000526 if (CONFIG_IS_ENABLED(DM_USB_GADGET) &&
527 (dr_mode == USB_DR_MODE_PERIPHERAL || dr_mode == USB_DR_MODE_OTG)) {
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900528 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
529 driver = "dwc3-generic-peripheral";
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000530 } else if (CONFIG_IS_ENABLED(USB_HOST) && dr_mode == USB_DR_MODE_HOST) {
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900531 debug("%s: dr_mode: HOST\n", __func__);
532 driver = "dwc3-generic-host";
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000533 } else {
534 debug("%s: unsupported dr_mode %d\n", __func__, dr_mode);
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900535 return -ENODEV;
Jonas Karlman5e8b61b2023-07-30 22:59:56 +0000536 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100537
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900538 ret = device_bind_driver_to_node(parent, driver, name,
539 node, &dev);
540 if (ret) {
541 debug("%s: not able to bind usb device mode\n",
542 __func__);
543 return ret;
544 }
545
546 return 0;
547}
548
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +0900549int dwc3_glue_bind(struct udevice *parent)
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900550{
551 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent);
552 ofnode node;
553 int ret;
554
555 if (ops && ops->glue_get_ctrl_dev) {
556 ret = ops->glue_get_ctrl_dev(parent, &node);
557 if (ret)
558 return ret;
559
560 return dwc3_glue_bind_common(parent, node);
561 }
562
563 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
564 ret = dwc3_glue_bind_common(parent, node);
565 if (ret == -ENXIO)
566 continue;
567 if (ret)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200568 return ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200569 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100570
571 return 0;
572}
573
574static int dwc3_glue_reset_init(struct udevice *dev,
575 struct dwc3_glue_data *glue)
576{
577 int ret;
578
579 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530580 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100581 return 0;
582 else if (ret)
583 return ret;
584
Caleb Connolly60d1fc22024-03-20 14:30:47 +0000585 if (device_is_compatible(dev, "qcom,dwc3")) {
586 reset_assert_bulk(&glue->resets);
587 /* We should wait at least 6 sleep clock cycles, that's
588 * (6 / 32764) * 1000000 ~= 200us. But some platforms
589 * have slower sleep clocks so we'll play it safe.
590 */
591 udelay(500);
592 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100593 ret = reset_deassert_bulk(&glue->resets);
594 if (ret) {
595 reset_release_bulk(&glue->resets);
596 return ret;
597 }
598
599 return 0;
600}
601
602static int dwc3_glue_clk_init(struct udevice *dev,
603 struct dwc3_glue_data *glue)
604{
605 int ret;
606
607 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530608 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100609 return 0;
610 if (ret)
611 return ret;
612
613#if CONFIG_IS_ENABLED(CLK)
614 ret = clk_enable_bulk(&glue->clks);
615 if (ret) {
616 clk_release_bulk(&glue->clks);
617 return ret;
618 }
619#endif
620
621 return 0;
622}
623
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +0900624int dwc3_glue_probe(struct udevice *dev)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100625{
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100626 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700627 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100628 struct udevice *child = NULL;
629 int index = 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100630 int ret;
Michal Simekbb19d622022-03-09 10:05:45 +0100631 struct phy phy;
632
633 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
634 if (!ret) {
635 ret = generic_phy_init(&phy);
636 if (ret)
637 return ret;
Jan Kiszka2fe2cf02022-04-25 13:26:45 +0200638 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simekbb19d622022-03-09 10:05:45 +0100639 debug("could not get phy (err %d)\n", ret);
640 return ret;
641 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100642
Kunihiko Hayashi54c277e2023-02-20 14:50:29 +0900643 glue->regs = dev_read_addr_size_index(dev, 0, &glue->size);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100644
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100645 ret = dwc3_glue_clk_init(dev, glue);
646 if (ret)
647 return ret;
648
649 ret = dwc3_glue_reset_init(dev, glue);
650 if (ret)
651 return ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200652
Jonas Karlmanfd5c3c22023-08-31 22:16:36 +0000653 if (generic_phy_valid(&phy)) {
Michal Simekbb19d622022-03-09 10:05:45 +0100654 ret = generic_phy_power_on(&phy);
655 if (ret)
656 return ret;
657 }
658
Jonas Karlmanee1e0702023-07-30 22:59:55 +0000659 device_find_first_child(dev, &child);
660 if (!child)
661 return 0;
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100662
Kunihiko Hayashidf0f5d32023-02-20 14:50:27 +0900663 if (glue->clks.count == 0) {
664 ret = dwc3_glue_clk_init(child, glue);
665 if (ret)
666 return ret;
667 }
668
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800669 if (glue->resets.count == 0) {
670 ret = dwc3_glue_reset_init(child, glue);
671 if (ret)
672 return ret;
673 }
674
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100675 while (child) {
676 enum usb_dr_mode dr_mode;
677
Simon Glassa7ece582020-12-19 10:40:14 -0700678 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100679 device_find_next_child(&child);
Marek Vasut68c86562022-04-13 00:42:55 +0200680 if (ops && ops->glue_configure)
681 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100682 index++;
683 }
684
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200685 return 0;
686}
687
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +0900688int dwc3_glue_remove(struct udevice *dev)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100689{
Simon Glassfa20e932020-12-03 16:55:20 -0700690 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100691
692 reset_release_bulk(&glue->resets);
693
694 clk_release_bulk(&glue->clks);
695
Jean-Jacques Hiblot5a945572019-07-05 09:33:56 +0200696 return 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100697}
698
699static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200700 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu1eb3c302020-05-12 08:36:01 +0200701 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot3e0684b2018-12-04 11:12:56 +0100702 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100703 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblotca848df2018-12-04 11:30:50 +0100704 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendrac6282952019-12-09 10:37:29 +0530705 { .compatible = "ti,am654-dwc3" },
Jagan Tekie5b93412023-06-06 22:39:14 +0530706 { .compatible = "rockchip,rk3328-dwc3", .data = (ulong)&rk_ops },
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800707 { .compatible = "rockchip,rk3399-dwc3" },
Jonas Karlman04c6ae82023-07-30 22:59:57 +0000708 { .compatible = "rockchip,rk3568-dwc3", .data = (ulong)&rk_ops },
Jonas Karlman39076d92023-11-12 15:25:25 +0000709 { .compatible = "rockchip,rk3588-dwc3", .data = (ulong)&rk_ops },
Caleb Connolly60d1fc22024-03-20 14:30:47 +0000710 { .compatible = "qcom,dwc3", .data = (ulong)&qcom_ops },
Marek Vasutae219342022-04-13 00:42:56 +0200711 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainslie6e382a82022-02-02 15:08:54 -0800712 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko221d7fa2020-12-03 19:45:01 +0200713 { .compatible = "intel,tangier-dwc3" },
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200714 { }
715};
716
717U_BOOT_DRIVER(dwc3_generic_wrapper) = {
718 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblotb49b5c22019-07-05 09:33:58 +0200719 .id = UCLASS_NOP,
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100720 .of_match = dwc3_glue_ids,
721 .bind = dwc3_glue_bind,
722 .probe = dwc3_glue_probe,
723 .remove = dwc3_glue_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -0700724 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100725
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200726};