pci.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2008-2009 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. { PCI_VDEVICE(ATHEROS, 0x002D) }, /* PCI */
  27. { PCI_VDEVICE(ATHEROS, 0x002E) }, /* 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, &u8tmp);
  35. *csz = (int)u8tmp;
  36. /*
  37. * This check was put in to avoid "unplesant" consequences if
  38. * the bootrom has not fully initialized all PCI devices.
  39. * Sometimes the cache line size register is not set
  40. */
  41. if (*csz == 0)
  42. *csz = DEFAULT_CACHELINE >> 2; /* Use the default size */
  43. }
  44. static void ath_pci_cleanup(struct ath_softc *sc)
  45. {
  46. struct pci_dev *pdev = to_pci_dev(sc->dev);
  47. pci_iounmap(pdev, sc->mem);
  48. pci_disable_device(pdev);
  49. pci_release_region(pdev, 0);
  50. }
  51. static bool ath_pci_eeprom_read(struct ath_hw *ah, u32 off, u16 *data)
  52. {
  53. (void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
  54. if (!ath9k_hw_wait(ah,
  55. AR_EEPROM_STATUS_DATA,
  56. AR_EEPROM_STATUS_DATA_BUSY |
  57. AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
  58. AH_WAIT_TIMEOUT)) {
  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_wiphy *aphy;
  74. struct ath_softc *sc;
  75. struct ieee80211_hw *hw;
  76. u8 csz;
  77. u16 subsysid;
  78. u32 val;
  79. int ret = 0;
  80. struct ath_hw *ah;
  81. if (pci_enable_device(pdev))
  82. return -EIO;
  83. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  84. if (ret) {
  85. printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
  86. goto bad;
  87. }
  88. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  89. if (ret) {
  90. printk(KERN_ERR "ath9k: 32-bit DMA consistent "
  91. "DMA enable failed\n");
  92. goto bad;
  93. }
  94. /*
  95. * Cache line size is used to size and align various
  96. * structures used to communicate with the hardware.
  97. */
  98. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  99. if (csz == 0) {
  100. /*
  101. * Linux 2.4.18 (at least) writes the cache line size
  102. * register as a 16-bit wide register which is wrong.
  103. * We must have this setup properly for rx buffer
  104. * DMA to work so force a reasonable value here if it
  105. * comes up zero.
  106. */
  107. csz = L1_CACHE_BYTES / sizeof(u32);
  108. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  109. }
  110. /*
  111. * The default setting of latency timer yields poor results,
  112. * set it to the value used by other systems. It may be worth
  113. * tweaking this setting more.
  114. */
  115. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  116. pci_set_master(pdev);
  117. /*
  118. * Disable the RETRY_TIMEOUT register (0x41) to keep
  119. * PCI Tx retries from interfering with C3 CPU state.
  120. */
  121. pci_read_config_dword(pdev, 0x40, &val);
  122. if ((val & 0x0000ff00) != 0)
  123. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  124. ret = pci_request_region(pdev, 0, "ath9k");
  125. if (ret) {
  126. dev_err(&pdev->dev, "PCI memory region reserve error\n");
  127. ret = -ENODEV;
  128. goto bad;
  129. }
  130. mem = pci_iomap(pdev, 0, 0);
  131. if (!mem) {
  132. printk(KERN_ERR "PCI memory map error\n") ;
  133. ret = -EIO;
  134. goto bad1;
  135. }
  136. hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy) +
  137. sizeof(struct ath_softc), &ath9k_ops);
  138. if (!hw) {
  139. dev_err(&pdev->dev, "no memory for ieee80211_hw\n");
  140. ret = -ENOMEM;
  141. goto bad2;
  142. }
  143. SET_IEEE80211_DEV(hw, &pdev->dev);
  144. pci_set_drvdata(pdev, hw);
  145. aphy = hw->priv;
  146. sc = (struct ath_softc *) (aphy + 1);
  147. aphy->sc = sc;
  148. aphy->hw = hw;
  149. sc->pri_wiphy = aphy;
  150. sc->hw = hw;
  151. sc->dev = &pdev->dev;
  152. sc->mem = mem;
  153. sc->bus_ops = &ath_pci_bus_ops;
  154. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &subsysid);
  155. ret = ath_init_device(id->device, sc, subsysid);
  156. if (ret) {
  157. dev_err(&pdev->dev, "failed to initialize device\n");
  158. goto bad3;
  159. }
  160. /* setup interrupt service routine */
  161. ret = request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath9k", sc);
  162. if (ret) {
  163. dev_err(&pdev->dev, "request_irq failed\n");
  164. goto bad4;
  165. }
  166. sc->irq = pdev->irq;
  167. ah = sc->sc_ah;
  168. printk(KERN_INFO
  169. "%s: Atheros AR%s MAC/BB Rev:%x "
  170. "AR%s RF Rev:%x: mem=0x%lx, irq=%d\n",
  171. wiphy_name(hw->wiphy),
  172. ath_mac_bb_name(ah->hw_version.macVersion),
  173. ah->hw_version.macRev,
  174. ath_rf_name((ah->hw_version.analog5GhzRev & AR_RADIO_SREV_MAJOR)),
  175. ah->hw_version.phyRev,
  176. (unsigned long)mem, pdev->irq);
  177. return 0;
  178. bad4:
  179. ath_detach(sc);
  180. bad3:
  181. ieee80211_free_hw(hw);
  182. bad2:
  183. pci_iounmap(pdev, mem);
  184. bad1:
  185. pci_release_region(pdev, 0);
  186. bad:
  187. pci_disable_device(pdev);
  188. return ret;
  189. }
  190. static void ath_pci_remove(struct pci_dev *pdev)
  191. {
  192. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  193. struct ath_wiphy *aphy = hw->priv;
  194. struct ath_softc *sc = aphy->sc;
  195. ath_cleanup(sc);
  196. }
  197. #ifdef CONFIG_PM
  198. static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  199. {
  200. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  201. struct ath_wiphy *aphy = hw->priv;
  202. struct ath_softc *sc = aphy->sc;
  203. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  204. pci_save_state(pdev);
  205. pci_disable_device(pdev);
  206. pci_set_power_state(pdev, PCI_D3hot);
  207. return 0;
  208. }
  209. static int ath_pci_resume(struct pci_dev *pdev)
  210. {
  211. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  212. struct ath_wiphy *aphy = hw->priv;
  213. struct ath_softc *sc = aphy->sc;
  214. u32 val;
  215. int err;
  216. pci_restore_state(pdev);
  217. err = pci_enable_device(pdev);
  218. if (err)
  219. return err;
  220. /*
  221. * Suspend/Resume resets the PCI configuration space, so we have to
  222. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  223. * PCI Tx retries from interfering with C3 CPU state
  224. */
  225. pci_read_config_dword(pdev, 0x40, &val);
  226. if ((val & 0x0000ff00) != 0)
  227. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  228. /* Enable LED */
  229. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  230. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  231. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  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. }