ar9003_calib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * Copyright (c) 2010 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. static void ar9003_hw_setup_calibration(struct ath_hw *ah,
  20. struct ath9k_cal_list *currCal)
  21. {
  22. struct ath_common *common = ath9k_hw_common(ah);
  23. /* Select calibration to run */
  24. switch (currCal->calData->calType) {
  25. case IQ_MISMATCH_CAL:
  26. /*
  27. * Start calibration with
  28. * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
  29. */
  30. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  31. AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX,
  32. currCal->calData->calCountMax);
  33. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  34. ath_print(common, ATH_DBG_CALIBRATE,
  35. "starting IQ Mismatch Calibration\n");
  36. /* Kick-off cal */
  37. REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
  38. break;
  39. case TEMP_COMP_CAL:
  40. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  41. AR_PHY_65NM_CH0_THERM_LOCAL, 1);
  42. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  43. AR_PHY_65NM_CH0_THERM_START, 1);
  44. ath_print(common, ATH_DBG_CALIBRATE,
  45. "starting Temperature Compensation Calibration\n");
  46. break;
  47. case ADC_GAIN_CAL:
  48. case ADC_DC_CAL:
  49. /* Not yet */
  50. break;
  51. }
  52. }
  53. /*
  54. * Generic calibration routine.
  55. * Recalibrate the lower PHY chips to account for temperature/environment
  56. * changes.
  57. */
  58. static bool ar9003_hw_per_calibration(struct ath_hw *ah,
  59. struct ath9k_channel *ichan,
  60. u8 rxchainmask,
  61. struct ath9k_cal_list *currCal)
  62. {
  63. struct ath9k_hw_cal_data *caldata = ah->caldata;
  64. /* Cal is assumed not done until explicitly set below */
  65. bool iscaldone = false;
  66. /* Calibration in progress. */
  67. if (currCal->calState == CAL_RUNNING) {
  68. /* Check to see if it has finished. */
  69. if (!(REG_READ(ah, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
  70. /*
  71. * Accumulate cal measures for active chains
  72. */
  73. currCal->calData->calCollect(ah);
  74. ah->cal_samples++;
  75. if (ah->cal_samples >=
  76. currCal->calData->calNumSamples) {
  77. unsigned int i, numChains = 0;
  78. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  79. if (rxchainmask & (1 << i))
  80. numChains++;
  81. }
  82. /*
  83. * Process accumulated data
  84. */
  85. currCal->calData->calPostProc(ah, numChains);
  86. /* Calibration has finished. */
  87. caldata->CalValid |= currCal->calData->calType;
  88. currCal->calState = CAL_DONE;
  89. iscaldone = true;
  90. } else {
  91. /*
  92. * Set-up collection of another sub-sample until we
  93. * get desired number
  94. */
  95. ar9003_hw_setup_calibration(ah, currCal);
  96. }
  97. }
  98. } else if (!(caldata->CalValid & currCal->calData->calType)) {
  99. /* If current cal is marked invalid in channel, kick it off */
  100. ath9k_hw_reset_calibration(ah, currCal);
  101. }
  102. return iscaldone;
  103. }
  104. static bool ar9003_hw_calibrate(struct ath_hw *ah,
  105. struct ath9k_channel *chan,
  106. u8 rxchainmask,
  107. bool longcal)
  108. {
  109. bool iscaldone = true;
  110. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  111. /*
  112. * For given calibration:
  113. * 1. Call generic cal routine
  114. * 2. When this cal is done (isCalDone) if we have more cals waiting
  115. * (eg after reset), mask this to upper layers by not propagating
  116. * isCalDone if it is set to TRUE.
  117. * Instead, change isCalDone to FALSE and setup the waiting cal(s)
  118. * to be run.
  119. */
  120. if (currCal &&
  121. (currCal->calState == CAL_RUNNING ||
  122. currCal->calState == CAL_WAITING)) {
  123. iscaldone = ar9003_hw_per_calibration(ah, chan,
  124. rxchainmask, currCal);
  125. if (iscaldone) {
  126. ah->cal_list_curr = currCal = currCal->calNext;
  127. if (currCal->calState == CAL_WAITING) {
  128. iscaldone = false;
  129. ath9k_hw_reset_calibration(ah, currCal);
  130. }
  131. }
  132. }
  133. /* Do NF cal only at longer intervals */
  134. if (longcal) {
  135. /*
  136. * Get the value from the previous NF cal and update
  137. * history buffer.
  138. */
  139. ath9k_hw_getnf(ah, chan);
  140. /*
  141. * Load the NF from history buffer of the current channel.
  142. * NF is slow time-variant, so it is OK to use a historical
  143. * value.
  144. */
  145. ath9k_hw_loadnf(ah, ah->curchan);
  146. /* start NF calibration, without updating BB NF register */
  147. ath9k_hw_start_nfcal(ah, false);
  148. }
  149. return iscaldone;
  150. }
  151. static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
  152. {
  153. int i;
  154. /* Accumulate IQ cal measures for active chains */
  155. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  156. ah->totalPowerMeasI[i] +=
  157. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  158. ah->totalPowerMeasQ[i] +=
  159. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  160. ah->totalIqCorrMeas[i] +=
  161. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  162. ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  163. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  164. ah->cal_samples, i, ah->totalPowerMeasI[i],
  165. ah->totalPowerMeasQ[i],
  166. ah->totalIqCorrMeas[i]);
  167. }
  168. }
  169. static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
  170. {
  171. struct ath_common *common = ath9k_hw_common(ah);
  172. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  173. u32 qCoffDenom, iCoffDenom;
  174. int32_t qCoff, iCoff;
  175. int iqCorrNeg, i;
  176. const u_int32_t offset_array[3] = {
  177. AR_PHY_RX_IQCAL_CORR_B0,
  178. AR_PHY_RX_IQCAL_CORR_B1,
  179. AR_PHY_RX_IQCAL_CORR_B2,
  180. };
  181. for (i = 0; i < numChains; i++) {
  182. powerMeasI = ah->totalPowerMeasI[i];
  183. powerMeasQ = ah->totalPowerMeasQ[i];
  184. iqCorrMeas = ah->totalIqCorrMeas[i];
  185. ath_print(common, ATH_DBG_CALIBRATE,
  186. "Starting IQ Cal and Correction for Chain %d\n",
  187. i);
  188. ath_print(common, ATH_DBG_CALIBRATE,
  189. "Orignal: Chn %diq_corr_meas = 0x%08x\n",
  190. i, ah->totalIqCorrMeas[i]);
  191. iqCorrNeg = 0;
  192. if (iqCorrMeas > 0x80000000) {
  193. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  194. iqCorrNeg = 1;
  195. }
  196. ath_print(common, ATH_DBG_CALIBRATE,
  197. "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
  198. ath_print(common, ATH_DBG_CALIBRATE,
  199. "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
  200. ath_print(common, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
  201. iqCorrNeg);
  202. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
  203. qCoffDenom = powerMeasQ / 64;
  204. if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
  205. iCoff = iqCorrMeas / iCoffDenom;
  206. qCoff = powerMeasI / qCoffDenom - 64;
  207. ath_print(common, ATH_DBG_CALIBRATE,
  208. "Chn %d iCoff = 0x%08x\n", i, iCoff);
  209. ath_print(common, ATH_DBG_CALIBRATE,
  210. "Chn %d qCoff = 0x%08x\n", i, qCoff);
  211. /* Force bounds on iCoff */
  212. if (iCoff >= 63)
  213. iCoff = 63;
  214. else if (iCoff <= -63)
  215. iCoff = -63;
  216. /* Negate iCoff if iqCorrNeg == 0 */
  217. if (iqCorrNeg == 0x0)
  218. iCoff = -iCoff;
  219. /* Force bounds on qCoff */
  220. if (qCoff >= 63)
  221. qCoff = 63;
  222. else if (qCoff <= -63)
  223. qCoff = -63;
  224. iCoff = iCoff & 0x7f;
  225. qCoff = qCoff & 0x7f;
  226. ath_print(common, ATH_DBG_CALIBRATE,
  227. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  228. i, iCoff, qCoff);
  229. ath_print(common, ATH_DBG_CALIBRATE,
  230. "Register offset (0x%04x) "
  231. "before update = 0x%x\n",
  232. offset_array[i],
  233. REG_READ(ah, offset_array[i]));
  234. REG_RMW_FIELD(ah, offset_array[i],
  235. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  236. iCoff);
  237. REG_RMW_FIELD(ah, offset_array[i],
  238. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  239. qCoff);
  240. ath_print(common, ATH_DBG_CALIBRATE,
  241. "Register offset (0x%04x) QI COFF "
  242. "(bitfields 0x%08x) after update = 0x%x\n",
  243. offset_array[i],
  244. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  245. REG_READ(ah, offset_array[i]));
  246. ath_print(common, ATH_DBG_CALIBRATE,
  247. "Register offset (0x%04x) QQ COFF "
  248. "(bitfields 0x%08x) after update = 0x%x\n",
  249. offset_array[i],
  250. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  251. REG_READ(ah, offset_array[i]));
  252. ath_print(common, ATH_DBG_CALIBRATE,
  253. "IQ Cal and Correction done for Chain %d\n",
  254. i);
  255. }
  256. }
  257. REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
  258. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
  259. ath_print(common, ATH_DBG_CALIBRATE,
  260. "IQ Cal and Correction (offset 0x%04x) enabled "
  261. "(bit position 0x%08x). New Value 0x%08x\n",
  262. (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
  263. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
  264. REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
  265. }
  266. static const struct ath9k_percal_data iq_cal_single_sample = {
  267. IQ_MISMATCH_CAL,
  268. MIN_CAL_SAMPLES,
  269. PER_MAX_LOG_COUNT,
  270. ar9003_hw_iqcal_collect,
  271. ar9003_hw_iqcalibrate
  272. };
  273. static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
  274. {
  275. ah->iq_caldata.calData = &iq_cal_single_sample;
  276. ah->supp_cals = IQ_MISMATCH_CAL;
  277. }
  278. static bool ar9003_hw_iscal_supported(struct ath_hw *ah,
  279. enum ath9k_cal_types calType)
  280. {
  281. switch (calType & ah->supp_cals) {
  282. case IQ_MISMATCH_CAL:
  283. /*
  284. * XXX: Run IQ Mismatch for non-CCK only
  285. * Note that CHANNEL_B is never set though.
  286. */
  287. return true;
  288. case ADC_GAIN_CAL:
  289. case ADC_DC_CAL:
  290. return false;
  291. case TEMP_COMP_CAL:
  292. return true;
  293. }
  294. return false;
  295. }
  296. /*
  297. * solve 4x4 linear equation used in loopback iq cal.
  298. */
  299. static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
  300. s32 sin_2phi_1,
  301. s32 cos_2phi_1,
  302. s32 sin_2phi_2,
  303. s32 cos_2phi_2,
  304. s32 mag_a0_d0,
  305. s32 phs_a0_d0,
  306. s32 mag_a1_d0,
  307. s32 phs_a1_d0,
  308. s32 solved_eq[])
  309. {
  310. s32 f1 = cos_2phi_1 - cos_2phi_2,
  311. f3 = sin_2phi_1 - sin_2phi_2,
  312. f2;
  313. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  314. const s32 result_shift = 1 << 15;
  315. struct ath_common *common = ath9k_hw_common(ah);
  316. f2 = (f1 * f1 + f3 * f3) / result_shift;
  317. if (!f2) {
  318. ath_print(common, ATH_DBG_CALIBRATE, "Divide by 0\n");
  319. return false;
  320. }
  321. /* mag mismatch, tx */
  322. mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
  323. /* phs mismatch, tx */
  324. phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
  325. mag_tx = (mag_tx / f2);
  326. phs_tx = (phs_tx / f2);
  327. /* mag mismatch, rx */
  328. mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
  329. result_shift;
  330. /* phs mismatch, rx */
  331. phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
  332. result_shift;
  333. solved_eq[0] = mag_tx;
  334. solved_eq[1] = phs_tx;
  335. solved_eq[2] = mag_rx;
  336. solved_eq[3] = phs_rx;
  337. return true;
  338. }
  339. static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
  340. {
  341. s32 abs_i = abs(in_re),
  342. abs_q = abs(in_im),
  343. max_abs, min_abs;
  344. if (abs_i > abs_q) {
  345. max_abs = abs_i;
  346. min_abs = abs_q;
  347. } else {
  348. max_abs = abs_q;
  349. min_abs = abs_i;
  350. }
  351. return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
  352. }
  353. #define DELPT 32
  354. static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
  355. s32 chain_idx,
  356. const s32 iq_res[],
  357. s32 iqc_coeff[])
  358. {
  359. s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
  360. i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
  361. i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
  362. i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
  363. s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
  364. phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
  365. sin_2phi_1, cos_2phi_1,
  366. sin_2phi_2, cos_2phi_2;
  367. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  368. s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
  369. q_q_coff, q_i_coff;
  370. const s32 res_scale = 1 << 15;
  371. const s32 delpt_shift = 1 << 8;
  372. s32 mag1, mag2;
  373. struct ath_common *common = ath9k_hw_common(ah);
  374. i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
  375. i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
  376. iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
  377. if (i2_m_q2_a0_d0 > 0x800)
  378. i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
  379. if (i2_p_q2_a0_d0 > 0x800)
  380. i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
  381. if (iq_corr_a0_d0 > 0x800)
  382. iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
  383. i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
  384. i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
  385. iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
  386. if (i2_m_q2_a0_d1 > 0x800)
  387. i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
  388. if (i2_p_q2_a0_d1 > 0x800)
  389. i2_p_q2_a0_d1 = -((0xfff - i2_p_q2_a0_d1) + 1);
  390. if (iq_corr_a0_d1 > 0x800)
  391. iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
  392. i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
  393. i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
  394. iq_corr_a1_d0 = iq_res[4] & 0xfff;
  395. if (i2_m_q2_a1_d0 > 0x800)
  396. i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
  397. if (i2_p_q2_a1_d0 > 0x800)
  398. i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
  399. if (iq_corr_a1_d0 > 0x800)
  400. iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
  401. i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
  402. i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
  403. iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
  404. if (i2_m_q2_a1_d1 > 0x800)
  405. i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
  406. if (i2_p_q2_a1_d1 > 0x800)
  407. i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
  408. if (iq_corr_a1_d1 > 0x800)
  409. iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
  410. if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
  411. (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
  412. ath_print(common, ATH_DBG_CALIBRATE,
  413. "Divide by 0:\na0_d0=%d\n"
  414. "a0_d1=%d\na2_d0=%d\na1_d1=%d\n",
  415. i2_p_q2_a0_d0, i2_p_q2_a0_d1,
  416. i2_p_q2_a1_d0, i2_p_q2_a1_d1);
  417. return false;
  418. }
  419. mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  420. phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  421. mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  422. phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  423. mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  424. phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  425. mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  426. phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  427. /* w/o analog phase shift */
  428. sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
  429. /* w/o analog phase shift */
  430. cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
  431. /* w/ analog phase shift */
  432. sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
  433. /* w/ analog phase shift */
  434. cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
  435. /*
  436. * force sin^2 + cos^2 = 1;
  437. * find magnitude by approximation
  438. */
  439. mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
  440. mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
  441. if ((mag1 == 0) || (mag2 == 0)) {
  442. ath_print(common, ATH_DBG_CALIBRATE,
  443. "Divide by 0: mag1=%d, mag2=%d\n",
  444. mag1, mag2);
  445. return false;
  446. }
  447. /* normalization sin and cos by mag */
  448. sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
  449. cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
  450. sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
  451. cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
  452. /* calculate IQ mismatch */
  453. if (!ar9003_hw_solve_iq_cal(ah,
  454. sin_2phi_1, cos_2phi_1,
  455. sin_2phi_2, cos_2phi_2,
  456. mag_a0_d0, phs_a0_d0,
  457. mag_a1_d0,
  458. phs_a1_d0, solved_eq)) {
  459. ath_print(common, ATH_DBG_CALIBRATE,
  460. "Call to ar9003_hw_solve_iq_cal() failed.\n");
  461. return false;
  462. }
  463. mag_tx = solved_eq[0];
  464. phs_tx = solved_eq[1];
  465. mag_rx = solved_eq[2];
  466. phs_rx = solved_eq[3];
  467. ath_print(common, ATH_DBG_CALIBRATE,
  468. "chain %d: mag mismatch=%d phase mismatch=%d\n",
  469. chain_idx, mag_tx/res_scale, phs_tx/res_scale);
  470. if (res_scale == mag_tx) {
  471. ath_print(common, ATH_DBG_CALIBRATE,
  472. "Divide by 0: mag_tx=%d, res_scale=%d\n",
  473. mag_tx, res_scale);
  474. return false;
  475. }
  476. /* calculate and quantize Tx IQ correction factor */
  477. mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
  478. phs_corr_tx = -phs_tx;
  479. q_q_coff = (mag_corr_tx * 128 / res_scale);
  480. q_i_coff = (phs_corr_tx * 256 / res_scale);
  481. ath_print(common, ATH_DBG_CALIBRATE,
  482. "tx chain %d: mag corr=%d phase corr=%d\n",
  483. chain_idx, q_q_coff, q_i_coff);
  484. if (q_i_coff < -63)
  485. q_i_coff = -63;
  486. if (q_i_coff > 63)
  487. q_i_coff = 63;
  488. if (q_q_coff < -63)
  489. q_q_coff = -63;
  490. if (q_q_coff > 63)
  491. q_q_coff = 63;
  492. iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
  493. ath_print(common, ATH_DBG_CALIBRATE,
  494. "tx chain %d: iq corr coeff=%x\n",
  495. chain_idx, iqc_coeff[0]);
  496. if (-mag_rx == res_scale) {
  497. ath_print(common, ATH_DBG_CALIBRATE,
  498. "Divide by 0: mag_rx=%d, res_scale=%d\n",
  499. mag_rx, res_scale);
  500. return false;
  501. }
  502. /* calculate and quantize Rx IQ correction factors */
  503. mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
  504. phs_corr_rx = -phs_rx;
  505. q_q_coff = (mag_corr_rx * 128 / res_scale);
  506. q_i_coff = (phs_corr_rx * 256 / res_scale);
  507. ath_print(common, ATH_DBG_CALIBRATE,
  508. "rx chain %d: mag corr=%d phase corr=%d\n",
  509. chain_idx, q_q_coff, q_i_coff);
  510. if (q_i_coff < -63)
  511. q_i_coff = -63;
  512. if (q_i_coff > 63)
  513. q_i_coff = 63;
  514. if (q_q_coff < -63)
  515. q_q_coff = -63;
  516. if (q_q_coff > 63)
  517. q_q_coff = 63;
  518. iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
  519. ath_print(common, ATH_DBG_CALIBRATE,
  520. "rx chain %d: iq corr coeff=%x\n",
  521. chain_idx, iqc_coeff[1]);
  522. return true;
  523. }
  524. static void ar9003_hw_tx_iq_cal(struct ath_hw *ah)
  525. {
  526. struct ath_common *common = ath9k_hw_common(ah);
  527. const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
  528. AR_PHY_TX_IQCAL_STATUS_B0,
  529. AR_PHY_TX_IQCAL_STATUS_B1,
  530. AR_PHY_TX_IQCAL_STATUS_B2,
  531. };
  532. const u32 tx_corr_coeff[AR9300_MAX_CHAINS] = {
  533. AR_PHY_TX_IQCAL_CORR_COEFF_01_B0,
  534. AR_PHY_TX_IQCAL_CORR_COEFF_01_B1,
  535. AR_PHY_TX_IQCAL_CORR_COEFF_01_B2,
  536. };
  537. const u32 rx_corr[AR9300_MAX_CHAINS] = {
  538. AR_PHY_RX_IQCAL_CORR_B0,
  539. AR_PHY_RX_IQCAL_CORR_B1,
  540. AR_PHY_RX_IQCAL_CORR_B2,
  541. };
  542. const u_int32_t chan_info_tab[] = {
  543. AR_PHY_CHAN_INFO_TAB_0,
  544. AR_PHY_CHAN_INFO_TAB_1,
  545. AR_PHY_CHAN_INFO_TAB_2,
  546. };
  547. s32 iq_res[6];
  548. s32 iqc_coeff[2];
  549. s32 i, j;
  550. u32 num_chains = 0;
  551. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  552. if (ah->txchainmask & (1 << i))
  553. num_chains++;
  554. }
  555. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
  556. AR_PHY_TX_IQCAQL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
  557. DELPT);
  558. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
  559. AR_PHY_TX_IQCAL_START_DO_CAL,
  560. AR_PHY_TX_IQCAL_START_DO_CAL);
  561. if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
  562. AR_PHY_TX_IQCAL_START_DO_CAL,
  563. 0, AH_WAIT_TIMEOUT)) {
  564. ath_print(common, ATH_DBG_CALIBRATE,
  565. "Tx IQ Cal not complete.\n");
  566. goto TX_IQ_CAL_FAILED;
  567. }
  568. for (i = 0; i < num_chains; i++) {
  569. ath_print(common, ATH_DBG_CALIBRATE,
  570. "Doing Tx IQ Cal for chain %d.\n", i);
  571. if (REG_READ(ah, txiqcal_status[i]) &
  572. AR_PHY_TX_IQCAL_STATUS_FAILED) {
  573. ath_print(common, ATH_DBG_CALIBRATE,
  574. "Tx IQ Cal failed for chain %d.\n", i);
  575. goto TX_IQ_CAL_FAILED;
  576. }
  577. for (j = 0; j < 3; j++) {
  578. u_int8_t idx = 2 * j,
  579. offset = 4 * j;
  580. REG_RMW_FIELD(ah, AR_PHY_CHAN_INFO_MEMORY,
  581. AR_PHY_CHAN_INFO_TAB_S2_READ, 0);
  582. /* 32 bits */
  583. iq_res[idx] = REG_READ(ah, chan_info_tab[i] + offset);
  584. REG_RMW_FIELD(ah, AR_PHY_CHAN_INFO_MEMORY,
  585. AR_PHY_CHAN_INFO_TAB_S2_READ, 1);
  586. /* 16 bits */
  587. iq_res[idx+1] = 0xffff & REG_READ(ah,
  588. chan_info_tab[i] +
  589. offset);
  590. ath_print(common, ATH_DBG_CALIBRATE,
  591. "IQ RES[%d]=0x%x IQ_RES[%d]=0x%x\n",
  592. idx, iq_res[idx], idx+1, iq_res[idx+1]);
  593. }
  594. if (!ar9003_hw_calc_iq_corr(ah, i, iq_res, iqc_coeff)) {
  595. ath_print(common, ATH_DBG_CALIBRATE,
  596. "Failed in calculation of IQ correction.\n");
  597. goto TX_IQ_CAL_FAILED;
  598. }
  599. ath_print(common, ATH_DBG_CALIBRATE,
  600. "IQ_COEFF[0] = 0x%x IQ_COEFF[1] = 0x%x\n",
  601. iqc_coeff[0], iqc_coeff[1]);
  602. REG_RMW_FIELD(ah, tx_corr_coeff[i],
  603. AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
  604. iqc_coeff[0]);
  605. REG_RMW_FIELD(ah, rx_corr[i],
  606. AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_Q_COFF,
  607. iqc_coeff[1] >> 7);
  608. REG_RMW_FIELD(ah, rx_corr[i],
  609. AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_I_COFF,
  610. iqc_coeff[1]);
  611. }
  612. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
  613. AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
  614. REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
  615. AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
  616. return;
  617. TX_IQ_CAL_FAILED:
  618. ath_print(common, ATH_DBG_CALIBRATE, "Tx IQ Cal failed\n");
  619. }
  620. static bool ar9003_hw_init_cal(struct ath_hw *ah,
  621. struct ath9k_channel *chan)
  622. {
  623. struct ath_common *common = ath9k_hw_common(ah);
  624. /*
  625. * 0x7 = 0b111 , AR9003 needs to be configured for 3-chain mode before
  626. * running AGC/TxIQ cals
  627. */
  628. ar9003_hw_set_chain_masks(ah, 0x7, 0x7);
  629. /* Do Tx IQ Calibration */
  630. ar9003_hw_tx_iq_cal(ah);
  631. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  632. udelay(5);
  633. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  634. /* Calibrate the AGC */
  635. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  636. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  637. AR_PHY_AGC_CONTROL_CAL);
  638. /* Poll for offset calibration complete */
  639. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
  640. 0, AH_WAIT_TIMEOUT)) {
  641. ath_print(common, ATH_DBG_CALIBRATE,
  642. "offset calibration failed to "
  643. "complete in 1ms; noisy environment?\n");
  644. return false;
  645. }
  646. /* Revert chainmasks to their original values before NF cal */
  647. ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
  648. ath9k_hw_start_nfcal(ah, true);
  649. /* Initialize list pointers */
  650. ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
  651. if (ar9003_hw_iscal_supported(ah, IQ_MISMATCH_CAL)) {
  652. INIT_CAL(&ah->iq_caldata);
  653. INSERT_CAL(ah, &ah->iq_caldata);
  654. ath_print(common, ATH_DBG_CALIBRATE,
  655. "enabling IQ Calibration.\n");
  656. }
  657. if (ar9003_hw_iscal_supported(ah, TEMP_COMP_CAL)) {
  658. INIT_CAL(&ah->tempCompCalData);
  659. INSERT_CAL(ah, &ah->tempCompCalData);
  660. ath_print(common, ATH_DBG_CALIBRATE,
  661. "enabling Temperature Compensation Calibration.\n");
  662. }
  663. /* Initialize current pointer to first element in list */
  664. ah->cal_list_curr = ah->cal_list;
  665. if (ah->cal_list_curr)
  666. ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
  667. if (ah->caldata)
  668. ah->caldata->CalValid = 0;
  669. return true;
  670. }
  671. void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
  672. {
  673. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  674. struct ath_hw_ops *ops = ath9k_hw_ops(ah);
  675. priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
  676. priv_ops->init_cal = ar9003_hw_init_cal;
  677. priv_ops->setup_calibration = ar9003_hw_setup_calibration;
  678. priv_ops->iscal_supported = ar9003_hw_iscal_supported;
  679. ops->calibrate = ar9003_hw_calibrate;
  680. }