blob: 466b25a0c38ea42ff0699d07ed893b124093f00e [file] [log] [blame]
Patrick Delaunay22929e12018-10-26 09:02:52 +02001// SPDX-License-Identifier: GPL-2.0
Michal Simek49d67452018-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 Glass1eb69ae2019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Michal Simek49d67452018-05-18 13:15:06 +020013#include <dm.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010016#include <dwc3-uboot.h>
Michal Simek142d50f2022-03-09 10:05:45 +010017#include <generic-phy.h>
Simon Glasscd93d622020-05-10 11:40:13 -060018#include <linux/bitops.h>
Frank Wang5d422ab2020-05-26 11:34:31 +080019#include <linux/delay.h>
Michal Simek49d67452018-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 Hiblot446e3a22018-11-29 10:52:48 +010026#include <reset.h>
27#include <clk.h>
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +020028#include <usb/xhci.h>
T Karthik Reddyb252d792022-07-08 11:21:59 +020029#include <asm/gpio.h>
Michal Simek49d67452018-05-18 13:15:06 +020030
Frank Wang5d422ab2020-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 Hiblot3a38a0a2019-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 Hiblot3a38a0a2019-09-11 11:33:48 +020043struct dwc3_generic_priv {
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020044 void *base;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010045 struct dwc3 dwc3;
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +020046 struct phy_bulk phys;
T Karthik Reddyb252d792022-07-08 11:21:59 +020047 struct gpio_desc ulpi_reset;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010048};
49
Jean-Jacques Hiblotb575e902019-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 Hiblot1af590d2019-09-11 11:33:49 +020055static int dwc3_generic_probe(struct udevice *dev,
56 struct dwc3_generic_priv *priv)
Michal Simek49d67452018-05-18 13:15:06 +020057{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010058 int rc;
Simon Glassc69cda22020-12-03 16:55:20 -070059 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010060 struct dwc3 *dwc3 = &priv->dwc3;
Simon Glassc69cda22020-12-03 16:55:20 -070061 struct dwc3_glue_data *glue = dev_get_plat(dev->parent);
Michal Simek49d67452018-05-18 13:15:06 +020062
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020063 dwc3->dev = dev;
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020064 dwc3->maximum_speed = plat->maximum_speed;
65 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020066#if CONFIG_IS_ENABLED(OF_CONTROL)
67 dwc3_of_parse(dwc3);
68#endif
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020069
Frank Wang5d422ab2020-05-26 11:34:31 +080070 /*
71 * It must hold whole USB3.0 OTG controller in resetting to hold pipe
72 * power state in P2 before initializing TypeC PHY on RK3399 platform.
73 */
74 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) {
75 reset_assert_bulk(&glue->resets);
76 udelay(1);
77 }
78
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +020079 rc = dwc3_setup_phy(dev, &priv->phys);
Siva Durga Prasad Paladugue7f9e1f2020-10-21 14:17:31 +020080 if (rc && rc != -ENOTSUPP)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010081 return rc;
82
T Karthik Reddyb252d792022-07-08 11:21:59 +020083 if (CONFIG_IS_ENABLED(DM_GPIO) &&
84 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
85 rc = gpio_request_by_name(dev->parent, "reset-gpios", 0,
86 &priv->ulpi_reset, GPIOD_ACTIVE_LOW);
87 if (rc)
88 return rc;
89
90 /* Toggle ulpi to reset the phy. */
91 rc = dm_gpio_set_value(&priv->ulpi_reset, 1);
92 if (rc)
93 return rc;
94
95 mdelay(5);
96
97 rc = dm_gpio_set_value(&priv->ulpi_reset, 0);
98 if (rc)
99 return rc;
100
101 mdelay(5);
102 }
103
Frank Wang5d422ab2020-05-26 11:34:31 +0800104 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3"))
105 reset_deassert_bulk(&glue->resets);
106
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200107 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
108 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +0200109
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100110
111 rc = dwc3_init(dwc3);
112 if (rc) {
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200113 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100114 return rc;
115 }
116
117 return 0;
Michal Simek49d67452018-05-18 13:15:06 +0200118}
119
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200120static int dwc3_generic_remove(struct udevice *dev,
121 struct dwc3_generic_priv *priv)
Michal Simek49d67452018-05-18 13:15:06 +0200122{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100123 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +0200124
T Karthik Reddyb252d792022-07-08 11:21:59 +0200125 if (CONFIG_IS_ENABLED(DM_GPIO) &&
126 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
127 struct gpio_desc *ulpi_reset = &priv->ulpi_reset;
128
129 dm_gpio_free(ulpi_reset->dev, ulpi_reset);
130 }
131
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100132 dwc3_remove(dwc3);
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +0200133 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100134 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek49d67452018-05-18 13:15:06 +0200135
136 return 0;
137}
138
Simon Glassd1998a92020-12-03 16:55:21 -0700139static int dwc3_generic_of_to_plat(struct udevice *dev)
Michal Simek49d67452018-05-18 13:15:06 +0200140{
Simon Glassc69cda22020-12-03 16:55:20 -0700141 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Simon Glassf10643c2020-12-19 10:40:14 -0700142 ofnode node = dev_ofnode(dev);
Michal Simek49d67452018-05-18 13:15:06 +0200143
Angus Ainsliec08db052022-02-02 15:08:54 -0800144 if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) {
145 /* This is a leaf so check the parent */
146 plat->base = dev_read_addr(dev->parent);
147 } else {
148 plat->base = dev_read_addr(dev);
149 }
Michal Simek49d67452018-05-18 13:15:06 +0200150
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200151 plat->maximum_speed = usb_get_maximum_speed(node);
152 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot1a63e5e2019-09-11 11:33:51 +0200153 pr_info("No USB maximum speed specified. Using super speed\n");
154 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek49d67452018-05-18 13:15:06 +0200155 }
156
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200157 plat->dr_mode = usb_get_dr_mode(node);
158 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Angus Ainsliec08db052022-02-02 15:08:54 -0800159 /* might be a leaf so check the parent for mode */
160 node = dev_ofnode(dev->parent);
161 plat->dr_mode = usb_get_dr_mode(node);
162 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
163 pr_err("Invalid usb mode setup\n");
164 return -ENODEV;
165 }
Michal Simek49d67452018-05-18 13:15:06 +0200166 }
167
168 return 0;
169}
170
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200171#if CONFIG_IS_ENABLED(DM_USB_GADGET)
172int dm_usb_gadget_handle_interrupts(struct udevice *dev)
173{
174 struct dwc3_generic_priv *priv = dev_get_priv(dev);
175 struct dwc3 *dwc3 = &priv->dwc3;
176
177 dwc3_gadget_uboot_handle_interrupt(dwc3);
178
179 return 0;
180}
181
182static int dwc3_generic_peripheral_probe(struct udevice *dev)
183{
184 struct dwc3_generic_priv *priv = dev_get_priv(dev);
185
186 return dwc3_generic_probe(dev, priv);
187}
188
189static int dwc3_generic_peripheral_remove(struct udevice *dev)
190{
191 struct dwc3_generic_priv *priv = dev_get_priv(dev);
192
193 return dwc3_generic_remove(dev, priv);
194}
195
Michal Simek49d67452018-05-18 13:15:06 +0200196U_BOOT_DRIVER(dwc3_generic_peripheral) = {
197 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100198 .id = UCLASS_USB_GADGET_GENERIC,
Simon Glassd1998a92020-12-03 16:55:21 -0700199 .of_to_plat = dwc3_generic_of_to_plat,
Michal Simek49d67452018-05-18 13:15:06 +0200200 .probe = dwc3_generic_peripheral_probe,
201 .remove = dwc3_generic_peripheral_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700202 .priv_auto = sizeof(struct dwc3_generic_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700203 .plat_auto = sizeof(struct dwc3_generic_plat),
Michal Simek49d67452018-05-18 13:15:06 +0200204};
Jean-Jacques Hiblot687ab542018-11-29 10:52:42 +0100205#endif
Michal Simek49d67452018-05-18 13:15:06 +0200206
Simon Glass333e4a62021-07-10 21:14:29 -0600207#if defined(CONFIG_SPL_USB_HOST) || \
Kunihiko Hayashia5f9be12021-05-12 23:11:14 +0900208 !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST)
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200209static int dwc3_generic_host_probe(struct udevice *dev)
210{
211 struct xhci_hcor *hcor;
212 struct xhci_hccr *hccr;
213 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
214 int rc;
215
216 rc = dwc3_generic_probe(dev, &priv->gen_priv);
217 if (rc)
218 return rc;
219
220 hccr = (struct xhci_hccr *)priv->gen_priv.base;
221 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
222 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
223
224 return xhci_register(dev, hccr, hcor);
225}
226
227static int dwc3_generic_host_remove(struct udevice *dev)
228{
229 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
230 int rc;
231
232 rc = xhci_deregister(dev);
233 if (rc)
234 return rc;
235
236 return dwc3_generic_remove(dev, &priv->gen_priv);
237}
238
239U_BOOT_DRIVER(dwc3_generic_host) = {
240 .name = "dwc3-generic-host",
241 .id = UCLASS_USB,
Simon Glassd1998a92020-12-03 16:55:21 -0700242 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200243 .probe = dwc3_generic_host_probe,
244 .remove = dwc3_generic_host_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700245 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700246 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200247 .ops = &xhci_usb_ops,
248 .flags = DM_FLAG_ALLOC_PRIV_DMA,
249};
250#endif
251
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100252struct dwc3_glue_ops {
Marek Vasutf1ef9552022-04-13 00:42:55 +0200253 void (*glue_configure)(struct udevice *dev, int index,
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100254 enum usb_dr_mode mode);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100255};
256
Marek Vasutd0f7a052022-04-13 00:42:56 +0200257void dwc3_imx8mp_glue_configure(struct udevice *dev, int index,
258 enum usb_dr_mode mode)
259{
260/* USB glue registers */
261#define USB_CTRL0 0x00
262#define USB_CTRL1 0x04
263
264#define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
265#define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
266#define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
267
268#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
269#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
270 fdt_addr_t regs = dev_read_addr_index(dev, 1);
271 void *base = map_physmem(regs, 0x8, MAP_NOCACHE);
272 u32 value;
273
274 value = readl(base + USB_CTRL0);
275
276 if (dev_read_bool(dev, "fsl,permanently-attached"))
277 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
278 else
279 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
280
281 if (dev_read_bool(dev, "fsl,disable-port-power-control"))
282 value &= ~(USB_CTRL0_PORTPWR_EN);
283 else
284 value |= USB_CTRL0_PORTPWR_EN;
285
286 writel(value, base + USB_CTRL0);
287
288 value = readl(base + USB_CTRL1);
289 if (dev_read_bool(dev, "fsl,over-current-active-low"))
290 value |= USB_CTRL1_OC_POLARITY;
291 else
292 value &= ~USB_CTRL1_OC_POLARITY;
293
294 if (dev_read_bool(dev, "fsl,power-active-low"))
295 value |= USB_CTRL1_PWR_POLARITY;
296 else
297 value &= ~USB_CTRL1_PWR_POLARITY;
298
299 writel(value, base + USB_CTRL1);
300
301 unmap_physmem(base, MAP_NOCACHE);
302}
303
304struct dwc3_glue_ops imx8mp_ops = {
305 .glue_configure = dwc3_imx8mp_glue_configure,
306};
307
Marek Vasutf1ef9552022-04-13 00:42:55 +0200308void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100309 enum usb_dr_mode mode)
310{
311#define USBOTGSS_UTMI_OTG_STATUS 0x0084
312#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
313
314/* UTMI_OTG_STATUS REGISTER */
315#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
316#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
317#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
318#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
319#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
320#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
321#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
322enum dwc3_omap_utmi_mode {
323 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
324 DWC3_OMAP_UTMI_MODE_HW,
325 DWC3_OMAP_UTMI_MODE_SW,
326};
327
328 u32 use_id_pin;
329 u32 host_mode;
330 u32 reg;
331 u32 utmi_mode;
332 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
333
Simon Glassc69cda22020-12-03 16:55:20 -0700334 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100335 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
336
337 if (device_is_compatible(dev, "ti,am437x-dwc3"))
338 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
339
340 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
341 DWC3_OMAP_UTMI_MODE_UNKNOWN);
342 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
343 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
344 dev->name);
345 mode = USB_DR_MODE_PERIPHERAL;
346 }
347
348 switch (mode) {
349 case USB_DR_MODE_PERIPHERAL:
350 use_id_pin = 0;
351 host_mode = 0;
352 break;
353 case USB_DR_MODE_HOST:
354 use_id_pin = 0;
355 host_mode = 1;
356 break;
357 case USB_DR_MODE_OTG:
358 default:
359 use_id_pin = 1;
360 host_mode = 0;
361 break;
362 }
363
364 reg = readl(base + utmi_status_offset);
365
366 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
367 if (!use_id_pin)
368 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
369
370 writel(reg, base + utmi_status_offset);
371
372 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
373 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
374 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
375
376 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
377 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
378
379 if (!host_mode)
380 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
381 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
382
383 writel(reg, base + utmi_status_offset);
384
385 unmap_physmem(base, MAP_NOCACHE);
386}
387
388struct dwc3_glue_ops ti_ops = {
Marek Vasutf1ef9552022-04-13 00:42:55 +0200389 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100390};
391
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100392static int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200393{
Kever Yangac28e592020-03-04 08:59:50 +0800394 ofnode node;
Michal Simek49d67452018-05-18 13:15:06 +0200395 int ret;
Angus Ainsliec08db052022-02-02 15:08:54 -0800396 enum usb_dr_mode dr_mode;
397
398 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
Michal Simek49d67452018-05-18 13:15:06 +0200399
Simon Glassf10643c2020-12-19 10:40:14 -0700400 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
Kever Yangac28e592020-03-04 08:59:50 +0800401 const char *name = ofnode_get_name(node);
Michal Simek49d67452018-05-18 13:15:06 +0200402 struct udevice *dev;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100403 const char *driver = NULL;
Michal Simek49d67452018-05-18 13:15:06 +0200404
405 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek49d67452018-05-18 13:15:06 +0200406
Angus Ainsliec08db052022-02-02 15:08:54 -0800407 /* if the parent node doesn't have a mode check the leaf */
408 if (!dr_mode)
409 dr_mode = usb_get_dr_mode(node);
Michal Simek49d67452018-05-18 13:15:06 +0200410
411 switch (dr_mode) {
412 case USB_DR_MODE_PERIPHERAL:
413 case USB_DR_MODE_OTG:
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100414#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Michal Simek49d67452018-05-18 13:15:06 +0200415 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
416 driver = "dwc3-generic-peripheral";
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100417#endif
Michal Simek49d67452018-05-18 13:15:06 +0200418 break;
Simon Glass333e4a62021-07-10 21:14:29 -0600419#if defined(CONFIG_SPL_USB_HOST) || !defined(CONFIG_SPL_BUILD)
Michal Simek49d67452018-05-18 13:15:06 +0200420 case USB_DR_MODE_HOST:
421 debug("%s: dr_mode: HOST\n", __func__);
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200422 driver = "dwc3-generic-host";
Michal Simek49d67452018-05-18 13:15:06 +0200423 break;
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200424#endif
Michal Simek49d67452018-05-18 13:15:06 +0200425 default:
426 debug("%s: unsupported dr_mode\n", __func__);
427 return -ENODEV;
428 };
429
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100430 if (!driver)
431 continue;
432
Michal Simek49d67452018-05-18 13:15:06 +0200433 ret = device_bind_driver_to_node(parent, driver, name,
Kever Yangac28e592020-03-04 08:59:50 +0800434 node, &dev);
Michal Simek49d67452018-05-18 13:15:06 +0200435 if (ret) {
436 debug("%s: not able to bind usb device mode\n",
437 __func__);
438 return ret;
439 }
440 }
441
442 return 0;
443}
444
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100445static int dwc3_glue_reset_init(struct udevice *dev,
446 struct dwc3_glue_data *glue)
447{
448 int ret;
449
450 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530451 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100452 return 0;
453 else if (ret)
454 return ret;
455
456 ret = reset_deassert_bulk(&glue->resets);
457 if (ret) {
458 reset_release_bulk(&glue->resets);
459 return ret;
460 }
461
462 return 0;
463}
464
465static int dwc3_glue_clk_init(struct udevice *dev,
466 struct dwc3_glue_data *glue)
467{
468 int ret;
469
470 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530471 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100472 return 0;
473 if (ret)
474 return ret;
475
476#if CONFIG_IS_ENABLED(CLK)
477 ret = clk_enable_bulk(&glue->clks);
478 if (ret) {
479 clk_release_bulk(&glue->clks);
480 return ret;
481 }
482#endif
483
484 return 0;
485}
486
487static int dwc3_glue_probe(struct udevice *dev)
488{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100489 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700490 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100491 struct udevice *child = NULL;
492 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100493 int ret;
Michal Simek142d50f2022-03-09 10:05:45 +0100494 struct phy phy;
495
496 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
497 if (!ret) {
498 ret = generic_phy_init(&phy);
499 if (ret)
500 return ret;
Jan Kiszka868d58f2022-04-25 13:26:45 +0200501 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simek142d50f2022-03-09 10:05:45 +0100502 debug("could not get phy (err %d)\n", ret);
503 return ret;
Jan Kiszka868d58f2022-04-25 13:26:45 +0200504 } else {
505 phy.dev = NULL;
Michal Simek142d50f2022-03-09 10:05:45 +0100506 }
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100507
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100508 glue->regs = dev_read_addr(dev);
509
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100510 ret = dwc3_glue_clk_init(dev, glue);
511 if (ret)
512 return ret;
513
514 ret = dwc3_glue_reset_init(dev, glue);
515 if (ret)
516 return ret;
517
Michal Simek142d50f2022-03-09 10:05:45 +0100518 if (phy.dev) {
519 ret = generic_phy_power_on(&phy);
520 if (ret)
521 return ret;
522 }
523
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100524 ret = device_find_first_child(dev, &child);
525 if (ret)
526 return ret;
527
Frank Wang5d422ab2020-05-26 11:34:31 +0800528 if (glue->resets.count == 0) {
529 ret = dwc3_glue_reset_init(child, glue);
530 if (ret)
531 return ret;
532 }
533
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100534 while (child) {
535 enum usb_dr_mode dr_mode;
536
Simon Glassf10643c2020-12-19 10:40:14 -0700537 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100538 device_find_next_child(&child);
Marek Vasutf1ef9552022-04-13 00:42:55 +0200539 if (ops && ops->glue_configure)
540 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100541 index++;
542 }
543
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100544 return 0;
545}
546
547static int dwc3_glue_remove(struct udevice *dev)
548{
Simon Glassc69cda22020-12-03 16:55:20 -0700549 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100550
551 reset_release_bulk(&glue->resets);
552
553 clk_release_bulk(&glue->clks);
554
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200555 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100556}
557
558static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200559 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu648856a2020-05-12 08:36:01 +0200560 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100561 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100562 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100563 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendracab4e272019-12-09 10:37:29 +0530564 { .compatible = "ti,am654-dwc3" },
Frank Wang5d422ab2020-05-26 11:34:31 +0800565 { .compatible = "rockchip,rk3328-dwc3" },
566 { .compatible = "rockchip,rk3399-dwc3" },
Robert Marko74a703a2020-09-10 16:00:05 +0200567 { .compatible = "qcom,dwc3" },
Marek Vasutd0f7a052022-04-13 00:42:56 +0200568 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainsliec08db052022-02-02 15:08:54 -0800569 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko23cdbba2020-12-03 19:45:01 +0200570 { .compatible = "intel,tangier-dwc3" },
Michal Simek49d67452018-05-18 13:15:06 +0200571 { }
572};
573
574U_BOOT_DRIVER(dwc3_generic_wrapper) = {
575 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblot3b838292019-07-05 09:33:58 +0200576 .id = UCLASS_NOP,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100577 .of_match = dwc3_glue_ids,
578 .bind = dwc3_glue_bind,
579 .probe = dwc3_glue_probe,
580 .remove = dwc3_glue_remove,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700581 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100582
Michal Simek49d67452018-05-18 13:15:06 +0200583};