blob: 484e7a7b8c8bf6be176fd4d4d86923aabee73774 [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>
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +010013#include <asm-generic/io.h>
Michal Simek49d67452018-05-18 13:15:06 +020014#include <dm.h>
15#include <dm/device-internal.h>
16#include <dm/lists.h>
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010017#include <dwc3-uboot.h>
Simon Glasscd93d622020-05-10 11:40:13 -060018#include <linux/bitops.h>
Michal Simek49d67452018-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 Hiblot446e3a22018-11-29 10:52:48 +010025#include <reset.h>
26#include <clk.h>
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +020027#include <usb/xhci.h>
Michal Simek49d67452018-05-18 13:15:06 +020028
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020029struct dwc3_generic_plat {
30 fdt_addr_t base;
31 u32 maximum_speed;
32 enum usb_dr_mode dr_mode;
33};
34
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020035struct dwc3_generic_priv {
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020036 void *base;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010037 struct dwc3 dwc3;
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +020038 struct phy_bulk phys;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010039};
40
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +020041struct dwc3_generic_host_priv {
42 struct xhci_ctrl xhci_ctrl;
43 struct dwc3_generic_priv gen_priv;
44};
45
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020046static int dwc3_generic_probe(struct udevice *dev,
47 struct dwc3_generic_priv *priv)
Michal Simek49d67452018-05-18 13:15:06 +020048{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010049 int rc;
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020050 struct dwc3_generic_plat *plat = dev_get_platdata(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010051 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020052
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020053 dwc3->dev = dev;
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020054 dwc3->maximum_speed = plat->maximum_speed;
55 dwc3->dr_mode = plat->dr_mode;
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020056#if CONFIG_IS_ENABLED(OF_CONTROL)
57 dwc3_of_parse(dwc3);
58#endif
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020059
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +020060 rc = dwc3_setup_phy(dev, &priv->phys);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010061 if (rc)
62 return rc;
63
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020064 priv->base = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
65 dwc3->regs = priv->base + DWC3_GLOBALS_REGS_START;
Jean-Jacques Hiblotba6c5f72019-09-11 11:33:52 +020066
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010067
68 rc = dwc3_init(dwc3);
69 if (rc) {
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020070 unmap_physmem(priv->base, MAP_NOCACHE);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010071 return rc;
72 }
73
74 return 0;
Michal Simek49d67452018-05-18 13:15:06 +020075}
76
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020077static int dwc3_generic_remove(struct udevice *dev,
78 struct dwc3_generic_priv *priv)
Michal Simek49d67452018-05-18 13:15:06 +020079{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010080 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020081
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010082 dwc3_remove(dwc3);
Chunfeng Yun6dfb8a82020-05-02 11:35:13 +020083 dwc3_shutdown_phy(dev, &priv->phys);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010084 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek49d67452018-05-18 13:15:06 +020085
86 return 0;
87}
88
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +020089static int dwc3_generic_ofdata_to_platdata(struct udevice *dev)
Michal Simek49d67452018-05-18 13:15:06 +020090{
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020091 struct dwc3_generic_plat *plat = dev_get_platdata(dev);
Kever Yangac28e592020-03-04 08:59:50 +080092 ofnode node = dev->node;
Michal Simek49d67452018-05-18 13:15:06 +020093
Kever Yangac28e592020-03-04 08:59:50 +080094 plat->base = dev_read_addr(dev);
Michal Simek49d67452018-05-18 13:15:06 +020095
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020096 plat->maximum_speed = usb_get_maximum_speed(node);
97 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Jean-Jacques Hiblot1a63e5e2019-09-11 11:33:51 +020098 pr_info("No USB maximum speed specified. Using super speed\n");
99 plat->maximum_speed = USB_SPEED_SUPER;
Michal Simek49d67452018-05-18 13:15:06 +0200100 }
101
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200102 plat->dr_mode = usb_get_dr_mode(node);
103 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Michal Simek49d67452018-05-18 13:15:06 +0200104 pr_err("Invalid usb mode setup\n");
105 return -ENODEV;
106 }
107
108 return 0;
109}
110
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200111#if CONFIG_IS_ENABLED(DM_USB_GADGET)
112int dm_usb_gadget_handle_interrupts(struct udevice *dev)
113{
114 struct dwc3_generic_priv *priv = dev_get_priv(dev);
115 struct dwc3 *dwc3 = &priv->dwc3;
116
117 dwc3_gadget_uboot_handle_interrupt(dwc3);
118
119 return 0;
120}
121
122static int dwc3_generic_peripheral_probe(struct udevice *dev)
123{
124 struct dwc3_generic_priv *priv = dev_get_priv(dev);
125
126 return dwc3_generic_probe(dev, priv);
127}
128
129static int dwc3_generic_peripheral_remove(struct udevice *dev)
130{
131 struct dwc3_generic_priv *priv = dev_get_priv(dev);
132
133 return dwc3_generic_remove(dev, priv);
134}
135
Michal Simek49d67452018-05-18 13:15:06 +0200136U_BOOT_DRIVER(dwc3_generic_peripheral) = {
137 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100138 .id = UCLASS_USB_GADGET_GENERIC,
Jean-Jacques Hiblot1af590d2019-09-11 11:33:49 +0200139 .ofdata_to_platdata = dwc3_generic_ofdata_to_platdata,
Michal Simek49d67452018-05-18 13:15:06 +0200140 .probe = dwc3_generic_peripheral_probe,
141 .remove = dwc3_generic_peripheral_remove,
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200142 .priv_auto_alloc_size = sizeof(struct dwc3_generic_priv),
143 .platdata_auto_alloc_size = sizeof(struct dwc3_generic_plat),
Michal Simek49d67452018-05-18 13:15:06 +0200144};
Jean-Jacques Hiblot687ab542018-11-29 10:52:42 +0100145#endif
Michal Simek49d67452018-05-18 13:15:06 +0200146
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200147#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
148static int dwc3_generic_host_probe(struct udevice *dev)
149{
150 struct xhci_hcor *hcor;
151 struct xhci_hccr *hccr;
152 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
153 int rc;
154
155 rc = dwc3_generic_probe(dev, &priv->gen_priv);
156 if (rc)
157 return rc;
158
159 hccr = (struct xhci_hccr *)priv->gen_priv.base;
160 hcor = (struct xhci_hcor *)(priv->gen_priv.base +
161 HC_LENGTH(xhci_readl(&(hccr)->cr_capbase)));
162
163 return xhci_register(dev, hccr, hcor);
164}
165
166static int dwc3_generic_host_remove(struct udevice *dev)
167{
168 struct dwc3_generic_host_priv *priv = dev_get_priv(dev);
169 int rc;
170
171 rc = xhci_deregister(dev);
172 if (rc)
173 return rc;
174
175 return dwc3_generic_remove(dev, &priv->gen_priv);
176}
177
178U_BOOT_DRIVER(dwc3_generic_host) = {
179 .name = "dwc3-generic-host",
180 .id = UCLASS_USB,
181 .ofdata_to_platdata = dwc3_generic_ofdata_to_platdata,
182 .probe = dwc3_generic_host_probe,
183 .remove = dwc3_generic_host_remove,
184 .priv_auto_alloc_size = sizeof(struct dwc3_generic_host_priv),
185 .platdata_auto_alloc_size = sizeof(struct dwc3_generic_plat),
186 .ops = &xhci_usb_ops,
187 .flags = DM_FLAG_ALLOC_PRIV_DMA,
188};
189#endif
190
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100191struct dwc3_glue_data {
192 struct clk_bulk clks;
193 struct reset_ctl_bulk resets;
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100194 fdt_addr_t regs;
195};
196
197struct dwc3_glue_ops {
198 void (*select_dr_mode)(struct udevice *dev, int index,
199 enum usb_dr_mode mode);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100200};
201
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100202void dwc3_ti_select_dr_mode(struct udevice *dev, int index,
203 enum usb_dr_mode mode)
204{
205#define USBOTGSS_UTMI_OTG_STATUS 0x0084
206#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
207
208/* UTMI_OTG_STATUS REGISTER */
209#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
210#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
211#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
212#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
213#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
214#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
215#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
216enum dwc3_omap_utmi_mode {
217 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
218 DWC3_OMAP_UTMI_MODE_HW,
219 DWC3_OMAP_UTMI_MODE_SW,
220};
221
222 u32 use_id_pin;
223 u32 host_mode;
224 u32 reg;
225 u32 utmi_mode;
226 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
227
228 struct dwc3_glue_data *glue = dev_get_platdata(dev);
229 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
230
231 if (device_is_compatible(dev, "ti,am437x-dwc3"))
232 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
233
234 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
235 DWC3_OMAP_UTMI_MODE_UNKNOWN);
236 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
237 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
238 dev->name);
239 mode = USB_DR_MODE_PERIPHERAL;
240 }
241
242 switch (mode) {
243 case USB_DR_MODE_PERIPHERAL:
244 use_id_pin = 0;
245 host_mode = 0;
246 break;
247 case USB_DR_MODE_HOST:
248 use_id_pin = 0;
249 host_mode = 1;
250 break;
251 case USB_DR_MODE_OTG:
252 default:
253 use_id_pin = 1;
254 host_mode = 0;
255 break;
256 }
257
258 reg = readl(base + utmi_status_offset);
259
260 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
261 if (!use_id_pin)
262 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
263
264 writel(reg, base + utmi_status_offset);
265
266 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
267 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
268 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
269
270 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
271 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
272
273 if (!host_mode)
274 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
275 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
276
277 writel(reg, base + utmi_status_offset);
278
279 unmap_physmem(base, MAP_NOCACHE);
280}
281
282struct dwc3_glue_ops ti_ops = {
283 .select_dr_mode = dwc3_ti_select_dr_mode,
284};
285
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100286static int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200287{
Kever Yangac28e592020-03-04 08:59:50 +0800288 ofnode node;
Michal Simek49d67452018-05-18 13:15:06 +0200289 int ret;
290
Kever Yangac28e592020-03-04 08:59:50 +0800291 ofnode_for_each_subnode(node, parent->node) {
292 const char *name = ofnode_get_name(node);
Michal Simek49d67452018-05-18 13:15:06 +0200293 enum usb_dr_mode dr_mode;
294 struct udevice *dev;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100295 const char *driver = NULL;
Michal Simek49d67452018-05-18 13:15:06 +0200296
297 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek49d67452018-05-18 13:15:06 +0200298
299 dr_mode = usb_get_dr_mode(node);
300
301 switch (dr_mode) {
302 case USB_DR_MODE_PERIPHERAL:
303 case USB_DR_MODE_OTG:
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100304#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Michal Simek49d67452018-05-18 13:15:06 +0200305 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
306 driver = "dwc3-generic-peripheral";
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100307#endif
Michal Simek49d67452018-05-18 13:15:06 +0200308 break;
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200309#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || !defined(CONFIG_SPL_BUILD)
Michal Simek49d67452018-05-18 13:15:06 +0200310 case USB_DR_MODE_HOST:
311 debug("%s: dr_mode: HOST\n", __func__);
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200312 driver = "dwc3-generic-host";
Michal Simek49d67452018-05-18 13:15:06 +0200313 break;
Jean-Jacques Hiblotb575e902019-09-11 11:33:50 +0200314#endif
Michal Simek49d67452018-05-18 13:15:06 +0200315 default:
316 debug("%s: unsupported dr_mode\n", __func__);
317 return -ENODEV;
318 };
319
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100320 if (!driver)
321 continue;
322
Michal Simek49d67452018-05-18 13:15:06 +0200323 ret = device_bind_driver_to_node(parent, driver, name,
Kever Yangac28e592020-03-04 08:59:50 +0800324 node, &dev);
Michal Simek49d67452018-05-18 13:15:06 +0200325 if (ret) {
326 debug("%s: not able to bind usb device mode\n",
327 __func__);
328 return ret;
329 }
330 }
331
332 return 0;
333}
334
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100335static int dwc3_glue_reset_init(struct udevice *dev,
336 struct dwc3_glue_data *glue)
337{
338 int ret;
339
340 ret = reset_get_bulk(dev, &glue->resets);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530341 if (ret == -ENOTSUPP || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100342 return 0;
343 else if (ret)
344 return ret;
345
346 ret = reset_deassert_bulk(&glue->resets);
347 if (ret) {
348 reset_release_bulk(&glue->resets);
349 return ret;
350 }
351
352 return 0;
353}
354
355static int dwc3_glue_clk_init(struct udevice *dev,
356 struct dwc3_glue_data *glue)
357{
358 int ret;
359
360 ret = clk_get_bulk(dev, &glue->clks);
Vignesh Raghavendrad6244342019-10-25 13:48:05 +0530361 if (ret == -ENOSYS || ret == -ENOENT)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100362 return 0;
363 if (ret)
364 return ret;
365
366#if CONFIG_IS_ENABLED(CLK)
367 ret = clk_enable_bulk(&glue->clks);
368 if (ret) {
369 clk_release_bulk(&glue->clks);
370 return ret;
371 }
372#endif
373
374 return 0;
375}
376
377static int dwc3_glue_probe(struct udevice *dev)
378{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100379 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100380 struct dwc3_glue_data *glue = dev_get_platdata(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100381 struct udevice *child = NULL;
382 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100383 int ret;
384
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100385 glue->regs = dev_read_addr(dev);
386
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100387 ret = dwc3_glue_clk_init(dev, glue);
388 if (ret)
389 return ret;
390
391 ret = dwc3_glue_reset_init(dev, glue);
392 if (ret)
393 return ret;
394
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100395 ret = device_find_first_child(dev, &child);
396 if (ret)
397 return ret;
398
399 while (child) {
400 enum usb_dr_mode dr_mode;
401
Kever Yangac28e592020-03-04 08:59:50 +0800402 dr_mode = usb_get_dr_mode(child->node);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100403 device_find_next_child(&child);
404 if (ops && ops->select_dr_mode)
405 ops->select_dr_mode(dev, index, dr_mode);
406 index++;
407 }
408
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100409 return 0;
410}
411
412static int dwc3_glue_remove(struct udevice *dev)
413{
414 struct dwc3_glue_data *glue = dev_get_platdata(dev);
415
416 reset_release_bulk(&glue->resets);
417
418 clk_release_bulk(&glue->clks);
419
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200420 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100421}
422
423static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200424 { .compatible = "xlnx,zynqmp-dwc3" },
Siva Durga Prasad Paladugu648856a2020-05-12 08:36:01 +0200425 { .compatible = "xlnx,versal-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100426 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100427 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100428 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Vignesh Raghavendracab4e272019-12-09 10:37:29 +0530429 { .compatible = "ti,am654-dwc3" },
Michal Simek49d67452018-05-18 13:15:06 +0200430 { }
431};
432
433U_BOOT_DRIVER(dwc3_generic_wrapper) = {
434 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblot3b838292019-07-05 09:33:58 +0200435 .id = UCLASS_NOP,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100436 .of_match = dwc3_glue_ids,
437 .bind = dwc3_glue_bind,
438 .probe = dwc3_glue_probe,
439 .remove = dwc3_glue_remove,
440 .platdata_auto_alloc_size = sizeof(struct dwc3_glue_data),
441
Michal Simek49d67452018-05-18 13:15:06 +0200442};