eeprom.c 8.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
S
Stanislaw Gruszka 已提交
2 3 4 5 6 7
/*
 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
 */

8
#include <linux/module.h>
S
Stanislaw Gruszka 已提交
9 10 11 12 13 14 15
#include <linux/of.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/etherdevice.h>
#include <asm/unaligned.h>
#include "mt76x0.h"
#include "eeprom.h"
16
#include "../mt76x02_phy.h"
S
Stanislaw Gruszka 已提交
17

K
Kees Cook 已提交
18
#define MT_MAP_READS	DIV_ROUND_UP(MT_EFUSE_USAGE_MAP_SIZE, 16)
S
Stanislaw Gruszka 已提交
19
static int
20
mt76x0_efuse_physical_size_check(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
21
{
K
Kees Cook 已提交
22
	u8 data[MT_MAP_READS * 16];
S
Stanislaw Gruszka 已提交
23 24 25
	int ret, i;
	u32 start = 0, end = 0, cnt_free;

26 27
	ret = mt76x02_get_efuse_data(dev, MT_EE_USAGE_MAP_START, data,
				     sizeof(data), MT_EE_PHYSICAL_READ);
28 29
	if (ret)
		return ret;
S
Stanislaw Gruszka 已提交
30 31 32 33 34 35 36 37 38 39

	for (i = 0; i < MT_EFUSE_USAGE_MAP_SIZE; i++)
		if (!data[i]) {
			if (!start)
				start = MT_EE_USAGE_MAP_START + i;
			end = MT_EE_USAGE_MAP_START + i;
		}
	cnt_free = end - start + 1;

	if (MT_EFUSE_USAGE_MAP_SIZE - cnt_free < 5) {
40 41
		dev_err(dev->mt76.dev,
			"driver does not support default EEPROM\n");
S
Stanislaw Gruszka 已提交
42 43 44 45 46 47
		return -EINVAL;
	}

	return 0;
}

48
static void mt76x0_set_chip_cap(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
49
{
50 51
	u16 nic_conf0 = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_0);
	u16 nic_conf1 = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_1);
S
Stanislaw Gruszka 已提交
52

53
	mt76x02_eeprom_parse_hw_cap(dev);
54
	dev_dbg(dev->mt76.dev, "2GHz %d 5GHz %d\n",
55
		dev->mt76.cap.has_2ghz, dev->mt76.cap.has_5ghz);
S
Stanislaw Gruszka 已提交
56

57 58 59 60 61
	if (dev->no_2ghz) {
		dev->mt76.cap.has_2ghz = false;
		dev_dbg(dev->mt76.dev, "mask out 2GHz support\n");
	}

62 63 64 65 66
	if (is_mt7630(dev)) {
		dev->mt76.cap.has_5ghz = false;
		dev_dbg(dev->mt76.dev, "mask out 5GHz support\n");
	}

67
	if (!mt76x02_field_valid(nic_conf1 & 0xff))
S
Stanislaw Gruszka 已提交
68 69 70 71
		nic_conf1 &= 0xff00;

	if (nic_conf1 & MT_EE_NIC_CONF_1_HW_RF_CTRL)
		dev_err(dev->mt76.dev,
72
			"driver does not support HW RF ctrl\n");
S
Stanislaw Gruszka 已提交
73

74
	if (!mt76x02_field_valid(nic_conf0 >> 8))
S
Stanislaw Gruszka 已提交
75 76 77 78
		return;

	if (FIELD_GET(MT_EE_NIC_CONF_0_RX_PATH, nic_conf0) > 1 ||
	    FIELD_GET(MT_EE_NIC_CONF_0_TX_PATH, nic_conf0) > 1)
79
		dev_err(dev->mt76.dev, "invalid tx-rx stream\n");
S
Stanislaw Gruszka 已提交
80 81
}

