pci.c 12 KB

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