Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 1 | /* |
Sujith | cee075a | 2009-03-13 09:07:23 +0530 | [diff] [blame] | 2 | * Copyright (c) 2008-2009 Atheros Communications Inc. |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 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 | |
Luis R. Rodriguez | c46917b | 2009-09-13 02:42:02 -0700 | [diff] [blame] | 17 | #include "hw.h" |
Felix Fietkau | 641d992 | 2010-04-15 17:38:49 -0400 | [diff] [blame] | 18 | #include "hw-ops.h" |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 19 | |
Luis R. Rodriguez | 795f5e2 | 2010-04-15 17:39:00 -0400 | [diff] [blame] | 20 | /* Common calibration code */ |
| 21 | |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 22 | /* We can tune this as we go by monitoring really low values */ |
| 23 | #define ATH9K_NF_TOO_LOW -60 |
| 24 | |
| 25 | /* AR5416 may return very high value (like -31 dBm), in those cases the nf |
| 26 | * is incorrect and we should use the static NF value. Later we can try to |
| 27 | * find out why they are reporting these values */ |
| 28 | |
Sujith | cbe61d8 | 2009-02-09 13:27:12 +0530 | [diff] [blame] | 29 | static bool ath9k_hw_nf_in_range(struct ath_hw *ah, s16 nf) |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 30 | { |
| 31 | if (nf > ATH9K_NF_TOO_LOW) { |
Luis R. Rodriguez | c46917b | 2009-09-13 02:42:02 -0700 | [diff] [blame] | 32 | ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE, |
| 33 | "noise floor value detected (%d) is " |
| 34 | "lower than what we think is a " |
| 35 | "reasonable value (%d)\n", |
| 36 | nf, ATH9K_NF_TOO_LOW); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer) |
| 43 | { |
| 44 | int16_t nfval; |
| 45 | int16_t sort[ATH9K_NF_CAL_HIST_MAX]; |
| 46 | int i, j; |
| 47 | |
| 48 | for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++) |
| 49 | sort[i] = nfCalBuffer[i]; |
| 50 | |
| 51 | for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) { |
| 52 | for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) { |
| 53 | if (sort[j] > sort[j - 1]) { |
| 54 | nfval = sort[j]; |
| 55 | sort[j] = sort[j - 1]; |
| 56 | sort[j - 1] = nfval; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1]; |
| 61 | |
| 62 | return nfval; |
| 63 | } |
| 64 | |
| 65 | static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h, |
| 66 | int16_t *nfarray) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < NUM_NF_READINGS; i++) { |
| 71 | h[i].nfCalBuffer[h[i].currIndex] = nfarray[i]; |
| 72 | |
| 73 | if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX) |
| 74 | h[i].currIndex = 0; |
| 75 | |
| 76 | if (h[i].invalidNFcount > 0) { |
| 77 | if (nfarray[i] < AR_PHY_CCA_MIN_BAD_VALUE || |
| 78 | nfarray[i] > AR_PHY_CCA_MAX_HIGH_VALUE) { |
| 79 | h[i].invalidNFcount = ATH9K_NF_CAL_HIST_MAX; |
| 80 | } else { |
| 81 | h[i].invalidNFcount--; |
| 82 | h[i].privNF = nfarray[i]; |
| 83 | } |
| 84 | } else { |
| 85 | h[i].privNF = |
| 86 | ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer); |
| 87 | } |
| 88 | } |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 89 | } |
| 90 | |
Luis R. Rodriguez | b43d59f | 2010-04-15 17:38:58 -0400 | [diff] [blame] | 91 | static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah, |
| 92 | enum ieee80211_band band, |
| 93 | int16_t *nft) |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 94 | { |
Luis R. Rodriguez | 76061ab | 2008-12-23 15:58:41 -0800 | [diff] [blame] | 95 | switch (band) { |
| 96 | case IEEE80211_BAND_5GHZ: |
Sujith | f74df6f | 2009-02-09 13:27:24 +0530 | [diff] [blame] | 97 | *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 98 | break; |
Luis R. Rodriguez | 76061ab | 2008-12-23 15:58:41 -0800 | [diff] [blame] | 99 | case IEEE80211_BAND_2GHZ: |
Sujith | f74df6f | 2009-02-09 13:27:24 +0530 | [diff] [blame] | 100 | *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 101 | break; |
| 102 | default: |
Luis R. Rodriguez | 76061ab | 2008-12-23 15:58:41 -0800 | [diff] [blame] | 103 | BUG_ON(1); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
Luis R. Rodriguez | 795f5e2 | 2010-04-15 17:39:00 -0400 | [diff] [blame] | 110 | void ath9k_hw_reset_calibration(struct ath_hw *ah, |
| 111 | struct ath9k_cal_list *currCal) |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 112 | { |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 113 | int i; |
| 114 | |
| 115 | ath9k_hw_setup_calibration(ah, currCal); |
| 116 | |
| 117 | currCal->calState = CAL_RUNNING; |
| 118 | |
| 119 | for (i = 0; i < AR5416_MAX_CHAINS; i++) { |
Sujith | 2660b81 | 2009-02-09 13:27:26 +0530 | [diff] [blame] | 120 | ah->meas0.sign[i] = 0; |
| 121 | ah->meas1.sign[i] = 0; |
| 122 | ah->meas2.sign[i] = 0; |
| 123 | ah->meas3.sign[i] = 0; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 124 | } |
| 125 | |
Sujith | 2660b81 | 2009-02-09 13:27:26 +0530 | [diff] [blame] | 126 | ah->cal_samples = 0; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 127 | } |
| 128 | |
Luis R. Rodriguez | c9e27d9 | 2008-12-23 15:58:42 -0800 | [diff] [blame] | 129 | /* This is done for the currently configured channel */ |
Sujith | cbe61d8 | 2009-02-09 13:27:12 +0530 | [diff] [blame] | 130 | bool ath9k_hw_reset_calvalid(struct ath_hw *ah) |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 131 | { |
Luis R. Rodriguez | c46917b | 2009-09-13 02:42:02 -0700 | [diff] [blame] | 132 | struct ath_common *common = ath9k_hw_common(ah); |
| 133 | struct ieee80211_conf *conf = &common->hw->conf; |
Sujith | cbfe946 | 2009-04-13 21:56:56 +0530 | [diff] [blame] | 134 | struct ath9k_cal_list *currCal = ah->cal_list_curr; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 135 | |
Sujith | 2660b81 | 2009-02-09 13:27:26 +0530 | [diff] [blame] | 136 | if (!ah->curchan) |
Luis R. Rodriguez | c9e27d9 | 2008-12-23 15:58:42 -0800 | [diff] [blame] | 137 | return true; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 138 | |
| 139 | if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah)) |
Luis R. Rodriguez | c9e27d9 | 2008-12-23 15:58:42 -0800 | [diff] [blame] | 140 | return true; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 141 | |
| 142 | if (currCal == NULL) |
Luis R. Rodriguez | c9e27d9 | 2008-12-23 15:58:42 -0800 | [diff] [blame] | 143 | return true; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 144 | |
| 145 | if (currCal->calState != CAL_DONE) { |
Luis R. Rodriguez | c46917b | 2009-09-13 02:42:02 -0700 | [diff] [blame] | 146 | ath_print(common, ATH_DBG_CALIBRATE, |
| 147 | "Calibration state incorrect, %d\n", |
| 148 | currCal->calState); |
Luis R. Rodriguez | c9e27d9 | 2008-12-23 15:58:42 -0800 | [diff] [blame] | 149 | return true; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 150 | } |
| 151 | |
Luis R. Rodriguez | c9e27d9 | 2008-12-23 15:58:42 -0800 | [diff] [blame] | 152 | if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType)) |
| 153 | return true; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 154 | |
Luis R. Rodriguez | c46917b | 2009-09-13 02:42:02 -0700 | [diff] [blame] | 155 | ath_print(common, ATH_DBG_CALIBRATE, |
| 156 | "Resetting Cal %d state for channel %u\n", |
| 157 | currCal->calData->calType, conf->channel->center_freq); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 158 | |
Sujith | 2660b81 | 2009-02-09 13:27:26 +0530 | [diff] [blame] | 159 | ah->curchan->CalValid &= ~currCal->calData->calType; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 160 | currCal->calState = CAL_WAITING; |
| 161 | |
Luis R. Rodriguez | c9e27d9 | 2008-12-23 15:58:42 -0800 | [diff] [blame] | 162 | return false; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 163 | } |
Luis R. Rodriguez | 7322fd1 | 2009-09-23 23:07:00 -0400 | [diff] [blame] | 164 | EXPORT_SYMBOL(ath9k_hw_reset_calvalid); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 165 | |
Sujith | cbe61d8 | 2009-02-09 13:27:12 +0530 | [diff] [blame] | 166 | void ath9k_hw_start_nfcal(struct ath_hw *ah) |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 167 | { |
| 168 | REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, |
| 169 | AR_PHY_AGC_CONTROL_ENABLE_NF); |
| 170 | REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, |
| 171 | AR_PHY_AGC_CONTROL_NO_UPDATE_NF); |
| 172 | REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF); |
| 173 | } |
| 174 | |
Sujith | cbe61d8 | 2009-02-09 13:27:12 +0530 | [diff] [blame] | 175 | int16_t ath9k_hw_getnf(struct ath_hw *ah, |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 176 | struct ath9k_channel *chan) |
| 177 | { |
Luis R. Rodriguez | c46917b | 2009-09-13 02:42:02 -0700 | [diff] [blame] | 178 | struct ath_common *common = ath9k_hw_common(ah); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 179 | int16_t nf, nfThresh; |
| 180 | int16_t nfarray[NUM_NF_READINGS] = { 0 }; |
| 181 | struct ath9k_nfcal_hist *h; |
Luis R. Rodriguez | 76061ab | 2008-12-23 15:58:41 -0800 | [diff] [blame] | 182 | struct ieee80211_channel *c = chan->chan; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 183 | |
| 184 | chan->channelFlags &= (~CHANNEL_CW_INT); |
| 185 | if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) { |
Luis R. Rodriguez | c46917b | 2009-09-13 02:42:02 -0700 | [diff] [blame] | 186 | ath_print(common, ATH_DBG_CALIBRATE, |
| 187 | "NF did not complete in calibration window\n"); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 188 | nf = 0; |
| 189 | chan->rawNoiseFloor = nf; |
| 190 | return chan->rawNoiseFloor; |
| 191 | } else { |
| 192 | ath9k_hw_do_getnf(ah, nfarray); |
| 193 | nf = nfarray[0]; |
Luis R. Rodriguez | b43d59f | 2010-04-15 17:38:58 -0400 | [diff] [blame] | 194 | if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh) |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 195 | && nf > nfThresh) { |
Luis R. Rodriguez | c46917b | 2009-09-13 02:42:02 -0700 | [diff] [blame] | 196 | ath_print(common, ATH_DBG_CALIBRATE, |
| 197 | "noise floor failed detected; " |
| 198 | "detected %d, threshold %d\n", |
| 199 | nf, nfThresh); |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 200 | chan->channelFlags |= CHANNEL_CW_INT; |
| 201 | } |
| 202 | } |
| 203 | |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 204 | h = ah->nfCalHist; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 205 | |
| 206 | ath9k_hw_update_nfcal_hist_buffer(h, nfarray); |
| 207 | chan->rawNoiseFloor = h[0].privNF; |
| 208 | |
| 209 | return chan->rawNoiseFloor; |
| 210 | } |
| 211 | |
Sujith | cbe61d8 | 2009-02-09 13:27:12 +0530 | [diff] [blame] | 212 | void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah) |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 213 | { |
| 214 | int i, j; |
Senthil Balasubramanian | a59b5a5 | 2009-07-14 20:17:07 -0400 | [diff] [blame] | 215 | s16 noise_floor; |
| 216 | |
| 217 | if (AR_SREV_9280(ah)) |
| 218 | noise_floor = AR_PHY_CCA_MAX_AR9280_GOOD_VALUE; |
Sujith | 6398dc0 | 2010-03-17 14:25:19 +0530 | [diff] [blame] | 219 | else if (AR_SREV_9285(ah) || AR_SREV_9271(ah)) |
Senthil Balasubramanian | a59b5a5 | 2009-07-14 20:17:07 -0400 | [diff] [blame] | 220 | noise_floor = AR_PHY_CCA_MAX_AR9285_GOOD_VALUE; |
Vivek Natarajan | 6170cd5 | 2009-09-17 09:24:24 +0530 | [diff] [blame] | 221 | else if (AR_SREV_9287(ah)) |
| 222 | noise_floor = AR_PHY_CCA_MAX_AR9287_GOOD_VALUE; |
Senthil Balasubramanian | a59b5a5 | 2009-07-14 20:17:07 -0400 | [diff] [blame] | 223 | else |
| 224 | noise_floor = AR_PHY_CCA_MAX_AR5416_GOOD_VALUE; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 225 | |
| 226 | for (i = 0; i < NUM_NF_READINGS; i++) { |
| 227 | ah->nfCalHist[i].currIndex = 0; |
Senthil Balasubramanian | a59b5a5 | 2009-07-14 20:17:07 -0400 | [diff] [blame] | 228 | ah->nfCalHist[i].privNF = noise_floor; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 229 | ah->nfCalHist[i].invalidNFcount = |
| 230 | AR_PHY_CCA_FILTERWINDOW_LENGTH; |
| 231 | for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) { |
Senthil Balasubramanian | a59b5a5 | 2009-07-14 20:17:07 -0400 | [diff] [blame] | 232 | ah->nfCalHist[i].nfCalBuffer[j] = noise_floor; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 233 | } |
| 234 | } |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 235 | } |
| 236 | |
Sujith | cbe61d8 | 2009-02-09 13:27:12 +0530 | [diff] [blame] | 237 | s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan) |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 238 | { |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 239 | s16 nf; |
| 240 | |
Luis R. Rodriguez | 5f8e077 | 2009-01-22 15:16:48 -0800 | [diff] [blame] | 241 | if (chan->rawNoiseFloor == 0) |
Luis R. Rodriguez | e56db71 | 2008-12-23 15:58:47 -0800 | [diff] [blame] | 242 | nf = -96; |
| 243 | else |
Luis R. Rodriguez | 5f8e077 | 2009-01-22 15:16:48 -0800 | [diff] [blame] | 244 | nf = chan->rawNoiseFloor; |
Sujith | f1dc560 | 2008-10-29 10:16:30 +0530 | [diff] [blame] | 245 | |
| 246 | if (!ath9k_hw_nf_in_range(ah, nf)) |
| 247 | nf = ATH_DEFAULT_NOISE_FLOOR; |
| 248 | |
| 249 | return nf; |
| 250 | } |
Luis R. Rodriguez | 7322fd1 | 2009-09-23 23:07:00 -0400 | [diff] [blame] | 251 | EXPORT_SYMBOL(ath9k_hw_getchan_noise); |