xref: /openbmc/linux/drivers/net/wireless/ath/wil6210/pcie_bus.c (revision bbde9fc1824aab58bc78c084163007dd6c03fe5b)
1 /*
2  * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/moduleparam.h>
20 #include <linux/interrupt.h>
21 
22 #include "wil6210.h"
23 
24 static int use_msi = 1;
25 module_param(use_msi, int, S_IRUGO);
26 MODULE_PARM_DESC(use_msi,
27 		 " Use MSI interrupt: "
28 		 "0 - don't, 1 - (default) - single, or 3");
29 
30 static
31 void wil_set_capabilities(struct wil6210_priv *wil)
32 {
33 	u32 rev_id = ioread32(wil->csr + HOSTADDR(RGF_USER_JTAG_DEV_ID));
34 
35 	bitmap_zero(wil->hw_capabilities, hw_capability_last);
36 
37 	switch (rev_id) {
38 	case JTAG_DEV_ID_SPARROW_B0:
39 		wil->hw_name = "Sparrow B0";
40 		wil->hw_version = HW_VER_SPARROW_B0;
41 		break;
42 	default:
43 		wil_err(wil, "Unknown board hardware 0x%08x\n", rev_id);
44 		wil->hw_name = "Unknown";
45 		wil->hw_version = HW_VER_UNKNOWN;
46 	}
47 
48 	wil_info(wil, "Board hardware is %s\n", wil->hw_name);
49 }
50 
51 void wil_disable_irq(struct wil6210_priv *wil)
52 {
53 	int irq = wil->pdev->irq;
54 
55 	disable_irq(irq);
56 	if (wil->n_msi == 3) {
57 		disable_irq(irq + 1);
58 		disable_irq(irq + 2);
59 	}
60 }
61 
62 void wil_enable_irq(struct wil6210_priv *wil)
63 {
64 	int irq = wil->pdev->irq;
65 
66 	enable_irq(irq);
67 	if (wil->n_msi == 3) {
68 		enable_irq(irq + 1);
69 		enable_irq(irq + 2);
70 	}
71 }
72 
73 /* Bus ops */
74 static int wil_if_pcie_enable(struct wil6210_priv *wil)
75 {
76 	struct pci_dev *pdev = wil->pdev;
77 	int rc;
78 	/* on platforms with buggy ACPI, pdev->msi_enabled may be set to
79 	 * allow pci_enable_device to work. This indicates INTx was not routed
80 	 * and only MSI should be used
81 	 */
82 	int msi_only = pdev->msi_enabled;
83 
84 	wil_dbg_misc(wil, "%s()\n", __func__);
85 
86 	pdev->msi_enabled = 0;
87 
88 	pci_set_master(pdev);
89 
90 	/*
91 	 * how many MSI interrupts to request?
92 	 */
93 	switch (use_msi) {
94 	case 3:
95 	case 1:
96 		wil_dbg_misc(wil, "Setup %d MSI interrupts\n", use_msi);
97 		break;
98 	case 0:
99 		wil_dbg_misc(wil, "MSI interrupts disabled, use INTx\n");
100 		break;
101 	default:
102 		wil_err(wil, "Invalid use_msi=%d, default to 1\n", use_msi);
103 		use_msi = 1;
104 	}
105 
106 	if (use_msi == 3 && pci_enable_msi_range(pdev, 3, 3) < 0) {
107 		wil_err(wil, "3 MSI mode failed, try 1 MSI\n");
108 		use_msi = 1;
109 	}
110 
111 	if (use_msi == 1 && pci_enable_msi(pdev)) {
112 		wil_err(wil, "pci_enable_msi failed, use INTx\n");
113 		use_msi = 0;
114 	}
115 
116 	wil->n_msi = use_msi;
117 
118 	if ((wil->n_msi == 0) && msi_only) {
119 		wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
120 		rc = -ENODEV;
121 		goto stop_master;
122 	}
123 
124 	rc = wil6210_init_irq(wil, pdev->irq);
125 	if (rc)
126 		goto stop_master;
127 
128 	/* need reset here to obtain MAC */
129 	mutex_lock(&wil->mutex);
130 	rc = wil_reset(wil, false);
131 	mutex_unlock(&wil->mutex);
132 	if (rc)
133 		goto release_irq;
134 
135 	return 0;
136 
137  release_irq:
138 	wil6210_fini_irq(wil, pdev->irq);
139 	/* safe to call if no MSI */
140 	pci_disable_msi(pdev);
141  stop_master:
142 	pci_clear_master(pdev);
143 	return rc;
144 }
145 
146 static int wil_if_pcie_disable(struct wil6210_priv *wil)
147 {
148 	struct pci_dev *pdev = wil->pdev;
149 
150 	wil_dbg_misc(wil, "%s()\n", __func__);
151 
152 	pci_clear_master(pdev);
153 	/* disable and release IRQ */
154 	wil6210_fini_irq(wil, pdev->irq);
155 	/* safe to call if no MSI */
156 	pci_disable_msi(pdev);
157 	/* TODO: disable HW */
158 
159 	return 0;
160 }
161 
162 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
163 {
164 	struct wil6210_priv *wil;
165 	struct device *dev = &pdev->dev;
166 	int rc;
167 
168 	/* check HW */
169 	dev_info(&pdev->dev, WIL_NAME
170 		 " device found [%04x:%04x] (rev %x)\n",
171 		 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
172 
173 	if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
174 		dev_err(&pdev->dev, "Not " WIL_NAME "? "
175 			"BAR0 size is %lu while expecting %lu\n",
176 			(ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
177 		return -ENODEV;
178 	}
179 
180 	wil = wil_if_alloc(dev);
181 	if (IS_ERR(wil)) {
182 		rc = (int)PTR_ERR(wil);
183 		dev_err(dev, "wil_if_alloc failed: %d\n", rc);
184 		return rc;
185 	}
186 	wil->pdev = pdev;
187 	pci_set_drvdata(pdev, wil);
188 	/* rollback to if_free */
189 
190 	wil->platform_handle =
191 			wil_platform_init(&pdev->dev, &wil->platform_ops);
192 	if (!wil->platform_handle) {
193 		rc = -ENODEV;
194 		wil_err(wil, "wil_platform_init failed\n");
195 		goto if_free;
196 	}
197 	/* rollback to err_plat */
198 
199 	rc = pci_enable_device(pdev);
200 	if (rc) {
201 		wil_err(wil,
202 			"pci_enable_device failed, retry with MSI only\n");
203 		/* Work around for platforms that can't allocate IRQ:
204 		 * retry with MSI only
205 		 */
206 		pdev->msi_enabled = 1;
207 		rc = pci_enable_device(pdev);
208 	}
209 	if (rc) {
210 		wil_err(wil,
211 			"pci_enable_device failed, even with MSI only\n");
212 		goto err_plat;
213 	}
214 	/* rollback to err_disable_pdev */
215 
216 	rc = pci_request_region(pdev, 0, WIL_NAME);
217 	if (rc) {
218 		wil_err(wil, "pci_request_region failed\n");
219 		goto err_disable_pdev;
220 	}
221 	/* rollback to err_release_reg */
222 
223 	wil->csr = pci_ioremap_bar(pdev, 0);
224 	if (!wil->csr) {
225 		wil_err(wil, "pci_ioremap_bar failed\n");
226 		rc = -ENODEV;
227 		goto err_release_reg;
228 	}
229 	/* rollback to err_iounmap */
230 	wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
231 
232 	wil_set_capabilities(wil);
233 	wil6210_clear_irq(wil);
234 
235 	/* FW should raise IRQ when ready */
236 	rc = wil_if_pcie_enable(wil);
237 	if (rc) {
238 		wil_err(wil, "Enable device failed\n");
239 		goto err_iounmap;
240 	}
241 	/* rollback to bus_disable */
242 
243 	rc = wil_if_add(wil);
244 	if (rc) {
245 		wil_err(wil, "wil_if_add failed: %d\n", rc);
246 		goto bus_disable;
247 	}
248 
249 	wil6210_debugfs_init(wil);
250 
251 
252 	return 0;
253 
254 bus_disable:
255 	wil_if_pcie_disable(wil);
256 err_iounmap:
257 	pci_iounmap(pdev, wil->csr);
258 err_release_reg:
259 	pci_release_region(pdev, 0);
260 err_disable_pdev:
261 	pci_disable_device(pdev);
262 err_plat:
263 	if (wil->platform_ops.uninit)
264 		wil->platform_ops.uninit(wil->platform_handle);
265 if_free:
266 	wil_if_free(wil);
267 
268 	return rc;
269 }
270 
271 static void wil_pcie_remove(struct pci_dev *pdev)
272 {
273 	struct wil6210_priv *wil = pci_get_drvdata(pdev);
274 	void __iomem *csr = wil->csr;
275 
276 	wil_dbg_misc(wil, "%s()\n", __func__);
277 
278 	wil6210_debugfs_remove(wil);
279 	wil_if_remove(wil);
280 	wil_if_pcie_disable(wil);
281 	pci_iounmap(pdev, csr);
282 	pci_release_region(pdev, 0);
283 	pci_disable_device(pdev);
284 	if (wil->platform_ops.uninit)
285 		wil->platform_ops.uninit(wil->platform_handle);
286 	wil_if_free(wil);
287 }
288 
289 static const struct pci_device_id wil6210_pcie_ids[] = {
290 	{ PCI_DEVICE(0x1ae9, 0x0310) },
291 	{ PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
292 	{ /* end: all zeroes */	},
293 };
294 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
295 
296 static struct pci_driver wil6210_driver = {
297 	.probe		= wil_pcie_probe,
298 	.remove		= wil_pcie_remove,
299 	.id_table	= wil6210_pcie_ids,
300 	.name		= WIL_NAME,
301 };
302 
303 static int __init wil6210_driver_init(void)
304 {
305 	int rc;
306 
307 	rc = wil_platform_modinit();
308 	if (rc)
309 		return rc;
310 
311 	rc = pci_register_driver(&wil6210_driver);
312 	if (rc)
313 		wil_platform_modexit();
314 	return rc;
315 }
316 module_init(wil6210_driver_init);
317 
318 static void __exit wil6210_driver_exit(void)
319 {
320 	pci_unregister_driver(&wil6210_driver);
321 	wil_platform_modexit();
322 }
323 module_exit(wil6210_driver_exit);
324 
325 MODULE_LICENSE("Dual BSD/GPL");
326 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
327 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");
328