blob: 7a00529a2a8e055d471d21b8c90cd8829b7a2ff9 [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
Caleb Connolly850b3072024-03-20 14:30:47 +0000428/* USB QSCRATCH Hardware registers */
429#define QSCRATCH_GENERAL_CFG 0x08
430#define PIPE_UTMI_CLK_SEL BIT(0)
431#define PIPE3_PHYSTATUS_SW BIT(3)
432#define PIPE_UTMI_CLK_DIS BIT(8)
433
434#define QSCRATCH_HS_PHY_CTRL 0x10
435#define UTMI_OTG_VBUS_VALID BIT(20)
436#define SW_SESSVLD_SEL BIT(28)
437
438#define QSCRATCH_SS_PHY_CTRL 0x30
439#define LANE0_PWR_PRESENT BIT(24)
440
441#define PWR_EVNT_IRQ_STAT_REG 0x58
442#define PWR_EVNT_LPM_IN_L2_MASK BIT(4)
443#define PWR_EVNT_LPM_OUT_L2_MASK BIT(5)
444
445#define SDM845_QSCRATCH_BASE_OFFSET 0xf8800
446#define SDM845_QSCRATCH_SIZE 0x400
447#define SDM845_DWC3_CORE_SIZE 0xcd00
448
449static void dwc3_qcom_vbus_override_enable(void __iomem *qscratch_base, bool enable)
450{
451 if (enable) {
452 setbits_le32(qscratch_base + QSCRATCH_SS_PHY_CTRL,
453 LANE0_PWR_PRESENT);
454 setbits_le32(qscratch_base + QSCRATCH_HS_PHY_CTRL,
455 UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
456 } else {
457 clrbits_le32(qscratch_base + QSCRATCH_SS_PHY_CTRL,
458 LANE0_PWR_PRESENT);
459 clrbits_le32(qscratch_base + QSCRATCH_HS_PHY_CTRL,
460 UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
461 }
462}
463
464/* For controllers running without superspeed PHYs */
465static void dwc3_qcom_select_utmi_clk(void __iomem *qscratch_base)
466{
467 /* Configure dwc3 to use UTMI clock as PIPE clock not present */
468 setbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG,
469 PIPE_UTMI_CLK_DIS);
470
471 setbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG,
472 PIPE_UTMI_CLK_SEL | PIPE3_PHYSTATUS_SW);
473
474 clrbits_le32(qscratch_base + QSCRATCH_GENERAL_CFG,
475 PIPE_UTMI_CLK_DIS);
476}
477
478static void dwc3_qcom_glue_configure(struct udevice *dev, int index,
479 enum usb_dr_mode mode)
480{
481 struct dwc3_glue_data *glue = dev_get_plat(dev);
482 void __iomem *qscratch_base = map_physmem(glue->regs, 0x400, MAP_NOCACHE);
483 if (IS_ERR_OR_NULL(qscratch_base)) {
484 log_err("%s: Invalid qscratch base address\n", dev->name);
485 return;
486 }
487
488 if (dev_read_bool(dev, "qcom,select-utmi-as-pipe-clk"))
489 dwc3_qcom_select_utmi_clk(qscratch_base);
490
491 if (mode != USB_DR_MODE_HOST)
492 dwc3_qcom_vbus_override_enable(qscratch_base, true);
493}
494
495struct dwc3_glue_ops qcom_ops = {
496 .glue_configure = dwc3_qcom_glue_configure,
497};
498
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000499static int dwc3_rk_glue_get_ctrl_dev(struct udevice *dev, ofnode *node)
500{
501 *node = dev_ofnode(dev);
502 if (!ofnode_valid(*node))
503 return -EINVAL;
504
505 return 0;
506}
507
508struct dwc3_glue_ops rk_ops = {
509 .glue_get_ctrl_dev = dwc3_rk_glue_get_ctrl_dev,
510};
511
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900512static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)
513{
514 const char *name = ofnode_get_name(node);
Jonas Karlman6913c302023-07-30 22:59:56 +0000515 const char *driver;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900516 enum usb_dr_mode dr_mode;
517 struct udevice *dev;
518 int ret;
519
520 debug("%s: subnode name: %s\n", __func__, name);
521
522 /* if the parent node doesn't have a mode check the leaf */
523 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
524 if (!dr_mode)
525 dr_mode = usb_get_dr_mode(node);
526
Jonas Karlman6913c302023-07-30 22:59:56 +0000527 if (CONFIG_IS_ENABLED(DM_USB_GADGET) &&
528 (dr_mode == USB_DR_MODE_PERIPHERAL || dr_mode == USB_DR_MODE_OTG)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900529 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
530 driver = "dwc3-generic-peripheral";
Jonas Karlman6913c302023-07-30 22:59:56 +0000531 } else if (CONFIG_IS_ENABLED(USB_HOST) && dr_mode == USB_DR_MODE_HOST) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900532 debug("%s: dr_mode: HOST\n", __func__);
533 driver = "dwc3-generic-host";
Jonas Karlman6913c302023-07-30 22:59:56 +0000534 } else {
535 debug("%s: unsupported dr_mode %d\n", __func__, dr_mode);
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900536 return -ENODEV;
Jonas Karlman6913c302023-07-30 22:59:56 +0000537 }
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900538
539 ret = device_bind_driver_to_node(parent, driver, name,
540 node, &dev);
541 if (ret) {
542 debug("%s: not able to bind usb device mode\n",
543 __func__);
544 return ret;
545 }
546
547 return 0;
548}
549
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900550int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200551{
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900552 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent);
Kever Yangac28e592020-03-04 08:59:50 +0800553 ofnode node;
Michal Simek49d67452018-05-18 13:15:06 +0200554 int ret;
Angus Ainsliec08db052022-02-02 15:08:54 -0800555
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900556 if (ops && ops->glue_get_ctrl_dev) {
557 ret = ops->glue_get_ctrl_dev(parent, &node);
558 if (ret)
559 return ret;
560
561 return dwc3_glue_bind_common(parent, node);
562 }
Michal Simek49d67452018-05-18 13:15:06 +0200563
Simon Glassf10643c2020-12-19 10:40:14 -0700564 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900565 ret = dwc3_glue_bind_common(parent, node);
566 if (ret == -ENXIO)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100567 continue;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900568 if (ret)
Michal Simek49d67452018-05-18 13:15:06 +0200569 return ret;
Michal Simek49d67452018-05-18 13:15:06 +0200570 }
571
572 return 0;
573}
574
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100575static int dwc3_glue_reset_init(struct udevice *dev,
576 struct dwc3_glue_data *glue)
577{
578 int ret;
579
580 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530581 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100582 return 0;
583 else if (ret)
584 return ret;
585
Caleb Connolly850b3072024-03-20 14:30:47 +0000586 if (device_is_compatible(dev, "qcom,dwc3")) {
587 reset_assert_bulk(&glue->resets);
588 /* We should wait at least 6 sleep clock cycles, that's
589 * (6 / 32764) * 1000000 ~= 200us. But some platforms
590 * have slower sleep clocks so we'll play it safe.
591 */
592 udelay(500);
593 }
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100594 ret = reset_deassert_bulk(&glue->resets);
595 if (ret) {
596 reset_release_bulk(&glue->resets);
597 return ret;
598 }
599
600 return 0;
601}
602
603static int dwc3_glue_clk_init(struct udevice *dev,
604 struct dwc3_glue_data *glue)
605{
606 int ret;
607
608 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530609 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100610 return 0;
611 if (ret)
612 return ret;
613
614#if CONFIG_IS_ENABLED(CLK)
615 ret = clk_enable_bulk(&glue->clks);
616 if (ret) {
617 clk_release_bulk(&glue->clks);
618 return ret;
619 }
620#endif
621
622 return 0;
623}
624
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900625int dwc3_glue_probe(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100626{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100627 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700628 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100629 struct udevice *child = NULL;
630 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100631 int ret;
Michal Simek142d50f2022-03-09 10:05:45 +0100632 struct phy phy;
633
634 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
635 if (!ret) {
636 ret = generic_phy_init(&phy);
637 if (ret)
638 return ret;
Jan Kiszka868d58f2022-04-25 13:26:45 +0200639 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simek142d50f2022-03-09 10:05:45 +0100640 debug("could not get phy (err %d)\n", ret);
641 return ret;
642 }
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100643
Kunihiko Hayashi211a0662023-02-20 14:50:29 +0900644 glue->regs = dev_read_addr_size_index(dev, 0, &glue->size);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100645
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100646 ret = dwc3_glue_clk_init(dev, glue);
647 if (ret)
648 return ret;
649
650 ret = dwc3_glue_reset_init(dev, glue);
651 if (ret)
652 return ret;
653
Jonas Karlman5ccfdd82023-08-31 22:16:36 +0000654 if (generic_phy_valid(&phy)) {
Michal Simek142d50f2022-03-09 10:05:45 +0100655 ret = generic_phy_power_on(&phy);
656 if (ret)
657 return ret;
658 }
659
Jonas Karlman4412a2b2023-07-30 22:59:55 +0000660 device_find_first_child(dev, &child);
661 if (!child)
662 return 0;
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100663
Kunihiko Hayashi7c71c682023-02-20 14:50:27 +0900664 if (glue->clks.count == 0) {
665 ret = dwc3_glue_clk_init(child, glue);
666 if (ret)
667 return ret;
668 }
669
Frank Wang5d422ab2020-05-26 11:34:31 +0800670 if (glue->resets.count == 0) {
671 ret = dwc3_glue_reset_init(child, glue);
672 if (ret)
673 return ret;
674 }
675
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100676 while (child) {
677 enum usb_dr_mode dr_mode;
678
Simon Glassf10643c2020-12-19 10:40:14 -0700679 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100680 device_find_next_child(&child);
Marek Vasutf1ef9552022-04-13 00:42:55 +0200681 if (ops && ops->glue_configure)
682 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100683 index++;
684 }
685
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100686 return 0;
687}
688
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900689int dwc3_glue_remove(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100690{
Simon Glassc69cda22020-12-03 16:55:20 -0700691 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100692
693 reset_release_bulk(&glue->resets);
694
695 clk_release_bulk(&glue->clks);
696
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200697 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100698}
699
700static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200701 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu648856a2020-05-12 08:36:01 +0200702 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100703 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100704 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100705 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendracab4e272019-12-09 10:37:29 +0530706 { .compatible = "ti,am654-dwc3" },
Jagan Teki185571b2023-06-06 22:39:14 +0530707 { .compatible = "rockchip,rk3328-dwc3", .data = (ulong)&rk_ops },
Frank Wang5d422ab2020-05-26 11:34:31 +0800708 { .compatible = "rockchip,rk3399-dwc3" },
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000709 { .compatible = "rockchip,rk3568-dwc3", .data = (ulong)&rk_ops },
Jonas Karlman6e91df92023-11-12 15:25:25 +0000710 { .compatible = "rockchip,rk3588-dwc3", .data = (ulong)&rk_ops },
Caleb Connolly850b3072024-03-20 14:30:47 +0000711 { .compatible = "qcom,dwc3", .data = (ulong)&qcom_ops },
Marek Vasutd0f7a052022-04-13 00:42:56 +0200712 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainsliec08db052022-02-02 15:08:54 -0800713 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko23cdbba2020-12-03 19:45:01 +0200714 { .compatible = "intel,tangier-dwc3" },
Michal Simek49d67452018-05-18 13:15:06 +0200715 { }
716};
717
718U_BOOT_DRIVER(dwc3_generic_wrapper) = {
719 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblot3b838292019-07-05 09:33:58 +0200720 .id = UCLASS_NOP,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100721 .of_match = dwc3_glue_ids,
722 .bind = dwc3_glue_bind,
723 .probe = dwc3_glue_probe,
724 .remove = dwc3_glue_remove,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700725 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100726
Michal Simek49d67452018-05-18 13:15:06 +0200727};