2 * (c) Copyright 2002-2010, Ralink Technology, Inc.
3 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
20 #include "initvals_phy.h"
22 #include <linux/etherdevice.h>
24 static void mt7601u_agc_reset(struct mt7601u_dev
*dev
);
27 mt7601u_rf_wr(struct mt7601u_dev
*dev
, u8 bank
, u8 offset
, u8 value
)
31 if (WARN_ON(!test_bit(MT7601U_STATE_WLAN_RUNNING
, &dev
->state
)) ||
34 if (test_bit(MT7601U_STATE_REMOVED
, &dev
->state
))
37 mutex_lock(&dev
->reg_atomic_mutex
);
39 if (!mt76_poll(dev
, MT_RF_CSR_CFG
, MT_RF_CSR_CFG_KICK
, 0, 100)) {
44 mt7601u_wr(dev
, MT_RF_CSR_CFG
, MT76_SET(MT_RF_CSR_CFG_DATA
, value
) |
45 MT76_SET(MT_RF_CSR_CFG_REG_BANK
, bank
) |
46 MT76_SET(MT_RF_CSR_CFG_REG_ID
, offset
) |
49 trace_rf_write(dev
, bank
, offset
, value
);
51 mutex_unlock(&dev
->reg_atomic_mutex
);
54 dev_err(dev
->dev
, "Error: RF write %02hhx:%02hhx failed:%d!!\n",
61 mt7601u_rf_rr(struct mt7601u_dev
*dev
, u8 bank
, u8 offset
)
66 if (WARN_ON(!test_bit(MT7601U_STATE_WLAN_RUNNING
, &dev
->state
)) ||
69 if (test_bit(MT7601U_STATE_REMOVED
, &dev
->state
))
72 mutex_lock(&dev
->reg_atomic_mutex
);
74 if (!mt76_poll(dev
, MT_RF_CSR_CFG
, MT_RF_CSR_CFG_KICK
, 0, 100))
77 mt7601u_wr(dev
, MT_RF_CSR_CFG
, MT76_SET(MT_RF_CSR_CFG_REG_BANK
, bank
) |
78 MT76_SET(MT_RF_CSR_CFG_REG_ID
, offset
) |
81 if (!mt76_poll(dev
, MT_RF_CSR_CFG
, MT_RF_CSR_CFG_KICK
, 0, 100))
84 val
= mt7601u_rr(dev
, MT_RF_CSR_CFG
);
85 if (MT76_GET(MT_RF_CSR_CFG_REG_ID
, val
) == offset
&&
86 MT76_GET(MT_RF_CSR_CFG_REG_BANK
, val
) == bank
) {
87 ret
= MT76_GET(MT_RF_CSR_CFG_DATA
, val
);
88 trace_rf_read(dev
, bank
, offset
, ret
);
91 mutex_unlock(&dev
->reg_atomic_mutex
);
94 dev_err(dev
->dev
, "Error: RF read %02hhx:%02hhx failed:%d!!\n",
101 mt7601u_rf_rmw(struct mt7601u_dev
*dev
, u8 bank
, u8 offset
, u8 mask
, u8 val
)
105 ret
= mt7601u_rf_rr(dev
, bank
, offset
);
109 ret
= mt7601u_rf_wr(dev
, bank
, offset
, val
);
117 mt7601u_rf_set(struct mt7601u_dev
*dev
, u8 bank
, u8 offset
, u8 val
)
119 return mt7601u_rf_rmw(dev
, bank
, offset
, 0, val
);
123 mt7601u_rf_clear(struct mt7601u_dev
*dev
, u8 bank
, u8 offset
, u8 mask
)
125 return mt7601u_rf_rmw(dev
, bank
, offset
, mask
, 0);
128 static void mt7601u_bbp_wr(struct mt7601u_dev
*dev
, u8 offset
, u8 val
)
130 if (WARN_ON(!test_bit(MT7601U_STATE_WLAN_RUNNING
, &dev
->state
)) ||
131 test_bit(MT7601U_STATE_REMOVED
, &dev
->state
))
134 mutex_lock(&dev
->reg_atomic_mutex
);
136 if (!mt76_poll(dev
, MT_BBP_CSR_CFG
, MT_BBP_CSR_CFG_BUSY
, 0, 1000)) {
137 dev_err(dev
->dev
, "Error: BBP write %02hhx failed!!\n", offset
);
141 mt7601u_wr(dev
, MT_BBP_CSR_CFG
,
142 MT76_SET(MT_BBP_CSR_CFG_VAL
, val
) |
143 MT76_SET(MT_BBP_CSR_CFG_REG_NUM
, offset
) |
144 MT_BBP_CSR_CFG_RW_MODE
| MT_BBP_CSR_CFG_BUSY
);
145 trace_bbp_write(dev
, offset
, val
);
147 mutex_unlock(&dev
->reg_atomic_mutex
);
150 static int mt7601u_bbp_rr(struct mt7601u_dev
*dev
, u8 offset
)
153 int ret
= -ETIMEDOUT
;
155 if (WARN_ON(!test_bit(MT7601U_STATE_WLAN_RUNNING
, &dev
->state
)))
157 if (test_bit(MT7601U_STATE_REMOVED
, &dev
->state
))
160 mutex_lock(&dev
->reg_atomic_mutex
);
162 if (!mt76_poll(dev
, MT_BBP_CSR_CFG
, MT_BBP_CSR_CFG_BUSY
, 0, 1000))
165 mt7601u_wr(dev
, MT_BBP_CSR_CFG
,
166 MT76_SET(MT_BBP_CSR_CFG_REG_NUM
, offset
) |
167 MT_BBP_CSR_CFG_RW_MODE
| MT_BBP_CSR_CFG_BUSY
|
168 MT_BBP_CSR_CFG_READ
);
170 if (!mt76_poll(dev
, MT_BBP_CSR_CFG
, MT_BBP_CSR_CFG_BUSY
, 0, 1000))
173 val
= mt7601u_rr(dev
, MT_BBP_CSR_CFG
);
174 if (MT76_GET(MT_BBP_CSR_CFG_REG_NUM
, val
) == offset
) {
175 ret
= MT76_GET(MT_BBP_CSR_CFG_VAL
, val
);
176 trace_bbp_read(dev
, offset
, ret
);
179 mutex_unlock(&dev
->reg_atomic_mutex
);
182 dev_err(dev
->dev
, "Error: BBP read %02hhx failed:%d!!\n",
188 static int mt7601u_bbp_rmw(struct mt7601u_dev
*dev
, u8 offset
, u8 mask
, u8 val
)
192 ret
= mt7601u_bbp_rr(dev
, offset
);
196 mt7601u_bbp_wr(dev
, offset
, val
);
201 static u8
mt7601u_bbp_rmc(struct mt7601u_dev
*dev
, u8 offset
, u8 mask
, u8 val
)
205 ret
= mt7601u_bbp_rr(dev
, offset
);
210 mt7601u_bbp_wr(dev
, offset
, val
);
215 int mt7601u_wait_bbp_ready(struct mt7601u_dev
*dev
)
221 val
= mt7601u_bbp_rr(dev
, MT_BBP_REG_VERSION
);
227 dev_err(dev
->dev
, "Error: BBP is not ready\n");
234 u32
mt7601u_bbp_set_ctrlch(struct mt7601u_dev
*dev
, bool below
)
236 return mt7601u_bbp_rmc(dev
, 3, 0x20, below
? 0x20 : 0);
239 int mt7601u_phy_get_rssi(struct mt7601u_dev
*dev
,
240 struct mt7601u_rxwi
*rxwi
, u16 rate
)
242 static const s8 lna
[2][2][3] = {
244 /* bw20 */ { -2, 15, 33 },
245 /* bw40 */ { 0, 16, 34 }
248 /* bw20 */ { -2, 15, 33 },
249 /* bw40 */ { -2, 16, 34 }
252 int bw
= MT76_GET(MT_RXWI_RATE_BW
, rate
);
253 int aux_lna
= MT76_GET(MT_RXWI_ANT_AUX_LNA
, rxwi
->ant
);
254 int lna_id
= MT76_GET(MT_RXWI_GAIN_RSSI_LNA_ID
, rxwi
->gain
);
257 if (lna_id
) /* LNA id can be 0, 2, 3. */
261 val
-= lna
[aux_lna
][bw
][lna_id
];
262 val
-= MT76_GET(MT_RXWI_GAIN_RSSI_VAL
, rxwi
->gain
);
263 val
-= dev
->ee
->lna_gain
;
264 val
-= dev
->ee
->rssi_offset
[0];
269 static void mt7601u_vco_cal(struct mt7601u_dev
*dev
)
271 mt7601u_rf_wr(dev
, 0, 4, 0x0a);
272 mt7601u_rf_wr(dev
, 0, 5, 0x20);
273 mt7601u_rf_set(dev
, 0, 4, BIT(7));
277 static int mt7601u_set_bw_filter(struct mt7601u_dev
*dev
, bool cal
)
284 if (dev
->bw
!= MT_BW_20
)
288 ret
= mt7601u_mcu_calibrate(dev
, MCU_CAL_BW
, filter
| 1);
292 return mt7601u_mcu_calibrate(dev
, MCU_CAL_BW
, filter
);
295 static int mt7601u_load_bbp_temp_table_bw(struct mt7601u_dev
*dev
)
297 const struct reg_table
*t
;
299 if (WARN_ON(dev
->temp_mode
> MT_TEMP_MODE_LOW
))
302 t
= &bbp_mode_table
[dev
->temp_mode
][dev
->bw
];
304 return mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
, t
->regs
, t
->n
);
307 static int mt7601u_bbp_temp(struct mt7601u_dev
*dev
, int mode
, const char *name
)
309 const struct reg_table
*t
;
312 if (dev
->temp_mode
== mode
)
315 dev
->temp_mode
= mode
;
316 trace_temp_mode(dev
, mode
);
318 t
= bbp_mode_table
[dev
->temp_mode
];
319 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
,
324 return mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
,
325 t
[dev
->bw
].regs
, t
[dev
->bw
].n
);
328 static void mt7601u_apply_ch14_fixup(struct mt7601u_dev
*dev
, int hw_chan
)
330 struct mt7601u_rate_power
*t
= &dev
->ee
->power_rate_table
;
332 if (hw_chan
!= 14 || dev
->bw
!= MT_BW_20
) {
333 mt7601u_bbp_rmw(dev
, 4, 0x20, 0);
334 mt7601u_bbp_wr(dev
, 178, 0xff);
336 t
->cck
[0].bw20
= dev
->ee
->real_cck_bw20
[0];
337 t
->cck
[1].bw20
= dev
->ee
->real_cck_bw20
[1];
338 } else { /* Apply CH14 OBW fixup */
339 mt7601u_bbp_wr(dev
, 4, 0x60);
340 mt7601u_bbp_wr(dev
, 178, 0);
342 /* Note: vendor code is buggy here for negative values */
343 t
->cck
[0].bw20
= dev
->ee
->real_cck_bw20
[0] - 2;
344 t
->cck
[1].bw20
= dev
->ee
->real_cck_bw20
[1] - 2;
348 static int __mt7601u_phy_set_channel(struct mt7601u_dev
*dev
,
349 struct cfg80211_chan_def
*chandef
)
351 #define FREQ_PLAN_REGS 4
352 static const u8 freq_plan
[14][FREQ_PLAN_REGS
] = {
353 { 0x99, 0x99, 0x09, 0x50 },
354 { 0x46, 0x44, 0x0a, 0x50 },
355 { 0xec, 0xee, 0x0a, 0x50 },
356 { 0x99, 0x99, 0x0b, 0x50 },
357 { 0x46, 0x44, 0x08, 0x51 },
358 { 0xec, 0xee, 0x08, 0x51 },
359 { 0x99, 0x99, 0x09, 0x51 },
360 { 0x46, 0x44, 0x0a, 0x51 },
361 { 0xec, 0xee, 0x0a, 0x51 },
362 { 0x99, 0x99, 0x0b, 0x51 },
363 { 0x46, 0x44, 0x08, 0x52 },
364 { 0xec, 0xee, 0x08, 0x52 },
365 { 0x99, 0x99, 0x09, 0x52 },
366 { 0x33, 0x33, 0x0b, 0x52 },
368 struct mt76_reg_pair channel_freq_plan
[FREQ_PLAN_REGS
] = {
369 { 17, 0 }, { 18, 0 }, { 19, 0 }, { 20, 0 },
371 struct mt76_reg_pair bbp_settings
[3] = {
372 { 62, 0x37 - dev
->ee
->lna_gain
},
373 { 63, 0x37 - dev
->ee
->lna_gain
},
374 { 64, 0x37 - dev
->ee
->lna_gain
},
377 struct ieee80211_channel
*chan
= chandef
->chan
;
378 enum nl80211_channel_type chan_type
=
379 cfg80211_get_chandef_type(chandef
);
380 struct mt7601u_rate_power
*t
= &dev
->ee
->power_rate_table
;
387 chan_ext_below
= (chan_type
== NL80211_CHAN_HT40MINUS
);
388 chan_idx
= chan
->hw_value
- 1;
390 if (chandef
->width
== NL80211_CHAN_WIDTH_40
) {
393 if (chan_idx
> 1 && chan_type
== NL80211_CHAN_HT40MINUS
)
395 else if (chan_idx
< 12 && chan_type
== NL80211_CHAN_HT40PLUS
)
398 dev_err(dev
->dev
, "Error: invalid 40MHz channel!!\n");
401 if (bw
!= dev
->bw
|| chan_ext_below
!= dev
->chan_ext_below
) {
402 dev_dbg(dev
->dev
, "Info: switching HT mode bw:%d below:%d\n",
405 mt7601u_bbp_set_bw(dev
, bw
);
407 mt7601u_bbp_set_ctrlch(dev
, chan_ext_below
);
408 mt7601u_mac_set_ctrlch(dev
, chan_ext_below
);
409 dev
->chan_ext_below
= chan_ext_below
;
412 for (i
= 0; i
< FREQ_PLAN_REGS
; i
++)
413 channel_freq_plan
[i
].value
= freq_plan
[chan_idx
][i
];
415 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_RF
,
416 channel_freq_plan
, FREQ_PLAN_REGS
);
420 mt7601u_rmw(dev
, MT_TX_ALC_CFG_0
, 0x3f3f,
421 dev
->ee
->chan_pwr
[chan_idx
] & 0x3f);
423 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
,
424 bbp_settings
, ARRAY_SIZE(bbp_settings
));
428 mt7601u_vco_cal(dev
);
429 mt7601u_bbp_set_bw(dev
, bw
);
430 ret
= mt7601u_set_bw_filter(dev
, false);
434 mt7601u_apply_ch14_fixup(dev
, chan
->hw_value
);
435 mt7601u_wr(dev
, MT_TX_PWR_CFG_0
, int_to_s6(t
->ofdm
[1].bw20
) << 24 |
436 int_to_s6(t
->ofdm
[0].bw20
) << 16 |
437 int_to_s6(t
->cck
[1].bw20
) << 8 |
438 int_to_s6(t
->cck
[0].bw20
));
440 if (test_bit(MT7601U_STATE_SCANNING
, &dev
->state
))
441 mt7601u_agc_reset(dev
);
443 dev
->chandef
= *chandef
;
448 int mt7601u_phy_set_channel(struct mt7601u_dev
*dev
,
449 struct cfg80211_chan_def
*chandef
)
453 cancel_delayed_work_sync(&dev
->cal_work
);
454 cancel_delayed_work_sync(&dev
->freq_cal
.work
);
456 mutex_lock(&dev
->hw_atomic_mutex
);
457 ret
= __mt7601u_phy_set_channel(dev
, chandef
);
458 mutex_unlock(&dev
->hw_atomic_mutex
);
462 if (test_bit(MT7601U_STATE_SCANNING
, &dev
->state
))
465 ieee80211_queue_delayed_work(dev
->hw
, &dev
->cal_work
,
466 MT_CALIBRATE_INTERVAL
);
467 if (dev
->freq_cal
.enabled
)
468 ieee80211_queue_delayed_work(dev
->hw
, &dev
->freq_cal
.work
,
469 MT_FREQ_CAL_INIT_DELAY
);
473 #define BBP_R47_FLAG GENMASK(2, 0)
474 #define BBP_R47_F_TSSI 0
475 #define BBP_R47_F_PKT_T 1
476 #define BBP_R47_F_TX_RATE 2
477 #define BBP_R47_F_TEMP 4
479 * mt7601u_bbp_r47_get - read value through BBP R47/R49 pair
480 * @dev: pointer to adapter structure
481 * @reg: value of BBP R47 before the operation
482 * @flag: one of the BBP_R47_F_* flags
484 * Convenience helper for reading values through BBP R47/R49 pair.
485 * Takes old value of BBP R47 as @reg, because callers usually have it
488 * Return: value of BBP R49.
490 static u8
mt7601u_bbp_r47_get(struct mt7601u_dev
*dev
, u8 reg
, u8 flag
)
492 flag
|= reg
& ~BBP_R47_FLAG
;
493 mt7601u_bbp_wr(dev
, 47, flag
);
494 usleep_range(500, 700);
495 return mt7601u_bbp_rr(dev
, 49);
498 static s8
mt7601u_read_bootup_temp(struct mt7601u_dev
*dev
)
504 rf_set
= mt7601u_rr(dev
, MT_RF_SETTING_0
);
505 rf_bp
= mt7601u_rr(dev
, MT_RF_BYPASS_0
);
507 mt7601u_wr(dev
, MT_RF_BYPASS_0
, 0);
508 mt7601u_wr(dev
, MT_RF_SETTING_0
, 0x00000010);
509 mt7601u_wr(dev
, MT_RF_BYPASS_0
, 0x00000010);
511 bbp_val
= mt7601u_bbp_rmw(dev
, 47, 0, 0x10);
513 mt7601u_bbp_wr(dev
, 22, 0x40);
515 for (i
= 100; i
&& (bbp_val
& 0x10); i
--)
516 bbp_val
= mt7601u_bbp_rr(dev
, 47);
518 temp
= mt7601u_bbp_r47_get(dev
, bbp_val
, BBP_R47_F_TEMP
);
520 mt7601u_bbp_wr(dev
, 22, 0);
522 bbp_val
= mt7601u_bbp_rr(dev
, 21);
524 mt7601u_bbp_wr(dev
, 21, bbp_val
);
526 mt7601u_bbp_wr(dev
, 21, bbp_val
);
528 mt7601u_wr(dev
, MT_RF_BYPASS_0
, 0);
529 mt7601u_wr(dev
, MT_RF_SETTING_0
, rf_set
);
530 mt7601u_wr(dev
, MT_RF_BYPASS_0
, rf_bp
);
532 trace_read_temp(dev
, temp
);
536 static s8
mt7601u_read_temp(struct mt7601u_dev
*dev
)
542 val
= mt7601u_bbp_rmw(dev
, 47, 0x7f, 0x10);
544 /* Note: this rarely succeeds, temp can change even if it fails. */
545 for (i
= 100; i
&& (val
& 0x10); i
--)
546 val
= mt7601u_bbp_rr(dev
, 47);
548 temp
= mt7601u_bbp_r47_get(dev
, val
, BBP_R47_F_TEMP
);
550 trace_read_temp(dev
, temp
);
554 static void mt7601u_rxdc_cal(struct mt7601u_dev
*dev
)
556 static const struct mt76_reg_pair intro
[] = {
557 { 158, 0x8d }, { 159, 0xfc },
558 { 158, 0x8c }, { 159, 0x4c },
560 { 158, 0x8d }, { 159, 0xe0 },
565 mac_ctrl
= mt7601u_rr(dev
, MT_MAC_SYS_CTRL
);
566 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, MT_MAC_SYS_CTRL_ENABLE_RX
);
568 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
,
569 intro
, ARRAY_SIZE(intro
));
571 dev_err(dev
->dev
, "%s intro failed:%d\n", __func__
, ret
);
573 for (i
= 20; i
; i
--) {
574 usleep_range(300, 500);
576 mt7601u_bbp_wr(dev
, 158, 0x8c);
577 if (mt7601u_bbp_rr(dev
, 159) == 0x0c)
581 dev_err(dev
->dev
, "%s timed out\n", __func__
);
583 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, 0);
585 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
,
586 outro
, ARRAY_SIZE(outro
));
588 dev_err(dev
->dev
, "%s outro failed:%d\n", __func__
, ret
);
590 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, mac_ctrl
);
593 void mt7601u_phy_recalibrate_after_assoc(struct mt7601u_dev
*dev
)
595 mt7601u_mcu_calibrate(dev
, MCU_CAL_DPD
, dev
->curr_temp
);
597 mt7601u_rxdc_cal(dev
);
600 /* Note: function copied from vendor driver */
601 static s16
lin2dBd(u16 linear
)
604 unsigned int mantisa
;
607 if (WARN_ON(!linear
))
612 exp
= fls(mantisa
) - 16;
616 mantisa
<<= abs(exp
);
618 if (mantisa
<= 0xb800)
619 app
= (mantisa
+ (mantisa
>> 3) + (mantisa
>> 4) - 0x9600);
621 app
= (mantisa
- (mantisa
>> 3) - (mantisa
>> 6) - 0x5a00);
625 dBd
= ((15 + exp
) << 15) + app
;
626 dBd
= (dBd
<< 2) + (dBd
<< 1) + (dBd
>> 6) + (dBd
>> 7);
633 mt7601u_set_initial_tssi(struct mt7601u_dev
*dev
, s16 tssi_db
, s16 tssi_hvga_db
)
635 struct tssi_data
*d
= &dev
->ee
->tssi_data
;
638 init_offset
= -((tssi_db
* d
->slope
+ d
->offset
[1]) / 4096) + 10;
640 mt76_rmw(dev
, MT_TX_ALC_CFG_1
, MT_TX_ALC_CFG_1_TEMP_COMP
,
641 int_to_s6(init_offset
) & MT_TX_ALC_CFG_1_TEMP_COMP
);
644 static void mt7601u_tssi_dc_gain_cal(struct mt7601u_dev
*dev
)
646 u8 rf_vga
, rf_mixer
, bbp_r47
;
649 s16 tssi_init_db
, tssi_init_hvga_db
;
651 mt7601u_wr(dev
, MT_RF_SETTING_0
, 0x00000030);
652 mt7601u_wr(dev
, MT_RF_BYPASS_0
, 0x000c0030);
653 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, 0);
655 mt7601u_bbp_wr(dev
, 58, 0);
656 mt7601u_bbp_wr(dev
, 241, 0x2);
657 mt7601u_bbp_wr(dev
, 23, 0x8);
658 bbp_r47
= mt7601u_bbp_rr(dev
, 47);
661 rf_vga
= mt7601u_rf_rr(dev
, 5, 3);
662 mt7601u_rf_wr(dev
, 5, 3, 8);
665 rf_mixer
= mt7601u_rf_rr(dev
, 4, 39);
666 mt7601u_rf_wr(dev
, 4, 39, 0);
668 for (i
= 0; i
< 4; i
++) {
669 mt7601u_rf_wr(dev
, 4, 39, (i
& 1) ? rf_mixer
: 0);
671 mt7601u_bbp_wr(dev
, 23, (i
< 2) ? 0x08 : 0x02);
672 mt7601u_rf_wr(dev
, 5, 3, (i
< 2) ? 0x08 : 0x11);
674 /* BBP TSSI initial and soft reset */
675 mt7601u_bbp_wr(dev
, 22, 0);
676 mt7601u_bbp_wr(dev
, 244, 0);
678 mt7601u_bbp_wr(dev
, 21, 1);
680 mt7601u_bbp_wr(dev
, 21, 0);
682 /* TSSI measurement */
683 mt7601u_bbp_wr(dev
, 47, 0x50);
684 mt7601u_bbp_wr(dev
, (i
& 1) ? 244 : 22, (i
& 1) ? 0x31 : 0x40);
687 if (!(mt7601u_bbp_rr(dev
, 47) & 0x10))
690 dev_err(dev
->dev
, "%s timed out\n", __func__
);
693 mt7601u_bbp_wr(dev
, 47, 0x40);
694 res
[i
] = mt7601u_bbp_rr(dev
, 49);
697 tssi_init_db
= lin2dBd((short)res
[1] - res
[0]);
698 tssi_init_hvga_db
= lin2dBd(((short)res
[3] - res
[2]) * 4);
699 dev
->tssi_init
= res
[0];
700 dev
->tssi_init_hvga
= res
[2];
701 dev
->tssi_init_hvga_offset_db
= tssi_init_hvga_db
- tssi_init_db
;
704 "TSSI_init:%hhx db:%hx hvga:%hhx hvga_db:%hx off_db:%hx\n",
705 dev
->tssi_init
, tssi_init_db
, dev
->tssi_init_hvga
,
706 tssi_init_hvga_db
, dev
->tssi_init_hvga_offset_db
);
708 mt7601u_bbp_wr(dev
, 22, 0);
709 mt7601u_bbp_wr(dev
, 244, 0);
711 mt7601u_bbp_wr(dev
, 21, 1);
713 mt7601u_bbp_wr(dev
, 21, 0);
715 mt7601u_wr(dev
, MT_RF_BYPASS_0
, 0);
716 mt7601u_wr(dev
, MT_RF_SETTING_0
, 0);
718 mt7601u_rf_wr(dev
, 5, 3, rf_vga
);
719 mt7601u_rf_wr(dev
, 4, 39, rf_mixer
);
720 mt7601u_bbp_wr(dev
, 47, bbp_r47
);
722 mt7601u_set_initial_tssi(dev
, tssi_init_db
, tssi_init_hvga_db
);
725 static int mt7601u_temp_comp(struct mt7601u_dev
*dev
, bool on
)
727 int ret
, temp
, hi_temp
= 400, lo_temp
= -200;
729 temp
= (dev
->raw_temp
- dev
->ee
->ref_temp
) * MT_EE_TEMPERATURE_SLOPE
;
730 dev
->curr_temp
= temp
;
732 /* DPD Calibration */
733 if (temp
- dev
->dpd_temp
> 450 || temp
- dev
->dpd_temp
< -450) {
734 dev
->dpd_temp
= temp
;
736 ret
= mt7601u_mcu_calibrate(dev
, MCU_CAL_DPD
, dev
->dpd_temp
);
740 mt7601u_vco_cal(dev
);
742 dev_dbg(dev
->dev
, "Recalibrate DPD\n");
745 /* PLL Lock Protect */
746 if (temp
< -50 && !dev
->pll_lock_protect
) { /* < 20C */
747 dev
->pll_lock_protect
= true;
749 mt7601u_rf_wr(dev
, 4, 4, 6);
750 mt7601u_rf_clear(dev
, 4, 10, 0x30);
752 dev_dbg(dev
->dev
, "PLL lock protect on - too cold\n");
753 } else if (temp
> 50 && dev
->pll_lock_protect
) { /* > 30C */
754 dev
->pll_lock_protect
= false;
756 mt7601u_rf_wr(dev
, 4, 4, 0);
757 mt7601u_rf_rmw(dev
, 4, 10, 0x30, 0x10);
759 dev_dbg(dev
->dev
, "PLL lock protect off\n");
767 /* BBP CR for H, L, N temperature */
769 return mt7601u_bbp_temp(dev
, MT_TEMP_MODE_HIGH
, "high");
770 else if (temp
> lo_temp
)
771 return mt7601u_bbp_temp(dev
, MT_TEMP_MODE_NORMAL
, "normal");
773 return mt7601u_bbp_temp(dev
, MT_TEMP_MODE_LOW
, "low");
776 /* Note: this is used only with TSSI, we can just use trgt_pwr from eeprom. */
777 static int mt7601u_current_tx_power(struct mt7601u_dev
*dev
)
779 return dev
->ee
->chan_pwr
[dev
->chandef
.chan
->hw_value
- 1];
782 static bool mt7601u_use_hvga(struct mt7601u_dev
*dev
)
784 return !(mt7601u_current_tx_power(dev
) > 20);
788 mt7601u_phy_rf_pa_mode_val(struct mt7601u_dev
*dev
, int phy_mode
, int tx_rate
)
790 static const s16 decode_tb
[] = { 0, 8847, -5734, -5734 };
794 case MT_PHY_TYPE_OFDM
:
796 case MT_PHY_TYPE_CCK
:
797 reg
= dev
->rf_pa_mode
[0];
800 reg
= dev
->rf_pa_mode
[1];
804 return decode_tb
[(reg
>> (tx_rate
* 2)) & 0x3];
807 static struct mt7601u_tssi_params
808 mt7601u_tssi_params_get(struct mt7601u_dev
*dev
)
810 static const u8 ofdm_pkt2rate
[8] = { 6, 4, 2, 0, 7, 5, 3, 1 };
811 static const int static_power
[4] = { 0, -49152, -98304, 49152 };
812 struct mt7601u_tssi_params p
;
813 u8 bbp_r47
, pkt_type
, tx_rate
;
814 struct power_per_rate
*rate_table
;
816 bbp_r47
= mt7601u_bbp_rr(dev
, 47);
818 p
.tssi0
= mt7601u_bbp_r47_get(dev
, bbp_r47
, BBP_R47_F_TSSI
);
819 dev
->raw_temp
= mt7601u_bbp_r47_get(dev
, bbp_r47
, BBP_R47_F_TEMP
);
820 pkt_type
= mt7601u_bbp_r47_get(dev
, bbp_r47
, BBP_R47_F_PKT_T
);
822 p
.trgt_power
= mt7601u_current_tx_power(dev
);
824 switch (pkt_type
& 0x03) {
825 case MT_PHY_TYPE_CCK
:
826 tx_rate
= (pkt_type
>> 4) & 0x03;
827 rate_table
= dev
->ee
->power_rate_table
.cck
;
830 case MT_PHY_TYPE_OFDM
:
831 tx_rate
= ofdm_pkt2rate
[(pkt_type
>> 4) & 0x07];
832 rate_table
= dev
->ee
->power_rate_table
.ofdm
;
836 tx_rate
= mt7601u_bbp_r47_get(dev
, bbp_r47
, BBP_R47_F_TX_RATE
);
838 rate_table
= dev
->ee
->power_rate_table
.ht
;
842 if (dev
->bw
== MT_BW_20
)
843 p
.trgt_power
+= rate_table
[tx_rate
/ 2].bw20
;
845 p
.trgt_power
+= rate_table
[tx_rate
/ 2].bw40
;
849 dev_dbg(dev
->dev
, "tx_rate:%02hhx pwr:%08x\n", tx_rate
, p
.trgt_power
);
851 p
.trgt_power
+= mt7601u_phy_rf_pa_mode_val(dev
, pkt_type
& 0x03,
854 /* Channel 14, cck, bw20 */
855 if ((pkt_type
& 0x03) == MT_PHY_TYPE_CCK
) {
856 if (mt7601u_bbp_rr(dev
, 4) & 0x20)
857 p
.trgt_power
+= mt7601u_bbp_rr(dev
, 178) ? 18022 : 9830;
859 p
.trgt_power
+= mt7601u_bbp_rr(dev
, 178) ? 819 : 24576;
862 p
.trgt_power
+= static_power
[mt7601u_bbp_rr(dev
, 1) & 0x03];
864 p
.trgt_power
+= dev
->ee
->tssi_data
.tx0_delta_offset
;
867 "tssi:%02hhx t_power:%08x temp:%02hhx pkt_type:%02hhx\n",
868 p
.tssi0
, p
.trgt_power
, dev
->raw_temp
, pkt_type
);
873 static bool mt7601u_tssi_read_ready(struct mt7601u_dev
*dev
)
875 return !(mt7601u_bbp_rr(dev
, 47) & 0x10);
878 static int mt7601u_tssi_cal(struct mt7601u_dev
*dev
)
880 struct mt7601u_tssi_params params
;
881 int curr_pwr
, diff_pwr
;
884 s16 tssi_m_dc
, tssi_db
;
888 if (!dev
->ee
->tssi_enabled
)
891 hvga
= mt7601u_use_hvga(dev
);
892 if (!dev
->tssi_read_trig
)
893 return mt7601u_mcu_tssi_read_kick(dev
, hvga
);
895 if (!mt7601u_tssi_read_ready(dev
))
898 params
= mt7601u_tssi_params_get(dev
);
900 tssi_init
= (hvga
? dev
->tssi_init_hvga
: dev
->tssi_init
);
901 tssi_m_dc
= params
.tssi0
- tssi_init
;
902 tssi_db
= lin2dBd(tssi_m_dc
);
903 dev_dbg(dev
->dev
, "tssi dc:%04hx db:%04hx hvga:%d\n",
904 tssi_m_dc
, tssi_db
, hvga
);
906 if (dev
->chandef
.chan
->hw_value
< 5)
907 tssi_offset
= dev
->ee
->tssi_data
.offset
[0];
908 else if (dev
->chandef
.chan
->hw_value
< 9)
909 tssi_offset
= dev
->ee
->tssi_data
.offset
[1];
911 tssi_offset
= dev
->ee
->tssi_data
.offset
[2];
914 tssi_db
-= dev
->tssi_init_hvga_offset_db
;
916 curr_pwr
= tssi_db
* dev
->ee
->tssi_data
.slope
+ (tssi_offset
<< 9);
917 diff_pwr
= params
.trgt_power
- curr_pwr
;
918 dev_dbg(dev
->dev
, "Power curr:%08x diff:%08x\n", curr_pwr
, diff_pwr
);
920 if (params
.tssi0
> 126 && diff_pwr
> 0) {
921 dev_err(dev
->dev
, "Error: TSSI upper saturation\n");
924 if (params
.tssi0
- tssi_init
< 1 && diff_pwr
< 0) {
925 dev_err(dev
->dev
, "Error: TSSI lower saturation\n");
929 if ((dev
->prev_pwr_diff
^ diff_pwr
) < 0 && abs(diff_pwr
) < 4096 &&
930 (abs(diff_pwr
) > abs(dev
->prev_pwr_diff
) ||
931 (diff_pwr
> 0 && diff_pwr
== -dev
->prev_pwr_diff
)))
934 dev
->prev_pwr_diff
= diff_pwr
;
936 diff_pwr
+= (diff_pwr
> 0) ? 2048 : -2048;
939 dev_dbg(dev
->dev
, "final diff: %08x\n", diff_pwr
);
941 val
= mt7601u_rr(dev
, MT_TX_ALC_CFG_1
);
942 curr_pwr
= s6_to_int(MT76_GET(MT_TX_ALC_CFG_1_TEMP_COMP
, val
));
943 diff_pwr
+= curr_pwr
;
944 val
= (val
& ~MT_TX_ALC_CFG_1_TEMP_COMP
) | int_to_s6(diff_pwr
);
945 mt7601u_wr(dev
, MT_TX_ALC_CFG_1
, val
);
947 return mt7601u_mcu_tssi_read_kick(dev
, hvga
);
950 static u8
mt7601u_agc_default(struct mt7601u_dev
*dev
)
952 return (dev
->ee
->lna_gain
- 8) * 2 + 0x34;
955 static void mt7601u_agc_reset(struct mt7601u_dev
*dev
)
957 u8 agc
= mt7601u_agc_default(dev
);
959 mt7601u_bbp_wr(dev
, 66, agc
);
962 void mt7601u_agc_save(struct mt7601u_dev
*dev
)
964 dev
->agc_save
= mt7601u_bbp_rr(dev
, 66);
967 void mt7601u_agc_restore(struct mt7601u_dev
*dev
)
969 mt7601u_bbp_wr(dev
, 66, dev
->agc_save
);
972 static void mt7601u_agc_tune(struct mt7601u_dev
*dev
)
974 u8 val
= mt7601u_agc_default(dev
);
976 if (test_bit(MT7601U_STATE_SCANNING
, &dev
->state
))
979 /* Note: only in STA mode and not dozing; perhaps do this only if
980 * there is enough rssi updates since last run?
981 * Rssi updates are only on beacons and U2M so should work...
983 spin_lock_bh(&dev
->con_mon_lock
);
984 if (dev
->avg_rssi
<= -70)
986 else if (dev
->avg_rssi
<= -60)
988 spin_unlock_bh(&dev
->con_mon_lock
);
990 if (val
!= mt7601u_bbp_rr(dev
, 66))
991 mt7601u_bbp_wr(dev
, 66, val
);
993 /* TODO: also if lost a lot of beacons try resetting
994 * (see RTMPSetAGCInitValue() call in mlme.c).
998 static void mt7601u_phy_calibrate(struct work_struct
*work
)
1000 struct mt7601u_dev
*dev
= container_of(work
, struct mt7601u_dev
,
1003 mt7601u_agc_tune(dev
);
1004 mt7601u_tssi_cal(dev
);
1005 /* If TSSI calibration was run it already updated temperature. */
1006 if (!dev
->ee
->tssi_enabled
)
1007 dev
->raw_temp
= mt7601u_read_temp(dev
);
1008 mt7601u_temp_comp(dev
, true); /* TODO: find right value for @on */
1010 ieee80211_queue_delayed_work(dev
->hw
, &dev
->cal_work
,
1011 MT_CALIBRATE_INTERVAL
);
1014 static unsigned long
1015 __mt7601u_phy_freq_cal(struct mt7601u_dev
*dev
, s8 last_offset
, u8 phy_mode
)
1017 u8 activate_threshold
, deactivate_threshold
;
1019 trace_freq_cal_offset(dev
, phy_mode
, last_offset
);
1021 /* No beacons received - reschedule soon */
1022 if (last_offset
== MT_FREQ_OFFSET_INVALID
)
1023 return MT_FREQ_CAL_ADJ_INTERVAL
;
1026 case MT_PHY_TYPE_CCK
:
1027 activate_threshold
= 19;
1028 deactivate_threshold
= 5;
1030 case MT_PHY_TYPE_OFDM
:
1031 activate_threshold
= 102;
1032 deactivate_threshold
= 32;
1034 case MT_PHY_TYPE_HT
:
1035 case MT_PHY_TYPE_HT_GF
:
1036 activate_threshold
= 82;
1037 deactivate_threshold
= 20;
1041 return MT_FREQ_CAL_CHECK_INTERVAL
;
1044 if (abs(last_offset
) >= activate_threshold
)
1045 dev
->freq_cal
.adjusting
= true;
1046 else if (abs(last_offset
) <= deactivate_threshold
)
1047 dev
->freq_cal
.adjusting
= false;
1049 if (!dev
->freq_cal
.adjusting
)
1050 return MT_FREQ_CAL_CHECK_INTERVAL
;
1052 if (last_offset
> deactivate_threshold
) {
1053 if (dev
->freq_cal
.freq
> 0)
1054 dev
->freq_cal
.freq
--;
1056 dev
->freq_cal
.adjusting
= false;
1057 } else if (last_offset
< -deactivate_threshold
) {
1058 if (dev
->freq_cal
.freq
< 0xbf)
1059 dev
->freq_cal
.freq
++;
1061 dev
->freq_cal
.adjusting
= false;
1064 trace_freq_cal_adjust(dev
, dev
->freq_cal
.freq
);
1065 mt7601u_rf_wr(dev
, 0, 12, dev
->freq_cal
.freq
);
1066 mt7601u_vco_cal(dev
);
1068 return dev
->freq_cal
.adjusting
? MT_FREQ_CAL_ADJ_INTERVAL
:
1069 MT_FREQ_CAL_CHECK_INTERVAL
;
1072 static void mt7601u_phy_freq_cal(struct work_struct
*work
)
1074 struct mt7601u_dev
*dev
= container_of(work
, struct mt7601u_dev
,
1075 freq_cal
.work
.work
);
1078 unsigned long delay
;
1080 spin_lock_bh(&dev
->con_mon_lock
);
1081 last_offset
= dev
->bcn_freq_off
;
1082 phy_mode
= dev
->bcn_phy_mode
;
1083 spin_unlock_bh(&dev
->con_mon_lock
);
1085 delay
= __mt7601u_phy_freq_cal(dev
, last_offset
, phy_mode
);
1086 ieee80211_queue_delayed_work(dev
->hw
, &dev
->freq_cal
.work
, delay
);
1088 spin_lock_bh(&dev
->con_mon_lock
);
1089 dev
->bcn_freq_off
= MT_FREQ_OFFSET_INVALID
;
1090 spin_unlock_bh(&dev
->con_mon_lock
);
1093 void mt7601u_phy_con_cal_onoff(struct mt7601u_dev
*dev
,
1094 struct ieee80211_bss_conf
*info
)
1097 cancel_delayed_work_sync(&dev
->freq_cal
.work
);
1099 /* Start/stop collecting beacon data */
1100 spin_lock_bh(&dev
->con_mon_lock
);
1101 ether_addr_copy(dev
->ap_bssid
, info
->bssid
);
1103 dev
->bcn_freq_off
= MT_FREQ_OFFSET_INVALID
;
1104 spin_unlock_bh(&dev
->con_mon_lock
);
1106 dev
->freq_cal
.freq
= dev
->ee
->rf_freq_off
;
1107 dev
->freq_cal
.enabled
= info
->assoc
;
1108 dev
->freq_cal
.adjusting
= false;
1111 ieee80211_queue_delayed_work(dev
->hw
, &dev
->freq_cal
.work
,
1112 MT_FREQ_CAL_INIT_DELAY
);
1115 static int mt7601u_init_cal(struct mt7601u_dev
*dev
)
1120 dev
->raw_temp
= mt7601u_read_bootup_temp(dev
);
1121 dev
->curr_temp
= (dev
->raw_temp
- dev
->ee
->ref_temp
) *
1122 MT_EE_TEMPERATURE_SLOPE
;
1123 dev
->dpd_temp
= dev
->curr_temp
;
1125 mac_ctrl
= mt7601u_rr(dev
, MT_MAC_SYS_CTRL
);
1127 ret
= mt7601u_mcu_calibrate(dev
, MCU_CAL_R
, 0);
1131 ret
= mt7601u_rf_rr(dev
, 0, 4);
1135 ret
= mt7601u_rf_wr(dev
, 0, 4, ret
);
1140 ret
= mt7601u_mcu_calibrate(dev
, MCU_CAL_TXDCOC
, 0);
1144 mt7601u_rxdc_cal(dev
);
1146 ret
= mt7601u_set_bw_filter(dev
, true);
1149 ret
= mt7601u_mcu_calibrate(dev
, MCU_CAL_LOFT
, 0);
1152 ret
= mt7601u_mcu_calibrate(dev
, MCU_CAL_TXIQ
, 0);
1155 ret
= mt7601u_mcu_calibrate(dev
, MCU_CAL_RXIQ
, 0);
1158 ret
= mt7601u_mcu_calibrate(dev
, MCU_CAL_DPD
, dev
->dpd_temp
);
1162 mt7601u_rxdc_cal(dev
);
1164 mt7601u_tssi_dc_gain_cal(dev
);
1166 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, mac_ctrl
);
1168 mt7601u_temp_comp(dev
, true);
1173 int mt7601u_bbp_set_bw(struct mt7601u_dev
*dev
, int bw
)
1177 if (bw
== dev
->bw
) {
1178 /* Vendor driver does the rmc even when no change is needed. */
1179 mt7601u_bbp_rmc(dev
, 4, 0x18, bw
== MT_BW_20
? 0 : 0x10);
1185 /* Stop MAC for the time of bw change */
1186 old
= mt7601u_rr(dev
, MT_MAC_SYS_CTRL
);
1187 val
= old
& ~(MT_MAC_SYS_CTRL_ENABLE_TX
| MT_MAC_SYS_CTRL_ENABLE_RX
);
1188 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, val
);
1189 mt76_poll(dev
, MT_MAC_STATUS
, MT_MAC_STATUS_TX
| MT_MAC_STATUS_RX
,
1192 mt7601u_bbp_rmc(dev
, 4, 0x18, bw
== MT_BW_20
? 0 : 0x10);
1194 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, old
);
1196 return mt7601u_load_bbp_temp_table_bw(dev
);
1200 * mt7601u_set_rx_path - set rx path in BBP
1201 * @dev: pointer to adapter structure
1202 * @path: rx path to set values are 0-based
1204 void mt7601u_set_rx_path(struct mt7601u_dev
*dev
, u8 path
)
1206 mt7601u_bbp_rmw(dev
, 3, 0x18, path
<< 3);
1210 * mt7601u_set_tx_dac - set which tx DAC to use
1211 * @dev: pointer to adapter structure
1212 * @path: DAC index, values are 0-based
1214 void mt7601u_set_tx_dac(struct mt7601u_dev
*dev
, u8 dac
)
1216 mt7601u_bbp_rmc(dev
, 1, 0x18, dac
<< 3);
1219 int mt7601u_phy_init(struct mt7601u_dev
*dev
)
1223 dev
->rf_pa_mode
[0] = mt7601u_rr(dev
, MT_RF_PA_MODE_CFG0
);
1224 dev
->rf_pa_mode
[1] = mt7601u_rr(dev
, MT_RF_PA_MODE_CFG1
);
1226 ret
= mt7601u_rf_wr(dev
, 0, 12, dev
->ee
->rf_freq_off
);
1229 ret
= mt7601u_write_reg_pairs(dev
, 0, rf_central
,
1230 ARRAY_SIZE(rf_central
));
1233 ret
= mt7601u_write_reg_pairs(dev
, 0, rf_channel
,
1234 ARRAY_SIZE(rf_channel
));
1237 ret
= mt7601u_write_reg_pairs(dev
, 0, rf_vga
, ARRAY_SIZE(rf_vga
));
1241 ret
= mt7601u_init_cal(dev
);
1245 dev
->prev_pwr_diff
= 100;
1247 INIT_DELAYED_WORK(&dev
->cal_work
, mt7601u_phy_calibrate
);
1248 INIT_DELAYED_WORK(&dev
->freq_cal
.work
, mt7601u_phy_freq_cal
);