pci.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2008 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. #include <linux/nl80211.h>
  17. #include <linux/pci.h>
  18. #include "core.h"
  19. #include "reg.h"
  20. #include "hw.h"
  21. static struct pci_device_id ath_pci_id_table[] __devinitdata = {
  22. { PCI_VDEVICE(ATHEROS, 0x0023) }, /* PCI */
  23. { PCI_VDEVICE(ATHEROS, 0x0024) }, /* PCI-E */
  24. { PCI_VDEVICE(ATHEROS, 0x0027) }, /* PCI */
  25. { PCI_VDEVICE(ATHEROS, 0x0029) }, /* PCI */
  26. { PCI_VDEVICE(ATHEROS, 0x002A) }, /* PCI-E */
  27. { PCI_VDEVICE(ATHEROS, 0x002B) }, /* PCI-E */
  28. { 0 }
  29. };
  30. /* return bus cachesize in 4B word units */
  31. static void ath_pci_read_cachesize(struct ath_softc *sc, int *csz)
  32. {
  33. u8 u8tmp;
  34. pci_read_config_byte(to_pci_dev(sc->dev), PCI_CACHE_LINE_SIZE,
  35. (u8 *)&u8tmp);
  36. *csz = (int)u8tmp;
  37. /*
  38. * This check was put in to avoid "unplesant" consequences if
  39. * the bootrom has not fully initialized all PCI devices.
  40. * Sometimes the cache line size register is not set
  41. */
  42. if (*csz == 0)
  43. *csz = DEFAULT_CACHELINE >> 2; /* Use the default size */
  44. }
  45. static void ath_pci_cleanup(struct ath_softc *sc)
  46. {
  47. struct pci_dev *pdev = to_pci_dev(sc->dev);
  48. pci_iounmap(pdev, sc->mem);
  49. pci_release_region(pdev, 0);
  50. pci_disable_device(pdev);
  51. }
  52. static bool ath_pci_eeprom_read(struct ath_hal *ah, u32 off, u16 *data)
  53. {
  54. (void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
  55. if (!ath9k_hw_wait(ah,
  56. AR_EEPROM_STATUS_DATA,
  57. AR_EEPROM_STATUS_DATA_BUSY |
  58. AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0)) {
  59. return false;
  60. }
  61. *data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
  62. AR_EEPROM_STATUS_DATA_VAL);
  63. return true;
  64. }
  65. static struct ath_bus_ops ath_pci_bus_ops = {
  66. .read_cachesize = ath_pci_read_cachesize,
  67. .cleanup = ath_pci_cleanup,
  68. .eeprom_read = ath_pci_eeprom_read,
  69. };
  70. static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  71. {
  72. void __iomem *mem;
  73. struct ath_softc *sc;
  74. struct ieee80211_hw *hw;
  75. u8 csz;
  76. u32 val;
  77. int ret = 0;
  78. struct ath_hal *ah;
  79. if (pci_enable_device(pdev))
  80. return -EIO;
  81. ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  82. if (ret) {
  83. printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
  84. goto bad;
  85. }
  86. ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  87. if (ret) {
  88. printk(KERN_ERR "ath9k: 32-bit DMA consistent "
  89. "DMA enable failed\n");
  90. goto bad;
  91. }
  92. /*
  93. * Cache line size is used to size and align various
  94. * structures used to communicate with the hardware.
  95. */
  96. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  97. if (csz == 0) {
  98. /*
  99. * Linux 2.4.18 (at least) writes the cache line size
  100. * register as a 16-bit wide register which is wrong.
  101. * We must have this setup properly for rx buffer
  102. * DMA to work so force a reasonable value here if it
  103. * comes up zero.
  104. */
  105. csz = L1_CACHE_BYTES / sizeof(u32);
  106. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  107. }
  108. /*
  109. * The default setting of latency timer yields poor results,
  110. * set it to the value used by other systems. It may be worth
  111. * tweaking this setting more.
  112. */
  113. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  114. pci_set_master(pdev);
  115. /*
  116. * Disable the RETRY_TIMEOUT register (0x41) to keep
  117. * PCI Tx retries from interfering with C3 CPU state.
  118. */
  119. pci_read_config_dword(pdev, 0x40, &val);
  120. if ((val & 0x0000ff00) != 0)
  121. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  122. ret = pci_request_region(pdev, 0, "ath9k");
  123. if (ret) {
  124. dev_err(&pdev->dev, "PCI memory region reserve error\n");
  125. ret = -ENODEV;
  126. goto bad;
  127. }
  128. mem = pci_iomap(pdev, 0, 0);
  129. if (!mem) {
  130. printk(KERN_ERR "PCI memory map error\n") ;
  131. ret = -EIO;
  132. goto bad1;
  133. }
  134. hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
  135. if (hw == NULL) {
  136. printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
  137. goto bad2;
  138. }
  139. SET_IEEE80211_DEV(hw, &pdev->dev);
  140. pci_set_drvdata(pdev, hw);
  141. sc = hw->priv;
  142. sc->hw = hw;
  143. sc->dev = &pdev->dev;
  144. sc->mem = mem;
  145. sc->bus_ops = &ath_pci_bus_ops;
  146. if (ath_attach(id->device, sc) != 0) {
  147. ret = -ENODEV;
  148. goto bad3;
  149. }
  150. /* setup interrupt service routine */
  151. if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
  152. printk(KERN_ERR "%s: request_irq failed\n",
  153. wiphy_name(hw->wiphy));
  154. ret = -EIO;
  155. goto bad4;
  156. }
  157. sc->irq = pdev->irq;
  158. ah = sc->sc_ah;
  159. printk(KERN_INFO
  160. "%s: Atheros AR%s MAC/BB Rev:%x "
  161. "AR%s RF Rev:%x: mem=0x%lx, irq=%d\n",
  162. wiphy_name(hw->wiphy),
  163. ath_mac_bb_name(ah->ah_macVersion),
  164. ah->ah_macRev,
  165. ath_rf_name((ah->ah_analog5GhzRev & AR_RADIO_SREV_MAJOR)),
  166. ah->ah_phyRev,
  167. (unsigned long)mem, pdev->irq);
  168. return 0;
  169. bad4:
  170. ath_detach(sc);
  171. bad3:
  172. ieee80211_free_hw(hw);
  173. bad2:
  174. pci_iounmap(pdev, mem);
  175. bad1:
  176. pci_release_region(pdev, 0);
  177. bad:
  178. pci_disable_device(pdev);
  179. return ret;
  180. }
  181. static void ath_pci_remove(struct pci_dev *pdev)
  182. {
  183. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  184. struct ath_softc *sc = hw->priv;
  185. ath_cleanup(sc);
  186. }
  187. #ifdef CONFIG_PM
  188. static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  189. {
  190. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  191. struct ath_softc *sc = hw->priv;
  192. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  193. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  194. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  195. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  196. #endif
  197. pci_save_state(pdev);
  198. pci_disable_device(pdev);
  199. pci_set_power_state(pdev, PCI_D3hot);
  200. return 0;
  201. }
  202. static int ath_pci_resume(struct pci_dev *pdev)
  203. {
  204. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  205. struct ath_softc *sc = hw->priv;
  206. u32 val;
  207. int err;
  208. err = pci_enable_device(pdev);
  209. if (err)
  210. return err;
  211. pci_restore_state(pdev);
  212. /*
  213. * Suspend/Resume resets the PCI configuration space, so we have to
  214. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  215. * PCI Tx retries from interfering with C3 CPU state
  216. */
  217. pci_read_config_dword(pdev, 0x40, &val);
  218. if ((val & 0x0000ff00) != 0)
  219. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  220. /* Enable LED */
  221. ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
  222. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  223. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  224. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  225. /*
  226. * check the h/w rfkill state on resume
  227. * and start the rfkill poll timer
  228. */
  229. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  230. queue_delayed_work(sc->hw->workqueue,
  231. &sc->rf_kill.rfkill_poll, 0);
  232. #endif
  233. return 0;
  234. }
  235. #endif /* CONFIG_PM */
  236. MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
  237. static struct pci_driver ath_pci_driver = {
  238. .name = "ath9k",
  239. .id_table = ath_pci_id_table,
  240. .probe = ath_pci_probe,
  241. .remove = ath_pci_remove,
  242. #ifdef CONFIG_PM
  243. .suspend = ath_pci_suspend,
  244. .resume = ath_pci_resume,
  245. #endif /* CONFIG_PM */
  246. };
  247. int __init ath_pci_init(void)
  248. {
  249. return pci_register_driver(&ath_pci_driver);
  250. }
  251. void ath_pci_exit(void)
  252. {
  253. pci_unregister_driver(&ath_pci_driver);
  254. }