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