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