pci.c 9.3 KB

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