2022-03-06 08:01:06

by Vincent Shih

[permalink] [raw]
Subject: [PATCH v3 0/2] Add driver for ehci in Sunplus SP7021

This is a patch series for ehci driver for Sunplus SP7021 SoC.

Sunplus SP7021 is an ARM Coretex A7 (4 cores) based SoC. It integrates
many peripherals (ex: UART, I2C, SPI, SDIO, eMMC, USB, SD Card and
etc.) into a single chip. It is designed for industrial control.

Refer to:
https://sunplus-tibbo.atlassian.net/wiki/spaces/doc/overview
https://tibbo.com/store/plus1.html

Vincent Shih (2):
usb: host: ehci-sunplus: Add driver for ehci in Sunplus SP7021
dt-bindings: usb: Add bindings doc for Sunplus EHCI driver

.../bindings/usb/sunplus,sp7021-usb-ehci.yaml | 63 ++++++
MAINTAINERS | 7 +
drivers/usb/host/Kconfig | 12 +
drivers/usb/host/Makefile | 1 +
drivers/usb/host/ehci-sunplus.c | 241 +++++++++++++++++++++
5 files changed, 324 insertions(+)
create mode 100644 Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml
create mode 100644 drivers/usb/host/ehci-sunplus.c

--
2.7.4


2022-03-06 11:12:50

by Vincent Shih

[permalink] [raw]
Subject: [PATCH v3 1/2] usb: host: ehci-sunplus: Add driver for ehci in Sunplus SP7021

Add driver for ehci in Sunplus SP7021

Signed-off-by: Vincent Shih <[email protected]>
---
Changes in v2:
- Address the comments by Alan Stern.
- Address the comments by Rob Herring.
- Address the comments by Krzysztof Kozlowski.
- Implement phy binding.
- Clean up codes.

MAINTAINERS | 6 +
drivers/usb/host/Kconfig | 12 ++
drivers/usb/host/Makefile | 1 +
drivers/usb/host/ehci-sunplus.c | 241 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 260 insertions(+)
create mode 100644 drivers/usb/host/ehci-sunplus.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 80eebc1..6c2faf3 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17947,6 +17947,12 @@ L: [email protected]
S: Maintained
F: drivers/net/ethernet/dlink/sundance.c

+SUNPLUS USB EHCI DRIVER
+M: Vincent Shih <[email protected]>
+L: [email protected]
+S: Maintained
+F: drivers/usb/host/ehci-sunplus.c
+
SUPERH
M: Yoshinori Sato <[email protected]>
M: Rich Felker <[email protected]>
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index c4736d1..ad13fdd 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -339,6 +339,18 @@ config USB_OCTEON_EHCI
USB 2.0 device support. All CN6XXX based chips with USB are
supported.

+config USB_EHCI_SUNPLUS
+ tristate "EHCI support for Sunplus USB controller"
+ depends on SOC_SP7021
+ select NVMEM_SUNPLUS_OCOTP
+ select PHY_SUNPLUS_USB
+ help
+ Enables support for the on-chip EHCI controller in Sunplus
+ SoCs. It supports 32-bit AHB/AXI config bus and 64-bit AXI
+ data bus.
+ This driver can also be built as a module. If so, the module
+ will be called ehci-sunplus.
+
endif # USB_EHCI_HCD

config USB_OXU210HP_HCD
diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile
index 171de4d..7e9b5f1 100644
--- a/drivers/usb/host/Makefile
+++ b/drivers/usb/host/Makefile
@@ -46,6 +46,7 @@ obj-$(CONFIG_USB_EHCI_HCD_STI) += ehci-st.o
obj-$(CONFIG_USB_EHCI_EXYNOS) += ehci-exynos.o
obj-$(CONFIG_USB_EHCI_HCD_AT91) += ehci-atmel.o
obj-$(CONFIG_USB_EHCI_BRCMSTB) += ehci-brcm.o
+obj-$(CONFIG_USB_EHCI_SUNPLUS) += ehci-sunplus.o

