2 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
18 #include <linux/etherdevice.h>
19 #include <asm/unaligned.h>
31 field_validate(u8 val
)
33 if (!field_valid(val
))
40 mt7601u_efuse_read(struct mt7601u_dev
*dev
, u16 addr
, u8
*data
,
41 enum mt7601u_eeprom_access_modes mode
)
46 val
= mt76_rr(dev
, MT_EFUSE_CTRL
);
47 val
&= ~(MT_EFUSE_CTRL_AIN
|
49 val
|= FIELD_PREP(MT_EFUSE_CTRL_AIN
, addr
& ~0xf) |
50 FIELD_PREP(MT_EFUSE_CTRL_MODE
, mode
) |
52 mt76_wr(dev
, MT_EFUSE_CTRL
, val
);
54 if (!mt76_poll(dev
, MT_EFUSE_CTRL
, MT_EFUSE_CTRL_KICK
, 0, 1000))
57 val
= mt76_rr(dev
, MT_EFUSE_CTRL
);
58 if ((val
& MT_EFUSE_CTRL_AOUT
) == MT_EFUSE_CTRL_AOUT
) {
59 /* Parts of eeprom not in the usage map (0x80-0xc0,0xf0)
60 * will not return valid data but it's ok.
62 memset(data
, 0xff, 16);
66 for (i
= 0; i
< 4; i
++) {
67 val
= mt76_rr(dev
, MT_EFUSE_DATA(i
));
68 put_unaligned_le32(val
, data
+ 4 * i
);
75 mt7601u_efuse_physical_size_check(struct mt7601u_dev
*dev
)
77 const int map_reads
= DIV_ROUND_UP(MT_EFUSE_USAGE_MAP_SIZE
, 16);
78 u8 data
[round_up(MT_EFUSE_USAGE_MAP_SIZE
, 16)];
80 u32 start
= 0, end
= 0, cnt_free
;
82 for (i
= 0; i
< map_reads
; i
++) {
83 ret
= mt7601u_efuse_read(dev
, MT_EE_USAGE_MAP_START
+ i
* 16,
84 data
+ i
* 16, MT_EE_PHYSICAL_READ
);
89 for (i
= 0; i
< MT_EFUSE_USAGE_MAP_SIZE
; i
++)
92 start
= MT_EE_USAGE_MAP_START
+ i
;
93 end
= MT_EE_USAGE_MAP_START
+ i
;
95 cnt_free
= end
- start
+ 1;
97 if (MT_EFUSE_USAGE_MAP_SIZE
- cnt_free
< 5) {
98 dev_err(dev
->dev
, "Error: your device needs default EEPROM file and this driver doesn't support it!\n");
106 mt7601u_has_tssi(struct mt7601u_dev
*dev
, u8
*eeprom
)
108 u16 nic_conf1
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_1
);
110 return ~nic_conf1
&& (nic_conf1
& MT_EE_NIC_CONF_1_TX_ALC_EN
);
114 mt7601u_set_chip_cap(struct mt7601u_dev
*dev
, u8
*eeprom
)
116 u16 nic_conf0
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_0
);
117 u16 nic_conf1
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_1
);
119 if (!field_valid(nic_conf1
& 0xff))
122 dev
->ee
->tssi_enabled
= mt7601u_has_tssi(dev
, eeprom
) &&
123 !(nic_conf1
& MT_EE_NIC_CONF_1_TEMP_TX_ALC
);
125 if (nic_conf1
& MT_EE_NIC_CONF_1_HW_RF_CTRL
)
127 "Error: this driver does not support HW RF ctrl\n");
129 if (!field_valid(nic_conf0
>> 8))
132 if (FIELD_GET(MT_EE_NIC_CONF_0_RX_PATH
, nic_conf0
) > 1 ||
133 FIELD_GET(MT_EE_NIC_CONF_0_TX_PATH
, nic_conf0
) > 1)
135 "Error: device has more than 1 RX/TX stream!\n");
138 static void mt7601u_set_channel_target_power(struct mt7601u_dev
*dev
,
139 u8
*eeprom
, u8 max_pwr
)
141 u8 trgt_pwr
= eeprom
[MT_EE_TX_TSSI_TARGET_POWER
];
143 if (trgt_pwr
> max_pwr
|| !trgt_pwr
) {
144 dev_warn(dev
->dev
, "Error: EEPROM trgt power invalid %hhx!\n",
149 memset(dev
->ee
->chan_pwr
, trgt_pwr
, sizeof(dev
->ee
->chan_pwr
));
153 mt7601u_set_channel_power(struct mt7601u_dev
*dev
, u8
*eeprom
)
158 val
= mt7601u_rr(dev
, MT_TX_ALC_CFG_0
);
159 max_pwr
= FIELD_GET(MT_TX_ALC_CFG_0_LIMIT_0
, val
);
161 if (mt7601u_has_tssi(dev
, eeprom
)) {
162 mt7601u_set_channel_target_power(dev
, eeprom
, max_pwr
);
166 for (i
= 0; i
< 14; i
++) {
167 s8 power
= field_validate(eeprom
[MT_EE_TX_POWER_OFFSET
+ i
]);
169 if (power
> max_pwr
|| power
< 0)
170 power
= MT7601U_DEFAULT_TX_POWER
;
172 dev
->ee
->chan_pwr
[i
] = power
;
177 mt7601u_set_country_reg(struct mt7601u_dev
*dev
, u8
*eeprom
)
179 /* Note: - region 31 is not valid for mt7601u (see rtmp_init.c)
180 * - comments in rtmp_def.h are incorrect (see rt_channel.c)
182 static const struct reg_channel_bounds chan_bounds
[] = {
183 /* EEPROM country regions 0 - 7 */
184 { 1, 11 }, { 1, 13 }, { 10, 2 }, { 10, 4 },
185 { 14, 1 }, { 1, 14 }, { 3, 7 }, { 5, 9 },
186 /* EEPROM country regions 32 - 33 */
189 u8 val
= eeprom
[MT_EE_COUNTRY_REGION
];
194 if (val
> 31 && val
< 33)
199 "EEPROM country region %02hhx (channels %hhd-%hhd)\n",
200 val
, chan_bounds
[idx
].start
,
201 chan_bounds
[idx
].start
+ chan_bounds
[idx
].num
- 1);
203 idx
= 5; /* channels 1 - 14 */
205 dev
->ee
->reg
= chan_bounds
[idx
];
207 /* TODO: country region 33 is special - phy should be set to B-mode
208 * before entering channel 14 (see sta/connect.c)
213 mt7601u_set_rf_freq_off(struct mt7601u_dev
*dev
, u8
*eeprom
)
217 dev
->ee
->rf_freq_off
= field_validate(eeprom
[MT_EE_FREQ_OFFSET
]);
218 comp
= field_validate(eeprom
[MT_EE_FREQ_OFFSET_COMPENSATION
]);
221 dev
->ee
->rf_freq_off
-= comp
& 0x7f;
223 dev
->ee
->rf_freq_off
+= comp
;
227 mt7601u_set_rssi_offset(struct mt7601u_dev
*dev
, u8
*eeprom
)
230 s8
*rssi_offset
= dev
->ee
->rssi_offset
;
232 for (i
= 0; i
< 2; i
++) {
233 rssi_offset
[i
] = eeprom
[MT_EE_RSSI_OFFSET
+ i
];
235 if (rssi_offset
[i
] < -10 || rssi_offset
[i
] > 10) {
237 "Warning: EEPROM RSSI is invalid %02hhx\n",
245 mt7601u_extra_power_over_mac(struct mt7601u_dev
*dev
)
249 val
= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_1
) & 0x0000ff00) >> 8);
250 val
|= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_2
) & 0x0000ff00) << 8);
251 mt7601u_wr(dev
, MT_TX_PWR_CFG_7
, val
);
253 val
= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_4
) & 0x0000ff00) >> 8);
254 mt7601u_wr(dev
, MT_TX_PWR_CFG_9
, val
);
258 mt7601u_set_power_rate(struct power_per_rate
*rate
, s8 delta
, u8 value
)
260 /* Invalid? Note: vendor driver does not handle this */
264 rate
->raw
= s6_validate(value
);
265 rate
->bw20
= s6_to_int(value
);
266 /* Note: vendor driver does cap the value to s6 right away */
267 rate
->bw40
= rate
->bw20
+ delta
;
271 mt7601u_save_power_rate(struct mt7601u_dev
*dev
, s8 delta
, u32 val
, int i
)
273 struct mt7601u_rate_power
*t
= &dev
->ee
->power_rate_table
;
277 mt7601u_set_power_rate(&t
->cck
[0], delta
, (val
>> 0) & 0xff);
278 mt7601u_set_power_rate(&t
->cck
[1], delta
, (val
>> 8) & 0xff);
279 /* Save cck bw20 for fixups of channel 14 */
280 dev
->ee
->real_cck_bw20
[0] = t
->cck
[0].bw20
;
281 dev
->ee
->real_cck_bw20
[1] = t
->cck
[1].bw20
;
283 mt7601u_set_power_rate(&t
->ofdm
[0], delta
, (val
>> 16) & 0xff);
284 mt7601u_set_power_rate(&t
->ofdm
[1], delta
, (val
>> 24) & 0xff);
287 mt7601u_set_power_rate(&t
->ofdm
[2], delta
, (val
>> 0) & 0xff);
288 mt7601u_set_power_rate(&t
->ofdm
[3], delta
, (val
>> 8) & 0xff);
289 mt7601u_set_power_rate(&t
->ht
[0], delta
, (val
>> 16) & 0xff);
290 mt7601u_set_power_rate(&t
->ht
[1], delta
, (val
>> 24) & 0xff);
293 mt7601u_set_power_rate(&t
->ht
[2], delta
, (val
>> 0) & 0xff);
294 mt7601u_set_power_rate(&t
->ht
[3], delta
, (val
>> 8) & 0xff);
304 if (!field_valid(val
) || !(val
& BIT(7)))
317 mt7601u_config_tx_power_per_rate(struct mt7601u_dev
*dev
, u8
*eeprom
)
323 bw40_delta
= get_delta(eeprom
[MT_EE_TX_POWER_DELTA_BW40
]);
325 for (i
= 0; i
< 5; i
++) {
326 val
= get_unaligned_le32(eeprom
+ MT_EE_TX_POWER_BYRATE(i
));
328 mt7601u_save_power_rate(dev
, bw40_delta
, val
, i
);
331 mt7601u_wr(dev
, MT_TX_PWR_CFG_0
+ i
* 4, val
);
334 mt7601u_extra_power_over_mac(dev
);
338 mt7601u_init_tssi_params(struct mt7601u_dev
*dev
, u8
*eeprom
)
340 struct tssi_data
*d
= &dev
->ee
->tssi_data
;
342 if (!dev
->ee
->tssi_enabled
)
345 d
->slope
= eeprom
[MT_EE_TX_TSSI_SLOPE
];
346 d
->tx0_delta_offset
= eeprom
[MT_EE_TX_TSSI_OFFSET
] * 1024;
347 d
->offset
[0] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
];
348 d
->offset
[1] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
+ 1];
349 d
->offset
[2] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
+ 2];
353 mt7601u_eeprom_init(struct mt7601u_dev
*dev
)
358 ret
= mt7601u_efuse_physical_size_check(dev
);
362 dev
->ee
= devm_kzalloc(dev
->dev
, sizeof(*dev
->ee
), GFP_KERNEL
);
366 eeprom
= kmalloc(MT7601U_EEPROM_SIZE
, GFP_KERNEL
);
370 for (i
= 0; i
+ 16 <= MT7601U_EEPROM_SIZE
; i
+= 16) {
371 ret
= mt7601u_efuse_read(dev
, i
, eeprom
+ i
, MT_EE_READ
);
376 if (eeprom
[MT_EE_VERSION_EE
] > MT7601U_EE_MAX_VER
)
378 "Warning: unsupported EEPROM version %02hhx\n",
379 eeprom
[MT_EE_VERSION_EE
]);
380 dev_info(dev
->dev
, "EEPROM ver:%02hhx fae:%02hhx\n",
381 eeprom
[MT_EE_VERSION_EE
], eeprom
[MT_EE_VERSION_FAE
]);
383 mt7601u_set_macaddr(dev
, eeprom
+ MT_EE_MAC_ADDR
);
384 mt7601u_set_chip_cap(dev
, eeprom
);
385 mt7601u_set_channel_power(dev
, eeprom
);
386 mt7601u_set_country_reg(dev
, eeprom
);
387 mt7601u_set_rf_freq_off(dev
, eeprom
);
388 mt7601u_set_rssi_offset(dev
, eeprom
);
389 dev
->ee
->ref_temp
= eeprom
[MT_EE_REF_TEMP
];
390 dev
->ee
->lna_gain
= eeprom
[MT_EE_LNA_GAIN
];
392 mt7601u_config_tx_power_per_rate(dev
, eeprom
);
394 mt7601u_init_tssi_params(dev
, eeprom
);