12774f206SBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
22774f206SBjoern A. Zeeb /* Copyright(c) 2018-2019 Realtek Corporation
32774f206SBjoern A. Zeeb */
42774f206SBjoern A. Zeeb
52774f206SBjoern A. Zeeb #include <linux/iopoll.h>
62774f206SBjoern A. Zeeb
72774f206SBjoern A. Zeeb #include "main.h"
82774f206SBjoern A. Zeeb #include "efuse.h"
92774f206SBjoern A. Zeeb #include "reg.h"
102774f206SBjoern A. Zeeb #include "debug.h"
112774f206SBjoern A. Zeeb
122774f206SBjoern A. Zeeb #define RTW_EFUSE_BANK_WIFI 0x0
132774f206SBjoern A. Zeeb
switch_efuse_bank(struct rtw_dev * rtwdev)142774f206SBjoern A. Zeeb static void switch_efuse_bank(struct rtw_dev *rtwdev)
152774f206SBjoern A. Zeeb {
162774f206SBjoern A. Zeeb rtw_write32_mask(rtwdev, REG_LDO_EFUSE_CTRL, BIT_MASK_EFUSE_BANK_SEL,
172774f206SBjoern A. Zeeb RTW_EFUSE_BANK_WIFI);
182774f206SBjoern A. Zeeb }
192774f206SBjoern A. Zeeb
202774f206SBjoern A. Zeeb #define invalid_efuse_header(hdr1, hdr2) \
212774f206SBjoern A. Zeeb ((hdr1) == 0xff || (((hdr1) & 0x1f) == 0xf && (hdr2) == 0xff))
222774f206SBjoern A. Zeeb #define invalid_efuse_content(word_en, i) \
232774f206SBjoern A. Zeeb (((word_en) & BIT(i)) != 0x0)
242774f206SBjoern A. Zeeb #define get_efuse_blk_idx_2_byte(hdr1, hdr2) \
252774f206SBjoern A. Zeeb ((((hdr2) & 0xf0) >> 1) | (((hdr1) >> 5) & 0x07))
262774f206SBjoern A. Zeeb #define get_efuse_blk_idx_1_byte(hdr1) \
272774f206SBjoern A. Zeeb (((hdr1) & 0xf0) >> 4)
282774f206SBjoern A. Zeeb #define block_idx_to_logical_idx(blk_idx, i) \
292774f206SBjoern A. Zeeb (((blk_idx) << 3) + ((i) << 1))
302774f206SBjoern A. Zeeb
312774f206SBjoern A. Zeeb /* efuse header format
322774f206SBjoern A. Zeeb *
332774f206SBjoern A. Zeeb * | 7 5 4 0 | 7 4 3 0 | 15 8 7 0 |
342774f206SBjoern A. Zeeb * block[2:0] 0 1111 block[6:3] word_en[3:0] byte0 byte1
352774f206SBjoern A. Zeeb * | header 1 (optional) | header 2 | word N |
362774f206SBjoern A. Zeeb *
372774f206SBjoern A. Zeeb * word_en: 4 bits each word. 0 -> write; 1 -> not write
382774f206SBjoern A. Zeeb * N: 1~4, depends on word_en
392774f206SBjoern A. Zeeb */
rtw_dump_logical_efuse_map(struct rtw_dev * rtwdev,u8 * phy_map,u8 * log_map)402774f206SBjoern A. Zeeb static int rtw_dump_logical_efuse_map(struct rtw_dev *rtwdev, u8 *phy_map,
412774f206SBjoern A. Zeeb u8 *log_map)
422774f206SBjoern A. Zeeb {
432774f206SBjoern A. Zeeb u32 physical_size = rtwdev->efuse.physical_size;
442774f206SBjoern A. Zeeb u32 protect_size = rtwdev->efuse.protect_size;
452774f206SBjoern A. Zeeb u32 logical_size = rtwdev->efuse.logical_size;
462774f206SBjoern A. Zeeb u32 phy_idx, log_idx;
472774f206SBjoern A. Zeeb u8 hdr1, hdr2;
482774f206SBjoern A. Zeeb u8 blk_idx;
492774f206SBjoern A. Zeeb u8 word_en;
502774f206SBjoern A. Zeeb int i;
512774f206SBjoern A. Zeeb
522774f206SBjoern A. Zeeb for (phy_idx = 0; phy_idx < physical_size - protect_size;) {
532774f206SBjoern A. Zeeb hdr1 = phy_map[phy_idx];
542774f206SBjoern A. Zeeb hdr2 = phy_map[phy_idx + 1];
552774f206SBjoern A. Zeeb if (invalid_efuse_header(hdr1, hdr2))
562774f206SBjoern A. Zeeb break;
572774f206SBjoern A. Zeeb
582774f206SBjoern A. Zeeb if ((hdr1 & 0x1f) == 0xf) {
592774f206SBjoern A. Zeeb /* 2-byte header format */
602774f206SBjoern A. Zeeb blk_idx = get_efuse_blk_idx_2_byte(hdr1, hdr2);
612774f206SBjoern A. Zeeb word_en = hdr2 & 0xf;
622774f206SBjoern A. Zeeb phy_idx += 2;
632774f206SBjoern A. Zeeb } else {
642774f206SBjoern A. Zeeb /* 1-byte header format */
652774f206SBjoern A. Zeeb blk_idx = get_efuse_blk_idx_1_byte(hdr1);
662774f206SBjoern A. Zeeb word_en = hdr1 & 0xf;
672774f206SBjoern A. Zeeb phy_idx += 1;
682774f206SBjoern A. Zeeb }
692774f206SBjoern A. Zeeb
702774f206SBjoern A. Zeeb for (i = 0; i < 4; i++) {
712774f206SBjoern A. Zeeb if (invalid_efuse_content(word_en, i))
722774f206SBjoern A. Zeeb continue;
732774f206SBjoern A. Zeeb
742774f206SBjoern A. Zeeb log_idx = block_idx_to_logical_idx(blk_idx, i);
752774f206SBjoern A. Zeeb if (phy_idx + 1 > physical_size - protect_size ||
762774f206SBjoern A. Zeeb log_idx + 1 > logical_size)
772774f206SBjoern A. Zeeb return -EINVAL;
782774f206SBjoern A. Zeeb
792774f206SBjoern A. Zeeb log_map[log_idx] = phy_map[phy_idx];
802774f206SBjoern A. Zeeb log_map[log_idx + 1] = phy_map[phy_idx + 1];
812774f206SBjoern A. Zeeb phy_idx += 2;
822774f206SBjoern A. Zeeb }
832774f206SBjoern A. Zeeb }
842774f206SBjoern A. Zeeb return 0;
852774f206SBjoern A. Zeeb }
862774f206SBjoern A. Zeeb
rtw_dump_physical_efuse_map(struct rtw_dev * rtwdev,u8 * map)872774f206SBjoern A. Zeeb static int rtw_dump_physical_efuse_map(struct rtw_dev *rtwdev, u8 *map)
882774f206SBjoern A. Zeeb {
89*90aac0d8SBjoern A. Zeeb const struct rtw_chip_info *chip = rtwdev->chip;
902774f206SBjoern A. Zeeb u32 size = rtwdev->efuse.physical_size;
912774f206SBjoern A. Zeeb u32 efuse_ctl;
922774f206SBjoern A. Zeeb u32 addr;
932774f206SBjoern A. Zeeb u32 cnt;
942774f206SBjoern A. Zeeb
952774f206SBjoern A. Zeeb rtw_chip_efuse_grant_on(rtwdev);
962774f206SBjoern A. Zeeb
972774f206SBjoern A. Zeeb switch_efuse_bank(rtwdev);
982774f206SBjoern A. Zeeb
992774f206SBjoern A. Zeeb /* disable 2.5V LDO */
1002774f206SBjoern A. Zeeb chip->ops->cfg_ldo25(rtwdev, false);
1012774f206SBjoern A. Zeeb
1022774f206SBjoern A. Zeeb efuse_ctl = rtw_read32(rtwdev, REG_EFUSE_CTRL);
1032774f206SBjoern A. Zeeb
1042774f206SBjoern A. Zeeb for (addr = 0; addr < size; addr++) {
1052774f206SBjoern A. Zeeb efuse_ctl &= ~(BIT_MASK_EF_DATA | BITS_EF_ADDR);
1062774f206SBjoern A. Zeeb efuse_ctl |= (addr & BIT_MASK_EF_ADDR) << BIT_SHIFT_EF_ADDR;
1072774f206SBjoern A. Zeeb rtw_write32(rtwdev, REG_EFUSE_CTRL, efuse_ctl & (~BIT_EF_FLAG));
1082774f206SBjoern A. Zeeb
1092774f206SBjoern A. Zeeb cnt = 1000000;
1102774f206SBjoern A. Zeeb do {
1112774f206SBjoern A. Zeeb udelay(1);
1122774f206SBjoern A. Zeeb efuse_ctl = rtw_read32(rtwdev, REG_EFUSE_CTRL);
1132774f206SBjoern A. Zeeb if (--cnt == 0)
1142774f206SBjoern A. Zeeb return -EBUSY;
1152774f206SBjoern A. Zeeb } while (!(efuse_ctl & BIT_EF_FLAG));
1162774f206SBjoern A. Zeeb
1172774f206SBjoern A. Zeeb *(map + addr) = (u8)(efuse_ctl & BIT_MASK_EF_DATA);
1182774f206SBjoern A. Zeeb }
1192774f206SBjoern A. Zeeb
1202774f206SBjoern A. Zeeb rtw_chip_efuse_grant_off(rtwdev);
1212774f206SBjoern A. Zeeb
1222774f206SBjoern A. Zeeb return 0;
1232774f206SBjoern A. Zeeb }
1242774f206SBjoern A. Zeeb
rtw_read8_physical_efuse(struct rtw_dev * rtwdev,u16 addr,u8 * data)1252774f206SBjoern A. Zeeb int rtw_read8_physical_efuse(struct rtw_dev *rtwdev, u16 addr, u8 *data)
1262774f206SBjoern A. Zeeb {
1272774f206SBjoern A. Zeeb u32 efuse_ctl;
1282774f206SBjoern A. Zeeb int ret;
1292774f206SBjoern A. Zeeb
1302774f206SBjoern A. Zeeb rtw_write32_mask(rtwdev, REG_EFUSE_CTRL, 0x3ff00, addr);
1312774f206SBjoern A. Zeeb rtw_write32_clr(rtwdev, REG_EFUSE_CTRL, BIT_EF_FLAG);
1322774f206SBjoern A. Zeeb
1332774f206SBjoern A. Zeeb ret = read_poll_timeout(rtw_read32, efuse_ctl, efuse_ctl & BIT_EF_FLAG,
1342774f206SBjoern A. Zeeb 1000, 100000, false, rtwdev, REG_EFUSE_CTRL);
1352774f206SBjoern A. Zeeb if (ret) {
1362774f206SBjoern A. Zeeb *data = EFUSE_READ_FAIL;
1372774f206SBjoern A. Zeeb return ret;
1382774f206SBjoern A. Zeeb }
1392774f206SBjoern A. Zeeb
1402774f206SBjoern A. Zeeb *data = rtw_read8(rtwdev, REG_EFUSE_CTRL);
1412774f206SBjoern A. Zeeb
1422774f206SBjoern A. Zeeb return 0;
1432774f206SBjoern A. Zeeb }
1442774f206SBjoern A. Zeeb EXPORT_SYMBOL(rtw_read8_physical_efuse);
1452774f206SBjoern A. Zeeb
rtw_parse_efuse_map(struct rtw_dev * rtwdev)1462774f206SBjoern A. Zeeb int rtw_parse_efuse_map(struct rtw_dev *rtwdev)
1472774f206SBjoern A. Zeeb {
148*90aac0d8SBjoern A. Zeeb const struct rtw_chip_info *chip = rtwdev->chip;
1492774f206SBjoern A. Zeeb struct rtw_efuse *efuse = &rtwdev->efuse;
1502774f206SBjoern A. Zeeb u32 phy_size = efuse->physical_size;
1512774f206SBjoern A. Zeeb u32 log_size = efuse->logical_size;
1522774f206SBjoern A. Zeeb u8 *phy_map = NULL;
1532774f206SBjoern A. Zeeb u8 *log_map = NULL;
1542774f206SBjoern A. Zeeb int ret = 0;
1552774f206SBjoern A. Zeeb
1562774f206SBjoern A. Zeeb phy_map = kmalloc(phy_size, GFP_KERNEL);
1572774f206SBjoern A. Zeeb log_map = kmalloc(log_size, GFP_KERNEL);
1582774f206SBjoern A. Zeeb if (!phy_map || !log_map) {
1592774f206SBjoern A. Zeeb ret = -ENOMEM;
1602774f206SBjoern A. Zeeb goto out_free;
1612774f206SBjoern A. Zeeb }
1622774f206SBjoern A. Zeeb
1632774f206SBjoern A. Zeeb ret = rtw_dump_physical_efuse_map(rtwdev, phy_map);
1642774f206SBjoern A. Zeeb if (ret) {
1652774f206SBjoern A. Zeeb rtw_err(rtwdev, "failed to dump efuse physical map\n");
1662774f206SBjoern A. Zeeb goto out_free;
1672774f206SBjoern A. Zeeb }
1682774f206SBjoern A. Zeeb
1692774f206SBjoern A. Zeeb memset(log_map, 0xff, log_size);
1702774f206SBjoern A. Zeeb ret = rtw_dump_logical_efuse_map(rtwdev, phy_map, log_map);
1712774f206SBjoern A. Zeeb if (ret) {
1722774f206SBjoern A. Zeeb rtw_err(rtwdev, "failed to dump efuse logical map\n");
1732774f206SBjoern A. Zeeb goto out_free;
1742774f206SBjoern A. Zeeb }
1752774f206SBjoern A. Zeeb
1762774f206SBjoern A. Zeeb ret = chip->ops->read_efuse(rtwdev, log_map);
1772774f206SBjoern A. Zeeb if (ret) {
1782774f206SBjoern A. Zeeb rtw_err(rtwdev, "failed to read efuse map\n");
1792774f206SBjoern A. Zeeb goto out_free;
1802774f206SBjoern A. Zeeb }
1812774f206SBjoern A. Zeeb
1822774f206SBjoern A. Zeeb out_free:
1832774f206SBjoern A. Zeeb kfree(log_map);
1842774f206SBjoern A. Zeeb kfree(phy_map);
1852774f206SBjoern A. Zeeb
1862774f206SBjoern A. Zeeb return ret;
1872774f206SBjoern A. Zeeb }
188