pci.c 10 KB

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