pci.c 7.1 KB

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