obj-$(CONFIG_USB_OXU210HP_HCD) += oxu210hp-hcd.o
obj-$(CONFIG_USB_ISP116X_HCD) += isp116x-hcd.o
diff --git a/drivers/usb/host/ehci-sunplus.c b/drivers/usb/host/ehci-sunplus.c
new file mode 100644
index 0000000..48e270c
--- /dev/null
+++ b/drivers/usb/host/ehci-sunplus.c
@@ -0,0 +1,241 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * The EHCI driver for Sunplus SP7021
+ *
+ * Copyright (C) 2021 Sunplus Technology Inc., All rights reserved.
+ */
+
+#include <linux/clk.h>
+#include <linux/module.h>
+#include <linux/phy/phy.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+#include <linux/usb.h>
+#include <linux/usb/ehci_pdriver.h>
+#include <linux/usb/hcd.h>
+
+#include "ehci.h"
+
+#define hcd_to_sp_ehci_priv(h) \
+ ((struct sp_ehci_priv *)hcd_to_ehci(h)->priv)
+
+struct sp_ehci_priv {
+ struct clk *ehci_clk;
+ struct reset_control *ehci_rstc;
+ struct phy *phy;
+ u32 disc_vol_addr_off;
+};
+
+static struct usb_ehci_pdata usb_ehci_pdata = {
+ .has_tt = 1,
+ .has_synopsys_hc_bug = 1,
+ .big_endian_desc = 1,
+ .big_endian_mmio = 1,
+};
+
+static int ehci_sunplus_reset(struct usb_hcd *hcd)
+{
+ struct platform_device *pdev = to_platform_device(hcd->self.controller);
+ struct usb_ehci_pdata *pdata = pdev->dev.platform_data;
+ struct ehci_hcd *ehci = hcd_to_ehci(hcd);
+ int retval;
+
+ hcd->has_tt = pdata->has_tt;
+ ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
+ ehci->big_endian_desc = pdata->big_endian_desc;
+ ehci->big_endian_mmio = pdata->big_endian_mmio;
+ ehci->caps = hcd->regs;
+
+ retval = ehci_setup(hcd);
+
+ return retval;
+}
+
+static struct hc_driver __read_mostly ehci_sunplus_driver;
+
+static const struct ehci_driver_overrides ehci_sunplus_overrides __initconst = {
+ .reset = ehci_sunplus_reset,
+ .extra_priv_size = sizeof(struct sp_ehci_priv),
+};
+
+static int ehci_sunplus_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct sp_ehci_priv *sp_priv;
+ struct resource *res_mem;
+ struct usb_hcd *hcd;
+ int irq, ret;
+
+ if (usb_disabled())
+ return -ENODEV;
+
+ pdev->dev.platform_data = &usb_ehci_pdata;
+
+ /* initialize hcd */
+ hcd = usb_create_hcd(&ehci_sunplus_driver, dev, dev_name(dev));
+ if (!hcd)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, hcd);
+ sp_priv = hcd_to_sp_ehci_priv(hcd);
+
+ /* phy */
+ sp_priv->phy = devm_phy_get(dev, "uphy");
+ if (IS_ERR(sp_priv->phy)) {
+ ret = PTR_ERR(sp_priv->phy);
+ goto err_put_hcd;
+ }
+
+ ret = phy_init(sp_priv->phy);
+ if (ret)
+ goto err_put_hcd;
+
+ /* ehci */
+ res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ hcd->regs = devm_ioremap_resource(dev, res_mem);
+ if (IS_ERR(hcd->regs)) {
+ ret = PTR_ERR(hcd->regs);
+ goto err_put_hcd;
+ }
+
+ hcd->rsrc_start = res_mem->start;
+ hcd->rsrc_len = resource_size(res_mem);
+
+ irq = platform_get_irq(pdev, 0);
+ if (irq < 0) {
+ ret = irq;
+ goto err_put_hcd;
+ }
+ dev_dbg(&pdev->dev, "ehci_id:%d,irq:%d\n", pdev->id, irq);
+
+ sp_priv->ehci_clk = devm_clk_get(dev, NULL);
+
+ if (IS_ERR(sp_priv->ehci_clk)) {
+ ret = PTR_ERR(sp_priv->ehci_clk);
+ goto err_put_hcd;
+ }
+
+ sp_priv->ehci_rstc = devm_reset_control_get_exclusive(dev, NULL);
+ if (IS_ERR(sp_priv->ehci_rstc)) {
+ ret = PTR_ERR(sp_priv->ehci_rstc);
+ goto err_put_hcd;
+ }
+
+ ret = clk_prepare_enable(sp_priv->ehci_clk);
+ if (ret)
+ goto err_ehci_clk;
+
+ ret = reset_control_deassert(sp_priv->ehci_rstc);
+ if (ret)
+ goto err_ehci_reset;
+
+ ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
+ if (ret)
+ goto err_ehci_reset;
+
+ dev_dbg(dev, "hcd_irq:%d,%d\n", hcd->irq, irq);
+
+ return ret;
+
+err_ehci_reset:
+ reset_control_assert(sp_priv->ehci_rstc);
+err_ehci_clk:
+ clk_disable_unprepare(sp_priv->ehci_clk);
+err_put_hcd:
+ usb_put_hcd(hcd);
+
+ return ret;
+}
+
+static int ehci_sunplus_remove(struct platform_device *pdev)
+{
+ struct usb_hcd *hcd = platform_get_drvdata(pdev);
+ struct sp_ehci_priv *sp_priv = hcd_to_sp_ehci_priv(hcd);
+
+ usb_remove_hcd(hcd);
+ usb_put_hcd(hcd);
+
+ reset_control_assert(sp_priv->ehci_rstc);
+ clk_disable_unprepare(sp_priv->ehci_clk);
+
+ return 0;
+}
+
+#ifdef CONFIG_PM
+static int ehci_sunplus_drv_suspend(struct device *dev)
+{
+ struct usb_hcd *hcd = dev_get_drvdata(dev);
+ struct sp_ehci_priv *sp_priv = hcd_to_sp_ehci_priv(hcd);
+ bool do_wakeup = device_may_wakeup(dev);
+ int rc;
+
+ rc = ehci_suspend(hcd, do_wakeup);
+ if (rc)
+ return rc;
+
+ reset_control_assert(sp_priv->ehci_rstc);
+ clk_disable_unprepare(sp_priv->ehci_clk);
+
+ return 0;
+}
+
+static int ehci_sunplus_drv_resume(struct device *dev)
+{
+ struct usb_hcd *hcd = dev_get_drvdata(dev);
+ struct sp_ehci_priv *sp_priv = hcd_to_sp_ehci_priv(hcd);
+
+ clk_prepare_enable(sp_priv->ehci_clk);
+ reset_control_deassert(sp_priv->ehci_rstc);
+
+ ehci_resume(hcd, false);
+
+ return 0;
+}
+
+static const struct dev_pm_ops ehci_sunplus_pm_ops = {
+ .suspend = ehci_sunplus_drv_suspend,
+ .resume = ehci_sunplus_drv_resume,
+};
+#endif
+
+static const struct of_device_id ehci_sunplus_dt_ids[] = {
+ { .compatible = "sunplus,sp7021-usb-ehci" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, ehci_sunplus_dt_ids);
+
+static struct platform_driver ehci_hcd_sunplus_driver = {
+ .probe = ehci_sunplus_probe,
+ .remove = ehci_sunplus_remove,
+ .shutdown = usb_hcd_platform_shutdown,
+ .driver = {
+ .name = "ehci-sunplus",
+ .of_match_table = ehci_sunplus_dt_ids,
+#ifdef CONFIG_PM
+ .pm = &ehci_sunplus_pm_ops,
+#endif
+ }
+};
+
+static int __init ehci_sunplus_init(void)
+{
+ if (usb_disabled())
+ return -ENODEV;
+
+ ehci_init_driver(&ehci_sunplus_driver, &ehci_sunplus_overrides);
+ return platform_driver_register(&ehci_hcd_sunplus_driver);
+}
+module_init(ehci_sunplus_init);
+
+static void __exit ehci_sunplus_cleanup(void)
+{
+ platform_driver_unregister(&ehci_hcd_sunplus_driver);
+}
+module_exit(ehci_sunplus_cleanup);
+
+MODULE_AUTHOR("Vincent Shih <[email protected]>");
+MODULE_DESCRIPTION("Sunplus USB EHCI driver");
+MODULE_LICENSE("GPL");
+
--
2.7.4

2022-03-06 13:30:03

by Vincent Shih

[permalink] [raw]
Subject: [PATCH v3 2/2] dt-bindings: usb: Add bindings doc for Sunplus EHCI driver

Add bindings doc for Sunplus EHCI driver

Signed-off-by: Vincent Shih <[email protected]>
---
Changes in v2:
- Address the comments by Rob Herring.

.../bindings/usb/sunplus,sp7021-usb-ehci.yaml | 63 ++++++++++++++++++++++
MAINTAINERS | 1 +
2 files changed, 64 insertions(+)
create mode 100644 Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml

diff --git a/Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml b/Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml
new file mode 100644
index 0000000..905f68c
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml
@@ -0,0 +1,63 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+# Copyright (C) Sunplus Co., Ltd. 2021
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/usb/sunplus,sp7021-usb-ehci.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Sunplus SP7021 EHCI Controller Device Tree bindings
+
+allOf:
+ - $ref: usb-hcd.yaml#
+
+maintainers:
+ - Vincent Shih <[email protected]>
+
+properties:
+ compatible:
+ const: sunplus,sp7021-usb2-ehci
+
+ clocks:
+ maxItems: 1
+
+ resets:
+ minItems: 1
+
+ reg:
+ maxItems: 1
+
+ interrupts:
+ maxItems: 1
+
+ phys:
+ maxItems: 1
+
+ phy-names:
+ maxItems: 1
+
+required:
+ - compatible
+ - clocks
+ - resets
+ - reg
+ - interrupts
+ - phys
+ - phy-names
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/interrupt-controller/irq.h>
+
+ sp_ehci0: usb@9c102100 {
+ compatible = "sunplus,sp7021-usb-ehci";
+ clocks = <&clkc 0x3a>;
+ resets = <&rstc 0x2a>;
+ reg = <0x9c102100 0x68>;
+ interrupt-parent = <&intc>;
+ interrupts = <14 IRQ_TYPE_LEVEL_HIGH>;
+ phys = <&sp_uphy0>;
+ phy-names = "uphy";
+ };
+ ...
diff --git a/MAINTAINERS b/MAINTAINERS
index 6c2faf3..4e66993 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17951,6 +17951,7 @@ SUNPLUS USB EHCI DRIVER
M: Vincent Shih <[email protected]>
L: [email protected]
S: Maintained
+F: Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml
F: drivers/usb/host/ehci-sunplus.c

SUPERH
--
2.7.4

2022-03-06 17:45:51

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] dt-bindings: usb: Add bindings doc for Sunplus EHCI driver

