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