ar9003_calib.c 22 KB

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