drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / drivers / usb / cdns3 / cdnsp-pci.c
blob2d05368a6745a1a4fcc166056c019aa4f151af06
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Cadence PCI Glue driver.
5 * Copyright (C) 2019 Cadence.
7 * Author: Pawel Laszczak <pawell@cadence.com>
9 */
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>
18 #include "core.h"
19 #include "gadget-export.h"
21 #define PCI_BAR_HOST 0
22 #define PCI_BAR_OTG 0
23 #define PCI_BAR_DEV 2
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);
49 return NULL;
52 static int cdnsp_pci_probe(struct pci_dev *pdev,
53 const struct pci_device_id *id)
55 struct device *dev = &pdev->dev;
56 struct pci_dev *func;
57 struct resource *res;
58 struct cdns *cdnsp;
59 int ret;
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))
67 return -EINVAL;
69 func = cdnsp_get_second_fun(pdev);
70 if (!func)
71 return -EINVAL;
73 if (func->class == PCI_CLASS_SERIAL_USB_XHCI ||
74 pdev->class == PCI_CLASS_SERIAL_USB_XHCI) {
75 ret = -EINVAL;
76 goto put_pci;
79 ret = pcim_enable_device(pdev);
80 if (ret) {
81 dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", ret);
82 goto put_pci;
85 pci_set_master(pdev);
86 if (pci_is_enabled(func)) {
87 cdnsp = pci_get_drvdata(func);
88 } else {
89 cdnsp = kzalloc(sizeof(*cdnsp), GFP_KERNEL);
90 if (!cdnsp) {
91 ret = -ENOMEM;
92 goto disable_pci;
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");
105 if (!res) {
106 dev_dbg(dev, "controller already in use\n");
107 ret = -EBUSY;
108 goto free_cdnsp;
111 cdnsp->dev_regs = devm_ioremap(dev, rsrc_start, rsrc_len);
112 if (!cdnsp->dev_regs) {
113 dev_dbg(dev, "error mapping memory\n");
114 ret = -EFAULT;
115 goto free_cdnsp;
118 cdnsp->dev_irq = pdev->irq;
119 dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n",
120 &rsrc_start);
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);
125 res->name = "xhci";
126 res->flags = IORESOURCE_MEM;
127 dev_dbg(dev, "USBSS-XHCI physical base addr: %pa\n",
128 &res->start);
130 /* Interrupt for XHCI, */
131 res = &cdnsp->xhci_res[1];
132 res->start = pdev->irq;
133 res->name = "host";
134 res->flags = IORESOURCE_IRQ;
135 } else {
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);
139 res->name = "otg";
140 res->flags = IORESOURCE_MEM;
141 dev_dbg(dev, "CDNSP-DRD physical base addr: %pa\n",
142 &res->start);
144 /* Interrupt for OTG/DRD. */
145 cdnsp->otg_irq = pdev->irq;
148 if (pci_is_enabled(func)) {
149 cdnsp->dev = dev;
150 cdnsp->gadget_init = cdnsp_gadget_init;
152 ret = cdns_init(cdnsp);
153 if (ret)
154 goto free_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);
163 return 0;
165 free_cdnsp:
166 if (!pci_is_enabled(func))
167 kfree(cdnsp);
169 disable_pci:
170 pci_disable_device(pdev);
172 put_pci:
173 pci_dev_put(func);
175 return ret;
178 static void cdnsp_pci_remove(struct pci_dev *pdev)
180 struct cdns *cdnsp;
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)) {
190 cdns_remove(cdnsp);
191 } else {
192 kfree(cdnsp);
195 pci_dev_put(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);
208 unsigned long flags;
209 int ret;
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);
216 return ret;
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 },
230 { 0, }
233 static struct pci_driver cdnsp_pci_driver = {
234 .name = "cdnsp-pci",
235 .id_table = cdnsp_pci_ids,
236 .probe = cdnsp_pci_probe,
237 .remove = cdnsp_pci_remove,
238 .driver = {
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");