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
24static int use_msi = 1;
25module_param(use_msi, int, S_IRUGO);
26MODULE_PARM_DESC(use_msi,
27		 " Use MSI interrupt: "
28		 "0 - don't, 1 - (default) - single, or 3");
29
30static bool debug_fw; /* = false; */
31module_param(debug_fw, bool, S_IRUGO);
32MODULE_PARM_DESC(debug_fw, " load driver if FW not ready. For FW debug");
33
34static
35void wil_set_capabilities(struct wil6210_priv *wil)
36{
37	u32 rev_id = ioread32(wil->csr + HOSTADDR(RGF_USER_JTAG_DEV_ID));
38
39	bitmap_zero(wil->hw_capabilities, hw_capability_last);
40
41	switch (rev_id) {
42	case JTAG_DEV_ID_SPARROW_B0:
43		wil->hw_name = "Sparrow B0";
44		wil->hw_version = HW_VER_SPARROW_B0;
45		break;
46	default:
47		wil_err(wil, "Unknown board hardware 0x%08x\n", rev_id);
48		wil->hw_name = "Unknown";
49		wil->hw_version = HW_VER_UNKNOWN;
50	}
51
52	wil_info(wil, "Board hardware is %s\n", wil->hw_name);
53}
54
55void wil_disable_irq(struct wil6210_priv *wil)
56{
57	int irq = wil->pdev->irq;
58
59	disable_irq(irq);
60	if (wil->n_msi == 3) {
61		disable_irq(irq + 1);
62		disable_irq(irq + 2);
63	}
64}
65
66void wil_enable_irq(struct wil6210_priv *wil)
67{
68	int irq = wil->pdev->irq;
69
70	enable_irq(irq);
71	if (wil->n_msi == 3) {
72		enable_irq(irq + 1);
73		enable_irq(irq + 2);
74	}
75}
76
77/* Bus ops */
78static int wil_if_pcie_enable(struct wil6210_priv *wil)
79{
80	struct pci_dev *pdev = wil->pdev;
81	int rc;
82	/* on platforms with buggy ACPI, pdev->msi_enabled may be set to
83	 * allow pci_enable_device to work. This indicates INTx was not routed
84	 * and only MSI should be used
85	 */
86	int msi_only = pdev->msi_enabled;
87
88	wil_dbg_misc(wil, "%s()\n", __func__);
89
90	pdev->msi_enabled = 0;
91
92	pci_set_master(pdev);
93
94	/*
95	 * how many MSI interrupts to request?
96	 */
97	switch (use_msi) {
98	case 3:
99	case 1:
100		wil_dbg_misc(wil, "Setup %d MSI interrupts\n", use_msi);
101		break;
102	case 0:
103		wil_dbg_misc(wil, "MSI interrupts disabled, use INTx\n");
104		break;
105	default:
106		wil_err(wil, "Invalid use_msi=%d, default to 1\n", use_msi);
107		use_msi = 1;
108	}
109
110	if (use_msi == 3 && pci_enable_msi_range(pdev, 3, 3) < 0) {
111		wil_err(wil, "3 MSI mode failed, try 1 MSI\n");
112		use_msi = 1;
113	}
114
115	if (use_msi == 1 && pci_enable_msi(pdev)) {
116		wil_err(wil, "pci_enable_msi failed, use INTx\n");
117		use_msi = 0;
118	}
119
120	wil->n_msi = use_msi;
121
122	if ((wil->n_msi == 0) && msi_only) {
123		wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
124		rc = -ENODEV;
125		goto stop_master;
126	}
127
128	rc = wil6210_init_irq(wil, pdev->irq);
129	if (rc)
130		goto stop_master;
131
132	/* need reset here to obtain MAC */
133	mutex_lock(&wil->mutex);
134	rc = wil_reset(wil, false);
135	mutex_unlock(&wil->mutex);
136	if (debug_fw)
137		rc = 0;
138	if (rc)
139		goto release_irq;
140
141	return 0;
142
143 release_irq:
144	wil6210_fini_irq(wil, pdev->irq);
145	/* safe to call if no MSI */
146	pci_disable_msi(pdev);
147 stop_master:
148	pci_clear_master(pdev);
149	return rc;
150}
151
152static int wil_if_pcie_disable(struct wil6210_priv *wil)
153{
154	struct pci_dev *pdev = wil->pdev;
155
156	wil_dbg_misc(wil, "%s()\n", __func__);
157
158	pci_clear_master(pdev);
159	/* disable and release IRQ */
160	wil6210_fini_irq(wil, pdev->irq);
161	/* safe to call if no MSI */
162	pci_disable_msi(pdev);
163	/* TODO: disable HW */
164
165	return 0;
166}
167
168static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
169{
170	struct wil6210_priv *wil;
171	struct device *dev = &pdev->dev;
172	void __iomem *csr;
173	int rc;
174
175	/* check HW */
176	dev_info(&pdev->dev, WIL_NAME
177		 " device found [%04x:%04x] (rev %x)\n",
178		 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
179
180	if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
181		dev_err(&pdev->dev, "Not " WIL_NAME "? "
182			"BAR0 size is %lu while expecting %lu\n",
183			(ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
184		return -ENODEV;
185	}
186
187	rc = pci_enable_device(pdev);
188	if (rc) {
189		dev_err(&pdev->dev,
190			"pci_enable_device failed, retry with MSI only\n");
191		/* Work around for platforms that can't allocate IRQ:
192		 * retry with MSI only
193		 */
194		pdev->msi_enabled = 1;
195		rc = pci_enable_device(pdev);
196	}
197	if (rc)
198		return -ENODEV;
199	/* rollback to err_disable_pdev */
200
201	rc = pci_request_region(pdev, 0, WIL_NAME);
202	if (rc) {
203		dev_err(&pdev->dev, "pci_request_region failed\n");
204		goto err_disable_pdev;
205	}
206	/* rollback to err_release_reg */
207
208	csr = pci_ioremap_bar(pdev, 0);
209	if (!csr) {
210		dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
211		rc = -ENODEV;
212		goto err_release_reg;
213	}
214	/* rollback to err_iounmap */
215	dev_info(&pdev->dev, "CSR at %pR -> 0x%p\n", &pdev->resource[0], csr);
216
217	wil = wil_if_alloc(dev, csr);
218	if (IS_ERR(wil)) {
219		rc = (int)PTR_ERR(wil);
220		dev_err(dev, "wil_if_alloc failed: %d\n", rc);
221		goto err_iounmap;
222	}
223	/* rollback to if_free */
224
225	pci_set_drvdata(pdev, wil);
226	wil->pdev = pdev;
227	wil_set_capabilities(wil);
228	wil6210_clear_irq(wil);
229
230	wil->platform_handle =
231			wil_platform_init(&pdev->dev, &wil->platform_ops);
232
233	/* FW should raise IRQ when ready */
234	rc = wil_if_pcie_enable(wil);
235	if (rc) {
236		wil_err(wil, "Enable device failed\n");
237		goto if_free;
238	}
239	/* rollback to bus_disable */
240
241	rc = wil_if_add(wil);
242	if (rc) {
243		wil_err(wil, "wil_if_add failed: %d\n", rc);
244		goto bus_disable;
245	}
246
247	wil6210_debugfs_init(wil);
248
249
250	return 0;
251
252 bus_disable:
253	wil_if_pcie_disable(wil);
254 if_free:
255	if (wil->platform_ops.uninit)
256		wil->platform_ops.uninit(wil->platform_handle);
257	wil_if_free(wil);
258 err_iounmap:
259	pci_iounmap(pdev, csr);
260 err_release_reg:
261	pci_release_region(pdev, 0);
262 err_disable_pdev:
263	pci_disable_device(pdev);
264
265	return rc;
266}
267
268static void wil_pcie_remove(struct pci_dev *pdev)
269{
270	struct wil6210_priv *wil = pci_get_drvdata(pdev);
271	void __iomem *csr = wil->csr;
272
273	wil_dbg_misc(wil, "%s()\n", __func__);
274
275	wil6210_debugfs_remove(wil);
276	wil_if_remove(wil);
277	wil_if_pcie_disable(wil);
278	if (wil->platform_ops.uninit)
279		wil->platform_ops.uninit(wil->platform_handle);
280	wil_if_free(wil);
281	pci_iounmap(pdev, csr);
282	pci_release_region(pdev, 0);
283	pci_disable_device(pdev);
284}
285
286static const struct pci_device_id wil6210_pcie_ids[] = {
287	{ PCI_DEVICE(0x1ae9, 0x0310) },
288	{ PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
289	{ /* end: all zeroes */	},
290};
291MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
292
293static struct pci_driver wil6210_driver = {
294	.probe		= wil_pcie_probe,
295	.remove		= wil_pcie_remove,
296	.id_table	= wil6210_pcie_ids,
297	.name		= WIL_NAME,
298};
299
300module_pci_driver(wil6210_driver);
301
302MODULE_LICENSE("Dual BSD/GPL");
303MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
304MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");
305