1 // SPDX-License-Identifier: GPL-2.0+
3 * MISC driver for TI MUSB Glue.
6 * Texas Instruments Incorporated, <www.ti.com>
12 #include <linux/usb/otg.h>
13 #include <dm/device-internal.h>
17 #include <asm/omap_musb.h>
18 #include "musb_uboot.h"
20 DECLARE_GLOBAL_DATA_PTR;
22 #if CONFIG_IS_ENABLED(DM_USB)
23 /* USB 2.0 PHY Control */
24 #define CM_PHY_PWRDN (1 << 0)
25 #define CM_PHY_OTG_PWRDN (1 << 1)
26 #define OTGVDET_EN (1 << 19)
27 #define OTGSESSENDEN (1 << 20)
29 #define AM335X_USB0_CTRL 0x0
30 #define AM335X_USB1_CTRL 0x8
32 static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
34 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
36 if (!platdata->ctrl_mod_base)
40 clrsetbits_le32(platdata->ctrl_mod_base,
41 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
42 OTGVDET_EN | OTGSESSENDEN);
44 clrsetbits_le32(platdata->ctrl_mod_base, 0,
45 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
49 #if CONFIG_IS_ENABLED(OF_CONTROL)
51 static int ti_musb_get_usb_index(int node)
53 const void *fdt = gd->fdt_blob;
56 const char *alias_path;
59 fdt_get_path(fdt, node, path, sizeof(path));
62 snprintf(alias, sizeof(alias), "usb%d", i);
63 alias_path = fdt_get_alias(fdt, alias);
64 if (alias_path == NULL) {
65 debug("USB index not found\n");
69 if (!strcmp(path, alias_path))
78 static int ti_musb_ofdata_to_platdata(struct udevice *dev)
80 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
81 const void *fdt = gd->fdt_blob;
82 int node = dev_of_offset(dev);
86 struct musb_hdrc_config *musb_config;
88 platdata->base = (void *)devfdt_get_addr_index(dev, 1);
90 phys = fdtdec_lookup_phandle(fdt, node, "phys");
91 ctrl_mod = fdtdec_lookup_phandle(fdt, phys, "ti,ctrl_mod");
92 platdata->ctrl_mod_base = (void *)fdtdec_get_addr(fdt, ctrl_mod, "reg");
93 usb_index = ti_musb_get_usb_index(node);
96 platdata->ctrl_mod_base += AM335X_USB1_CTRL;
99 platdata->ctrl_mod_base += AM335X_USB0_CTRL;
105 musb_config = malloc(sizeof(struct musb_hdrc_config));
106 memset(musb_config, 0, sizeof(struct musb_hdrc_config));
108 musb_config->multipoint = fdtdec_get_int(fdt, node,
109 "mentor,multipoint", -1);
110 if (musb_config->multipoint < 0) {
111 pr_err("MUSB multipoint DT entry missing\n");
115 musb_config->dyn_fifo = 1;
117 musb_config->num_eps = fdtdec_get_int(fdt, node, "mentor,num-eps",
119 if (musb_config->num_eps < 0) {
120 pr_err("MUSB num-eps DT entry missing\n");
124 musb_config->ram_bits = fdtdec_get_int(fdt, node, "mentor,ram-bits",
126 if (musb_config->ram_bits < 0) {
127 pr_err("MUSB ram-bits DT entry missing\n");
131 platdata->plat.config = musb_config;
133 platdata->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
134 if (platdata->plat.power < 0) {
135 pr_err("MUSB mentor,power DT entry missing\n");
139 platdata->plat.platform_ops = &musb_dsps_ops;
145 static int ti_musb_host_probe(struct udevice *dev)
147 struct musb_host_data *host = dev_get_priv(dev);
148 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
149 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
152 priv->desc_before_addr = true;
154 host->host = musb_init_controller(&platdata->plat,
160 ti_musb_set_phy_power(dev, 1);
161 ret = musb_lowlevel_init(host);
166 static int ti_musb_host_remove(struct udevice *dev)
168 struct musb_host_data *host = dev_get_priv(dev);
170 musb_stop(host->host);
171 ti_musb_set_phy_power(dev, 0);
176 #if CONFIG_IS_ENABLED(OF_CONTROL)
177 static int ti_musb_host_ofdata_to_platdata(struct udevice *dev)
179 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
180 const void *fdt = gd->fdt_blob;
181 int node = dev_of_offset(dev);
184 ret = ti_musb_ofdata_to_platdata(dev);
186 pr_err("platdata dt parse error\n");
190 platdata->plat.mode = MUSB_HOST;
196 U_BOOT_DRIVER(ti_musb_host) = {
197 .name = "ti-musb-host",
199 #if CONFIG_IS_ENABLED(OF_CONTROL)
200 .ofdata_to_platdata = ti_musb_host_ofdata_to_platdata,
202 .probe = ti_musb_host_probe,
203 .remove = ti_musb_host_remove,
204 .ops = &musb_usb_ops,
205 .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata),
206 .priv_auto_alloc_size = sizeof(struct musb_host_data),
209 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
210 struct ti_musb_peripheral {
214 #if CONFIG_IS_ENABLED(OF_CONTROL)
215 static int ti_musb_peripheral_ofdata_to_platdata(struct udevice *dev)
217 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
218 const void *fdt = gd->fdt_blob;
219 int node = dev_of_offset(dev);
222 ret = ti_musb_ofdata_to_platdata(dev);
224 pr_err("platdata dt parse error\n");
227 platdata->plat.mode = MUSB_PERIPHERAL;
233 int dm_usb_gadget_handle_interrupts(struct udevice *dev)
235 struct ti_musb_peripheral *priv = dev_get_priv(dev);
237 priv->periph->isr(0, priv->periph);
242 static int ti_musb_peripheral_probe(struct udevice *dev)
244 struct ti_musb_peripheral *priv = dev_get_priv(dev);
245 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
248 priv->periph = musb_init_controller(&platdata->plat,
254 ti_musb_set_phy_power(dev, 1);
255 musb_gadget_setup(priv->periph);
256 return usb_add_gadget_udc((struct device *)dev, &priv->periph->g);
259 static int ti_musb_peripheral_remove(struct udevice *dev)
261 struct ti_musb_peripheral *priv = dev_get_priv(dev);
263 usb_del_gadget_udc(&priv->periph->g);
264 ti_musb_set_phy_power(dev, 0);
269 U_BOOT_DRIVER(ti_musb_peripheral) = {
270 .name = "ti-musb-peripheral",
271 .id = UCLASS_USB_GADGET_GENERIC,
272 #if CONFIG_IS_ENABLED(OF_CONTROL)
273 .ofdata_to_platdata = ti_musb_peripheral_ofdata_to_platdata,
275 .probe = ti_musb_peripheral_probe,
276 .remove = ti_musb_peripheral_remove,
277 .ops = &musb_usb_ops,
278 .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata),
279 .priv_auto_alloc_size = sizeof(struct ti_musb_peripheral),
280 .flags = DM_FLAG_PRE_RELOC,
284 #if CONFIG_IS_ENABLED(OF_CONTROL)
285 static int ti_musb_wrapper_bind(struct udevice *parent)
287 const void *fdt = gd->fdt_blob;
291 for (node = fdt_first_subnode(fdt, dev_of_offset(parent)); node > 0;
292 node = fdt_next_subnode(fdt, node)) {
294 const char *name = fdt_get_name(fdt, node, NULL);
295 enum usb_dr_mode dr_mode;
298 if (strncmp(name, "usb@", 4))
301 dr_mode = usb_get_dr_mode(node);
303 case USB_DR_MODE_PERIPHERAL:
304 /* Bind MUSB device */
305 ret = device_bind_driver_to_node(parent,
306 "ti-musb-peripheral",
308 offset_to_ofnode(node),
311 pr_err("musb - not able to bind usb peripheral node\n");
313 case USB_DR_MODE_HOST:
315 ret = device_bind_driver_to_node(parent,
318 offset_to_ofnode(node),
321 pr_err("musb - not able to bind usb host node\n");
330 static const struct udevice_id ti_musb_ids[] = {
331 { .compatible = "ti,am33xx-usb" },
335 U_BOOT_DRIVER(ti_musb_wrapper) = {
336 .name = "ti-musb-wrapper",
338 .of_match = ti_musb_ids,
339 .bind = ti_musb_wrapper_bind,
341 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
343 #endif /* CONFIG_IS_ENABLED(DM_USB) */