Skip to content

Commit

Permalink
mt76: mt7915: add default calibrated data support
Browse files Browse the repository at this point in the history
Load the default eeprom data when the content of flash/efuse is invalid.
This could help to eliminate some issues due to incorrect or
insufficient rf values.

Co-developed-by: Bo Jiao <Bo.Jiao@mediatek.com>
Signed-off-by: Bo Jiao <Bo.Jiao@mediatek.com>
Signed-off-by: Shayne Chen <shayne.chen@mediatek.com>
Reviewed-by: Ryder Lee <ryder.lee@mediatek.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
  • Loading branch information
csyuanc authored and nbd168 committed Nov 21, 2021
1 parent dd054b7 commit f1520c9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 18 deletions.
83 changes: 65 additions & 18 deletions mt7915/eeprom.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: ISC
/* Copyright (C) 2020 MediaTek Inc. */

#include <linux/firmware.h>
#include "mt7915.h"
#include "eeprom.h"

Expand All @@ -10,6 +11,9 @@ static int mt7915_eeprom_load_precal(struct mt7915_dev *dev)
u8 *eeprom = mdev->eeprom.data;
u32 val = eeprom[MT_EE_DO_PRE_CAL];

if (!dev->flash_mode)
return 0;

if (val != (MT_EE_WIFI_CAL_DPD | MT_EE_WIFI_CAL_GROUP))
return 0;

Expand All @@ -21,6 +25,49 @@ static int mt7915_eeprom_load_precal(struct mt7915_dev *dev)
return mt76_get_of_eeprom(mdev, dev->cal, MT_EE_PRECAL, val);
}

static int mt7915_check_eeprom(struct mt7915_dev *dev)
{
u8 *eeprom = dev->mt76.eeprom.data;
u16 val = get_unaligned_le16(eeprom);

switch (val) {
case 0x7915:
return 0;
default:
return -EINVAL;
}
}

static int
mt7915_eeprom_load_default(struct mt7915_dev *dev)
{
char *default_bin = MT7915_EEPROM_DEFAULT;
u8 *eeprom = dev->mt76.eeprom.data;
const struct firmware *fw = NULL;
int ret;

if (dev->dbdc_support)
default_bin = MT7915_EEPROM_DEFAULT_DBDC;

ret = request_firmware(&fw, default_bin, dev->mt76.dev);
if (ret)
return ret;

if (!fw || !fw->data) {
dev_err(dev->mt76.dev, "Invalid default bin\n");
ret = -EINVAL;
goto out;
}

memcpy(eeprom, fw->data, MT7915_EEPROM_SIZE);
dev->flash_mode = true;

out:
release_firmware(fw);

return ret;
}

static int mt7915_eeprom_load(struct mt7915_dev *dev)
{
int ret;
Expand All @@ -31,31 +78,24 @@ static int mt7915_eeprom_load(struct mt7915_dev *dev)

if (ret) {
dev->flash_mode = true;
ret = mt7915_eeprom_load_precal(dev);
} else {
u8 free_block_num;
u32 block_num, i;

mt7915_mcu_get_eeprom_free_block(dev, &free_block_num);
/* efuse info not enough */
if (free_block_num >= 29)
return -EINVAL;

/* read eeprom data from efuse */
block_num = DIV_ROUND_UP(MT7915_EEPROM_SIZE,
MT7915_EEPROM_BLOCK_SIZE);
for (i = 0; i < block_num; i++)
mt7915_mcu_get_eeprom(dev,
i * MT7915_EEPROM_BLOCK_SIZE);
}

return ret;
}

static int mt7915_check_eeprom(struct mt7915_dev *dev)
{
u8 *eeprom = dev->mt76.eeprom.data;
u16 val = get_unaligned_le16(eeprom);

switch (val) {
case 0x7915:
return 0;
default:
return -EINVAL;
}
return mt7915_check_eeprom(dev);
}

void mt7915_eeprom_parse_band_config(struct mt7915_phy *phy)
Expand Down Expand Up @@ -117,10 +157,17 @@ int mt7915_eeprom_init(struct mt7915_dev *dev)
int ret;

ret = mt7915_eeprom_load(dev);
if (ret < 0)
return ret;
if (ret < 0) {
if (ret != -EINVAL)
return ret;

dev_warn(dev->mt76.dev, "eeprom load fail, use default bin\n");
ret = mt7915_eeprom_load_default(dev);
if (ret)
return ret;
}

ret = mt7915_check_eeprom(dev);
ret = mt7915_eeprom_load_precal(dev);
if (ret)
return ret;

Expand Down
24 changes: 24 additions & 0 deletions mt7915/mcu.c
Expand Up @@ -3543,6 +3543,30 @@ int mt7915_mcu_get_eeprom(struct mt7915_dev *dev, u32 offset)
return 0;
}

int mt7915_mcu_get_eeprom_free_block(struct mt7915_dev *dev, u8 *block_num)
{
struct {
u8 _rsv;
u8 version;
u8 die_idx;
u8 _rsv2;
} __packed req = {
.version = 1,
};
struct sk_buff *skb;
int ret;

ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_EXT_QUERY(EFUSE_FREE_BLOCK), &req,
sizeof(req), true, &skb);
if (ret)
return ret;

*block_num = *(u8 *)skb->data;
dev_kfree_skb(skb);

return 0;
}

static int mt7915_mcu_set_pre_cal(struct mt7915_dev *dev, u8 idx,
u8 *data, u32 len, int cmd)
{
Expand Down
1 change: 1 addition & 0 deletions mt7915/mcu.h
Expand Up @@ -278,6 +278,7 @@ enum {
MCU_EXT_CMD_MUAR_UPDATE = 0x48,
MCU_EXT_CMD_RX_AIRTIME_CTRL = 0x4a,
MCU_EXT_CMD_SET_RX_PATH = 0x4e,
MCU_EXT_CMD_EFUSE_FREE_BLOCK = 0x4f,
MCU_EXT_CMD_TX_POWER_FEATURE_CTRL = 0x58,
MCU_EXT_CMD_GET_MIB_INFO = 0x5a,
MCU_EXT_CMD_MWDS_SUPPORT = 0x80,
Expand Down
4 changes: 4 additions & 0 deletions mt7915/mt7915.h
Expand Up @@ -30,6 +30,9 @@
#define MT7915_FIRMWARE_WM "mediatek/mt7915_wm.bin"
#define MT7915_ROM_PATCH "mediatek/mt7915_rom_patch.bin"

#define MT7915_EEPROM_DEFAULT "mediatek/mt7915_eeprom.bin"
#define MT7915_EEPROM_DEFAULT_DBDC "mediatek/mt7915_eeprom_dbdc.bin"

#define MT7915_EEPROM_SIZE 3584
#define MT7915_EEPROM_BLOCK_SIZE 16
#define MT7915_TOKEN_SIZE 8192
Expand Down Expand Up @@ -423,6 +426,7 @@ int mt7915_mcu_set_fixed_rate_ctrl(struct mt7915_dev *dev,
void *data, u32 field);
int mt7915_mcu_set_eeprom(struct mt7915_dev *dev);
int mt7915_mcu_get_eeprom(struct mt7915_dev *dev, u32 offset);
int mt7915_mcu_get_eeprom_free_block(struct mt7915_dev *dev, u8 *block_num);
int mt7915_mcu_set_mac(struct mt7915_dev *dev, int band, bool enable,
bool hdr_trans);
int mt7915_mcu_set_test_param(struct mt7915_dev *dev, u8 param, bool test_mode,
Expand Down

0 comments on commit f1520c9

Please sign in to comment.