pci.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. char hw_name[64];
  100. if (pci_enable_device(pdev))
  101. return -EIO;
  102. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  103. if (ret) {
  104. printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
  105. goto bad;
  106. }
  107. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  108. if (ret) {
  109. printk(KERN_ERR "ath9k: 32-bit DMA consistent "
  110. "DMA enable failed\n");
  111. goto bad;
  112. }
  113. /*
  114. * Cache line size is used to size and align various
  115. * structures used to communicate with the hardware.
  116. */
  117. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  118. if (csz == 0) {
  119. /*
  120. * Linux 2.4.18 (at least) writes the cache line size
  121. * register as a 16-bit wide register which is wrong.
  122. * We must have this setup properly for rx buffer
  123. * DMA to work so force a reasonable value here if it
  124. * comes up zero.
  125. */
  126. csz = L1_CACHE_BYTES / sizeof(u32);
  127. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  128. }
  129. /*
  130. * The default setting of latency timer yields poor results,
  131. * set it to the value used by other systems. It may be worth
  132. * tweaking this setting more.
  133. */
  134. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  135. pci_set_master(pdev);
  136. /*
  137. * Disable the RETRY_TIMEOUT register (0x41) to keep
  138. * PCI Tx retries from interfering with C3 CPU state.
  139. */
  140. pci_read_config_dword(pdev, 0x40, &val);
  141. if ((val & 0x0000ff00) != 0)
  142. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  143. ret = pci_request_region(pdev, 0, "ath9k");
  144. if (ret) {
  145. dev_err(&pdev->dev, "PCI memory region reserve error\n");
  146. ret = -ENODEV;
  147. goto bad;
  148. }
  149. mem = pci_iomap(pdev, 0, 0);
  150. if (!mem) {
  151. printk(KERN_ERR "PCI memory map error\n") ;
  152. ret = -EIO;
  153. goto bad1;
  154. }
  155. hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy) +
  156. sizeof(struct ath_softc), &ath9k_ops);
  157. if (!hw) {
  158. dev_err(&pdev->dev, "no memory for ieee80211_hw\n");
  159. ret = -ENOMEM;
  160. goto bad2;
  161. }
  162. SET_IEEE80211_DEV(hw, &pdev->dev);
  163. pci_set_drvdata(pdev, hw);
  164. aphy = hw->priv;
  165. sc = (struct ath_softc *) (aphy + 1);
  166. aphy->sc = sc;
  167. aphy->hw = hw;
  168. sc->pri_wiphy = aphy;
  169. sc->hw = hw;
  170. sc->dev = &pdev->dev;
  171. sc->mem = mem;
  172. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &subsysid);
  173. ret = ath_init_device(id->device, sc, subsysid, &ath_pci_bus_ops);
  174. if (ret) {
  175. dev_err(&pdev->dev, "failed to initialize device\n");
  176. goto bad3;
  177. }
  178. /* setup interrupt service routine */
  179. ret = request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath9k", sc);
  180. if (ret) {
  181. dev_err(&pdev->dev, "request_irq failed\n");
  182. goto bad4;
  183. }
  184. sc->irq = pdev->irq;
  185. ah = sc->sc_ah;
  186. ath9k_hw_name(ah, hw_name, sizeof(hw_name));
  187. printk(KERN_INFO
  188. "%s: %s mem=0x%lx, irq=%d\n",
  189. wiphy_name(hw->wiphy),
  190. hw_name,
  191. (unsigned long)mem, pdev->irq);
  192. return 0;
  193. bad4:
  194. ath_detach(sc);
  195. bad3:
  196. ieee80211_free_hw(hw);
  197. bad2:
  198. pci_iounmap(pdev, mem);
  199. bad1:
  200. pci_release_region(pdev, 0);
  201. bad:
  202. pci_disable_device(pdev);
  203. return ret;
  204. }
  205. static void ath_pci_remove(struct pci_dev *pdev)
  206. {
  207. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  208. struct ath_wiphy *aphy = hw->priv;
  209. struct ath_softc *sc = aphy->sc;
  210. ath_cleanup(sc);
  211. }
  212. #ifdef CONFIG_PM
  213. static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  214. {
  215. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  216. struct ath_wiphy *aphy = hw->priv;
  217. struct ath_softc *sc = aphy->sc;
  218. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  219. pci_save_state(pdev);
  220. pci_disable_device(pdev);
  221. pci_set_power_state(pdev, PCI_D3hot);
  222. return 0;
  223. }
  224. static int ath_pci_resume(struct pci_dev *pdev)
  225. {
  226. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  227. struct ath_wiphy *aphy = hw->priv;
  228. struct ath_softc *sc = aphy->sc;
  229. u32 val;
  230. int err;
  231. pci_restore_state(pdev);
  232. err = pci_enable_device(pdev);
  233. if (err)
  234. return err;
  235. /*
  236. * Suspend/Resume resets the PCI configuration space, so we have to
  237. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  238. * PCI Tx retries from interfering with C3 CPU state
  239. */
  240. pci_read_config_dword(pdev, 0x40, &val);
  241. if ((val & 0x0000ff00) != 0)
  242. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  243. /* Enable LED */
  244. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  245. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  246. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  247. return 0;
  248. }
  249. #endif /* CONFIG_PM */
  250. MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
  251. static struct pci_driver ath_pci_driver = {
  252. .name = "ath9k",
  253. .id_table = ath_pci_id_table,
  254. .probe = ath_pci_probe,
  255. .remove = ath_pci_remove,
  256. #ifdef CONFIG_PM
  257. .suspend = ath_pci_suspend,
  258. .resume = ath_pci_resume,
  259. #endif /* CONFIG_PM */
  260. };
  261. int ath_pci_init(void)
  262. {
  263. return pci_register_driver(&ath_pci_driver);
  264. }
  265. void ath_pci_exit(void)
  266. {
  267. pci_unregister_driver(&ath_pci_driver);
  268. }