Path: blob/main/sys/contrib/dev/mediatek/mt76/mt76x2/usb_phy.c
48526 views
// SPDX-License-Identifier: ISC1/*2* Copyright (C) 2018 Lorenzo Bianconi <[email protected]>3*/45#include "mt76x2u.h"6#include "eeprom.h"7#include "../mt76x02_phy.h"89static void10mt76x2u_phy_channel_calibrate(struct mt76x02_dev *dev, bool mac_stopped)11{12struct ieee80211_channel *chan = dev->mphy.chandef.chan;13bool is_5ghz = chan->band == NL80211_BAND_5GHZ;1415if (dev->cal.channel_cal_done)16return;1718if (mt76x2_channel_silent(dev))19return;2021if (!mac_stopped)22mt76x2u_mac_stop(dev);2324if (is_5ghz)25mt76x02_mcu_calibrate(dev, MCU_CAL_LC, 0);2627mt76x02_mcu_calibrate(dev, MCU_CAL_TX_LOFT, is_5ghz);28mt76x02_mcu_calibrate(dev, MCU_CAL_TXIQ, is_5ghz);29mt76x02_mcu_calibrate(dev, MCU_CAL_RXIQC_FI, is_5ghz);30mt76x02_mcu_calibrate(dev, MCU_CAL_TEMP_SENSOR, 0);31mt76x02_mcu_calibrate(dev, MCU_CAL_TX_SHAPING, 0);3233if (!mac_stopped)34mt76x2_mac_resume(dev);35mt76x2_apply_gain_adj(dev);36mt76x02_edcca_init(dev);3738dev->cal.channel_cal_done = true;39}4041void mt76x2u_phy_calibrate(struct work_struct *work)42{43struct mt76x02_dev *dev;4445dev = container_of(work, struct mt76x02_dev, cal_work.work);4647mutex_lock(&dev->mt76.mutex);4849mt76x2u_phy_channel_calibrate(dev, false);50mt76x2_phy_tssi_compensate(dev);51mt76x2_phy_update_channel_gain(dev);5253mutex_unlock(&dev->mt76.mutex);5455ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work,56MT_CALIBRATE_INTERVAL);57}5859int mt76x2u_phy_set_channel(struct mt76x02_dev *dev,60struct cfg80211_chan_def *chandef)61{62u32 ext_cca_chan[4] = {63[0] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 0) |64FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 1) |65FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) |66FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) |67FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(0)),68[1] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 1) |69FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 0) |70FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) |71FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) |72FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(1)),73[2] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 2) |74FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 3) |75FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) |76FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) |77FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(2)),78[3] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 3) |79FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 2) |80FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) |81FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) |82FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(3)),83};84bool scan = test_bit(MT76_SCANNING, &dev->mphy.state);85struct ieee80211_channel *chan = chandef->chan;86u8 channel = chan->hw_value, bw, bw_index;87int ch_group_index, freq, freq1, ret;8889dev->cal.channel_cal_done = false;90freq = chandef->chan->center_freq;91freq1 = chandef->center_freq1;9293switch (chandef->width) {94case NL80211_CHAN_WIDTH_40:95bw = 1;96if (freq1 > freq) {97bw_index = 1;98ch_group_index = 0;99} else {100bw_index = 3;101ch_group_index = 1;102}103channel += 2 - ch_group_index * 4;104break;105case NL80211_CHAN_WIDTH_80:106ch_group_index = (freq - freq1 + 30) / 20;107if (WARN_ON(ch_group_index < 0 || ch_group_index > 3))108ch_group_index = 0;109bw = 2;110bw_index = ch_group_index;111channel += 6 - ch_group_index * 4;112break;113default:114bw = 0;115bw_index = 0;116ch_group_index = 0;117break;118}119120mt76x2_read_rx_gain(dev);121mt76x2_phy_set_txpower_regs(dev, chan->band);122mt76x2_configure_tx_delay(dev, chan->band, bw);123mt76x2_phy_set_txpower(dev);124125mt76x02_phy_set_band(dev, chan->band, ch_group_index & 1);126mt76x02_phy_set_bw(dev, chandef->width, ch_group_index);127128mt76_rmw(dev, MT_EXT_CCA_CFG,129(MT_EXT_CCA_CFG_CCA0 |130MT_EXT_CCA_CFG_CCA1 |131MT_EXT_CCA_CFG_CCA2 |132MT_EXT_CCA_CFG_CCA3 |133MT_EXT_CCA_CFG_CCA_MASK),134ext_cca_chan[ch_group_index]);135136ret = mt76x2_mcu_set_channel(dev, channel, bw, bw_index, scan);137if (ret)138return ret;139140mt76x2_mcu_init_gain(dev, channel, dev->cal.rx.mcu_gain, true);141142/* Enable LDPC Rx */143if (mt76xx_rev(dev) >= MT76XX_REV_E3)144mt76_set(dev, MT_BBP(RXO, 13), BIT(10));145146if (!dev->cal.init_cal_done) {147u8 val = mt76x02_eeprom_get(dev, MT_EE_BT_RCAL_RESULT);148149if (val != 0xff)150mt76x02_mcu_calibrate(dev, MCU_CAL_R, 0);151}152153mt76x02_mcu_calibrate(dev, MCU_CAL_RXDCOC, channel);154155/* Rx LPF calibration */156if (!dev->cal.init_cal_done)157mt76x02_mcu_calibrate(dev, MCU_CAL_RC, 0);158dev->cal.init_cal_done = true;159160mt76_wr(dev, MT_BBP(AGC, 61), 0xff64a4e2);161mt76_wr(dev, MT_BBP(AGC, 7), 0x08081010);162mt76_wr(dev, MT_BBP(AGC, 11), 0x00000404);163mt76_wr(dev, MT_BBP(AGC, 2), 0x00007070);164mt76_wr(dev, MT_TXOP_CTRL_CFG, 0X04101b3f);165166mt76_set(dev, MT_BBP(TXO, 4), BIT(25));167mt76_set(dev, MT_BBP(RXO, 13), BIT(8));168169if (scan)170return 0;171172mt76x2u_phy_channel_calibrate(dev, true);173mt76x02_init_agc_gain(dev);174175if (mt76x2_tssi_enabled(dev)) {176/* init default values for temp compensation */177mt76_rmw_field(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP,1780x38);179mt76_rmw_field(dev, MT_TX_ALC_CFG_2, MT_TX_ALC_CFG_2_TEMP_COMP,1800x38);181182/* init tssi calibration */183if (!mt76x2_channel_silent(dev)) {184struct ieee80211_channel *chan;185u32 flag = 0;186187chan = dev->mphy.chandef.chan;188if (chan->band == NL80211_BAND_5GHZ)189flag |= BIT(0);190if (mt76x02_ext_pa_enabled(dev, chan->band))191flag |= BIT(8);192mt76x02_mcu_calibrate(dev, MCU_CAL_TSSI, flag);193dev->cal.tssi_cal_done = true;194}195}196197ieee80211_queue_delayed_work(mt76_hw(dev), &dev->cal_work,198MT_CALIBRATE_INTERVAL);199return 0;200}201202203