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