[PATCH 04/31] usb: usbssp: Added USBSSP platform driver

From: Pawel Laszczak
Date: Thu Jul 19 2018 - 14:03:18 EST


This patch adds platform driver that is entry point for loading and
unloading usbssp.ko modules.
It also adds information about this driver to drivers/usb/Kconfig
and drivers/usb/Makefile files and create Kconfig and Makefile
files in drivers/usb/usbssp directory.

Patch also adds template for some function ivokked from
usbssp_plat.c file. These function will be implemented in next patches.

This patch also introduce usbssp_trb_virt_to_dma that converts
virtual address of TRB's to DMA address. In this moment this
function is used only in gadget-trace.h.

>From this moment the driver can be compiled.

Signed-off-by: Pawel Laszczak <pawell@xxxxxxxxxxx>
---
drivers/usb/Kconfig | 2 +
drivers/usb/Makefile | 2 +
drivers/usb/usbssp/Kconfig | 21 ++++
drivers/usb/usbssp/Makefile | 11 ++
drivers/usb/usbssp/gadget-ring.c | 48 ++++++++
drivers/usb/usbssp/gadget.c | 64 +++++++++++
drivers/usb/usbssp/gadget.h | 16 ++-
drivers/usb/usbssp/usbssp-plat.c | 186 +++++++++++++++++++++++++++++++
8 files changed, 349 insertions(+), 1 deletion(-)
create mode 100644 drivers/usb/usbssp/Kconfig
create mode 100644 drivers/usb/usbssp/Makefile
create mode 100644 drivers/usb/usbssp/gadget-ring.c
create mode 100644 drivers/usb/usbssp/gadget.c
create mode 100644 drivers/usb/usbssp/usbssp-plat.c

diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
index f699abab1787..dc05f384c34c 100644
--- a/drivers/usb/Kconfig
+++ b/drivers/usb/Kconfig
@@ -110,6 +110,8 @@ source "drivers/usb/mtu3/Kconfig"

source "drivers/usb/musb/Kconfig"

+source "drivers/usb/usbssp/Kconfig"
+
source "drivers/usb/dwc3/Kconfig"

source "drivers/usb/dwc2/Kconfig"
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 060643a1b5c8..b1cd5f83d440 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -8,6 +8,8 @@
obj-$(CONFIG_USB) += core/
obj-$(CONFIG_USB_SUPPORT) += phy/

