attach.c 10 KB

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