calib.c 33 KB

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