[PATCH 31/31] usb: usbssp: add pci to platform driver wrapper.

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


Patch adds PCI specific glue drivier that creaties and registers in
system usbssp platform device. Thanks to this we can use
the existing usbssp platform driver for usbssp controller
build on PCI board

Signed-off-by: Pawel Laszczak <pawell@xxxxxxxxxxx>
---
drivers/usb/usbssp/Kconfig | 10 +-
drivers/usb/usbssp/Makefile | 3 +
drivers/usb/usbssp/usbssp-pci-wrap.c | 226 +++++++++++++++++++++++++++
3 files changed, 238 insertions(+), 1 deletion(-)
create mode 100644 drivers/usb/usbssp/usbssp-pci-wrap.c

diff --git a/drivers/usb/usbssp/Kconfig b/drivers/usb/usbssp/Kconfig
index ee20b01753dc..bada82e5d223 100644
--- a/drivers/usb/usbssp/Kconfig
+++ b/drivers/usb/usbssp/Kconfig
@@ -17,5 +17,13 @@ config USB_USBSSP_GADGET
depends on USB_GADGET=y || USB_GADGET=USB_USBSSP
help
Select this when you want to use USBSSP in gadget mode only,
-endif

+config USB_USBSSP_PCI
+ tristate "PCIe-based Platforms"
+ depends on USB_PCI && ACPI
+ default USB_USBSSP
+ help
+ If you're using the USBSSP Core IP with a PCIe, please say
+ 'Y' or 'M' here.
+
+endif
diff --git a/drivers/usb/usbssp/Makefile b/drivers/usb/usbssp/Makefile
index b267fadcb104..fbf5d55500a4 100644
--- a/drivers/usb/usbssp/Makefile
+++ b/drivers/usb/usbssp/Makefile
@@ -10,3 +10,6 @@ usbssp-y := usbssp-plat.o gadget-ring.o \
ifneq ($(CONFIG_TRACING),)
usbssp-y += gadget-trace.o
endif
+
+obj-$(CONFIG_USB_USBSSP_PCI) += usbssp-pci.o
+usbssp-pci-y := usbssp-pci-wrap.o
diff --git a/drivers/usb/usbssp/usbssp-pci-wrap.c b/drivers/usb/usbssp/usbssp-pci-wrap.c
new file mode 100644
index 000000000000..10c0189c61f7
--- /dev/null
+++ b/drivers/usb/usbssp/usbssp-pci-wrap.c
@@ -0,0 +1,226 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * USBSSP device controller driver - PCIe wrapper
+ *
+ * Copyright (C) 2018 Cadence.
+ *
+ * Author: Pawel Laszczak
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/platform_device.h>
+#include <linux/dma-mapping.h>
+#include <linux/slab.h>
+
+struct usbssp_pci {
+ struct platform_device *plat_cdns;
+ struct pci_dev *otg;
+ struct pci_dev *hg_dev;
+ struct resource res[5];
+};
+
+struct usbssp_pci usbssp;
+
+/**
+ * usbssp_pci_probe - Probe function for Cadence USB wrapper driver
+ * @pdev: platform device object
+ * @id: pci device id
+ *
+ * Returns 0 on success otherwise negative errno
+ */
+static int usbssp_pci_probe(struct pci_dev *pdev,
+ const struct pci_device_id *id)
+{
+ int rv = 0;
+ struct platform_device *usbssp_plat;
+
+ if (pdev->devfn > 1)
+ return 0;
+
+ if (!id)
+ return -EINVAL;
+
+ if (pcim_enable_device(pdev) < 0) {
+ dev_err(&pdev->dev, "failed to enable PCI device\n");
+ return -ENOMEM;
+ }
+
+ pci_set_master(pdev);
+
+ /*
+ * function 0: device(BAR_2) + host(BAR_0)
+ * function 1: otg(BAR_0))cdns3_remove_roles
+ */
+ if (pdev->devfn == 0 || pdev->devfn == 1) {
+ if (!usbssp.plat_cdns) {
+ usbssp_plat = platform_device_alloc("usbssp-dev",
+ PLATFORM_DEVID_AUTO);
+ memset(usbssp.res, 0x00, sizeof(struct resource) *
+ ARRAY_SIZE(usbssp.res));
+ usbssp.plat_cdns = usbssp_plat;
+
+ if (!usbssp_plat->dev.dma_mask) {
+ dma_coerce_mask_and_coherent(&usbssp_plat->dev,
+ DMA_BIT_MASK(32));
+ }
+ }
+ } else
+ return -EINVAL;
+
+ /* for GADGET device function number is 0 */
+ if (pdev->devfn == 0) {
+ usbssp.hg_dev = pdev;
+ //device resource
+ usbssp.res[0].start = pci_resource_start(pdev, 2);
+ usbssp.res[0].end = pci_resource_end(pdev, 2);
+ usbssp.res[0].name = "usbssp-dev-regs";
+ usbssp.res[0].flags = IORESOURCE_MEM;
+
+ /* Interrupt resource*/
+ usbssp.res[1].start = pdev->irq;
+ usbssp.res[1].name = "usbssp-dev-irq";
+ usbssp.res[1].flags = IORESOURCE_IRQ;
+ } else if (pdev->devfn == 1) {
+ usbssp.otg = pdev;
+ //OTG
+ usbssp.res[2].start = pci_resource_start(pdev, 0);
+ usbssp.res[2].end = pci_resource_end(pdev, 0);
+ usbssp.res[2].name = "otg";
+ usbssp.res[2].flags = IORESOURCE_MEM;
+
+ usbssp.res[3].start = pci_resource_start(pdev, 1);
+ usbssp.res[3].end = pci_resource_end(pdev, 1);
+ usbssp.res[3].name = "debug1";
+ usbssp.res[3].flags = IORESOURCE_MEM;
+
+ usbssp.res[4].start = pci_resource_start(pdev, 2);
+ usbssp.res[4].end = pci_resource_end(pdev, 2);
+ usbssp.res[4].name = "debug2";
+ usbssp.res[4].flags = IORESOURCE_MEM;
+ } else {
+ return -EIO;
+ }
+
+ if (usbssp.otg && usbssp.hg_dev) {
+
+ struct pci_dev *pci_hg = usbssp.hg_dev;
+ struct platform_device *plat_dev = usbssp.plat_cdns;
+
+ rv = platform_device_add_resources(plat_dev, usbssp.res,
+ ARRAY_SIZE(usbssp.res));
+ if (rv) {
+ dev_err(&plat_dev->dev,
+ "couldn't add resources to cdns device\n");
+ return rv;
+ }
+
+ rv = platform_device_add(plat_dev);
+
+ if (rv) {
+ dev_err(&pci_hg->dev,
+ "failed to register cdns device\n");
+ platform_device_put(plat_dev);
+ return rv;
+ }
+ }
+
+ pci_set_drvdata(pdev, &usbssp);
+
+ return rv;
+}
+
+void usbssp_pci_remove(struct pci_dev *pdev)
+{
+ struct usbssp_pci *usbssp = pci_get_drvdata(pdev);
+
+ if (pdev->devfn > 1)
+ return;
+
+ if (pdev->devfn == 1)
+ usbssp->otg = NULL;
+ else if (pdev->devfn == 0)
+ usbssp->hg_dev = NULL;
+
+ if (!usbssp->hg_dev && !usbssp->otg)
+ platform_device_unregister(usbssp->plat_cdns);
+}
+
+#if CONFIG_PM || CONFIG_PM_SLEEP
+static int usbssp_generic_suspend(struct usbssp_pci *usbssp, int param)
+{
+ /*TODO*/
+ return -ENOSYS;
+}
+
+static int usbssp_generic_resume(struct usbssp_pci *usbssp, int param)
+{
+ /*TODO*/
+ return -ENOSYS;
+}
+
+#endif /*CONFIG_PM || CONFIG_PM_SLEEP*/
+
+#ifdef CONFIG_PM
+static int usbssp_runtime_suspend(struct device *dev)
+{
+ struct usbssp_pci *usbssp = dev_get_drvdata(dev);
+
+ return usbssp_generic_suspend(usbssp, 0);
+}
+
+static int usbssp_runtime_resume(struct device *dev)
+{
+ struct usbssp_pci *usbssp = dev_get_drvdata(dev);
+
+ return usbssp_generic_resume(usbssp, 0);
+}
+#endif /* CONFIG_PM */
+
+#ifdef CONFIG_PM_SLEEP
+static int usbssp_pci_suspend(struct device *dev)
+{
+ struct usbssp_pci *usbssp = dev_get_drvdata(dev);
+
+ return usbssp_generic_suspend(usbssp, 0);
+}
+
+static int usbssp_pci_resume(struct device *dev)
+{
+ struct usbssp_pci *usbssp = dev_get_drvdata(dev);
+
+ return usbssp_generic_resume(usbssp, 0);
+}
+#endif /* CONFIG_PM_SLEEP */
+
+static const struct dev_pm_ops usbssp_pci_dev_pm_ops = {
+ SET_SYSTEM_SLEEP_PM_OPS(usbssp_runtime_suspend, usbssp_runtime_resume)
+ SET_RUNTIME_PM_OPS(usbssp_pci_suspend, usbssp_pci_resume, NULL)
+};
+
+#define PCI_VENDOR_ID_CDZ 0x17CD
+static const struct pci_device_id usbssp_pci_ids[] = {
+ {PCI_VDEVICE(CDZ, 0x0100), },
+ { /* terminate list */}
+};
+
+MODULE_DEVICE_TABLE(pci, usbssp_pci_ids);
+
+static struct pci_driver usbssp_pci_driver = {
+ .name = "usbssp-pci",
+ .id_table = &usbssp_pci_ids[0],
+ .probe = usbssp_pci_probe,
+ .remove = usbssp_pci_remove,
+ .driver = {
+ .pm = &usbssp_pci_dev_pm_ops,
+ }
+};
+
+MODULE_AUTHOR("Pawel Laszczak <pawell@xxxxxxxxxxx>");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Cadence USBSSP PCI Glue Layer");
+
+module_pci_driver(usbssp_pci_driver);
+
--
2.17.1