1 From 70479a40954cf353e87a486997a3477108c75aa9 Mon Sep 17 00:00:00 2001
2 From: Frank <Frank.Sae@motor-comm.com>
3 Date: Fri, 28 Oct 2022 17:26:21 +0800
4 Subject: [PATCH] net: phy: Add driver for Motorcomm yt8521 gigabit ethernet
7 Add a driver for the motorcomm yt8521 gigabit ethernet phy. We have verified
8 the driver on StarFive VisionFive development board, which is developed by
9 Shanghai StarFive Technology Co., Ltd.. On the board, yt8521 gigabit ethernet
10 phy works in utp mode, RGMII interface, supports 1000M/100M/10M speeds, and
13 Signed-off-by: Frank <Frank.Sae@motor-comm.com>
14 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
15 Signed-off-by: David S. Miller <davem@davemloft.net>
18 drivers/net/phy/Kconfig | 2 +-
19 drivers/net/phy/motorcomm.c | 1635 ++++++++++++++++++++++++++++++++++-
20 3 files changed, 1635 insertions(+), 3 deletions(-)
24 @@ -12693,6 +12693,7 @@ F: include/uapi/linux/meye.h
27 M: Peter Geis <pgwipeout@gmail.com>
28 +M: Frank <Frank.Sae@motor-comm.com>
29 L: netdev@vger.kernel.org
31 F: drivers/net/phy/motorcomm.c
32 --- a/drivers/net/phy/Kconfig
33 +++ b/drivers/net/phy/Kconfig
34 @@ -245,7 +245,7 @@ config MOTORCOMM_PHY
35 tristate "Motorcomm PHYs"
37 Enables support for Motorcomm network PHYs.
38 - Currently supports the YT8511 gigabit PHY.
39 + Currently supports the YT8511, YT8521 Gigabit Ethernet PHYs.
42 tristate "National Semiconductor PHYs"
43 --- a/drivers/net/phy/motorcomm.c
44 +++ b/drivers/net/phy/motorcomm.c
46 // SPDX-License-Identifier: GPL-2.0+
48 - * Driver for Motorcomm PHYs
49 + * Motorcomm 8511/8521 PHY driver.
51 * Author: Peter Geis <pgwipeout@gmail.com>
52 + * Author: Frank <Frank.Sae@motor-comm.com>
55 +#include <linux/etherdevice.h>
56 #include <linux/kernel.h>
57 #include <linux/module.h>
58 #include <linux/phy.h>
60 #define PHY_ID_YT8511 0x0000010a
61 +#define PHY_ID_YT8521 0x0000011A
63 +/* YT8521 Register Overview
64 + * UTP Register space | FIBER Register space
65 + * ------------------------------------------------------------
66 + * | UTP MII | FIBER MII |
68 + * | UTP Extended | FIBER Extended |
69 + * ------------------------------------------------------------
70 + * | Common Extended |
71 + * ------------------------------------------------------------
74 +/* 0x10 ~ 0x15 , 0x1E and 0x1F are common MII registers of yt phy */
76 +/* Specific Function Control Register */
77 +#define YTPHY_SPECIFIC_FUNCTION_CONTROL_REG 0x10
79 +/* 2b00 Manual MDI configuration
80 + * 2b01 Manual MDIX configuration
82 + * 2b11 Enable automatic crossover for all modes *default*
84 +#define YTPHY_SFCR_MDI_CROSSOVER_MODE_MASK (BIT(6) | BIT(5))
85 +#define YTPHY_SFCR_CROSSOVER_EN BIT(3)
86 +#define YTPHY_SFCR_SQE_TEST_EN BIT(2)
87 +#define YTPHY_SFCR_POLARITY_REVERSAL_EN BIT(1)
88 +#define YTPHY_SFCR_JABBER_DIS BIT(0)
90 +/* Specific Status Register */
91 +#define YTPHY_SPECIFIC_STATUS_REG 0x11
92 +#define YTPHY_SSR_SPEED_MODE_OFFSET 14
94 +#define YTPHY_SSR_SPEED_MODE_MASK (BIT(15) | BIT(14))
95 +#define YTPHY_SSR_SPEED_10M 0x0
96 +#define YTPHY_SSR_SPEED_100M 0x1
97 +#define YTPHY_SSR_SPEED_1000M 0x2
98 +#define YTPHY_SSR_DUPLEX_OFFSET 13
99 +#define YTPHY_SSR_DUPLEX BIT(13)
100 +#define YTPHY_SSR_PAGE_RECEIVED BIT(12)
101 +#define YTPHY_SSR_SPEED_DUPLEX_RESOLVED BIT(11)
102 +#define YTPHY_SSR_LINK BIT(10)
103 +#define YTPHY_SSR_MDIX_CROSSOVER BIT(6)
104 +#define YTPHY_SSR_DOWNGRADE BIT(5)
105 +#define YTPHY_SSR_TRANSMIT_PAUSE BIT(3)
106 +#define YTPHY_SSR_RECEIVE_PAUSE BIT(2)
107 +#define YTPHY_SSR_POLARITY BIT(1)
108 +#define YTPHY_SSR_JABBER BIT(0)
110 +/* Interrupt enable Register */
111 +#define YTPHY_INTERRUPT_ENABLE_REG 0x12
112 +#define YTPHY_IER_WOL BIT(6)
114 +/* Interrupt Status Register */
115 +#define YTPHY_INTERRUPT_STATUS_REG 0x13
116 +#define YTPHY_ISR_AUTONEG_ERR BIT(15)
117 +#define YTPHY_ISR_SPEED_CHANGED BIT(14)
118 +#define YTPHY_ISR_DUPLEX_CHANGED BIT(13)
119 +#define YTPHY_ISR_PAGE_RECEIVED BIT(12)
120 +#define YTPHY_ISR_LINK_FAILED BIT(11)
121 +#define YTPHY_ISR_LINK_SUCCESSED BIT(10)
122 +#define YTPHY_ISR_WOL BIT(6)
123 +#define YTPHY_ISR_WIRESPEED_DOWNGRADE BIT(5)
124 +#define YTPHY_ISR_SERDES_LINK_FAILED BIT(3)
125 +#define YTPHY_ISR_SERDES_LINK_SUCCESSED BIT(2)
126 +#define YTPHY_ISR_POLARITY_CHANGED BIT(1)
127 +#define YTPHY_ISR_JABBER_HAPPENED BIT(0)
129 +/* Speed Auto Downgrade Control Register */
130 +#define YTPHY_SPEED_AUTO_DOWNGRADE_CONTROL_REG 0x14
131 +#define YTPHY_SADCR_SPEED_DOWNGRADE_EN BIT(5)
133 +/* If these bits are set to 3, the PHY attempts five times ( 3(set value) +
134 + * additional 2) before downgrading, default 0x3
136 +#define YTPHY_SADCR_SPEED_RETRY_LIMIT (0x3 << 2)
138 +/* Rx Error Counter Register */
139 +#define YTPHY_RX_ERROR_COUNTER_REG 0x15
141 +/* Extended Register's Address Offset Register */
142 +#define YTPHY_PAGE_SELECT 0x1E
144 +/* Extended Register's Data Register */
145 +#define YTPHY_PAGE_DATA 0x1F
147 +/* FIBER Auto-Negotiation link partner ability */
148 +#define YTPHY_FLPA_PAUSE (0x3 << 7)
149 +#define YTPHY_FLPA_ASYM_PAUSE (0x2 << 7)
151 #define YT8511_PAGE_SELECT 0x1e
152 #define YT8511_PAGE 0x1f
154 #define YT8511_DELAY_FE_TX_EN (0xf << 12)
155 #define YT8511_DELAY_FE_TX_DIS (0x2 << 12)
157 +/* Extended register is different from MMD Register and MII Register.
158 + * We can use ytphy_read_ext/ytphy_write_ext/ytphy_modify_ext function to
159 + * operate extended register.
160 + * Extended Register start
163 +/* Phy gmii clock gating Register */
164 +#define YT8521_CLOCK_GATING_REG 0xC
165 +#define YT8521_CGR_RX_CLK_EN BIT(12)
167 +#define YT8521_EXTREG_SLEEP_CONTROL1_REG 0x27
168 +#define YT8521_ESC1R_SLEEP_SW BIT(15)
169 +#define YT8521_ESC1R_PLLON_SLP BIT(14)
171 +/* Phy fiber Link timer cfg2 Register */
172 +#define YT8521_LINK_TIMER_CFG2_REG 0xA5
173 +#define YT8521_LTCR_EN_AUTOSEN BIT(15)
175 +/* 0xA000, 0xA001, 0xA003 ,and 0xA006 ~ 0xA00A are common ext registers
176 + * of yt8521 phy. There is no need to switch reg space when operating these
180 +#define YT8521_REG_SPACE_SELECT_REG 0xA000
181 +#define YT8521_RSSR_SPACE_MASK BIT(1)
182 +#define YT8521_RSSR_FIBER_SPACE (0x1 << 1)
183 +#define YT8521_RSSR_UTP_SPACE (0x0 << 1)
184 +#define YT8521_RSSR_TO_BE_ARBITRATED (0xFF)
186 +#define YT8521_CHIP_CONFIG_REG 0xA001
187 +#define YT8521_CCR_SW_RST BIT(15)
189 +#define YT8521_CCR_MODE_SEL_MASK (BIT(2) | BIT(1) | BIT(0))
190 +#define YT8521_CCR_MODE_UTP_TO_RGMII 0
191 +#define YT8521_CCR_MODE_FIBER_TO_RGMII 1
192 +#define YT8521_CCR_MODE_UTP_FIBER_TO_RGMII 2
193 +#define YT8521_CCR_MODE_UTP_TO_SGMII 3
194 +#define YT8521_CCR_MODE_SGPHY_TO_RGMAC 4
195 +#define YT8521_CCR_MODE_SGMAC_TO_RGPHY 5
196 +#define YT8521_CCR_MODE_UTP_TO_FIBER_AUTO 6
197 +#define YT8521_CCR_MODE_UTP_TO_FIBER_FORCE 7
199 +/* 3 phy polling modes,poll mode combines utp and fiber mode*/
200 +#define YT8521_MODE_FIBER 0x1
201 +#define YT8521_MODE_UTP 0x2
202 +#define YT8521_MODE_POLL 0x3
204 +#define YT8521_RGMII_CONFIG1_REG 0xA003
206 +/* TX Gig-E Delay is bits 3:0, default 0x1
207 + * TX Fast-E Delay is bits 7:4, default 0xf
208 + * RX Delay is bits 13:10, default 0x0
209 + * Delay = 150ps * N
210 + * On = 2250ps, off = 0ps
212 +#define YT8521_RC1R_RX_DELAY_MASK (0xF << 10)
213 +#define YT8521_RC1R_RX_DELAY_EN (0xF << 10)
214 +#define YT8521_RC1R_RX_DELAY_DIS (0x0 << 10)
215 +#define YT8521_RC1R_FE_TX_DELAY_MASK (0xF << 4)
216 +#define YT8521_RC1R_FE_TX_DELAY_EN (0xF << 4)
217 +#define YT8521_RC1R_FE_TX_DELAY_DIS (0x0 << 4)
218 +#define YT8521_RC1R_GE_TX_DELAY_MASK (0xF << 0)
219 +#define YT8521_RC1R_GE_TX_DELAY_EN (0xF << 0)
220 +#define YT8521_RC1R_GE_TX_DELAY_DIS (0x0 << 0)
222 +#define YTPHY_MISC_CONFIG_REG 0xA006
223 +#define YTPHY_MCR_FIBER_SPEED_MASK BIT(0)
224 +#define YTPHY_MCR_FIBER_1000BX (0x1 << 0)
225 +#define YTPHY_MCR_FIBER_100FX (0x0 << 0)
227 +/* WOL MAC ADDR: MACADDR2(highest), MACADDR1(middle), MACADDR0(lowest) */
228 +#define YTPHY_WOL_MACADDR2_REG 0xA007
229 +#define YTPHY_WOL_MACADDR1_REG 0xA008
230 +#define YTPHY_WOL_MACADDR0_REG 0xA009
232 +#define YTPHY_WOL_CONFIG_REG 0xA00A
233 +#define YTPHY_WCR_INTR_SEL BIT(6)
234 +#define YTPHY_WCR_ENABLE BIT(3)
237 + * 2b01 168ms *default*
241 +#define YTPHY_WCR_PULSE_WIDTH_MASK (BIT(2) | BIT(1))
242 +#define YTPHY_WCR_PULSE_WIDTH_672MS (BIT(2) | BIT(1))
244 +/* 1b0 Interrupt and WOL events is level triggered and active LOW *default*
245 + * 1b1 Interrupt and WOL events is pulse triggered and active LOW
247 +#define YTPHY_WCR_TYPE_PULSE BIT(0)
249 +/* Extended Register end */
251 +struct yt8521_priv {
252 + /* combo_advertising is used for case of YT8521 in combo mode,
253 + * this means that yt8521 may work in utp or fiber mode which depends
254 + * on which media is connected (YT8521_RSSR_TO_BE_ARBITRATED).
256 + __ETHTOOL_DECLARE_LINK_MODE_MASK(combo_advertising);
258 + /* YT8521_MODE_FIBER / YT8521_MODE_UTP / YT8521_MODE_POLL*/
260 + u8 strap_mode; /* 8 working modes */
261 + /* current reg page of yt8521 phy:
262 + * YT8521_RSSR_UTP_SPACE
263 + * YT8521_RSSR_FIBER_SPACE
264 + * YT8521_RSSR_TO_BE_ARBITRATED
270 + * ytphy_read_ext() - read a PHY's extended register
271 + * @phydev: a pointer to a &struct phy_device
272 + * @regnum: register number to read
274 + * NOTE:The caller must have taken the MDIO bus lock.
276 + * returns the value of regnum reg or negative error code
278 +static int ytphy_read_ext(struct phy_device *phydev, u16 regnum)
282 + ret = __phy_write(phydev, YTPHY_PAGE_SELECT, regnum);
286 + return __phy_read(phydev, YTPHY_PAGE_DATA);
290 + * ytphy_read_ext_with_lock() - read a PHY's extended register
291 + * @phydev: a pointer to a &struct phy_device
292 + * @regnum: register number to read
294 + * returns the value of regnum reg or negative error code
296 +static int ytphy_read_ext_with_lock(struct phy_device *phydev, u16 regnum)
300 + phy_lock_mdio_bus(phydev);
301 + ret = ytphy_read_ext(phydev, regnum);
302 + phy_unlock_mdio_bus(phydev);
308 + * ytphy_write_ext() - write a PHY's extended register
309 + * @phydev: a pointer to a &struct phy_device
310 + * @regnum: register number to write
311 + * @val: value to write to @regnum
313 + * NOTE:The caller must have taken the MDIO bus lock.
315 + * returns 0 or negative error code
317 +static int ytphy_write_ext(struct phy_device *phydev, u16 regnum, u16 val)
321 + ret = __phy_write(phydev, YTPHY_PAGE_SELECT, regnum);
325 + return __phy_write(phydev, YTPHY_PAGE_DATA, val);
329 + * ytphy_write_ext_with_lock() - write a PHY's extended register
330 + * @phydev: a pointer to a &struct phy_device
331 + * @regnum: register number to write
332 + * @val: value to write to @regnum
334 + * returns 0 or negative error code
336 +static int ytphy_write_ext_with_lock(struct phy_device *phydev, u16 regnum,
341 + phy_lock_mdio_bus(phydev);
342 + ret = ytphy_write_ext(phydev, regnum, val);
343 + phy_unlock_mdio_bus(phydev);
349 + * ytphy_modify_ext() - bits modify a PHY's extended register
350 + * @phydev: a pointer to a &struct phy_device
351 + * @regnum: register number to write
352 + * @mask: bit mask of bits to clear
353 + * @set: bit mask of bits to set
355 + * NOTE: Convenience function which allows a PHY's extended register to be
356 + * modified as new register value = (old register value & ~mask) | set.
357 + * The caller must have taken the MDIO bus lock.
359 + * returns 0 or negative error code
361 +static int ytphy_modify_ext(struct phy_device *phydev, u16 regnum, u16 mask,
366 + ret = __phy_write(phydev, YTPHY_PAGE_SELECT, regnum);
370 + return __phy_modify(phydev, YTPHY_PAGE_DATA, mask, set);
374 + * ytphy_modify_ext_with_lock() - bits modify a PHY's extended register
375 + * @phydev: a pointer to a &struct phy_device
376 + * @regnum: register number to write
377 + * @mask: bit mask of bits to clear
378 + * @set: bit mask of bits to set
380 + * NOTE: Convenience function which allows a PHY's extended register to be
381 + * modified as new register value = (old register value & ~mask) | set.
383 + * returns 0 or negative error code
385 +static int ytphy_modify_ext_with_lock(struct phy_device *phydev, u16 regnum,
390 + phy_lock_mdio_bus(phydev);
391 + ret = ytphy_modify_ext(phydev, regnum, mask, set);
392 + phy_unlock_mdio_bus(phydev);
398 + * ytphy_get_wol() - report whether wake-on-lan is enabled
399 + * @phydev: a pointer to a &struct phy_device
400 + * @wol: a pointer to a &struct ethtool_wolinfo
402 + * NOTE: YTPHY_WOL_CONFIG_REG is common ext reg.
404 +static void ytphy_get_wol(struct phy_device *phydev,
405 + struct ethtool_wolinfo *wol)
409 + wol->supported = WAKE_MAGIC;
412 + wol_config = ytphy_read_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG);
413 + if (wol_config < 0)
416 + if (wol_config & YTPHY_WCR_ENABLE)
417 + wol->wolopts |= WAKE_MAGIC;
421 + * ytphy_set_wol() - turn wake-on-lan on or off
422 + * @phydev: a pointer to a &struct phy_device
423 + * @wol: a pointer to a &struct ethtool_wolinfo
425 + * NOTE: YTPHY_WOL_CONFIG_REG, YTPHY_WOL_MACADDR2_REG, YTPHY_WOL_MACADDR1_REG
426 + * and YTPHY_WOL_MACADDR0_REG are common ext reg. The
427 + * YTPHY_INTERRUPT_ENABLE_REG of UTP is special, fiber also use this register.
429 + * returns 0 or negative errno code
431 +static int ytphy_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
433 + struct net_device *p_attached_dev;
434 + const u16 mac_addr_reg[] = {
435 + YTPHY_WOL_MACADDR2_REG,
436 + YTPHY_WOL_MACADDR1_REG,
437 + YTPHY_WOL_MACADDR0_REG,
439 + const u8 *mac_addr;
446 + if (wol->wolopts & WAKE_MAGIC) {
447 + p_attached_dev = phydev->attached_dev;
448 + if (!p_attached_dev)
451 + mac_addr = (const u8 *)p_attached_dev->dev_addr;
452 + if (!is_valid_ether_addr(mac_addr))
455 + /* lock mdio bus then switch to utp reg space */
456 + old_page = phy_select_page(phydev, YT8521_RSSR_UTP_SPACE);
458 + goto err_restore_page;
460 + /* Store the device address for the magic packet */
461 + for (i = 0; i < 3; i++) {
462 + ret = ytphy_write_ext(phydev, mac_addr_reg[i],
463 + ((mac_addr[i * 2] << 8)) |
464 + (mac_addr[i * 2 + 1]));
466 + goto err_restore_page;
469 + /* Enable WOL feature */
470 + mask = YTPHY_WCR_PULSE_WIDTH_MASK | YTPHY_WCR_INTR_SEL;
471 + val = YTPHY_WCR_ENABLE | YTPHY_WCR_INTR_SEL;
472 + val |= YTPHY_WCR_TYPE_PULSE | YTPHY_WCR_PULSE_WIDTH_672MS;
473 + ret = ytphy_modify_ext(phydev, YTPHY_WOL_CONFIG_REG, mask, val);
475 + goto err_restore_page;
477 + /* Enable WOL interrupt */
478 + ret = __phy_modify(phydev, YTPHY_INTERRUPT_ENABLE_REG, 0,
481 + goto err_restore_page;
484 + old_page = phy_select_page(phydev, YT8521_RSSR_UTP_SPACE);
486 + goto err_restore_page;
488 + /* Disable WOL feature */
489 + mask = YTPHY_WCR_ENABLE | YTPHY_WCR_INTR_SEL;
490 + ret = ytphy_modify_ext(phydev, YTPHY_WOL_CONFIG_REG, mask, 0);
492 + /* Disable WOL interrupt */
493 + ret = __phy_modify(phydev, YTPHY_INTERRUPT_ENABLE_REG,
496 + goto err_restore_page;
500 + return phy_restore_page(phydev, old_page, ret);
503 static int yt8511_read_page(struct phy_device *phydev)
505 return __phy_read(phydev, YT8511_PAGE_SELECT);
506 @@ -111,6 +548,1181 @@ err_restore_page:
507 return phy_restore_page(phydev, oldpage, ret);
511 + * yt8521_read_page() - read reg page
512 + * @phydev: a pointer to a &struct phy_device
514 + * returns current reg space of yt8521 (YT8521_RSSR_FIBER_SPACE/
515 + * YT8521_RSSR_UTP_SPACE) or negative errno code
517 +static int yt8521_read_page(struct phy_device *phydev)
521 + old_page = ytphy_read_ext(phydev, YT8521_REG_SPACE_SELECT_REG);
525 + if ((old_page & YT8521_RSSR_SPACE_MASK) == YT8521_RSSR_FIBER_SPACE)
526 + return YT8521_RSSR_FIBER_SPACE;
528 + return YT8521_RSSR_UTP_SPACE;
532 + * yt8521_write_page() - write reg page
533 + * @phydev: a pointer to a &struct phy_device
534 + * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to write.
536 + * returns 0 or negative errno code
538 +static int yt8521_write_page(struct phy_device *phydev, int page)
540 + int mask = YT8521_RSSR_SPACE_MASK;
543 + if ((page & YT8521_RSSR_SPACE_MASK) == YT8521_RSSR_FIBER_SPACE)
544 + set = YT8521_RSSR_FIBER_SPACE;
546 + set = YT8521_RSSR_UTP_SPACE;
548 + return ytphy_modify_ext(phydev, YT8521_REG_SPACE_SELECT_REG, mask, set);
552 + * yt8521_probe() - read chip config then set suitable polling_mode
553 + * @phydev: a pointer to a &struct phy_device
555 + * returns 0 or negative errno code
557 +static int yt8521_probe(struct phy_device *phydev)
559 + struct device *dev = &phydev->mdio.dev;
560 + struct yt8521_priv *priv;
564 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
568 + phydev->priv = priv;
570 + chip_config = ytphy_read_ext_with_lock(phydev, YT8521_CHIP_CONFIG_REG);
571 + if (chip_config < 0)
572 + return chip_config;
574 + priv->strap_mode = chip_config & YT8521_CCR_MODE_SEL_MASK;
575 + switch (priv->strap_mode) {
576 + case YT8521_CCR_MODE_FIBER_TO_RGMII:
577 + case YT8521_CCR_MODE_SGPHY_TO_RGMAC:
578 + case YT8521_CCR_MODE_SGMAC_TO_RGPHY:
579 + priv->polling_mode = YT8521_MODE_FIBER;
580 + priv->reg_page = YT8521_RSSR_FIBER_SPACE;
581 + phydev->port = PORT_FIBRE;
583 + case YT8521_CCR_MODE_UTP_FIBER_TO_RGMII:
584 + case YT8521_CCR_MODE_UTP_TO_FIBER_AUTO:
585 + case YT8521_CCR_MODE_UTP_TO_FIBER_FORCE:
586 + priv->polling_mode = YT8521_MODE_POLL;
587 + priv->reg_page = YT8521_RSSR_TO_BE_ARBITRATED;
588 + phydev->port = PORT_NONE;
590 + case YT8521_CCR_MODE_UTP_TO_SGMII:
591 + case YT8521_CCR_MODE_UTP_TO_RGMII:
592 + priv->polling_mode = YT8521_MODE_UTP;
593 + priv->reg_page = YT8521_RSSR_UTP_SPACE;
594 + phydev->port = PORT_TP;
597 + /* set default reg space */
598 + if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
599 + ret = ytphy_write_ext_with_lock(phydev,
600 + YT8521_REG_SPACE_SELECT_REG,
610 + * ytphy_utp_read_lpa() - read LPA then setup lp_advertising for utp
611 + * @phydev: a pointer to a &struct phy_device
613 + * NOTE:The caller must have taken the MDIO bus lock.
615 + * returns 0 or negative errno code
617 +static int ytphy_utp_read_lpa(struct phy_device *phydev)
621 + if (phydev->autoneg == AUTONEG_ENABLE) {
622 + if (!phydev->autoneg_complete) {
623 + mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
625 + mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
629 + if (phydev->is_gigabit_capable) {
630 + lpagb = __phy_read(phydev, MII_STAT1000);
634 + if (lpagb & LPA_1000MSFAIL) {
635 + int adv = __phy_read(phydev, MII_CTRL1000);
640 + if (adv & CTL1000_ENABLE_MASTER)
641 + phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
643 + phydev_err(phydev, "Master/Slave resolution failed\n");
647 + mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
651 + lpa = __phy_read(phydev, MII_LPA);
655 + mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
657 + linkmode_zero(phydev->lp_advertising);
664 + * yt8521_adjust_status() - update speed and duplex to phydev. when in fiber
665 + * mode, adjust speed and duplex.
666 + * @phydev: a pointer to a &struct phy_device
667 + * @status: yt8521 status read from YTPHY_SPECIFIC_STATUS_REG
668 + * @is_utp: false(yt8521 work in fiber mode) or true(yt8521 work in utp mode)
670 + * NOTE:The caller must have taken the MDIO bus lock.
674 +static int yt8521_adjust_status(struct phy_device *phydev, int status,
677 + int speed_mode, duplex;
683 + duplex = (status & YTPHY_SSR_DUPLEX) >> YTPHY_SSR_DUPLEX_OFFSET;
685 + duplex = DUPLEX_FULL; /* for fiber, it always DUPLEX_FULL */
687 + speed_mode = (status & YTPHY_SSR_SPEED_MODE_MASK) >>
688 + YTPHY_SSR_SPEED_MODE_OFFSET;
690 + switch (speed_mode) {
691 + case YTPHY_SSR_SPEED_10M:
695 + /* for fiber, it will never run here, default to
698 + speed = SPEED_UNKNOWN;
700 + case YTPHY_SSR_SPEED_100M:
703 + case YTPHY_SSR_SPEED_1000M:
704 + speed = SPEED_1000;
707 + speed = SPEED_UNKNOWN;
711 + phydev->speed = speed;
712 + phydev->duplex = duplex;
715 + err = ytphy_utp_read_lpa(phydev);
719 + phy_resolve_aneg_pause(phydev);
721 + lpa = __phy_read(phydev, MII_LPA);
725 + /* only support 1000baseX Full */
726 + linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
727 + phydev->lp_advertising, lpa & LPA_1000XFULL);
729 + if (!(lpa & YTPHY_FLPA_PAUSE)) {
731 + phydev->asym_pause = 0;
732 + } else if ((lpa & YTPHY_FLPA_ASYM_PAUSE)) {
734 + phydev->asym_pause = 1;
737 + phydev->asym_pause = 0;
745 + * yt8521_read_status_paged() - determines the speed and duplex of one page
746 + * @phydev: a pointer to a &struct phy_device
747 + * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
750 + * returns 1 (utp or fiber link),0 (no link) or negative errno code
752 +static int yt8521_read_status_paged(struct phy_device *phydev, int page)
754 + int fiber_latch_val;
755 + int fiber_curr_val;
761 + linkmode_zero(phydev->lp_advertising);
762 + phydev->duplex = DUPLEX_UNKNOWN;
763 + phydev->speed = SPEED_UNKNOWN;
764 + phydev->asym_pause = 0;
767 + /* YT8521 has two reg space (utp/fiber) for linkup with utp/fiber
768 + * respectively. but for utp/fiber combo mode, reg space should be
769 + * arbitrated based on media priority. by default, utp takes
770 + * priority. reg space should be properly set before read
771 + * YTPHY_SPECIFIC_STATUS_REG.
774 + page &= YT8521_RSSR_SPACE_MASK;
775 + old_page = phy_select_page(phydev, page);
777 + goto err_restore_page;
779 + /* Read YTPHY_SPECIFIC_STATUS_REG, which indicates the speed and duplex
780 + * of the PHY is actually using.
782 + ret = __phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
784 + goto err_restore_page;
787 + link = !!(status & YTPHY_SSR_LINK);
789 + /* When PHY is in fiber mode, speed transferred from 1000Mbps to
790 + * 100Mbps,there is not link down from YTPHY_SPECIFIC_STATUS_REG, so
791 + * we need check MII_BMSR to identify such case.
793 + if (page == YT8521_RSSR_FIBER_SPACE) {
794 + ret = __phy_read(phydev, MII_BMSR);
796 + goto err_restore_page;
798 + fiber_latch_val = ret;
799 + ret = __phy_read(phydev, MII_BMSR);
801 + goto err_restore_page;
803 + fiber_curr_val = ret;
804 + if (link && fiber_latch_val != fiber_curr_val) {
806 + phydev_info(phydev,
807 + "%s, fiber link down detect, latch = %04x, curr = %04x\n",
808 + __func__, fiber_latch_val, fiber_curr_val);
811 + /* Read autonegotiation status */
812 + ret = __phy_read(phydev, MII_BMSR);
814 + goto err_restore_page;
816 + phydev->autoneg_complete = ret & BMSR_ANEGCOMPLETE ? 1 : 0;
820 + if (page == YT8521_RSSR_UTP_SPACE)
821 + yt8521_adjust_status(phydev, status, true);
823 + yt8521_adjust_status(phydev, status, false);
825 + return phy_restore_page(phydev, old_page, link);
828 + return phy_restore_page(phydev, old_page, ret);
832 + * yt8521_read_status() - determines the negotiated speed and duplex
833 + * @phydev: a pointer to a &struct phy_device
835 + * returns 0 or negative errno code
837 +static int yt8521_read_status(struct phy_device *phydev)
839 + struct yt8521_priv *priv = phydev->priv;
840 + int link_fiber = 0;
845 + if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
846 + link = yt8521_read_status_paged(phydev, priv->reg_page);
850 + /* when page is YT8521_RSSR_TO_BE_ARBITRATED, arbitration is
851 + * needed. by default, utp is higher priority.
854 + link_utp = yt8521_read_status_paged(phydev,
855 + YT8521_RSSR_UTP_SPACE);
860 + link_fiber = yt8521_read_status_paged(phydev,
861 + YT8521_RSSR_FIBER_SPACE);
862 + if (link_fiber < 0)
866 + link = link_utp || link_fiber;
870 + if (phydev->link == 0) {
871 + /* arbitrate reg space based on linkup media type. */
872 + if (priv->polling_mode == YT8521_MODE_POLL &&
873 + priv->reg_page == YT8521_RSSR_TO_BE_ARBITRATED) {
876 + YT8521_RSSR_FIBER_SPACE;
878 + priv->reg_page = YT8521_RSSR_UTP_SPACE;
880 + ret = ytphy_write_ext_with_lock(phydev,
881 + YT8521_REG_SPACE_SELECT_REG,
886 + phydev->port = link_fiber ? PORT_FIBRE : PORT_TP;
888 + phydev_info(phydev, "%s, link up, media: %s\n",
890 + (phydev->port == PORT_TP) ?
896 + if (phydev->link == 1) {
897 + phydev_info(phydev, "%s, link down, media: %s\n",
898 + __func__, (phydev->port == PORT_TP) ?
901 + /* When in YT8521_MODE_POLL mode, need prepare for next
904 + if (priv->polling_mode == YT8521_MODE_POLL) {
905 + priv->reg_page = YT8521_RSSR_TO_BE_ARBITRATED;
906 + phydev->port = PORT_NONE;
917 + * yt8521_modify_bmcr_paged - bits modify a PHY's BMCR register of one page
918 + * @phydev: the phy_device struct
919 + * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to operate
920 + * @mask: bit mask of bits to clear
921 + * @set: bit mask of bits to set
923 + * NOTE: Convenience function which allows a PHY's BMCR register to be
924 + * modified as new register value = (old register value & ~mask) | set.
925 + * YT8521 has two space (utp/fiber) and three mode (utp/fiber/poll), each space
926 + * has MII_BMCR. poll mode combines utp and faber,so need do both.
927 + * If it is reset, it will wait for completion.
929 + * returns 0 or negative errno code
931 +static int yt8521_modify_bmcr_paged(struct phy_device *phydev, int page,
934 + int max_cnt = 500; /* the max wait time of reset ~ 500 ms */
938 + old_page = phy_select_page(phydev, page & YT8521_RSSR_SPACE_MASK);
940 + goto err_restore_page;
942 + ret = __phy_modify(phydev, MII_BMCR, mask, set);
944 + goto err_restore_page;
946 + /* If it is reset, need to wait for the reset to complete */
947 + if (set == BMCR_RESET) {
948 + while (max_cnt--) {
949 + usleep_range(1000, 1100);
950 + ret = __phy_read(phydev, MII_BMCR);
952 + goto err_restore_page;
954 + if (!(ret & BMCR_RESET))
955 + return phy_restore_page(phydev, old_page, 0);
960 + return phy_restore_page(phydev, old_page, ret);
964 + * yt8521_modify_utp_fiber_bmcr - bits modify a PHY's BMCR register
965 + * @phydev: the phy_device struct
966 + * @mask: bit mask of bits to clear
967 + * @set: bit mask of bits to set
969 + * NOTE: Convenience function which allows a PHY's BMCR register to be
970 + * modified as new register value = (old register value & ~mask) | set.
971 + * YT8521 has two space (utp/fiber) and three mode (utp/fiber/poll), each space
972 + * has MII_BMCR. poll mode combines utp and faber,so need do both.
974 + * returns 0 or negative errno code
976 +static int yt8521_modify_utp_fiber_bmcr(struct phy_device *phydev, u16 mask,
979 + struct yt8521_priv *priv = phydev->priv;
982 + if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
983 + ret = yt8521_modify_bmcr_paged(phydev, priv->reg_page, mask,
988 + ret = yt8521_modify_bmcr_paged(phydev, YT8521_RSSR_UTP_SPACE,
993 + ret = yt8521_modify_bmcr_paged(phydev, YT8521_RSSR_FIBER_SPACE,
1002 + * yt8521_soft_reset() - called to issue a PHY software reset
1003 + * @phydev: a pointer to a &struct phy_device
1005 + * returns 0 or negative errno code
1007 +static int yt8521_soft_reset(struct phy_device *phydev)
1009 + return yt8521_modify_utp_fiber_bmcr(phydev, 0, BMCR_RESET);
1013 + * yt8521_suspend() - suspend the hardware
1014 + * @phydev: a pointer to a &struct phy_device
1016 + * returns 0 or negative errno code
1018 +static int yt8521_suspend(struct phy_device *phydev)
1022 + /* YTPHY_WOL_CONFIG_REG is common ext reg */
1023 + wol_config = ytphy_read_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG);
1024 + if (wol_config < 0)
1025 + return wol_config;
1027 + /* if wol enable, do nothing */
1028 + if (wol_config & YTPHY_WCR_ENABLE)
1031 + return yt8521_modify_utp_fiber_bmcr(phydev, 0, BMCR_PDOWN);
1035 + * yt8521_resume() - resume the hardware
1036 + * @phydev: a pointer to a &struct phy_device
1038 + * returns 0 or negative errno code
1040 +static int yt8521_resume(struct phy_device *phydev)
1045 + /* disable auto sleep */
1046 + ret = ytphy_modify_ext_with_lock(phydev,
1047 + YT8521_EXTREG_SLEEP_CONTROL1_REG,
1048 + YT8521_ESC1R_SLEEP_SW, 0);
1052 + wol_config = ytphy_read_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG);
1053 + if (wol_config < 0)
1054 + return wol_config;
1056 + /* if wol enable, do nothing */
1057 + if (wol_config & YTPHY_WCR_ENABLE)
1060 + return yt8521_modify_utp_fiber_bmcr(phydev, BMCR_PDOWN, 0);
1064 + * yt8521_config_init() - called to initialize the PHY
1065 + * @phydev: a pointer to a &struct phy_device
1067 + * returns 0 or negative errno code
1069 +static int yt8521_config_init(struct phy_device *phydev)
1075 + old_page = phy_select_page(phydev, YT8521_RSSR_UTP_SPACE);
1077 + goto err_restore_page;
1079 + switch (phydev->interface) {
1080 + case PHY_INTERFACE_MODE_RGMII:
1081 + val = YT8521_RC1R_GE_TX_DELAY_DIS | YT8521_RC1R_GE_TX_DELAY_DIS;
1082 + val |= YT8521_RC1R_RX_DELAY_DIS;
1084 + case PHY_INTERFACE_MODE_RGMII_RXID:
1085 + val = YT8521_RC1R_GE_TX_DELAY_DIS | YT8521_RC1R_GE_TX_DELAY_DIS;
1086 + val |= YT8521_RC1R_RX_DELAY_EN;
1088 + case PHY_INTERFACE_MODE_RGMII_TXID:
1089 + val = YT8521_RC1R_GE_TX_DELAY_EN | YT8521_RC1R_GE_TX_DELAY_EN;
1090 + val |= YT8521_RC1R_RX_DELAY_DIS;
1092 + case PHY_INTERFACE_MODE_RGMII_ID:
1093 + val = YT8521_RC1R_GE_TX_DELAY_EN | YT8521_RC1R_GE_TX_DELAY_EN;
1094 + val |= YT8521_RC1R_RX_DELAY_EN;
1096 + case PHY_INTERFACE_MODE_SGMII:
1098 + default: /* do not support other modes */
1099 + ret = -EOPNOTSUPP;
1100 + goto err_restore_page;
1103 + /* set rgmii delay mode */
1104 + if (phydev->interface != PHY_INTERFACE_MODE_SGMII) {
1105 + ret = ytphy_modify_ext(phydev, YT8521_RGMII_CONFIG1_REG,
1106 + (YT8521_RC1R_RX_DELAY_MASK |
1107 + YT8521_RC1R_FE_TX_DELAY_MASK |
1108 + YT8521_RC1R_GE_TX_DELAY_MASK),
1111 + goto err_restore_page;
1114 + /* disable auto sleep */
1115 + ret = ytphy_modify_ext(phydev, YT8521_EXTREG_SLEEP_CONTROL1_REG,
1116 + YT8521_ESC1R_SLEEP_SW, 0);
1118 + goto err_restore_page;
1120 + /* enable RXC clock when no wire plug */
1121 + ret = ytphy_modify_ext(phydev, YT8521_CLOCK_GATING_REG,
1122 + YT8521_CGR_RX_CLK_EN, 0);
1124 + goto err_restore_page;
1127 + return phy_restore_page(phydev, old_page, ret);
1131 + * yt8521_prepare_fiber_features() - A small helper function that setup
1132 + * fiber's features.
1133 + * @phydev: a pointer to a &struct phy_device
1134 + * @dst: a pointer to store fiber's features
1136 +static void yt8521_prepare_fiber_features(struct phy_device *phydev,
1137 + unsigned long *dst)
1139 + linkmode_set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT, dst);
1140 + linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, dst);
1141 + linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, dst);
1142 + linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, dst);
1146 + * yt8521_fiber_setup_forced - configures/forces speed from @phydev
1147 + * @phydev: target phy_device struct
1149 + * NOTE:The caller must have taken the MDIO bus lock.
1151 + * returns 0 or negative errno code
1153 +static int yt8521_fiber_setup_forced(struct phy_device *phydev)
1158 + if (phydev->speed == SPEED_1000)
1159 + val = YTPHY_MCR_FIBER_1000BX;
1160 + else if (phydev->speed == SPEED_100)
1161 + val = YTPHY_MCR_FIBER_100FX;
1165 + ret = __phy_modify(phydev, MII_BMCR, BMCR_ANENABLE, 0);
1169 + /* disable Fiber auto sensing */
1170 + ret = ytphy_modify_ext(phydev, YT8521_LINK_TIMER_CFG2_REG,
1171 + YT8521_LTCR_EN_AUTOSEN, 0);
1175 + ret = ytphy_modify_ext(phydev, YTPHY_MISC_CONFIG_REG,
1176 + YTPHY_MCR_FIBER_SPEED_MASK, val);
1180 + return ytphy_modify_ext(phydev, YT8521_CHIP_CONFIG_REG,
1181 + YT8521_CCR_SW_RST, 0);
1185 + * ytphy_check_and_restart_aneg - Enable and restart auto-negotiation
1186 + * @phydev: target phy_device struct
1187 + * @restart: whether aneg restart is requested
1189 + * NOTE:The caller must have taken the MDIO bus lock.
1191 + * returns 0 or negative errno code
1193 +static int ytphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
1198 + /* Advertisement hasn't changed, but maybe aneg was never on to
1199 + * begin with? Or maybe phy was isolated?
1201 + ret = __phy_read(phydev, MII_BMCR);
1205 + if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
1208 + /* Enable and Restart Autonegotiation
1209 + * Don't isolate the PHY if we're negotiating
1212 + return __phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1213 + BMCR_ANENABLE | BMCR_ANRESTART);
1219 + * yt8521_fiber_config_aneg - restart auto-negotiation or write
1220 + * YTPHY_MISC_CONFIG_REG.
1221 + * @phydev: target phy_device struct
1223 + * NOTE:The caller must have taken the MDIO bus lock.
1225 + * returns 0 or negative errno code
1227 +static int yt8521_fiber_config_aneg(struct phy_device *phydev)
1229 + int err, changed = 0;
1233 + if (phydev->autoneg != AUTONEG_ENABLE)
1234 + return yt8521_fiber_setup_forced(phydev);
1236 + /* enable Fiber auto sensing */
1237 + err = ytphy_modify_ext(phydev, YT8521_LINK_TIMER_CFG2_REG,
1238 + 0, YT8521_LTCR_EN_AUTOSEN);
1242 + err = ytphy_modify_ext(phydev, YT8521_CHIP_CONFIG_REG,
1243 + YT8521_CCR_SW_RST, 0);
1247 + bmcr = __phy_read(phydev, MII_BMCR);
1251 + /* When it is coming from fiber forced mode, add bmcr power down
1252 + * and power up to let aneg work fine.
1254 + if (!(bmcr & BMCR_ANENABLE)) {
1255 + __phy_modify(phydev, MII_BMCR, 0, BMCR_PDOWN);
1256 + usleep_range(1000, 1100);
1257 + __phy_modify(phydev, MII_BMCR, BMCR_PDOWN, 0);
1260 + adv = linkmode_adv_to_mii_adv_x(phydev->advertising,
1261 + ETHTOOL_LINK_MODE_1000baseX_Full_BIT);
1263 + /* Setup fiber advertisement */
1264 + err = __phy_modify_changed(phydev, MII_ADVERTISE,
1265 + ADVERTISE_1000XHALF | ADVERTISE_1000XFULL |
1266 + ADVERTISE_1000XPAUSE |
1267 + ADVERTISE_1000XPSE_ASYM,
1275 + return ytphy_check_and_restart_aneg(phydev, changed);
1279 + * ytphy_setup_master_slave
1280 + * @phydev: target phy_device struct
1282 + * NOTE: The caller must have taken the MDIO bus lock.
1284 + * returns 0 or negative errno code
1286 +static int ytphy_setup_master_slave(struct phy_device *phydev)
1290 + if (!phydev->is_gigabit_capable)
1293 + switch (phydev->master_slave_set) {
1294 + case MASTER_SLAVE_CFG_MASTER_PREFERRED:
1295 + ctl |= CTL1000_PREFER_MASTER;
1297 + case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
1299 + case MASTER_SLAVE_CFG_MASTER_FORCE:
1300 + ctl |= CTL1000_AS_MASTER;
1302 + case MASTER_SLAVE_CFG_SLAVE_FORCE:
1303 + ctl |= CTL1000_ENABLE_MASTER;
1305 + case MASTER_SLAVE_CFG_UNKNOWN:
1306 + case MASTER_SLAVE_CFG_UNSUPPORTED:
1309 + phydev_warn(phydev, "Unsupported Master/Slave mode\n");
1310 + return -EOPNOTSUPP;
1313 + return __phy_modify_changed(phydev, MII_CTRL1000,
1314 + (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
1315 + CTL1000_PREFER_MASTER), ctl);
1319 + * ytphy_utp_config_advert - sanitize and advertise auto-negotiation parameters
1320 + * @phydev: target phy_device struct
1322 + * NOTE: Writes MII_ADVERTISE with the appropriate values,
1323 + * after sanitizing the values to make sure we only advertise
1324 + * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1325 + * hasn't changed, and > 0 if it has changed.
1326 + * The caller must have taken the MDIO bus lock.
1328 + * returns 0 or negative errno code
1330 +static int ytphy_utp_config_advert(struct phy_device *phydev)
1332 + int err, bmsr, changed = 0;
1335 + /* Only allow advertising what this PHY supports */
1336 + linkmode_and(phydev->advertising, phydev->advertising,
1337 + phydev->supported);
1339 + adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1341 + /* Setup standard advertisement */
1342 + err = __phy_modify_changed(phydev, MII_ADVERTISE,
1343 + ADVERTISE_ALL | ADVERTISE_100BASE4 |
1344 + ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1351 + bmsr = __phy_read(phydev, MII_BMSR);
1355 + /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1356 + * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1359 + if (!(bmsr & BMSR_ESTATEN))
1362 + adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
1364 + err = __phy_modify_changed(phydev, MII_CTRL1000,
1365 + ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1376 + * ytphy_utp_config_aneg - restart auto-negotiation or write BMCR
1377 + * @phydev: target phy_device struct
1378 + * @changed: whether autoneg is requested
1380 + * NOTE: If auto-negotiation is enabled, we configure the
1381 + * advertising, and then restart auto-negotiation. If it is not
1382 + * enabled, then we write the BMCR.
1383 + * The caller must have taken the MDIO bus lock.
1385 + * returns 0 or negative errno code
1387 +static int ytphy_utp_config_aneg(struct phy_device *phydev, bool changed)
1392 + err = ytphy_setup_master_slave(phydev);
1398 + if (phydev->autoneg != AUTONEG_ENABLE) {
1399 + /* configures/forces speed/duplex from @phydev */
1401 + ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
1403 + return __phy_modify(phydev, MII_BMCR, ~(BMCR_LOOPBACK |
1404 + BMCR_ISOLATE | BMCR_PDOWN), ctl);
1407 + err = ytphy_utp_config_advert(phydev);
1408 + if (err < 0) /* error */
1413 + return ytphy_check_and_restart_aneg(phydev, changed);
1417 + * yt8521_config_aneg_paged() - switch reg space then call genphy_config_aneg
1419 + * @phydev: a pointer to a &struct phy_device
1420 + * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
1423 + * returns 0 or negative errno code
1425 +static int yt8521_config_aneg_paged(struct phy_device *phydev, int page)
1427 + __ETHTOOL_DECLARE_LINK_MODE_MASK(fiber_supported);
1428 + struct yt8521_priv *priv = phydev->priv;
1432 + page &= YT8521_RSSR_SPACE_MASK;
1434 + old_page = phy_select_page(phydev, page);
1436 + goto err_restore_page;
1438 + /* If reg_page is YT8521_RSSR_TO_BE_ARBITRATED,
1439 + * phydev->advertising should be updated.
1441 + if (priv->reg_page == YT8521_RSSR_TO_BE_ARBITRATED) {
1442 + linkmode_zero(fiber_supported);
1443 + yt8521_prepare_fiber_features(phydev, fiber_supported);
1445 + /* prepare fiber_supported, then setup advertising. */
1446 + if (page == YT8521_RSSR_FIBER_SPACE) {
1447 + linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1449 + linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1451 + linkmode_and(phydev->advertising,
1452 + priv->combo_advertising, fiber_supported);
1454 + /* ETHTOOL_LINK_MODE_Autoneg_BIT is also used in utp */
1455 + linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1457 + linkmode_andnot(phydev->advertising,
1458 + priv->combo_advertising,
1463 + if (page == YT8521_RSSR_FIBER_SPACE)
1464 + ret = yt8521_fiber_config_aneg(phydev);
1466 + ret = ytphy_utp_config_aneg(phydev, false);
1469 + return phy_restore_page(phydev, old_page, ret);
1473 + * yt8521_config_aneg() - change reg space then call yt8521_config_aneg_paged
1474 + * @phydev: a pointer to a &struct phy_device
1476 + * returns 0 or negative errno code
1478 +static int yt8521_config_aneg(struct phy_device *phydev)
1480 + struct yt8521_priv *priv = phydev->priv;
1483 + if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
1484 + ret = yt8521_config_aneg_paged(phydev, priv->reg_page);
1488 + /* If reg_page is YT8521_RSSR_TO_BE_ARBITRATED,
1489 + * phydev->advertising need to be saved at first run.
1490 + * Because it contains the advertising which supported by both
1491 + * mac and yt8521(utp and fiber).
1493 + if (linkmode_empty(priv->combo_advertising)) {
1494 + linkmode_copy(priv->combo_advertising,
1495 + phydev->advertising);
1498 + ret = yt8521_config_aneg_paged(phydev, YT8521_RSSR_UTP_SPACE);
1502 + ret = yt8521_config_aneg_paged(phydev, YT8521_RSSR_FIBER_SPACE);
1506 + /* we don't known which will be link, so restore
1507 + * phydev->advertising as default value.
1509 + linkmode_copy(phydev->advertising, priv->combo_advertising);
1515 + * yt8521_aneg_done_paged() - determines the auto negotiation result of one
1517 + * @phydev: a pointer to a &struct phy_device
1518 + * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
1521 + * returns 0(no link)or 1(fiber or utp link) or negative errno code
1523 +static int yt8521_aneg_done_paged(struct phy_device *phydev, int page)
1529 + old_page = phy_select_page(phydev, page & YT8521_RSSR_SPACE_MASK);
1531 + goto err_restore_page;
1533 + ret = __phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
1535 + goto err_restore_page;
1537 + link = !!(ret & YTPHY_SSR_LINK);
1541 + return phy_restore_page(phydev, old_page, ret);
1545 + * yt8521_aneg_done() - determines the auto negotiation result
1546 + * @phydev: a pointer to a &struct phy_device
1548 + * returns 0(no link)or 1(fiber or utp link) or negative errno code
1550 +static int yt8521_aneg_done(struct phy_device *phydev)
1552 + struct yt8521_priv *priv = phydev->priv;
1553 + int link_fiber = 0;
1557 + if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
1558 + link = yt8521_aneg_done_paged(phydev, priv->reg_page);
1560 + link_utp = yt8521_aneg_done_paged(phydev,
1561 + YT8521_RSSR_UTP_SPACE);
1566 + link_fiber = yt8521_aneg_done_paged(phydev,
1567 + YT8521_RSSR_FIBER_SPACE);
1568 + if (link_fiber < 0)
1569 + return link_fiber;
1571 + link = link_fiber || link_utp;
1572 + phydev_info(phydev, "%s, link_fiber: %d, link_utp: %d\n",
1573 + __func__, link_fiber, link_utp);
1580 + * ytphy_utp_read_abilities - read PHY abilities from Clause 22 registers
1581 + * @phydev: target phy_device struct
1583 + * NOTE: Reads the PHY's abilities and populates
1584 + * phydev->supported accordingly.
1585 + * The caller must have taken the MDIO bus lock.
1587 + * returns 0 or negative errno code
1589 +static int ytphy_utp_read_abilities(struct phy_device *phydev)
1593 + linkmode_set_bit_array(phy_basic_ports_array,
1594 + ARRAY_SIZE(phy_basic_ports_array),
1595 + phydev->supported);
1597 + val = __phy_read(phydev, MII_BMSR);
1601 + linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
1602 + val & BMSR_ANEGCAPABLE);
1604 + linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
1605 + val & BMSR_100FULL);
1606 + linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
1607 + val & BMSR_100HALF);
1608 + linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
1609 + val & BMSR_10FULL);
1610 + linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
1611 + val & BMSR_10HALF);
1613 + if (val & BMSR_ESTATEN) {
1614 + val = __phy_read(phydev, MII_ESTATUS);
1618 + linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1619 + phydev->supported, val & ESTATUS_1000_TFULL);
1620 + linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1621 + phydev->supported, val & ESTATUS_1000_THALF);
1622 + linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1623 + phydev->supported, val & ESTATUS_1000_XFULL);
1630 + * yt8521_get_features_paged() - read supported link modes for one page
1631 + * @phydev: a pointer to a &struct phy_device
1632 + * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
1635 + * returns 0 or negative errno code
1637 +static int yt8521_get_features_paged(struct phy_device *phydev, int page)
1642 + page &= YT8521_RSSR_SPACE_MASK;
1643 + old_page = phy_select_page(phydev, page);
1645 + goto err_restore_page;
1647 + if (page == YT8521_RSSR_FIBER_SPACE) {
1648 + linkmode_zero(phydev->supported);
1649 + yt8521_prepare_fiber_features(phydev, phydev->supported);
1651 + ret = ytphy_utp_read_abilities(phydev);
1653 + goto err_restore_page;
1657 + return phy_restore_page(phydev, old_page, ret);
1661 + * yt8521_get_features - switch reg space then call yt8521_get_features_paged
1662 + * @phydev: target phy_device struct
1664 + * returns 0 or negative errno code
1666 +static int yt8521_get_features(struct phy_device *phydev)
1668 + struct yt8521_priv *priv = phydev->priv;
1671 + if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
1672 + ret = yt8521_get_features_paged(phydev, priv->reg_page);
1674 + ret = yt8521_get_features_paged(phydev,
1675 + YT8521_RSSR_UTP_SPACE);
1679 + /* add fiber's features to phydev->supported */
1680 + yt8521_prepare_fiber_features(phydev, phydev->supported);
1685 static struct phy_driver motorcomm_phy_drvs[] = {
1687 PHY_ID_MATCH_EXACT(PHY_ID_YT8511),
1688 @@ -121,16 +1733,35 @@ static struct phy_driver motorcomm_phy_d
1689 .read_page = yt8511_read_page,
1690 .write_page = yt8511_write_page,
1693 + PHY_ID_MATCH_EXACT(PHY_ID_YT8521),
1694 + .name = "YT8521 Gigabit Ethernet",
1695 + .get_features = yt8521_get_features,
1696 + .probe = yt8521_probe,
1697 + .read_page = yt8521_read_page,
1698 + .write_page = yt8521_write_page,
1699 + .get_wol = ytphy_get_wol,
1700 + .set_wol = ytphy_set_wol,
1701 + .config_aneg = yt8521_config_aneg,
1702 + .aneg_done = yt8521_aneg_done,
1703 + .config_init = yt8521_config_init,
1704 + .read_status = yt8521_read_status,
1705 + .soft_reset = yt8521_soft_reset,
1706 + .suspend = yt8521_suspend,
1707 + .resume = yt8521_resume,
1711 module_phy_driver(motorcomm_phy_drvs);
1713 -MODULE_DESCRIPTION("Motorcomm PHY driver");
1714 +MODULE_DESCRIPTION("Motorcomm 8511/8521 PHY driver");
1715 MODULE_AUTHOR("Peter Geis");
1716 +MODULE_AUTHOR("Frank");
1717 MODULE_LICENSE("GPL");
1719 static const struct mdio_device_id __maybe_unused motorcomm_tbl[] = {
1720 { PHY_ID_MATCH_EXACT(PHY_ID_YT8511) },
1721 + { PHY_ID_MATCH_EXACT(PHY_ID_YT8521) },