phy.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /*
  2. * PHY functions
  3. *
  4. * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
  5. * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>
  6. * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
  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. #define _ATH5K_PHY
  22. #include <linux/delay.h>
  23. #include "ath5k.h"
  24. #include "reg.h"
  25. #include "base.h"
  26. #include "rfbuffer.h"
  27. #include "rfgain.h"
  28. /*
  29. * Used to modify RF Banks before writing them to AR5K_RF_BUFFER
  30. */
  31. static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,
  32. const struct ath5k_rf_reg *rf_regs,
  33. u32 val, u8 reg_id, bool set)
  34. {
  35. const struct ath5k_rf_reg *rfreg = NULL;
  36. u8 offset, bank, num_bits, col, position;
  37. u16 entry;
  38. u32 mask, data, last_bit, bits_shifted, first_bit;
  39. u32 *rfb;
  40. s32 bits_left;
  41. int i;
  42. data = 0;
  43. rfb = ah->ah_rf_banks;
  44. for (i = 0; i < ah->ah_rf_regs_count; i++) {
  45. if (rf_regs[i].index == reg_id) {
  46. rfreg = &rf_regs[i];
  47. break;
  48. }
  49. }
  50. if (rfb == NULL || rfreg == NULL) {
  51. ATH5K_PRINTF("Rf register not found!\n");
  52. /* should not happen */
  53. return 0;
  54. }
  55. bank = rfreg->bank;
  56. num_bits = rfreg->field.len;
  57. first_bit = rfreg->field.pos;
  58. col = rfreg->field.col;
  59. /* first_bit is an offset from bank's
  60. * start. Since we have all banks on
  61. * the same array, we use this offset
  62. * to mark each bank's start */
  63. offset = ah->ah_offset[bank];
  64. /* Boundary check */
  65. if (!(col <= 3 && num_bits <= 32 && first_bit + num_bits <= 319)) {
  66. ATH5K_PRINTF("invalid values at offset %u\n", offset);
  67. return 0;
  68. }
  69. entry = ((first_bit - 1) / 8) + offset;
  70. position = (first_bit - 1) % 8;
  71. if (set)
  72. data = ath5k_hw_bitswap(val, num_bits);
  73. for (bits_shifted = 0, bits_left = num_bits; bits_left > 0;
  74. position = 0, entry++) {
  75. last_bit = (position + bits_left > 8) ? 8 :
  76. position + bits_left;
  77. mask = (((1 << last_bit) - 1) ^ ((1 << position) - 1)) <<
  78. (col * 8);
  79. if (set) {
  80. rfb[entry] &= ~mask;
  81. rfb[entry] |= ((data << position) << (col * 8)) & mask;
  82. data >>= (8 - position);
  83. } else {
  84. data |= (((rfb[entry] & mask) >> (col * 8)) >> position)
  85. << bits_shifted;
  86. bits_shifted += last_bit - position;
  87. }
  88. bits_left -= 8 - position;
  89. }
  90. data = set ? 1 : ath5k_hw_bitswap(data, num_bits);
  91. return data;
  92. }
  93. /**********************\
  94. * RF Gain optimization *
  95. \**********************/
  96. /*
  97. * This code is used to optimize rf gain on different environments
  98. * (temprature mostly) based on feedback from a power detector.
  99. *
  100. * It's only used on RF5111 and RF5112, later RF chips seem to have
  101. * auto adjustment on hw -notice they have a much smaller BANK 7 and
  102. * no gain optimization ladder-.
  103. *
  104. * For more infos check out this patent doc
  105. * http://www.freepatentsonline.com/7400691.html
  106. *
  107. * This paper describes power drops as seen on the receiver due to
  108. * probe packets
  109. * http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
  110. * %20of%20Power%20Control.pdf
  111. *
  112. * And this is the MadWiFi bug entry related to the above
  113. * http://madwifi-project.org/ticket/1659
  114. * with various measurements and diagrams
  115. *
  116. * TODO: Deal with power drops due to probes by setting an apropriate
  117. * tx power on the probe packets ! Make this part of the calibration process.
  118. */
  119. /* Initialize ah_gain durring attach */
  120. int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
  121. {
  122. /* Initialize the gain optimization values */
  123. switch (ah->ah_radio) {
  124. case AR5K_RF5111:
  125. ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
  126. ah->ah_gain.g_low = 20;
  127. ah->ah_gain.g_high = 35;
  128. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  129. break;
  130. case AR5K_RF5112:
  131. ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
  132. ah->ah_gain.g_low = 20;
  133. ah->ah_gain.g_high = 85;
  134. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  135. break;
  136. default:
  137. return -EINVAL;
  138. }
  139. return 0;
  140. }
  141. /* Schedule a gain probe check on the next transmited packet.
  142. * That means our next packet is going to be sent with lower
  143. * tx power and a Peak to Average Power Detector (PAPD) will try
  144. * to measure the gain.
  145. *
  146. * TODO: Use propper tx power setting for the probe packet so
  147. * that we don't observe a serious power drop on the receiver
  148. *
  149. * XXX: How about forcing a tx packet (bypassing PCU arbitrator etc)
  150. * just after we enable the probe so that we don't mess with
  151. * standard traffic ? Maybe it's time to use sw interrupts and
  152. * a probe tasklet !!!
  153. */
  154. static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
  155. {
  156. /* Skip if gain calibration is inactive or
  157. * we already handle a probe request */
  158. if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
  159. return;
  160. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_max,
  161. AR5K_PHY_PAPD_PROBE_TXPOWER) |
  162. AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
  163. ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
  164. }
  165. /* Calculate gain_F measurement correction
  166. * based on the current step for RF5112 rev. 2 */
  167. static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
  168. {
  169. u32 mix, step;
  170. u32 *rf;
  171. const struct ath5k_gain_opt *go;
  172. const struct ath5k_gain_opt_step *g_step;
  173. const struct ath5k_rf_reg *rf_regs;
  174. /* Only RF5112 Rev. 2 supports it */
  175. if ((ah->ah_radio != AR5K_RF5112) ||
  176. (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
  177. return 0;
  178. go = &rfgain_opt_5112;
  179. rf_regs = rf_regs_5112a;
  180. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  181. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  182. if (ah->ah_rf_banks == NULL)
  183. return 0;
  184. rf = ah->ah_rf_banks;
  185. ah->ah_gain.g_f_corr = 0;
  186. /* No VGA (Variable Gain Amplifier) override, skip */
  187. if (ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR, false) != 1)
  188. return 0;
  189. /* Mix gain stepping */
  190. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXGAIN_STEP, false);
  191. /* Mix gain override */
  192. mix = g_step->gos_param[0];
  193. switch (mix) {
  194. case 3:
  195. ah->ah_gain.g_f_corr = step * 2;
  196. break;
  197. case 2:
  198. ah->ah_gain.g_f_corr = (step - 5) * 2;
  199. break;
  200. case 1:
  201. ah->ah_gain.g_f_corr = step;
  202. break;
  203. default:
  204. ah->ah_gain.g_f_corr = 0;
  205. break;
  206. }
  207. return ah->ah_gain.g_f_corr;
  208. }
  209. /* Check if current gain_F measurement is in the range of our
  210. * power detector windows. If we get a measurement outside range
  211. * we know it's not accurate (detectors can't measure anything outside
  212. * their detection window) so we must ignore it */
  213. static bool ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
  214. {
  215. const struct ath5k_rf_reg *rf_regs;
  216. u32 step, mix_ovr, level[4];
  217. u32 *rf;
  218. if (ah->ah_rf_banks == NULL)
  219. return false;
  220. rf = ah->ah_rf_banks;
  221. if (ah->ah_radio == AR5K_RF5111) {
  222. rf_regs = rf_regs_5111;
  223. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  224. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_RFGAIN_STEP,
  225. false);
  226. level[0] = 0;
  227. level[1] = (step == 63) ? 50 : step + 4;
  228. level[2] = (step != 63) ? 64 : level[0];
  229. level[3] = level[2] + 50 ;
  230. ah->ah_gain.g_high = level[3] -
  231. (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
  232. ah->ah_gain.g_low = level[0] +
  233. (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
  234. } else {
  235. rf_regs = rf_regs_5112;
  236. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  237. mix_ovr = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR,
  238. false);
  239. level[0] = level[2] = 0;
  240. if (mix_ovr == 1) {
  241. level[1] = level[3] = 83;
  242. } else {
  243. level[1] = level[3] = 107;
  244. ah->ah_gain.g_high = 55;
  245. }
  246. }
  247. return (ah->ah_gain.g_current >= level[0] &&
  248. ah->ah_gain.g_current <= level[1]) ||
  249. (ah->ah_gain.g_current >= level[2] &&
  250. ah->ah_gain.g_current <= level[3]);
  251. }
  252. /* Perform gain_F adjustment by choosing the right set
  253. * of parameters from rf gain optimization ladder */
  254. static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
  255. {
  256. const struct ath5k_gain_opt *go;
  257. const struct ath5k_gain_opt_step *g_step;
  258. int ret = 0;
  259. switch (ah->ah_radio) {
  260. case AR5K_RF5111:
  261. go = &rfgain_opt_5111;
  262. break;
  263. case AR5K_RF5112:
  264. go = &rfgain_opt_5112;
  265. break;
  266. default:
  267. return 0;
  268. }
  269. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  270. if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
  271. /* Reached maximum */
  272. if (ah->ah_gain.g_step_idx == 0)
  273. return -1;
  274. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  275. ah->ah_gain.g_target >= ah->ah_gain.g_high &&
  276. ah->ah_gain.g_step_idx > 0;
  277. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  278. ah->ah_gain.g_target -= 2 *
  279. (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
  280. g_step->gos_gain);
  281. ret = 1;
  282. goto done;
  283. }
  284. if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
  285. /* Reached minimum */
  286. if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
  287. return -2;
  288. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  289. ah->ah_gain.g_target <= ah->ah_gain.g_low &&
  290. ah->ah_gain.g_step_idx < go->go_steps_count-1;
  291. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  292. ah->ah_gain.g_target -= 2 *
  293. (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
  294. g_step->gos_gain);
  295. ret = 2;
  296. goto done;
  297. }
  298. done:
  299. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  300. "ret %d, gain step %u, current gain %u, target gain %u\n",
  301. ret, ah->ah_gain.g_step_idx, ah->ah_gain.g_current,
  302. ah->ah_gain.g_target);
  303. return ret;
  304. }
  305. /* Main callback for thermal rf gain calibration engine
  306. * Check for a new gain reading and schedule an adjustment
  307. * if needed.
  308. *
  309. * TODO: Use sw interrupt to schedule reset if gain_F needs
  310. * adjustment */
  311. enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
  312. {
  313. u32 data, type;
  314. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  315. ATH5K_TRACE(ah->ah_sc);
  316. if (ah->ah_rf_banks == NULL ||
  317. ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
  318. return AR5K_RFGAIN_INACTIVE;
  319. /* No check requested, either engine is inactive
  320. * or an adjustment is already requested */
  321. if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
  322. goto done;
  323. /* Read the PAPD (Peak to Average Power Detector)
  324. * register */
  325. data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
  326. /* No probe is scheduled, read gain_F measurement */
  327. if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
  328. ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
  329. type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
  330. /* If tx packet is CCK correct the gain_F measurement
  331. * by cck ofdm gain delta */
  332. if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
  333. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
  334. ah->ah_gain.g_current +=
  335. ee->ee_cck_ofdm_gain_delta;
  336. else
  337. ah->ah_gain.g_current +=
  338. AR5K_GAIN_CCK_PROBE_CORR;
  339. }
  340. /* Further correct gain_F measurement for
  341. * RF5112A radios */
  342. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  343. ath5k_hw_rf_gainf_corr(ah);
  344. ah->ah_gain.g_current =
  345. ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
  346. (ah->ah_gain.g_current-ah->ah_gain.g_f_corr) :
  347. 0;
  348. }
  349. /* Check if measurement is ok and if we need
  350. * to adjust gain, schedule a gain adjustment,
  351. * else switch back to the acive state */
  352. if (ath5k_hw_rf_check_gainf_readback(ah) &&
  353. AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
  354. ath5k_hw_rf_gainf_adjust(ah)) {
  355. ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
  356. } else {
  357. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  358. }
  359. }
  360. done:
  361. return ah->ah_gain.g_state;
  362. }
  363. /* Write initial rf gain table to set the RF sensitivity
  364. * this one works on all RF chips and has nothing to do
  365. * with gain_F calibration */
  366. int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)
  367. {
  368. const struct ath5k_ini_rfgain *ath5k_rfg;
  369. unsigned int i, size;
  370. switch (ah->ah_radio) {
  371. case AR5K_RF5111:
  372. ath5k_rfg = rfgain_5111;
  373. size = ARRAY_SIZE(rfgain_5111);
  374. break;
  375. case AR5K_RF5112:
  376. ath5k_rfg = rfgain_5112;
  377. size = ARRAY_SIZE(rfgain_5112);
  378. break;
  379. case AR5K_RF2413:
  380. ath5k_rfg = rfgain_2413;
  381. size = ARRAY_SIZE(rfgain_2413);
  382. break;
  383. case AR5K_RF2316:
  384. ath5k_rfg = rfgain_2316;
  385. size = ARRAY_SIZE(rfgain_2316);
  386. break;
  387. case AR5K_RF5413:
  388. ath5k_rfg = rfgain_5413;
  389. size = ARRAY_SIZE(rfgain_5413);
  390. break;
  391. case AR5K_RF2317:
  392. case AR5K_RF2425:
  393. ath5k_rfg = rfgain_2425;
  394. size = ARRAY_SIZE(rfgain_2425);
  395. break;
  396. default:
  397. return -EINVAL;
  398. }
  399. switch (freq) {
  400. case AR5K_INI_RFGAIN_2GHZ:
  401. case AR5K_INI_RFGAIN_5GHZ:
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. for (i = 0; i < size; i++) {
  407. AR5K_REG_WAIT(i);
  408. ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[freq],
  409. (u32)ath5k_rfg[i].rfg_register);
  410. }
  411. return 0;
  412. }
  413. /********************\
  414. * RF Registers setup *
  415. \********************/
  416. /*
  417. * Setup RF registers by writing rf buffer on hw
  418. */
  419. int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  420. unsigned int mode)
  421. {
  422. const struct ath5k_rf_reg *rf_regs;
  423. const struct ath5k_ini_rfbuffer *ini_rfb;
  424. const struct ath5k_gain_opt *go = NULL;
  425. const struct ath5k_gain_opt_step *g_step;
  426. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  427. u8 ee_mode = 0;
  428. u32 *rfb;
  429. int i, obdb = -1, bank = -1;
  430. switch (ah->ah_radio) {
  431. case AR5K_RF5111:
  432. rf_regs = rf_regs_5111;
  433. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  434. ini_rfb = rfb_5111;
  435. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5111);
  436. go = &rfgain_opt_5111;
  437. break;
  438. case AR5K_RF5112:
  439. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  440. rf_regs = rf_regs_5112a;
  441. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  442. ini_rfb = rfb_5112a;
  443. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112a);
  444. } else {
  445. rf_regs = rf_regs_5112;
  446. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  447. ini_rfb = rfb_5112;
  448. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112);
  449. }
  450. go = &rfgain_opt_5112;
  451. break;
  452. case AR5K_RF2413:
  453. rf_regs = rf_regs_2413;
  454. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2413);
  455. ini_rfb = rfb_2413;
  456. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2413);
  457. break;
  458. case AR5K_RF2316:
  459. rf_regs = rf_regs_2316;
  460. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2316);
  461. ini_rfb = rfb_2316;
  462. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2316);
  463. break;
  464. case AR5K_RF5413:
  465. rf_regs = rf_regs_5413;
  466. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5413);
  467. ini_rfb = rfb_5413;
  468. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5413);
  469. break;
  470. case AR5K_RF2317:
  471. rf_regs = rf_regs_2425;
  472. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  473. ini_rfb = rfb_2317;
  474. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2317);
  475. break;
  476. case AR5K_RF2425:
  477. rf_regs = rf_regs_2425;
  478. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  479. if (ah->ah_mac_srev < AR5K_SREV_AR2417) {
  480. ini_rfb = rfb_2425;
  481. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2425);
  482. } else {
  483. ini_rfb = rfb_2417;
  484. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2417);
  485. }
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. /* If it's the first time we set rf buffer, allocate
  491. * ah->ah_rf_banks based on ah->ah_rf_banks_size
  492. * we set above */
  493. if (ah->ah_rf_banks == NULL) {
  494. ah->ah_rf_banks = kmalloc(sizeof(u32) * ah->ah_rf_banks_size,
  495. GFP_KERNEL);
  496. if (ah->ah_rf_banks == NULL) {
  497. ATH5K_ERR(ah->ah_sc, "out of memory\n");
  498. return -ENOMEM;
  499. }
  500. }
  501. /* Copy values to modify them */
  502. rfb = ah->ah_rf_banks;
  503. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  504. if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) {
  505. ATH5K_ERR(ah->ah_sc, "invalid bank\n");
  506. return -EINVAL;
  507. }
  508. /* Bank changed, write down the offset */
  509. if (bank != ini_rfb[i].rfb_bank) {
  510. bank = ini_rfb[i].rfb_bank;
  511. ah->ah_offset[bank] = i;
  512. }
  513. rfb[i] = ini_rfb[i].rfb_mode_data[mode];
  514. }
  515. /* Set Output and Driver bias current (OB/DB) */
  516. if (channel->hw_value & CHANNEL_2GHZ) {
  517. if (channel->hw_value & CHANNEL_CCK)
  518. ee_mode = AR5K_EEPROM_MODE_11B;
  519. else
  520. ee_mode = AR5K_EEPROM_MODE_11G;
  521. /* For RF511X/RF211X combination we
  522. * use b_OB and b_DB parameters stored
  523. * in eeprom on ee->ee_ob[ee_mode][0]
  524. *
  525. * For all other chips we use OB/DB for 2Ghz
  526. * stored in the b/g modal section just like
  527. * 802.11a on ee->ee_ob[ee_mode][1] */
  528. if ((ah->ah_radio == AR5K_RF5111) ||
  529. (ah->ah_radio == AR5K_RF5112))
  530. obdb = 0;
  531. else
  532. obdb = 1;
  533. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  534. AR5K_RF_OB_2GHZ, true);
  535. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  536. AR5K_RF_DB_2GHZ, true);
  537. /* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */
  538. } else if ((channel->hw_value & CHANNEL_5GHZ) ||
  539. (ah->ah_radio == AR5K_RF5111)) {
  540. /* For 11a, Turbo and XR we need to choose
  541. * OB/DB based on frequency range */
  542. ee_mode = AR5K_EEPROM_MODE_11A;
  543. obdb = channel->center_freq >= 5725 ? 3 :
  544. (channel->center_freq >= 5500 ? 2 :
  545. (channel->center_freq >= 5260 ? 1 :
  546. (channel->center_freq > 4000 ? 0 : -1)));
  547. if (obdb < 0)
  548. return -EINVAL;
  549. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  550. AR5K_RF_OB_5GHZ, true);
  551. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  552. AR5K_RF_DB_5GHZ, true);
  553. }
  554. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  555. /* Bank Modifications (chip-specific) */
  556. if (ah->ah_radio == AR5K_RF5111) {
  557. /* Set gain_F settings according to current step */
  558. if (channel->hw_value & CHANNEL_OFDM) {
  559. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
  560. AR5K_PHY_FRAME_CTL_TX_CLIP,
  561. g_step->gos_param[0]);
  562. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  563. AR5K_RF_PWD_90, true);
  564. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  565. AR5K_RF_PWD_84, true);
  566. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  567. AR5K_RF_RFGAIN_SEL, true);
  568. /* We programmed gain_F parameters, switch back
  569. * to active state */
  570. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  571. }
  572. /* Bank 6/7 setup */
  573. ath5k_hw_rfb_op(ah, rf_regs, !ee->ee_xpd[ee_mode],
  574. AR5K_RF_PWD_XPD, true);
  575. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_x_gain[ee_mode],
  576. AR5K_RF_XPD_GAIN, true);
  577. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  578. AR5K_RF_GAIN_I, true);
  579. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  580. AR5K_RF_PLO_SEL, true);
  581. /* TODO: Half/quarter channel support */
  582. }
  583. if (ah->ah_radio == AR5K_RF5112) {
  584. /* Set gain_F settings according to current step */
  585. if (channel->hw_value & CHANNEL_OFDM) {
  586. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],
  587. AR5K_RF_MIXGAIN_OVR, true);
  588. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  589. AR5K_RF_PWD_138, true);
  590. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  591. AR5K_RF_PWD_137, true);
  592. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  593. AR5K_RF_PWD_136, true);
  594. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[4],
  595. AR5K_RF_PWD_132, true);
  596. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[5],
  597. AR5K_RF_PWD_131, true);
  598. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[6],
  599. AR5K_RF_PWD_130, true);
  600. /* We programmed gain_F parameters, switch back
  601. * to active state */
  602. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  603. }
  604. /* Bank 6/7 setup */
  605. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  606. AR5K_RF_XPD_SEL, true);
  607. if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
  608. /* Rev. 1 supports only one xpd */
  609. ath5k_hw_rfb_op(ah, rf_regs,
  610. ee->ee_x_gain[ee_mode],
  611. AR5K_RF_XPD_GAIN, true);
  612. } else {
  613. /* TODO: Set high and low gain bits */
  614. ath5k_hw_rfb_op(ah, rf_regs,
  615. ee->ee_x_gain[ee_mode],
  616. AR5K_RF_PD_GAIN_LO, true);
  617. ath5k_hw_rfb_op(ah, rf_regs,
  618. ee->ee_x_gain[ee_mode],
  619. AR5K_RF_PD_GAIN_HI, true);
  620. /* Lower synth voltage on Rev 2 */
  621. ath5k_hw_rfb_op(ah, rf_regs, 2,
  622. AR5K_RF_HIGH_VC_CP, true);
  623. ath5k_hw_rfb_op(ah, rf_regs, 2,
  624. AR5K_RF_MID_VC_CP, true);
  625. ath5k_hw_rfb_op(ah, rf_regs, 2,
  626. AR5K_RF_LOW_VC_CP, true);
  627. ath5k_hw_rfb_op(ah, rf_regs, 2,
  628. AR5K_RF_PUSH_UP, true);
  629. /* Decrease power consumption on 5213+ BaseBand */
  630. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  631. ath5k_hw_rfb_op(ah, rf_regs, 1,
  632. AR5K_RF_PAD2GND, true);
  633. ath5k_hw_rfb_op(ah, rf_regs, 1,
  634. AR5K_RF_XB2_LVL, true);
  635. ath5k_hw_rfb_op(ah, rf_regs, 1,
  636. AR5K_RF_XB5_LVL, true);
  637. ath5k_hw_rfb_op(ah, rf_regs, 1,
  638. AR5K_RF_PWD_167, true);
  639. ath5k_hw_rfb_op(ah, rf_regs, 1,
  640. AR5K_RF_PWD_166, true);
  641. }
  642. }
  643. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  644. AR5K_RF_GAIN_I, true);
  645. /* TODO: Half/quarter channel support */
  646. }
  647. if (ah->ah_radio == AR5K_RF5413 &&
  648. channel->hw_value & CHANNEL_2GHZ) {
  649. ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,
  650. true);
  651. /* Set optimum value for early revisions (on pci-e chips) */
  652. if (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
  653. ah->ah_mac_srev < AR5K_SREV_AR5413)
  654. ath5k_hw_rfb_op(ah, rf_regs, ath5k_hw_bitswap(6, 3),
  655. AR5K_RF_PWD_ICLOBUF_2G, true);
  656. }
  657. /* Write RF banks on hw */
  658. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  659. AR5K_REG_WAIT(i);
  660. ath5k_hw_reg_write(ah, rfb[i], ini_rfb[i].rfb_ctrl_register);
  661. }
  662. return 0;
  663. }
  664. /**************************\
  665. PHY/RF channel functions
  666. \**************************/
  667. /*
  668. * Check if a channel is supported
  669. */
  670. bool ath5k_channel_ok(struct ath5k_hw *ah, u16 freq, unsigned int flags)
  671. {
  672. /* Check if the channel is in our supported range */
  673. if (flags & CHANNEL_2GHZ) {
  674. if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
  675. (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
  676. return true;
  677. } else if (flags & CHANNEL_5GHZ)
  678. if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
  679. (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
  680. return true;
  681. return false;
  682. }
  683. /*
  684. * Convertion needed for RF5110
  685. */
  686. static u32 ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)
  687. {
  688. u32 athchan;
  689. /*
  690. * Convert IEEE channel/MHz to an internal channel value used
  691. * by the AR5210 chipset. This has not been verified with
  692. * newer chipsets like the AR5212A who have a completely
  693. * different RF/PHY part.
  694. */
  695. athchan = (ath5k_hw_bitswap(
  696. (ieee80211_frequency_to_channel(
  697. channel->center_freq) - 24) / 2, 5)
  698. << 1) | (1 << 6) | 0x1;
  699. return athchan;
  700. }
  701. /*
  702. * Set channel on RF5110
  703. */
  704. static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
  705. struct ieee80211_channel *channel)
  706. {
  707. u32 data;
  708. /*
  709. * Set the channel and wait
  710. */
  711. data = ath5k_hw_rf5110_chan2athchan(channel);
  712. ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);
  713. ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0);
  714. mdelay(1);
  715. return 0;
  716. }
  717. /*
  718. * Convertion needed for 5111
  719. */
  720. static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
  721. struct ath5k_athchan_2ghz *athchan)
  722. {
  723. int channel;
  724. /* Cast this value to catch negative channel numbers (>= -19) */
  725. channel = (int)ieee;
  726. /*
  727. * Map 2GHz IEEE channel to 5GHz Atheros channel
  728. */
  729. if (channel <= 13) {
  730. athchan->a2_athchan = 115 + channel;
  731. athchan->a2_flags = 0x46;
  732. } else if (channel == 14) {
  733. athchan->a2_athchan = 124;
  734. athchan->a2_flags = 0x44;
  735. } else if (channel >= 15 && channel <= 26) {
  736. athchan->a2_athchan = ((channel - 14) * 4) + 132;
  737. athchan->a2_flags = 0x46;
  738. } else
  739. return -EINVAL;
  740. return 0;
  741. }
  742. /*
  743. * Set channel on 5111
  744. */
  745. static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
  746. struct ieee80211_channel *channel)
  747. {
  748. struct ath5k_athchan_2ghz ath5k_channel_2ghz;
  749. unsigned int ath5k_channel =
  750. ieee80211_frequency_to_channel(channel->center_freq);
  751. u32 data0, data1, clock;
  752. int ret;
  753. /*
  754. * Set the channel on the RF5111 radio
  755. */
  756. data0 = data1 = 0;
  757. if (channel->hw_value & CHANNEL_2GHZ) {
  758. /* Map 2GHz channel to 5GHz Atheros channel ID */
  759. ret = ath5k_hw_rf5111_chan2athchan(
  760. ieee80211_frequency_to_channel(channel->center_freq),
  761. &ath5k_channel_2ghz);
  762. if (ret)
  763. return ret;
  764. ath5k_channel = ath5k_channel_2ghz.a2_athchan;
  765. data0 = ((ath5k_hw_bitswap(ath5k_channel_2ghz.a2_flags, 8) & 0xff)
  766. << 5) | (1 << 4);
  767. }
  768. if (ath5k_channel < 145 || !(ath5k_channel & 1)) {
  769. clock = 1;
  770. data1 = ((ath5k_hw_bitswap(ath5k_channel - 24, 8) & 0xff) << 2) |
  771. (clock << 1) | (1 << 10) | 1;
  772. } else {
  773. clock = 0;
  774. data1 = ((ath5k_hw_bitswap((ath5k_channel - 24) / 2, 8) & 0xff)
  775. << 2) | (clock << 1) | (1 << 10) | 1;
  776. }
  777. ath5k_hw_reg_write(ah, (data1 & 0xff) | ((data0 & 0xff) << 8),
  778. AR5K_RF_BUFFER);
  779. ath5k_hw_reg_write(ah, ((data1 >> 8) & 0xff) | (data0 & 0xff00),
  780. AR5K_RF_BUFFER_CONTROL_3);
  781. return 0;
  782. }
  783. /*
  784. * Set channel on 5112 and newer
  785. */
  786. static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah,
  787. struct ieee80211_channel *channel)
  788. {
  789. u32 data, data0, data1, data2;
  790. u16 c;
  791. data = data0 = data1 = data2 = 0;
  792. c = channel->center_freq;
  793. if (c < 4800) {
  794. if (!((c - 2224) % 5)) {
  795. data0 = ((2 * (c - 704)) - 3040) / 10;
  796. data1 = 1;
  797. } else if (!((c - 2192) % 5)) {
  798. data0 = ((2 * (c - 672)) - 3040) / 10;
  799. data1 = 0;
  800. } else
  801. return -EINVAL;
  802. data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
  803. } else if ((c - (c % 5)) != 2 || c > 5435) {
  804. if (!(c % 20) && c >= 5120) {
  805. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  806. data2 = ath5k_hw_bitswap(3, 2);
  807. } else if (!(c % 10)) {
  808. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  809. data2 = ath5k_hw_bitswap(2, 2);
  810. } else if (!(c % 5)) {
  811. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  812. data2 = ath5k_hw_bitswap(1, 2);
  813. } else
  814. return -EINVAL;
  815. } else {
  816. data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
  817. data2 = ath5k_hw_bitswap(0, 2);
  818. }
  819. data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
  820. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  821. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  822. return 0;
  823. }
  824. /*
  825. * Set the channel on the RF2425
  826. */
  827. static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
  828. struct ieee80211_channel *channel)
  829. {
  830. u32 data, data0, data2;
  831. u16 c;
  832. data = data0 = data2 = 0;
  833. c = channel->center_freq;
  834. if (c < 4800) {
  835. data0 = ath5k_hw_bitswap((c - 2272), 8);
  836. data2 = 0;
  837. /* ? 5GHz ? */
  838. } else if ((c - (c % 5)) != 2 || c > 5435) {
  839. if (!(c % 20) && c < 5120)
  840. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  841. else if (!(c % 10))
  842. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  843. else if (!(c % 5))
  844. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  845. else
  846. return -EINVAL;
  847. data2 = ath5k_hw_bitswap(1, 2);
  848. } else {
  849. data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
  850. data2 = ath5k_hw_bitswap(0, 2);
  851. }
  852. data = (data0 << 4) | data2 << 2 | 0x1001;
  853. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  854. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  855. return 0;
  856. }
  857. /*
  858. * Set a channel on the radio chip
  859. */
  860. int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel)
  861. {
  862. int ret;
  863. /*
  864. * Check bounds supported by the PHY (we don't care about regultory
  865. * restrictions at this point). Note: hw_value already has the band
  866. * (CHANNEL_2GHZ, or CHANNEL_5GHZ) so we inform ath5k_channel_ok()
  867. * of the band by that */
  868. if (!ath5k_channel_ok(ah, channel->center_freq, channel->hw_value)) {
  869. ATH5K_ERR(ah->ah_sc,
  870. "channel frequency (%u MHz) out of supported "
  871. "band range\n",
  872. channel->center_freq);
  873. return -EINVAL;
  874. }
  875. /*
  876. * Set the channel and wait
  877. */
  878. switch (ah->ah_radio) {
  879. case AR5K_RF5110:
  880. ret = ath5k_hw_rf5110_channel(ah, channel);
  881. break;
  882. case AR5K_RF5111:
  883. ret = ath5k_hw_rf5111_channel(ah, channel);
  884. break;
  885. case AR5K_RF2425:
  886. ret = ath5k_hw_rf2425_channel(ah, channel);
  887. break;
  888. default:
  889. ret = ath5k_hw_rf5112_channel(ah, channel);
  890. break;
  891. }
  892. if (ret)
  893. return ret;
  894. /* Set JAPAN setting for channel 14 */
  895. if (channel->center_freq == 2484) {
  896. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  897. AR5K_PHY_CCKTXCTL_JAPAN);
  898. } else {
  899. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  900. AR5K_PHY_CCKTXCTL_WORLD);
  901. }
  902. ah->ah_current_channel.center_freq = channel->center_freq;
  903. ah->ah_current_channel.hw_value = channel->hw_value;
  904. ah->ah_turbo = channel->hw_value == CHANNEL_T ? true : false;
  905. return 0;
  906. }
  907. /*****************\
  908. PHY calibration
  909. \*****************/
  910. /**
  911. * ath5k_hw_noise_floor_calibration - perform PHY noise floor calibration
  912. *
  913. * @ah: struct ath5k_hw pointer we are operating on
  914. * @freq: the channel frequency, just used for error logging
  915. *
  916. * This function performs a noise floor calibration of the PHY and waits for
  917. * it to complete. Then the noise floor value is compared to some maximum
  918. * noise floor we consider valid.
  919. *
  920. * Note that this is different from what the madwifi HAL does: it reads the
  921. * noise floor and afterwards initiates the calibration. Since the noise floor
  922. * calibration can take some time to finish, depending on the current channel
  923. * use, that avoids the occasional timeout warnings we are seeing now.
  924. *
  925. * See the following link for an Atheros patent on noise floor calibration:
  926. * http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL \
  927. * &p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=7245893.PN.&OS=PN/7
  928. *
  929. * XXX: Since during noise floor calibration antennas are detached according to
  930. * the patent, we should stop tx queues here.
  931. */
  932. int
  933. ath5k_hw_noise_floor_calibration(struct ath5k_hw *ah, short freq)
  934. {
  935. int ret;
  936. unsigned int i;
  937. s32 noise_floor;
  938. /*
  939. * Enable noise floor calibration
  940. */
  941. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  942. AR5K_PHY_AGCCTL_NF);
  943. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  944. AR5K_PHY_AGCCTL_NF, 0, false);
  945. if (ret) {
  946. ATH5K_ERR(ah->ah_sc,
  947. "noise floor calibration timeout (%uMHz)\n", freq);
  948. return -EAGAIN;
  949. }
  950. /* Wait until the noise floor is calibrated and read the value */
  951. for (i = 20; i > 0; i--) {
  952. mdelay(1);
  953. noise_floor = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
  954. noise_floor = AR5K_PHY_NF_RVAL(noise_floor);
  955. if (noise_floor & AR5K_PHY_NF_ACTIVE) {
  956. noise_floor = AR5K_PHY_NF_AVAL(noise_floor);
  957. if (noise_floor <= AR5K_TUNE_NOISE_FLOOR)
  958. break;
  959. }
  960. }
  961. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  962. "noise floor %d\n", noise_floor);
  963. if (noise_floor > AR5K_TUNE_NOISE_FLOOR) {
  964. ATH5K_ERR(ah->ah_sc,
  965. "noise floor calibration failed (%uMHz)\n", freq);
  966. return -EAGAIN;
  967. }
  968. ah->ah_noise_floor = noise_floor;
  969. return 0;
  970. }
  971. /*
  972. * Perform a PHY calibration on RF5110
  973. * -Fix BPSK/QAM Constellation (I/Q correction)
  974. * -Calculate Noise Floor
  975. */
  976. static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
  977. struct ieee80211_channel *channel)
  978. {
  979. u32 phy_sig, phy_agc, phy_sat, beacon;
  980. int ret;
  981. /*
  982. * Disable beacons and RX/TX queues, wait
  983. */
  984. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
  985. AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
  986. beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
  987. ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
  988. mdelay(2);
  989. /*
  990. * Set the channel (with AGC turned off)
  991. */
  992. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  993. udelay(10);
  994. ret = ath5k_hw_channel(ah, channel);
  995. /*
  996. * Activate PHY and wait
  997. */
  998. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  999. mdelay(1);
  1000. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1001. if (ret)
  1002. return ret;
  1003. /*
  1004. * Calibrate the radio chip
  1005. */
  1006. /* Remember normal state */
  1007. phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
  1008. phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
  1009. phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
  1010. /* Update radio registers */
  1011. ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
  1012. AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
  1013. ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
  1014. AR5K_PHY_AGCCOARSE_LO)) |
  1015. AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
  1016. AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
  1017. ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
  1018. AR5K_PHY_ADCSAT_THR)) |
  1019. AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
  1020. AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
  1021. udelay(20);
  1022. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1023. udelay(10);
  1024. ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
  1025. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1026. mdelay(1);
  1027. /*
  1028. * Enable calibration and wait until completion
  1029. */
  1030. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
  1031. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1032. AR5K_PHY_AGCCTL_CAL, 0, false);
  1033. /* Reset to normal state */
  1034. ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
  1035. ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
  1036. ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
  1037. if (ret) {
  1038. ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n",
  1039. channel->center_freq);
  1040. return ret;
  1041. }
  1042. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  1043. /*
  1044. * Re-enable RX/TX and beacons
  1045. */
  1046. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1047. AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
  1048. ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
  1049. return 0;
  1050. }
  1051. /*
  1052. * Perform a PHY calibration on RF5111/5112 and newer chips
  1053. */
  1054. static int ath5k_hw_rf511x_calibrate(struct ath5k_hw *ah,
  1055. struct ieee80211_channel *channel)
  1056. {
  1057. u32 i_pwr, q_pwr;
  1058. s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
  1059. int i;
  1060. ATH5K_TRACE(ah->ah_sc);
  1061. if (!ah->ah_calibration ||
  1062. ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN)
  1063. goto done;
  1064. /* Calibration has finished, get the results and re-run */
  1065. for (i = 0; i <= 10; i++) {
  1066. iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
  1067. i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
  1068. q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
  1069. }
  1070. i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
  1071. q_coffd = q_pwr >> 7;
  1072. /* No correction */
  1073. if (i_coffd == 0 || q_coffd == 0)
  1074. goto done;
  1075. i_coff = ((-iq_corr) / i_coffd) & 0x3f;
  1076. /* Boundary check */
  1077. if (i_coff > 31)
  1078. i_coff = 31;
  1079. if (i_coff < -32)
  1080. i_coff = -32;
  1081. q_coff = (((s32)i_pwr / q_coffd) - 128) & 0x1f;
  1082. /* Boundary check */
  1083. if (q_coff > 15)
  1084. q_coff = 15;
  1085. if (q_coff < -16)
  1086. q_coff = -16;
  1087. /* Commit new I/Q value */
  1088. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE |
  1089. ((u32)q_coff) | ((u32)i_coff << AR5K_PHY_IQ_CORR_Q_I_COFF_S));
  1090. /* Re-enable calibration -if we don't we'll commit
  1091. * the same values again and again */
  1092. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1093. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1094. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
  1095. done:
  1096. /* TODO: Separate noise floor calibration from I/Q calibration
  1097. * since noise floor calibration interrupts rx path while I/Q
  1098. * calibration doesn't. We don't need to run noise floor calibration
  1099. * as often as I/Q calibration.*/
  1100. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  1101. /* Initiate a gain_F calibration */
  1102. ath5k_hw_request_rfgain_probe(ah);
  1103. return 0;
  1104. }
  1105. /*
  1106. * Perform a PHY calibration
  1107. */
  1108. int ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
  1109. struct ieee80211_channel *channel)
  1110. {
  1111. int ret;
  1112. if (ah->ah_radio == AR5K_RF5110)
  1113. ret = ath5k_hw_rf5110_calibrate(ah, channel);
  1114. else
  1115. ret = ath5k_hw_rf511x_calibrate(ah, channel);
  1116. return ret;
  1117. }
  1118. int ath5k_hw_phy_disable(struct ath5k_hw *ah)
  1119. {
  1120. ATH5K_TRACE(ah->ah_sc);
  1121. /*Just a try M.F.*/
  1122. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  1123. return 0;
  1124. }
  1125. /********************\
  1126. Misc PHY functions
  1127. \********************/
  1128. /*
  1129. * Get the PHY Chip revision
  1130. */
  1131. u16 ath5k_hw_radio_revision(struct ath5k_hw *ah, unsigned int chan)
  1132. {
  1133. unsigned int i;
  1134. u32 srev;
  1135. u16 ret;
  1136. ATH5K_TRACE(ah->ah_sc);
  1137. /*
  1138. * Set the radio chip access register
  1139. */
  1140. switch (chan) {
  1141. case CHANNEL_2GHZ:
  1142. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
  1143. break;
  1144. case CHANNEL_5GHZ:
  1145. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1146. break;
  1147. default:
  1148. return 0;
  1149. }
  1150. mdelay(2);
  1151. /* ...wait until PHY is ready and read the selected radio revision */
  1152. ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
  1153. for (i = 0; i < 8; i++)
  1154. ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
  1155. if (ah->ah_version == AR5K_AR5210) {
  1156. srev = ath5k_hw_reg_read(ah, AR5K_PHY(256) >> 28) & 0xf;
  1157. ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
  1158. } else {
  1159. srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
  1160. ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
  1161. ((srev & 0x0f) << 4), 8);
  1162. }
  1163. /* Reset to the 5GHz mode */
  1164. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1165. return ret;
  1166. }
  1167. void /*TODO:Boundary check*/
  1168. ath5k_hw_set_def_antenna(struct ath5k_hw *ah, unsigned int ant)
  1169. {
  1170. ATH5K_TRACE(ah->ah_sc);
  1171. /*Just a try M.F.*/
  1172. if (ah->ah_version != AR5K_AR5210)
  1173. ath5k_hw_reg_write(ah, ant, AR5K_DEFAULT_ANTENNA);
  1174. }
  1175. unsigned int ath5k_hw_get_def_antenna(struct ath5k_hw *ah)
  1176. {
  1177. ATH5K_TRACE(ah->ah_sc);
  1178. /*Just a try M.F.*/
  1179. if (ah->ah_version != AR5K_AR5210)
  1180. return ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
  1181. return false; /*XXX: What do we return for 5210 ?*/
  1182. }
  1183. /*
  1184. * TX power setup
  1185. */
  1186. /*
  1187. * Initialize the tx power table (not fully implemented)
  1188. */
  1189. static void ath5k_txpower_table(struct ath5k_hw *ah,
  1190. struct ieee80211_channel *channel, s16 max_power)
  1191. {
  1192. unsigned int i, min, max, n;
  1193. u16 txpower, *rates;
  1194. rates = ah->ah_txpower.txp_rates;
  1195. txpower = AR5K_TUNE_DEFAULT_TXPOWER * 2;
  1196. if (max_power > txpower)
  1197. txpower = max_power > AR5K_TUNE_MAX_TXPOWER ?
  1198. AR5K_TUNE_MAX_TXPOWER : max_power;
  1199. for (i = 0; i < AR5K_MAX_RATES; i++)
  1200. rates[i] = txpower;
  1201. /* XXX setup target powers by rate */
  1202. ah->ah_txpower.txp_min = rates[7];
  1203. ah->ah_txpower.txp_max = rates[0];
  1204. ah->ah_txpower.txp_ofdm = rates[0];
  1205. /* Calculate the power table */
  1206. n = ARRAY_SIZE(ah->ah_txpower.txp_pcdac);
  1207. min = AR5K_EEPROM_PCDAC_START;
  1208. max = AR5K_EEPROM_PCDAC_STOP;
  1209. for (i = 0; i < n; i += AR5K_EEPROM_PCDAC_STEP)
  1210. ah->ah_txpower.txp_pcdac[i] =
  1211. #ifdef notyet
  1212. min + ((i * (max - min)) / n);
  1213. #else
  1214. min;
  1215. #endif
  1216. }
  1217. /*
  1218. * Set transmition power
  1219. */
  1220. int /*O.K. - txpower_table is unimplemented so this doesn't work*/
  1221. ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  1222. unsigned int txpower)
  1223. {
  1224. bool tpc = ah->ah_txpower.txp_tpc;
  1225. unsigned int i;
  1226. ATH5K_TRACE(ah->ah_sc);
  1227. if (txpower > AR5K_TUNE_MAX_TXPOWER) {
  1228. ATH5K_ERR(ah->ah_sc, "invalid tx power: %u\n", txpower);
  1229. return -EINVAL;
  1230. }
  1231. /*
  1232. * RF2413 for some reason can't
  1233. * transmit anything if we call
  1234. * this funtion, so we skip it
  1235. * until we fix txpower.
  1236. *
  1237. * XXX: Assume same for RF2425
  1238. * to be safe.
  1239. */
  1240. if ((ah->ah_radio == AR5K_RF2413) || (ah->ah_radio == AR5K_RF2425))
  1241. return 0;
  1242. /* Reset TX power values */
  1243. memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
  1244. ah->ah_txpower.txp_tpc = tpc;
  1245. /* Initialize TX power table */
  1246. ath5k_txpower_table(ah, channel, txpower);
  1247. /*
  1248. * Write TX power values
  1249. */
  1250. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  1251. ath5k_hw_reg_write(ah,
  1252. ((((ah->ah_txpower.txp_pcdac[(i << 1) + 1] << 8) | 0xff) & 0xffff) << 16) |
  1253. (((ah->ah_txpower.txp_pcdac[(i << 1) ] << 8) | 0xff) & 0xffff),
  1254. AR5K_PHY_PCDAC_TXPOWER(i));
  1255. }
  1256. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
  1257. AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
  1258. AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
  1259. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
  1260. AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
  1261. AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
  1262. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
  1263. AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
  1264. AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
  1265. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
  1266. AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
  1267. AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
  1268. if (ah->ah_txpower.txp_tpc)
  1269. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
  1270. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  1271. else
  1272. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX |
  1273. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  1274. return 0;
  1275. }
  1276. int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, unsigned int power)
  1277. {
  1278. /*Just a try M.F.*/
  1279. struct ieee80211_channel *channel = &ah->ah_current_channel;
  1280. ATH5K_TRACE(ah->ah_sc);
  1281. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_TXPOWER,
  1282. "changing txpower to %d\n", power);
  1283. return ath5k_hw_txpower(ah, channel, power);
  1284. }
  1285. #undef _ATH5K_PHY