ar9003_calib.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * Copyright (c) 2010-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "hw.h"
  17. #include "hw-ops.h"
  18. #include "ar9003_phy.h"
  19. #define MAX_MEASUREMENT 8
  20. #define MAX_MAG_DELTA 11
  21. #define MAX_PHS_DELTA 10
  22. struct coeff {
  23. int mag_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
  24. int phs_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
  25. int iqc_coeff[2];
  26. };
  27. enum ar9003_cal_types {
  28. IQ_MISMATCH_CAL = BIT(0),
  29. TEMP_COMP_CAL = BIT(1),
  30. };
  31. static void ar9003_hw_setup_calibration(struct ath_hw *ah,
  32. struct ath9k_cal_list *currCal)
  33. {
  34. struct ath_common *common = ath9k_hw_common(ah);
  35. /* Select calibration to run */
  36. switch (currCal->calData->calType) {
  37. case IQ_MISMATCH_CAL:
  38. /*
  39. * Start calibration with
  40. * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
  41. */
  42. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  43. AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX,
  44. currCal->calData->calCountMax);
  45. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  46. ath_dbg(common, ATH_DBG_CALIBRATE,
  47. "starting IQ Mismatch Calibration\n");
  48. /* Kick-off cal */
  49. REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
  50. break;
  51. case TEMP_COMP_CAL:
  52. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  53. AR_PHY_65NM_CH0_THERM_LOCAL, 1);
  54. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  55. AR_PHY_65NM_CH0_THERM_START, 1);
  56. ath_dbg(common, ATH_DBG_CALIBRATE,
  57. "starting Temperature Compensation Calibration\n");
  58. break;
  59. }
  60. }
  61. /*
  62. * Generic calibration routine.
  63. * Recalibrate the lower PHY chips to account for temperature/environment
  64. * changes.
  65. */
  66. static bool ar9003_hw_per_calibration(struct ath_hw *ah,
  67. struct ath9k_channel *ichan,
  68. u8 rxchainmask,
  69. struct ath9k_cal_list *currCal)
  70. {
  71. struct ath9k_hw_cal_data *caldata = ah->caldata;
  72. /* Cal is assumed not done until explicitly set below */
  73. bool iscaldone = false;
  74. /* Calibration in progress. */
  75. if (currCal->calState == CAL_RUNNING) {
  76. /* Check to see if it has finished. */
  77. if (!(REG_READ(ah, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
  78. /*
  79. * Accumulate cal measures for active chains
  80. */
  81. currCal->calData->calCollect(ah);
  82. ah->cal_samples++;
  83. if (ah->cal_samples >=
  84. currCal->calData->calNumSamples) {
  85. unsigned int i, numChains = 0;
  86. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  87. if (rxchainmask & (1 << i))
  88. numChains++;
  89. }
  90. /*
  91. * Process accumulated data
  92. */
  93. currCal->calData->calPostProc(ah, numChains);
  94. /* Calibration has finished. */
  95. caldata->CalValid |= currCal->calData->calType;
  96. currCal->calState = CAL_DONE;
  97. iscaldone = true;
  98. } else {
  99. /*
  100. * Set-up collection of another sub-sample until we
  101. * get desired number
  102. */
  103. ar9003_hw_setup_calibration(ah, currCal);
  104. }
  105. }
  106. } else if (!(caldata->CalValid & currCal->calData->calType)) {
  107. /* If current cal is marked invalid in channel, kick it off */
  108. ath9k_hw_reset_calibration(ah, currCal);
  109. }
  110. return iscaldone;
  111. }
  112. static bool ar9003_hw_calibrate(struct ath_hw *ah,
  113. struct ath9k_channel *chan,
  114. u8 rxchainmask,
  115. bool longcal)
  116. {
  117. bool iscaldone = true;
  118. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  119. /*
  120. * For given calibration:
  121. * 1. Call generic cal routine
  122. * 2. When this cal is done (isCalDone) if we have more cals waiting
  123. * (eg after reset), mask this to upper layers by not propagating
  124. * isCalDone if it is set to TRUE.
  125. * Instead, change isCalDone to FALSE and setup the waiting cal(s)
  126. * to be run.
  127. */
  128. if (currCal &&
  129. (currCal->calState == CAL_RUNNING ||
  130. currCal->calState == CAL_WAITING)) {
  131. iscaldone = ar9003_hw_per_calibration(ah, chan,
  132. rxchainmask, currCal);
  133. if (iscaldone) {
  134. ah->cal_list_curr = currCal = currCal->calNext;
  135. if (currCal->calState == CAL_WAITING) {
  136. iscaldone = false;
  137. ath9k_hw_reset_calibration(ah, currCal);
  138. }
  139. }
  140. }
  141. /* Do NF cal only at longer intervals */
  142. if (longcal) {
  143. /*
  144. * Get the value from the previous NF cal and update
  145. * history buffer.
  146. */
  147. ath9k_hw_getnf(ah, chan);
  148. /*
  149. * Load the NF from history buffer of the current channel.
  150. * NF is slow time-variant, so it is OK to use a historical
  151. * value.
  152. */
  153. ath9k_hw_loadnf(ah, ah->curchan);
  154. /* start NF calibration, without updating BB NF register */
  155. ath9k_hw_start_nfcal(ah, false);
  156. }
  157. return iscaldone;
  158. }
  159. static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
  160. {
  161. int i;
  162. /* Accumulate IQ cal measures for active chains */
  163. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  164. if (ah->txchainmask & BIT(i)) {
  165. ah->totalPowerMeasI[i] +=
  166. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  167. ah->totalPowerMeasQ[i] +=
  168. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  169. ah->totalIqCorrMeas[i] +=
  170. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  171. ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  172. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  173. ah->cal_samples, i, ah->totalPowerMeasI[i],
  174. ah->totalPowerMeasQ[i],
  175. ah->totalIqCorrMeas[i]);
  176. }
  177. }
  178. }
  179. static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
  180. {
  181. struct ath_common *common = ath9k_hw_common(ah);
  182. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  183. u32 qCoffDenom, iCoffDenom;
  184. int32_t qCoff, iCoff;
  185. int iqCorrNeg, i;
  186. static const u_int32_t offset_array[3] = {
  187. AR_PHY_RX_IQCAL_CORR_B0,
  188. AR_PHY_RX_IQCAL_CORR_B1,
  189. AR_PHY_RX_IQCAL_CORR_B2,
  190. };
  191. for (i = 0; i < numChains; i++) {
  192. powerMeasI = ah->totalPowerMeasI[i];
  193. powerMeasQ = ah->totalPowerMeasQ[i];
  194. iqCorrMeas = ah->totalIqCorrMeas[i];
  195. ath_dbg(common, ATH_DBG_CALIBRATE,
  196. "Starting IQ Cal and Correction for Chain %d\n",
  197. i);
  198. ath_dbg(common, ATH_DBG_CALIBRATE,
  199. "Orignal: Chn %diq_corr_meas = 0x%08x\n",
  200. i, ah->totalIqCorrMeas[i]);
  201. iqCorrNeg = 0;
  202. if (iqCorrMeas > 0x80000000) {
  203. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  204. iqCorrNeg = 1;
  205. }
  206. ath_dbg(common, ATH_DBG_CALIBRATE,
  207. "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
  208. ath_dbg(common, ATH_DBG_CALIBRATE,
  209. "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
  210. ath_dbg(common, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
  211. iqCorrNeg);
  212. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
  213. qCoffDenom = powerMeasQ / 64;
  214. if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
  215. iCoff = iqCorrMeas / iCoffDenom;
  216. qCoff = powerMeasI / qCoffDenom - 64;
  217. ath_dbg(common, ATH_DBG_CALIBRATE,
  218. "Chn %d iCoff = 0x%08x\n", i, iCoff);
  219. ath_dbg(common, ATH_DBG_CALIBRATE,
  220. "Chn %d qCoff = 0x%08x\n", i, qCoff);
  221. /* Force bounds on iCoff */
  222. if (iCoff >= 63)
  223. iCoff = 63;
  224. else if (iCoff <= -63)
  225. iCoff = -63;
  226. /* Negate iCoff if iqCorrNeg == 0 */
  227. if (iqCorrNeg == 0x0)
  228. iCoff = -iCoff;
  229. /* Force bounds on qCoff */
  230. if (qCoff >= 63)
  231. qCoff = 63;
  232. else if (qCoff <= -63)
  233. qCoff = -63;
  234. iCoff = iCoff & 0x7f;
  235. qCoff = qCoff & 0x7f;
  236. ath_dbg(common, ATH_DBG_CALIBRATE,
  237. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  238. i, iCoff, qCoff);
  239. ath_dbg(common, ATH_DBG_CALIBRATE,
  240. "Register offset (0x%04x) before update = 0x%x\n",
  241. offset_array[i],
  242. REG_READ(ah, offset_array[i]));
  243. REG_RMW_FIELD(ah, offset_array[i],
  244. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  245. iCoff);
  246. REG_RMW_FIELD(ah, offset_array[i],
  247. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  248. qCoff);
  249. ath_dbg(common, ATH_DBG_CALIBRATE,
  250. "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
  251. offset_array[i],
  252. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  253. REG_READ(ah, offset_array[i]));
  254. ath_dbg(common, ATH_DBG_CALIBRATE,
  255. "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
  256. offset_array[i],
  257. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  258. REG_READ(ah, offset_array[i]));
  259. ath_dbg(common, ATH_DBG_CALIBRATE,
  260. "IQ Cal and Correction done for Chain %d\n", i);
  261. }
  262. }
  263. REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
  264. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
  265. ath_dbg(common, ATH_DBG_CALIBRATE,
  266. "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
  267. (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
  268. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
  269. REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
  270. }
  271. static const struct ath9k_percal_data iq_cal_single_sample = {
  272. IQ_MISMATCH_CAL,
  273. MIN_CAL_SAMPLES,
  274. PER_MAX_LOG_COUNT,
  275. ar9003_hw_iqcal_collect,
  276. ar9003_hw_iqcalibrate
  277. };
  278. static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
  279. {
  280. ah->iq_caldata.calData = &iq_cal_single_sample;
  281. }
  282. /*
  283. * solve 4x4 linear equation used in loopback iq cal.
  284. */
  285. static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
  286. s32 sin_2phi_1,
  287. s32 cos_2phi_1,
  288. s32 sin_2phi_2,
  289. s32 cos_2phi_2,
  290. s32 mag_a0_d0,
  291. s32 phs_a0_d0,
  292. s32 mag_a1_d0,
  293. s32 phs_a1_d0,
  294. s32 solved_eq[])
  295. {
  296. s32 f1 = cos_2phi_1 - cos_2phi_2,
  297. f3 = sin_2phi_1 - sin_2phi_2,
  298. f2;
  299. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  300. const s32 result_shift = 1 << 15;
  301. struct ath_common *common = ath9k_hw_common(ah);
  302. f2 = (f1 * f1 + f3 * f3) / result_shift;
  303. if (!f2) {
  304. ath_dbg(common, ATH_DBG_CALIBRATE, "Divide by 0\n");
  305. return false;
  306. }
  307. /* mag mismatch, tx */
  308. mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
  309. /* phs mismatch, tx */
  310. phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
  311. mag_tx = (mag_tx / f2);
  312. phs_tx = (phs_tx / f2);
  313. /* mag mismatch, rx */
  314. mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
  315. result_shift;
  316. /* phs mismatch, rx */
  317. phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
  318. result_shift;
  319. solved_eq[0] = mag_tx;
  320. solved_eq[1] = phs_tx;
  321. solved_eq[2] = mag_rx;
  322. solved_eq[3] = phs_rx;
  323. return true;
  324. }
  325. static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
  326. {
  327. s32 abs_i = abs(in_re),
  328. abs_q = abs(in_im),
  329. max_abs, min_abs;
  330. if (abs_i > abs_q) {
  331. max_abs = abs_i;
  332. min_abs = abs_q;
  333. } else {
  334. max_abs = abs_q;
  335. min_abs = abs_i;
  336. }
  337. return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
  338. }
  339. #define DELPT 32
  340. static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
  341. s32 chain_idx,
  342. const s32 iq_res[],
  343. s32 iqc_coeff[])
  344. {
  345. s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
  346. i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
  347. i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
  348. i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
  349. s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
  350. phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
  351. sin_2phi_1, cos_2phi_1,
  352. sin_2phi_2, cos_2phi_2;
  353. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  354. s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
  355. q_q_coff, q_i_coff;
  356. const s32 res_scale = 1 << 15;
  357. const s32 delpt_shift = 1 << 8;
  358. s32 mag1, mag2;
  359. struct ath_common *common = ath9k_hw_common(ah);
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  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 false;
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  432. "Divide by 0: mag1=%d, mag2=%d\n",
  433. mag1, mag2);
  434. return false;
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  449. "Call to ar9003_hw_solve_iq_cal() failed.\n");
  450. return false;
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  461. "Divide by 0: mag_tx=%d, res_scale=%d\n",
  462. mag_tx, res_scale);
  463. return false;
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  483. "tx chain %d: iq corr coeff=%x\n",
  484. chain_idx, iqc_coeff[0]);
  485. if (-mag_rx == res_scale) {
  486. ath_dbg(common, ATH_DBG_CALIBRATE,
  487. "Divide by 0: mag_rx=%d, res_scale=%d\n",
  488. mag_rx, res_scale);
  489. return false;
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  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. ath_dbg(common, ATH_DBG_CALIBRATE,
  509. "rx chain %d: iq corr coeff=%x\n",
  510. chain_idx, iqc_coeff[1]);
  511. return true;
  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 bool ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
  601. {
  602. struct ath_common *common = ath9k_hw_common(ah);
  603. u8 tx_gain_forced;
  604. tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  605. AR_PHY_TXGAIN_FORCE);
  606. if (tx_gain_forced)
  607. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  608. AR_PHY_TXGAIN_FORCE, 0);
  609. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
  610. AR_PHY_TX_IQCAL_START_DO_CAL, 1);
  611. if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
  612. AR_PHY_TX_IQCAL_START_DO_CAL, 0,
  613. AH_WAIT_TIMEOUT)) {
  614. ath_dbg(common, ATH_DBG_CALIBRATE,
  615. "Tx IQ Cal is not completed.\n");
  616. return false;
  617. }
  618. return true;
  619. }
  620. static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah)
  621. {
  622. struct ath_common *common = ath9k_hw_common(ah);
  623. const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
  624. AR_PHY_TX_IQCAL_STATUS_B0,
  625. AR_PHY_TX_IQCAL_STATUS_B1,
  626. AR_PHY_TX_IQCAL_STATUS_B2,
  627. };
  628. const u_int32_t chan_info_tab[] = {
  629. AR_PHY_CHAN_INFO_TAB_0,
  630. AR_PHY_CHAN_INFO_TAB_1,
  631. AR_PHY_CHAN_INFO_TAB_2,
  632. };
  633. struct coeff coeff;
  634. s32 iq_res[6];
  635. u8 num_chains = 0;
  636. int i, im, j;
  637. int nmeasurement;
  638. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  639. if (ah->txchainmask & (1 << i))
  640. num_chains++;
  641. }
  642. for (i = 0; i < num_chains; i++) {
  643. nmeasurement = REG_READ_FIELD(ah,
  644. AR_PHY_TX_IQCAL_STATUS_B0,
  645. AR_PHY_CALIBRATED_GAINS_0);
  646. if (nmeasurement > MAX_MEASUREMENT)
  647. nmeasurement = MAX_MEASUREMENT;
  648. for (im = 0; im < nmeasurement; im++) {
  649. ath_dbg(common, ATH_DBG_CALIBRATE,
  650. "Doing Tx IQ Cal for chain %d.\n", i);
  651. if (REG_READ(ah, txiqcal_status[i]) &
  652. AR_PHY_TX_IQCAL_STATUS_FAILED) {
  653. ath_dbg(common, ATH_DBG_CALIBRATE,
  654. "Tx IQ Cal failed for chain %d.\n", i);
  655. goto tx_iqcal_fail;
  656. }
  657. for (j = 0; j < 3; j++) {
  658. u32 idx = 2 * j, offset = 4 * (3 * im + j);
  659. REG_RMW_FIELD(ah,
  660. AR_PHY_CHAN_INFO_MEMORY,
  661. AR_PHY_CHAN_INFO_TAB_S2_READ,
  662. 0);
  663. /* 32 bits */
  664. iq_res[idx] = REG_READ(ah,
  665. chan_info_tab[i] +
  666. offset);
  667. REG_RMW_FIELD(ah,
  668. AR_PHY_CHAN_INFO_MEMORY,
  669. AR_PHY_CHAN_INFO_TAB_S2_READ,
  670. 1);
  671. /* 16 bits */
  672. iq_res[idx + 1] = 0xffff & REG_READ(ah,
  673. chan_info_tab[i] + offset);
  674. ath_dbg(common, ATH_DBG_CALIBRATE,
  675. "IQ RES[%d]=0x%x"
  676. "IQ_RES[%d]=0x%x\n",
  677. idx, iq_res[idx], idx + 1,
  678. iq_res[idx + 1]);
  679. }
  680. if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
  681. coeff.iqc_coeff)) {
  682. ath_dbg(common, ATH_DBG_CALIBRATE,
  683. "Failed in calculation of \
  684. IQ correction.\n");
  685. goto tx_iqcal_fail;
  686. }
  687. coeff.mag_coeff[i][im] = coeff.iqc_coeff[0] & 0x7f;
  688. coeff.phs_coeff[i][im] =
  689. (coeff.iqc_coeff[0] >> 7) & 0x7f;
  690. if (coeff.mag_coeff[i][im] > 63)
  691. coeff.mag_coeff[i][im] -= 128;
  692. if (coeff.phs_coeff[i][im] > 63)
  693. coeff.phs_coeff[i][im] -= 128;
  694. }
  695. }
  696. ar9003_hw_tx_iqcal_load_avg_2_passes(ah, num_chains, &coeff);
  697. return;
  698. tx_iqcal_fail:
  699. ath_dbg(common, ATH_DBG_CALIBRATE, "Tx IQ Cal failed\n");
  700. return;
  701. }
  702. static bool ar9003_hw_init_cal(struct ath_hw *ah,
  703. struct ath9k_channel *chan)
  704. {
  705. struct ath_common *common = ath9k_hw_common(ah);
  706. struct ath9k_hw_capabilities *pCap = &ah->caps;
  707. int val;
  708. bool txiqcal_done = false;
  709. val = REG_READ(ah, AR_ENT_OTP);
  710. ath_dbg(common, ATH_DBG_CALIBRATE, "ath9k: AR_ENT_OTP 0x%x\n", val);
  711. /* Configure rx/tx chains before running AGC/TxiQ cals */
  712. if (val & AR_ENT_OTP_CHAIN2_DISABLE)
  713. ar9003_hw_set_chain_masks(ah, 0x3, 0x3);
  714. else
  715. ar9003_hw_set_chain_masks(ah, pCap->rx_chainmask,
  716. pCap->tx_chainmask);
  717. /* Do Tx IQ Calibration */
  718. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
  719. AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
  720. DELPT);
  721. /*
  722. * For AR9485 or later chips, TxIQ cal runs as part of
  723. * AGC calibration
  724. */
  725. if (AR_SREV_9485_OR_LATER(ah))
  726. txiqcal_done = true;
  727. else {
  728. txiqcal_done = ar9003_hw_tx_iq_cal_run(ah);
  729. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  730. udelay(5);
  731. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  732. }
  733. /* Calibrate the AGC */
  734. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  735. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  736. AR_PHY_AGC_CONTROL_CAL);
  737. /* Poll for offset calibration complete */
  738. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
  739. 0, AH_WAIT_TIMEOUT)) {
  740. ath_dbg(common, ATH_DBG_CALIBRATE,
  741. "offset calibration failed to complete in 1ms; noisy environment?\n");
  742. return false;
  743. }
  744. if (txiqcal_done)
  745. ar9003_hw_tx_iq_cal_post_proc(ah);
  746. /* Revert chainmasks to their original values before NF cal */
  747. ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
  748. ath9k_hw_start_nfcal(ah, true);
  749. /* Initialize list pointers */
  750. ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
  751. ah->supp_cals = IQ_MISMATCH_CAL;
  752. if (ah->supp_cals & IQ_MISMATCH_CAL) {
  753. INIT_CAL(&ah->iq_caldata);
  754. INSERT_CAL(ah, &ah->iq_caldata);
  755. ath_dbg(common, ATH_DBG_CALIBRATE,
  756. "enabling IQ Calibration.\n");
  757. }
  758. if (ah->supp_cals & TEMP_COMP_CAL) {
  759. INIT_CAL(&ah->tempCompCalData);
  760. INSERT_CAL(ah, &ah->tempCompCalData);
  761. ath_dbg(common, ATH_DBG_CALIBRATE,
  762. "enabling Temperature Compensation Calibration.\n");
  763. }
  764. /* Initialize current pointer to first element in list */
  765. ah->cal_list_curr = ah->cal_list;
  766. if (ah->cal_list_curr)
  767. ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
  768. if (ah->caldata)
  769. ah->caldata->CalValid = 0;
  770. return true;
  771. }
  772. void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
  773. {
  774. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  775. struct ath_hw_ops *ops = ath9k_hw_ops(ah);
  776. priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
  777. priv_ops->init_cal = ar9003_hw_init_cal;
  778. priv_ops->setup_calibration = ar9003_hw_setup_calibration;
  779. ops->calibrate = ar9003_hw_calibrate;
  780. }