blob: 66da5a8d6f8c2b71c443205a48f722ec9f179282 [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
10#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020013#include <dm.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010016#include <dwc3-uboot.h>
Michal Simekbb19d622022-03-09 10:05:45 +010017#include <generic-phy.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Frank Wangf5a6c5b2020-05-26 11:34:31 +080019#include <linux/delay.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020020#include <linux/usb/ch9.h>
21#include <linux/usb/gadget.h>
22#include <malloc.h>
23#include <usb.h>
24#include "core.h"
25#include "gadget.h"
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010026#include <reset.h>
27#include <clk.h>
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020028#include <usb/xhci.h>
T Karthik Reddyf7adf892022-07-08 11:21:59 +020029#include <asm/gpio.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020030
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +090031#include "dwc3-generic.h"
Frank Wangf5a6c5b2020-05-26 11:34:31 +080032
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020033struct dwc3_generic_plat {
34 fdt_addr_t base;
35 u32 maximum_speed;
36 enum usb_dr_mode dr_mode;
37};
38
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020039struct dwc3_generic_priv {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020040 void *base;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010041 struct dwc3 dwc3;
developerf8bced12020-05-02 11:35:13 +020042 struct phy_bulk phys;
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +053043 struct gpio_desc *ulpi_reset;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010044};
45
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020046struct dwc3_generic_host_priv {
47 struct xhci_ctrl xhci_ctrl;
48 struct dwc3_generic_priv gen_priv;
49};
50
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020051static int dwc3_generic_probe(struct udevice *dev,
52 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020053{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010054 int rc;
Simon Glassfa20e932020-12-03 16:55:20 -070055 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010056 struct dwc3 *dwc3 = &priv->dwc3;
Simon Glassfa20e932020-12-03 16:55:20 -070057 struct dwc3_glue_data *glue = dev_get_plat(dev->parent);
Marek Vasutaacbcb62022-11-27 15:31:52 +010058 int __maybe_unused index;
59 ofnode __maybe_unused node;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010060
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020061 dwc3->dev = dev;
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020062 dwc3->maximum_speed = plat->maximum_speed;
63 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020064#if CONFIG_IS_ENABLED(OF_CONTROL)
65 dwc3_of_parse(dwc3);
Marek Vasutaacbcb62022-11-27 15:31:52 +010066
Marek Vasut4d285722023-02-20 14:50:25 +090067 /*
68 * There are currently four disparate placement possibilities of DWC3
69 * reference clock phandle in SoC DTs:
70 * - in top level glue node, with generic subnode without clock (ZynqMP)
71 * - in top level generic node, with no subnode (i.MX8MQ)
72 * - in generic subnode, with other clock in top level node (i.MX8MP)
73 * - in both top level node and generic subnode (Rockchip)
74 * Cover all the possibilities here by looking into both nodes, start
75 * with the top level node as that seems to be used in majority of DTs
76 * to reference the clock.
77 */
Marek Vasutaacbcb62022-11-27 15:31:52 +010078 node = dev_ofnode(dev->parent);
79 index = ofnode_stringlist_search(node, "clock-names", "ref");
80 if (index < 0)
81 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
Marek Vasut4d285722023-02-20 14:50:25 +090082 if (index < 0) {
83 node = dev_ofnode(dev);
84 index = ofnode_stringlist_search(node, "clock-names", "ref");
85 if (index < 0)
86 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
87 }
Marek Vasutaacbcb62022-11-27 15:31:52 +010088 if (index >= 0)
89 dwc3->ref_clk = &glue->clks.clks[index];
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020090#endif
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020091
Frank Wangf5a6c5b2020-05-26 11:34:31 +080092 /*
93 * It must hold whole USB3.0 OTG controller in resetting to hold pipe
94 * power state in P2 before initializing TypeC PHY on RK3399 platform.
95 */
96 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) {
97 reset_assert_bulk(&glue->resets);
98 udelay(1);
99 }
100
developerf8bced12020-05-02 11:35:13 +0200101 rc = dwc3_setup_phy(dev, &priv->phys);
Siva Durga Prasad Paladuguc37f8f32020-10-21 14:17:31 +0200102 if (rc && rc != -ENOTSUPP)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100103 return rc;
104
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200105 if (CONFIG_IS_ENABLED(DM_GPIO) &&
106 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530107 priv->ulpi_reset = devm_gpiod_get_optional(dev->parent, "reset",
108 GPIOD_ACTIVE_LOW);
109 /* property is optional, don't return error! */
110 if (priv->ulpi_reset) {
111 /* Toggle ulpi to reset the phy. */
112 rc = dm_gpio_set_value(priv->ulpi_reset, 1);
113 if (rc)
114 return rc;
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200115
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530116 mdelay(5);
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200117
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530118 rc = dm_gpio_set_value(priv->ulpi_reset, 0);
119 if (rc)
120 return rc;
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200121
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530122 mdelay(5);
123 }
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200124 }
125
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800126 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3"))
127 reset_deassert_bulk(&glue->resets);
128
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200129 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
130 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +0200131
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100132
133 rc = dwc3_init(dwc3);
134 if (rc) {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200135 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100136 return rc;
137 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200138
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100139 return 0;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200140}
141
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200142static int dwc3_generic_remove(struct udevice *dev,
143 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200144{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100145 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200146
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200147 if (CONFIG_IS_ENABLED(DM_GPIO) &&
148 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
Venkatesh Yadav Abbarapu5f70b0a2023-01-13 10:42:02 +0530149 struct gpio_desc *ulpi_reset = priv->ulpi_reset;
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200150
151 dm_gpio_free(ulpi_reset->dev, ulpi_reset);
152 }
153
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100154 dwc3_remove(dwc3);
developerf8bced12020-05-02 11:35:13 +0200155 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100156 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200157
158 return 0;
159}
160
Simon Glassaad29ae2020-12-03 16:55:21 -0700161static int dwc3_generic_of_to_plat(struct udevice *dev)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200162{
Simon Glassfa20e932020-12-03 16:55:20 -0700163 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Simon Glassa7ece582020-12-19 10:40:14 -0700164 ofnode node = dev_ofnode(dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200165
Angus Ainslie6e382a82022-02-02 15:08:54 -0800166 if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) {
167 /* This is a leaf so check the parent */
168 plat->base = dev_read_addr(dev->parent);
169 } else {
170 plat->base = dev_read_addr(dev);
171 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200172
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200173 plat->maximum_speed = usb_get_maximum_speed(node);
174 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot547df0d2019-09-11 11:33:51 +0200175 pr_info("No USB maximum speed specified. Using super speed\n");
176 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200177 }
178
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200179 plat->dr_mode = usb_get_dr_mode(node);
180 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Angus Ainslie6e382a82022-02-02 15:08:54 -0800181 /* might be a leaf so check the parent for mode */
182 node = dev_ofnode(dev->parent);
183 plat->dr_mode = usb_get_dr_mode(node);
184 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
185 pr_err("Invalid usb mode setup\n");
186 return -ENODEV;
187 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200188 }
189
190 return 0;
191}
192
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200193#if CONFIG_IS_ENABLED(DM_USB_GADGET)
194int dm_usb_gadget_handle_interrupts(struct udevice *dev)
195{
196 struct dwc3_generic_priv *priv = dev_get_priv(dev);
197 struct dwc3 *dwc3 = &priv->dwc3;
198
199 dwc3_gadget_uboot_handle_interrupt(dwc3);
200
201 return 0;
202}
203
204static int dwc3_generic_peripheral_probe(struct udevice *dev)
205{
206 struct dwc3_generic_priv *priv = dev_get_priv(dev);
207
208 return dwc3_generic_probe(dev, priv);
209}
210
211static int dwc3_generic_peripheral_remove(struct udevice *dev)
212{
213 struct dwc3_generic_priv *priv = dev_get_priv(dev);
214
215 return dwc3_generic_remove(dev, priv);
216}
217
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200218U_BOOT_DRIVER(dwc3_generic_peripheral) = {
219 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100220 .id = UCLASS_USB_GADGET_GENERIC,
Simon Glassaad29ae2020-12-03 16:55:21 -0700221 .of_to_plat = dwc3_generic_of_to_plat,
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200222 .probe = dwc3_generic_peripheral_probe,
223 .remove = dwc3_generic_peripheral_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700224 .priv_auto = sizeof(struct dwc3_generic_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700225 .plat_auto = sizeof(struct dwc3_generic_plat),
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200226};
Jean-Jacques Hiblot44aaec72018-11-29 10:52:42 +0100227#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200228
Simon Glass1f2440c2021-07-10 21:14:29 -0600229#if defined(CONFIG_SPL_USB_HOST) || \
Kunihiko Hayashib032aa62021-05-12 23:11:14 +0900230 !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST)
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200231static int dwc3_generic_host_probe(struct udevice *dev)
232{
233 struct xhci_hcor *hcor;
234 struct xhci_hccr *hccr;
235 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
236 int rc;
237
238 rc = dwc3_generic_probe(dev, &priv->gen_priv);
239 if (rc)
240 return rc;
241
242 hccr = (struct xhci_hccr *)priv->gen_priv.base;
243 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
244 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
245
246 return xhci_register(dev, hccr, hcor);
247}
248
249static int dwc3_generic_host_remove(struct udevice *dev)
250{
251 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
252 int rc;
253
254 rc = xhci_deregister(dev);
255 if (rc)
256 return rc;
257
258 return dwc3_generic_remove(dev, &priv->gen_priv);
259}
260
261U_BOOT_DRIVER(dwc3_generic_host) = {
262 .name = "dwc3-generic-host",
263 .id = UCLASS_USB,
Simon Glassaad29ae2020-12-03 16:55:21 -0700264 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200265 .probe = dwc3_generic_host_probe,
266 .remove = dwc3_generic_host_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700267 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700268 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200269 .ops = &xhci_usb_ops,
270 .flags = DM_FLAG_ALLOC_PRIV_DMA,
271};
272#endif
273
Marek Vasutae219342022-04-13 00:42:56 +0200274void dwc3_imx8mp_glue_configure(struct udevice *dev, int index,
275 enum usb_dr_mode mode)
276{
277/* USB glue registers */
278#define USB_CTRL0 0x00
279#define USB_CTRL1 0x04
280
281#define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
282#define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
283#define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
284
285#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
286#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
287 fdt_addr_t regs = dev_read_addr_index(dev, 1);
288 void *base = map_physmem(regs, 0x8, MAP_NOCACHE);
289 u32 value;
290
291 value = readl(base + USB_CTRL0);
292
293 if (dev_read_bool(dev, "fsl,permanently-attached"))
294 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
295 else
296 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
297
298 if (dev_read_bool(dev, "fsl,disable-port-power-control"))
299 value &= ~(USB_CTRL0_PORTPWR_EN);
300 else
301 value |= USB_CTRL0_PORTPWR_EN;
302
303 writel(value, base + USB_CTRL0);
304
305 value = readl(base + USB_CTRL1);
306 if (dev_read_bool(dev, "fsl,over-current-active-low"))
307 value |= USB_CTRL1_OC_POLARITY;
308 else
309 value &= ~USB_CTRL1_OC_POLARITY;
310
311 if (dev_read_bool(dev, "fsl,power-active-low"))
312 value |= USB_CTRL1_PWR_POLARITY;
313 else
314 value &= ~USB_CTRL1_PWR_POLARITY;
315
316 writel(value, base + USB_CTRL1);
317
318 unmap_physmem(base, MAP_NOCACHE);
319}
320
321struct dwc3_glue_ops imx8mp_ops = {
322 .glue_configure = dwc3_imx8mp_glue_configure,
323};
324
Marek Vasut68c86562022-04-13 00:42:55 +0200325void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100326 enum usb_dr_mode mode)
327{
328#define USBOTGSS_UTMI_OTG_STATUS 0x0084
329#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
330
331/* UTMI_OTG_STATUS REGISTER */
332#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
333#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
334#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
335#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
336#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
337#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
338#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
339enum dwc3_omap_utmi_mode {
340 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
341 DWC3_OMAP_UTMI_MODE_HW,
342 DWC3_OMAP_UTMI_MODE_SW,
343};
344
345 u32 use_id_pin;
346 u32 host_mode;
347 u32 reg;
348 u32 utmi_mode;
349 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
350
Simon Glassfa20e932020-12-03 16:55:20 -0700351 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100352 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
353
354 if (device_is_compatible(dev, "ti,am437x-dwc3"))
355 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
356
357 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
358 DWC3_OMAP_UTMI_MODE_UNKNOWN);
359 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
360 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
361 dev->name);
362 mode = USB_DR_MODE_PERIPHERAL;
363 }
364
365 switch (mode) {
366 case USB_DR_MODE_PERIPHERAL:
367 use_id_pin = 0;
368 host_mode = 0;
369 break;
370 case USB_DR_MODE_HOST:
371 use_id_pin = 0;
372 host_mode = 1;
373 break;
374 case USB_DR_MODE_OTG:
375 default:
376 use_id_pin = 1;
377 host_mode = 0;
378 break;
379 }
380
381 reg = readl(base + utmi_status_offset);
382
383 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
384 if (!use_id_pin)
385 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
386
387 writel(reg, base + utmi_status_offset);
388
389 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
390 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
391 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
392
393 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
394 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
395
396 if (!host_mode)
397 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
398 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
399
400 writel(reg, base + utmi_status_offset);
401
402 unmap_physmem(base, MAP_NOCACHE);
403}
404
405struct dwc3_glue_ops ti_ops = {
Marek Vasut68c86562022-04-13 00:42:55 +0200406 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100407};
408
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900409static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200410{
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900411 const char *name = ofnode_get_name(node);
412 const char *driver = NULL;
Angus Ainslie6e382a82022-02-02 15:08:54 -0800413 enum usb_dr_mode dr_mode;
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900414 struct udevice *dev;
415 int ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200416
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900417 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200418
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900419 /* if the parent node doesn't have a mode check the leaf */
420 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
421 if (!dr_mode)
422 dr_mode = usb_get_dr_mode(node);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200423
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900424 switch (dr_mode) {
425 case USB_DR_MODE_PERIPHERAL:
426 case USB_DR_MODE_OTG:
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100427#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900428 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
429 driver = "dwc3-generic-peripheral";
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100430#endif
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900431 break;
Simon Glass1f2440c2021-07-10 21:14:29 -0600432#if defined(CONFIG_SPL_USB_HOST) || !defined(CONFIG_SPL_BUILD)
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900433 case USB_DR_MODE_HOST:
434 debug("%s: dr_mode: HOST\n", __func__);
435 driver = "dwc3-generic-host";
436 break;
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200437#endif
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900438 default:
439 debug("%s: unsupported dr_mode\n", __func__);
440 return -ENODEV;
441 };
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200442
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900443 if (!driver)
444 return -ENXIO;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100445
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900446 ret = device_bind_driver_to_node(parent, driver, name,
447 node, &dev);
448 if (ret) {
449 debug("%s: not able to bind usb device mode\n",
450 __func__);
451 return ret;
452 }
453
454 return 0;
455}
456
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +0900457int dwc3_glue_bind(struct udevice *parent)
Kunihiko Hayashi8c420372023-02-20 14:50:26 +0900458{
459 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent);
460 ofnode node;
461 int ret;
462
463 if (ops && ops->glue_get_ctrl_dev) {
464 ret = ops->glue_get_ctrl_dev(parent, &node);
465 if (ret)
466 return ret;
467
468 return dwc3_glue_bind_common(parent, node);
469 }
470
471 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
472 ret = dwc3_glue_bind_common(parent, node);
473 if (ret == -ENXIO)
474 continue;
475 if (ret)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200476 return ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200477 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100478
479 return 0;
480}
481
482static int dwc3_glue_reset_init(struct udevice *dev,
483 struct dwc3_glue_data *glue)
484{
485 int ret;
486
487 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530488 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100489 return 0;
490 else if (ret)
491 return ret;
492
493 ret = reset_deassert_bulk(&glue->resets);
494 if (ret) {
495 reset_release_bulk(&glue->resets);
496 return ret;
497 }
498
499 return 0;
500}
501
502static int dwc3_glue_clk_init(struct udevice *dev,
503 struct dwc3_glue_data *glue)
504{
505 int ret;
506
507 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530508 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100509 return 0;
510 if (ret)
511 return ret;
512
513#if CONFIG_IS_ENABLED(CLK)
514 ret = clk_enable_bulk(&glue->clks);
515 if (ret) {
516 clk_release_bulk(&glue->clks);
517 return ret;
518 }
519#endif
520
521 return 0;
522}
523
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +0900524int dwc3_glue_probe(struct udevice *dev)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100525{
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100526 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700527 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100528 struct udevice *child = NULL;
529 int index = 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100530 int ret;
Michal Simekbb19d622022-03-09 10:05:45 +0100531 struct phy phy;
532
533 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
534 if (!ret) {
535 ret = generic_phy_init(&phy);
536 if (ret)
537 return ret;
Jan Kiszka2fe2cf02022-04-25 13:26:45 +0200538 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simekbb19d622022-03-09 10:05:45 +0100539 debug("could not get phy (err %d)\n", ret);
540 return ret;
Jan Kiszka2fe2cf02022-04-25 13:26:45 +0200541 } else {
542 phy.dev = NULL;
Michal Simekbb19d622022-03-09 10:05:45 +0100543 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100544
Kunihiko Hayashi54c277e2023-02-20 14:50:29 +0900545 glue->regs = dev_read_addr_size_index(dev, 0, &glue->size);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100546
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100547 ret = dwc3_glue_clk_init(dev, glue);
548 if (ret)
549 return ret;
550
551 ret = dwc3_glue_reset_init(dev, glue);
552 if (ret)
553 return ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200554
Michal Simekbb19d622022-03-09 10:05:45 +0100555 if (phy.dev) {
556 ret = generic_phy_power_on(&phy);
557 if (ret)
558 return ret;
559 }
560
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100561 ret = device_find_first_child(dev, &child);
562 if (ret)
563 return ret;
564
Kunihiko Hayashidf0f5d32023-02-20 14:50:27 +0900565 if (glue->clks.count == 0) {
566 ret = dwc3_glue_clk_init(child, glue);
567 if (ret)
568 return ret;
569 }
570
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800571 if (glue->resets.count == 0) {
572 ret = dwc3_glue_reset_init(child, glue);
573 if (ret)
574 return ret;
575 }
576
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100577 while (child) {
578 enum usb_dr_mode dr_mode;
579
Simon Glassa7ece582020-12-19 10:40:14 -0700580 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100581 device_find_next_child(&child);
Marek Vasut68c86562022-04-13 00:42:55 +0200582 if (ops && ops->glue_configure)
583 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100584 index++;
585 }
586
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200587 return 0;
588}
589
Kunihiko Hayashi6cf357e2023-02-20 14:50:28 +0900590int dwc3_glue_remove(struct udevice *dev)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100591{
Simon Glassfa20e932020-12-03 16:55:20 -0700592 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100593
594 reset_release_bulk(&glue->resets);
595
596 clk_release_bulk(&glue->clks);
597
Jean-Jacques Hiblot5a945572019-07-05 09:33:56 +0200598 return 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100599}
600
601static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200602 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu1eb3c302020-05-12 08:36:01 +0200603 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot3e0684b2018-12-04 11:12:56 +0100604 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100605 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblotca848df2018-12-04 11:30:50 +0100606 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendrac6282952019-12-09 10:37:29 +0530607 { .compatible = "ti,am654-dwc3" },
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800608 { .compatible = "rockchip,rk3328-dwc3" },
609 { .compatible = "rockchip,rk3399-dwc3" },
Robert Marko746862b2020-09-10 16:00:05 +0200610 { .compatible = "qcom,dwc3" },
Marek Vasutae219342022-04-13 00:42:56 +0200611 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainslie6e382a82022-02-02 15:08:54 -0800612 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko221d7fa2020-12-03 19:45:01 +0200613 { .compatible = "intel,tangier-dwc3" },
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200614 { }
615};
616
617U_BOOT_DRIVER(dwc3_generic_wrapper) = {
618 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblotb49b5c22019-07-05 09:33:58 +0200619 .id = UCLASS_NOP,
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100620 .of_match = dwc3_glue_ids,
621 .bind = dwc3_glue_bind,
622 .probe = dwc3_glue_probe,
623 .remove = dwc3_glue_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -0700624 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100625
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200626};