1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Cadence PCI Glue driver.
4 *
5 * Copyright (C) 2019 Cadence.
6 *
7 * Author: Pawel Laszczak <pawell@cadence.com>
8 *
9 */
10
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>
17
18 #include "core.h"
19 #include "gadget-export.h"
20
21 #define PCI_BAR_HOST 0
22 #define PCI_BAR_OTG 0
23 #define PCI_BAR_DEV 2
24
25 #define PCI_DEV_FN_HOST_DEVICE 0
26 #define PCI_DEV_FN_OTG 1
27
28 #define PCI_DRIVER_NAME "cdns-pci-usbssp"
29 #define PLAT_DRIVER_NAME "cdns-usbssp"
30
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)
35
36 #define CHICKEN_APB_TIMEOUT_VALUE 0x1C20
37
cdnsp_get_second_fun(struct pci_dev * pdev)38 static struct pci_dev *cdnsp_get_second_fun(struct pci_dev *pdev)
39 {
40 /*
41 * Gets the second function.
42 * Platform has two function. The fist keeps resources for
43 * Host/Device while the secon keeps resources for DRD/OTG.
44 */
45 if (pdev->device == CDNS_DEVICE_ID)
46 return pci_get_device(pdev->vendor, CDNS_DRD_ID, NULL);
47 else if (pdev->device == CDNS_DRD_ID)
48 return pci_get_device(pdev->vendor, CDNS_DEVICE_ID, NULL);
49
50 return NULL;
51 }
52
cdnsp_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)53 static int cdnsp_pci_probe(struct pci_dev *pdev,
54 const struct pci_device_id *id)
55 {
56 struct device *dev = &pdev->dev;
57 struct pci_dev *func;
58 struct resource *res;
59 struct cdns *cdnsp;
60 int ret;
61
62 /*
63 * For GADGET/HOST PCI (devfn) function number is 0,
64 * for OTG PCI (devfn) function number is 1.
65 */
66 if (!id || (pdev->devfn != PCI_DEV_FN_HOST_DEVICE &&
67 pdev->devfn != PCI_DEV_FN_OTG))
68 return -EINVAL;
69
70 func = cdnsp_get_second_fun(pdev);
71 if (!func)
72 return -EINVAL;
73
74 if (func->class == PCI_CLASS_SERIAL_USB_XHCI ||
75 pdev->class == PCI_CLASS_SERIAL_USB_XHCI) {
76 ret = -EINVAL;
77 goto put_pci;
78 }
79
80 ret = pcim_enable_device(pdev);
81 if (ret) {
82 dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", ret);
83 goto put_pci;
84 }
85
86 pci_set_master(pdev);
87 if (pci_is_enabled(func)) {
88 cdnsp = pci_get_drvdata(func);
89 } else {
90 cdnsp = kzalloc(sizeof(*cdnsp), GFP_KERNEL);
91 if (!cdnsp) {
92 ret = -ENOMEM;
93 goto disable_pci;
94 }
95 }
96
97 /* For GADGET device function number is 0. */
98 if (pdev->devfn == 0) {
99 resource_size_t rsrc_start, rsrc_len;
100
101 /* Function 0: host(BAR_0) + device(BAR_1).*/
102 dev_dbg(dev, "Initialize resources\n");
103 rsrc_start = pci_resource_start(pdev, PCI_BAR_DEV);
104 rsrc_len = pci_resource_len(pdev, PCI_BAR_DEV);
105 res = devm_request_mem_region(dev, rsrc_start, rsrc_len, "dev");
106 if (!res) {
107 dev_dbg(dev, "controller already in use\n");
108 ret = -EBUSY;
109 goto free_cdnsp;
110 }
111
112 cdnsp->dev_regs = devm_ioremap(dev, rsrc_start, rsrc_len);
113 if (!cdnsp->dev_regs) {
114 dev_dbg(dev, "error mapping memory\n");
115 ret = -EFAULT;
116 goto free_cdnsp;
117 }
118
119 cdnsp->dev_irq = pdev->irq;
120 dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n",
121 &rsrc_start);
122
123 res = &cdnsp->xhci_res[0];
124 res->start = pci_resource_start(pdev, PCI_BAR_HOST);
125 res->end = pci_resource_end(pdev, PCI_BAR_HOST);
126 res->name = "xhci";
127 res->flags = IORESOURCE_MEM;
128 dev_dbg(dev, "USBSS-XHCI physical base addr: %pa\n",
129 &res->start);
130
131 /* Interrupt for XHCI, */
132 res = &cdnsp->xhci_res[1];
133 res->start = pdev->irq;
134 res->name = "host";
135 res->flags = IORESOURCE_IRQ;
136 } else {
137 res = &cdnsp->otg_res;
138 res->start = pci_resource_start(pdev, PCI_BAR_OTG);
139 res->end = pci_resource_end(pdev, PCI_BAR_OTG);
140 res->name = "otg";
141 res->flags = IORESOURCE_MEM;
142 dev_dbg(dev, "CDNSP-DRD physical base addr: %pa\n",
143 &res->start);
144
145 /* Interrupt for OTG/DRD. */
146 cdnsp->otg_irq = pdev->irq;
147 }
148
149 /*
150 * Cadence PCI based platform require some longer timeout for APB
151 * to fixes domain clock synchronization issue after resuming
152 * controller from L1 state.
153 */
154 cdnsp->override_apb_timeout = CHICKEN_APB_TIMEOUT_VALUE;
155 pci_set_drvdata(pdev, cdnsp);
156
157 if (pci_is_enabled(func)) {
158 cdnsp->dev = dev;
159 cdnsp->gadget_init = cdnsp_gadget_init;
160
161 ret = cdns_init(cdnsp);
162 if (ret)
163 goto free_cdnsp;
164 }
165
166 device_wakeup_enable(&pdev->dev);
167 if (pci_dev_run_wake(pdev))
168 pm_runtime_put_noidle(&pdev->dev);
169
170 return 0;
171
172 free_cdnsp:
173 if (!pci_is_enabled(func))
174 kfree(cdnsp);
175
176 disable_pci:
177 pci_disable_device(pdev);
178
179 put_pci:
180 pci_dev_put(func);
181
182 return ret;
183 }
184
cdnsp_pci_remove(struct pci_dev * pdev)185 static void cdnsp_pci_remove(struct pci_dev *pdev)
186 {
187 struct cdns *cdnsp;
188 struct pci_dev *func;
189
190 func = cdnsp_get_second_fun(pdev);
191 cdnsp = (struct cdns *)pci_get_drvdata(pdev);
192
193 if (pci_dev_run_wake(pdev))
194 pm_runtime_get_noresume(&pdev->dev);
195
196 if (pci_is_enabled(func)) {
197 cdns_remove(cdnsp);
198 } else {
199 kfree(cdnsp);
200 }
201
202 pci_dev_put(func);
203 }
204
cdnsp_pci_suspend(struct device * dev)205 static int __maybe_unused cdnsp_pci_suspend(struct device *dev)
206 {
207 struct cdns *cdns = dev_get_drvdata(dev);
208
209 return cdns_suspend(cdns);
210 }
211
cdnsp_pci_resume(struct device * dev)212 static int __maybe_unused cdnsp_pci_resume(struct device *dev)
213 {
214 struct cdns *cdns = dev_get_drvdata(dev);
215 unsigned long flags;
216 int ret;
217
218 spin_lock_irqsave(&cdns->lock, flags);
219 ret = cdns_resume(cdns);
220 spin_unlock_irqrestore(&cdns->lock, flags);
221 cdns_set_active(cdns, 1);
222
223 return ret;
224 }
225
226 static const struct dev_pm_ops cdnsp_pci_pm_ops = {
227 SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend, cdnsp_pci_resume)
228 };
229
230 static const struct pci_device_id cdnsp_pci_ids[] = {
231 { PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,
232 PCI_CLASS_SERIAL_USB_DEVICE, PCI_ANY_ID },
233 { PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,
234 CDNS_DRD_IF, PCI_ANY_ID },
235 { PCI_VENDOR_ID_CDNS, CDNS_DRD_ID, PCI_ANY_ID, PCI_ANY_ID,
236 CDNS_DRD_IF, PCI_ANY_ID },
237 { 0, }
238 };
239
240 static struct pci_driver cdnsp_pci_driver = {
241 .name = "cdnsp-pci",
242 .id_table = &cdnsp_pci_ids[0],
243 .probe = cdnsp_pci_probe,
244 .remove = cdnsp_pci_remove,
245 .driver = {
246 .pm = &cdnsp_pci_pm_ops,
247 }
248 };
249
250 module_pci_driver(cdnsp_pci_driver);
251 MODULE_DEVICE_TABLE(pci, cdnsp_pci_ids);
252
253 MODULE_ALIAS("pci:cdnsp");
254 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
255 MODULE_LICENSE("GPL v2");
256 MODULE_DESCRIPTION("Cadence CDNSP PCI driver");
257