blob: e66e7961c70f33779e4c2f87214c5394ab97e44b [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>
Caleb Connollyc66c92c2023-09-12 22:01:31 +010024#include <power/regulator.h>
Michal Simek49d67452018-05-18 13:15:06 +020025#include <usb.h>
26#include "core.h"
27#include "gadget.h"
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010028#include <reset.h>
29#include <clk.h>
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +020030#include <usb/xhci.h>
T Karthik Reddyb252d792022-07-08 11:21:59 +020031#include <asm/gpio.h>
Michal Simek49d67452018-05-18 13:15:06 +020032
Kunihiko Hayashief2313b2023-02-20 14:50:28 +090033#include "dwc3-generic.h"
Frank Wang5d422ab2020-05-26 11:34:31 +080034
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020035struct dwc3_generic_plat {
36 fdt_addr_t base;
37 u32 maximum_speed;
38 enum usb_dr_mode dr_mode;
39};
40
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020041struct dwc3_generic_priv {
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020042 void *base;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010043 struct dwc3 dwc3;
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +020044 struct phy_bulk phys;
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +053045 struct gpio_desc *ulpi_reset;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010046};
47
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +020048struct dwc3_generic_host_priv {
49 struct xhci_ctrl xhci_ctrl;
50 struct dwc3_generic_priv gen_priv;
Caleb Connollyc66c92c2023-09-12 22:01:31 +010051 struct udevice *vbus_dev;
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +020052};
53
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020054static int dwc3_generic_probe(struct udevice *dev,
55 struct dwc3_generic_priv *priv)
Michal Simek49d67452018-05-18 13:15:06 +020056{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010057 int rc;
Simon Glassc69cda22020-12-03 16:55:20 -070058 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010059 struct dwc3 *dwc3 = &priv->dwc3;
Simon Glassc69cda22020-12-03 16:55:20 -070060 struct dwc3_glue_data *glue = dev_get_plat(dev->parent);
Marek Vasut8ae84e62022-11-27 15:31:52 +010061 int __maybe_unused index;
62 ofnode __maybe_unused node;
Michal Simek49d67452018-05-18 13:15:06 +020063
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020064 dwc3->dev = dev;
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020065 dwc3->maximum_speed = plat->maximum_speed;
66 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020067#if CONFIG_IS_ENABLED(OF_CONTROL)
68 dwc3_of_parse(dwc3);
Marek Vasut8ae84e62022-11-27 15:31:52 +010069
Marek Vasutc6583352023-02-20 14:50:25 +090070 /*
71 * There are currently four disparate placement possibilities of DWC3
72 * reference clock phandle in SoC DTs:
73 * - in top level glue node, with generic subnode without clock (ZynqMP)
74 * - in top level generic node, with no subnode (i.MX8MQ)
75 * - in generic subnode, with other clock in top level node (i.MX8MP)
76 * - in both top level node and generic subnode (Rockchip)
77 * Cover all the possibilities here by looking into both nodes, start
78 * with the top level node as that seems to be used in majority of DTs
79 * to reference the clock.
80 */
Marek Vasut8ae84e62022-11-27 15:31:52 +010081 node = dev_ofnode(dev->parent);
82 index = ofnode_stringlist_search(node, "clock-names", "ref");
83 if (index < 0)
84 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
Marek Vasutc6583352023-02-20 14:50:25 +090085 if (index < 0) {
86 node = dev_ofnode(dev);
87 index = ofnode_stringlist_search(node, "clock-names", "ref");
88 if (index < 0)
89 index = ofnode_stringlist_search(node, "clock-names", "ref_clk");
90 }
Marek Vasut8ae84e62022-11-27 15:31:52 +010091 if (index >= 0)
92 dwc3->ref_clk = &glue->clks.clks[index];
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020093#endif
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020094
Frank Wang5d422ab2020-05-26 11:34:31 +080095 /*
96 * It must hold whole USB3.0 OTG controller in resetting to hold pipe
97 * power state in P2 before initializing TypeC PHY on RK3399 platform.
98 */
99 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) {
100 reset_assert_bulk(&glue->resets);
101 udelay(1);
102 }
103
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +0200104 rc = dwc3_setup_phy(dev, &priv->phys);
Siva Durga Prasad Paladugue7f9e1f2020-10-21 14:17:31 +0200105 if (rc && rc != -ENOTSUPP)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100106 return rc;
107
T Karthik Reddyb252d792022-07-08 11:21:59 +0200108 if (CONFIG_IS_ENABLED(DM_GPIO) &&
109 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3")) {
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530110 priv->ulpi_reset = devm_gpiod_get_optional(dev->parent, "reset",
Peter Korsgaardd266d4b2023-06-28 14:26:48 +0200111 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530112 /* property is optional, don't return error! */
113 if (priv->ulpi_reset) {
114 /* Toggle ulpi to reset the phy. */
115 rc = dm_gpio_set_value(priv->ulpi_reset, 1);
116 if (rc)
117 return rc;
T Karthik Reddyb252d792022-07-08 11:21:59 +0200118
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530119 mdelay(5);
T Karthik Reddyb252d792022-07-08 11:21:59 +0200120
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530121 rc = dm_gpio_set_value(priv->ulpi_reset, 0);
122 if (rc)
123 return rc;
T Karthik Reddyb252d792022-07-08 11:21:59 +0200124
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530125 mdelay(5);
126 }
T Karthik Reddyb252d792022-07-08 11:21:59 +0200127 }
128
Frank Wang5d422ab2020-05-26 11:34:31 +0800129 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3"))
130 reset_deassert_bulk(&glue->resets);
131
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200132 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
133 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +0200134
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100135
136 rc = dwc3_init(dwc3);
137 if (rc) {
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200138 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100139 return rc;
140 }
141
142 return 0;
Michal Simek49d67452018-05-18 13:15:06 +0200143}
144
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200145static int dwc3_generic_remove(struct udevice *dev,
146 struct dwc3_generic_priv *priv)
Michal Simek49d67452018-05-18 13:15:06 +0200147{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100148 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +0200149
T Karthik Reddyb252d792022-07-08 11:21:59 +0200150 if (CONFIG_IS_ENABLED(DM_GPIO) &&
Venkatesh Yadav Abbarapu9871b0e2023-08-09 09:03:50 +0530151 device_is_compatible(dev->parent, "xlnx,zynqmp-dwc3") &&
152 priv->ulpi_reset) {
Venkatesh Yadav Abbarapu237d1f62023-01-13 10:42:02 +0530153 struct gpio_desc *ulpi_reset = priv->ulpi_reset;
T Karthik Reddyb252d792022-07-08 11:21:59 +0200154
155 dm_gpio_free(ulpi_reset->dev, ulpi_reset);
156 }
157
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100158 dwc3_remove(dwc3);
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +0200159 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100160 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek49d67452018-05-18 13:15:06 +0200161
162 return 0;
163}
164
Simon Glassd1998a92020-12-03 16:55:21 -0700165static int dwc3_generic_of_to_plat(struct udevice *dev)
Michal Simek49d67452018-05-18 13:15:06 +0200166{
Simon Glassc69cda22020-12-03 16:55:20 -0700167 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Simon Glassf10643c2020-12-19 10:40:14 -0700168 ofnode node = dev_ofnode(dev);
Michal Simek49d67452018-05-18 13:15:06 +0200169
Angus Ainsliec08db052022-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 Simek49d67452018-05-18 13:15:06 +0200176
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200177 plat->maximum_speed = usb_get_maximum_speed(node);
178 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot1a63e5e2019-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 Simek49d67452018-05-18 13:15:06 +0200181 }
182
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200183 plat->dr_mode = usb_get_dr_mode(node);
184 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Angus Ainsliec08db052022-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 Simek49d67452018-05-18 13:15:06 +0200192 }
193
194 return 0;
195}
196
Jean-Jacques Hiblot1af590d2019-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 Simek49d67452018-05-18 13:15:06 +0200222U_BOOT_DRIVER(dwc3_generic_peripheral) = {
223 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100224 .id = UCLASS_USB_GADGET_GENERIC,
Simon Glassd1998a92020-12-03 16:55:21 -0700225 .of_to_plat = dwc3_generic_of_to_plat,
Michal Simek49d67452018-05-18 13:15:06 +0200226 .probe = dwc3_generic_peripheral_probe,
227 .remove = dwc3_generic_peripheral_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700228 .priv_auto = sizeof(struct dwc3_generic_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700229 .plat_auto = sizeof(struct dwc3_generic_plat),
Michal Simek49d67452018-05-18 13:15:06 +0200230};
Jean-Jacques Hiblot687ab542018-11-29 10:52:42 +0100231#endif
Michal Simek49d67452018-05-18 13:15:06 +0200232
Jonas Karlman6913c302023-07-30 22:59:56 +0000233#if CONFIG_IS_ENABLED(USB_HOST)
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200234static int dwc3_generic_host_probe(struct udevice *dev)
235{
236 struct xhci_hcor *hcor;
237 struct xhci_hccr *hccr;
238 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
239 int rc;
240
241 rc = dwc3_generic_probe(dev, &priv->gen_priv);
242 if (rc)
243 return rc;
244
Caleb Connollyc66c92c2023-09-12 22:01:31 +0100245 rc = device_get_supply_regulator(dev, "vbus-supply", &priv->vbus_dev);
246 if (rc)
247 debug("%s: No vbus regulator found: %d\n", dev->name, rc);
248
249 if (priv->vbus_dev)
250 regulator_set_enable(priv->vbus_dev, true);
251
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200252 hccr = (struct xhci_hccr *)priv->gen_priv.base;
253 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
254 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
255
256 return xhci_register(dev, hccr, hcor);
257}
258
259static int dwc3_generic_host_remove(struct udevice *dev)
260{
261 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
262 int rc;
263
264 rc = xhci_deregister(dev);
265 if (rc)
266 return rc;
267
Caleb Connollyc66c92c2023-09-12 22:01:31 +0100268 if (priv->vbus_dev)
269 regulator_set_enable(priv->vbus_dev, false);
270
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200271 return dwc3_generic_remove(dev, &priv->gen_priv);
272}
273
274U_BOOT_DRIVER(dwc3_generic_host) = {
275 .name = "dwc3-generic-host",
276 .id = UCLASS_USB,
Simon Glassd1998a92020-12-03 16:55:21 -0700277 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200278 .probe = dwc3_generic_host_probe,
279 .remove = dwc3_generic_host_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700280 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700281 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200282 .ops = &xhci_usb_ops,
283 .flags = DM_FLAG_ALLOC_PRIV_DMA,
284};
285#endif
286
Marek Vasutd0f7a052022-04-13 00:42:56 +0200287void dwc3_imx8mp_glue_configure(struct udevice *dev, int index,
288 enum usb_dr_mode mode)
289{
290/* USB glue registers */
291#define USB_CTRL0 0x00
292#define USB_CTRL1 0x04
293
294#define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
295#define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
296#define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
297
298#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
299#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
300 fdt_addr_t regs = dev_read_addr_index(dev, 1);
301 void *base = map_physmem(regs, 0x8, MAP_NOCACHE);
302 u32 value;
303
304 value = readl(base + USB_CTRL0);
305
306 if (dev_read_bool(dev, "fsl,permanently-attached"))
307 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
308 else
309 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
310
311 if (dev_read_bool(dev, "fsl,disable-port-power-control"))
312 value &= ~(USB_CTRL0_PORTPWR_EN);
313 else
314 value |= USB_CTRL0_PORTPWR_EN;
315
316 writel(value, base + USB_CTRL0);
317
318 value = readl(base + USB_CTRL1);
319 if (dev_read_bool(dev, "fsl,over-current-active-low"))
320 value |= USB_CTRL1_OC_POLARITY;
321 else
322 value &= ~USB_CTRL1_OC_POLARITY;
323
324 if (dev_read_bool(dev, "fsl,power-active-low"))
325 value |= USB_CTRL1_PWR_POLARITY;
326 else
327 value &= ~USB_CTRL1_PWR_POLARITY;
328
329 writel(value, base + USB_CTRL1);
330
331 unmap_physmem(base, MAP_NOCACHE);
332}
333
334struct dwc3_glue_ops imx8mp_ops = {
335 .glue_configure = dwc3_imx8mp_glue_configure,
336};
337
Marek Vasutf1ef9552022-04-13 00:42:55 +0200338void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100339 enum usb_dr_mode mode)
340{
341#define USBOTGSS_UTMI_OTG_STATUS 0x0084
342#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
343
344/* UTMI_OTG_STATUS REGISTER */
345#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
346#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
347#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
348#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
349#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
350#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
351#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
352enum dwc3_omap_utmi_mode {
353 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
354 DWC3_OMAP_UTMI_MODE_HW,
355 DWC3_OMAP_UTMI_MODE_SW,
356};
357
358 u32 use_id_pin;
359 u32 host_mode;
360 u32 reg;
361 u32 utmi_mode;
362 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
363
Simon Glassc69cda22020-12-03 16:55:20 -0700364 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100365 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
366
367 if (device_is_compatible(dev, "ti,am437x-dwc3"))
368 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
369
370 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
371 DWC3_OMAP_UTMI_MODE_UNKNOWN);
372 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
373 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
374 dev->name);
375 mode = USB_DR_MODE_PERIPHERAL;
376 }
377
378 switch (mode) {
379 case USB_DR_MODE_PERIPHERAL:
380 use_id_pin = 0;
381 host_mode = 0;
382 break;
383 case USB_DR_MODE_HOST:
384 use_id_pin = 0;
385 host_mode = 1;
386 break;
387 case USB_DR_MODE_OTG:
388 default:
389 use_id_pin = 1;
390 host_mode = 0;
391 break;
392 }
393
394 reg = readl(base + utmi_status_offset);
395
396 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
397 if (!use_id_pin)
398 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
399
400 writel(reg, base + utmi_status_offset);
401
402 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
403 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
404 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
405
406 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
407 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
408
409 if (!host_mode)
410 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
411 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
412
413 writel(reg, base + utmi_status_offset);
414
415 unmap_physmem(base, MAP_NOCACHE);
416}
417
418struct dwc3_glue_ops ti_ops = {
Marek Vasutf1ef9552022-04-13 00:42:55 +0200419 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100420};
421
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000422static int dwc3_rk_glue_get_ctrl_dev(struct udevice *dev, ofnode *node)
423{
424 *node = dev_ofnode(dev);
425 if (!ofnode_valid(*node))
426 return -EINVAL;
427
428 return 0;
429}
430
431struct dwc3_glue_ops rk_ops = {
432 .glue_get_ctrl_dev = dwc3_rk_glue_get_ctrl_dev,
433};
434
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900435static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)
436{
437 const char *name = ofnode_get_name(node);
Jonas Karlman6913c302023-07-30 22:59:56 +0000438 const char *driver;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900439 enum usb_dr_mode dr_mode;
440 struct udevice *dev;
441 int ret;
442
443 debug("%s: subnode name: %s\n", __func__, name);
444
445 /* if the parent node doesn't have a mode check the leaf */
446 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
447 if (!dr_mode)
448 dr_mode = usb_get_dr_mode(node);
449
Jonas Karlman6913c302023-07-30 22:59:56 +0000450 if (CONFIG_IS_ENABLED(DM_USB_GADGET) &&
451 (dr_mode == USB_DR_MODE_PERIPHERAL || dr_mode == USB_DR_MODE_OTG)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900452 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
453 driver = "dwc3-generic-peripheral";
Jonas Karlman6913c302023-07-30 22:59:56 +0000454 } else if (CONFIG_IS_ENABLED(USB_HOST) && dr_mode == USB_DR_MODE_HOST) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900455 debug("%s: dr_mode: HOST\n", __func__);
456 driver = "dwc3-generic-host";
Jonas Karlman6913c302023-07-30 22:59:56 +0000457 } else {
458 debug("%s: unsupported dr_mode %d\n", __func__, dr_mode);
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900459 return -ENODEV;
Jonas Karlman6913c302023-07-30 22:59:56 +0000460 }
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900461
462 ret = device_bind_driver_to_node(parent, driver, name,
463 node, &dev);
464 if (ret) {
465 debug("%s: not able to bind usb device mode\n",
466 __func__);
467 return ret;
468 }
469
470 return 0;
471}
472
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900473int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200474{
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900475 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent);
Kever Yangac28e592020-03-04 08:59:50 +0800476 ofnode node;
Michal Simek49d67452018-05-18 13:15:06 +0200477 int ret;
Angus Ainsliec08db052022-02-02 15:08:54 -0800478
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900479 if (ops && ops->glue_get_ctrl_dev) {
480 ret = ops->glue_get_ctrl_dev(parent, &node);
481 if (ret)
482 return ret;
483
484 return dwc3_glue_bind_common(parent, node);
485 }
Michal Simek49d67452018-05-18 13:15:06 +0200486
Simon Glassf10643c2020-12-19 10:40:14 -0700487 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900488 ret = dwc3_glue_bind_common(parent, node);
489 if (ret == -ENXIO)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100490 continue;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900491 if (ret)
Michal Simek49d67452018-05-18 13:15:06 +0200492 return ret;
Michal Simek49d67452018-05-18 13:15:06 +0200493 }
494
495 return 0;
496}
497
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100498static int dwc3_glue_reset_init(struct udevice *dev,
499 struct dwc3_glue_data *glue)
500{
501 int ret;
502
503 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530504 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100505 return 0;
506 else if (ret)
507 return ret;
508
509 ret = reset_deassert_bulk(&glue->resets);
510 if (ret) {
511 reset_release_bulk(&glue->resets);
512 return ret;
513 }
514
515 return 0;
516}
517
518static int dwc3_glue_clk_init(struct udevice *dev,
519 struct dwc3_glue_data *glue)
520{
521 int ret;
522
523 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530524 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100525 return 0;
526 if (ret)
527 return ret;
528
529#if CONFIG_IS_ENABLED(CLK)
530 ret = clk_enable_bulk(&glue->clks);
531 if (ret) {
532 clk_release_bulk(&glue->clks);
533 return ret;
534 }
535#endif
536
537 return 0;
538}
539
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900540int dwc3_glue_probe(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100541{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100542 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700543 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100544 struct udevice *child = NULL;
545 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100546 int ret;
Michal Simek142d50f2022-03-09 10:05:45 +0100547 struct phy phy;
548
549 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
550 if (!ret) {
551 ret = generic_phy_init(&phy);
552 if (ret)
553 return ret;
Jan Kiszka868d58f2022-04-25 13:26:45 +0200554 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simek142d50f2022-03-09 10:05:45 +0100555 debug("could not get phy (err %d)\n", ret);
556 return ret;
557 }
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100558
Kunihiko Hayashi211a0662023-02-20 14:50:29 +0900559 glue->regs = dev_read_addr_size_index(dev, 0, &glue->size);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100560
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100561 ret = dwc3_glue_clk_init(dev, glue);
562 if (ret)
563 return ret;
564
565 ret = dwc3_glue_reset_init(dev, glue);
566 if (ret)
567 return ret;
568
Jonas Karlman5ccfdd82023-08-31 22:16:36 +0000569 if (generic_phy_valid(&phy)) {
Michal Simek142d50f2022-03-09 10:05:45 +0100570 ret = generic_phy_power_on(&phy);
571 if (ret)
572 return ret;
573 }
574
Jonas Karlman4412a2b2023-07-30 22:59:55 +0000575 device_find_first_child(dev, &child);
576 if (!child)
577 return 0;
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100578
Kunihiko Hayashi7c71c682023-02-20 14:50:27 +0900579 if (glue->clks.count == 0) {
580 ret = dwc3_glue_clk_init(child, glue);
581 if (ret)
582 return ret;
583 }
584
Frank Wang5d422ab2020-05-26 11:34:31 +0800585 if (glue->resets.count == 0) {
586 ret = dwc3_glue_reset_init(child, glue);
587 if (ret)
588 return ret;
589 }
590
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100591 while (child) {
592 enum usb_dr_mode dr_mode;
593
Simon Glassf10643c2020-12-19 10:40:14 -0700594 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100595 device_find_next_child(&child);
Marek Vasutf1ef9552022-04-13 00:42:55 +0200596 if (ops && ops->glue_configure)
597 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100598 index++;
599 }
600
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100601 return 0;
602}
603
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900604int dwc3_glue_remove(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100605{
Simon Glassc69cda22020-12-03 16:55:20 -0700606 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100607
608 reset_release_bulk(&glue->resets);
609
610 clk_release_bulk(&glue->clks);
611
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200612 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100613}
614
615static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200616 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu648856a2020-05-12 08:36:01 +0200617 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100618 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100619 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100620 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendracab4e272019-12-09 10:37:29 +0530621 { .compatible = "ti,am654-dwc3" },
Jagan Teki185571b2023-06-06 22:39:14 +0530622 { .compatible = "rockchip,rk3328-dwc3", .data = (ulong)&rk_ops },
Frank Wang5d422ab2020-05-26 11:34:31 +0800623 { .compatible = "rockchip,rk3399-dwc3" },
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000624 { .compatible = "rockchip,rk3568-dwc3", .data = (ulong)&rk_ops },
Robert Marko74a703a2020-09-10 16:00:05 +0200625 { .compatible = "qcom,dwc3" },
Marek Vasutd0f7a052022-04-13 00:42:56 +0200626 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainsliec08db052022-02-02 15:08:54 -0800627 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko23cdbba2020-12-03 19:45:01 +0200628 { .compatible = "intel,tangier-dwc3" },
Michal Simek49d67452018-05-18 13:15:06 +0200629 { }
630};
631
632U_BOOT_DRIVER(dwc3_generic_wrapper) = {
633 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblot3b838292019-07-05 09:33:58 +0200634 .id = UCLASS_NOP,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100635 .of_match = dwc3_glue_ids,
636 .bind = dwc3_glue_bind,
637 .probe = dwc3_glue_probe,
638 .remove = dwc3_glue_remove,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700639 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100640
Michal Simek49d67452018-05-18 13:15:06 +0200641};