1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence PCI Glue driver.
5 * Copyright (C) 2019 Cadence.
7 * Author: Pawel Laszczak <pawell@cadence.com>
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 static struct pci_dev
*cdnsp_get_second_fun(struct pci_dev
*pdev
)
34 * Gets the second function.
35 * Platform has two function. The fist keeps resources for
36 * Host/Device while the secon keeps resources for DRD/OTG.
38 if (pdev
->device
== PCI_DEVICE_ID_CDNS_USBSSP
)
39 return pci_get_device(pdev
->vendor
, PCI_DEVICE_ID_CDNS_USBSS
, NULL
);
40 if (pdev
->device
== PCI_DEVICE_ID_CDNS_USBSS
)
41 return pci_get_device(pdev
->vendor
, PCI_DEVICE_ID_CDNS_USBSSP
, NULL
);
46 static int cdnsp_pci_probe(struct pci_dev
*pdev
,
47 const struct pci_device_id
*id
)
49 struct device
*dev
= &pdev
->dev
;
56 * For GADGET/HOST PCI (devfn) function number is 0,
57 * for OTG PCI (devfn) function number is 1.
59 if (!id
|| (pdev
->devfn
!= PCI_DEV_FN_HOST_DEVICE
&&
60 pdev
->devfn
!= PCI_DEV_FN_OTG
))
63 func
= cdnsp_get_second_fun(pdev
);
67 if (func
->class == PCI_CLASS_SERIAL_USB_XHCI
||
68 pdev
->class == PCI_CLASS_SERIAL_USB_XHCI
) {
73 ret
= pcim_enable_device(pdev
);
75 dev_err(&pdev
->dev
, "Enabling PCI device has failed %d\n", ret
);
80 if (pci_is_enabled(func
)) {
81 cdnsp
= pci_get_drvdata(func
);
83 cdnsp
= kzalloc(sizeof(*cdnsp
), GFP_KERNEL
);
90 /* For GADGET device function number is 0. */
91 if (pdev
->devfn
== 0) {
92 resource_size_t rsrc_start
, rsrc_len
;
94 /* Function 0: host(BAR_0) + device(BAR_1).*/
95 dev_dbg(dev
, "Initialize resources\n");
96 rsrc_start
= pci_resource_start(pdev
, PCI_BAR_DEV
);
97 rsrc_len
= pci_resource_len(pdev
, PCI_BAR_DEV
);
98 res
= devm_request_mem_region(dev
, rsrc_start
, rsrc_len
, "dev");
100 dev_dbg(dev
, "controller already in use\n");
105 cdnsp
->dev_regs
= devm_ioremap(dev
, rsrc_start
, rsrc_len
);
106 if (!cdnsp
->dev_regs
) {
107 dev_dbg(dev
, "error mapping memory\n");
112 cdnsp
->dev_irq
= pdev
->irq
;
113 dev_dbg(dev
, "USBSS-DEV physical base addr: %pa\n",
116 res
= &cdnsp
->xhci_res
[0];
117 res
->start
= pci_resource_start(pdev
, PCI_BAR_HOST
);
118 res
->end
= pci_resource_end(pdev
, PCI_BAR_HOST
);
120 res
->flags
= IORESOURCE_MEM
;
121 dev_dbg(dev
, "USBSS-XHCI physical base addr: %pa\n",
124 /* Interrupt for XHCI, */
125 res
= &cdnsp
->xhci_res
[1];
126 res
->start
= pdev
->irq
;
128 res
->flags
= IORESOURCE_IRQ
;
130 res
= &cdnsp
->otg_res
;
131 res
->start
= pci_resource_start(pdev
, PCI_BAR_OTG
);
132 res
->end
= pci_resource_end(pdev
, PCI_BAR_OTG
);
134 res
->flags
= IORESOURCE_MEM
;
135 dev_dbg(dev
, "CDNSP-DRD physical base addr: %pa\n",
138 /* Interrupt for OTG/DRD. */
139 cdnsp
->otg_irq
= pdev
->irq
;
142 if (pci_is_enabled(func
)) {
144 cdnsp
->gadget_init
= cdnsp_gadget_init
;
146 ret
= cdns_init(cdnsp
);
151 pci_set_drvdata(pdev
, cdnsp
);
153 device_wakeup_enable(&pdev
->dev
);
154 if (pci_dev_run_wake(pdev
))
155 pm_runtime_put_noidle(&pdev
->dev
);
160 if (!pci_is_enabled(func
))
164 pci_disable_device(pdev
);
172 static void cdnsp_pci_remove(struct pci_dev
*pdev
)
175 struct pci_dev
*func
;
177 func
= cdnsp_get_second_fun(pdev
);
178 cdnsp
= (struct cdns
*)pci_get_drvdata(pdev
);
180 if (pci_dev_run_wake(pdev
))
181 pm_runtime_get_noresume(&pdev
->dev
);
183 if (pci_is_enabled(func
)) {
192 static int __maybe_unused
cdnsp_pci_suspend(struct device
*dev
)
194 struct cdns
*cdns
= dev_get_drvdata(dev
);
196 return cdns_suspend(cdns
);
199 static int __maybe_unused
cdnsp_pci_resume(struct device
*dev
)
201 struct cdns
*cdns
= dev_get_drvdata(dev
);
205 spin_lock_irqsave(&cdns
->lock
, flags
);
206 ret
= cdns_resume(cdns
);
207 spin_unlock_irqrestore(&cdns
->lock
, flags
);
208 cdns_set_active(cdns
, 1);
213 static const struct dev_pm_ops cdnsp_pci_pm_ops
= {
214 SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend
, cdnsp_pci_resume
)
217 static const struct pci_device_id cdnsp_pci_ids
[] = {
218 { PCI_DEVICE(PCI_VENDOR_ID_CDNS
, PCI_DEVICE_ID_CDNS_USBSSP
),
219 .class = PCI_CLASS_SERIAL_USB_DEVICE
},
220 { PCI_DEVICE(PCI_VENDOR_ID_CDNS
, PCI_DEVICE_ID_CDNS_USBSSP
),
221 .class = PCI_CLASS_SERIAL_USB_CDNS
},
222 { PCI_DEVICE(PCI_VENDOR_ID_CDNS
, PCI_DEVICE_ID_CDNS_USBSS
),
223 .class = PCI_CLASS_SERIAL_USB_CDNS
},
227 static struct pci_driver cdnsp_pci_driver
= {
229 .id_table
= cdnsp_pci_ids
,
230 .probe
= cdnsp_pci_probe
,
231 .remove
= cdnsp_pci_remove
,
233 .pm
= &cdnsp_pci_pm_ops
,
237 module_pci_driver(cdnsp_pci_driver
);
238 MODULE_DEVICE_TABLE(pci
, cdnsp_pci_ids
);
240 MODULE_ALIAS("pci:cdnsp");
241 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
242 MODULE_LICENSE("GPL v2");
243 MODULE_DESCRIPTION("Cadence CDNSP PCI driver");