1 // SPDX-License-Identifier: GPL-2.0
3 * Generic DWC3 Glue layer
5 * Copyright (C) 2016 - 2018 Xilinx, Inc.
7 * Based on dwc3-omap.c.
12 #include <asm-generic/io.h>
14 #include <dm/device-internal.h>
16 #include <dwc3-uboot.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
27 struct dwc3_generic_plat {
30 enum usb_dr_mode dr_mode;
33 struct dwc3_generic_priv {
40 struct dwc3_generic_host_priv {
41 struct xhci_ctrl xhci_ctrl;
42 struct dwc3_generic_priv gen_priv;
45 static int dwc3_generic_probe(struct udevice *dev,
46 struct dwc3_generic_priv *priv)
49 struct dwc3_generic_plat *plat = dev_get_platdata(dev);
50 struct dwc3 *dwc3 = &priv->dwc3;
53 dwc3->maximum_speed = plat->maximum_speed;
54 dwc3->dr_mode = plat->dr_mode;
55 #if CONFIG_IS_ENABLED(OF_CONTROL)
59 rc = dwc3_setup_phy(dev, &priv->phys, &priv->num_phys);
63 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
64 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
69 unmap_physmem(priv->base, MAP_NOCACHE);
76 static int dwc3_generic_remove(struct udevice *dev,
77 struct dwc3_generic_priv *priv)
79 struct dwc3 *dwc3 = &priv->dwc3;
82 dwc3_shutdown_phy(dev, priv->phys, priv->num_phys);
83 unmap_physmem(dwc3->regs, MAP_NOCACHE);
88 static int dwc3_generic_ofdata_to_platdata(struct udevice *dev)
90 struct dwc3_generic_plat *plat = dev_get_platdata(dev);
91 ofnode node = dev->node;
93 plat->base = dev_read_addr(dev);
95 plat->maximum_speed = usb_get_maximum_speed(node);
96 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
97 pr_info("No USB maximum speed specified. Using super speed\n");
98 plat->maximum_speed = USB_SPEED_SUPER;
101 plat->dr_mode = usb_get_dr_mode(node);
102 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
103 pr_err("Invalid usb mode setup\n");
110 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
111 int dm_usb_gadget_handle_interrupts(struct udevice *dev)
113 struct dwc3_generic_priv *priv = dev_get_priv(dev);
114 struct dwc3 *dwc3 = &priv->dwc3;
116 dwc3_gadget_uboot_handle_interrupt(dwc3);
121 static int dwc3_generic_peripheral_probe(struct udevice *dev)
123 struct dwc3_generic_priv *priv = dev_get_priv(dev);
125 return dwc3_generic_probe(dev, priv);
128 static int dwc3_generic_peripheral_remove(struct udevice *dev)
130 struct dwc3_generic_priv *priv = dev_get_priv(dev);
132 return dwc3_generic_remove(dev, priv);
135 U_BOOT_DRIVER(dwc3_generic_peripheral) = {
136 .name = "dwc3-generic-peripheral",
137 .id = UCLASS_USB_GADGET_GENERIC,
138 .ofdata_to_platdata = dwc3_generic_ofdata_to_platdata,
139 .probe = dwc3_generic_peripheral_probe,
140 .remove = dwc3_generic_peripheral_remove,
141 .priv_auto_alloc_size = sizeof(struct dwc3_generic_priv),
142 .platdata_auto_alloc_size = sizeof(struct dwc3_generic_plat),
146 #if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
147 static int dwc3_generic_host_probe(struct udevice *dev)
149 struct xhci_hcor *hcor;
150 struct xhci_hccr *hccr;
151 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
154 rc = dwc3_generic_probe(dev, &priv->gen_priv);
158 hccr = (struct xhci_hccr *)priv->gen_priv.base;
159 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
160 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
162 return xhci_register(dev, hccr, hcor);
165 static int dwc3_generic_host_remove(struct udevice *dev)
167 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
170 rc = xhci_deregister(dev);
174 return dwc3_generic_remove(dev, &priv->gen_priv);
177 U_BOOT_DRIVER(dwc3_generic_host) = {
178 .name = "dwc3-generic-host",
180 .ofdata_to_platdata = dwc3_generic_ofdata_to_platdata,
181 .probe = dwc3_generic_host_probe,
182 .remove = dwc3_generic_host_remove,
183 .priv_auto_alloc_size = sizeof(struct dwc3_generic_host_priv),
184 .platdata_auto_alloc_size = sizeof(struct dwc3_generic_plat),
185 .ops = &xhci_usb_ops,
186 .flags = DM_FLAG_ALLOC_PRIV_DMA,
190 struct dwc3_glue_data {
191 struct clk_bulk clks;
192 struct reset_ctl_bulk resets;
196 struct dwc3_glue_ops {
197 void (*select_dr_mode)(struct udevice *dev, int index,
198 enum usb_dr_mode mode);
201 void dwc3_ti_select_dr_mode(struct udevice *dev, int index,
202 enum usb_dr_mode mode)
204 #define USBOTGSS_UTMI_OTG_STATUS 0x0084
205 #define USBOTGSS_UTMI_OTG_OFFSET 0x0480
207 /* UTMI_OTG_STATUS REGISTER */
208 #define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
209 #define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
210 #define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
211 #define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
212 #define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
213 #define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
214 #define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
215 enum dwc3_omap_utmi_mode {
216 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
217 DWC3_OMAP_UTMI_MODE_HW,
218 DWC3_OMAP_UTMI_MODE_SW,
225 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
227 struct dwc3_glue_data *glue = dev_get_platdata(dev);
228 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
230 if (device_is_compatible(dev, "ti,am437x-dwc3"))
231 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
233 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
234 DWC3_OMAP_UTMI_MODE_UNKNOWN);
235 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
236 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
238 mode = USB_DR_MODE_PERIPHERAL;
242 case USB_DR_MODE_PERIPHERAL:
246 case USB_DR_MODE_HOST:
250 case USB_DR_MODE_OTG:
257 reg = readl(base + utmi_status_offset);
259 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
261 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
263 writel(reg, base + utmi_status_offset);
265 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
266 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
267 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
269 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
270 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
273 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
274 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
276 writel(reg, base + utmi_status_offset);
278 unmap_physmem(base, MAP_NOCACHE);
281 struct dwc3_glue_ops ti_ops = {
282 .select_dr_mode = dwc3_ti_select_dr_mode,
285 static int dwc3_glue_bind(struct udevice *parent)
290 ofnode_for_each_subnode(node, parent->node) {
291 const char *name = ofnode_get_name(node);
292 enum usb_dr_mode dr_mode;
294 const char *driver = NULL;
296 debug("%s: subnode name: %s\n", __func__, name);
298 dr_mode = usb_get_dr_mode(node);
301 case USB_DR_MODE_PERIPHERAL:
302 case USB_DR_MODE_OTG:
303 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
304 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
305 driver = "dwc3-generic-peripheral";
308 #if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
309 case USB_DR_MODE_HOST:
310 debug("%s: dr_mode: HOST\n", __func__);
311 driver = "dwc3-generic-host";
315 debug("%s: unsupported dr_mode\n", __func__);
322 ret = device_bind_driver_to_node(parent, driver, name,
325 debug("%s: not able to bind usb device mode\n",
334 static int dwc3_glue_reset_init(struct udevice *dev,
335 struct dwc3_glue_data *glue)
339 ret = reset_get_bulk(dev, &glue->resets);
340 if (ret == -ENOTSUPP || ret == -ENOENT)
345 ret = reset_deassert_bulk(&glue->resets);
347 reset_release_bulk(&glue->resets);
354 static int dwc3_glue_clk_init(struct udevice *dev,
355 struct dwc3_glue_data *glue)
359 ret = clk_get_bulk(dev, &glue->clks);
360 if (ret == -ENOSYS || ret == -ENOENT)
365 #if CONFIG_IS_ENABLED(CLK)
366 ret = clk_enable_bulk(&glue->clks);
368 clk_release_bulk(&glue->clks);
376 static int dwc3_glue_probe(struct udevice *dev)
378 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
379 struct dwc3_glue_data *glue = dev_get_platdata(dev);
380 struct udevice *child = NULL;
384 glue->regs = dev_read_addr(dev);
386 ret = dwc3_glue_clk_init(dev, glue);
390 ret = dwc3_glue_reset_init(dev, glue);
394 ret = device_find_first_child(dev, &child);
399 enum usb_dr_mode dr_mode;
401 dr_mode = usb_get_dr_mode(child->node);
402 device_find_next_child(&child);
403 if (ops && ops->select_dr_mode)
404 ops->select_dr_mode(dev, index, dr_mode);
411 static int dwc3_glue_remove(struct udevice *dev)
413 struct dwc3_glue_data *glue = dev_get_platdata(dev);
415 reset_release_bulk(&glue->resets);
417 clk_release_bulk(&glue->clks);
422 static const struct udevice_id dwc3_glue_ids[] = {
423 { .compatible = "xlnx,zynqmp-dwc3" },
424 { .compatible = "ti,keystone-dwc3"},
425 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
426 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
427 { .compatible = "ti,am654-dwc3" },
431 U_BOOT_DRIVER(dwc3_generic_wrapper) = {
432 .name = "dwc3-generic-wrapper",
434 .of_match = dwc3_glue_ids,
435 .bind = dwc3_glue_bind,
436 .probe = dwc3_glue_probe,
437 .remove = dwc3_glue_remove,
438 .platdata_auto_alloc_size = sizeof(struct dwc3_glue_data),