Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2008-2011 Atheros Communications Inc.
0003  *
0004  * Permission to use, copy, modify, and/or distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 
0017 #include "hw.h"
0018 #include "hw-ops.h"
0019 #include "../regd.h"
0020 #include "ar9002_phy.h"
0021 
0022 /* All code below is for AR5008, AR9001, AR9002 */
0023 
0024 #define AR5008_OFDM_RATES       8
0025 #define AR5008_HT_SS_RATES      8
0026 #define AR5008_HT_DS_RATES      8
0027 
0028 #define AR5008_HT20_SHIFT       16
0029 #define AR5008_HT40_SHIFT       24
0030 
0031 #define AR5008_11NA_OFDM_SHIFT      0
0032 #define AR5008_11NA_HT_SS_SHIFT     8
0033 #define AR5008_11NA_HT_DS_SHIFT     16
0034 
0035 #define AR5008_11NG_OFDM_SHIFT      4
0036 #define AR5008_11NG_HT_SS_SHIFT     12
0037 #define AR5008_11NG_HT_DS_SHIFT     20
0038 
0039 /*
0040  * register values to turn OFDM weak signal detection OFF
0041  */
0042 static const int m1ThreshLow_off = 127;
0043 static const int m2ThreshLow_off = 127;
0044 static const int m1Thresh_off = 127;
0045 static const int m2Thresh_off = 127;
0046 static const int m2CountThr_off =  31;
0047 static const int m2CountThrLow_off =  63;
0048 static const int m1ThreshLowExt_off = 127;
0049 static const int m2ThreshLowExt_off = 127;
0050 static const int m1ThreshExt_off = 127;
0051 static const int m2ThreshExt_off = 127;
0052 
0053 static const u32 ar5416Bank0[][2] = {
0054     /* Addr      allmodes  */
0055     {0x000098b0, 0x1e5795e5},
0056     {0x000098e0, 0x02008020},
0057 };
0058 
0059 static const u32 ar5416Bank1[][2] = {
0060     /* Addr      allmodes  */
0061     {0x000098b0, 0x02108421},
0062     {0x000098ec, 0x00000008},
0063 };
0064 
0065 static const u32 ar5416Bank2[][2] = {
0066     /* Addr      allmodes  */
0067     {0x000098b0, 0x0e73ff17},
0068     {0x000098e0, 0x00000420},
0069 };
0070 
0071 static const u32 ar5416Bank3[][3] = {
0072     /* Addr      5G          2G        */
0073     {0x000098f0, 0x01400018, 0x01c00018},
0074 };
0075 
0076 static const u32 ar5416Bank7[][2] = {
0077     /* Addr      allmodes  */
0078     {0x0000989c, 0x00000500},
0079     {0x0000989c, 0x00000800},
0080     {0x000098cc, 0x0000000e},
0081 };
0082 
0083 static const struct ar5416IniArray bank0 = STATIC_INI_ARRAY(ar5416Bank0);
0084 static const struct ar5416IniArray bank1 = STATIC_INI_ARRAY(ar5416Bank1);
0085 static const struct ar5416IniArray bank2 = STATIC_INI_ARRAY(ar5416Bank2);
0086 static const struct ar5416IniArray bank3 = STATIC_INI_ARRAY(ar5416Bank3);
0087 static const struct ar5416IniArray bank7 = STATIC_INI_ARRAY(ar5416Bank7);
0088 
0089 static void ar5008_write_bank6(struct ath_hw *ah, unsigned int *writecnt)
0090 {
0091     struct ar5416IniArray *array = &ah->iniBank6;
0092     u32 *data = ah->analogBank6Data;
0093     int r;
0094 
0095     ENABLE_REGWRITE_BUFFER(ah);
0096 
0097     for (r = 0; r < array->ia_rows; r++) {
0098         REG_WRITE(ah, INI_RA(array, r, 0), data[r]);
0099         DO_DELAY(*writecnt);
0100     }
0101 
0102     REGWRITE_BUFFER_FLUSH(ah);
0103 }
0104 
0105 /*
0106  * ar5008_hw_phy_modify_rx_buffer() - perform analog swizzling of parameters
0107  *
0108  * Performs analog "swizzling" of parameters into their location.
0109  * Used on external AR2133/AR5133 radios.
0110  */
0111 static void ar5008_hw_phy_modify_rx_buffer(u32 *rfBuf, u32 reg32,
0112                        u32 numBits, u32 firstBit,
0113                        u32 column)
0114 {
0115     u32 tmp32, mask, arrayEntry, lastBit;
0116     int32_t bitPosition, bitsLeft;
0117 
0118     tmp32 = ath9k_hw_reverse_bits(reg32, numBits);
0119     arrayEntry = (firstBit - 1) / 8;
0120     bitPosition = (firstBit - 1) % 8;
0121     bitsLeft = numBits;
0122     while (bitsLeft > 0) {
0123         lastBit = (bitPosition + bitsLeft > 8) ?
0124             8 : bitPosition + bitsLeft;
0125         mask = (((1 << lastBit) - 1) ^ ((1 << bitPosition) - 1)) <<
0126             (column * 8);
0127         rfBuf[arrayEntry] &= ~mask;
0128         rfBuf[arrayEntry] |= ((tmp32 << bitPosition) <<
0129                       (column * 8)) & mask;
0130         bitsLeft -= 8 - bitPosition;
0131         tmp32 = tmp32 >> (8 - bitPosition);
0132         bitPosition = 0;
0133         arrayEntry++;
0134     }
0135 }
0136 
0137 /*
0138  * Fix on 2.4 GHz band for orientation sensitivity issue by increasing
0139  * rf_pwd_icsyndiv.
0140  *
0141  * Theoretical Rules:
0142  *   if 2 GHz band
0143  *      if forceBiasAuto
0144  *         if synth_freq < 2412
0145  *            bias = 0
0146  *         else if 2412 <= synth_freq <= 2422
0147  *            bias = 1
0148  *         else // synth_freq > 2422
0149  *            bias = 2
0150  *      else if forceBias > 0
0151  *         bias = forceBias & 7
0152  *      else
0153  *         no change, use value from ini file
0154  *   else
0155  *      no change, invalid band
0156  *
0157  *  1st Mod:
0158  *    2422 also uses value of 2
0159  *    <approved>
0160  *
0161  *  2nd Mod:
0162  *    Less than 2412 uses value of 0, 2412 and above uses value of 2
0163  */
0164 static void ar5008_hw_force_bias(struct ath_hw *ah, u16 synth_freq)
0165 {
0166     struct ath_common *common = ath9k_hw_common(ah);
0167     u32 tmp_reg;
0168     int reg_writes = 0;
0169     u32 new_bias = 0;
0170 
0171     if (!AR_SREV_5416(ah) || synth_freq >= 3000)
0172         return;
0173 
0174     BUG_ON(AR_SREV_9280_20_OR_LATER(ah));
0175 
0176     if (synth_freq < 2412)
0177         new_bias = 0;
0178     else if (synth_freq < 2422)
0179         new_bias = 1;
0180     else
0181         new_bias = 2;
0182 
0183     /* pre-reverse this field */
0184     tmp_reg = ath9k_hw_reverse_bits(new_bias, 3);
0185 
0186     ath_dbg(common, CONFIG, "Force rf_pwd_icsyndiv to %1d on %4d\n",
0187         new_bias, synth_freq);
0188 
0189     /* swizzle rf_pwd_icsyndiv */
0190     ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data, tmp_reg, 3, 181, 3);
0191 
0192     /* write Bank 6 with new params */
0193     ar5008_write_bank6(ah, &reg_writes);
0194 }
0195 
0196 /*
0197  * ar5008_hw_set_channel - tune to a channel on the external AR2133/AR5133 radios
0198  *
0199  * For the external AR2133/AR5133 radios, takes the MHz channel value and set
0200  * the channel value. Assumes writes enabled to analog bus and bank6 register
0201  * cache in ah->analogBank6Data.
0202  */
0203 static int ar5008_hw_set_channel(struct ath_hw *ah, struct ath9k_channel *chan)
0204 {
0205     struct ath_common *common = ath9k_hw_common(ah);
0206     u32 channelSel = 0;
0207     u32 bModeSynth = 0;
0208     u32 aModeRefSel = 0;
0209     u32 reg32 = 0;
0210     u16 freq;
0211     struct chan_centers centers;
0212 
0213     ath9k_hw_get_channel_centers(ah, chan, &centers);
0214     freq = centers.synth_center;
0215 
0216     if (freq < 4800) {
0217         u32 txctl;
0218 
0219         if (((freq - 2192) % 5) == 0) {
0220             channelSel = ((freq - 672) * 2 - 3040) / 10;
0221             bModeSynth = 0;
0222         } else if (((freq - 2224) % 5) == 0) {
0223             channelSel = ((freq - 704) * 2 - 3040) / 10;
0224             bModeSynth = 1;
0225         } else {
0226             ath_err(common, "Invalid channel %u MHz\n", freq);
0227             return -EINVAL;
0228         }
0229 
0230         channelSel = (channelSel << 2) & 0xff;
0231         channelSel = ath9k_hw_reverse_bits(channelSel, 8);
0232 
0233         txctl = REG_READ(ah, AR_PHY_CCK_TX_CTRL);
0234         if (freq == 2484) {
0235 
0236             REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
0237                   txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
0238         } else {
0239             REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
0240                   txctl & ~AR_PHY_CCK_TX_CTRL_JAPAN);
0241         }
0242 
0243     } else if ((freq % 20) == 0 && freq >= 5120) {
0244         channelSel =
0245             ath9k_hw_reverse_bits(((freq - 4800) / 20 << 2), 8);
0246         aModeRefSel = ath9k_hw_reverse_bits(1, 2);
0247     } else if ((freq % 10) == 0) {
0248         channelSel =
0249             ath9k_hw_reverse_bits(((freq - 4800) / 10 << 1), 8);
0250         if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah))
0251             aModeRefSel = ath9k_hw_reverse_bits(2, 2);
0252         else
0253             aModeRefSel = ath9k_hw_reverse_bits(1, 2);
0254     } else if ((freq % 5) == 0) {
0255         channelSel = ath9k_hw_reverse_bits((freq - 4800) / 5, 8);
0256         aModeRefSel = ath9k_hw_reverse_bits(1, 2);
0257     } else {
0258         ath_err(common, "Invalid channel %u MHz\n", freq);
0259         return -EINVAL;
0260     }
0261 
0262     ar5008_hw_force_bias(ah, freq);
0263 
0264     reg32 =
0265         (channelSel << 8) | (aModeRefSel << 2) | (bModeSynth << 1) |
0266         (1 << 5) | 0x1;
0267 
0268     REG_WRITE(ah, AR_PHY(0x37), reg32);
0269 
0270     ah->curchan = chan;
0271 
0272     return 0;
0273 }
0274 
0275 void ar5008_hw_cmn_spur_mitigate(struct ath_hw *ah,
0276               struct ath9k_channel *chan, int bin)
0277 {
0278     int cur_bin;
0279     int upper, lower, cur_vit_mask;
0280     int i;
0281     int8_t mask_m[123] = {0};
0282     int8_t mask_p[123] = {0};
0283     int8_t mask_amt;
0284     int tmp_mask;
0285     static const int pilot_mask_reg[4] = {
0286         AR_PHY_TIMING7, AR_PHY_TIMING8,
0287         AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60
0288     };
0289     static const int chan_mask_reg[4] = {
0290         AR_PHY_TIMING9, AR_PHY_TIMING10,
0291         AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60
0292     };
0293     static const int inc[4] = { 0, 100, 0, 0 };
0294 
0295     cur_bin = -6000;
0296     upper = bin + 100;
0297     lower = bin - 100;
0298 
0299     for (i = 0; i < 4; i++) {
0300         int pilot_mask = 0;
0301         int chan_mask = 0;
0302         int bp = 0;
0303 
0304         for (bp = 0; bp < 30; bp++) {
0305             if ((cur_bin > lower) && (cur_bin < upper)) {
0306                 pilot_mask = pilot_mask | 0x1 << bp;
0307                 chan_mask = chan_mask | 0x1 << bp;
0308             }
0309             cur_bin += 100;
0310         }
0311         cur_bin += inc[i];
0312         REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
0313         REG_WRITE(ah, chan_mask_reg[i], chan_mask);
0314     }
0315 
0316     cur_vit_mask = 6100;
0317     upper = bin + 120;
0318     lower = bin - 120;
0319 
0320     for (i = 0; i < ARRAY_SIZE(mask_m); i++) {
0321         if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
0322             /* workaround for gcc bug #37014 */
0323             volatile int tmp_v = abs(cur_vit_mask - bin);
0324 
0325             if (tmp_v < 75)
0326                 mask_amt = 1;
0327             else
0328                 mask_amt = 0;
0329             if (cur_vit_mask < 0)
0330                 mask_m[abs(cur_vit_mask / 100)] = mask_amt;
0331             else
0332                 mask_p[cur_vit_mask / 100] = mask_amt;
0333         }
0334         cur_vit_mask -= 100;
0335     }
0336 
0337     tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
0338         | (mask_m[48] << 26) | (mask_m[49] << 24)
0339         | (mask_m[50] << 22) | (mask_m[51] << 20)
0340         | (mask_m[52] << 18) | (mask_m[53] << 16)
0341         | (mask_m[54] << 14) | (mask_m[55] << 12)
0342         | (mask_m[56] << 10) | (mask_m[57] << 8)
0343         | (mask_m[58] << 6) | (mask_m[59] << 4)
0344         | (mask_m[60] << 2) | (mask_m[61] << 0);
0345     REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
0346     REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
0347 
0348     tmp_mask = (mask_m[31] << 28)
0349         | (mask_m[32] << 26) | (mask_m[33] << 24)
0350         | (mask_m[34] << 22) | (mask_m[35] << 20)
0351         | (mask_m[36] << 18) | (mask_m[37] << 16)
0352         | (mask_m[48] << 14) | (mask_m[39] << 12)
0353         | (mask_m[40] << 10) | (mask_m[41] << 8)
0354         | (mask_m[42] << 6) | (mask_m[43] << 4)
0355         | (mask_m[44] << 2) | (mask_m[45] << 0);
0356     REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
0357     REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
0358 
0359     tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
0360         | (mask_m[18] << 26) | (mask_m[18] << 24)
0361         | (mask_m[20] << 22) | (mask_m[20] << 20)
0362         | (mask_m[22] << 18) | (mask_m[22] << 16)
0363         | (mask_m[24] << 14) | (mask_m[24] << 12)
0364         | (mask_m[25] << 10) | (mask_m[26] << 8)
0365         | (mask_m[27] << 6) | (mask_m[28] << 4)
0366         | (mask_m[29] << 2) | (mask_m[30] << 0);
0367     REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
0368     REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
0369 
0370     tmp_mask = (mask_m[0] << 30) | (mask_m[1] << 28)
0371         | (mask_m[2] << 26) | (mask_m[3] << 24)
0372         | (mask_m[4] << 22) | (mask_m[5] << 20)
0373         | (mask_m[6] << 18) | (mask_m[7] << 16)
0374         | (mask_m[8] << 14) | (mask_m[9] << 12)
0375         | (mask_m[10] << 10) | (mask_m[11] << 8)
0376         | (mask_m[12] << 6) | (mask_m[13] << 4)
0377         | (mask_m[14] << 2) | (mask_m[15] << 0);
0378     REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
0379     REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
0380 
0381     tmp_mask = (mask_p[15] << 28)
0382         | (mask_p[14] << 26) | (mask_p[13] << 24)
0383         | (mask_p[12] << 22) | (mask_p[11] << 20)
0384         | (mask_p[10] << 18) | (mask_p[9] << 16)
0385         | (mask_p[8] << 14) | (mask_p[7] << 12)
0386         | (mask_p[6] << 10) | (mask_p[5] << 8)
0387         | (mask_p[4] << 6) | (mask_p[3] << 4)
0388         | (mask_p[2] << 2) | (mask_p[1] << 0);
0389     REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
0390     REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
0391 
0392     tmp_mask = (mask_p[30] << 28)
0393         | (mask_p[29] << 26) | (mask_p[28] << 24)
0394         | (mask_p[27] << 22) | (mask_p[26] << 20)
0395         | (mask_p[25] << 18) | (mask_p[24] << 16)
0396         | (mask_p[23] << 14) | (mask_p[22] << 12)
0397         | (mask_p[21] << 10) | (mask_p[20] << 8)
0398         | (mask_p[19] << 6) | (mask_p[18] << 4)
0399         | (mask_p[17] << 2) | (mask_p[16] << 0);
0400     REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
0401     REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
0402 
0403     tmp_mask = (mask_p[45] << 28)
0404         | (mask_p[44] << 26) | (mask_p[43] << 24)
0405         | (mask_p[42] << 22) | (mask_p[41] << 20)
0406         | (mask_p[40] << 18) | (mask_p[39] << 16)
0407         | (mask_p[38] << 14) | (mask_p[37] << 12)
0408         | (mask_p[36] << 10) | (mask_p[35] << 8)
0409         | (mask_p[34] << 6) | (mask_p[33] << 4)
0410         | (mask_p[32] << 2) | (mask_p[31] << 0);
0411     REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
0412     REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
0413 
0414     tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
0415         | (mask_p[59] << 26) | (mask_p[58] << 24)
0416         | (mask_p[57] << 22) | (mask_p[56] << 20)
0417         | (mask_p[55] << 18) | (mask_p[54] << 16)
0418         | (mask_p[53] << 14) | (mask_p[52] << 12)
0419         | (mask_p[51] << 10) | (mask_p[50] << 8)
0420         | (mask_p[49] << 6) | (mask_p[48] << 4)
0421         | (mask_p[47] << 2) | (mask_p[46] << 0);
0422     REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
0423     REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
0424 }
0425 
0426 /*
0427  * ar5008_hw_spur_mitigate - convert baseband spur frequency for external radios
0428  *
0429  * For non single-chip solutions. Converts to baseband spur frequency given the
0430  * input channel frequency and compute register settings below.
0431  */
0432 static void ar5008_hw_spur_mitigate(struct ath_hw *ah,
0433                     struct ath9k_channel *chan)
0434 {
0435     int bb_spur = AR_NO_SPUR;
0436     int bin;
0437     int spur_freq_sd;
0438     int spur_delta_phase;
0439     int denominator;
0440     int tmp, new;
0441     int i;
0442 
0443     int cur_bb_spur;
0444     bool is2GHz = IS_CHAN_2GHZ(chan);
0445 
0446     for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
0447         cur_bb_spur = ah->eep_ops->get_spur_channel(ah, i, is2GHz);
0448         if (AR_NO_SPUR == cur_bb_spur)
0449             break;
0450         cur_bb_spur = cur_bb_spur - (chan->channel * 10);
0451         if ((cur_bb_spur > -95) && (cur_bb_spur < 95)) {
0452             bb_spur = cur_bb_spur;
0453             break;
0454         }
0455     }
0456 
0457     if (AR_NO_SPUR == bb_spur)
0458         return;
0459 
0460     bin = bb_spur * 32;
0461 
0462     tmp = REG_READ(ah, AR_PHY_TIMING_CTRL4(0));
0463     new = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
0464              AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
0465              AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
0466              AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
0467 
0468     REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0), new);
0469 
0470     new = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
0471            AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
0472            AR_PHY_SPUR_REG_MASK_RATE_SELECT |
0473            AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
0474            SM(SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
0475     REG_WRITE(ah, AR_PHY_SPUR_REG, new);
0476 
0477     spur_delta_phase = ((bb_spur * 524288) / 100) &
0478         AR_PHY_TIMING11_SPUR_DELTA_PHASE;
0479 
0480     denominator = IS_CHAN_2GHZ(chan) ? 440 : 400;
0481     spur_freq_sd = ((bb_spur * 2048) / denominator) & 0x3ff;
0482 
0483     new = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
0484            SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
0485            SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
0486     REG_WRITE(ah, AR_PHY_TIMING11, new);
0487 
0488     ar5008_hw_cmn_spur_mitigate(ah, chan, bin);
0489 }
0490 
0491 /**
0492  * ar5008_hw_rf_alloc_ext_banks - allocates banks for external radio programming
0493  * @ah: atheros hardware structure
0494  *
0495  * Only required for older devices with external AR2133/AR5133 radios.
0496  */
0497 static int ar5008_hw_rf_alloc_ext_banks(struct ath_hw *ah)
0498 {
0499     int size = ah->iniBank6.ia_rows * sizeof(u32);
0500 
0501     if (AR_SREV_9280_20_OR_LATER(ah))
0502         return 0;
0503 
0504     ah->analogBank6Data = devm_kzalloc(ah->dev, size, GFP_KERNEL);
0505     if (!ah->analogBank6Data)
0506         return -ENOMEM;
0507 
0508     return 0;
0509 }
0510 
0511 
0512 /* *
0513  * ar5008_hw_set_rf_regs - programs rf registers based on EEPROM
0514  * @ah: atheros hardware structure
0515  * @chan:
0516  * @modesIndex:
0517  *
0518  * Used for the external AR2133/AR5133 radios.
0519  *
0520  * Reads the EEPROM header info from the device structure and programs
0521  * all rf registers. This routine requires access to the analog
0522  * rf device. This is not required for single-chip devices.
0523  */
0524 static bool ar5008_hw_set_rf_regs(struct ath_hw *ah,
0525                   struct ath9k_channel *chan,
0526                   u16 modesIndex)
0527 {
0528     u32 eepMinorRev;
0529     u32 ob5GHz = 0, db5GHz = 0;
0530     u32 ob2GHz = 0, db2GHz = 0;
0531     int regWrites = 0;
0532     int i;
0533 
0534     /*
0535      * Software does not need to program bank data
0536      * for single chip devices, that is AR9280 or anything
0537      * after that.
0538      */
0539     if (AR_SREV_9280_20_OR_LATER(ah))
0540         return true;
0541 
0542     /* Setup rf parameters */
0543     eepMinorRev = ah->eep_ops->get_eeprom_rev(ah);
0544 
0545     for (i = 0; i < ah->iniBank6.ia_rows; i++)
0546         ah->analogBank6Data[i] = INI_RA(&ah->iniBank6, i, modesIndex);
0547 
0548     /* Only the 5 or 2 GHz OB/DB need to be set for a mode */
0549     if (eepMinorRev >= 2) {
0550         if (IS_CHAN_2GHZ(chan)) {
0551             ob2GHz = ah->eep_ops->get_eeprom(ah, EEP_OB_2);
0552             db2GHz = ah->eep_ops->get_eeprom(ah, EEP_DB_2);
0553             ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data,
0554                                ob2GHz, 3, 197, 0);
0555             ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data,
0556                                db2GHz, 3, 194, 0);
0557         } else {
0558             ob5GHz = ah->eep_ops->get_eeprom(ah, EEP_OB_5);
0559             db5GHz = ah->eep_ops->get_eeprom(ah, EEP_DB_5);
0560             ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data,
0561                                ob5GHz, 3, 203, 0);
0562             ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data,
0563                                db5GHz, 3, 200, 0);
0564         }
0565     }
0566 
0567     /* Write Analog registers */
0568     REG_WRITE_ARRAY(&bank0, 1, regWrites);
0569     REG_WRITE_ARRAY(&bank1, 1, regWrites);
0570     REG_WRITE_ARRAY(&bank2, 1, regWrites);
0571     REG_WRITE_ARRAY(&bank3, modesIndex, regWrites);
0572     ar5008_write_bank6(ah, &regWrites);
0573     REG_WRITE_ARRAY(&bank7, 1, regWrites);
0574 
0575     return true;
0576 }
0577 
0578 static void ar5008_hw_init_bb(struct ath_hw *ah,
0579                   struct ath9k_channel *chan)
0580 {
0581     u32 synthDelay;
0582 
0583     synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
0584 
0585     REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
0586 
0587     ath9k_hw_synth_delay(ah, chan, synthDelay);
0588 }
0589 
0590 static void ar5008_hw_init_chain_masks(struct ath_hw *ah)
0591 {
0592     int rx_chainmask, tx_chainmask;
0593 
0594     rx_chainmask = ah->rxchainmask;
0595     tx_chainmask = ah->txchainmask;
0596 
0597 
0598     switch (rx_chainmask) {
0599     case 0x5:
0600         REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
0601                 AR_PHY_SWAP_ALT_CHAIN);
0602         fallthrough;
0603     case 0x3:
0604         if (ah->hw_version.macVersion == AR_SREV_REVISION_5416_10) {
0605             REG_WRITE(ah, AR_PHY_RX_CHAINMASK, 0x7);
0606             REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, 0x7);
0607             break;
0608         }
0609         fallthrough;
0610     case 0x1:
0611     case 0x2:
0612     case 0x7:
0613         ENABLE_REGWRITE_BUFFER(ah);
0614         REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx_chainmask);
0615         REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx_chainmask);
0616         break;
0617     default:
0618         ENABLE_REGWRITE_BUFFER(ah);
0619         break;
0620     }
0621 
0622     REG_WRITE(ah, AR_SELFGEN_MASK, tx_chainmask);
0623 
0624     REGWRITE_BUFFER_FLUSH(ah);
0625 
0626     if (tx_chainmask == 0x5) {
0627         REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
0628                 AR_PHY_SWAP_ALT_CHAIN);
0629     }
0630     if (AR_SREV_9100(ah))
0631         REG_WRITE(ah, AR_PHY_ANALOG_SWAP,
0632               REG_READ(ah, AR_PHY_ANALOG_SWAP) | 0x00000001);
0633 }
0634 
0635 static void ar5008_hw_override_ini(struct ath_hw *ah,
0636                    struct ath9k_channel *chan)
0637 {
0638     u32 val;
0639 
0640     /*
0641      * Set the RX_ABORT and RX_DIS and clear if off only after
0642      * RXE is set for MAC. This prevents frames with corrupted
0643      * descriptor status.
0644      */
0645     REG_SET_BIT(ah, AR_DIAG_SW, (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
0646 
0647     if (AR_SREV_9280_20_OR_LATER(ah)) {
0648         /*
0649          * For AR9280 and above, there is a new feature that allows
0650          * Multicast search based on both MAC Address and Key ID.
0651          * By default, this feature is enabled. But since the driver
0652          * is not using this feature, we switch it off; otherwise
0653          * multicast search based on MAC addr only will fail.
0654          */
0655         val = REG_READ(ah, AR_PCU_MISC_MODE2) &
0656             (~AR_ADHOC_MCAST_KEYID_ENABLE);
0657 
0658         if (!AR_SREV_9271(ah))
0659             val &= ~AR_PCU_MISC_MODE2_HWWAR1;
0660 
0661         if (AR_SREV_9287_11_OR_LATER(ah))
0662             val = val & (~AR_PCU_MISC_MODE2_HWWAR2);
0663 
0664         val |= AR_PCU_MISC_MODE2_CFP_IGNORE;
0665 
0666         REG_WRITE(ah, AR_PCU_MISC_MODE2, val);
0667     }
0668 
0669     if (AR_SREV_9280_20_OR_LATER(ah))
0670         return;
0671     /*
0672      * Disable BB clock gating
0673      * Necessary to avoid issues on AR5416 2.0
0674      */
0675     REG_WRITE(ah, 0x9800 + (651 << 2), 0x11);
0676 
0677     /*
0678      * Disable RIFS search on some chips to avoid baseband
0679      * hang issues.
0680      */
0681     if (AR_SREV_9100(ah) || AR_SREV_9160(ah)) {
0682         val = REG_READ(ah, AR_PHY_HEAVY_CLIP_FACTOR_RIFS);
0683         val &= ~AR_PHY_RIFS_INIT_DELAY;
0684         REG_WRITE(ah, AR_PHY_HEAVY_CLIP_FACTOR_RIFS, val);
0685     }
0686 }
0687 
0688 static void ar5008_hw_set_channel_regs(struct ath_hw *ah,
0689                        struct ath9k_channel *chan)
0690 {
0691     u32 phymode;
0692     u32 enableDacFifo = 0;
0693 
0694     if (AR_SREV_9285_12_OR_LATER(ah))
0695         enableDacFifo = (REG_READ(ah, AR_PHY_TURBO) &
0696                      AR_PHY_FC_ENABLE_DAC_FIFO);
0697 
0698     phymode = AR_PHY_FC_HT_EN | AR_PHY_FC_SHORT_GI_40
0699         | AR_PHY_FC_SINGLE_HT_LTF1 | AR_PHY_FC_WALSH | enableDacFifo;
0700 
0701     if (IS_CHAN_HT40(chan)) {
0702         phymode |= AR_PHY_FC_DYN2040_EN;
0703 
0704         if (IS_CHAN_HT40PLUS(chan))
0705             phymode |= AR_PHY_FC_DYN2040_PRI_CH;
0706 
0707     }
0708     ENABLE_REGWRITE_BUFFER(ah);
0709     REG_WRITE(ah, AR_PHY_TURBO, phymode);
0710 
0711     /* This function do only REG_WRITE, so
0712      * we can include it to REGWRITE_BUFFER. */
0713     ath9k_hw_set11nmac2040(ah, chan);
0714 
0715     REG_WRITE(ah, AR_GTXTO, 25 << AR_GTXTO_TIMEOUT_LIMIT_S);
0716     REG_WRITE(ah, AR_CST, 0xF << AR_CST_TIMEOUT_LIMIT_S);
0717 
0718     REGWRITE_BUFFER_FLUSH(ah);
0719 }
0720 
0721 
0722 static int ar5008_hw_process_ini(struct ath_hw *ah,
0723                  struct ath9k_channel *chan)
0724 {
0725     struct ath_common *common = ath9k_hw_common(ah);
0726     int i, regWrites = 0;
0727     u32 modesIndex, freqIndex;
0728 
0729     if (IS_CHAN_5GHZ(chan)) {
0730         freqIndex = 1;
0731         modesIndex = IS_CHAN_HT40(chan) ? 2 : 1;
0732     } else {
0733         freqIndex = 2;
0734         modesIndex = IS_CHAN_HT40(chan) ? 3 : 4;
0735     }
0736 
0737     /*
0738      * Set correct baseband to analog shift setting to
0739      * access analog chips.
0740      */
0741     REG_WRITE(ah, AR_PHY(0), 0x00000007);
0742 
0743     /* Write ADDAC shifts */
0744     REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_EXTERNAL_RADIO);
0745     if (ah->eep_ops->set_addac)
0746         ah->eep_ops->set_addac(ah, chan);
0747 
0748     REG_WRITE_ARRAY(&ah->iniAddac, 1, regWrites);
0749     REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
0750 
0751     ENABLE_REGWRITE_BUFFER(ah);
0752 
0753     for (i = 0; i < ah->iniModes.ia_rows; i++) {
0754         u32 reg = INI_RA(&ah->iniModes, i, 0);
0755         u32 val = INI_RA(&ah->iniModes, i, modesIndex);
0756 
0757         if (reg == AR_AN_TOP2 && ah->need_an_top2_fixup)
0758             val &= ~AR_AN_TOP2_PWDCLKIND;
0759 
0760         REG_WRITE(ah, reg, val);
0761 
0762         if (reg >= 0x7800 && reg < 0x78a0
0763             && ah->config.analog_shiftreg
0764             && (common->bus_ops->ath_bus_type != ATH_USB)) {
0765             udelay(100);
0766         }
0767 
0768         DO_DELAY(regWrites);
0769     }
0770 
0771     REGWRITE_BUFFER_FLUSH(ah);
0772 
0773     if (AR_SREV_9280(ah) || AR_SREV_9287_11_OR_LATER(ah))
0774         REG_WRITE_ARRAY(&ah->iniModesRxGain, modesIndex, regWrites);
0775 
0776     if (AR_SREV_9280(ah) || AR_SREV_9285_12_OR_LATER(ah) ||
0777         AR_SREV_9287_11_OR_LATER(ah))
0778         REG_WRITE_ARRAY(&ah->iniModesTxGain, modesIndex, regWrites);
0779 
0780     if (AR_SREV_9271_10(ah)) {
0781         REG_SET_BIT(ah, AR_PHY_SPECTRAL_SCAN, AR_PHY_SPECTRAL_SCAN_ENA);
0782         REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_ADC_ON, 0xa);
0783     }
0784 
0785     ENABLE_REGWRITE_BUFFER(ah);
0786 
0787     /* Write common array parameters */
0788     for (i = 0; i < ah->iniCommon.ia_rows; i++) {
0789         u32 reg = INI_RA(&ah->iniCommon, i, 0);
0790         u32 val = INI_RA(&ah->iniCommon, i, 1);
0791 
0792         REG_WRITE(ah, reg, val);
0793 
0794         if (reg >= 0x7800 && reg < 0x78a0
0795             && ah->config.analog_shiftreg
0796             && (common->bus_ops->ath_bus_type != ATH_USB)) {
0797             udelay(100);
0798         }
0799 
0800         DO_DELAY(regWrites);
0801     }
0802 
0803     REGWRITE_BUFFER_FLUSH(ah);
0804 
0805     REG_WRITE_ARRAY(&ah->iniBB_RfGain, freqIndex, regWrites);
0806 
0807     if (IS_CHAN_A_FAST_CLOCK(ah, chan))
0808         REG_WRITE_ARRAY(&ah->iniModesFastClock, modesIndex,
0809                 regWrites);
0810 
0811     ar5008_hw_override_ini(ah, chan);
0812     ar5008_hw_set_channel_regs(ah, chan);
0813     ar5008_hw_init_chain_masks(ah);
0814     ath9k_olc_init(ah);
0815     ath9k_hw_apply_txpower(ah, chan, false);
0816 
0817     /* Write analog registers */
0818     if (!ath9k_hw_set_rf_regs(ah, chan, freqIndex)) {
0819         ath_err(ath9k_hw_common(ah), "ar5416SetRfRegs failed\n");
0820         return -EIO;
0821     }
0822 
0823     return 0;
0824 }
0825 
0826 static void ar5008_hw_set_rfmode(struct ath_hw *ah, struct ath9k_channel *chan)
0827 {
0828     u32 rfMode = 0;
0829 
0830     if (chan == NULL)
0831         return;
0832 
0833     if (IS_CHAN_2GHZ(chan))
0834         rfMode |= AR_PHY_MODE_DYNAMIC;
0835     else
0836         rfMode |= AR_PHY_MODE_OFDM;
0837 
0838     if (!AR_SREV_9280_20_OR_LATER(ah))
0839         rfMode |= (IS_CHAN_5GHZ(chan)) ?
0840             AR_PHY_MODE_RF5GHZ : AR_PHY_MODE_RF2GHZ;
0841 
0842     if (IS_CHAN_A_FAST_CLOCK(ah, chan))
0843         rfMode |= (AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE);
0844 
0845     REG_WRITE(ah, AR_PHY_MODE, rfMode);
0846 }
0847 
0848 static void ar5008_hw_mark_phy_inactive(struct ath_hw *ah)
0849 {
0850     REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
0851 }
0852 
0853 static void ar5008_hw_set_delta_slope(struct ath_hw *ah,
0854                       struct ath9k_channel *chan)
0855 {
0856     u32 coef_scaled, ds_coef_exp, ds_coef_man;
0857     u32 clockMhzScaled = 0x64000000;
0858     struct chan_centers centers;
0859 
0860     if (IS_CHAN_HALF_RATE(chan))
0861         clockMhzScaled = clockMhzScaled >> 1;
0862     else if (IS_CHAN_QUARTER_RATE(chan))
0863         clockMhzScaled = clockMhzScaled >> 2;
0864 
0865     ath9k_hw_get_channel_centers(ah, chan, &centers);
0866     coef_scaled = clockMhzScaled / centers.synth_center;
0867 
0868     ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
0869                       &ds_coef_exp);
0870 
0871     REG_RMW_FIELD(ah, AR_PHY_TIMING3,
0872               AR_PHY_TIMING3_DSC_MAN, ds_coef_man);
0873     REG_RMW_FIELD(ah, AR_PHY_TIMING3,
0874               AR_PHY_TIMING3_DSC_EXP, ds_coef_exp);
0875 
0876     coef_scaled = (9 * coef_scaled) / 10;
0877 
0878     ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
0879                       &ds_coef_exp);
0880 
0881     REG_RMW_FIELD(ah, AR_PHY_HALFGI,
0882               AR_PHY_HALFGI_DSC_MAN, ds_coef_man);
0883     REG_RMW_FIELD(ah, AR_PHY_HALFGI,
0884               AR_PHY_HALFGI_DSC_EXP, ds_coef_exp);
0885 }
0886 
0887 static bool ar5008_hw_rfbus_req(struct ath_hw *ah)
0888 {
0889     REG_WRITE(ah, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
0890     return ath9k_hw_wait(ah, AR_PHY_RFBUS_GRANT, AR_PHY_RFBUS_GRANT_EN,
0891                AR_PHY_RFBUS_GRANT_EN, AH_WAIT_TIMEOUT);
0892 }
0893 
0894 static void ar5008_hw_rfbus_done(struct ath_hw *ah)
0895 {
0896     u32 synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
0897 
0898     ath9k_hw_synth_delay(ah, ah->curchan, synthDelay);
0899 
0900     REG_WRITE(ah, AR_PHY_RFBUS_REQ, 0);
0901 }
0902 
0903 static void ar5008_restore_chainmask(struct ath_hw *ah)
0904 {
0905     int rx_chainmask = ah->rxchainmask;
0906 
0907     if ((rx_chainmask == 0x5) || (rx_chainmask == 0x3)) {
0908         REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx_chainmask);
0909         REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx_chainmask);
0910     }
0911 }
0912 
0913 static u32 ar9160_hw_compute_pll_control(struct ath_hw *ah,
0914                      struct ath9k_channel *chan)
0915 {
0916     u32 pll;
0917 
0918     pll = SM(0x5, AR_RTC_9160_PLL_REFDIV);
0919 
0920     if (chan && IS_CHAN_HALF_RATE(chan))
0921         pll |= SM(0x1, AR_RTC_9160_PLL_CLKSEL);
0922     else if (chan && IS_CHAN_QUARTER_RATE(chan))
0923         pll |= SM(0x2, AR_RTC_9160_PLL_CLKSEL);
0924 
0925     if (chan && IS_CHAN_5GHZ(chan))
0926         pll |= SM(0x50, AR_RTC_9160_PLL_DIV);
0927     else
0928         pll |= SM(0x58, AR_RTC_9160_PLL_DIV);
0929 
0930     return pll;
0931 }
0932 
0933 static u32 ar5008_hw_compute_pll_control(struct ath_hw *ah,
0934                      struct ath9k_channel *chan)
0935 {
0936     u32 pll;
0937 
0938     pll = AR_RTC_PLL_REFDIV_5 | AR_RTC_PLL_DIV2;
0939 
0940     if (chan && IS_CHAN_HALF_RATE(chan))
0941         pll |= SM(0x1, AR_RTC_PLL_CLKSEL);
0942     else if (chan && IS_CHAN_QUARTER_RATE(chan))
0943         pll |= SM(0x2, AR_RTC_PLL_CLKSEL);
0944 
0945     if (chan && IS_CHAN_5GHZ(chan))
0946         pll |= SM(0xa, AR_RTC_PLL_DIV);
0947     else
0948         pll |= SM(0xb, AR_RTC_PLL_DIV);
0949 
0950     return pll;
0951 }
0952 
0953 static bool ar5008_hw_ani_control_new(struct ath_hw *ah,
0954                       enum ath9k_ani_cmd cmd,
0955                       int param)
0956 {
0957     struct ath_common *common = ath9k_hw_common(ah);
0958     struct ath9k_channel *chan = ah->curchan;
0959     struct ar5416AniState *aniState = &ah->ani;
0960     s32 value;
0961 
0962     switch (cmd & ah->ani_function) {
0963     case ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION:{
0964         /*
0965          * on == 1 means ofdm weak signal detection is ON
0966          * on == 1 is the default, for less noise immunity
0967          *
0968          * on == 0 means ofdm weak signal detection is OFF
0969          * on == 0 means more noise imm
0970          */
0971         u32 on = param ? 1 : 0;
0972         /*
0973          * make register setting for default
0974          * (weak sig detect ON) come from INI file
0975          */
0976         int m1ThreshLow = on ?
0977             aniState->iniDef.m1ThreshLow : m1ThreshLow_off;
0978         int m2ThreshLow = on ?
0979             aniState->iniDef.m2ThreshLow : m2ThreshLow_off;
0980         int m1Thresh = on ?
0981             aniState->iniDef.m1Thresh : m1Thresh_off;
0982         int m2Thresh = on ?
0983             aniState->iniDef.m2Thresh : m2Thresh_off;
0984         int m2CountThr = on ?
0985             aniState->iniDef.m2CountThr : m2CountThr_off;
0986         int m2CountThrLow = on ?
0987             aniState->iniDef.m2CountThrLow : m2CountThrLow_off;
0988         int m1ThreshLowExt = on ?
0989             aniState->iniDef.m1ThreshLowExt : m1ThreshLowExt_off;
0990         int m2ThreshLowExt = on ?
0991             aniState->iniDef.m2ThreshLowExt : m2ThreshLowExt_off;
0992         int m1ThreshExt = on ?
0993             aniState->iniDef.m1ThreshExt : m1ThreshExt_off;
0994         int m2ThreshExt = on ?
0995             aniState->iniDef.m2ThreshExt : m2ThreshExt_off;
0996 
0997         REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
0998                   AR_PHY_SFCORR_LOW_M1_THRESH_LOW,
0999                   m1ThreshLow);
1000         REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
1001                   AR_PHY_SFCORR_LOW_M2_THRESH_LOW,
1002                   m2ThreshLow);
1003         REG_RMW_FIELD(ah, AR_PHY_SFCORR,
1004                   AR_PHY_SFCORR_M1_THRESH, m1Thresh);
1005         REG_RMW_FIELD(ah, AR_PHY_SFCORR,
1006                   AR_PHY_SFCORR_M2_THRESH, m2Thresh);
1007         REG_RMW_FIELD(ah, AR_PHY_SFCORR,
1008                   AR_PHY_SFCORR_M2COUNT_THR, m2CountThr);
1009         REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
1010                   AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW,
1011                   m2CountThrLow);
1012 
1013         REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
1014                   AR_PHY_SFCORR_EXT_M1_THRESH_LOW, m1ThreshLowExt);
1015         REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
1016                   AR_PHY_SFCORR_EXT_M2_THRESH_LOW, m2ThreshLowExt);
1017         REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
1018                   AR_PHY_SFCORR_EXT_M1_THRESH, m1ThreshExt);
1019         REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
1020                   AR_PHY_SFCORR_EXT_M2_THRESH, m2ThreshExt);
1021 
1022         if (on)
1023             REG_SET_BIT(ah, AR_PHY_SFCORR_LOW,
1024                     AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
1025         else
1026             REG_CLR_BIT(ah, AR_PHY_SFCORR_LOW,
1027                     AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
1028 
1029         if (on != aniState->ofdmWeakSigDetect) {
1030             ath_dbg(common, ANI,
1031                 "** ch %d: ofdm weak signal: %s=>%s\n",
1032                 chan->channel,
1033                 aniState->ofdmWeakSigDetect ?
1034                 "on" : "off",
1035                 on ? "on" : "off");
1036             if (on)
1037                 ah->stats.ast_ani_ofdmon++;
1038             else
1039                 ah->stats.ast_ani_ofdmoff++;
1040             aniState->ofdmWeakSigDetect = on;
1041         }
1042         break;
1043     }
1044     case ATH9K_ANI_FIRSTEP_LEVEL:{
1045         u32 level = param;
1046 
1047         value = level * 2;
1048         REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,
1049                   AR_PHY_FIND_SIG_FIRSTEP, value);
1050         REG_RMW_FIELD(ah, AR_PHY_FIND_SIG_LOW,
1051                   AR_PHY_FIND_SIG_FIRSTEP_LOW, value);
1052 
1053         if (level != aniState->firstepLevel) {
1054             ath_dbg(common, ANI,
1055                 "** ch %d: level %d=>%d[def:%d] firstep[level]=%d ini=%d\n",
1056                 chan->channel,
1057                 aniState->firstepLevel,
1058                 level,
1059                 ATH9K_ANI_FIRSTEP_LVL,
1060                 value,
1061                 aniState->iniDef.firstep);
1062             ath_dbg(common, ANI,
1063                 "** ch %d: level %d=>%d[def:%d] firstep_low[level]=%d ini=%d\n",
1064                 chan->channel,
1065                 aniState->firstepLevel,
1066                 level,
1067                 ATH9K_ANI_FIRSTEP_LVL,
1068                 value,
1069                 aniState->iniDef.firstepLow);
1070             if (level > aniState->firstepLevel)
1071                 ah->stats.ast_ani_stepup++;
1072             else if (level < aniState->firstepLevel)
1073                 ah->stats.ast_ani_stepdown++;
1074             aniState->firstepLevel = level;
1075         }
1076         break;
1077     }
1078     case ATH9K_ANI_SPUR_IMMUNITY_LEVEL:{
1079         u32 level = param;
1080 
1081         value = (level + 1) * 2;
1082         REG_RMW_FIELD(ah, AR_PHY_TIMING5,
1083                   AR_PHY_TIMING5_CYCPWR_THR1, value);
1084 
1085         REG_RMW_FIELD(ah, AR_PHY_EXT_CCA,
1086                   AR_PHY_EXT_TIMING5_CYCPWR_THR1, value - 1);
1087 
1088         if (level != aniState->spurImmunityLevel) {
1089             ath_dbg(common, ANI,
1090                 "** ch %d: level %d=>%d[def:%d] cycpwrThr1[level]=%d ini=%d\n",
1091                 chan->channel,
1092                 aniState->spurImmunityLevel,
1093                 level,
1094                 ATH9K_ANI_SPUR_IMMUNE_LVL,
1095                 value,
1096                 aniState->iniDef.cycpwrThr1);
1097             ath_dbg(common, ANI,
1098                 "** ch %d: level %d=>%d[def:%d] cycpwrThr1Ext[level]=%d ini=%d\n",
1099                 chan->channel,
1100                 aniState->spurImmunityLevel,
1101                 level,
1102                 ATH9K_ANI_SPUR_IMMUNE_LVL,
1103                 value,
1104                 aniState->iniDef.cycpwrThr1Ext);
1105             if (level > aniState->spurImmunityLevel)
1106                 ah->stats.ast_ani_spurup++;
1107             else if (level < aniState->spurImmunityLevel)
1108                 ah->stats.ast_ani_spurdown++;
1109             aniState->spurImmunityLevel = level;
1110         }
1111         break;
1112     }
1113     case ATH9K_ANI_MRC_CCK:
1114         /*
1115          * You should not see this as AR5008, AR9001, AR9002
1116          * does not have hardware support for MRC CCK.
1117          */
1118         WARN_ON(1);
1119         break;
1120     default:
1121         ath_dbg(common, ANI, "invalid cmd %u\n", cmd);
1122         return false;
1123     }
1124 
1125     ath_dbg(common, ANI,
1126         "ANI parameters: SI=%d, ofdmWS=%s FS=%d MRCcck=%s listenTime=%d ofdmErrs=%d cckErrs=%d\n",
1127         aniState->spurImmunityLevel,
1128         aniState->ofdmWeakSigDetect ? "on" : "off",
1129         aniState->firstepLevel,
1130         aniState->mrcCCK ? "on" : "off",
1131         aniState->listenTime,
1132         aniState->ofdmPhyErrCount,
1133         aniState->cckPhyErrCount);
1134     return true;
1135 }
1136 
1137 static void ar5008_hw_do_getnf(struct ath_hw *ah,
1138                   int16_t nfarray[NUM_NF_READINGS])
1139 {
1140     int16_t nf;
1141 
1142     nf = MS(REG_READ(ah, AR_PHY_CCA), AR_PHY_MINCCA_PWR);
1143     nfarray[0] = sign_extend32(nf, 8);
1144 
1145     nf = MS(REG_READ(ah, AR_PHY_CH1_CCA), AR_PHY_CH1_MINCCA_PWR);
1146     nfarray[1] = sign_extend32(nf, 8);
1147 
1148     nf = MS(REG_READ(ah, AR_PHY_CH2_CCA), AR_PHY_CH2_MINCCA_PWR);
1149     nfarray[2] = sign_extend32(nf, 8);
1150 
1151     if (!IS_CHAN_HT40(ah->curchan))
1152         return;
1153 
1154     nf = MS(REG_READ(ah, AR_PHY_EXT_CCA), AR_PHY_EXT_MINCCA_PWR);
1155     nfarray[3] = sign_extend32(nf, 8);
1156 
1157     nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR_PHY_CH1_EXT_MINCCA_PWR);
1158     nfarray[4] = sign_extend32(nf, 8);
1159 
1160     nf = MS(REG_READ(ah, AR_PHY_CH2_EXT_CCA), AR_PHY_CH2_EXT_MINCCA_PWR);
1161     nfarray[5] = sign_extend32(nf, 8);
1162 }
1163 
1164 /*
1165  * Initialize the ANI register values with default (ini) values.
1166  * This routine is called during a (full) hardware reset after
1167  * all the registers are initialised from the INI.
1168  */
1169 static void ar5008_hw_ani_cache_ini_regs(struct ath_hw *ah)
1170 {
1171     struct ath_common *common = ath9k_hw_common(ah);
1172     struct ath9k_channel *chan = ah->curchan;
1173     struct ar5416AniState *aniState = &ah->ani;
1174     struct ath9k_ani_default *iniDef;
1175     u32 val;
1176 
1177     iniDef = &aniState->iniDef;
1178 
1179     ath_dbg(common, ANI, "ver %d.%d opmode %u chan %d Mhz\n",
1180         ah->hw_version.macVersion,
1181         ah->hw_version.macRev,
1182         ah->opmode,
1183         chan->channel);
1184 
1185     val = REG_READ(ah, AR_PHY_SFCORR);
1186     iniDef->m1Thresh = MS(val, AR_PHY_SFCORR_M1_THRESH);
1187     iniDef->m2Thresh = MS(val, AR_PHY_SFCORR_M2_THRESH);
1188     iniDef->m2CountThr = MS(val, AR_PHY_SFCORR_M2COUNT_THR);
1189 
1190     val = REG_READ(ah, AR_PHY_SFCORR_LOW);
1191     iniDef->m1ThreshLow = MS(val, AR_PHY_SFCORR_LOW_M1_THRESH_LOW);
1192     iniDef->m2ThreshLow = MS(val, AR_PHY_SFCORR_LOW_M2_THRESH_LOW);
1193     iniDef->m2CountThrLow = MS(val, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW);
1194 
1195     val = REG_READ(ah, AR_PHY_SFCORR_EXT);
1196     iniDef->m1ThreshExt = MS(val, AR_PHY_SFCORR_EXT_M1_THRESH);
1197     iniDef->m2ThreshExt = MS(val, AR_PHY_SFCORR_EXT_M2_THRESH);
1198     iniDef->m1ThreshLowExt = MS(val, AR_PHY_SFCORR_EXT_M1_THRESH_LOW);
1199     iniDef->m2ThreshLowExt = MS(val, AR_PHY_SFCORR_EXT_M2_THRESH_LOW);
1200     iniDef->firstep = REG_READ_FIELD(ah,
1201                      AR_PHY_FIND_SIG,
1202                      AR_PHY_FIND_SIG_FIRSTEP);
1203     iniDef->firstepLow = REG_READ_FIELD(ah,
1204                         AR_PHY_FIND_SIG_LOW,
1205                         AR_PHY_FIND_SIG_FIRSTEP_LOW);
1206     iniDef->cycpwrThr1 = REG_READ_FIELD(ah,
1207                         AR_PHY_TIMING5,
1208                         AR_PHY_TIMING5_CYCPWR_THR1);
1209     iniDef->cycpwrThr1Ext = REG_READ_FIELD(ah,
1210                            AR_PHY_EXT_CCA,
1211                            AR_PHY_EXT_TIMING5_CYCPWR_THR1);
1212 
1213     /* these levels just got reset to defaults by the INI */
1214     aniState->spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
1215     aniState->firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
1216     aniState->ofdmWeakSigDetect = true;
1217     aniState->mrcCCK = false; /* not available on pre AR9003 */
1218 }
1219 
1220 static void ar5008_hw_set_nf_limits(struct ath_hw *ah)
1221 {
1222     ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_5416_2GHZ;
1223     ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_5416_2GHZ;
1224     ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_5416_2GHZ;
1225     ah->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_5416_5GHZ;
1226     ah->nf_5g.min = AR_PHY_CCA_MIN_GOOD_VAL_5416_5GHZ;
1227     ah->nf_5g.nominal = AR_PHY_CCA_NOM_VAL_5416_5GHZ;
1228 }
1229 
1230 static void ar5008_hw_set_radar_params(struct ath_hw *ah,
1231                        struct ath_hw_radar_conf *conf)
1232 {
1233     u32 radar_0 = 0, radar_1;
1234 
1235     if (!conf) {
1236         REG_CLR_BIT(ah, AR_PHY_RADAR_0, AR_PHY_RADAR_0_ENA);
1237         return;
1238     }
1239 
1240     radar_0 |= AR_PHY_RADAR_0_ENA | AR_PHY_RADAR_0_FFT_ENA;
1241     radar_0 |= SM(conf->fir_power, AR_PHY_RADAR_0_FIRPWR);
1242     radar_0 |= SM(conf->radar_rssi, AR_PHY_RADAR_0_RRSSI);
1243     radar_0 |= SM(conf->pulse_height, AR_PHY_RADAR_0_HEIGHT);
1244     radar_0 |= SM(conf->pulse_rssi, AR_PHY_RADAR_0_PRSSI);
1245     radar_0 |= SM(conf->pulse_inband, AR_PHY_RADAR_0_INBAND);
1246 
1247     radar_1 = REG_READ(ah, AR_PHY_RADAR_1);
1248     radar_1 &= ~(AR_PHY_RADAR_1_MAXLEN | AR_PHY_RADAR_1_RELSTEP_THRESH |
1249              AR_PHY_RADAR_1_RELPWR_THRESH);
1250     radar_1 |= AR_PHY_RADAR_1_MAX_RRSSI;
1251     radar_1 |= AR_PHY_RADAR_1_BLOCK_CHECK;
1252     radar_1 |= SM(conf->pulse_maxlen, AR_PHY_RADAR_1_MAXLEN);
1253     radar_1 |= SM(conf->pulse_inband_step, AR_PHY_RADAR_1_RELSTEP_THRESH);
1254     radar_1 |= SM(conf->radar_inband, AR_PHY_RADAR_1_RELPWR_THRESH);
1255 
1256     REG_WRITE(ah, AR_PHY_RADAR_0, radar_0);
1257     REG_WRITE(ah, AR_PHY_RADAR_1, radar_1);
1258     if (conf->ext_channel)
1259         REG_SET_BIT(ah, AR_PHY_RADAR_EXT, AR_PHY_RADAR_EXT_ENA);
1260     else
1261         REG_CLR_BIT(ah, AR_PHY_RADAR_EXT, AR_PHY_RADAR_EXT_ENA);
1262 }
1263 
1264 static void ar5008_hw_set_radar_conf(struct ath_hw *ah)
1265 {
1266     struct ath_hw_radar_conf *conf = &ah->radar_conf;
1267 
1268     conf->fir_power = -33;
1269     conf->radar_rssi = 20;
1270     conf->pulse_height = 10;
1271     conf->pulse_rssi = 15;
1272     conf->pulse_inband = 15;
1273     conf->pulse_maxlen = 255;
1274     conf->pulse_inband_step = 12;
1275     conf->radar_inband = 8;
1276 }
1277 
1278 static void ar5008_hw_init_txpower_cck(struct ath_hw *ah, int16_t *rate_array)
1279 {
1280 #define CCK_DELTA(x) ((OLC_FOR_AR9280_20_LATER) ? max((x) - 2, 0) : (x))
1281     ah->tx_power[0] = CCK_DELTA(rate_array[rate1l]);
1282     ah->tx_power[1] = CCK_DELTA(min(rate_array[rate2l],
1283                     rate_array[rate2s]));
1284     ah->tx_power[2] = CCK_DELTA(min(rate_array[rate5_5l],
1285                     rate_array[rate5_5s]));
1286     ah->tx_power[3] = CCK_DELTA(min(rate_array[rate11l],
1287                     rate_array[rate11s]));
1288 #undef CCK_DELTA
1289 }
1290 
1291 static void ar5008_hw_init_txpower_ofdm(struct ath_hw *ah, int16_t *rate_array,
1292                     int offset)
1293 {
1294     int i, idx = 0;
1295 
1296     for (i = offset; i < offset + AR5008_OFDM_RATES; i++) {
1297         ah->tx_power[i] = rate_array[idx];
1298         idx++;
1299     }
1300 }
1301 
1302 static void ar5008_hw_init_txpower_ht(struct ath_hw *ah, int16_t *rate_array,
1303                       int ss_offset, int ds_offset,
1304                       bool is_40, int ht40_delta)
1305 {
1306     int i, mcs_idx = (is_40) ? AR5008_HT40_SHIFT : AR5008_HT20_SHIFT;
1307 
1308     for (i = ss_offset; i < ss_offset + AR5008_HT_SS_RATES; i++) {
1309         ah->tx_power[i] = rate_array[mcs_idx] + ht40_delta;
1310         mcs_idx++;
1311     }
1312     memcpy(&ah->tx_power[ds_offset], &ah->tx_power[ss_offset],
1313            AR5008_HT_SS_RATES);
1314 }
1315 
1316 void ar5008_hw_init_rate_txpower(struct ath_hw *ah, int16_t *rate_array,
1317                  struct ath9k_channel *chan, int ht40_delta)
1318 {
1319     if (IS_CHAN_5GHZ(chan)) {
1320         ar5008_hw_init_txpower_ofdm(ah, rate_array,
1321                         AR5008_11NA_OFDM_SHIFT);
1322         if (IS_CHAN_HT20(chan) || IS_CHAN_HT40(chan)) {
1323             ar5008_hw_init_txpower_ht(ah, rate_array,
1324                           AR5008_11NA_HT_SS_SHIFT,
1325                           AR5008_11NA_HT_DS_SHIFT,
1326                           IS_CHAN_HT40(chan),
1327                           ht40_delta);
1328         }
1329     } else {
1330         ar5008_hw_init_txpower_cck(ah, rate_array);
1331         ar5008_hw_init_txpower_ofdm(ah, rate_array,
1332                         AR5008_11NG_OFDM_SHIFT);
1333         if (IS_CHAN_HT20(chan) || IS_CHAN_HT40(chan)) {
1334             ar5008_hw_init_txpower_ht(ah, rate_array,
1335                           AR5008_11NG_HT_SS_SHIFT,
1336                           AR5008_11NG_HT_DS_SHIFT,
1337                           IS_CHAN_HT40(chan),
1338                           ht40_delta);
1339         }
1340     }
1341 }
1342 
1343 int ar5008_hw_attach_phy_ops(struct ath_hw *ah)
1344 {
1345     struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1346     static const u32 ar5416_cca_regs[6] = {
1347         AR_PHY_CCA,
1348         AR_PHY_CH1_CCA,
1349         AR_PHY_CH2_CCA,
1350         AR_PHY_EXT_CCA,
1351         AR_PHY_CH1_EXT_CCA,
1352         AR_PHY_CH2_EXT_CCA
1353     };
1354     int ret;
1355 
1356     ret = ar5008_hw_rf_alloc_ext_banks(ah);
1357     if (ret)
1358         return ret;
1359 
1360     priv_ops->rf_set_freq = ar5008_hw_set_channel;
1361     priv_ops->spur_mitigate_freq = ar5008_hw_spur_mitigate;
1362 
1363     priv_ops->set_rf_regs = ar5008_hw_set_rf_regs;
1364     priv_ops->set_channel_regs = ar5008_hw_set_channel_regs;
1365     priv_ops->init_bb = ar5008_hw_init_bb;
1366     priv_ops->process_ini = ar5008_hw_process_ini;
1367     priv_ops->set_rfmode = ar5008_hw_set_rfmode;
1368     priv_ops->mark_phy_inactive = ar5008_hw_mark_phy_inactive;
1369     priv_ops->set_delta_slope = ar5008_hw_set_delta_slope;
1370     priv_ops->rfbus_req = ar5008_hw_rfbus_req;
1371     priv_ops->rfbus_done = ar5008_hw_rfbus_done;
1372     priv_ops->restore_chainmask = ar5008_restore_chainmask;
1373     priv_ops->do_getnf = ar5008_hw_do_getnf;
1374     priv_ops->set_radar_params = ar5008_hw_set_radar_params;
1375 
1376     priv_ops->ani_control = ar5008_hw_ani_control_new;
1377     priv_ops->ani_cache_ini_regs = ar5008_hw_ani_cache_ini_regs;
1378 
1379     if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah))
1380         priv_ops->compute_pll_control = ar9160_hw_compute_pll_control;
1381     else
1382         priv_ops->compute_pll_control = ar5008_hw_compute_pll_control;
1383 
1384     ar5008_hw_set_nf_limits(ah);
1385     ar5008_hw_set_radar_conf(ah);
1386     memcpy(ah->nf_regs, ar5416_cca_regs, sizeof(ah->nf_regs));
1387     return 0;
1388 }