1 // SPDX-License-Identifier: GPL-2.0+
3 * MISC driver for TI MUSB Glue.
6 * Texas Instruments Incorporated, <www.ti.com>
13 #include <linux/usb/otg.h>
14 #include <dm/device-internal.h>
18 #include <asm/omap_musb.h>
19 #include "musb_uboot.h"
21 DECLARE_GLOBAL_DATA_PTR;
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)
30 #define AM335X_USB0_CTRL 0x0
31 #define AM335X_USB1_CTRL 0x8
33 static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
35 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
37 if (!platdata->ctrl_mod_base)
41 clrsetbits_le32(platdata->ctrl_mod_base,
42 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
43 OTGVDET_EN | OTGSESSENDEN);
45 clrsetbits_le32(platdata->ctrl_mod_base, 0,
46 CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
50 #if CONFIG_IS_ENABLED(OF_CONTROL)
52 static int ti_musb_get_usb_index(int node)
54 const void *fdt = gd->fdt_blob;
57 const char *alias_path;
60 fdt_get_path(fdt, node, path, sizeof(path));
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");
70 if (!strcmp(path, alias_path))
79 static int ti_musb_ofdata_to_platdata(struct udevice *dev)
81 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
82 const void *fdt = gd->fdt_blob;
83 int node = dev_of_offset(dev);
87 struct musb_hdrc_config *musb_config;
89 platdata->base = (void *)devfdt_get_addr_index(dev, 1);
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);
97 platdata->ctrl_mod_base += AM335X_USB1_CTRL;
100 platdata->ctrl_mod_base += AM335X_USB0_CTRL;
106 musb_config = malloc(sizeof(struct musb_hdrc_config));
107 memset(musb_config, 0, sizeof(struct musb_hdrc_config));
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");
116 musb_config->dyn_fifo = 1;
118 musb_config->num_eps = fdtdec_get_int(fdt, node, "mentor,num-eps",
120 if (musb_config->num_eps < 0) {
121 pr_err("MUSB num-eps DT entry missing\n");
125 musb_config->ram_bits = fdtdec_get_int(fdt, node, "mentor,ram-bits",
127 if (musb_config->ram_bits < 0) {
128 pr_err("MUSB ram-bits DT entry missing\n");
132 platdata->plat.config = musb_config;
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");
140 platdata->plat.platform_ops = &musb_dsps_ops;
146 static int ti_musb_host_probe(struct udevice *dev)
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);
153 priv->desc_before_addr = true;
155 host->host = musb_init_controller(&platdata->plat,
161 ti_musb_set_phy_power(dev, 1);
162 ret = musb_lowlevel_init(host);
167 static int ti_musb_host_remove(struct udevice *dev)
169 struct musb_host_data *host = dev_get_priv(dev);
171 musb_stop(host->host);
172 ti_musb_set_phy_power(dev, 0);
177 #if CONFIG_IS_ENABLED(OF_CONTROL)
178 static int ti_musb_host_ofdata_to_platdata(struct udevice *dev)
180 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
181 const void *fdt = gd->fdt_blob;
182 int node = dev_of_offset(dev);
185 ret = ti_musb_ofdata_to_platdata(dev);
187 pr_err("platdata dt parse error\n");
191 platdata->plat.mode = MUSB_HOST;
197 U_BOOT_DRIVER(ti_musb_host) = {
198 .name = "ti-musb-host",
200 #if CONFIG_IS_ENABLED(OF_CONTROL)
201 .ofdata_to_platdata = ti_musb_host_ofdata_to_platdata,
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),
210 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
211 struct ti_musb_peripheral {
215 #if CONFIG_IS_ENABLED(OF_CONTROL)
216 static int ti_musb_peripheral_ofdata_to_platdata(struct udevice *dev)
218 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
219 const void *fdt = gd->fdt_blob;
220 int node = dev_of_offset(dev);
223 ret = ti_musb_ofdata_to_platdata(dev);
225 pr_err("platdata dt parse error\n");
228 platdata->plat.mode = MUSB_PERIPHERAL;
234 int dm_usb_gadget_handle_interrupts(struct udevice *dev)
236 struct ti_musb_peripheral *priv = dev_get_priv(dev);
238 priv->periph->isr(0, priv->periph);
243 static int ti_musb_peripheral_probe(struct udevice *dev)
245 struct ti_musb_peripheral *priv = dev_get_priv(dev);
246 struct ti_musb_platdata *platdata = dev_get_platdata(dev);
249 priv->periph = musb_init_controller(&platdata->plat,
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);
260 static int ti_musb_peripheral_remove(struct udevice *dev)
262 struct ti_musb_peripheral *priv = dev_get_priv(dev);
264 usb_del_gadget_udc(&priv->periph->g);
265 ti_musb_set_phy_power(dev, 0);
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,
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,
285 #if CONFIG_IS_ENABLED(OF_CONTROL)
286 static int ti_musb_wrapper_bind(struct udevice *parent)
288 const void *fdt = gd->fdt_blob;
292 for (node = fdt_first_subnode(fdt, dev_of_offset(parent)); node > 0;
293 node = fdt_next_subnode(fdt, node)) {
295 const char *name = fdt_get_name(fdt, node, NULL);
296 enum usb_dr_mode dr_mode;
299 if (strncmp(name, "usb@", 4))
302 dr_mode = usb_get_dr_mode(node);
304 case USB_DR_MODE_PERIPHERAL:
305 /* Bind MUSB device */
306 ret = device_bind_driver_to_node(parent,
307 "ti-musb-peripheral",
309 offset_to_ofnode(node),
312 pr_err("musb - not able to bind usb peripheral node\n");
314 case USB_DR_MODE_HOST:
316 ret = device_bind_driver_to_node(parent,
319 offset_to_ofnode(node),
322 pr_err("musb - not able to bind usb host node\n");
331 static const struct udevice_id ti_musb_ids[] = {
332 { .compatible = "ti,am33xx-usb" },
336 U_BOOT_DRIVER(ti_musb_wrapper) = {
337 .name = "ti-musb-wrapper",
339 .of_match = ti_musb_ids,
340 .bind = ti_musb_wrapper_bind,
342 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
344 #endif /* CONFIG_IS_ENABLED(DM_USB) */