1 // SPDX-License-Identifier: GPL-2.0+
2 #include <linux/bitfield.h>
3 #include <linux/bitmap.h>
4 #include <linux/mfd/syscon.h>
5 #include <linux/module.h>
6 #include <linux/nvmem-consumer.h>
7 #include <linux/pinctrl/consumer.h>
9 #include <linux/regmap.h>
11 #define MTK_GPHY_ID_MT7981 0x03a29461
12 #define MTK_GPHY_ID_MT7988 0x03a29481
14 #define MTK_EXT_PAGE_ACCESS 0x1f
15 #define MTK_PHY_PAGE_STANDARD 0x0000
16 #define MTK_PHY_PAGE_EXTENDED_3 0x0003
18 #define MTK_PHY_LPI_REG_14 0x14
19 #define MTK_PHY_LPI_WAKE_TIMER_1000_MASK GENMASK(8, 0)
21 #define MTK_PHY_LPI_REG_1c 0x1c
22 #define MTK_PHY_SMI_DET_ON_THRESH_MASK GENMASK(13, 8)
24 #define MTK_PHY_PAGE_EXTENDED_2A30 0x2a30
25 #define MTK_PHY_PAGE_EXTENDED_52B5 0x52b5
27 #define ANALOG_INTERNAL_OPERATION_MAX_US 20
28 #define TXRESERVE_MIN 0
29 #define TXRESERVE_MAX 7
31 #define MTK_PHY_ANARG_RG 0x10
32 #define MTK_PHY_TCLKOFFSET_MASK GENMASK(12, 8)
34 /* Registers on MDIO_MMD_VEND1 */
35 #define MTK_PHY_TXVLD_DA_RG 0x12
36 #define MTK_PHY_DA_TX_I2MPB_A_GBE_MASK GENMASK(15, 10)
37 #define MTK_PHY_DA_TX_I2MPB_A_TBT_MASK GENMASK(5, 0)
39 #define MTK_PHY_TX_I2MPB_TEST_MODE_A2 0x16
40 #define MTK_PHY_DA_TX_I2MPB_A_HBT_MASK GENMASK(15, 10)
41 #define MTK_PHY_DA_TX_I2MPB_A_TST_MASK GENMASK(5, 0)
43 #define MTK_PHY_TX_I2MPB_TEST_MODE_B1 0x17
44 #define MTK_PHY_DA_TX_I2MPB_B_GBE_MASK GENMASK(13, 8)
45 #define MTK_PHY_DA_TX_I2MPB_B_TBT_MASK GENMASK(5, 0)
47 #define MTK_PHY_TX_I2MPB_TEST_MODE_B2 0x18
48 #define MTK_PHY_DA_TX_I2MPB_B_HBT_MASK GENMASK(13, 8)
49 #define MTK_PHY_DA_TX_I2MPB_B_TST_MASK GENMASK(5, 0)
51 #define MTK_PHY_TX_I2MPB_TEST_MODE_C1 0x19
52 #define MTK_PHY_DA_TX_I2MPB_C_GBE_MASK GENMASK(13, 8)
53 #define MTK_PHY_DA_TX_I2MPB_C_TBT_MASK GENMASK(5, 0)
55 #define MTK_PHY_TX_I2MPB_TEST_MODE_C2 0x20
56 #define MTK_PHY_DA_TX_I2MPB_C_HBT_MASK GENMASK(13, 8)
57 #define MTK_PHY_DA_TX_I2MPB_C_TST_MASK GENMASK(5, 0)
59 #define MTK_PHY_TX_I2MPB_TEST_MODE_D1 0x21
60 #define MTK_PHY_DA_TX_I2MPB_D_GBE_MASK GENMASK(13, 8)
61 #define MTK_PHY_DA_TX_I2MPB_D_TBT_MASK GENMASK(5, 0)
63 #define MTK_PHY_TX_I2MPB_TEST_MODE_D2 0x22
64 #define MTK_PHY_DA_TX_I2MPB_D_HBT_MASK GENMASK(13, 8)
65 #define MTK_PHY_DA_TX_I2MPB_D_TST_MASK GENMASK(5, 0)
67 #define MTK_PHY_RXADC_CTRL_RG7 0xc6
68 #define MTK_PHY_DA_AD_BUF_BIAS_LP_MASK GENMASK(9, 8)
70 #define MTK_PHY_RXADC_CTRL_RG9 0xc8
71 #define MTK_PHY_DA_RX_PSBN_TBT_MASK GENMASK(14, 12)
72 #define MTK_PHY_DA_RX_PSBN_HBT_MASK GENMASK(10, 8)
73 #define MTK_PHY_DA_RX_PSBN_GBE_MASK GENMASK(6, 4)
74 #define MTK_PHY_DA_RX_PSBN_LP_MASK GENMASK(2, 0)
76 #define MTK_PHY_LDO_OUTPUT_V 0xd7
78 #define MTK_PHY_RG_ANA_CAL_RG0 0xdb
79 #define MTK_PHY_RG_CAL_CKINV BIT(12)
80 #define MTK_PHY_RG_ANA_CALEN BIT(8)
81 #define MTK_PHY_RG_ZCALEN_A BIT(0)
83 #define MTK_PHY_RG_ANA_CAL_RG1 0xdc
84 #define MTK_PHY_RG_ZCALEN_B BIT(12)
85 #define MTK_PHY_RG_ZCALEN_C BIT(8)
86 #define MTK_PHY_RG_ZCALEN_D BIT(4)
87 #define MTK_PHY_RG_TXVOS_CALEN BIT(0)
89 #define MTK_PHY_RG_ANA_CAL_RG5 0xe0
90 #define MTK_PHY_RG_REXT_TRIM_MASK GENMASK(13, 8)
92 #define MTK_PHY_RG_TX_FILTER 0xfe
94 #define MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG120 0x120
95 #define MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK GENMASK(12, 8)
96 #define MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK GENMASK(4, 0)
98 #define MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122 0x122
99 #define MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK GENMASK(7, 0)
101 #define MTK_PHY_RG_TESTMUX_ADC_CTRL 0x144
102 #define MTK_PHY_RG_TXEN_DIG_MASK GENMASK(5, 5)
104 #define MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B 0x172
105 #define MTK_PHY_CR_TX_AMP_OFFSET_A_MASK GENMASK(13, 8)
106 #define MTK_PHY_CR_TX_AMP_OFFSET_B_MASK GENMASK(6, 0)
108 #define MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D 0x173
109 #define MTK_PHY_CR_TX_AMP_OFFSET_C_MASK GENMASK(13, 8)
110 #define MTK_PHY_CR_TX_AMP_OFFSET_D_MASK GENMASK(6, 0)
112 #define MTK_PHY_RG_AD_CAL_COMP 0x17a
113 #define MTK_PHY_AD_CAL_COMP_OUT_SHIFT (8)
115 #define MTK_PHY_RG_AD_CAL_CLK 0x17b
116 #define MTK_PHY_DA_CAL_CLK BIT(0)
118 #define MTK_PHY_RG_AD_CALIN 0x17c
119 #define MTK_PHY_DA_CALIN_FLAG BIT(0)
121 #define MTK_PHY_RG_DASN_DAC_IN0_A 0x17d
122 #define MTK_PHY_DASN_DAC_IN0_A_MASK GENMASK(9, 0)
124 #define MTK_PHY_RG_DASN_DAC_IN0_B 0x17e
125 #define MTK_PHY_DASN_DAC_IN0_B_MASK GENMASK(9, 0)
127 #define MTK_PHY_RG_DASN_DAC_IN0_C 0x17f
128 #define MTK_PHY_DASN_DAC_IN0_C_MASK GENMASK(9, 0)
130 #define MTK_PHY_RG_DASN_DAC_IN0_D 0x180
131 #define MTK_PHY_DASN_DAC_IN0_D_MASK GENMASK(9, 0)
133 #define MTK_PHY_RG_DASN_DAC_IN1_A 0x181
134 #define MTK_PHY_DASN_DAC_IN1_A_MASK GENMASK(9, 0)
136 #define MTK_PHY_RG_DASN_DAC_IN1_B 0x182
137 #define MTK_PHY_DASN_DAC_IN1_B_MASK GENMASK(9, 0)
139 #define MTK_PHY_RG_DASN_DAC_IN1_C 0x183
140 #define MTK_PHY_DASN_DAC_IN1_C_MASK GENMASK(9, 0)
142 #define MTK_PHY_RG_DASN_DAC_IN1_D 0x184
143 #define MTK_PHY_DASN_DAC_IN1_D_MASK GENMASK(9, 0)
145 #define MTK_PHY_RG_DEV1E_REG19b 0x19b
146 #define MTK_PHY_BYPASS_DSP_LPI_READY BIT(8)
148 #define MTK_PHY_RG_LP_IIR2_K1_L 0x22a
149 #define MTK_PHY_RG_LP_IIR2_K1_U 0x22b
150 #define MTK_PHY_RG_LP_IIR2_K2_L 0x22c
151 #define MTK_PHY_RG_LP_IIR2_K2_U 0x22d
152 #define MTK_PHY_RG_LP_IIR2_K3_L 0x22e
153 #define MTK_PHY_RG_LP_IIR2_K3_U 0x22f
154 #define MTK_PHY_RG_LP_IIR2_K4_L 0x230
155 #define MTK_PHY_RG_LP_IIR2_K4_U 0x231
156 #define MTK_PHY_RG_LP_IIR2_K5_L 0x232
157 #define MTK_PHY_RG_LP_IIR2_K5_U 0x233
159 #define MTK_PHY_RG_DEV1E_REG234 0x234
160 #define MTK_PHY_TR_OPEN_LOOP_EN_MASK GENMASK(0, 0)
161 #define MTK_PHY_LPF_X_AVERAGE_MASK GENMASK(7, 4)
162 #define MTK_PHY_TR_LP_IIR_EEE_EN BIT(12)
164 #define MTK_PHY_RG_LPF_CNT_VAL 0x235
166 #define MTK_PHY_RG_DEV1E_REG238 0x238
167 #define MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK GENMASK(8, 0)
168 #define MTK_PHY_LPI_SLV_SEND_TX_EN BIT(12)
170 #define MTK_PHY_RG_DEV1E_REG239 0x239
171 #define MTK_PHY_LPI_SEND_LOC_TIMER_MASK GENMASK(8, 0)
172 #define MTK_PHY_LPI_TXPCS_LOC_RCV BIT(12)
174 #define MTK_PHY_RG_DEV1E_REG27C 0x27c
175 #define MTK_PHY_VGASTATE_FFE_THR_ST1_MASK GENMASK(12, 8)
176 #define MTK_PHY_RG_DEV1E_REG27D 0x27d
177 #define MTK_PHY_VGASTATE_FFE_THR_ST2_MASK GENMASK(4, 0)
179 #define MTK_PHY_RG_DEV1E_REG2C7 0x2c7
180 #define MTK_PHY_MAX_GAIN_MASK GENMASK(4, 0)
181 #define MTK_PHY_MIN_GAIN_MASK GENMASK(12, 8)
183 #define MTK_PHY_RG_DEV1E_REG2D1 0x2d1
184 #define MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK GENMASK(7, 0)
185 #define MTK_PHY_LPI_SKIP_SD_SLV_TR BIT(8)
186 #define MTK_PHY_LPI_TR_READY BIT(9)
187 #define MTK_PHY_LPI_VCO_EEE_STG0_EN BIT(10)
189 #define MTK_PHY_RG_DEV1E_REG323 0x323
190 #define MTK_PHY_EEE_WAKE_MAS_INT_DC BIT(0)
191 #define MTK_PHY_EEE_WAKE_SLV_INT_DC BIT(4)
193 #define MTK_PHY_RG_DEV1E_REG324 0x324
194 #define MTK_PHY_SMI_DETCNT_MAX_MASK GENMASK(5, 0)
195 #define MTK_PHY_SMI_DET_MAX_EN BIT(8)
197 #define MTK_PHY_RG_DEV1E_REG326 0x326
198 #define MTK_PHY_LPI_MODE_SD_ON BIT(0)
199 #define MTK_PHY_RESET_RANDUPD_CNT BIT(1)
200 #define MTK_PHY_TREC_UPDATE_ENAB_CLR BIT(2)
201 #define MTK_PHY_LPI_QUIT_WAIT_DFE_SIG_DET_OFF BIT(4)
202 #define MTK_PHY_TR_READY_SKIP_AFE_WAKEUP BIT(5)
204 #define MTK_PHY_LDO_PUMP_EN_PAIRAB 0x502
205 #define MTK_PHY_LDO_PUMP_EN_PAIRCD 0x503
207 #define MTK_PHY_DA_TX_R50_PAIR_A 0x53d
208 #define MTK_PHY_DA_TX_R50_PAIR_B 0x53e
209 #define MTK_PHY_DA_TX_R50_PAIR_C 0x53f
210 #define MTK_PHY_DA_TX_R50_PAIR_D 0x540
212 /* Registers on MDIO_MMD_VEND2 */
213 #define MTK_PHY_LED0_ON_CTRL 0x24
214 #define MTK_PHY_LED1_ON_CTRL 0x26
215 #define MTK_PHY_LED_ON_MASK GENMASK(6, 0)
216 #define MTK_PHY_LED_ON_LINK1000 BIT(0)
217 #define MTK_PHY_LED_ON_LINK100 BIT(1)
218 #define MTK_PHY_LED_ON_LINK10 BIT(2)
219 #define MTK_PHY_LED_ON_LINKDOWN BIT(3)
220 #define MTK_PHY_LED_ON_FDX BIT(4) /* Full duplex */
221 #define MTK_PHY_LED_ON_HDX BIT(5) /* Half duplex */
222 #define MTK_PHY_LED_ON_FORCE_ON BIT(6)
223 #define MTK_PHY_LED_ON_POLARITY BIT(14)
224 #define MTK_PHY_LED_ON_ENABLE BIT(15)
226 #define MTK_PHY_LED0_BLINK_CTRL 0x25
227 #define MTK_PHY_LED1_BLINK_CTRL 0x27
228 #define MTK_PHY_LED_BLINK_1000TX BIT(0)
229 #define MTK_PHY_LED_BLINK_1000RX BIT(1)
230 #define MTK_PHY_LED_BLINK_100TX BIT(2)
231 #define MTK_PHY_LED_BLINK_100RX BIT(3)
232 #define MTK_PHY_LED_BLINK_10TX BIT(4)
233 #define MTK_PHY_LED_BLINK_10RX BIT(5)
234 #define MTK_PHY_LED_BLINK_COLLISION BIT(6)
235 #define MTK_PHY_LED_BLINK_RX_CRC_ERR BIT(7)
236 #define MTK_PHY_LED_BLINK_RX_IDLE_ERR BIT(8)
237 #define MTK_PHY_LED_BLINK_FORCE_BLINK BIT(9)
239 #define MTK_PHY_LED1_DEFAULT_POLARITIES BIT(1)
241 #define MTK_PHY_RG_BG_RASEL 0x115
242 #define MTK_PHY_RG_BG_RASEL_MASK GENMASK(2, 0)
244 /* 'boottrap' register reflecting the configuration of the 4 PHY LEDs */
245 #define RG_GPIO_MISC_TPBANK0 0x6f0
246 #define RG_GPIO_MISC_TPBANK0_BOOTMODE GENMASK(11, 8)
248 /* These macro privides efuse parsing for internal phy. */
249 #define EFS_DA_TX_I2MPB_A(x) (((x) >> 0) & GENMASK(5, 0))
250 #define EFS_DA_TX_I2MPB_B(x) (((x) >> 6) & GENMASK(5, 0))
251 #define EFS_DA_TX_I2MPB_C(x) (((x) >> 12) & GENMASK(5, 0))
252 #define EFS_DA_TX_I2MPB_D(x) (((x) >> 18) & GENMASK(5, 0))
253 #define EFS_DA_TX_AMP_OFFSET_A(x) (((x) >> 24) & GENMASK(5, 0))
255 #define EFS_DA_TX_AMP_OFFSET_B(x) (((x) >> 0) & GENMASK(5, 0))
256 #define EFS_DA_TX_AMP_OFFSET_C(x) (((x) >> 6) & GENMASK(5, 0))
257 #define EFS_DA_TX_AMP_OFFSET_D(x) (((x) >> 12) & GENMASK(5, 0))
258 #define EFS_DA_TX_R50_A(x) (((x) >> 18) & GENMASK(5, 0))
259 #define EFS_DA_TX_R50_B(x) (((x) >> 24) & GENMASK(5, 0))
261 #define EFS_DA_TX_R50_C(x) (((x) >> 0) & GENMASK(5, 0))
262 #define EFS_DA_TX_R50_D(x) (((x) >> 6) & GENMASK(5, 0))
264 #define EFS_RG_BG_RASEL(x) (((x) >> 4) & GENMASK(2, 0))
265 #define EFS_RG_REXT_TRIM(x) (((x) >> 7) & GENMASK(5, 0))
275 enum calibration_mode {
293 #define MTK_PHY_LED_STATE_FORCE_ON 0
294 #define MTK_PHY_LED_STATE_FORCE_BLINK 1
295 #define MTK_PHY_LED_STATE_NETDEV 2
297 struct mtk_socphy_priv {
298 unsigned long led_state;
301 struct mtk_socphy_shared {
303 struct mtk_socphy_priv priv[4];
306 static int mtk_socphy_read_page(struct phy_device *phydev)
308 return __phy_read(phydev, MTK_EXT_PAGE_ACCESS);
311 static int mtk_socphy_write_page(struct phy_device *phydev, int page)
313 return __phy_write(phydev, MTK_EXT_PAGE_ACCESS, page);
316 /* One calibration cycle consists of:
317 * 1.Set DA_CALIN_FLAG high to start calibration. Keep it high
318 * until AD_CAL_COMP is ready to output calibration result.
319 * 2.Wait until DA_CAL_CLK is available.
320 * 3.Fetch AD_CAL_COMP_OUT.
322 static int cal_cycle(struct phy_device *phydev, int devad,
323 u32 regnum, u16 mask, u16 cal_val)
328 phy_modify_mmd(phydev, devad, regnum,
330 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CALIN,
331 MTK_PHY_DA_CALIN_FLAG);
333 ret = phy_read_mmd_poll_timeout(phydev, MDIO_MMD_VEND1,
334 MTK_PHY_RG_AD_CAL_CLK, reg_val,
335 reg_val & MTK_PHY_DA_CAL_CLK, 500,
336 ANALOG_INTERNAL_OPERATION_MAX_US, false);
338 phydev_err(phydev, "Calibration cycle timeout\n");
342 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CALIN,
343 MTK_PHY_DA_CALIN_FLAG);
344 ret = phy_read_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_AD_CAL_COMP) >>
345 MTK_PHY_AD_CAL_COMP_OUT_SHIFT;
346 phydev_dbg(phydev, "cal_val: 0x%x, ret: %d\n", cal_val, ret);
351 static int rext_fill_result(struct phy_device *phydev, u16 *buf)
353 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG5,
354 MTK_PHY_RG_REXT_TRIM_MASK, buf[0] << 8);
355 phy_modify_mmd(phydev, MDIO_MMD_VEND2, MTK_PHY_RG_BG_RASEL,
356 MTK_PHY_RG_BG_RASEL_MASK, buf[1]);
361 static int rext_cal_efuse(struct phy_device *phydev, u32 *buf)
365 rext_cal_val[0] = EFS_RG_REXT_TRIM(buf[3]);
366 rext_cal_val[1] = EFS_RG_BG_RASEL(buf[3]);
367 rext_fill_result(phydev, rext_cal_val);
372 static int tx_offset_fill_result(struct phy_device *phydev, u16 *buf)
374 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B,
375 MTK_PHY_CR_TX_AMP_OFFSET_A_MASK, buf[0] << 8);
376 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_A_B,
377 MTK_PHY_CR_TX_AMP_OFFSET_B_MASK, buf[1]);
378 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D,
379 MTK_PHY_CR_TX_AMP_OFFSET_C_MASK, buf[2] << 8);
380 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_CR_TX_AMP_OFFSET_C_D,
381 MTK_PHY_CR_TX_AMP_OFFSET_D_MASK, buf[3]);
386 static int tx_offset_cal_efuse(struct phy_device *phydev, u32 *buf)
388 u16 tx_offset_cal_val[4];
390 tx_offset_cal_val[0] = EFS_DA_TX_AMP_OFFSET_A(buf[0]);
391 tx_offset_cal_val[1] = EFS_DA_TX_AMP_OFFSET_B(buf[1]);
392 tx_offset_cal_val[2] = EFS_DA_TX_AMP_OFFSET_C(buf[1]);
393 tx_offset_cal_val[3] = EFS_DA_TX_AMP_OFFSET_D(buf[1]);
395 tx_offset_fill_result(phydev, tx_offset_cal_val);
400 static int tx_amp_fill_result(struct phy_device *phydev, u16 *buf)
404 const int vals_9461[16] = { 7, 1, 4, 7,
408 const int vals_9481[16] = { 10, 6, 6, 10,
412 switch (phydev->drv->phy_id) {
413 case MTK_GPHY_ID_MT7981:
414 /* We add some calibration to efuse values
415 * due to board level influence.
416 * GBE: +7, TBT: +1, HBT: +4, TST: +7
418 memcpy(bias, (const void *)vals_9461, sizeof(bias));
420 case MTK_GPHY_ID_MT7988:
421 memcpy(bias, (const void *)vals_9481, sizeof(bias));
425 /* Prevent overflow */
426 for (i = 0; i < 12; i++) {
427 if (buf[i >> 2] + bias[i] > 63) {
433 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TXVLD_DA_RG,
434 MTK_PHY_DA_TX_I2MPB_A_GBE_MASK, (buf[0] + bias[0]) << 10);
435 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TXVLD_DA_RG,
436 MTK_PHY_DA_TX_I2MPB_A_TBT_MASK, buf[0] + bias[1]);
437 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_A2,
438 MTK_PHY_DA_TX_I2MPB_A_HBT_MASK, (buf[0] + bias[2]) << 10);
439 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_A2,
440 MTK_PHY_DA_TX_I2MPB_A_TST_MASK, buf[0] + bias[3]);
442 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B1,
443 MTK_PHY_DA_TX_I2MPB_B_GBE_MASK, (buf[1] + bias[4]) << 8);
444 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B1,
445 MTK_PHY_DA_TX_I2MPB_B_TBT_MASK, buf[1] + bias[5]);
446 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B2,
447 MTK_PHY_DA_TX_I2MPB_B_HBT_MASK, (buf[1] + bias[6]) << 8);
448 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_B2,
449 MTK_PHY_DA_TX_I2MPB_B_TST_MASK, buf[1] + bias[7]);
451 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C1,
452 MTK_PHY_DA_TX_I2MPB_C_GBE_MASK, (buf[2] + bias[8]) << 8);
453 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C1,
454 MTK_PHY_DA_TX_I2MPB_C_TBT_MASK, buf[2] + bias[9]);
455 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C2,
456 MTK_PHY_DA_TX_I2MPB_C_HBT_MASK, (buf[2] + bias[10]) << 8);
457 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_C2,
458 MTK_PHY_DA_TX_I2MPB_C_TST_MASK, buf[2] + bias[11]);
460 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D1,
461 MTK_PHY_DA_TX_I2MPB_D_GBE_MASK, (buf[3] + bias[12]) << 8);
462 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D1,
463 MTK_PHY_DA_TX_I2MPB_D_TBT_MASK, buf[3] + bias[13]);
464 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D2,
465 MTK_PHY_DA_TX_I2MPB_D_HBT_MASK, (buf[3] + bias[14]) << 8);
466 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_TX_I2MPB_TEST_MODE_D2,
467 MTK_PHY_DA_TX_I2MPB_D_TST_MASK, buf[3] + bias[15]);
472 static int tx_amp_cal_efuse(struct phy_device *phydev, u32 *buf)
474 u16 tx_amp_cal_val[4];
476 tx_amp_cal_val[0] = EFS_DA_TX_I2MPB_A(buf[0]);
477 tx_amp_cal_val[1] = EFS_DA_TX_I2MPB_B(buf[0]);
478 tx_amp_cal_val[2] = EFS_DA_TX_I2MPB_C(buf[0]);
479 tx_amp_cal_val[3] = EFS_DA_TX_I2MPB_D(buf[0]);
480 tx_amp_fill_result(phydev, tx_amp_cal_val);
485 static int tx_r50_fill_result(struct phy_device *phydev, u16 tx_r50_cal_val,
491 if (phydev->drv->phy_id == MTK_GPHY_ID_MT7988)
494 val = clamp_val(bias + tx_r50_cal_val, 0, 63);
496 switch (txg_calen_x) {
498 reg = MTK_PHY_DA_TX_R50_PAIR_A;
501 reg = MTK_PHY_DA_TX_R50_PAIR_B;
504 reg = MTK_PHY_DA_TX_R50_PAIR_C;
507 reg = MTK_PHY_DA_TX_R50_PAIR_D;
513 phy_write_mmd(phydev, MDIO_MMD_VEND1, reg, val | val << 8);
518 static int tx_r50_cal_efuse(struct phy_device *phydev, u32 *buf,
523 switch (txg_calen_x) {
525 tx_r50_cal_val = EFS_DA_TX_R50_A(buf[1]);
528 tx_r50_cal_val = EFS_DA_TX_R50_B(buf[1]);
531 tx_r50_cal_val = EFS_DA_TX_R50_C(buf[2]);
534 tx_r50_cal_val = EFS_DA_TX_R50_D(buf[2]);
539 tx_r50_fill_result(phydev, tx_r50_cal_val, txg_calen_x);
544 static int tx_vcm_cal_sw(struct phy_device *phydev, u8 rg_txreserve_x)
546 u8 lower_idx, upper_idx, txreserve_val;
547 u8 lower_ret, upper_ret;
550 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
551 MTK_PHY_RG_ANA_CALEN);
552 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
553 MTK_PHY_RG_CAL_CKINV);
554 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
555 MTK_PHY_RG_TXVOS_CALEN);
557 switch (rg_txreserve_x) {
559 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
560 MTK_PHY_RG_DASN_DAC_IN0_A,
561 MTK_PHY_DASN_DAC_IN0_A_MASK);
562 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
563 MTK_PHY_RG_DASN_DAC_IN1_A,
564 MTK_PHY_DASN_DAC_IN1_A_MASK);
565 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
566 MTK_PHY_RG_ANA_CAL_RG0,
567 MTK_PHY_RG_ZCALEN_A);
570 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
571 MTK_PHY_RG_DASN_DAC_IN0_B,
572 MTK_PHY_DASN_DAC_IN0_B_MASK);
573 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
574 MTK_PHY_RG_DASN_DAC_IN1_B,
575 MTK_PHY_DASN_DAC_IN1_B_MASK);
576 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
577 MTK_PHY_RG_ANA_CAL_RG1,
578 MTK_PHY_RG_ZCALEN_B);
581 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
582 MTK_PHY_RG_DASN_DAC_IN0_C,
583 MTK_PHY_DASN_DAC_IN0_C_MASK);
584 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
585 MTK_PHY_RG_DASN_DAC_IN1_C,
586 MTK_PHY_DASN_DAC_IN1_C_MASK);
587 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
588 MTK_PHY_RG_ANA_CAL_RG1,
589 MTK_PHY_RG_ZCALEN_C);
592 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
593 MTK_PHY_RG_DASN_DAC_IN0_D,
594 MTK_PHY_DASN_DAC_IN0_D_MASK);
595 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
596 MTK_PHY_RG_DASN_DAC_IN1_D,
597 MTK_PHY_DASN_DAC_IN1_D_MASK);
598 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
599 MTK_PHY_RG_ANA_CAL_RG1,
600 MTK_PHY_RG_ZCALEN_D);
607 lower_idx = TXRESERVE_MIN;
608 upper_idx = TXRESERVE_MAX;
610 phydev_dbg(phydev, "Start TX-VCM SW cal.\n");
611 while ((upper_idx - lower_idx) > 1) {
612 txreserve_val = DIV_ROUND_CLOSEST(lower_idx + upper_idx, 2);
613 ret = cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
614 MTK_PHY_DA_RX_PSBN_TBT_MASK |
615 MTK_PHY_DA_RX_PSBN_HBT_MASK |
616 MTK_PHY_DA_RX_PSBN_GBE_MASK |
617 MTK_PHY_DA_RX_PSBN_LP_MASK,
618 txreserve_val << 12 | txreserve_val << 8 |
619 txreserve_val << 4 | txreserve_val);
621 upper_idx = txreserve_val;
623 } else if (ret == 0) {
624 lower_idx = txreserve_val;
631 if (lower_idx == TXRESERVE_MIN) {
632 lower_ret = cal_cycle(phydev, MDIO_MMD_VEND1,
633 MTK_PHY_RXADC_CTRL_RG9,
634 MTK_PHY_DA_RX_PSBN_TBT_MASK |
635 MTK_PHY_DA_RX_PSBN_HBT_MASK |
636 MTK_PHY_DA_RX_PSBN_GBE_MASK |
637 MTK_PHY_DA_RX_PSBN_LP_MASK,
638 lower_idx << 12 | lower_idx << 8 |
639 lower_idx << 4 | lower_idx);
641 } else if (upper_idx == TXRESERVE_MAX) {
642 upper_ret = cal_cycle(phydev, MDIO_MMD_VEND1,
643 MTK_PHY_RXADC_CTRL_RG9,
644 MTK_PHY_DA_RX_PSBN_TBT_MASK |
645 MTK_PHY_DA_RX_PSBN_HBT_MASK |
646 MTK_PHY_DA_RX_PSBN_GBE_MASK |
647 MTK_PHY_DA_RX_PSBN_LP_MASK,
648 upper_idx << 12 | upper_idx << 8 |
649 upper_idx << 4 | upper_idx);
655 /* We calibrate TX-VCM in different logic. Check upper index and then
656 * lower index. If this calibration is valid, apply lower index's result.
658 ret = upper_ret - lower_ret;
661 /* Make sure we use upper_idx in our calibration system */
662 cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
663 MTK_PHY_DA_RX_PSBN_TBT_MASK |
664 MTK_PHY_DA_RX_PSBN_HBT_MASK |
665 MTK_PHY_DA_RX_PSBN_GBE_MASK |
666 MTK_PHY_DA_RX_PSBN_LP_MASK,
667 upper_idx << 12 | upper_idx << 8 |
668 upper_idx << 4 | upper_idx);
669 phydev_dbg(phydev, "TX-VCM SW cal result: 0x%x\n", upper_idx);
670 } else if (lower_idx == TXRESERVE_MIN && upper_ret == 1 &&
673 cal_cycle(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG9,
674 MTK_PHY_DA_RX_PSBN_TBT_MASK |
675 MTK_PHY_DA_RX_PSBN_HBT_MASK |
676 MTK_PHY_DA_RX_PSBN_GBE_MASK |
677 MTK_PHY_DA_RX_PSBN_LP_MASK,
678 lower_idx << 12 | lower_idx << 8 |
679 lower_idx << 4 | lower_idx);
680 phydev_warn(phydev, "TX-VCM SW cal result at low margin 0x%x\n",
682 } else if (upper_idx == TXRESERVE_MAX && upper_ret == 0 &&
685 phydev_warn(phydev, "TX-VCM SW cal result at high margin 0x%x\n",
692 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
693 MTK_PHY_RG_ANA_CALEN);
694 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
695 MTK_PHY_RG_TXVOS_CALEN);
696 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG0,
697 MTK_PHY_RG_ZCALEN_A);
698 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_ANA_CAL_RG1,
699 MTK_PHY_RG_ZCALEN_B | MTK_PHY_RG_ZCALEN_C |
700 MTK_PHY_RG_ZCALEN_D);
705 static void mt798x_phy_common_finetune(struct phy_device *phydev)
707 phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
708 /* SlvDSPreadyTime = 24, MasDSPreadyTime = 24 */
709 __phy_write(phydev, 0x11, 0xc71);
710 __phy_write(phydev, 0x12, 0xc);
711 __phy_write(phydev, 0x10, 0x8fae);
713 /* EnabRandUpdTrig = 1 */
714 __phy_write(phydev, 0x11, 0x2f00);
715 __phy_write(phydev, 0x12, 0xe);
716 __phy_write(phydev, 0x10, 0x8fb0);
718 /* NormMseLoThresh = 85 */
719 __phy_write(phydev, 0x11, 0x55a0);
720 __phy_write(phydev, 0x12, 0x0);
721 __phy_write(phydev, 0x10, 0x83aa);
723 /* FfeUpdGainForce = 1(Enable), FfeUpdGainForceVal = 4 */
724 __phy_write(phydev, 0x11, 0x240);
725 __phy_write(phydev, 0x12, 0x0);
726 __phy_write(phydev, 0x10, 0x9680);
728 /* TrFreeze = 0 (mt7988 default) */
729 __phy_write(phydev, 0x11, 0x0);
730 __phy_write(phydev, 0x12, 0x0);
731 __phy_write(phydev, 0x10, 0x9686);
735 /* SSTrKp1000Mas = 5 */
736 /* SSTrKf1000Mas = 6 */
737 /* SSTrKp1000Slv = 5 */
738 /* SSTrKf1000Slv = 6 */
739 __phy_write(phydev, 0x11, 0xbaef);
740 __phy_write(phydev, 0x12, 0x2e);
741 __phy_write(phydev, 0x10, 0x968c);
742 phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
745 static void mt7981_phy_finetune(struct phy_device *phydev)
747 u16 val[8] = { 0x01ce, 0x01c1,
753 /* 100M eye finetune:
754 * Keep middle level of TX MLT3 shapper as default.
755 * Only change TX MLT3 overshoot level here.
757 for (k = 0, i = 1; i < 12; i++) {
760 phy_write_mmd(phydev, MDIO_MMD_VEND1, i, val[k++]);
763 phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
764 /* ResetSyncOffset = 6 */
765 __phy_write(phydev, 0x11, 0x600);
766 __phy_write(phydev, 0x12, 0x0);
767 __phy_write(phydev, 0x10, 0x8fc0);
770 __phy_write(phydev, 0x11, 0x4c2a);
771 __phy_write(phydev, 0x12, 0x3e);
772 __phy_write(phydev, 0x10, 0x8fa4);
774 /* MrvlTrFix100Kp = 3, MrvlTrFix100Kf = 2,
775 * MrvlTrFix1000Kp = 3, MrvlTrFix1000Kf = 2
777 __phy_write(phydev, 0x11, 0xd10a);
778 __phy_write(phydev, 0x12, 0x34);
779 __phy_write(phydev, 0x10, 0x8f82);
781 /* VcoSlicerThreshBitsHigh */
782 __phy_write(phydev, 0x11, 0x5555);
783 __phy_write(phydev, 0x12, 0x55);
784 __phy_write(phydev, 0x10, 0x8ec0);
785 phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
787 /* TR_OPEN_LOOP_EN = 1, lpf_x_average = 9 */
788 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG234,
789 MTK_PHY_TR_OPEN_LOOP_EN_MASK | MTK_PHY_LPF_X_AVERAGE_MASK,
790 BIT(0) | FIELD_PREP(MTK_PHY_LPF_X_AVERAGE_MASK, 0x9));
792 /* rg_tr_lpf_cnt_val = 512 */
793 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LPF_CNT_VAL, 0x200);
796 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K1_L, 0x82);
797 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K1_U, 0x0);
798 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K2_L, 0x103);
799 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K2_U, 0x0);
800 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K3_L, 0x82);
801 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K3_U, 0x0);
802 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K4_L, 0xd177);
803 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K4_U, 0x3);
804 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K5_L, 0x2c82);
805 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LP_IIR2_K5_U, 0xe);
808 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG27C,
809 MTK_PHY_VGASTATE_FFE_THR_ST1_MASK, 0x1b << 8);
810 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG27D,
811 MTK_PHY_VGASTATE_FFE_THR_ST2_MASK, 0x1e);
813 /* Disable LDO pump */
814 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_PUMP_EN_PAIRAB, 0x0);
815 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_PUMP_EN_PAIRCD, 0x0);
816 /* Adjust LDO output voltage */
817 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_LDO_OUTPUT_V, 0x2222);
820 static void mt7988_phy_finetune(struct phy_device *phydev)
822 u16 val[12] = { 0x0187, 0x01cd, 0x01c8, 0x0182,
823 0x020d, 0x0206, 0x0384, 0x03d0,
824 0x03c6, 0x030a, 0x0011, 0x0005 };
827 /* Set default MLT3 shaper first */
828 for (i = 0; i < 12; i++)
829 phy_write_mmd(phydev, MDIO_MMD_VEND1, i, val[i]);
832 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_TX_FILTER, 0x5);
834 phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
835 /* ResetSyncOffset = 5 */
836 __phy_write(phydev, 0x11, 0x500);
837 __phy_write(phydev, 0x12, 0x0);
838 __phy_write(phydev, 0x10, 0x8fc0);
840 /* VgaDecRate is 1 at default on mt7988 */
842 /* MrvlTrFix100Kp = 6, MrvlTrFix100Kf = 7,
843 * MrvlTrFix1000Kp = 6, MrvlTrFix1000Kf = 7
845 __phy_write(phydev, 0x11, 0xb90a);
846 __phy_write(phydev, 0x12, 0x6f);
847 __phy_write(phydev, 0x10, 0x8f82);
849 /* RemAckCntLimitCtrl = 1 */
850 __phy_write(phydev, 0x11, 0xfbba);
851 __phy_write(phydev, 0x12, 0xc3);
852 __phy_write(phydev, 0x10, 0x87f8);
854 phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
856 /* TR_OPEN_LOOP_EN = 1, lpf_x_average = 10 */
857 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG234,
858 MTK_PHY_TR_OPEN_LOOP_EN_MASK | MTK_PHY_LPF_X_AVERAGE_MASK,
859 BIT(0) | FIELD_PREP(MTK_PHY_LPF_X_AVERAGE_MASK, 0xa));
861 /* rg_tr_lpf_cnt_val = 1023 */
862 phy_write_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_LPF_CNT_VAL, 0x3ff);
865 static void mt798x_phy_eee(struct phy_device *phydev)
867 phy_modify_mmd(phydev, MDIO_MMD_VEND1,
868 MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG120,
869 MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK |
870 MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK,
871 FIELD_PREP(MTK_PHY_LPI_SIG_EN_LO_THRESH1000_MASK, 0x0) |
872 FIELD_PREP(MTK_PHY_LPI_SIG_EN_HI_THRESH1000_MASK, 0x14));
874 phy_modify_mmd(phydev, MDIO_MMD_VEND1,
875 MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122,
876 MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
877 FIELD_PREP(MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
880 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
881 MTK_PHY_RG_TESTMUX_ADC_CTRL,
882 MTK_PHY_RG_TXEN_DIG_MASK);
884 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1,
885 MTK_PHY_RG_DEV1E_REG19b, MTK_PHY_BYPASS_DSP_LPI_READY);
887 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1,
888 MTK_PHY_RG_DEV1E_REG234, MTK_PHY_TR_LP_IIR_EEE_EN);
890 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG238,
891 MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK |
892 MTK_PHY_LPI_SLV_SEND_TX_EN,
893 FIELD_PREP(MTK_PHY_LPI_SLV_SEND_TX_TIMER_MASK, 0x120));
895 /* Keep MTK_PHY_LPI_SEND_LOC_TIMER as 375 */
896 phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG239,
897 MTK_PHY_LPI_TXPCS_LOC_RCV);
899 /* This also fixes some IoT issues, such as CH340 */
900 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG2C7,
901 MTK_PHY_MAX_GAIN_MASK | MTK_PHY_MIN_GAIN_MASK,
902 FIELD_PREP(MTK_PHY_MAX_GAIN_MASK, 0x8) |
903 FIELD_PREP(MTK_PHY_MIN_GAIN_MASK, 0x13));
905 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG2D1,
906 MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK,
907 FIELD_PREP(MTK_PHY_VCO_SLICER_THRESH_BITS_HIGH_EEE_MASK,
909 MTK_PHY_LPI_SKIP_SD_SLV_TR | MTK_PHY_LPI_TR_READY |
910 MTK_PHY_LPI_VCO_EEE_STG0_EN);
912 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG323,
913 MTK_PHY_EEE_WAKE_MAS_INT_DC |
914 MTK_PHY_EEE_WAKE_SLV_INT_DC);
916 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG324,
917 MTK_PHY_SMI_DETCNT_MAX_MASK,
918 FIELD_PREP(MTK_PHY_SMI_DETCNT_MAX_MASK, 0x3f) |
919 MTK_PHY_SMI_DET_MAX_EN);
921 phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RG_DEV1E_REG326,
922 MTK_PHY_LPI_MODE_SD_ON | MTK_PHY_RESET_RANDUPD_CNT |
923 MTK_PHY_TREC_UPDATE_ENAB_CLR |
924 MTK_PHY_LPI_QUIT_WAIT_DFE_SIG_DET_OFF |
925 MTK_PHY_TR_READY_SKIP_AFE_WAKEUP);
927 phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
928 /* Regsigdet_sel_1000 = 0 */
929 __phy_write(phydev, 0x11, 0xb);
930 __phy_write(phydev, 0x12, 0x0);
931 __phy_write(phydev, 0x10, 0x9690);
933 /* REG_EEE_st2TrKf1000 = 2 */
934 __phy_write(phydev, 0x11, 0x114f);
935 __phy_write(phydev, 0x12, 0x2);
936 __phy_write(phydev, 0x10, 0x969a);
938 /* RegEEE_slv_wake_tr_timer_tar = 6, RegEEE_slv_remtx_timer_tar = 20 */
939 __phy_write(phydev, 0x11, 0x3028);
940 __phy_write(phydev, 0x12, 0x0);
941 __phy_write(phydev, 0x10, 0x969e);
943 /* RegEEE_slv_wake_int_timer_tar = 8 */
944 __phy_write(phydev, 0x11, 0x5010);
945 __phy_write(phydev, 0x12, 0x0);
946 __phy_write(phydev, 0x10, 0x96a0);
948 /* RegEEE_trfreeze_timer2 = 586 */
949 __phy_write(phydev, 0x11, 0x24a);
950 __phy_write(phydev, 0x12, 0x0);
951 __phy_write(phydev, 0x10, 0x96a8);
953 /* RegEEE100Stg1_tar = 16 */
954 __phy_write(phydev, 0x11, 0x3210);
955 __phy_write(phydev, 0x12, 0x0);
956 __phy_write(phydev, 0x10, 0x96b8);
958 /* REGEEE_wake_slv_tr_wait_dfesigdet_en = 0 */
959 __phy_write(phydev, 0x11, 0x1463);
960 __phy_write(phydev, 0x12, 0x0);
961 __phy_write(phydev, 0x10, 0x96ca);
963 /* DfeTailEnableVgaThresh1000 = 27 */
964 __phy_write(phydev, 0x11, 0x36);
965 __phy_write(phydev, 0x12, 0x0);
966 __phy_write(phydev, 0x10, 0x8f80);
967 phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
969 phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_3);
970 __phy_modify(phydev, MTK_PHY_LPI_REG_14, MTK_PHY_LPI_WAKE_TIMER_1000_MASK,
971 FIELD_PREP(MTK_PHY_LPI_WAKE_TIMER_1000_MASK, 0x19c));
973 __phy_modify(phydev, MTK_PHY_LPI_REG_1c, MTK_PHY_SMI_DET_ON_THRESH_MASK,
974 FIELD_PREP(MTK_PHY_SMI_DET_ON_THRESH_MASK, 0xc));
975 phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
977 phy_modify_mmd(phydev, MDIO_MMD_VEND1,
978 MTK_PHY_RG_LPI_PCS_DSP_CTRL_REG122,
979 MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK,
980 FIELD_PREP(MTK_PHY_LPI_NORM_MSE_HI_THRESH1000_MASK, 0xff));
983 static int cal_sw(struct phy_device *phydev, enum CAL_ITEM cal_item,
984 u8 start_pair, u8 end_pair)
989 for (pair_n = start_pair; pair_n <= end_pair; pair_n++) {
990 /* TX_OFFSET & TX_AMP have no SW calibration. */
993 ret = tx_vcm_cal_sw(phydev, pair_n);
1004 static int cal_efuse(struct phy_device *phydev, enum CAL_ITEM cal_item,
1005 u8 start_pair, u8 end_pair, u32 *buf)
1010 for (pair_n = start_pair; pair_n <= end_pair; pair_n++) {
1011 /* TX_VCM has no efuse calibration. */
1014 ret = rext_cal_efuse(phydev, buf);
1017 ret = tx_offset_cal_efuse(phydev, buf);
1020 ret = tx_amp_cal_efuse(phydev, buf);
1023 ret = tx_r50_cal_efuse(phydev, buf, pair_n);
1035 static int start_cal(struct phy_device *phydev, enum CAL_ITEM cal_item,
1036 enum CAL_MODE cal_mode, u8 start_pair,
1037 u8 end_pair, u32 *buf)
1043 ret = cal_efuse(phydev, cal_item, start_pair,
1047 ret = cal_sw(phydev, cal_item, start_pair, end_pair);
1054 phydev_err(phydev, "cal %d failed\n", cal_item);
1061 static int mt798x_phy_calibration(struct phy_device *phydev)
1066 struct nvmem_cell *cell;
1068 cell = nvmem_cell_get(&phydev->mdio.dev, "phy-cal-data");
1070 if (PTR_ERR(cell) == -EPROBE_DEFER)
1071 return PTR_ERR(cell);
1075 buf = (u32 *)nvmem_cell_read(cell, &len);
1077 return PTR_ERR(buf);
1078 nvmem_cell_put(cell);
1080 if (!buf[0] || !buf[1] || !buf[2] || !buf[3] || len < 4 * sizeof(u32)) {
1081 phydev_err(phydev, "invalid efuse data\n");
1086 ret = start_cal(phydev, REXT, EFUSE_M, NO_PAIR, NO_PAIR, buf);
1089 ret = start_cal(phydev, TX_OFFSET, EFUSE_M, NO_PAIR, NO_PAIR, buf);
1092 ret = start_cal(phydev, TX_AMP, EFUSE_M, NO_PAIR, NO_PAIR, buf);
1095 ret = start_cal(phydev, TX_R50, EFUSE_M, PAIR_A, PAIR_D, buf);
1098 ret = start_cal(phydev, TX_VCM, SW_M, PAIR_A, PAIR_A, buf);
1107 static int mt798x_phy_config_init(struct phy_device *phydev)
1109 switch (phydev->drv->phy_id) {
1110 case MTK_GPHY_ID_MT7981:
1111 mt7981_phy_finetune(phydev);
1113 case MTK_GPHY_ID_MT7988:
1114 mt7988_phy_finetune(phydev);
1118 mt798x_phy_common_finetune(phydev);
1119 mt798x_phy_eee(phydev);
1121 return mt798x_phy_calibration(phydev);
1124 static int mt798x_phy_hw_led_on_set(struct phy_device *phydev, u8 index,
1127 unsigned int bit_on = MTK_PHY_LED_STATE_FORCE_ON + (index ? 16 : 0);
1128 struct mtk_socphy_priv *priv = phydev->priv;
1132 changed = !test_and_set_bit(bit_on, &priv->led_state);
1134 changed = !!test_and_clear_bit(bit_on, &priv->led_state);
1136 changed |= !!test_and_clear_bit(MTK_PHY_LED_STATE_NETDEV +
1137 (index ? 16 : 0), &priv->led_state);
1139 return phy_modify_mmd(phydev, MDIO_MMD_VEND2, index ?
1140 MTK_PHY_LED1_ON_CTRL : MTK_PHY_LED0_ON_CTRL,
1141 MTK_PHY_LED_ON_MASK,
1142 on ? MTK_PHY_LED_ON_FORCE_ON : 0);
1147 static int mt798x_phy_hw_led_blink_set(struct phy_device *phydev, u8 index,
1150 unsigned int bit_blink = MTK_PHY_LED_STATE_FORCE_BLINK + (index ? 16 : 0);
1151 struct mtk_socphy_priv *priv = phydev->priv;
1155 changed = !test_and_set_bit(bit_blink, &priv->led_state);
1157 changed = !!test_and_clear_bit(bit_blink, &priv->led_state);
1159 changed |= !!test_bit(MTK_PHY_LED_STATE_NETDEV +
1160 (index ? 16 : 0), &priv->led_state);
1162 return phy_write_mmd(phydev, MDIO_MMD_VEND2, index ?
1163 MTK_PHY_LED1_BLINK_CTRL : MTK_PHY_LED0_BLINK_CTRL,
1164 blinking ? MTK_PHY_LED_BLINK_FORCE_BLINK : 0);
1169 static int mt798x_phy_led_blink_set(struct phy_device *phydev, u8 index,
1170 unsigned long *delay_on,
1171 unsigned long *delay_off)
1173 bool blinking = false;
1179 if (delay_on && delay_off && (*delay_on > 0) && (*delay_off > 0)) {
1185 err = mt798x_phy_hw_led_blink_set(phydev, index, blinking);
1189 return mt798x_phy_hw_led_on_set(phydev, index, false);
1192 static int mt798x_phy_led_brightness_set(struct phy_device *phydev,
1193 u8 index, enum led_brightness value)
1197 err = mt798x_phy_hw_led_blink_set(phydev, index, false);
1201 return mt798x_phy_hw_led_on_set(phydev, index, (value != LED_OFF));
1204 static const unsigned long supported_triggers = (BIT(TRIGGER_NETDEV_FULL_DUPLEX) |
1205 BIT(TRIGGER_NETDEV_HALF_DUPLEX) |
1206 BIT(TRIGGER_NETDEV_LINK) |
1207 BIT(TRIGGER_NETDEV_LINK_10) |
1208 BIT(TRIGGER_NETDEV_LINK_100) |
1209 BIT(TRIGGER_NETDEV_LINK_1000) |
1210 BIT(TRIGGER_NETDEV_RX) |
1211 BIT(TRIGGER_NETDEV_TX));
1213 static int mt798x_phy_led_hw_is_supported(struct phy_device *phydev, u8 index,
1214 unsigned long rules)
1219 /* All combinations of the supported triggers are allowed */
1220 if (rules & ~supported_triggers)
1226 static int mt798x_phy_led_hw_control_get(struct phy_device *phydev, u8 index,
1227 unsigned long *rules)
1229 unsigned int bit_blink = MTK_PHY_LED_STATE_FORCE_BLINK + (index ? 16 : 0);
1230 unsigned int bit_netdev = MTK_PHY_LED_STATE_NETDEV + (index ? 16 : 0);
1231 unsigned int bit_on = MTK_PHY_LED_STATE_FORCE_ON + (index ? 16 : 0);
1232 struct mtk_socphy_priv *priv = phydev->priv;
1238 on = phy_read_mmd(phydev, MDIO_MMD_VEND2,
1239 index ? MTK_PHY_LED1_ON_CTRL : MTK_PHY_LED0_ON_CTRL);
1244 blink = phy_read_mmd(phydev, MDIO_MMD_VEND2,
1245 index ? MTK_PHY_LED1_BLINK_CTRL :
1246 MTK_PHY_LED0_BLINK_CTRL);
1250 if ((on & (MTK_PHY_LED_ON_LINK1000 | MTK_PHY_LED_ON_LINK100 |
1251 MTK_PHY_LED_ON_LINK10)) ||
1252 (blink & (MTK_PHY_LED_BLINK_1000RX | MTK_PHY_LED_BLINK_100RX |
1253 MTK_PHY_LED_BLINK_10RX | MTK_PHY_LED_BLINK_1000TX |
1254 MTK_PHY_LED_BLINK_100TX | MTK_PHY_LED_BLINK_10TX)))
1255 set_bit(bit_netdev, &priv->led_state);
1257 clear_bit(bit_netdev, &priv->led_state);
1259 if (on & MTK_PHY_LED_ON_FORCE_ON)
1260 set_bit(bit_on, &priv->led_state);
1262 clear_bit(bit_on, &priv->led_state);
1264 if (blink & MTK_PHY_LED_BLINK_FORCE_BLINK)
1265 set_bit(bit_blink, &priv->led_state);
1267 clear_bit(bit_blink, &priv->led_state);
1272 if (on & (MTK_PHY_LED_ON_LINK1000 | MTK_PHY_LED_ON_LINK100 | MTK_PHY_LED_ON_LINK10))
1273 *rules |= BIT(TRIGGER_NETDEV_LINK);
1275 if (on & MTK_PHY_LED_ON_LINK10)
1276 *rules |= BIT(TRIGGER_NETDEV_LINK_10);
1278 if (on & MTK_PHY_LED_ON_LINK100)
1279 *rules |= BIT(TRIGGER_NETDEV_LINK_100);
1281 if (on & MTK_PHY_LED_ON_LINK1000)
1282 *rules |= BIT(TRIGGER_NETDEV_LINK_1000);
1284 if (on & MTK_PHY_LED_ON_FDX)
1285 *rules |= BIT(TRIGGER_NETDEV_FULL_DUPLEX);
1287 if (on & MTK_PHY_LED_ON_HDX)
1288 *rules |= BIT(TRIGGER_NETDEV_HALF_DUPLEX);
1290 if (blink & (MTK_PHY_LED_BLINK_1000RX | MTK_PHY_LED_BLINK_100RX | MTK_PHY_LED_BLINK_10RX))
1291 *rules |= BIT(TRIGGER_NETDEV_RX);
1293 if (blink & (MTK_PHY_LED_BLINK_1000TX | MTK_PHY_LED_BLINK_100TX | MTK_PHY_LED_BLINK_10TX))
1294 *rules |= BIT(TRIGGER_NETDEV_TX);
1299 static int mt798x_phy_led_hw_control_set(struct phy_device *phydev, u8 index,
1300 unsigned long rules)
1302 unsigned int bit_netdev = MTK_PHY_LED_STATE_NETDEV + (index ? 16 : 0);
1303 struct mtk_socphy_priv *priv = phydev->priv;
1304 u16 on = 0, blink = 0;
1310 if (rules & BIT(TRIGGER_NETDEV_FULL_DUPLEX))
1311 on |= MTK_PHY_LED_ON_FDX;
1313 if (rules & BIT(TRIGGER_NETDEV_HALF_DUPLEX))
1314 on |= MTK_PHY_LED_ON_HDX;
1316 if (rules & (BIT(TRIGGER_NETDEV_LINK_10) | BIT(TRIGGER_NETDEV_LINK)))
1317 on |= MTK_PHY_LED_ON_LINK10;
1319 if (rules & (BIT(TRIGGER_NETDEV_LINK_100) | BIT(TRIGGER_NETDEV_LINK)))
1320 on |= MTK_PHY_LED_ON_LINK100;
1322 if (rules & (BIT(TRIGGER_NETDEV_LINK_1000) | BIT(TRIGGER_NETDEV_LINK)))
1323 on |= MTK_PHY_LED_ON_LINK1000;
1325 if (rules & BIT(TRIGGER_NETDEV_RX)) {
1326 blink |= MTK_PHY_LED_BLINK_10RX |
1327 MTK_PHY_LED_BLINK_100RX |
1328 MTK_PHY_LED_BLINK_1000RX;
1331 if (rules & BIT(TRIGGER_NETDEV_TX)) {
1332 blink |= MTK_PHY_LED_BLINK_10TX |
1333 MTK_PHY_LED_BLINK_100TX |
1334 MTK_PHY_LED_BLINK_1000TX;
1338 set_bit(bit_netdev, &priv->led_state);
1340 clear_bit(bit_netdev, &priv->led_state);
1342 ret = phy_modify_mmd(phydev, MDIO_MMD_VEND2, index ?
1343 MTK_PHY_LED1_ON_CTRL :
1344 MTK_PHY_LED0_ON_CTRL,
1345 MTK_PHY_LED_ON_FDX |
1346 MTK_PHY_LED_ON_HDX |
1347 MTK_PHY_LED_ON_LINK10 |
1348 MTK_PHY_LED_ON_LINK100 |
1349 MTK_PHY_LED_ON_LINK1000,
1355 return phy_write_mmd(phydev, MDIO_MMD_VEND2, index ?
1356 MTK_PHY_LED1_BLINK_CTRL :
1357 MTK_PHY_LED0_BLINK_CTRL, blink);
1360 static bool mt7988_phy_led_get_polarity(struct phy_device *phydev, int led_num)
1362 struct mtk_socphy_shared *priv = phydev->shared->priv;
1366 polarities = ~(priv->boottrap);
1368 polarities = MTK_PHY_LED1_DEFAULT_POLARITIES;
1370 if (polarities & BIT(phydev->mdio.addr))
1376 static int mt7988_phy_fix_leds_polarities(struct phy_device *phydev)
1378 struct pinctrl *pinctrl;
1381 /* Setup LED polarity according to bootstrap use of LED pins */
1382 for (index = 0; index < 2; ++index)
1383 phy_modify_mmd(phydev, MDIO_MMD_VEND2, index ?
1384 MTK_PHY_LED1_ON_CTRL : MTK_PHY_LED0_ON_CTRL,
1385 MTK_PHY_LED_ON_POLARITY,
1386 mt7988_phy_led_get_polarity(phydev, index) ?
1387 MTK_PHY_LED_ON_POLARITY : 0);
1389 /* Only now setup pinctrl to avoid bogus blinking */
1390 pinctrl = devm_pinctrl_get_select(&phydev->mdio.dev, "gbe-led");
1391 if (IS_ERR(pinctrl))
1392 dev_err(&phydev->mdio.bus->dev, "Failed to setup PHY LED pinctrl\n");
1397 static int mt7988_phy_probe_shared(struct phy_device *phydev)
1399 struct device_node *np = dev_of_node(&phydev->mdio.bus->dev);
1400 struct mtk_socphy_shared *shared = phydev->shared->priv;
1401 struct regmap *regmap;
1405 /* The LED0 of the 4 PHYs in MT7988 are wired to SoC pins LED_A, LED_B,
1406 * LED_C and LED_D respectively. At the same time those pins are used to
1407 * bootstrap configuration of the reference clock source (LED_A),
1408 * DRAM DDRx16b x2/x1 (LED_B) and boot device (LED_C, LED_D).
1409 * In practise this is done using a LED and a resistor pulling the pin
1410 * either to GND or to VIO.
1411 * The detected value at boot time is accessible at run-time using the
1412 * TPBANK0 register located in the gpio base of the pinctrl, in order
1413 * to read it here it needs to be referenced by a phandle called
1414 * 'mediatek,pio' in the MDIO bus hosting the PHY.
1415 * The 4 bits in TPBANK0 are kept as package shared data and are used to
1416 * set LED polarity for each of the LED0.
1418 regmap = syscon_regmap_lookup_by_phandle(np, "mediatek,pio");
1420 return PTR_ERR(regmap);
1422 ret = regmap_read(regmap, RG_GPIO_MISC_TPBANK0, ®);
1426 shared->boottrap = FIELD_GET(RG_GPIO_MISC_TPBANK0_BOOTMODE, reg);
1431 static void mt798x_phy_leds_state_init(struct phy_device *phydev)
1435 for (i = 0; i < 2; ++i)
1436 mt798x_phy_led_hw_control_get(phydev, i, NULL);
1439 static int mt7988_phy_probe(struct phy_device *phydev)
1441 struct mtk_socphy_shared *shared;
1442 struct mtk_socphy_priv *priv;
1445 if (phydev->mdio.addr > 3)
1448 err = devm_phy_package_join(&phydev->mdio.dev, phydev, 0,
1449 sizeof(struct mtk_socphy_shared));
1453 if (phy_package_probe_once(phydev)) {
1454 err = mt7988_phy_probe_shared(phydev);
1459 shared = phydev->shared->priv;
1460 priv = &shared->priv[phydev->mdio.addr];
1462 phydev->priv = priv;
1464 mt798x_phy_leds_state_init(phydev);
1466 err = mt7988_phy_fix_leds_polarities(phydev);
1470 /* Disable TX power saving at probing to:
1471 * 1. Meet common mode compliance test criteria
1472 * 2. Make sure that TX-VCM calibration works fine
1474 phy_modify_mmd(phydev, MDIO_MMD_VEND1, MTK_PHY_RXADC_CTRL_RG7,
1475 MTK_PHY_DA_AD_BUF_BIAS_LP_MASK, 0x3 << 8);
1477 return mt798x_phy_calibration(phydev);
1480 static int mt7981_phy_probe(struct phy_device *phydev)
1482 struct mtk_socphy_priv *priv;
1484 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(struct mtk_socphy_priv),
1489 phydev->priv = priv;
1491 mt798x_phy_leds_state_init(phydev);
1493 return mt798x_phy_calibration(phydev);
1496 static struct phy_driver mtk_socphy_driver[] = {
1498 PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7981),
1499 .name = "MediaTek MT7981 PHY",
1500 .config_init = mt798x_phy_config_init,
1501 .config_intr = genphy_no_config_intr,
1502 .handle_interrupt = genphy_handle_interrupt_no_ack,
1503 .probe = mt7981_phy_probe,
1504 .suspend = genphy_suspend,
1505 .resume = genphy_resume,
1506 .read_page = mtk_socphy_read_page,
1507 .write_page = mtk_socphy_write_page,
1508 .led_blink_set = mt798x_phy_led_blink_set,
1509 .led_brightness_set = mt798x_phy_led_brightness_set,
1510 .led_hw_is_supported = mt798x_phy_led_hw_is_supported,
1511 .led_hw_control_set = mt798x_phy_led_hw_control_set,
1512 .led_hw_control_get = mt798x_phy_led_hw_control_get,
1515 PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7988),
1516 .name = "MediaTek MT7988 PHY",
1517 .config_init = mt798x_phy_config_init,
1518 .config_intr = genphy_no_config_intr,
1519 .handle_interrupt = genphy_handle_interrupt_no_ack,
1520 .probe = mt7988_phy_probe,
1521 .suspend = genphy_suspend,
1522 .resume = genphy_resume,
1523 .read_page = mtk_socphy_read_page,
1524 .write_page = mtk_socphy_write_page,
1525 .led_blink_set = mt798x_phy_led_blink_set,
1526 .led_brightness_set = mt798x_phy_led_brightness_set,
1527 .led_hw_is_supported = mt798x_phy_led_hw_is_supported,
1528 .led_hw_control_set = mt798x_phy_led_hw_control_set,
1529 .led_hw_control_get = mt798x_phy_led_hw_control_get,
1533 module_phy_driver(mtk_socphy_driver);
1535 static struct mdio_device_id __maybe_unused mtk_socphy_tbl[] = {
1536 { PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7981) },
1537 { PHY_ID_MATCH_EXACT(MTK_GPHY_ID_MT7988) },
1541 MODULE_DESCRIPTION("MediaTek SoC Gigabit Ethernet PHY driver");
1542 MODULE_AUTHOR("Daniel Golle <daniel@makrotopia.org>");
1543 MODULE_AUTHOR("SkyLake Huang <SkyLake.Huang@mediatek.com>");
1544 MODULE_LICENSE("GPL");
1546 MODULE_DEVICE_TABLE(mdio, mtk_socphy_tbl);