xref: /linux/drivers/usb/cdns3/cdnsp-pci.c (revision a6021aa24f6417416d93318bbfa022ab229c33c8)
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 PCI_DEVICE_ID_CDNS_USB3		0x0100
32 #define PCI_DEVICE_ID_CDNS_UDC		0x0200
33 
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
36 
37 static struct pci_dev *cdnsp_get_second_fun(struct pci_dev *pdev)
38 {
39 	/*
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.
43 	 */
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);
48 
49 	return NULL;
50 }
51 
52 static int cdnsp_pci_probe(struct pci_dev *pdev,
53 			   const struct pci_device_id *id)
54 {
55 	struct device *dev = &pdev->dev;
56 	struct pci_dev *func;
57 	struct resource *res;
58 	struct cdns *cdnsp;
59 	int ret;
60 
61 	/*
62 	 * For GADGET/HOST PCI (devfn) function number is 0,
63 	 * for OTG PCI (devfn) function number is 1.
64 	 */
65 	if (!id || (pdev->devfn != PCI_DEV_FN_HOST_DEVICE &&
66 		    pdev->devfn != PCI_DEV_FN_OTG))
67 		return -EINVAL;
68 
69 	func = cdnsp_get_second_fun(pdev);
70 	if (!func)
71 		return -EINVAL;
72 
73 	if (func->class == PCI_CLASS_SERIAL_USB_XHCI ||
74 	    pdev->class == PCI_CLASS_SERIAL_USB_XHCI) {
75 		ret = -EINVAL;
76 		goto put_pci;
77 	}
78 
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;
83 	}
84 
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;
93 		}
94 	}
95 
96 	/* For GADGET device function number is 0. */
97 	if (pdev->devfn == 0) {
98 		resource_size_t rsrc_start, rsrc_len;
99 
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;
109 		}
110 
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;
116 		}
117 
118 		cdnsp->dev_irq = pdev->irq;
119 		dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n",
120 			&rsrc_start);
121 
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);
129 
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);
143 
144 		/* Interrupt for OTG/DRD. */
145 		cdnsp->otg_irq = pdev->irq;
146 	}
147 
148 	if (pci_is_enabled(func)) {
149 		cdnsp->dev = dev;
150 		cdnsp->gadget_init = cdnsp_gadget_init;
151 
152 		ret = cdns_init(cdnsp);
153 		if (ret)
154 			goto free_cdnsp;
155 	}
156 
157 	pci_set_drvdata(pdev, cdnsp);
158 
159 	device_wakeup_enable(&pdev->dev);
160 	if (pci_dev_run_wake(pdev))
161 		pm_runtime_put_noidle(&pdev->dev);
162 
163 	return 0;
164 
165 free_cdnsp:
166 	if (!pci_is_enabled(func))
167 		kfree(cdnsp);
168 
169 disable_pci:
170 	pci_disable_device(pdev);
171 
172 put_pci:
173 	pci_dev_put(func);
174 
175 	return ret;
176 }
177 
178 static void cdnsp_pci_remove(struct pci_dev *pdev)
179 {
180 	struct cdns *cdnsp;
181 	struct pci_dev *func;
182 
183 	func = cdnsp_get_second_fun(pdev);
184 	cdnsp = (struct cdns *)pci_get_drvdata(pdev);
185 
186 	if (pci_dev_run_wake(pdev))
187 		pm_runtime_get_noresume(&pdev->dev);
188 
189 	if (pci_is_enabled(func)) {
190 		cdns_remove(cdnsp);
191 	} else {
192 		kfree(cdnsp);
193 	}
194 
195 	pci_dev_put(func);
196 }
197 
198 static int __maybe_unused cdnsp_pci_suspend(struct device *dev)
199 {
200 	struct cdns *cdns = dev_get_drvdata(dev);
201 
202 	return cdns_suspend(cdns);
203 }
204 
205 static int __maybe_unused cdnsp_pci_resume(struct device *dev)
206 {
207 	struct cdns *cdns = dev_get_drvdata(dev);
208 	unsigned long flags;
209 	int ret;
210 
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);
215 
216 	return ret;
217 }
218 
219 static const struct dev_pm_ops cdnsp_pci_pm_ops = {
220 	SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend, cdnsp_pci_resume)
221 };
222 
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, }
231 };
232 
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,
240 	}
241 };
242 
243 module_pci_driver(cdnsp_pci_driver);
244 MODULE_DEVICE_TABLE(pci, cdnsp_pci_ids);
245 
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");
250