calib.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*
  2. * Copyright (c) 2008-2009 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. /* We can tune this as we go by monitoring really low values */
  18. #define ATH9K_NF_TOO_LOW -60
  19. #define AR9285_CLCAL_REDO_THRESH 1
  20. /* AR5416 may return very high value (like -31 dBm), in those cases the nf
  21. * is incorrect and we should use the static NF value. Later we can try to
  22. * find out why they are reporting these values */
  23. static bool ath9k_hw_nf_in_range(struct ath_hw *ah, s16 nf)
  24. {
  25. if (nf > ATH9K_NF_TOO_LOW) {
  26. ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  27. "noise floor value detected (%d) is "
  28. "lower than what we think is a "
  29. "reasonable value (%d)\n",
  30. nf, ATH9K_NF_TOO_LOW);
  31. return false;
  32. }
  33. return true;
  34. }
  35. static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
  36. {
  37. int16_t nfval;
  38. int16_t sort[ATH9K_NF_CAL_HIST_MAX];
  39. int i, j;
  40. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
  41. sort[i] = nfCalBuffer[i];
  42. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
  43. for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
  44. if (sort[j] > sort[j - 1]) {
  45. nfval = sort[j];
  46. sort[j] = sort[j - 1];
  47. sort[j - 1] = nfval;
  48. }
  49. }
  50. }
  51. nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
  52. return nfval;
  53. }
  54. static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
  55. int16_t *nfarray)
  56. {
  57. int i;
  58. for (i = 0; i < NUM_NF_READINGS; i++) {
  59. h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
  60. if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
  61. h[i].currIndex = 0;
  62. if (h[i].invalidNFcount > 0) {
  63. if (nfarray[i] < AR_PHY_CCA_MIN_BAD_VALUE ||
  64. nfarray[i] > AR_PHY_CCA_MAX_HIGH_VALUE) {
  65. h[i].invalidNFcount = ATH9K_NF_CAL_HIST_MAX;
  66. } else {
  67. h[i].invalidNFcount--;
  68. h[i].privNF = nfarray[i];
  69. }
  70. } else {
  71. h[i].privNF =
  72. ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
  73. }
  74. }
  75. return;
  76. }
  77. static void ath9k_hw_do_getnf(struct ath_hw *ah,
  78. int16_t nfarray[NUM_NF_READINGS])
  79. {
  80. struct ath_common *common = ath9k_hw_common(ah);
  81. int16_t nf;
  82. if (AR_SREV_9280_10_OR_LATER(ah))
  83. nf = MS(REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);
  84. else
  85. nf = MS(REG_READ(ah, AR_PHY_CCA), AR_PHY_MINCCA_PWR);
  86. if (nf & 0x100)
  87. nf = 0 - ((nf ^ 0x1ff) + 1);
  88. ath_print(common, ATH_DBG_CALIBRATE,
  89. "NF calibrated [ctl] [chain 0] is %d\n", nf);
  90. if (AR_SREV_9271(ah) && (nf >= -114))
  91. nf = -116;
  92. nfarray[0] = nf;
  93. if (!AR_SREV_9285(ah) && !AR_SREV_9271(ah)) {
  94. if (AR_SREV_9280_10_OR_LATER(ah))
  95. nf = MS(REG_READ(ah, AR_PHY_CH1_CCA),
  96. AR9280_PHY_CH1_MINCCA_PWR);
  97. else
  98. nf = MS(REG_READ(ah, AR_PHY_CH1_CCA),
  99. AR_PHY_CH1_MINCCA_PWR);
  100. if (nf & 0x100)
  101. nf = 0 - ((nf ^ 0x1ff) + 1);
  102. ath_print(common, ATH_DBG_CALIBRATE,
  103. "NF calibrated [ctl] [chain 1] is %d\n", nf);
  104. nfarray[1] = nf;
  105. if (!AR_SREV_9280(ah) && !AR_SREV_9287(ah)) {
  106. nf = MS(REG_READ(ah, AR_PHY_CH2_CCA),
  107. AR_PHY_CH2_MINCCA_PWR);
  108. if (nf & 0x100)
  109. nf = 0 - ((nf ^ 0x1ff) + 1);
  110. ath_print(common, ATH_DBG_CALIBRATE,
  111. "NF calibrated [ctl] [chain 2] is %d\n", nf);
  112. nfarray[2] = nf;
  113. }
  114. }
  115. if (AR_SREV_9280_10_OR_LATER(ah))
  116. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA),
  117. AR9280_PHY_EXT_MINCCA_PWR);
  118. else
  119. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA),
  120. AR_PHY_EXT_MINCCA_PWR);
  121. if (nf & 0x100)
  122. nf = 0 - ((nf ^ 0x1ff) + 1);
  123. ath_print(common, ATH_DBG_CALIBRATE,
  124. "NF calibrated [ext] [chain 0] is %d\n", nf);
  125. if (AR_SREV_9271(ah) && (nf >= -114))
  126. nf = -116;
  127. nfarray[3] = nf;
  128. if (!AR_SREV_9285(ah) && !AR_SREV_9271(ah)) {
  129. if (AR_SREV_9280_10_OR_LATER(ah))
  130. nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA),
  131. AR9280_PHY_CH1_EXT_MINCCA_PWR);
  132. else
  133. nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA),
  134. AR_PHY_CH1_EXT_MINCCA_PWR);
  135. if (nf & 0x100)
  136. nf = 0 - ((nf ^ 0x1ff) + 1);
  137. ath_print(common, ATH_DBG_CALIBRATE,
  138. "NF calibrated [ext] [chain 1] is %d\n", nf);
  139. nfarray[4] = nf;
  140. if (!AR_SREV_9280(ah) && !AR_SREV_9287(ah)) {
  141. nf = MS(REG_READ(ah, AR_PHY_CH2_EXT_CCA),
  142. AR_PHY_CH2_EXT_MINCCA_PWR);
  143. if (nf & 0x100)
  144. nf = 0 - ((nf ^ 0x1ff) + 1);
  145. ath_print(common, ATH_DBG_CALIBRATE,
  146. "NF calibrated [ext] [chain 2] is %d\n", nf);
  147. nfarray[5] = nf;
  148. }
  149. }
  150. }
  151. static bool getNoiseFloorThresh(struct ath_hw *ah,
  152. enum ieee80211_band band,
  153. int16_t *nft)
  154. {
  155. switch (band) {
  156. case IEEE80211_BAND_5GHZ:
  157. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
  158. break;
  159. case IEEE80211_BAND_2GHZ:
  160. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
  161. break;
  162. default:
  163. BUG_ON(1);
  164. return false;
  165. }
  166. return true;
  167. }
  168. static void ath9k_hw_setup_calibration(struct ath_hw *ah,
  169. struct ath9k_cal_list *currCal)
  170. {
  171. struct ath_common *common = ath9k_hw_common(ah);
  172. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(0),
  173. AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
  174. currCal->calData->calCountMax);
  175. switch (currCal->calData->calType) {
  176. case IQ_MISMATCH_CAL:
  177. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  178. ath_print(common, ATH_DBG_CALIBRATE,
  179. "starting IQ Mismatch Calibration\n");
  180. break;
  181. case ADC_GAIN_CAL:
  182. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
  183. ath_print(common, ATH_DBG_CALIBRATE,
  184. "starting ADC Gain Calibration\n");
  185. break;
  186. case ADC_DC_CAL:
  187. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
  188. ath_print(common, ATH_DBG_CALIBRATE,
  189. "starting ADC DC Calibration\n");
  190. break;
  191. case ADC_DC_INIT_CAL:
  192. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
  193. ath_print(common, ATH_DBG_CALIBRATE,
  194. "starting Init ADC DC Calibration\n");
  195. break;
  196. }
  197. REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
  198. AR_PHY_TIMING_CTRL4_DO_CAL);
  199. }
  200. static void ath9k_hw_reset_calibration(struct ath_hw *ah,
  201. struct ath9k_cal_list *currCal)
  202. {
  203. int i;
  204. ath9k_hw_setup_calibration(ah, currCal);
  205. currCal->calState = CAL_RUNNING;
  206. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  207. ah->meas0.sign[i] = 0;
  208. ah->meas1.sign[i] = 0;
  209. ah->meas2.sign[i] = 0;
  210. ah->meas3.sign[i] = 0;
  211. }
  212. ah->cal_samples = 0;
  213. }
  214. static bool ath9k_hw_per_calibration(struct ath_hw *ah,
  215. struct ath9k_channel *ichan,
  216. u8 rxchainmask,
  217. struct ath9k_cal_list *currCal)
  218. {
  219. bool iscaldone = false;
  220. if (currCal->calState == CAL_RUNNING) {
  221. if (!(REG_READ(ah, AR_PHY_TIMING_CTRL4(0)) &
  222. AR_PHY_TIMING_CTRL4_DO_CAL)) {
  223. currCal->calData->calCollect(ah);
  224. ah->cal_samples++;
  225. if (ah->cal_samples >= currCal->calData->calNumSamples) {
  226. int i, numChains = 0;
  227. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  228. if (rxchainmask & (1 << i))
  229. numChains++;
  230. }
  231. currCal->calData->calPostProc(ah, numChains);
  232. ichan->CalValid |= currCal->calData->calType;
  233. currCal->calState = CAL_DONE;
  234. iscaldone = true;
  235. } else {
  236. ath9k_hw_setup_calibration(ah, currCal);
  237. }
  238. }
  239. } else if (!(ichan->CalValid & currCal->calData->calType)) {
  240. ath9k_hw_reset_calibration(ah, currCal);
  241. }
  242. return iscaldone;
  243. }
  244. /* Assumes you are talking about the currently configured channel */
  245. static bool ath9k_hw_iscal_supported(struct ath_hw *ah,
  246. enum ath9k_cal_types calType)
  247. {
  248. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  249. switch (calType & ah->supp_cals) {
  250. case IQ_MISMATCH_CAL: /* Both 2 GHz and 5 GHz support OFDM */
  251. return true;
  252. case ADC_GAIN_CAL:
  253. case ADC_DC_CAL:
  254. if (!(conf->channel->band == IEEE80211_BAND_2GHZ &&
  255. conf_is_ht20(conf)))
  256. return true;
  257. break;
  258. }
  259. return false;
  260. }
  261. static void ath9k_hw_iqcal_collect(struct ath_hw *ah)
  262. {
  263. int i;
  264. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  265. ah->totalPowerMeasI[i] +=
  266. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  267. ah->totalPowerMeasQ[i] +=
  268. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  269. ah->totalIqCorrMeas[i] +=
  270. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  271. ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  272. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  273. ah->cal_samples, i, ah->totalPowerMeasI[i],
  274. ah->totalPowerMeasQ[i],
  275. ah->totalIqCorrMeas[i]);
  276. }
  277. }
  278. static void ath9k_hw_adc_gaincal_collect(struct ath_hw *ah)
  279. {
  280. int i;
  281. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  282. ah->totalAdcIOddPhase[i] +=
  283. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  284. ah->totalAdcIEvenPhase[i] +=
  285. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  286. ah->totalAdcQOddPhase[i] +=
  287. REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  288. ah->totalAdcQEvenPhase[i] +=
  289. REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
  290. ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  291. "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
  292. "oddq=0x%08x; evenq=0x%08x;\n",
  293. ah->cal_samples, i,
  294. ah->totalAdcIOddPhase[i],
  295. ah->totalAdcIEvenPhase[i],
  296. ah->totalAdcQOddPhase[i],
  297. ah->totalAdcQEvenPhase[i]);
  298. }
  299. }
  300. static void ath9k_hw_adc_dccal_collect(struct ath_hw *ah)
  301. {
  302. int i;
  303. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  304. ah->totalAdcDcOffsetIOddPhase[i] +=
  305. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  306. ah->totalAdcDcOffsetIEvenPhase[i] +=
  307. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  308. ah->totalAdcDcOffsetQOddPhase[i] +=
  309. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  310. ah->totalAdcDcOffsetQEvenPhase[i] +=
  311. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
  312. ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  313. "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
  314. "oddq=0x%08x; evenq=0x%08x;\n",
  315. ah->cal_samples, i,
  316. ah->totalAdcDcOffsetIOddPhase[i],
  317. ah->totalAdcDcOffsetIEvenPhase[i],
  318. ah->totalAdcDcOffsetQOddPhase[i],
  319. ah->totalAdcDcOffsetQEvenPhase[i]);
  320. }
  321. }
  322. static void ath9k_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
  323. {
  324. struct ath_common *common = ath9k_hw_common(ah);
  325. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  326. u32 qCoffDenom, iCoffDenom;
  327. int32_t qCoff, iCoff;
  328. int iqCorrNeg, i;
  329. for (i = 0; i < numChains; i++) {
  330. powerMeasI = ah->totalPowerMeasI[i];
  331. powerMeasQ = ah->totalPowerMeasQ[i];
  332. iqCorrMeas = ah->totalIqCorrMeas[i];
  333. ath_print(common, ATH_DBG_CALIBRATE,
  334. "Starting IQ Cal and Correction for Chain %d\n",
  335. i);
  336. ath_print(common, ATH_DBG_CALIBRATE,
  337. "Orignal: Chn %diq_corr_meas = 0x%08x\n",
  338. i, ah->totalIqCorrMeas[i]);
  339. iqCorrNeg = 0;
  340. if (iqCorrMeas > 0x80000000) {
  341. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  342. iqCorrNeg = 1;
  343. }
  344. ath_print(common, ATH_DBG_CALIBRATE,
  345. "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
  346. ath_print(common, ATH_DBG_CALIBRATE,
  347. "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
  348. ath_print(common, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
  349. iqCorrNeg);
  350. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 128;
  351. qCoffDenom = powerMeasQ / 64;
  352. if ((powerMeasQ != 0) && (iCoffDenom != 0) &&
  353. (qCoffDenom != 0)) {
  354. iCoff = iqCorrMeas / iCoffDenom;
  355. qCoff = powerMeasI / qCoffDenom - 64;
  356. ath_print(common, ATH_DBG_CALIBRATE,
  357. "Chn %d iCoff = 0x%08x\n", i, iCoff);
  358. ath_print(common, ATH_DBG_CALIBRATE,
  359. "Chn %d qCoff = 0x%08x\n", i, qCoff);
  360. iCoff = iCoff & 0x3f;
  361. ath_print(common, ATH_DBG_CALIBRATE,
  362. "New: Chn %d iCoff = 0x%08x\n", i, iCoff);
  363. if (iqCorrNeg == 0x0)
  364. iCoff = 0x40 - iCoff;
  365. if (qCoff > 15)
  366. qCoff = 15;
  367. else if (qCoff <= -16)
  368. qCoff = 16;
  369. ath_print(common, ATH_DBG_CALIBRATE,
  370. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  371. i, iCoff, qCoff);
  372. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
  373. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
  374. iCoff);
  375. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
  376. AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
  377. qCoff);
  378. ath_print(common, ATH_DBG_CALIBRATE,
  379. "IQ Cal and Correction done for Chain %d\n",
  380. i);
  381. }
  382. }
  383. REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
  384. AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
  385. }
  386. static void ath9k_hw_adc_gaincal_calibrate(struct ath_hw *ah, u8 numChains)
  387. {
  388. struct ath_common *common = ath9k_hw_common(ah);
  389. u32 iOddMeasOffset, iEvenMeasOffset, qOddMeasOffset, qEvenMeasOffset;
  390. u32 qGainMismatch, iGainMismatch, val, i;
  391. for (i = 0; i < numChains; i++) {
  392. iOddMeasOffset = ah->totalAdcIOddPhase[i];
  393. iEvenMeasOffset = ah->totalAdcIEvenPhase[i];
  394. qOddMeasOffset = ah->totalAdcQOddPhase[i];
  395. qEvenMeasOffset = ah->totalAdcQEvenPhase[i];
  396. ath_print(common, ATH_DBG_CALIBRATE,
  397. "Starting ADC Gain Cal for Chain %d\n", i);
  398. ath_print(common, ATH_DBG_CALIBRATE,
  399. "Chn %d pwr_meas_odd_i = 0x%08x\n", i,
  400. iOddMeasOffset);
  401. ath_print(common, ATH_DBG_CALIBRATE,
  402. "Chn %d pwr_meas_even_i = 0x%08x\n", i,
  403. iEvenMeasOffset);
  404. ath_print(common, ATH_DBG_CALIBRATE,
  405. "Chn %d pwr_meas_odd_q = 0x%08x\n", i,
  406. qOddMeasOffset);
  407. ath_print(common, ATH_DBG_CALIBRATE,
  408. "Chn %d pwr_meas_even_q = 0x%08x\n", i,
  409. qEvenMeasOffset);
  410. if (iOddMeasOffset != 0 && qEvenMeasOffset != 0) {
  411. iGainMismatch =
  412. ((iEvenMeasOffset * 32) /
  413. iOddMeasOffset) & 0x3f;
  414. qGainMismatch =
  415. ((qOddMeasOffset * 32) /
  416. qEvenMeasOffset) & 0x3f;
  417. ath_print(common, ATH_DBG_CALIBRATE,
  418. "Chn %d gain_mismatch_i = 0x%08x\n", i,
  419. iGainMismatch);
  420. ath_print(common, ATH_DBG_CALIBRATE,
  421. "Chn %d gain_mismatch_q = 0x%08x\n", i,
  422. qGainMismatch);
  423. val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
  424. val &= 0xfffff000;
  425. val |= (qGainMismatch) | (iGainMismatch << 6);
  426. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
  427. ath_print(common, ATH_DBG_CALIBRATE,
  428. "ADC Gain Cal done for Chain %d\n", i);
  429. }
  430. }
  431. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
  432. REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
  433. AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
  434. }
  435. static void ath9k_hw_adc_dccal_calibrate(struct ath_hw *ah, u8 numChains)
  436. {
  437. struct ath_common *common = ath9k_hw_common(ah);
  438. u32 iOddMeasOffset, iEvenMeasOffset, val, i;
  439. int32_t qOddMeasOffset, qEvenMeasOffset, qDcMismatch, iDcMismatch;
  440. const struct ath9k_percal_data *calData =
  441. ah->cal_list_curr->calData;
  442. u32 numSamples =
  443. (1 << (calData->calCountMax + 5)) * calData->calNumSamples;
  444. for (i = 0; i < numChains; i++) {
  445. iOddMeasOffset = ah->totalAdcDcOffsetIOddPhase[i];
  446. iEvenMeasOffset = ah->totalAdcDcOffsetIEvenPhase[i];
  447. qOddMeasOffset = ah->totalAdcDcOffsetQOddPhase[i];
  448. qEvenMeasOffset = ah->totalAdcDcOffsetQEvenPhase[i];
  449. ath_print(common, ATH_DBG_CALIBRATE,
  450. "Starting ADC DC Offset Cal for Chain %d\n", i);
  451. ath_print(common, ATH_DBG_CALIBRATE,
  452. "Chn %d pwr_meas_odd_i = %d\n", i,
  453. iOddMeasOffset);
  454. ath_print(common, ATH_DBG_CALIBRATE,
  455. "Chn %d pwr_meas_even_i = %d\n", i,
  456. iEvenMeasOffset);
  457. ath_print(common, ATH_DBG_CALIBRATE,
  458. "Chn %d pwr_meas_odd_q = %d\n", i,
  459. qOddMeasOffset);
  460. ath_print(common, ATH_DBG_CALIBRATE,
  461. "Chn %d pwr_meas_even_q = %d\n", i,
  462. qEvenMeasOffset);
  463. iDcMismatch = (((iEvenMeasOffset - iOddMeasOffset) * 2) /
  464. numSamples) & 0x1ff;
  465. qDcMismatch = (((qOddMeasOffset - qEvenMeasOffset) * 2) /
  466. numSamples) & 0x1ff;
  467. ath_print(common, ATH_DBG_CALIBRATE,
  468. "Chn %d dc_offset_mismatch_i = 0x%08x\n", i,
  469. iDcMismatch);
  470. ath_print(common, ATH_DBG_CALIBRATE,
  471. "Chn %d dc_offset_mismatch_q = 0x%08x\n", i,
  472. qDcMismatch);
  473. val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
  474. val &= 0xc0000fff;
  475. val |= (qDcMismatch << 12) | (iDcMismatch << 21);
  476. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
  477. ath_print(common, ATH_DBG_CALIBRATE,
  478. "ADC DC Offset Cal done for Chain %d\n", i);
  479. }
  480. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
  481. REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
  482. AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
  483. }
  484. /* This is done for the currently configured channel */
  485. bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
  486. {
  487. struct ath_common *common = ath9k_hw_common(ah);
  488. struct ieee80211_conf *conf = &common->hw->conf;
  489. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  490. if (!ah->curchan)
  491. return true;
  492. if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
  493. return true;
  494. if (currCal == NULL)
  495. return true;
  496. if (currCal->calState != CAL_DONE) {
  497. ath_print(common, ATH_DBG_CALIBRATE,
  498. "Calibration state incorrect, %d\n",
  499. currCal->calState);
  500. return true;
  501. }
  502. if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
  503. return true;
  504. ath_print(common, ATH_DBG_CALIBRATE,
  505. "Resetting Cal %d state for channel %u\n",
  506. currCal->calData->calType, conf->channel->center_freq);
  507. ah->curchan->CalValid &= ~currCal->calData->calType;
  508. currCal->calState = CAL_WAITING;
  509. return false;
  510. }
  511. EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
  512. void ath9k_hw_start_nfcal(struct ath_hw *ah)
  513. {
  514. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  515. AR_PHY_AGC_CONTROL_ENABLE_NF);
  516. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  517. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  518. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  519. }
  520. void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
  521. {
  522. struct ath9k_nfcal_hist *h;
  523. int i, j;
  524. int32_t val;
  525. const u32 ar5416_cca_regs[6] = {
  526. AR_PHY_CCA,
  527. AR_PHY_CH1_CCA,
  528. AR_PHY_CH2_CCA,
  529. AR_PHY_EXT_CCA,
  530. AR_PHY_CH1_EXT_CCA,
  531. AR_PHY_CH2_EXT_CCA
  532. };
  533. u8 chainmask, rx_chain_status;
  534. rx_chain_status = REG_READ(ah, AR_PHY_RX_CHAINMASK);
  535. if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  536. chainmask = 0x9;
  537. else if (AR_SREV_9280(ah) || AR_SREV_9287(ah)) {
  538. if ((rx_chain_status & 0x2) || (rx_chain_status & 0x4))
  539. chainmask = 0x1B;
  540. else
  541. chainmask = 0x09;
  542. } else {
  543. if (rx_chain_status & 0x4)
  544. chainmask = 0x3F;
  545. else if (rx_chain_status & 0x2)
  546. chainmask = 0x1B;
  547. else
  548. chainmask = 0x09;
  549. }
  550. h = ah->nfCalHist;
  551. for (i = 0; i < NUM_NF_READINGS; i++) {
  552. if (chainmask & (1 << i)) {
  553. val = REG_READ(ah, ar5416_cca_regs[i]);
  554. val &= 0xFFFFFE00;
  555. val |= (((u32) (h[i].privNF) << 1) & 0x1ff);
  556. REG_WRITE(ah, ar5416_cca_regs[i], val);
  557. }
  558. }
  559. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  560. AR_PHY_AGC_CONTROL_ENABLE_NF);
  561. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  562. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  563. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  564. for (j = 0; j < 5; j++) {
  565. if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
  566. AR_PHY_AGC_CONTROL_NF) == 0)
  567. break;
  568. udelay(50);
  569. }
  570. for (i = 0; i < NUM_NF_READINGS; i++) {
  571. if (chainmask & (1 << i)) {
  572. val = REG_READ(ah, ar5416_cca_regs[i]);
  573. val &= 0xFFFFFE00;
  574. val |= (((u32) (-50) << 1) & 0x1ff);
  575. REG_WRITE(ah, ar5416_cca_regs[i], val);
  576. }
  577. }
  578. }
  579. int16_t ath9k_hw_getnf(struct ath_hw *ah,
  580. struct ath9k_channel *chan)
  581. {
  582. struct ath_common *common = ath9k_hw_common(ah);
  583. int16_t nf, nfThresh;
  584. int16_t nfarray[NUM_NF_READINGS] = { 0 };
  585. struct ath9k_nfcal_hist *h;
  586. struct ieee80211_channel *c = chan->chan;
  587. chan->channelFlags &= (~CHANNEL_CW_INT);
  588. if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
  589. ath_print(common, ATH_DBG_CALIBRATE,
  590. "NF did not complete in calibration window\n");
  591. nf = 0;
  592. chan->rawNoiseFloor = nf;
  593. return chan->rawNoiseFloor;
  594. } else {
  595. ath9k_hw_do_getnf(ah, nfarray);
  596. nf = nfarray[0];
  597. if (getNoiseFloorThresh(ah, c->band, &nfThresh)
  598. && nf > nfThresh) {
  599. ath_print(common, ATH_DBG_CALIBRATE,
  600. "noise floor failed detected; "
  601. "detected %d, threshold %d\n",
  602. nf, nfThresh);
  603. chan->channelFlags |= CHANNEL_CW_INT;
  604. }
  605. }
  606. h = ah->nfCalHist;
  607. ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
  608. chan->rawNoiseFloor = h[0].privNF;
  609. return chan->rawNoiseFloor;
  610. }
  611. void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
  612. {
  613. int i, j;
  614. s16 noise_floor;
  615. if (AR_SREV_9280(ah))
  616. noise_floor = AR_PHY_CCA_MAX_AR9280_GOOD_VALUE;
  617. else if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  618. noise_floor = AR_PHY_CCA_MAX_AR9285_GOOD_VALUE;
  619. else if (AR_SREV_9287(ah))
  620. noise_floor = AR_PHY_CCA_MAX_AR9287_GOOD_VALUE;
  621. else
  622. noise_floor = AR_PHY_CCA_MAX_AR5416_GOOD_VALUE;
  623. for (i = 0; i < NUM_NF_READINGS; i++) {
  624. ah->nfCalHist[i].currIndex = 0;
  625. ah->nfCalHist[i].privNF = noise_floor;
  626. ah->nfCalHist[i].invalidNFcount =
  627. AR_PHY_CCA_FILTERWINDOW_LENGTH;
  628. for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
  629. ah->nfCalHist[i].nfCalBuffer[j] = noise_floor;
  630. }
  631. }
  632. }
  633. s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
  634. {
  635. s16 nf;
  636. if (chan->rawNoiseFloor == 0)
  637. nf = -96;
  638. else
  639. nf = chan->rawNoiseFloor;
  640. if (!ath9k_hw_nf_in_range(ah, nf))
  641. nf = ATH_DEFAULT_NOISE_FLOOR;
  642. return nf;
  643. }
  644. EXPORT_SYMBOL(ath9k_hw_getchan_noise);
  645. static void ath9k_olc_temp_compensation_9287(struct ath_hw *ah)
  646. {
  647. u32 rddata;
  648. int32_t delta, currPDADC, slope;
  649. rddata = REG_READ(ah, AR_PHY_TX_PWRCTRL4);
  650. currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
  651. if (ah->initPDADC == 0 || currPDADC == 0) {
  652. /*
  653. * Zero value indicates that no frames have been transmitted yet,
  654. * can't do temperature compensation until frames are transmitted.
  655. */
  656. return;
  657. } else {
  658. slope = ah->eep_ops->get_eeprom(ah, EEP_TEMPSENSE_SLOPE);
  659. if (slope == 0) { /* to avoid divide by zero case */
  660. delta = 0;
  661. } else {
  662. delta = ((currPDADC - ah->initPDADC)*4) / slope;
  663. }
  664. REG_RMW_FIELD(ah, AR_PHY_CH0_TX_PWRCTRL11,
  665. AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
  666. REG_RMW_FIELD(ah, AR_PHY_CH1_TX_PWRCTRL11,
  667. AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
  668. }
  669. }
  670. static void ath9k_olc_temp_compensation(struct ath_hw *ah)
  671. {
  672. u32 rddata, i;
  673. int delta, currPDADC, regval;
  674. if (OLC_FOR_AR9287_10_LATER) {
  675. ath9k_olc_temp_compensation_9287(ah);
  676. } else {
  677. rddata = REG_READ(ah, AR_PHY_TX_PWRCTRL4);
  678. currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
  679. if (ah->initPDADC == 0 || currPDADC == 0) {
  680. return;
  681. } else {
  682. if (ah->eep_ops->get_eeprom(ah, EEP_DAC_HPWR_5G))
  683. delta = (currPDADC - ah->initPDADC + 4) / 8;
  684. else
  685. delta = (currPDADC - ah->initPDADC + 5) / 10;
  686. if (delta != ah->PDADCdelta) {
  687. ah->PDADCdelta = delta;
  688. for (i = 1; i < AR9280_TX_GAIN_TABLE_SIZE; i++) {
  689. regval = ah->originalGain[i] - delta;
  690. if (regval < 0)
  691. regval = 0;
  692. REG_RMW_FIELD(ah,
  693. AR_PHY_TX_GAIN_TBL1 + i * 4,
  694. AR_PHY_TX_GAIN, regval);
  695. }
  696. }
  697. }
  698. }
  699. }
  700. static void ath9k_hw_9271_pa_cal(struct ath_hw *ah, bool is_reset)
  701. {
  702. u32 regVal;
  703. unsigned int i;
  704. u32 regList [][2] = {
  705. { 0x786c, 0 },
  706. { 0x7854, 0 },
  707. { 0x7820, 0 },
  708. { 0x7824, 0 },
  709. { 0x7868, 0 },
  710. { 0x783c, 0 },
  711. { 0x7838, 0 } ,
  712. { 0x7828, 0 } ,
  713. };
  714. for (i = 0; i < ARRAY_SIZE(regList); i++)
  715. regList[i][1] = REG_READ(ah, regList[i][0]);
  716. regVal = REG_READ(ah, 0x7834);
  717. regVal &= (~(0x1));
  718. REG_WRITE(ah, 0x7834, regVal);
  719. regVal = REG_READ(ah, 0x9808);
  720. regVal |= (0x1 << 27);
  721. REG_WRITE(ah, 0x9808, regVal);
  722. /* 786c,b23,1, pwddac=1 */
  723. REG_RMW_FIELD(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC, 1);
  724. /* 7854, b5,1, pdrxtxbb=1 */
  725. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1, 1);
  726. /* 7854, b7,1, pdv2i=1 */
  727. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I, 1);
  728. /* 7854, b8,1, pddacinterface=1 */
  729. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF, 1);
  730. /* 7824,b12,0, offcal=0 */
  731. REG_RMW_FIELD(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL, 0);
  732. /* 7838, b1,0, pwddb=0 */
  733. REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB, 0);
  734. /* 7820,b11,0, enpacal=0 */
  735. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL, 0);
  736. /* 7820,b25,1, pdpadrv1=0 */
  737. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1, 0);
  738. /* 7820,b24,0, pdpadrv2=0 */
  739. REG_RMW_FIELD(ah, AR9285_AN_RF2G1,AR9285_AN_RF2G1_PDPADRV2,0);
  740. /* 7820,b23,0, pdpaout=0 */
  741. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT, 0);
  742. /* 783c,b14-16,7, padrvgn2tab_0=7 */
  743. REG_RMW_FIELD(ah, AR9285_AN_RF2G8,AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
  744. /*
  745. * 7838,b29-31,0, padrvgn1tab_0=0
  746. * does not matter since we turn it off
  747. */
  748. REG_RMW_FIELD(ah, AR9285_AN_RF2G7,AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
  749. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9271_AN_RF2G3_CCOMP, 0xfff);
  750. /* Set:
  751. * localmode=1,bmode=1,bmoderxtx=1,synthon=1,
  752. * txon=1,paon=1,oscon=1,synthon_force=1
  753. */
  754. REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);
  755. udelay(30);
  756. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9271_AN_RF2G6_OFFS, 0);
  757. /* find off_6_1; */
  758. for (i = 6; i > 0; i--) {
  759. regVal = REG_READ(ah, 0x7834);
  760. regVal |= (1 << (20 + i));
  761. REG_WRITE(ah, 0x7834, regVal);
  762. udelay(1);
  763. //regVal = REG_READ(ah, 0x7834);
  764. regVal &= (~(0x1 << (20 + i)));
  765. regVal |= (MS(REG_READ(ah, 0x7840), AR9285_AN_RXTXBB1_SPARE9)
  766. << (20 + i));
  767. REG_WRITE(ah, 0x7834, regVal);
  768. }
  769. regVal = (regVal >>20) & 0x7f;
  770. /* Update PA cal info */
  771. if ((!is_reset) && (ah->pacal_info.prev_offset == regVal)) {
  772. if (ah->pacal_info.max_skipcount < MAX_PACAL_SKIPCOUNT)
  773. ah->pacal_info.max_skipcount =
  774. 2 * ah->pacal_info.max_skipcount;
  775. ah->pacal_info.skipcount = ah->pacal_info.max_skipcount;
  776. } else {
  777. ah->pacal_info.max_skipcount = 1;
  778. ah->pacal_info.skipcount = 0;
  779. ah->pacal_info.prev_offset = regVal;
  780. }
  781. regVal = REG_READ(ah, 0x7834);
  782. regVal |= 0x1;
  783. REG_WRITE(ah, 0x7834, regVal);
  784. regVal = REG_READ(ah, 0x9808);
  785. regVal &= (~(0x1 << 27));
  786. REG_WRITE(ah, 0x9808, regVal);
  787. for (i = 0; i < ARRAY_SIZE(regList); i++)
  788. REG_WRITE(ah, regList[i][0], regList[i][1]);
  789. }
  790. static inline void ath9k_hw_9285_pa_cal(struct ath_hw *ah, bool is_reset)
  791. {
  792. struct ath_common *common = ath9k_hw_common(ah);
  793. u32 regVal;
  794. int i, offset, offs_6_1, offs_0;
  795. u32 ccomp_org, reg_field;
  796. u32 regList[][2] = {
  797. { 0x786c, 0 },
  798. { 0x7854, 0 },
  799. { 0x7820, 0 },
  800. { 0x7824, 0 },
  801. { 0x7868, 0 },
  802. { 0x783c, 0 },
  803. { 0x7838, 0 },
  804. };
  805. ath_print(common, ATH_DBG_CALIBRATE, "Running PA Calibration\n");
  806. /* PA CAL is not needed for high power solution */
  807. if (ah->eep_ops->get_eeprom(ah, EEP_TXGAIN_TYPE) ==
  808. AR5416_EEP_TXGAIN_HIGH_POWER)
  809. return;
  810. if (AR_SREV_9285_11(ah)) {
  811. REG_WRITE(ah, AR9285_AN_TOP4, (AR9285_AN_TOP4_DEFAULT | 0x14));
  812. udelay(10);
  813. }
  814. for (i = 0; i < ARRAY_SIZE(regList); i++)
  815. regList[i][1] = REG_READ(ah, regList[i][0]);
  816. regVal = REG_READ(ah, 0x7834);
  817. regVal &= (~(0x1));
  818. REG_WRITE(ah, 0x7834, regVal);
  819. regVal = REG_READ(ah, 0x9808);
  820. regVal |= (0x1 << 27);
  821. REG_WRITE(ah, 0x9808, regVal);
  822. REG_RMW_FIELD(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC, 1);
  823. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1, 1);
  824. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I, 1);
  825. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF, 1);
  826. REG_RMW_FIELD(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL, 0);
  827. REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB, 0);
  828. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL, 0);
  829. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1, 0);
  830. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2, 0);
  831. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT, 0);
  832. REG_RMW_FIELD(ah, AR9285_AN_RF2G8, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
  833. REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
  834. ccomp_org = MS(REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_CCOMP);
  835. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, 0xf);
  836. REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);
  837. udelay(30);
  838. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, 0);
  839. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 0);
  840. for (i = 6; i > 0; i--) {
  841. regVal = REG_READ(ah, 0x7834);
  842. regVal |= (1 << (19 + i));
  843. REG_WRITE(ah, 0x7834, regVal);
  844. udelay(1);
  845. regVal = REG_READ(ah, 0x7834);
  846. regVal &= (~(0x1 << (19 + i)));
  847. reg_field = MS(REG_READ(ah, 0x7840), AR9285_AN_RXTXBB1_SPARE9);
  848. regVal |= (reg_field << (19 + i));
  849. REG_WRITE(ah, 0x7834, regVal);
  850. }
  851. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 1);
  852. udelay(1);
  853. reg_field = MS(REG_READ(ah, AR9285_AN_RF2G9), AR9285_AN_RXTXBB1_SPARE9);
  854. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, reg_field);
  855. offs_6_1 = MS(REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_OFFS);
  856. offs_0 = MS(REG_READ(ah, AR9285_AN_RF2G3), AR9285_AN_RF2G3_PDVCCOMP);
  857. offset = (offs_6_1<<1) | offs_0;
  858. offset = offset - 0;
  859. offs_6_1 = offset>>1;
  860. offs_0 = offset & 1;
  861. if ((!is_reset) && (ah->pacal_info.prev_offset == offset)) {
  862. if (ah->pacal_info.max_skipcount < MAX_PACAL_SKIPCOUNT)
  863. ah->pacal_info.max_skipcount =
  864. 2 * ah->pacal_info.max_skipcount;
  865. ah->pacal_info.skipcount = ah->pacal_info.max_skipcount;
  866. } else {
  867. ah->pacal_info.max_skipcount = 1;
  868. ah->pacal_info.skipcount = 0;
  869. ah->pacal_info.prev_offset = offset;
  870. }
  871. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, offs_6_1);
  872. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, offs_0);
  873. regVal = REG_READ(ah, 0x7834);
  874. regVal |= 0x1;
  875. REG_WRITE(ah, 0x7834, regVal);
  876. regVal = REG_READ(ah, 0x9808);
  877. regVal &= (~(0x1 << 27));
  878. REG_WRITE(ah, 0x9808, regVal);
  879. for (i = 0; i < ARRAY_SIZE(regList); i++)
  880. REG_WRITE(ah, regList[i][0], regList[i][1]);
  881. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, ccomp_org);
  882. if (AR_SREV_9285_11(ah))
  883. REG_WRITE(ah, AR9285_AN_TOP4, AR9285_AN_TOP4_DEFAULT);
  884. }
  885. bool ath9k_hw_calibrate(struct ath_hw *ah, struct ath9k_channel *chan,
  886. u8 rxchainmask, bool longcal)
  887. {
  888. bool iscaldone = true;
  889. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  890. if (currCal &&
  891. (currCal->calState == CAL_RUNNING ||
  892. currCal->calState == CAL_WAITING)) {
  893. iscaldone = ath9k_hw_per_calibration(ah, chan,
  894. rxchainmask, currCal);
  895. if (iscaldone) {
  896. ah->cal_list_curr = currCal = currCal->calNext;
  897. if (currCal->calState == CAL_WAITING) {
  898. iscaldone = false;
  899. ath9k_hw_reset_calibration(ah, currCal);
  900. }
  901. }
  902. }
  903. /* Do NF cal only at longer intervals */
  904. if (longcal) {
  905. /* Do periodic PAOffset Cal */
  906. if (AR_SREV_9271(ah)) {
  907. if (!ah->pacal_info.skipcount)
  908. ath9k_hw_9271_pa_cal(ah, false);
  909. else
  910. ah->pacal_info.skipcount--;
  911. } else if (AR_SREV_9285_11_OR_LATER(ah)) {
  912. if (!ah->pacal_info.skipcount)
  913. ath9k_hw_9285_pa_cal(ah, false);
  914. else
  915. ah->pacal_info.skipcount--;
  916. }
  917. if (OLC_FOR_AR9280_20_LATER || OLC_FOR_AR9287_10_LATER)
  918. ath9k_olc_temp_compensation(ah);
  919. /* Get the value from the previous NF cal and update history buffer */
  920. ath9k_hw_getnf(ah, chan);
  921. /*
  922. * Load the NF from history buffer of the current channel.
  923. * NF is slow time-variant, so it is OK to use a historical value.
  924. */
  925. ath9k_hw_loadnf(ah, ah->curchan);
  926. ath9k_hw_start_nfcal(ah);
  927. }
  928. return iscaldone;
  929. }
  930. EXPORT_SYMBOL(ath9k_hw_calibrate);
  931. /* Carrier leakage Calibration fix */
  932. static bool ar9285_cl_cal(struct ath_hw *ah, struct ath9k_channel *chan)
  933. {
  934. struct ath_common *common = ath9k_hw_common(ah);
  935. REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
  936. if (IS_CHAN_HT20(chan)) {
  937. REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
  938. REG_SET_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
  939. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  940. AR_PHY_AGC_CONTROL_FLTR_CAL);
  941. REG_CLR_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
  942. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
  943. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
  944. AR_PHY_AGC_CONTROL_CAL, 0, AH_WAIT_TIMEOUT)) {
  945. ath_print(common, ATH_DBG_CALIBRATE, "offset "
  946. "calibration failed to complete in "
  947. "1ms; noisy ??\n");
  948. return false;
  949. }
  950. REG_CLR_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
  951. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
  952. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
  953. }
  954. REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
  955. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
  956. REG_SET_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
  957. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
  958. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
  959. 0, AH_WAIT_TIMEOUT)) {
  960. ath_print(common, ATH_DBG_CALIBRATE, "offset calibration "
  961. "failed to complete in 1ms; noisy ??\n");
  962. return false;
  963. }
  964. REG_SET_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
  965. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
  966. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
  967. return true;
  968. }
  969. static bool ar9285_clc(struct ath_hw *ah, struct ath9k_channel *chan)
  970. {
  971. int i;
  972. u_int32_t txgain_max;
  973. u_int32_t clc_gain, gain_mask = 0, clc_num = 0;
  974. u_int32_t reg_clc_I0, reg_clc_Q0;
  975. u_int32_t i0_num = 0;
  976. u_int32_t q0_num = 0;
  977. u_int32_t total_num = 0;
  978. u_int32_t reg_rf2g5_org;
  979. bool retv = true;
  980. if (!(ar9285_cl_cal(ah, chan)))
  981. return false;
  982. txgain_max = MS(REG_READ(ah, AR_PHY_TX_PWRCTRL7),
  983. AR_PHY_TX_PWRCTRL_TX_GAIN_TAB_MAX);
  984. for (i = 0; i < (txgain_max+1); i++) {
  985. clc_gain = (REG_READ(ah, (AR_PHY_TX_GAIN_TBL1+(i<<2))) &
  986. AR_PHY_TX_GAIN_CLC) >> AR_PHY_TX_GAIN_CLC_S;
  987. if (!(gain_mask & (1 << clc_gain))) {
  988. gain_mask |= (1 << clc_gain);
  989. clc_num++;
  990. }
  991. }
  992. for (i = 0; i < clc_num; i++) {
  993. reg_clc_I0 = (REG_READ(ah, (AR_PHY_CLC_TBL1 + (i << 2)))
  994. & AR_PHY_CLC_I0) >> AR_PHY_CLC_I0_S;
  995. reg_clc_Q0 = (REG_READ(ah, (AR_PHY_CLC_TBL1 + (i << 2)))
  996. & AR_PHY_CLC_Q0) >> AR_PHY_CLC_Q0_S;
  997. if (reg_clc_I0 == 0)
  998. i0_num++;
  999. if (reg_clc_Q0 == 0)
  1000. q0_num++;
  1001. }
  1002. total_num = i0_num + q0_num;
  1003. if (total_num > AR9285_CLCAL_REDO_THRESH) {
  1004. reg_rf2g5_org = REG_READ(ah, AR9285_RF2G5);
  1005. if (AR_SREV_9285E_20(ah)) {
  1006. REG_WRITE(ah, AR9285_RF2G5,
  1007. (reg_rf2g5_org & AR9285_RF2G5_IC50TX) |
  1008. AR9285_RF2G5_IC50TX_XE_SET);
  1009. } else {
  1010. REG_WRITE(ah, AR9285_RF2G5,
  1011. (reg_rf2g5_org & AR9285_RF2G5_IC50TX) |
  1012. AR9285_RF2G5_IC50TX_SET);
  1013. }
  1014. retv = ar9285_cl_cal(ah, chan);
  1015. REG_WRITE(ah, AR9285_RF2G5, reg_rf2g5_org);
  1016. }
  1017. return retv;
  1018. }
  1019. bool ath9k_hw_init_cal(struct ath_hw *ah, struct ath9k_channel *chan)
  1020. {
  1021. struct ath_common *common = ath9k_hw_common(ah);
  1022. if (AR_SREV_9271(ah) || AR_SREV_9285_12_OR_LATER(ah)) {
  1023. if (!ar9285_clc(ah, chan))
  1024. return false;
  1025. } else {
  1026. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1027. if (!AR_SREV_9287_10_OR_LATER(ah))
  1028. REG_CLR_BIT(ah, AR_PHY_ADC_CTL,
  1029. AR_PHY_ADC_CTL_OFF_PWDADC);
  1030. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  1031. AR_PHY_AGC_CONTROL_FLTR_CAL);
  1032. }
  1033. /* Calibrate the AGC */
  1034. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  1035. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  1036. AR_PHY_AGC_CONTROL_CAL);
  1037. /* Poll for offset calibration complete */
  1038. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
  1039. 0, AH_WAIT_TIMEOUT)) {
  1040. ath_print(common, ATH_DBG_CALIBRATE,
  1041. "offset calibration failed to "
  1042. "complete in 1ms; noisy environment?\n");
  1043. return false;
  1044. }
  1045. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1046. if (!AR_SREV_9287_10_OR_LATER(ah))
  1047. REG_SET_BIT(ah, AR_PHY_ADC_CTL,
  1048. AR_PHY_ADC_CTL_OFF_PWDADC);
  1049. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  1050. AR_PHY_AGC_CONTROL_FLTR_CAL);
  1051. }
  1052. }
  1053. /* Do PA Calibration */
  1054. if (AR_SREV_9271(ah))
  1055. ath9k_hw_9271_pa_cal(ah, true);
  1056. else if (AR_SREV_9285_11_OR_LATER(ah))
  1057. ath9k_hw_9285_pa_cal(ah, true);
  1058. /* Do NF Calibration after DC offset and other calibrations */
  1059. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  1060. REG_READ(ah, AR_PHY_AGC_CONTROL) | AR_PHY_AGC_CONTROL_NF);
  1061. ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
  1062. /* Enable IQ, ADC Gain and ADC DC offset CALs */
  1063. if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah)) {
  1064. if (ath9k_hw_iscal_supported(ah, ADC_GAIN_CAL)) {
  1065. INIT_CAL(&ah->adcgain_caldata);
  1066. INSERT_CAL(ah, &ah->adcgain_caldata);
  1067. ath_print(common, ATH_DBG_CALIBRATE,
  1068. "enabling ADC Gain Calibration.\n");
  1069. }
  1070. if (ath9k_hw_iscal_supported(ah, ADC_DC_CAL)) {
  1071. INIT_CAL(&ah->adcdc_caldata);
  1072. INSERT_CAL(ah, &ah->adcdc_caldata);
  1073. ath_print(common, ATH_DBG_CALIBRATE,
  1074. "enabling ADC DC Calibration.\n");
  1075. }
  1076. if (ath9k_hw_iscal_supported(ah, IQ_MISMATCH_CAL)) {
  1077. INIT_CAL(&ah->iq_caldata);
  1078. INSERT_CAL(ah, &ah->iq_caldata);
  1079. ath_print(common, ATH_DBG_CALIBRATE,
  1080. "enabling IQ Calibration.\n");
  1081. }
  1082. ah->cal_list_curr = ah->cal_list;
  1083. if (ah->cal_list_curr)
  1084. ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
  1085. }
  1086. chan->CalValid = 0;
  1087. return true;
  1088. }
  1089. const struct ath9k_percal_data iq_cal_multi_sample = {
  1090. IQ_MISMATCH_CAL,
  1091. MAX_CAL_SAMPLES,
  1092. PER_MIN_LOG_COUNT,
  1093. ath9k_hw_iqcal_collect,
  1094. ath9k_hw_iqcalibrate
  1095. };
  1096. const struct ath9k_percal_data iq_cal_single_sample = {
  1097. IQ_MISMATCH_CAL,
  1098. MIN_CAL_SAMPLES,
  1099. PER_MAX_LOG_COUNT,
  1100. ath9k_hw_iqcal_collect,
  1101. ath9k_hw_iqcalibrate
  1102. };
  1103. const struct ath9k_percal_data adc_gain_cal_multi_sample = {
  1104. ADC_GAIN_CAL,
  1105. MAX_CAL_SAMPLES,
  1106. PER_MIN_LOG_COUNT,
  1107. ath9k_hw_adc_gaincal_collect,
  1108. ath9k_hw_adc_gaincal_calibrate
  1109. };
  1110. const struct ath9k_percal_data adc_gain_cal_single_sample = {
  1111. ADC_GAIN_CAL,
  1112. MIN_CAL_SAMPLES,
  1113. PER_MAX_LOG_COUNT,
  1114. ath9k_hw_adc_gaincal_collect,
  1115. ath9k_hw_adc_gaincal_calibrate
  1116. };
  1117. const struct ath9k_percal_data adc_dc_cal_multi_sample = {
  1118. ADC_DC_CAL,
  1119. MAX_CAL_SAMPLES,
  1120. PER_MIN_LOG_COUNT,
  1121. ath9k_hw_adc_dccal_collect,
  1122. ath9k_hw_adc_dccal_calibrate
  1123. };
  1124. const struct ath9k_percal_data adc_dc_cal_single_sample = {
  1125. ADC_DC_CAL,
  1126. MIN_CAL_SAMPLES,
  1127. PER_MAX_LOG_COUNT,
  1128. ath9k_hw_adc_dccal_collect,
  1129. ath9k_hw_adc_dccal_calibrate
  1130. };
  1131. const struct ath9k_percal_data adc_init_dc_cal = {
  1132. ADC_DC_INIT_CAL,
  1133. MIN_CAL_SAMPLES,
  1134. INIT_LOG_COUNT,
  1135. ath9k_hw_adc_dccal_collect,
  1136. ath9k_hw_adc_dccal_calibrate
  1137. };