pci.c 9.5 KB

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