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, true); 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 mt76x2u_phy_channel_calibrate(dev, false); 59 mt76x2_phy_tssi_compensate(dev); 60 mt76x2_phy_update_channel_gain(dev); 61 62 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work, 63 MT_CALIBRATE_INTERVAL); 64 } 65 66 int mt76x2u_phy_set_channel(struct mt76x02_dev *dev, 67 struct cfg80211_chan_def *chandef) 68 { 69 u32 ext_cca_chan[4] = { 70 [0] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 0) | 71 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 1) | 72 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) | 73 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) | 74 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(0)), 75 [1] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 1) | 76 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 0) | 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(1)), 80 [2] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 2) | 81 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 3) | 82 FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) | 83 FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) | 84 FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(2)), 85 [3] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 3) | 86 FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 2) | 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(3)), 90 }; 91 bool scan = test_bit(MT76_SCANNING, &dev->mt76.state); 92 struct ieee80211_channel *chan = chandef->chan; 93 u8 channel = chan->hw_value, bw, bw_index; 94 int ch_group_index, freq, freq1, ret; 95 96 dev->cal.channel_cal_done = false; 97 freq = chandef->chan->center_freq; 98 freq1 = chandef->center_freq1; 99 100 switch (chandef->width) { 101 case NL80211_CHAN_WIDTH_40: 102 bw = 1; 103 if (freq1 > freq) { 104 bw_index = 1; 105 ch_group_index = 0; 106 } else { 107 bw_index = 3; 108 ch_group_index = 1; 109 } 110 channel += 2 - ch_group_index * 4; 111 break; 112 case NL80211_CHAN_WIDTH_80: 113 ch_group_index = (freq - freq1 + 30) / 20; 114 if (WARN_ON(ch_group_index < 0 || ch_group_index > 3)) 115 ch_group_index = 0; 116 bw = 2; 117 bw_index = ch_group_index; 118 channel += 6 - ch_group_index * 4; 119 break; 120 default: 121 bw = 0; 122 bw_index = 0; 123 ch_group_index = 0; 124 break; 125 } 126 127 mt76x2_read_rx_gain(dev); 128 mt76x2_phy_set_txpower_regs(dev, chan->band); 129 mt76x2_configure_tx_delay(dev, chan->band, bw); 130 mt76x2_phy_set_txpower(dev); 131 132 mt76x02_phy_set_band(dev, chan->band, ch_group_index & 1); 133 mt76x02_phy_set_bw(dev, chandef->width, ch_group_index); 134 135 mt76_rmw(dev, MT_EXT_CCA_CFG, 136 (MT_EXT_CCA_CFG_CCA0 | 137 MT_EXT_CCA_CFG_CCA1 | 138 MT_EXT_CCA_CFG_CCA2 | 139 MT_EXT_CCA_CFG_CCA3 | 140 MT_EXT_CCA_CFG_CCA_MASK), 141 ext_cca_chan[ch_group_index]); 142 143 ret = mt76x2_mcu_set_channel(dev, channel, bw, bw_index, scan); 144 if (ret) 145 return ret; 146 147 mt76x2_mcu_init_gain(dev, channel, dev->cal.rx.mcu_gain, true); 148 149 /* Enable LDPC Rx */ 150 if (mt76xx_rev(dev) >= MT76XX_REV_E3) 151 mt76_set(dev, MT_BBP(RXO, 13), BIT(10)); 152 153 if (!dev->cal.init_cal_done) { 154 u8 val = mt76x02_eeprom_get(dev, MT_EE_BT_RCAL_RESULT); 155 156 if (val != 0xff) 157 mt76x02_mcu_calibrate(dev, MCU_CAL_R, 0); 158 } 159 160 mt76x02_mcu_calibrate(dev, MCU_CAL_RXDCOC, channel); 161 162 /* Rx LPF calibration */ 163 if (!dev->cal.init_cal_done) 164 mt76x02_mcu_calibrate(dev, MCU_CAL_RC, 0); 165 dev->cal.init_cal_done = true; 166 167 mt76_wr(dev, MT_BBP(AGC, 61), 0xff64a4e2); 168 mt76_wr(dev, MT_BBP(AGC, 7), 0x08081010); 169 mt76_wr(dev, MT_BBP(AGC, 11), 0x00000404); 170 mt76_wr(dev, MT_BBP(AGC, 2), 0x00007070); 171 mt76_wr(dev, MT_TXOP_CTRL_CFG, 0X04101b3f); 172 173 mt76_set(dev, MT_BBP(TXO, 4), BIT(25)); 174 mt76_set(dev, MT_BBP(RXO, 13), BIT(8)); 175 176 if (scan) 177 return 0; 178 179 mt76x2u_phy_channel_calibrate(dev, true); 180 mt76x02_init_agc_gain(dev); 181 182 if (mt76x2_tssi_enabled(dev)) { 183 /* init default values for temp compensation */ 184 mt76_rmw_field(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP, 185 0x38); 186 mt76_rmw_field(dev, MT_TX_ALC_CFG_2, MT_TX_ALC_CFG_2_TEMP_COMP, 187 0x38); 188 189 /* init tssi calibration */ 190 if (!mt76x2_channel_silent(dev)) { 191 struct ieee80211_channel *chan; 192 u32 flag = 0; 193 194 chan = dev->mt76.chandef.chan; 195 if (chan->band == NL80211_BAND_5GHZ) 196 flag |= BIT(0); 197 if (mt76x02_ext_pa_enabled(dev, chan->band)) 198 flag |= BIT(8); 199 mt76x02_mcu_calibrate(dev, MCU_CAL_TSSI, flag); 200 dev->cal.tssi_cal_done = true; 201 } 202 } 203 204 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work, 205 MT_CALIBRATE_INTERVAL); 206 return 0; 207 } 208