On 06/03/2022 07:10, Vincent Shih wrote:
> Add bindings doc for Sunplus EHCI driver
>
> Signed-off-by: Vincent Shih <[email protected]>
> ---
> Changes in v2:
> - Address the comments by Rob Herring.
>
> .../bindings/usb/sunplus,sp7021-usb-ehci.yaml | 63 ++++++++++++++++++++++
> MAINTAINERS | 1 +
> 2 files changed, 64 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml
>
> diff --git a/Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml b/Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml
> new file mode 100644
> index 0000000..905f68c
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml
> @@ -0,0 +1,63 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +# Copyright (C) Sunplus Co., Ltd. 2021
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/usb/sunplus,sp7021-usb-ehci.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Sunplus SP7021 EHCI Controller Device Tree bindings

s/Device Tree bindings//
(title describes hardware)

> +
> +allOf:
> + - $ref: usb-hcd.yaml#
> +
> +maintainers:
> + - Vincent Shih <[email protected]>
> +
> +properties:
> + compatible:
> + const: sunplus,sp7021-usb2-ehci

You do not use this compatible in the driver.

> +
> + clocks:
> + maxItems: 1
> +
> + resets:
> + minItems: 1

Suddenly it became minItems. This looks odd, it should be maxItems: 1
rather. How many reset lines do you have there?


