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