blob: 6cf844cb483b15b8c883ca20e7e6522b4a823dc8 [file] [log] [blame]
Patrick Delaunayd6e53c72018-10-26 09:02:52 +02001// SPDX-License-Identifier: GPL-2.0
Michal Simek9d8cbbf2018-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 Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020013#include <dm.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010016#include <dwc3-uboot.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Frank Wangf5a6c5b2020-05-26 11:34:31 +080018#include <linux/delay.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020019#include <linux/usb/ch9.h>
20#include <linux/usb/gadget.h>
21#include <malloc.h>
22#include <usb.h>
23#include "core.h"
24#include "gadget.h"
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010025#include <reset.h>
26#include <clk.h>
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020027#include <usb/xhci.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020028
Frank Wangf5a6c5b2020-05-26 11:34:31 +080029struct dwc3_glue_data {
30 struct clk_bulk clks;
31 struct reset_ctl_bulk resets;
32 fdt_addr_t regs;
33};
34
Jean-Jacques Hiblota33aa762019-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 Hiblota33aa762019-09-11 11:33:48 +020041struct dwc3_generic_priv {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020042 void *base;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010043 struct dwc3 dwc3;
developerf8bced12020-05-02 11:35:13 +020044 struct phy_bulk phys;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010045};
46
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020047struct dwc3_generic_host_priv {
48 struct xhci_ctrl xhci_ctrl;
49 struct dwc3_generic_priv gen_priv;
50};
51
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020052static int dwc3_generic_probe(struct udevice *dev,
53 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020054{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010055 int rc;
Simon Glassfa20e932020-12-03 16:55:20 -070056 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010057 struct dwc3 *dwc3 = &priv->dwc3;
Simon Glassfa20e932020-12-03 16:55:20 -070058 struct dwc3_glue_data *glue = dev_get_plat(dev->parent);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010059
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020060 dwc3->dev = dev;
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020061 dwc3->maximum_speed = plat->maximum_speed;
62 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020063#if CONFIG_IS_ENABLED(OF_CONTROL)
64 dwc3_of_parse(dwc3);
65#endif
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020066
Frank Wangf5a6c5b2020-05-26 11:34:31 +080067 /*
68 * It must hold whole USB3.0 OTG controller in resetting to hold pipe
69 * power state in P2 before initializing TypeC PHY on RK3399 platform.
70 */
71 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3")) {
72 reset_assert_bulk(&glue->resets);
73 udelay(1);
74 }
75
developerf8bced12020-05-02 11:35:13 +020076 rc = dwc3_setup_phy(dev, &priv->phys);
Siva Durga Prasad Paladuguc37f8f32020-10-21 14:17:31 +020077 if (rc && rc != -ENOTSUPP)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010078 return rc;
79
Frank Wangf5a6c5b2020-05-26 11:34:31 +080080 if (device_is_compatible(dev->parent, "rockchip,rk3399-dwc3"))
81 reset_deassert_bulk(&glue->resets);
82
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020083 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
84 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020085
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010086
87 rc = dwc3_init(dwc3);
88 if (rc) {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020089 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010090 return rc;
91 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +020092
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010093 return 0;
Michal Simek9d8cbbf2018-05-18 13:15:06 +020094}
95
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020096static int dwc3_generic_remove(struct udevice *dev,
97 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020098{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010099 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200100
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100101 dwc3_remove(dwc3);
developerf8bced12020-05-02 11:35:13 +0200102 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100103 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200104
105 return 0;
106}
107
Simon Glassaad29ae2020-12-03 16:55:21 -0700108static int dwc3_generic_of_to_plat(struct udevice *dev)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200109{
Simon Glassfa20e932020-12-03 16:55:20 -0700110 struct dwc3_generic_plat *plat = dev_get_plat(dev);
Simon Glassa7ece582020-12-19 10:40:14 -0700111 ofnode node = dev_ofnode(dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200112
Angus Ainslie6e382a82022-02-02 15:08:54 -0800113 if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) {
114 /* This is a leaf so check the parent */
115 plat->base = dev_read_addr(dev->parent);
116 } else {
117 plat->base = dev_read_addr(dev);
118 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200119
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200120 plat->maximum_speed = usb_get_maximum_speed(node);
121 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot547df0d2019-09-11 11:33:51 +0200122 pr_info("No USB maximum speed specified. Using super speed\n");
123 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200124 }
125
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200126 plat->dr_mode = usb_get_dr_mode(node);
127 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Angus Ainslie6e382a82022-02-02 15:08:54 -0800128 /* might be a leaf so check the parent for mode */
129 node = dev_ofnode(dev->parent);
130 plat->dr_mode = usb_get_dr_mode(node);
131 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
132 pr_err("Invalid usb mode setup\n");
133 return -ENODEV;
134 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200135 }
136
137 return 0;
138}
139
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200140#if CONFIG_IS_ENABLED(DM_USB_GADGET)
141int dm_usb_gadget_handle_interrupts(struct udevice *dev)
142{
143 struct dwc3_generic_priv *priv = dev_get_priv(dev);
144 struct dwc3 *dwc3 = &priv->dwc3;
145
146 dwc3_gadget_uboot_handle_interrupt(dwc3);
147
148 return 0;
149}
150
151static int dwc3_generic_peripheral_probe(struct udevice *dev)
152{
153 struct dwc3_generic_priv *priv = dev_get_priv(dev);
154
155 return dwc3_generic_probe(dev, priv);
156}
157
158static int dwc3_generic_peripheral_remove(struct udevice *dev)
159{
160 struct dwc3_generic_priv *priv = dev_get_priv(dev);
161
162 return dwc3_generic_remove(dev, priv);
163}
164
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200165U_BOOT_DRIVER(dwc3_generic_peripheral) = {
166 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100167 .id = UCLASS_USB_GADGET_GENERIC,
Simon Glassaad29ae2020-12-03 16:55:21 -0700168 .of_to_plat = dwc3_generic_of_to_plat,
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200169 .probe = dwc3_generic_peripheral_probe,
170 .remove = dwc3_generic_peripheral_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700171 .priv_auto = sizeof(struct dwc3_generic_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700172 .plat_auto = sizeof(struct dwc3_generic_plat),
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200173};
Jean-Jacques Hiblot44aaec72018-11-29 10:52:42 +0100174#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200175
Simon Glass1f2440c2021-07-10 21:14:29 -0600176#if defined(CONFIG_SPL_USB_HOST) || \
Kunihiko Hayashib032aa62021-05-12 23:11:14 +0900177 !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST)
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200178static int dwc3_generic_host_probe(struct udevice *dev)
179{
180 struct xhci_hcor *hcor;
181 struct xhci_hccr *hccr;
182 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
183 int rc;
184
185 rc = dwc3_generic_probe(dev, &priv->gen_priv);
186 if (rc)
187 return rc;
188
189 hccr = (struct xhci_hccr *)priv->gen_priv.base;
190 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
191 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
192
193 return xhci_register(dev, hccr, hcor);
194}
195
196static int dwc3_generic_host_remove(struct udevice *dev)
197{
198 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
199 int rc;
200
201 rc = xhci_deregister(dev);
202 if (rc)
203 return rc;
204
205 return dwc3_generic_remove(dev, &priv->gen_priv);
206}
207
208U_BOOT_DRIVER(dwc3_generic_host) = {
209 .name = "dwc3-generic-host",
210 .id = UCLASS_USB,
Simon Glassaad29ae2020-12-03 16:55:21 -0700211 .of_to_plat = dwc3_generic_of_to_plat,
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200212 .probe = dwc3_generic_host_probe,
213 .remove = dwc3_generic_host_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700214 .priv_auto = sizeof(struct dwc3_generic_host_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700215 .plat_auto = sizeof(struct dwc3_generic_plat),
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200216 .ops = &xhci_usb_ops,
217 .flags = DM_FLAG_ALLOC_PRIV_DMA,
218};
219#endif
220
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100221struct dwc3_glue_ops {
Marek Vasut68c86562022-04-13 00:42:55 +0200222 void (*glue_configure)(struct udevice *dev, int index,
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100223 enum usb_dr_mode mode);
224};
225
Marek Vasutae219342022-04-13 00:42:56 +0200226void dwc3_imx8mp_glue_configure(struct udevice *dev, int index,
227 enum usb_dr_mode mode)
228{
229/* USB glue registers */
230#define USB_CTRL0 0x00
231#define USB_CTRL1 0x04
232
233#define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
234#define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
235#define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
236
237#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
238#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
239 fdt_addr_t regs = dev_read_addr_index(dev, 1);
240 void *base = map_physmem(regs, 0x8, MAP_NOCACHE);
241 u32 value;
242
243 value = readl(base + USB_CTRL0);
244
245 if (dev_read_bool(dev, "fsl,permanently-attached"))
246 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
247 else
248 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
249
250 if (dev_read_bool(dev, "fsl,disable-port-power-control"))
251 value &= ~(USB_CTRL0_PORTPWR_EN);
252 else
253 value |= USB_CTRL0_PORTPWR_EN;
254
255 writel(value, base + USB_CTRL0);
256
257 value = readl(base + USB_CTRL1);
258 if (dev_read_bool(dev, "fsl,over-current-active-low"))
259 value |= USB_CTRL1_OC_POLARITY;
260 else
261 value &= ~USB_CTRL1_OC_POLARITY;
262
263 if (dev_read_bool(dev, "fsl,power-active-low"))
264 value |= USB_CTRL1_PWR_POLARITY;
265 else
266 value &= ~USB_CTRL1_PWR_POLARITY;
267
268 writel(value, base + USB_CTRL1);
269
270 unmap_physmem(base, MAP_NOCACHE);
271}
272
273struct dwc3_glue_ops imx8mp_ops = {
274 .glue_configure = dwc3_imx8mp_glue_configure,
275};
276
Marek Vasut68c86562022-04-13 00:42:55 +0200277void dwc3_ti_glue_configure(struct udevice *dev, int index,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100278 enum usb_dr_mode mode)
279{
280#define USBOTGSS_UTMI_OTG_STATUS 0x0084
281#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
282
283/* UTMI_OTG_STATUS REGISTER */
284#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
285#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
286#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
287#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
288#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
289#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
290#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
291enum dwc3_omap_utmi_mode {
292 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
293 DWC3_OMAP_UTMI_MODE_HW,
294 DWC3_OMAP_UTMI_MODE_SW,
295};
296
297 u32 use_id_pin;
298 u32 host_mode;
299 u32 reg;
300 u32 utmi_mode;
301 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
302
Simon Glassfa20e932020-12-03 16:55:20 -0700303 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100304 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
305
306 if (device_is_compatible(dev, "ti,am437x-dwc3"))
307 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
308
309 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
310 DWC3_OMAP_UTMI_MODE_UNKNOWN);
311 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
312 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
313 dev->name);
314 mode = USB_DR_MODE_PERIPHERAL;
315 }
316
317 switch (mode) {
318 case USB_DR_MODE_PERIPHERAL:
319 use_id_pin = 0;
320 host_mode = 0;
321 break;
322 case USB_DR_MODE_HOST:
323 use_id_pin = 0;
324 host_mode = 1;
325 break;
326 case USB_DR_MODE_OTG:
327 default:
328 use_id_pin = 1;
329 host_mode = 0;
330 break;
331 }
332
333 reg = readl(base + utmi_status_offset);
334
335 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
336 if (!use_id_pin)
337 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
338
339 writel(reg, base + utmi_status_offset);
340
341 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
342 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
343 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
344
345 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
346 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
347
348 if (!host_mode)
349 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
350 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
351
352 writel(reg, base + utmi_status_offset);
353
354 unmap_physmem(base, MAP_NOCACHE);
355}
356
357struct dwc3_glue_ops ti_ops = {
Marek Vasut68c86562022-04-13 00:42:55 +0200358 .glue_configure = dwc3_ti_glue_configure,
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100359};
360
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100361static int dwc3_glue_bind(struct udevice *parent)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200362{
Kever Yang1b807052020-03-04 08:59:50 +0800363 ofnode node;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200364 int ret;
Angus Ainslie6e382a82022-02-02 15:08:54 -0800365 enum usb_dr_mode dr_mode;
366
367 dr_mode = usb_get_dr_mode(dev_ofnode(parent));
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200368
Simon Glassa7ece582020-12-19 10:40:14 -0700369 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
Kever Yang1b807052020-03-04 08:59:50 +0800370 const char *name = ofnode_get_name(node);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200371 struct udevice *dev;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100372 const char *driver = NULL;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200373
374 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200375
Angus Ainslie6e382a82022-02-02 15:08:54 -0800376 /* if the parent node doesn't have a mode check the leaf */
377 if (!dr_mode)
378 dr_mode = usb_get_dr_mode(node);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200379
380 switch (dr_mode) {
381 case USB_DR_MODE_PERIPHERAL:
382 case USB_DR_MODE_OTG:
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100383#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200384 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
385 driver = "dwc3-generic-peripheral";
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100386#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200387 break;
Simon Glass1f2440c2021-07-10 21:14:29 -0600388#if defined(CONFIG_SPL_USB_HOST) || !defined(CONFIG_SPL_BUILD)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200389 case USB_DR_MODE_HOST:
390 debug("%s: dr_mode: HOST\n", __func__);
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200391 driver = "dwc3-generic-host";
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200392 break;
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200393#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200394 default:
395 debug("%s: unsupported dr_mode\n", __func__);
396 return -ENODEV;
397 };
398
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100399 if (!driver)
400 continue;
401
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200402 ret = device_bind_driver_to_node(parent, driver, name,
Kever Yang1b807052020-03-04 08:59:50 +0800403 node, &dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200404 if (ret) {
405 debug("%s: not able to bind usb device mode\n",
406 __func__);
407 return ret;
408 }
409 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100410
411 return 0;
412}
413
414static int dwc3_glue_reset_init(struct udevice *dev,
415 struct dwc3_glue_data *glue)
416{
417 int ret;
418
419 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530420 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100421 return 0;
422 else if (ret)
423 return ret;
424
425 ret = reset_deassert_bulk(&glue->resets);
426 if (ret) {
427 reset_release_bulk(&glue->resets);
428 return ret;
429 }
430
431 return 0;
432}
433
434static int dwc3_glue_clk_init(struct udevice *dev,
435 struct dwc3_glue_data *glue)
436{
437 int ret;
438
439 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530440 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100441 return 0;
442 if (ret)
443 return ret;
444
445#if CONFIG_IS_ENABLED(CLK)
446 ret = clk_enable_bulk(&glue->clks);
447 if (ret) {
448 clk_release_bulk(&glue->clks);
449 return ret;
450 }
451#endif
452
453 return 0;
454}
455
456static int dwc3_glue_probe(struct udevice *dev)
457{
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100458 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700459 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100460 struct udevice *child = NULL;
461 int index = 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100462 int ret;
463
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100464 glue->regs = dev_read_addr(dev);
465
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100466 ret = dwc3_glue_clk_init(dev, glue);
467 if (ret)
468 return ret;
469
470 ret = dwc3_glue_reset_init(dev, glue);
471 if (ret)
472 return ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200473
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100474 ret = device_find_first_child(dev, &child);
475 if (ret)
476 return ret;
477
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800478 if (glue->resets.count == 0) {
479 ret = dwc3_glue_reset_init(child, glue);
480 if (ret)
481 return ret;
482 }
483
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100484 while (child) {
485 enum usb_dr_mode dr_mode;
486
Simon Glassa7ece582020-12-19 10:40:14 -0700487 dr_mode = usb_get_dr_mode(dev_ofnode(child));
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100488 device_find_next_child(&child);
Marek Vasut68c86562022-04-13 00:42:55 +0200489 if (ops && ops->glue_configure)
490 ops->glue_configure(dev, index, dr_mode);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100491 index++;
492 }
493
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200494 return 0;
495}
496
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100497static int dwc3_glue_remove(struct udevice *dev)
498{
Simon Glassfa20e932020-12-03 16:55:20 -0700499 struct dwc3_glue_data *glue = dev_get_plat(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100500
501 reset_release_bulk(&glue->resets);
502
503 clk_release_bulk(&glue->clks);
504
Jean-Jacques Hiblot5a945572019-07-05 09:33:56 +0200505 return 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100506}
507
508static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200509 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu1eb3c302020-05-12 08:36:01 +0200510 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot3e0684b2018-12-04 11:12:56 +0100511 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100512 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblotca848df2018-12-04 11:30:50 +0100513 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendrac6282952019-12-09 10:37:29 +0530514 { .compatible = "ti,am654-dwc3" },
Frank Wangf5a6c5b2020-05-26 11:34:31 +0800515 { .compatible = "rockchip,rk3328-dwc3" },
516 { .compatible = "rockchip,rk3399-dwc3" },
Robert Marko746862b2020-09-10 16:00:05 +0200517 { .compatible = "qcom,dwc3" },
Marek Vasutae219342022-04-13 00:42:56 +0200518 { .compatible = "fsl,imx8mp-dwc3", .data = (ulong)&imx8mp_ops },
Angus Ainslie6e382a82022-02-02 15:08:54 -0800519 { .compatible = "fsl,imx8mq-dwc3" },
Andy Shevchenko221d7fa2020-12-03 19:45:01 +0200520 { .compatible = "intel,tangier-dwc3" },
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200521 { }
522};
523
524U_BOOT_DRIVER(dwc3_generic_wrapper) = {
525 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblotb49b5c22019-07-05 09:33:58 +0200526 .id = UCLASS_NOP,
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100527 .of_match = dwc3_glue_ids,
528 .bind = dwc3_glue_bind,
529 .probe = dwc3_glue_probe,
530 .remove = dwc3_glue_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -0700531 .plat_auto = sizeof(struct dwc3_glue_data),
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100532
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200533};