blob: bfa63317daf7e0389d06a0d8f8607fdc85e05f96 [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
Caleb Connolly896e5282023-09-12 22:00:12 +0100422/* USB QSCRATCH Hardware registers */
423#define QSCRATCH_HS_PHY_CTRL 0x10
424#define UTMI_OTG_VBUS_VALID BIT(20)
425#define SW_SESSVLD_SEL BIT(28)
426
427#define QSCRATCH_SS_PHY_CTRL 0x30
428#define LANE0_PWR_PRESENT BIT(24)
429
430#define QSCRATCH_GENERAL_CFG 0x08
431#define PIPE_UTMI_CLK_SEL BIT(0)
432#define PIPE3_PHYSTATUS_SW BIT(3)
433#define PIPE_UTMI_CLK_DIS BIT(8)
434
435#define PWR_EVNT_IRQ_STAT_REG 0x58
436#define PWR_EVNT_LPM_IN_L2_MASK BIT(4)
437#define PWR_EVNT_LPM_OUT_L2_MASK BIT(5)
438
439#define SDM845_QSCRATCH_BASE_OFFSET 0xf8800
440#define SDM845_QSCRATCH_SIZE 0x400
441#define SDM845_DWC3_CORE_SIZE 0xcd00
442static inline void dwc3_qcom_setbits(void __iomem *base, u32 offset, u32 val)
443{
444 u32 reg;
445
446 reg = readl(base + offset);
447 reg |= val;
448 writel(reg, base + offset);
449
450 /* ensure that above write is through */
451 readl(base + offset);
452}
453
454static inline void dwc3_qcom_clrbits(void __iomem *base, u32 offset, u32 val)
455{
456 u32 reg;
457
458 reg = readl(base + offset);
459 reg &= ~val;
460 writel(reg, base + offset);
461
462 /* ensure that above write is through */
463 readl(base + offset);
464}
465
466static void dwc3_qcom_vbus_override_enable(void __iomem *qscratch_base, bool enable)
467{
468 if (enable) {
469 dwc3_qcom_setbits(qscratch_base, QSCRATCH_SS_PHY_CTRL,
470 LANE0_PWR_PRESENT);
471 dwc3_qcom_setbits(qscratch_base, QSCRATCH_HS_PHY_CTRL,
472 UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
473 } else {
474 dwc3_qcom_clrbits(qscratch_base, QSCRATCH_SS_PHY_CTRL,
475 LANE0_PWR_PRESENT);
476 dwc3_qcom_clrbits(qscratch_base, QSCRATCH_HS_PHY_CTRL,
477 UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
478 }
479}
480
481static void dwc3_qcom_select_utmi_clk(void __iomem *qscratch_base)
482{
483
484 /* Configure dwc3 to use UTMI clock as PIPE clock not present */
485 dwc3_qcom_setbits(qscratch_base, QSCRATCH_GENERAL_CFG,
486 PIPE_UTMI_CLK_DIS);
487
488 udelay(500);
489
490 dwc3_qcom_setbits(qscratch_base, QSCRATCH_GENERAL_CFG,
491 PIPE_UTMI_CLK_SEL | PIPE3_PHYSTATUS_SW);
492
493 udelay(500);
494
495 dwc3_qcom_clrbits(qscratch_base, QSCRATCH_GENERAL_CFG,
496 PIPE_UTMI_CLK_DIS);
497}
498
499static void dwc3_qcom_glue_configure(struct udevice *dev, int index,
500 enum usb_dr_mode mode)
501{
502 void __iomem *qscratch_base = (void __iomem *)dev_read_addr(dev);
503
504 debug("%s: qscratch_base = %p mode %d\n", __func__, qscratch_base, mode);
505
506 if (dev_read_bool(dev, "qcom,select-utmi-as-pipe-clk"))
507 dwc3_qcom_select_utmi_clk(qscratch_base);
508
509 if (mode != USB_DR_MODE_HOST)
510 dwc3_qcom_vbus_override_enable(qscratch_base, true);
511}
512
513struct dwc3_glue_ops qcom_ops = {
514 .glue_configure = dwc3_qcom_glue_configure,
515};
516
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000517static int dwc3_rk_glue_get_ctrl_dev(struct udevice *dev, ofnode *node)
518{
519 *node = dev_ofnode(dev);
520 if (!ofnode_valid(*node))
521 return -EINVAL;
522
523 return 0;
524}
525
526struct dwc3_glue_ops rk_ops = {
527 .glue_get_ctrl_dev = dwc3_rk_glue_get_ctrl_dev,
528};
529
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900530static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)
531{
532 const char *name = ofnode_get_name(node);
Jonas Karlman6913c302023-07-30 22:59:56 +0000533 const char *driver;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900534 enum usb_dr_mode dr_mode;
535 struct udevice *dev;
536 int ret;
537
538 debug("%s: subnode name: %s\n", __func__, name);
539
540 /* if the parent node doesn't have a mode check the leaf */
541 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
542 if (!dr_mode)
543 dr_mode = usb_get_dr_mode(node);
544
Jonas Karlman6913c302023-07-30 22:59:56 +0000545 if (CONFIG_IS_ENABLED(DM_USB_GADGET) &&
546 (dr_mode == USB_DR_MODE_PERIPHERAL || dr_mode == USB_DR_MODE_OTG)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900547 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
548 driver = "dwc3-generic-peripheral";
Jonas Karlman6913c302023-07-30 22:59:56 +0000549 } else if (CONFIG_IS_ENABLED(USB_HOST) && dr_mode == USB_DR_MODE_HOST) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900550 debug("%s: dr_mode: HOST\n", __func__);
551 driver = "dwc3-generic-host";
Jonas Karlman6913c302023-07-30 22:59:56 +0000552 } else {
553 debug("%s: unsupported dr_mode %d\n", __func__, dr_mode);
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900554 return -ENODEV;
Jonas Karlman6913c302023-07-30 22:59:56 +0000555 }
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900556
557 ret = device_bind_driver_to_node(parent, driver, name,
558 node, &dev);
559 if (ret) {
560 debug("%s: not able to bind usb device mode\n",
561 __func__);
562 return ret;
563 }
564
565 return 0;
566}
567
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900568int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200569{
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900570 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(parent);
Kever Yangac28e592020-03-04 08:59:50 +0800571 ofnode node;
Michal Simek49d67452018-05-18 13:15:06 +0200572 int ret;
Angus Ainsliec08db052022-02-02 15:08:54 -0800573
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900574 if (ops && ops->glue_get_ctrl_dev) {
575 ret = ops->glue_get_ctrl_dev(parent, &node);
576 if (ret)
577 return ret;
578
579 return dwc3_glue_bind_common(parent, node);
580 }
Michal Simek49d67452018-05-18 13:15:06 +0200581
Simon Glassf10643c2020-12-19 10:40:14 -0700582 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900583 ret = dwc3_glue_bind_common(parent, node);
584 if (ret == -ENXIO)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100585 continue;
Kunihiko Hayashif7b7c722023-02-20 14:50:26 +0900586 if (ret)
Michal Simek49d67452018-05-18 13:15:06 +0200587 return ret;
Michal Simek49d67452018-05-18 13:15:06 +0200588 }
589
590 return 0;
591}
592
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100593static int dwc3_glue_reset_init(struct udevice *dev,
594 struct dwc3_glue_data *glue)
595{
596 int ret;
597
598 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530599 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100600 return 0;
601 else if (ret)
602 return ret;
603
Caleb Connolly896e5282023-09-12 22:00:12 +0100604 if (device_is_compatible(dev, "qcom,dwc3")) {
605 reset_assert_bulk(&glue->resets);
606 udelay(500);
607 }
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100608 ret = reset_deassert_bulk(&glue->resets);
609 if (ret) {
610 reset_release_bulk(&glue->resets);
611 return ret;
612 }
613
614 return 0;
615}
616
617static int dwc3_glue_clk_init(struct udevice *dev,
618 struct dwc3_glue_data *glue)
619{
620 int ret;
621
622 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530623 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100624 return 0;
625 if (ret)
626 return ret;
627
628#if CONFIG_IS_ENABLED(CLK)
629 ret = clk_enable_bulk(&glue->clks);
630 if (ret) {
631 clk_release_bulk(&glue->clks);
632 return ret;
633 }
634#endif
635
636 return 0;
637}
638
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900639int dwc3_glue_probe(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100640{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100641 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700642 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100643 struct udevice *child = NULL;
644 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100645 int ret;
Michal Simek142d50f2022-03-09 10:05:45 +0100646 struct phy phy;
647
648 ret = generic_phy_get_by_name(dev, "usb3-phy", &phy);
649 if (!ret) {
650 ret = generic_phy_init(&phy);
651 if (ret)
652 return ret;
Jan Kiszka868d58f2022-04-25 13:26:45 +0200653 } else if (ret != -ENOENT && ret != -ENODATA) {
Michal Simek142d50f2022-03-09 10:05:45 +0100654 debug("could not get phy (err %d)\n", ret);
655 return ret;
656 }
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100657
Kunihiko Hayashi211a0662023-02-20 14:50:29 +0900658 glue->regs = dev_read_addr_size_index(dev, 0, &glue->size);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100659
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100660 ret = dwc3_glue_clk_init(dev, glue);
661 if (ret)
662 return ret;
663
664 ret = dwc3_glue_reset_init(dev, glue);
665 if (ret)
666 return ret;
667
Jonas Karlman5ccfdd82023-08-31 22:16:36 +0000668 if (generic_phy_valid(&phy)) {
Michal Simek142d50f2022-03-09 10:05:45 +0100669 ret = generic_phy_power_on(&phy);
670 if (ret)
671 return ret;
672 }
673
Jonas Karlman4412a2b2023-07-30 22:59:55 +0000674 device_find_first_child(dev, &child);
675 if (!child)
676 return 0;
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100677
Kunihiko Hayashi7c71c682023-02-20 14:50:27 +0900678 if (glue->clks.count == 0) {
679 ret = dwc3_glue_clk_init(child, glue);
680 if (ret)
681 return ret;
682 }
683
Frank Wang5d422ab2020-05-26 11:34:31 +0800684 if (glue->resets.count == 0) {
685 ret = dwc3_glue_reset_init(child, glue);
686 if (ret)
687 return ret;
688 }
689
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100690 while (child) {
691 enum usb_dr_mode dr_mode;
692
Simon Glassf10643c2020-12-19 10:40:14 -0700693 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100694 device_find_next_child(&child);
Marek Vasutf1ef9552022-04-13 00:42:55 +0200695 if (ops && ops->glue_configure)
696 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100697 index++;
698 }
699
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100700 return 0;
701}
702
Kunihiko Hayashief2313b2023-02-20 14:50:28 +0900703int dwc3_glue_remove(struct udevice *dev)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100704{
Simon Glassc69cda22020-12-03 16:55:20 -0700705 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100706
707 reset_release_bulk(&glue->resets);
708
709 clk_release_bulk(&glue->clks);
710
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200711 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100712}
713
714static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200715 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu648856a2020-05-12 08:36:01 +0200716 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100717 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100718 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100719 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendracab4e272019-12-09 10:37:29 +0530720 { .compatible = "ti,am654-dwc3" },
Jagan Teki185571b2023-06-06 22:39:14 +0530721 { .compatible = "rockchip,rk3328-dwc3", .data = (ulong)&rk_ops },
Frank Wang5d422ab2020-05-26 11:34:31 +0800722 { .compatible = "rockchip,rk3399-dwc3" },
Jonas Karlmancaaeac82023-07-30 22:59:57 +0000723 { .compatible = "rockchip,rk3568-dwc3", .data = (ulong)&rk_ops },
Caleb Connolly896e5282023-09-12 22:00:12 +0100724 { .compatible = "qcom,dwc3", .data = (ulong)&qcom_ops },
Marek Vasutd0f7a052022-04-13 00:42:56 +0200725 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainsliec08db052022-02-02 15:08:54 -0800726 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko23cdbba2020-12-03 19:45:01 +0200727 { .compatible = "intel,tangier-dwc3" },
Michal Simek49d67452018-05-18 13:15:06 +0200728 { }
729};
730
731U_BOOT_DRIVER(dwc3_generic_wrapper) = {
732 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblot3b838292019-07-05 09:33:58 +0200733 .id = UCLASS_NOP,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100734 .of_match = dwc3_glue_ids,
735 .bind = dwc3_glue_bind,
736 .probe = dwc3_glue_probe,
737 .remove = dwc3_glue_remove,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700738 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100739
Michal Simek49d67452018-05-18 13:15:06 +0200740};