blob: 05c1c9d6f9c229b9f47a995377d4fbe3418c1032 [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
2 * Copyright (c) 2012 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
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080017#include <linux/module.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080018#include <linux/pci.h>
19#include <linux/moduleparam.h>
20
21#include "wil6210.h"
22
23static int use_msi = 1;
24module_param(use_msi, int, S_IRUGO);
25MODULE_PARM_DESC(use_msi,
26 " Use MSI interrupt: "
27 "0 - don't, 1 - (default) - single, or 3");
28
Vladimir Kondratiev94b7b642014-06-16 19:37:06 +030029static bool debug_fw; /* = false; */
30module_param(debug_fw, bool, S_IRUGO);
31MODULE_PARM_DESC(debug_fw, " load driver if FW not ready. For FW debug");
32
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080033/* Bus ops */
34static int wil_if_pcie_enable(struct wil6210_priv *wil)
35{
36 struct pci_dev *pdev = wil->pdev;
37 int rc;
38
39 pci_set_master(pdev);
40
41 /*
42 * how many MSI interrupts to request?
43 */
44 switch (use_msi) {
45 case 3:
46 case 1:
Alexander Gordeevb4b39062014-02-18 11:12:04 +010047 wil_dbg_misc(wil, "Setup %d MSI interrupts\n", use_msi);
48 break;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080049 case 0:
Alexander Gordeevb4b39062014-02-18 11:12:04 +010050 wil_dbg_misc(wil, "MSI interrupts disabled, use INTx\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080051 break;
52 default:
Alexander Gordeevb4b39062014-02-18 11:12:04 +010053 wil_err(wil, "Invalid use_msi=%d, default to 1\n", use_msi);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080054 use_msi = 1;
55 }
Alexander Gordeevb4b39062014-02-18 11:12:04 +010056
57 if (use_msi == 3 && pci_enable_msi_range(pdev, 3, 3) < 0) {
58 wil_err(wil, "3 MSI mode failed, try 1 MSI\n");
59 use_msi = 1;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080060 }
61
Alexander Gordeevb4b39062014-02-18 11:12:04 +010062 if (use_msi == 1 && pci_enable_msi(pdev)) {
63 wil_err(wil, "pci_enable_msi failed, use INTx\n");
64 use_msi = 0;
65 }
66
67 wil->n_msi = use_msi;
68
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080069 rc = wil6210_init_irq(wil, pdev->irq);
70 if (rc)
71 goto stop_master;
72
73 /* need reset here to obtain MAC */
Vladimir Kondratiev097638a2014-03-17 15:34:25 +020074 mutex_lock(&wil->mutex);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080075 rc = wil_reset(wil);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +020076 mutex_unlock(&wil->mutex);
Vladimir Kondratiev94b7b642014-06-16 19:37:06 +030077 if (debug_fw)
78 rc = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080079 if (rc)
80 goto release_irq;
81
82 return 0;
83
84 release_irq:
85 wil6210_fini_irq(wil, pdev->irq);
86 /* safe to call if no MSI */
87 pci_disable_msi(pdev);
88 stop_master:
89 pci_clear_master(pdev);
90 return rc;
91}
92
93static int wil_if_pcie_disable(struct wil6210_priv *wil)
94{
95 struct pci_dev *pdev = wil->pdev;
96
97 pci_clear_master(pdev);
98 /* disable and release IRQ */
99 wil6210_fini_irq(wil, pdev->irq);
100 /* safe to call if no MSI */
101 pci_disable_msi(pdev);
102 /* TODO: disable HW */
103
104 return 0;
105}
106
107static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
108{
109 struct wil6210_priv *wil;
110 struct device *dev = &pdev->dev;
111 void __iomem *csr;
112 int rc;
113
114 /* check HW */
115 dev_info(&pdev->dev, WIL_NAME " device found [%04x:%04x] (rev %x)\n",
116 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
117
118 if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
119 dev_err(&pdev->dev, "Not " WIL_NAME "? "
120 "BAR0 size is %lu while expecting %lu\n",
121 (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
122 return -ENODEV;
123 }
124
125 rc = pci_enable_device(pdev);
126 if (rc) {
127 dev_err(&pdev->dev, "pci_enable_device failed\n");
128 return -ENODEV;
129 }
130 /* rollback to err_disable_pdev */
131
132 rc = pci_request_region(pdev, 0, WIL_NAME);
133 if (rc) {
134 dev_err(&pdev->dev, "pci_request_region failed\n");
135 goto err_disable_pdev;
136 }
137 /* rollback to err_release_reg */
138
139 csr = pci_ioremap_bar(pdev, 0);
140 if (!csr) {
141 dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
142 rc = -ENODEV;
143 goto err_release_reg;
144 }
145 /* rollback to err_iounmap */
Vladimir Kondratiev39c52ee2014-05-27 14:45:49 +0300146 dev_info(&pdev->dev, "CSR at %pR -> 0x%p\n", &pdev->resource[0], csr);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800147
148 wil = wil_if_alloc(dev, csr);
149 if (IS_ERR(wil)) {
150 rc = (int)PTR_ERR(wil);
151 dev_err(dev, "wil_if_alloc failed: %d\n", rc);
152 goto err_iounmap;
153 }
154 /* rollback to if_free */
155
156 pci_set_drvdata(pdev, wil);
157 wil->pdev = pdev;
158
Vladimir Kondratievf4b5a802014-03-17 15:34:13 +0200159 wil6210_clear_irq(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800160 /* FW should raise IRQ when ready */
161 rc = wil_if_pcie_enable(wil);
162 if (rc) {
163 wil_err(wil, "Enable device failed\n");
164 goto if_free;
165 }
166 /* rollback to bus_disable */
167
168 rc = wil_if_add(wil);
169 if (rc) {
170 wil_err(wil, "wil_if_add failed: %d\n", rc);
171 goto bus_disable;
172 }
173
174 wil6210_debugfs_init(wil);
175
176 /* check FW is alive */
177 wmi_echo(wil);
178
179 return 0;
180
181 bus_disable:
182 wil_if_pcie_disable(wil);
183 if_free:
184 wil_if_free(wil);
185 err_iounmap:
186 pci_iounmap(pdev, csr);
187 err_release_reg:
188 pci_release_region(pdev, 0);
189 err_disable_pdev:
190 pci_disable_device(pdev);
191
192 return rc;
193}
194
195static void wil_pcie_remove(struct pci_dev *pdev)
196{
197 struct wil6210_priv *wil = pci_get_drvdata(pdev);
198
199 wil6210_debugfs_remove(wil);
200 wil_if_pcie_disable(wil);
201 wil_if_remove(wil);
202 wil_if_free(wil);
203 pci_iounmap(pdev, wil->csr);
204 pci_release_region(pdev, 0);
205 pci_disable_device(pdev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800206}
207
208static DEFINE_PCI_DEVICE_TABLE(wil6210_pcie_ids) = {
209 { PCI_DEVICE(0x1ae9, 0x0301) },
210 { /* end: all zeroes */ },
211};
212MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
213
214static struct pci_driver wil6210_driver = {
215 .probe = wil_pcie_probe,
216 .remove = wil_pcie_remove,
217 .id_table = wil6210_pcie_ids,
218 .name = WIL_NAME,
219};
220
221module_pci_driver(wil6210_driver);
222
223MODULE_LICENSE("Dual BSD/GPL");
224MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
225MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");