ar9003_calib.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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. #define MAXIQCAL 3
  25. struct coeff {
  26. int mag_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT][MAXIQCAL];
  27. int phs_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT][MAXIQCAL];
  28. int iqc_coeff[2];
  29. };
  30. enum ar9003_cal_types {
  31. IQ_MISMATCH_CAL = BIT(0),
  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. default:
  54. ath_err(common, "Invalid calibration type\n");
  55. break;
  56. }
  57. }
  58. /*
  59. * Generic calibration routine.
  60. * Recalibrate the lower PHY chips to account for temperature/environment
  61. * changes.
  62. */
  63. static bool ar9003_hw_per_calibration(struct ath_hw *ah,
  64. struct ath9k_channel *ichan,
  65. u8 rxchainmask,
  66. struct ath9k_cal_list *currCal)
  67. {
  68. struct ath9k_hw_cal_data *caldata = ah->caldata;
  69. const struct ath9k_percal_data *cur_caldata = currCal->calData;
  70. /* Calibration in progress. */
  71. if (currCal->calState == CAL_RUNNING) {
  72. /* Check to see if it has finished. */
  73. if (REG_READ(ah, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)
  74. return false;
  75. /*
  76. * Accumulate cal measures for active chains
  77. */
  78. cur_caldata->calCollect(ah);
  79. ah->cal_samples++;
  80. if (ah->cal_samples >= cur_caldata->calNumSamples) {
  81. unsigned int i, numChains = 0;
  82. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  83. if (rxchainmask & (1 << i))
  84. numChains++;
  85. }
  86. /*
  87. * Process accumulated data
  88. */
  89. cur_caldata->calPostProc(ah, numChains);
  90. /* Calibration has finished. */
  91. caldata->CalValid |= cur_caldata->calType;
  92. currCal->calState = CAL_DONE;
  93. return true;
  94. } else {
  95. /*
  96. * Set-up collection of another sub-sample until we
  97. * get desired number
  98. */
  99. ar9003_hw_setup_calibration(ah, currCal);
  100. }
  101. } else if (!(caldata->CalValid & cur_caldata->calType)) {
  102. /* If current cal is marked invalid in channel, kick it off */
  103. ath9k_hw_reset_calibration(ah, currCal);
  104. }
  105. return false;
  106. }
  107. static int ar9003_hw_calibrate(struct ath_hw *ah, struct ath9k_channel *chan,
  108. u8 rxchainmask, bool longcal)
  109. {
  110. bool iscaldone = true;
  111. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  112. int ret;
  113. /*
  114. * For given calibration:
  115. * 1. Call generic cal routine
  116. * 2. When this cal is done (isCalDone) if we have more cals waiting
  117. * (eg after reset), mask this to upper layers by not propagating
  118. * isCalDone if it is set to TRUE.
  119. * Instead, change isCalDone to FALSE and setup the waiting cal(s)
  120. * to be run.
  121. */
  122. if (currCal &&
  123. (currCal->calState == CAL_RUNNING ||
  124. currCal->calState == CAL_WAITING)) {
  125. iscaldone = ar9003_hw_per_calibration(ah, chan,
  126. rxchainmask, currCal);
  127. if (iscaldone) {
  128. ah->cal_list_curr = currCal = currCal->calNext;
  129. if (currCal->calState == CAL_WAITING) {
  130. iscaldone = false;
  131. ath9k_hw_reset_calibration(ah, currCal);
  132. }
  133. }
  134. }
  135. /*
  136. * Do NF cal only at longer intervals. Get the value from
  137. * the previous NF cal and update history buffer.
  138. */
  139. if (longcal && 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. ret = ath9k_hw_loadnf(ah, ah->curchan);
  146. if (ret < 0)
  147. return ret;
  148. /* start NF calibration, without updating BB NF register */
  149. ath9k_hw_start_nfcal(ah, false);
  150. }
  151. return iscaldone;
  152. }
  153. static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
  154. {
  155. int i;
  156. /* Accumulate IQ cal measures for active chains */
  157. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  158. if (ah->txchainmask & BIT(i)) {
  159. ah->totalPowerMeasI[i] +=
  160. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  161. ah->totalPowerMeasQ[i] +=
  162. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  163. ah->totalIqCorrMeas[i] +=
  164. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  165. ath_dbg(ath9k_hw_common(ah), CALIBRATE,
  166. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  167. ah->cal_samples, i, ah->totalPowerMeasI[i],
  168. ah->totalPowerMeasQ[i],
  169. ah->totalIqCorrMeas[i]);
  170. }
  171. }
  172. }
  173. static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
  174. {
  175. struct ath_common *common = ath9k_hw_common(ah);
  176. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  177. u32 qCoffDenom, iCoffDenom;
  178. int32_t qCoff, iCoff;
  179. int iqCorrNeg, i;
  180. static const u_int32_t offset_array[3] = {
  181. AR_PHY_RX_IQCAL_CORR_B0,
  182. AR_PHY_RX_IQCAL_CORR_B1,
  183. AR_PHY_RX_IQCAL_CORR_B2,
  184. };
  185. for (i = 0; i < numChains; i++) {
  186. powerMeasI = ah->totalPowerMeasI[i];
  187. powerMeasQ = ah->totalPowerMeasQ[i];
  188. iqCorrMeas = ah->totalIqCorrMeas[i];
  189. ath_dbg(common, CALIBRATE,
  190. "Starting IQ Cal and Correction for Chain %d\n", i);
  191. ath_dbg(common, CALIBRATE,
  192. "Original: Chn %d iq_corr_meas = 0x%08x\n",
  193. i, ah->totalIqCorrMeas[i]);
  194. iqCorrNeg = 0;
  195. if (iqCorrMeas > 0x80000000) {
  196. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  197. iqCorrNeg = 1;
  198. }
  199. ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_i = 0x%08x\n",
  200. i, powerMeasI);
  201. ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_q = 0x%08x\n",
  202. i, powerMeasQ);
  203. ath_dbg(common, CALIBRATE, "iqCorrNeg is 0x%08x\n", iqCorrNeg);
  204. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
  205. qCoffDenom = powerMeasQ / 64;
  206. if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
  207. iCoff = iqCorrMeas / iCoffDenom;
  208. qCoff = powerMeasI / qCoffDenom - 64;
  209. ath_dbg(common, CALIBRATE, "Chn %d iCoff = 0x%08x\n",
  210. i, iCoff);
  211. ath_dbg(common, CALIBRATE, "Chn %d qCoff = 0x%08x\n",
  212. i, qCoff);
  213. /* Force bounds on iCoff */
  214. if (iCoff >= 63)
  215. iCoff = 63;
  216. else if (iCoff <= -63)
  217. iCoff = -63;
  218. /* Negate iCoff if iqCorrNeg == 0 */
  219. if (iqCorrNeg == 0x0)
  220. iCoff = -iCoff;
  221. /* Force bounds on qCoff */
  222. if (qCoff >= 63)
  223. qCoff = 63;
  224. else if (qCoff <= -63)
  225. qCoff = -63;
  226. iCoff = iCoff & 0x7f;
  227. qCoff = qCoff & 0x7f;
  228. ath_dbg(common, CALIBRATE,
  229. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  230. i, iCoff, qCoff);
  231. ath_dbg(common, CALIBRATE,
  232. "Register offset (0x%04x) before update = 0x%x\n",
  233. offset_array[i],
  234. REG_READ(ah, offset_array[i]));
  235. if (AR_SREV_9565(ah) &&
  236. (iCoff == 63 || qCoff == 63 ||
  237. iCoff == -63 || qCoff == -63))
  238. return;
  239. REG_RMW_FIELD(ah, offset_array[i],
  240. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  241. iCoff);
  242. REG_RMW_FIELD(ah, offset_array[i],
  243. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  244. qCoff);
  245. ath_dbg(common, CALIBRATE,
  246. "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
  247. offset_array[i],
  248. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
  249. REG_READ(ah, offset_array[i]));
  250. ath_dbg(common, CALIBRATE,
  251. "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
  252. offset_array[i],
  253. AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
  254. REG_READ(ah, offset_array[i]));
  255. ath_dbg(common, CALIBRATE,
  256. "IQ Cal and Correction done for Chain %d\n", i);
  257. }
  258. }
  259. REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
  260. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
  261. ath_dbg(common, CALIBRATE,
  262. "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
  263. (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
  264. AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
  265. REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
  266. }
  267. static const struct ath9k_percal_data iq_cal_single_sample = {
  268. IQ_MISMATCH_CAL,
  269. MIN_CAL_SAMPLES,
  270. PER_MAX_LOG_COUNT,
  271. ar9003_hw_iqcal_collect,
  272. ar9003_hw_iqcalibrate
  273. };
  274. static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
  275. {
  276. ah->iq_caldata.calData = &iq_cal_single_sample;
  277. if (AR_SREV_9300_20_OR_LATER(ah)) {
  278. ah->enabled_cals |= TX_IQ_CAL;
  279. if (AR_SREV_9485_OR_LATER(ah) && !AR_SREV_9340(ah))
  280. ah->enabled_cals |= TX_IQ_ON_AGC_CAL;
  281. }
  282. ah->supp_cals = IQ_MISMATCH_CAL;
  283. }
  284. #define OFF_UPPER_LT 24
  285. #define OFF_LOWER_LT 7
  286. static bool ar9003_hw_dynamic_osdac_selection(struct ath_hw *ah,
  287. bool txiqcal_done)
  288. {
  289. struct ath_common *common = ath9k_hw_common(ah);
  290. int ch0_done, osdac_ch0, dc_off_ch0_i1, dc_off_ch0_q1, dc_off_ch0_i2,
  291. dc_off_ch0_q2, dc_off_ch0_i3, dc_off_ch0_q3;
  292. int ch1_done, osdac_ch1, dc_off_ch1_i1, dc_off_ch1_q1, dc_off_ch1_i2,
  293. dc_off_ch1_q2, dc_off_ch1_i3, dc_off_ch1_q3;
  294. int ch2_done, osdac_ch2, dc_off_ch2_i1, dc_off_ch2_q1, dc_off_ch2_i2,
  295. dc_off_ch2_q2, dc_off_ch2_i3, dc_off_ch2_q3;
  296. bool status;
  297. u32 temp, val;
  298. /*
  299. * Clear offset and IQ calibration, run AGC cal.
  300. */
  301. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  302. AR_PHY_AGC_CONTROL_OFFSET_CAL);
  303. REG_CLR_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
  304. AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
  305. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  306. REG_READ(ah, AR_PHY_AGC_CONTROL) | AR_PHY_AGC_CONTROL_CAL);
  307. status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
  308. AR_PHY_AGC_CONTROL_CAL,
  309. 0, AH_WAIT_TIMEOUT);
  310. if (!status) {
  311. ath_dbg(common, CALIBRATE,
  312. "AGC cal without offset cal failed to complete in 1ms");
  313. return false;
  314. }
  315. /*
  316. * Allow only offset calibration and disable the others
  317. * (Carrier Leak calibration, TX Filter calibration and
  318. * Peak Detector offset calibration).
  319. */
  320. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  321. AR_PHY_AGC_CONTROL_OFFSET_CAL);
  322. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL,
  323. AR_PHY_CL_CAL_ENABLE);
  324. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  325. AR_PHY_AGC_CONTROL_FLTR_CAL);
  326. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  327. AR_PHY_AGC_CONTROL_PKDET_CAL);
  328. ch0_done = 0;
  329. ch1_done = 0;
  330. ch2_done = 0;
  331. while ((ch0_done == 0) || (ch1_done == 0) || (ch2_done == 0)) {
  332. osdac_ch0 = (REG_READ(ah, AR_PHY_65NM_CH0_BB1) >> 30) & 0x3;
  333. osdac_ch1 = (REG_READ(ah, AR_PHY_65NM_CH1_BB1) >> 30) & 0x3;
  334. osdac_ch2 = (REG_READ(ah, AR_PHY_65NM_CH2_BB1) >> 30) & 0x3;
  335. REG_SET_BIT(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  336. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  337. REG_READ(ah, AR_PHY_AGC_CONTROL) | AR_PHY_AGC_CONTROL_CAL);
  338. status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
  339. AR_PHY_AGC_CONTROL_CAL,
  340. 0, AH_WAIT_TIMEOUT);
  341. if (!status) {
  342. ath_dbg(common, CALIBRATE,
  343. "DC offset cal failed to complete in 1ms");
  344. return false;
  345. }
  346. REG_CLR_BIT(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  347. /*
  348. * High gain.
  349. */
  350. REG_WRITE(ah, AR_PHY_65NM_CH0_BB3,
  351. ((REG_READ(ah, AR_PHY_65NM_CH0_BB3) & 0xfffffcff) | (1 << 8)));
  352. REG_WRITE(ah, AR_PHY_65NM_CH1_BB3,
  353. ((REG_READ(ah, AR_PHY_65NM_CH1_BB3) & 0xfffffcff) | (1 << 8)));
  354. REG_WRITE(ah, AR_PHY_65NM_CH2_BB3,
  355. ((REG_READ(ah, AR_PHY_65NM_CH2_BB3) & 0xfffffcff) | (1 << 8)));
  356. temp = REG_READ(ah, AR_PHY_65NM_CH0_BB3);
  357. dc_off_ch0_i1 = (temp >> 26) & 0x1f;
  358. dc_off_ch0_q1 = (temp >> 21) & 0x1f;
  359. temp = REG_READ(ah, AR_PHY_65NM_CH1_BB3);
  360. dc_off_ch1_i1 = (temp >> 26) & 0x1f;
  361. dc_off_ch1_q1 = (temp >> 21) & 0x1f;
  362. temp = REG_READ(ah, AR_PHY_65NM_CH2_BB3);
  363. dc_off_ch2_i1 = (temp >> 26) & 0x1f;
  364. dc_off_ch2_q1 = (temp >> 21) & 0x1f;
  365. /*
  366. * Low gain.
  367. */
  368. REG_WRITE(ah, AR_PHY_65NM_CH0_BB3,
  369. ((REG_READ(ah, AR_PHY_65NM_CH0_BB3) & 0xfffffcff) | (2 << 8)));
  370. REG_WRITE(ah, AR_PHY_65NM_CH1_BB3,
  371. ((REG_READ(ah, AR_PHY_65NM_CH1_BB3) & 0xfffffcff) | (2 << 8)));
  372. REG_WRITE(ah, AR_PHY_65NM_CH2_BB3,
  373. ((REG_READ(ah, AR_PHY_65NM_CH2_BB3) & 0xfffffcff) | (2 << 8)));
  374. temp = REG_READ(ah, AR_PHY_65NM_CH0_BB3);
  375. dc_off_ch0_i2 = (temp >> 26) & 0x1f;
  376. dc_off_ch0_q2 = (temp >> 21) & 0x1f;
  377. temp = REG_READ(ah, AR_PHY_65NM_CH1_BB3);
  378. dc_off_ch1_i2 = (temp >> 26) & 0x1f;
  379. dc_off_ch1_q2 = (temp >> 21) & 0x1f;
  380. temp = REG_READ(ah, AR_PHY_65NM_CH2_BB3);
  381. dc_off_ch2_i2 = (temp >> 26) & 0x1f;
  382. dc_off_ch2_q2 = (temp >> 21) & 0x1f;
  383. /*
  384. * Loopback.
  385. */
  386. REG_WRITE(ah, AR_PHY_65NM_CH0_BB3,
  387. ((REG_READ(ah, AR_PHY_65NM_CH0_BB3) & 0xfffffcff) | (3 << 8)));
  388. REG_WRITE(ah, AR_PHY_65NM_CH1_BB3,
  389. ((REG_READ(ah, AR_PHY_65NM_CH1_BB3) & 0xfffffcff) | (3 << 8)));
  390. REG_WRITE(ah, AR_PHY_65NM_CH2_BB3,
  391. ((REG_READ(ah, AR_PHY_65NM_CH2_BB3) & 0xfffffcff) | (3 << 8)));
  392. temp = REG_READ(ah, AR_PHY_65NM_CH0_BB3);
  393. dc_off_ch0_i3 = (temp >> 26) & 0x1f;
  394. dc_off_ch0_q3 = (temp >> 21) & 0x1f;
  395. temp = REG_READ(ah, AR_PHY_65NM_CH1_BB3);
  396. dc_off_ch1_i3 = (temp >> 26) & 0x1f;
  397. dc_off_ch1_q3 = (temp >> 21) & 0x1f;
  398. temp = REG_READ(ah, AR_PHY_65NM_CH2_BB3);
  399. dc_off_ch2_i3 = (temp >> 26) & 0x1f;
  400. dc_off_ch2_q3 = (temp >> 21) & 0x1f;
  401. if ((dc_off_ch0_i1 > OFF_UPPER_LT) || (dc_off_ch0_i1 < OFF_LOWER_LT) ||
  402. (dc_off_ch0_i2 > OFF_UPPER_LT) || (dc_off_ch0_i2 < OFF_LOWER_LT) ||
  403. (dc_off_ch0_i3 > OFF_UPPER_LT) || (dc_off_ch0_i3 < OFF_LOWER_LT) ||
  404. (dc_off_ch0_q1 > OFF_UPPER_LT) || (dc_off_ch0_q1 < OFF_LOWER_LT) ||
  405. (dc_off_ch0_q2 > OFF_UPPER_LT) || (dc_off_ch0_q2 < OFF_LOWER_LT) ||
  406. (dc_off_ch0_q3 > OFF_UPPER_LT) || (dc_off_ch0_q3 < OFF_LOWER_LT)) {
  407. if (osdac_ch0 == 3) {
  408. ch0_done = 1;
  409. } else {
  410. osdac_ch0++;
  411. val = REG_READ(ah, AR_PHY_65NM_CH0_BB1) & 0x3fffffff;
  412. val |= (osdac_ch0 << 30);
  413. REG_WRITE(ah, AR_PHY_65NM_CH0_BB1, val);
  414. ch0_done = 0;
  415. }
  416. } else {
  417. ch0_done = 1;
  418. }
  419. if ((dc_off_ch1_i1 > OFF_UPPER_LT) || (dc_off_ch1_i1 < OFF_LOWER_LT) ||
  420. (dc_off_ch1_i2 > OFF_UPPER_LT) || (dc_off_ch1_i2 < OFF_LOWER_LT) ||
  421. (dc_off_ch1_i3 > OFF_UPPER_LT) || (dc_off_ch1_i3 < OFF_LOWER_LT) ||
  422. (dc_off_ch1_q1 > OFF_UPPER_LT) || (dc_off_ch1_q1 < OFF_LOWER_LT) ||
  423. (dc_off_ch1_q2 > OFF_UPPER_LT) || (dc_off_ch1_q2 < OFF_LOWER_LT) ||
  424. (dc_off_ch1_q3 > OFF_UPPER_LT) || (dc_off_ch1_q3 < OFF_LOWER_LT)) {
  425. if (osdac_ch1 == 3) {
  426. ch1_done = 1;
  427. } else {
  428. osdac_ch1++;
  429. val = REG_READ(ah, AR_PHY_65NM_CH1_BB1) & 0x3fffffff;
  430. val |= (osdac_ch1 << 30);
  431. REG_WRITE(ah, AR_PHY_65NM_CH1_BB1, val);
  432. ch1_done = 0;
  433. }
  434. } else {
  435. ch1_done = 1;
  436. }
  437. if ((dc_off_ch2_i1 > OFF_UPPER_LT) || (dc_off_ch2_i1 < OFF_LOWER_LT) ||
  438. (dc_off_ch2_i2 > OFF_UPPER_LT) || (dc_off_ch2_i2 < OFF_LOWER_LT) ||
  439. (dc_off_ch2_i3 > OFF_UPPER_LT) || (dc_off_ch2_i3 < OFF_LOWER_LT) ||
  440. (dc_off_ch2_q1 > OFF_UPPER_LT) || (dc_off_ch2_q1 < OFF_LOWER_LT) ||
  441. (dc_off_ch2_q2 > OFF_UPPER_LT) || (dc_off_ch2_q2 < OFF_LOWER_LT) ||
  442. (dc_off_ch2_q3 > OFF_UPPER_LT) || (dc_off_ch2_q3 < OFF_LOWER_LT)) {
  443. if (osdac_ch2 == 3) {
  444. ch2_done = 1;
  445. } else {
  446. osdac_ch2++;
  447. val = REG_READ(ah, AR_PHY_65NM_CH2_BB1) & 0x3fffffff;
  448. val |= (osdac_ch2 << 30);
  449. REG_WRITE(ah, AR_PHY_65NM_CH2_BB1, val);
  450. ch2_done = 0;
  451. }
  452. } else {
  453. ch2_done = 1;
  454. }
  455. }
  456. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  457. AR_PHY_AGC_CONTROL_OFFSET_CAL);
  458. REG_SET_BIT(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  459. /*
  460. * We don't need to check txiqcal_done here since it is always
  461. * set for AR9550.
  462. */
  463. REG_SET_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
  464. AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
  465. return true;
  466. }
  467. /*
  468. * solve 4x4 linear equation used in loopback iq cal.
  469. */
  470. static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
  471. s32 sin_2phi_1,
  472. s32 cos_2phi_1,
  473. s32 sin_2phi_2,
  474. s32 cos_2phi_2,
  475. s32 mag_a0_d0,
  476. s32 phs_a0_d0,
  477. s32 mag_a1_d0,
  478. s32 phs_a1_d0,
  479. s32 solved_eq[])
  480. {
  481. s32 f1 = cos_2phi_1 - cos_2phi_2,
  482. f3 = sin_2phi_1 - sin_2phi_2,
  483. f2;
  484. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  485. const s32 result_shift = 1 << 15;
  486. struct ath_common *common = ath9k_hw_common(ah);
  487. f2 = ((f1 >> 3) * (f1 >> 3) + (f3 >> 3) * (f3 >> 3)) >> 9;
  488. if (!f2) {
  489. ath_dbg(common, CALIBRATE, "Divide by 0\n");
  490. return false;
  491. }
  492. /* mag mismatch, tx */
  493. mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
  494. /* phs mismatch, tx */
  495. phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
  496. mag_tx = (mag_tx / f2);
  497. phs_tx = (phs_tx / f2);
  498. /* mag mismatch, rx */
  499. mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
  500. result_shift;
  501. /* phs mismatch, rx */
  502. phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
  503. result_shift;
  504. solved_eq[0] = mag_tx;
  505. solved_eq[1] = phs_tx;
  506. solved_eq[2] = mag_rx;
  507. solved_eq[3] = phs_rx;
  508. return true;
  509. }
  510. static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
  511. {
  512. s32 abs_i = abs(in_re),
  513. abs_q = abs(in_im),
  514. max_abs, min_abs;
  515. if (abs_i > abs_q) {
  516. max_abs = abs_i;
  517. min_abs = abs_q;
  518. } else {
  519. max_abs = abs_q;
  520. min_abs = abs_i;
  521. }
  522. return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
  523. }
  524. #define DELPT 32
  525. static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
  526. s32 chain_idx,
  527. const s32 iq_res[],
  528. s32 iqc_coeff[])
  529. {
  530. s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
  531. i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
  532. i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
  533. i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
  534. s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
  535. phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
  536. sin_2phi_1, cos_2phi_1,
  537. sin_2phi_2, cos_2phi_2;
  538. s32 mag_tx, phs_tx, mag_rx, phs_rx;
  539. s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
  540. q_q_coff, q_i_coff;
  541. const s32 res_scale = 1 << 15;
  542. const s32 delpt_shift = 1 << 8;
  543. s32 mag1, mag2;
  544. struct ath_common *common = ath9k_hw_common(ah);
  545. i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
  546. i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
  547. iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
  548. if (i2_m_q2_a0_d0 > 0x800)
  549. i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
  550. if (i2_p_q2_a0_d0 > 0x800)
  551. i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
  552. if (iq_corr_a0_d0 > 0x800)
  553. iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
  554. i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
  555. i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
  556. iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
  557. if (i2_m_q2_a0_d1 > 0x800)
  558. i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
  559. if (iq_corr_a0_d1 > 0x800)
  560. iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
  561. i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
  562. i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
  563. iq_corr_a1_d0 = iq_res[4] & 0xfff;
  564. if (i2_m_q2_a1_d0 > 0x800)
  565. i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
  566. if (i2_p_q2_a1_d0 > 0x800)
  567. i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
  568. if (iq_corr_a1_d0 > 0x800)
  569. iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
  570. i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
  571. i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
  572. iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
  573. if (i2_m_q2_a1_d1 > 0x800)
  574. i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
  575. if (i2_p_q2_a1_d1 > 0x800)
  576. i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
  577. if (iq_corr_a1_d1 > 0x800)
  578. iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
  579. if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
  580. (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
  581. ath_dbg(common, CALIBRATE,
  582. "Divide by 0:\n"
  583. "a0_d0=%d\n"
  584. "a0_d1=%d\n"
  585. "a2_d0=%d\n"
  586. "a1_d1=%d\n",
  587. i2_p_q2_a0_d0, i2_p_q2_a0_d1,
  588. i2_p_q2_a1_d0, i2_p_q2_a1_d1);
  589. return false;
  590. }
  591. if ((i2_p_q2_a0_d0 < 1024) || (i2_p_q2_a0_d0 > 2047) ||
  592. (i2_p_q2_a1_d0 < 0) || (i2_p_q2_a1_d1 < 0) ||
  593. (i2_p_q2_a0_d0 <= i2_m_q2_a0_d0) ||
  594. (i2_p_q2_a0_d0 <= iq_corr_a0_d0) ||
  595. (i2_p_q2_a0_d1 <= i2_m_q2_a0_d1) ||
  596. (i2_p_q2_a0_d1 <= iq_corr_a0_d1) ||
  597. (i2_p_q2_a1_d0 <= i2_m_q2_a1_d0) ||
  598. (i2_p_q2_a1_d0 <= iq_corr_a1_d0) ||
  599. (i2_p_q2_a1_d1 <= i2_m_q2_a1_d1) ||
  600. (i2_p_q2_a1_d1 <= iq_corr_a1_d1)) {
  601. return false;
  602. }
  603. mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  604. phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
  605. mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  606. phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
  607. mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  608. phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
  609. mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  610. phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
  611. /* w/o analog phase shift */
  612. sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
  613. /* w/o analog phase shift */
  614. cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
  615. /* w/ analog phase shift */
  616. sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
  617. /* w/ analog phase shift */
  618. cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
  619. /*
  620. * force sin^2 + cos^2 = 1;
  621. * find magnitude by approximation
  622. */
  623. mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
  624. mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
  625. if ((mag1 == 0) || (mag2 == 0)) {
  626. ath_dbg(common, CALIBRATE, "Divide by 0: mag1=%d, mag2=%d\n",
  627. mag1, mag2);
  628. return false;
  629. }
  630. /* normalization sin and cos by mag */
  631. sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
  632. cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
  633. sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
  634. cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
  635. /* calculate IQ mismatch */
  636. if (!ar9003_hw_solve_iq_cal(ah,
  637. sin_2phi_1, cos_2phi_1,
  638. sin_2phi_2, cos_2phi_2,
  639. mag_a0_d0, phs_a0_d0,
  640. mag_a1_d0,
  641. phs_a1_d0, solved_eq)) {
  642. ath_dbg(common, CALIBRATE,
  643. "Call to ar9003_hw_solve_iq_cal() failed\n");
  644. return false;
  645. }
  646. mag_tx = solved_eq[0];
  647. phs_tx = solved_eq[1];
  648. mag_rx = solved_eq[2];
  649. phs_rx = solved_eq[3];
  650. ath_dbg(common, CALIBRATE,
  651. "chain %d: mag mismatch=%d phase mismatch=%d\n",
  652. chain_idx, mag_tx/res_scale, phs_tx/res_scale);
  653. if (res_scale == mag_tx) {
  654. ath_dbg(common, CALIBRATE,
  655. "Divide by 0: mag_tx=%d, res_scale=%d\n",
  656. mag_tx, res_scale);
  657. return false;
  658. }
  659. /* calculate and quantize Tx IQ correction factor */
  660. mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
  661. phs_corr_tx = -phs_tx;
  662. q_q_coff = (mag_corr_tx * 128 / res_scale);
  663. q_i_coff = (phs_corr_tx * 256 / res_scale);
  664. ath_dbg(common, CALIBRATE, "tx chain %d: mag corr=%d phase corr=%d\n",
  665. chain_idx, q_q_coff, q_i_coff);
  666. if (q_i_coff < -63)
  667. q_i_coff = -63;
  668. if (q_i_coff > 63)
  669. q_i_coff = 63;
  670. if (q_q_coff < -63)
  671. q_q_coff = -63;
  672. if (q_q_coff > 63)
  673. q_q_coff = 63;
  674. iqc_coeff[0] = (q_q_coff * 128) + (0x7f & q_i_coff);
  675. ath_dbg(common, CALIBRATE, "tx chain %d: iq corr coeff=%x\n",
  676. chain_idx, iqc_coeff[0]);
  677. if (-mag_rx == res_scale) {
  678. ath_dbg(common, CALIBRATE,
  679. "Divide by 0: mag_rx=%d, res_scale=%d\n",
  680. mag_rx, res_scale);
  681. return false;
  682. }
  683. /* calculate and quantize Rx IQ correction factors */
  684. mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
  685. phs_corr_rx = -phs_rx;
  686. q_q_coff = (mag_corr_rx * 128 / res_scale);
  687. q_i_coff = (phs_corr_rx * 256 / res_scale);
  688. ath_dbg(common, CALIBRATE, "rx chain %d: mag corr=%d phase corr=%d\n",
  689. chain_idx, q_q_coff, q_i_coff);
  690. if (q_i_coff < -63)
  691. q_i_coff = -63;
  692. if (q_i_coff > 63)
  693. q_i_coff = 63;
  694. if (q_q_coff < -63)
  695. q_q_coff = -63;
  696. if (q_q_coff > 63)
  697. q_q_coff = 63;
  698. iqc_coeff[1] = (q_q_coff * 128) + (0x7f & q_i_coff);
  699. ath_dbg(common, CALIBRATE, "rx chain %d: iq corr coeff=%x\n",
  700. chain_idx, iqc_coeff[1]);
  701. return true;
  702. }
  703. static void ar9003_hw_detect_outlier(int mp_coeff[][MAXIQCAL],
  704. int nmeasurement,
  705. int max_delta)
  706. {
  707. int mp_max = -64, max_idx = 0;
  708. int mp_min = 63, min_idx = 0;
  709. int mp_avg = 0, i, outlier_idx = 0, mp_count = 0;
  710. /* find min/max mismatch across all calibrated gains */
  711. for (i = 0; i < nmeasurement; i++) {
  712. if (mp_coeff[i][0] > mp_max) {
  713. mp_max = mp_coeff[i][0];
  714. max_idx = i;
  715. } else if (mp_coeff[i][0] < mp_min) {
  716. mp_min = mp_coeff[i][0];
  717. min_idx = i;
  718. }
  719. }
  720. /* find average (exclude max abs value) */
  721. for (i = 0; i < nmeasurement; i++) {
  722. if ((abs(mp_coeff[i][0]) < abs(mp_max)) ||
  723. (abs(mp_coeff[i][0]) < abs(mp_min))) {
  724. mp_avg += mp_coeff[i][0];
  725. mp_count++;
  726. }
  727. }
  728. /*
  729. * finding mean magnitude/phase if possible, otherwise
  730. * just use the last value as the mean
  731. */
  732. if (mp_count)
  733. mp_avg /= mp_count;
  734. else
  735. mp_avg = mp_coeff[nmeasurement - 1][0];
  736. /* detect outlier */
  737. if (abs(mp_max - mp_min) > max_delta) {
  738. if (abs(mp_max - mp_avg) > abs(mp_min - mp_avg))
  739. outlier_idx = max_idx;
  740. else
  741. outlier_idx = min_idx;
  742. mp_coeff[outlier_idx][0] = mp_avg;
  743. }
  744. }
  745. static void ar9003_hw_tx_iq_cal_outlier_detection(struct ath_hw *ah,
  746. struct coeff *coeff,
  747. bool is_reusable)
  748. {
  749. int i, im, nmeasurement;
  750. int magnitude, phase;
  751. u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
  752. struct ath9k_hw_cal_data *caldata = ah->caldata;
  753. memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
  754. for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
  755. tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
  756. AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
  757. if (!AR_SREV_9485(ah)) {
  758. tx_corr_coeff[i * 2][1] =
  759. tx_corr_coeff[(i * 2) + 1][1] =
  760. AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
  761. tx_corr_coeff[i * 2][2] =
  762. tx_corr_coeff[(i * 2) + 1][2] =
  763. AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
  764. }
  765. }
  766. /* Load the average of 2 passes */
  767. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  768. if (!(ah->txchainmask & (1 << i)))
  769. continue;
  770. nmeasurement = REG_READ_FIELD(ah,
  771. AR_PHY_TX_IQCAL_STATUS_B0,
  772. AR_PHY_CALIBRATED_GAINS_0);
  773. if (nmeasurement > MAX_MEASUREMENT)
  774. nmeasurement = MAX_MEASUREMENT;
  775. /*
  776. * Skip normal outlier detection for AR9550.
  777. */
  778. if (!AR_SREV_9550(ah)) {
  779. /* detect outlier only if nmeasurement > 1 */
  780. if (nmeasurement > 1) {
  781. /* Detect magnitude outlier */
  782. ar9003_hw_detect_outlier(coeff->mag_coeff[i],
  783. nmeasurement,
  784. MAX_MAG_DELTA);
  785. /* Detect phase outlier */
  786. ar9003_hw_detect_outlier(coeff->phs_coeff[i],
  787. nmeasurement,
  788. MAX_PHS_DELTA);
  789. }
  790. }
  791. for (im = 0; im < nmeasurement; im++) {
  792. magnitude = coeff->mag_coeff[i][im][0];
  793. phase = coeff->phs_coeff[i][im][0];
  794. coeff->iqc_coeff[0] =
  795. (phase & 0x7f) | ((magnitude & 0x7f) << 7);
  796. if ((im % 2) == 0)
  797. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  798. AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
  799. coeff->iqc_coeff[0]);
  800. else
  801. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  802. AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
  803. coeff->iqc_coeff[0]);
  804. if (caldata)
  805. caldata->tx_corr_coeff[im][i] =
  806. coeff->iqc_coeff[0];
  807. }
  808. if (caldata)
  809. caldata->num_measures[i] = nmeasurement;
  810. }
  811. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
  812. AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
  813. REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
  814. AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
  815. if (caldata) {
  816. if (is_reusable)
  817. set_bit(TXIQCAL_DONE, &caldata->cal_flags);
  818. else
  819. clear_bit(TXIQCAL_DONE, &caldata->cal_flags);
  820. }
  821. return;
  822. }
  823. static bool ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
  824. {
  825. struct ath_common *common = ath9k_hw_common(ah);
  826. u8 tx_gain_forced;
  827. tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  828. AR_PHY_TXGAIN_FORCE);
  829. if (tx_gain_forced)
  830. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  831. AR_PHY_TXGAIN_FORCE, 0);
  832. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
  833. AR_PHY_TX_IQCAL_START_DO_CAL, 1);
  834. if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
  835. AR_PHY_TX_IQCAL_START_DO_CAL, 0,
  836. AH_WAIT_TIMEOUT)) {
  837. ath_dbg(common, CALIBRATE, "Tx IQ Cal is not completed\n");
  838. return false;
  839. }
  840. return true;
  841. }
  842. static void __ar955x_tx_iq_cal_sort(struct ath_hw *ah,
  843. struct coeff *coeff,
  844. int i, int nmeasurement)
  845. {
  846. struct ath_common *common = ath9k_hw_common(ah);
  847. int im, ix, iy, temp;
  848. for (im = 0; im < nmeasurement; im++) {
  849. for (ix = 0; ix < MAXIQCAL - 1; ix++) {
  850. for (iy = ix + 1; iy <= MAXIQCAL - 1; iy++) {
  851. if (coeff->mag_coeff[i][im][iy] <
  852. coeff->mag_coeff[i][im][ix]) {
  853. temp = coeff->mag_coeff[i][im][ix];
  854. coeff->mag_coeff[i][im][ix] =
  855. coeff->mag_coeff[i][im][iy];
  856. coeff->mag_coeff[i][im][iy] = temp;
  857. }
  858. if (coeff->phs_coeff[i][im][iy] <
  859. coeff->phs_coeff[i][im][ix]) {
  860. temp = coeff->phs_coeff[i][im][ix];
  861. coeff->phs_coeff[i][im][ix] =
  862. coeff->phs_coeff[i][im][iy];
  863. coeff->phs_coeff[i][im][iy] = temp;
  864. }
  865. }
  866. }
  867. coeff->mag_coeff[i][im][0] = coeff->mag_coeff[i][im][MAXIQCAL / 2];
  868. coeff->phs_coeff[i][im][0] = coeff->phs_coeff[i][im][MAXIQCAL / 2];
  869. ath_dbg(common, CALIBRATE,
  870. "IQCAL: Median [ch%d][gain%d]: mag = %d phase = %d\n",
  871. i, im,
  872. coeff->mag_coeff[i][im][0],
  873. coeff->phs_coeff[i][im][0]);
  874. }
  875. }
  876. static bool ar955x_tx_iq_cal_median(struct ath_hw *ah,
  877. struct coeff *coeff,
  878. int iqcal_idx,
  879. int nmeasurement)
  880. {
  881. int i;
  882. if ((iqcal_idx + 1) != MAXIQCAL)
  883. return false;
  884. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  885. __ar955x_tx_iq_cal_sort(ah, coeff, i, nmeasurement);
  886. }
  887. return true;
  888. }
  889. static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah,
  890. int iqcal_idx,
  891. bool is_reusable)
  892. {
  893. struct ath_common *common = ath9k_hw_common(ah);
  894. const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
  895. AR_PHY_TX_IQCAL_STATUS_B0,
  896. AR_PHY_TX_IQCAL_STATUS_B1,
  897. AR_PHY_TX_IQCAL_STATUS_B2,
  898. };
  899. const u_int32_t chan_info_tab[] = {
  900. AR_PHY_CHAN_INFO_TAB_0,
  901. AR_PHY_CHAN_INFO_TAB_1,
  902. AR_PHY_CHAN_INFO_TAB_2,
  903. };
  904. static struct coeff coeff;
  905. s32 iq_res[6];
  906. int i, im, j;
  907. int nmeasurement = 0;
  908. bool outlier_detect = true;
  909. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  910. if (!(ah->txchainmask & (1 << i)))
  911. continue;
  912. nmeasurement = REG_READ_FIELD(ah,
  913. AR_PHY_TX_IQCAL_STATUS_B0,
  914. AR_PHY_CALIBRATED_GAINS_0);
  915. if (nmeasurement > MAX_MEASUREMENT)
  916. nmeasurement = MAX_MEASUREMENT;
  917. for (im = 0; im < nmeasurement; im++) {
  918. ath_dbg(common, CALIBRATE,
  919. "Doing Tx IQ Cal for chain %d\n", i);
  920. if (REG_READ(ah, txiqcal_status[i]) &
  921. AR_PHY_TX_IQCAL_STATUS_FAILED) {
  922. ath_dbg(common, CALIBRATE,
  923. "Tx IQ Cal failed for chain %d\n", i);
  924. goto tx_iqcal_fail;
  925. }
  926. for (j = 0; j < 3; j++) {
  927. u32 idx = 2 * j, offset = 4 * (3 * im + j);
  928. REG_RMW_FIELD(ah,
  929. AR_PHY_CHAN_INFO_MEMORY,
  930. AR_PHY_CHAN_INFO_TAB_S2_READ,
  931. 0);
  932. /* 32 bits */
  933. iq_res[idx] = REG_READ(ah,
  934. chan_info_tab[i] +
  935. offset);
  936. REG_RMW_FIELD(ah,
  937. AR_PHY_CHAN_INFO_MEMORY,
  938. AR_PHY_CHAN_INFO_TAB_S2_READ,
  939. 1);
  940. /* 16 bits */
  941. iq_res[idx + 1] = 0xffff & REG_READ(ah,
  942. chan_info_tab[i] + offset);
  943. ath_dbg(common, CALIBRATE,
  944. "IQ_RES[%d]=0x%x IQ_RES[%d]=0x%x\n",
  945. idx, iq_res[idx], idx + 1,
  946. iq_res[idx + 1]);
  947. }
  948. if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
  949. coeff.iqc_coeff)) {
  950. ath_dbg(common, CALIBRATE,
  951. "Failed in calculation of IQ correction\n");
  952. goto tx_iqcal_fail;
  953. }
  954. coeff.phs_coeff[i][im][iqcal_idx] =
  955. coeff.iqc_coeff[0] & 0x7f;
  956. coeff.mag_coeff[i][im][iqcal_idx] =
  957. (coeff.iqc_coeff[0] >> 7) & 0x7f;
  958. if (coeff.mag_coeff[i][im][iqcal_idx] > 63)
  959. coeff.mag_coeff[i][im][iqcal_idx] -= 128;
  960. if (coeff.phs_coeff[i][im][iqcal_idx] > 63)
  961. coeff.phs_coeff[i][im][iqcal_idx] -= 128;
  962. }
  963. }
  964. if (AR_SREV_9550(ah))
  965. outlier_detect = ar955x_tx_iq_cal_median(ah, &coeff,
  966. iqcal_idx, nmeasurement);
  967. if (outlier_detect)
  968. ar9003_hw_tx_iq_cal_outlier_detection(ah, &coeff, is_reusable);
  969. return;
  970. tx_iqcal_fail:
  971. ath_dbg(common, CALIBRATE, "Tx IQ Cal failed\n");
  972. return;
  973. }
  974. static void ar9003_hw_tx_iq_cal_reload(struct ath_hw *ah)
  975. {
  976. struct ath9k_hw_cal_data *caldata = ah->caldata;
  977. u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
  978. int i, im;
  979. memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
  980. for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
  981. tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
  982. AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
  983. if (!AR_SREV_9485(ah)) {
  984. tx_corr_coeff[i * 2][1] =
  985. tx_corr_coeff[(i * 2) + 1][1] =
  986. AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
  987. tx_corr_coeff[i * 2][2] =
  988. tx_corr_coeff[(i * 2) + 1][2] =
  989. AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
  990. }
  991. }
  992. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  993. if (!(ah->txchainmask & (1 << i)))
  994. continue;
  995. for (im = 0; im < caldata->num_measures[i]; im++) {
  996. if ((im % 2) == 0)
  997. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  998. AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
  999. caldata->tx_corr_coeff[im][i]);
  1000. else
  1001. REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
  1002. AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
  1003. caldata->tx_corr_coeff[im][i]);
  1004. }
  1005. }
  1006. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
  1007. AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
  1008. REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
  1009. AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
  1010. }
  1011. static void ar9003_hw_manual_peak_cal(struct ath_hw *ah, u8 chain, bool is_2g)
  1012. {
  1013. int offset[8] = {0}, total = 0, test;
  1014. int agc_out, i, peak_detect_threshold = 0;
  1015. if (AR_SREV_9550(ah) || AR_SREV_9531(ah))
  1016. peak_detect_threshold = 8;
  1017. else if (AR_SREV_9561(ah))
  1018. peak_detect_threshold = 11;
  1019. /*
  1020. * Turn off LNA/SW.
  1021. */
  1022. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
  1023. AR_PHY_65NM_RXRF_GAINSTAGES_RX_OVERRIDE, 0x1);
  1024. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
  1025. AR_PHY_65NM_RXRF_GAINSTAGES_LNAON_CALDC, 0x0);
  1026. if (AR_SREV_9003_PCOEM(ah) || AR_SREV_9330_11(ah)) {
  1027. if (is_2g)
  1028. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
  1029. AR_PHY_65NM_RXRF_GAINSTAGES_LNA2G_GAIN_OVR, 0x0);
  1030. else
  1031. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
  1032. AR_PHY_65NM_RXRF_GAINSTAGES_LNA5G_GAIN_OVR, 0x0);
  1033. }
  1034. /*
  1035. * Turn off RXON.
  1036. */
  1037. REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
  1038. AR_PHY_65NM_RXTX2_RXON_OVR, 0x1);
  1039. REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
  1040. AR_PHY_65NM_RXTX2_RXON, 0x0);
  1041. /*
  1042. * Turn on AGC for cal.
  1043. */
  1044. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1045. AR_PHY_65NM_RXRF_AGC_AGC_OVERRIDE, 0x1);
  1046. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1047. AR_PHY_65NM_RXRF_AGC_AGC_ON_OVR, 0x1);
  1048. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1049. AR_PHY_65NM_RXRF_AGC_AGC_CAL_OVR, 0x1);
  1050. if (AR_SREV_9330_11(ah))
  1051. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1052. AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR, 0x0);
  1053. if (is_2g)
  1054. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1055. AR_PHY_65NM_RXRF_AGC_AGC2G_DBDAC_OVR,
  1056. peak_detect_threshold);
  1057. else
  1058. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1059. AR_PHY_65NM_RXRF_AGC_AGC5G_DBDAC_OVR,
  1060. peak_detect_threshold);
  1061. for (i = 6; i > 0; i--) {
  1062. offset[i] = BIT(i - 1);
  1063. test = total + offset[i];
  1064. if (is_2g)
  1065. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1066. AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR,
  1067. test);
  1068. else
  1069. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1070. AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR,
  1071. test);
  1072. udelay(100);
  1073. agc_out = REG_READ_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1074. AR_PHY_65NM_RXRF_AGC_AGC_OUT);
  1075. offset[i] = (agc_out) ? 0 : 1;
  1076. total += (offset[i] << (i - 1));
  1077. }
  1078. if (is_2g)
  1079. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1080. AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR, total);
  1081. else
  1082. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1083. AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR, total);
  1084. /*
  1085. * Turn on LNA.
  1086. */
  1087. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
  1088. AR_PHY_65NM_RXRF_GAINSTAGES_RX_OVERRIDE, 0);
  1089. /*
  1090. * Turn off RXON.
  1091. */
  1092. REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
  1093. AR_PHY_65NM_RXTX2_RXON_OVR, 0);
  1094. /*
  1095. * Turn off peak detect calibration.
  1096. */
  1097. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  1098. AR_PHY_65NM_RXRF_AGC_AGC_CAL_OVR, 0);
  1099. }
  1100. static void ar9003_hw_do_pcoem_manual_peak_cal(struct ath_hw *ah,
  1101. struct ath9k_channel *chan,
  1102. bool run_rtt_cal)
  1103. {
  1104. struct ath9k_hw_cal_data *caldata = ah->caldata;
  1105. int i;
  1106. if ((ah->caps.hw_caps & ATH9K_HW_CAP_RTT) && !run_rtt_cal)
  1107. return;
  1108. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  1109. if (!(ah->rxchainmask & (1 << i)))
  1110. continue;
  1111. ar9003_hw_manual_peak_cal(ah, i, IS_CHAN_2GHZ(chan));
  1112. }
  1113. if (caldata)
  1114. set_bit(SW_PKDET_DONE, &caldata->cal_flags);
  1115. if ((ah->caps.hw_caps & ATH9K_HW_CAP_RTT) && caldata) {
  1116. if (IS_CHAN_2GHZ(chan)){
  1117. caldata->caldac[0] = REG_READ_FIELD(ah,
  1118. AR_PHY_65NM_RXRF_AGC(0),
  1119. AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR);
  1120. caldata->caldac[1] = REG_READ_FIELD(ah,
  1121. AR_PHY_65NM_RXRF_AGC(1),
  1122. AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR);
  1123. } else {
  1124. caldata->caldac[0] = REG_READ_FIELD(ah,
  1125. AR_PHY_65NM_RXRF_AGC(0),
  1126. AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR);
  1127. caldata->caldac[1] = REG_READ_FIELD(ah,
  1128. AR_PHY_65NM_RXRF_AGC(1),
  1129. AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR);
  1130. }
  1131. }
  1132. }
  1133. static void ar9003_hw_cl_cal_post_proc(struct ath_hw *ah, bool is_reusable)
  1134. {
  1135. u32 cl_idx[AR9300_MAX_CHAINS] = { AR_PHY_CL_TAB_0,
  1136. AR_PHY_CL_TAB_1,
  1137. AR_PHY_CL_TAB_2 };
  1138. struct ath9k_hw_cal_data *caldata = ah->caldata;
  1139. bool txclcal_done = false;
  1140. int i, j;
  1141. if (!caldata || !(ah->enabled_cals & TX_CL_CAL))
  1142. return;
  1143. txclcal_done = !!(REG_READ(ah, AR_PHY_AGC_CONTROL) &
  1144. AR_PHY_AGC_CONTROL_CLC_SUCCESS);
  1145. if (test_bit(TXCLCAL_DONE, &caldata->cal_flags)) {
  1146. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  1147. if (!(ah->txchainmask & (1 << i)))
  1148. continue;
  1149. for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
  1150. REG_WRITE(ah, CL_TAB_ENTRY(cl_idx[i]),
  1151. caldata->tx_clcal[i][j]);
  1152. }
  1153. } else if (is_reusable && txclcal_done) {
  1154. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  1155. if (!(ah->txchainmask & (1 << i)))
  1156. continue;
  1157. for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
  1158. caldata->tx_clcal[i][j] =
  1159. REG_READ(ah, CL_TAB_ENTRY(cl_idx[i]));
  1160. }
  1161. set_bit(TXCLCAL_DONE, &caldata->cal_flags);
  1162. }
  1163. }
  1164. static void ar9003_hw_init_cal_common(struct ath_hw *ah)
  1165. {
  1166. struct ath9k_hw_cal_data *caldata = ah->caldata;
  1167. /* Initialize list pointers */
  1168. ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
  1169. INIT_CAL(&ah->iq_caldata);
  1170. INSERT_CAL(ah, &ah->iq_caldata);
  1171. /* Initialize current pointer to first element in list */
  1172. ah->cal_list_curr = ah->cal_list;
  1173. if (ah->cal_list_curr)
  1174. ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
  1175. if (caldata)
  1176. caldata->CalValid = 0;
  1177. }
  1178. static bool ar9003_hw_init_cal_pcoem(struct ath_hw *ah,
  1179. struct ath9k_channel *chan)
  1180. {
  1181. struct ath_common *common = ath9k_hw_common(ah);
  1182. struct ath9k_hw_cal_data *caldata = ah->caldata;
  1183. bool txiqcal_done = false;
  1184. bool is_reusable = true, status = true;
  1185. bool run_rtt_cal = false, run_agc_cal;
  1186. bool rtt = !!(ah->caps.hw_caps & ATH9K_HW_CAP_RTT);
  1187. u32 rx_delay = 0;
  1188. u32 agc_ctrl = 0, agc_supp_cals = AR_PHY_AGC_CONTROL_OFFSET_CAL |
  1189. AR_PHY_AGC_CONTROL_FLTR_CAL |
  1190. AR_PHY_AGC_CONTROL_PKDET_CAL;
  1191. /* Use chip chainmask only for calibration */
  1192. ar9003_hw_set_chain_masks(ah, ah->caps.rx_chainmask, ah->caps.tx_chainmask);
  1193. if (rtt) {
  1194. if (!ar9003_hw_rtt_restore(ah, chan))
  1195. run_rtt_cal = true;
  1196. if (run_rtt_cal)
  1197. ath_dbg(common, CALIBRATE, "RTT calibration to be done\n");
  1198. }
  1199. run_agc_cal = run_rtt_cal;
  1200. if (run_rtt_cal) {
  1201. ar9003_hw_rtt_enable(ah);
  1202. ar9003_hw_rtt_set_mask(ah, 0x00);
  1203. ar9003_hw_rtt_clear_hist(ah);
  1204. }
  1205. if (rtt) {
  1206. if (!run_rtt_cal) {
  1207. agc_ctrl = REG_READ(ah, AR_PHY_AGC_CONTROL);
  1208. agc_supp_cals &= agc_ctrl;
  1209. agc_ctrl &= ~(AR_PHY_AGC_CONTROL_OFFSET_CAL |
  1210. AR_PHY_AGC_CONTROL_FLTR_CAL |
  1211. AR_PHY_AGC_CONTROL_PKDET_CAL);
  1212. REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
  1213. } else {
  1214. if (ah->ah_flags & AH_FASTCC)
  1215. run_agc_cal = true;
  1216. }
  1217. }
  1218. if (ah->enabled_cals & TX_CL_CAL) {
  1219. if (caldata && test_bit(TXCLCAL_DONE, &caldata->cal_flags))
  1220. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL,
  1221. AR_PHY_CL_CAL_ENABLE);
  1222. else {
  1223. REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL,
  1224. AR_PHY_CL_CAL_ENABLE);
  1225. run_agc_cal = true;
  1226. }
  1227. }
  1228. if ((IS_CHAN_HALF_RATE(chan) || IS_CHAN_QUARTER_RATE(chan)) ||
  1229. !(ah->enabled_cals & TX_IQ_CAL))
  1230. goto skip_tx_iqcal;
  1231. /* Do Tx IQ Calibration */
  1232. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
  1233. AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
  1234. DELPT);
  1235. /*
  1236. * For AR9485 or later chips, TxIQ cal runs as part of
  1237. * AGC calibration
  1238. */
  1239. if (ah->enabled_cals & TX_IQ_ON_AGC_CAL) {
  1240. if (caldata && !test_bit(TXIQCAL_DONE, &caldata->cal_flags))
  1241. REG_SET_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
  1242. AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
  1243. else
  1244. REG_CLR_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
  1245. AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
  1246. txiqcal_done = run_agc_cal = true;
  1247. }
  1248. skip_tx_iqcal:
  1249. if (ath9k_hw_mci_is_enabled(ah) && IS_CHAN_2GHZ(chan) && run_agc_cal)
  1250. ar9003_mci_init_cal_req(ah, &is_reusable);
  1251. if (REG_READ(ah, AR_PHY_CL_CAL_CTL) & AR_PHY_CL_CAL_ENABLE) {
  1252. rx_delay = REG_READ(ah, AR_PHY_RX_DELAY);
  1253. /* Disable BB_active */
  1254. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  1255. udelay(5);
  1256. REG_WRITE(ah, AR_PHY_RX_DELAY, AR_PHY_RX_DELAY_DELAY);
  1257. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  1258. }
  1259. if (run_agc_cal || !(ah->ah_flags & AH_FASTCC)) {
  1260. /* Calibrate the AGC */
  1261. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  1262. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  1263. AR_PHY_AGC_CONTROL_CAL);
  1264. /* Poll for offset calibration complete */
  1265. status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
  1266. AR_PHY_AGC_CONTROL_CAL,
  1267. 0, AH_WAIT_TIMEOUT);
  1268. ar9003_hw_do_pcoem_manual_peak_cal(ah, chan, run_rtt_cal);
  1269. }
  1270. if (REG_READ(ah, AR_PHY_CL_CAL_CTL) & AR_PHY_CL_CAL_ENABLE) {
  1271. REG_WRITE(ah, AR_PHY_RX_DELAY, rx_delay);
  1272. udelay(5);
  1273. }
  1274. if (ath9k_hw_mci_is_enabled(ah) && IS_CHAN_2GHZ(chan) && run_agc_cal)
  1275. ar9003_mci_init_cal_done(ah);
  1276. if (rtt && !run_rtt_cal) {
  1277. agc_ctrl |= agc_supp_cals;
  1278. REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
  1279. }
  1280. if (!status) {
  1281. if (run_rtt_cal)
  1282. ar9003_hw_rtt_disable(ah);
  1283. ath_dbg(common, CALIBRATE,
  1284. "offset calibration failed to complete in %d ms; noisy environment?\n",
  1285. AH_WAIT_TIMEOUT / 1000);
  1286. return false;
  1287. }
  1288. if (txiqcal_done)
  1289. ar9003_hw_tx_iq_cal_post_proc(ah, 0, is_reusable);
  1290. else if (caldata && test_bit(TXIQCAL_DONE, &caldata->cal_flags))
  1291. ar9003_hw_tx_iq_cal_reload(ah);
  1292. ar9003_hw_cl_cal_post_proc(ah, is_reusable);
  1293. if (run_rtt_cal && caldata) {
  1294. if (is_reusable) {
  1295. if (!ath9k_hw_rfbus_req(ah)) {
  1296. ath_err(ath9k_hw_common(ah),
  1297. "Could not stop baseband\n");
  1298. } else {
  1299. ar9003_hw_rtt_fill_hist(ah);
  1300. if (test_bit(SW_PKDET_DONE, &caldata->cal_flags))
  1301. ar9003_hw_rtt_load_hist(ah);
  1302. }
  1303. ath9k_hw_rfbus_done(ah);
  1304. }
  1305. ar9003_hw_rtt_disable(ah);
  1306. }
  1307. /* Revert chainmask to runtime parameters */
  1308. ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
  1309. ar9003_hw_init_cal_common(ah);
  1310. return true;
  1311. }
  1312. static bool do_ar9003_agc_cal(struct ath_hw *ah)
  1313. {
  1314. struct ath_common *common = ath9k_hw_common(ah);
  1315. bool status;
  1316. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  1317. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  1318. AR_PHY_AGC_CONTROL_CAL);
  1319. status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
  1320. AR_PHY_AGC_CONTROL_CAL,
  1321. 0, AH_WAIT_TIMEOUT);
  1322. if (!status) {
  1323. ath_dbg(common, CALIBRATE,
  1324. "offset calibration failed to complete in %d ms,"
  1325. "noisy environment?\n",
  1326. AH_WAIT_TIMEOUT / 1000);
  1327. return false;
  1328. }
  1329. return true;
  1330. }
  1331. static bool ar9003_hw_init_cal_soc(struct ath_hw *ah,
  1332. struct ath9k_channel *chan)
  1333. {
  1334. bool txiqcal_done = false;
  1335. bool status = true;
  1336. bool run_agc_cal = false, sep_iq_cal = false;
  1337. int i = 0;
  1338. /* Use chip chainmask only for calibration */
  1339. ar9003_hw_set_chain_masks(ah, ah->caps.rx_chainmask, ah->caps.tx_chainmask);
  1340. if (ah->enabled_cals & TX_CL_CAL) {
  1341. REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
  1342. run_agc_cal = true;
  1343. }
  1344. if (IS_CHAN_HALF_RATE(chan) || IS_CHAN_QUARTER_RATE(chan))
  1345. goto skip_tx_iqcal;
  1346. /* Do Tx IQ Calibration */
  1347. REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
  1348. AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
  1349. DELPT);
  1350. /*
  1351. * For AR9485 or later chips, TxIQ cal runs as part of
  1352. * AGC calibration. Specifically, AR9550 in SoC chips.
  1353. */
  1354. if (ah->enabled_cals & TX_IQ_ON_AGC_CAL) {
  1355. if (REG_READ_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_0,
  1356. AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL)) {
  1357. txiqcal_done = true;
  1358. } else {
  1359. txiqcal_done = false;
  1360. }
  1361. run_agc_cal = true;
  1362. } else {
  1363. sep_iq_cal = true;
  1364. run_agc_cal = true;
  1365. }
  1366. /*
  1367. * In the SoC family, this will run for AR9300, AR9331 and AR9340.
  1368. */
  1369. if (sep_iq_cal) {
  1370. txiqcal_done = ar9003_hw_tx_iq_cal_run(ah);
  1371. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  1372. udelay(5);
  1373. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  1374. }
  1375. if (AR_SREV_9550(ah) && IS_CHAN_2GHZ(chan)) {
  1376. if (!ar9003_hw_dynamic_osdac_selection(ah, txiqcal_done))
  1377. return false;
  1378. }
  1379. skip_tx_iqcal:
  1380. if (run_agc_cal || !(ah->ah_flags & AH_FASTCC)) {
  1381. for (i = 0; i < AR9300_MAX_CHAINS; i++) {
  1382. if (!(ah->rxchainmask & (1 << i)))
  1383. continue;
  1384. ar9003_hw_manual_peak_cal(ah, i,
  1385. IS_CHAN_2GHZ(chan));
  1386. }
  1387. /*
  1388. * For non-AR9550 chips, we just trigger AGC calibration
  1389. * in the HW, poll for completion and then process
  1390. * the results.
  1391. *
  1392. * For AR955x, we run it multiple times and use
  1393. * median IQ correction.
  1394. */
  1395. if (!AR_SREV_9550(ah)) {
  1396. status = do_ar9003_agc_cal(ah);
  1397. if (!status)
  1398. return false;
  1399. if (txiqcal_done)
  1400. ar9003_hw_tx_iq_cal_post_proc(ah, 0, false);
  1401. } else {
  1402. if (!txiqcal_done) {
  1403. status = do_ar9003_agc_cal(ah);
  1404. if (!status)
  1405. return false;
  1406. } else {
  1407. for (i = 0; i < MAXIQCAL; i++) {
  1408. status = do_ar9003_agc_cal(ah);
  1409. if (!status)
  1410. return false;
  1411. ar9003_hw_tx_iq_cal_post_proc(ah, i, false);
  1412. }
  1413. }
  1414. }
  1415. }
  1416. /* Revert chainmask to runtime parameters */
  1417. ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
  1418. ar9003_hw_init_cal_common(ah);
  1419. return true;
  1420. }
  1421. void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
  1422. {
  1423. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  1424. struct ath_hw_ops *ops = ath9k_hw_ops(ah);
  1425. if (AR_SREV_9003_PCOEM(ah))
  1426. priv_ops->init_cal = ar9003_hw_init_cal_pcoem;
  1427. else
  1428. priv_ops->init_cal = ar9003_hw_init_cal_soc;
  1429. priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
  1430. priv_ops->setup_calibration = ar9003_hw_setup_calibration;
  1431. ops->calibrate = ar9003_hw_calibrate;
  1432. }