10e3d6777SRyder Lee // SPDX-License-Identifier: ISC
21613c621SLorenzo Bianconi /*
31613c621SLorenzo Bianconi * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
41613c621SLorenzo Bianconi */
51613c621SLorenzo Bianconi
61613c621SLorenzo Bianconi #include <linux/delay.h>
71613c621SLorenzo Bianconi #include "mt76x2.h"
81613c621SLorenzo Bianconi #include "mcu.h"
91613c621SLorenzo Bianconi #include "eeprom.h"
1050b9e8d5SLorenzo Bianconi #include "../mt76x02_phy.h"
111613c621SLorenzo Bianconi
121613c621SLorenzo Bianconi static bool
mt76x2_phy_tssi_init_cal(struct mt76x02_dev * dev)13e40803f2SLorenzo Bianconi mt76x2_phy_tssi_init_cal(struct mt76x02_dev *dev)
141613c621SLorenzo Bianconi {
1596747a51SFelix Fietkau struct ieee80211_channel *chan = dev->mphy.chandef.chan;
161613c621SLorenzo Bianconi u32 flag = 0;
171613c621SLorenzo Bianconi
184afeb396SLorenzo Bianconi if (!mt76x2_tssi_enabled(dev))
191613c621SLorenzo Bianconi return false;
201613c621SLorenzo Bianconi
211613c621SLorenzo Bianconi if (mt76x2_channel_silent(dev))
221613c621SLorenzo Bianconi return false;
231613c621SLorenzo Bianconi
241613c621SLorenzo Bianconi if (chan->band == NL80211_BAND_5GHZ)
251613c621SLorenzo Bianconi flag |= BIT(0);
261613c621SLorenzo Bianconi
2726a9daa6SLorenzo Bianconi if (mt76x02_ext_pa_enabled(dev, chan->band))
281613c621SLorenzo Bianconi flag |= BIT(8);
291613c621SLorenzo Bianconi
304ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_TSSI, flag);
311613c621SLorenzo Bianconi dev->cal.tssi_cal_done = true;
321613c621SLorenzo Bianconi return true;
331613c621SLorenzo Bianconi }
341613c621SLorenzo Bianconi
351613c621SLorenzo Bianconi static void
mt76x2_phy_channel_calibrate(struct mt76x02_dev * dev,bool mac_stopped)36e40803f2SLorenzo Bianconi mt76x2_phy_channel_calibrate(struct mt76x02_dev *dev, bool mac_stopped)
371613c621SLorenzo Bianconi {
3896747a51SFelix Fietkau struct ieee80211_channel *chan = dev->mphy.chandef.chan;
391613c621SLorenzo Bianconi bool is_5ghz = chan->band == NL80211_BAND_5GHZ;
401613c621SLorenzo Bianconi
411613c621SLorenzo Bianconi if (dev->cal.channel_cal_done)
421613c621SLorenzo Bianconi return;
431613c621SLorenzo Bianconi
441613c621SLorenzo Bianconi if (mt76x2_channel_silent(dev))
451613c621SLorenzo Bianconi return;
461613c621SLorenzo Bianconi
471613c621SLorenzo Bianconi if (!dev->cal.tssi_cal_done)
481613c621SLorenzo Bianconi mt76x2_phy_tssi_init_cal(dev);
491613c621SLorenzo Bianconi
501613c621SLorenzo Bianconi if (!mac_stopped)
511613c621SLorenzo Bianconi mt76x2_mac_stop(dev, false);
521613c621SLorenzo Bianconi
531613c621SLorenzo Bianconi if (is_5ghz)
544ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_LC, 0);
551613c621SLorenzo Bianconi
564ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_TX_LOFT, is_5ghz);
574ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_TXIQ, is_5ghz);
584ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_RXIQC_FI, is_5ghz);
594ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_TEMP_SENSOR, 0);
604ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_TX_SHAPING, 0);
611613c621SLorenzo Bianconi
621613c621SLorenzo Bianconi if (!mac_stopped)
631613c621SLorenzo Bianconi mt76x2_mac_resume(dev);
641613c621SLorenzo Bianconi
651613c621SLorenzo Bianconi mt76x2_apply_gain_adj(dev);
66a78f1547SLorenzo Bianconi mt76x02_edcca_init(dev);
671613c621SLorenzo Bianconi
681613c621SLorenzo Bianconi dev->cal.channel_cal_done = true;
691613c621SLorenzo Bianconi }
701613c621SLorenzo Bianconi
mt76x2_phy_set_antenna(struct mt76x02_dev * dev)71e40803f2SLorenzo Bianconi void mt76x2_phy_set_antenna(struct mt76x02_dev *dev)
721613c621SLorenzo Bianconi {
731613c621SLorenzo Bianconi u32 val;
741613c621SLorenzo Bianconi
751613c621SLorenzo Bianconi val = mt76_rr(dev, MT_BBP(AGC, 0));
761613c621SLorenzo Bianconi val &= ~(BIT(4) | BIT(1));
77*beaaeb6bSFelix Fietkau switch (dev->mphy.antenna_mask) {
781613c621SLorenzo Bianconi case 1:
791613c621SLorenzo Bianconi /* disable mac DAC control */
801613c621SLorenzo Bianconi mt76_clear(dev, MT_BBP(IBI, 9), BIT(11));
811613c621SLorenzo Bianconi mt76_clear(dev, MT_BBP(TXBE, 5), 3);
821613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_TX_PIN_CFG, MT_TX_PIN_CFG_TXANT, 0x3);
831613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_BBP(CORE, 32), GENMASK(21, 20), 2);
841613c621SLorenzo Bianconi /* disable DAC 1 */
851613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_BBP(CORE, 33), GENMASK(12, 9), 4);
861613c621SLorenzo Bianconi
871613c621SLorenzo Bianconi val &= ~(BIT(3) | BIT(0));
881613c621SLorenzo Bianconi break;
891613c621SLorenzo Bianconi case 2:
901613c621SLorenzo Bianconi /* disable mac DAC control */
911613c621SLorenzo Bianconi mt76_clear(dev, MT_BBP(IBI, 9), BIT(11));
921613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_BBP(TXBE, 5), 3, 1);
931613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_TX_PIN_CFG, MT_TX_PIN_CFG_TXANT, 0xc);
941613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_BBP(CORE, 32), GENMASK(21, 20), 1);
951613c621SLorenzo Bianconi /* disable DAC 0 */
961613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_BBP(CORE, 33), GENMASK(12, 9), 1);
971613c621SLorenzo Bianconi
981613c621SLorenzo Bianconi val &= ~BIT(3);
991613c621SLorenzo Bianconi val |= BIT(0);
1001613c621SLorenzo Bianconi break;
1011613c621SLorenzo Bianconi case 3:
1021613c621SLorenzo Bianconi default:
1031613c621SLorenzo Bianconi /* enable mac DAC control */
1041613c621SLorenzo Bianconi mt76_set(dev, MT_BBP(IBI, 9), BIT(11));
1051613c621SLorenzo Bianconi mt76_set(dev, MT_BBP(TXBE, 5), 3);
1061613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_TX_PIN_CFG, MT_TX_PIN_CFG_TXANT, 0xf);
1071613c621SLorenzo Bianconi mt76_clear(dev, MT_BBP(CORE, 32), GENMASK(21, 20));
1081613c621SLorenzo Bianconi mt76_clear(dev, MT_BBP(CORE, 33), GENMASK(12, 9));
1091613c621SLorenzo Bianconi
1101613c621SLorenzo Bianconi val &= ~BIT(0);
1111613c621SLorenzo Bianconi val |= BIT(3);
1121613c621SLorenzo Bianconi break;
1131613c621SLorenzo Bianconi }
1141613c621SLorenzo Bianconi mt76_wr(dev, MT_BBP(AGC, 0), val);
1151613c621SLorenzo Bianconi }
1161613c621SLorenzo Bianconi
mt76x2_phy_set_channel(struct mt76x02_dev * dev,struct cfg80211_chan_def * chandef)117e40803f2SLorenzo Bianconi int mt76x2_phy_set_channel(struct mt76x02_dev *dev,
1181613c621SLorenzo Bianconi struct cfg80211_chan_def *chandef)
1191613c621SLorenzo Bianconi {
1201613c621SLorenzo Bianconi struct ieee80211_channel *chan = chandef->chan;
121011849e0SFelix Fietkau bool scan = test_bit(MT76_SCANNING, &dev->mphy.state);
1221613c621SLorenzo Bianconi enum nl80211_band band = chan->band;
1231613c621SLorenzo Bianconi u8 channel;
1241613c621SLorenzo Bianconi
1251613c621SLorenzo Bianconi u32 ext_cca_chan[4] = {
1261613c621SLorenzo Bianconi [0] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 0) |
1271613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 1) |
1281613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) |
1291613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) |
1301613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(0)),
1311613c621SLorenzo Bianconi [1] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 1) |
1321613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 0) |
1331613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) |
1341613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) |
1351613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(1)),
1361613c621SLorenzo Bianconi [2] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 2) |
1371613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 3) |
1381613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) |
1391613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) |
1401613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(2)),
1411613c621SLorenzo Bianconi [3] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 3) |
1421613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 2) |
1431613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) |
1441613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) |
1451613c621SLorenzo Bianconi FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(3)),
1461613c621SLorenzo Bianconi };
1471613c621SLorenzo Bianconi int ch_group_index;
1481613c621SLorenzo Bianconi u8 bw, bw_index;
1491613c621SLorenzo Bianconi int freq, freq1;
1501613c621SLorenzo Bianconi int ret;
1511613c621SLorenzo Bianconi
1521613c621SLorenzo Bianconi dev->cal.channel_cal_done = false;
1531613c621SLorenzo Bianconi freq = chandef->chan->center_freq;
1541613c621SLorenzo Bianconi freq1 = chandef->center_freq1;
1551613c621SLorenzo Bianconi channel = chan->hw_value;
1561613c621SLorenzo Bianconi
1571613c621SLorenzo Bianconi switch (chandef->width) {
1581613c621SLorenzo Bianconi case NL80211_CHAN_WIDTH_40:
1591613c621SLorenzo Bianconi bw = 1;
1601613c621SLorenzo Bianconi if (freq1 > freq) {
1611613c621SLorenzo Bianconi bw_index = 1;
1621613c621SLorenzo Bianconi ch_group_index = 0;
1631613c621SLorenzo Bianconi } else {
1641613c621SLorenzo Bianconi bw_index = 3;
1651613c621SLorenzo Bianconi ch_group_index = 1;
1661613c621SLorenzo Bianconi }
1671613c621SLorenzo Bianconi channel += 2 - ch_group_index * 4;
1681613c621SLorenzo Bianconi break;
1691613c621SLorenzo Bianconi case NL80211_CHAN_WIDTH_80:
1701613c621SLorenzo Bianconi ch_group_index = (freq - freq1 + 30) / 20;
1711613c621SLorenzo Bianconi if (WARN_ON(ch_group_index < 0 || ch_group_index > 3))
1721613c621SLorenzo Bianconi ch_group_index = 0;
1731613c621SLorenzo Bianconi bw = 2;
1741613c621SLorenzo Bianconi bw_index = ch_group_index;
1751613c621SLorenzo Bianconi channel += 6 - ch_group_index * 4;
1761613c621SLorenzo Bianconi break;
1771613c621SLorenzo Bianconi default:
1781613c621SLorenzo Bianconi bw = 0;
1791613c621SLorenzo Bianconi bw_index = 0;
1801613c621SLorenzo Bianconi ch_group_index = 0;
1811613c621SLorenzo Bianconi break;
1821613c621SLorenzo Bianconi }
1831613c621SLorenzo Bianconi
1841613c621SLorenzo Bianconi mt76x2_read_rx_gain(dev);
1851613c621SLorenzo Bianconi mt76x2_phy_set_txpower_regs(dev, band);
1861613c621SLorenzo Bianconi mt76x2_configure_tx_delay(dev, band, bw);
1871613c621SLorenzo Bianconi mt76x2_phy_set_txpower(dev);
1881613c621SLorenzo Bianconi
189370c6415SLorenzo Bianconi mt76x02_phy_set_band(dev, chan->band, ch_group_index & 1);
190032c08f4SLorenzo Bianconi mt76x02_phy_set_bw(dev, chandef->width, ch_group_index);
1911613c621SLorenzo Bianconi
1921613c621SLorenzo Bianconi mt76_rmw(dev, MT_EXT_CCA_CFG,
1931613c621SLorenzo Bianconi (MT_EXT_CCA_CFG_CCA0 |
1941613c621SLorenzo Bianconi MT_EXT_CCA_CFG_CCA1 |
1951613c621SLorenzo Bianconi MT_EXT_CCA_CFG_CCA2 |
1961613c621SLorenzo Bianconi MT_EXT_CCA_CFG_CCA3 |
1971613c621SLorenzo Bianconi MT_EXT_CCA_CFG_CCA_MASK),
1981613c621SLorenzo Bianconi ext_cca_chan[ch_group_index]);
1991613c621SLorenzo Bianconi
2001613c621SLorenzo Bianconi ret = mt76x2_mcu_set_channel(dev, channel, bw, bw_index, scan);
2011613c621SLorenzo Bianconi if (ret)
2021613c621SLorenzo Bianconi return ret;
2031613c621SLorenzo Bianconi
2041613c621SLorenzo Bianconi mt76x2_mcu_init_gain(dev, channel, dev->cal.rx.mcu_gain, true);
2051613c621SLorenzo Bianconi
2061613c621SLorenzo Bianconi mt76x2_phy_set_antenna(dev);
2071613c621SLorenzo Bianconi
2081613c621SLorenzo Bianconi /* Enable LDPC Rx */
2091613c621SLorenzo Bianconi if (mt76xx_rev(dev) >= MT76XX_REV_E3)
2101613c621SLorenzo Bianconi mt76_set(dev, MT_BBP(RXO, 13), BIT(10));
2111613c621SLorenzo Bianconi
2121613c621SLorenzo Bianconi if (!dev->cal.init_cal_done) {
21326a9daa6SLorenzo Bianconi u8 val = mt76x02_eeprom_get(dev, MT_EE_BT_RCAL_RESULT);
2141613c621SLorenzo Bianconi
2151613c621SLorenzo Bianconi if (val != 0xff)
2164ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_R, 0);
2171613c621SLorenzo Bianconi }
2181613c621SLorenzo Bianconi
2194ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_RXDCOC, channel);
2201613c621SLorenzo Bianconi
2211613c621SLorenzo Bianconi /* Rx LPF calibration */
2221613c621SLorenzo Bianconi if (!dev->cal.init_cal_done)
2234ece1e0aSStanislaw Gruszka mt76x02_mcu_calibrate(dev, MCU_CAL_RC, 0);
2241613c621SLorenzo Bianconi
2251613c621SLorenzo Bianconi dev->cal.init_cal_done = true;
2261613c621SLorenzo Bianconi
2271613c621SLorenzo Bianconi mt76_wr(dev, MT_BBP(AGC, 61), 0xFF64A4E2);
2281613c621SLorenzo Bianconi mt76_wr(dev, MT_BBP(AGC, 7), 0x08081010);
2291613c621SLorenzo Bianconi mt76_wr(dev, MT_BBP(AGC, 11), 0x00000404);
2301613c621SLorenzo Bianconi mt76_wr(dev, MT_BBP(AGC, 2), 0x00007070);
2311613c621SLorenzo Bianconi mt76_wr(dev, MT_TXOP_CTRL_CFG, 0x04101B3F);
2321613c621SLorenzo Bianconi
2334606a26cSFelix Fietkau if (scan)
2341613c621SLorenzo Bianconi return 0;
2351613c621SLorenzo Bianconi
2361613c621SLorenzo Bianconi mt76x2_phy_channel_calibrate(dev, true);
237e48797e9SLorenzo Bianconi mt76x02_init_agc_gain(dev);
2381613c621SLorenzo Bianconi
2391613c621SLorenzo Bianconi /* init default values for temp compensation */
2404afeb396SLorenzo Bianconi if (mt76x2_tssi_enabled(dev)) {
2411613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP,
2421613c621SLorenzo Bianconi 0x38);
2431613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_TX_ALC_CFG_2, MT_TX_ALC_CFG_2_TEMP_COMP,
2441613c621SLorenzo Bianconi 0x38);
2451613c621SLorenzo Bianconi }
2461613c621SLorenzo Bianconi
2471613c621SLorenzo Bianconi ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work,
2481613c621SLorenzo Bianconi MT_CALIBRATE_INTERVAL);
2491613c621SLorenzo Bianconi
2501613c621SLorenzo Bianconi return 0;
2511613c621SLorenzo Bianconi }
2521613c621SLorenzo Bianconi
2531613c621SLorenzo Bianconi static void
mt76x2_phy_temp_compensate(struct mt76x02_dev * dev)254e40803f2SLorenzo Bianconi mt76x2_phy_temp_compensate(struct mt76x02_dev *dev)
2551613c621SLorenzo Bianconi {
2561613c621SLorenzo Bianconi struct mt76x2_temp_comp t;
2571613c621SLorenzo Bianconi int temp, db_diff;
2581613c621SLorenzo Bianconi
2591613c621SLorenzo Bianconi if (mt76x2_get_temp_comp(dev, &t))
2601613c621SLorenzo Bianconi return;
2611613c621SLorenzo Bianconi
2621613c621SLorenzo Bianconi temp = mt76_get_field(dev, MT_TEMP_SENSOR, MT_TEMP_SENSOR_VAL);
2631613c621SLorenzo Bianconi temp -= t.temp_25_ref;
2641613c621SLorenzo Bianconi temp = (temp * 1789) / 1000 + 25;
2651613c621SLorenzo Bianconi dev->cal.temp = temp;
2661613c621SLorenzo Bianconi
2671613c621SLorenzo Bianconi if (temp > 25)
2681613c621SLorenzo Bianconi db_diff = (temp - 25) / t.high_slope;
2691613c621SLorenzo Bianconi else
2701613c621SLorenzo Bianconi db_diff = (25 - temp) / t.low_slope;
2711613c621SLorenzo Bianconi
2721613c621SLorenzo Bianconi db_diff = min(db_diff, t.upper_bound);
2731613c621SLorenzo Bianconi db_diff = max(db_diff, t.lower_bound);
2741613c621SLorenzo Bianconi
2751613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP,
2761613c621SLorenzo Bianconi db_diff * 2);
2771613c621SLorenzo Bianconi mt76_rmw_field(dev, MT_TX_ALC_CFG_2, MT_TX_ALC_CFG_2_TEMP_COMP,
2781613c621SLorenzo Bianconi db_diff * 2);
2791613c621SLorenzo Bianconi }
2801613c621SLorenzo Bianconi
mt76x2_phy_calibrate(struct work_struct * work)2811613c621SLorenzo Bianconi void mt76x2_phy_calibrate(struct work_struct *work)
2821613c621SLorenzo Bianconi {
283e40803f2SLorenzo Bianconi struct mt76x02_dev *dev;
2841613c621SLorenzo Bianconi
285e40803f2SLorenzo Bianconi dev = container_of(work, struct mt76x02_dev, cal_work.work);
2868aac454dSLorenzo Bianconi
2878aac454dSLorenzo Bianconi mutex_lock(&dev->mt76.mutex);
2888aac454dSLorenzo Bianconi
2891613c621SLorenzo Bianconi mt76x2_phy_channel_calibrate(dev, false);
2904ece1e0aSStanislaw Gruszka mt76x2_phy_tssi_compensate(dev);
2911613c621SLorenzo Bianconi mt76x2_phy_temp_compensate(dev);
2921613c621SLorenzo Bianconi mt76x2_phy_update_channel_gain(dev);
2938aac454dSLorenzo Bianconi
2948aac454dSLorenzo Bianconi mutex_unlock(&dev->mt76.mutex);
2958aac454dSLorenzo Bianconi
2961613c621SLorenzo Bianconi ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work,
2971613c621SLorenzo Bianconi MT_CALIBRATE_INTERVAL);
2981613c621SLorenzo Bianconi }
2991613c621SLorenzo Bianconi
mt76x2_phy_start(struct mt76x02_dev * dev)300e40803f2SLorenzo Bianconi int mt76x2_phy_start(struct mt76x02_dev *dev)
3011613c621SLorenzo Bianconi {
3021613c621SLorenzo Bianconi int ret;
3031613c621SLorenzo Bianconi
304c6950536SStanislaw Gruszka ret = mt76x02_mcu_set_radio_state(dev, true);
3051613c621SLorenzo Bianconi if (ret)
3061613c621SLorenzo Bianconi return ret;
3071613c621SLorenzo Bianconi
3081613c621SLorenzo Bianconi mt76x2_mcu_load_cr(dev, MT_RF_BBP_CR, 0, 0);
3091613c621SLorenzo Bianconi
3101613c621SLorenzo Bianconi return ret;
3111613c621SLorenzo Bianconi }
312