xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x0/eeprom.c (revision 1802d0beecafe581ad584634ba92f8a471d8a63a)
1*1802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e87b5039SStanislaw Gruszka /*
3e87b5039SStanislaw Gruszka  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4e87b5039SStanislaw Gruszka  * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
5e87b5039SStanislaw Gruszka  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
6e87b5039SStanislaw Gruszka  */
7e87b5039SStanislaw Gruszka 
8db6bb5c6SFelix Fietkau #include <linux/module.h>
9e87b5039SStanislaw Gruszka #include <linux/of.h>
10e87b5039SStanislaw Gruszka #include <linux/mtd/mtd.h>
11e87b5039SStanislaw Gruszka #include <linux/mtd/partitions.h>
12e87b5039SStanislaw Gruszka #include <linux/etherdevice.h>
13e87b5039SStanislaw Gruszka #include <asm/unaligned.h>
14e87b5039SStanislaw Gruszka #include "mt76x0.h"
15e87b5039SStanislaw Gruszka #include "eeprom.h"
16b37bbc8cSLorenzo Bianconi #include "../mt76x02_phy.h"
17e87b5039SStanislaw Gruszka 
1817ad18fdSKees Cook #define MT_MAP_READS	DIV_ROUND_UP(MT_EFUSE_USAGE_MAP_SIZE, 16)
19e87b5039SStanislaw Gruszka static int
20b2d871c0SLorenzo Bianconi mt76x0_efuse_physical_size_check(struct mt76x02_dev *dev)
21e87b5039SStanislaw Gruszka {
2217ad18fdSKees Cook 	u8 data[MT_MAP_READS * 16];
23e87b5039SStanislaw Gruszka 	int ret, i;
24e87b5039SStanislaw Gruszka 	u32 start = 0, end = 0, cnt_free;
25e87b5039SStanislaw Gruszka 
2626a9daa6SLorenzo Bianconi 	ret = mt76x02_get_efuse_data(dev, MT_EE_USAGE_MAP_START, data,
2726a9daa6SLorenzo Bianconi 				     sizeof(data), MT_EE_PHYSICAL_READ);
28e87b5039SStanislaw Gruszka 	if (ret)
29e87b5039SStanislaw Gruszka 		return ret;
30e87b5039SStanislaw Gruszka 
31e87b5039SStanislaw Gruszka 	for (i = 0; i < MT_EFUSE_USAGE_MAP_SIZE; i++)
32e87b5039SStanislaw Gruszka 		if (!data[i]) {
33e87b5039SStanislaw Gruszka 			if (!start)
34e87b5039SStanislaw Gruszka 				start = MT_EE_USAGE_MAP_START + i;
35e87b5039SStanislaw Gruszka 			end = MT_EE_USAGE_MAP_START + i;
36e87b5039SStanislaw Gruszka 		}
37e87b5039SStanislaw Gruszka 	cnt_free = end - start + 1;
38e87b5039SStanislaw Gruszka 
39e87b5039SStanislaw Gruszka 	if (MT_EFUSE_USAGE_MAP_SIZE - cnt_free < 5) {
40bd724b8fSLorenzo Bianconi 		dev_err(dev->mt76.dev,
41bd724b8fSLorenzo Bianconi 			"driver does not support default EEPROM\n");
42e87b5039SStanislaw Gruszka 		return -EINVAL;
43e87b5039SStanislaw Gruszka 	}
44e87b5039SStanislaw Gruszka 
45e87b5039SStanislaw Gruszka 	return 0;
46e87b5039SStanislaw Gruszka }
47e87b5039SStanislaw Gruszka 
48b2d871c0SLorenzo Bianconi static void mt76x0_set_chip_cap(struct mt76x02_dev *dev)
49e87b5039SStanislaw Gruszka {
5026a9daa6SLorenzo Bianconi 	u16 nic_conf0 = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_0);
5126a9daa6SLorenzo Bianconi 	u16 nic_conf1 = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_1);
52e87b5039SStanislaw Gruszka 
5326a9daa6SLorenzo Bianconi 	mt76x02_eeprom_parse_hw_cap(dev);
543d66939aSLorenzo Bianconi 	dev_dbg(dev->mt76.dev, "2GHz %d 5GHz %d\n",
55540399d5SLorenzo Bianconi 		dev->mt76.cap.has_2ghz, dev->mt76.cap.has_5ghz);
56e87b5039SStanislaw Gruszka 
57d6500cf3SStanislaw Gruszka 	if (dev->no_2ghz) {
58d6500cf3SStanislaw Gruszka 		dev->mt76.cap.has_2ghz = false;
59d6500cf3SStanislaw Gruszka 		dev_dbg(dev->mt76.dev, "mask out 2GHz support\n");
60d6500cf3SStanislaw Gruszka 	}
61d6500cf3SStanislaw Gruszka 
6286c71d3dSLorenzo Bianconi 	if (!mt76x02_field_valid(nic_conf1 & 0xff))
63e87b5039SStanislaw Gruszka 		nic_conf1 &= 0xff00;
64e87b5039SStanislaw Gruszka 
65e87b5039SStanislaw Gruszka 	if (nic_conf1 & MT_EE_NIC_CONF_1_HW_RF_CTRL)
66e87b5039SStanislaw Gruszka 		dev_err(dev->mt76.dev,
673d66939aSLorenzo Bianconi 			"driver does not support HW RF ctrl\n");
68e87b5039SStanislaw Gruszka 
6986c71d3dSLorenzo Bianconi 	if (!mt76x02_field_valid(nic_conf0 >> 8))
70e87b5039SStanislaw Gruszka 		return;
71e87b5039SStanislaw Gruszka 
72e87b5039SStanislaw Gruszka 	if (FIELD_GET(MT_EE_NIC_CONF_0_RX_PATH, nic_conf0) > 1 ||
73e87b5039SStanislaw Gruszka 	    FIELD_GET(MT_EE_NIC_CONF_0_TX_PATH, nic_conf0) > 1)
743d66939aSLorenzo Bianconi 		dev_err(dev->mt76.dev, "invalid tx-rx stream\n");
75e87b5039SStanislaw Gruszka }
76e87b5039SStanislaw Gruszka 
77b2d871c0SLorenzo Bianconi static void mt76x0_set_temp_offset(struct mt76x02_dev *dev)
78e87b5039SStanislaw Gruszka {
792c0db839SLorenzo Bianconi 	u8 val;
80e87b5039SStanislaw Gruszka 
8126a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, MT_EE_2G_TARGET_POWER) >> 8;
822c0db839SLorenzo Bianconi 	if (mt76x02_field_valid(val))
83b2d871c0SLorenzo Bianconi 		dev->cal.rx.temp_offset = mt76x02_sign_extend(val, 8);
84e87b5039SStanislaw Gruszka 	else
85b2d871c0SLorenzo Bianconi 		dev->cal.rx.temp_offset = -10;
86e87b5039SStanislaw Gruszka }
87e87b5039SStanislaw Gruszka 
88b2d871c0SLorenzo Bianconi static void mt76x0_set_freq_offset(struct mt76x02_dev *dev)
89e87b5039SStanislaw Gruszka {
90b2d871c0SLorenzo Bianconi 	struct mt76x02_rx_freq_cal *caldata = &dev->cal.rx;
9177d0f465SLorenzo Bianconi 	u8 val;
92e87b5039SStanislaw Gruszka 
9326a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, MT_EE_FREQ_OFFSET);
9477d0f465SLorenzo Bianconi 	if (!mt76x02_field_valid(val))
9577d0f465SLorenzo Bianconi 		val = 0;
9677d0f465SLorenzo Bianconi 	caldata->freq_offset = val;
9786c71d3dSLorenzo Bianconi 
9826a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, MT_EE_TSSI_BOUND4) >> 8;
9977d0f465SLorenzo Bianconi 	if (!mt76x02_field_valid(val))
10077d0f465SLorenzo Bianconi 		val = 0;
101e87b5039SStanislaw Gruszka 
10277d0f465SLorenzo Bianconi 	caldata->freq_offset -= mt76x02_sign_extend(val, 8);
103e87b5039SStanislaw Gruszka }
104e87b5039SStanislaw Gruszka 
105b2d871c0SLorenzo Bianconi void mt76x0_read_rx_gain(struct mt76x02_dev *dev)
106e87b5039SStanislaw Gruszka {
1072daa6758SLorenzo Bianconi 	struct ieee80211_channel *chan = dev->mt76.chandef.chan;
108b2d871c0SLorenzo Bianconi 	struct mt76x02_rx_freq_cal *caldata = &dev->cal.rx;
109564d7f0aSLorenzo Bianconi 	s8 val, lna_5g[3], lna_2g;
1102daa6758SLorenzo Bianconi 	u16 rssi_offset;
111564d7f0aSLorenzo Bianconi 	int i;
112e87b5039SStanislaw Gruszka 
11326a9daa6SLorenzo Bianconi 	mt76x02_get_rx_gain(dev, chan->band, &rssi_offset, &lna_2g, lna_5g);
11426a9daa6SLorenzo Bianconi 	caldata->lna_gain = mt76x02_get_lna_gain(dev, &lna_2g, lna_5g, chan);
115e87b5039SStanislaw Gruszka 
116564d7f0aSLorenzo Bianconi 	for (i = 0; i < ARRAY_SIZE(caldata->rssi_offset); i++) {
117564d7f0aSLorenzo Bianconi 		val = rssi_offset >> (8 * i);
118564d7f0aSLorenzo Bianconi 		if (val < -10 || val > 10)
119564d7f0aSLorenzo Bianconi 			val = 0;
120e87b5039SStanislaw Gruszka 
121564d7f0aSLorenzo Bianconi 		caldata->rssi_offset[i] = val;
122e87b5039SStanislaw Gruszka 	}
123e87b5039SStanislaw Gruszka }
124e87b5039SStanislaw Gruszka 
12526a9daa6SLorenzo Bianconi static s8 mt76x0_get_delta(struct mt76x02_dev *dev)
126e87b5039SStanislaw Gruszka {
12726a9daa6SLorenzo Bianconi 	struct cfg80211_chan_def *chandef = &dev->mt76.chandef;
128b37bbc8cSLorenzo Bianconi 	u8 val;
129e87b5039SStanislaw Gruszka 
130b37bbc8cSLorenzo Bianconi 	if (chandef->width == NL80211_CHAN_WIDTH_80) {
131b37bbc8cSLorenzo Bianconi 		val = mt76x02_eeprom_get(dev, MT_EE_5G_TARGET_POWER) >> 8;
132b37bbc8cSLorenzo Bianconi 	} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
133b37bbc8cSLorenzo Bianconi 		u16 data;
134e87b5039SStanislaw Gruszka 
135b37bbc8cSLorenzo Bianconi 		data = mt76x02_eeprom_get(dev, MT_EE_TX_POWER_DELTA_BW40);
136b37bbc8cSLorenzo Bianconi 		if (chandef->chan->band == NL80211_BAND_5GHZ)
137b37bbc8cSLorenzo Bianconi 			val = data >> 8;
138b37bbc8cSLorenzo Bianconi 		else
139b37bbc8cSLorenzo Bianconi 			val = data;
140b37bbc8cSLorenzo Bianconi 	} else {
141b37bbc8cSLorenzo Bianconi 		return 0;
142e87b5039SStanislaw Gruszka 	}
143e87b5039SStanislaw Gruszka 
144b37bbc8cSLorenzo Bianconi 	return mt76x02_rate_power_val(val);
145b37bbc8cSLorenzo Bianconi }
146b37bbc8cSLorenzo Bianconi 
1471ffe410eSLorenzo Bianconi void mt76x0_get_tx_power_per_rate(struct mt76x02_dev *dev,
1481ffe410eSLorenzo Bianconi 				  struct ieee80211_channel *chan,
1491ffe410eSLorenzo Bianconi 				  struct mt76_rate_power *t)
150e87b5039SStanislaw Gruszka {
151b37bbc8cSLorenzo Bianconi 	bool is_2ghz = chan->band == NL80211_BAND_2GHZ;
152b37bbc8cSLorenzo Bianconi 	u16 val, addr;
15307e54852SLorenzo Bianconi 	s8 delta;
154e87b5039SStanislaw Gruszka 
155b37bbc8cSLorenzo Bianconi 	memset(t, 0, sizeof(*t));
156e87b5039SStanislaw Gruszka 
157b37bbc8cSLorenzo Bianconi 	/* cck 1M, 2M, 5.5M, 11M */
15826a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, MT_EE_TX_POWER_BYRATE_BASE);
159b37bbc8cSLorenzo Bianconi 	t->cck[0] = t->cck[1] = s6_to_s8(val);
160b37bbc8cSLorenzo Bianconi 	t->cck[2] = t->cck[3] = s6_to_s8(val >> 8);
161e87b5039SStanislaw Gruszka 
162b37bbc8cSLorenzo Bianconi 	/* ofdm 6M, 9M, 12M, 18M */
163b37bbc8cSLorenzo Bianconi 	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 2 : 0x120;
16426a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, addr);
165b37bbc8cSLorenzo Bianconi 	t->ofdm[0] = t->ofdm[1] = s6_to_s8(val);
166b37bbc8cSLorenzo Bianconi 	t->ofdm[2] = t->ofdm[3] = s6_to_s8(val >> 8);
167e87b5039SStanislaw Gruszka 
168b37bbc8cSLorenzo Bianconi 	/* ofdm 24M, 36M, 48M, 54M */
169b37bbc8cSLorenzo Bianconi 	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 4 : 0x122;
17026a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, addr);
171b37bbc8cSLorenzo Bianconi 	t->ofdm[4] = t->ofdm[5] = s6_to_s8(val);
172b37bbc8cSLorenzo Bianconi 	t->ofdm[6] = t->ofdm[7] = s6_to_s8(val >> 8);
173e87b5039SStanislaw Gruszka 
174b37bbc8cSLorenzo Bianconi 	/* ht-vht mcs 1ss 0, 1, 2, 3 */
175b37bbc8cSLorenzo Bianconi 	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 6 : 0x124;
17626a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, addr);
177b37bbc8cSLorenzo Bianconi 	t->ht[0] = t->ht[1] = t->vht[0] = t->vht[1] = s6_to_s8(val);
178b37bbc8cSLorenzo Bianconi 	t->ht[2] = t->ht[3] = t->vht[2] = t->vht[3] = s6_to_s8(val >> 8);
179e87b5039SStanislaw Gruszka 
180b37bbc8cSLorenzo Bianconi 	/* ht-vht mcs 1ss 4, 5, 6 */
181b37bbc8cSLorenzo Bianconi 	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 8 : 0x126;
18226a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, addr);
183b37bbc8cSLorenzo Bianconi 	t->ht[4] = t->ht[5] = t->vht[4] = t->vht[5] = s6_to_s8(val);
184b29e46b7SLorenzo Bianconi 	t->ht[6] = t->ht[7] = t->vht[6] = t->vht[7] = s6_to_s8(val >> 8);
185e87b5039SStanislaw Gruszka 
186b37bbc8cSLorenzo Bianconi 	/* ht-vht mcs 1ss 0, 1, 2, 3 stbc */
187b37bbc8cSLorenzo Bianconi 	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 14 : 0xec;
18826a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, addr);
189b37bbc8cSLorenzo Bianconi 	t->stbc[0] = t->stbc[1] = s6_to_s8(val);
190b37bbc8cSLorenzo Bianconi 	t->stbc[2] = t->stbc[3] = s6_to_s8(val >> 8);
191e87b5039SStanislaw Gruszka 
192b37bbc8cSLorenzo Bianconi 	/* ht-vht mcs 1ss 4, 5, 6 stbc */
193b37bbc8cSLorenzo Bianconi 	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 16 : 0xee;
19426a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, addr);
195b37bbc8cSLorenzo Bianconi 	t->stbc[4] = t->stbc[5] = s6_to_s8(val);
196b37bbc8cSLorenzo Bianconi 	t->stbc[6] = t->stbc[7] = s6_to_s8(val >> 8);
197e87b5039SStanislaw Gruszka 
198b37bbc8cSLorenzo Bianconi 	/* vht mcs 8, 9 5GHz */
19926a9daa6SLorenzo Bianconi 	val = mt76x02_eeprom_get(dev, 0x132);
200b29e46b7SLorenzo Bianconi 	t->vht[8] = s6_to_s8(val);
201b29e46b7SLorenzo Bianconi 	t->vht[9] = s6_to_s8(val >> 8);
202b37bbc8cSLorenzo Bianconi 
20307e54852SLorenzo Bianconi 	delta = mt76x0_tssi_enabled(dev) ? 0 : mt76x0_get_delta(dev);
204b37bbc8cSLorenzo Bianconi 	mt76x02_add_rate_power_offset(t, delta);
205e87b5039SStanislaw Gruszka }
206e87b5039SStanislaw Gruszka 
2071ffe410eSLorenzo Bianconi void mt76x0_get_power_info(struct mt76x02_dev *dev,
2081ffe410eSLorenzo Bianconi 			   struct ieee80211_channel *chan, s8 *tp)
209e87b5039SStanislaw Gruszka {
210f2a2e819SLorenzo Bianconi 	struct mt76x0_chan_map {
211f2a2e819SLorenzo Bianconi 		u8 chan;
212f2a2e819SLorenzo Bianconi 		u8 offset;
213f2a2e819SLorenzo Bianconi 	} chan_map[] = {
21405672636SLorenzo Bianconi 		{   2,  0 }, {   4,  2 }, {   6,  4 }, {   8,  6 },
21505672636SLorenzo Bianconi 		{  10,  8 }, {  12, 10 }, {  14, 12 }, {  38,  0 },
21605672636SLorenzo Bianconi 		{  44,  2 }, {  48,  4 }, {  54,  6 }, {  60,  8 },
21705672636SLorenzo Bianconi 		{  64, 10 }, { 102, 12 }, { 108, 14 }, { 112, 16 },
21805672636SLorenzo Bianconi 		{ 118, 18 }, { 124, 20 }, { 128, 22 }, { 134, 24 },
21905672636SLorenzo Bianconi 		{ 140, 26 }, { 151, 28 }, { 157, 30 }, { 161, 32 },
22005672636SLorenzo Bianconi 		{ 167, 34 }, { 171, 36 }, { 175, 38 },
221f2a2e819SLorenzo Bianconi 	};
222f2a2e819SLorenzo Bianconi 	u8 offset, addr;
22305672636SLorenzo Bianconi 	int i, idx = 0;
224f2a2e819SLorenzo Bianconi 	u16 data;
225e87b5039SStanislaw Gruszka 
22607e54852SLorenzo Bianconi 	if (mt76x0_tssi_enabled(dev)) {
22707e54852SLorenzo Bianconi 		s8 target_power;
22807e54852SLorenzo Bianconi 
22907e54852SLorenzo Bianconi 		if (chan->band == NL80211_BAND_5GHZ)
23007e54852SLorenzo Bianconi 			data = mt76x02_eeprom_get(dev, MT_EE_5G_TARGET_POWER);
23107e54852SLorenzo Bianconi 		else
23207e54852SLorenzo Bianconi 			data = mt76x02_eeprom_get(dev, MT_EE_2G_TARGET_POWER);
23307e54852SLorenzo Bianconi 		target_power = (data & 0xff) - dev->mt76.rate_power.ofdm[7];
23405672636SLorenzo Bianconi 		*tp = target_power + mt76x0_get_delta(dev);
23507e54852SLorenzo Bianconi 
23607e54852SLorenzo Bianconi 		return;
23707e54852SLorenzo Bianconi 	}
23807e54852SLorenzo Bianconi 
239f2a2e819SLorenzo Bianconi 	for (i = 0; i < ARRAY_SIZE(chan_map); i++) {
24005672636SLorenzo Bianconi 		if (chan->hw_value <= chan_map[i].chan) {
24105672636SLorenzo Bianconi 			idx = (chan->hw_value == chan_map[i].chan);
242f2a2e819SLorenzo Bianconi 			offset = chan_map[i].offset;
243f2a2e819SLorenzo Bianconi 			break;
244f2a2e819SLorenzo Bianconi 		}
245f2a2e819SLorenzo Bianconi 	}
246f2a2e819SLorenzo Bianconi 	if (i == ARRAY_SIZE(chan_map))
247f2a2e819SLorenzo Bianconi 		offset = chan_map[0].offset;
248f2a2e819SLorenzo Bianconi 
249f2a2e819SLorenzo Bianconi 	if (chan->band == NL80211_BAND_2GHZ) {
250f2a2e819SLorenzo Bianconi 		addr = MT_EE_TX_POWER_DELTA_BW80 + offset;
251f2a2e819SLorenzo Bianconi 	} else {
252f2a2e819SLorenzo Bianconi 		switch (chan->hw_value) {
25305672636SLorenzo Bianconi 		case 42:
25405672636SLorenzo Bianconi 			offset = 2;
25505672636SLorenzo Bianconi 			break;
256f2a2e819SLorenzo Bianconi 		case 58:
257f2a2e819SLorenzo Bianconi 			offset = 8;
258f2a2e819SLorenzo Bianconi 			break;
259f2a2e819SLorenzo Bianconi 		case 106:
260f2a2e819SLorenzo Bianconi 			offset = 14;
261f2a2e819SLorenzo Bianconi 			break;
26205672636SLorenzo Bianconi 		case 122:
263f2a2e819SLorenzo Bianconi 			offset = 20;
264f2a2e819SLorenzo Bianconi 			break;
265f2a2e819SLorenzo Bianconi 		case 155:
266f2a2e819SLorenzo Bianconi 			offset = 30;
267f2a2e819SLorenzo Bianconi 			break;
268f2a2e819SLorenzo Bianconi 		default:
269f2a2e819SLorenzo Bianconi 			break;
270f2a2e819SLorenzo Bianconi 		}
271f2a2e819SLorenzo Bianconi 		addr = MT_EE_TX_POWER_0_GRP4_TSSI_SLOPE + 2 + offset;
272e87b5039SStanislaw Gruszka 	}
273e87b5039SStanislaw Gruszka 
27426a9daa6SLorenzo Bianconi 	data = mt76x02_eeprom_get(dev, addr);
27505672636SLorenzo Bianconi 	*tp = data >> (8 * idx);
27605672636SLorenzo Bianconi 	if (*tp < 0 || *tp > 0x3f)
27705672636SLorenzo Bianconi 		*tp = 5;
278e87b5039SStanislaw Gruszka }
279e87b5039SStanislaw Gruszka 
280b2d871c0SLorenzo Bianconi static int mt76x0_check_eeprom(struct mt76x02_dev *dev)
2818d98c153SLorenzo Bianconi {
2828d98c153SLorenzo Bianconi 	u16 val;
2838d98c153SLorenzo Bianconi 
2848d98c153SLorenzo Bianconi 	val = get_unaligned_le16(dev->mt76.eeprom.data);
2858d98c153SLorenzo Bianconi 	if (!val)
2868d98c153SLorenzo Bianconi 		val = get_unaligned_le16(dev->mt76.eeprom.data +
2878d98c153SLorenzo Bianconi 					 MT_EE_PCI_ID);
2888d98c153SLorenzo Bianconi 
2898d98c153SLorenzo Bianconi 	switch (val) {
2908d98c153SLorenzo Bianconi 	case 0x7650:
2918d98c153SLorenzo Bianconi 	case 0x7610:
2928d98c153SLorenzo Bianconi 		return 0;
2938d98c153SLorenzo Bianconi 	default:
2948d98c153SLorenzo Bianconi 		dev_err(dev->mt76.dev, "EEPROM data check failed: %04x\n",
2958d98c153SLorenzo Bianconi 			val);
2968d98c153SLorenzo Bianconi 		return -EINVAL;
2978d98c153SLorenzo Bianconi 	}
2988d98c153SLorenzo Bianconi }
2998d98c153SLorenzo Bianconi 
300b2d871c0SLorenzo Bianconi static int mt76x0_load_eeprom(struct mt76x02_dev *dev)
3018d98c153SLorenzo Bianconi {
3028d98c153SLorenzo Bianconi 	int found;
3038d98c153SLorenzo Bianconi 
3048d98c153SLorenzo Bianconi 	found = mt76_eeprom_init(&dev->mt76, MT76X0_EEPROM_SIZE);
3058d98c153SLorenzo Bianconi 	if (found < 0)
3068d98c153SLorenzo Bianconi 		return found;
3078d98c153SLorenzo Bianconi 
3088d98c153SLorenzo Bianconi 	if (found && !mt76x0_check_eeprom(dev))
3098d98c153SLorenzo Bianconi 		return 0;
3108d98c153SLorenzo Bianconi 
3118d98c153SLorenzo Bianconi 	found = mt76x0_efuse_physical_size_check(dev);
3128d98c153SLorenzo Bianconi 	if (found < 0)
3138d98c153SLorenzo Bianconi 		return found;
3148d98c153SLorenzo Bianconi 
31526a9daa6SLorenzo Bianconi 	return mt76x02_get_efuse_data(dev, 0, dev->mt76.eeprom.data,
3168d98c153SLorenzo Bianconi 				      MT76X0_EEPROM_SIZE, MT_EE_READ);
3178d98c153SLorenzo Bianconi }
3188d98c153SLorenzo Bianconi 
319b2d871c0SLorenzo Bianconi int mt76x0_eeprom_init(struct mt76x02_dev *dev)
320e87b5039SStanislaw Gruszka {
321f2a2e819SLorenzo Bianconi 	u8 version, fae;
322f2a2e819SLorenzo Bianconi 	u16 data;
3238d98c153SLorenzo Bianconi 	int err;
324e87b5039SStanislaw Gruszka 
3258d98c153SLorenzo Bianconi 	err = mt76x0_load_eeprom(dev);
3268d98c153SLorenzo Bianconi 	if (err < 0)
3278d98c153SLorenzo Bianconi 		return err;
328e87b5039SStanislaw Gruszka 
32926a9daa6SLorenzo Bianconi 	data = mt76x02_eeprom_get(dev, MT_EE_VERSION);
330f2a2e819SLorenzo Bianconi 	version = data >> 8;
331f2a2e819SLorenzo Bianconi 	fae = data;
332f2a2e819SLorenzo Bianconi 
333f2a2e819SLorenzo Bianconi 	if (version > MT76X0U_EE_MAX_VER)
334e87b5039SStanislaw Gruszka 		dev_warn(dev->mt76.dev,
335e87b5039SStanislaw Gruszka 			 "Warning: unsupported EEPROM version %02hhx\n",
336f2a2e819SLorenzo Bianconi 			 version);
337e87b5039SStanislaw Gruszka 	dev_info(dev->mt76.dev, "EEPROM ver:%02hhx fae:%02hhx\n",
338f2a2e819SLorenzo Bianconi 		 version, fae);
339e87b5039SStanislaw Gruszka 
3408d66af49SLorenzo Bianconi 	mt76x02_mac_setaddr(dev, dev->mt76.eeprom.data + MT_EE_MAC_ADDR);
3413d66939aSLorenzo Bianconi 	mt76x0_set_chip_cap(dev);
34277d0f465SLorenzo Bianconi 	mt76x0_set_freq_offset(dev);
3432c0db839SLorenzo Bianconi 	mt76x0_set_temp_offset(dev);
344f2a2e819SLorenzo Bianconi 
345f2a2e819SLorenzo Bianconi 	return 0;
346e87b5039SStanislaw Gruszka }
347c2a4d9fbSStanislaw Gruszka 
348c2a4d9fbSStanislaw Gruszka MODULE_LICENSE("Dual BSD/GPL");
349