pandora: defconfig: update
[pandora-kernel.git] / drivers / net / wireless / ath / ath9k / pci.c
1 /*
2  * Copyright (c) 2008-2011 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 <linux/ath9k_platform.h>
21 #include <linux/module.h>
22 #include "ath9k.h"
23
24 static DEFINE_PCI_DEVICE_TABLE(ath_pci_id_table) = {
25         { PCI_VDEVICE(ATHEROS, 0x0023) }, /* PCI   */
26         { PCI_VDEVICE(ATHEROS, 0x0024) }, /* PCI-E */
27         { PCI_VDEVICE(ATHEROS, 0x0027) }, /* PCI   */
28         { PCI_VDEVICE(ATHEROS, 0x0029) }, /* PCI   */
29         { PCI_VDEVICE(ATHEROS, 0x002A) }, /* PCI-E */
30         { PCI_VDEVICE(ATHEROS, 0x002B) }, /* PCI-E */
31         { PCI_VDEVICE(ATHEROS, 0x002C) }, /* PCI-E 802.11n bonded out */
32         { PCI_VDEVICE(ATHEROS, 0x002D) }, /* PCI   */
33         { PCI_VDEVICE(ATHEROS, 0x002E) }, /* PCI-E */
34         { PCI_VDEVICE(ATHEROS, 0x0030) }, /* PCI-E  AR9300 */
35         { PCI_VDEVICE(ATHEROS, 0x0032) }, /* PCI-E  AR9485 */
36         { PCI_VDEVICE(ATHEROS, 0x0033) }, /* PCI-E  AR9580 */
37         { PCI_VDEVICE(ATHEROS, 0x0034) }, /* PCI-E  AR9462 */
38         { PCI_VDEVICE(ATHEROS, 0x0037) }, /* PCI-E  AR1111/AR9485 */
39         { 0 }
40 };
41
42
43 /* return bus cachesize in 4B word units */
44 static void ath_pci_read_cachesize(struct ath_common *common, int *csz)
45 {
46         struct ath_softc *sc = (struct ath_softc *) common->priv;
47         u8 u8tmp;
48
49         pci_read_config_byte(to_pci_dev(sc->dev), PCI_CACHE_LINE_SIZE, &u8tmp);
50         *csz = (int)u8tmp;
51
52         /*
53          * This check was put in to avoid "unpleasant" consequences if
54          * the bootrom has not fully initialized all PCI devices.
55          * Sometimes the cache line size register is not set
56          */
57
58         if (*csz == 0)
59                 *csz = DEFAULT_CACHELINE >> 2;   /* Use the default size */
60 }
61
62 static bool ath_pci_eeprom_read(struct ath_common *common, u32 off, u16 *data)
63 {
64         struct ath_softc *sc = (struct ath_softc *) common->priv;
65         struct ath9k_platform_data *pdata = sc->dev->platform_data;
66
67         if (pdata) {
68                 if (off >= (ARRAY_SIZE(pdata->eeprom_data))) {
69                         ath_err(common,
70                                 "%s: eeprom read failed, offset %08x is out of range\n",
71                                 __func__, off);
72                 }
73
74                 *data = pdata->eeprom_data[off];
75         } else {
76                 struct ath_hw *ah = (struct ath_hw *) common->ah;
77
78                 common->ops->read(ah, AR5416_EEPROM_OFFSET +
79                                       (off << AR5416_EEPROM_S));
80
81                 if (!ath9k_hw_wait(ah,
82                                    AR_EEPROM_STATUS_DATA,
83                                    AR_EEPROM_STATUS_DATA_BUSY |
84                                    AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
85                                    AH_WAIT_TIMEOUT)) {
86                         return false;
87                 }
88
89                 *data = MS(common->ops->read(ah, AR_EEPROM_STATUS_DATA),
90                            AR_EEPROM_STATUS_DATA_VAL);
91         }
92
93         return true;
94 }
95
96 static void ath_pci_extn_synch_enable(struct ath_common *common)
97 {
98         struct ath_softc *sc = (struct ath_softc *) common->priv;
99         struct pci_dev *pdev = to_pci_dev(sc->dev);
100         u8 lnkctl;
101
102         pci_read_config_byte(pdev, sc->sc_ah->caps.pcie_lcr_offset, &lnkctl);
103         lnkctl |= PCI_EXP_LNKCTL_ES;
104         pci_write_config_byte(pdev, sc->sc_ah->caps.pcie_lcr_offset, lnkctl);
105 }
106
107 /* Need to be called after we discover btcoex capabilities */
108 static void ath_pci_aspm_init(struct ath_common *common)
109 {
110         struct ath_softc *sc = (struct ath_softc *) common->priv;
111         struct ath_hw *ah = sc->sc_ah;
112         struct pci_dev *pdev = to_pci_dev(sc->dev);
113         struct pci_dev *parent;
114         int pos;
115         u8 aspm;
116
117         pos = pci_pcie_cap(pdev);
118         if (!pos)
119                 return;
120
121         parent = pdev->bus->self;
122         if (!parent)
123                 return;
124
125         if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
126             (AR_SREV_9285(ah))) {
127                 /* Bluetooth coexistance requires disabling ASPM for AR9285. */
128                 pci_read_config_byte(pdev, pos + PCI_EXP_LNKCTL, &aspm);
129                 aspm &= ~(PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
130                 pci_write_config_byte(pdev, pos + PCI_EXP_LNKCTL, aspm);
131
132                 /*
133                  * Both upstream and downstream PCIe components should
134                  * have the same ASPM settings.
135                  */
136                 pos = pci_pcie_cap(parent);
137                 pci_read_config_byte(parent, pos + PCI_EXP_LNKCTL, &aspm);
138                 aspm &= ~(PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
139                 pci_write_config_byte(parent, pos + PCI_EXP_LNKCTL, aspm);
140
141                 return;
142         }
143
144         pos = pci_pcie_cap(parent);
145         pci_read_config_byte(parent, pos +  PCI_EXP_LNKCTL, &aspm);
146         if (aspm & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1)) {
147                 ah->aspm_enabled = true;
148                 /* Initialize PCIe PM and SERDES registers. */
149                 ath9k_hw_configpcipowersave(ah, false);
150         }
151 }
152
153 static const struct ath_bus_ops ath_pci_bus_ops = {
154         .ath_bus_type = ATH_PCI,
155         .read_cachesize = ath_pci_read_cachesize,
156         .eeprom_read = ath_pci_eeprom_read,
157         .extn_synch_en = ath_pci_extn_synch_enable,
158         .aspm_init = ath_pci_aspm_init,
159 };
160
161 static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
162 {
163         void __iomem *mem;
164         struct ath_softc *sc;
165         struct ieee80211_hw *hw;
166         u8 csz;
167         u32 val;
168         int ret = 0;
169         char hw_name[64];
170
171         if (pci_enable_device(pdev))
172                 return -EIO;
173
174         ret =  pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
175         if (ret) {
176                 printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
177                 goto err_dma;
178         }
179
180         ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
181         if (ret) {
182                 printk(KERN_ERR "ath9k: 32-bit DMA consistent "
183                         "DMA enable failed\n");
184                 goto err_dma;
185         }
186
187         /*
188          * Cache line size is used to size and align various
189          * structures used to communicate with the hardware.
190          */
191         pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
192         if (csz == 0) {
193                 /*
194                  * Linux 2.4.18 (at least) writes the cache line size
195                  * register as a 16-bit wide register which is wrong.
196                  * We must have this setup properly for rx buffer
197                  * DMA to work so force a reasonable value here if it
198                  * comes up zero.
199                  */
200                 csz = L1_CACHE_BYTES / sizeof(u32);
201                 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
202         }
203         /*
204          * The default setting of latency timer yields poor results,
205          * set it to the value used by other systems. It may be worth
206          * tweaking this setting more.
207          */
208         pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
209
210         pci_set_master(pdev);
211
212         /*
213          * Disable the RETRY_TIMEOUT register (0x41) to keep
214          * PCI Tx retries from interfering with C3 CPU state.
215          */
216         pci_read_config_dword(pdev, 0x40, &val);
217         if ((val & 0x0000ff00) != 0)
218                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
219
220         ret = pci_request_region(pdev, 0, "ath9k");
221         if (ret) {
222                 dev_err(&pdev->dev, "PCI memory region reserve error\n");
223                 ret = -ENODEV;
224                 goto err_region;
225         }
226
227         mem = pci_iomap(pdev, 0, 0);
228         if (!mem) {
229                 printk(KERN_ERR "PCI memory map error\n") ;
230                 ret = -EIO;
231                 goto err_iomap;
232         }
233
234         hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
235         if (!hw) {
236                 dev_err(&pdev->dev, "No memory for ieee80211_hw\n");
237                 ret = -ENOMEM;
238                 goto err_alloc_hw;
239         }
240
241         SET_IEEE80211_DEV(hw, &pdev->dev);
242         pci_set_drvdata(pdev, hw);
243
244         sc = hw->priv;
245         sc->hw = hw;
246         sc->dev = &pdev->dev;
247         sc->mem = mem;
248
249         /* Will be cleared in ath9k_start() */
250         sc->sc_flags |= SC_OP_INVALID;
251
252         ret = request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath9k", sc);
253         if (ret) {
254                 dev_err(&pdev->dev, "request_irq failed\n");
255                 goto err_irq;
256         }
257
258         sc->irq = pdev->irq;
259
260         ret = ath9k_init_device(id->device, sc, &ath_pci_bus_ops);
261         if (ret) {
262                 dev_err(&pdev->dev, "Failed to initialize device\n");
263                 goto err_init;
264         }
265
266         ath9k_hw_name(sc->sc_ah, hw_name, sizeof(hw_name));
267         wiphy_info(hw->wiphy, "%s mem=0x%lx, irq=%d\n",
268                    hw_name, (unsigned long)mem, pdev->irq);
269
270         return 0;
271
272 err_init:
273         free_irq(sc->irq, sc);
274 err_irq:
275         ieee80211_free_hw(hw);
276 err_alloc_hw:
277         pci_iounmap(pdev, mem);
278 err_iomap:
279         pci_release_region(pdev, 0);
280 err_region:
281         /* Nothing */
282 err_dma:
283         pci_disable_device(pdev);
284         return ret;
285 }
286
287 static void ath_pci_remove(struct pci_dev *pdev)
288 {
289         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
290         struct ath_softc *sc = hw->priv;
291         void __iomem *mem = sc->mem;
292
293         if (!is_ath9k_unloaded)
294                 sc->sc_ah->ah_flags |= AH_UNPLUGGED;
295         ath9k_deinit_device(sc);
296         free_irq(sc->irq, sc);
297         ieee80211_free_hw(sc->hw);
298
299         pci_iounmap(pdev, mem);
300         pci_disable_device(pdev);
301         pci_release_region(pdev, 0);
302 }
303
304 #ifdef CONFIG_PM
305
306 static int ath_pci_suspend(struct device *device)
307 {
308         struct pci_dev *pdev = to_pci_dev(device);
309         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
310         struct ath_softc *sc = hw->priv;
311
312         ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
313
314         /* The device has to be moved to FULLSLEEP forcibly.
315          * Otherwise the chip never moved to full sleep,
316          * when no interface is up.
317          */
318         ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
319
320         return 0;
321 }
322
323 static int ath_pci_resume(struct device *device)
324 {
325         struct pci_dev *pdev = to_pci_dev(device);
326         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
327         struct ath_softc *sc = hw->priv;
328         u32 val;
329
330         /*
331          * Suspend/Resume resets the PCI configuration space, so we have to
332          * re-disable the RETRY_TIMEOUT register (0x41) to keep
333          * PCI Tx retries from interfering with C3 CPU state
334          */
335         pci_read_config_dword(pdev, 0x40, &val);
336         if ((val & 0x0000ff00) != 0)
337                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
338
339         ath9k_ps_wakeup(sc);
340         /* Enable LED */
341         ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
342                             AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
343         ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
344
345           /*
346            * Reset key cache to sane defaults (all entries cleared) instead of
347            * semi-random values after suspend/resume.
348            */
349         ath9k_cmn_init_crypto(sc->sc_ah);
350         ath9k_ps_restore(sc);
351
352         sc->ps_idle = true;
353         ath_radio_disable(sc, hw);
354
355         return 0;
356 }
357
358 static const struct dev_pm_ops ath9k_pm_ops = {
359         .suspend = ath_pci_suspend,
360         .resume = ath_pci_resume,
361         .freeze = ath_pci_suspend,
362         .thaw = ath_pci_resume,
363         .poweroff = ath_pci_suspend,
364         .restore = ath_pci_resume,
365 };
366
367 #define ATH9K_PM_OPS    (&ath9k_pm_ops)
368
369 #else /* !CONFIG_PM */
370
371 #define ATH9K_PM_OPS    NULL
372
373 #endif /* !CONFIG_PM */
374
375
376 MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
377
378 static struct pci_driver ath_pci_driver = {
379         .name       = "ath9k",
380         .id_table   = ath_pci_id_table,
381         .probe      = ath_pci_probe,
382         .remove     = ath_pci_remove,
383         .driver.pm  = ATH9K_PM_OPS,
384 };
385
386 int ath_pci_init(void)
387 {
388         return pci_register_driver(&ath_pci_driver);
389 }
390
391 void ath_pci_exit(void)
392 {
393         pci_unregister_driver(&ath_pci_driver);
394 }