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