blob: a379a0002e7742c8392f8b84ae40a658a7285f2c [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 Connollyde451d52024-02-26 17:26:06 +000024#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 Connollyde451d52024-02-26 17:26:06 +000051 struct udevice *vbus_supply;
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 Connollyde451d52024-02-26 17:26:06 +0000245 rc = device_get_supply_regulator(dev, "vbus-supply", &priv->vbus_supply);
246 if (rc)
247 debug("%s: No vbus regulator found: %d\n", dev->name, rc);
248
249 /* Only returns an error if regulator is valid and failed to enable due to a driver issue */
250 rc = regulator_set_enable_if_allowed(priv->vbus_supply, true);
251 if (rc)
252 return rc;
253
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200254 hccr = (struct xhci_hccr *)priv->gen_priv.base;
255 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
256 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
257
Caleb Connollyde451d52024-02-26 17:26:06 +0000258 rc = xhci_register(dev, hccr, hcor);
259 if (rc)
260 regulator_set_enable_if_allowed(priv->vbus_supply, false);
261
262 return rc;
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200263}
264
265static int dwc3_generic_host_remove(struct udevice *dev)
266{
267 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
268 int rc;
269
Caleb Connollyde451d52024-02-26 17:26:06 +0000270 /* This function always returns 0 */
271 xhci_deregister(dev);
272
273 rc = regulator_set_enable_if_allowed(priv->vbus_supply, false);
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200274 if (rc)
Caleb Connollyde451d52024-02-26 17:26:06 +0000275 debug("%s: Failed to disable vbus regulator: %d\n", dev->name, rc);
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200276
277 return dwc3_generic_remove(dev, &priv->gen_priv);
278}
279
280U_BOOT_DRIVER(dwc3_generic_host) = {
281 .name = "dwc3-generic-host",
282 .id = UCLASS_USB,
Simon Glassd1998a92020-12-03 16:55:21 -0700283 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200284 .probe = dwc3_generic_host_probe,
285 .remove = dwc3_generic_host_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700286 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700287 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200288 .ops = &xhci_usb_ops,
289 .flags = DM_FLAG_ALLOC_PRIV_DMA,
290};
291#endif
292
Marek Vasutd0f7a052022-04-13 00:42:56 +0200293void dwc3_imx8mp_glue_configure(struct udevice *dev, int index,
294 enum usb_dr_mode mode)
295{
296/* USB glue registers */
297#define USB_CTRL0 0x00
298#define USB_CTRL1 0x04
299
300#define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
301#define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
302#define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
303
304#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
305#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
306 fdt_addr_t regs = dev_read_addr_index(dev, 1);
307 void *base = map_physmem(regs, 0x8, MAP_NOCACHE);
308 u32 value;
309
310 value = readl(base + USB_CTRL0);
311
312 if (dev_read_bool(dev, "fsl,permanently-attached"))
313 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
314 else
315 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
316
317 if (dev_read_bool(dev, "fsl,disable-port-power-control"))
318 value &= ~(USB_CTRL0_PORTPWR_EN);
319 else
320 value |= USB_CTRL0_PORTPWR_EN;
321
322 writel(value, base + USB_CTRL0);
323
324 value = readl(base + USB_CTRL1);
325 if (dev_read_bool(dev, "fsl,over-current-active-low"))
326 value |= USB_CTRL1_OC_POLARITY;
327 else
328 value &= ~USB_CTRL1_OC_POLARITY;
329
330 if (dev_read_bool(dev, "fsl,power-active-low"))
331 value |= USB_CTRL1_PWR_POLARITY;
332 else
333 value &= ~USB_CTRL1_PWR_POLARITY;
334
335 writel(value, base + USB_CTRL1);
336
337 unmap_physmem(base, MAP_NOCACHE);
338}
339
340struct dwc3_glue_ops imx8mp_ops = {
341 .glue_configure = dwc3_imx8mp_glue_configure,
342};
343
Marek Vasutf1ef9552022-04-13 00:42:55 +0200344void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100345 enum usb_dr_mode mode)
346{
347#define USBOTGSS_UTMI_OTG_STATUS 0x0084
348#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
349
350/* UTMI_OTG_STATUS REGISTER */
351#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
352#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
353#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
354#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
355#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
356#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
357#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
358enum dwc3_omap_utmi_mode {
359 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
360 DWC3_OMAP_UTMI_MODE_HW,
361 DWC3_OMAP_UTMI_MODE_SW,
362};
363
364 u32 use_id_pin;
365 u32 host_mode;
366 u32 reg;
367 u32 utmi_mode;
368 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
369
Simon Glassc69cda22020-12-03 16:55:20 -0700370 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100371 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
372
373 if (device_is_compatible(dev, "ti,am437x-dwc3"))
374 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
375
376 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
377 DWC3_OMAP_UTMI_MODE_UNKNOWN);
378 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
379 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
380 dev->name);
381 mode = USB_DR_MODE_PERIPHERAL;
382 }
383
384 switch (mode) {
385 case USB_DR_MODE_PERIPHERAL:
386 use_id_pin = 0;
387 host_mode = 0;
388 break;
389 case USB_DR_MODE_HOST:
390 use_id_pin = 0;
391 host_mode = 1;
392 break;
393 case USB_DR_MODE_OTG:
394 default:
395 use_id_pin = 1;
396 host_mode = 0;
397 break;
398 }
399
400 reg = readl(base + utmi_status_offset);
401
402 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
403 if (!use_id_pin)
404 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
405
406 writel(reg, base + utmi_status_offset);
407
408 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
409 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
410 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
411
412 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
413 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
414
415 if (!host_mode)
416 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
417 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
418
419 writel(reg, base + utmi_status_offset);
420
421 unmap_physmem(base, MAP_NOCACHE);
422}
423
424struct dwc3_glue_ops ti_ops = {
Marek Vasutf1ef9552022-04-13 00:42:55 +0200425 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100426};
427
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000428static int dwc3_rk_glue_get_ctrl_dev(struct udevice *dev, ofnode *node)
429{
430 *node = dev_ofnode(dev);
431 if (!ofnode_valid(*node))
432 return -EINVAL;
433
434 return 0;
435}
436
437struct dwc3_glue_ops rk_ops = {
438 .glue_get_ctrl_dev = dwc3_rk_glue_get_ctrl_dev,
439};
440
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900441static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)
442{
443 const char *name = ofnode_get_name(node);
Jonas Karlman6913c302023-07-30 22:59:56 +0000444 const char *driver;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900445 enum usb_dr_mode dr_mode;
446 struct udevice *dev;
447 int ret;
448
449 debug("%s: subnode name: %s\n", __func__, name);
450
451 /* if the parent node doesn't have a mode check the leaf */
452 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
453 if (!dr_mode)
454 dr_mode = usb_get_dr_mode(node);
455
Jonas Karlman6913c302023-07-30 22:59:56 +0000456 if (CONFIG_IS_ENABLED(DM_USB_GADGET) &&
457 (dr_mode == USB_DR_MODE_PERIPHERAL || dr_mode == USB_DR_MODE_OTG)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900458 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
459 driver = "dwc3-generic-peripheral";
Jonas Karlman6913c302023-07-30 22:59:56 +0000460 } else if (CONFIG_IS_ENABLED(USB_HOST) && dr_mode == USB_DR_MODE_HOST) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900461 debug("%s: dr_mode: HOST\n", __func__);
462 driver = "dwc3-generic-host";
Jonas Karlman6913c302023-07-30 22:59:56 +0000463 } else {
464 debug("%s: unsupported dr_mode %d\n", __func__, dr_mode);
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900465 return -ENODEV;
Jonas Karlman6913c302023-07-30 22:59:56 +0000466 }
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900467
468 ret = device_bind_driver_to_node(parent, driver, name,
469 node, &dev);
470 if (ret) {
471 debug("%s: not able to bind usb device mode\n",
472 __func__);
473 return ret;
474 }
475
476 return 0;
477}
478
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900479int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200480{
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900481 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent);
Kever Yangac28e592020-03-04 08:59:50 +0800482 ofnode node;
Michal Simek49d67452018-05-18 13:15:06 +0200483 int ret;
Angus Ainsliec08db052022-02-02 15:08:54 -0800484
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900485 if (ops && ops->glue_get_ctrl_dev) {
486 ret = ops->glue_get_ctrl_dev(parent, &node);
487 if (ret)
488 return ret;
489
490 return dwc3_glue_bind_common(parent, node);
491 }
Michal Simek49d67452018-05-18 13:15:06 +0200492
Simon Glassf10643c2020-12-19 10:40:14 -0700493 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900494 ret = dwc3_glue_bind_common(parent, node);
495 if (ret == -ENXIO)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100496 continue;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900497 if (ret)
Michal Simek49d67452018-05-18 13:15:06 +0200498 return ret;
Michal Simek49d67452018-05-18 13:15:06 +0200499 }
500
501 return 0;
502}
503
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100504static int dwc3_glue_reset_init(struct udevice *dev,
505 struct dwc3_glue_data *glue)
506{
507 int ret;
508
509 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530510 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100511 return 0;
512 else if (ret)
513 return ret;
514
515 ret = reset_deassert_bulk(&glue->resets);
516 if (ret) {
517 reset_release_bulk(&glue->resets);
518 return ret;
519 }
520
521 return 0;
522}
523
524static int dwc3_glue_clk_init(struct udevice *dev,
525 struct dwc3_glue_data *glue)
526{
527 int ret;
528
529 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530530 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100531 return 0;
532 if (ret)
533 return ret;
534
535#if CONFIG_IS_ENABLED(CLK)
536 ret = clk_enable_bulk(&glue->clks);
537 if (ret) {
538 clk_release_bulk(&glue->clks);
539 return ret;
540 }
541#endif
542
543 return 0;
544}
545
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900546int dwc3_glue_probe(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100547{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100548 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700549 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100550 struct udevice *child = NULL;
551 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100552 int ret;
Michal Simek142d50f2022-03-09 10:05:45 +0100553 struct phy phy;
554
555 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
556 if (!ret) {
557 ret = generic_phy_init(&phy);
558 if (ret)
559 return ret;
Jan Kiszka868d58f2022-04-25 13:26:45 +0200560 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simek142d50f2022-03-09 10:05:45 +0100561 debug("could not get phy (err %d)\n", ret);
562 return ret;
563 }
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100564
Kunihiko Hayashi211a0662023-02-20 14:50:29 +0900565 glue->regs = dev_read_addr_size_index(dev, 0, &glue->size);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100566
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100567 ret = dwc3_glue_clk_init(dev, glue);
568 if (ret)
569 return ret;
570
571 ret = dwc3_glue_reset_init(dev, glue);
572 if (ret)
573 return ret;
574
Jonas Karlman5ccfdd82023-08-31 22:16:36 +0000575 if (generic_phy_valid(&phy)) {
Michal Simek142d50f2022-03-09 10:05:45 +0100576 ret = generic_phy_power_on(&phy);
577 if (ret)
578 return ret;
579 }
580
Jonas Karlman4412a2b2023-07-30 22:59:55 +0000581 device_find_first_child(dev, &child);
582 if (!child)
583 return 0;
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100584
Kunihiko Hayashi7c71c682023-02-20 14:50:27 +0900585 if (glue->clks.count == 0) {
586 ret = dwc3_glue_clk_init(child, glue);
587 if (ret)
588 return ret;
589 }
590
Frank Wang5d422ab2020-05-26 11:34:31 +0800591 if (glue->resets.count == 0) {
592 ret = dwc3_glue_reset_init(child, glue);
593 if (ret)
594 return ret;
595 }
596
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100597 while (child) {
598 enum usb_dr_mode dr_mode;
599
Simon Glassf10643c2020-12-19 10:40:14 -0700600 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100601 device_find_next_child(&child);
Marek Vasutf1ef9552022-04-13 00:42:55 +0200602 if (ops && ops->glue_configure)
603 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100604 index++;
605 }
606
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100607 return 0;
608}
609
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900610int dwc3_glue_remove(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100611{
Simon Glassc69cda22020-12-03 16:55:20 -0700612 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100613
614 reset_release_bulk(&glue->resets);
615
616 clk_release_bulk(&glue->clks);
617
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200618 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100619}
620
621static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200622 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu648856a2020-05-12 08:36:01 +0200623 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100624 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100625 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100626 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendracab4e272019-12-09 10:37:29 +0530627 { .compatible = "ti,am654-dwc3" },
Jagan Teki185571b2023-06-06 22:39:14 +0530628 { .compatible = "rockchip,rk3328-dwc3", .data = (ulong)&rk_ops },
Frank Wang5d422ab2020-05-26 11:34:31 +0800629 { .compatible = "rockchip,rk3399-dwc3" },
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000630 { .compatible = "rockchip,rk3568-dwc3", .data = (ulong)&rk_ops },
Jonas Karlman6e91df92023-11-12 15:25:25 +0000631 { .compatible = "rockchip,rk3588-dwc3", .data = (ulong)&rk_ops },
Robert Marko74a703a2020-09-10 16:00:05 +0200632 { .compatible = "qcom,dwc3" },
Marek Vasutd0f7a052022-04-13 00:42:56 +0200633 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainsliec08db052022-02-02 15:08:54 -0800634 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko23cdbba2020-12-03 19:45:01 +0200635 { .compatible = "intel,tangier-dwc3" },
Michal Simek49d67452018-05-18 13:15:06 +0200636 { }
637};
638
639U_BOOT_DRIVER(dwc3_generic_wrapper) = {
640 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblot3b838292019-07-05 09:33:58 +0200641 .id = UCLASS_NOP,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100642 .of_match = dwc3_glue_ids,
643 .bind = dwc3_glue_bind,
644 .probe = dwc3_glue_probe,
645 .remove = dwc3_glue_remove,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700646 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100647
Michal Simek49d67452018-05-18 13:15:06 +0200648};