Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ath9k_ar9003_calib.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /*
  2. * Copyright (c) 2010-2011 Atheros Communications Inc.
  3. *
  4. * Modified for iPXE by Scott K Logan <logans@cottsay.net> July 2011
  5. * Original from Linux kernel 3.0.1
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include <ipxe/io.h>
  20. #include "hw.h"
  21. #include "hw-ops.h"
  22. #include "ar9003_phy.h"
  23. #define MAX_MEASUREMENT 8
  24. #define MAX_MAG_DELTA 11
  25. #define MAX_PHS_DELTA 10
  26. struct coeff {
  27. int mag_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
  28. int phs_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
  29. int iqc_coeff[2];
  30. };
  31. enum ar9003_cal_types {
  32. IQ_MISMATCH_CAL = BIT(0),
  33. TEMP_COMP_CAL = BIT(1),
  34. };
  35. static void ar9003_hw_setup_calibration(struct ath_hw *ah,
  36. struct ath9k_cal_list *currCal)
  37. {
  38. /* Select calibration to run */
  39. switch (currCal->calData->calType) {
  40. case IQ_MISMATCH_CAL:
  41. /*
  42. * Start calibration with
  43. * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
  44. */
  45. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  46. AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX,
  47. currCal->calData->calCountMax);
  48. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  49. DBG2("ath9k: "
  50. "starting IQ Mismatch Calibration\n");
  51. /* Kick-off cal */
  52. REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
  53. break;
  54. case TEMP_COMP_CAL:
  55. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  56. AR_PHY_65NM_CH0_THERM_LOCAL, 1);
  57. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  58. AR_PHY_65NM_CH0_THERM_START, 1);
  59. DBG2("ath9k: "
  60. "starting Temperature Compensation Calibration\n");
  61. break;
  62. }
  63. }
  64. /*
  65. * Generic calibration routine.
  66. * Recalibrate the lower PHY chips to account for temperature/environment
  67. * changes.
  68. */
  69. static int ar9003_hw_per_calibration(struct ath_hw *ah,
  70. struct ath9k_channel *ichan __unused,
  71. u8 rxchainmask,
  72. struct ath9k_cal_list *currCal)
  73. {
  74. struct ath9k_hw_cal_data *caldata = ah->caldata;
  75. /* Cal is assumed not done until explicitly set below */
  76. int iscaldone = 0;
  77. /* Calibration in progress. */
  78. if (currCal->calState == CAL_RUNNING) {
  79. /* Check to see if it has finished. */
  80. if (!(REG_READ(ah, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
  81. /*
  82. * Accumulate cal measures for active chains
  83. */
  84. currCal->calData->calCollect(ah);
  85. ah->cal_samples++;
  86. if (ah->cal_samples >=
  87. currCal->calData->calNumSamples) {
  88. unsigned int i, numChains = 0;
  89. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  90. if (rxchainmask & (1 << i))
  91. numChains++;
  92. }
  93. /*
  94. * Process accumulated data
  95. */
  96. currCal->calData->calPostProc(ah, numChains);
  97. /* Calibration has finished. */
  98. caldata->CalValid |= currCal->calData->calType;
  99. currCal->calState = CAL_DONE;
  100. iscaldone = 1;
  101. } else {
  102. /*
  103. * Set-up collection of another sub-sample until we
  104. * get desired number
  105. */
  106. ar9003_hw_setup_calibration(ah, currCal);
  107. }
  108. }
  109. } else if (!(caldata->CalValid & currCal->calData->calType)) {
  110. /* If current cal is marked invalid in channel, kick it off */
  111. ath9k_hw_reset_calibration(ah, currCal);
  112. }
  113. return iscaldone;
  114. }
  115. static int ar9003_hw_calibrate(struct ath_hw *ah,
  116. struct ath9k_channel *chan,
  117. u8 rxchainmask,
  118. int longcal)
  119. {
  120. int iscaldone = 1;
  121. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  122. /*
  123. * For given calibration:
  124. * 1. Call generic cal routine
  125. * 2. When this cal is done (isCalDone) if we have more cals waiting
  126. * (eg after reset), mask this to upper layers by not propagating
  127. * isCalDone if it is set to TRUE.
  128. * Instead, change isCalDone to FALSE and setup the waiting cal(s)
  129. * to be run.
  130. */
  131. if (currCal &&
  132. (currCal->calState == CAL_RUNNING ||
  133. currCal->calState == CAL_WAITING)) {
  134. iscaldone = ar9003_hw_per_calibration(ah, chan,
  135. rxchainmask, currCal);
  136. if (iscaldone) {
  137. ah->cal_list_curr = currCal = currCal->calNext;
  138. if (currCal->calState == CAL_WAITING) {
  139. iscaldone = 0;
  140. ath9k_hw_reset_calibration(ah, currCal);
  141. }
  142. }
  143. }
  144. /* Do NF cal only at longer intervals */
  145. if (longcal) {
  146. /*
  147. * Get the value from the previous NF cal and update
  148. * history buffer.
  149. */
  150. ath9k_hw_getnf(ah, chan);
  151. /*
  152. * Load the NF from history buffer of the current channel.
  153. * NF is slow time-variant, so it is OK to use a historical
  154. * value.
  155. */
  156. ath9k_hw_loadnf(ah, ah->curchan);
  157. /* start NF calibration, without updating BB NF register */
  158. ath9k_hw_start_nfcal(ah, 0);
  159. }
  160. return iscaldone;
  161. }
  162. static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
  163. {
  164. int i;
  165. /* Accumulate IQ cal measures for active chains */
  166. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  167. if (ah->txchainmask & BIT(i)) {
  168. ah->totalPowerMeasI[i] +=
  169. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  170. ah->totalPowerMeasQ[i] +=
  171. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  172. ah->totalIqCorrMeas[i] +=
  173. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  174. DBG2("ath9k: "
  175. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  176. ah->cal_samples, i, ah->totalPowerMeasI[i],
  177. ah->totalPowerMeasQ[i],
  178. ah->totalIqCorrMeas[i]);
  179. }
  180. }
  181. }
  182. static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
  183. {
  184. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  185. u32 qCoffDenom, iCoffDenom;
  186. int32_t qCoff, iCoff;
  187. int iqCorrNeg, i;
  188. static const uint32_t offset_array[3] = {
  189. AR_PHY_RX_IQCAL_CORR_B0,
  190. AR_PHY_RX_IQCAL_CORR_B1,
  191. AR_PHY_RX_IQCAL_CORR_B2,
  192. };
  193. for (i = 0; i < numChains; i++) {
  194. powerMeasI = ah->totalPowerMeasI[i];
  195. powerMeasQ = ah->totalPowerMeasQ[i];
  196. iqCorrMeas = ah->totalIqCorrMeas[i];
  197. DBG2("ath9k: "
  198. "Starting IQ Cal and Correction for Chain %d\n",
  199. i);
  200. DBG2("ath9k: "
  201. "Orignal: Chn %diq_corr_meas = 0x%08x\n",
  202. i, ah->totalIqCorrMeas[i]);
  203. iqCorrNeg = 0;
  204. if (iqCorrMeas > 0x80000000) {
  205. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  206. iqCorrNeg = 1;
  207. }
  208. DBG2("ath9k: "
  209. "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
  210. DBG2("ath9k: "
  211. "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
  212. DBG2("ath9k: iqCorrNeg is 0x%08x\n",
  213. iqCorrNeg);
  214. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
  215. qCoffDenom = powerMeasQ / 64;
  216. if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
  217. iCoff = iqCorrMeas / iCoffDenom;
  218. qCoff = powerMeasI / qCoffDenom - 64;
  219. DBG2("ath9k: "
  220. "Chn %d iCoff = 0x%08x\n", i, iCoff);
  221. DBG2("ath9k: "
  222. "Chn %d qCoff = 0x%08x\n", i, qCoff);
  223. /* Force bounds on iCoff */
  224. if (iCoff >= 63)
  225. iCoff = 63;
  226. else if (iCoff <= -63)
  227. iCoff = -63;
  228. /* Negate iCoff if iqCorrNeg == 0 */
  229. if (iqCorrNeg == 0x0)
  230. iCoff = -iCoff;
  231. /* Force bounds on qCoff */
  232. if (qCoff >= 63)
  233. qCoff = 63;
  234. else if (qCoff <= -63)
  235. qCoff = -63;
  236. iCoff = iCoff & 0x7f;
  237. qCoff = qCoff & 0x7f;
  238. DBG2("ath9k: "
  239. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  240. i, iCoff, qCoff);
  241. DBG2("ath9k: "
  242. "Register offset (0x%04x) before update = 0x%x\n",
  243. offset_array[i],
  244. REG_READ(ah, offset_array[i]));
  245. REG_RMW_FIELD(ah, offset_array[i],
  246. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  247. iCoff);
  248. REG_RMW_FIELD(ah, offset_array[i],
  249. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  250. qCoff);
  251. DBG2("ath9k: "
  252. "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
  253. offset_array[i],
  254. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  255. REG_READ(ah, offset_array[i]));
  256. DBG2("ath9k: "
  257. "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
  258. offset_array[i],
  259. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  260. REG_READ(ah, offset_array[i]));
  261. DBG2("ath9k: "
  262. "IQ Cal and Correction done for Chain %d\n", i);
  263. }
  264. }
  265. REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
  266. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
  267. DBG2("ath9k: "
  268. "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
  269. (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
  270. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
  271. REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
  272. }
  273. static const struct ath9k_percal_data iq_cal_single_sample = {
  274. IQ_MISMATCH_CAL,
  275. MIN_CAL_SAMPLES,
  276. PER_MAX_LOG_COUNT,
  277. ar9003_hw_iqcal_collect,
  278. ar9003_hw_iqcalibrate
  279. };
  280. static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
  281. {
  282. ah->iq_caldata.calData = &iq_cal_single_sample;
  283. }
  284. /*
  285. * solve 4x4 linear equation used in loopback iq cal.
  286. */
  287. static int ar9003_hw_solve_iq_cal(struct ath_hw *ah __unused,
  288. s32 sin_2phi_1,
  289. s32 cos_2phi_1,
  290. s32 sin_2phi_2,
  291. s32 cos_2phi_2,
  292. s32 mag_a0_d0,
  293. s32 phs_a0_d0,
  294. s32 mag_a1_d0,
  295. s32 phs_a1_d0,
  296. s32 solved_eq[])
  297. {
  298. s32 f1 = cos_2phi_1 - cos_2phi_2,
  299. f3 = sin_2phi_1 - sin_2phi_2,
  300. f2;
  301. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  302. const s32 result_shift = 1 << 15;
  303. f2 = (f1 * f1 + f3 * f3) / result_shift;
  304. if (!f2) {
  305. DBG("ath9k: Divide by 0\n");
  306. return 0;
  307. }
  308. /* mag mismatch, tx */
  309. mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
  310. /* phs mismatch, tx */
  311. phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
  312. mag_tx = (mag_tx / f2);
  313. phs_tx = (phs_tx / f2);
  314. /* mag mismatch, rx */
  315. mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
  316. result_shift;
  317. /* phs mismatch, rx */
  318. phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
  319. result_shift;
  320. solved_eq[0] = mag_tx;
  321. solved_eq[1] = phs_tx;
  322. solved_eq[2] = mag_rx;
  323. solved_eq[3] = phs_rx;
  324. return 1;
  325. }
  326. static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah __unused, s32 in_re, s32 in_im)
  327. {
  328. s32 abs_i = abs(in_re),
  329. abs_q = abs(in_im),
  330. max_abs, min_abs;
  331. if (abs_i > abs_q) {
  332. max_abs = abs_i;
  333. min_abs = abs_q;
  334. } else {
  335. max_abs = abs_q;
  336. min_abs = abs_i;
  337. }
  338. return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
  339. }
  340. #define DELPT 32
  341. static int ar9003_hw_calc_iq_corr(struct ath_hw *ah,
  342. s32 chain_idx,
  343. const s32 iq_res[],
  344. s32 iqc_coeff[])
  345. {
  346. s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
  347. i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
  348. i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
  349. i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
  350. s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
  351. phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
  352. sin_2phi_1, cos_2phi_1,
  353. sin_2phi_2, cos_2phi_2;
  354. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  355. s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
  356. q_q_coff, q_i_coff;
  357. const s32 res_scale = 1 << 15;
  358. const s32 delpt_shift = 1 << 8;
  359. s32 mag1, mag2;
  360. i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
  361. i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
  362. iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
  363. if (i2_m_q2_a0_d0 > 0x800)
  364. i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
  365. if (i2_p_q2_a0_d0 > 0x800)
  366. i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
  367. if (iq_corr_a0_d0 > 0x800)
  368. iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
  369. i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
  370. i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
  371. iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
  372. if (i2_m_q2_a0_d1 > 0x800)
  373. i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
  374. if (i2_p_q2_a0_d1 > 0x800)
  375. i2_p_q2_a0_d1 = -((0xfff - i2_p_q2_a0_d1) + 1);
  376. if (iq_corr_a0_d1 > 0x800)
  377. iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
  378. i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
  379. i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
  380. iq_corr_a1_d0 = iq_res[4] & 0xfff;
  381. if (i2_m_q2_a1_d0 > 0x800)
  382. i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
  383. if (i2_p_q2_a1_d0 > 0x800)
  384. i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
  385. if (iq_corr_a1_d0 > 0x800)
  386. iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
  387. i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
  388. i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
  389. iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
  390. if (i2_m_q2_a1_d1 > 0x800)
  391. i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
  392. if (i2_p_q2_a1_d1 > 0x800)
  393. i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
  394. if (iq_corr_a1_d1 > 0x800)
  395. iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
  396. if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
  397. (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
  398. DBG("ath9k: "
  399. "Divide by 0:\n"
  400. "a0_d0=%d\n"
  401. "a0_d1=%d\n"
  402. "a2_d0=%d\n"
  403. "a1_d1=%d\n",
  404. i2_p_q2_a0_d0, i2_p_q2_a0_d1,
  405. i2_p_q2_a1_d0, i2_p_q2_a1_d1);
  406. return 0;
  407. }
  408. mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  409. phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  410. mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  411. phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  412. mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  413. phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  414. mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  415. phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  416. /* w/o analog phase shift */
  417. sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
  418. /* w/o analog phase shift */
  419. cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
  420. /* w/ analog phase shift */
  421. sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
  422. /* w/ analog phase shift */
  423. cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
  424. /*
  425. * force sin^2 + cos^2 = 1;
  426. * find magnitude by approximation
  427. */
  428. mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
  429. mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
  430. if ((mag1 == 0) || (mag2 == 0)) {
  431. DBG("ath9k: "
  432. "Divide by 0: mag1=%d, mag2=%d\n",
  433. mag1, mag2);
  434. return 0;
  435. }
  436. /* normalization sin and cos by mag */
  437. sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
  438. cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
  439. sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
  440. cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
  441. /* calculate IQ mismatch */
  442. if (!ar9003_hw_solve_iq_cal(ah,
  443. sin_2phi_1, cos_2phi_1,
  444. sin_2phi_2, cos_2phi_2,
  445. mag_a0_d0, phs_a0_d0,
  446. mag_a1_d0,
  447. phs_a1_d0, solved_eq)) {
  448. DBG("ath9k: "
  449. "Call to ar9003_hw_solve_iq_cal() failed.\n");
  450. return 0;
  451. }
  452. mag_tx = solved_eq[0];
  453. phs_tx = solved_eq[1];
  454. mag_rx = solved_eq[2];
  455. phs_rx = solved_eq[3];
  456. DBG2("ath9k: "
  457. "chain %d: mag mismatch=%d phase mismatch=%d\n",
  458. chain_idx, mag_tx/res_scale, phs_tx/res_scale);
  459. if (res_scale == mag_tx) {
  460. DBG("ath9k: "
  461. "Divide by 0: mag_tx=%d, res_scale=%d\n",
  462. mag_tx, res_scale);
  463. return 0;
  464. }
  465. /* calculate and quantize Tx IQ correction factor */
  466. mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
  467. phs_corr_tx = -phs_tx;
  468. q_q_coff = (mag_corr_tx * 128 / res_scale);
  469. q_i_coff = (phs_corr_tx * 256 / res_scale);
  470. DBG2("ath9k: "
  471. "tx chain %d: mag corr=%d phase corr=%d\n",
  472. chain_idx, q_q_coff, q_i_coff);
  473. if (q_i_coff < -63)
  474. q_i_coff = -63;
  475. if (q_i_coff > 63)
  476. q_i_coff = 63;
  477. if (q_q_coff < -63)
  478. q_q_coff = -63;
  479. if (q_q_coff > 63)
  480. q_q_coff = 63;
  481. iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
  482. DBG2("ath9k: "
  483. "tx chain %d: iq corr coeff=%x\n",
  484. chain_idx, iqc_coeff[0]);
  485. if (-mag_rx == res_scale) {
  486. DBG("ath9k: "
  487. "Divide by 0: mag_rx=%d, res_scale=%d\n",
  488. mag_rx, res_scale);
  489. return 0;
  490. }
  491. /* calculate and quantize Rx IQ correction factors */
  492. mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
  493. phs_corr_rx = -phs_rx;
  494. q_q_coff = (mag_corr_rx * 128 / res_scale);
  495. q_i_coff = (phs_corr_rx * 256 / res_scale);
  496. DBG("ath9k: "
  497. "rx chain %d: mag corr=%d phase corr=%d\n",
  498. chain_idx, q_q_coff, q_i_coff);
  499. if (q_i_coff < -63)
  500. q_i_coff = -63;
  501. if (q_i_coff > 63)
  502. q_i_coff = 63;
  503. if (q_q_coff < -63)
  504. q_q_coff = -63;
  505. if (q_q_coff > 63)
  506. q_q_coff = 63;
  507. iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
  508. DBG2("ath9k: "
  509. "rx chain %d: iq corr coeff=%x\n",
  510. chain_idx, iqc_coeff[1]);
  511. return 1;
  512. }
  513. static void ar9003_hw_detect_outlier(int *mp_coeff, int nmeasurement,
  514. int max_delta)
  515. {
  516. int mp_max = -64, max_idx = 0;
  517. int mp_min = 63, min_idx = 0;
  518. int mp_avg = 0, i, outlier_idx = 0;
  519. /* find min/max mismatch across all calibrated gains */
  520. for (i = 0; i < nmeasurement; i++) {
  521. mp_avg += mp_coeff[i];
  522. if (mp_coeff[i] > mp_max) {
  523. mp_max = mp_coeff[i];
  524. max_idx = i;
  525. } else if (mp_coeff[i] < mp_min) {
  526. mp_min = mp_coeff[i];
  527. min_idx = i;
  528. }
  529. }
  530. /* find average (exclude max abs value) */
  531. for (i = 0; i < nmeasurement; i++) {
  532. if ((abs(mp_coeff[i]) < abs(mp_max)) ||
  533. (abs(mp_coeff[i]) < abs(mp_min)))
  534. mp_avg += mp_coeff[i];
  535. }
  536. mp_avg /= (nmeasurement - 1);
  537. /* detect outlier */
  538. if (abs(mp_max - mp_min) > max_delta) {
  539. if (abs(mp_max - mp_avg) > abs(mp_min - mp_avg))
  540. outlier_idx = max_idx;
  541. else
  542. outlier_idx = min_idx;
  543. }
  544. mp_coeff[outlier_idx] = mp_avg;
  545. }
  546. static void ar9003_hw_tx_iqcal_load_avg_2_passes(struct ath_hw *ah,
  547. u8 num_chains,
  548. struct coeff *coeff)
  549. {
  550. int i, im, nmeasurement;
  551. u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
  552. memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
  553. for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
  554. tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
  555. AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
  556. if (!AR_SREV_9485(ah)) {
  557. tx_corr_coeff[i * 2][1] =
  558. tx_corr_coeff[(i * 2) + 1][1] =
  559. AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
  560. tx_corr_coeff[i * 2][2] =
  561. tx_corr_coeff[(i * 2) + 1][2] =
  562. AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
  563. }
  564. }
  565. /* Load the average of 2 passes */
  566. for (i = 0; i < num_chains; i++) {
  567. nmeasurement = REG_READ_FIELD(ah,
  568. AR_PHY_TX_IQCAL_STATUS_B0,
  569. AR_PHY_CALIBRATED_GAINS_0);
  570. if (nmeasurement > MAX_MEASUREMENT)
  571. nmeasurement = MAX_MEASUREMENT;
  572. /* detect outlier only if nmeasurement > 1 */
  573. if (nmeasurement > 1) {
  574. /* Detect magnitude outlier */
  575. ar9003_hw_detect_outlier(coeff->mag_coeff[i],
  576. nmeasurement, MAX_MAG_DELTA);
  577. /* Detect phase outlier */
  578. ar9003_hw_detect_outlier(coeff->phs_coeff[i],
  579. nmeasurement, MAX_PHS_DELTA);
  580. }
  581. for (im = 0; im < nmeasurement; im++) {
  582. coeff->iqc_coeff[0] = (coeff->mag_coeff[i][im] & 0x7f) |
  583. ((coeff->phs_coeff[i][im] & 0x7f) << 7);
  584. if ((im % 2) == 0)
  585. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  586. AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
  587. coeff->iqc_coeff[0]);
  588. else
  589. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  590. AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
  591. coeff->iqc_coeff[0]);
  592. }
  593. }
  594. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
  595. AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
  596. REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
  597. AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
  598. return;
  599. }
  600. static int ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
  601. {
  602. u8 tx_gain_forced;
  603. tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  604. AR_PHY_TXGAIN_FORCE);
  605. if (tx_gain_forced)
  606. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  607. AR_PHY_TXGAIN_FORCE, 0);
  608. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
  609. AR_PHY_TX_IQCAL_START_DO_CAL, 1);
  610. if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
  611. AR_PHY_TX_IQCAL_START_DO_CAL, 0,
  612. AH_WAIT_TIMEOUT)) {
  613. DBG2("ath9k: "
  614. "Tx IQ Cal is not completed.\n");
  615. return 0;
  616. }
  617. return 1;
  618. }
  619. static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah)
  620. {
  621. const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
  622. AR_PHY_TX_IQCAL_STATUS_B0,
  623. AR_PHY_TX_IQCAL_STATUS_B1,
  624. AR_PHY_TX_IQCAL_STATUS_B2,
  625. };
  626. const uint32_t chan_info_tab[] = {
  627. AR_PHY_CHAN_INFO_TAB_0,
  628. AR_PHY_CHAN_INFO_TAB_1,
  629. AR_PHY_CHAN_INFO_TAB_2,
  630. };
  631. struct coeff coeff;
  632. s32 iq_res[6];
  633. u8 num_chains = 0;
  634. int i, im, j;
  635. int nmeasurement;
  636. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  637. if (ah->txchainmask & (1 << i))
  638. num_chains++;
  639. }
  640. for (i = 0; i < num_chains; i++) {
  641. nmeasurement = REG_READ_FIELD(ah,
  642. AR_PHY_TX_IQCAL_STATUS_B0,
  643. AR_PHY_CALIBRATED_GAINS_0);
  644. if (nmeasurement > MAX_MEASUREMENT)
  645. nmeasurement = MAX_MEASUREMENT;
  646. for (im = 0; im < nmeasurement; im++) {
  647. DBG2("ath9k: "
  648. "Doing Tx IQ Cal for chain %d.\n", i);
  649. if (REG_READ(ah, txiqcal_status[i]) &
  650. AR_PHY_TX_IQCAL_STATUS_FAILED) {
  651. DBG("ath9k: "
  652. "Tx IQ Cal failed for chain %d.\n", i);
  653. goto tx_iqcal_fail;
  654. }
  655. for (j = 0; j < 3; j++) {
  656. u32 idx = 2 * j, offset = 4 * (3 * im + j);
  657. REG_RMW_FIELD(ah,
  658. AR_PHY_CHAN_INFO_MEMORY,
  659. AR_PHY_CHAN_INFO_TAB_S2_READ,
  660. 0);
  661. /* 32 bits */
  662. iq_res[idx] = REG_READ(ah,
  663. chan_info_tab[i] +
  664. offset);
  665. REG_RMW_FIELD(ah,
  666. AR_PHY_CHAN_INFO_MEMORY,
  667. AR_PHY_CHAN_INFO_TAB_S2_READ,
  668. 1);
  669. /* 16 bits */
  670. iq_res[idx + 1] = 0xffff & REG_READ(ah,
  671. chan_info_tab[i] + offset);
  672. DBG2("ath9k: "
  673. "IQ RES[%d]=0x%x"
  674. "IQ_RES[%d]=0x%x\n",
  675. idx, iq_res[idx], idx + 1,
  676. iq_res[idx + 1]);
  677. }
  678. if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
  679. coeff.iqc_coeff)) {
  680. DBG("ath9k: "
  681. "Failed in calculation of \
  682. IQ correction.\n");
  683. goto tx_iqcal_fail;
  684. }
  685. coeff.mag_coeff[i][im] = coeff.iqc_coeff[0] & 0x7f;
  686. coeff.phs_coeff[i][im] =
  687. (coeff.iqc_coeff[0] >> 7) & 0x7f;
  688. if (coeff.mag_coeff[i][im] > 63)
  689. coeff.mag_coeff[i][im] -= 128;
  690. if (coeff.phs_coeff[i][im] > 63)
  691. coeff.phs_coeff[i][im] -= 128;
  692. }
  693. }
  694. ar9003_hw_tx_iqcal_load_avg_2_passes(ah, num_chains, &coeff);
  695. return;
  696. tx_iqcal_fail:
  697. DBG("ath9k: Tx IQ Cal failed\n");
  698. return;
  699. }
  700. static int ar9003_hw_init_cal(struct ath_hw *ah,
  701. struct ath9k_channel *chan __unused)
  702. {
  703. struct ath9k_hw_capabilities *pCap = &ah->caps;
  704. int val;
  705. int txiqcal_done = 0;
  706. val = REG_READ(ah, AR_ENT_OTP);
  707. DBG2("ath9k: ath9k: AR_ENT_OTP 0x%x\n", val);
  708. /* Configure rx/tx chains before running AGC/TxiQ cals */
  709. if (val & AR_ENT_OTP_CHAIN2_DISABLE)
  710. ar9003_hw_set_chain_masks(ah, 0x3, 0x3);
  711. else
  712. ar9003_hw_set_chain_masks(ah, pCap->rx_chainmask,
  713. pCap->tx_chainmask);
  714. /* Do Tx IQ Calibration */
  715. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
  716. AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
  717. DELPT);
  718. /*
  719. * For AR9485 or later chips, TxIQ cal runs as part of
  720. * AGC calibration
  721. */
  722. if (AR_SREV_9485_OR_LATER(ah))
  723. txiqcal_done = 1;
  724. else {
  725. txiqcal_done = ar9003_hw_tx_iq_cal_run(ah);
  726. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  727. udelay(5);
  728. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  729. }
  730. /* Calibrate the AGC */
  731. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  732. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  733. AR_PHY_AGC_CONTROL_CAL);
  734. /* Poll for offset calibration complete */
  735. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
  736. 0, AH_WAIT_TIMEOUT)) {
  737. DBG("ath9k: "
  738. "offset calibration failed to complete in 1ms; noisy environment?\n");
  739. return 0;
  740. }
  741. if (txiqcal_done)
  742. ar9003_hw_tx_iq_cal_post_proc(ah);
  743. /* Revert chainmasks to their original values before NF cal */
  744. ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
  745. ath9k_hw_start_nfcal(ah, 1);
  746. /* Initialize list pointers */
  747. ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
  748. ah->supp_cals = IQ_MISMATCH_CAL;
  749. if (ah->supp_cals & IQ_MISMATCH_CAL) {
  750. INIT_CAL(&ah->iq_caldata);
  751. INSERT_CAL(ah, &ah->iq_caldata);
  752. DBG2("ath9k: "
  753. "enabling IQ Calibration.\n");
  754. }
  755. if (ah->supp_cals & TEMP_COMP_CAL) {
  756. INIT_CAL(&ah->tempCompCalData);
  757. INSERT_CAL(ah, &ah->tempCompCalData);
  758. DBG2("ath9k: "
  759. "enabling Temperature Compensation Calibration.\n");
  760. }
  761. /* Initialize current pointer to first element in list */
  762. ah->cal_list_curr = ah->cal_list;
  763. if (ah->cal_list_curr)
  764. ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
  765. if (ah->caldata)
  766. ah->caldata->CalValid = 0;
  767. return 1;
  768. }
  769. void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
  770. {
  771. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  772. struct ath_hw_ops *ops = ath9k_hw_ops(ah);
  773. priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
  774. priv_ops->init_cal = ar9003_hw_init_cal;
  775. priv_ops->setup_calibration = ar9003_hw_setup_calibration;
  776. ops->calibrate = ar9003_hw_calibrate;
  777. }