1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence PCI Glue driver.
5 * Copyright (C) 2019 Cadence.
11 #include <linux/platform_device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/pci.h>
19 #include "gadget-export.h"
21 #define PCI_BAR_HOST 0
25 #define PCI_DEV_FN_HOST_DEVICE 0
26 #define PCI_DEV_FN_OTG 1
28 #define PCI_DRIVER_NAME "cdns-pci-usbssp"
29 #define PLAT_DRIVER_NAME "cdns-usbssp"
31 #define CDNS_VENDOR_ID 0x17cd
32 #define CDNS_DEVICE_ID 0x0200
33 #define CDNS_DRD_ID 0x0100
34 #define CDNS_DRD_IF (PCI_CLASS_SERIAL_USB << 8 | 0x80)
36 static struct pci_dev *cdnsp_get_second_fun(struct pci_dev *pdev)
39 * Gets the second function.
40 * Platform has two function. The fist keeps resources for
41 * Host/Device while the secon keeps resources for DRD/OTG.
43 if (pdev->device == CDNS_DEVICE_ID)
44 return pci_get_device(pdev->vendor, CDNS_DRD_ID, NULL);
45 else if (pdev->device == CDNS_DRD_ID)
46 return pci_get_device(pdev->vendor, CDNS_DEVICE_ID, NULL);
51 static int cdnsp_pci_probe(struct pci_dev *pdev,
52 const struct pci_device_id *id)
54 struct device *dev = &pdev->dev;
61 * For GADGET/HOST PCI (devfn) function number is 0,
62 * for OTG PCI (devfn) function number is 1.
64 if (!id || (pdev->devfn != PCI_DEV_FN_HOST_DEVICE &&
65 pdev->devfn != PCI_DEV_FN_OTG))
68 func = cdnsp_get_second_fun(pdev);
72 if (func->class == PCI_CLASS_SERIAL_USB_XHCI ||
73 pdev->class == PCI_CLASS_SERIAL_USB_XHCI) {
78 ret = pcim_enable_device(pdev);
80 dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", ret);
85 if (pci_is_enabled(func)) {
86 cdnsp = pci_get_drvdata(func);
88 cdnsp = kzalloc(sizeof(*cdnsp), GFP_KERNEL);
95 /* For GADGET device function number is 0. */
96 if (pdev->devfn == 0) {
97 resource_size_t rsrc_start, rsrc_len;
99 /* Function 0: host(BAR_0) + device(BAR_1).*/
100 dev_dbg(dev, "Initialize resources\n");
101 rsrc_start = pci_resource_start(pdev, PCI_BAR_DEV);
102 rsrc_len = pci_resource_len(pdev, PCI_BAR_DEV);
103 res = devm_request_mem_region(dev, rsrc_start, rsrc_len, "dev");
105 dev_dbg(dev, "controller already in use\n");
110 cdnsp->dev_regs = devm_ioremap(dev, rsrc_start, rsrc_len);
111 if (!cdnsp->dev_regs) {
112 dev_dbg(dev, "error mapping memory\n");
117 cdnsp->dev_irq = pdev->irq;
118 dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n",
121 res = &cdnsp->xhci_res[0];
122 res->start = pci_resource_start(pdev, PCI_BAR_HOST);
123 res->end = pci_resource_end(pdev, PCI_BAR_HOST);
125 res->flags = IORESOURCE_MEM;
126 dev_dbg(dev, "USBSS-XHCI physical base addr: %pa\n",
129 /* Interrupt for XHCI, */
130 res = &cdnsp->xhci_res[1];
131 res->start = pdev->irq;
133 res->flags = IORESOURCE_IRQ;
135 res = &cdnsp->otg_res;
136 res->start = pci_resource_start(pdev, PCI_BAR_OTG);
137 res->end = pci_resource_end(pdev, PCI_BAR_OTG);
139 res->flags = IORESOURCE_MEM;
140 dev_dbg(dev, "CDNSP-DRD physical base addr: %pa\n",
143 /* Interrupt for OTG/DRD. */
144 cdnsp->otg_irq = pdev->irq;
147 if (pci_is_enabled(func)) {
149 cdnsp->gadget_init = cdnsp_gadget_init;
151 ret = cdns_init(cdnsp);
156 pci_set_drvdata(pdev, cdnsp);
158 device_wakeup_enable(&pdev->dev);
159 if (pci_dev_run_wake(pdev))
160 pm_runtime_put_noidle(&pdev->dev);
165 if (!pci_is_enabled(func))
169 pci_disable_device(pdev);
177 static void cdnsp_pci_remove(struct pci_dev *pdev)
180 struct pci_dev *func;
182 func = cdnsp_get_second_fun(pdev);
183 cdnsp = (struct cdns *)pci_get_drvdata(pdev);
185 if (pci_dev_run_wake(pdev))
186 pm_runtime_get_noresume(&pdev->dev);
188 if (pci_is_enabled(func)) {
197 static int __maybe_unused cdnsp_pci_suspend(struct device *dev)
199 struct cdns *cdns = dev_get_drvdata(dev);
201 return cdns_suspend(cdns);
204 static int __maybe_unused cdnsp_pci_resume(struct device *dev)
206 struct cdns *cdns = dev_get_drvdata(dev);
210 spin_lock_irqsave(&cdns->lock, flags);
211 ret = cdns_resume(cdns);
212 spin_unlock_irqrestore(&cdns->lock, flags);
213 cdns_set_active(cdns, 1);
218 static const struct dev_pm_ops cdnsp_pci_pm_ops = {
219 SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend, cdnsp_pci_resume)
222 static const struct pci_device_id cdnsp_pci_ids[] = {
223 { PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,
224 PCI_CLASS_SERIAL_USB_DEVICE, PCI_ANY_ID },
225 { PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,
226 CDNS_DRD_IF, PCI_ANY_ID },
227 { PCI_VENDOR_ID_CDNS, CDNS_DRD_ID, PCI_ANY_ID, PCI_ANY_ID,
228 CDNS_DRD_IF, PCI_ANY_ID },
232 static struct pci_driver cdnsp_pci_driver = {
234 .id_table = &cdnsp_pci_ids[0],
235 .probe = cdnsp_pci_probe,
236 .remove = cdnsp_pci_remove,
238 .pm = &cdnsp_pci_pm_ops,
242 module_pci_driver(cdnsp_pci_driver);
243 MODULE_DEVICE_TABLE(pci, cdnsp_pci_ids);
245 MODULE_ALIAS("pci:cdnsp");
247 MODULE_LICENSE("GPL v2");
248 MODULE_DESCRIPTION("Cadence CDNSP PCI driver");