+obj-$(CONFIG_USB_USBSSP) += usbssp/
+
obj-$(CONFIG_USB_DWC3) += dwc3/
obj-$(CONFIG_USB_DWC2) += dwc2/
obj-$(CONFIG_USB_ISP1760) += isp1760/
diff --git a/drivers/usb/usbssp/Kconfig b/drivers/usb/usbssp/Kconfig
new file mode 100644
index 000000000000..ee20b01753dc
--- /dev/null
+++ b/drivers/usb/usbssp/Kconfig
@@ -0,0 +1,21 @@
+config USB_USBSSP
+ tristate "Cadence USBSSP DRD Controller"
+ depends on (USB || USB_GADGET) && HAS_DMA
+ select USB_USBSSP_GADGET
+ help
+ Say Y here if your system has a cadence USBSSP dual-role controller.
+ It supports: dual-role switch Host-only, and Peripheral-only.
+
+ If you choose to build this driver is a dynamically linked
+ module, the module will be called usbssp.ko.
+
+if USB_USBSSP
+
+config USB_USBSSP_GADGET
+ tristate "Gadget only mode"
+ default USB_USBSSP
+ depends on USB_GADGET=y || USB_GADGET=USB_USBSSP
+ help
+ Select this when you want to use USBSSP in gadget mode only,
+endif
+
diff --git a/drivers/usb/usbssp/Makefile b/drivers/usb/usbssp/Makefile
new file mode 100644
index 000000000000..d85f15afb51c
--- /dev/null
+++ b/drivers/usb/usbssp/Makefile
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0
+# define_trace.h needs to know how to find our header
+CFLAGS_gadget-trace.o := -I$(src)
+
+obj-$(CONFIG_USB_USBSSP_GADGET) += usbssp.o
+usbssp-y := usbssp-plat.o gadget-ring.o \
+ gadget.o
+
+ifneq ($(CONFIG_TRACING),)
+ usbssp-y += gadget-trace.o
+endif
diff --git a/drivers/usb/usbssp/gadget-ring.c b/drivers/usb/usbssp/gadget-ring.c
new file mode 100644
index 000000000000..d1da59306d02
--- /dev/null
+++ b/drivers/usb/usbssp/gadget-ring.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * USBSSP device controller driver
+ *
+ * Copyright (C) 2018 Cadence.
+ *
+ * Author: Pawel Laszczak
+ *
+ * A lot of code based on Linux XHCI driver.
+ * Origin: Copyright (C) 2008 Intel Corp
+ */
+
+#include <linux/scatterlist.h>
+#include <linux/slab.h>
+#include <linux/dma-mapping.h>
+#include <linux/irq.h>
+#include "gadget-trace.h"
+#include "gadget.h"
+
+/*
+ * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
+ * address of the TRB.
+ */
+dma_addr_t usbssp_trb_virt_to_dma(struct usbssp_segment *seg,
+ union usbssp_trb *trb)
+{
+ unsigned long segment_offset;
+
+ if (!seg || !trb || trb < seg->trbs)
+ return 0;
+ /* offset in TRBs */
+ segment_offset = trb - seg->trbs;
+ if (segment_offset >= TRBS_PER_SEGMENT)
+ return 0;
+ return seg->dma + (segment_offset * sizeof(*trb));
+}
+
+irqreturn_t usbssp_irq(int irq, void *priv)
+{
+ struct usbssp_udc *usbssp_data = (struct usbssp_udc *)priv;
+ irqreturn_t ret = IRQ_NONE;
+ unsigned long flags;
+
+ spin_lock_irqsave(&usbssp_data->lock, flags);
+
+ spin_unlock_irqrestore(&usbssp_data->lock, flags);
+ return ret;
+}
diff --git a/drivers/usb/usbssp/gadget.c b/drivers/usb/usbssp/gadget.c
new file mode 100644
index 000000000000..2f60d7dd1fe4
--- /dev/null
+++ b/drivers/usb/usbssp/gadget.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * USBSSP device controller driver
+ *
+ * Copyright (C) 2018 Cadence.
+ *
+ * Author: Pawel Laszczak
+ *
+ * A lot of code based on Linux XHCI driver.
+ * Origin: Copyright (C) 2008 Intel Corp
+ */
+
+#include <linux/pci.h>
+#include <linux/irq.h>
+#include <linux/log2.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/slab.h>
+#include <linux/dmi.h>
+#include <linux/dma-mapping.h>
+#include <linux/delay.h>
+
+#include "gadget-trace.h"
+#include "gadget.h"
+
+#ifdef CONFIG_PM
+/*
+ * Stop DC (not bus-specific)
+ *
+ * This is called when the machine transition into S3/S4 mode.
+ *
+ */
+int usbssp_suspend(struct usbssp_udc *usbssp_data, bool do_wakeup)
+{
+ /*TODO*/
+ return -ENOSYS;
+}
+
+/*
+ * start DC (not bus-specific)
+ *
+ * This is called when the machine transition from S3/S4 mode.
+ *
+ */
+int usbssp_resume(struct usbssp_udc *usbssp_data, bool hibernated)
+{
+ /*TODO*/
+ return -ENOSYS;
+}
+
+#endif /* CONFIG_PM */
+
+int usbssp_gadget_init(struct usbssp_udc *usbssp_data)
+{
+ int ret;
+ return ret;
+}
+
+int usbssp_gadget_exit(struct usbssp_udc *usbssp_data)
+{
+ int ret = 0;
+
+ return ret;
+}
diff --git a/drivers/usb/usbssp/gadget.h b/drivers/usb/usbssp/gadget.h
index b5c17603af78..55e20795d900 100644
--- a/drivers/usb/usbssp/gadget.h
+++ b/drivers/usb/usbssp/gadget.h
@@ -9,7 +9,6 @@
* A lot of code based on Linux XHCI driver.
* Origin: Copyright (C) 2008 Intel Corp.
*/
-
#ifndef __LINUX_USBSSP_GADGET_H
#define __LINUX_USBSSP_GADGET_H

