pci.c 9.0 KB

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