Patrick Delaunay | 22929e1 | 2018-10-26 09:02:52 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 2 | /* |
| 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 Hiblot | 93991cf | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 11 | #include <asm-generic/io.h> |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/lists.h> |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 15 | #include <dwc3-uboot.h> |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 16 | #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 Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 22 | #include <reset.h> |
| 23 | #include <clk.h> |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 24 | |
Jean-Jacques Hiblot | 687ab54 | 2018-11-29 10:52:42 +0100 | [diff] [blame] | 25 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 26 | struct dwc3_generic_peripheral { |
| 27 | struct dwc3 dwc3; |
| 28 | struct phy *phys; |
| 29 | int num_phys; |
| 30 | fdt_addr_t base; |
| 31 | }; |
| 32 | |
Jean-Jacques Hiblot | ff8d755 | 2018-11-29 10:52:43 +0100 | [diff] [blame] | 33 | int dm_usb_gadget_handle_interrupts(struct udevice *dev) |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 34 | { |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 35 | struct dwc3_generic_peripheral *priv = dev_get_priv(dev); |
| 36 | struct dwc3 *dwc3 = &priv->dwc3; |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 37 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 38 | dwc3_gadget_uboot_handle_interrupt(dwc3); |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int dwc3_generic_peripheral_probe(struct udevice *dev) |
| 44 | { |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 45 | int rc; |
| 46 | struct dwc3_generic_peripheral *priv = dev_get_priv(dev); |
| 47 | struct dwc3 *dwc3 = &priv->dwc3; |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 48 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 49 | 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 Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int dwc3_generic_peripheral_remove(struct udevice *dev) |
| 67 | { |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 68 | struct dwc3_generic_peripheral *priv = dev_get_priv(dev); |
| 69 | struct dwc3 *dwc3 = &priv->dwc3; |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 70 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 71 | dwc3_remove(dwc3); |
| 72 | dwc3_shutdown_phy(dev, priv->phys, priv->num_phys); |
| 73 | unmap_physmem(dwc3->regs, MAP_NOCACHE); |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int dwc3_generic_peripheral_ofdata_to_platdata(struct udevice *dev) |
| 79 | { |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 80 | struct dwc3_generic_peripheral *priv = dev_get_priv(dev); |
| 81 | struct dwc3 *dwc3 = &priv->dwc3; |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 82 | int node = dev_of_offset(dev); |
| 83 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 84 | priv->base = devfdt_get_addr(dev); |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 85 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 86 | dwc3->maximum_speed = usb_get_maximum_speed(node); |
| 87 | if (dwc3->maximum_speed == USB_SPEED_UNKNOWN) { |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 88 | pr_err("Invalid usb maximum speed\n"); |
| 89 | return -ENODEV; |
| 90 | } |
| 91 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 92 | dwc3->dr_mode = usb_get_dr_mode(node); |
| 93 | if (dwc3->dr_mode == USB_DR_MODE_UNKNOWN) { |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 94 | pr_err("Invalid usb mode setup\n"); |
| 95 | return -ENODEV; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 101 | U_BOOT_DRIVER(dwc3_generic_peripheral) = { |
| 102 | .name = "dwc3-generic-peripheral", |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 103 | .id = UCLASS_USB_GADGET_GENERIC, |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 104 | .ofdata_to_platdata = dwc3_generic_peripheral_ofdata_to_platdata, |
| 105 | .probe = dwc3_generic_peripheral_probe, |
| 106 | .remove = dwc3_generic_peripheral_remove, |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 107 | .priv_auto_alloc_size = sizeof(struct dwc3_generic_peripheral), |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 108 | }; |
Jean-Jacques Hiblot | 687ab54 | 2018-11-29 10:52:42 +0100 | [diff] [blame] | 109 | #endif |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 110 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 111 | struct dwc3_glue_data { |
| 112 | struct clk_bulk clks; |
| 113 | struct reset_ctl_bulk resets; |
Jean-Jacques Hiblot | 93991cf | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 114 | fdt_addr_t regs; |
| 115 | }; |
| 116 | |
| 117 | struct dwc3_glue_ops { |
| 118 | void (*select_dr_mode)(struct udevice *dev, int index, |
| 119 | enum usb_dr_mode mode); |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Jean-Jacques Hiblot | d66e54a | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 122 | void 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) |
| 136 | enum 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 | |
| 202 | struct dwc3_glue_ops ti_ops = { |
| 203 | .select_dr_mode = dwc3_ti_select_dr_mode, |
| 204 | }; |
| 205 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 206 | static int dwc3_glue_bind(struct udevice *parent) |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 207 | { |
| 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 Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 217 | const char *driver = NULL; |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 218 | |
| 219 | debug("%s: subnode name: %s\n", __func__, name); |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 220 | |
| 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 Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 226 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 227 | debug("%s: dr_mode: OTG or Peripheral\n", __func__); |
| 228 | driver = "dwc3-generic-peripheral"; |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 229 | #endif |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 230 | break; |
| 231 | case USB_DR_MODE_HOST: |
| 232 | debug("%s: dr_mode: HOST\n", __func__); |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 233 | driver = "xhci-dwc3"; |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 234 | break; |
| 235 | default: |
| 236 | debug("%s: unsupported dr_mode\n", __func__); |
| 237 | return -ENODEV; |
| 238 | }; |
| 239 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 240 | if (!driver) |
| 241 | continue; |
| 242 | |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 243 | 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 Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 255 | static 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 | |
| 275 | static 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 | |
| 297 | static int dwc3_glue_probe(struct udevice *dev) |
| 298 | { |
Jean-Jacques Hiblot | 93991cf | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 299 | struct dwc3_glue_ops *ops = (struct dwc3_glue_ops *)dev_get_driver_data(dev); |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 300 | struct dwc3_glue_data *glue = dev_get_platdata(dev); |
Jean-Jacques Hiblot | 93991cf | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 301 | struct udevice *child = NULL; |
| 302 | int index = 0; |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 303 | int ret; |
| 304 | |
Jean-Jacques Hiblot | 93991cf | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 305 | glue->regs = dev_read_addr(dev); |
| 306 | |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 307 | 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 Hiblot | 93991cf | 2018-11-29 10:52:49 +0100 | [diff] [blame] | 315 | 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 Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static 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 Hiblot | e445d46 | 2019-07-05 09:33:56 +0200 | [diff] [blame^] | 340 | return 0; |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static const struct udevice_id dwc3_glue_ids[] = { |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 344 | { .compatible = "xlnx,zynqmp-dwc3" }, |
Jean-Jacques Hiblot | 1c03ade | 2018-12-04 11:12:56 +0100 | [diff] [blame] | 345 | { .compatible = "ti,keystone-dwc3"}, |
Jean-Jacques Hiblot | d66e54a | 2018-11-29 10:57:40 +0100 | [diff] [blame] | 346 | { .compatible = "ti,dwc3", .data = (ulong)&ti_ops }, |
Jean-Jacques Hiblot | 1ce5f1f | 2018-12-04 11:30:50 +0100 | [diff] [blame] | 347 | { .compatible = "ti,am437x-dwc3", .data = (ulong)&ti_ops }, |
Michal Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 348 | { } |
| 349 | }; |
| 350 | |
| 351 | U_BOOT_DRIVER(dwc3_generic_wrapper) = { |
| 352 | .name = "dwc3-generic-wrapper", |
| 353 | .id = UCLASS_MISC, |
Jean-Jacques Hiblot | 446e3a2 | 2018-11-29 10:52:48 +0100 | [diff] [blame] | 354 | .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 Simek | 49d6745 | 2018-05-18 13:15:06 +0200 | [diff] [blame] | 360 | }; |