pci.c 11 KB

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