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 #define PCI_DEVICE_ID_CDNS_USB3 0x0100
32 #define PCI_DEVICE_ID_CDNS_UDC 0x0200
34 #define PCI_CLASS_SERIAL_USB_CDNS_USB3 (PCI_CLASS_SERIAL_USB << 8 | 0x80)
35 #define PCI_CLASS_SERIAL_USB_CDNS_UDC PCI_CLASS_SERIAL_USB_DEVICE
37 static struct pci_dev
*cdnsp_get_second_fun(struct pci_dev
*pdev
)
40 * Gets the second function.
41 * Platform has two function. The fist keeps resources for
42 * Host/Device while the secon keeps resources for DRD/OTG.
44 if (pdev
->device
== PCI_DEVICE_ID_CDNS_UDC
)
45 return pci_get_device(pdev
->vendor
, PCI_DEVICE_ID_CDNS_USB3
, NULL
);
46 if (pdev
->device
== PCI_DEVICE_ID_CDNS_USB3
)
47 return pci_get_device(pdev
->vendor
, PCI_DEVICE_ID_CDNS_UDC
, NULL
);
52 static int cdnsp_pci_probe(struct pci_dev
*pdev
,
53 const struct pci_device_id
*id
)
55 struct device
*dev
= &pdev
->dev
;
62 * For GADGET/HOST PCI (devfn) function number is 0,
63 * for OTG PCI (devfn) function number is 1.
65 if (!id
|| (pdev
->devfn
!= PCI_DEV_FN_HOST_DEVICE
&&
66 pdev
->devfn
!= PCI_DEV_FN_OTG
))
69 func
= cdnsp_get_second_fun(pdev
);
73 if (func
->class == PCI_CLASS_SERIAL_USB_XHCI
||
74 pdev
->class == PCI_CLASS_SERIAL_USB_XHCI
) {
79 ret
= pcim_enable_device(pdev
);
81 dev_err(&pdev
->dev
, "Enabling PCI device has failed %d\n", ret
);
86 if (pci_is_enabled(func
)) {
87 cdnsp
= pci_get_drvdata(func
);
89 cdnsp
= kzalloc(sizeof(*cdnsp
), GFP_KERNEL
);
96 /* For GADGET device function number is 0. */
97 if (pdev
->devfn
== 0) {
98 resource_size_t rsrc_start
, rsrc_len
;
100 /* Function 0: host(BAR_0) + device(BAR_1).*/
101 dev_dbg(dev
, "Initialize resources\n");
102 rsrc_start
= pci_resource_start(pdev
, PCI_BAR_DEV
);
103 rsrc_len
= pci_resource_len(pdev
, PCI_BAR_DEV
);
104 res
= devm_request_mem_region(dev
, rsrc_start
, rsrc_len
, "dev");
106 dev_dbg(dev
, "controller already in use\n");
111 cdnsp
->dev_regs
= devm_ioremap(dev
, rsrc_start
, rsrc_len
);
112 if (!cdnsp
->dev_regs
) {
113 dev_dbg(dev
, "error mapping memory\n");
118 cdnsp
->dev_irq
= pdev
->irq
;
119 dev_dbg(dev
, "USBSS-DEV physical base addr: %pa\n",
122 res
= &cdnsp
->xhci_res
[0];
123 res
->start
= pci_resource_start(pdev
, PCI_BAR_HOST
);
124 res
->end
= pci_resource_end(pdev
, PCI_BAR_HOST
);
126 res
->flags
= IORESOURCE_MEM
;
127 dev_dbg(dev
, "USBSS-XHCI physical base addr: %pa\n",
130 /* Interrupt for XHCI, */
131 res
= &cdnsp
->xhci_res
[1];
132 res
->start
= pdev
->irq
;
134 res
->flags
= IORESOURCE_IRQ
;
136 res
= &cdnsp
->otg_res
;
137 res
->start
= pci_resource_start(pdev
, PCI_BAR_OTG
);
138 res
->end
= pci_resource_end(pdev
, PCI_BAR_OTG
);
140 res
->flags
= IORESOURCE_MEM
;
141 dev_dbg(dev
, "CDNSP-DRD physical base addr: %pa\n",
144 /* Interrupt for OTG/DRD. */
145 cdnsp
->otg_irq
= pdev
->irq
;
148 if (pci_is_enabled(func
)) {
150 cdnsp
->gadget_init
= cdnsp_gadget_init
;
152 ret
= cdns_init(cdnsp
);
157 pci_set_drvdata(pdev
, cdnsp
);
159 device_wakeup_enable(&pdev
->dev
);
160 if (pci_dev_run_wake(pdev
))
161 pm_runtime_put_noidle(&pdev
->dev
);
166 if (!pci_is_enabled(func
))
170 pci_disable_device(pdev
);
178 static void cdnsp_pci_remove(struct pci_dev
*pdev
)
181 struct pci_dev
*func
;
183 func
= cdnsp_get_second_fun(pdev
);
184 cdnsp
= (struct cdns
*)pci_get_drvdata(pdev
);
186 if (pci_dev_run_wake(pdev
))
187 pm_runtime_get_noresume(&pdev
->dev
);
189 if (pci_is_enabled(func
)) {
198 static int __maybe_unused
cdnsp_pci_suspend(struct device
*dev
)
200 struct cdns
*cdns
= dev_get_drvdata(dev
);
202 return cdns_suspend(cdns
);
205 static int __maybe_unused
cdnsp_pci_resume(struct device
*dev
)
207 struct cdns
*cdns
= dev_get_drvdata(dev
);
211 spin_lock_irqsave(&cdns
->lock
, flags
);
212 ret
= cdns_resume(cdns
);
213 spin_unlock_irqrestore(&cdns
->lock
, flags
);
214 cdns_set_active(cdns
, 1);
219 static const struct dev_pm_ops cdnsp_pci_pm_ops
= {
220 SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend
, cdnsp_pci_resume
)
223 static const struct pci_device_id cdnsp_pci_ids
[] = {
224 { PCI_DEVICE(PCI_VENDOR_ID_CDNS
, PCI_DEVICE_ID_CDNS_UDC
),
225 .class = PCI_CLASS_SERIAL_USB_CDNS_UDC
},
226 { PCI_DEVICE(PCI_VENDOR_ID_CDNS
, PCI_DEVICE_ID_CDNS_UDC
),
227 .class = PCI_CLASS_SERIAL_USB_CDNS_USB3
},
228 { PCI_DEVICE(PCI_VENDOR_ID_CDNS
, PCI_DEVICE_ID_CDNS_USB3
),
229 .class = PCI_CLASS_SERIAL_USB_CDNS_USB3
},
233 static struct pci_driver cdnsp_pci_driver
= {
235 .id_table
= cdnsp_pci_ids
,
236 .probe
= cdnsp_pci_probe
,
237 .remove
= cdnsp_pci_remove
,
239 .pm
= &cdnsp_pci_pm_ops
,
243 module_pci_driver(cdnsp_pci_driver
);
244 MODULE_DEVICE_TABLE(pci
, cdnsp_pci_ids
);
246 MODULE_ALIAS("pci:cdnsp");
247 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
248 MODULE_LICENSE("GPL v2");
249 MODULE_DESCRIPTION("Cadence CDNSP PCI driver");