ar9003_calib.c 22 KB

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