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 "mt76x2.h"
19 #include "mt76x2_eeprom.h"
20 
21 #define CCK_RATE(_idx, _rate) {					\
22 	.bitrate = _rate,					\
23 	.flags = IEEE80211_RATE_SHORT_PREAMBLE,			\
24 	.hw_value = (MT_PHY_TYPE_CCK << 8) | _idx,		\
25 	.hw_value_short = (MT_PHY_TYPE_CCK << 8) | (8 + _idx),	\
26 }
27 
28 #define OFDM_RATE(_idx, _rate) {				\
29 	.bitrate = _rate,					\
30 	.hw_value = (MT_PHY_TYPE_OFDM << 8) | _idx,		\
31 	.hw_value_short = (MT_PHY_TYPE_OFDM << 8) | _idx,	\
32 }
33 
34 struct ieee80211_rate mt76x2_rates[] = {
35 	CCK_RATE(0, 10),
36 	CCK_RATE(1, 20),
37 	CCK_RATE(2, 55),
38 	CCK_RATE(3, 110),
39 	OFDM_RATE(0, 60),
40 	OFDM_RATE(1, 90),
41 	OFDM_RATE(2, 120),
42 	OFDM_RATE(3, 180),
43 	OFDM_RATE(4, 240),
44 	OFDM_RATE(5, 360),
45 	OFDM_RATE(6, 480),
46 	OFDM_RATE(7, 540),
47 };
48 EXPORT_SYMBOL_GPL(mt76x2_rates);
49 
50 struct mt76x2_reg_pair {
51 	u32 reg;
52 	u32 value;
53 };
54 
55 static void
mt76x2_set_wlan_state(struct mt76x2_dev * dev,bool enable)56 mt76x2_set_wlan_state(struct mt76x2_dev *dev, bool enable)
57 {
58 	u32 val = mt76_rr(dev, MT_WLAN_FUN_CTRL);
59 
60 	if (enable)
61 		val |= (MT_WLAN_FUN_CTRL_WLAN_EN |
62 			MT_WLAN_FUN_CTRL_WLAN_CLK_EN);
63 	else
64 		val &= ~(MT_WLAN_FUN_CTRL_WLAN_EN |
65 			 MT_WLAN_FUN_CTRL_WLAN_CLK_EN);
66 
67 	mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
68 	udelay(20);
69 }
70 
mt76x2_reset_wlan(struct mt76x2_dev * dev,bool enable)71 void mt76x2_reset_wlan(struct mt76x2_dev *dev, bool enable)
72 {
73 	u32 val;
74 
75 	val = mt76_rr(dev, MT_WLAN_FUN_CTRL);
76 
77 	val &= ~MT_WLAN_FUN_CTRL_FRC_WL_ANT_SEL;
78 
79 	if (val & MT_WLAN_FUN_CTRL_WLAN_EN) {
80 		val |= MT_WLAN_FUN_CTRL_WLAN_RESET_RF;
81 		mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
82 		udelay(20);
83 
84 		val &= ~MT_WLAN_FUN_CTRL_WLAN_RESET_RF;
85 	}
86 
87 	mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
88 	udelay(20);
89 
90 	mt76x2_set_wlan_state(dev, enable);
91 }
92 EXPORT_SYMBOL_GPL(mt76x2_reset_wlan);
93 
94 static void
mt76x2_write_reg_pairs(struct mt76x2_dev * dev,const struct mt76x2_reg_pair * data,int len)95 mt76x2_write_reg_pairs(struct mt76x2_dev *dev,
96 		       const struct mt76x2_reg_pair *data, int len)
97 {
98 	while (len > 0) {
99 		mt76_wr(dev, data->reg, data->value);
100 		len--;
101 		data++;
102 	}
103 }
104 
mt76_write_mac_initvals(struct mt76x2_dev * dev)105 void mt76_write_mac_initvals(struct mt76x2_dev *dev)
106 {
107 #define DEFAULT_PROT_CFG_CCK				\
108 	(FIELD_PREP(MT_PROT_CFG_RATE, 0x3) |		\
109 	 FIELD_PREP(MT_PROT_CFG_NAV, 1) |		\
110 	 FIELD_PREP(MT_PROT_CFG_TXOP_ALLOW, 0x3f) |	\
111 	 MT_PROT_CFG_RTS_THRESH)
112 
113 #define DEFAULT_PROT_CFG_OFDM				\
114 	(FIELD_PREP(MT_PROT_CFG_RATE, 0x2004) |		\
115 	 FIELD_PREP(MT_PROT_CFG_NAV, 1) |			\
116 	 FIELD_PREP(MT_PROT_CFG_TXOP_ALLOW, 0x3f) |	\
117 	 MT_PROT_CFG_RTS_THRESH)
118 
119 #define DEFAULT_PROT_CFG_20				\
120 	(FIELD_PREP(MT_PROT_CFG_RATE, 0x2004) |		\
121 	 FIELD_PREP(MT_PROT_CFG_CTRL, 1) |		\
122 	 FIELD_PREP(MT_PROT_CFG_NAV, 1) |			\
123 	 FIELD_PREP(MT_PROT_CFG_TXOP_ALLOW, 0x17))
124 
125 #define DEFAULT_PROT_CFG_40				\
126 	(FIELD_PREP(MT_PROT_CFG_RATE, 0x2084) |		\
127 	 FIELD_PREP(MT_PROT_CFG_CTRL, 1) |		\
128 	 FIELD_PREP(MT_PROT_CFG_NAV, 1) |			\
129 	 FIELD_PREP(MT_PROT_CFG_TXOP_ALLOW, 0x3f))
130 
131 	static const struct mt76x2_reg_pair vals[] = {
132 		/* Copied from MediaTek reference source */
133 		{ MT_PBF_SYS_CTRL,		0x00080c00 },
134 		{ MT_PBF_CFG,			0x1efebcff },
135 		{ MT_FCE_PSE_CTRL,		0x00000001 },
136 		{ MT_MAC_SYS_CTRL,		0x0000000c },
137 		{ MT_MAX_LEN_CFG,		0x003e3f00 },
138 		{ MT_AMPDU_MAX_LEN_20M1S,	0xaaa99887 },
139 		{ MT_AMPDU_MAX_LEN_20M2S,	0x000000aa },
140 		{ MT_XIFS_TIME_CFG,		0x33a40d0a },
141 		{ MT_BKOFF_SLOT_CFG,		0x00000209 },
142 		{ MT_TBTT_SYNC_CFG,		0x00422010 },
143 		{ MT_PWR_PIN_CFG,		0x00000000 },
144 		{ 0x1238,			0x001700c8 },
145 		{ MT_TX_SW_CFG0,		0x00101001 },
146 		{ MT_TX_SW_CFG1,		0x00010000 },
147 		{ MT_TX_SW_CFG2,		0x00000000 },
148 		{ MT_TXOP_CTRL_CFG,		0x0400583f },
149 		{ MT_TX_RTS_CFG,		0x00100020 },
150 		{ MT_TX_TIMEOUT_CFG,		0x000a2290 },
151 		{ MT_TX_RETRY_CFG,		0x47f01f0f },
152 		{ MT_EXP_ACK_TIME,		0x002c00dc },
153 		{ MT_TX_PROT_CFG6,		0xe3f42004 },
154 		{ MT_TX_PROT_CFG7,		0xe3f42084 },
155 		{ MT_TX_PROT_CFG8,		0xe3f42104 },
156 		{ MT_PIFS_TX_CFG,		0x00060fff },
157 		{ MT_RX_FILTR_CFG,		0x00015f97 },
158 		{ MT_LEGACY_BASIC_RATE,		0x0000017f },
159 		{ MT_HT_BASIC_RATE,		0x00004003 },
160 		{ MT_PN_PAD_MODE,		0x00000003 },
161 		{ MT_TXOP_HLDR_ET,		0x00000002 },
162 		{ 0xa44,			0x00000000 },
163 		{ MT_HEADER_TRANS_CTRL_REG,	0x00000000 },
164 		{ MT_TSO_CTRL,			0x00000000 },
165 		{ MT_AUX_CLK_CFG,		0x00000000 },
166 		{ MT_DACCLK_EN_DLY_CFG,		0x00000000 },
167 		{ MT_TX_ALC_CFG_4,		0x00000000 },
168 		{ MT_TX_ALC_VGA3,		0x00000000 },
169 		{ MT_TX_PWR_CFG_0,		0x3a3a3a3a },
170 		{ MT_TX_PWR_CFG_1,		0x3a3a3a3a },
171 		{ MT_TX_PWR_CFG_2,		0x3a3a3a3a },
172 		{ MT_TX_PWR_CFG_3,		0x3a3a3a3a },
173 		{ MT_TX_PWR_CFG_4,		0x3a3a3a3a },
174 		{ MT_TX_PWR_CFG_7,		0x3a3a3a3a },
175 		{ MT_TX_PWR_CFG_8,		0x0000003a },
176 		{ MT_TX_PWR_CFG_9,		0x0000003a },
177 		{ MT_EFUSE_CTRL,		0x0000d000 },
178 		{ MT_PAUSE_ENABLE_CONTROL1,	0x0000000a },
179 		{ MT_FCE_WLAN_FLOW_CONTROL1,	0x60401c18 },
180 		{ MT_WPDMA_DELAY_INT_CFG,	0x94ff0000 },
181 		{ MT_TX_SW_CFG3,		0x00000004 },
182 		{ MT_HT_FBK_TO_LEGACY,		0x00001818 },
183 		{ MT_VHT_HT_FBK_CFG1,		0xedcba980 },
184 		{ MT_PROT_AUTO_TX_CFG,		0x00830083 },
185 		{ MT_HT_CTRL_CFG,		0x000001ff },
186 	};
187 	struct mt76x2_reg_pair prot_vals[] = {
188 		{ MT_CCK_PROT_CFG,		DEFAULT_PROT_CFG_CCK },
189 		{ MT_OFDM_PROT_CFG,		DEFAULT_PROT_CFG_OFDM },
190 		{ MT_MM20_PROT_CFG,		DEFAULT_PROT_CFG_20 },
191 		{ MT_MM40_PROT_CFG,		DEFAULT_PROT_CFG_40 },
192 		{ MT_GF20_PROT_CFG,		DEFAULT_PROT_CFG_20 },
193 		{ MT_GF40_PROT_CFG,		DEFAULT_PROT_CFG_40 },
194 	};
195 
196 	mt76x2_write_reg_pairs(dev, vals, ARRAY_SIZE(vals));
197 	mt76x2_write_reg_pairs(dev, prot_vals, ARRAY_SIZE(prot_vals));
198 }
199 EXPORT_SYMBOL_GPL(mt76_write_mac_initvals);
200 
mt76x2_init_device(struct mt76x2_dev * dev)201 void mt76x2_init_device(struct mt76x2_dev *dev)
202 {
203 	struct ieee80211_hw *hw = mt76_hw(dev);
204 
205 	hw->queues = 4;
206 	hw->max_rates = 1;
207 	hw->max_report_rates = 7;
208 	hw->max_rate_tries = 1;
209 	hw->extra_tx_headroom = 2;
210 
211 	hw->sta_data_size = sizeof(struct mt76x2_sta);
212 	hw->vif_data_size = sizeof(struct mt76x2_vif);
213 
214 	ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
215 	ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
216 
217 	dev->mt76.sband_2g.sband.ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
218 	dev->mt76.sband_5g.sband.ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
219 
220 	dev->chainmask = 0x202;
221 	dev->global_wcid.idx = 255;
222 	dev->global_wcid.hw_key_idx = -1;
223 	dev->slottime = 9;
224 
225 	/* init antenna configuration */
226 	dev->mt76.antenna_mask = 3;
227 }
228 EXPORT_SYMBOL_GPL(mt76x2_init_device);
229 
mt76x2_init_txpower(struct mt76x2_dev * dev,struct ieee80211_supported_band * sband)230 void mt76x2_init_txpower(struct mt76x2_dev *dev,
231 			 struct ieee80211_supported_band *sband)
232 {
233 	struct ieee80211_channel *chan;
234 	struct mt76x2_tx_power_info txp;
235 	struct mt76_rate_power t = {};
236 	int target_power;
237 	int i;
238 
239 	for (i = 0; i < sband->n_channels; i++) {
240 		chan = &sband->channels[i];
241 
242 		mt76x2_get_power_info(dev, &txp, chan);
243 
244 		target_power = max_t(int, (txp.chain[0].target_power +
245 					   txp.chain[0].delta),
246 					  (txp.chain[1].target_power +
247 					   txp.chain[1].delta));
248 
249 		mt76x2_get_rate_power(dev, &t, chan);
250 
251 		chan->max_power = mt76x2_get_max_rate_power(&t) +
252 				  target_power;
253 		chan->max_power /= 2;
254 
255 		/* convert to combined output power on 2x2 devices */
256 		chan->max_power += 3;
257 	}
258 }
259 EXPORT_SYMBOL_GPL(mt76x2_init_txpower);
260