@@ -1676,6 +1675,21 @@ static inline void usbssp_write_64(struct usbssp_udc *usbssp_data,
{
lo_hi_writeq(val, regs);
}
+
+/* USBSSP Device controller glue */
+int usbssp_suspend(struct usbssp_udc *usbssp_data, bool do_wakeup);
+int usbssp_resume(struct usbssp_udc *usbssp_data, bool hibernated);
+
+irqreturn_t usbssp_irq(int irq, void *priv);
+
+/* USBSSP ring, segment, TRB, and TD functions */
+dma_addr_t usbssp_trb_virt_to_dma(struct usbssp_segment *seg,
+ union usbssp_trb *trb);
+
+/* USBSSP gadget interface*/
+int usbssp_gadget_init(struct usbssp_udc *usbssp_data);
+int usbssp_gadget_exit(struct usbssp_udc *usbssp_data);
+
static inline char *usbssp_slot_state_string(u32 state)
{
switch (state) {
diff --git a/drivers/usb/usbssp/usbssp-plat.c b/drivers/usb/usbssp/usbssp-plat.c
new file mode 100644
index 000000000000..c048044148aa
--- /dev/null
+++ b/drivers/usb/usbssp/usbssp-plat.c
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * USBSSP device controller driver
+ *
+ * Copyright (C) 2018 Cadence.
+ *
+ * Author: Pawel Laszczak
+ *
+ */
+
+#include <linux/clk.h>
+#include <linux/dma-mapping.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/usb/phy.h>
+#include <linux/slab.h>
+#include <linux/acpi.h>
+
+#include "gadget.h"
+
+#define DRIVER_AUTHOR "Pawel Laszczak"
+#define DRIVER_DESC "USBSSP Device Controller (USBSSP) Driver"
+
+#ifdef CONFIG_OF
+
+static const struct of_device_id usbssp_dev_of_match[] = {
+ {
+ .compatible = "Cadence, usbssp-dev",
+ },
+ {},
+};
+MODULE_DEVICE_TABLE(of, usbssp_dev_of_match);
+#endif
+
+int usbssp_is_platform(void)
+{
+ return 1;
+}
+
+static int usbssp_plat_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct resource *res;
+ struct usbssp_udc *usbssp_data;
+ int ret = 0;
+ int irq;
+ struct device *sysdev;
+
+ irq = platform_get_irq(pdev, 0);
+ if (irq < 0) {
+ dev_err(&pdev->dev, "Incorrect IRQ number\n");
+ return -ENODEV;
+ }
+
+ usbssp_data = devm_kzalloc(dev, sizeof(*usbssp_data), GFP_KERNEL);
+ if (!usbssp_data)
+ return -ENOMEM;
+
+ for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
+ if (is_of_node(sysdev->fwnode) ||
+ is_acpi_device_node(sysdev->fwnode))
+ break;
+#ifdef CONFIG_PCI
+ else if (sysdev->bus == &pci_bus_type)
+ break;
+#endif
+ }
+
+ if (!sysdev)
+ sysdev = &pdev->dev;
+
+ /* Try to set 64-bit DMA first */
+ if (WARN_ON(!dev->dma_mask))
+ /* Platform did not initialize dma_mask */
+ ret = dma_coerce_mask_and_coherent(dev,
+ DMA_BIT_MASK(64));
+ else
+ ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
+
+ /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
+ if (ret) {
+ ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+ if (ret)
+ return ret;
+ }
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ usbssp_data->regs = devm_ioremap_resource(dev, res);
+
+ if (IS_ERR(usbssp_data->regs)) {
+ ret = PTR_ERR(usbssp_data->regs);
+ return ret;
+ }
+
+ usbssp_data->rsrc_start = res->start;
+ usbssp_data->rsrc_len = resource_size(res);
+
+ ret = devm_request_irq(dev, irq, usbssp_irq, IRQF_SHARED,
+ dev_name(dev), usbssp_data);
+
+ if (ret < 0)
+ return ret;
+
+ usbssp_data->irq = irq;
+ usbssp_data->dev = dev;
+ platform_set_drvdata(pdev, usbssp_data);
+ ret = usbssp_gadget_init(usbssp_data);
+
+ return ret;
+}
+
+static int usbssp_plat_remove(struct platform_device *pdev)
+{
+ int ret = 0;
+ struct usbssp_udc *usbssp_data;
+
+ usbssp_data = (struct usbssp_udc *)platform_get_drvdata(pdev);
+ ret = usbssp_gadget_exit(usbssp_data);
+ return ret;
+
+}
+
+static int __maybe_unused usbssp_plat_suspend(struct device *dev)
+{
+ struct usbssp_udc *usbssp_data = dev_get_drvdata(dev);
+
+ return usbssp_suspend(usbssp_data, device_may_wakeup(dev));
+}
+
+static int __maybe_unused usbssp_plat_resume(struct device *dev)
+{
+ struct usbssp_udc *usbssp_data = dev_get_drvdata(dev);
+
+ return usbssp_resume(usbssp_data, 0);
+}
+
+static int __maybe_unused usbssp_plat_runtime_suspend(struct device *dev)
+{
+ struct usbssp_udc *usbssp_data = dev_get_drvdata(dev);
+
+ return usbssp_suspend(usbssp_data, true);
+}
+
+static int __maybe_unused usbssp_plat_runtime_resume(struct device *dev)
+{
+ struct usbssp_udc *usbssp_data = dev_get_drvdata(dev);
+
+ return usbssp_resume(usbssp_data, 0);
+}
+
+static const struct dev_pm_ops usbssp_plat_pm_ops = {
+ SET_SYSTEM_SLEEP_PM_OPS(usbssp_plat_suspend, usbssp_plat_resume)
+
+ SET_RUNTIME_PM_OPS(usbssp_plat_runtime_suspend,
+ usbssp_plat_runtime_resume,
+ NULL)
+};
+
+static struct platform_driver usbssp_driver = {
+ .probe = usbssp_plat_probe,
+ .remove = usbssp_plat_remove,
+ .driver = {
+ .name = "usbssp-dev",
+ .pm = &usbssp_plat_pm_ops,
+ .of_match_table = of_match_ptr(usbssp_dev_of_match),
+ },
+};
+
+static int __init usbssp_plat_init(void)
+{
+ return platform_driver_register(&usbssp_driver);
+}
+module_init(usbssp_plat_init);
+
+static void __exit usbssp_plat_exit(void)
+{
+ platform_driver_unregister(&usbssp_driver);
+}
+module_exit(usbssp_plat_exit);
+
+MODULE_ALIAS("platform:usbss-gadget");
+MODULE_DESCRIPTION("USBSSP' Device Controller (USBSSP) Driver");
+MODULE_LICENSE("GPL v2");
--
2.17.1