ar9003_calib.c 47 KB

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