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>
30 field_validate(u8 val
)
32 if (!field_valid(val
))
39 mt7601u_efuse_read(struct mt7601u_dev
*dev
, u16 addr
, u8
*data
,
40 enum mt7601u_eeprom_access_modes mode
)
45 val
= mt76_rr(dev
, MT_EFUSE_CTRL
);
46 val
&= ~(MT_EFUSE_CTRL_AIN
|
48 val
|= MT76_SET(MT_EFUSE_CTRL_AIN
, addr
& ~0xf) |
49 MT76_SET(MT_EFUSE_CTRL_MODE
, mode
) |
51 mt76_wr(dev
, MT_EFUSE_CTRL
, val
);
53 if (!mt76_poll(dev
, MT_EFUSE_CTRL
, MT_EFUSE_CTRL_KICK
, 0, 1000))
56 val
= mt76_rr(dev
, MT_EFUSE_CTRL
);
57 if ((val
& MT_EFUSE_CTRL_AOUT
) == MT_EFUSE_CTRL_AOUT
) {
58 /* Parts of eeprom not in the usage map (0x80-0xc0,0xf0)
59 * will not return valid data but it's ok.
61 memset(data
, 0xff, 16);
65 for (i
= 0; i
< 4; i
++) {
66 val
= mt76_rr(dev
, MT_EFUSE_DATA(i
));
67 put_unaligned_le32(val
, data
+ 4 * i
);
74 mt7601u_efuse_physical_size_check(struct mt7601u_dev
*dev
)
76 const int map_reads
= DIV_ROUND_UP(MT_EFUSE_USAGE_MAP_SIZE
, 16);
77 u8 data
[map_reads
* 16];
79 u32 start
= 0, end
= 0, cnt_free
;
81 for (i
= 0; i
< map_reads
; i
++) {
82 ret
= mt7601u_efuse_read(dev
, MT_EE_USAGE_MAP_START
+ i
* 16,
83 data
+ i
* 16, MT_EE_PHYSICAL_READ
);
88 for (i
= 0; i
< MT_EFUSE_USAGE_MAP_SIZE
; i
++)
91 start
= MT_EE_USAGE_MAP_START
+ i
;
92 end
= MT_EE_USAGE_MAP_START
+ i
;
94 cnt_free
= end
- start
+ 1;
96 if (MT_EFUSE_USAGE_MAP_SIZE
- cnt_free
< 5) {
97 dev_err(dev
->dev
, "Error: your device needs default EEPROM file and this driver doesn't support it!\n");
105 mt7601u_has_tssi(struct mt7601u_dev
*dev
, u8
*eeprom
)
107 u16 nic_conf1
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_1
);
109 return ~nic_conf1
&& (nic_conf1
& MT_EE_NIC_CONF_1_TX_ALC_EN
);
113 mt7601u_set_chip_cap(struct mt7601u_dev
*dev
, u8
*eeprom
)
115 u16 nic_conf0
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_0
);
116 u16 nic_conf1
= get_unaligned_le16(eeprom
+ MT_EE_NIC_CONF_1
);
118 if (!field_valid(nic_conf1
& 0xff))
121 dev
->ee
->tssi_enabled
= mt7601u_has_tssi(dev
, eeprom
) &&
122 !(nic_conf1
& MT_EE_NIC_CONF_1_TEMP_TX_ALC
);
124 if (nic_conf1
& MT_EE_NIC_CONF_1_HW_RF_CTRL
)
126 "Error: this driver does not support HW RF ctrl\n");
128 if (!field_valid(nic_conf0
>> 8))
131 if (MT76_GET(MT_EE_NIC_CONF_0_RX_PATH
, nic_conf0
) > 1 ||
132 MT76_GET(MT_EE_NIC_CONF_0_TX_PATH
, nic_conf0
) > 1)
134 "Error: device has more than 1 RX/TX stream!\n");
138 mt7601u_set_macaddr(struct mt7601u_dev
*dev
, const u8
*eeprom
)
140 const void *src
= eeprom
+ MT_EE_MAC_ADDR
;
142 ether_addr_copy(dev
->macaddr
, src
);
144 if (!is_valid_ether_addr(dev
->macaddr
)) {
145 eth_random_addr(dev
->macaddr
);
147 "Invalid MAC address, using random address %pM\n",
151 mt76_wr(dev
, MT_MAC_ADDR_DW0
, get_unaligned_le32(dev
->macaddr
));
152 mt76_wr(dev
, MT_MAC_ADDR_DW1
, get_unaligned_le16(dev
->macaddr
+ 4) |
153 MT76_SET(MT_MAC_ADDR_DW1_U2ME_MASK
, 0xff));
158 static void mt7601u_set_channel_target_power(struct mt7601u_dev
*dev
,
159 u8
*eeprom
, u8 max_pwr
)
161 u8 trgt_pwr
= eeprom
[MT_EE_TX_TSSI_TARGET_POWER
];
163 if (trgt_pwr
> max_pwr
|| !trgt_pwr
) {
164 dev_warn(dev
->dev
, "Error: EEPROM trgt power invalid %hhx!\n",
169 memset(dev
->ee
->chan_pwr
, trgt_pwr
, sizeof(dev
->ee
->chan_pwr
));
173 mt7601u_set_channel_power(struct mt7601u_dev
*dev
, u8
*eeprom
)
178 val
= mt7601u_rr(dev
, MT_TX_ALC_CFG_0
);
179 max_pwr
= MT76_GET(MT_TX_ALC_CFG_0_LIMIT_0
, val
);
181 if (mt7601u_has_tssi(dev
, eeprom
)) {
182 mt7601u_set_channel_target_power(dev
, eeprom
, max_pwr
);
186 for (i
= 0; i
< 14; i
++) {
187 s8 power
= field_validate(eeprom
[MT_EE_TX_POWER_OFFSET
+ i
]);
189 if (power
> max_pwr
|| power
< 0)
190 power
= MT7601U_DEFAULT_TX_POWER
;
192 dev
->ee
->chan_pwr
[i
] = power
;
197 mt7601u_set_country_reg(struct mt7601u_dev
*dev
, u8
*eeprom
)
199 /* Note: - region 31 is not valid for mt7601u (see rtmp_init.c)
200 * - comments in rtmp_def.h are incorrect (see rt_channel.c)
202 static const struct reg_channel_bounds chan_bounds
[] = {
203 /* EEPROM country regions 0 - 7 */
204 { 1, 11 }, { 1, 13 }, { 10, 2 }, { 10, 4 },
205 { 14, 1 }, { 1, 14 }, { 3, 7 }, { 5, 9 },
206 /* EEPROM country regions 32 - 33 */
209 u8 val
= eeprom
[MT_EE_COUNTRY_REGION
];
214 if (val
> 31 && val
< 33)
219 "EEPROM country region %02hhx (channels %hhd-%hhd)\n",
220 val
, chan_bounds
[idx
].start
,
221 chan_bounds
[idx
].start
+ chan_bounds
[idx
].num
- 1);
223 idx
= 5; /* channels 1 - 14 */
225 dev
->ee
->reg
= chan_bounds
[idx
];
227 /* TODO: country region 33 is special - phy should be set to B-mode
228 * before entering channel 14 (see sta/connect.c)
233 mt7601u_set_rf_freq_off(struct mt7601u_dev
*dev
, u8
*eeprom
)
237 dev
->ee
->rf_freq_off
= field_validate(eeprom
[MT_EE_FREQ_OFFSET
]);
238 comp
= field_validate(eeprom
[MT_EE_FREQ_OFFSET_COMPENSATION
]);
241 dev
->ee
->rf_freq_off
-= comp
& 0x7f;
243 dev
->ee
->rf_freq_off
+= comp
;
247 mt7601u_set_rssi_offset(struct mt7601u_dev
*dev
, u8
*eeprom
)
250 s8
*rssi_offset
= dev
->ee
->rssi_offset
;
252 for (i
= 0; i
< 2; i
++) {
253 rssi_offset
[i
] = eeprom
[MT_EE_RSSI_OFFSET
+ i
];
255 if (rssi_offset
[i
] < -10 || rssi_offset
[i
] > 10) {
257 "Warning: EEPROM RSSI is invalid %02hhx\n",
265 mt7601u_extra_power_over_mac(struct mt7601u_dev
*dev
)
269 val
= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_1
) & 0x0000ff00) >> 8);
270 val
|= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_2
) & 0x0000ff00) << 8);
271 mt7601u_wr(dev
, MT_TX_PWR_CFG_7
, val
);
273 val
= ((mt7601u_rr(dev
, MT_TX_PWR_CFG_4
) & 0x0000ff00) >> 8);
274 mt7601u_wr(dev
, MT_TX_PWR_CFG_9
, val
);
278 mt7601u_set_power_rate(struct power_per_rate
*rate
, s8 delta
, u8 value
)
280 rate
->raw
= s6_validate(value
);
281 rate
->bw20
= s6_to_int(value
);
282 /* Note: vendor driver does cap the value to s6 right away */
283 rate
->bw40
= rate
->bw20
+ delta
;
287 mt7601u_save_power_rate(struct mt7601u_dev
*dev
, s8 delta
, u32 val
, int i
)
289 struct mt7601u_rate_power
*t
= &dev
->ee
->power_rate_table
;
293 mt7601u_set_power_rate(&t
->cck
[0], delta
, (val
>> 0) & 0xff);
294 mt7601u_set_power_rate(&t
->cck
[1], delta
, (val
>> 8) & 0xff);
295 /* Save cck bw20 for fixups of channel 14 */
296 dev
->ee
->real_cck_bw20
[0] = t
->cck
[0].bw20
;
297 dev
->ee
->real_cck_bw20
[1] = t
->cck
[1].bw20
;
299 mt7601u_set_power_rate(&t
->ofdm
[0], delta
, (val
>> 16) & 0xff);
300 mt7601u_set_power_rate(&t
->ofdm
[1], delta
, (val
>> 24) & 0xff);
303 mt7601u_set_power_rate(&t
->ofdm
[2], delta
, (val
>> 0) & 0xff);
304 mt7601u_set_power_rate(&t
->ofdm
[3], delta
, (val
>> 8) & 0xff);
305 mt7601u_set_power_rate(&t
->ht
[0], delta
, (val
>> 16) & 0xff);
306 mt7601u_set_power_rate(&t
->ht
[1], delta
, (val
>> 24) & 0xff);
309 mt7601u_set_power_rate(&t
->ht
[2], delta
, (val
>> 0) & 0xff);
310 mt7601u_set_power_rate(&t
->ht
[3], delta
, (val
>> 8) & 0xff);
320 if (!field_valid(val
) || !(val
& BIT(7)))
333 mt7601u_config_tx_power_per_rate(struct mt7601u_dev
*dev
, u8
*eeprom
)
339 bw40_delta
= get_delta(eeprom
[MT_EE_TX_POWER_DELTA_BW40
]);
341 for (i
= 0; i
< 5; i
++) {
342 val
= get_unaligned_le32(eeprom
+ MT_EE_TX_POWER_BYRATE(i
));
344 mt7601u_save_power_rate(dev
, bw40_delta
, val
, i
);
347 mt7601u_wr(dev
, MT_TX_PWR_CFG_0
+ i
* 4, val
);
350 mt7601u_extra_power_over_mac(dev
);
354 mt7601u_init_tssi_params(struct mt7601u_dev
*dev
, u8
*eeprom
)
356 struct tssi_data
*d
= &dev
->ee
->tssi_data
;
358 if (!dev
->ee
->tssi_enabled
)
361 d
->slope
= eeprom
[MT_EE_TX_TSSI_SLOPE
];
362 d
->tx0_delta_offset
= eeprom
[MT_EE_TX_TSSI_OFFSET
] * 1024;
363 d
->offset
[0] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
];
364 d
->offset
[1] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
+ 1];
365 d
->offset
[2] = eeprom
[MT_EE_TX_TSSI_OFFSET_GROUP
+ 2];
369 mt7601u_eeprom_init(struct mt7601u_dev
*dev
)
374 ret
= mt7601u_efuse_physical_size_check(dev
);
378 dev
->ee
= devm_kzalloc(dev
->dev
, sizeof(*dev
->ee
), GFP_KERNEL
);
382 eeprom
= kmalloc(MT7601U_EEPROM_SIZE
, GFP_KERNEL
);
386 for (i
= 0; i
+ 16 <= MT7601U_EEPROM_SIZE
; i
+= 16) {
387 ret
= mt7601u_efuse_read(dev
, i
, eeprom
+ i
, MT_EE_READ
);
392 if (eeprom
[MT_EE_VERSION_EE
] > MT7601U_EE_MAX_VER
)
394 "Warning: unsupported EEPROM version %02hhx\n",
395 eeprom
[MT_EE_VERSION_EE
]);
396 dev_info(dev
->dev
, "EEPROM ver:%02hhx fae:%02hhx\n",
397 eeprom
[MT_EE_VERSION_EE
], eeprom
[MT_EE_VERSION_FAE
]);
399 mt7601u_set_macaddr(dev
, eeprom
);
400 mt7601u_set_chip_cap(dev
, eeprom
);
401 mt7601u_set_channel_power(dev
, eeprom
);
402 mt7601u_set_country_reg(dev
, eeprom
);
403 mt7601u_set_rf_freq_off(dev
, eeprom
);
404 mt7601u_set_rssi_offset(dev
, eeprom
);
405 dev
->ee
->ref_temp
= eeprom
[MT_EE_REF_TEMP
];
406 dev
->ee
->lna_gain
= eeprom
[MT_EE_LNA_GAIN
];
408 mt7601u_config_tx_power_per_rate(dev
, eeprom
);
410 mt7601u_init_tssi_params(dev
, eeprom
);