blob: f29b93d191f1b4cccb26eb8f398de22e359ca712 [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>
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +010011#include <asm-generic/io.h>
Michal Simek49d67452018-05-18 13:15:06 +020012#include <dm.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010015#include <dwc3-uboot.h>
Michal Simek49d67452018-05-18 13:15:06 +020016#include <linux/usb/ch9.h>
17#include <linux/usb/gadget.h>
18#include <malloc.h>
19#include <usb.h>
20#include "core.h"
21#include "gadget.h"
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010022#include <reset.h>
23#include <clk.h>
Michal Simek49d67452018-05-18 13:15:06 +020024
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020025struct dwc3_generic_plat {
26 fdt_addr_t base;
27 u32 maximum_speed;
28 enum usb_dr_mode dr_mode;
29};
30
Jean-Jacques Hiblot687ab542018-11-29 10:52:42 +010031#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020032struct dwc3_generic_priv {
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010033 struct dwc3 dwc3;
34 struct phy *phys;
35 int num_phys;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010036};
37
Jean-Jacques Hiblotff8d7552018-11-29 10:52:43 +010038int dm_usb_gadget_handle_interrupts(struct udevice *dev)
Michal Simek49d67452018-05-18 13:15:06 +020039{
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020040 struct dwc3_generic_priv *priv = dev_get_priv(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010041 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020042
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010043 dwc3_gadget_uboot_handle_interrupt(dwc3);
Michal Simek49d67452018-05-18 13:15:06 +020044
45 return 0;
46}
47
48static int dwc3_generic_peripheral_probe(struct udevice *dev)
49{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010050 int rc;
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020051 struct dwc3_generic_priv *priv = dev_get_priv(dev);
52 struct dwc3_generic_plat *plat = dev_get_platdata(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010053 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020054
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020055 dwc3->maximum_speed = plat->maximum_speed;
56 dwc3->dr_mode = plat->dr_mode;
57
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010058 rc = dwc3_setup_phy(dev, &priv->phys, &priv->num_phys);
59 if (rc)
60 return rc;
61
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020062 dwc3->regs = map_physmem(plat->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010063 dwc3->regs += DWC3_GLOBALS_REGS_START;
64 dwc3->dev = dev;
65
66 rc = dwc3_init(dwc3);
67 if (rc) {
68 unmap_physmem(dwc3->regs, MAP_NOCACHE);
69 return rc;
70 }
71
72 return 0;
Michal Simek49d67452018-05-18 13:15:06 +020073}
74
75static int dwc3_generic_peripheral_remove(struct udevice *dev)
76{
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020077 struct dwc3_generic_priv *priv = dev_get_priv(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010078 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020079
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010080 dwc3_remove(dwc3);
81 dwc3_shutdown_phy(dev, priv->phys, priv->num_phys);
82 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek49d67452018-05-18 13:15:06 +020083
84 return 0;
85}
86
87static int dwc3_generic_peripheral_ofdata_to_platdata(struct udevice *dev)
88{
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020089 struct dwc3_generic_plat *plat = dev_get_platdata(dev);
Michal Simek49d67452018-05-18 13:15:06 +020090 int node = dev_of_offset(dev);
91
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020092 plat->base = devfdt_get_addr(dev);
Michal Simek49d67452018-05-18 13:15:06 +020093
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +020094 plat->maximum_speed = usb_get_maximum_speed(node);
95 if (plat->maximum_speed == USB_SPEED_UNKNOWN) {
Michal Simek49d67452018-05-18 13:15:06 +020096 pr_err("Invalid usb maximum speed\n");
97 return -ENODEV;
98 }
99
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200100 plat->dr_mode = usb_get_dr_mode(node);
101 if (plat->dr_mode == USB_DR_MODE_UNKNOWN) {
Michal Simek49d67452018-05-18 13:15:06 +0200102 pr_err("Invalid usb mode setup\n");
103 return -ENODEV;
104 }
105
106 return 0;
107}
108
Michal Simek49d67452018-05-18 13:15:06 +0200109U_BOOT_DRIVER(dwc3_generic_peripheral) = {
110 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100111 .id = UCLASS_USB_GADGET_GENERIC,
Michal Simek49d67452018-05-18 13:15:06 +0200112 .ofdata_to_platdata = dwc3_generic_peripheral_ofdata_to_platdata,
113 .probe = dwc3_generic_peripheral_probe,
114 .remove = dwc3_generic_peripheral_remove,
Jean-Jacques Hiblot3a38a0a2019-09-11 11:33:48 +0200115 .priv_auto_alloc_size = sizeof(struct dwc3_generic_priv),
116 .platdata_auto_alloc_size = sizeof(struct dwc3_generic_plat),
Michal Simek49d67452018-05-18 13:15:06 +0200117};
Jean-Jacques Hiblot687ab542018-11-29 10:52:42 +0100118#endif
Michal Simek49d67452018-05-18 13:15:06 +0200119
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100120struct dwc3_glue_data {
121 struct clk_bulk clks;
122 struct reset_ctl_bulk resets;
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100123 fdt_addr_t regs;
124};
125
126struct dwc3_glue_ops {
127 void (*select_dr_mode)(struct udevice *dev, int index,
128 enum usb_dr_mode mode);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100129};
130
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100131void dwc3_ti_select_dr_mode(struct udevice *dev, int index,
132 enum usb_dr_mode mode)
133{
134#define USBOTGSS_UTMI_OTG_STATUS 0x0084
135#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
136
137/* UTMI_OTG_STATUS REGISTER */
138#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
139#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
140#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
141#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
142#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
143#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
144#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
145enum dwc3_omap_utmi_mode {
146 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
147 DWC3_OMAP_UTMI_MODE_HW,
148 DWC3_OMAP_UTMI_MODE_SW,
149};
150
151 u32 use_id_pin;
152 u32 host_mode;
153 u32 reg;
154 u32 utmi_mode;
155 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
156
157 struct dwc3_glue_data *glue = dev_get_platdata(dev);
158 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
159
160 if (device_is_compatible(dev, "ti,am437x-dwc3"))
161 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
162
163 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
164 DWC3_OMAP_UTMI_MODE_UNKNOWN);
165 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
166 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
167 dev->name);
168 mode = USB_DR_MODE_PERIPHERAL;
169 }
170
171 switch (mode) {
172 case USB_DR_MODE_PERIPHERAL:
173 use_id_pin = 0;
174 host_mode = 0;
175 break;
176 case USB_DR_MODE_HOST:
177 use_id_pin = 0;
178 host_mode = 1;
179 break;
180 case USB_DR_MODE_OTG:
181 default:
182 use_id_pin = 1;
183 host_mode = 0;
184 break;
185 }
186
187 reg = readl(base + utmi_status_offset);
188
189 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
190 if (!use_id_pin)
191 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
192
193 writel(reg, base + utmi_status_offset);
194
195 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
196 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
197 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
198
199 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
200 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
201
202 if (!host_mode)
203 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
204 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
205
206 writel(reg, base + utmi_status_offset);
207
208 unmap_physmem(base, MAP_NOCACHE);
209}
210
211struct dwc3_glue_ops ti_ops = {
212 .select_dr_mode = dwc3_ti_select_dr_mode,
213};
214
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100215static int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200216{
217 const void *fdt = gd->fdt_blob;
218 int node;
219 int ret;
220
221 for (node = fdt_first_subnode(fdt, dev_of_offset(parent)); node > 0;
222 node = fdt_next_subnode(fdt, node)) {
223 const char *name = fdt_get_name(fdt, node, NULL);
224 enum usb_dr_mode dr_mode;
225 struct udevice *dev;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100226 const char *driver = NULL;
Michal Simek49d67452018-05-18 13:15:06 +0200227
228 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek49d67452018-05-18 13:15:06 +0200229
230 dr_mode = usb_get_dr_mode(node);
231
232 switch (dr_mode) {
233 case USB_DR_MODE_PERIPHERAL:
234 case USB_DR_MODE_OTG:
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100235#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Michal Simek49d67452018-05-18 13:15:06 +0200236 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
237 driver = "dwc3-generic-peripheral";
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100238#endif
Michal Simek49d67452018-05-18 13:15:06 +0200239 break;
240 case USB_DR_MODE_HOST:
241 debug("%s: dr_mode: HOST\n", __func__);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100242 driver = "xhci-dwc3";
Michal Simek49d67452018-05-18 13:15:06 +0200243 break;
244 default:
245 debug("%s: unsupported dr_mode\n", __func__);
246 return -ENODEV;
247 };
248
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100249 if (!driver)
250 continue;
251
Michal Simek49d67452018-05-18 13:15:06 +0200252 ret = device_bind_driver_to_node(parent, driver, name,
253 offset_to_ofnode(node), &dev);
254 if (ret) {
255 debug("%s: not able to bind usb device mode\n",
256 __func__);
257 return ret;
258 }
259 }
260
261 return 0;
262}
263
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100264static int dwc3_glue_reset_init(struct udevice *dev,
265 struct dwc3_glue_data *glue)
266{
267 int ret;
268
269 ret = reset_get_bulk(dev, &glue->resets);
270 if (ret == -ENOTSUPP)
271 return 0;
272 else if (ret)
273 return ret;
274
275 ret = reset_deassert_bulk(&glue->resets);
276 if (ret) {
277 reset_release_bulk(&glue->resets);
278 return ret;
279 }
280
281 return 0;
282}
283
284static int dwc3_glue_clk_init(struct udevice *dev,
285 struct dwc3_glue_data *glue)
286{
287 int ret;
288
289 ret = clk_get_bulk(dev, &glue->clks);
290 if (ret == -ENOSYS)
291 return 0;
292 if (ret)
293 return ret;
294
295#if CONFIG_IS_ENABLED(CLK)
296 ret = clk_enable_bulk(&glue->clks);
297 if (ret) {
298 clk_release_bulk(&glue->clks);
299 return ret;
300 }
301#endif
302
303 return 0;
304}
305
306static int dwc3_glue_probe(struct udevice *dev)
307{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100308 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100309 struct dwc3_glue_data *glue = dev_get_platdata(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100310 struct udevice *child = NULL;
311 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100312 int ret;
313
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100314 glue->regs = dev_read_addr(dev);
315
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100316 ret = dwc3_glue_clk_init(dev, glue);
317 if (ret)
318 return ret;
319
320 ret = dwc3_glue_reset_init(dev, glue);
321 if (ret)
322 return ret;
323
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100324 ret = device_find_first_child(dev, &child);
325 if (ret)
326 return ret;
327
328 while (child) {
329 enum usb_dr_mode dr_mode;
330
331 dr_mode = usb_get_dr_mode(dev_of_offset(child));
332 device_find_next_child(&child);
333 if (ops && ops->select_dr_mode)
334 ops->select_dr_mode(dev, index, dr_mode);
335 index++;
336 }
337
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100338 return 0;
339}
340
341static int dwc3_glue_remove(struct udevice *dev)
342{
343 struct dwc3_glue_data *glue = dev_get_platdata(dev);
344
345 reset_release_bulk(&glue->resets);
346
347 clk_release_bulk(&glue->clks);
348
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200349 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100350}
351
352static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200353 { .compatible = "xlnx,zynqmp-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100354 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100355 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100356 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Michal Simek49d67452018-05-18 13:15:06 +0200357 { }
358};
359
360U_BOOT_DRIVER(dwc3_generic_wrapper) = {
361 .name = "dwc3-generic-wrapper",
Jean-Jacques Hiblot3b838292019-07-05 09:33:58 +0200362 .id = UCLASS_NOP,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100363 .of_match = dwc3_glue_ids,
364 .bind = dwc3_glue_bind,
365 .probe = dwc3_glue_probe,
366 .remove = dwc3_glue_remove,
367 .platdata_auto_alloc_size = sizeof(struct dwc3_glue_data),
368
Michal Simek49d67452018-05-18 13:15:06 +0200369};