Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / wireless / mediatek / mt7601u / eeprom.c
blobc868582c5d225c8b3e1b4d757c636b75f38c34e1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
5 */
7 #include <linux/of.h>
8 #include <linux/mtd/mtd.h>
9 #include <linux/mtd/partitions.h>
10 #include <linux/etherdevice.h>
11 #include <asm/unaligned.h>
12 #include "mt7601u.h"
13 #include "eeprom.h"
14 #include "mac.h"
16 static bool
17 field_valid(u8 val)
19 return val != 0xff;
22 static s8
23 field_validate(u8 val)
25 if (!field_valid(val))
26 return 0;
28 return val;
31 static int
32 mt7601u_efuse_read(struct mt7601u_dev *dev, u16 addr, u8 *data,
33 enum mt7601u_eeprom_access_modes mode)
35 u32 val;
36 int i;
38 val = mt76_rr(dev, MT_EFUSE_CTRL);
39 val &= ~(MT_EFUSE_CTRL_AIN |
40 MT_EFUSE_CTRL_MODE);
41 val |= FIELD_PREP(MT_EFUSE_CTRL_AIN, addr & ~0xf) |
42 FIELD_PREP(MT_EFUSE_CTRL_MODE, mode) |
43 MT_EFUSE_CTRL_KICK;
44 mt76_wr(dev, MT_EFUSE_CTRL, val);
46 if (!mt76_poll(dev, MT_EFUSE_CTRL, MT_EFUSE_CTRL_KICK, 0, 1000))
47 return -ETIMEDOUT;
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);
55 return 0;
58 for (i = 0; i < 4; i++) {
59 val = mt76_rr(dev, MT_EFUSE_DATA(i));
60 put_unaligned_le32(val, data + 4 * i);
63 return 0;
66 static int
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)];
71 int ret, i;
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);
77 if (ret)
78 return ret;
81 for (i = 0; i < MT_EFUSE_USAGE_MAP_SIZE; i++)
82 if (!data[i]) {
83 if (!start)
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");
91 return -EINVAL;
94 return 0;
97 static bool
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);
105 static void
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))
112 nic_conf1 &= 0xff00;
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)
118 dev_err(dev->dev,
119 "Error: this driver does not support HW RF ctrl\n");
121 if (!field_valid(nic_conf0 >> 8))
122 return;
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)
126 dev_err(dev->dev,
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",
137 trgt_pwr);
138 trgt_pwr = 0x20;
141 memset(dev->ee->chan_pwr, trgt_pwr, sizeof(dev->ee->chan_pwr));
144 static void
145 mt7601u_set_channel_power(struct mt7601u_dev *dev, u8 *eeprom)
147 u32 i, val;
148 u8 max_pwr;
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);
155 return;
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;
168 static void
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 */
179 { 1, 11 }, { 1, 14 }
181 u8 val = eeprom[MT_EE_COUNTRY_REGION];
182 int idx = -1;
184 if (val < 8)
185 idx = val;
186 if (val > 31 && val < 33)
187 idx = val - 32 + 8;
189 if (idx != -1)
190 dev_info(dev->dev,
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);
194 else
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)
204 static void
205 mt7601u_set_rf_freq_off(struct mt7601u_dev *dev, u8 *eeprom)
207 u8 comp;
209 dev->ee->rf_freq_off = field_validate(eeprom[MT_EE_FREQ_OFFSET]);
210 comp = field_validate(eeprom[MT_EE_FREQ_OFFSET_COMPENSATION]);
212 if (comp & BIT(7))
213 dev->ee->rf_freq_off -= comp & 0x7f;
214 else
215 dev->ee->rf_freq_off += comp;
218 static void
219 mt7601u_set_rssi_offset(struct mt7601u_dev *dev, u8 *eeprom)
221 int i;
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) {
228 dev_warn(dev->dev,
229 "Warning: EEPROM RSSI is invalid %02hhx\n",
230 rssi_offset[i]);
231 rssi_offset[i] = 0;
236 static void
237 mt7601u_extra_power_over_mac(struct mt7601u_dev *dev)
239 u32 val;
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);
249 static void
250 mt7601u_set_power_rate(struct power_per_rate *rate, s8 delta, u8 value)
252 /* Invalid? Note: vendor driver does not handle this */
253 if (value == 0xff)
254 return;
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;
262 static void
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;
267 switch (i) {
268 case 0:
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);
277 break;
278 case 1:
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);
283 break;
284 case 2:
285 mt7601u_set_power_rate(&t->ht[2], delta, (val >> 0) & 0xff);
286 mt7601u_set_power_rate(&t->ht[3], delta, (val >> 8) & 0xff);
287 break;
291 static s8
292 get_delta(u8 val)
294 s8 ret;
296 if (!field_valid(val) || !(val & BIT(7)))
297 return 0;
299 ret = val & 0x1f;
300 if (ret > 8)
301 ret = 8;
302 if (val & BIT(6))
303 ret = -ret;
305 return ret;
308 static void
309 mt7601u_config_tx_power_per_rate(struct mt7601u_dev *dev, u8 *eeprom)
311 u32 val;
312 s8 bw40_delta;
313 int i;
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);
322 if (~val)
323 mt7601u_wr(dev, MT_TX_PWR_CFG_0 + i * 4, val);
326 mt7601u_extra_power_over_mac(dev);
329 static void
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)
335 return;
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)
347 u8 *eeprom;
348 int i, ret;
350 ret = mt7601u_efuse_physical_size_check(dev);
351 if (ret)
352 return ret;
354 dev->ee = devm_kzalloc(dev->dev, sizeof(*dev->ee), GFP_KERNEL);
355 if (!dev->ee)
356 return -ENOMEM;
358 eeprom = kmalloc(MT7601U_EEPROM_SIZE, GFP_KERNEL);
359 if (!eeprom)
360 return -ENOMEM;
362 for (i = 0; i + 16 <= MT7601U_EEPROM_SIZE; i += 16) {
363 ret = mt7601u_efuse_read(dev, i, eeprom + i, MT_EE_READ);
364 if (ret)
365 goto out;
368 if (eeprom[MT_EE_VERSION_EE] > MT7601U_EE_MAX_VER)
369 dev_warn(dev->dev,
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);
387 out:
388 kfree(eeprom);
389 return ret;