1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Ryder Lee <ryder.lee@mediatek.com> 5 * Felix Fietkau <nbd@nbd.name> 6 */ 7 8 #include "mt7615.h" 9 #include "eeprom.h" 10 11 static int mt7615_efuse_read(struct mt7615_dev *dev, u32 base, 12 u16 addr, u8 *data) 13 { 14 u32 val; 15 int i; 16 17 val = mt76_rr(dev, base + MT_EFUSE_CTRL); 18 val &= ~(MT_EFUSE_CTRL_AIN | MT_EFUSE_CTRL_MODE); 19 val |= FIELD_PREP(MT_EFUSE_CTRL_AIN, addr & ~0xf); 20 val |= MT_EFUSE_CTRL_KICK; 21 mt76_wr(dev, base + MT_EFUSE_CTRL, val); 22 23 if (!mt76_poll(dev, base + MT_EFUSE_CTRL, MT_EFUSE_CTRL_KICK, 0, 1000)) 24 return -ETIMEDOUT; 25 26 udelay(2); 27 28 val = mt76_rr(dev, base + MT_EFUSE_CTRL); 29 if ((val & MT_EFUSE_CTRL_AOUT) == MT_EFUSE_CTRL_AOUT || 30 WARN_ON_ONCE(!(val & MT_EFUSE_CTRL_VALID))) { 31 memset(data, 0x0, 16); 32 return 0; 33 } 34 35 for (i = 0; i < 4; i++) { 36 val = mt76_rr(dev, base + MT_EFUSE_RDATA(i)); 37 put_unaligned_le32(val, data + 4 * i); 38 } 39 40 return 0; 41 } 42 43 static int mt7615_efuse_init(struct mt7615_dev *dev) 44 { 45 u32 base = mt7615_reg_map(dev, MT_EFUSE_BASE); 46 int len = MT7615_EEPROM_SIZE; 47 int ret, i; 48 void *buf; 49 50 if (mt76_rr(dev, base + MT_EFUSE_BASE_CTRL) & MT_EFUSE_BASE_CTRL_EMPTY) 51 return -EINVAL; 52 53 dev->mt76.otp.data = devm_kzalloc(dev->mt76.dev, len, GFP_KERNEL); 54 dev->mt76.otp.size = len; 55 if (!dev->mt76.otp.data) 56 return -ENOMEM; 57 58 buf = dev->mt76.otp.data; 59 for (i = 0; i + 16 <= len; i += 16) { 60 ret = mt7615_efuse_read(dev, base, i, buf + i); 61 if (ret) 62 return ret; 63 } 64 65 return 0; 66 } 67 68 static int mt7615_eeprom_load(struct mt7615_dev *dev) 69 { 70 int ret; 71 72 ret = mt76_eeprom_init(&dev->mt76, MT7615_EEPROM_SIZE); 73 if (ret < 0) 74 return ret; 75 76 return mt7615_efuse_init(dev); 77 } 78 79 int mt7615_eeprom_init(struct mt7615_dev *dev) 80 { 81 int ret; 82 83 ret = mt7615_eeprom_load(dev); 84 if (ret < 0) 85 return ret; 86 87 memcpy(dev->mt76.eeprom.data, dev->mt76.otp.data, MT7615_EEPROM_SIZE); 88 89 dev->mt76.cap.has_2ghz = true; 90 dev->mt76.cap.has_5ghz = true; 91 92 memcpy(dev->mt76.macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR, 93 ETH_ALEN); 94 95 mt76_eeprom_override(&dev->mt76); 96 97 return 0; 98 } 99