1 From 98c485eaf509bc0e2a85f9b58d17cd501f274c4e Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Sun, 11 Jun 2023 00:48:10 +0100
4 Subject: [PATCH] net: phy: add driver for MediaTek SoC built-in GE PHYs
6 Some of MediaTek's Filogic SoCs come with built-in gigabit Ethernet
7 PHYs which require calibration data from the SoC's efuse.
8 Despite the similar design the driver doesn't share any code with the
9 existing mediatek-ge.c.
10 Add support for such PHYs by introducing a new driver with basic
11 support for MediaTek SoCs MT7981 and MT7988 built-in 1GE PHYs.
13 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
14 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
15 Signed-off-by: David S. Miller <davem@davemloft.net>
18 drivers/net/phy/Kconfig | 12 +
19 drivers/net/phy/Makefile | 1 +
20 drivers/net/phy/mediatek-ge-soc.c | 1116 +++++++++++++++++++++++++++++
21 drivers/net/phy/mediatek-ge.c | 3 +-
22 5 files changed, 1140 insertions(+), 1 deletion(-)
23 create mode 100644 drivers/net/phy/mediatek-ge-soc.c
27 @@ -12938,6 +12938,15 @@ F: drivers/net/pcs/pcs-mtk-usxgmii.c
28 F: include/linux/pcs/pcs-mtk-lynxi.h
29 F: include/linux/pcs/pcs-mtk-usxgmii.h
31 +MEDIATEK ETHERNET PHY DRIVERS
32 +M: Daniel Golle <daniel@makrotopia.org>
33 +M: Qingfang Deng <dqfext@gmail.com>
34 +M: SkyLake Huang <SkyLake.Huang@mediatek.com>
35 +L: netdev@vger.kernel.org
37 +F: drivers/net/phy/mediatek-ge-soc.c
38 +F: drivers/net/phy/mediatek-ge.c
40 MEDIATEK I2C CONTROLLER DRIVER
41 M: Qii Wang <qii.wang@mediatek.com>
42 L: linux-i2c@vger.kernel.org
43 --- a/drivers/net/phy/Kconfig
44 +++ b/drivers/net/phy/Kconfig
45 @@ -314,6 +314,18 @@ config MEDIATEK_GE_PHY
47 Supports the MediaTek Gigabit Ethernet PHYs.
49 +config MEDIATEK_GE_SOC_PHY
50 + tristate "MediaTek SoC Ethernet PHYs"
51 + depends on (ARM64 && ARCH_MEDIATEK) || COMPILE_TEST
52 + select NVMEM_MTK_EFUSE
54 + Supports MediaTek SoC built-in Gigabit Ethernet PHYs.
56 + Include support for built-in Ethernet PHYs which are present in
57 + the MT7981 and MT7988 SoCs. These PHYs need calibration data
58 + present in the SoCs efuse and will dynamically calibrate VCM
59 + (common-mode voltage) during startup.
62 tristate "Micrel PHYs"
63 depends on PTP_1588_CLOCK_OPTIONAL
64 --- a/drivers/net/phy/Makefile
65 +++ b/drivers/net/phy/Makefile
66 @@ -80,6 +80,7 @@ obj-$(CONFIG_MARVELL_PHY) += marvell.o
67 obj-$(CONFIG_MARVELL_88X2222_PHY) += marvell-88x2222.o
68 obj-$(CONFIG_MAXLINEAR_GPHY) += mxl-gpy.o
69 obj-$(CONFIG_MEDIATEK_GE_PHY) += mediatek-ge.o
70 +obj-$(CONFIG_MEDIATEK_GE_SOC_PHY) += mediatek-ge-soc.o
71 obj-$(CONFIG_MESON_GXL_PHY) += meson-gxl.o
72 obj-$(CONFIG_MICREL_KS8995MA) += spi_ks8995.o
73 obj-$(CONFIG_MICREL_PHY) += micrel.o
75 +++ b/drivers/net/phy/mediatek-ge-soc.c
77 +// SPDX-License-Identifier: GPL-2.0+
78 +#include <linux/bitfield.h>
79 +#include <linux/module.h>
80 +#include <linux/nvmem-consumer.h>
81 +#include <linux/of_address.h>
82 +#include <linux/of_platform.h>
83 +#include <linux/pinctrl/consumer.h>
84 +#include <linux/phy.h>
86 +#define MTK_GPHY_ID_MT7981 0x03a29461
87 +#define MTK_GPHY_ID_MT7988 0x03a29481
89 +#define MTK_EXT_PAGE_ACCESS 0x1f
90 +#define MTK_PHY_PAGE_STANDARD 0x0000
91 +#define MTK_PHY_PAGE_EXTENDED_3 0x0003
93 +#define MTK_PHY_LPI_REG_14 0x14
94 +#define MTK_PHY_LPI_WAKE_TIMER_1000_MASK GENMASK(8, 0)
96 +#define MTK_PHY_LPI_REG_1c 0x1c
97 +#define MTK_PHY_SMI_DET_ON_THRESH_MASK GENMASK(13, 8)
99 +#define MTK_PHY_PAGE_EXTENDED_2A30 0x2a30
100 +#define MTK_PHY_PAGE_EXTENDED_52B5 0x52b5
102 +#define ANALOG_INTERNAL_OPERATION_MAX_US 20
103 +#define TXRESERVE_MIN 0
104 +#define TXRESERVE_MAX 7
106 +#define MTK_PHY_ANARG_RG 0x10
107 +#define MTK_PHY_TCLKOFFSET_MASK GENMASK(12, 8)
109 +/* Registers on MDIO_MMD_VEND1 */
110 +#define MTK_PHY_TXVLD_DA_RG 0x12
111 +#define MTK_PHY_DA_TX_I2MPB_A_GBE_MASK GENMASK(15, 10)
112 +#define MTK_PHY_DA_TX_I2MPB_A_TBT_MASK GENMASK(5, 0)
114 +#define MTK_PHY_TX_I2MPB_TEST_MODE_A2 0x16
115 +#define MTK_PHY_DA_TX_I2MPB_A_HBT_MASK GENMASK(15, 10)
116 +#define MTK_PHY_DA_TX_I2MPB_A_TST_MASK GENMASK(5, 0)
118 +#define MTK_PHY_TX_I2MPB_TEST_MODE_B1 0x17
119 +#define MTK_PHY_DA_TX_I2MPB_B_GBE_MASK GENMASK(13, 8)
120 +#define MTK_PHY_DA_TX_I2MPB_B_TBT_MASK GENMASK(5, 0)
122 +#define MTK_PHY_TX_I2MPB_TEST_MODE_B2 0x18
123 +#define MTK_PHY_DA_TX_I2MPB_B_HBT_MASK GENMASK(13, 8)
124 +#define MTK_PHY_DA_TX_I2MPB_B_TST_MASK GENMASK(5, 0)
126 +#define MTK_PHY_TX_I2MPB_TEST_MODE_C1 0x19
127 +#define MTK_PHY_DA_TX_I2MPB_C_GBE_MASK GENMASK(13, 8)
128 +#define MTK_PHY_DA_TX_I2MPB_C_TBT_MASK GENMASK(5, 0)
130 +#define MTK_PHY_TX_I2MPB_TEST_MODE_C2 0x20
131 +#define MTK_PHY_DA_TX_I2MPB_C_HBT_MASK GENMASK(13, 8)
132 +#define MTK_PHY_DA_TX_I2MPB_C_TST_MASK GENMASK(5, 0)
134 +#define MTK_PHY_TX_I2MPB_TEST_MODE_D1 0x21
135 +#define MTK_PHY_DA_TX_I2MPB_D_GBE_MASK GENMASK(13, 8)
136 +#define MTK_PHY_DA_TX_I2MPB_D_TBT_MASK GENMASK(5, 0)
138 +#define MTK_PHY_TX_I2MPB_TEST_MODE_D2 0x22
139 +#define MTK_PHY_DA_TX_I2MPB_D_HBT_MASK GENMASK(13, 8)
140 +#define MTK_PHY_DA_TX_I2MPB_D_TST_MASK GENMASK(5, 0)
142 +#define MTK_PHY_RXADC_CTRL_RG7 0xc6
143 +#define MTK_PHY_DA_AD_BUF_BIAS_LP_MASK GENMASK(9, 8)
145 +#define MTK_PHY_RXADC_CTRL_RG9 0xc8
146 +#define MTK_PHY_DA_RX_PSBN_TBT_MASK GENMASK(14, 12)
147 +#define MTK_PHY_DA_RX_PSBN_HBT_MASK GENMASK(10, 8)
148 +#define MTK_PHY_DA_RX_PSBN_GBE_MASK GENMASK(6, 4)
149 +#define MTK_PHY_DA_RX_PSBN_LP_MASK GENMASK(2, 0)
151 +#define MTK_PHY_LDO_OUTPUT_V 0xd7
153 +#define MTK_PHY_RG_ANA_CAL_RG0 0xdb
154 +#define MTK_PHY_RG_CAL_CKINV BIT(12)
155 +#define MTK_PHY_RG_ANA_CALEN BIT(8)
156 +#define MTK_PHY_RG_ZCALEN_A BIT(0)
158 +#define MTK_PHY_RG_ANA_CAL_RG1 0xdc
159 +#define MTK_PHY_RG_ZCALEN_B BIT(12)
160 +#define MTK_PHY_RG_ZCALEN_C BIT(8)
161 +#define MTK_PHY_RG_ZCALEN_D BIT(4)
162 +#define MTK_PHY_RG_TXVOS_CALEN BIT(0)
164 +#define MTK_PHY_RG_ANA_CAL_RG5 0xe0
165 +#define MTK_PHY_RG_REXT_TRIM_MASK GENMASK(13, 8)
167 +#define MTK_PHY_RG_TX_FILTER 0xfe
169 +#define MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG120 0x120
170 +#define MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK GENMASK(12, 8)
171 +#define MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK GENMASK(4, 0)
173 +#define MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122 0x122
174 +#define MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK GENMASK(7, 0)
176 +#define MTK_PHY_RG_TESTMUX_ADC_CTRL 0x144
177 +#define MTK_PHY_RG_TXEN_DIG_MASK GENMASK(5, 5)
179 +#define MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B 0x172
180 +#define MTK_PHY_CR_TX_AMP_OFFSET_A_MASK GENMASK(13, 8)
181 +#define MTK_PHY_CR_TX_AMP_OFFSET_B_MASK GENMASK(6, 0)
183 +#define MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D 0x173
184 +#define MTK_PHY_CR_TX_AMP_OFFSET_C_MASK GENMASK(13, 8)
185 +#define MTK_PHY_CR_TX_AMP_OFFSET_D_MASK GENMASK(6, 0)
187 +#define MTK_PHY_RG_AD_CAL_COMP 0x17a
188 +#define MTK_PHY_AD_CAL_COMP_OUT_SHIFT (8)
190 +#define MTK_PHY_RG_AD_CAL_CLK 0x17b
191 +#define MTK_PHY_DA_CAL_CLK BIT(0)
193 +#define MTK_PHY_RG_AD_CALIN 0x17c
194 +#define MTK_PHY_DA_CALIN_FLAG BIT(0)
196 +#define MTK_PHY_RG_DASN_DAC_IN0_A 0x17d
197 +#define MTK_PHY_DASN_DAC_IN0_A_MASK GENMASK(9, 0)
199 +#define MTK_PHY_RG_DASN_DAC_IN0_B 0x17e
200 +#define MTK_PHY_DASN_DAC_IN0_B_MASK GENMASK(9, 0)
202 +#define MTK_PHY_RG_DASN_DAC_IN0_C 0x17f
203 +#define MTK_PHY_DASN_DAC_IN0_C_MASK GENMASK(9, 0)
205 +#define MTK_PHY_RG_DASN_DAC_IN0_D 0x180
206 +#define MTK_PHY_DASN_DAC_IN0_D_MASK GENMASK(9, 0)
208 +#define MTK_PHY_RG_DASN_DAC_IN1_A 0x181
209 +#define MTK_PHY_DASN_DAC_IN1_A_MASK GENMASK(9, 0)
211 +#define MTK_PHY_RG_DASN_DAC_IN1_B 0x182
212 +#define MTK_PHY_DASN_DAC_IN1_B_MASK GENMASK(9, 0)
214 +#define MTK_PHY_RG_DASN_DAC_IN1_C 0x183
215 +#define MTK_PHY_DASN_DAC_IN1_C_MASK GENMASK(9, 0)
217 +#define MTK_PHY_RG_DASN_DAC_IN1_D 0x184
218 +#define MTK_PHY_DASN_DAC_IN1_D_MASK GENMASK(9, 0)
220 +#define MTK_PHY_RG_DEV1E_REG19b 0x19b
221 +#define MTK_PHY_BYPASS_DSP_LPI_READY BIT(8)
223 +#define MTK_PHY_RG_LP_IIR2_K1_L 0x22a
224 +#define MTK_PHY_RG_LP_IIR2_K1_U 0x22b
225 +#define MTK_PHY_RG_LP_IIR2_K2_L 0x22c
226 +#define MTK_PHY_RG_LP_IIR2_K2_U 0x22d
227 +#define MTK_PHY_RG_LP_IIR2_K3_L 0x22e
228 +#define MTK_PHY_RG_LP_IIR2_K3_U 0x22f
229 +#define MTK_PHY_RG_LP_IIR2_K4_L 0x230
230 +#define MTK_PHY_RG_LP_IIR2_K4_U 0x231
231 +#define MTK_PHY_RG_LP_IIR2_K5_L 0x232
232 +#define MTK_PHY_RG_LP_IIR2_K5_U 0x233
234 +#define MTK_PHY_RG_DEV1E_REG234 0x234
235 +#define MTK_PHY_TR_OPEN_LOOP_EN_MASK GENMASK(0, 0)
236 +#define MTK_PHY_LPF_X_AVERAGE_MASK GENMASK(7, 4)
237 +#define MTK_PHY_TR_LP_IIR_EEE_EN BIT(12)
239 +#define MTK_PHY_RG_LPF_CNT_VAL 0x235
241 +#define MTK_PHY_RG_DEV1E_REG238 0x238
242 +#define MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK GENMASK(8, 0)
243 +#define MTK_PHY_LPI_SLV_SEND_TX_EN BIT(12)
245 +#define MTK_PHY_RG_DEV1E_REG239 0x239
246 +#define MTK_PHY_LPI_SEND_LOC_TIMER_MASK GENMASK(8, 0)
247 +#define MTK_PHY_LPI_TXPCS_LOC_RCV BIT(12)
249 +#define MTK_PHY_RG_DEV1E_REG27C 0x27c
250 +#define MTK_PHY_VGASTATE_FFE_THR_ST1_MASK GENMASK(12, 8)
251 +#define MTK_PHY_RG_DEV1E_REG27D 0x27d
252 +#define MTK_PHY_VGASTATE_FFE_THR_ST2_MASK GENMASK(4, 0)
254 +#define MTK_PHY_RG_DEV1E_REG2C7 0x2c7
255 +#define MTK_PHY_MAX_GAIN_MASK GENMASK(4, 0)
256 +#define MTK_PHY_MIN_GAIN_MASK GENMASK(12, 8)
258 +#define MTK_PHY_RG_DEV1E_REG2D1 0x2d1
259 +#define MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK GENMASK(7, 0)
260 +#define MTK_PHY_LPI_SKIP_SD_SLV_TR BIT(8)
261 +#define MTK_PHY_LPI_TR_READY BIT(9)
262 +#define MTK_PHY_LPI_VCO_EEE_STG0_EN BIT(10)
264 +#define MTK_PHY_RG_DEV1E_REG323 0x323
265 +#define MTK_PHY_EEE_WAKE_MAS_INT_DC BIT(0)
266 +#define MTK_PHY_EEE_WAKE_SLV_INT_DC BIT(4)
268 +#define MTK_PHY_RG_DEV1E_REG324 0x324
269 +#define MTK_PHY_SMI_DETCNT_MAX_MASK GENMASK(5, 0)
270 +#define MTK_PHY_SMI_DET_MAX_EN BIT(8)
272 +#define MTK_PHY_RG_DEV1E_REG326 0x326
273 +#define MTK_PHY_LPI_MODE_SD_ON BIT(0)
274 +#define MTK_PHY_RESET_RANDUPD_CNT BIT(1)
275 +#define MTK_PHY_TREC_UPDATE_ENAB_CLR BIT(2)
276 +#define MTK_PHY_LPI_QUIT_WAIT_DFE_SIG_DET_OFF BIT(4)
277 +#define MTK_PHY_TR_READY_SKIP_AFE_WAKEUP BIT(5)
279 +#define MTK_PHY_LDO_PUMP_EN_PAIRAB 0x502
280 +#define MTK_PHY_LDO_PUMP_EN_PAIRCD 0x503
282 +#define MTK_PHY_DA_TX_R50_PAIR_A 0x53d
283 +#define MTK_PHY_DA_TX_R50_PAIR_B 0x53e
284 +#define MTK_PHY_DA_TX_R50_PAIR_C 0x53f
285 +#define MTK_PHY_DA_TX_R50_PAIR_D 0x540
287 +#define MTK_PHY_RG_BG_RASEL 0x115
288 +#define MTK_PHY_RG_BG_RASEL_MASK GENMASK(2, 0)
290 +/* These macro privides efuse parsing for internal phy. */
291 +#define EFS_DA_TX_I2MPB_A(x) (((x) >> 0) & GENMASK(5, 0))
292 +#define EFS_DA_TX_I2MPB_B(x) (((x) >> 6) & GENMASK(5, 0))
293 +#define EFS_DA_TX_I2MPB_C(x) (((x) >> 12) & GENMASK(5, 0))
294 +#define EFS_DA_TX_I2MPB_D(x) (((x) >> 18) & GENMASK(5, 0))
295 +#define EFS_DA_TX_AMP_OFFSET_A(x) (((x) >> 24) & GENMASK(5, 0))
297 +#define EFS_DA_TX_AMP_OFFSET_B(x) (((x) >> 0) & GENMASK(5, 0))
298 +#define EFS_DA_TX_AMP_OFFSET_C(x) (((x) >> 6) & GENMASK(5, 0))
299 +#define EFS_DA_TX_AMP_OFFSET_D(x) (((x) >> 12) & GENMASK(5, 0))
300 +#define EFS_DA_TX_R50_A(x) (((x) >> 18) & GENMASK(5, 0))
301 +#define EFS_DA_TX_R50_B(x) (((x) >> 24) & GENMASK(5, 0))
303 +#define EFS_DA_TX_R50_C(x) (((x) >> 0) & GENMASK(5, 0))
304 +#define EFS_DA_TX_R50_D(x) (((x) >> 6) & GENMASK(5, 0))
306 +#define EFS_RG_BG_RASEL(x) (((x) >> 4) & GENMASK(2, 0))
307 +#define EFS_RG_REXT_TRIM(x) (((x) >> 7) & GENMASK(5, 0))
324 +enum calibration_mode {
342 +static int mtk_socphy_read_page(struct phy_device *phydev)
344 + return __phy_read(phydev, MTK_EXT_PAGE_ACCESS);
347 +static int mtk_socphy_write_page(struct phy_device *phydev, int page)
349 + return __phy_write(phydev, MTK_EXT_PAGE_ACCESS, page);
352 +/* One calibration cycle consists of:
353 + * 1.Set DA_CALIN_FLAG high to start calibration. Keep it high
354 + * until AD_CAL_COMP is ready to output calibration result.
355 + * 2.Wait until DA_CAL_CLK is available.
356 + * 3.Fetch AD_CAL_COMP_OUT.
358 +static int cal_cycle(struct phy_device *phydev, int devad,
359 + u32 regnum, u16 mask, u16 cal_val)
364 + phy_modify_mmd(phydev, devad, regnum,
366 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CALIN,
367 + MTK_PHY_DA_CALIN_FLAG);
369 + ret = phy_read_mmd_poll_timeout(phydev, MDIO_MMD_VEND1,
370 + MTK_PHY_RG_AD_CAL_CLK, reg_val,
371 + reg_val & MTK_PHY_DA_CAL_CLK, 500,
372 + ANALOG_INTERNAL_OPERATION_MAX_US, false);
374 + phydev_err(phydev, "Calibration cycle timeout\n");
378 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CALIN,
379 + MTK_PHY_DA_CALIN_FLAG);
380 + ret = phy_read_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CAL_COMP) >>
381 + MTK_PHY_AD_CAL_COMP_OUT_SHIFT;
382 + phydev_dbg(phydev, "cal_val: 0x%x, ret: %d\n", cal_val, ret);
387 +static int rext_fill_result(struct phy_device *phydev, u16 *buf)
389 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG5,
390 + MTK_PHY_RG_REXT_TRIM_MASK, buf[0] << 8);
391 + phy_modify_mmd(phydev, MDIO_MMD_VEND2, MTK_PHY_RG_BG_RASEL,
392 + MTK_PHY_RG_BG_RASEL_MASK, buf[1]);
397 +static int rext_cal_efuse(struct phy_device *phydev, u32 *buf)
399 + u16 rext_cal_val[2];
401 + rext_cal_val[0] = EFS_RG_REXT_TRIM(buf[3]);
402 + rext_cal_val[1] = EFS_RG_BG_RASEL(buf[3]);
403 + rext_fill_result(phydev, rext_cal_val);
408 +static int tx_offset_fill_result(struct phy_device *phydev, u16 *buf)
410 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B,
411 + MTK_PHY_CR_TX_AMP_OFFSET_A_MASK, buf[0] << 8);
412 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B,
413 + MTK_PHY_CR_TX_AMP_OFFSET_B_MASK, buf[1]);
414 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D,
415 + MTK_PHY_CR_TX_AMP_OFFSET_C_MASK, buf[2] << 8);
416 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D,
417 + MTK_PHY_CR_TX_AMP_OFFSET_D_MASK, buf[3]);
422 +static int tx_offset_cal_efuse(struct phy_device *phydev, u32 *buf)
424 + u16 tx_offset_cal_val[4];
426 + tx_offset_cal_val[0] = EFS_DA_TX_AMP_OFFSET_A(buf[0]);
427 + tx_offset_cal_val[1] = EFS_DA_TX_AMP_OFFSET_B(buf[1]);
428 + tx_offset_cal_val[2] = EFS_DA_TX_AMP_OFFSET_C(buf[1]);
429 + tx_offset_cal_val[3] = EFS_DA_TX_AMP_OFFSET_D(buf[1]);
431 + tx_offset_fill_result(phydev, tx_offset_cal_val);
436 +static int tx_amp_fill_result(struct phy_device *phydev, u16 *buf)
440 + const int vals_9461[16] = { 7, 1, 4, 7,
444 + const int vals_9481[16] = { 10, 6, 6, 10,
448 + switch (phydev->drv->phy_id) {
449 + case MTK_GPHY_ID_MT7981:
450 + /* We add some calibration to efuse values
451 + * due to board level influence.
452 + * GBE: +7, TBT: +1, HBT: +4, TST: +7
454 + memcpy(bias, (const void *)vals_9461, sizeof(bias));
456 + case MTK_GPHY_ID_MT7988:
457 + memcpy(bias, (const void *)vals_9481, sizeof(bias));
461 + /* Prevent overflow */
462 + for (i = 0; i < 12; i++) {
463 + if (buf[i >> 2] + bias[i] > 63) {
469 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TXVLD_DA_RG,
470 + MTK_PHY_DA_TX_I2MPB_A_GBE_MASK, (buf[0] + bias[0]) << 10);
471 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TXVLD_DA_RG,
472 + MTK_PHY_DA_TX_I2MPB_A_TBT_MASK, buf[0] + bias[1]);
473 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_A2,
474 + MTK_PHY_DA_TX_I2MPB_A_HBT_MASK, (buf[0] + bias[2]) << 10);
475 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_A2,
476 + MTK_PHY_DA_TX_I2MPB_A_TST_MASK, buf[0] + bias[3]);
478 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B1,
479 + MTK_PHY_DA_TX_I2MPB_B_GBE_MASK, (buf[1] + bias[4]) << 8);
480 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B1,
481 + MTK_PHY_DA_TX_I2MPB_B_TBT_MASK, buf[1] + bias[5]);
482 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B2,
483 + MTK_PHY_DA_TX_I2MPB_B_HBT_MASK, (buf[1] + bias[6]) << 8);
484 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B2,
485 + MTK_PHY_DA_TX_I2MPB_B_TST_MASK, buf[1] + bias[7]);
487 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C1,
488 + MTK_PHY_DA_TX_I2MPB_C_GBE_MASK, (buf[2] + bias[8]) << 8);
489 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C1,
490 + MTK_PHY_DA_TX_I2MPB_C_TBT_MASK, buf[2] + bias[9]);
491 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C2,
492 + MTK_PHY_DA_TX_I2MPB_C_HBT_MASK, (buf[2] + bias[10]) << 8);
493 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C2,
494 + MTK_PHY_DA_TX_I2MPB_C_TST_MASK, buf[2] + bias[11]);
496 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D1,
497 + MTK_PHY_DA_TX_I2MPB_D_GBE_MASK, (buf[3] + bias[12]) << 8);
498 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D1,
499 + MTK_PHY_DA_TX_I2MPB_D_TBT_MASK, buf[3] + bias[13]);
500 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D2,
501 + MTK_PHY_DA_TX_I2MPB_D_HBT_MASK, (buf[3] + bias[14]) << 8);
502 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D2,
503 + MTK_PHY_DA_TX_I2MPB_D_TST_MASK, buf[3] + bias[15]);
508 +static int tx_amp_cal_efuse(struct phy_device *phydev, u32 *buf)
510 + u16 tx_amp_cal_val[4];
512 + tx_amp_cal_val[0] = EFS_DA_TX_I2MPB_A(buf[0]);
513 + tx_amp_cal_val[1] = EFS_DA_TX_I2MPB_B(buf[0]);
514 + tx_amp_cal_val[2] = EFS_DA_TX_I2MPB_C(buf[0]);
515 + tx_amp_cal_val[3] = EFS_DA_TX_I2MPB_D(buf[0]);
516 + tx_amp_fill_result(phydev, tx_amp_cal_val);
521 +static int tx_r50_fill_result(struct phy_device *phydev, u16 tx_r50_cal_val,
527 + if (phydev->drv->phy_id == MTK_GPHY_ID_MT7988)
530 + val = clamp_val(bias + tx_r50_cal_val, 0, 63);
532 + switch (txg_calen_x) {
534 + reg = MTK_PHY_DA_TX_R50_PAIR_A;
537 + reg = MTK_PHY_DA_TX_R50_PAIR_B;
540 + reg = MTK_PHY_DA_TX_R50_PAIR_C;
543 + reg = MTK_PHY_DA_TX_R50_PAIR_D;
549 + phy_write_mmd(phydev, MDIO_MMD_VEND1, reg, val | val << 8);
554 +static int tx_r50_cal_efuse(struct phy_device *phydev, u32 *buf,
557 + u16 tx_r50_cal_val;
559 + switch (txg_calen_x) {
561 + tx_r50_cal_val = EFS_DA_TX_R50_A(buf[1]);
564 + tx_r50_cal_val = EFS_DA_TX_R50_B(buf[1]);
567 + tx_r50_cal_val = EFS_DA_TX_R50_C(buf[2]);
570 + tx_r50_cal_val = EFS_DA_TX_R50_D(buf[2]);
575 + tx_r50_fill_result(phydev, tx_r50_cal_val, txg_calen_x);
580 +static int tx_vcm_cal_sw(struct phy_device *phydev, u8 rg_txreserve_x)
582 + u8 lower_idx, upper_idx, txreserve_val;
583 + u8 lower_ret, upper_ret;
586 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
587 + MTK_PHY_RG_ANA_CALEN);
588 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
589 + MTK_PHY_RG_CAL_CKINV);
590 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
591 + MTK_PHY_RG_TXVOS_CALEN);
593 + switch (rg_txreserve_x) {
595 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
596 + MTK_PHY_RG_DASN_DAC_IN0_A,
597 + MTK_PHY_DASN_DAC_IN0_A_MASK);
598 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
599 + MTK_PHY_RG_DASN_DAC_IN1_A,
600 + MTK_PHY_DASN_DAC_IN1_A_MASK);
601 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
602 + MTK_PHY_RG_ANA_CAL_RG0,
603 + MTK_PHY_RG_ZCALEN_A);
606 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
607 + MTK_PHY_RG_DASN_DAC_IN0_B,
608 + MTK_PHY_DASN_DAC_IN0_B_MASK);
609 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
610 + MTK_PHY_RG_DASN_DAC_IN1_B,
611 + MTK_PHY_DASN_DAC_IN1_B_MASK);
612 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
613 + MTK_PHY_RG_ANA_CAL_RG1,
614 + MTK_PHY_RG_ZCALEN_B);
617 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
618 + MTK_PHY_RG_DASN_DAC_IN0_C,
619 + MTK_PHY_DASN_DAC_IN0_C_MASK);
620 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
621 + MTK_PHY_RG_DASN_DAC_IN1_C,
622 + MTK_PHY_DASN_DAC_IN1_C_MASK);
623 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
624 + MTK_PHY_RG_ANA_CAL_RG1,
625 + MTK_PHY_RG_ZCALEN_C);
628 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
629 + MTK_PHY_RG_DASN_DAC_IN0_D,
630 + MTK_PHY_DASN_DAC_IN0_D_MASK);
631 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
632 + MTK_PHY_RG_DASN_DAC_IN1_D,
633 + MTK_PHY_DASN_DAC_IN1_D_MASK);
634 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
635 + MTK_PHY_RG_ANA_CAL_RG1,
636 + MTK_PHY_RG_ZCALEN_D);
643 + lower_idx = TXRESERVE_MIN;
644 + upper_idx = TXRESERVE_MAX;
646 + phydev_dbg(phydev, "Start TX-VCM SW cal.\n");
647 + while ((upper_idx - lower_idx) > 1) {
648 + txreserve_val = DIV_ROUND_CLOSEST(lower_idx + upper_idx, 2);
649 + ret = cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
650 + MTK_PHY_DA_RX_PSBN_TBT_MASK |
651 + MTK_PHY_DA_RX_PSBN_HBT_MASK |
652 + MTK_PHY_DA_RX_PSBN_GBE_MASK |
653 + MTK_PHY_DA_RX_PSBN_LP_MASK,
654 + txreserve_val << 12 | txreserve_val << 8 |
655 + txreserve_val << 4 | txreserve_val);
657 + upper_idx = txreserve_val;
659 + } else if (ret == 0) {
660 + lower_idx = txreserve_val;
667 + if (lower_idx == TXRESERVE_MIN) {
668 + lower_ret = cal_cycle(phydev, MDIO_MMD_VEND1,
669 + MTK_PHY_RXADC_CTRL_RG9,
670 + MTK_PHY_DA_RX_PSBN_TBT_MASK |
671 + MTK_PHY_DA_RX_PSBN_HBT_MASK |
672 + MTK_PHY_DA_RX_PSBN_GBE_MASK |
673 + MTK_PHY_DA_RX_PSBN_LP_MASK,
674 + lower_idx << 12 | lower_idx << 8 |
675 + lower_idx << 4 | lower_idx);
677 + } else if (upper_idx == TXRESERVE_MAX) {
678 + upper_ret = cal_cycle(phydev, MDIO_MMD_VEND1,
679 + MTK_PHY_RXADC_CTRL_RG9,
680 + MTK_PHY_DA_RX_PSBN_TBT_MASK |
681 + MTK_PHY_DA_RX_PSBN_HBT_MASK |
682 + MTK_PHY_DA_RX_PSBN_GBE_MASK |
683 + MTK_PHY_DA_RX_PSBN_LP_MASK,
684 + upper_idx << 12 | upper_idx << 8 |
685 + upper_idx << 4 | upper_idx);
691 + /* We calibrate TX-VCM in different logic. Check upper index and then
692 + * lower index. If this calibration is valid, apply lower index's result.
694 + ret = upper_ret - lower_ret;
697 + /* Make sure we use upper_idx in our calibration system */
698 + cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
699 + MTK_PHY_DA_RX_PSBN_TBT_MASK |
700 + MTK_PHY_DA_RX_PSBN_HBT_MASK |
701 + MTK_PHY_DA_RX_PSBN_GBE_MASK |
702 + MTK_PHY_DA_RX_PSBN_LP_MASK,
703 + upper_idx << 12 | upper_idx << 8 |
704 + upper_idx << 4 | upper_idx);
705 + phydev_dbg(phydev, "TX-VCM SW cal result: 0x%x\n", upper_idx);
706 + } else if (lower_idx == TXRESERVE_MIN && upper_ret == 1 &&
709 + cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
710 + MTK_PHY_DA_RX_PSBN_TBT_MASK |
711 + MTK_PHY_DA_RX_PSBN_HBT_MASK |
712 + MTK_PHY_DA_RX_PSBN_GBE_MASK |
713 + MTK_PHY_DA_RX_PSBN_LP_MASK,
714 + lower_idx << 12 | lower_idx << 8 |
715 + lower_idx << 4 | lower_idx);
716 + phydev_warn(phydev, "TX-VCM SW cal result at low margin 0x%x\n",
718 + } else if (upper_idx == TXRESERVE_MAX && upper_ret == 0 &&
721 + phydev_warn(phydev, "TX-VCM SW cal result at high margin 0x%x\n",
728 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
729 + MTK_PHY_RG_ANA_CALEN);
730 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
731 + MTK_PHY_RG_TXVOS_CALEN);
732 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
733 + MTK_PHY_RG_ZCALEN_A);
734 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
735 + MTK_PHY_RG_ZCALEN_B | MTK_PHY_RG_ZCALEN_C |
736 + MTK_PHY_RG_ZCALEN_D);
741 +static void mt798x_phy_common_finetune(struct phy_device *phydev)
743 + phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
744 + /* EnabRandUpdTrig = 1 */
745 + __phy_write(phydev, 0x11, 0x2f00);
746 + __phy_write(phydev, 0x12, 0xe);
747 + __phy_write(phydev, 0x10, 0x8fb0);
749 + /* NormMseLoThresh = 85 */
750 + __phy_write(phydev, 0x11, 0x55a0);
751 + __phy_write(phydev, 0x12, 0x0);
752 + __phy_write(phydev, 0x10, 0x83aa);
755 + __phy_write(phydev, 0x11, 0x0);
756 + __phy_write(phydev, 0x12, 0x0);
757 + __phy_write(phydev, 0x10, 0x9686);
759 + /* SSTrKp1000Slv = 5 */
760 + __phy_write(phydev, 0x11, 0xbaef);
761 + __phy_write(phydev, 0x12, 0x2e);
762 + __phy_write(phydev, 0x10, 0x968c);
764 + /* MrvlTrFix100Kp = 3, MrvlTrFix100Kf = 2,
765 + * MrvlTrFix1000Kp = 3, MrvlTrFix1000Kf = 2
767 + __phy_write(phydev, 0x11, 0xd10a);
768 + __phy_write(phydev, 0x12, 0x34);
769 + __phy_write(phydev, 0x10, 0x8f82);
771 + /* VcoSlicerThreshBitsHigh */
772 + __phy_write(phydev, 0x11, 0x5555);
773 + __phy_write(phydev, 0x12, 0x55);
774 + __phy_write(phydev, 0x10, 0x8ec0);
775 + phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
777 + /* TR_OPEN_LOOP_EN = 1, lpf_x_average = 9*/
778 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG234,
779 + MTK_PHY_TR_OPEN_LOOP_EN_MASK | MTK_PHY_LPF_X_AVERAGE_MASK,
780 + BIT(0) | FIELD_PREP(MTK_PHY_LPF_X_AVERAGE_MASK, 0x9));
782 + /* rg_tr_lpf_cnt_val = 512 */
783 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LPF_CNT_VAL, 0x200);
786 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K1_L, 0x82);
787 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K1_U, 0x0);
788 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K2_L, 0x103);
789 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K2_U, 0x0);
790 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K3_L, 0x82);
791 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K3_U, 0x0);
792 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K4_L, 0xd177);
793 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K4_U, 0x3);
794 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K5_L, 0x2c82);
795 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K5_U, 0xe);
798 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG27C,
799 + MTK_PHY_VGASTATE_FFE_THR_ST1_MASK, 0x1b << 8);
800 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG27D,
801 + MTK_PHY_VGASTATE_FFE_THR_ST2_MASK, 0x1e);
803 + /* Disable LDO pump */
804 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_PUMP_EN_PAIRAB, 0x0);
805 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_PUMP_EN_PAIRCD, 0x0);
806 + /* Adjust LDO output voltage */
807 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_OUTPUT_V, 0x2222);
810 +static void mt7981_phy_finetune(struct phy_device *phydev)
812 + u16 val[8] = { 0x01ce, 0x01c1,
818 + /* 100M eye finetune:
819 + * Keep middle level of TX MLT3 shapper as default.
820 + * Only change TX MLT3 overshoot level here.
822 + for (k = 0, i = 1; i < 12; i++) {
825 + phy_write_mmd(phydev, MDIO_MMD_VEND1, i, val[k++]);
828 + phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
829 + /* SlvDSPreadyTime = 24, MasDSPreadyTime = 24 */
830 + __phy_write(phydev, 0x11, 0xc71);
831 + __phy_write(phydev, 0x12, 0xc);
832 + __phy_write(phydev, 0x10, 0x8fae);
834 + /* ResetSyncOffset = 6 */
835 + __phy_write(phydev, 0x11, 0x600);
836 + __phy_write(phydev, 0x12, 0x0);
837 + __phy_write(phydev, 0x10, 0x8fc0);
839 + /* VgaDecRate = 1 */
840 + __phy_write(phydev, 0x11, 0x4c2a);
841 + __phy_write(phydev, 0x12, 0x3e);
842 + __phy_write(phydev, 0x10, 0x8fa4);
844 + /* FfeUpdGainForce = 4 */
845 + __phy_write(phydev, 0x11, 0x240);
846 + __phy_write(phydev, 0x12, 0x0);
847 + __phy_write(phydev, 0x10, 0x9680);
849 + phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
852 +static void mt7988_phy_finetune(struct phy_device *phydev)
854 + u16 val[12] = { 0x0187, 0x01cd, 0x01c8, 0x0182,
855 + 0x020d, 0x0206, 0x0384, 0x03d0,
856 + 0x03c6, 0x030a, 0x0011, 0x0005 };
859 + /* Set default MLT3 shaper first */
860 + for (i = 0; i < 12; i++)
861 + phy_write_mmd(phydev, MDIO_MMD_VEND1, i, val[i]);
864 + phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_TX_FILTER, 0x5);
866 + /* Disable TX power saving */
867 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG7,
868 + MTK_PHY_DA_AD_BUF_BIAS_LP_MASK, 0x3 << 8);
870 + phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
872 + /* SlvDSPreadyTime = 24, MasDSPreadyTime = 12 */
873 + __phy_write(phydev, 0x11, 0x671);
874 + __phy_write(phydev, 0x12, 0xc);
875 + __phy_write(phydev, 0x10, 0x8fae);
877 + /* ResetSyncOffset = 5 */
878 + __phy_write(phydev, 0x11, 0x500);
879 + __phy_write(phydev, 0x12, 0x0);
880 + __phy_write(phydev, 0x10, 0x8fc0);
882 + /* VgaDecRate is 1 at default on mt7988 */
884 + phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
886 + phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_2A30);
887 + /* TxClkOffset = 2 */
888 + __phy_modify(phydev, MTK_PHY_ANARG_RG, MTK_PHY_TCLKOFFSET_MASK,
889 + FIELD_PREP(MTK_PHY_TCLKOFFSET_MASK, 0x2));
890 + phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
893 +static void mt798x_phy_eee(struct phy_device *phydev)
895 + phy_modify_mmd(phydev, MDIO_MMD_VEND1,
896 + MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG120,
897 + MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK |
898 + MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK,
899 + FIELD_PREP(MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK, 0x0) |
900 + FIELD_PREP(MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK, 0x14));
902 + phy_modify_mmd(phydev, MDIO_MMD_VEND1,
903 + MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122,
904 + MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
905 + FIELD_PREP(MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
908 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
909 + MTK_PHY_RG_TESTMUX_ADC_CTRL,
910 + MTK_PHY_RG_TXEN_DIG_MASK);
912 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
913 + MTK_PHY_RG_DEV1E_REG19b, MTK_PHY_BYPASS_DSP_LPI_READY);
915 + phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
916 + MTK_PHY_RG_DEV1E_REG234, MTK_PHY_TR_LP_IIR_EEE_EN);
918 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG238,
919 + MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK |
920 + MTK_PHY_LPI_SLV_SEND_TX_EN,
921 + FIELD_PREP(MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK, 0x120));
923 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG239,
924 + MTK_PHY_LPI_SEND_LOC_TIMER_MASK |
925 + MTK_PHY_LPI_TXPCS_LOC_RCV,
926 + FIELD_PREP(MTK_PHY_LPI_SEND_LOC_TIMER_MASK, 0x117));
928 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG2C7,
929 + MTK_PHY_MAX_GAIN_MASK | MTK_PHY_MIN_GAIN_MASK,
930 + FIELD_PREP(MTK_PHY_MAX_GAIN_MASK, 0x8) |
931 + FIELD_PREP(MTK_PHY_MIN_GAIN_MASK, 0x13));
933 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG2D1,
934 + MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK,
935 + FIELD_PREP(MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK,
937 + MTK_PHY_LPI_SKIP_SD_SLV_TR | MTK_PHY_LPI_TR_READY |
938 + MTK_PHY_LPI_VCO_EEE_STG0_EN);
940 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG323,
941 + MTK_PHY_EEE_WAKE_MAS_INT_DC |
942 + MTK_PHY_EEE_WAKE_SLV_INT_DC);
944 + phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG324,
945 + MTK_PHY_SMI_DETCNT_MAX_MASK,
946 + FIELD_PREP(MTK_PHY_SMI_DETCNT_MAX_MASK, 0x3f) |
947 + MTK_PHY_SMI_DET_MAX_EN);
949 + phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG326,
950 + MTK_PHY_LPI_MODE_SD_ON | MTK_PHY_RESET_RANDUPD_CNT |
951 + MTK_PHY_TREC_UPDATE_ENAB_CLR |
952 + MTK_PHY_LPI_QUIT_WAIT_DFE_SIG_DET_OFF |
953 + MTK_PHY_TR_READY_SKIP_AFE_WAKEUP);
955 + phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
956 + /* Regsigdet_sel_1000 = 0 */
957 + __phy_write(phydev, 0x11, 0xb);
958 + __phy_write(phydev, 0x12, 0x0);
959 + __phy_write(phydev, 0x10, 0x9690);
961 + /* REG_EEE_st2TrKf1000 = 3 */
962 + __phy_write(phydev, 0x11, 0x114f);
963 + __phy_write(phydev, 0x12, 0x2);
964 + __phy_write(phydev, 0x10, 0x969a);
966 + /* RegEEE_slv_wake_tr_timer_tar = 6, RegEEE_slv_remtx_timer_tar = 20 */
967 + __phy_write(phydev, 0x11, 0x3028);
968 + __phy_write(phydev, 0x12, 0x0);
969 + __phy_write(phydev, 0x10, 0x969e);
971 + /* RegEEE_slv_wake_int_timer_tar = 8 */
972 + __phy_write(phydev, 0x11, 0x5010);
973 + __phy_write(phydev, 0x12, 0x0);
974 + __phy_write(phydev, 0x10, 0x96a0);
976 + /* RegEEE_trfreeze_timer2 = 586 */
977 + __phy_write(phydev, 0x11, 0x24a);
978 + __phy_write(phydev, 0x12, 0x0);
979 + __phy_write(phydev, 0x10, 0x96a8);
981 + /* RegEEE100Stg1_tar = 16 */
982 + __phy_write(phydev, 0x11, 0x3210);
983 + __phy_write(phydev, 0x12, 0x0);
984 + __phy_write(phydev, 0x10, 0x96b8);
986 + /* REGEEE_wake_slv_tr_wait_dfesigdet_en = 1 */
987 + __phy_write(phydev, 0x11, 0x1463);
988 + __phy_write(phydev, 0x12, 0x0);
989 + __phy_write(phydev, 0x10, 0x96ca);
991 + /* DfeTailEnableVgaThresh1000 = 27 */
992 + __phy_write(phydev, 0x11, 0x36);
993 + __phy_write(phydev, 0x12, 0x0);
994 + __phy_write(phydev, 0x10, 0x8f80);
995 + phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
997 + phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_3);
998 + __phy_modify(phydev, MTK_PHY_LPI_REG_14, MTK_PHY_LPI_WAKE_TIMER_1000_MASK,
999 + FIELD_PREP(MTK_PHY_LPI_WAKE_TIMER_1000_MASK, 0x19c));
1001 + __phy_modify(phydev, MTK_PHY_LPI_REG_1c, MTK_PHY_SMI_DET_ON_THRESH_MASK,
1002 + FIELD_PREP(MTK_PHY_SMI_DET_ON_THRESH_MASK, 0xc));
1003 + phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
1005 + phy_modify_mmd(phydev, MDIO_MMD_VEND1,
1006 + MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122,
1007 + MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
1008 + FIELD_PREP(MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK, 0xff));
1011 +static int cal_sw(struct phy_device *phydev, enum CAL_ITEM cal_item,
1012 + u8 start_pair, u8 end_pair)
1017 + for (pair_n = start_pair; pair_n <= end_pair; pair_n++) {
1018 + /* TX_OFFSET & TX_AMP have no SW calibration. */
1019 + switch (cal_item) {
1021 + ret = tx_vcm_cal_sw(phydev, pair_n);
1032 +static int cal_efuse(struct phy_device *phydev, enum CAL_ITEM cal_item,
1033 + u8 start_pair, u8 end_pair, u32 *buf)
1038 + for (pair_n = start_pair; pair_n <= end_pair; pair_n++) {
1039 + /* TX_VCM has no efuse calibration. */
1040 + switch (cal_item) {
1042 + ret = rext_cal_efuse(phydev, buf);
1045 + ret = tx_offset_cal_efuse(phydev, buf);
1048 + ret = tx_amp_cal_efuse(phydev, buf);
1051 + ret = tx_r50_cal_efuse(phydev, buf, pair_n);
1063 +static int start_cal(struct phy_device *phydev, enum CAL_ITEM cal_item,
1064 + enum CAL_MODE cal_mode, u8 start_pair,
1065 + u8 end_pair, u32 *buf)
1069 + switch (cal_mode) {
1071 + ret = cal_efuse(phydev, cal_item, start_pair,
1075 + ret = cal_sw(phydev, cal_item, start_pair, end_pair);
1082 + phydev_err(phydev, "cal %d failed\n", cal_item);
1089 +static int mt798x_phy_calibration(struct phy_device *phydev)
1094 + struct nvmem_cell *cell;
1096 + cell = nvmem_cell_get(&phydev->mdio.dev, "phy-cal-data");
1097 + if (IS_ERR(cell)) {
1098 + if (PTR_ERR(cell) == -EPROBE_DEFER)
1099 + return PTR_ERR(cell);
1103 + buf = (u32 *)nvmem_cell_read(cell, &len);
1105 + return PTR_ERR(buf);
1106 + nvmem_cell_put(cell);
1108 + if (!buf[0] || !buf[1] || !buf[2] || !buf[3] || len < 4 * sizeof(u32)) {
1109 + phydev_err(phydev, "invalid efuse data\n");
1114 + ret = start_cal(phydev, REXT, EFUSE_M, NO_PAIR, NO_PAIR, buf);
1117 + ret = start_cal(phydev, TX_OFFSET, EFUSE_M, NO_PAIR, NO_PAIR, buf);
1120 + ret = start_cal(phydev, TX_AMP, EFUSE_M, NO_PAIR, NO_PAIR, buf);
1123 + ret = start_cal(phydev, TX_R50, EFUSE_M, PAIR_A, PAIR_D, buf);
1126 + ret = start_cal(phydev, TX_VCM, SW_M, PAIR_A, PAIR_A, buf);
1135 +static int mt798x_phy_config_init(struct phy_device *phydev)
1137 + switch (phydev->drv->phy_id) {
1138 + case MTK_GPHY_ID_MT7981:
1139 + mt7981_phy_finetune(phydev);
1141 + case MTK_GPHY_ID_MT7988:
1142 + mt7988_phy_finetune(phydev);
1146 + mt798x_phy_common_finetune(phydev);
1147 + mt798x_phy_eee(phydev);
1149 + return mt798x_phy_calibration(phydev);
1152 +static struct phy_driver mtk_socphy_driver[] = {
1154 + PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7981),
1155 + .name = "MediaTek MT7981 PHY",
1156 + .config_init = mt798x_phy_config_init,
1157 + .config_intr = genphy_no_config_intr,
1158 + .handle_interrupt = genphy_handle_interrupt_no_ack,
1159 + .probe = mt798x_phy_calibration,
1160 + .suspend = genphy_suspend,
1161 + .resume = genphy_resume,
1162 + .read_page = mtk_socphy_read_page,
1163 + .write_page = mtk_socphy_write_page,
1166 + PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7988),
1167 + .name = "MediaTek MT7988 PHY",
1168 + .config_init = mt798x_phy_config_init,
1169 + .config_intr = genphy_no_config_intr,
1170 + .handle_interrupt = genphy_handle_interrupt_no_ack,
1171 + .probe = mt798x_phy_calibration,
1172 + .suspend = genphy_suspend,
1173 + .resume = genphy_resume,
1174 + .read_page = mtk_socphy_read_page,
1175 + .write_page = mtk_socphy_write_page,
1179 +module_phy_driver(mtk_socphy_driver);
1181 +static struct mdio_device_id __maybe_unused mtk_socphy_tbl[] = {
1182 + { PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7981) },
1183 + { PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7988) },
1187 +MODULE_DESCRIPTION("MediaTek SoC Gigabit Ethernet PHY driver");
1188 +MODULE_AUTHOR("Daniel Golle <daniel@makrotopia.org>");
1189 +MODULE_AUTHOR("SkyLake Huang <SkyLake.Huang@mediatek.com>");
1190 +MODULE_LICENSE("GPL");
1192 +MODULE_DEVICE_TABLE(mdio, mtk_socphy_tbl);
1193 --- a/drivers/net/phy/mediatek-ge.c
1194 +++ b/drivers/net/phy/mediatek-ge.c
1195 @@ -136,7 +136,8 @@ static struct phy_driver mtk_gephy_drive
1196 module_phy_driver(mtk_gephy_driver);
1198 static struct mdio_device_id __maybe_unused mtk_gephy_tbl[] = {
1199 - { PHY_ID_MATCH_VENDOR(0x03a29400) },
1200 + { PHY_ID_MATCH_EXACT(0x03a29441) },
1201 + { PHY_ID_MATCH_EXACT(0x03a29412) },