[U-Boot] [PATCH v1 13/16] usb: musb-new: Add support for DM_USB_GADGET

Jean-Jacques Hiblot jjhiblot at ti.com
Wed Nov 21 10:51:15 UTC 2018


Enable DM for USB peripheral in the musb-new driver.
Also make sure that the driver can be used in the SPL.
This implies that:
* the driver must work with and without the OF_CONTROL option. That
in turn, implies that the platform data can be passed in a struct
ti_musb_platdata or be read from the dtb
* usb.o is linked in the SPL if host support is enabled

Signed-off-by: Jean-Jacques Hiblot <jjhiblot at ti.com>
---

 arch/arm/include/asm/omap_musb.h   |   8 ++
 common/Makefile                    |   3 +
 drivers/usb/musb-new/musb_gadget.c |  11 +++
 drivers/usb/musb-new/musb_uboot.c  |   4 +-
 drivers/usb/musb-new/ti-musb.c     | 179 ++++++++++++++++++++++++++++---------
 5 files changed, 160 insertions(+), 45 deletions(-)

diff --git a/arch/arm/include/asm/omap_musb.h b/arch/arm/include/asm/omap_musb.h
index 875f100..b40ea00 100644
--- a/arch/arm/include/asm/omap_musb.h
+++ b/arch/arm/include/asm/omap_musb.h
@@ -7,6 +7,7 @@
 
 #ifndef __ASM_ARM_OMAP_MUSB_H
 #define __ASM_ARM_OMAP_MUSB_H
+#include <linux/usb/musb.h>
 
 extern struct musb_platform_ops musb_dsps_ops;
 extern const struct musb_platform_ops am35x_ops;
@@ -21,4 +22,11 @@ struct omap_musb_board_data {
 };
 
 enum musb_interface    {MUSB_INTERFACE_ULPI, MUSB_INTERFACE_UTMI};
+
+struct ti_musb_platdata {
+	void *base;
+	void *ctrl_mod_base;
+	struct musb_hdrc_platform_data plat;
+};
+
 #endif /* __ASM_ARM_OMAP_MUSB_H */
diff --git a/common/Makefile b/common/Makefile
index a238836..44fdb92 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -72,9 +72,12 @@ obj-$(CONFIG_SPL_YMODEM_SUPPORT) += xyzModem.o
 obj-$(CONFIG_SPL_LOAD_FIT) += common_fit.o
 obj-$(CONFIG_SPL_NET_SUPPORT) += miiphyutil.o
 obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o
+
 ifdef CONFIG_SPL_USB_HOST_SUPPORT
 obj-$(CONFIG_SPL_USB_SUPPORT) += usb.o usb_hub.o
 obj-$(CONFIG_USB_STORAGE) += usb_storage.o
+else
+obj-$(CONFIG_USB_MUSB_HOST) += usb.o
 endif
 endif
 #others
diff --git a/drivers/usb/musb-new/musb_gadget.c b/drivers/usb/musb-new/musb_gadget.c
index 8b6cec1..b35d33f 100644
--- a/drivers/usb/musb-new/musb_gadget.c
+++ b/drivers/usb/musb-new/musb_gadget.c
@@ -1775,6 +1775,14 @@ static int musb_gadget_start(struct usb_gadget *g,
 		struct usb_gadget_driver *driver);
 static int musb_gadget_stop(struct usb_gadget *g,
 		struct usb_gadget_driver *driver);
