blob: eabd53a36dc9e85bf1c2319b60e9ab2daa8011da [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>
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +010012#include <asm-generic/io.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>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020017#include <linux/usb/ch9.h>
18#include <linux/usb/gadget.h>
19#include <malloc.h>
20#include <usb.h>
21#include "core.h"
22#include "gadget.h"
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010023#include <reset.h>
24#include <clk.h>
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020025#include <usb/xhci.h>
Michal Simek9d8cbbf2018-05-18 13:15:06 +020026
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020027struct dwc3_generic_plat {
28 fdt_addr_t base;
29 u32 maximum_speed;
30 enum usb_dr_mode dr_mode;
31};
32
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020033struct dwc3_generic_priv {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020034 void *base;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010035 struct dwc3 dwc3;
developerf8bced12020-05-02 11:35:13 +020036 struct phy_bulk phys;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010037};
38
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +020039struct dwc3_generic_host_priv {
40 struct xhci_ctrl xhci_ctrl;
41 struct dwc3_generic_priv gen_priv;
42};
43
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020044static int dwc3_generic_probe(struct udevice *dev,
45 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020046{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010047 int rc;
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020048 struct dwc3_generic_plat *plat = dev_get_platdata(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010049 struct dwc3 *dwc3 = &priv->dwc3;
50
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020051 dwc3->dev = dev;
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020052 dwc3->maximum_speed = plat->maximum_speed;
53 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020054#if CONFIG_IS_ENABLED(OF_CONTROL)
55 dwc3_of_parse(dwc3);
56#endif
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020057
developerf8bced12020-05-02 11:35:13 +020058 rc = dwc3_setup_phy(dev, &priv->phys);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010059 if (rc)
60 return rc;
61
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020062 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
63 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +020064
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010065
66 rc = dwc3_init(dwc3);
67 if (rc) {
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020068 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010069 return rc;
70 }
Michal Simek9d8cbbf2018-05-18 13:15:06 +020071
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010072 return 0;
Michal Simek9d8cbbf2018-05-18 13:15:06 +020073}
74
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020075static int dwc3_generic_remove(struct udevice *dev,
76 struct dwc3_generic_priv *priv)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020077{
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010078 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek9d8cbbf2018-05-18 13:15:06 +020079
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010080 dwc3_remove(dwc3);
developerf8bced12020-05-02 11:35:13 +020081 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +010082 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek9d8cbbf2018-05-18 13:15:06 +020083
84 return 0;
85}
86
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +020087static int dwc3_generic_ofdata_to_platdata(struct udevice *dev)
Michal Simek9d8cbbf2018-05-18 13:15:06 +020088{
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020089 struct dwc3_generic_plat *plat = dev_get_platdata(dev);
Kever Yang1b807052020-03-04 08:59:50 +080090 ofnode node = dev->node;
Michal Simek9d8cbbf2018-05-18 13:15:06 +020091
Kever Yang1b807052020-03-04 08:59:50 +080092 plat->base = dev_read_addr(dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +020093
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +020094 plat->maximum_speed = usb_get_maximum_speed(node);
95 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot547df0d2019-09-11 11:33:51 +020096 pr_info("No USB maximum speed specified. Using super speed\n");
97 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek9d8cbbf2018-05-18 13:15:06 +020098 }
99
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200100 plat->dr_mode = usb_get_dr_mode(node);
101 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200102 pr_err("Invalid usb mode setup\n");
103 return -ENODEV;
104 }
105
106 return 0;
107}
108
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200109#if CONFIG_IS_ENABLED(DM_USB_GADGET)
110int dm_usb_gadget_handle_interrupts(struct udevice *dev)
111{
112 struct dwc3_generic_priv *priv = dev_get_priv(dev);
113 struct dwc3 *dwc3 = &priv->dwc3;
114
115 dwc3_gadget_uboot_handle_interrupt(dwc3);
116
117 return 0;
118}
119
120static int dwc3_generic_peripheral_probe(struct udevice *dev)
121{
122 struct dwc3_generic_priv *priv = dev_get_priv(dev);
123
124 return dwc3_generic_probe(dev, priv);
125}
126
127static int dwc3_generic_peripheral_remove(struct udevice *dev)
128{
129 struct dwc3_generic_priv *priv = dev_get_priv(dev);
130
131 return dwc3_generic_remove(dev, priv);
132}
133
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200134U_BOOT_DRIVER(dwc3_generic_peripheral) = {
135 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100136 .id = UCLASS_USB_GADGET_GENERIC,
Jean-Jacques Hiblot2bf2c352019-09-11 11:33:49 +0200137 .ofdata_to_platdata = dwc3_generic_ofdata_to_platdata,
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200138 .probe = dwc3_generic_peripheral_probe,
139 .remove = dwc3_generic_peripheral_remove,
Jean-Jacques Hiblota33aa762019-09-11 11:33:48 +0200140 .priv_auto_alloc_size = sizeof(struct dwc3_generic_priv),
141 .platdata_auto_alloc_size = sizeof(struct dwc3_generic_plat),
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200142};
Jean-Jacques Hiblot44aaec72018-11-29 10:52:42 +0100143#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200144
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200145#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
146static int dwc3_generic_host_probe(struct udevice *dev)
147{
148 struct xhci_hcor *hcor;
149 struct xhci_hccr *hccr;
150 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
151 int rc;
152
153 rc = dwc3_generic_probe(dev, &priv->gen_priv);
154 if (rc)
155 return rc;
156
157 hccr = (struct xhci_hccr *)priv->gen_priv.base;
158 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
159 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
160
161 return xhci_register(dev, hccr, hcor);
162}
163
164static int dwc3_generic_host_remove(struct udevice *dev)
165{
166 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
167 int rc;
168
169 rc = xhci_deregister(dev);
170 if (rc)
171 return rc;
172
173 return dwc3_generic_remove(dev, &priv->gen_priv);
174}
175
176U_BOOT_DRIVER(dwc3_generic_host) = {
177 .name = "dwc3-generic-host",
178 .id = UCLASS_USB,
179 .ofdata_to_platdata = dwc3_generic_ofdata_to_platdata,
180 .probe = dwc3_generic_host_probe,
181 .remove = dwc3_generic_host_remove,
182 .priv_auto_alloc_size = sizeof(struct dwc3_generic_host_priv),
183 .platdata_auto_alloc_size = sizeof(struct dwc3_generic_plat),
184 .ops = &xhci_usb_ops,
185 .flags = DM_FLAG_ALLOC_PRIV_DMA,
186};
187#endif
188
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100189struct dwc3_glue_data {
190 struct clk_bulk clks;
191 struct reset_ctl_bulk resets;
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100192 fdt_addr_t regs;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100193};
194
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100195struct dwc3_glue_ops {
196 void (*select_dr_mode)(struct udevice *dev, int index,
197 enum usb_dr_mode mode);
198};
199
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100200void dwc3_ti_select_dr_mode(struct udevice *dev, int index,
201 enum usb_dr_mode mode)
202{
203#define USBOTGSS_UTMI_OTG_STATUS 0x0084
204#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
205
206/* UTMI_OTG_STATUS REGISTER */
207#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
208#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
209#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
210#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
211#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
212#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
213#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
214enum dwc3_omap_utmi_mode {
215 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
216 DWC3_OMAP_UTMI_MODE_HW,
217 DWC3_OMAP_UTMI_MODE_SW,
218};
219
220 u32 use_id_pin;
221 u32 host_mode;
222 u32 reg;
223 u32 utmi_mode;
224 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
225
226 struct dwc3_glue_data *glue = dev_get_platdata(dev);
227 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
228
229 if (device_is_compatible(dev, "ti,am437x-dwc3"))
230 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
231
232 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
233 DWC3_OMAP_UTMI_MODE_UNKNOWN);
234 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
235 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
236 dev->name);
237 mode = USB_DR_MODE_PERIPHERAL;
238 }
239
240 switch (mode) {
241 case USB_DR_MODE_PERIPHERAL:
242 use_id_pin = 0;
243 host_mode = 0;
244 break;
245 case USB_DR_MODE_HOST:
246 use_id_pin = 0;
247 host_mode = 1;
248 break;
249 case USB_DR_MODE_OTG:
250 default:
251 use_id_pin = 1;
252 host_mode = 0;
253 break;
254 }
255
256 reg = readl(base + utmi_status_offset);
257
258 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
259 if (!use_id_pin)
260 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
261
262 writel(reg, base + utmi_status_offset);
263
264 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
265 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
266 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
267
268 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
269 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
270
271 if (!host_mode)
272 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
273 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
274
275 writel(reg, base + utmi_status_offset);
276
277 unmap_physmem(base, MAP_NOCACHE);
278}
279
280struct dwc3_glue_ops ti_ops = {
281 .select_dr_mode = dwc3_ti_select_dr_mode,
282};
283
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100284static int dwc3_glue_bind(struct udevice *parent)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200285{
Kever Yang1b807052020-03-04 08:59:50 +0800286 ofnode node;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200287 int ret;
288
Kever Yang1b807052020-03-04 08:59:50 +0800289 ofnode_for_each_subnode(node, parent->node) {
290 const char *name = ofnode_get_name(node);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200291 enum usb_dr_mode dr_mode;
292 struct udevice *dev;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100293 const char *driver = NULL;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200294
295 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200296
297 dr_mode = usb_get_dr_mode(node);
298
299 switch (dr_mode) {
300 case USB_DR_MODE_PERIPHERAL:
301 case USB_DR_MODE_OTG:
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100302#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200303 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
304 driver = "dwc3-generic-peripheral";
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100305#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200306 break;
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200307#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200308 case USB_DR_MODE_HOST:
309 debug("%s: dr_mode: HOST\n", __func__);
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200310 driver = "dwc3-generic-host";
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200311 break;
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200312#endif
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200313 default:
314 debug("%s: unsupported dr_mode\n", __func__);
315 return -ENODEV;
316 };
317
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100318 if (!driver)
319 continue;
320
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200321 ret = device_bind_driver_to_node(parent, driver, name,
Kever Yang1b807052020-03-04 08:59:50 +0800322 node, &dev);
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200323 if (ret) {
324 debug("%s: not able to bind usb device mode\n",
325 __func__);
326 return ret;
327 }
328 }
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100329
330 return 0;
331}
332
333static int dwc3_glue_reset_init(struct udevice *dev,
334 struct dwc3_glue_data *glue)
335{
336 int ret;
337
338 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530339 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100340 return 0;
341 else if (ret)
342 return ret;
343
344 ret = reset_deassert_bulk(&glue->resets);
345 if (ret) {
346 reset_release_bulk(&glue->resets);
347 return ret;
348 }
349
350 return 0;
351}
352
353static int dwc3_glue_clk_init(struct udevice *dev,
354 struct dwc3_glue_data *glue)
355{
356 int ret;
357
358 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrae9310fc2019-10-25 13:48:05 +0530359 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100360 return 0;
361 if (ret)
362 return ret;
363
364#if CONFIG_IS_ENABLED(CLK)
365 ret = clk_enable_bulk(&glue->clks);
366 if (ret) {
367 clk_release_bulk(&glue->clks);
368 return ret;
369 }
370#endif
371
372 return 0;
373}
374
375static int dwc3_glue_probe(struct udevice *dev)
376{
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100377 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100378 struct dwc3_glue_data *glue = dev_get_platdata(dev);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100379 struct udevice *child = NULL;
380 int index = 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100381 int ret;
382
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100383 glue->regs = dev_read_addr(dev);
384
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100385 ret = dwc3_glue_clk_init(dev, glue);
386 if (ret)
387 return ret;
388
389 ret = dwc3_glue_reset_init(dev, glue);
390 if (ret)
391 return ret;
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200392
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100393 ret = device_find_first_child(dev, &child);
394 if (ret)
395 return ret;
396
397 while (child) {
398 enum usb_dr_mode dr_mode;
399
Kever Yang1b807052020-03-04 08:59:50 +0800400 dr_mode = usb_get_dr_mode(child->node);
Jean-Jacques Hiblotae004d32018-11-29 10:52:49 +0100401 device_find_next_child(&child);
402 if (ops && ops->select_dr_mode)
403 ops->select_dr_mode(dev, index, dr_mode);
404 index++;
405 }
406
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200407 return 0;
408}
409
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100410static int dwc3_glue_remove(struct udevice *dev)
411{
412 struct dwc3_glue_data *glue = dev_get_platdata(dev);
413
414 reset_release_bulk(&glue->resets);
415
416 clk_release_bulk(&glue->clks);
417
Jean-Jacques Hiblot5a945572019-07-05 09:33:56 +0200418 return 0;
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100419}
420
421static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200422 { .compatible = "xlnx,zynqmp-dwc3" },
Jean-Jacques Hiblot3e0684b2018-12-04 11:12:56 +0100423 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblot65596f12018-11-29 10:57:40 +0100424 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblotca848df2018-12-04 11:30:50 +0100425 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendrac6282952019-12-09 10:37:29 +0530426 { .compatible = "ti,am654-dwc3" },
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200427 { }
428};
429
430U_BOOT_DRIVER(dwc3_generic_wrapper) = {
431 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblotb49b5c22019-07-05 09:33:58 +0200432 .id = UCLASS_NOP,
Jean-Jacques Hiblotaa866a02018-11-29 10:52:48 +0100433 .of_match = dwc3_glue_ids,
434 .bind = dwc3_glue_bind,
435 .probe = dwc3_glue_probe,
436 .remove = dwc3_glue_remove,
437 .platdata_auto_alloc_size = sizeof(struct dwc3_glue_data),
438
Michal Simek9d8cbbf2018-05-18 13:15:06 +0200439};