blob: 3f26cf2001962b0a5bb5539e850ba4ab936b96e7 [file] [log] [blame]
Felix Fietkaue5b046d2010-12-02 10:27:01 +01001/*
2 * Copyright (c) 2008-2009 Atheros Communications 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/nl80211.h>
18#include <linux/pci.h>
19#include <linux/pci-aspm.h>
20#include "../ath.h"
21#include "ath5k.h"
22#include "debug.h"
23#include "base.h"
24#include "reg.h"
25
26/* Known PCI ids */
27static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
28 { PCI_VDEVICE(ATHEROS, 0x0207) }, /* 5210 early */
29 { PCI_VDEVICE(ATHEROS, 0x0007) }, /* 5210 */
30 { PCI_VDEVICE(ATHEROS, 0x0011) }, /* 5311 - this is on AHB bus !*/
31 { PCI_VDEVICE(ATHEROS, 0x0012) }, /* 5211 */
32 { PCI_VDEVICE(ATHEROS, 0x0013) }, /* 5212 */
33 { PCI_VDEVICE(3COM_2, 0x0013) }, /* 3com 5212 */
34 { PCI_VDEVICE(3COM, 0x0013) }, /* 3com 3CRDAG675 5212 */
35 { PCI_VDEVICE(ATHEROS, 0x1014) }, /* IBM minipci 5212 */
36 { PCI_VDEVICE(ATHEROS, 0x0014) }, /* 5212 combatible */
37 { PCI_VDEVICE(ATHEROS, 0x0015) }, /* 5212 combatible */
38 { PCI_VDEVICE(ATHEROS, 0x0016) }, /* 5212 combatible */
39 { PCI_VDEVICE(ATHEROS, 0x0017) }, /* 5212 combatible */
40 { PCI_VDEVICE(ATHEROS, 0x0018) }, /* 5212 combatible */
41 { PCI_VDEVICE(ATHEROS, 0x0019) }, /* 5212 combatible */
42 { PCI_VDEVICE(ATHEROS, 0x001a) }, /* 2413 Griffin-lite */
43 { PCI_VDEVICE(ATHEROS, 0x001b) }, /* 5413 Eagle */
44 { PCI_VDEVICE(ATHEROS, 0x001c) }, /* PCI-E cards */
45 { PCI_VDEVICE(ATHEROS, 0x001d) }, /* 2417 Nala */
46 { 0 }
47};
48
49/* return bus cachesize in 4B word units */
50static void ath5k_pci_read_cachesize(struct ath_common *common, int *csz)
51{
52 struct ath5k_softc *sc = (struct ath5k_softc *) common->priv;
53 u8 u8tmp;
54
55 pci_read_config_byte(sc->pdev, PCI_CACHE_LINE_SIZE, &u8tmp);
56 *csz = (int)u8tmp;
57
58 /*
59 * This check was put in to avoid "unplesant" consequences if
60 * the bootrom has not fully initialized all PCI devices.
61 * Sometimes the cache line size register is not set
62 */
63
64 if (*csz == 0)
65 *csz = L1_CACHE_BYTES >> 2; /* Use the default size */
66}
67
Felix Fietkau4aa5d782010-12-02 10:27:01 +010068/*
69 * Read from eeprom
70 */
71bool ath5k_pci_eeprom_read(struct ath_common *common, u32 offset, u16 *data)
72{
73 struct ath5k_hw *ah = (struct ath5k_hw *) common->ah;
74 u32 status, timeout;
75
76 /*
77 * Initialize EEPROM access
78 */
79 if (ah->ah_version == AR5K_AR5210) {
80 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
81 (void)ath5k_hw_reg_read(ah, AR5K_EEPROM_BASE + (4 * offset));
82 } else {
83 ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
84 AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
85 AR5K_EEPROM_CMD_READ);
86 }
87
88 for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
89 status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
90 if (status & AR5K_EEPROM_STAT_RDDONE) {
91 if (status & AR5K_EEPROM_STAT_RDERR)
92 return -EIO;
93 *data = (u16)(ath5k_hw_reg_read(ah, AR5K_EEPROM_DATA) &
94 0xffff);
95 return 0;
96 }
97 udelay(15);
98 }
99
100 return -ETIMEDOUT;
101}
102
Felix Fietkaue5b046d2010-12-02 10:27:01 +0100103/* Common ath_bus_opts structure */
104static const struct ath_bus_ops ath_pci_bus_ops = {
105 .ath_bus_type = ATH_PCI,
106 .read_cachesize = ath5k_pci_read_cachesize,
Felix Fietkau4aa5d782010-12-02 10:27:01 +0100107 .eeprom_read = ath5k_pci_eeprom_read,
Felix Fietkaue5b046d2010-12-02 10:27:01 +0100108};
109
110/********************\
111* PCI Initialization *
112\********************/
113
114static int __devinit
115ath5k_pci_probe(struct pci_dev *pdev,
116 const struct pci_device_id *id)
117{
118 void __iomem *mem;
119 struct ath5k_softc *sc;
120 struct ieee80211_hw *hw;
121 int ret;
122 u8 csz;
123
124 /*
125 * L0s needs to be disabled on all ath5k cards.
126 *
127 * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
128 * by default in the future in 2.6.36) this will also mean both L1 and
129 * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
130 * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
131 * though but cannot currently undue the effect of a blacklist, for
132 * details you can read pcie_aspm_sanity_check() and see how it adjusts
133 * the device link capability.
134 *
135 * It may be possible in the future to implement some PCI API to allow
136 * drivers to override blacklists for pre 1.1 PCIe but for now it is
137 * best to accept that both L0s and L1 will be disabled completely for
138 * distributions shipping with CONFIG_PCIEASPM rather than having this
139 * issue present. Motivation for adding this new API will be to help
140 * with power consumption for some of these devices.
141 */
142 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
143
144 ret = pci_enable_device(pdev);
145 if (ret) {
146 dev_err(&pdev->dev, "can't enable device\n");
147 goto err;
148 }
149
150 /* XXX 32-bit addressing only */
151 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
152 if (ret) {
153 dev_err(&pdev->dev, "32-bit DMA not available\n");
154 goto err_dis;
155 }
156
157 /*
158 * Cache line size is used to size and align various
159 * structures used to communicate with the hardware.
160 */
161 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
162 if (csz == 0) {
163 /*
164 * Linux 2.4.18 (at least) writes the cache line size
165 * register as a 16-bit wide register which is wrong.
166 * We must have this setup properly for rx buffer
167 * DMA to work so force a reasonable value here if it
168 * comes up zero.
169 */
170 csz = L1_CACHE_BYTES >> 2;
171 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
172 }
173 /*
174 * The default setting of latency timer yields poor results,
175 * set it to the value used by other systems. It may be worth
176 * tweaking this setting more.
177 */
178 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
179
180 /* Enable bus mastering */
181 pci_set_master(pdev);
182
183 /*
184 * Disable the RETRY_TIMEOUT register (0x41) to keep
185 * PCI Tx retries from interfering with C3 CPU state.
186 */
187 pci_write_config_byte(pdev, 0x41, 0);
188
189 ret = pci_request_region(pdev, 0, "ath5k");
190 if (ret) {
191 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
192 goto err_dis;
193 }
194
195 mem = pci_iomap(pdev, 0, 0);
196 if (!mem) {
197 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
198 ret = -EIO;
199 goto err_reg;
200 }
201
202 /*
203 * Allocate hw (mac80211 main struct)
204 * and hw->priv (driver private data)
205 */
206 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
207 if (hw == NULL) {
208 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
209 ret = -ENOMEM;
210 goto err_map;
211 }
212
213 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
214
215 sc = hw->priv;
216 sc->hw = hw;
217 sc->pdev = pdev;
218 sc->dev = &pdev->dev;
219 sc->irq = pdev->irq;
220 sc->devid = id->device;
221 sc->iobase = mem; /* So we can unmap it on detach */
222
223 /* Initialize */
224 ret = ath5k_init_softc(sc, &ath_pci_bus_ops);
225 if (ret)
226 goto err_free;
227
228 /* Set private data */
229 pci_set_drvdata(pdev, hw);
230
231 return 0;
232err_free:
233 ieee80211_free_hw(hw);
234err_map:
235 pci_iounmap(pdev, mem);
236err_reg:
237 pci_release_region(pdev, 0);
238err_dis:
239 pci_disable_device(pdev);
240err:
241 return ret;
242}
243
244static void __devexit
245ath5k_pci_remove(struct pci_dev *pdev)
246{
247 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
248 struct ath5k_softc *sc = hw->priv;
249
250 ath5k_deinit_softc(sc);
251 pci_iounmap(pdev, sc->iobase);
252 pci_release_region(pdev, 0);
253 pci_disable_device(pdev);
254 ieee80211_free_hw(hw);
255}
256
257#ifdef CONFIG_PM_SLEEP
258static int ath5k_pci_suspend(struct device *dev)
259{
260 struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
261
262 ath5k_led_off(sc);
263 return 0;
264}
265
266static int ath5k_pci_resume(struct device *dev)
267{
268 struct pci_dev *pdev = to_pci_dev(dev);
269 struct ath5k_softc *sc = pci_get_drvdata(pdev);
270
271 /*
272 * Suspend/Resume resets the PCI configuration space, so we have to
273 * re-disable the RETRY_TIMEOUT register (0x41) to keep
274 * PCI Tx retries from interfering with C3 CPU state
275 */
276 pci_write_config_byte(pdev, 0x41, 0);
277
278 ath5k_led_enable(sc);
279 return 0;
280}
281
282static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
283#define ATH5K_PM_OPS (&ath5k_pm_ops)
284#else
285#define ATH5K_PM_OPS NULL
286#endif /* CONFIG_PM_SLEEP */
287
288static struct pci_driver ath5k_pci_driver = {
289 .name = KBUILD_MODNAME,
290 .id_table = ath5k_pci_id_table,
291 .probe = ath5k_pci_probe,
292 .remove = __devexit_p(ath5k_pci_remove),
293 .driver.pm = ATH5K_PM_OPS,
294};
295
296/*
297 * Module init/exit functions
298 */
299static int __init
300init_ath5k_pci(void)
301{
302 int ret;
303
304 ret = pci_register_driver(&ath5k_pci_driver);
305 if (ret) {
306 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
307 return ret;
308 }
309
310 return 0;
311}
312
313static void __exit
314exit_ath5k_pci(void)
315{
316 pci_unregister_driver(&ath5k_pci_driver);
317}
318
319module_init(init_ath5k_pci);
320module_exit(exit_ath5k_pci);