treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wireless / mediatek / mt76 / mt7603 / eeprom.c
blob2b6a4d8a8dc70b62082fa364b07b7c839e469ed4
1 // SPDX-License-Identifier: ISC
3 #include "mt7603.h"
4 #include "eeprom.h"
6 static int
7 mt7603_efuse_read(struct mt7603_dev *dev, u32 base, u16 addr, u8 *data)
9 u32 val;
10 int i;
12 val = mt76_rr(dev, base + MT_EFUSE_CTRL);
13 val &= ~(MT_EFUSE_CTRL_AIN |
14 MT_EFUSE_CTRL_MODE);
15 val |= FIELD_PREP(MT_EFUSE_CTRL_AIN, addr & ~0xf);
16 val |= MT_EFUSE_CTRL_KICK;
17 mt76_wr(dev, base + MT_EFUSE_CTRL, val);
19 if (!mt76_poll(dev, base + MT_EFUSE_CTRL, MT_EFUSE_CTRL_KICK, 0, 1000))
20 return -ETIMEDOUT;
22 udelay(2);
24 val = mt76_rr(dev, base + MT_EFUSE_CTRL);
25 if ((val & MT_EFUSE_CTRL_AOUT) == MT_EFUSE_CTRL_AOUT ||
26 WARN_ON_ONCE(!(val & MT_EFUSE_CTRL_VALID))) {
27 memset(data, 0xff, 16);
28 return 0;
31 for (i = 0; i < 4; i++) {
32 val = mt76_rr(dev, base + MT_EFUSE_RDATA(i));
33 put_unaligned_le32(val, data + 4 * i);
36 return 0;
39 static int
40 mt7603_efuse_init(struct mt7603_dev *dev)
42 u32 base = mt7603_reg_map(dev, MT_EFUSE_BASE);
43 int len = MT7603_EEPROM_SIZE;
44 void *buf;
45 int ret, i;
47 if (mt76_rr(dev, base + MT_EFUSE_BASE_CTRL) & MT_EFUSE_BASE_CTRL_EMPTY)
48 return 0;
50 dev->mt76.otp.data = devm_kzalloc(dev->mt76.dev, len, GFP_KERNEL);
51 dev->mt76.otp.size = len;
52 if (!dev->mt76.otp.data)
53 return -ENOMEM;
55 buf = dev->mt76.otp.data;
56 for (i = 0; i + 16 <= len; i += 16) {
57 ret = mt7603_efuse_read(dev, base, i, buf + i);
58 if (ret)
59 return ret;
62 return 0;
65 static bool
66 mt7603_has_cal_free_data(struct mt7603_dev *dev, u8 *efuse)
68 if (!efuse[MT_EE_TEMP_SENSOR_CAL])
69 return false;
71 if (get_unaligned_le16(efuse + MT_EE_TX_POWER_0_START_2G) == 0)
72 return false;
74 if (get_unaligned_le16(efuse + MT_EE_TX_POWER_1_START_2G) == 0)
75 return false;
77 if (!efuse[MT_EE_CP_FT_VERSION])
78 return false;
80 if (!efuse[MT_EE_XTAL_FREQ_OFFSET])
81 return false;
83 if (!efuse[MT_EE_XTAL_WF_RFCAL])
84 return false;
86 return true;
89 static void
90 mt7603_apply_cal_free_data(struct mt7603_dev *dev, u8 *efuse)
92 static const u8 cal_free_bytes[] = {
93 MT_EE_TEMP_SENSOR_CAL,
94 MT_EE_CP_FT_VERSION,
95 MT_EE_XTAL_FREQ_OFFSET,
96 MT_EE_XTAL_WF_RFCAL,
97 /* Skip for MT7628 */
98 MT_EE_TX_POWER_0_START_2G,
99 MT_EE_TX_POWER_0_START_2G + 1,
100 MT_EE_TX_POWER_1_START_2G,
101 MT_EE_TX_POWER_1_START_2G + 1,
103 u8 *eeprom = dev->mt76.eeprom.data;
104 int n = ARRAY_SIZE(cal_free_bytes);
105 int i;
107 if (!mt7603_has_cal_free_data(dev, efuse))
108 return;
110 if (is_mt7628(dev))
111 n -= 4;
113 for (i = 0; i < n; i++) {
114 int offset = cal_free_bytes[i];
116 eeprom[offset] = efuse[offset];
120 static int
121 mt7603_eeprom_load(struct mt7603_dev *dev)
123 int ret;
125 ret = mt76_eeprom_init(&dev->mt76, MT7603_EEPROM_SIZE);
126 if (ret < 0)
127 return ret;
129 return mt7603_efuse_init(dev);
132 static int mt7603_check_eeprom(struct mt76_dev *dev)
134 u16 val = get_unaligned_le16(dev->eeprom.data);
136 switch (val) {
137 case 0x7628:
138 case 0x7603:
139 return 0;
140 default:
141 return -EINVAL;
145 int mt7603_eeprom_init(struct mt7603_dev *dev)
147 int ret;
149 ret = mt7603_eeprom_load(dev);
150 if (ret < 0)
151 return ret;
153 if (dev->mt76.otp.data) {
154 if (mt7603_check_eeprom(&dev->mt76) == 0)
155 mt7603_apply_cal_free_data(dev, dev->mt76.otp.data);
156 else
157 memcpy(dev->mt76.eeprom.data, dev->mt76.otp.data,
158 MT7603_EEPROM_SIZE);
161 dev->mt76.cap.has_2ghz = true;
162 memcpy(dev->mt76.macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR,
163 ETH_ALEN);
165 mt76_eeprom_override(&dev->mt76);
167 return 0;