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