Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 2 | /* |
| 3 | * MISC driver for TI MUSB Glue. |
| 4 | * |
| 5 | * (C) Copyright 2016 |
| 6 | * Texas Instruments Incorporated, <www.ti.com> |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 7 | */ |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
| 10 | #include <console.h> |
| 11 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 13 | #include <linux/usb/otg.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/lists.h> |
| 16 | |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/omap_musb.h> |
| 19 | #include "musb_uboot.h" |
| 20 | |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(DM_USB) |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 24 | /* USB 2.0 PHY Control */ |
| 25 | #define CM_PHY_PWRDN (1 << 0) |
| 26 | #define CM_PHY_OTG_PWRDN (1 << 1) |
| 27 | #define OTGVDET_EN (1 << 19) |
| 28 | #define OTGSESSENDEN (1 << 20) |
| 29 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 30 | #define AM335X_USB0_CTRL 0x0 |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 31 | #define AM335X_USB1_CTRL 0x8 |
| 32 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 33 | static void ti_musb_set_phy_power(struct udevice *dev, u8 on) |
| 34 | { |
| 35 | struct ti_musb_platdata *platdata = dev_get_platdata(dev); |
| 36 | |
| 37 | if (!platdata->ctrl_mod_base) |
| 38 | return; |
| 39 | |
| 40 | if (on) { |
| 41 | clrsetbits_le32(platdata->ctrl_mod_base, |
| 42 | CM_PHY_PWRDN | CM_PHY_OTG_PWRDN, |
| 43 | OTGVDET_EN | OTGSESSENDEN); |
| 44 | } else { |
| 45 | clrsetbits_le32(platdata->ctrl_mod_base, 0, |
| 46 | CM_PHY_PWRDN | CM_PHY_OTG_PWRDN); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 51 | |
| 52 | static int ti_musb_get_usb_index(int node) |
| 53 | { |
| 54 | const void *fdt = gd->fdt_blob; |
| 55 | int i = 0; |
| 56 | char path[64]; |
| 57 | const char *alias_path; |
| 58 | char alias[16]; |
| 59 | |
| 60 | fdt_get_path(fdt, node, path, sizeof(path)); |
| 61 | |
| 62 | do { |
| 63 | snprintf(alias, sizeof(alias), "usb%d", i); |
| 64 | alias_path = fdt_get_alias(fdt, alias); |
| 65 | if (alias_path == NULL) { |
| 66 | debug("USB index not found\n"); |
| 67 | return -ENOENT; |
| 68 | } |
| 69 | |
| 70 | if (!strcmp(path, alias_path)) |
| 71 | return i; |
| 72 | |
| 73 | i++; |
| 74 | } while (alias_path); |
| 75 | |
| 76 | return -ENOENT; |
| 77 | } |
| 78 | |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 79 | static int ti_musb_ofdata_to_platdata(struct udevice *dev) |
| 80 | { |
| 81 | struct ti_musb_platdata *platdata = dev_get_platdata(dev); |
| 82 | const void *fdt = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 83 | int node = dev_of_offset(dev); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 84 | int phys; |
| 85 | int ctrl_mod; |
| 86 | int usb_index; |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 87 | struct musb_hdrc_config *musb_config; |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 88 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 89 | platdata->base = (void *)devfdt_get_addr_index(dev, 1); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 90 | |
| 91 | phys = fdtdec_lookup_phandle(fdt, node, "phys"); |
| 92 | ctrl_mod = fdtdec_lookup_phandle(fdt, phys, "ti,ctrl_mod"); |
| 93 | platdata->ctrl_mod_base = (void *)fdtdec_get_addr(fdt, ctrl_mod, "reg"); |
| 94 | usb_index = ti_musb_get_usb_index(node); |
| 95 | switch (usb_index) { |
| 96 | case 1: |
| 97 | platdata->ctrl_mod_base += AM335X_USB1_CTRL; |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 98 | break; |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 99 | case 0: |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 100 | platdata->ctrl_mod_base += AM335X_USB0_CTRL; |
| 101 | break; |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 102 | default: |
| 103 | break; |
| 104 | } |
| 105 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 106 | musb_config = malloc(sizeof(struct musb_hdrc_config)); |
| 107 | memset(musb_config, 0, sizeof(struct musb_hdrc_config)); |
| 108 | |
| 109 | musb_config->multipoint = fdtdec_get_int(fdt, node, |
| 110 | "mentor,multipoint", -1); |
| 111 | if (musb_config->multipoint < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 112 | pr_err("MUSB multipoint DT entry missing\n"); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 113 | return -ENOENT; |
| 114 | } |
| 115 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 116 | musb_config->dyn_fifo = 1; |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 117 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 118 | musb_config->num_eps = fdtdec_get_int(fdt, node, "mentor,num-eps", |
| 119 | -1); |
| 120 | if (musb_config->num_eps < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 121 | pr_err("MUSB num-eps DT entry missing\n"); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 122 | return -ENOENT; |
| 123 | } |
| 124 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 125 | musb_config->ram_bits = fdtdec_get_int(fdt, node, "mentor,ram-bits", |
| 126 | -1); |
| 127 | if (musb_config->ram_bits < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 128 | pr_err("MUSB ram-bits DT entry missing\n"); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 129 | return -ENOENT; |
| 130 | } |
| 131 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 132 | platdata->plat.config = musb_config; |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 133 | |
| 134 | platdata->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1); |
| 135 | if (platdata->plat.power < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 136 | pr_err("MUSB mentor,power DT entry missing\n"); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 137 | return -ENOENT; |
| 138 | } |
| 139 | |
| 140 | platdata->plat.platform_ops = &musb_dsps_ops; |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 144 | #endif |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 145 | |
| 146 | static int ti_musb_host_probe(struct udevice *dev) |
| 147 | { |
| 148 | struct musb_host_data *host = dev_get_priv(dev); |
| 149 | struct ti_musb_platdata *platdata = dev_get_platdata(dev); |
| 150 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 151 | int ret; |
| 152 | |
| 153 | priv->desc_before_addr = true; |
| 154 | |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 155 | host->host = musb_init_controller(&platdata->plat, |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 156 | NULL, |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 157 | platdata->base); |
| 158 | if (!host->host) |
| 159 | return -EIO; |
| 160 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 161 | ti_musb_set_phy_power(dev, 1); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 162 | ret = musb_lowlevel_init(host); |
| 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | static int ti_musb_host_remove(struct udevice *dev) |
| 168 | { |
| 169 | struct musb_host_data *host = dev_get_priv(dev); |
| 170 | |
| 171 | musb_stop(host->host); |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 172 | ti_musb_set_phy_power(dev, 0); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 177 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 178 | static int ti_musb_host_ofdata_to_platdata(struct udevice *dev) |
| 179 | { |
| 180 | struct ti_musb_platdata *platdata = dev_get_platdata(dev); |
| 181 | const void *fdt = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 182 | int node = dev_of_offset(dev); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 183 | int ret; |
| 184 | |
| 185 | ret = ti_musb_ofdata_to_platdata(dev); |
| 186 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 187 | pr_err("platdata dt parse error\n"); |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | platdata->plat.mode = MUSB_HOST; |
| 192 | |
| 193 | return 0; |
| 194 | } |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 195 | #endif |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 196 | |
| 197 | U_BOOT_DRIVER(ti_musb_host) = { |
| 198 | .name = "ti-musb-host", |
| 199 | .id = UCLASS_USB, |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 200 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 201 | .ofdata_to_platdata = ti_musb_host_ofdata_to_platdata, |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 202 | #endif |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 203 | .probe = ti_musb_host_probe, |
| 204 | .remove = ti_musb_host_remove, |
| 205 | .ops = &musb_usb_ops, |
| 206 | .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata), |
| 207 | .priv_auto_alloc_size = sizeof(struct musb_host_data), |
| 208 | }; |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 209 | |
| 210 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
| 211 | struct ti_musb_peripheral { |
| 212 | struct musb *periph; |
| 213 | }; |
| 214 | |
| 215 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 216 | static int ti_musb_peripheral_ofdata_to_platdata(struct udevice *dev) |
| 217 | { |
| 218 | struct ti_musb_platdata *platdata = dev_get_platdata(dev); |
| 219 | const void *fdt = gd->fdt_blob; |
| 220 | int node = dev_of_offset(dev); |
| 221 | int ret; |
| 222 | |
| 223 | ret = ti_musb_ofdata_to_platdata(dev); |
| 224 | if (ret) { |
| 225 | pr_err("platdata dt parse error\n"); |
| 226 | return ret; |
| 227 | } |
| 228 | platdata->plat.mode = MUSB_PERIPHERAL; |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | int dm_usb_gadget_handle_interrupts(struct udevice *dev) |
| 235 | { |
| 236 | struct ti_musb_peripheral *priv = dev_get_priv(dev); |
| 237 | |
| 238 | priv->periph->isr(0, priv->periph); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int ti_musb_peripheral_probe(struct udevice *dev) |
| 244 | { |
| 245 | struct ti_musb_peripheral *priv = dev_get_priv(dev); |
| 246 | struct ti_musb_platdata *platdata = dev_get_platdata(dev); |
| 247 | int ret; |
| 248 | |
| 249 | priv->periph = musb_init_controller(&platdata->plat, |
| 250 | NULL, |
| 251 | platdata->base); |
| 252 | if (!priv->periph) |
| 253 | return -EIO; |
| 254 | |
| 255 | ti_musb_set_phy_power(dev, 1); |
| 256 | musb_gadget_setup(priv->periph); |
| 257 | return usb_add_gadget_udc((struct device *)dev, &priv->periph->g); |
| 258 | } |
| 259 | |
| 260 | static int ti_musb_peripheral_remove(struct udevice *dev) |
| 261 | { |
| 262 | struct ti_musb_peripheral *priv = dev_get_priv(dev); |
| 263 | |
| 264 | usb_del_gadget_udc(&priv->periph->g); |
| 265 | ti_musb_set_phy_power(dev, 0); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | U_BOOT_DRIVER(ti_musb_peripheral) = { |
| 271 | .name = "ti-musb-peripheral", |
| 272 | .id = UCLASS_USB_GADGET_GENERIC, |
| 273 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 274 | .ofdata_to_platdata = ti_musb_peripheral_ofdata_to_platdata, |
| 275 | #endif |
| 276 | .probe = ti_musb_peripheral_probe, |
| 277 | .remove = ti_musb_peripheral_remove, |
| 278 | .ops = &musb_usb_ops, |
| 279 | .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata), |
| 280 | .priv_auto_alloc_size = sizeof(struct ti_musb_peripheral), |
| 281 | .flags = DM_FLAG_PRE_RELOC, |
| 282 | }; |
| 283 | #endif |
Mugunthan V N | 2d6f8c0 | 2016-11-17 14:38:11 +0530 | [diff] [blame] | 284 | |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 285 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 286 | static int ti_musb_wrapper_bind(struct udevice *parent) |
| 287 | { |
| 288 | const void *fdt = gd->fdt_blob; |
| 289 | int node; |
| 290 | int ret; |
| 291 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 292 | for (node = fdt_first_subnode(fdt, dev_of_offset(parent)); node > 0; |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 293 | node = fdt_next_subnode(fdt, node)) { |
| 294 | struct udevice *dev; |
| 295 | const char *name = fdt_get_name(fdt, node, NULL); |
| 296 | enum usb_dr_mode dr_mode; |
| 297 | struct driver *drv; |
| 298 | |
| 299 | if (strncmp(name, "usb@", 4)) |
| 300 | continue; |
| 301 | |
| 302 | dr_mode = usb_get_dr_mode(node); |
| 303 | switch (dr_mode) { |
| 304 | case USB_DR_MODE_PERIPHERAL: |
| 305 | /* Bind MUSB device */ |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 306 | ret = device_bind_driver_to_node(parent, |
| 307 | "ti-musb-peripheral", |
| 308 | name, |
| 309 | offset_to_ofnode(node), |
| 310 | &dev); |
| 311 | if (ret) |
| 312 | pr_err("musb - not able to bind usb peripheral node\n"); |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 313 | break; |
| 314 | case USB_DR_MODE_HOST: |
| 315 | /* Bind MUSB host */ |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 316 | ret = device_bind_driver_to_node(parent, |
| 317 | "ti-musb-host", |
| 318 | name, |
| 319 | offset_to_ofnode(node), |
| 320 | &dev); |
| 321 | if (ret) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 322 | pr_err("musb - not able to bind usb host node\n"); |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 323 | break; |
| 324 | default: |
| 325 | break; |
| 326 | }; |
| 327 | } |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static const struct udevice_id ti_musb_ids[] = { |
| 332 | { .compatible = "ti,am33xx-usb" }, |
| 333 | { } |
| 334 | }; |
| 335 | |
| 336 | U_BOOT_DRIVER(ti_musb_wrapper) = { |
| 337 | .name = "ti-musb-wrapper", |
| 338 | .id = UCLASS_MISC, |
| 339 | .of_match = ti_musb_ids, |
| 340 | .bind = ti_musb_wrapper_bind, |
| 341 | }; |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 342 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
Mugunthan V N | 5b5479c | 2016-11-17 14:38:08 +0530 | [diff] [blame] | 343 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 344 | #endif /* CONFIG_IS_ENABLED(DM_USB) */ |