18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: ISC
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
48c2ecf20Sopenharmony_ci * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include "mt76x2.h"
88c2ecf20Sopenharmony_ci#include "eeprom.h"
98c2ecf20Sopenharmony_ci#include "../mt76x02_phy.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic void
128c2ecf20Sopenharmony_cimt76x2_set_wlan_state(struct mt76x02_dev *dev, bool enable)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	u32 val = mt76_rr(dev, MT_WLAN_FUN_CTRL);
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	if (enable)
178c2ecf20Sopenharmony_ci		val |= (MT_WLAN_FUN_CTRL_WLAN_EN |
188c2ecf20Sopenharmony_ci			MT_WLAN_FUN_CTRL_WLAN_CLK_EN);
198c2ecf20Sopenharmony_ci	else
208c2ecf20Sopenharmony_ci		val &= ~(MT_WLAN_FUN_CTRL_WLAN_EN |
218c2ecf20Sopenharmony_ci			 MT_WLAN_FUN_CTRL_WLAN_CLK_EN);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
248c2ecf20Sopenharmony_ci	udelay(20);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_civoid mt76x2_reset_wlan(struct mt76x02_dev *dev, bool enable)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	u32 val;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	if (!enable)
328c2ecf20Sopenharmony_ci		goto out;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	val = mt76_rr(dev, MT_WLAN_FUN_CTRL);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	val &= ~MT_WLAN_FUN_CTRL_FRC_WL_ANT_SEL;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	if (val & MT_WLAN_FUN_CTRL_WLAN_EN) {
398c2ecf20Sopenharmony_ci		val |= MT_WLAN_FUN_CTRL_WLAN_RESET_RF;
408c2ecf20Sopenharmony_ci		mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
418c2ecf20Sopenharmony_ci		udelay(20);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci		val &= ~MT_WLAN_FUN_CTRL_WLAN_RESET_RF;
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
478c2ecf20Sopenharmony_ci	udelay(20);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciout:
508c2ecf20Sopenharmony_ci	mt76x2_set_wlan_state(dev, enable);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x2_reset_wlan);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_civoid mt76_write_mac_initvals(struct mt76x02_dev *dev)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci#define DEFAULT_PROT_CFG_CCK				\
578c2ecf20Sopenharmony_ci	(FIELD_PREP(MT_PROT_CFG_RATE, 0x3) |		\
588c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_NAV, 1) |		\
598c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_TXOP_ALLOW, 0x3f) |	\
608c2ecf20Sopenharmony_ci	 MT_PROT_CFG_RTS_THRESH)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define DEFAULT_PROT_CFG_OFDM				\
638c2ecf20Sopenharmony_ci	(FIELD_PREP(MT_PROT_CFG_RATE, 0x2004) |		\
648c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_NAV, 1) |			\
658c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_TXOP_ALLOW, 0x3f) |	\
668c2ecf20Sopenharmony_ci	 MT_PROT_CFG_RTS_THRESH)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define DEFAULT_PROT_CFG_20				\
698c2ecf20Sopenharmony_ci	(FIELD_PREP(MT_PROT_CFG_RATE, 0x2004) |		\
708c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_CTRL, 1) |		\
718c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_NAV, 1) |			\
728c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_TXOP_ALLOW, 0x17))
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define DEFAULT_PROT_CFG_40				\
758c2ecf20Sopenharmony_ci	(FIELD_PREP(MT_PROT_CFG_RATE, 0x2084) |		\
768c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_CTRL, 1) |		\
778c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_NAV, 1) |			\
788c2ecf20Sopenharmony_ci	 FIELD_PREP(MT_PROT_CFG_TXOP_ALLOW, 0x3f))
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	static const struct mt76_reg_pair vals[] = {
818c2ecf20Sopenharmony_ci		/* Copied from MediaTek reference source */
828c2ecf20Sopenharmony_ci		{ MT_PBF_SYS_CTRL,		0x00080c00 },
838c2ecf20Sopenharmony_ci		{ MT_PBF_CFG,			0x1efebcff },
848c2ecf20Sopenharmony_ci		{ MT_FCE_PSE_CTRL,		0x00000001 },
858c2ecf20Sopenharmony_ci		{ MT_MAC_SYS_CTRL,		0x00000000 },
868c2ecf20Sopenharmony_ci		{ MT_MAX_LEN_CFG,		0x003e3f00 },
878c2ecf20Sopenharmony_ci		{ MT_AMPDU_MAX_LEN_20M1S,	0xaaa99887 },
888c2ecf20Sopenharmony_ci		{ MT_AMPDU_MAX_LEN_20M2S,	0x000000aa },
898c2ecf20Sopenharmony_ci		{ MT_XIFS_TIME_CFG,		0x33a40d0a },
908c2ecf20Sopenharmony_ci		{ MT_BKOFF_SLOT_CFG,		0x00000209 },
918c2ecf20Sopenharmony_ci		{ MT_TBTT_SYNC_CFG,		0x00422010 },
928c2ecf20Sopenharmony_ci		{ MT_PWR_PIN_CFG,		0x00000000 },
938c2ecf20Sopenharmony_ci		{ 0x1238,			0x001700c8 },
948c2ecf20Sopenharmony_ci		{ MT_TX_SW_CFG0,		0x00101001 },
958c2ecf20Sopenharmony_ci		{ MT_TX_SW_CFG1,		0x00010000 },
968c2ecf20Sopenharmony_ci		{ MT_TX_SW_CFG2,		0x00000000 },
978c2ecf20Sopenharmony_ci		{ MT_TXOP_CTRL_CFG,		0x0400583f },
988c2ecf20Sopenharmony_ci		{ MT_TX_RTS_CFG,		0x00ffff20 },
998c2ecf20Sopenharmony_ci		{ MT_TX_TIMEOUT_CFG,		0x000a2290 },
1008c2ecf20Sopenharmony_ci		{ MT_TX_RETRY_CFG,		0x47f01f0f },
1018c2ecf20Sopenharmony_ci		{ MT_EXP_ACK_TIME,		0x002c00dc },
1028c2ecf20Sopenharmony_ci		{ MT_TX_PROT_CFG6,		0xe3f42004 },
1038c2ecf20Sopenharmony_ci		{ MT_TX_PROT_CFG7,		0xe3f42084 },
1048c2ecf20Sopenharmony_ci		{ MT_TX_PROT_CFG8,		0xe3f42104 },
1058c2ecf20Sopenharmony_ci		{ MT_PIFS_TX_CFG,		0x00060fff },
1068c2ecf20Sopenharmony_ci		{ MT_RX_FILTR_CFG,		0x00015f97 },
1078c2ecf20Sopenharmony_ci		{ MT_LEGACY_BASIC_RATE,		0x0000017f },
1088c2ecf20Sopenharmony_ci		{ MT_HT_BASIC_RATE,		0x00004003 },
1098c2ecf20Sopenharmony_ci		{ MT_PN_PAD_MODE,		0x00000003 },
1108c2ecf20Sopenharmony_ci		{ MT_TXOP_HLDR_ET,		0x00000002 },
1118c2ecf20Sopenharmony_ci		{ 0xa44,			0x00000000 },
1128c2ecf20Sopenharmony_ci		{ MT_HEADER_TRANS_CTRL_REG,	0x00000000 },
1138c2ecf20Sopenharmony_ci		{ MT_TSO_CTRL,			0x00000000 },
1148c2ecf20Sopenharmony_ci		{ MT_AUX_CLK_CFG,		0x00000000 },
1158c2ecf20Sopenharmony_ci		{ MT_DACCLK_EN_DLY_CFG,		0x00000000 },
1168c2ecf20Sopenharmony_ci		{ MT_TX_ALC_CFG_4,		0x00000000 },
1178c2ecf20Sopenharmony_ci		{ MT_TX_ALC_VGA3,		0x00000000 },
1188c2ecf20Sopenharmony_ci		{ MT_TX_PWR_CFG_0,		0x3a3a3a3a },
1198c2ecf20Sopenharmony_ci		{ MT_TX_PWR_CFG_1,		0x3a3a3a3a },
1208c2ecf20Sopenharmony_ci		{ MT_TX_PWR_CFG_2,		0x3a3a3a3a },
1218c2ecf20Sopenharmony_ci		{ MT_TX_PWR_CFG_3,		0x3a3a3a3a },
1228c2ecf20Sopenharmony_ci		{ MT_TX_PWR_CFG_4,		0x3a3a3a3a },
1238c2ecf20Sopenharmony_ci		{ MT_TX_PWR_CFG_7,		0x3a3a3a3a },
1248c2ecf20Sopenharmony_ci		{ MT_TX_PWR_CFG_8,		0x0000003a },
1258c2ecf20Sopenharmony_ci		{ MT_TX_PWR_CFG_9,		0x0000003a },
1268c2ecf20Sopenharmony_ci		{ MT_EFUSE_CTRL,		0x0000d000 },
1278c2ecf20Sopenharmony_ci		{ MT_PAUSE_ENABLE_CONTROL1,	0x0000000a },
1288c2ecf20Sopenharmony_ci		{ MT_FCE_WLAN_FLOW_CONTROL1,	0x60401c18 },
1298c2ecf20Sopenharmony_ci		{ MT_WPDMA_DELAY_INT_CFG,	0x94ff0000 },
1308c2ecf20Sopenharmony_ci		{ MT_TX_SW_CFG3,		0x00000004 },
1318c2ecf20Sopenharmony_ci		{ MT_HT_FBK_TO_LEGACY,		0x00001818 },
1328c2ecf20Sopenharmony_ci		{ MT_VHT_HT_FBK_CFG1,		0xedcba980 },
1338c2ecf20Sopenharmony_ci		{ MT_PROT_AUTO_TX_CFG,		0x00830083 },
1348c2ecf20Sopenharmony_ci		{ MT_HT_CTRL_CFG,		0x000001ff },
1358c2ecf20Sopenharmony_ci		{ MT_TX_LINK_CFG,		0x00001020 },
1368c2ecf20Sopenharmony_ci	};
1378c2ecf20Sopenharmony_ci	struct mt76_reg_pair prot_vals[] = {
1388c2ecf20Sopenharmony_ci		{ MT_CCK_PROT_CFG,		DEFAULT_PROT_CFG_CCK },
1398c2ecf20Sopenharmony_ci		{ MT_OFDM_PROT_CFG,		DEFAULT_PROT_CFG_OFDM },
1408c2ecf20Sopenharmony_ci		{ MT_MM20_PROT_CFG,		DEFAULT_PROT_CFG_20 },
1418c2ecf20Sopenharmony_ci		{ MT_MM40_PROT_CFG,		DEFAULT_PROT_CFG_40 },
1428c2ecf20Sopenharmony_ci		{ MT_GF20_PROT_CFG,		DEFAULT_PROT_CFG_20 },
1438c2ecf20Sopenharmony_ci		{ MT_GF40_PROT_CFG,		DEFAULT_PROT_CFG_40 },
1448c2ecf20Sopenharmony_ci	};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	mt76_wr_rp(dev, 0, vals, ARRAY_SIZE(vals));
1478c2ecf20Sopenharmony_ci	mt76_wr_rp(dev, 0, prot_vals, ARRAY_SIZE(prot_vals));
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76_write_mac_initvals);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_civoid mt76x2_init_txpower(struct mt76x02_dev *dev,
1528c2ecf20Sopenharmony_ci			 struct ieee80211_supported_band *sband)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct ieee80211_channel *chan;
1558c2ecf20Sopenharmony_ci	struct mt76x2_tx_power_info txp;
1568c2ecf20Sopenharmony_ci	struct mt76_rate_power t = {};
1578c2ecf20Sopenharmony_ci	int i;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	for (i = 0; i < sband->n_channels; i++) {
1608c2ecf20Sopenharmony_ci		chan = &sband->channels[i];
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		mt76x2_get_power_info(dev, &txp, chan);
1638c2ecf20Sopenharmony_ci		mt76x2_get_rate_power(dev, &t, chan);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		chan->orig_mpwr = mt76x02_get_max_rate_power(&t) +
1668c2ecf20Sopenharmony_ci				  txp.target_power;
1678c2ecf20Sopenharmony_ci		chan->orig_mpwr = DIV_ROUND_UP(chan->orig_mpwr, 2);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		/* convert to combined output power on 2x2 devices */
1708c2ecf20Sopenharmony_ci		chan->orig_mpwr += 3;
1718c2ecf20Sopenharmony_ci		chan->max_power = min_t(int, chan->max_reg_power,
1728c2ecf20Sopenharmony_ci					chan->orig_mpwr);
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt76x2_init_txpower);
176