blob: ba600d0110239164ca7c287ba6347e20d9ae2931 [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 */
Adam Ford0b957cd2018-07-31 05:58:01 -050011#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassbd7a59a2019-11-14 12:57:23 -070013#include <serial.h>
Adam Ford0b957cd2018-07-31 05:58:01 -050014#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Adam Ford0b957cd2018-07-31 05:58:01 -050016#include <dm/lists.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060018#include <linux/printk.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>
24#include "linux-compat.h"
Ilya Yanoka3292f22012-11-06 13:48:29 +000025#include "musb_core.h"
26#include "omap2430.h"
Adam Ford0b957cd2018-07-31 05:58:01 -050027#include "musb_uboot.h"
Ilya Yanoka3292f22012-11-06 13:48:29 +000028
Ilya Yanoka3292f22012-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 Yanoka3292f22012-11-06 13:48:29 +000048static int omap2430_musb_init(struct musb *musb)
49{
50 u32 l;
51 int status = 0;
Paul Kocialkowski68dbdef2015-01-19 18:33:43 +010052 unsigned long int start;
Adam Ford0b957cd2018-07-31 05:58:01 -050053
Ilya Yanoka3292f22012-11-06 13:48:29 +000054 struct omap_musb_board_data *data =
55 (struct omap_musb_board_data *)musb->controller;
Ilya Yanoka3292f22012-11-06 13:48:29 +000056
Paul Kocialkowski68dbdef2015-01-19 18:33:43 +010057 /* Reset the controller */
58 musb_writel(musb->mregs, OTG_SYSCONFIG, SOFTRST);
59
60 start = get_timer(0);
61
62 while (1) {
63 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
64 if ((l & SOFTRST) == 0)
65 break;
66
67 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
68 dev_err(musb->controller, "MUSB reset is taking too long\n");
69 return -ENODEV;
70 }
71 }
Ilya Yanoka3292f22012-11-06 13:48:29 +000072
73 l = musb_readl(musb->mregs, OTG_INTERFSEL);
74
75 if (data->interface_type == MUSB_INTERFACE_UTMI) {
76 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
77 l &= ~ULPI_12PIN; /* Disable ULPI */
78 l |= UTMI_8BIT; /* Enable UTMI */
79 } else {
80 l |= ULPI_12PIN;
81 }
82
83 musb_writel(musb->mregs, OTG_INTERFSEL, l);
84
85 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
86 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
87 musb_readl(musb->mregs, OTG_REVISION),
88 musb_readl(musb->mregs, OTG_SYSCONFIG),
89 musb_readl(musb->mregs, OTG_SYSSTATUS),
90 musb_readl(musb->mregs, OTG_INTERFSEL),
91 musb_readl(musb->mregs, OTG_SIMENABLE));
Ilya Yanoka3292f22012-11-06 13:48:29 +000092 return 0;
93
94err1:
95 return status;
96}
97
Hans de Goede81c49982015-06-17 21:33:54 +020098static int omap2430_musb_enable(struct musb *musb)
Ilya Yanoka3292f22012-11-06 13:48:29 +000099{
Ilya Yanoka3292f22012-11-06 13:48:29 +0000100#ifdef CONFIG_TWL4030_USB
101 if (twl4030_usb_ulpi_init()) {
102 serial_printf("ERROR: %s Could not initialize PHY\n",
103 __PRETTY_FUNCTION__);
104 }
105#endif
Hans de Goede81c49982015-06-17 21:33:54 +0200106 return 0;
Ilya Yanoka3292f22012-11-06 13:48:29 +0000107}
108
109static void omap2430_musb_disable(struct musb *musb)
110{
Ilya Yanoka3292f22012-11-06 13:48:29 +0000111
Ilya Yanoka3292f22012-11-06 13:48:29 +0000112}
113
114static int omap2430_musb_exit(struct musb *musb)
115{
116 del_timer_sync(&musb_idle_timer);
117
118 omap2430_low_level_exit(musb);
119
120 return 0;
121}
122
Ilya Yanoka3292f22012-11-06 13:48:29 +0000123const struct musb_platform_ops omap2430_ops = {
Ilya Yanoka3292f22012-11-06 13:48:29 +0000124 .init = omap2430_musb_init,
125 .exit = omap2430_musb_exit,
Ilya Yanoka3292f22012-11-06 13:48:29 +0000126 .enable = omap2430_musb_enable,
127 .disable = omap2430_musb_disable,
128};
Adam Ford0b957cd2018-07-31 05:58:01 -0500129
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100130#if CONFIG_IS_ENABLED(DM_USB)
Adam Ford0b957cd2018-07-31 05:58:01 -0500131
Simon Glassb75b15b2020-12-03 16:55:23 -0700132struct omap2430_musb_plat {
Adam Ford0b957cd2018-07-31 05:58:01 -0500133 void *base;
134 void *ctrl_mod_base;
135 struct musb_hdrc_platform_data plat;
136 struct musb_hdrc_config musb_config;
137 struct omap_musb_board_data otg_board_data;
138};
139
Simon Glassaad29ae2020-12-03 16:55:21 -0700140static int omap2430_musb_of_to_plat(struct udevice *dev)
Adam Ford0b957cd2018-07-31 05:58:01 -0500141{
Simon Glassb75b15b2020-12-03 16:55:23 -0700142 struct omap2430_musb_plat *plat = dev_get_plat(dev);
Adam Ford0b957cd2018-07-31 05:58:01 -0500143 const void *fdt = gd->fdt_blob;
144 int node = dev_of_offset(dev);
145
Simon Glass71fa5b42020-12-03 16:55:18 -0700146 plat->base = (void *)dev_read_addr_ptr(dev);
Adam Ford0b957cd2018-07-31 05:58:01 -0500147
Simon Glass71fa5b42020-12-03 16:55:18 -0700148 plat->musb_config.multipoint = fdtdec_get_int(fdt, node, "multipoint",
149 -1);
150 if (plat->musb_config.multipoint < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500151 pr_err("MUSB multipoint DT entry missing\n");
152 return -ENOENT;
153 }
154
Simon Glass71fa5b42020-12-03 16:55:18 -0700155 plat->musb_config.dyn_fifo = 1;
156 plat->musb_config.num_eps = fdtdec_get_int(fdt, node, "num-eps", -1);
157 if (plat->musb_config.num_eps < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500158 pr_err("MUSB num-eps DT entry missing\n");
159 return -ENOENT;
160 }
161
Simon Glass71fa5b42020-12-03 16:55:18 -0700162 plat->musb_config.ram_bits = fdtdec_get_int(fdt, node, "ram-bits", -1);
163 if (plat->musb_config.ram_bits < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500164 pr_err("MUSB ram-bits DT entry missing\n");
165 return -ENOENT;
166 }
167
Simon Glass71fa5b42020-12-03 16:55:18 -0700168 plat->plat.power = fdtdec_get_int(fdt, node, "power", -1);
169 if (plat->plat.power < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500170 pr_err("MUSB power DT entry missing\n");
171 return -ENOENT;
172 }
173
Simon Glass71fa5b42020-12-03 16:55:18 -0700174 plat->otg_board_data.interface_type = fdtdec_get_int(fdt, node,
175 "interface-type",
176 -1);
177 if (plat->otg_board_data.interface_type < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500178 pr_err("MUSB interface-type DT entry missing\n");
179 return -ENOENT;
180 }
181
182#if 0 /* In a perfect world, mode would be set to OTG, mode 3 from DT */
Simon Glass71fa5b42020-12-03 16:55:18 -0700183 plat->plat.mode = fdtdec_get_int(fdt, node, "mode", -1);
184 if (plat->plat.mode < 0) {
Adam Ford0b957cd2018-07-31 05:58:01 -0500185 pr_err("MUSB mode DT entry missing\n");
186 return -ENOENT;
187 }
188#else /* MUSB_OTG, it doesn't work */
189#ifdef CONFIG_USB_MUSB_HOST /* Host seems to be the only option that works */
Simon Glass71fa5b42020-12-03 16:55:18 -0700190 plat->plat.mode = MUSB_HOST;
Adam Ford0b957cd2018-07-31 05:58:01 -0500191#else /* For that matter, MUSB_PERIPHERAL doesn't either */
Simon Glass71fa5b42020-12-03 16:55:18 -0700192 plat->plat.mode = MUSB_PERIPHERAL;
Adam Ford0b957cd2018-07-31 05:58:01 -0500193#endif
194#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700195 plat->otg_board_data.dev = dev;
196 plat->plat.config = &plat->musb_config;
197 plat->plat.platform_ops = &omap2430_ops;
198 plat->plat.board_data = &plat->otg_board_data;
Adam Ford0b957cd2018-07-31 05:58:01 -0500199 return 0;
200}
201
202static int omap2430_musb_probe(struct udevice *dev)
203{
Simon Glassb75b15b2020-12-03 16:55:23 -0700204 struct omap2430_musb_plat *plat = dev_get_plat(dev);
Adam Ford0b957cd2018-07-31 05:58:01 -0500205 struct omap_musb_board_data *otg_board_data;
Derald D. Woods3f12cf12019-05-27 21:22:00 -0500206 int ret = 0;
Adam Ford0b957cd2018-07-31 05:58:01 -0500207 void *base = dev_read_addr_ptr(dev);
Andreas Kemnade24ca8092023-01-09 08:13:30 +0100208 struct musb *musbp;
Adam Ford0b957cd2018-07-31 05:58:01 -0500209
Simon Glass71fa5b42020-12-03 16:55:18 -0700210 otg_board_data = &plat->otg_board_data;
Adam Ford0b957cd2018-07-31 05:58:01 -0500211
Simon Glass84858a62023-02-05 15:44:26 -0700212 if (IS_ENABLED(CONFIG_USB_MUSB_HOST)) {
Andreas Kemnade24ca8092023-01-09 08:13:30 +0100213 struct musb_host_data *host = dev_get_priv(dev);
214 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
215
216 priv->desc_before_addr = true;
217
218 host->host = musb_init_controller(&plat->plat,
219 (struct device *)otg_board_data,
220 plat->base);
221 if (!host->host)
222 return -EIO;
223
224 return musb_lowlevel_init(host);
Andreas Kemnade308d2652023-01-09 08:13:31 +0100225 } else if (CONFIG_IS_ENABLED(DM_USB_GADGET)) {
226 struct musb_host_data *host = dev_get_priv(dev);
227
228 host->host = musb_init_controller(&plat->plat,
229 (struct device *)otg_board_data,
230 plat->base);
231 if (!host->host)
232 return -EIO;
233
234 return usb_add_gadget_udc((struct device *)otg_board_data, &host->host->g);
Adam Ford0b957cd2018-07-31 05:58:01 -0500235 }
236
Simon Glass71fa5b42020-12-03 16:55:18 -0700237 musbp = musb_register(&plat->plat, (struct device *)otg_board_data,
238 plat->base);
Derald D. Woods3f12cf12019-05-27 21:22:00 -0500239 if (IS_ERR_OR_NULL(musbp))
240 return -EINVAL;
Andreas Kemnade24ca8092023-01-09 08:13:30 +0100241
242 return 0;
Adam Ford0b957cd2018-07-31 05:58:01 -0500243}
244
245static int omap2430_musb_remove(struct udevice *dev)
246{
247 struct musb_host_data *host = dev_get_priv(dev);
248
249 musb_stop(host->host);
250
251 return 0;
252}
253
Marek Vasutc7205372024-06-14 02:51:22 +0200254#ifndef CONFIG_USB_MUSB_HOST
255static int omap2340_gadget_handle_interrupts(struct udevice *dev)
256{
257 struct musb_host_data *host = dev_get_priv(dev);
258
259 host->host->isr(0, host->host);
260
261 return 0;
262}
263
264static const struct usb_gadget_generic_ops omap2340_gadget_ops = {
265 .handle_interrupts = omap2340_gadget_handle_interrupts,
266};
267#endif
268
Adam Ford0b957cd2018-07-31 05:58:01 -0500269static const struct udevice_id omap2430_musb_ids[] = {
270 { .compatible = "ti,omap3-musb" },
271 { .compatible = "ti,omap4-musb" },
272 { }
273};
274
275U_BOOT_DRIVER(omap2430_musb) = {
276 .name = "omap2430-musb",
277#ifdef CONFIG_USB_MUSB_HOST
278 .id = UCLASS_USB,
279#else
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100280 .id = UCLASS_USB_GADGET_GENERIC,
Marek Vasutc7205372024-06-14 02:51:22 +0200281 .ops = &omap2340_gadget_ops,
Adam Ford0b957cd2018-07-31 05:58:01 -0500282#endif
283 .of_match = omap2430_musb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700284 .of_to_plat = omap2430_musb_of_to_plat,
Adam Ford0b957cd2018-07-31 05:58:01 -0500285 .probe = omap2430_musb_probe,
286 .remove = omap2430_musb_remove,
287#ifdef CONFIG_USB_MUSB_HOST
288 .ops = &musb_usb_ops,
289#endif
Simon Glassb75b15b2020-12-03 16:55:23 -0700290 .plat_auto = sizeof(struct omap2430_musb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700291 .priv_auto = sizeof(struct musb_host_data),
Adam Ford0b957cd2018-07-31 05:58:01 -0500292};
293
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100294#endif /* CONFIG_IS_ENABLED(DM_USB) */