pci.c 8.1 KB

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