pci.c 10.0 KB

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