18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: ISC */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
48c2ecf20Sopenharmony_ci * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef __MT76x02_EEPROM_H
88c2ecf20Sopenharmony_ci#define __MT76x02_EEPROM_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "mt76x02.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cienum mt76x02_eeprom_field {
138c2ecf20Sopenharmony_ci	MT_EE_CHIP_ID =				0x000,
148c2ecf20Sopenharmony_ci	MT_EE_VERSION =				0x002,
158c2ecf20Sopenharmony_ci	MT_EE_MAC_ADDR =			0x004,
168c2ecf20Sopenharmony_ci	MT_EE_PCI_ID =				0x00A,
178c2ecf20Sopenharmony_ci	MT_EE_ANTENNA =				0x022,
188c2ecf20Sopenharmony_ci	MT_EE_CFG1_INIT =			0x024,
198c2ecf20Sopenharmony_ci	MT_EE_NIC_CONF_0 =			0x034,
208c2ecf20Sopenharmony_ci	MT_EE_NIC_CONF_1 =			0x036,
218c2ecf20Sopenharmony_ci	MT_EE_COUNTRY_REGION_5GHZ =		0x038,
228c2ecf20Sopenharmony_ci	MT_EE_COUNTRY_REGION_2GHZ =		0x039,
238c2ecf20Sopenharmony_ci	MT_EE_FREQ_OFFSET =			0x03a,
248c2ecf20Sopenharmony_ci	MT_EE_NIC_CONF_2 =			0x042,
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	MT_EE_XTAL_TRIM_1 =			0x03a,
278c2ecf20Sopenharmony_ci	MT_EE_XTAL_TRIM_2 =			0x09e,
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	MT_EE_LNA_GAIN =			0x044,
308c2ecf20Sopenharmony_ci	MT_EE_RSSI_OFFSET_2G_0 =		0x046,
318c2ecf20Sopenharmony_ci	MT_EE_RSSI_OFFSET_2G_1 =		0x048,
328c2ecf20Sopenharmony_ci	MT_EE_LNA_GAIN_5GHZ_1 =			0x049,
338c2ecf20Sopenharmony_ci	MT_EE_RSSI_OFFSET_5G_0 =		0x04a,
348c2ecf20Sopenharmony_ci	MT_EE_RSSI_OFFSET_5G_1 =		0x04c,
358c2ecf20Sopenharmony_ci	MT_EE_LNA_GAIN_5GHZ_2 =			0x04d,
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_DELTA_BW40 =		0x050,
388c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_DELTA_BW80 =		0x052,
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_EXT_PA_5G =		0x054,
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_0_START_2G =		0x056,
438c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_1_START_2G =		0x05c,
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* used as byte arrays */
468c2ecf20Sopenharmony_ci#define MT_TX_POWER_GROUP_SIZE_5G		5
478c2ecf20Sopenharmony_ci#define MT_TX_POWER_GROUPS_5G			6
488c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_0_START_5G =		0x062,
498c2ecf20Sopenharmony_ci	MT_EE_TSSI_SLOPE_2G =			0x06e,
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_0_GRP3_TX_POWER_DELTA =	0x074,
528c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_0_GRP4_TSSI_SLOPE =	0x076,
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_1_START_5G =		0x080,
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_CCK =			0x0a0,
578c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_OFDM_2G_6M =		0x0a2,
588c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_OFDM_2G_24M =		0x0a4,
598c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_OFDM_5G_6M =		0x0b2,
608c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_OFDM_5G_24M =		0x0b4,
618c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_HT_MCS0 =		0x0a6,
628c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_HT_MCS4 =		0x0a8,
638c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_HT_MCS8 =		0x0aa,
648c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_HT_MCS12 =		0x0ac,
658c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_VHT_MCS0 =		0x0ba,
668c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_VHT_MCS4 =		0x0bc,
678c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_VHT_MCS8 =		0x0be,
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	MT_EE_2G_TARGET_POWER =			0x0d0,
708c2ecf20Sopenharmony_ci	MT_EE_TEMP_OFFSET =			0x0d1,
718c2ecf20Sopenharmony_ci	MT_EE_5G_TARGET_POWER =			0x0d2,
728c2ecf20Sopenharmony_ci	MT_EE_TSSI_BOUND1 =			0x0d4,
738c2ecf20Sopenharmony_ci	MT_EE_TSSI_BOUND2 =			0x0d6,
748c2ecf20Sopenharmony_ci	MT_EE_TSSI_BOUND3 =			0x0d8,
758c2ecf20Sopenharmony_ci	MT_EE_TSSI_BOUND4 =			0x0da,
768c2ecf20Sopenharmony_ci	MT_EE_FREQ_OFFSET_COMPENSATION =	0x0db,
778c2ecf20Sopenharmony_ci	MT_EE_TSSI_BOUND5 =			0x0dc,
788c2ecf20Sopenharmony_ci	MT_EE_TX_POWER_BYRATE_BASE =		0x0de,
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	MT_EE_TSSI_SLOPE_5G =			0x0f0,
818c2ecf20Sopenharmony_ci	MT_EE_RF_TEMP_COMP_SLOPE_5G =		0x0f2,
828c2ecf20Sopenharmony_ci	MT_EE_RF_TEMP_COMP_SLOPE_2G =		0x0f4,
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	MT_EE_RF_2G_TSSI_OFF_TXPOWER =		0x0f6,
858c2ecf20Sopenharmony_ci	MT_EE_RF_2G_RX_HIGH_GAIN =		0x0f8,
868c2ecf20Sopenharmony_ci	MT_EE_RF_5G_GRP0_1_RX_HIGH_GAIN =	0x0fa,
878c2ecf20Sopenharmony_ci	MT_EE_RF_5G_GRP2_3_RX_HIGH_GAIN =	0x0fc,
888c2ecf20Sopenharmony_ci	MT_EE_RF_5G_GRP4_5_RX_HIGH_GAIN =	0x0fe,
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	MT_EE_BT_RCAL_RESULT =			0x138,
918c2ecf20Sopenharmony_ci	MT_EE_BT_VCDL_CALIBRATION =		0x13c,
928c2ecf20Sopenharmony_ci	MT_EE_BT_PMUCFG =			0x13e,
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	MT_EE_USAGE_MAP_START =			0x1e0,
958c2ecf20Sopenharmony_ci	MT_EE_USAGE_MAP_END =			0x1fc,
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	__MT_EE_MAX
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define MT_EE_ANTENNA_DUAL			BIT(15)
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_0_RX_PATH		GENMASK(3, 0)
1038c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_0_TX_PATH		GENMASK(7, 4)
1048c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_0_PA_TYPE		GENMASK(9, 8)
1058c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_0_PA_INT_2G		BIT(8)
1068c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_0_PA_INT_5G		BIT(9)
1078c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_0_PA_IO_CURRENT		BIT(10)
1088c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_0_BOARD_TYPE		GENMASK(13, 12)
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_1_HW_RF_CTRL		BIT(0)
1118c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_1_TEMP_TX_ALC		BIT(1)
1128c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_1_LNA_EXT_2G		BIT(2)
1138c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_1_LNA_EXT_5G		BIT(3)
1148c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_1_TX_ALC_EN		BIT(13)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_2_ANT_OPT		BIT(3)
1178c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_2_ANT_DIV		BIT(4)
1188c2ecf20Sopenharmony_ci#define MT_EE_NIC_CONF_2_XTAL_OPTION		GENMASK(10, 9)
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define MT_EFUSE_USAGE_MAP_SIZE			(MT_EE_USAGE_MAP_END - \
1218c2ecf20Sopenharmony_ci						 MT_EE_USAGE_MAP_START + 1)
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cienum mt76x02_eeprom_modes {
1248c2ecf20Sopenharmony_ci	MT_EE_READ,
1258c2ecf20Sopenharmony_ci	MT_EE_PHYSICAL_READ,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cienum mt76x02_board_type {
1298c2ecf20Sopenharmony_ci	BOARD_TYPE_2GHZ = 1,
1308c2ecf20Sopenharmony_ci	BOARD_TYPE_5GHZ = 2,
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic inline bool mt76x02_field_valid(u8 val)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return val != 0 && val != 0xff;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic inline int
1398c2ecf20Sopenharmony_cimt76x02_sign_extend(u32 val, unsigned int size)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	bool sign = val & BIT(size - 1);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	val &= BIT(size - 1) - 1;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	return sign ? val : -val;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic inline int
1498c2ecf20Sopenharmony_cimt76x02_sign_extend_optional(u32 val, unsigned int size)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	bool enable = val & BIT(size);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return enable ? mt76x02_sign_extend(val, size) : 0;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic inline s8 mt76x02_rate_power_val(u8 val)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	if (!mt76x02_field_valid(val))
1598c2ecf20Sopenharmony_ci		return 0;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return mt76x02_sign_extend_optional(val, 7);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic inline int
1658c2ecf20Sopenharmony_cimt76x02_eeprom_get(struct mt76x02_dev *dev,
1668c2ecf20Sopenharmony_ci		   enum mt76x02_eeprom_field field)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	if ((field & 1) || field >= __MT_EE_MAX)
1698c2ecf20Sopenharmony_ci		return -1;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	return get_unaligned_le16(dev->mt76.eeprom.data + field);
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cibool mt76x02_ext_pa_enabled(struct mt76x02_dev *dev, enum nl80211_band band);
1758c2ecf20Sopenharmony_ciint mt76x02_get_efuse_data(struct mt76x02_dev *dev, u16 base, void *buf,
1768c2ecf20Sopenharmony_ci			   int len, enum mt76x02_eeprom_modes mode);
1778c2ecf20Sopenharmony_civoid mt76x02_get_rx_gain(struct mt76x02_dev *dev, enum nl80211_band band,
1788c2ecf20Sopenharmony_ci			 u16 *rssi_offset, s8 *lna_2g, s8 *lna_5g);
1798c2ecf20Sopenharmony_ciu8 mt76x02_get_lna_gain(struct mt76x02_dev *dev,
1808c2ecf20Sopenharmony_ci			s8 *lna_2g, s8 *lna_5g,
1818c2ecf20Sopenharmony_ci			struct ieee80211_channel *chan);
1828c2ecf20Sopenharmony_civoid mt76x02_eeprom_parse_hw_cap(struct mt76x02_dev *dev);
1838c2ecf20Sopenharmony_ciint mt76x02_eeprom_copy(struct mt76x02_dev *dev,
1848c2ecf20Sopenharmony_ci			enum mt76x02_eeprom_field field,
1858c2ecf20Sopenharmony_ci			void *dest, int len);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci#endif /* __MT76x02_EEPROM_H */
188