1 /* 2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name> 3 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com> 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <linux/kernel.h> 19 20 #include "mt76x02.h" 21 #include "mt76x02_phy.h" 22 23 void mt76x02_phy_set_rxpath(struct mt76x02_dev *dev) 24 { 25 u32 val; 26 27 val = mt76_rr(dev, MT_BBP(AGC, 0)); 28 val &= ~BIT(4); 29 30 switch (dev->mt76.chainmask & 0xf) { 31 case 2: 32 val |= BIT(3); 33 break; 34 default: 35 val &= ~BIT(3); 36 break; 37 } 38 39 mt76_wr(dev, MT_BBP(AGC, 0), val); 40 mb(); 41 val = mt76_rr(dev, MT_BBP(AGC, 0)); 42 } 43 EXPORT_SYMBOL_GPL(mt76x02_phy_set_rxpath); 44 45 void mt76x02_phy_set_txdac(struct mt76x02_dev *dev) 46 { 47 int txpath; 48 49 txpath = (dev->mt76.chainmask >> 8) & 0xf; 50 switch (txpath) { 51 case 2: 52 mt76_set(dev, MT_BBP(TXBE, 5), 0x3); 53 break; 54 default: 55 mt76_clear(dev, MT_BBP(TXBE, 5), 0x3); 56 break; 57 } 58 } 59 EXPORT_SYMBOL_GPL(mt76x02_phy_set_txdac); 60 61 static u32 62 mt76x02_tx_power_mask(u8 v1, u8 v2, u8 v3, u8 v4) 63 { 64 u32 val = 0; 65 66 val |= (v1 & (BIT(6) - 1)) << 0; 67 val |= (v2 & (BIT(6) - 1)) << 8; 68 val |= (v3 & (BIT(6) - 1)) << 16; 69 val |= (v4 & (BIT(6) - 1)) << 24; 70 return val; 71 } 72 73 int mt76x02_get_max_rate_power(struct mt76_rate_power *r) 74 { 75 s8 ret = 0; 76 int i; 77 78 for (i = 0; i < sizeof(r->all); i++) 79 ret = max(ret, r->all[i]); 80 81 return ret; 82 } 83 EXPORT_SYMBOL_GPL(mt76x02_get_max_rate_power); 84 85 void mt76x02_limit_rate_power(struct mt76_rate_power *r, int limit) 86 { 87 int i; 88 89 for (i = 0; i < sizeof(r->all); i++) 90 if (r->all[i] > limit) 91 r->all[i] = limit; 92 } 93 EXPORT_SYMBOL_GPL(mt76x02_limit_rate_power); 94 95 void mt76x02_add_rate_power_offset(struct mt76_rate_power *r, int offset) 96 { 97 int i; 98 99 for (i = 0; i < sizeof(r->all); i++) 100 r->all[i] += offset; 101 } 102 EXPORT_SYMBOL_GPL(mt76x02_add_rate_power_offset); 103 104 void mt76x02_phy_set_txpower(struct mt76x02_dev *dev, int txp_0, int txp_1) 105 { 106 struct mt76_rate_power *t = &dev->mt76.rate_power; 107 108 mt76_rmw_field(dev, MT_TX_ALC_CFG_0, MT_TX_ALC_CFG_0_CH_INIT_0, txp_0); 109 mt76_rmw_field(dev, MT_TX_ALC_CFG_0, MT_TX_ALC_CFG_0_CH_INIT_1, txp_1); 110 111 mt76_wr(dev, MT_TX_PWR_CFG_0, 112 mt76x02_tx_power_mask(t->cck[0], t->cck[2], t->ofdm[0], 113 t->ofdm[2])); 114 mt76_wr(dev, MT_TX_PWR_CFG_1, 115 mt76x02_tx_power_mask(t->ofdm[4], t->ofdm[6], t->ht[0], 116 t->ht[2])); 117 mt76_wr(dev, MT_TX_PWR_CFG_2, 118 mt76x02_tx_power_mask(t->ht[4], t->ht[6], t->ht[8], 119 t->ht[10])); 120 mt76_wr(dev, MT_TX_PWR_CFG_3, 121 mt76x02_tx_power_mask(t->ht[12], t->ht[14], t->stbc[0], 122 t->stbc[2])); 123 mt76_wr(dev, MT_TX_PWR_CFG_4, 124 mt76x02_tx_power_mask(t->stbc[4], t->stbc[6], 0, 0)); 125 mt76_wr(dev, MT_TX_PWR_CFG_7, 126 mt76x02_tx_power_mask(t->ofdm[7], t->vht[8], t->ht[7], 127 t->vht[9])); 128 mt76_wr(dev, MT_TX_PWR_CFG_8, 129 mt76x02_tx_power_mask(t->ht[14], 0, t->vht[8], t->vht[9])); 130 mt76_wr(dev, MT_TX_PWR_CFG_9, 131 mt76x02_tx_power_mask(t->ht[7], 0, t->stbc[8], t->stbc[9])); 132 } 133 EXPORT_SYMBOL_GPL(mt76x02_phy_set_txpower); 134 135 void mt76x02_phy_set_bw(struct mt76x02_dev *dev, int width, u8 ctrl) 136 { 137 int core_val, agc_val; 138 139 switch (width) { 140 case NL80211_CHAN_WIDTH_80: 141 core_val = 3; 142 agc_val = 7; 143 break; 144 case NL80211_CHAN_WIDTH_40: 145 core_val = 2; 146 agc_val = 3; 147 break; 148 default: 149 core_val = 0; 150 agc_val = 1; 151 break; 152 } 153 154 mt76_rmw_field(dev, MT_BBP(CORE, 1), MT_BBP_CORE_R1_BW, core_val); 155 mt76_rmw_field(dev, MT_BBP(AGC, 0), MT_BBP_AGC_R0_BW, agc_val); 156 mt76_rmw_field(dev, MT_BBP(AGC, 0), MT_BBP_AGC_R0_CTRL_CHAN, ctrl); 157 mt76_rmw_field(dev, MT_BBP(TXBE, 0), MT_BBP_TXBE_R0_CTRL_CHAN, ctrl); 158 } 159 EXPORT_SYMBOL_GPL(mt76x02_phy_set_bw); 160 161 void mt76x02_phy_set_band(struct mt76x02_dev *dev, int band, 162 bool primary_upper) 163 { 164 switch (band) { 165 case NL80211_BAND_2GHZ: 166 mt76_set(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_2G); 167 mt76_clear(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_5G); 168 break; 169 case NL80211_BAND_5GHZ: 170 mt76_clear(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_2G); 171 mt76_set(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_5G); 172 break; 173 } 174 175 mt76_rmw_field(dev, MT_TX_BAND_CFG, MT_TX_BAND_CFG_UPPER_40M, 176 primary_upper); 177 } 178 EXPORT_SYMBOL_GPL(mt76x02_phy_set_band); 179 180 bool mt76x02_phy_adjust_vga_gain(struct mt76x02_dev *dev) 181 { 182 u8 limit = dev->cal.low_gain > 0 ? 16 : 4; 183 bool ret = false; 184 u32 false_cca; 185 186 false_cca = FIELD_GET(MT_RX_STAT_1_CCA_ERRORS, mt76_rr(dev, MT_RX_STAT_1)); 187 dev->cal.false_cca = false_cca; 188 if (false_cca > 800 && dev->cal.agc_gain_adjust < limit) { 189 dev->cal.agc_gain_adjust += 2; 190 ret = true; 191 } else if ((false_cca < 10 && dev->cal.agc_gain_adjust > 0) || 192 (dev->cal.agc_gain_adjust >= limit && false_cca < 500)) { 193 dev->cal.agc_gain_adjust -= 2; 194 ret = true; 195 } 196 197 return ret; 198 } 199 EXPORT_SYMBOL_GPL(mt76x02_phy_adjust_vga_gain); 200 201 void mt76x02_init_agc_gain(struct mt76x02_dev *dev) 202 { 203 dev->cal.agc_gain_init[0] = mt76_get_field(dev, MT_BBP(AGC, 8), 204 MT_BBP_AGC_GAIN); 205 dev->cal.agc_gain_init[1] = mt76_get_field(dev, MT_BBP(AGC, 9), 206 MT_BBP_AGC_GAIN); 207 memcpy(dev->cal.agc_gain_cur, dev->cal.agc_gain_init, 208 sizeof(dev->cal.agc_gain_cur)); 209 dev->cal.low_gain = -1; 210 dev->cal.gain_init_done = true; 211 } 212 EXPORT_SYMBOL_GPL(mt76x02_init_agc_gain); 213