ar9003_calib.c 22 KB

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