pci.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 DEFINE_PCI_DEVICE_TABLE(ath_pci_id_table) = {
  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, 0x002C) }, /* PCI-E 802.11n bonded out */
  27. { PCI_VDEVICE(ATHEROS, 0x002D) }, /* PCI */
  28. { PCI_VDEVICE(ATHEROS, 0x002E) }, /* PCI-E */
  29. { 0 }
  30. };
  31. /* return bus cachesize in 4B word units */
  32. static void ath_pci_read_cachesize(struct ath_common *common, int *csz)
  33. {
  34. struct ath_softc *sc = (struct ath_softc *) common->priv;
  35. u8 u8tmp;
  36. pci_read_config_byte(to_pci_dev(sc->dev), PCI_CACHE_LINE_SIZE, &u8tmp);
  37. *csz = (int)u8tmp;
  38. /*
  39. * This check was put in to avoid "unplesant" consequences if
  40. * the bootrom has not fully initialized all PCI devices.
  41. * Sometimes the cache line size register is not set
  42. */
  43. if (*csz == 0)
  44. *csz = DEFAULT_CACHELINE >> 2; /* Use the default size */
  45. }
  46. static bool ath_pci_eeprom_read(struct ath_common *common, u32 off, u16 *data)
  47. {
  48. struct ath_hw *ah = (struct ath_hw *) common->ah;
  49. common->ops->read(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
  50. if (!ath9k_hw_wait(ah,
  51. AR_EEPROM_STATUS_DATA,
  52. AR_EEPROM_STATUS_DATA_BUSY |
  53. AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
  54. AH_WAIT_TIMEOUT)) {
  55. return false;
  56. }
  57. *data = MS(common->ops->read(ah, AR_EEPROM_STATUS_DATA),
  58. AR_EEPROM_STATUS_DATA_VAL);
  59. return true;
  60. }
  61. /*
  62. * Bluetooth coexistance requires disabling ASPM.
  63. */
  64. static void ath_pci_bt_coex_prep(struct ath_common *common)
  65. {
  66. struct ath_softc *sc = (struct ath_softc *) common->priv;
  67. struct pci_dev *pdev = to_pci_dev(sc->dev);
  68. u8 aspm;
  69. if (!pdev->is_pcie)
  70. return;
  71. pci_read_config_byte(pdev, ATH_PCIE_CAP_LINK_CTRL, &aspm);
  72. aspm &= ~(ATH_PCIE_CAP_LINK_L0S | ATH_PCIE_CAP_LINK_L1);
  73. pci_write_config_byte(pdev, ATH_PCIE_CAP_LINK_CTRL, aspm);
  74. }
  75. static const struct ath_bus_ops ath_pci_bus_ops = {
  76. .ath_bus_type = ATH_PCI,
  77. .read_cachesize = ath_pci_read_cachesize,
  78. .eeprom_read = ath_pci_eeprom_read,
  79. .bt_coex_prep = ath_pci_bt_coex_prep,
  80. };
  81. static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  82. {
  83. void __iomem *mem;
  84. struct ath_wiphy *aphy;
  85. struct ath_softc *sc;
  86. struct ieee80211_hw *hw;
  87. u8 csz;
  88. u16 subsysid;
  89. u32 val;
  90. int ret = 0;
  91. char hw_name[64];
  92. if (pci_enable_device(pdev))
  93. return -EIO;
  94. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  95. if (ret) {
  96. printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
  97. goto err_dma;
  98. }
  99. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  100. if (ret) {
  101. printk(KERN_ERR "ath9k: 32-bit DMA consistent "
  102. "DMA enable failed\n");
  103. goto err_dma;
  104. }
  105. /*
  106. * Cache line size is used to size and align various
  107. * structures used to communicate with the hardware.
  108. */
  109. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  110. if (csz == 0) {
  111. /*
  112. * Linux 2.4.18 (at least) writes the cache line size
  113. * register as a 16-bit wide register which is wrong.
  114. * We must have this setup properly for rx buffer
  115. * DMA to work so force a reasonable value here if it
  116. * comes up zero.
  117. */
  118. csz = L1_CACHE_BYTES / sizeof(u32);
  119. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  120. }
  121. /*
  122. * The default setting of latency timer yields poor results,
  123. * set it to the value used by other systems. It may be worth
  124. * tweaking this setting more.
  125. */
  126. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  127. pci_set_master(pdev);
  128. /*
  129. * Disable the RETRY_TIMEOUT register (0x41) to keep
  130. * PCI Tx retries from interfering with C3 CPU state.
  131. */
  132. pci_read_config_dword(pdev, 0x40, &val);
  133. if ((val & 0x0000ff00) != 0)
  134. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  135. ret = pci_request_region(pdev, 0, "ath9k");
  136. if (ret) {
  137. dev_err(&pdev->dev, "PCI memory region reserve error\n");
  138. ret = -ENODEV;
  139. goto err_region;
  140. }
  141. mem = pci_iomap(pdev, 0, 0);
  142. if (!mem) {
  143. printk(KERN_ERR "PCI memory map error\n") ;
  144. ret = -EIO;
  145. goto err_iomap;
  146. }
  147. hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy) +
  148. sizeof(struct ath_softc), &ath9k_ops);
  149. if (!hw) {
  150. dev_err(&pdev->dev, "No memory for ieee80211_hw\n");
  151. ret = -ENOMEM;
  152. goto err_alloc_hw;
  153. }
  154. SET_IEEE80211_DEV(hw, &pdev->dev);
  155. pci_set_drvdata(pdev, hw);
  156. aphy = hw->priv;
  157. sc = (struct ath_softc *) (aphy + 1);
  158. aphy->sc = sc;
  159. aphy->hw = hw;
  160. sc->pri_wiphy = aphy;
  161. sc->hw = hw;
  162. sc->dev = &pdev->dev;
  163. sc->mem = mem;
  164. /* Will be cleared in ath9k_start() */
  165. sc->sc_flags |= SC_OP_INVALID;
  166. ret = request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath9k", sc);
  167. if (ret) {
  168. dev_err(&pdev->dev, "request_irq failed\n");
  169. goto err_irq;
  170. }
  171. sc->irq = pdev->irq;
  172. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &subsysid);
  173. ret = ath9k_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 err_init;
  177. }
  178. ath9k_hw_name(sc->sc_ah, hw_name, sizeof(hw_name));
  179. printk(KERN_INFO
  180. "%s: %s mem=0x%lx, irq=%d\n",
  181. wiphy_name(hw->wiphy),
  182. hw_name,
  183. (unsigned long)mem, pdev->irq);
  184. return 0;
  185. err_init:
  186. free_irq(sc->irq, sc);
  187. err_irq:
  188. ieee80211_free_hw(hw);
  189. err_alloc_hw:
  190. pci_iounmap(pdev, mem);
  191. err_iomap:
  192. pci_release_region(pdev, 0);
  193. err_region:
  194. /* Nothing */
  195. err_dma:
  196. pci_disable_device(pdev);
  197. return ret;
  198. }
  199. static void ath_pci_remove(struct pci_dev *pdev)
  200. {
  201. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  202. struct ath_wiphy *aphy = hw->priv;
  203. struct ath_softc *sc = aphy->sc;
  204. void __iomem *mem = sc->mem;
  205. ath9k_deinit_device(sc);
  206. free_irq(sc->irq, sc);
  207. ieee80211_free_hw(sc->hw);
  208. pci_iounmap(pdev, mem);
  209. pci_disable_device(pdev);
  210. pci_release_region(pdev, 0);
  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. }