1 /* 2 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com> 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 17 #include "mt76x2u.h" 18 #include "eeprom.h" 19 #include "../mt76x02_phy.h" 20 21 static void 22 mt76x2u_phy_channel_calibrate(struct mt76x02_dev *dev, bool mac_stopped) 23 { 24 struct ieee80211_channel *chan = dev->mt76.chandef.chan; 25 bool is_5ghz = chan->band == NL80211_BAND_5GHZ; 26 27 if (dev->cal.channel_cal_done) 28 return; 29 30 if (mt76x2_channel_silent(dev)) 31 return; 32 33 if (!mac_stopped) 34 mt76x2u_mac_stop(dev); 35 36 if (is_5ghz) 37 mt76x02_mcu_calibrate(dev, MCU_CAL_LC, 0); 38 39 mt76x02_mcu_calibrate(dev, MCU_CAL_TX_LOFT, is_5ghz); 40 mt76x02_mcu_calibrate(dev, MCU_CAL_TXIQ, is_5ghz); 41 mt76x02_mcu_calibrate(dev, MCU_CAL_RXIQC_FI, is_5ghz); 42 mt76x02_mcu_calibrate(dev, MCU_CAL_TEMP_SENSOR, 0); 43 mt76x02_mcu_calibrate(dev, MCU_CAL_TX_SHAPING, 0); 44 45 if (!mac_stopped) 46 mt76x2_mac_resume(dev); 47 mt76x2_apply_gain_adj(dev); 48 mt76x02_edcca_init(dev); 49 50 dev->cal.channel_cal_done = true; 51 } 52 53 void mt76x2u_phy_calibrate(struct work_struct *work) 54 { 55 struct mt76x02_dev *dev; 56 57 dev = container_of(work, struct mt76x02_dev, cal_work.work); 58 59 mutex_lock(&dev->mt76.mutex); 60 61 mt76x2u_phy_channel_calibrate(dev, false); 62 mt76x2_phy_tssi_compensate(dev); 63 mt76x2_phy_update_channel_gain(dev); 64 65 mutex_unlock(&dev->mt76.mutex); 66 67 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work, 68 MT_CALIBRATE_INTERVAL); 69 } 70 71 int mt76x2u_phy_set_channel(struct mt76x02_dev *dev, 72 struct cfg80211_chan_def *chandef) 73 { 74 u32 ext_cca_chan[4] = { 75 [0] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 0) | 76 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 1) | 77 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) | 78 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) | 79 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(0)), 80 [1] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 1) | 81 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 0) | 82 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) | 83 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) | 84 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(1)), 85 [2] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 2) | 86 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 3) | 87 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) | 88 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) | 89 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(2)), 90 [3] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 3) | 91 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 2) | 92 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) | 93 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) | 94 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(3)), 95 }; 96 bool scan = test_bit(MT76_SCANNING, &dev->mt76.state); 97 struct ieee80211_channel *chan = chandef->chan; 98 u8 channel = chan->hw_value, bw, bw_index; 99 int ch_group_index, freq, freq1, ret; 100 101 dev->cal.channel_cal_done = false; 102 freq = chandef->chan->center_freq; 103 freq1 = chandef->center_freq1; 104 105 switch (chandef->width) { 106 case NL80211_CHAN_WIDTH_40: 107 bw = 1; 108 if (freq1 > freq) { 109 bw_index = 1; 110 ch_group_index = 0; 111 } else { 112 bw_index = 3; 113 ch_group_index = 1; 114 } 115 channel += 2 - ch_group_index * 4; 116 break; 117 case NL80211_CHAN_WIDTH_80: 118 ch_group_index = (freq - freq1 + 30) / 20; 119 if (WARN_ON(ch_group_index < 0 || ch_group_index > 3)) 120 ch_group_index = 0; 121 bw = 2; 122 bw_index = ch_group_index; 123 channel += 6 - ch_group_index * 4; 124 break; 125 default: 126 bw = 0; 127 bw_index = 0; 128 ch_group_index = 0; 129 break; 130 } 131 132 mt76x2_read_rx_gain(dev); 133 mt76x2_phy_set_txpower_regs(dev, chan->band); 134 mt76x2_configure_tx_delay(dev, chan->band, bw); 135 mt76x2_phy_set_txpower(dev); 136 137 mt76x02_phy_set_band(dev, chan->band, ch_group_index & 1); 138 mt76x02_phy_set_bw(dev, chandef->width, ch_group_index); 139 140 mt76_rmw(dev, MT_EXT_CCA_CFG, 141 (MT_EXT_CCA_CFG_CCA0 | 142 MT_EXT_CCA_CFG_CCA1 | 143 MT_EXT_CCA_CFG_CCA2 | 144 MT_EXT_CCA_CFG_CCA3 | 145 MT_EXT_CCA_CFG_CCA_MASK), 146 ext_cca_chan[ch_group_index]); 147 148 ret = mt76x2_mcu_set_channel(dev, channel, bw, bw_index, scan); 149 if (ret) 150 return ret; 151 152 mt76x2_mcu_init_gain(dev, channel, dev->cal.rx.mcu_gain, true); 153 154 /* Enable LDPC Rx */ 155 if (mt76xx_rev(dev) >= MT76XX_REV_E3) 156 mt76_set(dev, MT_BBP(RXO, 13), BIT(10)); 157 158 if (!dev->cal.init_cal_done) { 159 u8 val = mt76x02_eeprom_get(dev, MT_EE_BT_RCAL_RESULT); 160 161 if (val != 0xff) 162 mt76x02_mcu_calibrate(dev, MCU_CAL_R, 0); 163 } 164 165 mt76x02_mcu_calibrate(dev, MCU_CAL_RXDCOC, channel); 166 167 /* Rx LPF calibration */ 168 if (!dev->cal.init_cal_done) 169 mt76x02_mcu_calibrate(dev, MCU_CAL_RC, 0); 170 dev->cal.init_cal_done = true; 171 172 mt76_wr(dev, MT_BBP(AGC, 61), 0xff64a4e2); 173 mt76_wr(dev, MT_BBP(AGC, 7), 0x08081010); 174 mt76_wr(dev, MT_BBP(AGC, 11), 0x00000404); 175 mt76_wr(dev, MT_BBP(AGC, 2), 0x00007070); 176 mt76_wr(dev, MT_TXOP_CTRL_CFG, 0X04101b3f); 177 178 mt76_set(dev, MT_BBP(TXO, 4), BIT(25)); 179 mt76_set(dev, MT_BBP(RXO, 13), BIT(8)); 180 181 if (scan) 182 return 0; 183 184 mt76x2u_phy_channel_calibrate(dev, true); 185 mt76x02_init_agc_gain(dev); 186 187 if (mt76x2_tssi_enabled(dev)) { 188 /* init default values for temp compensation */ 189 mt76_rmw_field(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP, 190 0x38); 191 mt76_rmw_field(dev, MT_TX_ALC_CFG_2, MT_TX_ALC_CFG_2_TEMP_COMP, 192 0x38); 193 194 /* init tssi calibration */ 195 if (!mt76x2_channel_silent(dev)) { 196 struct ieee80211_channel *chan; 197 u32 flag = 0; 198 199 chan = dev->mt76.chandef.chan; 200 if (chan->band == NL80211_BAND_5GHZ) 201 flag |= BIT(0); 202 if (mt76x02_ext_pa_enabled(dev, chan->band)) 203 flag |= BIT(8); 204 mt76x02_mcu_calibrate(dev, MCU_CAL_TSSI, flag); 205 dev->cal.tssi_cal_done = true; 206 } 207 } 208 209 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work, 210 MT_CALIBRATE_INTERVAL); 211 return 0; 212 } 213