ar9003_calib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. enum ar9003_cal_types {
  20. IQ_MISMATCH_CAL = BIT(0),
  21. TEMP_COMP_CAL = BIT(1),
  22. };
  23. static void ar9003_hw_setup_calibration(struct ath_hw *ah,
  24. struct ath9k_cal_list *currCal)
  25. {
  26. struct ath_common *common = ath9k_hw_common(ah);
  27. /* Select calibration to run */
  28. switch (currCal->calData->calType) {
  29. case IQ_MISMATCH_CAL:
  30. /*
  31. * Start calibration with
  32. * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
  33. */
  34. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  35. AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX,
  36. currCal->calData->calCountMax);
  37. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  38. ath_print(common, ATH_DBG_CALIBRATE,
  39. "starting IQ Mismatch Calibration\n");
  40. /* Kick-off cal */
  41. REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
  42. break;
  43. case TEMP_COMP_CAL:
  44. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  45. AR_PHY_65NM_CH0_THERM_LOCAL, 1);
  46. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  47. AR_PHY_65NM_CH0_THERM_START, 1);
  48. ath_print(common, ATH_DBG_CALIBRATE,
  49. "starting Temperature Compensation Calibration\n");
  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. }
  277. /*
  278. * solve 4x4 linear equation used in loopback iq cal.
  279. */
  280. static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
  281. s32 sin_2phi_1,
  282. s32 cos_2phi_1,
  283. s32 sin_2phi_2,
  284. s32 cos_2phi_2,
  285. s32 mag_a0_d0,
  286. s32 phs_a0_d0,
  287. s32 mag_a1_d0,
  288. s32 phs_a1_d0,
  289. s32 solved_eq[])
  290. {
  291. s32 f1 = cos_2phi_1 - cos_2phi_2,
  292. f3 = sin_2phi_1 - sin_2phi_2,
  293. f2;
  294. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  295. const s32 result_shift = 1 << 15;
  296. struct ath_common *common = ath9k_hw_common(ah);
  297. f2 = (f1 * f1 + f3 * f3) / result_shift;
  298. if (!f2) {
  299. ath_print(common, ATH_DBG_CALIBRATE, "Divide by 0\n");
  300. return false;
  301. }
  302. /* mag mismatch, tx */
  303. mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
  304. /* phs mismatch, tx */
  305. phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
  306. mag_tx = (mag_tx / f2);
  307. phs_tx = (phs_tx / f2);
  308. /* mag mismatch, rx */
  309. mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
  310. result_shift;
  311. /* phs mismatch, rx */
  312. phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
  313. result_shift;
  314. solved_eq[0] = mag_tx;
  315. solved_eq[1] = phs_tx;
  316. solved_eq[2] = mag_rx;
  317. solved_eq[3] = phs_rx;
  318. return true;
  319. }
  320. static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
  321. {
  322. s32 abs_i = abs(in_re),
  323. abs_q = abs(in_im),
  324. max_abs, min_abs;
  325. if (abs_i > abs_q) {
  326. max_abs = abs_i;
  327. min_abs = abs_q;
  328. } else {
  329. max_abs = abs_q;
  330. min_abs = abs_i;
  331. }
  332. return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
  333. }
  334. #define DELPT 32
  335. static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
  336. s32 chain_idx,
  337. const s32 iq_res[],
  338. s32 iqc_coeff[])
  339. {
  340. s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
  341. i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
  342. i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
  343. i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
  344. s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
  345. phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
  346. sin_2phi_1, cos_2phi_1,
  347. sin_2phi_2, cos_2phi_2;
  348. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  349. s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
  350. q_q_coff, q_i_coff;
  351. const s32 res_scale = 1 << 15;
  352. const s32 delpt_shift = 1 << 8;
  353. s32 mag1, mag2;
  354. struct ath_common *common = ath9k_hw_common(ah);
  355. i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
  356. i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
  357. iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
  358. if (i2_m_q2_a0_d0 > 0x800)
  359. i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
  360. if (i2_p_q2_a0_d0 > 0x800)
  361. i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
  362. if (iq_corr_a0_d0 > 0x800)
  363. iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
  364. i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
  365. i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
  366. iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
  367. if (i2_m_q2_a0_d1 > 0x800)
  368. i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
  369. if (i2_p_q2_a0_d1 > 0x800)
  370. i2_p_q2_a0_d1 = -((0xfff - i2_p_q2_a0_d1) + 1);
  371. if (iq_corr_a0_d1 > 0x800)
  372. iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
  373. i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
  374. i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
  375. iq_corr_a1_d0 = iq_res[4] & 0xfff;
  376. if (i2_m_q2_a1_d0 > 0x800)
  377. i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
  378. if (i2_p_q2_a1_d0 > 0x800)
  379. i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
  380. if (iq_corr_a1_d0 > 0x800)
  381. iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
  382. i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
  383. i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
  384. iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
  385. if (i2_m_q2_a1_d1 > 0x800)
  386. i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
  387. if (i2_p_q2_a1_d1 > 0x800)
  388. i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
  389. if (iq_corr_a1_d1 > 0x800)
  390. iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
  391. if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
  392. (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
  393. ath_print(common, ATH_DBG_CALIBRATE,
  394. "Divide by 0:\na0_d0=%d\n"
  395. "a0_d1=%d\na2_d0=%d\na1_d1=%d\n",
  396. i2_p_q2_a0_d0, i2_p_q2_a0_d1,
  397. i2_p_q2_a1_d0, i2_p_q2_a1_d1);
  398. return false;
  399. }
  400. mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  401. phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  402. mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  403. phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  404. mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  405. phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  406. mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  407. phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  408. /* w/o analog phase shift */
  409. sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
  410. /* w/o analog phase shift */
  411. cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
  412. /* w/ analog phase shift */
  413. sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
  414. /* w/ analog phase shift */
  415. cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
  416. /*
  417. * force sin^2 + cos^2 = 1;
  418. * find magnitude by approximation
  419. */
  420. mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
  421. mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
  422. if ((mag1 == 0) || (mag2 == 0)) {
  423. ath_print(common, ATH_DBG_CALIBRATE,
  424. "Divide by 0: mag1=%d, mag2=%d\n",
  425. mag1, mag2);
  426. return false;
  427. }
  428. /* normalization sin and cos by mag */
  429. sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
  430. cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
  431. sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
  432. cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
  433. /* calculate IQ mismatch */
  434. if (!ar9003_hw_solve_iq_cal(ah,
  435. sin_2phi_1, cos_2phi_1,
  436. sin_2phi_2, cos_2phi_2,
  437. mag_a0_d0, phs_a0_d0,
  438. mag_a1_d0,
  439. phs_a1_d0, solved_eq)) {
  440. ath_print(common, ATH_DBG_CALIBRATE,
  441. "Call to ar9003_hw_solve_iq_cal() failed.\n");
  442. return false;
  443. }
  444. mag_tx = solved_eq[0];
  445. phs_tx = solved_eq[1];
  446. mag_rx = solved_eq[2];
  447. phs_rx = solved_eq[3];
  448. ath_print(common, ATH_DBG_CALIBRATE,
  449. "chain %d: mag mismatch=%d phase mismatch=%d\n",
  450. chain_idx, mag_tx/res_scale, phs_tx/res_scale);
  451. if (res_scale == mag_tx) {
  452. ath_print(common, ATH_DBG_CALIBRATE,
  453. "Divide by 0: mag_tx=%d, res_scale=%d\n",
  454. mag_tx, res_scale);
  455. return false;
  456. }
  457. /* calculate and quantize Tx IQ correction factor */
  458. mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
  459. phs_corr_tx = -phs_tx;
  460. q_q_coff = (mag_corr_tx * 128 / res_scale);
  461. q_i_coff = (phs_corr_tx * 256 / res_scale);
  462. ath_print(common, ATH_DBG_CALIBRATE,
  463. "tx chain %d: mag corr=%d phase corr=%d\n",
  464. chain_idx, q_q_coff, q_i_coff);
  465. if (q_i_coff < -63)
  466. q_i_coff = -63;
  467. if (q_i_coff > 63)
  468. q_i_coff = 63;
  469. if (q_q_coff < -63)
  470. q_q_coff = -63;
  471. if (q_q_coff > 63)
  472. q_q_coff = 63;
  473. iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
  474. ath_print(common, ATH_DBG_CALIBRATE,
  475. "tx chain %d: iq corr coeff=%x\n",
  476. chain_idx, iqc_coeff[0]);
  477. if (-mag_rx == res_scale) {
  478. ath_print(common, ATH_DBG_CALIBRATE,
  479. "Divide by 0: mag_rx=%d, res_scale=%d\n",
  480. mag_rx, res_scale);
  481. return false;
  482. }
  483. /* calculate and quantize Rx IQ correction factors */
  484. mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
  485. phs_corr_rx = -phs_rx;
  486. q_q_coff = (mag_corr_rx * 128 / res_scale);
  487. q_i_coff = (phs_corr_rx * 256 / res_scale);
  488. ath_print(common, ATH_DBG_CALIBRATE,
  489. "rx chain %d: mag corr=%d phase corr=%d\n",
  490. chain_idx, q_q_coff, q_i_coff);
  491. if (q_i_coff < -63)
  492. q_i_coff = -63;
  493. if (q_i_coff > 63)
  494. q_i_coff = 63;
  495. if (q_q_coff < -63)
  496. q_q_coff = -63;
  497. if (q_q_coff > 63)
  498. q_q_coff = 63;
  499. iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
  500. ath_print(common, ATH_DBG_CALIBRATE,
  501. "rx chain %d: iq corr coeff=%x\n",
  502. chain_idx, iqc_coeff[1]);
  503. return true;
  504. }
  505. static void ar9003_hw_tx_iq_cal(struct ath_hw *ah)
  506. {
  507. struct ath_common *common = ath9k_hw_common(ah);
  508. const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
  509. AR_PHY_TX_IQCAL_STATUS_B0,
  510. AR_PHY_TX_IQCAL_STATUS_B1,
  511. AR_PHY_TX_IQCAL_STATUS_B2,
  512. };
  513. const u32 tx_corr_coeff[AR9300_MAX_CHAINS] = {
  514. AR_PHY_TX_IQCAL_CORR_COEFF_01_B0,
  515. AR_PHY_TX_IQCAL_CORR_COEFF_01_B1,
  516. AR_PHY_TX_IQCAL_CORR_COEFF_01_B2,
  517. };
  518. const u32 rx_corr[AR9300_MAX_CHAINS] = {
  519. AR_PHY_RX_IQCAL_CORR_B0,
  520. AR_PHY_RX_IQCAL_CORR_B1,
  521. AR_PHY_RX_IQCAL_CORR_B2,
  522. };
  523. const u_int32_t chan_info_tab[] = {
  524. AR_PHY_CHAN_INFO_TAB_0,
  525. AR_PHY_CHAN_INFO_TAB_1,
  526. AR_PHY_CHAN_INFO_TAB_2,
  527. };
  528. s32 iq_res[6];
  529. s32 iqc_coeff[2];
  530. s32 i, j;
  531. u32 num_chains = 0;
  532. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  533. if (ah->txchainmask & (1 << i))
  534. num_chains++;
  535. }
  536. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
  537. AR_PHY_TX_IQCAQL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
  538. DELPT);
  539. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
  540. AR_PHY_TX_IQCAL_START_DO_CAL,
  541. AR_PHY_TX_IQCAL_START_DO_CAL);
  542. if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
  543. AR_PHY_TX_IQCAL_START_DO_CAL,
  544. 0, AH_WAIT_TIMEOUT)) {
  545. ath_print(common, ATH_DBG_CALIBRATE,
  546. "Tx IQ Cal not complete.\n");
  547. goto TX_IQ_CAL_FAILED;
  548. }
  549. for (i = 0; i < num_chains; i++) {
  550. ath_print(common, ATH_DBG_CALIBRATE,
  551. "Doing Tx IQ Cal for chain %d.\n", i);
  552. if (REG_READ(ah, txiqcal_status[i]) &
  553. AR_PHY_TX_IQCAL_STATUS_FAILED) {
  554. ath_print(common, ATH_DBG_CALIBRATE,
  555. "Tx IQ Cal failed for chain %d.\n", i);
  556. goto TX_IQ_CAL_FAILED;
  557. }
  558. for (j = 0; j < 3; j++) {
  559. u_int8_t idx = 2 * j,
  560. offset = 4 * j;
  561. REG_RMW_FIELD(ah, AR_PHY_CHAN_INFO_MEMORY,
  562. AR_PHY_CHAN_INFO_TAB_S2_READ, 0);
  563. /* 32 bits */
  564. iq_res[idx] = REG_READ(ah, chan_info_tab[i] + offset);
  565. REG_RMW_FIELD(ah, AR_PHY_CHAN_INFO_MEMORY,
  566. AR_PHY_CHAN_INFO_TAB_S2_READ, 1);
  567. /* 16 bits */
  568. iq_res[idx+1] = 0xffff & REG_READ(ah,
  569. chan_info_tab[i] +
  570. offset);
  571. ath_print(common, ATH_DBG_CALIBRATE,
  572. "IQ RES[%d]=0x%x IQ_RES[%d]=0x%x\n",
  573. idx, iq_res[idx], idx+1, iq_res[idx+1]);
  574. }
  575. if (!ar9003_hw_calc_iq_corr(ah, i, iq_res, iqc_coeff)) {
  576. ath_print(common, ATH_DBG_CALIBRATE,
  577. "Failed in calculation of IQ correction.\n");
  578. goto TX_IQ_CAL_FAILED;
  579. }
  580. ath_print(common, ATH_DBG_CALIBRATE,
  581. "IQ_COEFF[0] = 0x%x IQ_COEFF[1] = 0x%x\n",
  582. iqc_coeff[0], iqc_coeff[1]);
  583. REG_RMW_FIELD(ah, tx_corr_coeff[i],
  584. AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
  585. iqc_coeff[0]);
  586. REG_RMW_FIELD(ah, rx_corr[i],
  587. AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_Q_COFF,
  588. iqc_coeff[1] >> 7);
  589. REG_RMW_FIELD(ah, rx_corr[i],
  590. AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_I_COFF,
  591. iqc_coeff[1]);
  592. }
  593. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
  594. AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
  595. REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
  596. AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
  597. return;
  598. TX_IQ_CAL_FAILED:
  599. ath_print(common, ATH_DBG_CALIBRATE, "Tx IQ Cal failed\n");
  600. }
  601. static bool ar9003_hw_init_cal(struct ath_hw *ah,
  602. struct ath9k_channel *chan)
  603. {
  604. struct ath_common *common = ath9k_hw_common(ah);
  605. /*
  606. * 0x7 = 0b111 , AR9003 needs to be configured for 3-chain mode before
  607. * running AGC/TxIQ cals
  608. */
  609. ar9003_hw_set_chain_masks(ah, 0x7, 0x7);
  610. /* Do Tx IQ Calibration */
  611. ar9003_hw_tx_iq_cal(ah);
  612. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  613. udelay(5);
  614. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  615. /* Calibrate the AGC */
  616. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  617. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  618. AR_PHY_AGC_CONTROL_CAL);
  619. /* Poll for offset calibration complete */
  620. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
  621. 0, AH_WAIT_TIMEOUT)) {
  622. ath_print(common, ATH_DBG_CALIBRATE,
  623. "offset calibration failed to "
  624. "complete in 1ms; noisy environment?\n");
  625. return false;
  626. }
  627. /* Revert chainmasks to their original values before NF cal */
  628. ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
  629. ath9k_hw_start_nfcal(ah, true);
  630. /* Initialize list pointers */
  631. ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
  632. ah->supp_cals = IQ_MISMATCH_CAL;
  633. if (ah->supp_cals & IQ_MISMATCH_CAL) {
  634. INIT_CAL(&ah->iq_caldata);
  635. INSERT_CAL(ah, &ah->iq_caldata);
  636. ath_print(common, ATH_DBG_CALIBRATE,
  637. "enabling IQ Calibration.\n");
  638. }
  639. if (ah->supp_cals & TEMP_COMP_CAL) {
  640. INIT_CAL(&ah->tempCompCalData);
  641. INSERT_CAL(ah, &ah->tempCompCalData);
  642. ath_print(common, ATH_DBG_CALIBRATE,
  643. "enabling Temperature Compensation Calibration.\n");
  644. }
  645. /* Initialize current pointer to first element in list */
  646. ah->cal_list_curr = ah->cal_list;
  647. if (ah->cal_list_curr)
  648. ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
  649. if (ah->caldata)
  650. ah->caldata->CalValid = 0;
  651. return true;
  652. }
  653. void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
  654. {
  655. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  656. struct ath_hw_ops *ops = ath9k_hw_ops(ah);
  657. priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
  658. priv_ops->init_cal = ar9003_hw_init_cal;
  659. priv_ops->setup_calibration = ar9003_hw_setup_calibration;
  660. ops->calibrate = ar9003_hw_calibrate;
  661. }