blob: 70431facb362472e8993956b9f1bd44bd354a09b [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 Hiblot687ab542018-11-29 10:52:42 +010025#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010026struct dwc3_generic_peripheral {
27 struct dwc3 dwc3;
28 struct phy *phys;
29 int num_phys;
30 fdt_addr_t base;
31};
32
Jean-Jacques Hiblotff8d7552018-11-29 10:52:43 +010033int dm_usb_gadget_handle_interrupts(struct udevice *dev)
Michal Simek49d67452018-05-18 13:15:06 +020034{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010035 struct dwc3_generic_peripheral *priv = dev_get_priv(dev);
36 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020037
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010038 dwc3_gadget_uboot_handle_interrupt(dwc3);
Michal Simek49d67452018-05-18 13:15:06 +020039
40 return 0;
41}
42
43static int dwc3_generic_peripheral_probe(struct udevice *dev)
44{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010045 int rc;
46 struct dwc3_generic_peripheral *priv = dev_get_priv(dev);
47 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020048
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010049 rc = dwc3_setup_phy(dev, &priv->phys, &priv->num_phys);
50 if (rc)
51 return rc;
52
53 dwc3->regs = map_physmem(priv->base, DWC3_OTG_REGS_END, MAP_NOCACHE);
54 dwc3->regs += DWC3_GLOBALS_REGS_START;
55 dwc3->dev = dev;
56
57 rc = dwc3_init(dwc3);
58 if (rc) {
59 unmap_physmem(dwc3->regs, MAP_NOCACHE);
60 return rc;
61 }
62
63 return 0;
Michal Simek49d67452018-05-18 13:15:06 +020064}
65
66static int dwc3_generic_peripheral_remove(struct udevice *dev)
67{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010068 struct dwc3_generic_peripheral *priv = dev_get_priv(dev);
69 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020070
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010071 dwc3_remove(dwc3);
72 dwc3_shutdown_phy(dev, priv->phys, priv->num_phys);
73 unmap_physmem(dwc3->regs, MAP_NOCACHE);
Michal Simek49d67452018-05-18 13:15:06 +020074
75 return 0;
76}
77
78static int dwc3_generic_peripheral_ofdata_to_platdata(struct udevice *dev)
79{
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010080 struct dwc3_generic_peripheral *priv = dev_get_priv(dev);
81 struct dwc3 *dwc3 = &priv->dwc3;
Michal Simek49d67452018-05-18 13:15:06 +020082 int node = dev_of_offset(dev);
83
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010084 priv->base = devfdt_get_addr(dev);
Michal Simek49d67452018-05-18 13:15:06 +020085
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010086 dwc3->maximum_speed = usb_get_maximum_speed(node);
87 if (dwc3->maximum_speed == USB_SPEED_UNKNOWN) {
Michal Simek49d67452018-05-18 13:15:06 +020088 pr_err("Invalid usb maximum speed\n");
89 return -ENODEV;
90 }
91
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +010092 dwc3->dr_mode = usb_get_dr_mode(node);
93 if (dwc3->dr_mode == USB_DR_MODE_UNKNOWN) {
Michal Simek49d67452018-05-18 13:15:06 +020094 pr_err("Invalid usb mode setup\n");
95 return -ENODEV;
96 }
97
98 return 0;
99}
100
Michal Simek49d67452018-05-18 13:15:06 +0200101U_BOOT_DRIVER(dwc3_generic_peripheral) = {
102 .name = "dwc3-generic-peripheral",
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100103 .id = UCLASS_USB_GADGET_GENERIC,
Michal Simek49d67452018-05-18 13:15:06 +0200104 .ofdata_to_platdata = dwc3_generic_peripheral_ofdata_to_platdata,
105 .probe = dwc3_generic_peripheral_probe,
106 .remove = dwc3_generic_peripheral_remove,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100107 .priv_auto_alloc_size = sizeof(struct dwc3_generic_peripheral),
Michal Simek49d67452018-05-18 13:15:06 +0200108};
Jean-Jacques Hiblot687ab542018-11-29 10:52:42 +0100109#endif
Michal Simek49d67452018-05-18 13:15:06 +0200110
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100111struct dwc3_glue_data {
112 struct clk_bulk clks;
113 struct reset_ctl_bulk resets;
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100114 fdt_addr_t regs;
115};
116
117struct dwc3_glue_ops {
118 void (*select_dr_mode)(struct udevice *dev, int index,
119 enum usb_dr_mode mode);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100120};
121
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100122void dwc3_ti_select_dr_mode(struct udevice *dev, int index,
123 enum usb_dr_mode mode)
124{
125#define USBOTGSS_UTMI_OTG_STATUS 0x0084
126#define USBOTGSS_UTMI_OTG_OFFSET 0x0480
127
128/* UTMI_OTG_STATUS REGISTER */
129#define USBOTGSS_UTMI_OTG_STATUS_SW_MODE BIT(31)
130#define USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT BIT(9)
131#define USBOTGSS_UTMI_OTG_STATUS_TXBITSTUFFENABLE BIT(8)
132#define USBOTGSS_UTMI_OTG_STATUS_IDDIG BIT(4)
133#define USBOTGSS_UTMI_OTG_STATUS_SESSEND BIT(3)
134#define USBOTGSS_UTMI_OTG_STATUS_SESSVALID BIT(2)
135#define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID BIT(1)
136enum dwc3_omap_utmi_mode {
137 DWC3_OMAP_UTMI_MODE_UNKNOWN = 0,
138 DWC3_OMAP_UTMI_MODE_HW,
139 DWC3_OMAP_UTMI_MODE_SW,
140};
141
142 u32 use_id_pin;
143 u32 host_mode;
144 u32 reg;
145 u32 utmi_mode;
146 u32 utmi_status_offset = USBOTGSS_UTMI_OTG_STATUS;
147
148 struct dwc3_glue_data *glue = dev_get_platdata(dev);
149 void *base = map_physmem(glue->regs, 0x10000, MAP_NOCACHE);
150
151 if (device_is_compatible(dev, "ti,am437x-dwc3"))
152 utmi_status_offset += USBOTGSS_UTMI_OTG_OFFSET;
153
154 utmi_mode = dev_read_u32_default(dev, "utmi-mode",
155 DWC3_OMAP_UTMI_MODE_UNKNOWN);
156 if (utmi_mode != DWC3_OMAP_UTMI_MODE_HW) {
157 debug("%s: OTG is not supported. defaulting to PERIPHERAL\n",
158 dev->name);
159 mode = USB_DR_MODE_PERIPHERAL;
160 }
161
162 switch (mode) {
163 case USB_DR_MODE_PERIPHERAL:
164 use_id_pin = 0;
165 host_mode = 0;
166 break;
167 case USB_DR_MODE_HOST:
168 use_id_pin = 0;
169 host_mode = 1;
170 break;
171 case USB_DR_MODE_OTG:
172 default:
173 use_id_pin = 1;
174 host_mode = 0;
175 break;
176 }
177
178 reg = readl(base + utmi_status_offset);
179
180 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SW_MODE);
181 if (!use_id_pin)
182 reg |= USBOTGSS_UTMI_OTG_STATUS_SW_MODE;
183
184 writel(reg, base + utmi_status_offset);
185
186 reg &= ~(USBOTGSS_UTMI_OTG_STATUS_SESSEND |
187 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID |
188 USBOTGSS_UTMI_OTG_STATUS_IDDIG);
189
190 reg |= USBOTGSS_UTMI_OTG_STATUS_SESSVALID |
191 USBOTGSS_UTMI_OTG_STATUS_POWERPRESENT;
192
193 if (!host_mode)
194 reg |= USBOTGSS_UTMI_OTG_STATUS_IDDIG |
195 USBOTGSS_UTMI_OTG_STATUS_VBUSVALID;
196
197 writel(reg, base + utmi_status_offset);
198
199 unmap_physmem(base, MAP_NOCACHE);
200}
201
202struct dwc3_glue_ops ti_ops = {
203 .select_dr_mode = dwc3_ti_select_dr_mode,
204};
205
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100206static int dwc3_glue_bind(struct udevice *parent)
Michal Simek49d67452018-05-18 13:15:06 +0200207{
208 const void *fdt = gd->fdt_blob;
209 int node;
210 int ret;
211
212 for (node = fdt_first_subnode(fdt, dev_of_offset(parent)); node > 0;
213 node = fdt_next_subnode(fdt, node)) {
214 const char *name = fdt_get_name(fdt, node, NULL);
215 enum usb_dr_mode dr_mode;
216 struct udevice *dev;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100217 const char *driver = NULL;
Michal Simek49d67452018-05-18 13:15:06 +0200218
219 debug("%s: subnode name: %s\n", __func__, name);
Michal Simek49d67452018-05-18 13:15:06 +0200220
221 dr_mode = usb_get_dr_mode(node);
222
223 switch (dr_mode) {
224 case USB_DR_MODE_PERIPHERAL:
225 case USB_DR_MODE_OTG:
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100226#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Michal Simek49d67452018-05-18 13:15:06 +0200227 debug("%s: dr_mode: OTG or Peripheral\n", __func__);
228 driver = "dwc3-generic-peripheral";
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100229#endif
Michal Simek49d67452018-05-18 13:15:06 +0200230 break;
231 case USB_DR_MODE_HOST:
232 debug("%s: dr_mode: HOST\n", __func__);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100233 driver = "xhci-dwc3";
Michal Simek49d67452018-05-18 13:15:06 +0200234 break;
235 default:
236 debug("%s: unsupported dr_mode\n", __func__);
237 return -ENODEV;
238 };
239
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100240 if (!driver)
241 continue;
242
Michal Simek49d67452018-05-18 13:15:06 +0200243 ret = device_bind_driver_to_node(parent, driver, name,
244 offset_to_ofnode(node), &dev);
245 if (ret) {
246 debug("%s: not able to bind usb device mode\n",
247 __func__);
248 return ret;
249 }
250 }
251
252 return 0;
253}
254
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100255static int dwc3_glue_reset_init(struct udevice *dev,
256 struct dwc3_glue_data *glue)
257{
258 int ret;
259
260 ret = reset_get_bulk(dev, &glue->resets);
261 if (ret == -ENOTSUPP)
262 return 0;
263 else if (ret)
264 return ret;
265
266 ret = reset_deassert_bulk(&glue->resets);
267 if (ret) {
268 reset_release_bulk(&glue->resets);
269 return ret;
270 }
271
272 return 0;
273}
274
275static int dwc3_glue_clk_init(struct udevice *dev,
276 struct dwc3_glue_data *glue)
277{
278 int ret;
279
280 ret = clk_get_bulk(dev, &glue->clks);
281 if (ret == -ENOSYS)
282 return 0;
283 if (ret)
284 return ret;
285
286#if CONFIG_IS_ENABLED(CLK)
287 ret = clk_enable_bulk(&glue->clks);
288 if (ret) {
289 clk_release_bulk(&glue->clks);
290 return ret;
291 }
292#endif
293
294 return 0;
295}
296
297static int dwc3_glue_probe(struct udevice *dev)
298{
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100299 struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev);
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100300 struct dwc3_glue_data *glue = dev_get_platdata(dev);
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100301 struct udevice *child = NULL;
302 int index = 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100303 int ret;
304
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100305 glue->regs = dev_read_addr(dev);
306
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100307 ret = dwc3_glue_clk_init(dev, glue);
308 if (ret)
309 return ret;
310
311 ret = dwc3_glue_reset_init(dev, glue);
312 if (ret)
313 return ret;
314
Jean-Jacques Hiblot93991cf2018-11-29 10:52:49 +0100315 ret = device_find_first_child(dev, &child);
316 if (ret)
317 return ret;
318
319 while (child) {
320 enum usb_dr_mode dr_mode;
321
322 dr_mode = usb_get_dr_mode(dev_of_offset(child));
323 device_find_next_child(&child);
324 if (ops && ops->select_dr_mode)
325 ops->select_dr_mode(dev, index, dr_mode);
326 index++;
327 }
328
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100329 return 0;
330}
331
332static int dwc3_glue_remove(struct udevice *dev)
333{
334 struct dwc3_glue_data *glue = dev_get_platdata(dev);
335
336 reset_release_bulk(&glue->resets);
337
338 clk_release_bulk(&glue->clks);
339
Jean-Jacques Hiblote445d462019-07-05 09:33:56 +0200340 return 0;
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100341}
342
343static const struct udevice_id dwc3_glue_ids[] = {
Michal Simek49d67452018-05-18 13:15:06 +0200344 { .compatible = "xlnx,zynqmp-dwc3" },
Jean-Jacques Hiblot1c03ade2018-12-04 11:12:56 +0100345 { .compatible = "ti,keystone-dwc3"},
Jean-Jacques Hiblotd66e54a2018-11-29 10:57:40 +0100346 { .compatible = "ti,dwc3", .data = (ulong)&ti_ops },
Jean-Jacques Hiblot1ce5f1f2018-12-04 11:30:50 +0100347 { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops },
Michal Simek49d67452018-05-18 13:15:06 +0200348 { }
349};
350
351U_BOOT_DRIVER(dwc3_generic_wrapper) = {
352 .name = "dwc3-generic-wrapper",
353 .id = UCLASS_MISC,
Jean-Jacques Hiblot446e3a22018-11-29 10:52:48 +0100354 .of_match = dwc3_glue_ids,
355 .bind = dwc3_glue_bind,
356 .probe = dwc3_glue_probe,
357 .remove = dwc3_glue_remove,
358 .platdata_auto_alloc_size = sizeof(struct dwc3_glue_data),
359
Michal Simek49d67452018-05-18 13:15:06 +0200360};