blob: 6fb2de8a5acef3ba237039acb8e41190556e2ff0 [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>
Simon Glass1e94b462023-09-14 18:21:46 -060020#include <linux/printk.h>
Michal Simek49d67452018-05-18 13:15:06 +020021#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
23#include <malloc.h>
24#include <usb.h>
25#include "core.h"
26#include "gadget.h"
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010027#include <reset.h>
28#include <clk.h>
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +020029#include <usb/xhci.h>
T Karthik Reddyb252d792022-07-08 11:21:59 +020030#include <asm/gpio.h>
Michal Simek49d67452018-05-18 13:15:06 +020031
Kunihiko Hayashief2313b2023-02-20 14:50:28 +090032#include "dwc3-generic.h"
Frank Wang5d422ab2020-05-26 11:34:31 +080033
Jean-Jacques Hiblot3a38a0a2019-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 Hiblot3a38a0a2019-09-11 11:33:48 +020040struct dwc3_generic_priv {
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020041 void *base;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010042 struct dwc3 dwc3;
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +020043 struct phy_bulk phys;
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +053044 struct gpio_desc *ulpi_reset;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010045};
46
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +020047struct dwc3_generic_host_priv {
48 struct xhci_ctrl xhci_ctrl;
49 struct dwc3_generic_priv gen_priv;
50};
51
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020052static int dwc3_generic_probe(struct udevice *dev,
53 struct dwc3_generic_priv *priv)
Michal Simek49d67452018-05-18 13:15:06 +020054{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010055 int rc;
Simon Glassc69cda22020-12-03 16:55:20 -070056 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010057 struct dwc3 *dwc3 = &priv->dwc3;
Simon Glassc69cda22020-12-03 16:55:20 -070058 struct dwc3_glue_data *glue = dev_get_plat(dev->parent);
Marek Vasut8ae84e62022-11-27 15:31:52 +010059 int __maybe_unused index;
60 ofnode __maybe_unused node;
Michal Simek49d67452018-05-18 13:15:06 +020061
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020062 dwc3->dev = dev;
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020063 dwc3->maximum_speed = plat->maximum_speed;
64 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020065#if CONFIG_IS_ENABLED(OF_CONTROL)
66 dwc3_of_parse(dwc3);
Marek Vasut8ae84e62022-11-27 15:31:52 +010067
Marek Vasutc6583352023-02-20 14:50:25 +090068 /*
69 * There are currently four disparate placement possibilities of DWC3
70 * reference clock phandle in SoC DTs:
71 * - in top level glue node, with generic subnode without clock (ZynqMP)
72 * - in top level generic node, with no subnode (i.MX8MQ)
73 * - in generic subnode, with other clock in top level node (i.MX8MP)
74 * - in both top level node and generic subnode (Rockchip)
75 * Cover all the possibilities here by looking into both nodes, start
76 * with the top level node as that seems to be used in majority of DTs
77 * to reference the clock.
78 */
Marek Vasut8ae84e62022-11-27 15:31:52 +010079 node = dev_ofnode(dev->parent);
80 index = ofnode_stringlist_search(node, "clock-names", "ref");
81 if (index < 0)
82 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
Marek Vasutc6583352023-02-20 14:50:25 +090083 if (index < 0) {
84 node = dev_ofnode(dev);
85 index = ofnode_stringlist_search(node, "clock-names", "ref");
86 if (index < 0)
87 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
88 }
Marek Vasut8ae84e62022-11-27 15:31:52 +010089 if (index >= 0)
90 dwc3->ref_clk = &glue->clks.clks[index];
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020091#endif
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020092
Frank Wang5d422ab2020-05-26 11:34:31 +080093 /*
94 * It must hold whole USB3.0 OTG controller in resetting to hold pipe
95 * power state in P2 before initializing TypeC PHY on RK3399 platform.
96 */
97 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) {
98 reset_assert_bulk(&glue->resets);
99 udelay(1);
100 }
101
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +0200102 rc = dwc3_setup_phy(dev, &priv->phys);
Siva Durga Prasad Paladugue7f9e1f2020-10-21 14:17:31 +0200103 if (rc && rc != -ENOTSUPP)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100104 return rc;
105
T Karthik Reddyb252d792022-07-08 11:21:59 +0200106 if (CONFIG_IS_ENABLED(DM_GPIO) &&
107 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530108 priv->ulpi_reset = devm_gpiod_get_optional(dev->parent, "reset",
Peter Korsgaardd266d4b2023-06-28 14:26:48 +0200109 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530110 /* property is optional, don't return error! */
111 if (priv->ulpi_reset) {
112 /* Toggle ulpi to reset the phy. */
113 rc = dm_gpio_set_value(priv->ulpi_reset, 1);
114 if (rc)
115 return rc;
T Karthik Reddyb252d792022-07-08 11:21:59 +0200116
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530117 mdelay(5);
T Karthik Reddyb252d792022-07-08 11:21:59 +0200118
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530119 rc = dm_gpio_set_value(priv->ulpi_reset, 0);
120 if (rc)
121 return rc;
T Karthik Reddyb252d792022-07-08 11:21:59 +0200122
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530123 mdelay(5);
124 }
T Karthik Reddyb252d792022-07-08 11:21:59 +0200125 }
126
Frank Wang5d422ab2020-05-26 11:34:31 +0800127 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3"))
128 reset_deassert_bulk(&glue->resets);
129
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200130 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
131 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +0200132
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100133
134 rc = dwc3_init(dwc3);
135 if (rc) {
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200136 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100137 return rc;
138 }
139
140 return 0;
Michal Simek49d67452018-05-18 13:15:06 +0200141}
142
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200143static int dwc3_generic_remove(struct udevice *dev,
144 struct dwc3_generic_priv *priv)
Michal Simek49d67452018-05-18 13:15:06 +0200145{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100146 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +0200147
T Karthik Reddyb252d792022-07-08 11:21:59 +0200148 if (CONFIG_IS_ENABLED(DM_GPIO) &&
Venkatesh Yadav Abbarapu9871b0e2023-08-09 09:03:50 +0530149 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3") &&
150 priv->ulpi_reset) {
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530151 struct gpio_desc *ulpi_reset = priv->ulpi_reset;
T Karthik Reddyb252d792022-07-08 11:21:59 +0200152
153 dm_gpio_free(ulpi_reset->dev, ulpi_reset);
154 }
155
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100156 dwc3_remove(dwc3);
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +0200157 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100158 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek49d67452018-05-18 13:15:06 +0200159
160 return 0;
161}
162
Simon Glassd1998a92020-12-03 16:55:21 -0700163static int dwc3_generic_of_to_plat(struct udevice *dev)
Michal Simek49d67452018-05-18 13:15:06 +0200164{
Simon Glassc69cda22020-12-03 16:55:20 -0700165 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Simon Glassf10643c2020-12-19 10:40:14 -0700166 ofnode node = dev_ofnode(dev);
Michal Simek49d67452018-05-18 13:15:06 +0200167
Angus Ainsliec08db052022-02-02 15:08:54 -0800168 if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) {
169 /* This is a leaf so check the parent */
170 plat->base = dev_read_addr(dev->parent);
171 } else {
172 plat->base = dev_read_addr(dev);
173 }
Michal Simek49d67452018-05-18 13:15:06 +0200174
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200175 plat->maximum_speed = usb_get_maximum_speed(node);
176 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot1a63e5e2019-09-11 11:33:51 +0200177 pr_info("No USB maximum speed specified. Using super speed\n");
178 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek49d67452018-05-18 13:15:06 +0200179 }
180
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200181 plat->dr_mode = usb_get_dr_mode(node);
182 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Angus Ainsliec08db052022-02-02 15:08:54 -0800183 /* might be a leaf so check the parent for mode */
184 node = dev_ofnode(dev->parent);
185 plat->dr_mode = usb_get_dr_mode(node);
186 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
187 pr_err("Invalid usb mode setup\n");
188 return -ENODEV;
189 }
Michal Simek49d67452018-05-18 13:15:06 +0200190 }
191
192 return 0;
193}
194
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200195#if CONFIG_IS_ENABLED(DM_USB_GADGET)
196int dm_usb_gadget_handle_interrupts(struct udevice *dev)
197{
198 struct dwc3_generic_priv *priv = dev_get_priv(dev);
199 struct dwc3 *dwc3 = &priv->dwc3;
200
201 dwc3_gadget_uboot_handle_interrupt(dwc3);
202
203 return 0;
204}
205
206static int dwc3_generic_peripheral_probe(struct udevice *dev)
207{
208 struct dwc3_generic_priv *priv = dev_get_priv(dev);
209
210 return dwc3_generic_probe(dev, priv);
211}
212
213static int dwc3_generic_peripheral_remove(struct udevice *dev)
214{
215 struct dwc3_generic_priv *priv = dev_get_priv(dev);
216
217 return dwc3_generic_remove(dev, priv);
218}
219
Michal Simek49d67452018-05-18 13:15:06 +0200220U_BOOT_DRIVER(dwc3_generic_peripheral) = {
221 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100222 .id = UCLASS_USB_GADGET_GENERIC,
Simon Glassd1998a92020-12-03 16:55:21 -0700223 .of_to_plat = dwc3_generic_of_to_plat,
Michal Simek49d67452018-05-18 13:15:06 +0200224 .probe = dwc3_generic_peripheral_probe,
225 .remove = dwc3_generic_peripheral_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700226 .priv_auto = sizeof(struct dwc3_generic_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700227 .plat_auto = sizeof(struct dwc3_generic_plat),
Michal Simek49d67452018-05-18 13:15:06 +0200228};
Jean-Jacques Hiblot687ab542018-11-29 10:52:42 +0100229#endif
Michal Simek49d67452018-05-18 13:15:06 +0200230
Jonas Karlman6913c302023-07-30 22:59:56 +0000231#if CONFIG_IS_ENABLED(USB_HOST)
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200232static int dwc3_generic_host_probe(struct udevice *dev)
233{
234 struct xhci_hcor *hcor;
235 struct xhci_hccr *hccr;
236 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
237 int rc;
238
239 rc = dwc3_generic_probe(dev, &priv->gen_priv);
240 if (rc)
241 return rc;
242
243 hccr = (struct xhci_hccr *)priv->gen_priv.base;
244 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
245 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
246
247 return xhci_register(dev, hccr, hcor);
248}
249
250static int dwc3_generic_host_remove(struct udevice *dev)
251{
252 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
253 int rc;
254
255 rc = xhci_deregister(dev);
256 if (rc)
257 return rc;
258
259 return dwc3_generic_remove(dev, &priv->gen_priv);
260}
261
262U_BOOT_DRIVER(dwc3_generic_host) = {
263 .name = "dwc3-generic-host",
264 .id = UCLASS_USB,
Simon Glassd1998a92020-12-03 16:55:21 -0700265 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200266 .probe = dwc3_generic_host_probe,
267 .remove = dwc3_generic_host_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700268 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700269 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200270 .ops = &xhci_usb_ops,
271 .flags = DM_FLAG_ALLOC_PRIV_DMA,
272};
273#endif
274
Marek Vasutd0f7a052022-04-13 00:42:56 +0200275void dwc3_imx8mp_glue_configure(struct udevice *dev, int index,
276 enum usb_dr_mode mode)
277{
278/* USB glue registers */
279#define USB_CTRL0 0x00
280#define USB_CTRL1 0x04
281
282#define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
283#define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
284#define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
285
286#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
287#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
288 fdt_addr_t regs = dev_read_addr_index(dev, 1);
289 void *base = map_physmem(regs, 0x8, MAP_NOCACHE);
290 u32 value;
291
292 value = readl(base + USB_CTRL0);
293
294 if (dev_read_bool(dev, "fsl,permanently-attached"))
295 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
296 else
297 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
298
299 if (dev_read_bool(dev, "fsl,disable-port-power-control"))
300 value &= ~(USB_CTRL0_PORTPWR_EN);
301 else
302 value |= USB_CTRL0_PORTPWR_EN;
303
304 writel(value, base + USB_CTRL0);
305
306 value = readl(base + USB_CTRL1);
307 if (dev_read_bool(dev, "fsl,over-current-active-low"))
308 value |= USB_CTRL1_OC_POLARITY;
309 else
310 value &= ~USB_CTRL1_OC_POLARITY;
311
312 if (dev_read_bool(dev, "fsl,power-active-low"))
313 value |= USB_CTRL1_PWR_POLARITY;
314 else
315 value &= ~USB_CTRL1_PWR_POLARITY;
316
317 writel(value, base + USB_CTRL1);
318
319 unmap_physmem(base, MAP_NOCACHE);
320}
321
322struct dwc3_glue_ops imx8mp_ops = {
323 .glue_configure = dwc3_imx8mp_glue_configure,
324};
325
Marek Vasutf1ef9552022-04-13 00:42:55 +0200326void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100327 enum usb_dr_mode mode)
328{
329#define USBOTGSS_UTMI_OTG_STATUS 0x0084
330#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
331
332/* UTMI_OTG_STATUS REGISTER */
333#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
334#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
335#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
336#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
337#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
338#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
339#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
340enum dwc3_omap_utmi_mode {
341 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
342 DWC3_OMAP_UTMI_MODE_HW,
343 DWC3_OMAP_UTMI_MODE_SW,
344};
345
346 u32 use_id_pin;
347 u32 host_mode;
348 u32 reg;
349 u32 utmi_mode;
350 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
351
Simon Glassc69cda22020-12-03 16:55:20 -0700352 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100353 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
354
355 if (device_is_compatible(dev, "ti,am437x-dwc3"))
356 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
357
358 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
359 DWC3_OMAP_UTMI_MODE_UNKNOWN);
360 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
361 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
362 dev->name);
363 mode = USB_DR_MODE_PERIPHERAL;
364 }
365
366 switch (mode) {
367 case USB_DR_MODE_PERIPHERAL:
368 use_id_pin = 0;
369 host_mode = 0;
370 break;
371 case USB_DR_MODE_HOST:
372 use_id_pin = 0;
373 host_mode = 1;
374 break;
375 case USB_DR_MODE_OTG:
376 default:
377 use_id_pin = 1;
378 host_mode = 0;
379 break;
380 }
381
382 reg = readl(base + utmi_status_offset);
383
384 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
385 if (!use_id_pin)
386 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
387
388 writel(reg, base + utmi_status_offset);
389
390 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
391 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
392 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
393
394 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
395 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
396
397 if (!host_mode)
398 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
399 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
400
401 writel(reg, base + utmi_status_offset);
402
403 unmap_physmem(base, MAP_NOCACHE);
404}
405
406struct dwc3_glue_ops ti_ops = {
Marek Vasutf1ef9552022-04-13 00:42:55 +0200407 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100408};
409
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000410static int dwc3_rk_glue_get_ctrl_dev(struct udevice *dev, ofnode *node)
411{
412 *node = dev_ofnode(dev);
413 if (!ofnode_valid(*node))
414 return -EINVAL;
415
416 return 0;
417}
418
419struct dwc3_glue_ops rk_ops = {
420 .glue_get_ctrl_dev = dwc3_rk_glue_get_ctrl_dev,
421};
422
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900423static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)
424{
425 const char *name = ofnode_get_name(node);
Jonas Karlman6913c302023-07-30 22:59:56 +0000426 const char *driver;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900427 enum usb_dr_mode dr_mode;
428 struct udevice *dev;
429 int ret;
430
431 debug("%s: subnode name: %s\n", __func__, name);
432
433 /* if the parent node doesn't have a mode check the leaf */
434 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
435 if (!dr_mode)
436 dr_mode = usb_get_dr_mode(node);
437
Jonas Karlman6913c302023-07-30 22:59:56 +0000438 if (CONFIG_IS_ENABLED(DM_USB_GADGET) &&
439 (dr_mode == USB_DR_MODE_PERIPHERAL || dr_mode == USB_DR_MODE_OTG)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900440 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
441 driver = "dwc3-generic-peripheral";
Jonas Karlman6913c302023-07-30 22:59:56 +0000442 } else if (CONFIG_IS_ENABLED(USB_HOST) && dr_mode == USB_DR_MODE_HOST) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900443 debug("%s: dr_mode: HOST\n", __func__);
444 driver = "dwc3-generic-host";
Jonas Karlman6913c302023-07-30 22:59:56 +0000445 } else {
446 debug("%s: unsupported dr_mode %d\n", __func__, dr_mode);
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900447 return -ENODEV;
Jonas Karlman6913c302023-07-30 22:59:56 +0000448 }
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900449
450 ret = device_bind_driver_to_node(parent, driver, name,
451 node, &dev);
452 if (ret) {
453 debug("%s: not able to bind usb device mode\n",
454 __func__);
455 return ret;
456 }
457
458 return 0;
459}
460
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900461int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200462{
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900463 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent);
Kever Yangac28e592020-03-04 08:59:50 +0800464 ofnode node;
Michal Simek49d67452018-05-18 13:15:06 +0200465 int ret;
Angus Ainsliec08db052022-02-02 15:08:54 -0800466
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900467 if (ops && ops->glue_get_ctrl_dev) {
468 ret = ops->glue_get_ctrl_dev(parent, &node);
469 if (ret)
470 return ret;
471
472 return dwc3_glue_bind_common(parent, node);
473 }
Michal Simek49d67452018-05-18 13:15:06 +0200474
Simon Glassf10643c2020-12-19 10:40:14 -0700475 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900476 ret = dwc3_glue_bind_common(parent, node);
477 if (ret == -ENXIO)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100478 continue;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900479 if (ret)
Michal Simek49d67452018-05-18 13:15:06 +0200480 return ret;
Michal Simek49d67452018-05-18 13:15:06 +0200481 }
482
483 return 0;
484}
485
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100486static int dwc3_glue_reset_init(struct udevice *dev,
487 struct dwc3_glue_data *glue)
488{
489 int ret;
490
491 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530492 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100493 return 0;
494 else if (ret)
495 return ret;
496
497 ret = reset_deassert_bulk(&glue->resets);
498 if (ret) {
499 reset_release_bulk(&glue->resets);
500 return ret;
501 }
502
503 return 0;
504}
505
506static int dwc3_glue_clk_init(struct udevice *dev,
507 struct dwc3_glue_data *glue)
508{
509 int ret;
510
511 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530512 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100513 return 0;
514 if (ret)
515 return ret;
516
517#if CONFIG_IS_ENABLED(CLK)
518 ret = clk_enable_bulk(&glue->clks);
519 if (ret) {
520 clk_release_bulk(&glue->clks);
521 return ret;
522 }
523#endif
524
525 return 0;
526}
527
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900528int dwc3_glue_probe(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100529{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100530 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700531 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100532 struct udevice *child = NULL;
533 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100534 int ret;
Michal Simek142d50f2022-03-09 10:05:45 +0100535 struct phy phy;
536
537 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
538 if (!ret) {
539 ret = generic_phy_init(&phy);
540 if (ret)
541 return ret;
Jan Kiszka868d58f2022-04-25 13:26:45 +0200542 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simek142d50f2022-03-09 10:05:45 +0100543 debug("could not get phy (err %d)\n", ret);
544 return ret;
545 }
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100546
Kunihiko Hayashi211a0662023-02-20 14:50:29 +0900547 glue->regs = dev_read_addr_size_index(dev, 0, &glue->size);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100548
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100549 ret = dwc3_glue_clk_init(dev, glue);
550 if (ret)
551 return ret;
552
553 ret = dwc3_glue_reset_init(dev, glue);
554 if (ret)
555 return ret;
556
Jonas Karlman5ccfdd82023-08-31 22:16:36 +0000557 if (generic_phy_valid(&phy)) {
Michal Simek142d50f2022-03-09 10:05:45 +0100558 ret = generic_phy_power_on(&phy);
559 if (ret)
560 return ret;
561 }
562
Jonas Karlman4412a2b2023-07-30 22:59:55 +0000563 device_find_first_child(dev, &child);
564 if (!child)
565 return 0;
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100566
Kunihiko Hayashi7c71c682023-02-20 14:50:27 +0900567 if (glue->clks.count == 0) {
568 ret = dwc3_glue_clk_init(child, glue);
569 if (ret)
570 return ret;
571 }
572
Frank Wang5d422ab2020-05-26 11:34:31 +0800573 if (glue->resets.count == 0) {
574 ret = dwc3_glue_reset_init(child, glue);
575 if (ret)
576 return ret;
577 }
578
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100579 while (child) {
580 enum usb_dr_mode dr_mode;
581
Simon Glassf10643c2020-12-19 10:40:14 -0700582 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100583 device_find_next_child(&child);
Marek Vasutf1ef9552022-04-13 00:42:55 +0200584 if (ops && ops->glue_configure)
585 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100586 index++;
587 }
588
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100589 return 0;
590}
591
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900592int dwc3_glue_remove(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100593{
Simon Glassc69cda22020-12-03 16:55:20 -0700594 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100595
596 reset_release_bulk(&glue->resets);
597
598 clk_release_bulk(&glue->clks);
599
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200600 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100601}
602
603static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200604 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu648856a2020-05-12 08:36:01 +0200605 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100606 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100607 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100608 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendracab4e272019-12-09 10:37:29 +0530609 { .compatible = "ti,am654-dwc3" },
Jagan Teki185571b2023-06-06 22:39:14 +0530610 { .compatible = "rockchip,rk3328-dwc3", .data = (ulong)&rk_ops },
Frank Wang5d422ab2020-05-26 11:34:31 +0800611 { .compatible = "rockchip,rk3399-dwc3" },
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000612 { .compatible = "rockchip,rk3568-dwc3", .data = (ulong)&rk_ops },
Jonas Karlman6e91df92023-11-12 15:25:25 +0000613 { .compatible = "rockchip,rk3588-dwc3", .data = (ulong)&rk_ops },
Robert Marko74a703a2020-09-10 16:00:05 +0200614 { .compatible = "qcom,dwc3" },
Marek Vasutd0f7a052022-04-13 00:42:56 +0200615 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainsliec08db052022-02-02 15:08:54 -0800616 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko23cdbba2020-12-03 19:45:01 +0200617 { .compatible = "intel,tangier-dwc3" },
Michal Simek49d67452018-05-18 13:15:06 +0200618 { }
619};
620
621U_BOOT_DRIVER(dwc3_generic_wrapper) = {
622 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblot3b838292019-07-05 09:33:58 +0200623 .id = UCLASS_NOP,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100624 .of_match = dwc3_glue_ids,
625 .bind = dwc3_glue_bind,
626 .probe = dwc3_glue_probe,
627 .remove = dwc3_glue_remove,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700628 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100629
Michal Simek49d67452018-05-18 13:15:06 +0200630};