pci.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_common *common, int *csz)
  32. {
  33. struct ath_softc *sc = (struct ath_softc *) common->priv;
  34. u8 u8tmp;
  35. pci_read_config_byte(to_pci_dev(sc->dev), PCI_CACHE_LINE_SIZE, &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_common *common)
  46. {
  47. struct ath_softc *sc = (struct ath_softc *) common->priv;
  48. struct pci_dev *pdev = to_pci_dev(sc->dev);
  49. pci_iounmap(pdev, sc->mem);
  50. pci_disable_device(pdev);
  51. pci_release_region(pdev, 0);
  52. }
  53. static bool ath_pci_eeprom_read(struct ath_common *common, u32 off, u16 *data)
  54. {
  55. struct ath_hw *ah = (struct ath_hw *) common->ah;
  56. common->ops->read(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
  57. if (!ath9k_hw_wait(ah,
  58. AR_EEPROM_STATUS_DATA,
  59. AR_EEPROM_STATUS_DATA_BUSY |
  60. AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
  61. AH_WAIT_TIMEOUT)) {
  62. return false;
  63. }
  64. *data = MS(common->ops->read(ah, AR_EEPROM_STATUS_DATA),
  65. AR_EEPROM_STATUS_DATA_VAL);
  66. return true;
  67. }
  68. /*
  69. * Bluetooth coexistance requires disabling ASPM.
  70. */
  71. static void ath_pci_bt_coex_prep(struct ath_common *common)
  72. {
  73. struct ath_softc *sc = (struct ath_softc *) common->priv;
  74. struct pci_dev *pdev = to_pci_dev(sc->dev);
  75. u8 aspm;
  76. if (!pdev->is_pcie)
  77. return;
  78. pci_read_config_byte(pdev, ATH_PCIE_CAP_LINK_CTRL, &aspm);
  79. aspm &= ~(ATH_PCIE_CAP_LINK_L0S | ATH_PCIE_CAP_LINK_L1);
  80. pci_write_config_byte(pdev, ATH_PCIE_CAP_LINK_CTRL, aspm);
  81. }
  82. const static struct ath_bus_ops ath_pci_bus_ops = {
  83. .read_cachesize = ath_pci_read_cachesize,
  84. .cleanup = ath_pci_cleanup,
  85. .eeprom_read = ath_pci_eeprom_read,
  86. .bt_coex_prep = ath_pci_bt_coex_prep,
  87. };
  88. static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  89. {
  90. void __iomem *mem;
  91. struct ath_wiphy *aphy;
  92. struct ath_softc *sc;
  93. struct ieee80211_hw *hw;
  94. u8 csz;
  95. u16 subsysid;
  96. u32 val;
  97. int ret = 0;
  98. struct ath_hw *ah;
  99. if (pci_enable_device(pdev))
  100. return -EIO;
  101. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  102. if (ret) {
  103. printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
  104. goto bad;
  105. }
  106. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  107. if (ret) {
  108. printk(KERN_ERR "ath9k: 32-bit DMA consistent "
  109. "DMA enable failed\n");
  110. goto bad;
  111. }
  112. /*
  113. * Cache line size is used to size and align various
  114. * structures used to communicate with the hardware.
  115. */
  116. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  117. if (csz == 0) {
  118. /*
  119. * Linux 2.4.18 (at least) writes the cache line size
  120. * register as a 16-bit wide register which is wrong.
  121. * We must have this setup properly for rx buffer
  122. * DMA to work so force a reasonable value here if it
  123. * comes up zero.
  124. */
  125. csz = L1_CACHE_BYTES / sizeof(u32);
  126. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  127. }
  128. /*
  129. * The default setting of latency timer yields poor results,
  130. * set it to the value used by other systems. It may be worth
  131. * tweaking this setting more.
  132. */
  133. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  134. pci_set_master(pdev);
  135. /*
  136. * Disable the RETRY_TIMEOUT register (0x41) to keep
  137. * PCI Tx retries from interfering with C3 CPU state.
  138. */
  139. pci_read_config_dword(pdev, 0x40, &val);
  140. if ((val & 0x0000ff00) != 0)
  141. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  142. ret = pci_request_region(pdev, 0, "ath9k");
  143. if (ret) {
  144. dev_err(&pdev->dev, "PCI memory region reserve error\n");
  145. ret = -ENODEV;
  146. goto bad;
  147. }
  148. mem = pci_iomap(pdev, 0, 0);
  149. if (!mem) {
  150. printk(KERN_ERR "PCI memory map error\n") ;
  151. ret = -EIO;
  152. goto bad1;
  153. }
  154. hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy) +
  155. sizeof(struct ath_softc), &ath9k_ops);
  156. if (!hw) {
  157. dev_err(&pdev->dev, "no memory for ieee80211_hw\n");
  158. ret = -ENOMEM;
  159. goto bad2;
  160. }
  161. SET_IEEE80211_DEV(hw, &pdev->dev);
  162. pci_set_drvdata(pdev, hw);
  163. aphy = hw->priv;
  164. sc = (struct ath_softc *) (aphy + 1);
  165. aphy->sc = sc;
  166. aphy->hw = hw;
  167. sc->pri_wiphy = aphy;
  168. sc->hw = hw;
  169. sc->dev = &pdev->dev;
  170. sc->mem = mem;
  171. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &subsysid);
  172. ret = ath_init_device(id->device, sc, subsysid, &ath_pci_bus_ops);
  173. if (ret) {
  174. dev_err(&pdev->dev, "failed to initialize device\n");
  175. goto bad3;
  176. }
  177. /* setup interrupt service routine */
  178. ret = request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath9k", sc);
  179. if (ret) {
  180. dev_err(&pdev->dev, "request_irq failed\n");
  181. goto bad4;
  182. }
  183. sc->irq = pdev->irq;
  184. ah = sc->sc_ah;
  185. printk(KERN_INFO
  186. "%s: Atheros AR%s MAC/BB Rev:%x "
  187. "AR%s RF Rev:%x: mem=0x%lx, irq=%d\n",
  188. wiphy_name(hw->wiphy),
  189. ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
  190. ah->hw_version.macRev,
  191. ath9k_hw_rf_name((ah->hw_version.analog5GhzRev &
  192. AR_RADIO_SREV_MAJOR)),
  193. ah->hw_version.phyRev,
  194. (unsigned long)mem, pdev->irq);
  195. return 0;
  196. bad4:
  197. ath_detach(sc);
  198. bad3:
  199. ieee80211_free_hw(hw);
  200. bad2:
  201. pci_iounmap(pdev, mem);
  202. bad1:
  203. pci_release_region(pdev, 0);
  204. bad:
  205. pci_disable_device(pdev);
  206. return ret;
  207. }
  208. static void ath_pci_remove(struct pci_dev *pdev)
  209. {
  210. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  211. struct ath_wiphy *aphy = hw->priv;
  212. struct ath_softc *sc = aphy->sc;
  213. ath_cleanup(sc);
  214. }
  215. #ifdef CONFIG_PM
  216. static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  217. {
  218. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  219. struct ath_wiphy *aphy = hw->priv;
  220. struct ath_softc *sc = aphy->sc;
  221. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  222. pci_save_state(pdev);
  223. pci_disable_device(pdev);
  224. pci_set_power_state(pdev, PCI_D3hot);
  225. return 0;
  226. }
  227. static int ath_pci_resume(struct pci_dev *pdev)
  228. {
  229. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  230. struct ath_wiphy *aphy = hw->priv;
  231. struct ath_softc *sc = aphy->sc;
  232. u32 val;
  233. int err;
  234. pci_restore_state(pdev);
  235. err = pci_enable_device(pdev);
  236. if (err)
  237. return err;
  238. /*
  239. * Suspend/Resume resets the PCI configuration space, so we have to
  240. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  241. * PCI Tx retries from interfering with C3 CPU state
  242. */
  243. pci_read_config_dword(pdev, 0x40, &val);
  244. if ((val & 0x0000ff00) != 0)
  245. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  246. /* Enable LED */
  247. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  248. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  249. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  250. return 0;
  251. }
  252. #endif /* CONFIG_PM */
  253. MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
  254. static struct pci_driver ath_pci_driver = {
  255. .name = "ath9k",
  256. .id_table = ath_pci_id_table,
  257. .probe = ath_pci_probe,
  258. .remove = ath_pci_remove,
  259. #ifdef CONFIG_PM
  260. .suspend = ath_pci_suspend,
  261. .resume = ath_pci_resume,
  262. #endif /* CONFIG_PM */
  263. };
  264. int ath_pci_init(void)
  265. {
  266. return pci_register_driver(&ath_pci_driver);
  267. }
  268. void ath_pci_exit(void)
  269. {
  270. pci_unregister_driver(&ath_pci_driver);
  271. }