162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci#include <linux/bitfield.h>
362306a36Sopenharmony_ci#include <linux/bitmap.h>
462306a36Sopenharmony_ci#include <linux/mfd/syscon.h>
562306a36Sopenharmony_ci#include <linux/module.h>
662306a36Sopenharmony_ci#include <linux/nvmem-consumer.h>
762306a36Sopenharmony_ci#include <linux/pinctrl/consumer.h>
862306a36Sopenharmony_ci#include <linux/phy.h>
962306a36Sopenharmony_ci#include <linux/regmap.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#define MTK_GPHY_ID_MT7981			0x03a29461
1262306a36Sopenharmony_ci#define MTK_GPHY_ID_MT7988			0x03a29481
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#define MTK_EXT_PAGE_ACCESS			0x1f
1562306a36Sopenharmony_ci#define MTK_PHY_PAGE_STANDARD			0x0000
1662306a36Sopenharmony_ci#define MTK_PHY_PAGE_EXTENDED_3			0x0003
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define MTK_PHY_LPI_REG_14			0x14
1962306a36Sopenharmony_ci#define MTK_PHY_LPI_WAKE_TIMER_1000_MASK	GENMASK(8, 0)
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define MTK_PHY_LPI_REG_1c			0x1c
2262306a36Sopenharmony_ci#define MTK_PHY_SMI_DET_ON_THRESH_MASK		GENMASK(13, 8)
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define MTK_PHY_PAGE_EXTENDED_2A30		0x2a30
2562306a36Sopenharmony_ci#define MTK_PHY_PAGE_EXTENDED_52B5		0x52b5
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define ANALOG_INTERNAL_OPERATION_MAX_US	20
2862306a36Sopenharmony_ci#define TXRESERVE_MIN				0
2962306a36Sopenharmony_ci#define TXRESERVE_MAX				7
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#define MTK_PHY_ANARG_RG			0x10
3262306a36Sopenharmony_ci#define   MTK_PHY_TCLKOFFSET_MASK		GENMASK(12, 8)
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/* Registers on MDIO_MMD_VEND1 */
3562306a36Sopenharmony_ci#define MTK_PHY_TXVLD_DA_RG			0x12
3662306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_A_GBE_MASK	GENMASK(15, 10)
3762306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_A_TBT_MASK	GENMASK(5, 0)
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#define MTK_PHY_TX_I2MPB_TEST_MODE_A2		0x16
4062306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_A_HBT_MASK	GENMASK(15, 10)
4162306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_A_TST_MASK	GENMASK(5, 0)
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci#define MTK_PHY_TX_I2MPB_TEST_MODE_B1		0x17
4462306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_B_GBE_MASK	GENMASK(13, 8)
4562306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_B_TBT_MASK	GENMASK(5, 0)
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define MTK_PHY_TX_I2MPB_TEST_MODE_B2		0x18
4862306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_B_HBT_MASK	GENMASK(13, 8)
4962306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_B_TST_MASK	GENMASK(5, 0)
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci#define MTK_PHY_TX_I2MPB_TEST_MODE_C1		0x19
5262306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_C_GBE_MASK	GENMASK(13, 8)
5362306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_C_TBT_MASK	GENMASK(5, 0)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define MTK_PHY_TX_I2MPB_TEST_MODE_C2		0x20
5662306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_C_HBT_MASK	GENMASK(13, 8)
5762306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_C_TST_MASK	GENMASK(5, 0)
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci#define MTK_PHY_TX_I2MPB_TEST_MODE_D1		0x21
6062306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_D_GBE_MASK	GENMASK(13, 8)
6162306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_D_TBT_MASK	GENMASK(5, 0)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci#define MTK_PHY_TX_I2MPB_TEST_MODE_D2		0x22
6462306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_D_HBT_MASK	GENMASK(13, 8)
6562306a36Sopenharmony_ci#define   MTK_PHY_DA_TX_I2MPB_D_TST_MASK	GENMASK(5, 0)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define MTK_PHY_RXADC_CTRL_RG7			0xc6
6862306a36Sopenharmony_ci#define   MTK_PHY_DA_AD_BUF_BIAS_LP_MASK	GENMASK(9, 8)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#define MTK_PHY_RXADC_CTRL_RG9			0xc8
7162306a36Sopenharmony_ci#define   MTK_PHY_DA_RX_PSBN_TBT_MASK		GENMASK(14, 12)
7262306a36Sopenharmony_ci#define   MTK_PHY_DA_RX_PSBN_HBT_MASK		GENMASK(10, 8)
7362306a36Sopenharmony_ci#define   MTK_PHY_DA_RX_PSBN_GBE_MASK		GENMASK(6, 4)
7462306a36Sopenharmony_ci#define   MTK_PHY_DA_RX_PSBN_LP_MASK		GENMASK(2, 0)
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#define MTK_PHY_LDO_OUTPUT_V			0xd7
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci#define MTK_PHY_RG_ANA_CAL_RG0			0xdb
7962306a36Sopenharmony_ci#define   MTK_PHY_RG_CAL_CKINV			BIT(12)
8062306a36Sopenharmony_ci#define   MTK_PHY_RG_ANA_CALEN			BIT(8)
8162306a36Sopenharmony_ci#define   MTK_PHY_RG_ZCALEN_A			BIT(0)
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci#define MTK_PHY_RG_ANA_CAL_RG1			0xdc
8462306a36Sopenharmony_ci#define   MTK_PHY_RG_ZCALEN_B			BIT(12)
8562306a36Sopenharmony_ci#define   MTK_PHY_RG_ZCALEN_C			BIT(8)
8662306a36Sopenharmony_ci#define   MTK_PHY_RG_ZCALEN_D			BIT(4)
8762306a36Sopenharmony_ci#define   MTK_PHY_RG_TXVOS_CALEN		BIT(0)
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#define MTK_PHY_RG_ANA_CAL_RG5			0xe0
9062306a36Sopenharmony_ci#define   MTK_PHY_RG_REXT_TRIM_MASK		GENMASK(13, 8)
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define MTK_PHY_RG_TX_FILTER			0xfe
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci#define MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG120	0x120
9562306a36Sopenharmony_ci#define   MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK	GENMASK(12, 8)
9662306a36Sopenharmony_ci#define   MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK	GENMASK(4, 0)
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci#define MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122	0x122
9962306a36Sopenharmony_ci#define   MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK	GENMASK(7, 0)
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#define MTK_PHY_RG_TESTMUX_ADC_CTRL		0x144
10262306a36Sopenharmony_ci#define   MTK_PHY_RG_TXEN_DIG_MASK		GENMASK(5, 5)
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B		0x172
10562306a36Sopenharmony_ci#define   MTK_PHY_CR_TX_AMP_OFFSET_A_MASK	GENMASK(13, 8)
10662306a36Sopenharmony_ci#define   MTK_PHY_CR_TX_AMP_OFFSET_B_MASK	GENMASK(6, 0)
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci#define MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D		0x173
10962306a36Sopenharmony_ci#define   MTK_PHY_CR_TX_AMP_OFFSET_C_MASK	GENMASK(13, 8)
11062306a36Sopenharmony_ci#define   MTK_PHY_CR_TX_AMP_OFFSET_D_MASK	GENMASK(6, 0)
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#define MTK_PHY_RG_AD_CAL_COMP			0x17a
11362306a36Sopenharmony_ci#define   MTK_PHY_AD_CAL_COMP_OUT_SHIFT		(8)
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define MTK_PHY_RG_AD_CAL_CLK			0x17b
11662306a36Sopenharmony_ci#define   MTK_PHY_DA_CAL_CLK			BIT(0)
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci#define MTK_PHY_RG_AD_CALIN			0x17c
11962306a36Sopenharmony_ci#define   MTK_PHY_DA_CALIN_FLAG			BIT(0)
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#define MTK_PHY_RG_DASN_DAC_IN0_A		0x17d
12262306a36Sopenharmony_ci#define   MTK_PHY_DASN_DAC_IN0_A_MASK		GENMASK(9, 0)
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci#define MTK_PHY_RG_DASN_DAC_IN0_B		0x17e
12562306a36Sopenharmony_ci#define   MTK_PHY_DASN_DAC_IN0_B_MASK		GENMASK(9, 0)
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci#define MTK_PHY_RG_DASN_DAC_IN0_C		0x17f
12862306a36Sopenharmony_ci#define   MTK_PHY_DASN_DAC_IN0_C_MASK		GENMASK(9, 0)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#define MTK_PHY_RG_DASN_DAC_IN0_D		0x180
13162306a36Sopenharmony_ci#define   MTK_PHY_DASN_DAC_IN0_D_MASK		GENMASK(9, 0)
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci#define MTK_PHY_RG_DASN_DAC_IN1_A		0x181
13462306a36Sopenharmony_ci#define   MTK_PHY_DASN_DAC_IN1_A_MASK		GENMASK(9, 0)
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#define MTK_PHY_RG_DASN_DAC_IN1_B		0x182
13762306a36Sopenharmony_ci#define   MTK_PHY_DASN_DAC_IN1_B_MASK		GENMASK(9, 0)
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#define MTK_PHY_RG_DASN_DAC_IN1_C		0x183
14062306a36Sopenharmony_ci#define   MTK_PHY_DASN_DAC_IN1_C_MASK		GENMASK(9, 0)
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci#define MTK_PHY_RG_DASN_DAC_IN1_D		0x184
14362306a36Sopenharmony_ci#define   MTK_PHY_DASN_DAC_IN1_D_MASK		GENMASK(9, 0)
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG19b			0x19b
14662306a36Sopenharmony_ci#define   MTK_PHY_BYPASS_DSP_LPI_READY		BIT(8)
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K1_L			0x22a
14962306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K1_U			0x22b
15062306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K2_L			0x22c
15162306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K2_U			0x22d
15262306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K3_L			0x22e
15362306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K3_U			0x22f
15462306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K4_L			0x230
15562306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K4_U			0x231
15662306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K5_L			0x232
15762306a36Sopenharmony_ci#define MTK_PHY_RG_LP_IIR2_K5_U			0x233
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG234			0x234
16062306a36Sopenharmony_ci#define   MTK_PHY_TR_OPEN_LOOP_EN_MASK		GENMASK(0, 0)
16162306a36Sopenharmony_ci#define   MTK_PHY_LPF_X_AVERAGE_MASK		GENMASK(7, 4)
16262306a36Sopenharmony_ci#define   MTK_PHY_TR_LP_IIR_EEE_EN		BIT(12)
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci#define MTK_PHY_RG_LPF_CNT_VAL			0x235
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG238			0x238
16762306a36Sopenharmony_ci#define   MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK	GENMASK(8, 0)
16862306a36Sopenharmony_ci#define   MTK_PHY_LPI_SLV_SEND_TX_EN		BIT(12)
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG239			0x239
17162306a36Sopenharmony_ci#define   MTK_PHY_LPI_SEND_LOC_TIMER_MASK	GENMASK(8, 0)
17262306a36Sopenharmony_ci#define   MTK_PHY_LPI_TXPCS_LOC_RCV		BIT(12)
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG27C			0x27c
17562306a36Sopenharmony_ci#define   MTK_PHY_VGASTATE_FFE_THR_ST1_MASK	GENMASK(12, 8)
17662306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG27D			0x27d
17762306a36Sopenharmony_ci#define   MTK_PHY_VGASTATE_FFE_THR_ST2_MASK	GENMASK(4, 0)
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG2C7			0x2c7
18062306a36Sopenharmony_ci#define   MTK_PHY_MAX_GAIN_MASK			GENMASK(4, 0)
18162306a36Sopenharmony_ci#define   MTK_PHY_MIN_GAIN_MASK			GENMASK(12, 8)
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG2D1			0x2d1
18462306a36Sopenharmony_ci#define   MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK	GENMASK(7, 0)
18562306a36Sopenharmony_ci#define   MTK_PHY_LPI_SKIP_SD_SLV_TR		BIT(8)
18662306a36Sopenharmony_ci#define   MTK_PHY_LPI_TR_READY			BIT(9)
18762306a36Sopenharmony_ci#define   MTK_PHY_LPI_VCO_EEE_STG0_EN		BIT(10)
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG323			0x323
19062306a36Sopenharmony_ci#define   MTK_PHY_EEE_WAKE_MAS_INT_DC		BIT(0)
19162306a36Sopenharmony_ci#define   MTK_PHY_EEE_WAKE_SLV_INT_DC		BIT(4)
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG324			0x324
19462306a36Sopenharmony_ci#define   MTK_PHY_SMI_DETCNT_MAX_MASK		GENMASK(5, 0)
19562306a36Sopenharmony_ci#define   MTK_PHY_SMI_DET_MAX_EN		BIT(8)
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci#define MTK_PHY_RG_DEV1E_REG326			0x326
19862306a36Sopenharmony_ci#define   MTK_PHY_LPI_MODE_SD_ON		BIT(0)
19962306a36Sopenharmony_ci#define   MTK_PHY_RESET_RANDUPD_CNT		BIT(1)
20062306a36Sopenharmony_ci#define   MTK_PHY_TREC_UPDATE_ENAB_CLR		BIT(2)
20162306a36Sopenharmony_ci#define   MTK_PHY_LPI_QUIT_WAIT_DFE_SIG_DET_OFF	BIT(4)
20262306a36Sopenharmony_ci#define   MTK_PHY_TR_READY_SKIP_AFE_WAKEUP	BIT(5)
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci#define MTK_PHY_LDO_PUMP_EN_PAIRAB		0x502
20562306a36Sopenharmony_ci#define MTK_PHY_LDO_PUMP_EN_PAIRCD		0x503
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci#define MTK_PHY_DA_TX_R50_PAIR_A		0x53d
20862306a36Sopenharmony_ci#define MTK_PHY_DA_TX_R50_PAIR_B		0x53e
20962306a36Sopenharmony_ci#define MTK_PHY_DA_TX_R50_PAIR_C		0x53f
21062306a36Sopenharmony_ci#define MTK_PHY_DA_TX_R50_PAIR_D		0x540
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci/* Registers on MDIO_MMD_VEND2 */
21362306a36Sopenharmony_ci#define MTK_PHY_LED0_ON_CTRL			0x24
21462306a36Sopenharmony_ci#define MTK_PHY_LED1_ON_CTRL			0x26
21562306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_MASK			GENMASK(6, 0)
21662306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_LINK1000		BIT(0)
21762306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_LINK100		BIT(1)
21862306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_LINK10			BIT(2)
21962306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_LINKDOWN		BIT(3)
22062306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_FDX			BIT(4) /* Full duplex */
22162306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_HDX			BIT(5) /* Half duplex */
22262306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_FORCE_ON		BIT(6)
22362306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_POLARITY		BIT(14)
22462306a36Sopenharmony_ci#define   MTK_PHY_LED_ON_ENABLE			BIT(15)
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci#define MTK_PHY_LED0_BLINK_CTRL			0x25
22762306a36Sopenharmony_ci#define MTK_PHY_LED1_BLINK_CTRL			0x27
22862306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_1000TX		BIT(0)
22962306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_1000RX		BIT(1)
23062306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_100TX		BIT(2)
23162306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_100RX		BIT(3)
23262306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_10TX		BIT(4)
23362306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_10RX		BIT(5)
23462306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_COLLISION		BIT(6)
23562306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_RX_CRC_ERR		BIT(7)
23662306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_RX_IDLE_ERR		BIT(8)
23762306a36Sopenharmony_ci#define   MTK_PHY_LED_BLINK_FORCE_BLINK		BIT(9)
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci#define MTK_PHY_LED1_DEFAULT_POLARITIES		BIT(1)
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci#define MTK_PHY_RG_BG_RASEL			0x115
24262306a36Sopenharmony_ci#define   MTK_PHY_RG_BG_RASEL_MASK		GENMASK(2, 0)
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci/* 'boottrap' register reflecting the configuration of the 4 PHY LEDs */
24562306a36Sopenharmony_ci#define RG_GPIO_MISC_TPBANK0			0x6f0
24662306a36Sopenharmony_ci#define   RG_GPIO_MISC_TPBANK0_BOOTMODE		GENMASK(11, 8)
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci/* These macro privides efuse parsing for internal phy. */
24962306a36Sopenharmony_ci#define EFS_DA_TX_I2MPB_A(x)			(((x) >> 0) & GENMASK(5, 0))
25062306a36Sopenharmony_ci#define EFS_DA_TX_I2MPB_B(x)			(((x) >> 6) & GENMASK(5, 0))
25162306a36Sopenharmony_ci#define EFS_DA_TX_I2MPB_C(x)			(((x) >> 12) & GENMASK(5, 0))
25262306a36Sopenharmony_ci#define EFS_DA_TX_I2MPB_D(x)			(((x) >> 18) & GENMASK(5, 0))
25362306a36Sopenharmony_ci#define EFS_DA_TX_AMP_OFFSET_A(x)		(((x) >> 24) & GENMASK(5, 0))
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci#define EFS_DA_TX_AMP_OFFSET_B(x)		(((x) >> 0) & GENMASK(5, 0))
25662306a36Sopenharmony_ci#define EFS_DA_TX_AMP_OFFSET_C(x)		(((x) >> 6) & GENMASK(5, 0))
25762306a36Sopenharmony_ci#define EFS_DA_TX_AMP_OFFSET_D(x)		(((x) >> 12) & GENMASK(5, 0))
25862306a36Sopenharmony_ci#define EFS_DA_TX_R50_A(x)			(((x) >> 18) & GENMASK(5, 0))
25962306a36Sopenharmony_ci#define EFS_DA_TX_R50_B(x)			(((x) >> 24) & GENMASK(5, 0))
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci#define EFS_DA_TX_R50_C(x)			(((x) >> 0) & GENMASK(5, 0))
26262306a36Sopenharmony_ci#define EFS_DA_TX_R50_D(x)			(((x) >> 6) & GENMASK(5, 0))
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci#define EFS_RG_BG_RASEL(x)			(((x) >> 4) & GENMASK(2, 0))
26562306a36Sopenharmony_ci#define EFS_RG_REXT_TRIM(x)			(((x) >> 7) & GENMASK(5, 0))
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_cienum {
26862306a36Sopenharmony_ci	NO_PAIR,
26962306a36Sopenharmony_ci	PAIR_A,
27062306a36Sopenharmony_ci	PAIR_B,
27162306a36Sopenharmony_ci	PAIR_C,
27262306a36Sopenharmony_ci	PAIR_D,
27362306a36Sopenharmony_ci};
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_cienum calibration_mode {
27662306a36Sopenharmony_ci	EFUSE_K,
27762306a36Sopenharmony_ci	SW_K
27862306a36Sopenharmony_ci};
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_cienum CAL_ITEM {
28162306a36Sopenharmony_ci	REXT,
28262306a36Sopenharmony_ci	TX_OFFSET,
28362306a36Sopenharmony_ci	TX_AMP,
28462306a36Sopenharmony_ci	TX_R50,
28562306a36Sopenharmony_ci	TX_VCM
28662306a36Sopenharmony_ci};
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_cienum CAL_MODE {
28962306a36Sopenharmony_ci	EFUSE_M,
29062306a36Sopenharmony_ci	SW_M
29162306a36Sopenharmony_ci};
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci#define MTK_PHY_LED_STATE_FORCE_ON	0
29462306a36Sopenharmony_ci#define MTK_PHY_LED_STATE_FORCE_BLINK	1
29562306a36Sopenharmony_ci#define MTK_PHY_LED_STATE_NETDEV	2
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_cistruct mtk_socphy_priv {
29862306a36Sopenharmony_ci	unsigned long		led_state;
29962306a36Sopenharmony_ci};
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_cistruct mtk_socphy_shared {
30262306a36Sopenharmony_ci	u32			boottrap;
30362306a36Sopenharmony_ci	struct mtk_socphy_priv	priv[4];
30462306a36Sopenharmony_ci};
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cistatic int mtk_socphy_read_page(struct phy_device *phydev)
30762306a36Sopenharmony_ci{
30862306a36Sopenharmony_ci	return __phy_read(phydev, MTK_EXT_PAGE_ACCESS);
30962306a36Sopenharmony_ci}
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_cistatic int mtk_socphy_write_page(struct phy_device *phydev, int page)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	return __phy_write(phydev, MTK_EXT_PAGE_ACCESS, page);
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci/* One calibration cycle consists of:
31762306a36Sopenharmony_ci * 1.Set DA_CALIN_FLAG high to start calibration. Keep it high
31862306a36Sopenharmony_ci *   until AD_CAL_COMP is ready to output calibration result.
31962306a36Sopenharmony_ci * 2.Wait until DA_CAL_CLK is available.
32062306a36Sopenharmony_ci * 3.Fetch AD_CAL_COMP_OUT.
32162306a36Sopenharmony_ci */
32262306a36Sopenharmony_cistatic int cal_cycle(struct phy_device *phydev, int devad,
32362306a36Sopenharmony_ci		     u32 regnum, u16 mask, u16 cal_val)
32462306a36Sopenharmony_ci{
32562306a36Sopenharmony_ci	int reg_val;
32662306a36Sopenharmony_ci	int ret;
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci	phy_modify_mmd(phydev, devad, regnum,
32962306a36Sopenharmony_ci		       mask, cal_val);
33062306a36Sopenharmony_ci	phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CALIN,
33162306a36Sopenharmony_ci			 MTK_PHY_DA_CALIN_FLAG);
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	ret = phy_read_mmd_poll_timeout(phydev, MDIO_MMD_VEND1,
33462306a36Sopenharmony_ci					MTK_PHY_RG_AD_CAL_CLK, reg_val,
33562306a36Sopenharmony_ci					reg_val & MTK_PHY_DA_CAL_CLK, 500,
33662306a36Sopenharmony_ci					ANALOG_INTERNAL_OPERATION_MAX_US, false);
33762306a36Sopenharmony_ci	if (ret) {
33862306a36Sopenharmony_ci		phydev_err(phydev, "Calibration cycle timeout\n");
33962306a36Sopenharmony_ci		return ret;
34062306a36Sopenharmony_ci	}
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CALIN,
34362306a36Sopenharmony_ci			   MTK_PHY_DA_CALIN_FLAG);
34462306a36Sopenharmony_ci	ret = phy_read_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CAL_COMP) >>
34562306a36Sopenharmony_ci			   MTK_PHY_AD_CAL_COMP_OUT_SHIFT;
34662306a36Sopenharmony_ci	phydev_dbg(phydev, "cal_val: 0x%x, ret: %d\n", cal_val, ret);
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	return ret;
34962306a36Sopenharmony_ci}
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_cistatic int rext_fill_result(struct phy_device *phydev, u16 *buf)
35262306a36Sopenharmony_ci{
35362306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG5,
35462306a36Sopenharmony_ci		       MTK_PHY_RG_REXT_TRIM_MASK, buf[0] << 8);
35562306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND2, MTK_PHY_RG_BG_RASEL,
35662306a36Sopenharmony_ci		       MTK_PHY_RG_BG_RASEL_MASK, buf[1]);
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	return 0;
35962306a36Sopenharmony_ci}
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_cistatic int rext_cal_efuse(struct phy_device *phydev, u32 *buf)
36262306a36Sopenharmony_ci{
36362306a36Sopenharmony_ci	u16 rext_cal_val[2];
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	rext_cal_val[0] = EFS_RG_REXT_TRIM(buf[3]);
36662306a36Sopenharmony_ci	rext_cal_val[1] = EFS_RG_BG_RASEL(buf[3]);
36762306a36Sopenharmony_ci	rext_fill_result(phydev, rext_cal_val);
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	return 0;
37062306a36Sopenharmony_ci}
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_cistatic int tx_offset_fill_result(struct phy_device *phydev, u16 *buf)
37362306a36Sopenharmony_ci{
37462306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B,
37562306a36Sopenharmony_ci		       MTK_PHY_CR_TX_AMP_OFFSET_A_MASK, buf[0] << 8);
37662306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B,
37762306a36Sopenharmony_ci		       MTK_PHY_CR_TX_AMP_OFFSET_B_MASK, buf[1]);
37862306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D,
37962306a36Sopenharmony_ci		       MTK_PHY_CR_TX_AMP_OFFSET_C_MASK, buf[2] << 8);
38062306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D,
38162306a36Sopenharmony_ci		       MTK_PHY_CR_TX_AMP_OFFSET_D_MASK, buf[3]);
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	return 0;
38462306a36Sopenharmony_ci}
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_cistatic int tx_offset_cal_efuse(struct phy_device *phydev, u32 *buf)
38762306a36Sopenharmony_ci{
38862306a36Sopenharmony_ci	u16 tx_offset_cal_val[4];
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	tx_offset_cal_val[0] = EFS_DA_TX_AMP_OFFSET_A(buf[0]);
39162306a36Sopenharmony_ci	tx_offset_cal_val[1] = EFS_DA_TX_AMP_OFFSET_B(buf[1]);
39262306a36Sopenharmony_ci	tx_offset_cal_val[2] = EFS_DA_TX_AMP_OFFSET_C(buf[1]);
39362306a36Sopenharmony_ci	tx_offset_cal_val[3] = EFS_DA_TX_AMP_OFFSET_D(buf[1]);
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci	tx_offset_fill_result(phydev, tx_offset_cal_val);
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci	return 0;
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_cistatic int tx_amp_fill_result(struct phy_device *phydev, u16 *buf)
40162306a36Sopenharmony_ci{
40262306a36Sopenharmony_ci	int i;
40362306a36Sopenharmony_ci	int bias[16] = {};
40462306a36Sopenharmony_ci	const int vals_9461[16] = { 7, 1, 4, 7,
40562306a36Sopenharmony_ci				    7, 1, 4, 7,
40662306a36Sopenharmony_ci				    7, 1, 4, 7,
40762306a36Sopenharmony_ci				    7, 1, 4, 7 };
40862306a36Sopenharmony_ci	const int vals_9481[16] = { 10, 6, 6, 10,
40962306a36Sopenharmony_ci				    10, 6, 6, 10,
41062306a36Sopenharmony_ci				    10, 6, 6, 10,
41162306a36Sopenharmony_ci				    10, 6, 6, 10 };
41262306a36Sopenharmony_ci	switch (phydev->drv->phy_id) {
41362306a36Sopenharmony_ci	case MTK_GPHY_ID_MT7981:
41462306a36Sopenharmony_ci		/* We add some calibration to efuse values
41562306a36Sopenharmony_ci		 * due to board level influence.
41662306a36Sopenharmony_ci		 * GBE: +7, TBT: +1, HBT: +4, TST: +7
41762306a36Sopenharmony_ci		 */
41862306a36Sopenharmony_ci		memcpy(bias, (const void *)vals_9461, sizeof(bias));
41962306a36Sopenharmony_ci		break;
42062306a36Sopenharmony_ci	case MTK_GPHY_ID_MT7988:
42162306a36Sopenharmony_ci		memcpy(bias, (const void *)vals_9481, sizeof(bias));
42262306a36Sopenharmony_ci		break;
42362306a36Sopenharmony_ci	}
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	/* Prevent overflow */
42662306a36Sopenharmony_ci	for (i = 0; i < 12; i++) {
42762306a36Sopenharmony_ci		if (buf[i >> 2] + bias[i] > 63) {
42862306a36Sopenharmony_ci			buf[i >> 2] = 63;
42962306a36Sopenharmony_ci			bias[i] = 0;
43062306a36Sopenharmony_ci		}
43162306a36Sopenharmony_ci	}
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TXVLD_DA_RG,
43462306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_A_GBE_MASK, (buf[0] + bias[0]) << 10);
43562306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TXVLD_DA_RG,
43662306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_A_TBT_MASK, buf[0] + bias[1]);
43762306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_A2,
43862306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_A_HBT_MASK, (buf[0] + bias[2]) << 10);
43962306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_A2,
44062306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_A_TST_MASK, buf[0] + bias[3]);
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B1,
44362306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_B_GBE_MASK, (buf[1] + bias[4]) << 8);
44462306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B1,
44562306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_B_TBT_MASK, buf[1] + bias[5]);
44662306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B2,
44762306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_B_HBT_MASK, (buf[1] + bias[6]) << 8);
44862306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B2,
44962306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_B_TST_MASK, buf[1] + bias[7]);
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C1,
45262306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_C_GBE_MASK, (buf[2] + bias[8]) << 8);
45362306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C1,
45462306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_C_TBT_MASK, buf[2] + bias[9]);
45562306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C2,
45662306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_C_HBT_MASK, (buf[2] + bias[10]) << 8);
45762306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C2,
45862306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_C_TST_MASK, buf[2] + bias[11]);
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D1,
46162306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_D_GBE_MASK, (buf[3] + bias[12]) << 8);
46262306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D1,
46362306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_D_TBT_MASK, buf[3] + bias[13]);
46462306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D2,
46562306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_D_HBT_MASK, (buf[3] + bias[14]) << 8);
46662306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D2,
46762306a36Sopenharmony_ci		       MTK_PHY_DA_TX_I2MPB_D_TST_MASK, buf[3] + bias[15]);
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci	return 0;
47062306a36Sopenharmony_ci}
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_cistatic int tx_amp_cal_efuse(struct phy_device *phydev, u32 *buf)
47362306a36Sopenharmony_ci{
47462306a36Sopenharmony_ci	u16 tx_amp_cal_val[4];
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	tx_amp_cal_val[0] = EFS_DA_TX_I2MPB_A(buf[0]);
47762306a36Sopenharmony_ci	tx_amp_cal_val[1] = EFS_DA_TX_I2MPB_B(buf[0]);
47862306a36Sopenharmony_ci	tx_amp_cal_val[2] = EFS_DA_TX_I2MPB_C(buf[0]);
47962306a36Sopenharmony_ci	tx_amp_cal_val[3] = EFS_DA_TX_I2MPB_D(buf[0]);
48062306a36Sopenharmony_ci	tx_amp_fill_result(phydev, tx_amp_cal_val);
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci	return 0;
48362306a36Sopenharmony_ci}
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_cistatic int tx_r50_fill_result(struct phy_device *phydev, u16 tx_r50_cal_val,
48662306a36Sopenharmony_ci			      u8 txg_calen_x)
48762306a36Sopenharmony_ci{
48862306a36Sopenharmony_ci	int bias = 0;
48962306a36Sopenharmony_ci	u16 reg, val;
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	if (phydev->drv->phy_id == MTK_GPHY_ID_MT7988)
49262306a36Sopenharmony_ci		bias = -1;
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci	val = clamp_val(bias + tx_r50_cal_val, 0, 63);
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci	switch (txg_calen_x) {
49762306a36Sopenharmony_ci	case PAIR_A:
49862306a36Sopenharmony_ci		reg = MTK_PHY_DA_TX_R50_PAIR_A;
49962306a36Sopenharmony_ci		break;
50062306a36Sopenharmony_ci	case PAIR_B:
50162306a36Sopenharmony_ci		reg = MTK_PHY_DA_TX_R50_PAIR_B;
50262306a36Sopenharmony_ci		break;
50362306a36Sopenharmony_ci	case PAIR_C:
50462306a36Sopenharmony_ci		reg = MTK_PHY_DA_TX_R50_PAIR_C;
50562306a36Sopenharmony_ci		break;
50662306a36Sopenharmony_ci	case PAIR_D:
50762306a36Sopenharmony_ci		reg = MTK_PHY_DA_TX_R50_PAIR_D;
50862306a36Sopenharmony_ci		break;
50962306a36Sopenharmony_ci	default:
51062306a36Sopenharmony_ci		return -EINVAL;
51162306a36Sopenharmony_ci	}
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, reg, val | val << 8);
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci	return 0;
51662306a36Sopenharmony_ci}
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_cistatic int tx_r50_cal_efuse(struct phy_device *phydev, u32 *buf,
51962306a36Sopenharmony_ci			    u8 txg_calen_x)
52062306a36Sopenharmony_ci{
52162306a36Sopenharmony_ci	u16 tx_r50_cal_val;
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci	switch (txg_calen_x) {
52462306a36Sopenharmony_ci	case PAIR_A:
52562306a36Sopenharmony_ci		tx_r50_cal_val = EFS_DA_TX_R50_A(buf[1]);
52662306a36Sopenharmony_ci		break;
52762306a36Sopenharmony_ci	case PAIR_B:
52862306a36Sopenharmony_ci		tx_r50_cal_val = EFS_DA_TX_R50_B(buf[1]);
52962306a36Sopenharmony_ci		break;
53062306a36Sopenharmony_ci	case PAIR_C:
53162306a36Sopenharmony_ci		tx_r50_cal_val = EFS_DA_TX_R50_C(buf[2]);
53262306a36Sopenharmony_ci		break;
53362306a36Sopenharmony_ci	case PAIR_D:
53462306a36Sopenharmony_ci		tx_r50_cal_val = EFS_DA_TX_R50_D(buf[2]);
53562306a36Sopenharmony_ci		break;
53662306a36Sopenharmony_ci	default:
53762306a36Sopenharmony_ci		return -EINVAL;
53862306a36Sopenharmony_ci	}
53962306a36Sopenharmony_ci	tx_r50_fill_result(phydev, tx_r50_cal_val, txg_calen_x);
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_ci	return 0;
54262306a36Sopenharmony_ci}
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_cistatic int tx_vcm_cal_sw(struct phy_device *phydev, u8 rg_txreserve_x)
54562306a36Sopenharmony_ci{
54662306a36Sopenharmony_ci	u8 lower_idx, upper_idx, txreserve_val;
54762306a36Sopenharmony_ci	u8 lower_ret, upper_ret;
54862306a36Sopenharmony_ci	int ret;
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci	phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
55162306a36Sopenharmony_ci			 MTK_PHY_RG_ANA_CALEN);
55262306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
55362306a36Sopenharmony_ci			   MTK_PHY_RG_CAL_CKINV);
55462306a36Sopenharmony_ci	phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
55562306a36Sopenharmony_ci			 MTK_PHY_RG_TXVOS_CALEN);
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci	switch (rg_txreserve_x) {
55862306a36Sopenharmony_ci	case PAIR_A:
55962306a36Sopenharmony_ci		phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
56062306a36Sopenharmony_ci				   MTK_PHY_RG_DASN_DAC_IN0_A,
56162306a36Sopenharmony_ci				   MTK_PHY_DASN_DAC_IN0_A_MASK);
56262306a36Sopenharmony_ci		phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
56362306a36Sopenharmony_ci				   MTK_PHY_RG_DASN_DAC_IN1_A,
56462306a36Sopenharmony_ci				   MTK_PHY_DASN_DAC_IN1_A_MASK);
56562306a36Sopenharmony_ci		phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
56662306a36Sopenharmony_ci				 MTK_PHY_RG_ANA_CAL_RG0,
56762306a36Sopenharmony_ci				 MTK_PHY_RG_ZCALEN_A);
56862306a36Sopenharmony_ci		break;
56962306a36Sopenharmony_ci	case PAIR_B:
57062306a36Sopenharmony_ci		phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
57162306a36Sopenharmony_ci				   MTK_PHY_RG_DASN_DAC_IN0_B,
57262306a36Sopenharmony_ci				   MTK_PHY_DASN_DAC_IN0_B_MASK);
57362306a36Sopenharmony_ci		phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
57462306a36Sopenharmony_ci				   MTK_PHY_RG_DASN_DAC_IN1_B,
57562306a36Sopenharmony_ci				   MTK_PHY_DASN_DAC_IN1_B_MASK);
57662306a36Sopenharmony_ci		phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
57762306a36Sopenharmony_ci				 MTK_PHY_RG_ANA_CAL_RG1,
57862306a36Sopenharmony_ci				 MTK_PHY_RG_ZCALEN_B);
57962306a36Sopenharmony_ci		break;
58062306a36Sopenharmony_ci	case PAIR_C:
58162306a36Sopenharmony_ci		phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
58262306a36Sopenharmony_ci				   MTK_PHY_RG_DASN_DAC_IN0_C,
58362306a36Sopenharmony_ci				   MTK_PHY_DASN_DAC_IN0_C_MASK);
58462306a36Sopenharmony_ci		phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
58562306a36Sopenharmony_ci				   MTK_PHY_RG_DASN_DAC_IN1_C,
58662306a36Sopenharmony_ci				   MTK_PHY_DASN_DAC_IN1_C_MASK);
58762306a36Sopenharmony_ci		phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
58862306a36Sopenharmony_ci				 MTK_PHY_RG_ANA_CAL_RG1,
58962306a36Sopenharmony_ci				 MTK_PHY_RG_ZCALEN_C);
59062306a36Sopenharmony_ci		break;
59162306a36Sopenharmony_ci	case PAIR_D:
59262306a36Sopenharmony_ci		phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
59362306a36Sopenharmony_ci				   MTK_PHY_RG_DASN_DAC_IN0_D,
59462306a36Sopenharmony_ci				   MTK_PHY_DASN_DAC_IN0_D_MASK);
59562306a36Sopenharmony_ci		phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
59662306a36Sopenharmony_ci				   MTK_PHY_RG_DASN_DAC_IN1_D,
59762306a36Sopenharmony_ci				   MTK_PHY_DASN_DAC_IN1_D_MASK);
59862306a36Sopenharmony_ci		phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
59962306a36Sopenharmony_ci				 MTK_PHY_RG_ANA_CAL_RG1,
60062306a36Sopenharmony_ci				 MTK_PHY_RG_ZCALEN_D);
60162306a36Sopenharmony_ci		break;
60262306a36Sopenharmony_ci	default:
60362306a36Sopenharmony_ci		ret = -EINVAL;
60462306a36Sopenharmony_ci		goto restore;
60562306a36Sopenharmony_ci	}
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci	lower_idx = TXRESERVE_MIN;
60862306a36Sopenharmony_ci	upper_idx = TXRESERVE_MAX;
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	phydev_dbg(phydev, "Start TX-VCM SW cal.\n");
61162306a36Sopenharmony_ci	while ((upper_idx - lower_idx) > 1) {
61262306a36Sopenharmony_ci		txreserve_val = DIV_ROUND_CLOSEST(lower_idx + upper_idx, 2);
61362306a36Sopenharmony_ci		ret = cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
61462306a36Sopenharmony_ci				MTK_PHY_DA_RX_PSBN_TBT_MASK |
61562306a36Sopenharmony_ci				MTK_PHY_DA_RX_PSBN_HBT_MASK |
61662306a36Sopenharmony_ci				MTK_PHY_DA_RX_PSBN_GBE_MASK |
61762306a36Sopenharmony_ci				MTK_PHY_DA_RX_PSBN_LP_MASK,
61862306a36Sopenharmony_ci				txreserve_val << 12 | txreserve_val << 8 |
61962306a36Sopenharmony_ci				txreserve_val << 4 | txreserve_val);
62062306a36Sopenharmony_ci		if (ret == 1) {
62162306a36Sopenharmony_ci			upper_idx = txreserve_val;
62262306a36Sopenharmony_ci			upper_ret = ret;
62362306a36Sopenharmony_ci		} else if (ret == 0) {
62462306a36Sopenharmony_ci			lower_idx = txreserve_val;
62562306a36Sopenharmony_ci			lower_ret = ret;
62662306a36Sopenharmony_ci		} else {
62762306a36Sopenharmony_ci			goto restore;
62862306a36Sopenharmony_ci		}
62962306a36Sopenharmony_ci	}
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci	if (lower_idx == TXRESERVE_MIN) {
63262306a36Sopenharmony_ci		lower_ret = cal_cycle(phydev, MDIO_MMD_VEND1,
63362306a36Sopenharmony_ci				      MTK_PHY_RXADC_CTRL_RG9,
63462306a36Sopenharmony_ci				      MTK_PHY_DA_RX_PSBN_TBT_MASK |
63562306a36Sopenharmony_ci				      MTK_PHY_DA_RX_PSBN_HBT_MASK |
63662306a36Sopenharmony_ci				      MTK_PHY_DA_RX_PSBN_GBE_MASK |
63762306a36Sopenharmony_ci				      MTK_PHY_DA_RX_PSBN_LP_MASK,
63862306a36Sopenharmony_ci				      lower_idx << 12 | lower_idx << 8 |
63962306a36Sopenharmony_ci				      lower_idx << 4 | lower_idx);
64062306a36Sopenharmony_ci		ret = lower_ret;
64162306a36Sopenharmony_ci	} else if (upper_idx == TXRESERVE_MAX) {
64262306a36Sopenharmony_ci		upper_ret = cal_cycle(phydev, MDIO_MMD_VEND1,
64362306a36Sopenharmony_ci				      MTK_PHY_RXADC_CTRL_RG9,
64462306a36Sopenharmony_ci				      MTK_PHY_DA_RX_PSBN_TBT_MASK |
64562306a36Sopenharmony_ci				      MTK_PHY_DA_RX_PSBN_HBT_MASK |
64662306a36Sopenharmony_ci				      MTK_PHY_DA_RX_PSBN_GBE_MASK |
64762306a36Sopenharmony_ci				      MTK_PHY_DA_RX_PSBN_LP_MASK,
64862306a36Sopenharmony_ci				      upper_idx << 12 | upper_idx << 8 |
64962306a36Sopenharmony_ci				      upper_idx << 4 | upper_idx);
65062306a36Sopenharmony_ci		ret = upper_ret;
65162306a36Sopenharmony_ci	}
65262306a36Sopenharmony_ci	if (ret < 0)
65362306a36Sopenharmony_ci		goto restore;
65462306a36Sopenharmony_ci
65562306a36Sopenharmony_ci	/* We calibrate TX-VCM in different logic. Check upper index and then
65662306a36Sopenharmony_ci	 * lower index. If this calibration is valid, apply lower index's result.
65762306a36Sopenharmony_ci	 */
65862306a36Sopenharmony_ci	ret = upper_ret - lower_ret;
65962306a36Sopenharmony_ci	if (ret == 1) {
66062306a36Sopenharmony_ci		ret = 0;
66162306a36Sopenharmony_ci		/* Make sure we use upper_idx in our calibration system */
66262306a36Sopenharmony_ci		cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
66362306a36Sopenharmony_ci			  MTK_PHY_DA_RX_PSBN_TBT_MASK |
66462306a36Sopenharmony_ci			  MTK_PHY_DA_RX_PSBN_HBT_MASK |
66562306a36Sopenharmony_ci			  MTK_PHY_DA_RX_PSBN_GBE_MASK |
66662306a36Sopenharmony_ci			  MTK_PHY_DA_RX_PSBN_LP_MASK,
66762306a36Sopenharmony_ci			  upper_idx << 12 | upper_idx << 8 |
66862306a36Sopenharmony_ci			  upper_idx << 4 | upper_idx);
66962306a36Sopenharmony_ci		phydev_dbg(phydev, "TX-VCM SW cal result: 0x%x\n", upper_idx);
67062306a36Sopenharmony_ci	} else if (lower_idx == TXRESERVE_MIN && upper_ret == 1 &&
67162306a36Sopenharmony_ci		   lower_ret == 1) {
67262306a36Sopenharmony_ci		ret = 0;
67362306a36Sopenharmony_ci		cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
67462306a36Sopenharmony_ci			  MTK_PHY_DA_RX_PSBN_TBT_MASK |
67562306a36Sopenharmony_ci			  MTK_PHY_DA_RX_PSBN_HBT_MASK |
67662306a36Sopenharmony_ci			  MTK_PHY_DA_RX_PSBN_GBE_MASK |
67762306a36Sopenharmony_ci			  MTK_PHY_DA_RX_PSBN_LP_MASK,
67862306a36Sopenharmony_ci			  lower_idx << 12 | lower_idx << 8 |
67962306a36Sopenharmony_ci			  lower_idx << 4 | lower_idx);
68062306a36Sopenharmony_ci		phydev_warn(phydev, "TX-VCM SW cal result at low margin 0x%x\n",
68162306a36Sopenharmony_ci			    lower_idx);
68262306a36Sopenharmony_ci	} else if (upper_idx == TXRESERVE_MAX && upper_ret == 0 &&
68362306a36Sopenharmony_ci		   lower_ret == 0) {
68462306a36Sopenharmony_ci		ret = 0;
68562306a36Sopenharmony_ci		phydev_warn(phydev, "TX-VCM SW cal result at high margin 0x%x\n",
68662306a36Sopenharmony_ci			    upper_idx);
68762306a36Sopenharmony_ci	} else {
68862306a36Sopenharmony_ci		ret = -EINVAL;
68962306a36Sopenharmony_ci	}
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_cirestore:
69262306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
69362306a36Sopenharmony_ci			   MTK_PHY_RG_ANA_CALEN);
69462306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
69562306a36Sopenharmony_ci			   MTK_PHY_RG_TXVOS_CALEN);
69662306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
69762306a36Sopenharmony_ci			   MTK_PHY_RG_ZCALEN_A);
69862306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
69962306a36Sopenharmony_ci			   MTK_PHY_RG_ZCALEN_B | MTK_PHY_RG_ZCALEN_C |
70062306a36Sopenharmony_ci			   MTK_PHY_RG_ZCALEN_D);
70162306a36Sopenharmony_ci
70262306a36Sopenharmony_ci	return ret;
70362306a36Sopenharmony_ci}
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_cistatic void mt798x_phy_common_finetune(struct phy_device *phydev)
70662306a36Sopenharmony_ci{
70762306a36Sopenharmony_ci	phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
70862306a36Sopenharmony_ci	/* SlvDSPreadyTime = 24, MasDSPreadyTime = 24 */
70962306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0xc71);
71062306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0xc);
71162306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8fae);
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci	/* EnabRandUpdTrig = 1 */
71462306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x2f00);
71562306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0xe);
71662306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8fb0);
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	/* NormMseLoThresh = 85 */
71962306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x55a0);
72062306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
72162306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x83aa);
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci	/* FfeUpdGainForce = 1(Enable), FfeUpdGainForceVal = 4 */
72462306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x240);
72562306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
72662306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x9680);
72762306a36Sopenharmony_ci
72862306a36Sopenharmony_ci	/* TrFreeze = 0 (mt7988 default) */
72962306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x0);
73062306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
73162306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x9686);
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci	/* SSTrKp100 = 5 */
73462306a36Sopenharmony_ci	/* SSTrKf100 = 6 */
73562306a36Sopenharmony_ci	/* SSTrKp1000Mas = 5 */
73662306a36Sopenharmony_ci	/* SSTrKf1000Mas = 6 */
73762306a36Sopenharmony_ci	/* SSTrKp1000Slv = 5 */
73862306a36Sopenharmony_ci	/* SSTrKf1000Slv = 6 */
73962306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0xbaef);
74062306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x2e);
74162306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x968c);
74262306a36Sopenharmony_ci	phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
74362306a36Sopenharmony_ci}
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_cistatic void mt7981_phy_finetune(struct phy_device *phydev)
74662306a36Sopenharmony_ci{
74762306a36Sopenharmony_ci	u16 val[8] = { 0x01ce, 0x01c1,
74862306a36Sopenharmony_ci		       0x020f, 0x0202,
74962306a36Sopenharmony_ci		       0x03d0, 0x03c0,
75062306a36Sopenharmony_ci		       0x0013, 0x0005 };
75162306a36Sopenharmony_ci	int i, k;
75262306a36Sopenharmony_ci
75362306a36Sopenharmony_ci	/* 100M eye finetune:
75462306a36Sopenharmony_ci	 * Keep middle level of TX MLT3 shapper as default.
75562306a36Sopenharmony_ci	 * Only change TX MLT3 overshoot level here.
75662306a36Sopenharmony_ci	 */
75762306a36Sopenharmony_ci	for (k = 0, i = 1; i < 12; i++) {
75862306a36Sopenharmony_ci		if (i % 3 == 0)
75962306a36Sopenharmony_ci			continue;
76062306a36Sopenharmony_ci		phy_write_mmd(phydev, MDIO_MMD_VEND1, i, val[k++]);
76162306a36Sopenharmony_ci	}
76262306a36Sopenharmony_ci
76362306a36Sopenharmony_ci	phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
76462306a36Sopenharmony_ci	/* ResetSyncOffset = 6 */
76562306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x600);
76662306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
76762306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8fc0);
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	/* VgaDecRate = 1 */
77062306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x4c2a);
77162306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x3e);
77262306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8fa4);
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci	/* MrvlTrFix100Kp = 3, MrvlTrFix100Kf = 2,
77562306a36Sopenharmony_ci	 * MrvlTrFix1000Kp = 3, MrvlTrFix1000Kf = 2
77662306a36Sopenharmony_ci	 */
77762306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0xd10a);
77862306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x34);
77962306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8f82);
78062306a36Sopenharmony_ci
78162306a36Sopenharmony_ci	/* VcoSlicerThreshBitsHigh */
78262306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x5555);
78362306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x55);
78462306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8ec0);
78562306a36Sopenharmony_ci	phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci	/* TR_OPEN_LOOP_EN = 1, lpf_x_average = 9 */
78862306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG234,
78962306a36Sopenharmony_ci		       MTK_PHY_TR_OPEN_LOOP_EN_MASK | MTK_PHY_LPF_X_AVERAGE_MASK,
79062306a36Sopenharmony_ci		       BIT(0) | FIELD_PREP(MTK_PHY_LPF_X_AVERAGE_MASK, 0x9));
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_ci	/* rg_tr_lpf_cnt_val = 512 */
79362306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LPF_CNT_VAL, 0x200);
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci	/* IIR2 related */
79662306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K1_L, 0x82);
79762306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K1_U, 0x0);
79862306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K2_L, 0x103);
79962306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K2_U, 0x0);
80062306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K3_L, 0x82);
80162306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K3_U, 0x0);
80262306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K4_L, 0xd177);
80362306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K4_U, 0x3);
80462306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K5_L, 0x2c82);
80562306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K5_U, 0xe);
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci	/* FFE peaking */
80862306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG27C,
80962306a36Sopenharmony_ci		       MTK_PHY_VGASTATE_FFE_THR_ST1_MASK, 0x1b << 8);
81062306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG27D,
81162306a36Sopenharmony_ci		       MTK_PHY_VGASTATE_FFE_THR_ST2_MASK, 0x1e);
81262306a36Sopenharmony_ci
81362306a36Sopenharmony_ci	/* Disable LDO pump */
81462306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_PUMP_EN_PAIRAB, 0x0);
81562306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_PUMP_EN_PAIRCD, 0x0);
81662306a36Sopenharmony_ci	/* Adjust LDO output voltage */
81762306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_OUTPUT_V, 0x2222);
81862306a36Sopenharmony_ci}
81962306a36Sopenharmony_ci
82062306a36Sopenharmony_cistatic void mt7988_phy_finetune(struct phy_device *phydev)
82162306a36Sopenharmony_ci{
82262306a36Sopenharmony_ci	u16 val[12] = { 0x0187, 0x01cd, 0x01c8, 0x0182,
82362306a36Sopenharmony_ci			0x020d, 0x0206, 0x0384, 0x03d0,
82462306a36Sopenharmony_ci			0x03c6, 0x030a, 0x0011, 0x0005 };
82562306a36Sopenharmony_ci	int i;
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci	/* Set default MLT3 shaper first */
82862306a36Sopenharmony_ci	for (i = 0; i < 12; i++)
82962306a36Sopenharmony_ci		phy_write_mmd(phydev, MDIO_MMD_VEND1, i, val[i]);
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ci	/* TCT finetune */
83262306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_TX_FILTER, 0x5);
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_ci	phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
83562306a36Sopenharmony_ci	/* ResetSyncOffset = 5 */
83662306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x500);
83762306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
83862306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8fc0);
83962306a36Sopenharmony_ci
84062306a36Sopenharmony_ci	/* VgaDecRate is 1 at default on mt7988 */
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci	/* MrvlTrFix100Kp = 6, MrvlTrFix100Kf = 7,
84362306a36Sopenharmony_ci	 * MrvlTrFix1000Kp = 6, MrvlTrFix1000Kf = 7
84462306a36Sopenharmony_ci	 */
84562306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0xb90a);
84662306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x6f);
84762306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8f82);
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci	/* RemAckCntLimitCtrl = 1 */
85062306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0xfbba);
85162306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0xc3);
85262306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x87f8);
85362306a36Sopenharmony_ci
85462306a36Sopenharmony_ci	phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ci	/* TR_OPEN_LOOP_EN = 1, lpf_x_average = 10 */
85762306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG234,
85862306a36Sopenharmony_ci		       MTK_PHY_TR_OPEN_LOOP_EN_MASK | MTK_PHY_LPF_X_AVERAGE_MASK,
85962306a36Sopenharmony_ci		       BIT(0) | FIELD_PREP(MTK_PHY_LPF_X_AVERAGE_MASK, 0xa));
86062306a36Sopenharmony_ci
86162306a36Sopenharmony_ci	/* rg_tr_lpf_cnt_val = 1023 */
86262306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LPF_CNT_VAL, 0x3ff);
86362306a36Sopenharmony_ci}
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_cistatic void mt798x_phy_eee(struct phy_device *phydev)
86662306a36Sopenharmony_ci{
86762306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1,
86862306a36Sopenharmony_ci		       MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG120,
86962306a36Sopenharmony_ci		       MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK |
87062306a36Sopenharmony_ci		       MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK,
87162306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK, 0x0) |
87262306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK, 0x14));
87362306a36Sopenharmony_ci
87462306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1,
87562306a36Sopenharmony_ci		       MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122,
87662306a36Sopenharmony_ci		       MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
87762306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
87862306a36Sopenharmony_ci				  0xff));
87962306a36Sopenharmony_ci
88062306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
88162306a36Sopenharmony_ci			   MTK_PHY_RG_TESTMUX_ADC_CTRL,
88262306a36Sopenharmony_ci			   MTK_PHY_RG_TXEN_DIG_MASK);
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci	phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
88562306a36Sopenharmony_ci			 MTK_PHY_RG_DEV1E_REG19b, MTK_PHY_BYPASS_DSP_LPI_READY);
88662306a36Sopenharmony_ci
88762306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
88862306a36Sopenharmony_ci			   MTK_PHY_RG_DEV1E_REG234, MTK_PHY_TR_LP_IIR_EEE_EN);
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG238,
89162306a36Sopenharmony_ci		       MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK |
89262306a36Sopenharmony_ci		       MTK_PHY_LPI_SLV_SEND_TX_EN,
89362306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK, 0x120));
89462306a36Sopenharmony_ci
89562306a36Sopenharmony_ci	/* Keep MTK_PHY_LPI_SEND_LOC_TIMER as 375 */
89662306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG239,
89762306a36Sopenharmony_ci			   MTK_PHY_LPI_TXPCS_LOC_RCV);
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci	/* This also fixes some IoT issues, such as CH340 */
90062306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG2C7,
90162306a36Sopenharmony_ci		       MTK_PHY_MAX_GAIN_MASK | MTK_PHY_MIN_GAIN_MASK,
90262306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_MAX_GAIN_MASK, 0x8) |
90362306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_MIN_GAIN_MASK, 0x13));
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG2D1,
90662306a36Sopenharmony_ci		       MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK,
90762306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK,
90862306a36Sopenharmony_ci				  0x33) |
90962306a36Sopenharmony_ci		       MTK_PHY_LPI_SKIP_SD_SLV_TR | MTK_PHY_LPI_TR_READY |
91062306a36Sopenharmony_ci		       MTK_PHY_LPI_VCO_EEE_STG0_EN);
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci	phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG323,
91362306a36Sopenharmony_ci			 MTK_PHY_EEE_WAKE_MAS_INT_DC |
91462306a36Sopenharmony_ci			 MTK_PHY_EEE_WAKE_SLV_INT_DC);
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG324,
91762306a36Sopenharmony_ci		       MTK_PHY_SMI_DETCNT_MAX_MASK,
91862306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_SMI_DETCNT_MAX_MASK, 0x3f) |
91962306a36Sopenharmony_ci		       MTK_PHY_SMI_DET_MAX_EN);
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci	phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG326,
92262306a36Sopenharmony_ci			 MTK_PHY_LPI_MODE_SD_ON | MTK_PHY_RESET_RANDUPD_CNT |
92362306a36Sopenharmony_ci			 MTK_PHY_TREC_UPDATE_ENAB_CLR |
92462306a36Sopenharmony_ci			 MTK_PHY_LPI_QUIT_WAIT_DFE_SIG_DET_OFF |
92562306a36Sopenharmony_ci			 MTK_PHY_TR_READY_SKIP_AFE_WAKEUP);
92662306a36Sopenharmony_ci
92762306a36Sopenharmony_ci	phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
92862306a36Sopenharmony_ci	/* Regsigdet_sel_1000 = 0 */
92962306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0xb);
93062306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
93162306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x9690);
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ci	/* REG_EEE_st2TrKf1000 = 2 */
93462306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x114f);
93562306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x2);
93662306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x969a);
93762306a36Sopenharmony_ci
93862306a36Sopenharmony_ci	/* RegEEE_slv_wake_tr_timer_tar = 6, RegEEE_slv_remtx_timer_tar = 20 */
93962306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x3028);
94062306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
94162306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x969e);
94262306a36Sopenharmony_ci
94362306a36Sopenharmony_ci	/* RegEEE_slv_wake_int_timer_tar = 8 */
94462306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x5010);
94562306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
94662306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x96a0);
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_ci	/* RegEEE_trfreeze_timer2 = 586 */
94962306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x24a);
95062306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
95162306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x96a8);
95262306a36Sopenharmony_ci
95362306a36Sopenharmony_ci	/* RegEEE100Stg1_tar = 16 */
95462306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x3210);
95562306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
95662306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x96b8);
95762306a36Sopenharmony_ci
95862306a36Sopenharmony_ci	/* REGEEE_wake_slv_tr_wait_dfesigdet_en = 0 */
95962306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x1463);
96062306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
96162306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x96ca);
96262306a36Sopenharmony_ci
96362306a36Sopenharmony_ci	/* DfeTailEnableVgaThresh1000 = 27 */
96462306a36Sopenharmony_ci	__phy_write(phydev, 0x11, 0x36);
96562306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x0);
96662306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8f80);
96762306a36Sopenharmony_ci	phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
96862306a36Sopenharmony_ci
96962306a36Sopenharmony_ci	phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_3);
97062306a36Sopenharmony_ci	__phy_modify(phydev, MTK_PHY_LPI_REG_14, MTK_PHY_LPI_WAKE_TIMER_1000_MASK,
97162306a36Sopenharmony_ci		     FIELD_PREP(MTK_PHY_LPI_WAKE_TIMER_1000_MASK, 0x19c));
97262306a36Sopenharmony_ci
97362306a36Sopenharmony_ci	__phy_modify(phydev, MTK_PHY_LPI_REG_1c, MTK_PHY_SMI_DET_ON_THRESH_MASK,
97462306a36Sopenharmony_ci		     FIELD_PREP(MTK_PHY_SMI_DET_ON_THRESH_MASK, 0xc));
97562306a36Sopenharmony_ci	phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1,
97862306a36Sopenharmony_ci		       MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122,
97962306a36Sopenharmony_ci		       MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
98062306a36Sopenharmony_ci		       FIELD_PREP(MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK, 0xff));
98162306a36Sopenharmony_ci}
98262306a36Sopenharmony_ci
98362306a36Sopenharmony_cistatic int cal_sw(struct phy_device *phydev, enum CAL_ITEM cal_item,
98462306a36Sopenharmony_ci		  u8 start_pair, u8 end_pair)
98562306a36Sopenharmony_ci{
98662306a36Sopenharmony_ci	u8 pair_n;
98762306a36Sopenharmony_ci	int ret;
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci	for (pair_n = start_pair; pair_n <= end_pair; pair_n++) {
99062306a36Sopenharmony_ci		/* TX_OFFSET & TX_AMP have no SW calibration. */
99162306a36Sopenharmony_ci		switch (cal_item) {
99262306a36Sopenharmony_ci		case TX_VCM:
99362306a36Sopenharmony_ci			ret = tx_vcm_cal_sw(phydev, pair_n);
99462306a36Sopenharmony_ci			break;
99562306a36Sopenharmony_ci		default:
99662306a36Sopenharmony_ci			return -EINVAL;
99762306a36Sopenharmony_ci		}
99862306a36Sopenharmony_ci		if (ret)
99962306a36Sopenharmony_ci			return ret;
100062306a36Sopenharmony_ci	}
100162306a36Sopenharmony_ci	return 0;
100262306a36Sopenharmony_ci}
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_cistatic int cal_efuse(struct phy_device *phydev, enum CAL_ITEM cal_item,
100562306a36Sopenharmony_ci		     u8 start_pair, u8 end_pair, u32 *buf)
100662306a36Sopenharmony_ci{
100762306a36Sopenharmony_ci	u8 pair_n;
100862306a36Sopenharmony_ci	int ret;
100962306a36Sopenharmony_ci
101062306a36Sopenharmony_ci	for (pair_n = start_pair; pair_n <= end_pair; pair_n++) {
101162306a36Sopenharmony_ci		/* TX_VCM has no efuse calibration. */
101262306a36Sopenharmony_ci		switch (cal_item) {
101362306a36Sopenharmony_ci		case REXT:
101462306a36Sopenharmony_ci			ret = rext_cal_efuse(phydev, buf);
101562306a36Sopenharmony_ci			break;
101662306a36Sopenharmony_ci		case TX_OFFSET:
101762306a36Sopenharmony_ci			ret = tx_offset_cal_efuse(phydev, buf);
101862306a36Sopenharmony_ci			break;
101962306a36Sopenharmony_ci		case TX_AMP:
102062306a36Sopenharmony_ci			ret = tx_amp_cal_efuse(phydev, buf);
102162306a36Sopenharmony_ci			break;
102262306a36Sopenharmony_ci		case TX_R50:
102362306a36Sopenharmony_ci			ret = tx_r50_cal_efuse(phydev, buf, pair_n);
102462306a36Sopenharmony_ci			break;
102562306a36Sopenharmony_ci		default:
102662306a36Sopenharmony_ci			return -EINVAL;
102762306a36Sopenharmony_ci		}
102862306a36Sopenharmony_ci		if (ret)
102962306a36Sopenharmony_ci			return ret;
103062306a36Sopenharmony_ci	}
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci	return 0;
103362306a36Sopenharmony_ci}
103462306a36Sopenharmony_ci
103562306a36Sopenharmony_cistatic int start_cal(struct phy_device *phydev, enum CAL_ITEM cal_item,
103662306a36Sopenharmony_ci		     enum CAL_MODE cal_mode, u8 start_pair,
103762306a36Sopenharmony_ci		     u8 end_pair, u32 *buf)
103862306a36Sopenharmony_ci{
103962306a36Sopenharmony_ci	int ret;
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	switch (cal_mode) {
104262306a36Sopenharmony_ci	case EFUSE_M:
104362306a36Sopenharmony_ci		ret = cal_efuse(phydev, cal_item, start_pair,
104462306a36Sopenharmony_ci				end_pair, buf);
104562306a36Sopenharmony_ci		break;
104662306a36Sopenharmony_ci	case SW_M:
104762306a36Sopenharmony_ci		ret = cal_sw(phydev, cal_item, start_pair, end_pair);
104862306a36Sopenharmony_ci		break;
104962306a36Sopenharmony_ci	default:
105062306a36Sopenharmony_ci		return -EINVAL;
105162306a36Sopenharmony_ci	}
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	if (ret) {
105462306a36Sopenharmony_ci		phydev_err(phydev, "cal %d failed\n", cal_item);
105562306a36Sopenharmony_ci		return -EIO;
105662306a36Sopenharmony_ci	}
105762306a36Sopenharmony_ci
105862306a36Sopenharmony_ci	return 0;
105962306a36Sopenharmony_ci}
106062306a36Sopenharmony_ci
106162306a36Sopenharmony_cistatic int mt798x_phy_calibration(struct phy_device *phydev)
106262306a36Sopenharmony_ci{
106362306a36Sopenharmony_ci	int ret = 0;
106462306a36Sopenharmony_ci	u32 *buf;
106562306a36Sopenharmony_ci	size_t len;
106662306a36Sopenharmony_ci	struct nvmem_cell *cell;
106762306a36Sopenharmony_ci
106862306a36Sopenharmony_ci	cell = nvmem_cell_get(&phydev->mdio.dev, "phy-cal-data");
106962306a36Sopenharmony_ci	if (IS_ERR(cell)) {
107062306a36Sopenharmony_ci		if (PTR_ERR(cell) == -EPROBE_DEFER)
107162306a36Sopenharmony_ci			return PTR_ERR(cell);
107262306a36Sopenharmony_ci		return 0;
107362306a36Sopenharmony_ci	}
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_ci	buf = (u32 *)nvmem_cell_read(cell, &len);
107662306a36Sopenharmony_ci	if (IS_ERR(buf))
107762306a36Sopenharmony_ci		return PTR_ERR(buf);
107862306a36Sopenharmony_ci	nvmem_cell_put(cell);
107962306a36Sopenharmony_ci
108062306a36Sopenharmony_ci	if (!buf[0] || !buf[1] || !buf[2] || !buf[3] || len < 4 * sizeof(u32)) {
108162306a36Sopenharmony_ci		phydev_err(phydev, "invalid efuse data\n");
108262306a36Sopenharmony_ci		ret = -EINVAL;
108362306a36Sopenharmony_ci		goto out;
108462306a36Sopenharmony_ci	}
108562306a36Sopenharmony_ci
108662306a36Sopenharmony_ci	ret = start_cal(phydev, REXT, EFUSE_M, NO_PAIR, NO_PAIR, buf);
108762306a36Sopenharmony_ci	if (ret)
108862306a36Sopenharmony_ci		goto out;
108962306a36Sopenharmony_ci	ret = start_cal(phydev, TX_OFFSET, EFUSE_M, NO_PAIR, NO_PAIR, buf);
109062306a36Sopenharmony_ci	if (ret)
109162306a36Sopenharmony_ci		goto out;
109262306a36Sopenharmony_ci	ret = start_cal(phydev, TX_AMP, EFUSE_M, NO_PAIR, NO_PAIR, buf);
109362306a36Sopenharmony_ci	if (ret)
109462306a36Sopenharmony_ci		goto out;
109562306a36Sopenharmony_ci	ret = start_cal(phydev, TX_R50, EFUSE_M, PAIR_A, PAIR_D, buf);
109662306a36Sopenharmony_ci	if (ret)
109762306a36Sopenharmony_ci		goto out;
109862306a36Sopenharmony_ci	ret = start_cal(phydev, TX_VCM, SW_M, PAIR_A, PAIR_A, buf);
109962306a36Sopenharmony_ci	if (ret)
110062306a36Sopenharmony_ci		goto out;
110162306a36Sopenharmony_ci
110262306a36Sopenharmony_ciout:
110362306a36Sopenharmony_ci	kfree(buf);
110462306a36Sopenharmony_ci	return ret;
110562306a36Sopenharmony_ci}
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_cistatic int mt798x_phy_config_init(struct phy_device *phydev)
110862306a36Sopenharmony_ci{
110962306a36Sopenharmony_ci	switch (phydev->drv->phy_id) {
111062306a36Sopenharmony_ci	case MTK_GPHY_ID_MT7981:
111162306a36Sopenharmony_ci		mt7981_phy_finetune(phydev);
111262306a36Sopenharmony_ci		break;
111362306a36Sopenharmony_ci	case MTK_GPHY_ID_MT7988:
111462306a36Sopenharmony_ci		mt7988_phy_finetune(phydev);
111562306a36Sopenharmony_ci		break;
111662306a36Sopenharmony_ci	}
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci	mt798x_phy_common_finetune(phydev);
111962306a36Sopenharmony_ci	mt798x_phy_eee(phydev);
112062306a36Sopenharmony_ci
112162306a36Sopenharmony_ci	return mt798x_phy_calibration(phydev);
112262306a36Sopenharmony_ci}
112362306a36Sopenharmony_ci
112462306a36Sopenharmony_cistatic int mt798x_phy_hw_led_on_set(struct phy_device *phydev, u8 index,
112562306a36Sopenharmony_ci				    bool on)
112662306a36Sopenharmony_ci{
112762306a36Sopenharmony_ci	unsigned int bit_on = MTK_PHY_LED_STATE_FORCE_ON + (index ? 16 : 0);
112862306a36Sopenharmony_ci	struct mtk_socphy_priv *priv = phydev->priv;
112962306a36Sopenharmony_ci	bool changed;
113062306a36Sopenharmony_ci
113162306a36Sopenharmony_ci	if (on)
113262306a36Sopenharmony_ci		changed = !test_and_set_bit(bit_on, &priv->led_state);
113362306a36Sopenharmony_ci	else
113462306a36Sopenharmony_ci		changed = !!test_and_clear_bit(bit_on, &priv->led_state);
113562306a36Sopenharmony_ci
113662306a36Sopenharmony_ci	changed |= !!test_and_clear_bit(MTK_PHY_LED_STATE_NETDEV +
113762306a36Sopenharmony_ci					(index ? 16 : 0), &priv->led_state);
113862306a36Sopenharmony_ci	if (changed)
113962306a36Sopenharmony_ci		return phy_modify_mmd(phydev, MDIO_MMD_VEND2, index ?
114062306a36Sopenharmony_ci				      MTK_PHY_LED1_ON_CTRL : MTK_PHY_LED0_ON_CTRL,
114162306a36Sopenharmony_ci				      MTK_PHY_LED_ON_MASK,
114262306a36Sopenharmony_ci				      on ? MTK_PHY_LED_ON_FORCE_ON : 0);
114362306a36Sopenharmony_ci	else
114462306a36Sopenharmony_ci		return 0;
114562306a36Sopenharmony_ci}
114662306a36Sopenharmony_ci
114762306a36Sopenharmony_cistatic int mt798x_phy_hw_led_blink_set(struct phy_device *phydev, u8 index,
114862306a36Sopenharmony_ci				       bool blinking)
114962306a36Sopenharmony_ci{
115062306a36Sopenharmony_ci	unsigned int bit_blink = MTK_PHY_LED_STATE_FORCE_BLINK + (index ? 16 : 0);
115162306a36Sopenharmony_ci	struct mtk_socphy_priv *priv = phydev->priv;
115262306a36Sopenharmony_ci	bool changed;
115362306a36Sopenharmony_ci
115462306a36Sopenharmony_ci	if (blinking)
115562306a36Sopenharmony_ci		changed = !test_and_set_bit(bit_blink, &priv->led_state);
115662306a36Sopenharmony_ci	else
115762306a36Sopenharmony_ci		changed = !!test_and_clear_bit(bit_blink, &priv->led_state);
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_ci	changed |= !!test_bit(MTK_PHY_LED_STATE_NETDEV +
116062306a36Sopenharmony_ci			      (index ? 16 : 0), &priv->led_state);
116162306a36Sopenharmony_ci	if (changed)
116262306a36Sopenharmony_ci		return phy_write_mmd(phydev, MDIO_MMD_VEND2, index ?
116362306a36Sopenharmony_ci				     MTK_PHY_LED1_BLINK_CTRL : MTK_PHY_LED0_BLINK_CTRL,
116462306a36Sopenharmony_ci				     blinking ? MTK_PHY_LED_BLINK_FORCE_BLINK : 0);
116562306a36Sopenharmony_ci	else
116662306a36Sopenharmony_ci		return 0;
116762306a36Sopenharmony_ci}
116862306a36Sopenharmony_ci
116962306a36Sopenharmony_cistatic int mt798x_phy_led_blink_set(struct phy_device *phydev, u8 index,
117062306a36Sopenharmony_ci				    unsigned long *delay_on,
117162306a36Sopenharmony_ci				    unsigned long *delay_off)
117262306a36Sopenharmony_ci{
117362306a36Sopenharmony_ci	bool blinking = false;
117462306a36Sopenharmony_ci	int err = 0;
117562306a36Sopenharmony_ci
117662306a36Sopenharmony_ci	if (index > 1)
117762306a36Sopenharmony_ci		return -EINVAL;
117862306a36Sopenharmony_ci
117962306a36Sopenharmony_ci	if (delay_on && delay_off && (*delay_on > 0) && (*delay_off > 0)) {
118062306a36Sopenharmony_ci		blinking = true;
118162306a36Sopenharmony_ci		*delay_on = 50;
118262306a36Sopenharmony_ci		*delay_off = 50;
118362306a36Sopenharmony_ci	}
118462306a36Sopenharmony_ci
118562306a36Sopenharmony_ci	err = mt798x_phy_hw_led_blink_set(phydev, index, blinking);
118662306a36Sopenharmony_ci	if (err)
118762306a36Sopenharmony_ci		return err;
118862306a36Sopenharmony_ci
118962306a36Sopenharmony_ci	return mt798x_phy_hw_led_on_set(phydev, index, false);
119062306a36Sopenharmony_ci}
119162306a36Sopenharmony_ci
119262306a36Sopenharmony_cistatic int mt798x_phy_led_brightness_set(struct phy_device *phydev,
119362306a36Sopenharmony_ci					 u8 index, enum led_brightness value)
119462306a36Sopenharmony_ci{
119562306a36Sopenharmony_ci	int err;
119662306a36Sopenharmony_ci
119762306a36Sopenharmony_ci	err = mt798x_phy_hw_led_blink_set(phydev, index, false);
119862306a36Sopenharmony_ci	if (err)
119962306a36Sopenharmony_ci		return err;
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_ci	return mt798x_phy_hw_led_on_set(phydev, index, (value != LED_OFF));
120262306a36Sopenharmony_ci}
120362306a36Sopenharmony_ci
120462306a36Sopenharmony_cistatic const unsigned long supported_triggers = (BIT(TRIGGER_NETDEV_FULL_DUPLEX) |
120562306a36Sopenharmony_ci						 BIT(TRIGGER_NETDEV_HALF_DUPLEX) |
120662306a36Sopenharmony_ci						 BIT(TRIGGER_NETDEV_LINK)        |
120762306a36Sopenharmony_ci						 BIT(TRIGGER_NETDEV_LINK_10)     |
120862306a36Sopenharmony_ci						 BIT(TRIGGER_NETDEV_LINK_100)    |
120962306a36Sopenharmony_ci						 BIT(TRIGGER_NETDEV_LINK_1000)   |
121062306a36Sopenharmony_ci						 BIT(TRIGGER_NETDEV_RX)          |
121162306a36Sopenharmony_ci						 BIT(TRIGGER_NETDEV_TX));
121262306a36Sopenharmony_ci
121362306a36Sopenharmony_cistatic int mt798x_phy_led_hw_is_supported(struct phy_device *phydev, u8 index,
121462306a36Sopenharmony_ci					  unsigned long rules)
121562306a36Sopenharmony_ci{
121662306a36Sopenharmony_ci	if (index > 1)
121762306a36Sopenharmony_ci		return -EINVAL;
121862306a36Sopenharmony_ci
121962306a36Sopenharmony_ci	/* All combinations of the supported triggers are allowed */
122062306a36Sopenharmony_ci	if (rules & ~supported_triggers)
122162306a36Sopenharmony_ci		return -EOPNOTSUPP;
122262306a36Sopenharmony_ci
122362306a36Sopenharmony_ci	return 0;
122462306a36Sopenharmony_ci};
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_cistatic int mt798x_phy_led_hw_control_get(struct phy_device *phydev, u8 index,
122762306a36Sopenharmony_ci					 unsigned long *rules)
122862306a36Sopenharmony_ci{
122962306a36Sopenharmony_ci	unsigned int bit_blink = MTK_PHY_LED_STATE_FORCE_BLINK + (index ? 16 : 0);
123062306a36Sopenharmony_ci	unsigned int bit_netdev = MTK_PHY_LED_STATE_NETDEV + (index ? 16 : 0);
123162306a36Sopenharmony_ci	unsigned int bit_on = MTK_PHY_LED_STATE_FORCE_ON + (index ? 16 : 0);
123262306a36Sopenharmony_ci	struct mtk_socphy_priv *priv = phydev->priv;
123362306a36Sopenharmony_ci	int on, blink;
123462306a36Sopenharmony_ci
123562306a36Sopenharmony_ci	if (index > 1)
123662306a36Sopenharmony_ci		return -EINVAL;
123762306a36Sopenharmony_ci
123862306a36Sopenharmony_ci	on = phy_read_mmd(phydev, MDIO_MMD_VEND2,
123962306a36Sopenharmony_ci			  index ? MTK_PHY_LED1_ON_CTRL : MTK_PHY_LED0_ON_CTRL);
124062306a36Sopenharmony_ci
124162306a36Sopenharmony_ci	if (on < 0)
124262306a36Sopenharmony_ci		return -EIO;
124362306a36Sopenharmony_ci
124462306a36Sopenharmony_ci	blink = phy_read_mmd(phydev, MDIO_MMD_VEND2,
124562306a36Sopenharmony_ci			     index ? MTK_PHY_LED1_BLINK_CTRL :
124662306a36Sopenharmony_ci				     MTK_PHY_LED0_BLINK_CTRL);
124762306a36Sopenharmony_ci	if (blink < 0)
124862306a36Sopenharmony_ci		return -EIO;
124962306a36Sopenharmony_ci
125062306a36Sopenharmony_ci	if ((on & (MTK_PHY_LED_ON_LINK1000 | MTK_PHY_LED_ON_LINK100 |
125162306a36Sopenharmony_ci		   MTK_PHY_LED_ON_LINK10)) ||
125262306a36Sopenharmony_ci	    (blink & (MTK_PHY_LED_BLINK_1000RX | MTK_PHY_LED_BLINK_100RX |
125362306a36Sopenharmony_ci		      MTK_PHY_LED_BLINK_10RX | MTK_PHY_LED_BLINK_1000TX |
125462306a36Sopenharmony_ci		      MTK_PHY_LED_BLINK_100TX | MTK_PHY_LED_BLINK_10TX)))
125562306a36Sopenharmony_ci		set_bit(bit_netdev, &priv->led_state);
125662306a36Sopenharmony_ci	else
125762306a36Sopenharmony_ci		clear_bit(bit_netdev, &priv->led_state);
125862306a36Sopenharmony_ci
125962306a36Sopenharmony_ci	if (on & MTK_PHY_LED_ON_FORCE_ON)
126062306a36Sopenharmony_ci		set_bit(bit_on, &priv->led_state);
126162306a36Sopenharmony_ci	else
126262306a36Sopenharmony_ci		clear_bit(bit_on, &priv->led_state);
126362306a36Sopenharmony_ci
126462306a36Sopenharmony_ci	if (blink & MTK_PHY_LED_BLINK_FORCE_BLINK)
126562306a36Sopenharmony_ci		set_bit(bit_blink, &priv->led_state);
126662306a36Sopenharmony_ci	else
126762306a36Sopenharmony_ci		clear_bit(bit_blink, &priv->led_state);
126862306a36Sopenharmony_ci
126962306a36Sopenharmony_ci	if (!rules)
127062306a36Sopenharmony_ci		return 0;
127162306a36Sopenharmony_ci
127262306a36Sopenharmony_ci	if (on & (MTK_PHY_LED_ON_LINK1000 | MTK_PHY_LED_ON_LINK100 | MTK_PHY_LED_ON_LINK10))
127362306a36Sopenharmony_ci		*rules |= BIT(TRIGGER_NETDEV_LINK);
127462306a36Sopenharmony_ci
127562306a36Sopenharmony_ci	if (on & MTK_PHY_LED_ON_LINK10)
127662306a36Sopenharmony_ci		*rules |= BIT(TRIGGER_NETDEV_LINK_10);
127762306a36Sopenharmony_ci
127862306a36Sopenharmony_ci	if (on & MTK_PHY_LED_ON_LINK100)
127962306a36Sopenharmony_ci		*rules |= BIT(TRIGGER_NETDEV_LINK_100);
128062306a36Sopenharmony_ci
128162306a36Sopenharmony_ci	if (on & MTK_PHY_LED_ON_LINK1000)
128262306a36Sopenharmony_ci		*rules |= BIT(TRIGGER_NETDEV_LINK_1000);
128362306a36Sopenharmony_ci
128462306a36Sopenharmony_ci	if (on & MTK_PHY_LED_ON_FDX)
128562306a36Sopenharmony_ci		*rules |= BIT(TRIGGER_NETDEV_FULL_DUPLEX);
128662306a36Sopenharmony_ci
128762306a36Sopenharmony_ci	if (on & MTK_PHY_LED_ON_HDX)
128862306a36Sopenharmony_ci		*rules |= BIT(TRIGGER_NETDEV_HALF_DUPLEX);
128962306a36Sopenharmony_ci
129062306a36Sopenharmony_ci	if (blink & (MTK_PHY_LED_BLINK_1000RX | MTK_PHY_LED_BLINK_100RX | MTK_PHY_LED_BLINK_10RX))
129162306a36Sopenharmony_ci		*rules |= BIT(TRIGGER_NETDEV_RX);
129262306a36Sopenharmony_ci
129362306a36Sopenharmony_ci	if (blink & (MTK_PHY_LED_BLINK_1000TX | MTK_PHY_LED_BLINK_100TX | MTK_PHY_LED_BLINK_10TX))
129462306a36Sopenharmony_ci		*rules |= BIT(TRIGGER_NETDEV_TX);
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci	return 0;
129762306a36Sopenharmony_ci};
129862306a36Sopenharmony_ci
129962306a36Sopenharmony_cistatic int mt798x_phy_led_hw_control_set(struct phy_device *phydev, u8 index,
130062306a36Sopenharmony_ci					 unsigned long rules)
130162306a36Sopenharmony_ci{
130262306a36Sopenharmony_ci	unsigned int bit_netdev = MTK_PHY_LED_STATE_NETDEV + (index ? 16 : 0);
130362306a36Sopenharmony_ci	struct mtk_socphy_priv *priv = phydev->priv;
130462306a36Sopenharmony_ci	u16 on = 0, blink = 0;
130562306a36Sopenharmony_ci	int ret;
130662306a36Sopenharmony_ci
130762306a36Sopenharmony_ci	if (index > 1)
130862306a36Sopenharmony_ci		return -EINVAL;
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci	if (rules & BIT(TRIGGER_NETDEV_FULL_DUPLEX))
131162306a36Sopenharmony_ci		on |= MTK_PHY_LED_ON_FDX;
131262306a36Sopenharmony_ci
131362306a36Sopenharmony_ci	if (rules & BIT(TRIGGER_NETDEV_HALF_DUPLEX))
131462306a36Sopenharmony_ci		on |= MTK_PHY_LED_ON_HDX;
131562306a36Sopenharmony_ci
131662306a36Sopenharmony_ci	if (rules & (BIT(TRIGGER_NETDEV_LINK_10) | BIT(TRIGGER_NETDEV_LINK)))
131762306a36Sopenharmony_ci		on |= MTK_PHY_LED_ON_LINK10;
131862306a36Sopenharmony_ci
131962306a36Sopenharmony_ci	if (rules & (BIT(TRIGGER_NETDEV_LINK_100) | BIT(TRIGGER_NETDEV_LINK)))
132062306a36Sopenharmony_ci		on |= MTK_PHY_LED_ON_LINK100;
132162306a36Sopenharmony_ci
132262306a36Sopenharmony_ci	if (rules & (BIT(TRIGGER_NETDEV_LINK_1000) | BIT(TRIGGER_NETDEV_LINK)))
132362306a36Sopenharmony_ci		on |= MTK_PHY_LED_ON_LINK1000;
132462306a36Sopenharmony_ci
132562306a36Sopenharmony_ci	if (rules & BIT(TRIGGER_NETDEV_RX)) {
132662306a36Sopenharmony_ci		blink |= MTK_PHY_LED_BLINK_10RX  |
132762306a36Sopenharmony_ci			 MTK_PHY_LED_BLINK_100RX |
132862306a36Sopenharmony_ci			 MTK_PHY_LED_BLINK_1000RX;
132962306a36Sopenharmony_ci	}
133062306a36Sopenharmony_ci
133162306a36Sopenharmony_ci	if (rules & BIT(TRIGGER_NETDEV_TX)) {
133262306a36Sopenharmony_ci		blink |= MTK_PHY_LED_BLINK_10TX  |
133362306a36Sopenharmony_ci			 MTK_PHY_LED_BLINK_100TX |
133462306a36Sopenharmony_ci			 MTK_PHY_LED_BLINK_1000TX;
133562306a36Sopenharmony_ci	}
133662306a36Sopenharmony_ci
133762306a36Sopenharmony_ci	if (blink || on)
133862306a36Sopenharmony_ci		set_bit(bit_netdev, &priv->led_state);
133962306a36Sopenharmony_ci	else
134062306a36Sopenharmony_ci		clear_bit(bit_netdev, &priv->led_state);
134162306a36Sopenharmony_ci
134262306a36Sopenharmony_ci	ret = phy_modify_mmd(phydev, MDIO_MMD_VEND2, index ?
134362306a36Sopenharmony_ci				MTK_PHY_LED1_ON_CTRL :
134462306a36Sopenharmony_ci				MTK_PHY_LED0_ON_CTRL,
134562306a36Sopenharmony_ci			     MTK_PHY_LED_ON_FDX     |
134662306a36Sopenharmony_ci			     MTK_PHY_LED_ON_HDX     |
134762306a36Sopenharmony_ci			     MTK_PHY_LED_ON_LINK10  |
134862306a36Sopenharmony_ci			     MTK_PHY_LED_ON_LINK100 |
134962306a36Sopenharmony_ci			     MTK_PHY_LED_ON_LINK1000,
135062306a36Sopenharmony_ci			     on);
135162306a36Sopenharmony_ci
135262306a36Sopenharmony_ci	if (ret)
135362306a36Sopenharmony_ci		return ret;
135462306a36Sopenharmony_ci
135562306a36Sopenharmony_ci	return phy_write_mmd(phydev, MDIO_MMD_VEND2, index ?
135662306a36Sopenharmony_ci				MTK_PHY_LED1_BLINK_CTRL :
135762306a36Sopenharmony_ci				MTK_PHY_LED0_BLINK_CTRL, blink);
135862306a36Sopenharmony_ci};
135962306a36Sopenharmony_ci
136062306a36Sopenharmony_cistatic bool mt7988_phy_led_get_polarity(struct phy_device *phydev, int led_num)
136162306a36Sopenharmony_ci{
136262306a36Sopenharmony_ci	struct mtk_socphy_shared *priv = phydev->shared->priv;
136362306a36Sopenharmony_ci	u32 polarities;
136462306a36Sopenharmony_ci
136562306a36Sopenharmony_ci	if (led_num == 0)
136662306a36Sopenharmony_ci		polarities = ~(priv->boottrap);
136762306a36Sopenharmony_ci	else
136862306a36Sopenharmony_ci		polarities = MTK_PHY_LED1_DEFAULT_POLARITIES;
136962306a36Sopenharmony_ci
137062306a36Sopenharmony_ci	if (polarities & BIT(phydev->mdio.addr))
137162306a36Sopenharmony_ci		return true;
137262306a36Sopenharmony_ci
137362306a36Sopenharmony_ci	return false;
137462306a36Sopenharmony_ci}
137562306a36Sopenharmony_ci
137662306a36Sopenharmony_cistatic int mt7988_phy_fix_leds_polarities(struct phy_device *phydev)
137762306a36Sopenharmony_ci{
137862306a36Sopenharmony_ci	struct pinctrl *pinctrl;
137962306a36Sopenharmony_ci	int index;
138062306a36Sopenharmony_ci
138162306a36Sopenharmony_ci	/* Setup LED polarity according to bootstrap use of LED pins */
138262306a36Sopenharmony_ci	for (index = 0; index < 2; ++index)
138362306a36Sopenharmony_ci		phy_modify_mmd(phydev, MDIO_MMD_VEND2, index ?
138462306a36Sopenharmony_ci				MTK_PHY_LED1_ON_CTRL : MTK_PHY_LED0_ON_CTRL,
138562306a36Sopenharmony_ci			       MTK_PHY_LED_ON_POLARITY,
138662306a36Sopenharmony_ci			       mt7988_phy_led_get_polarity(phydev, index) ?
138762306a36Sopenharmony_ci				MTK_PHY_LED_ON_POLARITY : 0);
138862306a36Sopenharmony_ci
138962306a36Sopenharmony_ci	/* Only now setup pinctrl to avoid bogus blinking */
139062306a36Sopenharmony_ci	pinctrl = devm_pinctrl_get_select(&phydev->mdio.dev, "gbe-led");
139162306a36Sopenharmony_ci	if (IS_ERR(pinctrl))
139262306a36Sopenharmony_ci		dev_err(&phydev->mdio.bus->dev, "Failed to setup PHY LED pinctrl\n");
139362306a36Sopenharmony_ci
139462306a36Sopenharmony_ci	return 0;
139562306a36Sopenharmony_ci}
139662306a36Sopenharmony_ci
139762306a36Sopenharmony_cistatic int mt7988_phy_probe_shared(struct phy_device *phydev)
139862306a36Sopenharmony_ci{
139962306a36Sopenharmony_ci	struct device_node *np = dev_of_node(&phydev->mdio.bus->dev);
140062306a36Sopenharmony_ci	struct mtk_socphy_shared *shared = phydev->shared->priv;
140162306a36Sopenharmony_ci	struct regmap *regmap;
140262306a36Sopenharmony_ci	u32 reg;
140362306a36Sopenharmony_ci	int ret;
140462306a36Sopenharmony_ci
140562306a36Sopenharmony_ci	/* The LED0 of the 4 PHYs in MT7988 are wired to SoC pins LED_A, LED_B,
140662306a36Sopenharmony_ci	 * LED_C and LED_D respectively. At the same time those pins are used to
140762306a36Sopenharmony_ci	 * bootstrap configuration of the reference clock source (LED_A),
140862306a36Sopenharmony_ci	 * DRAM DDRx16b x2/x1 (LED_B) and boot device (LED_C, LED_D).
140962306a36Sopenharmony_ci	 * In practise this is done using a LED and a resistor pulling the pin
141062306a36Sopenharmony_ci	 * either to GND or to VIO.
141162306a36Sopenharmony_ci	 * The detected value at boot time is accessible at run-time using the
141262306a36Sopenharmony_ci	 * TPBANK0 register located in the gpio base of the pinctrl, in order
141362306a36Sopenharmony_ci	 * to read it here it needs to be referenced by a phandle called
141462306a36Sopenharmony_ci	 * 'mediatek,pio' in the MDIO bus hosting the PHY.
141562306a36Sopenharmony_ci	 * The 4 bits in TPBANK0 are kept as package shared data and are used to
141662306a36Sopenharmony_ci	 * set LED polarity for each of the LED0.
141762306a36Sopenharmony_ci	 */
141862306a36Sopenharmony_ci	regmap = syscon_regmap_lookup_by_phandle(np, "mediatek,pio");
141962306a36Sopenharmony_ci	if (IS_ERR(regmap))
142062306a36Sopenharmony_ci		return PTR_ERR(regmap);
142162306a36Sopenharmony_ci
142262306a36Sopenharmony_ci	ret = regmap_read(regmap, RG_GPIO_MISC_TPBANK0, &reg);
142362306a36Sopenharmony_ci	if (ret)
142462306a36Sopenharmony_ci		return ret;
142562306a36Sopenharmony_ci
142662306a36Sopenharmony_ci	shared->boottrap = FIELD_GET(RG_GPIO_MISC_TPBANK0_BOOTMODE, reg);
142762306a36Sopenharmony_ci
142862306a36Sopenharmony_ci	return 0;
142962306a36Sopenharmony_ci}
143062306a36Sopenharmony_ci
143162306a36Sopenharmony_cistatic void mt798x_phy_leds_state_init(struct phy_device *phydev)
143262306a36Sopenharmony_ci{
143362306a36Sopenharmony_ci	int i;
143462306a36Sopenharmony_ci
143562306a36Sopenharmony_ci	for (i = 0; i < 2; ++i)
143662306a36Sopenharmony_ci		mt798x_phy_led_hw_control_get(phydev, i, NULL);
143762306a36Sopenharmony_ci}
143862306a36Sopenharmony_ci
143962306a36Sopenharmony_cistatic int mt7988_phy_probe(struct phy_device *phydev)
144062306a36Sopenharmony_ci{
144162306a36Sopenharmony_ci	struct mtk_socphy_shared *shared;
144262306a36Sopenharmony_ci	struct mtk_socphy_priv *priv;
144362306a36Sopenharmony_ci	int err;
144462306a36Sopenharmony_ci
144562306a36Sopenharmony_ci	if (phydev->mdio.addr > 3)
144662306a36Sopenharmony_ci		return -EINVAL;
144762306a36Sopenharmony_ci
144862306a36Sopenharmony_ci	err = devm_phy_package_join(&phydev->mdio.dev, phydev, 0,
144962306a36Sopenharmony_ci				    sizeof(struct mtk_socphy_shared));
145062306a36Sopenharmony_ci	if (err)
145162306a36Sopenharmony_ci		return err;
145262306a36Sopenharmony_ci
145362306a36Sopenharmony_ci	if (phy_package_probe_once(phydev)) {
145462306a36Sopenharmony_ci		err = mt7988_phy_probe_shared(phydev);
145562306a36Sopenharmony_ci		if (err)
145662306a36Sopenharmony_ci			return err;
145762306a36Sopenharmony_ci	}
145862306a36Sopenharmony_ci
145962306a36Sopenharmony_ci	shared = phydev->shared->priv;
146062306a36Sopenharmony_ci	priv = &shared->priv[phydev->mdio.addr];
146162306a36Sopenharmony_ci
146262306a36Sopenharmony_ci	phydev->priv = priv;
146362306a36Sopenharmony_ci
146462306a36Sopenharmony_ci	mt798x_phy_leds_state_init(phydev);
146562306a36Sopenharmony_ci
146662306a36Sopenharmony_ci	err = mt7988_phy_fix_leds_polarities(phydev);
146762306a36Sopenharmony_ci	if (err)
146862306a36Sopenharmony_ci		return err;
146962306a36Sopenharmony_ci
147062306a36Sopenharmony_ci	/* Disable TX power saving at probing to:
147162306a36Sopenharmony_ci	 * 1. Meet common mode compliance test criteria
147262306a36Sopenharmony_ci	 * 2. Make sure that TX-VCM calibration works fine
147362306a36Sopenharmony_ci	 */
147462306a36Sopenharmony_ci	phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG7,
147562306a36Sopenharmony_ci		       MTK_PHY_DA_AD_BUF_BIAS_LP_MASK, 0x3 << 8);
147662306a36Sopenharmony_ci
147762306a36Sopenharmony_ci	return mt798x_phy_calibration(phydev);
147862306a36Sopenharmony_ci}
147962306a36Sopenharmony_ci
148062306a36Sopenharmony_cistatic int mt7981_phy_probe(struct phy_device *phydev)
148162306a36Sopenharmony_ci{
148262306a36Sopenharmony_ci	struct mtk_socphy_priv *priv;
148362306a36Sopenharmony_ci
148462306a36Sopenharmony_ci	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(struct mtk_socphy_priv),
148562306a36Sopenharmony_ci			    GFP_KERNEL);
148662306a36Sopenharmony_ci	if (!priv)
148762306a36Sopenharmony_ci		return -ENOMEM;
148862306a36Sopenharmony_ci
148962306a36Sopenharmony_ci	phydev->priv = priv;
149062306a36Sopenharmony_ci
149162306a36Sopenharmony_ci	mt798x_phy_leds_state_init(phydev);
149262306a36Sopenharmony_ci
149362306a36Sopenharmony_ci	return mt798x_phy_calibration(phydev);
149462306a36Sopenharmony_ci}
149562306a36Sopenharmony_ci
149662306a36Sopenharmony_cistatic struct phy_driver mtk_socphy_driver[] = {
149762306a36Sopenharmony_ci	{
149862306a36Sopenharmony_ci		PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7981),
149962306a36Sopenharmony_ci		.name		= "MediaTek MT7981 PHY",
150062306a36Sopenharmony_ci		.config_init	= mt798x_phy_config_init,
150162306a36Sopenharmony_ci		.config_intr	= genphy_no_config_intr,
150262306a36Sopenharmony_ci		.handle_interrupt = genphy_handle_interrupt_no_ack,
150362306a36Sopenharmony_ci		.probe		= mt7981_phy_probe,
150462306a36Sopenharmony_ci		.suspend	= genphy_suspend,
150562306a36Sopenharmony_ci		.resume		= genphy_resume,
150662306a36Sopenharmony_ci		.read_page	= mtk_socphy_read_page,
150762306a36Sopenharmony_ci		.write_page	= mtk_socphy_write_page,
150862306a36Sopenharmony_ci		.led_blink_set	= mt798x_phy_led_blink_set,
150962306a36Sopenharmony_ci		.led_brightness_set = mt798x_phy_led_brightness_set,
151062306a36Sopenharmony_ci		.led_hw_is_supported = mt798x_phy_led_hw_is_supported,
151162306a36Sopenharmony_ci		.led_hw_control_set = mt798x_phy_led_hw_control_set,
151262306a36Sopenharmony_ci		.led_hw_control_get = mt798x_phy_led_hw_control_get,
151362306a36Sopenharmony_ci	},
151462306a36Sopenharmony_ci	{
151562306a36Sopenharmony_ci		PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7988),
151662306a36Sopenharmony_ci		.name		= "MediaTek MT7988 PHY",
151762306a36Sopenharmony_ci		.config_init	= mt798x_phy_config_init,
151862306a36Sopenharmony_ci		.config_intr	= genphy_no_config_intr,
151962306a36Sopenharmony_ci		.handle_interrupt = genphy_handle_interrupt_no_ack,
152062306a36Sopenharmony_ci		.probe		= mt7988_phy_probe,
152162306a36Sopenharmony_ci		.suspend	= genphy_suspend,
152262306a36Sopenharmony_ci		.resume		= genphy_resume,
152362306a36Sopenharmony_ci		.read_page	= mtk_socphy_read_page,
152462306a36Sopenharmony_ci		.write_page	= mtk_socphy_write_page,
152562306a36Sopenharmony_ci		.led_blink_set	= mt798x_phy_led_blink_set,
152662306a36Sopenharmony_ci		.led_brightness_set = mt798x_phy_led_brightness_set,
152762306a36Sopenharmony_ci		.led_hw_is_supported = mt798x_phy_led_hw_is_supported,
152862306a36Sopenharmony_ci		.led_hw_control_set = mt798x_phy_led_hw_control_set,
152962306a36Sopenharmony_ci		.led_hw_control_get = mt798x_phy_led_hw_control_get,
153062306a36Sopenharmony_ci	},
153162306a36Sopenharmony_ci};
153262306a36Sopenharmony_ci
153362306a36Sopenharmony_cimodule_phy_driver(mtk_socphy_driver);
153462306a36Sopenharmony_ci
153562306a36Sopenharmony_cistatic struct mdio_device_id __maybe_unused mtk_socphy_tbl[] = {
153662306a36Sopenharmony_ci	{ PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7981) },
153762306a36Sopenharmony_ci	{ PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7988) },
153862306a36Sopenharmony_ci	{ }
153962306a36Sopenharmony_ci};
154062306a36Sopenharmony_ci
154162306a36Sopenharmony_ciMODULE_DESCRIPTION("MediaTek SoC Gigabit Ethernet PHY driver");
154262306a36Sopenharmony_ciMODULE_AUTHOR("Daniel Golle <daniel@makrotopia.org>");
154362306a36Sopenharmony_ciMODULE_AUTHOR("SkyLake Huang <SkyLake.Huang@mediatek.com>");
154462306a36Sopenharmony_ciMODULE_LICENSE("GPL");
154562306a36Sopenharmony_ci
154662306a36Sopenharmony_ciMODULE_DEVICE_TABLE(mdio, mtk_socphy_tbl);
1547