blob: 42e7abddbc2b5eca45676444eabc92a7749dd5fb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Ilya Yanoka3292f22012-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 Yanoka3292f22012-11-06 13:48:29 +000010 */
Ilya Yanoka3292f22012-11-06 13:48:29 +000011#include <common.h>
Adam Ford0b957cd2018-07-31 05:58:01 -050012#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassbd7a59a2019-11-14 12:57:23 -070014#include <serial.h>
Adam Ford0b957cd2018-07-31 05:58:01 -050015#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Adam Ford0b957cd2018-07-31 05:58:01 -050017#include <dm/lists.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
Adam Ford0b957cd2018-07-31 05:58:01 -050019#include <linux/usb/otg.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Paul Kocialkowskid5a43ba2016-02-27 19:19:05 +010021#include <asm/omap_common.h>
Ilya Yanoka3292f22012-11-06 13:48:29 +000022#include <asm/omap_musb.h>
23#include <twl4030.h>
Paul Kocialkowskid5a43ba2016-02-27 19:19:05 +010024#include <twl6030.h>
Ilya Yanoka3292f22012-11-06 13:48:29 +000025#include "linux-compat.h"
Ilya Yanoka3292f22012-11-06 13:48:29 +000026#include "musb_core.h"
27#include "omap2430.h"
Adam Ford0b957cd2018-07-31 05:58:01 -050028#include "musb_uboot.h"
Ilya Yanoka3292f22012-11-06 13:48:29 +000029
Ilya Yanoka3292f22012-11-06 13:48:29 +000030static inline void omap2430_low_level_exit(struct musb *musb)
31{
32 u32 l;
33
34 /* in any role */
35 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
36 l |= ENABLEFORCE; /* enable MSTANDBY */
37 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
38}
39
40static inline void omap2430_low_level_init(struct musb *musb)
41{
42 u32 l;
43
44 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
45 l &= ~ENABLEFORCE; /* disable MSTANDBY */
46 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
47}
48
Andreas Kemnade308d2652023-01-09 08:13:31 +010049#ifdef CONFIG_DM_USB_GADGET
50int dm_usb_gadget_handle_interrupts(struct udevice *dev)
51{
52 struct musb_host_data *host = dev_get_priv(dev);
53
54 host->host->isr(0, host->host);
55 return 0;
56}
57#endif
Ilya Yanoka3292f22012-11-06 13:48:29 +000058
Ilya Yanoka3292f22012-11-06 13:48:29 +000059static int omap2430_musb_init(struct musb *musb)
60{
61 u32 l;
62 int status = 0;
Paul Kocialkowski68dbdef2015-01-19 18:33:43 +010063 unsigned long int start;
Adam Ford0b957cd2018-07-31 05:58:01 -050064
Ilya Yanoka3292f22012-11-06 13:48:29 +000065 struct omap_musb_board_data *data =
66 (struct omap_musb_board_data *)musb->controller;
Ilya Yanoka3292f22012-11-06 13:48:29 +000067
Paul Kocialkowski68dbdef2015-01-19 18:33:43 +010068 /* Reset the controller */
69 musb_writel(musb->mregs, OTG_SYSCONFIG, SOFTRST);
70
71 start = get_timer(0);
72
73 while (1) {
74 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
75 if ((l & SOFTRST) == 0)
76 break;
77
78 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
79 dev_err(musb->controller, "MUSB reset is taking too long\n");
80 return -ENODEV;
81 }
82 }
Ilya Yanoka3292f22012-11-06 13:48:29 +000083
84 l = musb_readl(musb->mregs, OTG_INTERFSEL);
85
86 if (data->interface_type == MUSB_INTERFACE_UTMI) {
87 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
88 l &= ~ULPI_12PIN; /* Disable ULPI */
89 l |= UTMI_8BIT; /* Enable UTMI */
90 } else {
91 l |= ULPI_12PIN;
92 }
93
94 musb_writel(musb->mregs, OTG_INTERFSEL, l);
95
96 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
97 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
98 musb_readl(musb->mregs, OTG_REVISION),
99 musb_readl(musb->mregs, OTG_SYSCONFIG),
100 musb_readl(musb->mregs, OTG_SYSSTATUS),
101 musb_readl(musb->mregs, OTG_INTERFSEL),
102 musb_readl(musb->mregs, OTG_SIMENABLE));
Ilya Yanoka3292f22012-11-06 13:48:29 +0000103 return 0;
104
105err1:
106 return status;
107}
108
Hans de Goede81c49982015-06-17 21:33:54 +0200109static int omap2430_musb_enable(struct musb *musb)
Ilya Yanoka3292f22012-11-06 13:48:29 +0000110{
Ilya Yanoka3292f22012-11-06 13:48:29 +0000111#ifdef CONFIG_TWL4030_USB
112 if (twl4030_usb_ulpi_init()) {
113 serial_printf("ERROR: %s Could not initialize PHY\n",
114 __PRETTY_FUNCTION__);
115 }
116#endif
Paul Kocialkowskid5a43ba2016-02-27 19:19:05 +0100117
118#ifdef CONFIG_TWL6030_POWER
119 twl6030_usb_device_settings();
120#endif
121
Tom Rini8d4bfb82017-05-12 22:33:19 -0400122#ifdef CONFIG_OMAP44XX
Paul Kocialkowskid5a43ba2016-02-27 19:19:05 +0100123 u32 *usbotghs_control = (u32 *)((*ctrl)->control_usbotghs_ctrl);
124 *usbotghs_control = USBOTGHS_CONTROL_AVALID |
125 USBOTGHS_CONTROL_VBUSVALID | USBOTGHS_CONTROL_IDDIG;
126#endif
127
Hans de Goede81c49982015-06-17 21:33:54 +0200128 return 0;
Ilya Yanoka3292f22012-11-06 13:48:29 +0000129}
130
131static void omap2430_musb_disable(struct musb *musb)
132{
Ilya Yanoka3292f22012-11-06 13:48:29 +0000133
Ilya Yanoka3292f22012-11-06 13:48:29 +0000134}
135
136static int omap2430_musb_exit(struct musb *musb)
137{
138 del_timer_sync(&musb_idle_timer);
139
140 omap2430_low_level_exit(musb);
141
142 return 0;
143}
144
Ilya Yanoka3292f22012-11-06 13:48:29 +0000145const struct musb_platform_ops omap2430_ops = {
Ilya Yanoka3292f22012-11-06 13:48:29 +0000146 .init = omap2430_musb_init,
147 .exit = omap2430_musb_exit,
Ilya Yanoka3292f22012-11-06 13:48:29 +0000148 .enable = omap2430_musb_enable,
149 .disable = omap2430_musb_disable,
150};
Adam Ford0b957cd2018-07-31 05:58:01 -0500151
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100152#if CONFIG_IS_ENABLED(DM_USB)
Adam Ford0b957cd2018-07-31 05:58:01 -0500153
Simon Glassb75b15b2020-12-03 16:55:23 -0700154struct omap2430_musb_plat {
Adam Ford0b957cd2018-07-31 05:58:01 -0500155 void *base;
156 void *ctrl_mod_base;
157 struct musb_hdrc_platform_data plat;
158 struct musb_hdrc_config musb_config;
159 struct omap_musb_board_data otg_board_data;
160};
161
Simon Glassaad29ae2020-12-03 16:55:21 -0700162static int omap2430_musb_of_to_plat(struct udevice *dev)
Adam Ford0b957cd2018-07-31 05:58:01 -0500163{
Simon Glassb75b15b2020-12-03 16:55:23 -0700164 struct omap2430_musb_plat *plat = dev_get_plat(dev);
Adam Ford0b957cd2018-07-31 05:58:01 -0500165 const void *fdt = gd->fdt_blob;
166 int node = dev_of_offset(dev);
167
Simon Glass71fa5b42020-12-03 16:55:18 -0700168 plat->base = (void *)dev_read_addr_ptr(dev);
Adam Ford0b957cd2018-07-31 05:58:01 -0500169
Simon Glass71fa5b42020-12-03 16:55:18 -0700170 plat->musb_config.multipoint = fdtdec_get_int(fdt, node, "multipoint",
171 -1);
172 if (plat->musb_config.multipoint < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500173 pr_err("MUSB multipoint DT entry missing\n");
174 return -ENOENT;
175 }
176
Simon Glass71fa5b42020-12-03 16:55:18 -0700177 plat->musb_config.dyn_fifo = 1;
178 plat->musb_config.num_eps = fdtdec_get_int(fdt, node, "num-eps", -1);
179 if (plat->musb_config.num_eps < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500180 pr_err("MUSB num-eps DT entry missing\n");
181 return -ENOENT;
182 }
183
Simon Glass71fa5b42020-12-03 16:55:18 -0700184 plat->musb_config.ram_bits = fdtdec_get_int(fdt, node, "ram-bits", -1);
185 if (plat->musb_config.ram_bits < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500186 pr_err("MUSB ram-bits DT entry missing\n");
187 return -ENOENT;
188 }
189
Simon Glass71fa5b42020-12-03 16:55:18 -0700190 plat->plat.power = fdtdec_get_int(fdt, node, "power", -1);
191 if (plat->plat.power < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500192 pr_err("MUSB power DT entry missing\n");
193 return -ENOENT;
194 }
195
Simon Glass71fa5b42020-12-03 16:55:18 -0700196 plat->otg_board_data.interface_type = fdtdec_get_int(fdt, node,
197 "interface-type",
198 -1);
199 if (plat->otg_board_data.interface_type < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500200 pr_err("MUSB interface-type DT entry missing\n");
201 return -ENOENT;
202 }
203
204#if 0 /* In a perfect world, mode would be set to OTG, mode 3 from DT */
Simon Glass71fa5b42020-12-03 16:55:18 -0700205 plat->plat.mode = fdtdec_get_int(fdt, node, "mode", -1);
206 if (plat->plat.mode < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500207 pr_err("MUSB mode DT entry missing\n");
208 return -ENOENT;
209 }
210#else /* MUSB_OTG, it doesn't work */
211#ifdef CONFIG_USB_MUSB_HOST /* Host seems to be the only option that works */
Simon Glass71fa5b42020-12-03 16:55:18 -0700212 plat->plat.mode = MUSB_HOST;
Adam Ford0b957cd2018-07-31 05:58:01 -0500213#else /* For that matter, MUSB_PERIPHERAL doesn't either */
Simon Glass71fa5b42020-12-03 16:55:18 -0700214 plat->plat.mode = MUSB_PERIPHERAL;
Adam Ford0b957cd2018-07-31 05:58:01 -0500215#endif
216#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700217 plat->otg_board_data.dev = dev;
218 plat->plat.config = &plat->musb_config;
219 plat->plat.platform_ops = &omap2430_ops;
220 plat->plat.board_data = &plat->otg_board_data;
Adam Ford0b957cd2018-07-31 05:58:01 -0500221 return 0;
222}
223
224static int omap2430_musb_probe(struct udevice *dev)
225{
Simon Glassb75b15b2020-12-03 16:55:23 -0700226 struct omap2430_musb_plat *plat = dev_get_plat(dev);
Adam Ford0b957cd2018-07-31 05:58:01 -0500227 struct omap_musb_board_data *otg_board_data;
Derald D. Woods3f12cf12019-05-27 21:22:00 -0500228 int ret = 0;
Adam Ford0b957cd2018-07-31 05:58:01 -0500229 void *base = dev_read_addr_ptr(dev);
Andreas Kemnade24ca8092023-01-09 08:13:30 +0100230 struct musb *musbp;
Adam Ford0b957cd2018-07-31 05:58:01 -0500231
Simon Glass71fa5b42020-12-03 16:55:18 -0700232 otg_board_data = &plat->otg_board_data;
Adam Ford0b957cd2018-07-31 05:58:01 -0500233
Andreas Kemnade24ca8092023-01-09 08:13:30 +0100234 if (CONFIG_IS_ENABLED(USB_MUSB_HOST)) {
235 struct musb_host_data *host = dev_get_priv(dev);
236 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
237
238 priv->desc_before_addr = true;
239
240 host->host = musb_init_controller(&plat->plat,
241 (struct device *)otg_board_data,
242 plat->base);
243 if (!host->host)
244 return -EIO;
245
246 return musb_lowlevel_init(host);
Andreas Kemnade308d2652023-01-09 08:13:31 +0100247 } else if (CONFIG_IS_ENABLED(DM_USB_GADGET)) {
248 struct musb_host_data *host = dev_get_priv(dev);
249
250 host->host = musb_init_controller(&plat->plat,
251 (struct device *)otg_board_data,
252 plat->base);
253 if (!host->host)
254 return -EIO;
255
256 return usb_add_gadget_udc((struct device *)otg_board_data, &host->host->g);
Adam Ford0b957cd2018-07-31 05:58:01 -0500257 }
258
Simon Glass71fa5b42020-12-03 16:55:18 -0700259 musbp = musb_register(&plat->plat, (struct device *)otg_board_data,
260 plat->base);
Derald D. Woods3f12cf12019-05-27 21:22:00 -0500261 if (IS_ERR_OR_NULL(musbp))
262 return -EINVAL;
Andreas Kemnade24ca8092023-01-09 08:13:30 +0100263
264 return 0;
Adam Ford0b957cd2018-07-31 05:58:01 -0500265}
266
267static int omap2430_musb_remove(struct udevice *dev)
268{
269 struct musb_host_data *host = dev_get_priv(dev);
270
271 musb_stop(host->host);
272
273 return 0;
274}
275
276static const struct udevice_id omap2430_musb_ids[] = {
277 { .compatible = "ti,omap3-musb" },
278 { .compatible = "ti,omap4-musb" },
279 { }
280};
281
282U_BOOT_DRIVER(omap2430_musb) = {
283 .name = "omap2430-musb",
284#ifdef CONFIG_USB_MUSB_HOST
285 .id = UCLASS_USB,
286#else
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100287 .id = UCLASS_USB_GADGET_GENERIC,
Adam Ford0b957cd2018-07-31 05:58:01 -0500288#endif
289 .of_match = omap2430_musb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700290 .of_to_plat = omap2430_musb_of_to_plat,
Adam Ford0b957cd2018-07-31 05:58:01 -0500291 .probe = omap2430_musb_probe,
292 .remove = omap2430_musb_remove,
293#ifdef CONFIG_USB_MUSB_HOST
294 .ops = &musb_usb_ops,
295#endif
Simon Glassb75b15b2020-12-03 16:55:23 -0700296 .plat_auto = sizeof(struct omap2430_musb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700297 .priv_auto = sizeof(struct musb_host_data),
Adam Ford0b957cd2018-07-31 05:58:01 -0500298};
299
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100300#endif /* CONFIG_IS_ENABLED(DM_USB) */