Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / phy / motorcomm.c
blob0e91f5d1a4fdbd43c0fe1ae98519d47593d2bfb1
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Motorcomm 8511/8521/8531/8531S/8821 PHY driver.
5 * Author: Peter Geis <pgwipeout@gmail.com>
6 * Author: Frank <Frank.Sae@motor-comm.com>
7 */
9 #include <linux/etherdevice.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/phy.h>
13 #include <linux/of.h>
15 #define PHY_ID_YT8511 0x0000010a
16 #define PHY_ID_YT8521 0x0000011a
17 #define PHY_ID_YT8531 0x4f51e91b
18 #define PHY_ID_YT8531S 0x4f51e91a
19 #define PHY_ID_YT8821 0x4f51ea19
20 /* YT8521/YT8531S/YT8821 Register Overview
21 * UTP Register space | FIBER Register space
22 * ------------------------------------------------------------
23 * | UTP MII | FIBER MII |
24 * | UTP MMD | |
25 * | UTP Extended | FIBER Extended |
26 * ------------------------------------------------------------
27 * | Common Extended |
28 * ------------------------------------------------------------
31 /* 0x10 ~ 0x15 , 0x1E and 0x1F are common MII registers of yt phy */
33 /* Specific Function Control Register */
34 #define YTPHY_SPECIFIC_FUNCTION_CONTROL_REG 0x10
36 /* 2b00 Manual MDI configuration
37 * 2b01 Manual MDIX configuration
38 * 2b10 Reserved
39 * 2b11 Enable automatic crossover for all modes *default*
41 #define YTPHY_SFCR_MDI_CROSSOVER_MODE_MASK (BIT(6) | BIT(5))
42 #define YTPHY_SFCR_CROSSOVER_EN BIT(3)
43 #define YTPHY_SFCR_SQE_TEST_EN BIT(2)
44 #define YTPHY_SFCR_POLARITY_REVERSAL_EN BIT(1)
45 #define YTPHY_SFCR_JABBER_DIS BIT(0)
47 /* Specific Status Register */
48 #define YTPHY_SPECIFIC_STATUS_REG 0x11
49 #define YTPHY_SSR_SPEED_MASK ((0x3 << 14) | BIT(9))
50 #define YTPHY_SSR_SPEED_10M ((0x0 << 14))
51 #define YTPHY_SSR_SPEED_100M ((0x1 << 14))
52 #define YTPHY_SSR_SPEED_1000M ((0x2 << 14))
53 #define YTPHY_SSR_SPEED_10G ((0x3 << 14))
54 #define YTPHY_SSR_SPEED_2500M ((0x0 << 14) | BIT(9))
55 #define YTPHY_SSR_DUPLEX_OFFSET 13
56 #define YTPHY_SSR_DUPLEX BIT(13)
57 #define YTPHY_SSR_PAGE_RECEIVED BIT(12)
58 #define YTPHY_SSR_SPEED_DUPLEX_RESOLVED BIT(11)
59 #define YTPHY_SSR_LINK BIT(10)
60 #define YTPHY_SSR_MDIX_CROSSOVER BIT(6)
61 #define YTPHY_SSR_DOWNGRADE BIT(5)
62 #define YTPHY_SSR_TRANSMIT_PAUSE BIT(3)
63 #define YTPHY_SSR_RECEIVE_PAUSE BIT(2)
64 #define YTPHY_SSR_POLARITY BIT(1)
65 #define YTPHY_SSR_JABBER BIT(0)
67 /* Interrupt enable Register */
68 #define YTPHY_INTERRUPT_ENABLE_REG 0x12
69 #define YTPHY_IER_WOL BIT(6)
71 /* Interrupt Status Register */
72 #define YTPHY_INTERRUPT_STATUS_REG 0x13
73 #define YTPHY_ISR_AUTONEG_ERR BIT(15)
74 #define YTPHY_ISR_SPEED_CHANGED BIT(14)
75 #define YTPHY_ISR_DUPLEX_CHANGED BIT(13)
76 #define YTPHY_ISR_PAGE_RECEIVED BIT(12)
77 #define YTPHY_ISR_LINK_FAILED BIT(11)
78 #define YTPHY_ISR_LINK_SUCCESSED BIT(10)
79 #define YTPHY_ISR_WOL BIT(6)
80 #define YTPHY_ISR_WIRESPEED_DOWNGRADE BIT(5)
81 #define YTPHY_ISR_SERDES_LINK_FAILED BIT(3)
82 #define YTPHY_ISR_SERDES_LINK_SUCCESSED BIT(2)
83 #define YTPHY_ISR_POLARITY_CHANGED BIT(1)
84 #define YTPHY_ISR_JABBER_HAPPENED BIT(0)
86 /* Speed Auto Downgrade Control Register */
87 #define YTPHY_SPEED_AUTO_DOWNGRADE_CONTROL_REG 0x14
88 #define YTPHY_SADCR_SPEED_DOWNGRADE_EN BIT(5)
90 /* If these bits are set to 3, the PHY attempts five times ( 3(set value) +
91 * additional 2) before downgrading, default 0x3
93 #define YTPHY_SADCR_SPEED_RETRY_LIMIT (0x3 << 2)
95 /* Rx Error Counter Register */
96 #define YTPHY_RX_ERROR_COUNTER_REG 0x15
98 /* Extended Register's Address Offset Register */
99 #define YTPHY_PAGE_SELECT 0x1E
101 /* Extended Register's Data Register */
102 #define YTPHY_PAGE_DATA 0x1F
104 /* FIBER Auto-Negotiation link partner ability */
105 #define YTPHY_FLPA_PAUSE (0x3 << 7)
106 #define YTPHY_FLPA_ASYM_PAUSE (0x2 << 7)
108 #define YT8511_PAGE_SELECT 0x1e
109 #define YT8511_PAGE 0x1f
110 #define YT8511_EXT_CLK_GATE 0x0c
111 #define YT8511_EXT_DELAY_DRIVE 0x0d
112 #define YT8511_EXT_SLEEP_CTRL 0x27
114 /* 2b00 25m from pll
115 * 2b01 25m from xtl *default*
116 * 2b10 62.m from pll
117 * 2b11 125m from pll
119 #define YT8511_CLK_125M (BIT(2) | BIT(1))
120 #define YT8511_PLLON_SLP BIT(14)
122 /* RX Delay enabled = 1.8ns 1000T, 8ns 10/100T */
123 #define YT8511_DELAY_RX BIT(0)
125 /* TX Gig-E Delay is bits 7:4, default 0x5
126 * TX Fast-E Delay is bits 15:12, default 0xf
127 * Delay = 150ps * N - 250ps
128 * On = 2000ps, off = 50ps
130 #define YT8511_DELAY_GE_TX_EN (0xf << 4)
131 #define YT8511_DELAY_GE_TX_DIS (0x2 << 4)
132 #define YT8511_DELAY_FE_TX_EN (0xf << 12)
133 #define YT8511_DELAY_FE_TX_DIS (0x2 << 12)
135 /* Extended register is different from MMD Register and MII Register.
136 * We can use ytphy_read_ext/ytphy_write_ext/ytphy_modify_ext function to
137 * operate extended register.
138 * Extended Register start
141 /* Phy gmii clock gating Register */
142 #define YT8521_CLOCK_GATING_REG 0xC
143 #define YT8521_CGR_RX_CLK_EN BIT(12)
145 #define YT8521_EXTREG_SLEEP_CONTROL1_REG 0x27
146 #define YT8521_ESC1R_SLEEP_SW BIT(15)
147 #define YT8521_ESC1R_PLLON_SLP BIT(14)
149 /* Phy fiber Link timer cfg2 Register */
150 #define YT8521_LINK_TIMER_CFG2_REG 0xA5
151 #define YT8521_LTCR_EN_AUTOSEN BIT(15)
153 /* 0xA000, 0xA001, 0xA003, 0xA006 ~ 0xA00A and 0xA012 are common ext registers
154 * of yt8521 phy. There is no need to switch reg space when operating these
155 * registers.
158 #define YT8521_REG_SPACE_SELECT_REG 0xA000
159 #define YT8521_RSSR_SPACE_MASK BIT(1)
160 #define YT8521_RSSR_FIBER_SPACE (0x1 << 1)
161 #define YT8521_RSSR_UTP_SPACE (0x0 << 1)
162 #define YT8521_RSSR_TO_BE_ARBITRATED (0xFF)
164 #define YT8521_CHIP_CONFIG_REG 0xA001
165 #define YT8521_CCR_SW_RST BIT(15)
166 #define YT8531_RGMII_LDO_VOL_MASK GENMASK(5, 4)
167 #define YT8531_LDO_VOL_3V3 0x0
168 #define YT8531_LDO_VOL_1V8 0x2
170 /* 1b0 disable 1.9ns rxc clock delay *default*
171 * 1b1 enable 1.9ns rxc clock delay
173 #define YT8521_CCR_RXC_DLY_EN BIT(8)
174 #define YT8521_CCR_RXC_DLY_1_900_NS 1900
176 #define YT8521_CCR_MODE_SEL_MASK (BIT(2) | BIT(1) | BIT(0))
177 #define YT8521_CCR_MODE_UTP_TO_RGMII 0
178 #define YT8521_CCR_MODE_FIBER_TO_RGMII 1
179 #define YT8521_CCR_MODE_UTP_FIBER_TO_RGMII 2
180 #define YT8521_CCR_MODE_UTP_TO_SGMII 3
181 #define YT8521_CCR_MODE_SGPHY_TO_RGMAC 4
182 #define YT8521_CCR_MODE_SGMAC_TO_RGPHY 5
183 #define YT8521_CCR_MODE_UTP_TO_FIBER_AUTO 6
184 #define YT8521_CCR_MODE_UTP_TO_FIBER_FORCE 7
186 /* 3 phy polling modes,poll mode combines utp and fiber mode*/
187 #define YT8521_MODE_FIBER 0x1
188 #define YT8521_MODE_UTP 0x2
189 #define YT8521_MODE_POLL 0x3
191 #define YT8521_RGMII_CONFIG1_REG 0xA003
192 /* 1b0 use original tx_clk_rgmii *default*
193 * 1b1 use inverted tx_clk_rgmii.
195 #define YT8521_RC1R_TX_CLK_SEL_INVERTED BIT(14)
196 #define YT8521_RC1R_RX_DELAY_MASK GENMASK(13, 10)
197 #define YT8521_RC1R_FE_TX_DELAY_MASK GENMASK(7, 4)
198 #define YT8521_RC1R_GE_TX_DELAY_MASK GENMASK(3, 0)
199 #define YT8521_RC1R_RGMII_0_000_NS 0
200 #define YT8521_RC1R_RGMII_0_150_NS 1
201 #define YT8521_RC1R_RGMII_0_300_NS 2
202 #define YT8521_RC1R_RGMII_0_450_NS 3
203 #define YT8521_RC1R_RGMII_0_600_NS 4
204 #define YT8521_RC1R_RGMII_0_750_NS 5
205 #define YT8521_RC1R_RGMII_0_900_NS 6
206 #define YT8521_RC1R_RGMII_1_050_NS 7
207 #define YT8521_RC1R_RGMII_1_200_NS 8
208 #define YT8521_RC1R_RGMII_1_350_NS 9
209 #define YT8521_RC1R_RGMII_1_500_NS 10
210 #define YT8521_RC1R_RGMII_1_650_NS 11
211 #define YT8521_RC1R_RGMII_1_800_NS 12
212 #define YT8521_RC1R_RGMII_1_950_NS 13
213 #define YT8521_RC1R_RGMII_2_100_NS 14
214 #define YT8521_RC1R_RGMII_2_250_NS 15
216 #define YTPHY_MISC_CONFIG_REG 0xA006
217 #define YTPHY_MCR_FIBER_SPEED_MASK BIT(0)
218 #define YTPHY_MCR_FIBER_1000BX (0x1 << 0)
219 #define YTPHY_MCR_FIBER_100FX (0x0 << 0)
221 /* WOL MAC ADDR: MACADDR2(highest), MACADDR1(middle), MACADDR0(lowest) */
222 #define YTPHY_WOL_MACADDR2_REG 0xA007
223 #define YTPHY_WOL_MACADDR1_REG 0xA008
224 #define YTPHY_WOL_MACADDR0_REG 0xA009
226 #define YTPHY_WOL_CONFIG_REG 0xA00A
227 #define YTPHY_WCR_INTR_SEL BIT(6)
228 #define YTPHY_WCR_ENABLE BIT(3)
230 /* 2b00 84ms
231 * 2b01 168ms *default*
232 * 2b10 336ms
233 * 2b11 672ms
235 #define YTPHY_WCR_PULSE_WIDTH_MASK (BIT(2) | BIT(1))
236 #define YTPHY_WCR_PULSE_WIDTH_672MS (BIT(2) | BIT(1))
238 /* 1b0 Interrupt and WOL events is level triggered and active LOW *default*
239 * 1b1 Interrupt and WOL events is pulse triggered and active LOW
241 #define YTPHY_WCR_TYPE_PULSE BIT(0)
243 #define YTPHY_PAD_DRIVE_STRENGTH_REG 0xA010
244 #define YT8531_RGMII_RXC_DS_MASK GENMASK(15, 13)
245 #define YT8531_RGMII_RXD_DS_HI_MASK BIT(12) /* Bit 2 of rxd_ds */
246 #define YT8531_RGMII_RXD_DS_LOW_MASK GENMASK(5, 4) /* Bit 1/0 of rxd_ds */
247 #define YT8531_RGMII_RX_DS_DEFAULT 0x3
249 #define YTPHY_SYNCE_CFG_REG 0xA012
250 #define YT8521_SCR_SYNCE_ENABLE BIT(5)
251 /* 1b0 output 25m clock
252 * 1b1 output 125m clock *default*
254 #define YT8521_SCR_CLK_FRE_SEL_125M BIT(3)
255 #define YT8521_SCR_CLK_SRC_MASK GENMASK(2, 1)
256 #define YT8521_SCR_CLK_SRC_PLL_125M 0
257 #define YT8521_SCR_CLK_SRC_UTP_RX 1
258 #define YT8521_SCR_CLK_SRC_SDS_RX 2
259 #define YT8521_SCR_CLK_SRC_REF_25M 3
260 #define YT8531_SCR_SYNCE_ENABLE BIT(6)
261 /* 1b0 output 25m clock *default*
262 * 1b1 output 125m clock
264 #define YT8531_SCR_CLK_FRE_SEL_125M BIT(4)
265 #define YT8531_SCR_CLK_SRC_MASK GENMASK(3, 1)
266 #define YT8531_SCR_CLK_SRC_PLL_125M 0
267 #define YT8531_SCR_CLK_SRC_UTP_RX 1
268 #define YT8531_SCR_CLK_SRC_SDS_RX 2
269 #define YT8531_SCR_CLK_SRC_CLOCK_FROM_DIGITAL 3
270 #define YT8531_SCR_CLK_SRC_REF_25M 4
271 #define YT8531_SCR_CLK_SRC_SSC_25M 5
273 #define YT8821_SDS_EXT_CSR_CTRL_REG 0x23
274 #define YT8821_SDS_EXT_CSR_VCO_LDO_EN BIT(15)
275 #define YT8821_SDS_EXT_CSR_VCO_BIAS_LPF_EN BIT(8)
277 #define YT8821_UTP_EXT_PI_CTRL_REG 0x56
278 #define YT8821_UTP_EXT_PI_RST_N_FIFO BIT(5)
279 #define YT8821_UTP_EXT_PI_TX_CLK_SEL_AFE BIT(4)
280 #define YT8821_UTP_EXT_PI_RX_CLK_3_SEL_AFE BIT(3)
281 #define YT8821_UTP_EXT_PI_RX_CLK_2_SEL_AFE BIT(2)
282 #define YT8821_UTP_EXT_PI_RX_CLK_1_SEL_AFE BIT(1)
283 #define YT8821_UTP_EXT_PI_RX_CLK_0_SEL_AFE BIT(0)
285 #define YT8821_UTP_EXT_VCT_CFG6_CTRL_REG 0x97
286 #define YT8821_UTP_EXT_FECHO_AMP_TH_HUGE GENMASK(15, 8)
288 #define YT8821_UTP_EXT_ECHO_CTRL_REG 0x336
289 #define YT8821_UTP_EXT_TRACE_LNG_GAIN_THR_1000 GENMASK(14, 8)
291 #define YT8821_UTP_EXT_GAIN_CTRL_REG 0x340
292 #define YT8821_UTP_EXT_TRACE_MED_GAIN_THR_1000 GENMASK(6, 0)
294 #define YT8821_UTP_EXT_RPDN_CTRL_REG 0x34E
295 #define YT8821_UTP_EXT_RPDN_BP_FFE_LNG_2500 BIT(15)
296 #define YT8821_UTP_EXT_RPDN_BP_FFE_SHT_2500 BIT(7)
297 #define YT8821_UTP_EXT_RPDN_IPR_SHT_2500 GENMASK(6, 0)
299 #define YT8821_UTP_EXT_TH_20DB_2500_CTRL_REG 0x36A
300 #define YT8821_UTP_EXT_TH_20DB_2500 GENMASK(15, 0)
302 #define YT8821_UTP_EXT_TRACE_CTRL_REG 0x372
303 #define YT8821_UTP_EXT_TRACE_LNG_GAIN_THE_2500 GENMASK(14, 8)
304 #define YT8821_UTP_EXT_TRACE_MED_GAIN_THE_2500 GENMASK(6, 0)
306 #define YT8821_UTP_EXT_ALPHA_IPR_CTRL_REG 0x374
307 #define YT8821_UTP_EXT_ALPHA_SHT_2500 GENMASK(14, 8)
308 #define YT8821_UTP_EXT_IPR_LNG_2500 GENMASK(6, 0)
310 #define YT8821_UTP_EXT_PLL_CTRL_REG 0x450
311 #define YT8821_UTP_EXT_PLL_SPARE_CFG GENMASK(7, 0)
313 #define YT8821_UTP_EXT_DAC_IMID_CH_2_3_CTRL_REG 0x466
314 #define YT8821_UTP_EXT_DAC_IMID_CH_3_10_ORG GENMASK(14, 8)
315 #define YT8821_UTP_EXT_DAC_IMID_CH_2_10_ORG GENMASK(6, 0)
317 #define YT8821_UTP_EXT_DAC_IMID_CH_0_1_CTRL_REG 0x467
318 #define YT8821_UTP_EXT_DAC_IMID_CH_1_10_ORG GENMASK(14, 8)
319 #define YT8821_UTP_EXT_DAC_IMID_CH_0_10_ORG GENMASK(6, 0)
321 #define YT8821_UTP_EXT_DAC_IMSB_CH_2_3_CTRL_REG 0x468
322 #define YT8821_UTP_EXT_DAC_IMSB_CH_3_10_ORG GENMASK(14, 8)
323 #define YT8821_UTP_EXT_DAC_IMSB_CH_2_10_ORG GENMASK(6, 0)
325 #define YT8821_UTP_EXT_DAC_IMSB_CH_0_1_CTRL_REG 0x469
326 #define YT8821_UTP_EXT_DAC_IMSB_CH_1_10_ORG GENMASK(14, 8)
327 #define YT8821_UTP_EXT_DAC_IMSB_CH_0_10_ORG GENMASK(6, 0)
329 #define YT8821_UTP_EXT_MU_COARSE_FR_CTRL_REG 0x4B3
330 #define YT8821_UTP_EXT_MU_COARSE_FR_F_FFE GENMASK(14, 12)
331 #define YT8821_UTP_EXT_MU_COARSE_FR_F_FBE GENMASK(10, 8)
333 #define YT8821_UTP_EXT_MU_FINE_FR_CTRL_REG 0x4B5
334 #define YT8821_UTP_EXT_MU_FINE_FR_F_FFE GENMASK(14, 12)
335 #define YT8821_UTP_EXT_MU_FINE_FR_F_FBE GENMASK(10, 8)
337 #define YT8821_UTP_EXT_VGA_LPF1_CAP_CTRL_REG 0x4D2
338 #define YT8821_UTP_EXT_VGA_LPF1_CAP_OTHER GENMASK(7, 4)
339 #define YT8821_UTP_EXT_VGA_LPF1_CAP_2500 GENMASK(3, 0)
341 #define YT8821_UTP_EXT_VGA_LPF2_CAP_CTRL_REG 0x4D3
342 #define YT8821_UTP_EXT_VGA_LPF2_CAP_OTHER GENMASK(7, 4)
343 #define YT8821_UTP_EXT_VGA_LPF2_CAP_2500 GENMASK(3, 0)
345 #define YT8821_UTP_EXT_TXGE_NFR_FR_THP_CTRL_REG 0x660
346 #define YT8821_UTP_EXT_NFR_TX_ABILITY BIT(3)
347 /* Extended Register end */
349 #define YTPHY_DTS_OUTPUT_CLK_DIS 0
350 #define YTPHY_DTS_OUTPUT_CLK_25M 25000000
351 #define YTPHY_DTS_OUTPUT_CLK_125M 125000000
353 #define YT8821_CHIP_MODE_AUTO_BX2500_SGMII 0
354 #define YT8821_CHIP_MODE_FORCE_BX2500 1
356 struct yt8521_priv {
357 /* combo_advertising is used for case of YT8521 in combo mode,
358 * this means that yt8521 may work in utp or fiber mode which depends
359 * on which media is connected (YT8521_RSSR_TO_BE_ARBITRATED).
361 __ETHTOOL_DECLARE_LINK_MODE_MASK(combo_advertising);
363 /* YT8521_MODE_FIBER / YT8521_MODE_UTP / YT8521_MODE_POLL*/
364 u8 polling_mode;
365 u8 strap_mode; /* 8 working modes */
366 /* current reg page of yt8521 phy:
367 * YT8521_RSSR_UTP_SPACE
368 * YT8521_RSSR_FIBER_SPACE
369 * YT8521_RSSR_TO_BE_ARBITRATED
371 u8 reg_page;
375 * ytphy_read_ext() - read a PHY's extended register
376 * @phydev: a pointer to a &struct phy_device
377 * @regnum: register number to read
379 * NOTE:The caller must have taken the MDIO bus lock.
381 * returns the value of regnum reg or negative error code
383 static int ytphy_read_ext(struct phy_device *phydev, u16 regnum)
385 int ret;
387 ret = __phy_write(phydev, YTPHY_PAGE_SELECT, regnum);
388 if (ret < 0)
389 return ret;
391 return __phy_read(phydev, YTPHY_PAGE_DATA);
395 * ytphy_read_ext_with_lock() - read a PHY's extended register
396 * @phydev: a pointer to a &struct phy_device
397 * @regnum: register number to read
399 * returns the value of regnum reg or negative error code
401 static int ytphy_read_ext_with_lock(struct phy_device *phydev, u16 regnum)
403 int ret;
405 phy_lock_mdio_bus(phydev);
406 ret = ytphy_read_ext(phydev, regnum);
407 phy_unlock_mdio_bus(phydev);
409 return ret;
413 * ytphy_write_ext() - write a PHY's extended register
414 * @phydev: a pointer to a &struct phy_device
415 * @regnum: register number to write
416 * @val: value to write to @regnum
418 * NOTE:The caller must have taken the MDIO bus lock.
420 * returns 0 or negative error code
422 static int ytphy_write_ext(struct phy_device *phydev, u16 regnum, u16 val)
424 int ret;
426 ret = __phy_write(phydev, YTPHY_PAGE_SELECT, regnum);
427 if (ret < 0)
428 return ret;
430 return __phy_write(phydev, YTPHY_PAGE_DATA, val);
434 * ytphy_write_ext_with_lock() - write a PHY's extended register
435 * @phydev: a pointer to a &struct phy_device
436 * @regnum: register number to write
437 * @val: value to write to @regnum
439 * returns 0 or negative error code
441 static int ytphy_write_ext_with_lock(struct phy_device *phydev, u16 regnum,
442 u16 val)
444 int ret;
446 phy_lock_mdio_bus(phydev);
447 ret = ytphy_write_ext(phydev, regnum, val);
448 phy_unlock_mdio_bus(phydev);
450 return ret;
454 * ytphy_modify_ext() - bits modify a PHY's extended register
455 * @phydev: a pointer to a &struct phy_device
456 * @regnum: register number to write
457 * @mask: bit mask of bits to clear
458 * @set: bit mask of bits to set
460 * NOTE: Convenience function which allows a PHY's extended register to be
461 * modified as new register value = (old register value & ~mask) | set.
462 * The caller must have taken the MDIO bus lock.
464 * returns 0 or negative error code
466 static int ytphy_modify_ext(struct phy_device *phydev, u16 regnum, u16 mask,
467 u16 set)
469 int ret;
471 ret = __phy_write(phydev, YTPHY_PAGE_SELECT, regnum);
472 if (ret < 0)
473 return ret;
475 return __phy_modify(phydev, YTPHY_PAGE_DATA, mask, set);
479 * ytphy_modify_ext_with_lock() - bits modify a PHY's extended register
480 * @phydev: a pointer to a &struct phy_device
481 * @regnum: register number to write
482 * @mask: bit mask of bits to clear
483 * @set: bit mask of bits to set
485 * NOTE: Convenience function which allows a PHY's extended register to be
486 * modified as new register value = (old register value & ~mask) | set.
488 * returns 0 or negative error code
490 static int ytphy_modify_ext_with_lock(struct phy_device *phydev, u16 regnum,
491 u16 mask, u16 set)
493 int ret;
495 phy_lock_mdio_bus(phydev);
496 ret = ytphy_modify_ext(phydev, regnum, mask, set);
497 phy_unlock_mdio_bus(phydev);
499 return ret;
503 * ytphy_get_wol() - report whether wake-on-lan is enabled
504 * @phydev: a pointer to a &struct phy_device
505 * @wol: a pointer to a &struct ethtool_wolinfo
507 * NOTE: YTPHY_WOL_CONFIG_REG is common ext reg.
509 static void ytphy_get_wol(struct phy_device *phydev,
510 struct ethtool_wolinfo *wol)
512 int wol_config;
514 wol->supported = WAKE_MAGIC;
515 wol->wolopts = 0;
517 wol_config = ytphy_read_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG);
518 if (wol_config < 0)
519 return;
521 if (wol_config & YTPHY_WCR_ENABLE)
522 wol->wolopts |= WAKE_MAGIC;
526 * ytphy_set_wol() - turn wake-on-lan on or off
527 * @phydev: a pointer to a &struct phy_device
528 * @wol: a pointer to a &struct ethtool_wolinfo
530 * NOTE: YTPHY_WOL_CONFIG_REG, YTPHY_WOL_MACADDR2_REG, YTPHY_WOL_MACADDR1_REG
531 * and YTPHY_WOL_MACADDR0_REG are common ext reg. The
532 * YTPHY_INTERRUPT_ENABLE_REG of UTP is special, fiber also use this register.
534 * returns 0 or negative errno code
536 static int ytphy_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
538 struct net_device *p_attached_dev;
539 const u16 mac_addr_reg[] = {
540 YTPHY_WOL_MACADDR2_REG,
541 YTPHY_WOL_MACADDR1_REG,
542 YTPHY_WOL_MACADDR0_REG,
544 const u8 *mac_addr;
545 int old_page;
546 int ret = 0;
547 u16 mask;
548 u16 val;
549 u8 i;
551 if (wol->wolopts & WAKE_MAGIC) {
552 p_attached_dev = phydev->attached_dev;
553 if (!p_attached_dev)
554 return -ENODEV;
556 mac_addr = (const u8 *)p_attached_dev->dev_addr;
557 if (!is_valid_ether_addr(mac_addr))
558 return -EINVAL;
560 /* lock mdio bus then switch to utp reg space */
561 old_page = phy_select_page(phydev, YT8521_RSSR_UTP_SPACE);
562 if (old_page < 0)
563 goto err_restore_page;
565 /* Store the device address for the magic packet */
566 for (i = 0; i < 3; i++) {
567 ret = ytphy_write_ext(phydev, mac_addr_reg[i],
568 ((mac_addr[i * 2] << 8)) |
569 (mac_addr[i * 2 + 1]));
570 if (ret < 0)
571 goto err_restore_page;
574 /* Enable WOL feature */
575 mask = YTPHY_WCR_PULSE_WIDTH_MASK | YTPHY_WCR_INTR_SEL;
576 val = YTPHY_WCR_ENABLE | YTPHY_WCR_INTR_SEL;
577 val |= YTPHY_WCR_TYPE_PULSE | YTPHY_WCR_PULSE_WIDTH_672MS;
578 ret = ytphy_modify_ext(phydev, YTPHY_WOL_CONFIG_REG, mask, val);
579 if (ret < 0)
580 goto err_restore_page;
582 /* Enable WOL interrupt */
583 ret = __phy_modify(phydev, YTPHY_INTERRUPT_ENABLE_REG, 0,
584 YTPHY_IER_WOL);
585 if (ret < 0)
586 goto err_restore_page;
588 } else {
589 old_page = phy_select_page(phydev, YT8521_RSSR_UTP_SPACE);
590 if (old_page < 0)
591 goto err_restore_page;
593 /* Disable WOL feature */
594 mask = YTPHY_WCR_ENABLE | YTPHY_WCR_INTR_SEL;
595 ret = ytphy_modify_ext(phydev, YTPHY_WOL_CONFIG_REG, mask, 0);
597 /* Disable WOL interrupt */
598 ret = __phy_modify(phydev, YTPHY_INTERRUPT_ENABLE_REG,
599 YTPHY_IER_WOL, 0);
600 if (ret < 0)
601 goto err_restore_page;
604 err_restore_page:
605 return phy_restore_page(phydev, old_page, ret);
608 static int yt8531_set_wol(struct phy_device *phydev,
609 struct ethtool_wolinfo *wol)
611 const u16 mac_addr_reg[] = {
612 YTPHY_WOL_MACADDR2_REG,
613 YTPHY_WOL_MACADDR1_REG,
614 YTPHY_WOL_MACADDR0_REG,
616 const u8 *mac_addr;
617 u16 mask, val;
618 int ret;
619 u8 i;
621 if (wol->wolopts & WAKE_MAGIC) {
622 mac_addr = phydev->attached_dev->dev_addr;
624 /* Store the device address for the magic packet */
625 for (i = 0; i < 3; i++) {
626 ret = ytphy_write_ext_with_lock(phydev, mac_addr_reg[i],
627 ((mac_addr[i * 2] << 8)) |
628 (mac_addr[i * 2 + 1]));
629 if (ret < 0)
630 return ret;
633 /* Enable WOL feature */
634 mask = YTPHY_WCR_PULSE_WIDTH_MASK | YTPHY_WCR_INTR_SEL;
635 val = YTPHY_WCR_ENABLE | YTPHY_WCR_INTR_SEL;
636 val |= YTPHY_WCR_TYPE_PULSE | YTPHY_WCR_PULSE_WIDTH_672MS;
637 ret = ytphy_modify_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG,
638 mask, val);
639 if (ret < 0)
640 return ret;
642 /* Enable WOL interrupt */
643 ret = phy_modify(phydev, YTPHY_INTERRUPT_ENABLE_REG, 0,
644 YTPHY_IER_WOL);
645 if (ret < 0)
646 return ret;
647 } else {
648 /* Disable WOL feature */
649 mask = YTPHY_WCR_ENABLE | YTPHY_WCR_INTR_SEL;
650 ret = ytphy_modify_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG,
651 mask, 0);
653 /* Disable WOL interrupt */
654 ret = phy_modify(phydev, YTPHY_INTERRUPT_ENABLE_REG,
655 YTPHY_IER_WOL, 0);
656 if (ret < 0)
657 return ret;
660 return 0;
663 static int yt8511_read_page(struct phy_device *phydev)
665 return __phy_read(phydev, YT8511_PAGE_SELECT);
668 static int yt8511_write_page(struct phy_device *phydev, int page)
670 return __phy_write(phydev, YT8511_PAGE_SELECT, page);
673 static int yt8511_config_init(struct phy_device *phydev)
675 int oldpage, ret = 0;
676 unsigned int ge, fe;
678 oldpage = phy_select_page(phydev, YT8511_EXT_CLK_GATE);
679 if (oldpage < 0)
680 goto err_restore_page;
682 /* set rgmii delay mode */
683 switch (phydev->interface) {
684 case PHY_INTERFACE_MODE_RGMII:
685 ge = YT8511_DELAY_GE_TX_DIS;
686 fe = YT8511_DELAY_FE_TX_DIS;
687 break;
688 case PHY_INTERFACE_MODE_RGMII_RXID:
689 ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_DIS;
690 fe = YT8511_DELAY_FE_TX_DIS;
691 break;
692 case PHY_INTERFACE_MODE_RGMII_TXID:
693 ge = YT8511_DELAY_GE_TX_EN;
694 fe = YT8511_DELAY_FE_TX_EN;
695 break;
696 case PHY_INTERFACE_MODE_RGMII_ID:
697 ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN;
698 fe = YT8511_DELAY_FE_TX_EN;
699 break;
700 default: /* do not support other modes */
701 ret = -EOPNOTSUPP;
702 goto err_restore_page;
705 ret = __phy_modify(phydev, YT8511_PAGE, (YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN), ge);
706 if (ret < 0)
707 goto err_restore_page;
709 /* set clock mode to 125mhz */
710 ret = __phy_modify(phydev, YT8511_PAGE, 0, YT8511_CLK_125M);
711 if (ret < 0)
712 goto err_restore_page;
714 /* fast ethernet delay is in a separate page */
715 ret = __phy_write(phydev, YT8511_PAGE_SELECT, YT8511_EXT_DELAY_DRIVE);
716 if (ret < 0)
717 goto err_restore_page;
719 ret = __phy_modify(phydev, YT8511_PAGE, YT8511_DELAY_FE_TX_EN, fe);
720 if (ret < 0)
721 goto err_restore_page;
723 /* leave pll enabled in sleep */
724 ret = __phy_write(phydev, YT8511_PAGE_SELECT, YT8511_EXT_SLEEP_CTRL);
725 if (ret < 0)
726 goto err_restore_page;
728 ret = __phy_modify(phydev, YT8511_PAGE, 0, YT8511_PLLON_SLP);
729 if (ret < 0)
730 goto err_restore_page;
732 err_restore_page:
733 return phy_restore_page(phydev, oldpage, ret);
737 * yt8521_read_page() - read reg page
738 * @phydev: a pointer to a &struct phy_device
740 * returns current reg space of yt8521 (YT8521_RSSR_FIBER_SPACE/
741 * YT8521_RSSR_UTP_SPACE) or negative errno code
743 static int yt8521_read_page(struct phy_device *phydev)
745 int old_page;
747 old_page = ytphy_read_ext(phydev, YT8521_REG_SPACE_SELECT_REG);
748 if (old_page < 0)
749 return old_page;
751 if ((old_page & YT8521_RSSR_SPACE_MASK) == YT8521_RSSR_FIBER_SPACE)
752 return YT8521_RSSR_FIBER_SPACE;
754 return YT8521_RSSR_UTP_SPACE;
758 * yt8521_write_page() - write reg page
759 * @phydev: a pointer to a &struct phy_device
760 * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to write.
762 * returns 0 or negative errno code
764 static int yt8521_write_page(struct phy_device *phydev, int page)
766 int mask = YT8521_RSSR_SPACE_MASK;
767 int set;
769 if ((page & YT8521_RSSR_SPACE_MASK) == YT8521_RSSR_FIBER_SPACE)
770 set = YT8521_RSSR_FIBER_SPACE;
771 else
772 set = YT8521_RSSR_UTP_SPACE;
774 return ytphy_modify_ext(phydev, YT8521_REG_SPACE_SELECT_REG, mask, set);
778 * struct ytphy_cfg_reg_map - map a config value to a register value
779 * @cfg: value in device configuration
780 * @reg: value in the register
782 struct ytphy_cfg_reg_map {
783 u32 cfg;
784 u32 reg;
787 static const struct ytphy_cfg_reg_map ytphy_rgmii_delays[] = {
788 /* for tx delay / rx delay with YT8521_CCR_RXC_DLY_EN is not set. */
789 { 0, YT8521_RC1R_RGMII_0_000_NS },
790 { 150, YT8521_RC1R_RGMII_0_150_NS },
791 { 300, YT8521_RC1R_RGMII_0_300_NS },
792 { 450, YT8521_RC1R_RGMII_0_450_NS },
793 { 600, YT8521_RC1R_RGMII_0_600_NS },
794 { 750, YT8521_RC1R_RGMII_0_750_NS },
795 { 900, YT8521_RC1R_RGMII_0_900_NS },
796 { 1050, YT8521_RC1R_RGMII_1_050_NS },
797 { 1200, YT8521_RC1R_RGMII_1_200_NS },
798 { 1350, YT8521_RC1R_RGMII_1_350_NS },
799 { 1500, YT8521_RC1R_RGMII_1_500_NS },
800 { 1650, YT8521_RC1R_RGMII_1_650_NS },
801 { 1800, YT8521_RC1R_RGMII_1_800_NS },
802 { 1950, YT8521_RC1R_RGMII_1_950_NS }, /* default tx/rx delay */
803 { 2100, YT8521_RC1R_RGMII_2_100_NS },
804 { 2250, YT8521_RC1R_RGMII_2_250_NS },
806 /* only for rx delay with YT8521_CCR_RXC_DLY_EN is set. */
807 { 0 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_0_000_NS },
808 { 150 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_0_150_NS },
809 { 300 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_0_300_NS },
810 { 450 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_0_450_NS },
811 { 600 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_0_600_NS },
812 { 750 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_0_750_NS },
813 { 900 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_0_900_NS },
814 { 1050 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_1_050_NS },
815 { 1200 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_1_200_NS },
816 { 1350 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_1_350_NS },
817 { 1500 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_1_500_NS },
818 { 1650 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_1_650_NS },
819 { 1800 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_1_800_NS },
820 { 1950 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_1_950_NS },
821 { 2100 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_2_100_NS },
822 { 2250 + YT8521_CCR_RXC_DLY_1_900_NS, YT8521_RC1R_RGMII_2_250_NS }
825 static u32 ytphy_get_delay_reg_value(struct phy_device *phydev,
826 const char *prop_name,
827 const struct ytphy_cfg_reg_map *tbl,
828 int tb_size,
829 u16 *rxc_dly_en,
830 u32 dflt)
832 struct device_node *node = phydev->mdio.dev.of_node;
833 int tb_size_half = tb_size / 2;
834 u32 val;
835 int i;
837 if (of_property_read_u32(node, prop_name, &val))
838 goto err_dts_val;
840 /* when rxc_dly_en is NULL, it is get the delay for tx, only half of
841 * tb_size is valid.
843 if (!rxc_dly_en)
844 tb_size = tb_size_half;
846 for (i = 0; i < tb_size; i++) {
847 if (tbl[i].cfg == val) {
848 if (rxc_dly_en && i < tb_size_half)
849 *rxc_dly_en = 0;
850 return tbl[i].reg;
854 phydev_warn(phydev, "Unsupported value %d for %s using default (%u)\n",
855 val, prop_name, dflt);
857 err_dts_val:
858 /* when rxc_dly_en is not NULL, it is get the delay for rx.
859 * The rx default in dts and ytphy_rgmii_clk_delay_config is 1950 ps,
860 * so YT8521_CCR_RXC_DLY_EN should not be set.
862 if (rxc_dly_en)
863 *rxc_dly_en = 0;
865 return dflt;
868 static int ytphy_rgmii_clk_delay_config(struct phy_device *phydev)
870 int tb_size = ARRAY_SIZE(ytphy_rgmii_delays);
871 u16 rxc_dly_en = YT8521_CCR_RXC_DLY_EN;
872 u32 rx_reg, tx_reg;
873 u16 mask, val = 0;
874 int ret;
876 rx_reg = ytphy_get_delay_reg_value(phydev, "rx-internal-delay-ps",
877 ytphy_rgmii_delays, tb_size,
878 &rxc_dly_en,
879 YT8521_RC1R_RGMII_1_950_NS);
880 tx_reg = ytphy_get_delay_reg_value(phydev, "tx-internal-delay-ps",
881 ytphy_rgmii_delays, tb_size, NULL,
882 YT8521_RC1R_RGMII_1_950_NS);
884 switch (phydev->interface) {
885 case PHY_INTERFACE_MODE_RGMII:
886 rxc_dly_en = 0;
887 break;
888 case PHY_INTERFACE_MODE_RGMII_RXID:
889 val |= FIELD_PREP(YT8521_RC1R_RX_DELAY_MASK, rx_reg);
890 break;
891 case PHY_INTERFACE_MODE_RGMII_TXID:
892 rxc_dly_en = 0;
893 val |= FIELD_PREP(YT8521_RC1R_GE_TX_DELAY_MASK, tx_reg);
894 break;
895 case PHY_INTERFACE_MODE_RGMII_ID:
896 val |= FIELD_PREP(YT8521_RC1R_RX_DELAY_MASK, rx_reg) |
897 FIELD_PREP(YT8521_RC1R_GE_TX_DELAY_MASK, tx_reg);
898 break;
899 default: /* do not support other modes */
900 return -EOPNOTSUPP;
903 ret = ytphy_modify_ext(phydev, YT8521_CHIP_CONFIG_REG,
904 YT8521_CCR_RXC_DLY_EN, rxc_dly_en);
905 if (ret < 0)
906 return ret;
908 /* Generally, it is not necessary to adjust YT8521_RC1R_FE_TX_DELAY */
909 mask = YT8521_RC1R_RX_DELAY_MASK | YT8521_RC1R_GE_TX_DELAY_MASK;
910 return ytphy_modify_ext(phydev, YT8521_RGMII_CONFIG1_REG, mask, val);
913 static int ytphy_rgmii_clk_delay_config_with_lock(struct phy_device *phydev)
915 int ret;
917 phy_lock_mdio_bus(phydev);
918 ret = ytphy_rgmii_clk_delay_config(phydev);
919 phy_unlock_mdio_bus(phydev);
921 return ret;
925 * struct ytphy_ldo_vol_map - map a current value to a register value
926 * @vol: ldo voltage
927 * @ds: value in the register
928 * @cur: value in device configuration
930 struct ytphy_ldo_vol_map {
931 u32 vol;
932 u32 ds;
933 u32 cur;
936 static const struct ytphy_ldo_vol_map yt8531_ldo_vol[] = {
937 {.vol = YT8531_LDO_VOL_1V8, .ds = 0, .cur = 1200},
938 {.vol = YT8531_LDO_VOL_1V8, .ds = 1, .cur = 2100},
939 {.vol = YT8531_LDO_VOL_1V8, .ds = 2, .cur = 2700},
940 {.vol = YT8531_LDO_VOL_1V8, .ds = 3, .cur = 2910},
941 {.vol = YT8531_LDO_VOL_1V8, .ds = 4, .cur = 3110},
942 {.vol = YT8531_LDO_VOL_1V8, .ds = 5, .cur = 3600},
943 {.vol = YT8531_LDO_VOL_1V8, .ds = 6, .cur = 3970},
944 {.vol = YT8531_LDO_VOL_1V8, .ds = 7, .cur = 4350},
945 {.vol = YT8531_LDO_VOL_3V3, .ds = 0, .cur = 3070},
946 {.vol = YT8531_LDO_VOL_3V3, .ds = 1, .cur = 4080},
947 {.vol = YT8531_LDO_VOL_3V3, .ds = 2, .cur = 4370},
948 {.vol = YT8531_LDO_VOL_3V3, .ds = 3, .cur = 4680},
949 {.vol = YT8531_LDO_VOL_3V3, .ds = 4, .cur = 5020},
950 {.vol = YT8531_LDO_VOL_3V3, .ds = 5, .cur = 5450},
951 {.vol = YT8531_LDO_VOL_3V3, .ds = 6, .cur = 5740},
952 {.vol = YT8531_LDO_VOL_3V3, .ds = 7, .cur = 6140},
955 static u32 yt8531_get_ldo_vol(struct phy_device *phydev)
957 u32 val;
959 val = ytphy_read_ext_with_lock(phydev, YT8521_CHIP_CONFIG_REG);
960 val = FIELD_GET(YT8531_RGMII_LDO_VOL_MASK, val);
962 return val <= YT8531_LDO_VOL_1V8 ? val : YT8531_LDO_VOL_1V8;
965 static int yt8531_get_ds_map(struct phy_device *phydev, u32 cur)
967 u32 vol;
968 int i;
970 vol = yt8531_get_ldo_vol(phydev);
971 for (i = 0; i < ARRAY_SIZE(yt8531_ldo_vol); i++) {
972 if (yt8531_ldo_vol[i].vol == vol && yt8531_ldo_vol[i].cur == cur)
973 return yt8531_ldo_vol[i].ds;
976 return -EINVAL;
979 static int yt8531_set_ds(struct phy_device *phydev)
981 struct device_node *node = phydev->mdio.dev.of_node;
982 u32 ds_field_low, ds_field_hi, val;
983 int ret, ds;
985 /* set rgmii rx clk driver strength */
986 if (!of_property_read_u32(node, "motorcomm,rx-clk-drv-microamp", &val)) {
987 ds = yt8531_get_ds_map(phydev, val);
988 if (ds < 0)
989 return dev_err_probe(&phydev->mdio.dev, ds,
990 "No matching current value was found.\n");
991 } else {
992 ds = YT8531_RGMII_RX_DS_DEFAULT;
995 ret = ytphy_modify_ext_with_lock(phydev,
996 YTPHY_PAD_DRIVE_STRENGTH_REG,
997 YT8531_RGMII_RXC_DS_MASK,
998 FIELD_PREP(YT8531_RGMII_RXC_DS_MASK, ds));
999 if (ret < 0)
1000 return ret;
1002 /* set rgmii rx data driver strength */
1003 if (!of_property_read_u32(node, "motorcomm,rx-data-drv-microamp", &val)) {
1004 ds = yt8531_get_ds_map(phydev, val);
1005 if (ds < 0)
1006 return dev_err_probe(&phydev->mdio.dev, ds,
1007 "No matching current value was found.\n");
1008 } else {
1009 ds = YT8531_RGMII_RX_DS_DEFAULT;
1012 ds_field_hi = FIELD_GET(BIT(2), ds);
1013 ds_field_hi = FIELD_PREP(YT8531_RGMII_RXD_DS_HI_MASK, ds_field_hi);
1015 ds_field_low = FIELD_GET(GENMASK(1, 0), ds);
1016 ds_field_low = FIELD_PREP(YT8531_RGMII_RXD_DS_LOW_MASK, ds_field_low);
1018 ret = ytphy_modify_ext_with_lock(phydev,
1019 YTPHY_PAD_DRIVE_STRENGTH_REG,
1020 YT8531_RGMII_RXD_DS_LOW_MASK | YT8531_RGMII_RXD_DS_HI_MASK,
1021 ds_field_low | ds_field_hi);
1022 if (ret < 0)
1023 return ret;
1025 return 0;
1029 * yt8521_probe() - read chip config then set suitable polling_mode
1030 * @phydev: a pointer to a &struct phy_device
1032 * returns 0 or negative errno code
1034 static int yt8521_probe(struct phy_device *phydev)
1036 struct device_node *node = phydev->mdio.dev.of_node;
1037 struct device *dev = &phydev->mdio.dev;
1038 struct yt8521_priv *priv;
1039 int chip_config;
1040 u16 mask, val;
1041 u32 freq;
1042 int ret;
1044 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1045 if (!priv)
1046 return -ENOMEM;
1048 phydev->priv = priv;
1050 chip_config = ytphy_read_ext_with_lock(phydev, YT8521_CHIP_CONFIG_REG);
1051 if (chip_config < 0)
1052 return chip_config;
1054 priv->strap_mode = chip_config & YT8521_CCR_MODE_SEL_MASK;
1055 switch (priv->strap_mode) {
1056 case YT8521_CCR_MODE_FIBER_TO_RGMII:
1057 case YT8521_CCR_MODE_SGPHY_TO_RGMAC:
1058 case YT8521_CCR_MODE_SGMAC_TO_RGPHY:
1059 priv->polling_mode = YT8521_MODE_FIBER;
1060 priv->reg_page = YT8521_RSSR_FIBER_SPACE;
1061 phydev->port = PORT_FIBRE;
1062 break;
1063 case YT8521_CCR_MODE_UTP_FIBER_TO_RGMII:
1064 case YT8521_CCR_MODE_UTP_TO_FIBER_AUTO:
1065 case YT8521_CCR_MODE_UTP_TO_FIBER_FORCE:
1066 priv->polling_mode = YT8521_MODE_POLL;
1067 priv->reg_page = YT8521_RSSR_TO_BE_ARBITRATED;
1068 phydev->port = PORT_NONE;
1069 break;
1070 case YT8521_CCR_MODE_UTP_TO_SGMII:
1071 case YT8521_CCR_MODE_UTP_TO_RGMII:
1072 priv->polling_mode = YT8521_MODE_UTP;
1073 priv->reg_page = YT8521_RSSR_UTP_SPACE;
1074 phydev->port = PORT_TP;
1075 break;
1077 /* set default reg space */
1078 if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
1079 ret = ytphy_write_ext_with_lock(phydev,
1080 YT8521_REG_SPACE_SELECT_REG,
1081 priv->reg_page);
1082 if (ret < 0)
1083 return ret;
1086 if (of_property_read_u32(node, "motorcomm,clk-out-frequency-hz", &freq))
1087 freq = YTPHY_DTS_OUTPUT_CLK_DIS;
1089 if (phydev->drv->phy_id == PHY_ID_YT8521) {
1090 switch (freq) {
1091 case YTPHY_DTS_OUTPUT_CLK_DIS:
1092 mask = YT8521_SCR_SYNCE_ENABLE;
1093 val = 0;
1094 break;
1095 case YTPHY_DTS_OUTPUT_CLK_25M:
1096 mask = YT8521_SCR_SYNCE_ENABLE |
1097 YT8521_SCR_CLK_SRC_MASK |
1098 YT8521_SCR_CLK_FRE_SEL_125M;
1099 val = YT8521_SCR_SYNCE_ENABLE |
1100 FIELD_PREP(YT8521_SCR_CLK_SRC_MASK,
1101 YT8521_SCR_CLK_SRC_REF_25M);
1102 break;
1103 case YTPHY_DTS_OUTPUT_CLK_125M:
1104 mask = YT8521_SCR_SYNCE_ENABLE |
1105 YT8521_SCR_CLK_SRC_MASK |
1106 YT8521_SCR_CLK_FRE_SEL_125M;
1107 val = YT8521_SCR_SYNCE_ENABLE |
1108 YT8521_SCR_CLK_FRE_SEL_125M |
1109 FIELD_PREP(YT8521_SCR_CLK_SRC_MASK,
1110 YT8521_SCR_CLK_SRC_PLL_125M);
1111 break;
1112 default:
1113 phydev_warn(phydev, "Freq err:%u\n", freq);
1114 return -EINVAL;
1116 } else if (phydev->drv->phy_id == PHY_ID_YT8531S) {
1117 switch (freq) {
1118 case YTPHY_DTS_OUTPUT_CLK_DIS:
1119 mask = YT8531_SCR_SYNCE_ENABLE;
1120 val = 0;
1121 break;
1122 case YTPHY_DTS_OUTPUT_CLK_25M:
1123 mask = YT8531_SCR_SYNCE_ENABLE |
1124 YT8531_SCR_CLK_SRC_MASK |
1125 YT8531_SCR_CLK_FRE_SEL_125M;
1126 val = YT8531_SCR_SYNCE_ENABLE |
1127 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
1128 YT8531_SCR_CLK_SRC_REF_25M);
1129 break;
1130 case YTPHY_DTS_OUTPUT_CLK_125M:
1131 mask = YT8531_SCR_SYNCE_ENABLE |
1132 YT8531_SCR_CLK_SRC_MASK |
1133 YT8531_SCR_CLK_FRE_SEL_125M;
1134 val = YT8531_SCR_SYNCE_ENABLE |
1135 YT8531_SCR_CLK_FRE_SEL_125M |
1136 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
1137 YT8531_SCR_CLK_SRC_PLL_125M);
1138 break;
1139 default:
1140 phydev_warn(phydev, "Freq err:%u\n", freq);
1141 return -EINVAL;
1143 } else {
1144 phydev_warn(phydev, "PHY id err\n");
1145 return -EINVAL;
1148 return ytphy_modify_ext_with_lock(phydev, YTPHY_SYNCE_CFG_REG, mask,
1149 val);
1152 static int yt8531_probe(struct phy_device *phydev)
1154 struct device_node *node = phydev->mdio.dev.of_node;
1155 u16 mask, val;
1156 u32 freq;
1158 if (of_property_read_u32(node, "motorcomm,clk-out-frequency-hz", &freq))
1159 freq = YTPHY_DTS_OUTPUT_CLK_DIS;
1161 switch (freq) {
1162 case YTPHY_DTS_OUTPUT_CLK_DIS:
1163 mask = YT8531_SCR_SYNCE_ENABLE;
1164 val = 0;
1165 break;
1166 case YTPHY_DTS_OUTPUT_CLK_25M:
1167 mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
1168 YT8531_SCR_CLK_FRE_SEL_125M;
1169 val = YT8531_SCR_SYNCE_ENABLE |
1170 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
1171 YT8531_SCR_CLK_SRC_REF_25M);
1172 break;
1173 case YTPHY_DTS_OUTPUT_CLK_125M:
1174 mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
1175 YT8531_SCR_CLK_FRE_SEL_125M;
1176 val = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_FRE_SEL_125M |
1177 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
1178 YT8531_SCR_CLK_SRC_PLL_125M);
1179 break;
1180 default:
1181 phydev_warn(phydev, "Freq err:%u\n", freq);
1182 return -EINVAL;
1185 return ytphy_modify_ext_with_lock(phydev, YTPHY_SYNCE_CFG_REG, mask,
1186 val);
1190 * ytphy_utp_read_lpa() - read LPA then setup lp_advertising for utp
1191 * @phydev: a pointer to a &struct phy_device
1193 * NOTE:The caller must have taken the MDIO bus lock.
1195 * returns 0 or negative errno code
1197 static int ytphy_utp_read_lpa(struct phy_device *phydev)
1199 int lpa, lpagb;
1201 if (phydev->autoneg == AUTONEG_ENABLE) {
1202 if (!phydev->autoneg_complete) {
1203 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1205 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
1206 return 0;
1209 if (phydev->is_gigabit_capable) {
1210 lpagb = __phy_read(phydev, MII_STAT1000);
1211 if (lpagb < 0)
1212 return lpagb;
1214 if (lpagb & LPA_1000MSFAIL) {
1215 int adv = __phy_read(phydev, MII_CTRL1000);
1217 if (adv < 0)
1218 return adv;
1220 if (adv & CTL1000_ENABLE_MASTER)
1221 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1222 else
1223 phydev_err(phydev, "Master/Slave resolution failed\n");
1224 return -ENOLINK;
1227 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1228 lpagb);
1231 lpa = __phy_read(phydev, MII_LPA);
1232 if (lpa < 0)
1233 return lpa;
1235 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
1236 } else {
1237 linkmode_zero(phydev->lp_advertising);
1240 return 0;
1244 * yt8521_adjust_status() - update speed and duplex to phydev. when in fiber
1245 * mode, adjust speed and duplex.
1246 * @phydev: a pointer to a &struct phy_device
1247 * @status: yt8521 status read from YTPHY_SPECIFIC_STATUS_REG
1248 * @is_utp: false(yt8521 work in fiber mode) or true(yt8521 work in utp mode)
1250 * NOTE:The caller must have taken the MDIO bus lock.
1252 * returns 0
1254 static int yt8521_adjust_status(struct phy_device *phydev, int status,
1255 bool is_utp)
1257 int speed_mode, duplex;
1258 int speed;
1259 int err;
1260 int lpa;
1262 if (is_utp)
1263 duplex = (status & YTPHY_SSR_DUPLEX) >> YTPHY_SSR_DUPLEX_OFFSET;
1264 else
1265 duplex = DUPLEX_FULL; /* for fiber, it always DUPLEX_FULL */
1267 speed_mode = status & YTPHY_SSR_SPEED_MASK;
1269 switch (speed_mode) {
1270 case YTPHY_SSR_SPEED_10M:
1271 if (is_utp)
1272 speed = SPEED_10;
1273 else
1274 /* for fiber, it will never run here, default to
1275 * SPEED_UNKNOWN
1277 speed = SPEED_UNKNOWN;
1278 break;
1279 case YTPHY_SSR_SPEED_100M:
1280 speed = SPEED_100;
1281 break;
1282 case YTPHY_SSR_SPEED_1000M:
1283 speed = SPEED_1000;
1284 break;
1285 default:
1286 speed = SPEED_UNKNOWN;
1287 break;
1290 phydev->speed = speed;
1291 phydev->duplex = duplex;
1293 if (is_utp) {
1294 err = ytphy_utp_read_lpa(phydev);
1295 if (err < 0)
1296 return err;
1298 phy_resolve_aneg_pause(phydev);
1299 } else {
1300 lpa = __phy_read(phydev, MII_LPA);
1301 if (lpa < 0)
1302 return lpa;
1304 /* only support 1000baseX Full */
1305 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1306 phydev->lp_advertising, lpa & LPA_1000XFULL);
1308 if (!(lpa & YTPHY_FLPA_PAUSE)) {
1309 phydev->pause = 0;
1310 phydev->asym_pause = 0;
1311 } else if ((lpa & YTPHY_FLPA_ASYM_PAUSE)) {
1312 phydev->pause = 1;
1313 phydev->asym_pause = 1;
1314 } else {
1315 phydev->pause = 1;
1316 phydev->asym_pause = 0;
1320 return 0;
1324 * yt8521_read_status_paged() - determines the speed and duplex of one page
1325 * @phydev: a pointer to a &struct phy_device
1326 * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
1327 * operate.
1329 * returns 1 (utp or fiber link),0 (no link) or negative errno code
1331 static int yt8521_read_status_paged(struct phy_device *phydev, int page)
1333 int fiber_latch_val;
1334 int fiber_curr_val;
1335 int old_page;
1336 int ret = 0;
1337 int status;
1338 int link;
1340 linkmode_zero(phydev->lp_advertising);
1341 phydev->duplex = DUPLEX_UNKNOWN;
1342 phydev->speed = SPEED_UNKNOWN;
1343 phydev->asym_pause = 0;
1344 phydev->pause = 0;
1346 /* YT8521 has two reg space (utp/fiber) for linkup with utp/fiber
1347 * respectively. but for utp/fiber combo mode, reg space should be
1348 * arbitrated based on media priority. by default, utp takes
1349 * priority. reg space should be properly set before read
1350 * YTPHY_SPECIFIC_STATUS_REG.
1353 page &= YT8521_RSSR_SPACE_MASK;
1354 old_page = phy_select_page(phydev, page);
1355 if (old_page < 0)
1356 goto err_restore_page;
1358 /* Read YTPHY_SPECIFIC_STATUS_REG, which indicates the speed and duplex
1359 * of the PHY is actually using.
1361 ret = __phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
1362 if (ret < 0)
1363 goto err_restore_page;
1365 status = ret;
1366 link = !!(status & YTPHY_SSR_LINK);
1368 /* When PHY is in fiber mode, speed transferred from 1000Mbps to
1369 * 100Mbps,there is not link down from YTPHY_SPECIFIC_STATUS_REG, so
1370 * we need check MII_BMSR to identify such case.
1372 if (page == YT8521_RSSR_FIBER_SPACE) {
1373 ret = __phy_read(phydev, MII_BMSR);
1374 if (ret < 0)
1375 goto err_restore_page;
1377 fiber_latch_val = ret;
1378 ret = __phy_read(phydev, MII_BMSR);
1379 if (ret < 0)
1380 goto err_restore_page;
1382 fiber_curr_val = ret;
1383 if (link && fiber_latch_val != fiber_curr_val) {
1384 link = 0;
1385 phydev_info(phydev,
1386 "%s, fiber link down detect, latch = %04x, curr = %04x\n",
1387 __func__, fiber_latch_val, fiber_curr_val);
1389 } else {
1390 /* Read autonegotiation status */
1391 ret = __phy_read(phydev, MII_BMSR);
1392 if (ret < 0)
1393 goto err_restore_page;
1395 phydev->autoneg_complete = ret & BMSR_ANEGCOMPLETE ? 1 : 0;
1398 if (link) {
1399 if (page == YT8521_RSSR_UTP_SPACE)
1400 yt8521_adjust_status(phydev, status, true);
1401 else
1402 yt8521_adjust_status(phydev, status, false);
1404 return phy_restore_page(phydev, old_page, link);
1406 err_restore_page:
1407 return phy_restore_page(phydev, old_page, ret);
1411 * yt8521_read_status() - determines the negotiated speed and duplex
1412 * @phydev: a pointer to a &struct phy_device
1414 * returns 0 or negative errno code
1416 static int yt8521_read_status(struct phy_device *phydev)
1418 struct yt8521_priv *priv = phydev->priv;
1419 int link_fiber = 0;
1420 int link_utp;
1421 int link;
1422 int ret;
1424 if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
1425 link = yt8521_read_status_paged(phydev, priv->reg_page);
1426 if (link < 0)
1427 return link;
1428 } else {
1429 /* when page is YT8521_RSSR_TO_BE_ARBITRATED, arbitration is
1430 * needed. by default, utp is higher priority.
1433 link_utp = yt8521_read_status_paged(phydev,
1434 YT8521_RSSR_UTP_SPACE);
1435 if (link_utp < 0)
1436 return link_utp;
1438 if (!link_utp) {
1439 link_fiber = yt8521_read_status_paged(phydev,
1440 YT8521_RSSR_FIBER_SPACE);
1441 if (link_fiber < 0)
1442 return link_fiber;
1445 link = link_utp || link_fiber;
1448 if (link) {
1449 if (phydev->link == 0) {
1450 /* arbitrate reg space based on linkup media type. */
1451 if (priv->polling_mode == YT8521_MODE_POLL &&
1452 priv->reg_page == YT8521_RSSR_TO_BE_ARBITRATED) {
1453 if (link_fiber)
1454 priv->reg_page =
1455 YT8521_RSSR_FIBER_SPACE;
1456 else
1457 priv->reg_page = YT8521_RSSR_UTP_SPACE;
1459 ret = ytphy_write_ext_with_lock(phydev,
1460 YT8521_REG_SPACE_SELECT_REG,
1461 priv->reg_page);
1462 if (ret < 0)
1463 return ret;
1465 phydev->port = link_fiber ? PORT_FIBRE : PORT_TP;
1467 phydev_info(phydev, "%s, link up, media: %s\n",
1468 __func__,
1469 (phydev->port == PORT_TP) ?
1470 "UTP" : "Fiber");
1473 phydev->link = 1;
1474 } else {
1475 if (phydev->link == 1) {
1476 phydev_info(phydev, "%s, link down, media: %s\n",
1477 __func__, (phydev->port == PORT_TP) ?
1478 "UTP" : "Fiber");
1480 /* When in YT8521_MODE_POLL mode, need prepare for next
1481 * arbitration.
1483 if (priv->polling_mode == YT8521_MODE_POLL) {
1484 priv->reg_page = YT8521_RSSR_TO_BE_ARBITRATED;
1485 phydev->port = PORT_NONE;
1489 phydev->link = 0;
1492 return 0;
1496 * yt8521_modify_bmcr_paged - bits modify a PHY's BMCR register of one page
1497 * @phydev: the phy_device struct
1498 * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to operate
1499 * @mask: bit mask of bits to clear
1500 * @set: bit mask of bits to set
1502 * NOTE: Convenience function which allows a PHY's BMCR register to be
1503 * modified as new register value = (old register value & ~mask) | set.
1504 * YT8521 has two space (utp/fiber) and three mode (utp/fiber/poll), each space
1505 * has MII_BMCR. poll mode combines utp and faber,so need do both.
1506 * If it is reset, it will wait for completion.
1508 * returns 0 or negative errno code
1510 static int yt8521_modify_bmcr_paged(struct phy_device *phydev, int page,
1511 u16 mask, u16 set)
1513 int max_cnt = 500; /* the max wait time of reset ~ 500 ms */
1514 int old_page;
1515 int ret = 0;
1517 old_page = phy_select_page(phydev, page & YT8521_RSSR_SPACE_MASK);
1518 if (old_page < 0)
1519 goto err_restore_page;
1521 ret = __phy_modify(phydev, MII_BMCR, mask, set);
1522 if (ret < 0)
1523 goto err_restore_page;
1525 /* If it is reset, need to wait for the reset to complete */
1526 if (set == BMCR_RESET) {
1527 while (max_cnt--) {
1528 usleep_range(1000, 1100);
1529 ret = __phy_read(phydev, MII_BMCR);
1530 if (ret < 0)
1531 goto err_restore_page;
1533 if (!(ret & BMCR_RESET))
1534 return phy_restore_page(phydev, old_page, 0);
1538 err_restore_page:
1539 return phy_restore_page(phydev, old_page, ret);
1543 * yt8521_modify_utp_fiber_bmcr - bits modify a PHY's BMCR register
1544 * @phydev: the phy_device struct
1545 * @mask: bit mask of bits to clear
1546 * @set: bit mask of bits to set
1548 * NOTE: Convenience function which allows a PHY's BMCR register to be
1549 * modified as new register value = (old register value & ~mask) | set.
1550 * YT8521 has two space (utp/fiber) and three mode (utp/fiber/poll), each space
1551 * has MII_BMCR. poll mode combines utp and faber,so need do both.
1553 * returns 0 or negative errno code
1555 static int yt8521_modify_utp_fiber_bmcr(struct phy_device *phydev, u16 mask,
1556 u16 set)
1558 struct yt8521_priv *priv = phydev->priv;
1559 int ret;
1561 if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
1562 ret = yt8521_modify_bmcr_paged(phydev, priv->reg_page, mask,
1563 set);
1564 if (ret < 0)
1565 return ret;
1566 } else {
1567 ret = yt8521_modify_bmcr_paged(phydev, YT8521_RSSR_UTP_SPACE,
1568 mask, set);
1569 if (ret < 0)
1570 return ret;
1572 ret = yt8521_modify_bmcr_paged(phydev, YT8521_RSSR_FIBER_SPACE,
1573 mask, set);
1574 if (ret < 0)
1575 return ret;
1577 return 0;
1581 * yt8521_soft_reset() - called to issue a PHY software reset
1582 * @phydev: a pointer to a &struct phy_device
1584 * returns 0 or negative errno code
1586 static int yt8521_soft_reset(struct phy_device *phydev)
1588 return yt8521_modify_utp_fiber_bmcr(phydev, 0, BMCR_RESET);
1592 * yt8521_suspend() - suspend the hardware
1593 * @phydev: a pointer to a &struct phy_device
1595 * returns 0 or negative errno code
1597 static int yt8521_suspend(struct phy_device *phydev)
1599 int wol_config;
1601 /* YTPHY_WOL_CONFIG_REG is common ext reg */
1602 wol_config = ytphy_read_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG);
1603 if (wol_config < 0)
1604 return wol_config;
1606 /* if wol enable, do nothing */
1607 if (wol_config & YTPHY_WCR_ENABLE)
1608 return 0;
1610 return yt8521_modify_utp_fiber_bmcr(phydev, 0, BMCR_PDOWN);
1614 * yt8521_resume() - resume the hardware
1615 * @phydev: a pointer to a &struct phy_device
1617 * returns 0 or negative errno code
1619 static int yt8521_resume(struct phy_device *phydev)
1621 int ret;
1622 int wol_config;
1624 /* disable auto sleep */
1625 ret = ytphy_modify_ext_with_lock(phydev,
1626 YT8521_EXTREG_SLEEP_CONTROL1_REG,
1627 YT8521_ESC1R_SLEEP_SW, 0);
1628 if (ret < 0)
1629 return ret;
1631 wol_config = ytphy_read_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG);
1632 if (wol_config < 0)
1633 return wol_config;
1635 /* if wol enable, do nothing */
1636 if (wol_config & YTPHY_WCR_ENABLE)
1637 return 0;
1639 return yt8521_modify_utp_fiber_bmcr(phydev, BMCR_PDOWN, 0);
1643 * yt8521_config_init() - called to initialize the PHY
1644 * @phydev: a pointer to a &struct phy_device
1646 * returns 0 or negative errno code
1648 static int yt8521_config_init(struct phy_device *phydev)
1650 struct device_node *node = phydev->mdio.dev.of_node;
1651 int old_page;
1652 int ret = 0;
1654 old_page = phy_select_page(phydev, YT8521_RSSR_UTP_SPACE);
1655 if (old_page < 0)
1656 goto err_restore_page;
1658 /* set rgmii delay mode */
1659 if (phydev->interface != PHY_INTERFACE_MODE_SGMII) {
1660 ret = ytphy_rgmii_clk_delay_config(phydev);
1661 if (ret < 0)
1662 goto err_restore_page;
1665 if (of_property_read_bool(node, "motorcomm,auto-sleep-disabled")) {
1666 /* disable auto sleep */
1667 ret = ytphy_modify_ext(phydev, YT8521_EXTREG_SLEEP_CONTROL1_REG,
1668 YT8521_ESC1R_SLEEP_SW, 0);
1669 if (ret < 0)
1670 goto err_restore_page;
1673 if (of_property_read_bool(node, "motorcomm,keep-pll-enabled")) {
1674 /* enable RXC clock when no wire plug */
1675 ret = ytphy_modify_ext(phydev, YT8521_CLOCK_GATING_REG,
1676 YT8521_CGR_RX_CLK_EN, 0);
1677 if (ret < 0)
1678 goto err_restore_page;
1680 err_restore_page:
1681 return phy_restore_page(phydev, old_page, ret);
1684 static int yt8531_config_init(struct phy_device *phydev)
1686 struct device_node *node = phydev->mdio.dev.of_node;
1687 int ret;
1689 ret = ytphy_rgmii_clk_delay_config_with_lock(phydev);
1690 if (ret < 0)
1691 return ret;
1693 if (of_property_read_bool(node, "motorcomm,auto-sleep-disabled")) {
1694 /* disable auto sleep */
1695 ret = ytphy_modify_ext_with_lock(phydev,
1696 YT8521_EXTREG_SLEEP_CONTROL1_REG,
1697 YT8521_ESC1R_SLEEP_SW, 0);
1698 if (ret < 0)
1699 return ret;
1702 if (of_property_read_bool(node, "motorcomm,keep-pll-enabled")) {
1703 /* enable RXC clock when no wire plug */
1704 ret = ytphy_modify_ext_with_lock(phydev,
1705 YT8521_CLOCK_GATING_REG,
1706 YT8521_CGR_RX_CLK_EN, 0);
1707 if (ret < 0)
1708 return ret;
1711 ret = yt8531_set_ds(phydev);
1712 if (ret < 0)
1713 return ret;
1715 return 0;
1719 * yt8531_link_change_notify() - Adjust the tx clock direction according to
1720 * the current speed and dts config.
1721 * @phydev: a pointer to a &struct phy_device
1723 * NOTE: This function is only used to adapt to VF2 with JH7110 SoC. Please
1724 * keep "motorcomm,tx-clk-adj-enabled" not exist in dts when the soc is not
1725 * JH7110.
1727 static void yt8531_link_change_notify(struct phy_device *phydev)
1729 struct device_node *node = phydev->mdio.dev.of_node;
1730 bool tx_clk_1000_inverted = false;
1731 bool tx_clk_100_inverted = false;
1732 bool tx_clk_10_inverted = false;
1733 bool tx_clk_adj_enabled = false;
1734 u16 val = 0;
1735 int ret;
1737 if (of_property_read_bool(node, "motorcomm,tx-clk-adj-enabled"))
1738 tx_clk_adj_enabled = true;
1740 if (!tx_clk_adj_enabled)
1741 return;
1743 if (of_property_read_bool(node, "motorcomm,tx-clk-10-inverted"))
1744 tx_clk_10_inverted = true;
1745 if (of_property_read_bool(node, "motorcomm,tx-clk-100-inverted"))
1746 tx_clk_100_inverted = true;
1747 if (of_property_read_bool(node, "motorcomm,tx-clk-1000-inverted"))
1748 tx_clk_1000_inverted = true;
1750 if (phydev->speed < 0)
1751 return;
1753 switch (phydev->speed) {
1754 case SPEED_1000:
1755 if (tx_clk_1000_inverted)
1756 val = YT8521_RC1R_TX_CLK_SEL_INVERTED;
1757 break;
1758 case SPEED_100:
1759 if (tx_clk_100_inverted)
1760 val = YT8521_RC1R_TX_CLK_SEL_INVERTED;
1761 break;
1762 case SPEED_10:
1763 if (tx_clk_10_inverted)
1764 val = YT8521_RC1R_TX_CLK_SEL_INVERTED;
1765 break;
1766 default:
1767 return;
1770 ret = ytphy_modify_ext_with_lock(phydev, YT8521_RGMII_CONFIG1_REG,
1771 YT8521_RC1R_TX_CLK_SEL_INVERTED, val);
1772 if (ret < 0)
1773 phydev_warn(phydev, "Modify TX_CLK_SEL err:%d\n", ret);
1777 * yt8521_prepare_fiber_features() - A small helper function that setup
1778 * fiber's features.
1779 * @phydev: a pointer to a &struct phy_device
1780 * @dst: a pointer to store fiber's features
1782 static void yt8521_prepare_fiber_features(struct phy_device *phydev,
1783 unsigned long *dst)
1785 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT, dst);
1786 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, dst);
1787 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, dst);
1788 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, dst);
1792 * yt8521_fiber_setup_forced - configures/forces speed from @phydev
1793 * @phydev: target phy_device struct
1795 * NOTE:The caller must have taken the MDIO bus lock.
1797 * returns 0 or negative errno code
1799 static int yt8521_fiber_setup_forced(struct phy_device *phydev)
1801 u16 val;
1802 int ret;
1804 if (phydev->speed == SPEED_1000)
1805 val = YTPHY_MCR_FIBER_1000BX;
1806 else if (phydev->speed == SPEED_100)
1807 val = YTPHY_MCR_FIBER_100FX;
1808 else
1809 return -EINVAL;
1811 ret = __phy_modify(phydev, MII_BMCR, BMCR_ANENABLE, 0);
1812 if (ret < 0)
1813 return ret;
1815 /* disable Fiber auto sensing */
1816 ret = ytphy_modify_ext(phydev, YT8521_LINK_TIMER_CFG2_REG,
1817 YT8521_LTCR_EN_AUTOSEN, 0);
1818 if (ret < 0)
1819 return ret;
1821 ret = ytphy_modify_ext(phydev, YTPHY_MISC_CONFIG_REG,
1822 YTPHY_MCR_FIBER_SPEED_MASK, val);
1823 if (ret < 0)
1824 return ret;
1826 return ytphy_modify_ext(phydev, YT8521_CHIP_CONFIG_REG,
1827 YT8521_CCR_SW_RST, 0);
1831 * ytphy_check_and_restart_aneg - Enable and restart auto-negotiation
1832 * @phydev: target phy_device struct
1833 * @restart: whether aneg restart is requested
1835 * NOTE:The caller must have taken the MDIO bus lock.
1837 * returns 0 or negative errno code
1839 static int ytphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
1841 int ret;
1843 if (!restart) {
1844 /* Advertisement hasn't changed, but maybe aneg was never on to
1845 * begin with? Or maybe phy was isolated?
1847 ret = __phy_read(phydev, MII_BMCR);
1848 if (ret < 0)
1849 return ret;
1851 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
1852 restart = true;
1854 /* Enable and Restart Autonegotiation
1855 * Don't isolate the PHY if we're negotiating
1857 if (restart)
1858 return __phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1859 BMCR_ANENABLE | BMCR_ANRESTART);
1861 return 0;
1865 * yt8521_fiber_config_aneg - restart auto-negotiation or write
1866 * YTPHY_MISC_CONFIG_REG.
1867 * @phydev: target phy_device struct
1869 * NOTE:The caller must have taken the MDIO bus lock.
1871 * returns 0 or negative errno code
1873 static int yt8521_fiber_config_aneg(struct phy_device *phydev)
1875 int err, changed = 0;
1876 int bmcr;
1877 u16 adv;
1879 if (phydev->autoneg != AUTONEG_ENABLE)
1880 return yt8521_fiber_setup_forced(phydev);
1882 /* enable Fiber auto sensing */
1883 err = ytphy_modify_ext(phydev, YT8521_LINK_TIMER_CFG2_REG,
1884 0, YT8521_LTCR_EN_AUTOSEN);
1885 if (err < 0)
1886 return err;
1888 err = ytphy_modify_ext(phydev, YT8521_CHIP_CONFIG_REG,
1889 YT8521_CCR_SW_RST, 0);
1890 if (err < 0)
1891 return err;
1893 bmcr = __phy_read(phydev, MII_BMCR);
1894 if (bmcr < 0)
1895 return bmcr;
1897 /* When it is coming from fiber forced mode, add bmcr power down
1898 * and power up to let aneg work fine.
1900 if (!(bmcr & BMCR_ANENABLE)) {
1901 __phy_modify(phydev, MII_BMCR, 0, BMCR_PDOWN);
1902 usleep_range(1000, 1100);
1903 __phy_modify(phydev, MII_BMCR, BMCR_PDOWN, 0);
1906 adv = linkmode_adv_to_mii_adv_x(phydev->advertising,
1907 ETHTOOL_LINK_MODE_1000baseX_Full_BIT);
1909 /* Setup fiber advertisement */
1910 err = __phy_modify_changed(phydev, MII_ADVERTISE,
1911 ADVERTISE_1000XHALF | ADVERTISE_1000XFULL |
1912 ADVERTISE_1000XPAUSE |
1913 ADVERTISE_1000XPSE_ASYM,
1914 adv);
1915 if (err < 0)
1916 return err;
1918 if (err > 0)
1919 changed = 1;
1921 return ytphy_check_and_restart_aneg(phydev, changed);
1925 * ytphy_setup_master_slave
1926 * @phydev: target phy_device struct
1928 * NOTE: The caller must have taken the MDIO bus lock.
1930 * returns 0 or negative errno code
1932 static int ytphy_setup_master_slave(struct phy_device *phydev)
1934 u16 ctl = 0;
1936 if (!phydev->is_gigabit_capable)
1937 return 0;
1939 switch (phydev->master_slave_set) {
1940 case MASTER_SLAVE_CFG_MASTER_PREFERRED:
1941 ctl |= CTL1000_PREFER_MASTER;
1942 break;
1943 case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
1944 break;
1945 case MASTER_SLAVE_CFG_MASTER_FORCE:
1946 ctl |= CTL1000_AS_MASTER;
1947 fallthrough;
1948 case MASTER_SLAVE_CFG_SLAVE_FORCE:
1949 ctl |= CTL1000_ENABLE_MASTER;
1950 break;
1951 case MASTER_SLAVE_CFG_UNKNOWN:
1952 case MASTER_SLAVE_CFG_UNSUPPORTED:
1953 return 0;
1954 default:
1955 phydev_warn(phydev, "Unsupported Master/Slave mode\n");
1956 return -EOPNOTSUPP;
1959 return __phy_modify_changed(phydev, MII_CTRL1000,
1960 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
1961 CTL1000_PREFER_MASTER), ctl);
1965 * ytphy_utp_config_advert - sanitize and advertise auto-negotiation parameters
1966 * @phydev: target phy_device struct
1968 * NOTE: Writes MII_ADVERTISE with the appropriate values,
1969 * after sanitizing the values to make sure we only advertise
1970 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1971 * hasn't changed, and > 0 if it has changed.
1972 * The caller must have taken the MDIO bus lock.
1974 * returns 0 or negative errno code
1976 static int ytphy_utp_config_advert(struct phy_device *phydev)
1978 int err, bmsr, changed = 0;
1979 u32 adv;
1981 /* Only allow advertising what this PHY supports */
1982 linkmode_and(phydev->advertising, phydev->advertising,
1983 phydev->supported);
1985 adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1987 /* Setup standard advertisement */
1988 err = __phy_modify_changed(phydev, MII_ADVERTISE,
1989 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1990 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1991 adv);
1992 if (err < 0)
1993 return err;
1994 if (err > 0)
1995 changed = 1;
1997 bmsr = __phy_read(phydev, MII_BMSR);
1998 if (bmsr < 0)
1999 return bmsr;
2001 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
2002 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
2003 * logical 1.
2005 if (!(bmsr & BMSR_ESTATEN))
2006 return changed;
2008 adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
2010 err = __phy_modify_changed(phydev, MII_CTRL1000,
2011 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
2012 adv);
2013 if (err < 0)
2014 return err;
2015 if (err > 0)
2016 changed = 1;
2018 return changed;
2022 * ytphy_utp_config_aneg - restart auto-negotiation or write BMCR
2023 * @phydev: target phy_device struct
2024 * @changed: whether autoneg is requested
2026 * NOTE: If auto-negotiation is enabled, we configure the
2027 * advertising, and then restart auto-negotiation. If it is not
2028 * enabled, then we write the BMCR.
2029 * The caller must have taken the MDIO bus lock.
2031 * returns 0 or negative errno code
2033 static int ytphy_utp_config_aneg(struct phy_device *phydev, bool changed)
2035 int err;
2036 u16 ctl;
2038 err = ytphy_setup_master_slave(phydev);
2039 if (err < 0)
2040 return err;
2041 else if (err)
2042 changed = true;
2044 if (phydev->autoneg != AUTONEG_ENABLE) {
2045 /* configures/forces speed/duplex from @phydev */
2047 ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2049 return __phy_modify(phydev, MII_BMCR, ~(BMCR_LOOPBACK |
2050 BMCR_ISOLATE | BMCR_PDOWN), ctl);
2053 err = ytphy_utp_config_advert(phydev);
2054 if (err < 0) /* error */
2055 return err;
2056 else if (err)
2057 changed = true;
2059 return ytphy_check_and_restart_aneg(phydev, changed);
2063 * yt8521_config_aneg_paged() - switch reg space then call genphy_config_aneg
2064 * of one page
2065 * @phydev: a pointer to a &struct phy_device
2066 * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
2067 * operate.
2069 * returns 0 or negative errno code
2071 static int yt8521_config_aneg_paged(struct phy_device *phydev, int page)
2073 __ETHTOOL_DECLARE_LINK_MODE_MASK(fiber_supported);
2074 struct yt8521_priv *priv = phydev->priv;
2075 int old_page;
2076 int ret = 0;
2078 page &= YT8521_RSSR_SPACE_MASK;
2080 old_page = phy_select_page(phydev, page);
2081 if (old_page < 0)
2082 goto err_restore_page;
2084 /* If reg_page is YT8521_RSSR_TO_BE_ARBITRATED,
2085 * phydev->advertising should be updated.
2087 if (priv->reg_page == YT8521_RSSR_TO_BE_ARBITRATED) {
2088 linkmode_zero(fiber_supported);
2089 yt8521_prepare_fiber_features(phydev, fiber_supported);
2091 /* prepare fiber_supported, then setup advertising. */
2092 if (page == YT8521_RSSR_FIBER_SPACE) {
2093 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2094 fiber_supported);
2095 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2096 fiber_supported);
2097 linkmode_and(phydev->advertising,
2098 priv->combo_advertising, fiber_supported);
2099 } else {
2100 /* ETHTOOL_LINK_MODE_Autoneg_BIT is also used in utp */
2101 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2102 fiber_supported);
2103 linkmode_andnot(phydev->advertising,
2104 priv->combo_advertising,
2105 fiber_supported);
2109 if (page == YT8521_RSSR_FIBER_SPACE)
2110 ret = yt8521_fiber_config_aneg(phydev);
2111 else
2112 ret = ytphy_utp_config_aneg(phydev, false);
2114 err_restore_page:
2115 return phy_restore_page(phydev, old_page, ret);
2119 * yt8521_config_aneg() - change reg space then call yt8521_config_aneg_paged
2120 * @phydev: a pointer to a &struct phy_device
2122 * returns 0 or negative errno code
2124 static int yt8521_config_aneg(struct phy_device *phydev)
2126 struct yt8521_priv *priv = phydev->priv;
2127 int ret;
2129 if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
2130 ret = yt8521_config_aneg_paged(phydev, priv->reg_page);
2131 if (ret < 0)
2132 return ret;
2133 } else {
2134 /* If reg_page is YT8521_RSSR_TO_BE_ARBITRATED,
2135 * phydev->advertising need to be saved at first run.
2136 * Because it contains the advertising which supported by both
2137 * mac and yt8521(utp and fiber).
2139 if (linkmode_empty(priv->combo_advertising)) {
2140 linkmode_copy(priv->combo_advertising,
2141 phydev->advertising);
2144 ret = yt8521_config_aneg_paged(phydev, YT8521_RSSR_UTP_SPACE);
2145 if (ret < 0)
2146 return ret;
2148 ret = yt8521_config_aneg_paged(phydev, YT8521_RSSR_FIBER_SPACE);
2149 if (ret < 0)
2150 return ret;
2152 /* we don't known which will be link, so restore
2153 * phydev->advertising as default value.
2155 linkmode_copy(phydev->advertising, priv->combo_advertising);
2157 return 0;
2161 * yt8521_aneg_done_paged() - determines the auto negotiation result of one
2162 * page.
2163 * @phydev: a pointer to a &struct phy_device
2164 * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
2165 * operate.
2167 * returns 0(no link)or 1(fiber or utp link) or negative errno code
2169 static int yt8521_aneg_done_paged(struct phy_device *phydev, int page)
2171 int old_page;
2172 int ret = 0;
2173 int link;
2175 old_page = phy_select_page(phydev, page & YT8521_RSSR_SPACE_MASK);
2176 if (old_page < 0)
2177 goto err_restore_page;
2179 ret = __phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
2180 if (ret < 0)
2181 goto err_restore_page;
2183 link = !!(ret & YTPHY_SSR_LINK);
2184 ret = link;
2186 err_restore_page:
2187 return phy_restore_page(phydev, old_page, ret);
2191 * yt8521_aneg_done() - determines the auto negotiation result
2192 * @phydev: a pointer to a &struct phy_device
2194 * returns 0(no link)or 1(fiber or utp link) or negative errno code
2196 static int yt8521_aneg_done(struct phy_device *phydev)
2198 struct yt8521_priv *priv = phydev->priv;
2199 int link_fiber = 0;
2200 int link_utp;
2201 int link;
2203 if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
2204 link = yt8521_aneg_done_paged(phydev, priv->reg_page);
2205 } else {
2206 link_utp = yt8521_aneg_done_paged(phydev,
2207 YT8521_RSSR_UTP_SPACE);
2208 if (link_utp < 0)
2209 return link_utp;
2211 if (!link_utp) {
2212 link_fiber = yt8521_aneg_done_paged(phydev,
2213 YT8521_RSSR_FIBER_SPACE);
2214 if (link_fiber < 0)
2215 return link_fiber;
2217 link = link_fiber || link_utp;
2218 phydev_info(phydev, "%s, link_fiber: %d, link_utp: %d\n",
2219 __func__, link_fiber, link_utp);
2222 return link;
2226 * ytphy_utp_read_abilities - read PHY abilities from Clause 22 registers
2227 * @phydev: target phy_device struct
2229 * NOTE: Reads the PHY's abilities and populates
2230 * phydev->supported accordingly.
2231 * The caller must have taken the MDIO bus lock.
2233 * returns 0 or negative errno code
2235 static int ytphy_utp_read_abilities(struct phy_device *phydev)
2237 int val;
2239 linkmode_set_bit_array(phy_basic_ports_array,
2240 ARRAY_SIZE(phy_basic_ports_array),
2241 phydev->supported);
2243 val = __phy_read(phydev, MII_BMSR);
2244 if (val < 0)
2245 return val;
2247 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2248 val & BMSR_ANEGCAPABLE);
2250 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2251 val & BMSR_100FULL);
2252 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2253 val & BMSR_100HALF);
2254 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2255 val & BMSR_10FULL);
2256 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2257 val & BMSR_10HALF);
2259 if (val & BMSR_ESTATEN) {
2260 val = __phy_read(phydev, MII_ESTATUS);
2261 if (val < 0)
2262 return val;
2264 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2265 phydev->supported, val & ESTATUS_1000_TFULL);
2266 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2267 phydev->supported, val & ESTATUS_1000_THALF);
2268 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2269 phydev->supported, val & ESTATUS_1000_XFULL);
2272 return 0;
2276 * yt8521_get_features_paged() - read supported link modes for one page
2277 * @phydev: a pointer to a &struct phy_device
2278 * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
2279 * operate.
2281 * returns 0 or negative errno code
2283 static int yt8521_get_features_paged(struct phy_device *phydev, int page)
2285 int old_page;
2286 int ret = 0;
2288 page &= YT8521_RSSR_SPACE_MASK;
2289 old_page = phy_select_page(phydev, page);
2290 if (old_page < 0)
2291 goto err_restore_page;
2293 if (page == YT8521_RSSR_FIBER_SPACE) {
2294 linkmode_zero(phydev->supported);
2295 yt8521_prepare_fiber_features(phydev, phydev->supported);
2296 } else {
2297 ret = ytphy_utp_read_abilities(phydev);
2298 if (ret < 0)
2299 goto err_restore_page;
2302 err_restore_page:
2303 return phy_restore_page(phydev, old_page, ret);
2307 * yt8521_get_features - switch reg space then call yt8521_get_features_paged
2308 * @phydev: target phy_device struct
2310 * returns 0 or negative errno code
2312 static int yt8521_get_features(struct phy_device *phydev)
2314 struct yt8521_priv *priv = phydev->priv;
2315 int ret;
2317 if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
2318 ret = yt8521_get_features_paged(phydev, priv->reg_page);
2319 } else {
2320 ret = yt8521_get_features_paged(phydev,
2321 YT8521_RSSR_UTP_SPACE);
2322 if (ret < 0)
2323 return ret;
2325 /* add fiber's features to phydev->supported */
2326 yt8521_prepare_fiber_features(phydev, phydev->supported);
2328 return ret;
2332 * yt8821_get_features - read mmd register to get 2.5G capability
2333 * @phydev: target phy_device struct
2335 * Returns: 0 or negative errno code
2337 static int yt8821_get_features(struct phy_device *phydev)
2339 int ret;
2341 ret = genphy_c45_pma_read_ext_abilities(phydev);
2342 if (ret < 0)
2343 return ret;
2345 return genphy_read_abilities(phydev);
2349 * yt8821_get_rate_matching - read register to get phy chip mode
2350 * @phydev: target phy_device struct
2351 * @iface: PHY data interface type
2353 * Returns: rate matching type or negative errno code
2355 static int yt8821_get_rate_matching(struct phy_device *phydev,
2356 phy_interface_t iface)
2358 int val;
2360 val = ytphy_read_ext_with_lock(phydev, YT8521_CHIP_CONFIG_REG);
2361 if (val < 0)
2362 return val;
2364 if (FIELD_GET(YT8521_CCR_MODE_SEL_MASK, val) ==
2365 YT8821_CHIP_MODE_FORCE_BX2500)
2366 return RATE_MATCH_PAUSE;
2368 return RATE_MATCH_NONE;
2372 * yt8821_aneg_done() - determines the auto negotiation result
2373 * @phydev: a pointer to a &struct phy_device
2375 * Returns: 0(no link)or 1(utp link) or negative errno code
2377 static int yt8821_aneg_done(struct phy_device *phydev)
2379 return yt8521_aneg_done_paged(phydev, YT8521_RSSR_UTP_SPACE);
2383 * yt8821_serdes_init() - serdes init
2384 * @phydev: a pointer to a &struct phy_device
2386 * Returns: 0 or negative errno code
2388 static int yt8821_serdes_init(struct phy_device *phydev)
2390 int old_page;
2391 int ret = 0;
2392 u16 mask;
2393 u16 set;
2395 old_page = phy_select_page(phydev, YT8521_RSSR_FIBER_SPACE);
2396 if (old_page < 0) {
2397 phydev_err(phydev, "Failed to select page: %d\n",
2398 old_page);
2399 goto err_restore_page;
2402 ret = __phy_modify(phydev, MII_BMCR, BMCR_ANENABLE, 0);
2403 if (ret < 0)
2404 goto err_restore_page;
2406 mask = YT8821_SDS_EXT_CSR_VCO_LDO_EN |
2407 YT8821_SDS_EXT_CSR_VCO_BIAS_LPF_EN;
2408 set = YT8821_SDS_EXT_CSR_VCO_LDO_EN;
2409 ret = ytphy_modify_ext(phydev, YT8821_SDS_EXT_CSR_CTRL_REG, mask,
2410 set);
2412 err_restore_page:
2413 return phy_restore_page(phydev, old_page, ret);
2417 * yt8821_utp_init() - utp init
2418 * @phydev: a pointer to a &struct phy_device
2420 * Returns: 0 or negative errno code
2422 static int yt8821_utp_init(struct phy_device *phydev)
2424 int old_page;
2425 int ret = 0;
2426 u16 mask;
2427 u16 save;
2428 u16 set;
2430 old_page = phy_select_page(phydev, YT8521_RSSR_UTP_SPACE);
2431 if (old_page < 0) {
2432 phydev_err(phydev, "Failed to select page: %d\n",
2433 old_page);
2434 goto err_restore_page;
2437 mask = YT8821_UTP_EXT_RPDN_BP_FFE_LNG_2500 |
2438 YT8821_UTP_EXT_RPDN_BP_FFE_SHT_2500 |
2439 YT8821_UTP_EXT_RPDN_IPR_SHT_2500;
2440 set = YT8821_UTP_EXT_RPDN_BP_FFE_LNG_2500 |
2441 YT8821_UTP_EXT_RPDN_BP_FFE_SHT_2500;
2442 ret = ytphy_modify_ext(phydev, YT8821_UTP_EXT_RPDN_CTRL_REG,
2443 mask, set);
2444 if (ret < 0)
2445 goto err_restore_page;
2447 mask = YT8821_UTP_EXT_VGA_LPF1_CAP_OTHER |
2448 YT8821_UTP_EXT_VGA_LPF1_CAP_2500;
2449 ret = ytphy_modify_ext(phydev,
2450 YT8821_UTP_EXT_VGA_LPF1_CAP_CTRL_REG,
2451 mask, 0);
2452 if (ret < 0)
2453 goto err_restore_page;
2455 mask = YT8821_UTP_EXT_VGA_LPF2_CAP_OTHER |
2456 YT8821_UTP_EXT_VGA_LPF2_CAP_2500;
2457 ret = ytphy_modify_ext(phydev,
2458 YT8821_UTP_EXT_VGA_LPF2_CAP_CTRL_REG,
2459 mask, 0);
2460 if (ret < 0)
2461 goto err_restore_page;
2463 mask = YT8821_UTP_EXT_TRACE_LNG_GAIN_THE_2500 |
2464 YT8821_UTP_EXT_TRACE_MED_GAIN_THE_2500;
2465 set = FIELD_PREP(YT8821_UTP_EXT_TRACE_LNG_GAIN_THE_2500, 0x5a) |
2466 FIELD_PREP(YT8821_UTP_EXT_TRACE_MED_GAIN_THE_2500, 0x3c);
2467 ret = ytphy_modify_ext(phydev, YT8821_UTP_EXT_TRACE_CTRL_REG,
2468 mask, set);
2469 if (ret < 0)
2470 goto err_restore_page;
2472 mask = YT8821_UTP_EXT_IPR_LNG_2500;
2473 set = FIELD_PREP(YT8821_UTP_EXT_IPR_LNG_2500, 0x6c);
2474 ret = ytphy_modify_ext(phydev,
2475 YT8821_UTP_EXT_ALPHA_IPR_CTRL_REG,
2476 mask, set);
2477 if (ret < 0)
2478 goto err_restore_page;
2480 mask = YT8821_UTP_EXT_TRACE_LNG_GAIN_THR_1000;
2481 set = FIELD_PREP(YT8821_UTP_EXT_TRACE_LNG_GAIN_THR_1000, 0x2a);
2482 ret = ytphy_modify_ext(phydev, YT8821_UTP_EXT_ECHO_CTRL_REG,
2483 mask, set);
2484 if (ret < 0)
2485 goto err_restore_page;
2487 mask = YT8821_UTP_EXT_TRACE_MED_GAIN_THR_1000;
2488 set = FIELD_PREP(YT8821_UTP_EXT_TRACE_MED_GAIN_THR_1000, 0x22);
2489 ret = ytphy_modify_ext(phydev, YT8821_UTP_EXT_GAIN_CTRL_REG,
2490 mask, set);
2491 if (ret < 0)
2492 goto err_restore_page;
2494 mask = YT8821_UTP_EXT_TH_20DB_2500;
2495 set = FIELD_PREP(YT8821_UTP_EXT_TH_20DB_2500, 0x8000);
2496 ret = ytphy_modify_ext(phydev,
2497 YT8821_UTP_EXT_TH_20DB_2500_CTRL_REG,
2498 mask, set);
2499 if (ret < 0)
2500 goto err_restore_page;
2502 mask = YT8821_UTP_EXT_MU_COARSE_FR_F_FFE |
2503 YT8821_UTP_EXT_MU_COARSE_FR_F_FBE;
2504 set = FIELD_PREP(YT8821_UTP_EXT_MU_COARSE_FR_F_FFE, 0x7) |
2505 FIELD_PREP(YT8821_UTP_EXT_MU_COARSE_FR_F_FBE, 0x7);
2506 ret = ytphy_modify_ext(phydev,
2507 YT8821_UTP_EXT_MU_COARSE_FR_CTRL_REG,
2508 mask, set);
2509 if (ret < 0)
2510 goto err_restore_page;
2512 mask = YT8821_UTP_EXT_MU_FINE_FR_F_FFE |
2513 YT8821_UTP_EXT_MU_FINE_FR_F_FBE;
2514 set = FIELD_PREP(YT8821_UTP_EXT_MU_FINE_FR_F_FFE, 0x2) |
2515 FIELD_PREP(YT8821_UTP_EXT_MU_FINE_FR_F_FBE, 0x2);
2516 ret = ytphy_modify_ext(phydev,
2517 YT8821_UTP_EXT_MU_FINE_FR_CTRL_REG,
2518 mask, set);
2519 if (ret < 0)
2520 goto err_restore_page;
2522 /* save YT8821_UTP_EXT_PI_CTRL_REG's val for use later */
2523 ret = ytphy_read_ext(phydev, YT8821_UTP_EXT_PI_CTRL_REG);
2524 if (ret < 0)
2525 goto err_restore_page;
2527 save = ret;
2529 mask = YT8821_UTP_EXT_PI_TX_CLK_SEL_AFE |
2530 YT8821_UTP_EXT_PI_RX_CLK_3_SEL_AFE |
2531 YT8821_UTP_EXT_PI_RX_CLK_2_SEL_AFE |
2532 YT8821_UTP_EXT_PI_RX_CLK_1_SEL_AFE |
2533 YT8821_UTP_EXT_PI_RX_CLK_0_SEL_AFE;
2534 ret = ytphy_modify_ext(phydev, YT8821_UTP_EXT_PI_CTRL_REG,
2535 mask, 0);
2536 if (ret < 0)
2537 goto err_restore_page;
2539 /* restore YT8821_UTP_EXT_PI_CTRL_REG's val */
2540 ret = ytphy_write_ext(phydev, YT8821_UTP_EXT_PI_CTRL_REG, save);
2541 if (ret < 0)
2542 goto err_restore_page;
2544 mask = YT8821_UTP_EXT_FECHO_AMP_TH_HUGE;
2545 set = FIELD_PREP(YT8821_UTP_EXT_FECHO_AMP_TH_HUGE, 0x38);
2546 ret = ytphy_modify_ext(phydev, YT8821_UTP_EXT_VCT_CFG6_CTRL_REG,
2547 mask, set);
2548 if (ret < 0)
2549 goto err_restore_page;
2551 mask = YT8821_UTP_EXT_NFR_TX_ABILITY;
2552 set = YT8821_UTP_EXT_NFR_TX_ABILITY;
2553 ret = ytphy_modify_ext(phydev,
2554 YT8821_UTP_EXT_TXGE_NFR_FR_THP_CTRL_REG,
2555 mask, set);
2556 if (ret < 0)
2557 goto err_restore_page;
2559 mask = YT8821_UTP_EXT_PLL_SPARE_CFG;
2560 set = FIELD_PREP(YT8821_UTP_EXT_PLL_SPARE_CFG, 0xe9);
2561 ret = ytphy_modify_ext(phydev, YT8821_UTP_EXT_PLL_CTRL_REG,
2562 mask, set);
2563 if (ret < 0)
2564 goto err_restore_page;
2566 mask = YT8821_UTP_EXT_DAC_IMID_CH_3_10_ORG |
2567 YT8821_UTP_EXT_DAC_IMID_CH_2_10_ORG;
2568 set = FIELD_PREP(YT8821_UTP_EXT_DAC_IMID_CH_3_10_ORG, 0x64) |
2569 FIELD_PREP(YT8821_UTP_EXT_DAC_IMID_CH_2_10_ORG, 0x64);
2570 ret = ytphy_modify_ext(phydev,
2571 YT8821_UTP_EXT_DAC_IMID_CH_2_3_CTRL_REG,
2572 mask, set);
2573 if (ret < 0)
2574 goto err_restore_page;
2576 mask = YT8821_UTP_EXT_DAC_IMID_CH_1_10_ORG |
2577 YT8821_UTP_EXT_DAC_IMID_CH_0_10_ORG;
2578 set = FIELD_PREP(YT8821_UTP_EXT_DAC_IMID_CH_1_10_ORG, 0x64) |
2579 FIELD_PREP(YT8821_UTP_EXT_DAC_IMID_CH_0_10_ORG, 0x64);
2580 ret = ytphy_modify_ext(phydev,
2581 YT8821_UTP_EXT_DAC_IMID_CH_0_1_CTRL_REG,
2582 mask, set);
2583 if (ret < 0)
2584 goto err_restore_page;
2586 mask = YT8821_UTP_EXT_DAC_IMSB_CH_3_10_ORG |
2587 YT8821_UTP_EXT_DAC_IMSB_CH_2_10_ORG;
2588 set = FIELD_PREP(YT8821_UTP_EXT_DAC_IMSB_CH_3_10_ORG, 0x64) |
2589 FIELD_PREP(YT8821_UTP_EXT_DAC_IMSB_CH_2_10_ORG, 0x64);
2590 ret = ytphy_modify_ext(phydev,
2591 YT8821_UTP_EXT_DAC_IMSB_CH_2_3_CTRL_REG,
2592 mask, set);
2593 if (ret < 0)
2594 goto err_restore_page;
2596 mask = YT8821_UTP_EXT_DAC_IMSB_CH_1_10_ORG |
2597 YT8821_UTP_EXT_DAC_IMSB_CH_0_10_ORG;
2598 set = FIELD_PREP(YT8821_UTP_EXT_DAC_IMSB_CH_1_10_ORG, 0x64) |
2599 FIELD_PREP(YT8821_UTP_EXT_DAC_IMSB_CH_0_10_ORG, 0x64);
2600 ret = ytphy_modify_ext(phydev,
2601 YT8821_UTP_EXT_DAC_IMSB_CH_0_1_CTRL_REG,
2602 mask, set);
2604 err_restore_page:
2605 return phy_restore_page(phydev, old_page, ret);
2609 * yt8821_auto_sleep_config() - phy auto sleep config
2610 * @phydev: a pointer to a &struct phy_device
2611 * @enable: true enable auto sleep, false disable auto sleep
2613 * Returns: 0 or negative errno code
2615 static int yt8821_auto_sleep_config(struct phy_device *phydev,
2616 bool enable)
2618 int old_page;
2619 int ret = 0;
2621 old_page = phy_select_page(phydev, YT8521_RSSR_UTP_SPACE);
2622 if (old_page < 0) {
2623 phydev_err(phydev, "Failed to select page: %d\n",
2624 old_page);
2625 goto err_restore_page;
2628 ret = ytphy_modify_ext(phydev,
2629 YT8521_EXTREG_SLEEP_CONTROL1_REG,
2630 YT8521_ESC1R_SLEEP_SW,
2631 enable ? 1 : 0);
2633 err_restore_page:
2634 return phy_restore_page(phydev, old_page, ret);
2638 * yt8821_soft_reset() - soft reset utp and serdes
2639 * @phydev: a pointer to a &struct phy_device
2641 * Returns: 0 or negative errno code
2643 static int yt8821_soft_reset(struct phy_device *phydev)
2645 return ytphy_modify_ext_with_lock(phydev, YT8521_CHIP_CONFIG_REG,
2646 YT8521_CCR_SW_RST, 0);
2650 * yt8821_config_init() - phy initializatioin
2651 * @phydev: a pointer to a &struct phy_device
2653 * Returns: 0 or negative errno code
2655 static int yt8821_config_init(struct phy_device *phydev)
2657 u8 mode = YT8821_CHIP_MODE_AUTO_BX2500_SGMII;
2658 int ret;
2659 u16 set;
2661 if (phydev->interface == PHY_INTERFACE_MODE_2500BASEX)
2662 mode = YT8821_CHIP_MODE_FORCE_BX2500;
2664 set = FIELD_PREP(YT8521_CCR_MODE_SEL_MASK, mode);
2665 ret = ytphy_modify_ext_with_lock(phydev,
2666 YT8521_CHIP_CONFIG_REG,
2667 YT8521_CCR_MODE_SEL_MASK,
2668 set);
2669 if (ret < 0)
2670 return ret;
2672 __set_bit(PHY_INTERFACE_MODE_2500BASEX,
2673 phydev->possible_interfaces);
2675 if (mode == YT8821_CHIP_MODE_AUTO_BX2500_SGMII) {
2676 __set_bit(PHY_INTERFACE_MODE_SGMII,
2677 phydev->possible_interfaces);
2679 phydev->rate_matching = RATE_MATCH_NONE;
2680 } else if (mode == YT8821_CHIP_MODE_FORCE_BX2500) {
2681 phydev->rate_matching = RATE_MATCH_PAUSE;
2684 ret = yt8821_serdes_init(phydev);
2685 if (ret < 0)
2686 return ret;
2688 ret = yt8821_utp_init(phydev);
2689 if (ret < 0)
2690 return ret;
2692 /* disable auto sleep */
2693 ret = yt8821_auto_sleep_config(phydev, false);
2694 if (ret < 0)
2695 return ret;
2697 /* soft reset */
2698 return yt8821_soft_reset(phydev);
2702 * yt8821_adjust_status() - update speed and duplex to phydev
2703 * @phydev: a pointer to a &struct phy_device
2704 * @val: read from YTPHY_SPECIFIC_STATUS_REG
2706 static void yt8821_adjust_status(struct phy_device *phydev, int val)
2708 int speed, duplex;
2709 int speed_mode;
2711 duplex = FIELD_GET(YTPHY_SSR_DUPLEX, val);
2712 speed_mode = val & YTPHY_SSR_SPEED_MASK;
2713 switch (speed_mode) {
2714 case YTPHY_SSR_SPEED_10M:
2715 speed = SPEED_10;
2716 break;
2717 case YTPHY_SSR_SPEED_100M:
2718 speed = SPEED_100;
2719 break;
2720 case YTPHY_SSR_SPEED_1000M:
2721 speed = SPEED_1000;
2722 break;
2723 case YTPHY_SSR_SPEED_2500M:
2724 speed = SPEED_2500;
2725 break;
2726 default:
2727 speed = SPEED_UNKNOWN;
2728 break;
2731 phydev->speed = speed;
2732 phydev->duplex = duplex;
2736 * yt8821_update_interface() - update interface per current speed
2737 * @phydev: a pointer to a &struct phy_device
2739 static void yt8821_update_interface(struct phy_device *phydev)
2741 if (!phydev->link)
2742 return;
2744 switch (phydev->speed) {
2745 case SPEED_2500:
2746 phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
2747 break;
2748 case SPEED_1000:
2749 case SPEED_100:
2750 case SPEED_10:
2751 phydev->interface = PHY_INTERFACE_MODE_SGMII;
2752 break;
2753 default:
2754 phydev_warn(phydev, "phy speed err :%d\n", phydev->speed);
2755 break;
2760 * yt8821_read_status() - determines the negotiated speed and duplex
2761 * @phydev: a pointer to a &struct phy_device
2763 * Returns: 0 or negative errno code
2765 static int yt8821_read_status(struct phy_device *phydev)
2767 int link;
2768 int ret;
2769 int val;
2771 ret = ytphy_write_ext_with_lock(phydev,
2772 YT8521_REG_SPACE_SELECT_REG,
2773 YT8521_RSSR_UTP_SPACE);
2774 if (ret < 0)
2775 return ret;
2777 ret = genphy_read_status(phydev);
2778 if (ret < 0)
2779 return ret;
2781 if (phydev->autoneg_complete) {
2782 ret = genphy_c45_read_lpa(phydev);
2783 if (ret < 0)
2784 return ret;
2787 ret = phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
2788 if (ret < 0)
2789 return ret;
2791 val = ret;
2793 link = val & YTPHY_SSR_LINK;
2794 if (link)
2795 yt8821_adjust_status(phydev, val);
2797 if (link) {
2798 if (phydev->link == 0)
2799 phydev_dbg(phydev,
2800 "%s, phy addr: %d, link up\n",
2801 __func__, phydev->mdio.addr);
2802 phydev->link = 1;
2803 } else {
2804 if (phydev->link == 1)
2805 phydev_dbg(phydev,
2806 "%s, phy addr: %d, link down\n",
2807 __func__, phydev->mdio.addr);
2808 phydev->link = 0;
2811 val = ytphy_read_ext_with_lock(phydev, YT8521_CHIP_CONFIG_REG);
2812 if (val < 0)
2813 return val;
2815 if (FIELD_GET(YT8521_CCR_MODE_SEL_MASK, val) ==
2816 YT8821_CHIP_MODE_AUTO_BX2500_SGMII)
2817 yt8821_update_interface(phydev);
2819 return 0;
2823 * yt8821_modify_utp_fiber_bmcr - bits modify a PHY's BMCR register
2824 * @phydev: the phy_device struct
2825 * @mask: bit mask of bits to clear
2826 * @set: bit mask of bits to set
2828 * NOTE: Convenience function which allows a PHY's BMCR register to be
2829 * modified as new register value = (old register value & ~mask) | set.
2831 * Returns: 0 or negative errno code
2833 static int yt8821_modify_utp_fiber_bmcr(struct phy_device *phydev,
2834 u16 mask, u16 set)
2836 int ret;
2838 ret = yt8521_modify_bmcr_paged(phydev, YT8521_RSSR_UTP_SPACE,
2839 mask, set);
2840 if (ret < 0)
2841 return ret;
2843 return yt8521_modify_bmcr_paged(phydev, YT8521_RSSR_FIBER_SPACE,
2844 mask, set);
2848 * yt8821_suspend() - suspend the hardware
2849 * @phydev: a pointer to a &struct phy_device
2851 * Returns: 0 or negative errno code
2853 static int yt8821_suspend(struct phy_device *phydev)
2855 int wol_config;
2857 wol_config = ytphy_read_ext_with_lock(phydev,
2858 YTPHY_WOL_CONFIG_REG);
2859 if (wol_config < 0)
2860 return wol_config;
2862 /* if wol enable, do nothing */
2863 if (wol_config & YTPHY_WCR_ENABLE)
2864 return 0;
2866 return yt8821_modify_utp_fiber_bmcr(phydev, 0, BMCR_PDOWN);
2870 * yt8821_resume() - resume the hardware
2871 * @phydev: a pointer to a &struct phy_device
2873 * Returns: 0 or negative errno code
2875 static int yt8821_resume(struct phy_device *phydev)
2877 int wol_config;
2878 int ret;
2880 /* disable auto sleep */
2881 ret = yt8821_auto_sleep_config(phydev, false);
2882 if (ret < 0)
2883 return ret;
2885 wol_config = ytphy_read_ext_with_lock(phydev,
2886 YTPHY_WOL_CONFIG_REG);
2887 if (wol_config < 0)
2888 return wol_config;
2890 /* if wol enable, do nothing */
2891 if (wol_config & YTPHY_WCR_ENABLE)
2892 return 0;
2894 return yt8821_modify_utp_fiber_bmcr(phydev, BMCR_PDOWN, 0);
2897 static struct phy_driver motorcomm_phy_drvs[] = {
2899 PHY_ID_MATCH_EXACT(PHY_ID_YT8511),
2900 .name = "YT8511 Gigabit Ethernet",
2901 .config_init = yt8511_config_init,
2902 .suspend = genphy_suspend,
2903 .resume = genphy_resume,
2904 .read_page = yt8511_read_page,
2905 .write_page = yt8511_write_page,
2908 PHY_ID_MATCH_EXACT(PHY_ID_YT8521),
2909 .name = "YT8521 Gigabit Ethernet",
2910 .get_features = yt8521_get_features,
2911 .probe = yt8521_probe,
2912 .read_page = yt8521_read_page,
2913 .write_page = yt8521_write_page,
2914 .get_wol = ytphy_get_wol,
2915 .set_wol = ytphy_set_wol,
2916 .config_aneg = yt8521_config_aneg,
2917 .aneg_done = yt8521_aneg_done,
2918 .config_init = yt8521_config_init,
2919 .read_status = yt8521_read_status,
2920 .soft_reset = yt8521_soft_reset,
2921 .suspend = yt8521_suspend,
2922 .resume = yt8521_resume,
2925 PHY_ID_MATCH_EXACT(PHY_ID_YT8531),
2926 .name = "YT8531 Gigabit Ethernet",
2927 .probe = yt8531_probe,
2928 .config_init = yt8531_config_init,
2929 .suspend = genphy_suspend,
2930 .resume = genphy_resume,
2931 .get_wol = ytphy_get_wol,
2932 .set_wol = yt8531_set_wol,
2933 .link_change_notify = yt8531_link_change_notify,
2936 PHY_ID_MATCH_EXACT(PHY_ID_YT8531S),
2937 .name = "YT8531S Gigabit Ethernet",
2938 .get_features = yt8521_get_features,
2939 .probe = yt8521_probe,
2940 .read_page = yt8521_read_page,
2941 .write_page = yt8521_write_page,
2942 .get_wol = ytphy_get_wol,
2943 .set_wol = ytphy_set_wol,
2944 .config_aneg = yt8521_config_aneg,
2945 .aneg_done = yt8521_aneg_done,
2946 .config_init = yt8521_config_init,
2947 .read_status = yt8521_read_status,
2948 .soft_reset = yt8521_soft_reset,
2949 .suspend = yt8521_suspend,
2950 .resume = yt8521_resume,
2953 PHY_ID_MATCH_EXACT(PHY_ID_YT8821),
2954 .name = "YT8821 2.5Gbps PHY",
2955 .get_features = yt8821_get_features,
2956 .read_page = yt8521_read_page,
2957 .write_page = yt8521_write_page,
2958 .get_wol = ytphy_get_wol,
2959 .set_wol = ytphy_set_wol,
2960 .config_aneg = genphy_config_aneg,
2961 .aneg_done = yt8821_aneg_done,
2962 .config_init = yt8821_config_init,
2963 .get_rate_matching = yt8821_get_rate_matching,
2964 .read_status = yt8821_read_status,
2965 .soft_reset = yt8821_soft_reset,
2966 .suspend = yt8821_suspend,
2967 .resume = yt8821_resume,
2971 module_phy_driver(motorcomm_phy_drvs);
2973 MODULE_DESCRIPTION("Motorcomm 8511/8521/8531/8531S/8821 PHY driver");
2974 MODULE_AUTHOR("Peter Geis");
2975 MODULE_AUTHOR("Frank");
2976 MODULE_LICENSE("GPL");
2978 static const struct mdio_device_id __maybe_unused motorcomm_tbl[] = {
2979 { PHY_ID_MATCH_EXACT(PHY_ID_YT8511) },
2980 { PHY_ID_MATCH_EXACT(PHY_ID_YT8521) },
2981 { PHY_ID_MATCH_EXACT(PHY_ID_YT8531) },
2982 { PHY_ID_MATCH_EXACT(PHY_ID_YT8531S) },
2983 { PHY_ID_MATCH_EXACT(PHY_ID_YT8821) },
2984 { /* sentinel */ }
2987 MODULE_DEVICE_TABLE(mdio, motorcomm_tbl);