pci.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/pci-aspm.h>
  19. #include "../ath.h"
  20. #include "ath5k.h"
  21. #include "debug.h"
  22. #include "base.h"
  23. #include "reg.h"
  24. /* Known PCI ids */
  25. static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
  26. { PCI_VDEVICE(ATHEROS, 0x0207) }, /* 5210 early */
  27. { PCI_VDEVICE(ATHEROS, 0x0007) }, /* 5210 */
  28. { PCI_VDEVICE(ATHEROS, 0x0011) }, /* 5311 - this is on AHB bus !*/
  29. { PCI_VDEVICE(ATHEROS, 0x0012) }, /* 5211 */
  30. { PCI_VDEVICE(ATHEROS, 0x0013) }, /* 5212 */
  31. { PCI_VDEVICE(3COM_2, 0x0013) }, /* 3com 5212 */
  32. { PCI_VDEVICE(3COM, 0x0013) }, /* 3com 3CRDAG675 5212 */
  33. { PCI_VDEVICE(ATHEROS, 0x1014) }, /* IBM minipci 5212 */
  34. { PCI_VDEVICE(ATHEROS, 0x0014) }, /* 5212 combatible */
  35. { PCI_VDEVICE(ATHEROS, 0x0015) }, /* 5212 combatible */
  36. { PCI_VDEVICE(ATHEROS, 0x0016) }, /* 5212 combatible */
  37. { PCI_VDEVICE(ATHEROS, 0x0017) }, /* 5212 combatible */
  38. { PCI_VDEVICE(ATHEROS, 0x0018) }, /* 5212 combatible */
  39. { PCI_VDEVICE(ATHEROS, 0x0019) }, /* 5212 combatible */
  40. { PCI_VDEVICE(ATHEROS, 0x001a) }, /* 2413 Griffin-lite */
  41. { PCI_VDEVICE(ATHEROS, 0x001b) }, /* 5413 Eagle */
  42. { PCI_VDEVICE(ATHEROS, 0x001c) }, /* PCI-E cards */
  43. { PCI_VDEVICE(ATHEROS, 0x001d) }, /* 2417 Nala */
  44. { 0 }
  45. };
  46. MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
  47. /* return bus cachesize in 4B word units */
  48. static void ath5k_pci_read_cachesize(struct ath_common *common, int *csz)
  49. {
  50. struct ath5k_softc *sc = (struct ath5k_softc *) common->priv;
  51. u8 u8tmp;
  52. pci_read_config_byte(sc->pdev, PCI_CACHE_LINE_SIZE, &u8tmp);
  53. *csz = (int)u8tmp;
  54. /*
  55. * This check was put in to avoid "unplesant" consequences if
  56. * the bootrom has not fully initialized all PCI devices.
  57. * Sometimes the cache line size register is not set
  58. */
  59. if (*csz == 0)
  60. *csz = L1_CACHE_BYTES >> 2; /* Use the default size */
  61. }
  62. /*
  63. * Read from eeprom
  64. */
  65. bool ath5k_pci_eeprom_read(struct ath_common *common, u32 offset, u16 *data)
  66. {
  67. struct ath5k_hw *ah = (struct ath5k_hw *) common->ah;
  68. u32 status, timeout;
  69. /*
  70. * Initialize EEPROM access
  71. */
  72. if (ah->ah_version == AR5K_AR5210) {
  73. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
  74. (void)ath5k_hw_reg_read(ah, AR5K_EEPROM_BASE + (4 * offset));
  75. } else {
  76. ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
  77. AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
  78. AR5K_EEPROM_CMD_READ);
  79. }
  80. for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
  81. status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
  82. if (status & AR5K_EEPROM_STAT_RDDONE) {
  83. if (status & AR5K_EEPROM_STAT_RDERR)
  84. return -EIO;
  85. *data = (u16)(ath5k_hw_reg_read(ah, AR5K_EEPROM_DATA) &
  86. 0xffff);
  87. return 0;
  88. }
  89. udelay(15);
  90. }
  91. return -ETIMEDOUT;
  92. }
  93. int ath5k_hw_read_srev(struct ath5k_hw *ah)
  94. {
  95. ah->ah_mac_srev = ath5k_hw_reg_read(ah, AR5K_SREV);
  96. return 0;
  97. }
  98. /* Common ath_bus_opts structure */
  99. static const struct ath_bus_ops ath_pci_bus_ops = {
  100. .ath_bus_type = ATH_PCI,
  101. .read_cachesize = ath5k_pci_read_cachesize,
  102. .eeprom_read = ath5k_pci_eeprom_read,
  103. };
  104. /********************\
  105. * PCI Initialization *
  106. \********************/
  107. static int __devinit
  108. ath5k_pci_probe(struct pci_dev *pdev,
  109. const struct pci_device_id *id)
  110. {
  111. void __iomem *mem;
  112. struct ath5k_softc *sc;
  113. struct ieee80211_hw *hw;
  114. int ret;
  115. u8 csz;
  116. /*
  117. * L0s needs to be disabled on all ath5k cards.
  118. *
  119. * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
  120. * by default in the future in 2.6.36) this will also mean both L1 and
  121. * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
  122. * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
  123. * though but cannot currently undue the effect of a blacklist, for
  124. * details you can read pcie_aspm_sanity_check() and see how it adjusts
  125. * the device link capability.
  126. *
  127. * It may be possible in the future to implement some PCI API to allow
  128. * drivers to override blacklists for pre 1.1 PCIe but for now it is
  129. * best to accept that both L0s and L1 will be disabled completely for
  130. * distributions shipping with CONFIG_PCIEASPM rather than having this
  131. * issue present. Motivation for adding this new API will be to help
  132. * with power consumption for some of these devices.
  133. */
  134. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
  135. ret = pci_enable_device(pdev);
  136. if (ret) {
  137. dev_err(&pdev->dev, "can't enable device\n");
  138. goto err;
  139. }
  140. /* XXX 32-bit addressing only */
  141. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  142. if (ret) {
  143. dev_err(&pdev->dev, "32-bit DMA not available\n");
  144. goto err_dis;
  145. }
  146. /*
  147. * Cache line size is used to size and align various
  148. * structures used to communicate with the hardware.
  149. */
  150. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  151. if (csz == 0) {
  152. /*
  153. * Linux 2.4.18 (at least) writes the cache line size
  154. * register as a 16-bit wide register which is wrong.
  155. * We must have this setup properly for rx buffer
  156. * DMA to work so force a reasonable value here if it
  157. * comes up zero.
  158. */
  159. csz = L1_CACHE_BYTES >> 2;
  160. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  161. }
  162. /*
  163. * The default setting of latency timer yields poor results,
  164. * set it to the value used by other systems. It may be worth
  165. * tweaking this setting more.
  166. */
  167. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  168. /* Enable bus mastering */
  169. pci_set_master(pdev);
  170. /*
  171. * Disable the RETRY_TIMEOUT register (0x41) to keep
  172. * PCI Tx retries from interfering with C3 CPU state.
  173. */
  174. pci_write_config_byte(pdev, 0x41, 0);
  175. ret = pci_request_region(pdev, 0, "ath5k");
  176. if (ret) {
  177. dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
  178. goto err_dis;
  179. }
  180. mem = pci_iomap(pdev, 0, 0);
  181. if (!mem) {
  182. dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
  183. ret = -EIO;
  184. goto err_reg;
  185. }
  186. /*
  187. * Allocate hw (mac80211 main struct)
  188. * and hw->priv (driver private data)
  189. */
  190. hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
  191. if (hw == NULL) {
  192. dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
  193. ret = -ENOMEM;
  194. goto err_map;
  195. }
  196. dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
  197. sc = hw->priv;
  198. sc->hw = hw;
  199. sc->pdev = pdev;
  200. sc->dev = &pdev->dev;
  201. sc->irq = pdev->irq;
  202. sc->devid = id->device;
  203. sc->iobase = mem; /* So we can unmap it on detach */
  204. /* Initialize */
  205. ret = ath5k_init_softc(sc, &ath_pci_bus_ops);
  206. if (ret)
  207. goto err_free;
  208. /* Set private data */
  209. pci_set_drvdata(pdev, hw);
  210. return 0;
  211. err_free:
  212. ieee80211_free_hw(hw);
  213. err_map:
  214. pci_iounmap(pdev, mem);
  215. err_reg:
  216. pci_release_region(pdev, 0);
  217. err_dis:
  218. pci_disable_device(pdev);
  219. err:
  220. return ret;
  221. }
  222. static void __devexit
  223. ath5k_pci_remove(struct pci_dev *pdev)
  224. {
  225. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  226. struct ath5k_softc *sc = hw->priv;
  227. ath5k_deinit_softc(sc);
  228. pci_iounmap(pdev, sc->iobase);
  229. pci_release_region(pdev, 0);
  230. pci_disable_device(pdev);
  231. ieee80211_free_hw(hw);
  232. }
  233. #ifdef CONFIG_PM_SLEEP
  234. static int ath5k_pci_suspend(struct device *dev)
  235. {
  236. struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
  237. ath5k_led_off(sc);
  238. return 0;
  239. }
  240. static int ath5k_pci_resume(struct device *dev)
  241. {
  242. struct pci_dev *pdev = to_pci_dev(dev);
  243. struct ath5k_softc *sc = pci_get_drvdata(pdev);
  244. /*
  245. * Suspend/Resume resets the PCI configuration space, so we have to
  246. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  247. * PCI Tx retries from interfering with C3 CPU state
  248. */
  249. pci_write_config_byte(pdev, 0x41, 0);
  250. ath5k_led_enable(sc);
  251. return 0;
  252. }
  253. static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
  254. #define ATH5K_PM_OPS (&ath5k_pm_ops)
  255. #else
  256. #define ATH5K_PM_OPS NULL
  257. #endif /* CONFIG_PM_SLEEP */
  258. static struct pci_driver ath5k_pci_driver = {
  259. .name = KBUILD_MODNAME,
  260. .id_table = ath5k_pci_id_table,
  261. .probe = ath5k_pci_probe,
  262. .remove = __devexit_p(ath5k_pci_remove),
  263. .driver.pm = ATH5K_PM_OPS,
  264. };
  265. /*
  266. * Module init/exit functions
  267. */
  268. static int __init
  269. init_ath5k_pci(void)
  270. {
  271. int ret;
  272. ret = pci_register_driver(&ath5k_pci_driver);
  273. if (ret) {
  274. printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
  275. return ret;
  276. }
  277. return 0;
  278. }
  279. static void __exit
  280. exit_ath5k_pci(void)
  281. {
  282. pci_unregister_driver(&ath5k_pci_driver);
  283. }
  284. module_init(init_ath5k_pci);
  285. module_exit(exit_ath5k_pci);