blob: 9fbe2d6861d38813a9b59dda828e702fc41369bf [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>
12#include <linux/usb/otg.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
15
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053016#include <asm/io.h>
17#include <asm/omap_musb.h>
18#include "musb_uboot.h"
19
Mugunthan V N5b5479c2016-11-17 14:38:08 +053020DECLARE_GLOBAL_DATA_PTR;
21
22#ifdef CONFIG_DM_USB
23
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
30#define AM335X_USB1_CTRL 0x8
31
32struct ti_musb_platdata {
33 void *base;
34 void *ctrl_mod_base;
35 struct musb_hdrc_platform_data plat;
36 struct musb_hdrc_config musb_config;
37 struct omap_musb_board_data otg_board_data;
38};
39
40static int ti_musb_get_usb_index(int node)
41{
42 const void *fdt = gd->fdt_blob;
43 int i = 0;
44 char path[64];
45 const char *alias_path;
46 char alias[16];
47
48 fdt_get_path(fdt, node, path, sizeof(path));
49
50 do {
51 snprintf(alias, sizeof(alias), "usb%d", i);
52 alias_path = fdt_get_alias(fdt, alias);
53 if (alias_path == NULL) {
54 debug("USB index not found\n");
55 return -ENOENT;
56 }
57
58 if (!strcmp(path, alias_path))
59 return i;
60
61 i++;
62 } while (alias_path);
63
64 return -ENOENT;
65}
66
67static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
68{
69 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
70
71 if (on) {
72 clrsetbits_le32(platdata->ctrl_mod_base,
73 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
74 OTGVDET_EN | OTGSESSENDEN);
75 } else {
76 clrsetbits_le32(platdata->ctrl_mod_base, 0,
77 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
78 }
79}
80
81static int ti_musb_ofdata_to_platdata(struct udevice *dev)
82{
83 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
84 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070085 int node = dev_of_offset(dev);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053086 int phys;
87 int ctrl_mod;
88 int usb_index;
89
Simon Glassba1dea42017-05-17 17:18:05 -060090 platdata->base = (void *)devfdt_get_addr_index(dev, 1);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +053091
92 phys = fdtdec_lookup_phandle(fdt, node, "phys");
93 ctrl_mod = fdtdec_lookup_phandle(fdt, phys, "ti,ctrl_mod");
94 platdata->ctrl_mod_base = (void *)fdtdec_get_addr(fdt, ctrl_mod, "reg");
95 usb_index = ti_musb_get_usb_index(node);
96 switch (usb_index) {
97 case 1:
98 platdata->ctrl_mod_base += AM335X_USB1_CTRL;
99 case 0:
100 default:
101 break;
102 }
103
104 platdata->musb_config.multipoint = fdtdec_get_int(fdt, node,
105 "mentor,multipoint",
106 -1);
107 if (platdata->musb_config.multipoint < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900108 pr_err("MUSB multipoint DT entry missing\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530109 return -ENOENT;
110 }
111
112 platdata->musb_config.dyn_fifo = 1;
113
114 platdata->musb_config.num_eps = fdtdec_get_int(fdt, node,
115 "mentor,num-eps", -1);
116 if (platdata->musb_config.num_eps < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900117 pr_err("MUSB num-eps DT entry missing\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530118 return -ENOENT;
119 }
120
121 platdata->musb_config.ram_bits = fdtdec_get_int(fdt, node,
122 "mentor,ram-bits", -1);
123 if (platdata->musb_config.ram_bits < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900124 pr_err("MUSB ram-bits DT entry missing\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530125 return -ENOENT;
126 }
127
128 platdata->otg_board_data.set_phy_power = ti_musb_set_phy_power;
129 platdata->otg_board_data.dev = dev;
130 platdata->plat.config = &platdata->musb_config;
131
132 platdata->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
133 if (platdata->plat.power < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900134 pr_err("MUSB mentor,power DT entry missing\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530135 return -ENOENT;
136 }
137
138 platdata->plat.platform_ops = &musb_dsps_ops;
139 platdata->plat.board_data = &platdata->otg_board_data;
140
141 return 0;
142}
143
144static int ti_musb_host_probe(struct udevice *dev)
145{
146 struct musb_host_data *host = dev_get_priv(dev);
147 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
148 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
149 struct omap_musb_board_data *otg_board_data;
150 int ret;
151
152 priv->desc_before_addr = true;
153
154 otg_board_data = &platdata->otg_board_data;
155
156 host->host = musb_init_controller(&platdata->plat,
157 (struct device *)otg_board_data,
158 platdata->base);
159 if (!host->host)
160 return -EIO;
161
162 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);
172
173 return 0;
174}
175
176static int ti_musb_host_ofdata_to_platdata(struct udevice *dev)
177{
178 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
179 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700180 int node = dev_of_offset(dev);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530181 int ret;
182
183 ret = ti_musb_ofdata_to_platdata(dev);
184 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900185 pr_err("platdata dt parse error\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530186 return ret;
187 }
188
189 platdata->plat.mode = MUSB_HOST;
190
191 return 0;
192}
193
194U_BOOT_DRIVER(ti_musb_host) = {
195 .name = "ti-musb-host",
196 .id = UCLASS_USB,
197 .ofdata_to_platdata = ti_musb_host_ofdata_to_platdata,
198 .probe = ti_musb_host_probe,
199 .remove = ti_musb_host_remove,
200 .ops = &musb_usb_ops,
201 .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata),
202 .priv_auto_alloc_size = sizeof(struct musb_host_data),
203};
204
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530205static int ti_musb_wrapper_bind(struct udevice *parent)
206{
207 const void *fdt = gd->fdt_blob;
208 int node;
209 int ret;
210
Simon Glassdd79d6e2017-01-17 16:52:55 -0700211 for (node = fdt_first_subnode(fdt, dev_of_offset(parent)); node > 0;
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530212 node = fdt_next_subnode(fdt, node)) {
213 struct udevice *dev;
214 const char *name = fdt_get_name(fdt, node, NULL);
215 enum usb_dr_mode dr_mode;
216 struct driver *drv;
217
218 if (strncmp(name, "usb@", 4))
219 continue;
220
221 dr_mode = usb_get_dr_mode(node);
222 switch (dr_mode) {
223 case USB_DR_MODE_PERIPHERAL:
224 /* Bind MUSB device */
225 break;
226 case USB_DR_MODE_HOST:
227 /* Bind MUSB host */
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530228 ret = device_bind_driver_to_node(parent, "ti-musb-host",
Simon Glasse6dd8da2017-05-18 20:09:07 -0600229 name, offset_to_ofnode(node), &dev);
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530230 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900231 pr_err("musb - not able to bind usb host node\n");
Mugunthan V N2d6f8c02016-11-17 14:38:11 +0530232 return ret;
233 }
Mugunthan V N5b5479c2016-11-17 14:38:08 +0530234 break;
235 default:
236 break;
237 };
238 }
239 return 0;
240}
241
242static const struct udevice_id ti_musb_ids[] = {
243 { .compatible = "ti,am33xx-usb" },
244 { }
245};
246
247U_BOOT_DRIVER(ti_musb_wrapper) = {
248 .name = "ti-musb-wrapper",
249 .id = UCLASS_MISC,
250 .of_match = ti_musb_ids,
251 .bind = ti_musb_wrapper_bind,
252};
253
254#endif /* CONFIG_DM_USB */