ar9003_calib.c 30 KB

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