ar9003_calib.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /*
  2. * Copyright (c) 2010-2011 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. #include "ar9003_rtt.h"
  20. #define MAX_MEASUREMENT MAX_IQCAL_MEASUREMENT
  21. #define MAX_MAG_DELTA 11
  22. #define MAX_PHS_DELTA 10
  23. struct coeff {
  24. int mag_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
  25. int phs_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
  26. int iqc_coeff[2];
  27. };
  28. enum ar9003_cal_types {
  29. IQ_MISMATCH_CAL = BIT(0),
  30. TEMP_COMP_CAL = BIT(1),
  31. };
  32. static void ar9003_hw_setup_calibration(struct ath_hw *ah,
  33. struct ath9k_cal_list *currCal)
  34. {
  35. struct ath_common *common = ath9k_hw_common(ah);
  36. /* Select calibration to run */
  37. switch (currCal->calData->calType) {
  38. case IQ_MISMATCH_CAL:
  39. /*
  40. * Start calibration with
  41. * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
  42. */
  43. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  44. AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX,
  45. currCal->calData->calCountMax);
  46. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  47. ath_dbg(common, ATH_DBG_CALIBRATE,
  48. "starting IQ Mismatch Calibration\n");
  49. /* Kick-off cal */
  50. REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
  51. break;
  52. case TEMP_COMP_CAL:
  53. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  54. AR_PHY_65NM_CH0_THERM_LOCAL, 1);
  55. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
  56. AR_PHY_65NM_CH0_THERM_START, 1);
  57. ath_dbg(common, ATH_DBG_CALIBRATE,
  58. "starting Temperature Compensation Calibration\n");
  59. break;
  60. }
  61. }
  62. /*
  63. * Generic calibration routine.
  64. * Recalibrate the lower PHY chips to account for temperature/environment
  65. * changes.
  66. */
  67. static bool ar9003_hw_per_calibration(struct ath_hw *ah,
  68. struct ath9k_channel *ichan,
  69. u8 rxchainmask,
  70. struct ath9k_cal_list *currCal)
  71. {
  72. struct ath9k_hw_cal_data *caldata = ah->caldata;
  73. /* Cal is assumed not done until explicitly set below */
  74. bool iscaldone = false;
  75. /* Calibration in progress. */
  76. if (currCal->calState == CAL_RUNNING) {
  77. /* Check to see if it has finished. */
  78. if (!(REG_READ(ah, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
  79. /*
  80. * Accumulate cal measures for active chains
  81. */
  82. currCal->calData->calCollect(ah);
  83. ah->cal_samples++;
  84. if (ah->cal_samples >=
  85. currCal->calData->calNumSamples) {
  86. unsigned int i, numChains = 0;
  87. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  88. if (rxchainmask & (1 << i))
  89. numChains++;
  90. }
  91. /*
  92. * Process accumulated data
  93. */
  94. currCal->calData->calPostProc(ah, numChains);
  95. /* Calibration has finished. */
  96. caldata->CalValid |= currCal->calData->calType;
  97. currCal->calState = CAL_DONE;
  98. iscaldone = true;
  99. } else {
  100. /*
  101. * Set-up collection of another sub-sample until we
  102. * get desired number
  103. */
  104. ar9003_hw_setup_calibration(ah, currCal);
  105. }
  106. }
  107. } else if (!(caldata->CalValid & currCal->calData->calType)) {
  108. /* If current cal is marked invalid in channel, kick it off */
  109. ath9k_hw_reset_calibration(ah, currCal);
  110. }
  111. return iscaldone;
  112. }
  113. static bool ar9003_hw_calibrate(struct ath_hw *ah,
  114. struct ath9k_channel *chan,
  115. u8 rxchainmask,
  116. bool longcal)
  117. {
  118. bool iscaldone = true;
  119. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  120. /*
  121. * For given calibration:
  122. * 1. Call generic cal routine
  123. * 2. When this cal is done (isCalDone) if we have more cals waiting
  124. * (eg after reset), mask this to upper layers by not propagating
  125. * isCalDone if it is set to TRUE.
  126. * Instead, change isCalDone to FALSE and setup the waiting cal(s)
  127. * to be run.
  128. */
  129. if (currCal &&
  130. (currCal->calState == CAL_RUNNING ||
  131. currCal->calState == CAL_WAITING)) {
  132. iscaldone = ar9003_hw_per_calibration(ah, chan,
  133. rxchainmask, currCal);
  134. if (iscaldone) {
  135. ah->cal_list_curr = currCal = currCal->calNext;
  136. if (currCal->calState == CAL_WAITING) {
  137. iscaldone = false;
  138. ath9k_hw_reset_calibration(ah, currCal);
  139. }
  140. }
  141. }
  142. /* Do NF cal only at longer intervals */
  143. if (longcal) {
  144. /*
  145. * Get the value from the previous NF cal and update
  146. * history buffer.
  147. */
  148. ath9k_hw_getnf(ah, chan);
  149. /*
  150. * Load the NF from history buffer of the current channel.
  151. * NF is slow time-variant, so it is OK to use a historical
  152. * value.
  153. */
  154. ath9k_hw_loadnf(ah, ah->curchan);
  155. /* start NF calibration, without updating BB NF register */
  156. ath9k_hw_start_nfcal(ah, false);
  157. }
  158. return iscaldone;
  159. }
  160. static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
  161. {
  162. int i;
  163. /* Accumulate IQ cal measures for active chains */
  164. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  165. if (ah->txchainmask & BIT(i)) {
  166. ah->totalPowerMeasI[i] +=
  167. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  168. ah->totalPowerMeasQ[i] +=
  169. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  170. ah->totalIqCorrMeas[i] +=
  171. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  172. ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  173. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  174. ah->cal_samples, i, ah->totalPowerMeasI[i],
  175. ah->totalPowerMeasQ[i],
  176. ah->totalIqCorrMeas[i]);
  177. }
  178. }
  179. }
  180. static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
  181. {
  182. struct ath_common *common = ath9k_hw_common(ah);
  183. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  184. u32 qCoffDenom, iCoffDenom;
  185. int32_t qCoff, iCoff;
  186. int iqCorrNeg, i;
  187. static const u_int32_t offset_array[3] = {
  188. AR_PHY_RX_IQCAL_CORR_B0,
  189. AR_PHY_RX_IQCAL_CORR_B1,
  190. AR_PHY_RX_IQCAL_CORR_B2,
  191. };
  192. for (i = 0; i < numChains; i++) {
  193. powerMeasI = ah->totalPowerMeasI[i];
  194. powerMeasQ = ah->totalPowerMeasQ[i];
  195. iqCorrMeas = ah->totalIqCorrMeas[i];
  196. ath_dbg(common, ATH_DBG_CALIBRATE,
  197. "Starting IQ Cal and Correction for Chain %d\n",
  198. i);
  199. ath_dbg(common, ATH_DBG_CALIBRATE,
  200. "Orignal: Chn %diq_corr_meas = 0x%08x\n",
  201. i, ah->totalIqCorrMeas[i]);
  202. iqCorrNeg = 0;
  203. if (iqCorrMeas > 0x80000000) {
  204. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  205. iqCorrNeg = 1;
  206. }
  207. ath_dbg(common, ATH_DBG_CALIBRATE,
  208. "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
  209. ath_dbg(common, ATH_DBG_CALIBRATE,
  210. "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
  211. ath_dbg(common, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
  212. iqCorrNeg);
  213. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
  214. qCoffDenom = powerMeasQ / 64;
  215. if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
  216. iCoff = iqCorrMeas / iCoffDenom;
  217. qCoff = powerMeasI / qCoffDenom - 64;
  218. ath_dbg(common, ATH_DBG_CALIBRATE,
  219. "Chn %d iCoff = 0x%08x\n", i, iCoff);
  220. ath_dbg(common, ATH_DBG_CALIBRATE,
  221. "Chn %d qCoff = 0x%08x\n", i, qCoff);
  222. /* Force bounds on iCoff */
  223. if (iCoff >= 63)
  224. iCoff = 63;
  225. else if (iCoff <= -63)
  226. iCoff = -63;
  227. /* Negate iCoff if iqCorrNeg == 0 */
  228. if (iqCorrNeg == 0x0)
  229. iCoff = -iCoff;
  230. /* Force bounds on qCoff */
  231. if (qCoff >= 63)
  232. qCoff = 63;
  233. else if (qCoff <= -63)
  234. qCoff = -63;
  235. iCoff = iCoff & 0x7f;
  236. qCoff = qCoff & 0x7f;
  237. ath_dbg(common, ATH_DBG_CALIBRATE,
  238. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  239. i, iCoff, qCoff);
  240. ath_dbg(common, ATH_DBG_CALIBRATE,
  241. "Register offset (0x%04x) before update = 0x%x\n",
  242. offset_array[i],
  243. REG_READ(ah, offset_array[i]));
  244. REG_RMW_FIELD(ah, offset_array[i],
  245. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  246. iCoff);
  247. REG_RMW_FIELD(ah, offset_array[i],
  248. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  249. qCoff);
  250. ath_dbg(common, ATH_DBG_CALIBRATE,
  251. "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
  252. offset_array[i],
  253. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  254. REG_READ(ah, offset_array[i]));
  255. ath_dbg(common, ATH_DBG_CALIBRATE,
  256. "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
  257. offset_array[i],
  258. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  259. REG_READ(ah, offset_array[i]));
  260. ath_dbg(common, ATH_DBG_CALIBRATE,
  261. "IQ Cal and Correction done for Chain %d\n", i);
  262. }
  263. }
  264. REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
  265. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
  266. ath_dbg(common, ATH_DBG_CALIBRATE,
  267. "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
  268. (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
  269. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
  270. REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
  271. }
  272. static const struct ath9k_percal_data iq_cal_single_sample = {
  273. IQ_MISMATCH_CAL,
  274. MIN_CAL_SAMPLES,
  275. PER_MAX_LOG_COUNT,
  276. ar9003_hw_iqcal_collect,
  277. ar9003_hw_iqcalibrate
  278. };
  279. static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
  280. {
  281. ah->iq_caldata.calData = &iq_cal_single_sample;
  282. }
  283. /*
  284. * solve 4x4 linear equation used in loopback iq cal.
  285. */
  286. static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
  287. s32 sin_2phi_1,
  288. s32 cos_2phi_1,
  289. s32 sin_2phi_2,
  290. s32 cos_2phi_2,
  291. s32 mag_a0_d0,
  292. s32 phs_a0_d0,
  293. s32 mag_a1_d0,
  294. s32 phs_a1_d0,
  295. s32 solved_eq[])
  296. {
  297. s32 f1 = cos_2phi_1 - cos_2phi_2,
  298. f3 = sin_2phi_1 - sin_2phi_2,
  299. f2;
  300. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  301. const s32 result_shift = 1 << 15;
  302. struct ath_common *common = ath9k_hw_common(ah);
  303. f2 = (f1 * f1 + f3 * f3) / result_shift;
  304. if (!f2) {
  305. ath_dbg(common, ATH_DBG_CALIBRATE, "Divide by 0\n");
  306. return false;
  307. }
  308. /* mag mismatch, tx */
  309. mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
  310. /* phs mismatch, tx */
  311. phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
  312. mag_tx = (mag_tx / f2);
  313. phs_tx = (phs_tx / f2);
  314. /* mag mismatch, rx */
  315. mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
  316. result_shift;
  317. /* phs mismatch, rx */
  318. phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
  319. result_shift;
  320. solved_eq[0] = mag_tx;
  321. solved_eq[1] = phs_tx;
  322. solved_eq[2] = mag_rx;
  323. solved_eq[3] = phs_rx;
  324. return true;
  325. }
  326. static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
  327. {
  328. s32 abs_i = abs(in_re),
  329. abs_q = abs(in_im),
  330. max_abs, min_abs;
  331. if (abs_i > abs_q) {
  332. max_abs = abs_i;
  333. min_abs = abs_q;
  334. } else {
  335. max_abs = abs_q;
  336. min_abs = abs_i;
  337. }
  338. return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
  339. }
  340. #define DELPT 32
  341. static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
  342. s32 chain_idx,
  343. const s32 iq_res[],
  344. s32 iqc_coeff[])
  345. {
  346. s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
  347. i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
  348. i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
  349. i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
  350. s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
  351. phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
  352. sin_2phi_1, cos_2phi_1,
  353. sin_2phi_2, cos_2phi_2;
  354. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  355. s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
  356. q_q_coff, q_i_coff;
  357. const s32 res_scale = 1 << 15;
  358. const s32 delpt_shift = 1 << 8;
  359. s32 mag1, mag2;
  360. struct ath_common *common = ath9k_hw_common(ah);
  361. i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
  362. i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
  363. iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
  364. if (i2_m_q2_a0_d0 > 0x800)
  365. i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
  366. if (i2_p_q2_a0_d0 > 0x800)
  367. i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
  368. if (iq_corr_a0_d0 > 0x800)
  369. iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
  370. i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
  371. i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
  372. iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
  373. if (i2_m_q2_a0_d1 > 0x800)
  374. i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
  375. if (i2_p_q2_a0_d1 > 0x800)
  376. i2_p_q2_a0_d1 = -((0xfff - i2_p_q2_a0_d1) + 1);
  377. if (iq_corr_a0_d1 > 0x800)
  378. iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
  379. i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
  380. i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
  381. iq_corr_a1_d0 = iq_res[4] & 0xfff;
  382. if (i2_m_q2_a1_d0 > 0x800)
  383. i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
  384. if (i2_p_q2_a1_d0 > 0x800)
  385. i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
  386. if (iq_corr_a1_d0 > 0x800)
  387. iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
  388. i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
  389. i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
  390. iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
  391. if (i2_m_q2_a1_d1 > 0x800)
  392. i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
  393. if (i2_p_q2_a1_d1 > 0x800)
  394. i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
  395. if (iq_corr_a1_d1 > 0x800)
  396. iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
  397. if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
  398. (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
  399. ath_dbg(common, ATH_DBG_CALIBRATE,
  400. "Divide by 0:\n"
  401. "a0_d0=%d\n"
  402. "a0_d1=%d\n"
  403. "a2_d0=%d\n"
  404. "a1_d1=%d\n",
  405. i2_p_q2_a0_d0, i2_p_q2_a0_d1,
  406. i2_p_q2_a1_d0, i2_p_q2_a1_d1);
  407. return false;
  408. }
  409. mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  410. phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  411. mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  412. phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  413. mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  414. phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  415. mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  416. phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  417. /* w/o analog phase shift */
  418. sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
  419. /* w/o analog phase shift */
  420. cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
  421. /* w/ analog phase shift */
  422. sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
  423. /* w/ analog phase shift */
  424. cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
  425. /*
  426. * force sin^2 + cos^2 = 1;
  427. * find magnitude by approximation
  428. */
  429. mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
  430. mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
  431. if ((mag1 == 0) || (mag2 == 0)) {
  432. ath_dbg(common, ATH_DBG_CALIBRATE,
  433. "Divide by 0: mag1=%d, mag2=%d\n",
  434. mag1, mag2);
  435. return false;
  436. }
  437. /* normalization sin and cos by mag */
  438. sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
  439. cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
  440. sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
  441. cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
  442. /* calculate IQ mismatch */
  443. if (!ar9003_hw_solve_iq_cal(ah,
  444. sin_2phi_1, cos_2phi_1,
  445. sin_2phi_2, cos_2phi_2,
  446. mag_a0_d0, phs_a0_d0,
  447. mag_a1_d0,
  448. phs_a1_d0, solved_eq)) {
  449. ath_dbg(common, ATH_DBG_CALIBRATE,
  450. "Call to ar9003_hw_solve_iq_cal() failed.\n");
  451. return false;
  452. }
  453. mag_tx = solved_eq[0];
  454. phs_tx = solved_eq[1];
  455. mag_rx = solved_eq[2];
  456. phs_rx = solved_eq[3];
  457. ath_dbg(common, ATH_DBG_CALIBRATE,
  458. "chain %d: mag mismatch=%d phase mismatch=%d\n",
  459. chain_idx, mag_tx/res_scale, phs_tx/res_scale);
  460. if (res_scale == mag_tx) {
  461. ath_dbg(common, ATH_DBG_CALIBRATE,
  462. "Divide by 0: mag_tx=%d, res_scale=%d\n",
  463. mag_tx, res_scale);
  464. return false;
  465. }
  466. /* calculate and quantize Tx IQ correction factor */
  467. mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
  468. phs_corr_tx = -phs_tx;
  469. q_q_coff = (mag_corr_tx * 128 / res_scale);
  470. q_i_coff = (phs_corr_tx * 256 / res_scale);
  471. ath_dbg(common, ATH_DBG_CALIBRATE,
  472. "tx chain %d: mag corr=%d phase corr=%d\n",
  473. chain_idx, q_q_coff, q_i_coff);
  474. if (q_i_coff < -63)
  475. q_i_coff = -63;
  476. if (q_i_coff > 63)
  477. q_i_coff = 63;
  478. if (q_q_coff < -63)
  479. q_q_coff = -63;
  480. if (q_q_coff > 63)
  481. q_q_coff = 63;
  482. iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
  483. ath_dbg(common, ATH_DBG_CALIBRATE,
  484. "tx chain %d: iq corr coeff=%x\n",
  485. chain_idx, iqc_coeff[0]);
  486. if (-mag_rx == res_scale) {
  487. ath_dbg(common, ATH_DBG_CALIBRATE,
  488. "Divide by 0: mag_rx=%d, res_scale=%d\n",
  489. mag_rx, res_scale);
  490. return false;
  491. }
  492. /* calculate and quantize Rx IQ correction factors */
  493. mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
  494. phs_corr_rx = -phs_rx;
  495. q_q_coff = (mag_corr_rx * 128 / res_scale);
  496. q_i_coff = (phs_corr_rx * 256 / res_scale);
  497. ath_dbg(common, ATH_DBG_CALIBRATE,
  498. "rx chain %d: mag corr=%d phase corr=%d\n",
  499. chain_idx, q_q_coff, q_i_coff);
  500. if (q_i_coff < -63)
  501. q_i_coff = -63;
  502. if (q_i_coff > 63)
  503. q_i_coff = 63;
  504. if (q_q_coff < -63)
  505. q_q_coff = -63;
  506. if (q_q_coff > 63)
  507. q_q_coff = 63;
  508. iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
  509. ath_dbg(common, ATH_DBG_CALIBRATE,
  510. "rx chain %d: iq corr coeff=%x\n",
  511. chain_idx, iqc_coeff[1]);
  512. return true;
  513. }
  514. static void ar9003_hw_detect_outlier(int *mp_coeff, int nmeasurement,
  515. int max_delta)
  516. {
  517. int mp_max = -64, max_idx = 0;
  518. int mp_min = 63, min_idx = 0;
  519. int mp_avg = 0, i, outlier_idx = 0, mp_count = 0;
  520. /* find min/max mismatch across all calibrated gains */
  521. for (i = 0; i < nmeasurement; i++) {
  522. if (mp_coeff[i] > mp_max) {
  523. mp_max = mp_coeff[i];
  524. max_idx = i;
  525. } else if (mp_coeff[i] < mp_min) {
  526. mp_min = mp_coeff[i];
  527. min_idx = i;
  528. }
  529. }
  530. /* find average (exclude max abs value) */
  531. for (i = 0; i < nmeasurement; i++) {
  532. if ((abs(mp_coeff[i]) < abs(mp_max)) ||
  533. (abs(mp_coeff[i]) < abs(mp_min))) {
  534. mp_avg += mp_coeff[i];
  535. mp_count++;
  536. }
  537. }
  538. /*
  539. * finding mean magnitude/phase if possible, otherwise
  540. * just use the last value as the mean
  541. */
  542. if (mp_count)
  543. mp_avg /= mp_count;
  544. else
  545. mp_avg = mp_coeff[nmeasurement - 1];
  546. /* detect outlier */
  547. if (abs(mp_max - mp_min) > max_delta) {
  548. if (abs(mp_max - mp_avg) > abs(mp_min - mp_avg))
  549. outlier_idx = max_idx;
  550. else
  551. outlier_idx = min_idx;
  552. mp_coeff[outlier_idx] = mp_avg;
  553. }
  554. }
  555. static void ar9003_hw_tx_iqcal_load_avg_2_passes(struct ath_hw *ah,
  556. u8 num_chains,
  557. struct coeff *coeff,
  558. bool is_reusable)
  559. {
  560. int i, im, nmeasurement;
  561. u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
  562. struct ath9k_hw_cal_data *caldata = ah->caldata;
  563. memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
  564. for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
  565. tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
  566. AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
  567. if (!AR_SREV_9485(ah)) {
  568. tx_corr_coeff[i * 2][1] =
  569. tx_corr_coeff[(i * 2) + 1][1] =
  570. AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
  571. tx_corr_coeff[i * 2][2] =
  572. tx_corr_coeff[(i * 2) + 1][2] =
  573. AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
  574. }
  575. }
  576. /* Load the average of 2 passes */
  577. for (i = 0; i < num_chains; i++) {
  578. nmeasurement = REG_READ_FIELD(ah,
  579. AR_PHY_TX_IQCAL_STATUS_B0,
  580. AR_PHY_CALIBRATED_GAINS_0);
  581. if (nmeasurement > MAX_MEASUREMENT)
  582. nmeasurement = MAX_MEASUREMENT;
  583. /* detect outlier only if nmeasurement > 1 */
  584. if (nmeasurement > 1) {
  585. /* Detect magnitude outlier */
  586. ar9003_hw_detect_outlier(coeff->mag_coeff[i],
  587. nmeasurement, MAX_MAG_DELTA);
  588. /* Detect phase outlier */
  589. ar9003_hw_detect_outlier(coeff->phs_coeff[i],
  590. nmeasurement, MAX_PHS_DELTA);
  591. }
  592. for (im = 0; im < nmeasurement; im++) {
  593. coeff->iqc_coeff[0] = (coeff->mag_coeff[i][im] & 0x7f) |
  594. ((coeff->phs_coeff[i][im] & 0x7f) << 7);
  595. if ((im % 2) == 0)
  596. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  597. AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
  598. coeff->iqc_coeff[0]);
  599. else
  600. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  601. AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
  602. coeff->iqc_coeff[0]);
  603. if (caldata)
  604. caldata->tx_corr_coeff[im][i] =
  605. coeff->iqc_coeff[0];
  606. }
  607. if (caldata)
  608. caldata->num_measures[i] = nmeasurement;
  609. }
  610. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
  611. AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
  612. REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
  613. AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
  614. if (caldata)
  615. caldata->done_txiqcal_once = is_reusable;
  616. return;
  617. }
  618. static bool ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
  619. {
  620. struct ath_common *common = ath9k_hw_common(ah);
  621. u8 tx_gain_forced;
  622. tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  623. AR_PHY_TXGAIN_FORCE);
  624. if (tx_gain_forced)
  625. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  626. AR_PHY_TXGAIN_FORCE, 0);
  627. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
  628. AR_PHY_TX_IQCAL_START_DO_CAL, 1);
  629. if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
  630. AR_PHY_TX_IQCAL_START_DO_CAL, 0,
  631. AH_WAIT_TIMEOUT)) {
  632. ath_dbg(common, ATH_DBG_CALIBRATE,
  633. "Tx IQ Cal is not completed.\n");
  634. return false;
  635. }
  636. return true;
  637. }
  638. static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah, bool is_reusable)
  639. {
  640. struct ath_common *common = ath9k_hw_common(ah);
  641. const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
  642. AR_PHY_TX_IQCAL_STATUS_B0,
  643. AR_PHY_TX_IQCAL_STATUS_B1,
  644. AR_PHY_TX_IQCAL_STATUS_B2,
  645. };
  646. const u_int32_t chan_info_tab[] = {
  647. AR_PHY_CHAN_INFO_TAB_0,
  648. AR_PHY_CHAN_INFO_TAB_1,
  649. AR_PHY_CHAN_INFO_TAB_2,
  650. };
  651. struct coeff coeff;
  652. s32 iq_res[6];
  653. u8 num_chains = 0;
  654. int i, im, j;
  655. int nmeasurement;
  656. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  657. if (ah->txchainmask & (1 << i))
  658. num_chains++;
  659. }
  660. for (i = 0; i < num_chains; i++) {
  661. nmeasurement = REG_READ_FIELD(ah,
  662. AR_PHY_TX_IQCAL_STATUS_B0,
  663. AR_PHY_CALIBRATED_GAINS_0);
  664. if (nmeasurement > MAX_MEASUREMENT)
  665. nmeasurement = MAX_MEASUREMENT;
  666. for (im = 0; im < nmeasurement; im++) {
  667. ath_dbg(common, ATH_DBG_CALIBRATE,
  668. "Doing Tx IQ Cal for chain %d.\n", i);
  669. if (REG_READ(ah, txiqcal_status[i]) &
  670. AR_PHY_TX_IQCAL_STATUS_FAILED) {
  671. ath_dbg(common, ATH_DBG_CALIBRATE,
  672. "Tx IQ Cal failed for chain %d.\n", i);
  673. goto tx_iqcal_fail;
  674. }
  675. for (j = 0; j < 3; j++) {
  676. u32 idx = 2 * j, offset = 4 * (3 * im + j);
  677. REG_RMW_FIELD(ah,
  678. AR_PHY_CHAN_INFO_MEMORY,
  679. AR_PHY_CHAN_INFO_TAB_S2_READ,
  680. 0);
  681. /* 32 bits */
  682. iq_res[idx] = REG_READ(ah,
  683. chan_info_tab[i] +
  684. offset);
  685. REG_RMW_FIELD(ah,
  686. AR_PHY_CHAN_INFO_MEMORY,
  687. AR_PHY_CHAN_INFO_TAB_S2_READ,
  688. 1);
  689. /* 16 bits */
  690. iq_res[idx + 1] = 0xffff & REG_READ(ah,
  691. chan_info_tab[i] + offset);
  692. ath_dbg(common, ATH_DBG_CALIBRATE,
  693. "IQ RES[%d]=0x%x"
  694. "IQ_RES[%d]=0x%x\n",
  695. idx, iq_res[idx], idx + 1,
  696. iq_res[idx + 1]);
  697. }
  698. if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
  699. coeff.iqc_coeff)) {
  700. ath_dbg(common, ATH_DBG_CALIBRATE,
  701. "Failed in calculation of \
  702. IQ correction.\n");
  703. goto tx_iqcal_fail;
  704. }
  705. coeff.mag_coeff[i][im] = coeff.iqc_coeff[0] & 0x7f;
  706. coeff.phs_coeff[i][im] =
  707. (coeff.iqc_coeff[0] >> 7) & 0x7f;
  708. if (coeff.mag_coeff[i][im] > 63)
  709. coeff.mag_coeff[i][im] -= 128;
  710. if (coeff.phs_coeff[i][im] > 63)
  711. coeff.phs_coeff[i][im] -= 128;
  712. }
  713. }
  714. ar9003_hw_tx_iqcal_load_avg_2_passes(ah, num_chains,
  715. &coeff, is_reusable);
  716. return;
  717. tx_iqcal_fail:
  718. ath_dbg(common, ATH_DBG_CALIBRATE, "Tx IQ Cal failed\n");
  719. return;
  720. }
  721. static void ar9003_hw_tx_iq_cal_reload(struct ath_hw *ah)
  722. {
  723. struct ath9k_hw_cal_data *caldata = ah->caldata;
  724. u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
  725. int i, im;
  726. memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
  727. for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
  728. tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
  729. AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
  730. if (!AR_SREV_9485(ah)) {
  731. tx_corr_coeff[i * 2][1] =
  732. tx_corr_coeff[(i * 2) + 1][1] =
  733. AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
  734. tx_corr_coeff[i * 2][2] =
  735. tx_corr_coeff[(i * 2) + 1][2] =
  736. AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
  737. }
  738. }
  739. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  740. if (!(ah->txchainmask & (1 << i)))
  741. continue;
  742. for (im = 0; im < caldata->num_measures[i]; im++) {
  743. if ((im % 2) == 0)
  744. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  745. AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
  746. caldata->tx_corr_coeff[im][i]);
  747. else
  748. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  749. AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
  750. caldata->tx_corr_coeff[im][i]);
  751. }
  752. }
  753. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
  754. AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
  755. REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
  756. AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
  757. }
  758. static bool ar9003_hw_rtt_restore(struct ath_hw *ah, struct ath9k_channel *chan)
  759. {
  760. struct ath9k_rtt_hist *hist;
  761. u32 *table;
  762. int i;
  763. bool restore;
  764. if (!ah->caldata)
  765. return false;
  766. hist = &ah->caldata->rtt_hist;
  767. if (!hist->num_readings)
  768. return false;
  769. ar9003_hw_rtt_enable(ah);
  770. ar9003_hw_rtt_set_mask(ah, 0x00);
  771. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  772. if (!(ah->rxchainmask & (1 << i)))
  773. continue;
  774. table = &hist->table[i][hist->num_readings][0];
  775. ar9003_hw_rtt_load_hist(ah, i, table);
  776. }
  777. restore = ar9003_hw_rtt_force_restore(ah);
  778. ar9003_hw_rtt_disable(ah);
  779. return restore;
  780. }
  781. static bool ar9003_hw_init_cal(struct ath_hw *ah,
  782. struct ath9k_channel *chan)
  783. {
  784. struct ath_common *common = ath9k_hw_common(ah);
  785. struct ath9k_hw_cal_data *caldata = ah->caldata;
  786. bool txiqcal_done = false, txclcal_done = false;
  787. bool is_reusable = true, status = true;
  788. bool run_rtt_cal = false, run_agc_cal;
  789. bool rtt = !!(ah->caps.hw_caps & ATH9K_HW_CAP_RTT);
  790. u32 agc_ctrl = 0, agc_supp_cals = AR_PHY_AGC_CONTROL_OFFSET_CAL |
  791. AR_PHY_AGC_CONTROL_FLTR_CAL |
  792. AR_PHY_AGC_CONTROL_PKDET_CAL;
  793. int i, j;
  794. u32 cl_idx[AR9300_MAX_CHAINS] = { AR_PHY_CL_TAB_0,
  795. AR_PHY_CL_TAB_1,
  796. AR_PHY_CL_TAB_2 };
  797. if (rtt) {
  798. if (!ar9003_hw_rtt_restore(ah, chan))
  799. run_rtt_cal = true;
  800. ath_dbg(common, ATH_DBG_CALIBRATE, "RTT restore %s\n",
  801. run_rtt_cal ? "failed" : "succeed");
  802. }
  803. run_agc_cal = run_rtt_cal;
  804. if (run_rtt_cal) {
  805. ar9003_hw_rtt_enable(ah);
  806. ar9003_hw_rtt_set_mask(ah, 0x00);
  807. ar9003_hw_rtt_clear_hist(ah);
  808. }
  809. if (rtt && !run_rtt_cal) {
  810. agc_ctrl = REG_READ(ah, AR_PHY_AGC_CONTROL);
  811. agc_supp_cals &= agc_ctrl;
  812. agc_ctrl &= ~(AR_PHY_AGC_CONTROL_OFFSET_CAL |
  813. AR_PHY_AGC_CONTROL_FLTR_CAL |
  814. AR_PHY_AGC_CONTROL_PKDET_CAL);
  815. REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
  816. }
  817. if (ah->enabled_cals & TX_CL_CAL) {
  818. if (caldata && caldata->done_txclcal_once)
  819. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL,
  820. AR_PHY_CL_CAL_ENABLE);
  821. else {
  822. REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL,
  823. AR_PHY_CL_CAL_ENABLE);
  824. run_agc_cal = true;
  825. }
  826. }
  827. if (!(ah->enabled_cals & TX_IQ_CAL))
  828. goto skip_tx_iqcal;
  829. /* Do Tx IQ Calibration */
  830. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
  831. AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
  832. DELPT);
  833. /*
  834. * For AR9485 or later chips, TxIQ cal runs as part of
  835. * AGC calibration
  836. */
  837. if (ah->enabled_cals & TX_IQ_ON_AGC_CAL) {
  838. if (caldata && !caldata->done_txiqcal_once)
  839. REG_SET_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
  840. AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
  841. else
  842. REG_CLR_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
  843. AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
  844. txiqcal_done = run_agc_cal = true;
  845. goto skip_tx_iqcal;
  846. } else if (caldata && !caldata->done_txiqcal_once)
  847. run_agc_cal = true;
  848. txiqcal_done = ar9003_hw_tx_iq_cal_run(ah);
  849. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  850. udelay(5);
  851. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  852. skip_tx_iqcal:
  853. if (run_agc_cal || !(ah->ah_flags & AH_FASTCC)) {
  854. /* Calibrate the AGC */
  855. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  856. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  857. AR_PHY_AGC_CONTROL_CAL);
  858. /* Poll for offset calibration complete */
  859. status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
  860. AR_PHY_AGC_CONTROL_CAL,
  861. 0, AH_WAIT_TIMEOUT);
  862. }
  863. if (rtt && !run_rtt_cal) {
  864. agc_ctrl |= agc_supp_cals;
  865. REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
  866. }
  867. if (!status) {
  868. if (run_rtt_cal)
  869. ar9003_hw_rtt_disable(ah);
  870. ath_dbg(common, ATH_DBG_CALIBRATE,
  871. "offset calibration failed to complete in 1ms;"
  872. "noisy environment?\n");
  873. return false;
  874. }
  875. if (txiqcal_done)
  876. ar9003_hw_tx_iq_cal_post_proc(ah, is_reusable);
  877. else if (caldata && caldata->done_txiqcal_once)
  878. ar9003_hw_tx_iq_cal_reload(ah);
  879. #define CL_TAB_ENTRY(reg_base) (reg_base + (4 * j))
  880. if (caldata && (ah->enabled_cals & TX_CL_CAL)) {
  881. txclcal_done = !!(REG_READ(ah, AR_PHY_AGC_CONTROL) &
  882. AR_PHY_AGC_CONTROL_CLC_SUCCESS);
  883. if (caldata->done_txclcal_once) {
  884. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  885. if (!(ah->txchainmask & (1 << i)))
  886. continue;
  887. for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
  888. REG_WRITE(ah, CL_TAB_ENTRY(cl_idx[i]),
  889. caldata->tx_clcal[i][j]);
  890. }
  891. } else if (is_reusable && txclcal_done) {
  892. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  893. if (!(ah->txchainmask & (1 << i)))
  894. continue;
  895. for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
  896. caldata->tx_clcal[i][j] =
  897. REG_READ(ah,
  898. CL_TAB_ENTRY(cl_idx[i]));
  899. }
  900. caldata->done_txclcal_once = true;
  901. }
  902. }
  903. #undef CL_TAB_ENTRY
  904. if (run_rtt_cal && caldata) {
  905. struct ath9k_rtt_hist *hist = &caldata->rtt_hist;
  906. if (is_reusable && (hist->num_readings < RTT_HIST_MAX)) {
  907. u32 *table;
  908. hist->num_readings++;
  909. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  910. if (!(ah->rxchainmask & (1 << i)))
  911. continue;
  912. table = &hist->table[i][hist->num_readings][0];
  913. ar9003_hw_rtt_fill_hist(ah, i, table);
  914. }
  915. }
  916. ar9003_hw_rtt_disable(ah);
  917. }
  918. /* Initialize list pointers */
  919. ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
  920. ah->supp_cals = IQ_MISMATCH_CAL;
  921. if (ah->supp_cals & IQ_MISMATCH_CAL) {
  922. INIT_CAL(&ah->iq_caldata);
  923. INSERT_CAL(ah, &ah->iq_caldata);
  924. ath_dbg(common, ATH_DBG_CALIBRATE,
  925. "enabling IQ Calibration.\n");
  926. }
  927. if (ah->supp_cals & TEMP_COMP_CAL) {
  928. INIT_CAL(&ah->tempCompCalData);
  929. INSERT_CAL(ah, &ah->tempCompCalData);
  930. ath_dbg(common, ATH_DBG_CALIBRATE,
  931. "enabling Temperature Compensation Calibration.\n");
  932. }
  933. /* Initialize current pointer to first element in list */
  934. ah->cal_list_curr = ah->cal_list;
  935. if (ah->cal_list_curr)
  936. ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
  937. if (caldata)
  938. caldata->CalValid = 0;
  939. return true;
  940. }
  941. void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
  942. {
  943. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  944. struct ath_hw_ops *ops = ath9k_hw_ops(ah);
  945. priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
  946. priv_ops->init_cal = ar9003_hw_init_cal;
  947. priv_ops->setup_calibration = ar9003_hw_setup_calibration;
  948. ops->calibrate = ar9003_hw_calibrate;
  949. }