pci.c 10 KB

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