pci.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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,
  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_disable_device(pdev);
  50. pci_release_region(pdev, 0);
  51. }
  52. static bool ath_pci_eeprom_read(struct ath_hw *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. AH_WAIT_TIMEOUT)) {
  60. return false;
  61. }
  62. *data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
  63. AR_EEPROM_STATUS_DATA_VAL);
  64. return true;
  65. }
  66. static struct ath_bus_ops ath_pci_bus_ops = {
  67. .read_cachesize = ath_pci_read_cachesize,
  68. .cleanup = ath_pci_cleanup,
  69. .eeprom_read = ath_pci_eeprom_read,
  70. };
  71. static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  72. {
  73. void __iomem *mem;
  74. struct ath_wiphy *aphy;
  75. struct ath_softc *sc;
  76. struct ieee80211_hw *hw;
  77. u8 csz;
  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 == NULL) {
  139. printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
  140. goto bad2;
  141. }
  142. SET_IEEE80211_DEV(hw, &pdev->dev);
  143. pci_set_drvdata(pdev, hw);
  144. aphy = hw->priv;
  145. sc = (struct ath_softc *) (aphy + 1);
  146. aphy->sc = sc;
  147. aphy->hw = hw;
  148. sc->pri_wiphy = aphy;
  149. sc->hw = hw;
  150. sc->dev = &pdev->dev;
  151. sc->mem = mem;
  152. sc->bus_ops = &ath_pci_bus_ops;
  153. if (ath_attach(id->device, sc) != 0) {
  154. ret = -ENODEV;
  155. goto bad3;
  156. }
  157. /* setup interrupt service routine */
  158. if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
  159. printk(KERN_ERR "%s: request_irq failed\n",
  160. wiphy_name(hw->wiphy));
  161. ret = -EIO;
  162. goto bad4;
  163. }
  164. sc->irq = pdev->irq;
  165. ah = sc->sc_ah;
  166. printk(KERN_INFO
  167. "%s: Atheros AR%s MAC/BB Rev:%x "
  168. "AR%s RF Rev:%x: mem=0x%lx, irq=%d\n",
  169. wiphy_name(hw->wiphy),
  170. ath_mac_bb_name(ah->hw_version.macVersion),
  171. ah->hw_version.macRev,
  172. ath_rf_name((ah->hw_version.analog5GhzRev & AR_RADIO_SREV_MAJOR)),
  173. ah->hw_version.phyRev,
  174. (unsigned long)mem, pdev->irq);
  175. return 0;
  176. bad4:
  177. ath_detach(sc);
  178. bad3:
  179. ieee80211_free_hw(hw);
  180. bad2:
  181. pci_iounmap(pdev, mem);
  182. bad1:
  183. pci_release_region(pdev, 0);
  184. bad:
  185. pci_disable_device(pdev);
  186. return ret;
  187. }
  188. static void ath_pci_remove(struct pci_dev *pdev)
  189. {
  190. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  191. struct ath_wiphy *aphy = hw->priv;
  192. struct ath_softc *sc = aphy->sc;
  193. ath_cleanup(sc);
  194. }
  195. #ifdef CONFIG_PM
  196. static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  197. {
  198. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  199. struct ath_wiphy *aphy = hw->priv;
  200. struct ath_softc *sc = aphy->sc;
  201. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  202. pci_save_state(pdev);
  203. pci_disable_device(pdev);
  204. pci_set_power_state(pdev, PCI_D3hot);
  205. return 0;
  206. }
  207. static int ath_pci_resume(struct pci_dev *pdev)
  208. {
  209. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  210. struct ath_wiphy *aphy = hw->priv;
  211. struct ath_softc *sc = aphy->sc;
  212. u32 val;
  213. int err;
  214. err = pci_enable_device(pdev);
  215. if (err)
  216. return err;
  217. pci_restore_state(pdev);
  218. /*
  219. * Suspend/Resume resets the PCI configuration space, so we have to
  220. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  221. * PCI Tx retries from interfering with C3 CPU state
  222. */
  223. pci_read_config_dword(pdev, 0x40, &val);
  224. if ((val & 0x0000ff00) != 0)
  225. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  226. /* Enable LED */
  227. ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
  228. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  229. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  230. return 0;
  231. }
  232. #endif /* CONFIG_PM */
  233. MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
  234. static struct pci_driver ath_pci_driver = {
  235. .name = "ath9k",
  236. .id_table = ath_pci_id_table,
  237. .probe = ath_pci_probe,
  238. .remove = ath_pci_remove,
  239. #ifdef CONFIG_PM
  240. .suspend = ath_pci_suspend,
  241. .resume = ath_pci_resume,
  242. #endif /* CONFIG_PM */
  243. };
  244. int ath_pci_init(void)
  245. {
  246. return pci_register_driver(&ath_pci_driver);
  247. }
  248. void ath_pci_exit(void)
  249. {
  250. pci_unregister_driver(&ath_pci_driver);
  251. }