You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ath5k_attach.c 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
  4. *
  5. * Modified for iPXE, July 2009, by Joshua Oreman <oremanj@rwcr.net>
  6. * Original from Linux kernel 2.6.30.
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. */
  21. FILE_LICENCE ( MIT );
  22. /*************************************\
  23. * Attach/Detach Functions and helpers *
  24. \*************************************/
  25. #include <ipxe/pci.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include "ath5k.h"
  29. #include "reg.h"
  30. #include "base.h"
  31. /**
  32. * ath5k_hw_post - Power On Self Test helper function
  33. *
  34. * @ah: The &struct ath5k_hw
  35. */
  36. static int ath5k_hw_post(struct ath5k_hw *ah)
  37. {
  38. static const u32 static_pattern[4] = {
  39. 0x55555555, 0xaaaaaaaa,
  40. 0x66666666, 0x99999999
  41. };
  42. static const u16 regs[2] = { AR5K_STA_ID0, AR5K_PHY(8) };
  43. int i, c;
  44. u16 cur_reg;
  45. u32 var_pattern;
  46. u32 init_val;
  47. u32 cur_val;
  48. for (c = 0; c < 2; c++) {
  49. cur_reg = regs[c];
  50. /* Save previous value */
  51. init_val = ath5k_hw_reg_read(ah, cur_reg);
  52. for (i = 0; i < 256; i++) {
  53. var_pattern = i << 16 | i;
  54. ath5k_hw_reg_write(ah, var_pattern, cur_reg);
  55. cur_val = ath5k_hw_reg_read(ah, cur_reg);
  56. if (cur_val != var_pattern) {
  57. DBG("ath5k: POST failed!\n");
  58. return -EAGAIN;
  59. }
  60. /* Found on ndiswrapper dumps */
  61. var_pattern = 0x0039080f;
  62. ath5k_hw_reg_write(ah, var_pattern, cur_reg);
  63. }
  64. for (i = 0; i < 4; i++) {
  65. var_pattern = static_pattern[i];
  66. ath5k_hw_reg_write(ah, var_pattern, cur_reg);
  67. cur_val = ath5k_hw_reg_read(ah, cur_reg);
  68. if (cur_val != var_pattern) {
  69. DBG("ath5k: POST failed!\n");
  70. return -EAGAIN;
  71. }
  72. /* Found on ndiswrapper dumps */
  73. var_pattern = 0x003b080f;
  74. ath5k_hw_reg_write(ah, var_pattern, cur_reg);
  75. }
  76. /* Restore previous value */
  77. ath5k_hw_reg_write(ah, init_val, cur_reg);
  78. }
  79. return 0;
  80. }
  81. /**
  82. * ath5k_hw_attach - Check if hw is supported and init the needed structs
  83. *
  84. * @sc: The &struct ath5k_softc we got from the driver's attach function
  85. * @mac_version: The mac version id (check out ath5k.h) based on pci id
  86. * @hw: Returned newly allocated hardware structure, on success
  87. *
  88. * Check if the device is supported, perform a POST and initialize the needed
  89. * structs. Returns -ENOMEM if we don't have memory for the needed structs,
  90. * -ENODEV if the device is not supported or prints an error msg if something
  91. * else went wrong.
  92. */
  93. int ath5k_hw_attach(struct ath5k_softc *sc, u8 mac_version,
  94. struct ath5k_hw **hw)
  95. {
  96. struct ath5k_hw *ah;
  97. struct pci_device *pdev = sc->pdev;
  98. int ret;
  99. u32 srev;
  100. ah = zalloc(sizeof(struct ath5k_hw));
  101. if (ah == NULL) {
  102. ret = -ENOMEM;
  103. DBG("ath5k: out of memory\n");
  104. goto err;
  105. }
  106. ah->ah_sc = sc;
  107. ah->ah_iobase = sc->iobase;
  108. /*
  109. * HW information
  110. */
  111. ah->ah_turbo = 0;
  112. ah->ah_txpower.txp_tpc = 0;
  113. ah->ah_imr = 0;
  114. ah->ah_atim_window = 0;
  115. ah->ah_aifs = AR5K_TUNE_AIFS;
  116. ah->ah_cw_min = AR5K_TUNE_CWMIN;
  117. ah->ah_limit_tx_retries = AR5K_INIT_TX_RETRY;
  118. ah->ah_software_retry = 0;
  119. ah->ah_ant_diversity = AR5K_TUNE_ANT_DIVERSITY;
  120. /*
  121. * Set the mac version based on the pci id
  122. */
  123. ah->ah_version = mac_version;
  124. /*Fill the ath5k_hw struct with the needed functions*/
  125. ret = ath5k_hw_init_desc_functions(ah);
  126. if (ret)
  127. goto err_free;
  128. /* Bring device out of sleep and reset it's units */
  129. ret = ath5k_hw_nic_wakeup(ah, CHANNEL_B, 1);
  130. if (ret)
  131. goto err_free;
  132. /* Get MAC, PHY and RADIO revisions */
  133. srev = ath5k_hw_reg_read(ah, AR5K_SREV);
  134. ah->ah_mac_srev = srev;
  135. ah->ah_mac_version = AR5K_REG_MS(srev, AR5K_SREV_VER);
  136. ah->ah_mac_revision = AR5K_REG_MS(srev, AR5K_SREV_REV);
  137. ah->ah_phy_revision = ath5k_hw_reg_read(ah, AR5K_PHY_CHIP_ID);
  138. ah->ah_radio_5ghz_revision = ath5k_hw_radio_revision(ah, CHANNEL_5GHZ);
  139. ah->ah_phy = AR5K_PHY(0);
  140. /* Try to identify radio chip based on it's srev */
  141. switch (ah->ah_radio_5ghz_revision & 0xf0) {
  142. case AR5K_SREV_RAD_5111:
  143. ah->ah_radio = AR5K_RF5111;
  144. ah->ah_single_chip = 0;
  145. ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
  146. CHANNEL_2GHZ);
  147. break;
  148. case AR5K_SREV_RAD_5112:
  149. case AR5K_SREV_RAD_2112:
  150. ah->ah_radio = AR5K_RF5112;
  151. ah->ah_single_chip = 0;
  152. ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
  153. CHANNEL_2GHZ);
  154. break;
  155. case AR5K_SREV_RAD_2413:
  156. ah->ah_radio = AR5K_RF2413;
  157. ah->ah_single_chip = 1;
  158. break;
  159. case AR5K_SREV_RAD_5413:
  160. ah->ah_radio = AR5K_RF5413;
  161. ah->ah_single_chip = 1;
  162. break;
  163. case AR5K_SREV_RAD_2316:
  164. ah->ah_radio = AR5K_RF2316;
  165. ah->ah_single_chip = 1;
  166. break;
  167. case AR5K_SREV_RAD_2317:
  168. ah->ah_radio = AR5K_RF2317;
  169. ah->ah_single_chip = 1;
  170. break;
  171. case AR5K_SREV_RAD_5424:
  172. if (ah->ah_mac_version == AR5K_SREV_AR2425 ||
  173. ah->ah_mac_version == AR5K_SREV_AR2417) {
  174. ah->ah_radio = AR5K_RF2425;
  175. } else {
  176. ah->ah_radio = AR5K_RF5413;
  177. }
  178. ah->ah_single_chip = 1;
  179. break;
  180. default:
  181. /* Identify radio based on mac/phy srev */
  182. if (ah->ah_version == AR5K_AR5210) {
  183. ah->ah_radio = AR5K_RF5110;
  184. ah->ah_single_chip = 0;
  185. } else if (ah->ah_version == AR5K_AR5211) {
  186. ah->ah_radio = AR5K_RF5111;
  187. ah->ah_single_chip = 0;
  188. ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
  189. CHANNEL_2GHZ);
  190. } else if (ah->ah_mac_version == (AR5K_SREV_AR2425 >> 4) ||
  191. ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4) ||
  192. ah->ah_phy_revision == AR5K_SREV_PHY_2425) {
  193. ah->ah_radio = AR5K_RF2425;
  194. ah->ah_single_chip = 1;
  195. ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_2425;
  196. } else if (srev == AR5K_SREV_AR5213A &&
  197. ah->ah_phy_revision == AR5K_SREV_PHY_5212B) {
  198. ah->ah_radio = AR5K_RF5112;
  199. ah->ah_single_chip = 0;
  200. ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_5112B;
  201. } else if (ah->ah_mac_version == (AR5K_SREV_AR2415 >> 4)) {
  202. ah->ah_radio = AR5K_RF2316;
  203. ah->ah_single_chip = 1;
  204. ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_2316;
  205. } else if (ah->ah_mac_version == (AR5K_SREV_AR5414 >> 4) ||
  206. ah->ah_phy_revision == AR5K_SREV_PHY_5413) {
  207. ah->ah_radio = AR5K_RF5413;
  208. ah->ah_single_chip = 1;
  209. ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_5413;
  210. } else if (ah->ah_mac_version == (AR5K_SREV_AR2414 >> 4) ||
  211. ah->ah_phy_revision == AR5K_SREV_PHY_2413) {
  212. ah->ah_radio = AR5K_RF2413;
  213. ah->ah_single_chip = 1;
  214. ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_2413;
  215. } else {
  216. DBG("ath5k: Couldn't identify radio revision.\n");
  217. ret = -ENOTSUP;
  218. goto err_free;
  219. }
  220. }
  221. /* Return on unsuported chips (unsupported eeprom etc) */
  222. if ((srev >= AR5K_SREV_AR5416) &&
  223. (srev < AR5K_SREV_AR2425)) {
  224. DBG("ath5k: Device not yet supported.\n");
  225. ret = -ENOTSUP;
  226. goto err_free;
  227. }
  228. /*
  229. * Write PCI-E power save settings
  230. */
  231. if ((ah->ah_version == AR5K_AR5212) &&
  232. pci_find_capability(pdev, PCI_CAP_ID_EXP)) {
  233. ath5k_hw_reg_write(ah, 0x9248fc00, AR5K_PCIE_SERDES);
  234. ath5k_hw_reg_write(ah, 0x24924924, AR5K_PCIE_SERDES);
  235. /* Shut off RX when elecidle is asserted */
  236. ath5k_hw_reg_write(ah, 0x28000039, AR5K_PCIE_SERDES);
  237. ath5k_hw_reg_write(ah, 0x53160824, AR5K_PCIE_SERDES);
  238. /* TODO: EEPROM work */
  239. ath5k_hw_reg_write(ah, 0xe5980579, AR5K_PCIE_SERDES);
  240. /* Shut off PLL and CLKREQ active in L1 */
  241. ath5k_hw_reg_write(ah, 0x001defff, AR5K_PCIE_SERDES);
  242. /* Preserce other settings */
  243. ath5k_hw_reg_write(ah, 0x1aaabe40, AR5K_PCIE_SERDES);
  244. ath5k_hw_reg_write(ah, 0xbe105554, AR5K_PCIE_SERDES);
  245. ath5k_hw_reg_write(ah, 0x000e3007, AR5K_PCIE_SERDES);
  246. /* Reset SERDES to load new settings */
  247. ath5k_hw_reg_write(ah, 0x00000000, AR5K_PCIE_SERDES_RESET);
  248. mdelay(1);
  249. }
  250. /*
  251. * POST
  252. */
  253. ret = ath5k_hw_post(ah);
  254. if (ret)
  255. goto err_free;
  256. /* Enable pci core retry fix on Hainan (5213A) and later chips */
  257. if (srev >= AR5K_SREV_AR5213A)
  258. ath5k_hw_reg_write(ah, AR5K_PCICFG_RETRY_FIX, AR5K_PCICFG);
  259. /*
  260. * Get card capabilities, calibration values etc
  261. * TODO: EEPROM work
  262. */
  263. ret = ath5k_eeprom_init(ah);
  264. if (ret) {
  265. DBG("ath5k: unable to init EEPROM\n");
  266. goto err_free;
  267. }
  268. /* Get misc capabilities */
  269. ret = ath5k_hw_set_capabilities(ah);
  270. if (ret) {
  271. DBG("ath5k: unable to get device capabilities: 0x%04x\n",
  272. sc->pdev->device);
  273. goto err_free;
  274. }
  275. if (srev >= AR5K_SREV_AR2414) {
  276. ah->ah_combined_mic = 1;
  277. AR5K_REG_ENABLE_BITS(ah, AR5K_MISC_MODE,
  278. AR5K_MISC_MODE_COMBINED_MIC);
  279. }
  280. /* Set BSSID to bcast address: ff:ff:ff:ff:ff:ff for now */
  281. memset(ah->ah_bssid, 0xff, ETH_ALEN);
  282. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  283. ath5k_hw_set_opmode(ah);
  284. ath5k_hw_rfgain_opt_init(ah);
  285. *hw = ah;
  286. return 0;
  287. err_free:
  288. free(ah);
  289. err:
  290. return ret;
  291. }
  292. /**
  293. * ath5k_hw_detach - Free the ath5k_hw struct
  294. *
  295. * @ah: The &struct ath5k_hw
  296. */
  297. void ath5k_hw_detach(struct ath5k_hw *ah)
  298. {
  299. free(ah->ah_rf_banks);
  300. ath5k_eeprom_detach(ah);
  301. free(ah);
  302. }