pci.c 7.5 KB

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