blob: e1c8cc4a4b92261bf37a3410ebd9cc4175082f9a [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/debugfs.h>
19#include <linux/pci.h>
20#include <linux/moduleparam.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
30/* Bus ops */
31static int wil_if_pcie_enable(struct wil6210_priv *wil)
32{
33 struct pci_dev *pdev = wil->pdev;
34 int rc;
35
36 pci_set_master(pdev);
37
38 /*
39 * how many MSI interrupts to request?
40 */
41 switch (use_msi) {
42 case 3:
43 case 1:
Alexander Gordeevb4b39062014-02-18 11:12:04 +010044 wil_dbg_misc(wil, "Setup %d MSI interrupts\n", use_msi);
45 break;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080046 case 0:
Alexander Gordeevb4b39062014-02-18 11:12:04 +010047 wil_dbg_misc(wil, "MSI interrupts disabled, use INTx\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080048 break;
49 default:
Alexander Gordeevb4b39062014-02-18 11:12:04 +010050 wil_err(wil, "Invalid use_msi=%d, default to 1\n", use_msi);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080051 use_msi = 1;
52 }
Alexander Gordeevb4b39062014-02-18 11:12:04 +010053
54 if (use_msi == 3 && pci_enable_msi_range(pdev, 3, 3) < 0) {
55 wil_err(wil, "3 MSI mode failed, try 1 MSI\n");
56 use_msi = 1;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080057 }
58
Alexander Gordeevb4b39062014-02-18 11:12:04 +010059 if (use_msi == 1 && pci_enable_msi(pdev)) {
60 wil_err(wil, "pci_enable_msi failed, use INTx\n");
61 use_msi = 0;
62 }
63
64 wil->n_msi = use_msi;
65
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080066 rc = wil6210_init_irq(wil, pdev->irq);
67 if (rc)
68 goto stop_master;
69
70 /* need reset here to obtain MAC */
71 rc = wil_reset(wil);
72 if (rc)
73 goto release_irq;
74
75 return 0;
76
77 release_irq:
78 wil6210_fini_irq(wil, pdev->irq);
79 /* safe to call if no MSI */
80 pci_disable_msi(pdev);
81 stop_master:
82 pci_clear_master(pdev);
83 return rc;
84}
85
86static int wil_if_pcie_disable(struct wil6210_priv *wil)
87{
88 struct pci_dev *pdev = wil->pdev;
89
90 pci_clear_master(pdev);
91 /* disable and release IRQ */
92 wil6210_fini_irq(wil, pdev->irq);
93 /* safe to call if no MSI */
94 pci_disable_msi(pdev);
95 /* TODO: disable HW */
96
97 return 0;
98}
99
100static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
101{
102 struct wil6210_priv *wil;
103 struct device *dev = &pdev->dev;
104 void __iomem *csr;
105 int rc;
106
107 /* check HW */
108 dev_info(&pdev->dev, WIL_NAME " device found [%04x:%04x] (rev %x)\n",
109 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
110
111 if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
112 dev_err(&pdev->dev, "Not " WIL_NAME "? "
113 "BAR0 size is %lu while expecting %lu\n",
114 (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
115 return -ENODEV;
116 }
117
118 rc = pci_enable_device(pdev);
119 if (rc) {
120 dev_err(&pdev->dev, "pci_enable_device failed\n");
121 return -ENODEV;
122 }
123 /* rollback to err_disable_pdev */
124
125 rc = pci_request_region(pdev, 0, WIL_NAME);
126 if (rc) {
127 dev_err(&pdev->dev, "pci_request_region failed\n");
128 goto err_disable_pdev;
129 }
130 /* rollback to err_release_reg */
131
132 csr = pci_ioremap_bar(pdev, 0);
133 if (!csr) {
134 dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
135 rc = -ENODEV;
136 goto err_release_reg;
137 }
138 /* rollback to err_iounmap */
139 dev_info(&pdev->dev, "CSR at %pR -> %p\n", &pdev->resource[0], csr);
140
141 wil = wil_if_alloc(dev, csr);
142 if (IS_ERR(wil)) {
143 rc = (int)PTR_ERR(wil);
144 dev_err(dev, "wil_if_alloc failed: %d\n", rc);
145 goto err_iounmap;
146 }
147 /* rollback to if_free */
148
149 pci_set_drvdata(pdev, wil);
150 wil->pdev = pdev;
151
152 /* FW should raise IRQ when ready */
153 rc = wil_if_pcie_enable(wil);
154 if (rc) {
155 wil_err(wil, "Enable device failed\n");
156 goto if_free;
157 }
158 /* rollback to bus_disable */
159
160 rc = wil_if_add(wil);
161 if (rc) {
162 wil_err(wil, "wil_if_add failed: %d\n", rc);
163 goto bus_disable;
164 }
165
166 wil6210_debugfs_init(wil);
167
168 /* check FW is alive */
169 wmi_echo(wil);
170
171 return 0;
172
173 bus_disable:
174 wil_if_pcie_disable(wil);
175 if_free:
176 wil_if_free(wil);
177 err_iounmap:
178 pci_iounmap(pdev, csr);
179 err_release_reg:
180 pci_release_region(pdev, 0);
181 err_disable_pdev:
182 pci_disable_device(pdev);
183
184 return rc;
185}
186
187static void wil_pcie_remove(struct pci_dev *pdev)
188{
189 struct wil6210_priv *wil = pci_get_drvdata(pdev);
190
191 wil6210_debugfs_remove(wil);
192 wil_if_pcie_disable(wil);
193 wil_if_remove(wil);
194 wil_if_free(wil);
195 pci_iounmap(pdev, wil->csr);
196 pci_release_region(pdev, 0);
197 pci_disable_device(pdev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800198}
199
200static DEFINE_PCI_DEVICE_TABLE(wil6210_pcie_ids) = {
201 { PCI_DEVICE(0x1ae9, 0x0301) },
202 { /* end: all zeroes */ },
203};
204MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
205
206static struct pci_driver wil6210_driver = {
207 .probe = wil_pcie_probe,
208 .remove = wil_pcie_remove,
209 .id_table = wil6210_pcie_ids,
210 .name = WIL_NAME,
211};
212
213module_pci_driver(wil6210_driver);
214
215MODULE_LICENSE("Dual BSD/GPL");
216MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
217MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");