pci.c 9.1 KB

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