+#else
+static int musb_gadget_stop(struct usb_gadget *g)
+{
+	struct musb	*musb = gadget_to_musb(g);
+
+	musb_stop(musb);
+	return 0;
+}
 #endif
 
 static const struct usb_gadget_ops musb_gadget_operations = {
@@ -1787,6 +1795,9 @@ static const struct usb_gadget_ops musb_gadget_operations = {
 #ifndef __UBOOT__
 	.udc_start		= musb_gadget_start,
 	.udc_stop		= musb_gadget_stop,
+#else
+	.udc_start		= musb_gadget_start,
+	.udc_stop		= musb_gadget_stop,
 #endif
 };
 
diff --git a/drivers/usb/musb-new/musb_uboot.c b/drivers/usb/musb-new/musb_uboot.c
index 2bf918e..90db5ef 100644
--- a/drivers/usb/musb-new/musb_uboot.c
+++ b/drivers/usb/musb-new/musb_uboot.c
@@ -367,7 +367,7 @@ struct dm_usb_ops musb_usb_ops = {
 #endif /* CONFIG_DM_USB */
 #endif /* CONFIG_USB_MUSB_HOST */
 
-#ifdef CONFIG_USB_MUSB_GADGET
+#if defined(CONFIG_USB_MUSB_GADGET) && !defined(CONFIG_DM_USB_GADGET)
 static struct musb *gadget;
 
 int usb_gadget_handle_interrupts(int index)
@@ -430,7 +430,7 @@ struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
 		musbp = &musb_host.host;
 		break;
 #endif
-#ifdef CONFIG_USB_MUSB_GADGET
+#if defined(CONFIG_USB_MUSB_GADGET) && !defined(CONFIG_DM_USB_GADGET)
 	case MUSB_PERIPHERAL:
 		musbp = &gadget;
 		break;
diff --git a/drivers/usb/musb-new/ti-musb.c b/drivers/usb/musb-new/ti-musb.c
index 9fbe2d6..c3d9ef0 100644
--- a/drivers/usb/musb-new/ti-musb.c
+++ b/drivers/usb/musb-new/ti-musb.c
@@ -20,22 +20,33 @@
 DECLARE_GLOBAL_DATA_PTR;
 
 #ifdef CONFIG_DM_USB
-
 /* USB 2.0 PHY Control */
 #define CM_PHY_PWRDN			(1 << 0)
 #define CM_PHY_OTG_PWRDN		(1 << 1)
 #define OTGVDET_EN			(1 << 19)
 #define OTGSESSENDEN			(1 << 20)
 
+#define AM335X_USB0_CTRL	0x0
 #define AM335X_USB1_CTRL	0x8
 
-struct ti_musb_platdata {
-	void *base;
-	void *ctrl_mod_base;
-	struct musb_hdrc_platform_data plat;
-	struct musb_hdrc_config musb_config;
-	struct omap_musb_board_data otg_board_data;
-};
+static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
+{
+	struct ti_musb_platdata *platdata = dev_get_platdata(dev);
+
+	if (!platdata->ctrl_mod_base)
+		return;
+
+	if (on) {
+		clrsetbits_le32(platdata->ctrl_mod_base,
+				CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
+				OTGVDET_EN | OTGSESSENDEN);
+	} else {
+		clrsetbits_le32(platdata->ctrl_mod_base, 0,
+				CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
+	}
+}
+
+#if CONFIG_IS_ENABLED(OF_CONTROL)
 
 static int ti_musb_get_usb_index(int node)
 {
@@ -64,20 +75,6 @@ static int ti_musb_get_usb_index(int node)
 	return -ENOENT;
 }
 
-static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
-{
-	struct ti_musb_platdata *platdata = dev_get_platdata(dev);
-
-	if (on) {
-		clrsetbits_le32(platdata->ctrl_mod_base,
-				CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
-				OTGVDET_EN | OTGSESSENDEN);
-	} else {
-		clrsetbits_le32(platdata->ctrl_mod_base, 0,
-				CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
-	}
-}
-
 static int ti_musb_ofdata_to_platdata(struct udevice *dev)
 {
 	struct ti_musb_platdata *platdata = dev_get_platdata(dev);
@@ -86,6 +83,7 @@ static int ti_musb_ofdata_to_platdata(struct udevice *dev)
 	int phys;
 	int ctrl_mod;
 	int usb_index;
+	struct musb_hdrc_config *musb_config;
 
 	platdata->base = (void *)devfdt_get_addr_index(dev, 1);
 
@@ -96,38 +94,41 @@ static int ti_musb_ofdata_to_platdata(struct udevice *dev)
 	switch (usb_index) {
 	case 1:
 		platdata->ctrl_mod_base += AM335X_USB1_CTRL;
+		break;
 	case 0:
+		platdata->ctrl_mod_base += AM335X_USB0_CTRL;
+		break;
 	default:
 		break;
 	}
 
-	platdata->musb_config.multipoint = fdtdec_get_int(fdt, node,
-							  "mentor,multipoint",
-							  -1);
-	if (platdata->musb_config.multipoint < 0) {
+	musb_config = malloc(sizeof(struct musb_hdrc_config));
+	memset(musb_config, 0, sizeof(struct musb_hdrc_config));
+
+	musb_config->multipoint = fdtdec_get_int(fdt, node,
+						 "mentor,multipoint", -1);
+	if (musb_config->multipoint < 0) {
 		pr_err("MUSB multipoint DT entry missing\n");
 		return -ENOENT;
 	}
 
-	platdata->musb_config.dyn_fifo = 1;
+	musb_config->dyn_fifo = 1;
 
-	platdata->musb_config.num_eps = fdtdec_get_int(fdt, node,
-						       "mentor,num-eps", -1);
-	if (platdata->musb_config.num_eps < 0) {
+	musb_config->num_eps = fdtdec_get_int(fdt, node, "mentor,num-eps",
+					      -1);
+	if (musb_config->num_eps < 0) {
 		pr_err("MUSB num-eps DT entry missing\n");
 		return -ENOENT;
 	}
 
-	platdata->musb_config.ram_bits = fdtdec_get_int(fdt, node,
-							"mentor,ram-bits", -1);
-	if (platdata->musb_config.ram_bits < 0) {
+	musb_config->ram_bits = fdtdec_get_int(fdt, node, "mentor,ram-bits",
+					       -1);
+	if (musb_config->ram_bits < 0) {
 		pr_err("MUSB ram-bits DT entry missing\n");
 		return -ENOENT;
 	}
 
-	platdata->otg_board_data.set_phy_power = ti_musb_set_phy_power;
-	platdata->otg_board_data.dev = dev;
-	platdata->plat.config = &platdata->musb_config;
+	platdata->plat.config = musb_config;
 
 	platdata->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
 	if (platdata->plat.power < 0) {
@@ -136,29 +137,27 @@ static int ti_musb_ofdata_to_platdata(struct udevice *dev)
 	}
 
 	platdata->plat.platform_ops = &musb_dsps_ops;
-	platdata->plat.board_data = &platdata->otg_board_data;
 
 	return 0;
 }
+#endif
 
 static int ti_musb_host_probe(struct udevice *dev)
 {
 	struct musb_host_data *host = dev_get_priv(dev);
 	struct ti_musb_platdata *platdata = dev_get_platdata(dev);
 	struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
-	struct omap_musb_board_data *otg_board_data;
 	int ret;
 
 	priv->desc_before_addr = true;
 
-	otg_board_data = &platdata->otg_board_data;
-
 	host->host = musb_init_controller(&platdata->plat,
-					  (struct device *)otg_board_data,
+					  NULL,
 					  platdata->base);
 	if (!host->host)
 		return -EIO;
 
+	ti_musb_set_phy_power(dev, 1);
 	ret = musb_lowlevel_init(host);
 
 	return ret;
@@ -169,10 +168,12 @@ static int ti_musb_host_remove(struct udevice *dev)
 	struct musb_host_data *host = dev_get_priv(dev);
 
 	musb_stop(host->host);
+	ti_musb_set_phy_power(dev, 0);
 
 	return 0;
 }
 
+#if CONFIG_IS_ENABLED(OF_CONTROL)
 static int ti_musb_host_ofdata_to_platdata(struct udevice *dev)
 {
 	struct ti_musb_platdata *platdata = dev_get_platdata(dev);
@@ -190,11 +191,14 @@ static int ti_musb_host_ofdata_to_platdata(struct udevice *dev)
 
 	return 0;
 }
+#endif
 
 U_BOOT_DRIVER(ti_musb_host) = {
 	.name	= "ti-musb-host",
 	.id	= UCLASS_USB,
+#if CONFIG_IS_ENABLED(OF_CONTROL)
 	.ofdata_to_platdata = ti_musb_host_ofdata_to_platdata,
+#endif
 	.probe = ti_musb_host_probe,
 	.remove = ti_musb_host_remove,
 	.ops	= &musb_usb_ops,
@@ -202,6 +206,82 @@ U_BOOT_DRIVER(ti_musb_host) = {
 	.priv_auto_alloc_size = sizeof(struct musb_host_data),
 };
 
+#ifdef CONFIG_DM_USB_GADGET
+struct ti_musb_peripheral {
+	struct musb *periph;
+};
+
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+static int ti_musb_peripheral_ofdata_to_platdata(struct udevice *dev)
+{
+	struct ti_musb_platdata *platdata = dev_get_platdata(dev);
+	const void *fdt = gd->fdt_blob;
+	int node = dev_of_offset(dev);
+	int ret;
+
+	ret = ti_musb_ofdata_to_platdata(dev);
+	if (ret) {
+		pr_err("platdata dt parse error\n");
+		return ret;
+	}
+	platdata->plat.mode = MUSB_PERIPHERAL;
+
+	return 0;
+}
+#endif
+
+int dm_usb_gadget_handle_interrupts(struct udevice *dev)
+{
+	struct ti_musb_peripheral *priv = dev_get_priv(dev);
+
+	priv->periph->isr(0, priv->periph);
+
+	return 0;
+}
+
+static int ti_musb_peripheral_probe(struct udevice *dev)
+{
+	struct ti_musb_peripheral *priv = dev_get_priv(dev);
+	struct ti_musb_platdata *platdata = dev_get_platdata(dev);
+	int ret;
+
+	priv->periph = musb_init_controller(&platdata->plat,
+					    NULL,
+					    platdata->base);
+	if (!priv->periph)
+		return -EIO;
+
+	ti_musb_set_phy_power(dev, 1);
+	musb_gadget_setup(priv->periph);
+	return usb_add_gadget_udc((struct device *)dev, &priv->periph->g);
+}
+
+static int ti_musb_peripheral_remove(struct udevice *dev)
+{
+	struct ti_musb_peripheral *priv = dev_get_priv(dev);
+
+	usb_del_gadget_udc(&priv->periph->g);
+	ti_musb_set_phy_power(dev, 0);
+
+	return 0;
+}
+
+U_BOOT_DRIVER(ti_musb_peripheral) = {
+	.name	= "ti-musb-peripheral",
+	.id	= UCLASS_USB_GADGET_GENERIC,
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+	.ofdata_to_platdata = ti_musb_peripheral_ofdata_to_platdata,
+#endif
+	.probe = ti_musb_peripheral_probe,
+	.remove = ti_musb_peripheral_remove,
+	.ops	= &musb_usb_ops,
+	.platdata_auto_alloc_size = sizeof(struct ti_musb_platdata),
+	.priv_auto_alloc_size = sizeof(struct ti_musb_peripheral),
+	.flags = DM_FLAG_PRE_RELOC,
+};
+#endif
+
+#if CONFIG_IS_ENABLED(OF_CONTROL)
 static int ti_musb_wrapper_bind(struct udevice *parent)
 {
 	const void *fdt = gd->fdt_blob;
@@ -222,11 +302,23 @@ static int ti_musb_wrapper_bind(struct udevice *parent)
 		switch (dr_mode) {
 		case USB_DR_MODE_PERIPHERAL:
 			/* Bind MUSB device */
+			ret = device_bind_driver_to_node(parent,
+							 "ti-musb-peripheral",
+							 name,
+							 offset_to_ofnode(node),
+							 &dev);
+			if (ret) {
+				pr_err("musb - not able to bind usb peripheral node\n");
+				return ret;
+			}
 			break;
 		case USB_DR_MODE_HOST:
 			/* Bind MUSB host */
-			ret = device_bind_driver_to_node(parent, "ti-musb-host",
-					name, offset_to_ofnode(node), &dev);
+			ret = device_bind_driver_to_node(parent,
+							 "ti-musb-host",
+							 name,
+							 offset_to_ofnode(node),
+							 &dev);
 			if (ret) {
 				pr_err("musb - not able to bind usb host node\n");
 				return ret;
@@ -250,5 +342,6 @@ U_BOOT_DRIVER(ti_musb_wrapper) = {
 	.of_match = ti_musb_ids,
 	.bind = ti_musb_wrapper_bind,
 };
+#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
 
 #endif /* CONFIG_DM_USB */
-- 
2.7.4



More information about the U-Boot mailing list