Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2005-2007 by Texas Instruments |
| 4 | * Some code has been taken from tusb6010.c |
| 5 | * Copyrights for that are attributable to: |
| 6 | * Copyright (C) 2006 Nokia Corporation |
| 7 | * Tony Lindgren <tony@atomide.com> |
| 8 | * |
| 9 | * This file is part of the Inventra Controller Driver for Linux. |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 10 | */ |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 11 | #include <common.h> |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | f516fd9 | 2019-11-14 12:57:23 -0700 | [diff] [blame] | 13 | #include <serial.h> |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 16 | #include <dm/lists.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <linux/err.h> |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 18 | #include <linux/usb/otg.h> |
Paul Kocialkowski | 27754d1 | 2016-02-27 19:19:05 +0100 | [diff] [blame] | 19 | #include <asm/omap_common.h> |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 20 | #include <asm/omap_musb.h> |
| 21 | #include <twl4030.h> |
Paul Kocialkowski | 27754d1 | 2016-02-27 19:19:05 +0100 | [diff] [blame] | 22 | #include <twl6030.h> |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 23 | #include "linux-compat.h" |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 24 | #include "musb_core.h" |
| 25 | #include "omap2430.h" |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 26 | #include "musb_uboot.h" |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 27 | |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 28 | static inline void omap2430_low_level_exit(struct musb *musb) |
| 29 | { |
| 30 | u32 l; |
| 31 | |
| 32 | /* in any role */ |
| 33 | l = musb_readl(musb->mregs, OTG_FORCESTDBY); |
| 34 | l |= ENABLEFORCE; /* enable MSTANDBY */ |
| 35 | musb_writel(musb->mregs, OTG_FORCESTDBY, l); |
| 36 | } |
| 37 | |
| 38 | static inline void omap2430_low_level_init(struct musb *musb) |
| 39 | { |
| 40 | u32 l; |
| 41 | |
| 42 | l = musb_readl(musb->mregs, OTG_FORCESTDBY); |
| 43 | l &= ~ENABLEFORCE; /* disable MSTANDBY */ |
| 44 | musb_writel(musb->mregs, OTG_FORCESTDBY, l); |
| 45 | } |
| 46 | |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 47 | |
| 48 | static int omap2430_musb_init(struct musb *musb) |
| 49 | { |
| 50 | u32 l; |
| 51 | int status = 0; |
Paul Kocialkowski | 193d7d1 | 2015-01-19 18:33:43 +0100 | [diff] [blame] | 52 | unsigned long int start; |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 53 | |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 54 | struct omap_musb_board_data *data = |
| 55 | (struct omap_musb_board_data *)musb->controller; |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 56 | |
Paul Kocialkowski | 193d7d1 | 2015-01-19 18:33:43 +0100 | [diff] [blame] | 57 | /* Reset the controller */ |
| 58 | musb_writel(musb->mregs, OTG_SYSCONFIG, SOFTRST); |
| 59 | |
| 60 | start = get_timer(0); |
| 61 | |
| 62 | while (1) { |
| 63 | l = musb_readl(musb->mregs, OTG_SYSCONFIG); |
| 64 | if ((l & SOFTRST) == 0) |
| 65 | break; |
| 66 | |
| 67 | if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) { |
| 68 | dev_err(musb->controller, "MUSB reset is taking too long\n"); |
| 69 | return -ENODEV; |
| 70 | } |
| 71 | } |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 72 | |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 73 | l = musb_readl(musb->mregs, OTG_INTERFSEL); |
| 74 | |
| 75 | if (data->interface_type == MUSB_INTERFACE_UTMI) { |
| 76 | /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */ |
| 77 | l &= ~ULPI_12PIN; /* Disable ULPI */ |
| 78 | l |= UTMI_8BIT; /* Enable UTMI */ |
| 79 | } else { |
| 80 | l |= ULPI_12PIN; |
| 81 | } |
| 82 | |
| 83 | musb_writel(musb->mregs, OTG_INTERFSEL, l); |
| 84 | |
| 85 | pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, " |
| 86 | "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n", |
| 87 | musb_readl(musb->mregs, OTG_REVISION), |
| 88 | musb_readl(musb->mregs, OTG_SYSCONFIG), |
| 89 | musb_readl(musb->mregs, OTG_SYSSTATUS), |
| 90 | musb_readl(musb->mregs, OTG_INTERFSEL), |
| 91 | musb_readl(musb->mregs, OTG_SIMENABLE)); |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 92 | return 0; |
| 93 | |
| 94 | err1: |
| 95 | return status; |
| 96 | } |
| 97 | |
Hans de Goede | 1583723 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 98 | static int omap2430_musb_enable(struct musb *musb) |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 99 | { |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 100 | #ifdef CONFIG_TWL4030_USB |
| 101 | if (twl4030_usb_ulpi_init()) { |
| 102 | serial_printf("ERROR: %s Could not initialize PHY\n", |
| 103 | __PRETTY_FUNCTION__); |
| 104 | } |
| 105 | #endif |
Paul Kocialkowski | 27754d1 | 2016-02-27 19:19:05 +0100 | [diff] [blame] | 106 | |
| 107 | #ifdef CONFIG_TWL6030_POWER |
| 108 | twl6030_usb_device_settings(); |
| 109 | #endif |
| 110 | |
Tom Rini | 77777f7 | 2017-05-12 22:33:19 -0400 | [diff] [blame] | 111 | #ifdef CONFIG_OMAP44XX |
Paul Kocialkowski | 27754d1 | 2016-02-27 19:19:05 +0100 | [diff] [blame] | 112 | u32 *usbotghs_control = (u32 *)((*ctrl)->control_usbotghs_ctrl); |
| 113 | *usbotghs_control = USBOTGHS_CONTROL_AVALID | |
| 114 | USBOTGHS_CONTROL_VBUSVALID | USBOTGHS_CONTROL_IDDIG; |
| 115 | #endif |
| 116 | |
Hans de Goede | 1583723 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 117 | return 0; |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void omap2430_musb_disable(struct musb *musb) |
| 121 | { |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 122 | |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int omap2430_musb_exit(struct musb *musb) |
| 126 | { |
| 127 | del_timer_sync(&musb_idle_timer); |
| 128 | |
| 129 | omap2430_low_level_exit(musb); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 134 | const struct musb_platform_ops omap2430_ops = { |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 135 | .init = omap2430_musb_init, |
| 136 | .exit = omap2430_musb_exit, |
Ilya Yanok | 673a524 | 2012-11-06 13:48:29 +0000 | [diff] [blame] | 137 | .enable = omap2430_musb_enable, |
| 138 | .disable = omap2430_musb_disable, |
| 139 | }; |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 140 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 141 | #if CONFIG_IS_ENABLED(DM_USB) |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 142 | |
| 143 | struct omap2430_musb_platdata { |
| 144 | void *base; |
| 145 | void *ctrl_mod_base; |
| 146 | struct musb_hdrc_platform_data plat; |
| 147 | struct musb_hdrc_config musb_config; |
| 148 | struct omap_musb_board_data otg_board_data; |
| 149 | }; |
| 150 | |
| 151 | static int omap2430_musb_ofdata_to_platdata(struct udevice *dev) |
| 152 | { |
| 153 | struct omap2430_musb_platdata *platdata = dev_get_platdata(dev); |
| 154 | const void *fdt = gd->fdt_blob; |
| 155 | int node = dev_of_offset(dev); |
| 156 | |
| 157 | platdata->base = (void *)dev_read_addr_ptr(dev); |
| 158 | |
| 159 | platdata->musb_config.multipoint = fdtdec_get_int(fdt, node, |
| 160 | "multipoint", |
| 161 | -1); |
| 162 | if (platdata->musb_config.multipoint < 0) { |
| 163 | pr_err("MUSB multipoint DT entry missing\n"); |
| 164 | return -ENOENT; |
| 165 | } |
| 166 | |
| 167 | platdata->musb_config.dyn_fifo = 1; |
| 168 | platdata->musb_config.num_eps = fdtdec_get_int(fdt, node, |
| 169 | "num-eps", -1); |
| 170 | if (platdata->musb_config.num_eps < 0) { |
| 171 | pr_err("MUSB num-eps DT entry missing\n"); |
| 172 | return -ENOENT; |
| 173 | } |
| 174 | |
| 175 | platdata->musb_config.ram_bits = fdtdec_get_int(fdt, node, |
| 176 | "ram-bits", -1); |
| 177 | if (platdata->musb_config.ram_bits < 0) { |
| 178 | pr_err("MUSB ram-bits DT entry missing\n"); |
| 179 | return -ENOENT; |
| 180 | } |
| 181 | |
| 182 | platdata->plat.power = fdtdec_get_int(fdt, node, |
| 183 | "power", -1); |
| 184 | if (platdata->plat.power < 0) { |
| 185 | pr_err("MUSB power DT entry missing\n"); |
| 186 | return -ENOENT; |
| 187 | } |
| 188 | |
| 189 | platdata->otg_board_data.interface_type = fdtdec_get_int(fdt, node, |
| 190 | "interface-type", -1); |
| 191 | if (platdata->otg_board_data.interface_type < 0) { |
| 192 | pr_err("MUSB interface-type DT entry missing\n"); |
| 193 | return -ENOENT; |
| 194 | } |
| 195 | |
| 196 | #if 0 /* In a perfect world, mode would be set to OTG, mode 3 from DT */ |
| 197 | platdata->plat.mode = fdtdec_get_int(fdt, node, |
| 198 | "mode", -1); |
| 199 | if (platdata->plat.mode < 0) { |
| 200 | pr_err("MUSB mode DT entry missing\n"); |
| 201 | return -ENOENT; |
| 202 | } |
| 203 | #else /* MUSB_OTG, it doesn't work */ |
| 204 | #ifdef CONFIG_USB_MUSB_HOST /* Host seems to be the only option that works */ |
| 205 | platdata->plat.mode = MUSB_HOST; |
| 206 | #else /* For that matter, MUSB_PERIPHERAL doesn't either */ |
| 207 | platdata->plat.mode = MUSB_PERIPHERAL; |
| 208 | #endif |
| 209 | #endif |
| 210 | platdata->otg_board_data.dev = dev; |
| 211 | platdata->plat.config = &platdata->musb_config; |
| 212 | platdata->plat.platform_ops = &omap2430_ops; |
| 213 | platdata->plat.board_data = &platdata->otg_board_data; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int omap2430_musb_probe(struct udevice *dev) |
| 218 | { |
| 219 | #ifdef CONFIG_USB_MUSB_HOST |
| 220 | struct musb_host_data *host = dev_get_priv(dev); |
Derald D. Woods | 9adaa03 | 2019-05-27 21:22:00 -0500 | [diff] [blame] | 221 | #else |
| 222 | struct musb *musbp; |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 223 | #endif |
| 224 | struct omap2430_musb_platdata *platdata = dev_get_platdata(dev); |
| 225 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); |
| 226 | struct omap_musb_board_data *otg_board_data; |
Derald D. Woods | 9adaa03 | 2019-05-27 21:22:00 -0500 | [diff] [blame] | 227 | int ret = 0; |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 228 | void *base = dev_read_addr_ptr(dev); |
| 229 | |
| 230 | priv->desc_before_addr = true; |
| 231 | |
| 232 | otg_board_data = &platdata->otg_board_data; |
| 233 | |
| 234 | #ifdef CONFIG_USB_MUSB_HOST |
| 235 | host->host = musb_init_controller(&platdata->plat, |
| 236 | (struct device *)otg_board_data, |
| 237 | platdata->base); |
| 238 | if (!host->host) { |
| 239 | return -EIO; |
| 240 | } |
| 241 | |
| 242 | ret = musb_lowlevel_init(host); |
| 243 | #else |
Derald D. Woods | 9adaa03 | 2019-05-27 21:22:00 -0500 | [diff] [blame] | 244 | musbp = musb_register(&platdata->plat, |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 245 | (struct device *)otg_board_data, |
| 246 | platdata->base); |
Derald D. Woods | 9adaa03 | 2019-05-27 21:22:00 -0500 | [diff] [blame] | 247 | if (IS_ERR_OR_NULL(musbp)) |
| 248 | return -EINVAL; |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 249 | #endif |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static int omap2430_musb_remove(struct udevice *dev) |
| 254 | { |
| 255 | struct musb_host_data *host = dev_get_priv(dev); |
| 256 | |
| 257 | musb_stop(host->host); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static const struct udevice_id omap2430_musb_ids[] = { |
| 263 | { .compatible = "ti,omap3-musb" }, |
| 264 | { .compatible = "ti,omap4-musb" }, |
| 265 | { } |
| 266 | }; |
| 267 | |
| 268 | U_BOOT_DRIVER(omap2430_musb) = { |
| 269 | .name = "omap2430-musb", |
| 270 | #ifdef CONFIG_USB_MUSB_HOST |
| 271 | .id = UCLASS_USB, |
| 272 | #else |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 273 | .id = UCLASS_USB_GADGET_GENERIC, |
Adam Ford | 1a35526 | 2018-07-31 05:58:01 -0500 | [diff] [blame] | 274 | #endif |
| 275 | .of_match = omap2430_musb_ids, |
| 276 | .ofdata_to_platdata = omap2430_musb_ofdata_to_platdata, |
| 277 | .probe = omap2430_musb_probe, |
| 278 | .remove = omap2430_musb_remove, |
| 279 | #ifdef CONFIG_USB_MUSB_HOST |
| 280 | .ops = &musb_usb_ops, |
| 281 | #endif |
| 282 | .platdata_auto_alloc_size = sizeof(struct omap2430_musb_platdata), |
| 283 | .priv_auto_alloc_size = sizeof(struct musb_host_data), |
| 284 | }; |
| 285 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 286 | #endif /* CONFIG_IS_ENABLED(DM_USB) */ |