82
static void mt76x0_set_temp_offset(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
83
{
84
	u8 val;
S
Stanislaw Gruszka 已提交
85

86
	val = mt76x02_eeprom_get(dev, MT_EE_2G_TARGET_POWER) >> 8;
87
	if (mt76x02_field_valid(val))
88
		dev->cal.rx.temp_offset = mt76x02_sign_extend(val, 8);
S
Stanislaw Gruszka 已提交
89
	else
90
		dev->cal.rx.temp_offset = -10;
S
Stanislaw Gruszka 已提交
91 92
}

93
static void mt76x0_set_freq_offset(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
94
{
95
	struct mt76x02_rx_freq_cal *caldata = &dev->cal.rx;
96
	u8 val;
S
Stanislaw Gruszka 已提交
97

98
	val = mt76x02_eeprom_get(dev, MT_EE_FREQ_OFFSET);
99 100 101
	if (!mt76x02_field_valid(val))
		val = 0;
	caldata->freq_offset = val;
102

103
	val = mt76x02_eeprom_get(dev, MT_EE_TSSI_BOUND4) >> 8;
104 105
	if (!mt76x02_field_valid(val))
		val = 0;
S
Stanislaw Gruszka 已提交
106

107
	caldata->freq_offset -= mt76x02_sign_extend(val, 8);
S
Stanislaw Gruszka 已提交
108 109
}

110
void mt76x0_read_rx_gain(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
111
{
L
Lorenzo Bianconi 已提交
112
	struct ieee80211_channel *chan = dev->mt76.chandef.chan;
113
	struct mt76x02_rx_freq_cal *caldata = &dev->cal.rx;
114
	s8 val, lna_5g[3], lna_2g;
L
Lorenzo Bianconi 已提交
115
	u16 rssi_offset;
116
	int i;
S
Stanislaw Gruszka 已提交
117

118 119
	mt76x02_get_rx_gain(dev, chan->band, &rssi_offset, &lna_2g, lna_5g);
	caldata->lna_gain = mt76x02_get_lna_gain(dev, &lna_2g, lna_5g, chan);
S
Stanislaw Gruszka 已提交
120

121 122 123 124
	for (i = 0; i < ARRAY_SIZE(caldata->rssi_offset); i++) {
		val = rssi_offset >> (8 * i);
		if (val < -10 || val > 10)
			val = 0;
S
Stanislaw Gruszka 已提交
125

126
		caldata->rssi_offset[i] = val;
S
Stanislaw Gruszka 已提交
127 128 129
	}
}

130
static s8 mt76x0_get_delta(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
131
{
132
	struct cfg80211_chan_def *chandef = &dev->mt76.chandef;
133
	u8 val;
S
Stanislaw Gruszka 已提交
134

135 136 137 138
	if (chandef->width == NL80211_CHAN_WIDTH_80) {
		val = mt76x02_eeprom_get(dev, MT_EE_5G_TARGET_POWER) >> 8;
	} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
		u16 data;
S
Stanislaw Gruszka 已提交
139

140 141 142 143 144 145
		data = mt76x02_eeprom_get(dev, MT_EE_TX_POWER_DELTA_BW40);
		if (chandef->chan->band == NL80211_BAND_5GHZ)
			val = data >> 8;
		else
			val = data;
	} else {
S
Stanislaw Gruszka 已提交
146
		return 0;
147
	}
S
Stanislaw Gruszka 已提交
148

149
	return mt76x02_rate_power_val(val);
S
Stanislaw Gruszka 已提交
150 151
}

152 153 154
void mt76x0_get_tx_power_per_rate(struct mt76x02_dev *dev,
				  struct ieee80211_channel *chan,
				  struct mt76_rate_power *t)
S
Stanislaw Gruszka 已提交
155
{
156 157
	bool is_2ghz = chan->band == NL80211_BAND_2GHZ;
	u16 val, addr;
158
	s8 delta;
159 160 161 162

	memset(t, 0, sizeof(*t));

	/* cck 1M, 2M, 5.5M, 11M */
163
	val = mt76x02_eeprom_get(dev, MT_EE_TX_POWER_BYRATE_BASE);
164 165 166 167 168
	t->cck[0] = t->cck[1] = s6_to_s8(val);
	t->cck[2] = t->cck[3] = s6_to_s8(val >> 8);

	/* ofdm 6M, 9M, 12M, 18M */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 2 : 0x120;
169
	val = mt76x02_eeprom_get(dev, addr);
170 171 172 173 174
	t->ofdm[0] = t->ofdm[1] = s6_to_s8(val);
	t->ofdm[2] = t->ofdm[3] = s6_to_s8(val >> 8);

	/* ofdm 24M, 36M, 48M, 54M */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 4 : 0x122;
175
	val = mt76x02_eeprom_get(dev, addr);
176 177 178 179 180
	t->ofdm[4] = t->ofdm[5] = s6_to_s8(val);
	t->ofdm[6] = t->ofdm[7] = s6_to_s8(val >> 8);

	/* ht-vht mcs 1ss 0, 1, 2, 3 */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 6 : 0x124;
181
	val = mt76x02_eeprom_get(dev, addr);
182 183 184 185 186
	t->ht[0] = t->ht[1] = t->vht[0] = t->vht[1] = s6_to_s8(val);
	t->ht[2] = t->ht[3] = t->vht[2] = t->vht[3] = s6_to_s8(val >> 8);

	/* ht-vht mcs 1ss 4, 5, 6 */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 8 : 0x126;
187
	val = mt76x02_eeprom_get(dev, addr);
188
	t->ht[4] = t->ht[5] = t->vht[4] = t->vht[5] = s6_to_s8(val);
189
	t->ht[6] = t->ht[7] = t->vht[6] = t->vht[7] = s6_to_s8(val >> 8);
190 191 192

	/* ht-vht mcs 1ss 0, 1, 2, 3 stbc */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 14 : 0xec;
193
	val = mt76x02_eeprom_get(dev, addr);
194 195 196 197 198
	t->stbc[0] = t->stbc[1] = s6_to_s8(val);
	t->stbc[2] = t->stbc[3] = s6_to_s8(val >> 8);

	/* ht-vht mcs 1ss 4, 5, 6 stbc */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 16 : 0xee;
199
	val = mt76x02_eeprom_get(dev, addr);
200 201 202 203
	t->stbc[4] = t->stbc[5] = s6_to_s8(val);
	t->stbc[6] = t->stbc[7] = s6_to_s8(val >> 8);

	/* vht mcs 8, 9 5GHz */
204
	val = mt76x02_eeprom_get(dev, 0x132);
205 206
	t->vht[8] = s6_to_s8(val);
	t->vht[9] = s6_to_s8(val >> 8);
207

208
	delta = mt76x0_tssi_enabled(dev) ? 0 : mt76x0_get_delta(dev);
209
	mt76x02_add_rate_power_offset(t, delta);
S
Stanislaw Gruszka 已提交
210 211
}

212 213
void mt76x0_get_power_info(struct mt76x02_dev *dev,
			   struct ieee80211_channel *chan, s8 *tp)
S
Stanislaw Gruszka 已提交
214
{
215
	static const struct mt76x0_chan_map {
216 217 218
		u8 chan;
		u8 offset;
	} chan_map[] = {
219 220 221 222 223 224 225
		{   2,  0 }, {   4,  2 }, {   6,  4 }, {   8,  6 },
		{  10,  8 }, {  12, 10 }, {  14, 12 }, {  38,  0 },
		{  44,  2 }, {  48,  4 }, {  54,  6 }, {  60,  8 },
		{  64, 10 }, { 102, 12 }, { 108, 14 }, { 112, 16 },
		{ 118, 18 }, { 124, 20 }, { 128, 22 }, { 134, 24 },
		{ 140, 26 }, { 151, 28 }, { 157, 30 }, { 161, 32 },
		{ 167, 34 }, { 171, 36 }, { 175, 38 },
226 227
	};
	u8 offset, addr;
228
	int i, idx = 0;
229
	u16 data;
S
Stanislaw Gruszka 已提交
230

231 232 233 234 235 236 237 238
	if (mt76x0_tssi_enabled(dev)) {
		s8 target_power;

		if (chan->band == NL80211_BAND_5GHZ)
			data = mt76x02_eeprom_get(dev, MT_EE_5G_TARGET_POWER);
		else
			data = mt76x02_eeprom_get(dev, MT_EE_2G_TARGET_POWER);
		target_power = (data & 0xff) - dev->mt76.rate_power.ofdm[7];
239
		*tp = target_power + mt76x0_get_delta(dev);
240 241 242 243

		return;
	}

244
	for (i = 0; i < ARRAY_SIZE(chan_map); i++) {
245 246
		if (chan->hw_value <= chan_map[i].chan) {
			idx = (chan->hw_value == chan_map[i].chan);
247 248 249
			offset = chan_map[i].offset;
			break;
		}
S
Stanislaw Gruszka 已提交
250
	}
251 252
	if (i == ARRAY_SIZE(chan_map))
		offset = chan_map[0].offset;
S
Stanislaw Gruszka 已提交
253

254 255 256 257
	if (chan->band == NL80211_BAND_2GHZ) {
		addr = MT_EE_TX_POWER_DELTA_BW80 + offset;
	} else {
		switch (chan->hw_value) {
258 259 260
		case 42:
			offset = 2;
			break;
261 262 263 264 265 266
		case 58:
			offset = 8;
			break;
		case 106:
			offset = 14;
			break;
267
		case 122:
268 269 270 271 272 273 274 275 276
			offset = 20;
			break;
		case 155:
			offset = 30;
			break;
		default:
			break;
		}
		addr = MT_EE_TX_POWER_0_GRP4_TSSI_SLOPE + 2 + offset;
S
Stanislaw Gruszka 已提交
277 278
	}

279
	data = mt76x02_eeprom_get(dev, addr);
280 281 282
	*tp = data >> (8 * idx);
	if (*tp < 0 || *tp > 0x3f)
		*tp = 5;
S
Stanislaw Gruszka 已提交
283 284
}

285
static int mt76x0_check_eeprom(struct mt76x02_dev *dev)
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
{
	u16 val;

	val = get_unaligned_le16(dev->mt76.eeprom.data);
	if (!val)
		val = get_unaligned_le16(dev->mt76.eeprom.data +
					 MT_EE_PCI_ID);

	switch (val) {
	case 0x7650:
	case 0x7610:
		return 0;
	default:
		dev_err(dev->mt76.dev, "EEPROM data check failed: %04x\n",
			val);
		return -EINVAL;
	}
}

305
static int mt76x0_load_eeprom(struct mt76x02_dev *dev)
306 307 308 309 310 311 312 313 314 315 316 317 318 319
{
	int found;

	found = mt76_eeprom_init(&dev->mt76, MT76X0_EEPROM_SIZE);
	if (found < 0)
		return found;

	if (found && !mt76x0_check_eeprom(dev))
		return 0;

	found = mt76x0_efuse_physical_size_check(dev);
	if (found < 0)
		return found;

320
	return mt76x02_get_efuse_data(dev, 0, dev->mt76.eeprom.data,
321 322 323
				      MT76X0_EEPROM_SIZE, MT_EE_READ);
}

324
int mt76x0_eeprom_init(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
325
{
326 327
	u8 version, fae;
	u16 data;
328
	int err;
S
Stanislaw Gruszka 已提交
329

330 331 332
	err = mt76x0_load_eeprom(dev);
	if (err < 0)
		return err;
333

334
	data = mt76x02_eeprom_get(dev, MT_EE_VERSION);
335 336
	version = data >> 8;
	fae = data;
S
Stanislaw Gruszka 已提交
337

338
	if (version > MT76X0U_EE_MAX_VER)
S
Stanislaw Gruszka 已提交
339 340
		dev_warn(dev->mt76.dev,
			 "Warning: unsupported EEPROM version %02hhx\n",
341
			 version);
S
Stanislaw Gruszka 已提交
342
	dev_info(dev->mt76.dev, "EEPROM ver:%02hhx fae:%02hhx\n",
343
		 version, fae);
S
Stanislaw Gruszka 已提交
344

345 346
	memcpy(dev->mt76.macaddr, (u8 *)dev->mt76.eeprom.data + MT_EE_MAC_ADDR,
	       ETH_ALEN);
347
	mt76_eeprom_override(&dev->mt76);
348 349
	mt76x02_mac_setaddr(dev, dev->mt76.macaddr);

350
	mt76x0_set_chip_cap(dev);
351
	mt76x0_set_freq_offset(dev);
352
	mt76x0_set_temp_offset(dev);
S
Stanislaw Gruszka 已提交
353

354
	return 0;
S
Stanislaw Gruszka 已提交
355
}
356 357

MODULE_LICENSE("Dual BSD/GPL");