blob: 3fdcaeaa5d01a02728f538ebf4a19d2c279cd94d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Ilya Yanok673a5242012-11-06 13:48:29 +00002/*
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 Yanok673a5242012-11-06 13:48:29 +000010 */
Ilya Yanok673a5242012-11-06 13:48:29 +000011#include <common.h>
Adam Ford1a355262018-07-31 05:58:01 -050012#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glassf516fd92019-11-14 12:57:23 -070014#include <serial.h>
Adam Ford1a355262018-07-31 05:58:01 -050015#include <dm/device-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Adam Ford1a355262018-07-31 05:58:01 -050017#include <dm/lists.h>
Simon Glass61b29b82020-02-03 07:36:15 -070018#include <linux/err.h>
Adam Ford1a355262018-07-31 05:58:01 -050019#include <linux/usb/otg.h>
Paul Kocialkowski27754d12016-02-27 19:19:05 +010020#include <asm/omap_common.h>
Ilya Yanok673a5242012-11-06 13:48:29 +000021#include <asm/omap_musb.h>
22#include <twl4030.h>
Paul Kocialkowski27754d12016-02-27 19:19:05 +010023#include <twl6030.h>
Ilya Yanok673a5242012-11-06 13:48:29 +000024#include "linux-compat.h"
Ilya Yanok673a5242012-11-06 13:48:29 +000025#include "musb_core.h"
26#include "omap2430.h"
Adam Ford1a355262018-07-31 05:58:01 -050027#include "musb_uboot.h"
Ilya Yanok673a5242012-11-06 13:48:29 +000028
Ilya Yanok673a5242012-11-06 13:48:29 +000029static 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
39static 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 Yanok673a5242012-11-06 13:48:29 +000048
49static int omap2430_musb_init(struct musb *musb)
50{
51 u32 l;
52 int status = 0;
Paul Kocialkowski193d7d12015-01-19 18:33:43 +010053 unsigned long int start;
Adam Ford1a355262018-07-31 05:58:01 -050054
Ilya Yanok673a5242012-11-06 13:48:29 +000055 struct omap_musb_board_data *data =
56 (struct omap_musb_board_data *)musb->controller;
Ilya Yanok673a5242012-11-06 13:48:29 +000057
Paul Kocialkowski193d7d12015-01-19 18:33:43 +010058 /* 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 Yanok673a5242012-11-06 13:48:29 +000073
Ilya Yanok673a5242012-11-06 13:48:29 +000074 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 Yanok673a5242012-11-06 13:48:29 +000093 return 0;
94
95err1:
96 return status;
97}
98
Hans de Goede15837232015-06-17 21:33:54 +020099static int omap2430_musb_enable(struct musb *musb)
Ilya Yanok673a5242012-11-06 13:48:29 +0000100{
Ilya Yanok673a5242012-11-06 13:48:29 +0000101#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 Kocialkowski27754d12016-02-27 19:19:05 +0100107
108#ifdef CONFIG_TWL6030_POWER
109 twl6030_usb_device_settings();
110#endif
111
Tom Rini77777f72017-05-12 22:33:19 -0400112#ifdef CONFIG_OMAP44XX
Paul Kocialkowski27754d12016-02-27 19:19:05 +0100113 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 Goede15837232015-06-17 21:33:54 +0200118 return 0;
Ilya Yanok673a5242012-11-06 13:48:29 +0000119}
120
121static void omap2430_musb_disable(struct musb *musb)
122{
Ilya Yanok673a5242012-11-06 13:48:29 +0000123
Ilya Yanok673a5242012-11-06 13:48:29 +0000124}
125
126static 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 Yanok673a5242012-11-06 13:48:29 +0000135const struct musb_platform_ops omap2430_ops = {
Ilya Yanok673a5242012-11-06 13:48:29 +0000136 .init = omap2430_musb_init,
137 .exit = omap2430_musb_exit,
Ilya Yanok673a5242012-11-06 13:48:29 +0000138 .enable = omap2430_musb_enable,
139 .disable = omap2430_musb_disable,
140};
Adam Ford1a355262018-07-31 05:58:01 -0500141
Sven Schwermerfd09c202018-11-21 08:43:56 +0100142#if CONFIG_IS_ENABLED(DM_USB)
Adam Ford1a355262018-07-31 05:58:01 -0500143
Simon Glass8a8d24b2020-12-03 16:55:23 -0700144struct omap2430_musb_plat {
Adam Ford1a355262018-07-31 05:58:01 -0500145 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
Simon Glassd1998a92020-12-03 16:55:21 -0700152static int omap2430_musb_of_to_plat(struct udevice *dev)
Adam Ford1a355262018-07-31 05:58:01 -0500153{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700154 struct omap2430_musb_plat *plat = dev_get_plat(dev);
Adam Ford1a355262018-07-31 05:58:01 -0500155 const void *fdt = gd->fdt_blob;
156 int node = dev_of_offset(dev);
157
Simon Glasscaa4daa2020-12-03 16:55:18 -0700158 plat->base = (void *)dev_read_addr_ptr(dev);
Adam Ford1a355262018-07-31 05:58:01 -0500159
Simon Glasscaa4daa2020-12-03 16:55:18 -0700160 plat->musb_config.multipoint = fdtdec_get_int(fdt, node, "multipoint",
161 -1);
162 if (plat->musb_config.multipoint < 0) {
Adam Ford1a355262018-07-31 05:58:01 -0500163 pr_err("MUSB multipoint DT entry missing\n");
164 return -ENOENT;
165 }
166
Simon Glasscaa4daa2020-12-03 16:55:18 -0700167 plat->musb_config.dyn_fifo = 1;
168 plat->musb_config.num_eps = fdtdec_get_int(fdt, node, "num-eps", -1);
169 if (plat->musb_config.num_eps < 0) {
Adam Ford1a355262018-07-31 05:58:01 -0500170 pr_err("MUSB num-eps DT entry missing\n");
171 return -ENOENT;
172 }
173
Simon Glasscaa4daa2020-12-03 16:55:18 -0700174 plat->musb_config.ram_bits = fdtdec_get_int(fdt, node, "ram-bits", -1);
175 if (plat->musb_config.ram_bits < 0) {
Adam Ford1a355262018-07-31 05:58:01 -0500176 pr_err("MUSB ram-bits DT entry missing\n");
177 return -ENOENT;
178 }
179
Simon Glasscaa4daa2020-12-03 16:55:18 -0700180 plat->plat.power = fdtdec_get_int(fdt, node, "power", -1);
181 if (plat->plat.power < 0) {
Adam Ford1a355262018-07-31 05:58:01 -0500182 pr_err("MUSB power DT entry missing\n");
183 return -ENOENT;
184 }
185
Simon Glasscaa4daa2020-12-03 16:55:18 -0700186 plat->otg_board_data.interface_type = fdtdec_get_int(fdt, node,
187 "interface-type",
188 -1);
189 if (plat->otg_board_data.interface_type < 0) {
Adam Ford1a355262018-07-31 05:58:01 -0500190 pr_err("MUSB interface-type DT entry missing\n");
191 return -ENOENT;
192 }
193
194#if 0 /* In a perfect world, mode would be set to OTG, mode 3 from DT */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700195 plat->plat.mode = fdtdec_get_int(fdt, node, "mode", -1);
196 if (plat->plat.mode < 0) {
Adam Ford1a355262018-07-31 05:58:01 -0500197 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 */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700202 plat->plat.mode = MUSB_HOST;
Adam Ford1a355262018-07-31 05:58:01 -0500203#else /* For that matter, MUSB_PERIPHERAL doesn't either */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700204 plat->plat.mode = MUSB_PERIPHERAL;
Adam Ford1a355262018-07-31 05:58:01 -0500205#endif
206#endif
Simon Glasscaa4daa2020-12-03 16:55:18 -0700207 plat->otg_board_data.dev = dev;
208 plat->plat.config = &plat->musb_config;
209 plat->plat.platform_ops = &omap2430_ops;
210 plat->plat.board_data = &plat->otg_board_data;
Adam Ford1a355262018-07-31 05:58:01 -0500211 return 0;
212}
213
214static 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. Woods9adaa032019-05-27 21:22:00 -0500218#else
219 struct musb *musbp;
Adam Ford1a355262018-07-31 05:58:01 -0500220#endif
Simon Glass8a8d24b2020-12-03 16:55:23 -0700221 struct omap2430_musb_plat *plat = dev_get_plat(dev);
Adam Ford1a355262018-07-31 05:58:01 -0500222 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
223 struct omap_musb_board_data *otg_board_data;
Derald D. Woods9adaa032019-05-27 21:22:00 -0500224 int ret = 0;
Adam Ford1a355262018-07-31 05:58:01 -0500225 void *base = dev_read_addr_ptr(dev);
226
227 priv->desc_before_addr = true;
228
Simon Glasscaa4daa2020-12-03 16:55:18 -0700229 otg_board_data = &plat->otg_board_data;
Adam Ford1a355262018-07-31 05:58:01 -0500230
231#ifdef CONFIG_USB_MUSB_HOST
Simon Glasscaa4daa2020-12-03 16:55:18 -0700232 host->host = musb_init_controller(&plat->plat,
Adam Ford1a355262018-07-31 05:58:01 -0500233 (struct device *)otg_board_data,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700234 plat->base);
Adam Ford1a355262018-07-31 05:58:01 -0500235 if (!host->host) {
236 return -EIO;
237 }
238
239 ret = musb_lowlevel_init(host);
240#else
Simon Glasscaa4daa2020-12-03 16:55:18 -0700241 musbp = musb_register(&plat->plat, (struct device *)otg_board_data,
242 plat->base);
Derald D. Woods9adaa032019-05-27 21:22:00 -0500243 if (IS_ERR_OR_NULL(musbp))
244 return -EINVAL;
Adam Ford1a355262018-07-31 05:58:01 -0500245#endif
246 return ret;
247}
248
249static int omap2430_musb_remove(struct udevice *dev)
250{
251 struct musb_host_data *host = dev_get_priv(dev);
252
253 musb_stop(host->host);
254
255 return 0;
256}
257
258static const struct udevice_id omap2430_musb_ids[] = {
259 { .compatible = "ti,omap3-musb" },
260 { .compatible = "ti,omap4-musb" },
261 { }
262};
263
264U_BOOT_DRIVER(omap2430_musb) = {
265 .name = "omap2430-musb",
266#ifdef CONFIG_USB_MUSB_HOST
267 .id = UCLASS_USB,
268#else
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100269 .id = UCLASS_USB_GADGET_GENERIC,
Adam Ford1a355262018-07-31 05:58:01 -0500270#endif
271 .of_match = omap2430_musb_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700272 .of_to_plat = omap2430_musb_of_to_plat,
Adam Ford1a355262018-07-31 05:58:01 -0500273 .probe = omap2430_musb_probe,
274 .remove = omap2430_musb_remove,
275#ifdef CONFIG_USB_MUSB_HOST
276 .ops = &musb_usb_ops,
277#endif
Simon Glass8a8d24b2020-12-03 16:55:23 -0700278 .plat_auto = sizeof(struct omap2430_musb_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700279 .priv_auto = sizeof(struct musb_host_data),
Adam Ford1a355262018-07-31 05:58:01 -0500280};
281
Sven Schwermerfd09c202018-11-21 08:43:56 +0100282#endif /* CONFIG_IS_ENABLED(DM_USB) */