dm: core: Create a new header file for 'compat' features
[oweals/u-boot.git] / drivers / usb / musb-new / ti-musb.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * MISC driver for TI MUSB Glue.
4  *
5  * (C) Copyright 2016
6  *     Texas Instruments Incorporated, <www.ti.com>
7  */
8 #include <common.h>
9 #include <command.h>
10 #include <console.h>
11 #include <dm.h>
12 #include <malloc.h>
13 #include <linux/usb/otg.h>
14 #include <dm/device-internal.h>
15 #include <dm/lists.h>
16
17 #include <asm/io.h>
18 #include <asm/omap_musb.h>
19 #include "musb_uboot.h"
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 #if CONFIG_IS_ENABLED(DM_USB)
24 /* 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_USB0_CTRL        0x0
31 #define AM335X_USB1_CTRL        0x8
32
33 static 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)
51
52 static 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
79 static 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;
83         int node = dev_of_offset(dev);
84         int phys;
85         int ctrl_mod;
86         int usb_index;
87         struct musb_hdrc_config *musb_config;
88
89         platdata->base = (void *)devfdt_get_addr_index(dev, 1);
90
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;
98                 break;
99         case 0:
100                 platdata->ctrl_mod_base += AM335X_USB0_CTRL;
101                 break;
102         default:
103                 break;
104         }
105
106         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) {
112                 pr_err("MUSB multipoint DT entry missing\n");
113                 return -ENOENT;
114         }
115
116         musb_config->dyn_fifo = 1;
117
118         musb_config->num_eps = fdtdec_get_int(fdt, node, "mentor,num-eps",
119                                               -1);
120         if (musb_config->num_eps < 0) {
121                 pr_err("MUSB num-eps DT entry missing\n");
122                 return -ENOENT;
123         }
124
125         musb_config->ram_bits = fdtdec_get_int(fdt, node, "mentor,ram-bits",
126                                                -1);
127         if (musb_config->ram_bits < 0) {
128                 pr_err("MUSB ram-bits DT entry missing\n");
129                 return -ENOENT;
130         }
131
132         platdata->plat.config = musb_config;
133
134         platdata->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
135         if (platdata->plat.power < 0) {
136                 pr_err("MUSB mentor,power DT entry missing\n");
137                 return -ENOENT;
138         }
139
140         platdata->plat.platform_ops = &musb_dsps_ops;
141
142         return 0;
143 }
144 #endif
145
146 static 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);
151         int ret;
152
153         priv->desc_before_addr = true;
154
155         host->host = musb_init_controller(&platdata->plat,
156                                           NULL,
157                                           platdata->base);
158         if (!host->host)
159                 return -EIO;
160
161         ti_musb_set_phy_power(dev, 1);
162         ret = musb_lowlevel_init(host);
163
164         return ret;
165 }
166
167 static 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         ti_musb_set_phy_power(dev, 0);
173
174         return 0;
175 }
176
177 #if CONFIG_IS_ENABLED(OF_CONTROL)
178 static 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;
182         int node = dev_of_offset(dev);
183         int ret;
184
185         ret = ti_musb_ofdata_to_platdata(dev);
186         if (ret) {
187                 pr_err("platdata dt parse error\n");
188                 return ret;
189         }
190
191         platdata->plat.mode = MUSB_HOST;
192
193         return 0;
194 }
195 #endif
196
197 U_BOOT_DRIVER(ti_musb_host) = {
198         .name   = "ti-musb-host",
199         .id     = UCLASS_USB,
200 #if CONFIG_IS_ENABLED(OF_CONTROL)
201         .ofdata_to_platdata = ti_musb_host_ofdata_to_platdata,
202 #endif
203         .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 };
209
210 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
211 struct ti_musb_peripheral {
212         struct musb *periph;
213 };
214
215 #if CONFIG_IS_ENABLED(OF_CONTROL)
216 static 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
234 int 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
243 static 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
260 static 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
270 U_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
284
285 #if CONFIG_IS_ENABLED(OF_CONTROL)
286 static int ti_musb_wrapper_bind(struct udevice *parent)
287 {
288         const void *fdt = gd->fdt_blob;
289         int node;
290         int ret;
291
292         for (node = fdt_first_subnode(fdt, dev_of_offset(parent)); node > 0;
293              node = fdt_next_subnode(fdt, node)) {
294                 struct udevice *dev;
295                 const char *name = fdt_get_name(fdt, node, NULL);
296                 enum usb_dr_mode dr_mode;
297                 struct driver *drv;
298
299                 if (strncmp(name, "usb@", 4))
300                         continue;
301
302                 dr_mode = usb_get_dr_mode(node);
303                 switch (dr_mode) {
304                 case USB_DR_MODE_PERIPHERAL:
305                         /* Bind MUSB device */
306                         ret = device_bind_driver_to_node(parent,
307                                                          "ti-musb-peripheral",
308                                                          name,
309                                                          offset_to_ofnode(node),
310                                                          &dev);
311                         if (ret)
312                                 pr_err("musb - not able to bind usb peripheral node\n");
313                         break;
314                 case USB_DR_MODE_HOST:
315                         /* Bind MUSB host */
316                         ret = device_bind_driver_to_node(parent,
317                                                          "ti-musb-host",
318                                                          name,
319                                                          offset_to_ofnode(node),
320                                                          &dev);
321                         if (ret)
322                                 pr_err("musb - not able to bind usb host node\n");
323                         break;
324                 default:
325                         break;
326                 };
327         }
328         return 0;
329 }
330
331 static const struct udevice_id ti_musb_ids[] = {
332         { .compatible = "ti,am33xx-usb" },
333         { }
334 };
335
336 U_BOOT_DRIVER(ti_musb_wrapper) = {
337         .name   = "ti-musb-wrapper",
338         .id     = UCLASS_MISC,
339         .of_match = ti_musb_ids,
340         .bind = ti_musb_wrapper_bind,
341 };
342 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
343
344 #endif /* CONFIG_IS_ENABLED(DM_USB) */