1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * (c) Copyright 2002-2010, Ralink Technology, Inc.
4  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5  * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
6  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/etherdevice.h>
11 
12 #include "mt76x0.h"
13 #include "mcu.h"
14 #include "eeprom.h"
15 #include "phy.h"
16 #include "initvals.h"
17 #include "initvals_phy.h"
18 #include "../mt76x02_phy.h"
19 
20 static int
21 mt76x0_rf_csr_wr(struct mt76x02_dev *dev, u32 offset, u8 value)
22 {
23 	int ret = 0;
24 	u8 bank, reg;
25 
26 	if (test_bit(MT76_REMOVED, &dev->mphy.state))
27 		return -ENODEV;
28 
29 	bank = MT_RF_BANK(offset);
30 	reg = MT_RF_REG(offset);
31 
32 	if (WARN_ON_ONCE(reg > 127) || WARN_ON_ONCE(bank > 8))
33 		return -EINVAL;
34 
35 	mutex_lock(&dev->phy_mutex);
36 
37 	if (!mt76_poll(dev, MT_RF_CSR_CFG, MT_RF_CSR_CFG_KICK, 0, 100)) {
38 		ret = -ETIMEDOUT;
39 		goto out;
40 	}
41 
42 	mt76_wr(dev, MT_RF_CSR_CFG,
43 		FIELD_PREP(MT_RF_CSR_CFG_DATA, value) |
44 		FIELD_PREP(MT_RF_CSR_CFG_REG_BANK, bank) |
45 		FIELD_PREP(MT_RF_CSR_CFG_REG_ID, reg) |
46 		MT_RF_CSR_CFG_WR |
47 		MT_RF_CSR_CFG_KICK);
48 
49 out:
50 	mutex_unlock(&dev->phy_mutex);
51 
52 	if (ret < 0)
53 		dev_err(dev->mt76.dev, "Error: RF write %d:%d failed:%d!!\n",
54 			bank, reg, ret);
55 
56 	return ret;
57 }
58 
59 static int mt76x0_rf_csr_rr(struct mt76x02_dev *dev, u32 offset)
60 {
61 	int ret = -ETIMEDOUT;
62 	u32 val;
63 	u8 bank, reg;
64 
65 	if (test_bit(MT76_REMOVED, &dev->mphy.state))
66 		return -ENODEV;
67 
68 	bank = MT_RF_BANK(offset);
69 	reg = MT_RF_REG(offset);
70 
71 	if (WARN_ON_ONCE(reg > 127) || WARN_ON_ONCE(bank > 8))
72 		return -EINVAL;
73 
74 	mutex_lock(&dev->phy_mutex);
75 
76 	if (!mt76_poll(dev, MT_RF_CSR_CFG, MT_RF_CSR_CFG_KICK, 0, 100))
77 		goto out;
78 
79 	mt76_wr(dev, MT_RF_CSR_CFG,
80 		FIELD_PREP(MT_RF_CSR_CFG_REG_BANK, bank) |
81 		FIELD_PREP(MT_RF_CSR_CFG_REG_ID, reg) |
82 		MT_RF_CSR_CFG_KICK);
83 
84 	if (!mt76_poll(dev, MT_RF_CSR_CFG, MT_RF_CSR_CFG_KICK, 0, 100))
85 		goto out;
86 
87 	val = mt76_rr(dev, MT_RF_CSR_CFG);
88 	if (FIELD_GET(MT_RF_CSR_CFG_REG_ID, val) == reg &&
89 	    FIELD_GET(MT_RF_CSR_CFG_REG_BANK, val) == bank)
90 		ret = FIELD_GET(MT_RF_CSR_CFG_DATA, val);
91 
92 out:
93 	mutex_unlock(&dev->phy_mutex);
94 
95 	if (ret < 0)
96 		dev_err(dev->mt76.dev, "Error: RF read %d:%d failed:%d!!\n",
97 			bank, reg, ret);
98 
99 	return ret;
100 }
101 
102 static int
103 mt76x0_rf_wr(struct mt76x02_dev *dev, u32 offset, u8 val)
104 {
105 	if (mt76_is_usb(&dev->mt76)) {
106 		struct mt76_reg_pair pair = {
107 			.reg = offset,
108 			.value = val,
109 		};
110 
111 		WARN_ON_ONCE(!test_bit(MT76_STATE_MCU_RUNNING,
112 				       &dev->mphy.state));
113 		return mt76_wr_rp(dev, MT_MCU_MEMMAP_RF, &pair, 1);
114 	} else {
115 		return mt76x0_rf_csr_wr(dev, offset, val);
116 	}
117 }
118 
119 static int mt76x0_rf_rr(struct mt76x02_dev *dev, u32 offset)
120 {
121 	int ret;
122 	u32 val;
123 
124 	if (mt76_is_usb(&dev->mt76)) {
125 		struct mt76_reg_pair pair = {
126 			.reg = offset,
127 		};
128 
129 		WARN_ON_ONCE(!test_bit(MT76_STATE_MCU_RUNNING,
130 				       &dev->mphy.state));
131 		ret = mt76_rd_rp(dev, MT_MCU_MEMMAP_RF, &pair, 1);
132 		val = pair.value;
133 	} else {
134 		ret = val = mt76x0_rf_csr_rr(dev, offset);
135 	}
136 
137 	return (ret < 0) ? ret : val;
138 }
139 
140 static int
141 mt76x0_rf_rmw(struct mt76x02_dev *dev, u32 offset, u8 mask, u8 val)
142 {
143 	int ret;
144 
145 	ret = mt76x0_rf_rr(dev, offset);
146 	if (ret < 0)
147 		return ret;
148 
149 	val |= ret & ~mask;
150 
151 	ret = mt76x0_rf_wr(dev, offset, val);
152 	return ret ? ret : val;
153 }
154 
155 static int
156 mt76x0_rf_set(struct mt76x02_dev *dev, u32 offset, u8 val)
157 {
158 	return mt76x0_rf_rmw(dev, offset, 0, val);
159 }
160 
161 static int
162 mt76x0_rf_clear(struct mt76x02_dev *dev, u32 offset, u8 mask)
163 {
164 	return mt76x0_rf_rmw(dev, offset, mask, 0);
165 }
166 
167 static void
168 mt76x0_phy_rf_csr_wr_rp(struct mt76x02_dev *dev,
169 			const struct mt76_reg_pair *data,
170 			int n)
171 {
172 	while (n-- > 0) {
173 		mt76x0_rf_csr_wr(dev, data->reg, data->value);
174 		data++;
175 	}
176 }
177 
178 #define RF_RANDOM_WRITE(dev, tab) do {					\
179 	if (mt76_is_mmio(&dev->mt76))					\
180 		mt76x0_phy_rf_csr_wr_rp(dev, tab, ARRAY_SIZE(tab));	\
181 	else								\
182 		mt76_wr_rp(dev, MT_MCU_MEMMAP_RF, tab, ARRAY_SIZE(tab));\
183 } while (0)
184 
185 int mt76x0_phy_wait_bbp_ready(struct mt76x02_dev *dev)
186 {
187 	int i = 20;
188 	u32 val;
189 
190 	do {
191 		val = mt76_rr(dev, MT_BBP(CORE, 0));
192 		if (val && ~val)
193 			break;
194 	} while (--i);
195 
196 	if (!i) {
197 		dev_err(dev->mt76.dev, "Error: BBP is not ready\n");
198 		return -EIO;
199 	}
200 
201 	dev_dbg(dev->mt76.dev, "BBP version %08x\n", val);
202 	return 0;
203 }
204 
205 static void
206 mt76x0_phy_set_band(struct mt76x02_dev *dev, enum nl80211_band band)
207 {
208 	switch (band) {
209 	case NL80211_BAND_2GHZ:
210 		RF_RANDOM_WRITE(dev, mt76x0_rf_2g_channel_0_tab);
211 
212 		mt76x0_rf_wr(dev, MT_RF(5, 0), 0x45);
213 		mt76x0_rf_wr(dev, MT_RF(6, 0), 0x44);
214 
215 		mt76_wr(dev, MT_TX_ALC_VGA3, 0x00050007);
216 		mt76_wr(dev, MT_TX0_RF_GAIN_CORR, 0x003E0002);
217 		break;
218 	case NL80211_BAND_5GHZ:
219 		RF_RANDOM_WRITE(dev, mt76x0_rf_5g_channel_0_tab);
220 
221 		mt76x0_rf_wr(dev, MT_RF(5, 0), 0x44);
222 		mt76x0_rf_wr(dev, MT_RF(6, 0), 0x45);
223 
224 		mt76_wr(dev, MT_TX_ALC_VGA3, 0x00000005);
225 		mt76_wr(dev, MT_TX0_RF_GAIN_CORR, 0x01010102);
226 		break;
227 	default:
228 		break;
229 	}
230 }
231 
232 static void
233 mt76x0_phy_set_chan_rf_params(struct mt76x02_dev *dev, u8 channel,
234 			      u16 rf_bw_band)
235 {
236 	const struct mt76x0_freq_item *freq_item;
237 	u16 rf_band = rf_bw_band & 0xff00;
238 	u16 rf_bw = rf_bw_band & 0x00ff;
239 	enum nl80211_band band;
240 	bool b_sdm = false;
241 	u32 mac_reg;
242 	int i;
243 
244 	for (i = 0; i < ARRAY_SIZE(mt76x0_sdm_channel); i++) {
245 		if (channel == mt76x0_sdm_channel[i]) {
246 			b_sdm = true;
247 			break;
248 		}
249 	}
250 
251 	for (i = 0; i < ARRAY_SIZE(mt76x0_frequency_plan); i++) {
252 		if (channel == mt76x0_frequency_plan[i].channel) {
253 			rf_band = mt76x0_frequency_plan[i].band;
254 
255 			if (b_sdm)
256 				freq_item = &mt76x0_sdm_frequency_plan[i];
257 			else
258 				freq_item = &mt76x0_frequency_plan[i];
259 
260 			mt76x0_rf_wr(dev, MT_RF(0, 37), freq_item->pllR37);
261 			mt76x0_rf_wr(dev, MT_RF(0, 36), freq_item->pllR36);
262 			mt76x0_rf_wr(dev, MT_RF(0, 35), freq_item->pllR35);
263 			mt76x0_rf_wr(dev, MT_RF(0, 34), freq_item->pllR34);
264 			mt76x0_rf_wr(dev, MT_RF(0, 33), freq_item->pllR33);
265 
266 			mt76x0_rf_rmw(dev, MT_RF(0, 32), 0xe0,
267 				      freq_item->pllR32_b7b5);
268 
269 			/* R32<4:0> pll_den: (Denomina - 8) */
270 			mt76x0_rf_rmw(dev, MT_RF(0, 32), MT_RF_PLL_DEN_MASK,
271 				      freq_item->pllR32_b4b0);
272 
273 			/* R31<7:5> */
274 			mt76x0_rf_rmw(dev, MT_RF(0, 31), 0xe0,
275 				      freq_item->pllR31_b7b5);
276 
277 			/* R31<4:0> pll_k(Nominator) */
278 			mt76x0_rf_rmw(dev, MT_RF(0, 31), MT_RF_PLL_K_MASK,
279 				      freq_item->pllR31_b4b0);
280 
281 			/* R30<7> sdm_reset_n */
282 			if (b_sdm) {
283 				mt76x0_rf_clear(dev, MT_RF(0, 30),
284 						MT_RF_SDM_RESET_MASK);
285 				mt76x0_rf_set(dev, MT_RF(0, 30),
286 					      MT_RF_SDM_RESET_MASK);
287 			} else {
288 				mt76x0_rf_rmw(dev, MT_RF(0, 30),
289 					      MT_RF_SDM_RESET_MASK,
290 					      freq_item->pllR30_b7);
291 			}
292 
293 			/* R30<6:2> sdmmash_prbs,sin */
294 			mt76x0_rf_rmw(dev, MT_RF(0, 30),
295 				      MT_RF_SDM_MASH_PRBS_MASK,
296 				      freq_item->pllR30_b6b2);
297 
298 			/* R30<1> sdm_bp */
299 			mt76x0_rf_rmw(dev, MT_RF(0, 30), MT_RF_SDM_BP_MASK,
300 				      freq_item->pllR30_b1 << 1);
301 
302 			/* R30<0> R29<7:0> (hex) pll_n */
303 			mt76x0_rf_wr(dev, MT_RF(0, 29),
304 				     freq_item->pll_n & 0xff);
305 
306 			mt76x0_rf_rmw(dev, MT_RF(0, 30), 0x1,
307 				      (freq_item->pll_n >> 8) & 0x1);
308 
309 			/* R28<7:6> isi_iso */
310 			mt76x0_rf_rmw(dev, MT_RF(0, 28), MT_RF_ISI_ISO_MASK,
311 				      freq_item->pllR28_b7b6);
312 
313 			/* R28<5:4> pfd_dly */
314 			mt76x0_rf_rmw(dev, MT_RF(0, 28), MT_RF_PFD_DLY_MASK,
315 				      freq_item->pllR28_b5b4);
316 
317 			/* R28<3:2> clksel option */
318 			mt76x0_rf_rmw(dev, MT_RF(0, 28), MT_RF_CLK_SEL_MASK,
319 				      freq_item->pllR28_b3b2);
320 
321 			/* R28<1:0> R27<7:0> R26<7:0> (hex) sdm_k */
322 			mt76x0_rf_wr(dev, MT_RF(0, 26),
323 				     freq_item->pll_sdm_k & 0xff);
324 			mt76x0_rf_wr(dev, MT_RF(0, 27),
325 				     (freq_item->pll_sdm_k >> 8) & 0xff);
326 
327 			mt76x0_rf_rmw(dev, MT_RF(0, 28), 0x3,
328 				      (freq_item->pll_sdm_k >> 16) & 0x3);
329 
330 			/* R24<1:0> xo_div */
331 			mt76x0_rf_rmw(dev, MT_RF(0, 24), MT_RF_XO_DIV_MASK,
332 				      freq_item->pllR24_b1b0);
333 
334 			break;
335 		}
336 	}
337 
338 	for (i = 0; i < ARRAY_SIZE(mt76x0_rf_bw_switch_tab); i++) {
339 		if (rf_bw == mt76x0_rf_bw_switch_tab[i].bw_band) {
340 			mt76x0_rf_wr(dev,
341 				     mt76x0_rf_bw_switch_tab[i].rf_bank_reg,
342 				     mt76x0_rf_bw_switch_tab[i].value);
343 		} else if ((rf_bw == (mt76x0_rf_bw_switch_tab[i].bw_band & 0xFF)) &&
344 			   (rf_band & mt76x0_rf_bw_switch_tab[i].bw_band)) {
345 			mt76x0_rf_wr(dev,
346 				     mt76x0_rf_bw_switch_tab[i].rf_bank_reg,
347 				     mt76x0_rf_bw_switch_tab[i].value);
348 		}
349 	}
350 
351 	for (i = 0; i < ARRAY_SIZE(mt76x0_rf_band_switch_tab); i++) {
352 		if (mt76x0_rf_band_switch_tab[i].bw_band & rf_band) {
353 			mt76x0_rf_wr(dev,
354 				     mt76x0_rf_band_switch_tab[i].rf_bank_reg,
355 				     mt76x0_rf_band_switch_tab[i].value);
356 		}
357 	}
358 
359 	mt76_clear(dev, MT_RF_MISC, 0xc);
360 
361 	band = (rf_band & RF_G_BAND) ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
362 	if (mt76x02_ext_pa_enabled(dev, band)) {
363 		/* MT_RF_MISC (offset: 0x0518)
364 		 * [2]1'b1: enable external A band PA
365 		 *    1'b0: disable external A band PA
366 		 * [3]1'b1: enable external G band PA
367 		 *    1'b0: disable external G band PA
368 		 */
369 		if (rf_band & RF_A_BAND)
370 			mt76_set(dev, MT_RF_MISC, BIT(2));
371 		else
372 			mt76_set(dev, MT_RF_MISC, BIT(3));
373 
374 		/* External PA */
375 		for (i = 0; i < ARRAY_SIZE(mt76x0_rf_ext_pa_tab); i++)
376 			if (mt76x0_rf_ext_pa_tab[i].bw_band & rf_band)
377 				mt76x0_rf_wr(dev,
378 					mt76x0_rf_ext_pa_tab[i].rf_bank_reg,
379 					mt76x0_rf_ext_pa_tab[i].value);
380 	}
381 
382 	if (rf_band & RF_G_BAND) {
383 		mt76_wr(dev, MT_TX0_RF_GAIN_ATTEN, 0x63707400);
384 		/* Set Atten mode = 2 For G band, Disable Tx Inc dcoc. */
385 		mac_reg = mt76_rr(dev, MT_TX_ALC_CFG_1);
386 		mac_reg &= 0x896400FF;
387 		mt76_wr(dev, MT_TX_ALC_CFG_1, mac_reg);
388 	} else {
389 		mt76_wr(dev, MT_TX0_RF_GAIN_ATTEN, 0x686A7800);
390 		/* Set Atten mode = 0
391 		 * For Ext A band, Disable Tx Inc dcoc Cal.
392 		 */
393 		mac_reg = mt76_rr(dev, MT_TX_ALC_CFG_1);
394 		mac_reg &= 0x890400FF;
395 		mt76_wr(dev, MT_TX_ALC_CFG_1, mac_reg);
396 	}
397 }
398 
399 static void
400 mt76x0_phy_set_chan_bbp_params(struct mt76x02_dev *dev, u16 rf_bw_band)
401 {
402 	int i;
403 
404 	for (i = 0; i < ARRAY_SIZE(mt76x0_bbp_switch_tab); i++) {
405 		const struct mt76x0_bbp_switch_item *item = &mt76x0_bbp_switch_tab[i];
406 		const struct mt76_reg_pair *pair = &item->reg_pair;
407 
408 		if ((rf_bw_band & item->bw_band) != rf_bw_band)
409 			continue;
410 
411 		if (pair->reg == MT_BBP(AGC, 8)) {
412 			u32 val = pair->value;
413 			u8 gain;
414 
415 			gain = FIELD_GET(MT_BBP_AGC_GAIN, val);
416 			gain -= dev->cal.rx.lna_gain * 2;
417 			val &= ~MT_BBP_AGC_GAIN;
418 			val |= FIELD_PREP(MT_BBP_AGC_GAIN, gain);
419 			mt76_wr(dev, pair->reg, val);
420 		} else {
421 			mt76_wr(dev, pair->reg, pair->value);
422 		}
423 	}
424 }
425 
426 static void mt76x0_phy_ant_select(struct mt76x02_dev *dev)
427 {
428 	u16 ee_ant = mt76x02_eeprom_get(dev, MT_EE_ANTENNA);
429 	u16 ee_cfg1 = mt76x02_eeprom_get(dev, MT_EE_CFG1_INIT);
430 	u16 nic_conf2 = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_2);
431 	u32 wlan, coex3;
432 	bool ant_div;
433 
434 	wlan = mt76_rr(dev, MT_WLAN_FUN_CTRL);
435 	coex3 = mt76_rr(dev, MT_COEXCFG3);
436 
437 	ee_ant &= ~(BIT(14) | BIT(12));
438 	wlan  &= ~(BIT(6) | BIT(5));
439 	coex3 &= ~GENMASK(5, 2);
440 
441 	if (ee_ant & MT_EE_ANTENNA_DUAL) {
442 		/* dual antenna mode */
443 		ant_div = !(nic_conf2 & MT_EE_NIC_CONF_2_ANT_OPT) &&
444 			  (nic_conf2 & MT_EE_NIC_CONF_2_ANT_DIV);
445 		if (ant_div)
446 			ee_ant |= BIT(12);
447 		else
448 			coex3 |= BIT(4);
449 		coex3 |= BIT(3);
450 		if (dev->mphy.cap.has_2ghz)
451 			wlan |= BIT(6);
452 	} else {
453 		/* sigle antenna mode */
454 		if (dev->mphy.cap.has_5ghz) {
455 			coex3 |= BIT(3) | BIT(4);
456 		} else {
457 			wlan |= BIT(6);
458 			coex3 |= BIT(1);
459 		}
460 	}
461 
462 	if (is_mt7630(dev))
463 		ee_ant |= BIT(14) | BIT(11);
464 
465 	mt76_wr(dev, MT_WLAN_FUN_CTRL, wlan);
466 	mt76_rmw(dev, MT_CMB_CTRL, GENMASK(15, 0), ee_ant);
467 	mt76_rmw(dev, MT_CSR_EE_CFG1, GENMASK(15, 0), ee_cfg1);
468 	mt76_clear(dev, MT_COEXCFG0, BIT(2));
469 	mt76_wr(dev, MT_COEXCFG3, coex3);
470 }
471 
472 static void
473 mt76x0_phy_bbp_set_bw(struct mt76x02_dev *dev, enum nl80211_chan_width width)
474 {
475 	enum { BW_20 = 0, BW_40 = 1, BW_80 = 2, BW_10 = 4};
476 	int bw;
477 
478 	switch (width) {
479 	default:
480 	case NL80211_CHAN_WIDTH_20_NOHT:
481 	case NL80211_CHAN_WIDTH_20:
482 		bw = BW_20;
483 		break;
484 	case NL80211_CHAN_WIDTH_40:
485 		bw = BW_40;
486 		break;
487 	case NL80211_CHAN_WIDTH_80:
488 		bw = BW_80;
489 		break;
490 	case NL80211_CHAN_WIDTH_10:
491 		bw = BW_10;
492 		break;
493 	case NL80211_CHAN_WIDTH_80P80:
494 	case NL80211_CHAN_WIDTH_160:
495 	case NL80211_CHAN_WIDTH_5:
496 		/* TODO error */
497 		return;
498 	}
499 
500 	mt76x02_mcu_function_select(dev, BW_SETTING, bw);
501 }
502 
503 static void mt76x0_phy_tssi_dc_calibrate(struct mt76x02_dev *dev)
504 {
505 	struct ieee80211_channel *chan = dev->mphy.chandef.chan;
506 	u32 val;
507 
508 	if (chan->band == NL80211_BAND_5GHZ)
509 		mt76x0_rf_clear(dev, MT_RF(0, 67), 0xf);
510 
511 	/* bypass ADDA control */
512 	mt76_wr(dev, MT_RF_SETTING_0, 0x60002237);
513 	mt76_wr(dev, MT_RF_BYPASS_0, 0xffffffff);
514 
515 	/* bbp sw reset */
516 	mt76_set(dev, MT_BBP(CORE, 4), BIT(0));
517 	usleep_range(500, 1000);
518 	mt76_clear(dev, MT_BBP(CORE, 4), BIT(0));
519 
520 	val = (chan->band == NL80211_BAND_5GHZ) ? 0x80055 : 0x80050;
521 	mt76_wr(dev, MT_BBP(CORE, 34), val);
522 
523 	/* enable TX with DAC0 input */
524 	mt76_wr(dev, MT_BBP(TXBE, 6), BIT(31));
525 
526 	mt76_poll_msec(dev, MT_BBP(CORE, 34), BIT(4), 0, 200);
527 	dev->cal.tssi_dc = mt76_rr(dev, MT_BBP(CORE, 35)) & 0xff;
528 
529 	/* stop bypass ADDA */
530 	mt76_wr(dev, MT_RF_BYPASS_0, 0);
531 	/* stop TX */
532 	mt76_wr(dev, MT_BBP(TXBE, 6), 0);
533 	/* bbp sw reset */
534 	mt76_set(dev, MT_BBP(CORE, 4), BIT(0));
535 	usleep_range(500, 1000);
536 	mt76_clear(dev, MT_BBP(CORE, 4), BIT(0));
537 
538 	if (chan->band == NL80211_BAND_5GHZ)
539 		mt76x0_rf_rmw(dev, MT_RF(0, 67), 0xf, 0x4);
540 }
541 
542 static int
543 mt76x0_phy_tssi_adc_calibrate(struct mt76x02_dev *dev, s16 *ltssi,
544 			      u8 *info)
545 {
546 	struct ieee80211_channel *chan = dev->mphy.chandef.chan;
547 	u32 val;
548 
549 	val = (chan->band == NL80211_BAND_5GHZ) ? 0x80055 : 0x80050;
550 	mt76_wr(dev, MT_BBP(CORE, 34), val);
551 
552 	if (!mt76_poll_msec(dev, MT_BBP(CORE, 34), BIT(4), 0, 200)) {
553 		mt76_clear(dev, MT_BBP(CORE, 34), BIT(4));
554 		return -ETIMEDOUT;
555 	}
556 
557 	*ltssi = mt76_rr(dev, MT_BBP(CORE, 35)) & 0xff;
558 	if (chan->band == NL80211_BAND_5GHZ)
559 		*ltssi += 128;
560 
561 	/* set packet info#1 mode */
562 	mt76_wr(dev, MT_BBP(CORE, 34), 0x80041);
563 	info[0] = mt76_rr(dev, MT_BBP(CORE, 35)) & 0xff;
564 
565 	/* set packet info#2 mode */
566 	mt76_wr(dev, MT_BBP(CORE, 34), 0x80042);
567 	info[1] = mt76_rr(dev, MT_BBP(CORE, 35)) & 0xff;
568 
569 	/* set packet info#3 mode */
570 	mt76_wr(dev, MT_BBP(CORE, 34), 0x80043);
571 	info[2] = mt76_rr(dev, MT_BBP(CORE, 35)) & 0xff;
572 
573 	return 0;
574 }
575 
576 static u8 mt76x0_phy_get_rf_pa_mode(struct mt76x02_dev *dev,
577 				    int index, u8 tx_rate)
578 {
579 	u32 val, reg;
580 
581 	reg = (index == 1) ? MT_RF_PA_MODE_CFG1 : MT_RF_PA_MODE_CFG0;
582 	val = mt76_rr(dev, reg);
583 	return (val & (3 << (tx_rate * 2))) >> (tx_rate * 2);
584 }
585 
586 static int
587 mt76x0_phy_get_target_power(struct mt76x02_dev *dev, u8 tx_mode,
588 			    u8 *info, s8 *target_power,
589 			    s8 *target_pa_power)
590 {
591 	u8 tx_rate, cur_power;
592 
593 	cur_power = mt76_rr(dev, MT_TX_ALC_CFG_0) & MT_TX_ALC_CFG_0_CH_INIT_0;
594 	switch (tx_mode) {
595 	case 0:
596 		/* cck rates */
597 		tx_rate = (info[0] & 0x60) >> 5;
598 		*target_power = cur_power + dev->rate_power.cck[tx_rate];
599 		*target_pa_power = mt76x0_phy_get_rf_pa_mode(dev, 0, tx_rate);
600 		break;
601 	case 1: {
602 		u8 index;
603 
604 		/* ofdm rates */
605 		tx_rate = (info[0] & 0xf0) >> 4;
606 		switch (tx_rate) {
607 		case 0xb:
608 			index = 0;
609 			break;
610 		case 0xf:
611 			index = 1;
612 			break;
613 		case 0xa:
614 			index = 2;
615 			break;
616 		case 0xe:
617 			index = 3;
618 			break;
619 		case 0x9:
620 			index = 4;
621 			break;
622 		case 0xd:
623 			index = 5;
624 			break;
625 		case 0x8:
626 			index = 6;
627 			break;
628 		case 0xc:
629 			index = 7;
630 			break;
631 		default:
632 			return -EINVAL;
633 		}
634 
635 		*target_power = cur_power + dev->rate_power.ofdm[index];
636 		*target_pa_power = mt76x0_phy_get_rf_pa_mode(dev, 0, index + 4);
637 		break;
638 	}
639 	case 4:
640 		/* vht rates */
641 		tx_rate = info[1] & 0xf;
642 		if (tx_rate > 9)
643 			return -EINVAL;
644 
645 		*target_power = cur_power + dev->rate_power.vht[tx_rate];
646 		*target_pa_power = mt76x0_phy_get_rf_pa_mode(dev, 1, tx_rate);
647 		break;
648 	default:
649 		/* ht rates */
650 		tx_rate = info[1] & 0x7f;
651 		if (tx_rate > 9)
652 			return -EINVAL;
653 
654 		*target_power = cur_power + dev->rate_power.ht[tx_rate];
655 		*target_pa_power = mt76x0_phy_get_rf_pa_mode(dev, 1, tx_rate);
656 		break;
657 	}
658 
659 	return 0;
660 }
661 
662 static s16 mt76x0_phy_lin2db(u16 val)
663 {
664 	u32 mantissa = val << 4;
665 	int ret, data;
666 	s16 exp = -4;
667 
668 	while (mantissa < BIT(15)) {
669 		mantissa <<= 1;
670 		if (--exp < -20)
671 			return -10000;
672 	}
673 	while (mantissa > 0xffff) {
674 		mantissa >>= 1;
675 		if (++exp > 20)
676 			return -10000;
677 	}
678 
679 	/* s(15,0) */
680 	if (mantissa <= 47104)
681 		data = mantissa + (mantissa >> 3) + (mantissa >> 4) - 38400;
682 	else
683 		data = mantissa - (mantissa >> 3) - (mantissa >> 6) - 23040;
684 	data = max_t(int, 0, data);
685 
686 	ret = ((15 + exp) << 15) + data;
687 	ret = (ret << 2) + (ret << 1) + (ret >> 6) + (ret >> 7);
688 	return ret >> 10;
689 }
690 
691 static int
692 mt76x0_phy_get_delta_power(struct mt76x02_dev *dev, u8 tx_mode,
693 			   s8 target_power, s8 target_pa_power,
694 			   s16 ltssi)
695 {
696 	struct ieee80211_channel *chan = dev->mphy.chandef.chan;
697 	int tssi_target = target_power << 12, tssi_slope;
698 	int tssi_offset, tssi_db, ret;
699 	u32 data;
700 	u16 val;
701 
702 	if (chan->band == NL80211_BAND_5GHZ) {
703 		u8 bound[7];
704 		int i, err;
705 
706 		err = mt76x02_eeprom_copy(dev, MT_EE_TSSI_BOUND1, bound,
707 					  sizeof(bound));
708 		if (err < 0)
709 			return err;
710 
711 		for (i = 0; i < ARRAY_SIZE(bound); i++) {
712 			if (chan->hw_value <= bound[i] || !bound[i])
713 				break;
714 		}
715 		val = mt76x02_eeprom_get(dev, MT_EE_TSSI_SLOPE_5G + i * 2);
716 
717 		tssi_offset = val >> 8;
718 		if ((tssi_offset >= 64 && tssi_offset <= 127) ||
719 		    (tssi_offset & BIT(7)))
720 			tssi_offset -= BIT(8);
721 	} else {
722 		val = mt76x02_eeprom_get(dev, MT_EE_TSSI_SLOPE_2G);
723 
724 		tssi_offset = val >> 8;
725 		if (tssi_offset & BIT(7))
726 			tssi_offset -= BIT(8);
727 	}
728 	tssi_slope = val & 0xff;
729 
730 	switch (target_pa_power) {
731 	case 1:
732 		if (chan->band == NL80211_BAND_2GHZ)
733 			tssi_target += 29491; /* 3.6 * 8192 */
734 		fallthrough;
735 	case 0:
736 		break;
737 	default:
738 		tssi_target += 4424; /* 0.54 * 8192 */
739 		break;
740 	}
741 
742 	if (!tx_mode) {
743 		data = mt76_rr(dev, MT_BBP(CORE, 1));
744 		if (is_mt7630(dev) && mt76_is_mmio(&dev->mt76)) {
745 			int offset;
746 
747 			/* 2.3 * 8192 or 1.5 * 8192 */
748 			offset = (data & BIT(5)) ? 18841 : 12288;
749 			tssi_target += offset;
750 		} else if (data & BIT(5)) {
751 			/* 0.8 * 8192 */
752 			tssi_target += 6554;
753 		}
754 	}
755 
756 	data = mt76_rr(dev, MT_BBP(TXBE, 4));
757 	switch (data & 0x3) {
758 	case 1:
759 		tssi_target -= 49152; /* -6db * 8192 */
760 		break;
761 	case 2:
762 		tssi_target -= 98304; /* -12db * 8192 */
763 		break;
764 	case 3:
765 		tssi_target += 49152; /* 6db * 8192 */
766 		break;
767 	default:
768 		break;
769 	}
770 
771 	tssi_db = mt76x0_phy_lin2db(ltssi - dev->cal.tssi_dc) * tssi_slope;
772 	if (chan->band == NL80211_BAND_5GHZ) {
773 		tssi_db += ((tssi_offset - 50) << 10); /* offset s4.3 */
774 		tssi_target -= tssi_db;
775 		if (ltssi > 254 && tssi_target > 0) {
776 			/* upper saturate */
777 			tssi_target = 0;
778 		}
779 	} else {
780 		tssi_db += (tssi_offset << 9); /* offset s3.4 */
781 		tssi_target -= tssi_db;
782 		/* upper-lower saturate */
783 		if ((ltssi > 126 && tssi_target > 0) ||
784 		    ((ltssi - dev->cal.tssi_dc) < 1 && tssi_target < 0)) {
785 			tssi_target = 0;
786 		}
787 	}
788 
789 	if ((dev->cal.tssi_target ^ tssi_target) < 0 &&
790 	    dev->cal.tssi_target > -4096 && dev->cal.tssi_target < 4096 &&
791 	    tssi_target > -4096 && tssi_target < 4096) {
792 		if ((tssi_target < 0 &&
793 		     tssi_target + dev->cal.tssi_target > 0) ||
794 		    (tssi_target > 0 &&
795 		     tssi_target + dev->cal.tssi_target <= 0))
796 			tssi_target = 0;
797 		else
798 			dev->cal.tssi_target = tssi_target;
799 	} else {
800 		dev->cal.tssi_target = tssi_target;
801 	}
802 
803 	/* make the compensate value to the nearest compensate code */
804 	if (tssi_target > 0)
805 		tssi_target += 2048;
806 	else
807 		tssi_target -= 2048;
808 	tssi_target >>= 12;
809 
810 	ret = mt76_get_field(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP);
811 	if (ret & BIT(5))
812 		ret -= BIT(6);
813 	ret += tssi_target;
814 
815 	ret = min_t(int, 31, ret);
816 	return max_t(int, -32, ret);
817 }
818 
819 static void mt76x0_phy_tssi_calibrate(struct mt76x02_dev *dev)
820 {
821 	s8 target_power, target_pa_power;
822 	u8 tssi_info[3], tx_mode;
823 	s16 ltssi;
824 	s8 val;
825 
826 	if (mt76x0_phy_tssi_adc_calibrate(dev, &ltssi, tssi_info) < 0)
827 		return;
828 
829 	tx_mode = tssi_info[0] & 0x7;
830 	if (mt76x0_phy_get_target_power(dev, tx_mode, tssi_info,
831 					&target_power, &target_pa_power) < 0)
832 		return;
833 
834 	val = mt76x0_phy_get_delta_power(dev, tx_mode, target_power,
835 					 target_pa_power, ltssi);
836 	mt76_rmw_field(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP, val);
837 }
838 
839 void mt76x0_phy_set_txpower(struct mt76x02_dev *dev)
840 {
841 	struct mt76x02_rate_power *t = &dev->rate_power;
842 	s8 info;
843 
844 	mt76x0_get_tx_power_per_rate(dev, dev->mphy.chandef.chan, t);
845 	mt76x0_get_power_info(dev, dev->mphy.chandef.chan, &info);
846 
847 	mt76x02_add_rate_power_offset(t, info);
848 	mt76x02_limit_rate_power(t, dev->txpower_conf);
849 	dev->mphy.txpower_cur = mt76x02_get_max_rate_power(t);
850 	mt76x02_add_rate_power_offset(t, -info);
851 
852 	dev->target_power = info;
853 	mt76x02_phy_set_txpower(dev, info, info);
854 }
855 
856 void mt76x0_phy_calibrate(struct mt76x02_dev *dev, bool power_on)
857 {
858 	struct ieee80211_channel *chan = dev->mphy.chandef.chan;
859 	int is_5ghz = (chan->band == NL80211_BAND_5GHZ) ? 1 : 0;
860 	u32 val, tx_alc, reg_val;
861 
862 	if (is_mt7630(dev))
863 		return;
864 
865 	if (power_on) {
866 		mt76x02_mcu_calibrate(dev, MCU_CAL_R, 0);
867 		mt76x02_mcu_calibrate(dev, MCU_CAL_VCO, chan->hw_value);
868 		usleep_range(10, 20);
869 
870 		if (mt76x0_tssi_enabled(dev)) {
871 			mt76_wr(dev, MT_MAC_SYS_CTRL,
872 				MT_MAC_SYS_CTRL_ENABLE_RX);
873 			mt76x0_phy_tssi_dc_calibrate(dev);
874 			mt76_wr(dev, MT_MAC_SYS_CTRL,
875 				MT_MAC_SYS_CTRL_ENABLE_TX |
876 				MT_MAC_SYS_CTRL_ENABLE_RX);
877 		}
878 	}
879 
880 	tx_alc = mt76_rr(dev, MT_TX_ALC_CFG_0);
881 	mt76_wr(dev, MT_TX_ALC_CFG_0, 0);
882 	usleep_range(500, 700);
883 
884 	reg_val = mt76_rr(dev, MT_BBP(IBI, 9));
885 	mt76_wr(dev, MT_BBP(IBI, 9), 0xffffff7e);
886 
887 	if (is_5ghz) {
888 		if (chan->hw_value < 100)
889 			val = 0x701;
890 		else if (chan->hw_value < 140)
891 			val = 0x801;
892 		else
893 			val = 0x901;
894 	} else {
895 		val = 0x600;
896 	}
897 
898 	mt76x02_mcu_calibrate(dev, MCU_CAL_FULL, val);
899 	mt76x02_mcu_calibrate(dev, MCU_CAL_LC, is_5ghz);
900 	usleep_range(15000, 20000);
901 
902 	mt76_wr(dev, MT_BBP(IBI, 9), reg_val);
903 	mt76_wr(dev, MT_TX_ALC_CFG_0, tx_alc);
904 	mt76x02_mcu_calibrate(dev, MCU_CAL_RXDCOC, 1);
905 }
906 EXPORT_SYMBOL_GPL(mt76x0_phy_calibrate);
907 
908 void mt76x0_phy_set_channel(struct mt76x02_dev *dev,
909 			    struct cfg80211_chan_def *chandef)
910 {
911 	u32 ext_cca_chan[4] = {
912 		[0] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 0) |
913 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 1) |
914 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) |
915 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) |
916 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(0)),
917 		[1] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 1) |
918 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 0) |
919 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 2) |
920 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 3) |
921 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(1)),
922 		[2] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 2) |
923 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 3) |
924 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) |
925 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) |
926 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(2)),
927 		[3] = FIELD_PREP(MT_EXT_CCA_CFG_CCA0, 3) |
928 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA1, 2) |
929 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA2, 1) |
930 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA3, 0) |
931 		      FIELD_PREP(MT_EXT_CCA_CFG_CCA_MASK, BIT(3)),
932 	};
933 	bool scan = test_bit(MT76_SCANNING, &dev->mphy.state);
934 	int ch_group_index, freq, freq1;
935 	u8 channel;
936 	u32 val;
937 	u16 rf_bw_band;
938 
939 	freq = chandef->chan->center_freq;
940 	freq1 = chandef->center_freq1;
941 	channel = chandef->chan->hw_value;
942 	rf_bw_band = (channel <= 14) ? RF_G_BAND : RF_A_BAND;
943 
944 	switch (chandef->width) {
945 	case NL80211_CHAN_WIDTH_40:
946 		if (freq1 > freq)
947 			ch_group_index = 0;
948 		else
949 			ch_group_index = 1;
950 		channel += 2 - ch_group_index * 4;
951 		rf_bw_band |= RF_BW_40;
952 		break;
953 	case NL80211_CHAN_WIDTH_80:
954 		ch_group_index = (freq - freq1 + 30) / 20;
955 		if (WARN_ON(ch_group_index < 0 || ch_group_index > 3))
956 			ch_group_index = 0;
957 		channel += 6 - ch_group_index * 4;
958 		rf_bw_band |= RF_BW_80;
959 		break;
960 	default:
961 		ch_group_index = 0;
962 		rf_bw_band |= RF_BW_20;
963 		break;
964 	}
965 
966 	if (mt76_is_usb(&dev->mt76)) {
967 		mt76x0_phy_bbp_set_bw(dev, chandef->width);
968 	} else {
969 		if (chandef->width == NL80211_CHAN_WIDTH_80 ||
970 		    chandef->width == NL80211_CHAN_WIDTH_40)
971 			val = 0x201;
972 		else
973 			val = 0x601;
974 		mt76_wr(dev, MT_TX_SW_CFG0, val);
975 	}
976 	mt76x02_phy_set_bw(dev, chandef->width, ch_group_index);
977 	mt76x02_phy_set_band(dev, chandef->chan->band,
978 			     ch_group_index & 1);
979 
980 	mt76_rmw(dev, MT_EXT_CCA_CFG,
981 		 (MT_EXT_CCA_CFG_CCA0 |
982 		  MT_EXT_CCA_CFG_CCA1 |
983 		  MT_EXT_CCA_CFG_CCA2 |
984 		  MT_EXT_CCA_CFG_CCA3 |
985 		  MT_EXT_CCA_CFG_CCA_MASK),
986 		 ext_cca_chan[ch_group_index]);
987 
988 	mt76x0_phy_set_band(dev, chandef->chan->band);
989 	mt76x0_phy_set_chan_rf_params(dev, channel, rf_bw_band);
990 
991 	/* set Japan Tx filter at channel 14 */
992 	if (channel == 14)
993 		mt76_set(dev, MT_BBP(CORE, 1), 0x20);
994 	else
995 		mt76_clear(dev, MT_BBP(CORE, 1), 0x20);
996 
997 	mt76x0_read_rx_gain(dev);
998 	mt76x0_phy_set_chan_bbp_params(dev, rf_bw_band);
999 
1000 	/* enable vco */
1001 	mt76x0_rf_set(dev, MT_RF(0, 4), BIT(7));
1002 	if (scan)
1003 		return;
1004 
1005 	mt76x02_init_agc_gain(dev);
1006 	mt76x0_phy_calibrate(dev, false);
1007 	mt76x0_phy_set_txpower(dev);
1008 
1009 	ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work,
1010 				     MT_CALIBRATE_INTERVAL);
1011 }
1012 
1013 static void mt76x0_phy_temp_sensor(struct mt76x02_dev *dev)
1014 {
1015 	u8 rf_b7_73, rf_b0_66, rf_b0_67;
1016 	s8 val;
1017 
1018 	rf_b7_73 = mt76x0_rf_rr(dev, MT_RF(7, 73));
1019 	rf_b0_66 = mt76x0_rf_rr(dev, MT_RF(0, 66));
1020 	rf_b0_67 = mt76x0_rf_rr(dev, MT_RF(0, 67));
1021 
1022 	mt76x0_rf_wr(dev, MT_RF(7, 73), 0x02);
1023 	mt76x0_rf_wr(dev, MT_RF(0, 66), 0x23);
1024 	mt76x0_rf_wr(dev, MT_RF(0, 67), 0x01);
1025 
1026 	mt76_wr(dev, MT_BBP(CORE, 34), 0x00080055);
1027 	if (!mt76_poll_msec(dev, MT_BBP(CORE, 34), BIT(4), 0, 200)) {
1028 		mt76_clear(dev, MT_BBP(CORE, 34), BIT(4));
1029 		goto done;
1030 	}
1031 
1032 	val = mt76_rr(dev, MT_BBP(CORE, 35));
1033 	val = (35 * (val - dev->cal.rx.temp_offset)) / 10 + 25;
1034 
1035 	if (abs(val - dev->cal.temp_vco) > 20) {
1036 		mt76x02_mcu_calibrate(dev, MCU_CAL_VCO,
1037 				      dev->mphy.chandef.chan->hw_value);
1038 		dev->cal.temp_vco = val;
1039 	}
1040 	if (abs(val - dev->cal.temp) > 30) {
1041 		mt76x0_phy_calibrate(dev, false);
1042 		dev->cal.temp = val;
1043 	}
1044 
1045 done:
1046 	mt76x0_rf_wr(dev, MT_RF(7, 73), rf_b7_73);
1047 	mt76x0_rf_wr(dev, MT_RF(0, 66), rf_b0_66);
1048 	mt76x0_rf_wr(dev, MT_RF(0, 67), rf_b0_67);
1049 }
1050 
1051 static void mt76x0_phy_set_gain_val(struct mt76x02_dev *dev)
1052 {
1053 	u8 gain = dev->cal.agc_gain_cur[0] - dev->cal.agc_gain_adjust;
1054 
1055 	mt76_rmw_field(dev, MT_BBP(AGC, 8), MT_BBP_AGC_GAIN, gain);
1056 
1057 	if ((dev->mphy.chandef.chan->flags & IEEE80211_CHAN_RADAR) &&
1058 	    !is_mt7630(dev))
1059 		mt76x02_phy_dfs_adjust_agc(dev);
1060 }
1061 
1062 static void
1063 mt76x0_phy_update_channel_gain(struct mt76x02_dev *dev)
1064 {
1065 	bool gain_change;
1066 	u8 gain_delta;
1067 	int low_gain;
1068 
1069 	dev->cal.avg_rssi_all = mt76_get_min_avg_rssi(&dev->mt76, false);
1070 	if (!dev->cal.avg_rssi_all)
1071 		dev->cal.avg_rssi_all = -75;
1072 
1073 	low_gain = (dev->cal.avg_rssi_all > mt76x02_get_rssi_gain_thresh(dev)) +
1074 		(dev->cal.avg_rssi_all > mt76x02_get_low_rssi_gain_thresh(dev));
1075 
1076 	gain_change = dev->cal.low_gain < 0 ||
1077 		      (dev->cal.low_gain & 2) ^ (low_gain & 2);
1078 	dev->cal.low_gain = low_gain;
1079 
1080 	if (!gain_change) {
1081 		if (mt76x02_phy_adjust_vga_gain(dev))
1082 			mt76x0_phy_set_gain_val(dev);
1083 		return;
1084 	}
1085 
1086 	dev->cal.agc_gain_adjust = (low_gain == 2) ? 0 : 10;
1087 	gain_delta = (low_gain == 2) ? 10 : 0;
1088 
1089 	dev->cal.agc_gain_cur[0] = dev->cal.agc_gain_init[0] - gain_delta;
1090 	mt76x0_phy_set_gain_val(dev);
1091 
1092 	/* clear false CCA counters */
1093 	mt76_rr(dev, MT_RX_STAT_1);
1094 }
1095 
1096 static void mt76x0_phy_calibration_work(struct work_struct *work)
1097 {
1098 	struct mt76x02_dev *dev = container_of(work, struct mt76x02_dev,
1099 					       cal_work.work);
1100 
1101 	mt76x0_phy_update_channel_gain(dev);
1102 	if (mt76x0_tssi_enabled(dev))
1103 		mt76x0_phy_tssi_calibrate(dev);
1104 	else
1105 		mt76x0_phy_temp_sensor(dev);
1106 
1107 	ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work,
1108 				     4 * MT_CALIBRATE_INTERVAL);
1109 }
1110 
1111 static void mt76x0_rf_patch_reg_array(struct mt76x02_dev *dev,
1112 				      const struct mt76_reg_pair *rp, int len)
1113 {
1114 	int i;
1115 
1116 	for (i = 0; i < len; i++) {
1117 		u32 reg = rp[i].reg;
1118 		u8 val = rp[i].value;
1119 
1120 		switch (reg) {
1121 		case MT_RF(0, 3):
1122 			if (mt76_is_mmio(&dev->mt76)) {
1123 				if (is_mt7630(dev))
1124 					val = 0x70;
1125 				else
1126 					val = 0x63;
1127 			} else {
1128 				val = 0x73;
1129 			}
1130 			break;
1131 		case MT_RF(0, 21):
1132 			if (is_mt7610e(dev))
1133 				val = 0x10;
1134 			else
1135 				val = 0x12;
1136 			break;
1137 		case MT_RF(5, 2):
1138 			if (is_mt7630(dev))
1139 				val = 0x1d;
1140 			else if (is_mt7610e(dev))
1141 				val = 0x00;
1142 			else
1143 				val = 0x0c;
1144 			break;
1145 		default:
1146 			break;
1147 		}
1148 		mt76x0_rf_wr(dev, reg, val);
1149 	}
1150 }
1151 
1152 static void mt76x0_phy_rf_init(struct mt76x02_dev *dev)
1153 {
1154 	int i;
1155 
1156 	mt76x0_rf_patch_reg_array(dev, mt76x0_rf_central_tab,
1157 				  ARRAY_SIZE(mt76x0_rf_central_tab));
1158 	mt76x0_rf_patch_reg_array(dev, mt76x0_rf_2g_channel_0_tab,
1159 				  ARRAY_SIZE(mt76x0_rf_2g_channel_0_tab));
1160 	RF_RANDOM_WRITE(dev, mt76x0_rf_5g_channel_0_tab);
1161 	RF_RANDOM_WRITE(dev, mt76x0_rf_vga_channel_0_tab);
1162 
1163 	for (i = 0; i < ARRAY_SIZE(mt76x0_rf_bw_switch_tab); i++) {
1164 		const struct mt76x0_rf_switch_item *item = &mt76x0_rf_bw_switch_tab[i];
1165 
1166 		if (item->bw_band == RF_BW_20)
1167 			mt76x0_rf_wr(dev, item->rf_bank_reg, item->value);
1168 		else if (((RF_G_BAND | RF_BW_20) & item->bw_band) ==
1169 			  (RF_G_BAND | RF_BW_20))
1170 			mt76x0_rf_wr(dev, item->rf_bank_reg, item->value);
1171 	}
1172 
1173 	for (i = 0; i < ARRAY_SIZE(mt76x0_rf_band_switch_tab); i++) {
1174 		if (mt76x0_rf_band_switch_tab[i].bw_band & RF_G_BAND) {
1175 			mt76x0_rf_wr(dev,
1176 				     mt76x0_rf_band_switch_tab[i].rf_bank_reg,
1177 				     mt76x0_rf_band_switch_tab[i].value);
1178 		}
1179 	}
1180 
1181 	/* Frequency calibration
1182 	 * E1: B0.R22<6:0>: xo_cxo<6:0>
1183 	 * E2: B0.R21<0>: xo_cxo<0>, B0.R22<7:0>: xo_cxo<8:1>
1184 	 */
1185 	mt76x0_rf_wr(dev, MT_RF(0, 22),
1186 		     min_t(u8, dev->cal.rx.freq_offset, 0xbf));
1187 	mt76x0_rf_rr(dev, MT_RF(0, 22));
1188 
1189 	/* Reset procedure DAC during power-up:
1190 	 * - set B0.R73<7>
1191 	 * - clear B0.R73<7>
1192 	 * - set B0.R73<7>
1193 	 */
1194 	mt76x0_rf_set(dev, MT_RF(0, 73), BIT(7));
1195 	mt76x0_rf_clear(dev, MT_RF(0, 73), BIT(7));
1196 	mt76x0_rf_set(dev, MT_RF(0, 73), BIT(7));
1197 
1198 	/* vcocal_en: initiate VCO calibration (reset after completion)) */
1199 	mt76x0_rf_set(dev, MT_RF(0, 4), 0x80);
1200 }
1201 
1202 void mt76x0_phy_init(struct mt76x02_dev *dev)
1203 {
1204 	INIT_DELAYED_WORK(&dev->cal_work, mt76x0_phy_calibration_work);
1205 
1206 	mt76x0_phy_ant_select(dev);
1207 	mt76x0_phy_rf_init(dev);
1208 	mt76x02_phy_set_rxpath(dev);
1209 	mt76x02_phy_set_txdac(dev);
1210 }
1211