> +
> + reg:
> + maxItems: 1
> +
> + interrupts:
> + maxItems: 1
> +
> + phys:
> + maxItems: 1
> +
> + phy-names:
> + maxItems: 1

phy-names needs list of names.

> +
> +required:
> + - compatible
> + - clocks
> + - resets
> + - reg
> + - interrupts
> + - phys
> + - phy-names
> +
> +additionalProperties: false

This should be unevaluatedProperties:false, because you reference
usb-hcd schema.

> +
> +examples:
> + - |
> + #include <dt-bindings/interrupt-controller/irq.h>
> +
> + sp_ehci0: usb@9c102100 {
> + compatible = "sunplus,sp7021-usb-ehci";
> + clocks = <&clkc 0x3a>;
> + resets = <&rstc 0x2a>;
> + reg = <0x9c102100 0x68>;

reg goes after compatible in DTS.

Best regards,
Krzysztof

2022-03-07 03:39:42

by Rob Herring (Arm)

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] dt-bindings: usb: Add bindings doc for Sunplus EHCI driver

On Sun, 06 Mar 2022 14:10:36 +0800, Vincent Shih wrote:
> Add bindings doc for Sunplus EHCI driver
>
> Signed-off-by: Vincent Shih <[email protected]>
> ---
> Changes in v2:
> - Address the comments by Rob Herring.
>
> .../bindings/usb/sunplus,sp7021-usb-ehci.yaml | 63 ++++++++++++++++++++++
> MAINTAINERS | 1 +
> 2 files changed, 64 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.yaml
>

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check'
on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Error: Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.example.dts:37.5-6 syntax error
FATAL ERROR: Unable to parse input tree
make[1]: *** [scripts/Makefile.lib:378: Documentation/devicetree/bindings/usb/sunplus,sp7021-usb-ehci.example.dt.yaml] Error 1
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:1398: dt_binding_check] Error 2

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/patch/1601662

This check can fail if there are any dependencies. The base for a patch
series is generally the most recent rc1.

If you already ran 'make dt_binding_check' and didn't see the above
error(s), then make sure 'yamllint' is installed and dt-schema is up to
date:

pip3 install dtschema --upgrade

Please check and re-submit.