pci.c 10 KB

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