blob: 608facefa3de72584aa9013e80d1542248010df3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mugunthan V N5b5479c2016-11-17 14:38:08 +05302/*
3 * MISC driver for TI MUSB Glue.
4 *
5 * (C) Copyright 2016
6 * Texas Instruments Incorporated, <www.ti.com>
Mugunthan V N5b5479c2016-11-17 14:38:08 +05307 */
8#include <common.h>
9#include <command.h>
10#include <console.h>
11#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Mugunthan V N5b5479c2016-11-17 14:38:08 +053013#include <linux/usb/otg.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
16
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053017#include <asm/io.h>
18#include <asm/omap_musb.h>
19#include "musb_uboot.h"
20
Mugunthan V N5b5479c2016-11-17 14:38:08 +053021DECLARE_GLOBAL_DATA_PTR;
22
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010023#if CONFIG_IS_ENABLED(DM_USB)
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053024/* USB 2.0 PHY Control */
25#define CM_PHY_PWRDN (1 << 0)
26#define CM_PHY_OTG_PWRDN (1 << 1)
27#define OTGVDET_EN (1 << 19)
28#define OTGSESSENDEN (1 << 20)
29
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +010030#define AM335X_USB0_CTRL 0x0
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053031#define AM335X_USB1_CTRL 0x8
32
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +010033static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
34{
35 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
36
37 if (!platdata->ctrl_mod_base)
38 return;
39
40 if (on) {
41 clrsetbits_le32(platdata->ctrl_mod_base,
42 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
43 OTGVDET_EN | OTGSESSENDEN);
44 } else {
45 clrsetbits_le32(platdata->ctrl_mod_base, 0,
46 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
47 }
48}
49
50#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053051
52static int ti_musb_get_usb_index(int node)
53{
54 const void *fdt = gd->fdt_blob;
55 int i = 0;
56 char path[64];
57 const char *alias_path;
58 char alias[16];
59
60 fdt_get_path(fdt, node, path, sizeof(path));
61
62 do {
63 snprintf(alias, sizeof(alias), "usb%d", i);
64 alias_path = fdt_get_alias(fdt, alias);
65 if (alias_path == NULL) {
66 debug("USB index not found\n");
67 return -ENOENT;
68 }
69
70 if (!strcmp(path, alias_path))
71 return i;
72
73 i++;
74 } while (alias_path);
75
76 return -ENOENT;
77}
78
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053079static int ti_musb_ofdata_to_platdata(struct udevice *dev)
80{
81 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
82 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070083 int node = dev_of_offset(dev);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053084 int phys;
85 int ctrl_mod;
86 int usb_index;
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +010087 struct musb_hdrc_config *musb_config;
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053088
Simon Glassba1dea42017-05-17 17:18:05 -060089 platdata->base = (void *)devfdt_get_addr_index(dev, 1);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053090
91 phys = fdtdec_lookup_phandle(fdt, node, "phys");
92 ctrl_mod = fdtdec_lookup_phandle(fdt, phys, "ti,ctrl_mod");
93 platdata->ctrl_mod_base = (void *)fdtdec_get_addr(fdt, ctrl_mod, "reg");
94 usb_index = ti_musb_get_usb_index(node);
95 switch (usb_index) {
96 case 1:
97 platdata->ctrl_mod_base += AM335X_USB1_CTRL;
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +010098 break;
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053099 case 0:
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100100 platdata->ctrl_mod_base += AM335X_USB0_CTRL;
101 break;
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530102 default:
103 break;
104 }
105
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100106 musb_config = malloc(sizeof(struct musb_hdrc_config));
107 memset(musb_config, 0, sizeof(struct musb_hdrc_config));
108
109 musb_config->multipoint = fdtdec_get_int(fdt, node,
110 "mentor,multipoint", -1);
111 if (musb_config->multipoint < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900112 pr_err("MUSB multipoint DT entry missing\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530113 return -ENOENT;
114 }
115
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100116 musb_config->dyn_fifo = 1;
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530117
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100118 musb_config->num_eps = fdtdec_get_int(fdt, node, "mentor,num-eps",
119 -1);
120 if (musb_config->num_eps < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900121 pr_err("MUSB num-eps DT entry missing\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530122 return -ENOENT;
123 }
124
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100125 musb_config->ram_bits = fdtdec_get_int(fdt, node, "mentor,ram-bits",
126 -1);
127 if (musb_config->ram_bits < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900128 pr_err("MUSB ram-bits DT entry missing\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530129 return -ENOENT;
130 }
131
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100132 platdata->plat.config = musb_config;
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530133
134 platdata->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
135 if (platdata->plat.power < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900136 pr_err("MUSB mentor,power DT entry missing\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530137 return -ENOENT;
138 }
139
140 platdata->plat.platform_ops = &musb_dsps_ops;
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530141
142 return 0;
143}
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100144#endif
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530145
146static int ti_musb_host_probe(struct udevice *dev)
147{
148 struct musb_host_data *host = dev_get_priv(dev);
149 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
150 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530151 int ret;
152
153 priv->desc_before_addr = true;
154
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530155 host->host = musb_init_controller(&platdata->plat,
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100156 NULL,
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530157 platdata->base);
158 if (!host->host)
159 return -EIO;
160
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100161 ti_musb_set_phy_power(dev, 1);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530162 ret = musb_lowlevel_init(host);
163
164 return ret;
165}
166
167static int ti_musb_host_remove(struct udevice *dev)
168{
169 struct musb_host_data *host = dev_get_priv(dev);
170
171 musb_stop(host->host);
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100172 ti_musb_set_phy_power(dev, 0);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530173
174 return 0;
175}
176
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100177#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530178static int ti_musb_host_ofdata_to_platdata(struct udevice *dev)
179{
180 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
181 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700182 int node = dev_of_offset(dev);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530183 int ret;
184
185 ret = ti_musb_ofdata_to_platdata(dev);
186 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900187 pr_err("platdata dt parse error\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530188 return ret;
189 }
190
191 platdata->plat.mode = MUSB_HOST;
192
193 return 0;
194}
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100195#endif
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530196
197U_BOOT_DRIVER(ti_musb_host) = {
198 .name = "ti-musb-host",
199 .id = UCLASS_USB,
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100200#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530201 .ofdata_to_platdata = ti_musb_host_ofdata_to_platdata,
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100202#endif
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530203 .probe = ti_musb_host_probe,
204 .remove = ti_musb_host_remove,
205 .ops = &musb_usb_ops,
206 .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata),
207 .priv_auto_alloc_size = sizeof(struct musb_host_data),
208};
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100209
210#if CONFIG_IS_ENABLED(DM_USB_GADGET)
211struct ti_musb_peripheral {
212 struct musb *periph;
213};
214
215#if CONFIG_IS_ENABLED(OF_CONTROL)
216static int ti_musb_peripheral_ofdata_to_platdata(struct udevice *dev)
217{
218 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
219 const void *fdt = gd->fdt_blob;
220 int node = dev_of_offset(dev);
221 int ret;
222
223 ret = ti_musb_ofdata_to_platdata(dev);
224 if (ret) {
225 pr_err("platdata dt parse error\n");
226 return ret;
227 }
228 platdata->plat.mode = MUSB_PERIPHERAL;
229
230 return 0;
231}
232#endif
233
234int dm_usb_gadget_handle_interrupts(struct udevice *dev)
235{
236 struct ti_musb_peripheral *priv = dev_get_priv(dev);
237
238 priv->periph->isr(0, priv->periph);
239
240 return 0;
241}
242
243static int ti_musb_peripheral_probe(struct udevice *dev)
244{
245 struct ti_musb_peripheral *priv = dev_get_priv(dev);
246 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
247 int ret;
248
249 priv->periph = musb_init_controller(&platdata->plat,
250 NULL,
251 platdata->base);
252 if (!priv->periph)
253 return -EIO;
254
255 ti_musb_set_phy_power(dev, 1);
256 musb_gadget_setup(priv->periph);
257 return usb_add_gadget_udc((struct device *)dev, &priv->periph->g);
258}
259
260static int ti_musb_peripheral_remove(struct udevice *dev)
261{
262 struct ti_musb_peripheral *priv = dev_get_priv(dev);
263
264 usb_del_gadget_udc(&priv->periph->g);
265 ti_musb_set_phy_power(dev, 0);
266
267 return 0;
268}
269
270U_BOOT_DRIVER(ti_musb_peripheral) = {
271 .name = "ti-musb-peripheral",
272 .id = UCLASS_USB_GADGET_GENERIC,
273#if CONFIG_IS_ENABLED(OF_CONTROL)
274 .ofdata_to_platdata = ti_musb_peripheral_ofdata_to_platdata,
275#endif
276 .probe = ti_musb_peripheral_probe,
277 .remove = ti_musb_peripheral_remove,
278 .ops = &musb_usb_ops,
279 .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata),
280 .priv_auto_alloc_size = sizeof(struct ti_musb_peripheral),
281 .flags = DM_FLAG_PRE_RELOC,
282};
283#endif
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530284
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100285#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530286static int ti_musb_wrapper_bind(struct udevice *parent)
287{
Kever Yang1b807052020-03-04 08:59:50 +0800288 ofnode node;
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530289 int ret;
290
Kever Yang1b807052020-03-04 08:59:50 +0800291 ofnode_for_each_subnode(node, parent->node) {
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530292 struct udevice *dev;
Kever Yang1b807052020-03-04 08:59:50 +0800293 const char *name = ofnode_get_name(node);
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530294 enum usb_dr_mode dr_mode;
295 struct driver *drv;
296
297 if (strncmp(name, "usb@", 4))
298 continue;
299
300 dr_mode = usb_get_dr_mode(node);
301 switch (dr_mode) {
302 case USB_DR_MODE_PERIPHERAL:
303 /* Bind MUSB device */
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100304 ret = device_bind_driver_to_node(parent,
305 "ti-musb-peripheral",
306 name,
Kever Yang1b807052020-03-04 08:59:50 +0800307 node,
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100308 &dev);
309 if (ret)
310 pr_err("musb - not able to bind usb peripheral node\n");
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530311 break;
312 case USB_DR_MODE_HOST:
313 /* Bind MUSB host */
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100314 ret = device_bind_driver_to_node(parent,
315 "ti-musb-host",
316 name,
Kever Yang1b807052020-03-04 08:59:50 +0800317 node,
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100318 &dev);
319 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900320 pr_err("musb - not able to bind usb host node\n");
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530321 break;
322 default:
323 break;
324 };
325 }
326 return 0;
327}
328
329static const struct udevice_id ti_musb_ids[] = {
330 { .compatible = "ti,am33xx-usb" },
331 { }
332};
333
334U_BOOT_DRIVER(ti_musb_wrapper) = {
335 .name = "ti-musb-wrapper",
336 .id = UCLASS_MISC,
337 .of_match = ti_musb_ids,
338 .bind = ti_musb_wrapper_bind,
339};
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100340#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530341
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100342#endif /* CONFIG_IS_ENABLED(DM_USB) */