blob: 42e92478f25a8fcbdec484240f39a255a6b368b3 [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
Frank Wangf5a6c5b2020-05-26 11:34:31 +080031struct dwc3_glue_data {
32 struct clk_bulk clks;
33 struct reset_ctl_bulk resets;
34 fdt_addr_t regs;
35};
36
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020037struct dwc3_generic_plat {
38 fdt_addr_t base;
39 u32 maximum_speed;
40 enum usb_dr_mode dr_mode;
41};
42
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020043struct dwc3_generic_priv {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020044 void *base;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010045 struct dwc3 dwc3;
developerf8bced12020-05-02 11:35:13 +020046 struct phy_bulk phys;
T Karthik Reddyf7adf892022-07-08 11:21:59 +020047 struct gpio_desc ulpi_reset;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010048};
49
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020050struct dwc3_generic_host_priv {
51 struct xhci_ctrl xhci_ctrl;
52 struct dwc3_generic_priv gen_priv;
53};
54
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020055static int dwc3_generic_probe(struct udevice *dev,
56 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020057{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010058 int rc;
Simon Glassfa20e932020-12-03 16:55:20 -070059 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010060 struct dwc3 *dwc3 = &priv->dwc3;
Simon Glassfa20e932020-12-03 16:55:20 -070061 struct dwc3_glue_data *glue = dev_get_plat(dev->parent);
Marek Vasutaacbcb62022-11-27 15:31:52 +010062 int __maybe_unused index;
63 ofnode __maybe_unused node;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010064
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020065 dwc3->dev = dev;
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020066 dwc3->maximum_speed = plat->maximum_speed;
67 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020068#if CONFIG_IS_ENABLED(OF_CONTROL)
69 dwc3_of_parse(dwc3);
Marek Vasutaacbcb62022-11-27 15:31:52 +010070
Marek Vasut4d285722023-02-20 14:50:25 +090071 /*
72 * There are currently four disparate placement possibilities of DWC3
73 * reference clock phandle in SoC DTs:
74 * - in top level glue node, with generic subnode without clock (ZynqMP)
75 * - in top level generic node, with no subnode (i.MX8MQ)
76 * - in generic subnode, with other clock in top level node (i.MX8MP)
77 * - in both top level node and generic subnode (Rockchip)
78 * Cover all the possibilities here by looking into both nodes, start
79 * with the top level node as that seems to be used in majority of DTs
80 * to reference the clock.
81 */
Marek Vasutaacbcb62022-11-27 15:31:52 +010082 node = dev_ofnode(dev->parent);
83 index = ofnode_stringlist_search(node, "clock-names", "ref");
84 if (index < 0)
85 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
Marek Vasut4d285722023-02-20 14:50:25 +090086 if (index < 0) {
87 node = dev_ofnode(dev);
88 index = ofnode_stringlist_search(node, "clock-names", "ref");
89 if (index < 0)
90 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
91 }
Marek Vasutaacbcb62022-11-27 15:31:52 +010092 if (index >= 0)
93 dwc3->ref_clk = &glue->clks.clks[index];
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020094#endif
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020095
Frank Wangf5a6c5b2020-05-26 11:34:31 +080096 /*
97 * It must hold whole USB3.0 OTG controller in resetting to hold pipe
98 * power state in P2 before initializing TypeC PHY on RK3399 platform.
99 */
100 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) {
101 reset_assert_bulk(&glue->resets);
102 udelay(1);
103 }
104
developerf8bced12020-05-02 11:35:13 +0200105 rc = dwc3_setup_phy(dev, &priv->phys);
Siva Durga Prasad Paladuguc37f8f32020-10-21 14:17:31 +0200106 if (rc && rc != -ENOTSUPP)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100107 return rc;
108
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200109 if (CONFIG_IS_ENABLED(DM_GPIO) &&
110 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
111 rc = gpio_request_by_name(dev->parent, "reset-gpios", 0,
112 &priv->ulpi_reset, GPIOD_ACTIVE_LOW);
113 if (rc)
114 return rc;
115
116 /* Toggle ulpi to reset the phy. */
117 rc = dm_gpio_set_value(&priv->ulpi_reset, 1);
118 if (rc)
119 return rc;
120
121 mdelay(5);
122
123 rc = dm_gpio_set_value(&priv->ulpi_reset, 0);
124 if (rc)
125 return rc;
126
127 mdelay(5);
128 }
129
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800130 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3"))
131 reset_deassert_bulk(&glue->resets);
132
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200133 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
134 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +0200135
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100136
137 rc = dwc3_init(dwc3);
138 if (rc) {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200139 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100140 return rc;
141 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200142
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100143 return 0;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200144}
145
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200146static int dwc3_generic_remove(struct udevice *dev,
147 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200148{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100149 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200150
T Karthik Reddyf7adf892022-07-08 11:21:59 +0200151 if (CONFIG_IS_ENABLED(DM_GPIO) &&
152 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
153 struct gpio_desc *ulpi_reset = &priv->ulpi_reset;
154
155 dm_gpio_free(ulpi_reset->dev, ulpi_reset);
156 }
157
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100158 dwc3_remove(dwc3);
developerf8bced12020-05-02 11:35:13 +0200159 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100160 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200161
162 return 0;
163}
164
Simon Glassaad29ae2020-12-03 16:55:21 -0700165static int dwc3_generic_of_to_plat(struct udevice *dev)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200166{
Simon Glassfa20e932020-12-03 16:55:20 -0700167 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Simon Glassa7ece582020-12-19 10:40:14 -0700168 ofnode node = dev_ofnode(dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200169
Angus Ainslie6e382a82022-02-02 15:08:54 -0800170 if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) {
171 /* This is a leaf so check the parent */
172 plat->base = dev_read_addr(dev->parent);
173 } else {
174 plat->base = dev_read_addr(dev);
175 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200176
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200177 plat->maximum_speed = usb_get_maximum_speed(node);
178 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot547df0d2019-09-11 11:33:51 +0200179 pr_info("No USB maximum speed specified. Using super speed\n");
180 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200181 }
182
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200183 plat->dr_mode = usb_get_dr_mode(node);
184 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Angus Ainslie6e382a82022-02-02 15:08:54 -0800185 /* might be a leaf so check the parent for mode */
186 node = dev_ofnode(dev->parent);
187 plat->dr_mode = usb_get_dr_mode(node);
188 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
189 pr_err("Invalid usb mode setup\n");
190 return -ENODEV;
191 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200192 }
193
194 return 0;
195}
196
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200197#if CONFIG_IS_ENABLED(DM_USB_GADGET)
198int dm_usb_gadget_handle_interrupts(struct udevice *dev)
199{
200 struct dwc3_generic_priv *priv = dev_get_priv(dev);
201 struct dwc3 *dwc3 = &priv->dwc3;
202
203 dwc3_gadget_uboot_handle_interrupt(dwc3);
204
205 return 0;
206}
207
208static int dwc3_generic_peripheral_probe(struct udevice *dev)
209{
210 struct dwc3_generic_priv *priv = dev_get_priv(dev);
211
212 return dwc3_generic_probe(dev, priv);
213}
214
215static int dwc3_generic_peripheral_remove(struct udevice *dev)
216{
217 struct dwc3_generic_priv *priv = dev_get_priv(dev);
218
219 return dwc3_generic_remove(dev, priv);
220}
221
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200222U_BOOT_DRIVER(dwc3_generic_peripheral) = {
223 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100224 .id = UCLASS_USB_GADGET_GENERIC,
Simon Glassaad29ae2020-12-03 16:55:21 -0700225 .of_to_plat = dwc3_generic_of_to_plat,
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200226 .probe = dwc3_generic_peripheral_probe,
227 .remove = dwc3_generic_peripheral_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700228 .priv_auto = sizeof(struct dwc3_generic_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700229 .plat_auto = sizeof(struct dwc3_generic_plat),
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200230};
Jean-Jacques Hiblot44aaec72018-11-29 10:52:42 +0100231#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200232
Simon Glass1f2440c2021-07-10 21:14:29 -0600233#if defined(CONFIG_SPL_USB_HOST) || \
Kunihiko Hayashib032aa62021-05-12 23:11:14 +0900234 !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST)
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200235static int dwc3_generic_host_probe(struct udevice *dev)
236{
237 struct xhci_hcor *hcor;
238 struct xhci_hccr *hccr;
239 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
240 int rc;
241
242 rc = dwc3_generic_probe(dev, &priv->gen_priv);
243 if (rc)
244 return rc;
245
246 hccr = (struct xhci_hccr *)priv->gen_priv.base;
247 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
248 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
249
250 return xhci_register(dev, hccr, hcor);
251}
252
253static int dwc3_generic_host_remove(struct udevice *dev)
254{
255 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
256 int rc;
257
258 rc = xhci_deregister(dev);
259 if (rc)
260 return rc;
261
262 return dwc3_generic_remove(dev, &priv->gen_priv);
263}
264
265U_BOOT_DRIVER(dwc3_generic_host) = {
266 .name = "dwc3-generic-host",
267 .id = UCLASS_USB,
Simon Glassaad29ae2020-12-03 16:55:21 -0700268 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200269 .probe = dwc3_generic_host_probe,
270 .remove = dwc3_generic_host_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700271 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700272 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200273 .ops = &xhci_usb_ops,
274 .flags = DM_FLAG_ALLOC_PRIV_DMA,
275};
276#endif
277
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100278struct dwc3_glue_ops {
Marek Vasut68c86562022-04-13 00:42:55 +0200279 void (*glue_configure)(struct udevice *dev, int index,
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100280 enum usb_dr_mode mode);
281};
282
Marek Vasutae219342022-04-13 00:42:56 +0200283void dwc3_imx8mp_glue_configure(struct udevice *dev, int index,
284 enum usb_dr_mode mode)
285{
286/* USB glue registers */
287#define USB_CTRL0 0x00
288#define USB_CTRL1 0x04
289
290#define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
291#define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
292#define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
293
294#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
295#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
296 fdt_addr_t regs = dev_read_addr_index(dev, 1);
297 void *base = map_physmem(regs, 0x8, MAP_NOCACHE);
298 u32 value;
299
300 value = readl(base + USB_CTRL0);
301
302 if (dev_read_bool(dev, "fsl,permanently-attached"))
303 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
304 else
305 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
306
307 if (dev_read_bool(dev, "fsl,disable-port-power-control"))
308 value &= ~(USB_CTRL0_PORTPWR_EN);
309 else
310 value |= USB_CTRL0_PORTPWR_EN;
311
312 writel(value, base + USB_CTRL0);
313
314 value = readl(base + USB_CTRL1);
315 if (dev_read_bool(dev, "fsl,over-current-active-low"))
316 value |= USB_CTRL1_OC_POLARITY;
317 else
318 value &= ~USB_CTRL1_OC_POLARITY;
319
320 if (dev_read_bool(dev, "fsl,power-active-low"))
321 value |= USB_CTRL1_PWR_POLARITY;
322 else
323 value &= ~USB_CTRL1_PWR_POLARITY;
324
325 writel(value, base + USB_CTRL1);
326
327 unmap_physmem(base, MAP_NOCACHE);
328}
329
330struct dwc3_glue_ops imx8mp_ops = {
331 .glue_configure = dwc3_imx8mp_glue_configure,
332};
333
Marek Vasut68c86562022-04-13 00:42:55 +0200334void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100335 enum usb_dr_mode mode)
336{
337#define USBOTGSS_UTMI_OTG_STATUS 0x0084
338#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
339
340/* UTMI_OTG_STATUS REGISTER */
341#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
342#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
343#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
344#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
345#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
346#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
347#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
348enum dwc3_omap_utmi_mode {
349 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
350 DWC3_OMAP_UTMI_MODE_HW,
351 DWC3_OMAP_UTMI_MODE_SW,
352};
353
354 u32 use_id_pin;
355 u32 host_mode;
356 u32 reg;
357 u32 utmi_mode;
358 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
359
Simon Glassfa20e932020-12-03 16:55:20 -0700360 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100361 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
362
363 if (device_is_compatible(dev, "ti,am437x-dwc3"))
364 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
365
366 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
367 DWC3_OMAP_UTMI_MODE_UNKNOWN);
368 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
369 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
370 dev->name);
371 mode = USB_DR_MODE_PERIPHERAL;
372 }
373
374 switch (mode) {
375 case USB_DR_MODE_PERIPHERAL:
376 use_id_pin = 0;
377 host_mode = 0;
378 break;
379 case USB_DR_MODE_HOST:
380 use_id_pin = 0;
381 host_mode = 1;
382 break;
383 case USB_DR_MODE_OTG:
384 default:
385 use_id_pin = 1;
386 host_mode = 0;
387 break;
388 }
389
390 reg = readl(base + utmi_status_offset);
391
392 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
393 if (!use_id_pin)
394 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
395
396 writel(reg, base + utmi_status_offset);
397
398 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
399 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
400 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
401
402 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
403 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
404
405 if (!host_mode)
406 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
407 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
408
409 writel(reg, base + utmi_status_offset);
410
411 unmap_physmem(base, MAP_NOCACHE);
412}
413
414struct dwc3_glue_ops ti_ops = {
Marek Vasut68c86562022-04-13 00:42:55 +0200415 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100416};
417
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100418static int dwc3_glue_bind(struct udevice *parent)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200419{
Kever Yang1b807052020-03-04 08:59:50 +0800420 ofnode node;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200421 int ret;
Angus Ainslie6e382a82022-02-02 15:08:54 -0800422 enum usb_dr_mode dr_mode;
423
424 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200425
Simon Glassa7ece582020-12-19 10:40:14 -0700426 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
Kever Yang1b807052020-03-04 08:59:50 +0800427 const char *name = ofnode_get_name(node);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200428 struct udevice *dev;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100429 const char *driver = NULL;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200430
431 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200432
Angus Ainslie6e382a82022-02-02 15:08:54 -0800433 /* if the parent node doesn't have a mode check the leaf */
434 if (!dr_mode)
435 dr_mode = usb_get_dr_mode(node);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200436
437 switch (dr_mode) {
438 case USB_DR_MODE_PERIPHERAL:
439 case USB_DR_MODE_OTG:
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100440#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200441 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
442 driver = "dwc3-generic-peripheral";
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100443#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200444 break;
Simon Glass1f2440c2021-07-10 21:14:29 -0600445#if defined(CONFIG_SPL_USB_HOST) || !defined(CONFIG_SPL_BUILD)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200446 case USB_DR_MODE_HOST:
447 debug("%s: dr_mode: HOST\n", __func__);
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200448 driver = "dwc3-generic-host";
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200449 break;
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200450#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200451 default:
452 debug("%s: unsupported dr_mode\n", __func__);
453 return -ENODEV;
454 };
455
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100456 if (!driver)
457 continue;
458
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200459 ret = device_bind_driver_to_node(parent, driver, name,
Kever Yang1b807052020-03-04 08:59:50 +0800460 node, &dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200461 if (ret) {
462 debug("%s: not able to bind usb device mode\n",
463 __func__);
464 return ret;
465 }
466 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100467
468 return 0;
469}
470
471static int dwc3_glue_reset_init(struct udevice *dev,
472 struct dwc3_glue_data *glue)
473{
474 int ret;
475
476 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530477 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100478 return 0;
479 else if (ret)
480 return ret;
481
482 ret = reset_deassert_bulk(&glue->resets);
483 if (ret) {
484 reset_release_bulk(&glue->resets);
485 return ret;
486 }
487
488 return 0;
489}
490
491static int dwc3_glue_clk_init(struct udevice *dev,
492 struct dwc3_glue_data *glue)
493{
494 int ret;
495
496 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530497 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100498 return 0;
499 if (ret)
500 return ret;
501
502#if CONFIG_IS_ENABLED(CLK)
503 ret = clk_enable_bulk(&glue->clks);
504 if (ret) {
505 clk_release_bulk(&glue->clks);
506 return ret;
507 }
508#endif
509
510 return 0;
511}
512
513static int dwc3_glue_probe(struct udevice *dev)
514{
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100515 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700516 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100517 struct udevice *child = NULL;
518 int index = 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100519 int ret;
Michal Simekbb19d622022-03-09 10:05:45 +0100520 struct phy phy;
521
522 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
523 if (!ret) {
524 ret = generic_phy_init(&phy);
525 if (ret)
526 return ret;
Jan Kiszka2fe2cf02022-04-25 13:26:45 +0200527 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simekbb19d622022-03-09 10:05:45 +0100528 debug("could not get phy (err %d)\n", ret);
529 return ret;
Jan Kiszka2fe2cf02022-04-25 13:26:45 +0200530 } else {
531 phy.dev = NULL;
Michal Simekbb19d622022-03-09 10:05:45 +0100532 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100533
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100534 glue->regs = dev_read_addr(dev);
535
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100536 ret = dwc3_glue_clk_init(dev, glue);
537 if (ret)
538 return ret;
539
540 ret = dwc3_glue_reset_init(dev, glue);
541 if (ret)
542 return ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200543
Michal Simekbb19d622022-03-09 10:05:45 +0100544 if (phy.dev) {
545 ret = generic_phy_power_on(&phy);
546 if (ret)
547 return ret;
548 }
549
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100550 ret = device_find_first_child(dev, &child);
551 if (ret)
552 return ret;
553
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800554 if (glue->resets.count == 0) {
555 ret = dwc3_glue_reset_init(child, glue);
556 if (ret)
557 return ret;
558 }
559
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100560 while (child) {
561 enum usb_dr_mode dr_mode;
562
Simon Glassa7ece582020-12-19 10:40:14 -0700563 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100564 device_find_next_child(&child);
Marek Vasut68c86562022-04-13 00:42:55 +0200565 if (ops && ops->glue_configure)
566 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100567 index++;
568 }
569
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200570 return 0;
571}
572
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100573static int dwc3_glue_remove(struct udevice *dev)
574{
Simon Glassfa20e932020-12-03 16:55:20 -0700575 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100576
577 reset_release_bulk(&glue->resets);
578
579 clk_release_bulk(&glue->clks);
580
Jean-Jacques Hiblot5a945572019-07-05 09:33:56 +0200581 return 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100582}
583
584static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200585 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu1eb3c302020-05-12 08:36:01 +0200586 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot3e0684b2018-12-04 11:12:56 +0100587 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100588 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblotca848df2018-12-04 11:30:50 +0100589 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendrac6282952019-12-09 10:37:29 +0530590 { .compatible = "ti,am654-dwc3" },
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800591 { .compatible = "rockchip,rk3328-dwc3" },
592 { .compatible = "rockchip,rk3399-dwc3" },
Robert Marko746862b2020-09-10 16:00:05 +0200593 { .compatible = "qcom,dwc3" },
Marek Vasutae219342022-04-13 00:42:56 +0200594 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainslie6e382a82022-02-02 15:08:54 -0800595 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko221d7fa2020-12-03 19:45:01 +0200596 { .compatible = "intel,tangier-dwc3" },
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200597 { }
598};
599
600U_BOOT_DRIVER(dwc3_generic_wrapper) = {
601 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblotb49b5c22019-07-05 09:33:58 +0200602 .id = UCLASS_NOP,
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100603 .of_match = dwc3_glue_ids,
604 .bind = dwc3_glue_bind,
605 .probe = dwc3_glue_probe,
606 .remove = dwc3_glue_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -0700607 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100608
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200609};