2 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
4 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@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.
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/etherdevice.h>
20 #include <asm/unaligned.h>
31 field_validate(u8 val)
33 if (!field_valid(val))
40 sign_extend(u32 val, unsigned int size)
42 bool sign = val & BIT(size - 1);
44 val &= BIT(size - 1) - 1;
46 return sign ? val : -val;
50 mt76x0_efuse_read(struct mt76x0_dev *dev, u16 addr, u8 *data,
51 enum mt76x0_eeprom_access_modes mode)
56 val = mt76_rr(dev, MT_EFUSE_CTRL);
57 val &= ~(MT_EFUSE_CTRL_AIN |
59 val |= FIELD_PREP(MT_EFUSE_CTRL_AIN, addr & ~0xf) |
60 FIELD_PREP(MT_EFUSE_CTRL_MODE, mode) |
62 mt76_wr(dev, MT_EFUSE_CTRL, val);
64 if (!mt76_poll(dev, MT_EFUSE_CTRL, MT_EFUSE_CTRL_KICK, 0, 1000))
67 val = mt76_rr(dev, MT_EFUSE_CTRL);
68 if ((val & MT_EFUSE_CTRL_AOUT) == MT_EFUSE_CTRL_AOUT) {
69 /* Parts of eeprom not in the usage map (0x80-0xc0,0xf0)
70 * will not return valid data but it's ok.
72 memset(data, 0xff, 16);
76 for (i = 0; i < 4; i++) {
77 val = mt76_rr(dev, MT_EFUSE_DATA(i));
78 put_unaligned_le32(val, data + 4 * i);
84 #define MT_MAP_READS DIV_ROUND_UP(MT_EFUSE_USAGE_MAP_SIZE, 16)
86 mt76x0_efuse_physical_size_check(struct mt76x0_dev *dev)
88 u8 data[MT_MAP_READS * 16];
90 u32 start = 0, end = 0, cnt_free;
92 for (i = 0; i < MT_MAP_READS; i++) {
93 ret = mt76x0_efuse_read(dev, MT_EE_USAGE_MAP_START + i * 16,
94 data + i * 16, MT_EE_PHYSICAL_READ);
99 for (i = 0; i < MT_EFUSE_USAGE_MAP_SIZE; i++)
102 start = MT_EE_USAGE_MAP_START + i;
103 end = MT_EE_USAGE_MAP_START + i;
105 cnt_free = end - start + 1;
107 if (MT_EFUSE_USAGE_MAP_SIZE - cnt_free < 5) {
108 dev_err(dev->mt76.dev, "Error: your device needs default EEPROM file and this driver doesn't support it!\n");
116 mt76x0_set_chip_cap(struct mt76x0_dev *dev, u8 *eeprom)
118 enum mt76x2_board_type { BOARD_TYPE_2GHZ = 1, BOARD_TYPE_5GHZ = 2 };
119 u16 nic_conf0 = get_unaligned_le16(eeprom + MT_EE_NIC_CONF_0);
120 u16 nic_conf1 = get_unaligned_le16(eeprom + MT_EE_NIC_CONF_1);
122 dev_dbg(dev->mt76.dev, "NIC_CONF0: %04x NIC_CONF1: %04x\n", nic_conf0, nic_conf1);
124 switch (FIELD_GET(MT_EE_NIC_CONF_0_BOARD_TYPE, nic_conf0)) {
125 case BOARD_TYPE_5GHZ:
126 dev->ee->has_5ghz = true;
128 case BOARD_TYPE_2GHZ:
129 dev->ee->has_2ghz = true;
132 dev->ee->has_2ghz = true;
133 dev->ee->has_5ghz = true;
137 dev_dbg(dev->mt76.dev, "Has 2GHZ %d 5GHZ %d\n", dev->ee->has_2ghz, dev->ee->has_5ghz);
139 if (!field_valid(nic_conf1 & 0xff))
142 if (nic_conf1 & MT_EE_NIC_CONF_1_HW_RF_CTRL)
143 dev_err(dev->mt76.dev,
144 "Error: this driver does not support HW RF ctrl\n");
146 if (!field_valid(nic_conf0 >> 8))
149 if (FIELD_GET(MT_EE_NIC_CONF_0_RX_PATH, nic_conf0) > 1 ||
150 FIELD_GET(MT_EE_NIC_CONF_0_TX_PATH, nic_conf0) > 1)
151 dev_err(dev->mt76.dev,
152 "Error: device has more than 1 RX/TX stream!\n");
154 dev->ee->pa_type = FIELD_GET(MT_EE_NIC_CONF_0_PA_TYPE, nic_conf0);
155 dev_dbg(dev->mt76.dev, "PA Type %d\n", dev->ee->pa_type);
159 mt76x0_set_macaddr(struct mt76x0_dev *dev, const u8 *eeprom)
161 const void *src = eeprom + MT_EE_MAC_ADDR;
163 ether_addr_copy(dev->macaddr, src);
165 if (!is_valid_ether_addr(dev->macaddr)) {
166 eth_random_addr(dev->macaddr);
167 dev_info(dev->mt76.dev,
168 "Invalid MAC address, using random address %pM\n",
172 mt76_wr(dev, MT_MAC_ADDR_DW0, get_unaligned_le32(dev->macaddr));
173 mt76_wr(dev, MT_MAC_ADDR_DW1, get_unaligned_le16(dev->macaddr + 4) |
174 FIELD_PREP(MT_MAC_ADDR_DW1_U2ME_MASK, 0xff));
180 mt76x0_set_temp_offset(struct mt76x0_dev *dev, u8 *eeprom)
182 u8 temp = eeprom[MT_EE_TEMP_OFFSET];
184 if (field_valid(temp))
185 dev->ee->temp_off = sign_extend(temp, 8);
187 dev->ee->temp_off = -10;
191 mt76x0_set_country_reg(struct mt76x0_dev *dev, u8 *eeprom)
193 /* Note: - region 31 is not valid for mt76x0 (see rtmp_init.c)
194 * - comments in rtmp_def.h are incorrect (see rt_channel.c)
196 static const struct reg_channel_bounds chan_bounds[] = {
197 /* EEPROM country regions 0 - 7 */
198 { 1, 11 }, { 1, 13 }, { 10, 2 }, { 10, 4 },
199 { 14, 1 }, { 1, 14 }, { 3, 7 }, { 5, 9 },
200 /* EEPROM country regions 32 - 33 */
203 u8 val = eeprom[MT_EE_COUNTRY_REGION_2GHZ];
206 dev_dbg(dev->mt76.dev, "REG 2GHZ %u REG 5GHZ %u\n", val, eeprom[MT_EE_COUNTRY_REGION_5GHZ]);
209 if (val > 31 && val < 33)
213 dev_info(dev->mt76.dev,
214 "EEPROM country region %02hhx (channels %hhd-%hhd)\n",
215 val, chan_bounds[idx].start,
216 chan_bounds[idx].start + chan_bounds[idx].num - 1);
218 idx = 5; /* channels 1 - 14 */
220 dev->ee->reg = chan_bounds[idx];
222 /* TODO: country region 33 is special - phy should be set to B-mode
223 * before entering channel 14 (see sta/connect.c)
228 mt76x0_set_rf_freq_off(struct mt76x0_dev *dev, u8 *eeprom)
232 dev->ee->rf_freq_off = field_validate(eeprom[MT_EE_FREQ_OFFSET]);
233 comp = field_validate(eeprom[MT_EE_FREQ_OFFSET_COMPENSATION]);
236 dev->ee->rf_freq_off -= comp & 0x7f;
238 dev->ee->rf_freq_off += comp;
242 mt76x0_set_lna_gain(struct mt76x0_dev *dev, u8 *eeprom)
246 dev->ee->lna_gain_2ghz = eeprom[MT_EE_LNA_GAIN_2GHZ];
247 dev->ee->lna_gain_5ghz[0] = eeprom[MT_EE_LNA_GAIN_5GHZ_0];
249 gain = eeprom[MT_EE_LNA_GAIN_5GHZ_1];
250 if (gain == 0xff || gain == 0)
251 dev->ee->lna_gain_5ghz[1] = dev->ee->lna_gain_5ghz[0];
253 dev->ee->lna_gain_5ghz[1] = gain;
255 gain = eeprom[MT_EE_LNA_GAIN_5GHZ_2];
256 if (gain == 0xff || gain == 0)
257 dev->ee->lna_gain_5ghz[2] = dev->ee->lna_gain_5ghz[0];
259 dev->ee->lna_gain_5ghz[2] = gain;
263 mt76x0_set_rssi_offset(struct mt76x0_dev *dev, u8 *eeprom)
266 s8 *rssi_offset = dev->ee->rssi_offset_2ghz;
268 for (i = 0; i < 2; i++) {
269 rssi_offset[i] = eeprom[MT_EE_RSSI_OFFSET + i];
271 if (rssi_offset[i] < -10 || rssi_offset[i] > 10) {
272 dev_warn(dev->mt76.dev,
273 "Warning: EEPROM RSSI is invalid %02hhx\n",
279 rssi_offset = dev->ee->rssi_offset_5ghz;
281 for (i = 0; i < 3; i++) {
282 rssi_offset[i] = eeprom[MT_EE_RSSI_OFFSET_5GHZ + i];
284 if (rssi_offset[i] < -10 || rssi_offset[i] > 10) {
285 dev_warn(dev->mt76.dev,
286 "Warning: EEPROM RSSI is invalid %02hhx\n",
294 calc_bw40_power_rate(u32 value, int delta)
299 for (i = 0; i < 4; i++) {
300 tmp = s6_to_int((value >> i*8) & 0xff) + delta;
301 ret |= (u32)(int_to_s6(tmp)) << i*8;
312 if (!field_valid(val) || !(val & BIT(7)))
325 mt76x0_set_tx_power_per_rate(struct mt76x0_dev *dev, u8 *eeprom)
327 s8 bw40_delta_2g, bw40_delta_5g;
331 bw40_delta_2g = get_delta(eeprom[MT_EE_TX_POWER_DELTA_BW40]);
332 bw40_delta_5g = get_delta(eeprom[MT_EE_TX_POWER_DELTA_BW40 + 1]);
334 for (i = 0; i < 5; i++) {
335 val = get_unaligned_le32(eeprom + MT_EE_TX_POWER_BYRATE(i));
337 /* Skip last 16 bits. */
341 dev->ee->tx_pwr_cfg_2g[i][0] = val;
342 dev->ee->tx_pwr_cfg_2g[i][1] = calc_bw40_power_rate(val, bw40_delta_2g);
345 /* Reading per rate tx power for 5 GHz band is a bit more complex. Note
346 * we mix 16 bit and 32 bit reads and sometimes do shifts.
348 val = get_unaligned_le16(eeprom + 0x120);
350 dev->ee->tx_pwr_cfg_5g[0][0] = val;
351 dev->ee->tx_pwr_cfg_5g[0][1] = calc_bw40_power_rate(val, bw40_delta_5g);
353 val = get_unaligned_le32(eeprom + 0x122);
354 dev->ee->tx_pwr_cfg_5g[1][0] = val;
355 dev->ee->tx_pwr_cfg_5g[1][1] = calc_bw40_power_rate(val, bw40_delta_5g);
357 val = get_unaligned_le16(eeprom + 0x126);
358 dev->ee->tx_pwr_cfg_5g[2][0] = val;
359 dev->ee->tx_pwr_cfg_5g[2][1] = calc_bw40_power_rate(val, bw40_delta_5g);
361 val = get_unaligned_le16(eeprom + 0xec);
363 dev->ee->tx_pwr_cfg_5g[3][0] = val;
364 dev->ee->tx_pwr_cfg_5g[3][1] = calc_bw40_power_rate(val, bw40_delta_5g);
366 val = get_unaligned_le16(eeprom + 0xee);
367 dev->ee->tx_pwr_cfg_5g[4][0] = val;
368 dev->ee->tx_pwr_cfg_5g[4][1] = calc_bw40_power_rate(val, bw40_delta_5g);
372 mt76x0_set_tx_power_per_chan(struct mt76x0_dev *dev, u8 *eeprom)
377 for (i = 0; i < 14; i++) {
378 tx_pwr = eeprom[MT_EE_TX_POWER_OFFSET_2GHZ + i];
379 if (tx_pwr <= 0x3f && tx_pwr > 0)
380 dev->ee->tx_pwr_per_chan[i] = tx_pwr;
382 dev->ee->tx_pwr_per_chan[i] = 5;
385 for (i = 0; i < 40; i++) {
386 tx_pwr = eeprom[MT_EE_TX_POWER_OFFSET_5GHZ + i];
387 if (tx_pwr <= 0x3f && tx_pwr > 0)
388 dev->ee->tx_pwr_per_chan[14 + i] = tx_pwr;
390 dev->ee->tx_pwr_per_chan[14 + i] = 5;
393 dev->ee->tx_pwr_per_chan[54] = dev->ee->tx_pwr_per_chan[22];
394 dev->ee->tx_pwr_per_chan[55] = dev->ee->tx_pwr_per_chan[28];
395 dev->ee->tx_pwr_per_chan[56] = dev->ee->tx_pwr_per_chan[34];
396 dev->ee->tx_pwr_per_chan[57] = dev->ee->tx_pwr_per_chan[44];
400 mt76x0_eeprom_init(struct mt76x0_dev *dev)
405 ret = mt76x0_efuse_physical_size_check(dev);
409 dev->ee = devm_kzalloc(dev->mt76.dev, sizeof(*dev->ee), GFP_KERNEL);
413 eeprom = kmalloc(MT76X0_EEPROM_SIZE, GFP_KERNEL);
417 for (i = 0; i + 16 <= MT76X0_EEPROM_SIZE; i += 16) {
418 ret = mt76x0_efuse_read(dev, i, eeprom + i, MT_EE_READ);
423 if (eeprom[MT_EE_VERSION_EE] > MT76X0U_EE_MAX_VER)
424 dev_warn(dev->mt76.dev,
425 "Warning: unsupported EEPROM version %02hhx\n",
426 eeprom[MT_EE_VERSION_EE]);
427 dev_info(dev->mt76.dev, "EEPROM ver:%02hhx fae:%02hhx\n",
428 eeprom[MT_EE_VERSION_EE], eeprom[MT_EE_VERSION_FAE]);
430 mt76x0_set_macaddr(dev, eeprom);
431 mt76x0_set_chip_cap(dev, eeprom);
432 mt76x0_set_country_reg(dev, eeprom);
433 mt76x0_set_rf_freq_off(dev, eeprom);
434 mt76x0_set_temp_offset(dev, eeprom);
435 mt76x0_set_lna_gain(dev, eeprom);
436 mt76x0_set_rssi_offset(dev, eeprom);
437 dev->chainmask = 0x0101;
439 mt76x0_set_tx_power_per_rate(dev, eeprom);
440 mt76x0_set_tx_power_per_chan(dev, eeprom);