This driver is a modifed and cleaned-up version of drivers/usb/host/ehci-mx6.c,
it is to replace the original ehci-mx6.c driver.

It dropped the hardcoding code and only support driver model with
phy/clk/regulator/etc enabled.

The usbmisc code is handling "fsl,usbmisc" related driver code, similar
with Linux Kernel.

Signed-off-by: Peng Fan <peng....@nxp.com>
---
 Makefile                                    |   1 +
 arch/arm/include/asm/mach-imx/regs-usbphy.h |   5 +
 arch/arm/include/asm/mach-imx/sys_proto.h   |   2 +
 drivers/Makefile                            |   1 +
 drivers/usb/Kconfig                         |   2 +
 drivers/usb/imx/Kconfig                     |  10 +
 drivers/usb/imx/Makefile                    |   5 +
 drivers/usb/imx/ehci-imx.c                  | 406 ++++++++++++++++++++
 drivers/usb/imx/imx-usb-misc.c              |  47 +++
 9 files changed, 479 insertions(+)
 create mode 100644 drivers/usb/imx/Kconfig
 create mode 100644 drivers/usb/imx/Makefile
 create mode 100644 drivers/usb/imx/ehci-imx.c
 create mode 100644 drivers/usb/imx/imx-usb-misc.c

diff --git a/Makefile b/Makefile
index 1de1f384e5..d1e9b19f34 100644
--- a/Makefile
+++ b/Makefile
@@ -799,6 +799,7 @@ libs-y += drivers/usb/musb/
 libs-y += drivers/usb/musb-new/
 libs-y += drivers/usb/phy/
 libs-y += drivers/usb/ulpi/
+libs-y += drivers/usb/imx/
 libs-y += cmd/
 libs-y += common/
 libs-y += env/
diff --git a/arch/arm/include/asm/mach-imx/regs-usbphy.h 
b/arch/arm/include/asm/mach-imx/regs-usbphy.h
index 2b18ec20f3..c1f0a3264c 100644
--- a/arch/arm/include/asm/mach-imx/regs-usbphy.h
+++ b/arch/arm/include/asm/mach-imx/regs-usbphy.h
@@ -7,6 +7,8 @@
 #ifndef __REGS_USBPHY_H__
 #define __REGS_USBPHY_H__
 
+#include <linux/bitops.h>
+
 #define USBPHY_CTRL                                            0x00000030
 #define USBPHY_CTRL_SET                                        0x00000034
 #define USBPHY_CTRL_CLR                                        0x00000038
@@ -22,4 +24,7 @@
 #define USBPHY_CTRL_CLKGATE                            (1 << 30)
 #define USBPHY_CTRL_SFTRST                             (1 << 31)
 
+#define USBNC_PHY_STATUS_OFFSET                        0x23C
+#define USBNC_PHYSTATUS_ID_DIG                 BIT(4)
+
 #endif /* __REGS_USBPHY_H__ */
diff --git a/arch/arm/include/asm/mach-imx/sys_proto.h 
b/arch/arm/include/asm/mach-imx/sys_proto.h
index 5f0c1ae218..c1e3cdcd88 100644
--- a/arch/arm/include/asm/mach-imx/sys_proto.h
+++ b/arch/arm/include/asm/mach-imx/sys_proto.h
@@ -212,4 +212,6 @@ unsigned long call_imx_sip_ret2(unsigned long id, unsigned 
long reg0,
                                unsigned long reg3);
 
 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac);
+
+int usbmisc_init(void __iomem *addr, int index);
 #endif
diff --git a/drivers/Makefile b/drivers/Makefile
index 9eb51453e5..f566217399 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -58,6 +58,7 @@ obj-$(CONFIG_SPL_USB_GADGET) += usb/common/
 obj-$(CONFIG_SPL_USB_GADGET) += usb/gadget/udc/
 obj-$(CONFIG_SPL_WATCHDOG_SUPPORT) += watchdog/
 obj-$(CONFIG_SPL_USB_HOST_SUPPORT) += usb/host/
+obj-$(CONFIG_SPL_USB_HOST_SUPPORT) += usb/imx/
 obj-$(CONFIG_OMAP_USB_PHY) += usb/phy/
 obj-$(CONFIG_SPL_SATA_SUPPORT) += ata/ scsi/
 obj-$(CONFIG_HAVE_BLOCK_DEVICE) += block/
diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
index 34881a12b8..18c266a900 100644
--- a/drivers/usb/Kconfig
+++ b/drivers/usb/Kconfig
@@ -68,6 +68,8 @@ config SPL_DM_USB_GADGET
 
 source "drivers/usb/host/Kconfig"
 
+source "drivers/usb/imx/Kconfig"
+
 source "drivers/usb/cdns3/Kconfig"
 
 source "drivers/usb/dwc3/Kconfig"
diff --git a/drivers/usb/imx/Kconfig b/drivers/usb/imx/Kconfig
new file mode 100644
index 0000000000..5ccf5282fc
--- /dev/null
+++ b/drivers/usb/imx/Kconfig
@@ -0,0 +1,10 @@
+if USB_EHCI_HCD
+
+config USB_EHCI_IMX
+       bool "Support for i.MX on-chip EHCI USB controller"
+       depends on IMX8MM || IMX8MN
+       ---help---
+         Enables support for the on-chip EHCI controller on i.MX SoCs.
+         This is to replace ehci-mx6.c
+
+endif
diff --git a/drivers/usb/imx/Makefile b/drivers/usb/imx/Makefile
new file mode 100644
index 0000000000..9e50882719
--- /dev/null
+++ b/drivers/usb/imx/Makefile
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright 2020 NXP
+
+obj-$(CONFIG_USB_EHCI_IMX) += ehci-imx.o imx-usb-misc.o
diff --git a/drivers/usb/imx/ehci-imx.c b/drivers/usb/imx/ehci-imx.c
new file mode 100644
index 0000000000..66ea8a1ab4
--- /dev/null
+++ b/drivers/usb/imx/ehci-imx.c
@@ -0,0 +1,406 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2020 NXP
+ */
+
+#include <common.h>
+#include <clk.h>
+#include <usb.h>
+#include <errno.h>
+#include <wait_bit.h>
+#include <linux/compat.h>
+#include <linux/compiler.h>
+#include <linux/delay.h>
+#include <usb/ehci-ci.h>
+#include <asm/io.h>
+#include <asm/arch/imx-regs.h>
+#include <asm/arch/clock.h>
+#include <asm/mach-imx/regs-usbphy.h>
+#include <asm/mach-imx/sys_proto.h>
+#include <dm.h>
+#include <asm/mach-types.h>
+#include <power-domain.h>
+#include <power/regulator.h>
+#include <linux/iopoll.h>
+#include <linux/usb/otg.h>
+
+#include "../host/ehci.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static void ehci_imx_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t 
*status_reg, uint32_t *reg)
+{
+       u32 result;
+       int ret;
+
+       mdelay(50);
+
+       ret = read_poll_timeout(ehci_readl, status_reg, result, !(result & 
EHCI_PS_PR), 5, 20000);
+       if (ret)
+               printf("%s timeout\n", __func__);
+
+       *reg = ehci_readl(status_reg);
+}
+
+/**
+ * board_ehci_hcd_init - set usb vbus voltage
+ * @port:      usb otg port
+ *
+ * Target board specific, setup iomux pad to setup supply vbus voltage
+ * for usb otg port. Machine board file overrides board_ehci_hcd_init
+ *
+ * Return: 0 Success
+ */
+int __weak board_ehci_hcd_init(int port)
+{
+       return 0;
+}
+
+#define  USB_INIT_UNKNOWN (USB_INIT_DEVICE + 1)
+
+struct ehci_imx_priv_data {
+       struct ehci_ctrl ctrl;
+       struct usb_ehci *ehci;
+       struct udevice *vbus_supply;
+       enum usb_init_type init_type;
+       void *__iomem phy_base;
+       int portnr;
+       int clock_count;
+       struct clk_bulk clks;
+       void *__iomem usbmisc_addr;
+       int usbmisc_index;
+       struct phy phy;
+};
+
+static int imx_init_after_reset(struct ehci_ctrl *dev)
+{
+       struct ehci_imx_priv_data *priv = dev->priv;
+       enum usb_init_type type = priv->init_type;
+       struct usb_ehci *ehci = priv->ehci;
+       int ret;
+
+       ret = board_usb_init(priv->portnr, priv->init_type);
+       if (ret) {
+               printf("Failed to initialize board for USB\n");
+               return ret;
+       }
+
+       /* Do board specific initialization */
+       ret = board_ehci_hcd_init(priv->portnr);
+       if (ret) {
+               dev_err(dev, "board_ehci_hcd_init failed %d\n", ret);
+               return ret;
+       }
+
+       ret = usbmisc_init(priv->usbmisc_addr, priv->usbmisc_index);
+       if (ret) {
+               dev_err(dev, "usbmisc_init %d\n", ret);
+               return ret;
+       }
+
+       if (CONFIG_IS_ENABLED(DM_REGULATOR) && priv->vbus_supply) {
+               ret = regulator_set_enable(priv->vbus_supply, (type == 
USB_INIT_DEVICE) ?
+                                          false : true);
+               if (ret && ret != -ENOSYS) {
+                       printf("Error enabling VBUS supply (ret=%i)\n", ret);
+                       return ret;
+               }
+       }
+
+       if (type == USB_INIT_DEVICE)
+               return 0;
+
+       setbits_le32(&ehci->usbmode, CM_HOST);
+       writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
+       setbits_le32(&ehci->portsc, USB_EN);
+
+       mdelay(10);
+
+       return 0;
+}
+
+static const struct ehci_ops imx_ehci_ops = {
+       .powerup_fixup          = ehci_imx_powerup_fixup,
+       .init_after_reset       = imx_init_after_reset
+};
+
+/**
+ * board_ehci_usb_phy_mode - override usb phy mode
+ * @port:      usb host/otg port
+ *
+ * Target board specific, override usb_phy_mode.
+ * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
+ * left disconnected in this case usb_phy_mode will not be able to identify
+ * the phy mode that usb port is used.
+ * Machine file overrides board_usb_phy_mode.
+ * When the extcon property is set in DTB, machine must provide this function, 
otherwise
+ * it will default return HOST.
+ *
+ * Return: USB_INIT_DEVICE or USB_INIT_HOST
+ */
+int __weak board_ehci_usb_phy_mode(struct udevice *dev)
+{
+       return USB_INIT_HOST;
+}
+
+static int ehci_usb_phy_mode(struct udevice *dev)
+{
+       struct ehci_imx_priv_data *priv = dev_get_priv(dev);
+       void *__iomem phy_status;
+       u32 val;
+
+       if (is_imx8mm() || is_imx8mn()) {
+               phy_status = (void __iomem *)(priv->phy_base +
+                                             USBNC_PHY_STATUS_OFFSET);
+               val = readl(phy_status);
+
+               if (val & USBNC_PHYSTATUS_ID_DIG)
+                       priv->init_type = USB_INIT_DEVICE;
+               else
+                       priv->init_type = USB_INIT_HOST;
+       } else {
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int ehci_get_usb_phy(struct udevice *dev)
+{
+       struct ehci_imx_priv_data *priv = dev_get_priv(dev);
+       void *__iomem addr = (void *__iomem)devfdt_get_addr(dev);
+
+       if (is_imx8mm() || is_imx8mn())
+               priv->phy_base = addr;
+       else
+               return -EINVAL;
+
+       return 0;
+}
+
+static int ehci_get_usbmisc(struct udevice *dev)
+{
+       struct ehci_imx_priv_data *priv = dev_get_priv(dev);
+       struct ofnode_phandle_args args;
+       int ret;
+
+       /*
+        * About fsl,usbmisc, Refer to
+        * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
+        */
+       if (!ofnode_get_property(dev_ofnode(dev), "fsl,usbmisc", NULL))
+               return 0;
+
+       ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "fsl,usbmisc", 
"#index-cells",
+                                            0, 0, &args);
+       if (ret) {
+               dev_err(dev, "failed ofnode_parse_phandle_with_args %d\n", ret);
+               return ret;
+       }
+
+       if (!ofnode_valid(args.node)) {
+               dev_err(dev, "%s: unable to find node\n", __func__);
+               return -ENOENT;
+       }
+
+       priv->usbmisc_addr = (void __iomem *)ofnode_get_addr(args.node);
+       if ((fdt_addr_t)priv->usbmisc_addr == FDT_ADDR_T_NONE) {
+               dev_err(dev, "failed ofnode_get_addr %d\n", ret);
+               return -EINVAL;
+       }
+
+       priv->usbmisc_index = args.args[0];
+
+       return 0;
+}
+
+static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
+{
+       struct usb_platdata *plat = dev_get_platdata(dev);
+       struct ehci_imx_priv_data *priv = dev_get_priv(dev);
+       enum usb_dr_mode dr_mode;
+       const void *extcon;
+
+       extcon = ofnode_get_property(dev->node, "extcon", NULL);
+       if (extcon) {
+               priv->init_type = board_ehci_usb_phy_mode(dev);
+               goto check_type;
+       }
+
+       dr_mode = usb_get_dr_mode(dev->node);
+
+       switch (dr_mode) {
+       case USB_DR_MODE_HOST:
+               priv->init_type = USB_INIT_HOST;
+               break;
+       case USB_DR_MODE_PERIPHERAL:
+               priv->init_type = USB_INIT_DEVICE;
+               break;
+       case USB_DR_MODE_OTG:
+       case USB_DR_MODE_UNKNOWN:
+               priv->init_type = USB_INIT_UNKNOWN;
+               break;
+       };
+
+check_type:
+       if (priv->init_type != USB_INIT_UNKNOWN && priv->init_type != 
plat->init_type) {
+               debug("Request USB type is %u, board forced type is %u\n",
+                     plat->init_type, priv->init_type);
+               return -ENODEV;
+       }
+
+       return 0;
+}
+
+static int ehci_usb_probe(struct udevice *dev)
+{
+       struct usb_platdata *plat = dev_get_platdata(dev);
+       struct usb_ehci *ehci = dev_read_addr_ptr(dev);
+       struct ehci_imx_priv_data *priv = dev_get_priv(dev);
+       enum usb_init_type type = plat->init_type;
+       struct ehci_hccr *hccr;
+       struct ehci_hcor *hcor;
+       int ret, alias_id;
+       u32 portsc;
+
+       ret = dev_read_alias_seq(dev, &alias_id);
+       if (ret < 0) {
+               dev_err(dev, "%s: Could not get alias id: %d\n", __func__, ret);
+               return ret;
+       }
+
+       priv->ehci = ehci;
+       priv->portnr = dev->req_seq;
+
+       /* Init usb board level according to the requested init type */
+       ret = board_usb_init(priv->portnr, type);
+       if (ret) {
+               printf("Failed to initialize board for USB\n");
+               return ret;
+       }
+
+       if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
+               ret = device_get_supply_regulator(dev, "vbus-supply", 
&priv->vbus_supply);
+               if (ret)
+                       debug("%s: No vbus supply\n", dev->name);
+       }
+
+       ret = ehci_get_usb_phy(dev);
+       if (ret) {
+               debug("%s: fail to get USB PHY base\n", dev->name);
+               return ret;
+       }
+
+       ret = ehci_get_usbmisc(dev);
+       if (ret) {
+               dev_err(dev, "fail to get USB misc, %d\n", ret);
+               return ret;
+       }
+
+       ret = clk_get_bulk(dev, &priv->clks);
+       if (ret) {
+               dev_err(dev, "clk_get_bulk failed %d\n", ret);
+               return ret;
+       }
+
+       ret = clk_enable_bulk(&priv->clks);
+       if (ret) {
+               dev_err(dev, "clk_enable_bulkj falied, %d\n", ret);
+               return ret;
+       }
+
+       mdelay(1);
+
+       portsc = readl(&ehci->portsc);
+       if (portsc & PORT_PTS_PHCD) {
+               debug("suspended: portsc %x, enabled it.\n", portsc);
+               clrbits_le32(&ehci->portsc, PORT_PTS_PHCD);
+       }
+
+       /* Do board specific initialization */
+       ret = board_ehci_hcd_init(priv->portnr);
+       if (ret) {
+               dev_err(dev, "board_ehci_hcd_init failed %d\n", ret);
+               goto release_clk;
+       }
+
+       ret = usbmisc_init(priv->usbmisc_addr, priv->usbmisc_index);
+       if (ret) {
+               dev_err(dev, "usbmisc_init %d\n", ret);
+               goto release_clk;
+       }
+
+       ret = ehci_setup_phy(dev, &priv->phy, 0);
+       if (ret < 0 && ret != -ENOSYS) {
+               dev_err(dev, "fail to setup USB PHY, %d\n", ret);
+               return ret;
+       }
+
+       /* If the init_type is unknown due to it is not forced in DTB, we use 
USB ID to detect */
+       if (priv->init_type == USB_INIT_UNKNOWN) {
+               ret = ehci_usb_phy_mode(dev);
+               if (ret)
+                       return ret;
+               if (priv->init_type != type)
+                       return -ENODEV;
+       }
+
+       if (CONFIG_IS_ENABLED(DM_REGULATOR) && priv->vbus_supply) {
+               ret = regulator_set_enable(priv->vbus_supply,
+                                          (priv->init_type == USB_INIT_DEVICE) 
?
+                                          false : true);
+               if (ret && ret != -ENOSYS) {
+                       printf("Error enabling VBUS supply (ret=%i)\n", ret);
+                       return ret;
+               }
+       }
+
+       if (priv->init_type == USB_INIT_HOST) {
+               setbits_le32(&ehci->usbmode, CM_HOST);
+               writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
+               setbits_le32(&ehci->portsc, USB_EN);
+       }
+
+       mdelay(10);
+
+       hccr = (struct ehci_hccr *)((uintptr_t)&ehci->caplength);
+       hcor = (struct ehci_hcor *)((uintptr_t)hccr +
+                       HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
+
+       return ehci_register(dev, hccr, hcor, &imx_ehci_ops, 0, 
priv->init_type);
+
+release_clk:
+       clk_release_bulk(&priv->clks);
+
+       return ret;
+}
+
+int ehci_usb_remove(struct udevice *dev)
+{
+       struct ehci_imx_priv_data *priv = dev_get_priv(dev);
+       struct usb_platdata *plat = dev_get_platdata(dev);
+
+       ehci_deregister(dev);
+
+       plat->init_type = 0; /* Clean the requested usb type to host mode */
+
+       return board_usb_cleanup(dev->req_seq, priv->init_type);
+}
+
+static const struct udevice_id imx_usb_ids[] = {
+       { .compatible = "fsl,imx8mm-usb" },
+       { }
+};
+
+U_BOOT_DRIVER(usb_imx) = {
+       .name   = "ehci_imx",
+       .id     = UCLASS_USB,
+       .of_match = imx_usb_ids,
+       .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
+       .probe  = ehci_usb_probe,
+       .remove = ehci_usb_remove,
+       .ops    = &ehci_usb_ops,
+       .platdata_auto_alloc_size = sizeof(struct usb_platdata),
+       .priv_auto_alloc_size = sizeof(struct ehci_imx_priv_data),
+       .flags  = DM_FLAG_ALLOC_PRIV_DMA,
+};
diff --git a/drivers/usb/imx/imx-usb-misc.c b/drivers/usb/imx/imx-usb-misc.c
new file mode 100644
index 0000000000..a9db71fdb1
--- /dev/null
+++ b/drivers/usb/imx/imx-usb-misc.c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2020 NXP
+ *     Peng Fan <peng....@nxp.com>
+ */
+
+#include <common.h>
+#include <asm/io.h>
+#include <asm/mach-imx/sys_proto.h>
+
+#define MX7D_USB_OTG_PHY_CFG2  0x34
+
+#define USBNC_PHYCFG2_ACAENB   (1 << 4) /* otg_id detection enable */
+#define UCTRL_PWR_POL          (1 << 9) /* OTG Polarity of Power Pin */
+#define UCTRL_OVER_CUR_POL     (1 << 8) /* OTG Polarity of Overcurrent */
+#define UCTRL_OVER_CUR_DIS     (1 << 7) /* Disable OTG Overcurrent Detection */
+
+int usbmisc_init(void __iomem *addr, int index)
+{
+       void __iomem *ctrl;
+
+       if (is_mx7() || is_imx8mm() || is_imx8mn())
+               clrbits_le32(addr + MX7D_USB_OTG_PHY_CFG2, 
USBNC_PHYCFG2_ACAENB);
+
+       if (is_mx6())
+               ctrl = addr + index * 4;
+       else
+               ctrl = addr;
+
+#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
+       /* mx6qarm2 seems to required a different setting*/
+       clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
+#else
+       setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
+#endif
+
+       setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
+
+       /* Set power polarity to high active */
+#ifdef CONFIG_MXC_USB_OTG_HACTIVE
+       setbits_le32(ctrl, UCTRL_PWR_POL);
+#else
+       clrbits_le32(ctrl, UCTRL_PWR_POL);
+#endif
+
+       return 0;
+}
-- 
2.28.0

Reply via email to