1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
8 #include <linux/mtd/mtd.h>
9 #include <linux/mtd/partitions.h>
10 #include <linux/etherdevice.h>
11 #include <asm/unaligned.h>
23 field_validate(u8 val
)
25 if (!field_valid(val
))
32 mt7601u_efuse_read(struct mt7601u_dev
*dev
, u16 addr
, u8
*data
,
33 enum mt7601u_eeprom_access_modes mode
)
38 val
= mt76_rr(dev
, MT_EFUSE_CTRL
);
39 val
&= ~(MT_EFUSE_CTRL_AIN
|
41 val
|= FIELD_PREP(MT_EFUSE_CTRL_AIN
, addr
& ~0xf) |
42 FIELD_PREP(MT_EFUSE_CTRL_MODE
, mode
) |
44 mt76_wr(dev
, MT_EFUSE_CTRL
, val
);
46 if (!mt76_poll(dev
, MT_EFUSE_CTRL
, MT_EFUSE_CTRL_KICK
, 0, 1000))
49 val
= mt76_rr(dev
, MT_EFUSE_CTRL
);
50 if ((val
& MT_EFUSE_CTRL_AOUT
) == MT_EFUSE_CTRL_AOUT
) {
51 /* Parts of eeprom not in the usage map (0x80-0xc0,0xf0)
52 * will not return valid data but it's ok.
54 memset(data
, 0xff, 16);
58 for (i
= 0; i
< 4; i
++) {
59 val
= mt76_rr(dev
, MT_EFUSE_DATA(i
));
60 put_unaligned_le32(val
, data
+ 4 * i
);
67 mt7601u_efuse_physical_size_check(struct mt7601u_dev
*dev
)
69 const int map_reads
= DIV_ROUND_UP(MT_EFUSE_USAGE_MAP_SIZE
, 16);
70 u8 data
[round_up(MT_EFUSE_USAGE_MAP_SIZE
, 16)];
72 u32 start
= 0, end
= 0, cnt_free
;
74 for (i
= 0; i
< map_reads
; i
++) {
75 ret
= mt7601u_efuse_read(dev
, MT_EE_USAGE_MAP_START
+ i
* 16,
76 data
+ i
* 16, MT_EE_PHYSICAL_READ
);
81 for (i
= 0; i
< MT_EFUSE_USAGE_MAP_SIZE
; i
++)
84 start
= MT_EE_USAGE_MAP_START
+ i
;
85 end
= MT_EE_USAGE_MAP_START
+ i
;
87 cnt_free
= end
- start
+ 1;
89 if (MT_EFUSE_USAGE_MAP_SIZE
- cnt_free
< 5) {
90 dev_err(dev
->dev
, "Error: your device needs default EEPROM file and this driver doesn't support it!\n");
98 mt7601u_has_tssi(struct mt7601u_dev
*dev
, u8
*eeprom
)
100 u16 nic_conf1
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_1
);
102 return ~nic_conf1
&& (nic_conf1
& MT_EE_NIC_CONF_1_TX_ALC_EN
);
106 mt7601u_set_chip_cap(struct mt7601u_dev
*dev
, u8
*eeprom
)
108 u16 nic_conf0
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_0
);
109 u16 nic_conf1
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_1
);
111 if (!field_valid(nic_conf1
& 0xff))
114 dev
->ee
->tssi_enabled
= mt7601u_has_tssi(dev
, eeprom
) &&
115 !(nic_conf1
& MT_EE_NIC_CONF_1_TEMP_TX_ALC
);
117 if (nic_conf1
& MT_EE_NIC_CONF_1_HW_RF_CTRL
)
119 "Error: this driver does not support HW RF ctrl\n");
121 if (!field_valid(nic_conf0
>> 8))
124 if (FIELD_GET(MT_EE_NIC_CONF_0_RX_PATH
, nic_conf0
) > 1 ||
125 FIELD_GET(MT_EE_NIC_CONF_0_TX_PATH
, nic_conf0
) > 1)
127 "Error: device has more than 1 RX/TX stream!\n");
130 static void mt7601u_set_channel_target_power(struct mt7601u_dev
*dev
,
131 u8
*eeprom
, u8 max_pwr
)
133 u8 trgt_pwr
= eeprom
[MT_EE_TX_TSSI_TARGET_POWER
];
135 if (trgt_pwr
> max_pwr
|| !trgt_pwr
) {
136 dev_warn(dev
->dev
, "Error: EEPROM trgt power invalid %hhx!\n",
141 memset(dev
->ee
->chan_pwr
, trgt_pwr
, sizeof(dev
->ee
->chan_pwr
));
145 mt7601u_set_channel_power(struct mt7601u_dev
*dev
, u8
*eeprom
)
150 val
= mt7601u_rr(dev
, MT_TX_ALC_CFG_0
);
151 max_pwr
= FIELD_GET(MT_TX_ALC_CFG_0_LIMIT_0
, val
);
153 if (mt7601u_has_tssi(dev
, eeprom
)) {
154 mt7601u_set_channel_target_power(dev
, eeprom
, max_pwr
);
158 for (i
= 0; i
< 14; i
++) {
159 s8 power
= field_validate(eeprom
[MT_EE_TX_POWER_OFFSET
+ i
]);
161 if (power
> max_pwr
|| power
< 0)
162 power
= MT7601U_DEFAULT_TX_POWER
;
164 dev
->ee
->chan_pwr
[i
] = power
;
169 mt7601u_set_country_reg(struct mt7601u_dev
*dev
, u8
*eeprom
)
171 /* Note: - region 31 is not valid for mt7601u (see rtmp_init.c)
172 * - comments in rtmp_def.h are incorrect (see rt_channel.c)
174 static const struct reg_channel_bounds chan_bounds
[] = {
175 /* EEPROM country regions 0 - 7 */
176 { 1, 11 }, { 1, 13 }, { 10, 2 }, { 10, 4 },
177 { 14, 1 }, { 1, 14 }, { 3, 7 }, { 5, 9 },
178 /* EEPROM country regions 32 - 33 */
181 u8 val
= eeprom
[MT_EE_COUNTRY_REGION
];
186 if (val
> 31 && val
< 33)
191 "EEPROM country region %02hhx (channels %hhd-%hhd)\n",
192 val
, chan_bounds
[idx
].start
,
193 chan_bounds
[idx
].start
+ chan_bounds
[idx
].num
- 1);
195 idx
= 5; /* channels 1 - 14 */
197 dev
->ee
->reg
= chan_bounds
[idx
];
199 /* TODO: country region 33 is special - phy should be set to B-mode
200 * before entering channel 14 (see sta/connect.c)
205 mt7601u_set_rf_freq_off(struct mt7601u_dev
*dev
, u8
*eeprom
)
209 dev
->ee
->rf_freq_off
= field_validate(eeprom
[MT_EE_FREQ_OFFSET
]);
210 comp
= field_validate(eeprom
[MT_EE_FREQ_OFFSET_COMPENSATION
]);
213 dev
->ee
->rf_freq_off
-= comp
& 0x7f;
215 dev
->ee
->rf_freq_off
+= comp
;
219 mt7601u_set_rssi_offset(struct mt7601u_dev
*dev
, u8
*eeprom
)
222 s8
*rssi_offset
= dev
->ee
->rssi_offset
;
224 for (i
= 0; i
< 2; i
++) {
225 rssi_offset
[i
] = eeprom
[MT_EE_RSSI_OFFSET
+ i
];
227 if (rssi_offset
[i
] < -10 || rssi_offset
[i
] > 10) {
229 "Warning: EEPROM RSSI is invalid %02hhx\n",
237 mt7601u_extra_power_over_mac(struct mt7601u_dev
*dev
)
241 val
= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_1
) & 0x0000ff00) >> 8);
242 val
|= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_2
) & 0x0000ff00) << 8);
243 mt7601u_wr(dev
, MT_TX_PWR_CFG_7
, val
);
245 val
= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_4
) & 0x0000ff00) >> 8);
246 mt7601u_wr(dev
, MT_TX_PWR_CFG_9
, val
);
250 mt7601u_set_power_rate(struct power_per_rate
*rate
, s8 delta
, u8 value
)
252 /* Invalid? Note: vendor driver does not handle this */
256 rate
->raw
= s6_validate(value
);
257 rate
->bw20
= s6_to_int(value
);
258 /* Note: vendor driver does cap the value to s6 right away */
259 rate
->bw40
= rate
->bw20
+ delta
;
263 mt7601u_save_power_rate(struct mt7601u_dev
*dev
, s8 delta
, u32 val
, int i
)
265 struct mt7601u_rate_power
*t
= &dev
->ee
->power_rate_table
;
269 mt7601u_set_power_rate(&t
->cck
[0], delta
, (val
>> 0) & 0xff);
270 mt7601u_set_power_rate(&t
->cck
[1], delta
, (val
>> 8) & 0xff);
271 /* Save cck bw20 for fixups of channel 14 */
272 dev
->ee
->real_cck_bw20
[0] = t
->cck
[0].bw20
;
273 dev
->ee
->real_cck_bw20
[1] = t
->cck
[1].bw20
;
275 mt7601u_set_power_rate(&t
->ofdm
[0], delta
, (val
>> 16) & 0xff);
276 mt7601u_set_power_rate(&t
->ofdm
[1], delta
, (val
>> 24) & 0xff);
279 mt7601u_set_power_rate(&t
->ofdm
[2], delta
, (val
>> 0) & 0xff);
280 mt7601u_set_power_rate(&t
->ofdm
[3], delta
, (val
>> 8) & 0xff);
281 mt7601u_set_power_rate(&t
->ht
[0], delta
, (val
>> 16) & 0xff);
282 mt7601u_set_power_rate(&t
->ht
[1], delta
, (val
>> 24) & 0xff);
285 mt7601u_set_power_rate(&t
->ht
[2], delta
, (val
>> 0) & 0xff);
286 mt7601u_set_power_rate(&t
->ht
[3], delta
, (val
>> 8) & 0xff);
296 if (!field_valid(val
) || !(val
& BIT(7)))
309 mt7601u_config_tx_power_per_rate(struct mt7601u_dev
*dev
, u8
*eeprom
)
315 bw40_delta
= get_delta(eeprom
[MT_EE_TX_POWER_DELTA_BW40
]);
317 for (i
= 0; i
< 5; i
++) {
318 val
= get_unaligned_le32(eeprom
+ MT_EE_TX_POWER_BYRATE(i
));
320 mt7601u_save_power_rate(dev
, bw40_delta
, val
, i
);
323 mt7601u_wr(dev
, MT_TX_PWR_CFG_0
+ i
* 4, val
);
326 mt7601u_extra_power_over_mac(dev
);
330 mt7601u_init_tssi_params(struct mt7601u_dev
*dev
, u8
*eeprom
)
332 struct tssi_data
*d
= &dev
->ee
->tssi_data
;
334 if (!dev
->ee
->tssi_enabled
)
337 d
->slope
= eeprom
[MT_EE_TX_TSSI_SLOPE
];
338 d
->tx0_delta_offset
= eeprom
[MT_EE_TX_TSSI_OFFSET
] * 1024;
339 d
->offset
[0] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
];
340 d
->offset
[1] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
+ 1];
341 d
->offset
[2] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
+ 2];
345 mt7601u_eeprom_init(struct mt7601u_dev
*dev
)
350 ret
= mt7601u_efuse_physical_size_check(dev
);
354 dev
->ee
= devm_kzalloc(dev
->dev
, sizeof(*dev
->ee
), GFP_KERNEL
);
358 eeprom
= kmalloc(MT7601U_EEPROM_SIZE
, GFP_KERNEL
);
362 for (i
= 0; i
+ 16 <= MT7601U_EEPROM_SIZE
; i
+= 16) {
363 ret
= mt7601u_efuse_read(dev
, i
, eeprom
+ i
, MT_EE_READ
);
368 if (eeprom
[MT_EE_VERSION_EE
] > MT7601U_EE_MAX_VER
)
370 "Warning: unsupported EEPROM version %02hhx\n",
371 eeprom
[MT_EE_VERSION_EE
]);
372 dev_info(dev
->dev
, "EEPROM ver:%02hhx fae:%02hhx\n",
373 eeprom
[MT_EE_VERSION_EE
], eeprom
[MT_EE_VERSION_FAE
]);
375 mt7601u_set_macaddr(dev
, eeprom
+ MT_EE_MAC_ADDR
);
376 mt7601u_set_chip_cap(dev
, eeprom
);
377 mt7601u_set_channel_power(dev
, eeprom
);
378 mt7601u_set_country_reg(dev
, eeprom
);
379 mt7601u_set_rf_freq_off(dev
, eeprom
);
380 mt7601u_set_rssi_offset(dev
, eeprom
);
381 dev
->ee
->ref_temp
= eeprom
[MT_EE_REF_TEMP
];
382 dev
->ee
->lna_gain
= eeprom
[MT_EE_LNA_GAIN
];
384 mt7601u_config_tx_power_per_rate(dev
, eeprom
);
386 mt7601u_init_tssi_params(dev
, eeprom
);