1 // SPDX-License-Identifier: GPL-2.0+
3 * MISC driver for TI MUSB Glue.
6 * Texas Instruments Incorporated, <www.ti.com>
14 #include <linux/usb/otg.h>
15 #include <dm/device-internal.h>
19 #include <asm/omap_musb.h>
20 #include "musb_uboot.h"
22 DECLARE_GLOBAL_DATA_PTR;
24 #if CONFIG_IS_ENABLED(DM_USB)
25 /* USB 2.0 PHY Control */
26 #define CM_PHY_PWRDN (1 << 0)
27 #define CM_PHY_OTG_PWRDN (1 << 1)
28 #define OTGVDET_EN (1 << 19)
29 #define OTGSESSENDEN (1 << 20)
31 #define AM335X_USB0_CTRL 0x0
32 #define AM335X_USB1_CTRL 0x8
34 static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
36 struct ti_musb_plat *plat = dev_get_plat(dev);
38 if (!plat->ctrl_mod_base)
42 clrsetbits_le32(plat->ctrl_mod_base,
43 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
44 OTGVDET_EN | OTGSESSENDEN);
46 clrsetbits_le32(plat->ctrl_mod_base, 0,
47 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
51 #if CONFIG_IS_ENABLED(OF_CONTROL)
53 static int ti_musb_get_usb_index(int node)
55 const void *fdt = gd->fdt_blob;
58 const char *alias_path;
61 fdt_get_path(fdt, node, path, sizeof(path));
64 snprintf(alias, sizeof(alias), "usb%d", i);
65 alias_path = fdt_get_alias(fdt, alias);
66 if (alias_path == NULL) {
67 debug("USB index not found\n");
71 if (!strcmp(path, alias_path))
80 static int ti_musb_of_to_plat(struct udevice *dev)
82 struct ti_musb_plat *plat = dev_get_plat(dev);
83 const void *fdt = gd->fdt_blob;
84 int node = dev_of_offset(dev);
88 struct musb_hdrc_config *musb_config;
90 plat->base = (void *)devfdt_get_addr_index(dev, 1);
92 phys = fdtdec_lookup_phandle(fdt, node, "phys");
93 ctrl_mod = fdtdec_lookup_phandle(fdt, phys, "ti,ctrl_mod");
94 plat->ctrl_mod_base = (void *)fdtdec_get_addr(fdt, ctrl_mod, "reg");
95 usb_index = ti_musb_get_usb_index(node);
98 plat->ctrl_mod_base += AM335X_USB1_CTRL;
101 plat->ctrl_mod_base += AM335X_USB0_CTRL;
107 musb_config = malloc(sizeof(struct musb_hdrc_config));
108 memset(musb_config, 0, sizeof(struct musb_hdrc_config));
110 musb_config->multipoint = fdtdec_get_int(fdt, node,
111 "mentor,multipoint", -1);
112 if (musb_config->multipoint < 0) {
113 pr_err("MUSB multipoint DT entry missing\n");
117 musb_config->dyn_fifo = 1;
119 musb_config->num_eps = fdtdec_get_int(fdt, node, "mentor,num-eps",
121 if (musb_config->num_eps < 0) {
122 pr_err("MUSB num-eps DT entry missing\n");
126 musb_config->ram_bits = fdtdec_get_int(fdt, node, "mentor,ram-bits",
128 if (musb_config->ram_bits < 0) {
129 pr_err("MUSB ram-bits DT entry missing\n");
133 plat->plat.config = musb_config;
135 plat->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
136 if (plat->plat.power < 0) {
137 pr_err("MUSB mentor,power DT entry missing\n");
141 plat->plat.platform_ops = &musb_dsps_ops;
147 static int ti_musb_host_probe(struct udevice *dev)
149 struct musb_host_data *host = dev_get_priv(dev);
150 struct ti_musb_plat *plat = dev_get_plat(dev);
151 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
154 priv->desc_before_addr = true;
156 host->host = musb_init_controller(&plat->plat,
162 ti_musb_set_phy_power(dev, 1);
163 ret = musb_lowlevel_init(host);
168 static int ti_musb_host_remove(struct udevice *dev)
170 struct musb_host_data *host = dev_get_priv(dev);
172 musb_stop(host->host);
173 ti_musb_set_phy_power(dev, 0);
178 #if CONFIG_IS_ENABLED(OF_CONTROL)
179 static int ti_musb_host_of_to_plat(struct udevice *dev)
181 struct ti_musb_plat *plat = dev_get_plat(dev);
182 const void *fdt = gd->fdt_blob;
183 int node = dev_of_offset(dev);
186 ret = ti_musb_of_to_plat(dev);
188 pr_err("plat dt parse error\n");
192 plat->plat.mode = MUSB_HOST;
198 U_BOOT_DRIVER(ti_musb_host) = {
199 .name = "ti-musb-host",
201 #if CONFIG_IS_ENABLED(OF_CONTROL)
202 .of_to_plat = ti_musb_host_of_to_plat,
204 .probe = ti_musb_host_probe,
205 .remove = ti_musb_host_remove,
206 .ops = &musb_usb_ops,
207 .plat_auto = sizeof(struct ti_musb_plat),
208 .priv_auto = sizeof(struct musb_host_data),
211 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
212 struct ti_musb_peripheral {
216 #if CONFIG_IS_ENABLED(OF_CONTROL)
217 static int ti_musb_peripheral_of_to_plat(struct udevice *dev)
219 struct ti_musb_plat *plat = dev_get_plat(dev);
220 const void *fdt = gd->fdt_blob;
221 int node = dev_of_offset(dev);
224 ret = ti_musb_of_to_plat(dev);
226 pr_err("plat dt parse error\n");
229 plat->plat.mode = MUSB_PERIPHERAL;
235 int dm_usb_gadget_handle_interrupts(struct udevice *dev)
237 struct ti_musb_peripheral *priv = dev_get_priv(dev);
239 priv->periph->isr(0, priv->periph);
244 static int ti_musb_peripheral_probe(struct udevice *dev)
246 struct ti_musb_peripheral *priv = dev_get_priv(dev);
247 struct ti_musb_plat *plat = dev_get_plat(dev);
250 priv->periph = musb_init_controller(&plat->plat,
256 ti_musb_set_phy_power(dev, 1);
257 musb_gadget_setup(priv->periph);
258 return usb_add_gadget_udc((struct device *)dev, &priv->periph->g);
261 static int ti_musb_peripheral_remove(struct udevice *dev)
263 struct ti_musb_peripheral *priv = dev_get_priv(dev);
265 usb_del_gadget_udc(&priv->periph->g);
266 ti_musb_set_phy_power(dev, 0);
271 U_BOOT_DRIVER(ti_musb_peripheral) = {
272 .name = "ti-musb-peripheral",
273 .id = UCLASS_USB_GADGET_GENERIC,
274 #if CONFIG_IS_ENABLED(OF_CONTROL)
275 .of_to_plat = ti_musb_peripheral_of_to_plat,
277 .probe = ti_musb_peripheral_probe,
278 .remove = ti_musb_peripheral_remove,
279 .ops = &musb_usb_ops,
280 .plat_auto = sizeof(struct ti_musb_plat),
281 .priv_auto = sizeof(struct ti_musb_peripheral),
282 .flags = DM_FLAG_PRE_RELOC,
286 #if CONFIG_IS_ENABLED(OF_CONTROL)
287 static int ti_musb_wrapper_bind(struct udevice *parent)
292 ofnode_for_each_subnode(node, dev_ofnode(parent)) {
294 const char *name = ofnode_get_name(node);
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",
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,
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) */