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