2 * Copyright (c) 2008-2011 Atheros Communications Inc.
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.
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.
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>
24 static DEFINE_PCI_DEVICE_TABLE(ath_pci_id_table) = {
25 { PCI_VDEVICE(ATHEROS, 0x0034) }, /* PCI-E AR9462 */
30 /* return bus cachesize in 4B word units */
31 static void ath_pci_read_cachesize(struct ath_common *common, int *csz)
33 struct ath_softc *sc = (struct ath_softc *) common->priv;
36 pci_read_config_byte(to_pci_dev(sc->dev), PCI_CACHE_LINE_SIZE, &u8tmp);
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
46 *csz = DEFAULT_CACHELINE >> 2; /* Use the default size */
49 static bool ath_pci_eeprom_read(struct ath_common *common, u32 off, u16 *data)
51 struct ath_softc *sc = (struct ath_softc *) common->priv;
52 struct ath9k_platform_data *pdata = sc->dev->platform_data;
55 if (off >= (ARRAY_SIZE(pdata->eeprom_data))) {
57 "%s: eeprom read failed, offset %08x is out of range\n",
61 *data = pdata->eeprom_data[off];
63 struct ath_hw *ah = (struct ath_hw *) common->ah;
65 common->ops->read(ah, AR5416_EEPROM_OFFSET +
66 (off << AR5416_EEPROM_S));
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,
76 *data = MS(common->ops->read(ah, AR_EEPROM_STATUS_DATA),
77 AR_EEPROM_STATUS_DATA_VAL);
83 static void ath_pci_extn_synch_enable(struct ath_common *common)
85 struct ath_softc *sc = (struct ath_softc *) common->priv;
86 struct pci_dev *pdev = to_pci_dev(sc->dev);
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);
94 /* Need to be called after we discover btcoex capabilities */
95 static void ath_pci_aspm_init(struct ath_common *common)
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;
104 if (!ah->is_pciexpress)
107 pos = pci_pcie_cap(pdev);
111 parent = pdev->bus->self;
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);
122 * Both upstream and downstream PCIe components should
123 * have the same ASPM settings.
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);
130 ath_info(common, "Disabling ASPM since BTCOEX is enabled\n");
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);
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,
152 static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
155 struct ath_softc *sc;
156 struct ieee80211_hw *hw;
162 if (pci_enable_device(pdev))
165 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
167 printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
171 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
173 printk(KERN_ERR "ath9k: 32-bit DMA consistent "
174 "DMA enable failed\n");
179 * Cache line size is used to size and align various
180 * structures used to communicate with the hardware.
182 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
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
191 csz = L1_CACHE_BYTES / sizeof(u32);
192 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
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.
199 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
201 pci_set_master(pdev);
204 * Disable the RETRY_TIMEOUT register (0x41) to keep
205 * PCI Tx retries from interfering with C3 CPU state.
207 pci_read_config_dword(pdev, 0x40, &val);
208 if ((val & 0x0000ff00) != 0)
209 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
211 ret = pci_request_region(pdev, 0, "ath9k");
213 dev_err(&pdev->dev, "PCI memory region reserve error\n");
218 mem = pci_iomap(pdev, 0, 0);
220 printk(KERN_ERR "PCI memory map error\n") ;
225 hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
227 dev_err(&pdev->dev, "No memory for ieee80211_hw\n");
232 SET_IEEE80211_DEV(hw, &pdev->dev);
233 pci_set_drvdata(pdev, hw);
237 sc->dev = &pdev->dev;
240 /* Will be cleared in ath9k_start() */
241 set_bit(SC_OP_INVALID, &sc->sc_flags);
243 ret = request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath9k", sc);
245 dev_err(&pdev->dev, "request_irq failed\n");
251 ret = ath9k_init_device(id->device, sc, &ath_pci_bus_ops);
253 dev_err(&pdev->dev, "Failed to initialize device\n");
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);
264 free_irq(sc->irq, sc);
266 ieee80211_free_hw(hw);
268 pci_iounmap(pdev, mem);
270 pci_release_region(pdev, 0);
274 pci_disable_device(pdev);
278 static void ath_pci_remove(struct pci_dev *pdev)
280 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
281 struct ath_softc *sc = hw->priv;
282 void __iomem *mem = sc->mem;
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);
290 pci_iounmap(pdev, mem);
291 pci_disable_device(pdev);
292 pci_release_region(pdev, 0);
297 static int ath_pci_suspend(struct device *device)
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;
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.
307 ath9k_btcoex_ath9k_hw_disable(sc->sc_ah);
308 ath9k_btcoex_ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
313 static int ath_pci_resume(struct device *device)
315 struct pci_dev *pdev = to_pci_dev(device);
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
323 pci_read_config_dword(pdev, 0x40, &val);
324 if ((val & 0x0000ff00) != 0)
325 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
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,
339 #define ATH9K_PM_OPS (&ath9k_pm_ops)
341 #else /* !CONFIG_PM */
343 #define ATH9K_PM_OPS NULL
345 #endif /* !CONFIG_PM */
348 MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
350 static struct pci_driver ath_pci_driver = {
352 .id_table = ath_pci_id_table,
353 .probe = ath_pci_probe,
354 .remove = ath_pci_remove,
355 .driver.pm = ATH9K_PM_OPS,
358 int ath_pci_init(void)
360 return pci_register_driver(&ath_pci_driver);
363 void ath_pci_exit(void)
365 pci_unregister_driver(&ath_pci_driver);