18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015-2017 Broadcom
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include "bcm-phy-lib.h"
78c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
88c2ecf20Sopenharmony_ci#include <linux/brcmphy.h>
98c2ecf20Sopenharmony_ci#include <linux/export.h>
108c2ecf20Sopenharmony_ci#include <linux/mdio.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/phy.h>
138c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
148c2ecf20Sopenharmony_ci#include <linux/ethtool_netlink.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define MII_BCM_CHANNEL_WIDTH     0x2000
178c2ecf20Sopenharmony_ci#define BCM_CL45VEN_EEE_ADV       0x3c
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciint __bcm_phy_write_exp(struct phy_device *phydev, u16 reg, u16 val)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	int rc;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	rc = __phy_write(phydev, MII_BCM54XX_EXP_SEL, reg);
248c2ecf20Sopenharmony_ci	if (rc < 0)
258c2ecf20Sopenharmony_ci		return rc;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	return __phy_write(phydev, MII_BCM54XX_EXP_DATA, val);
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__bcm_phy_write_exp);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciint bcm_phy_write_exp(struct phy_device *phydev, u16 reg, u16 val)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	int rc;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	phy_lock_mdio_bus(phydev);
368c2ecf20Sopenharmony_ci	rc = __bcm_phy_write_exp(phydev, reg, val);
378c2ecf20Sopenharmony_ci	phy_unlock_mdio_bus(phydev);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	return rc;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_write_exp);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciint __bcm_phy_read_exp(struct phy_device *phydev, u16 reg)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	int val;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	val = __phy_write(phydev, MII_BCM54XX_EXP_SEL, reg);
488c2ecf20Sopenharmony_ci	if (val < 0)
498c2ecf20Sopenharmony_ci		return val;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	val = __phy_read(phydev, MII_BCM54XX_EXP_DATA);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	/* Restore default value.  It's O.K. if this write fails. */
548c2ecf20Sopenharmony_ci	__phy_write(phydev, MII_BCM54XX_EXP_SEL, 0);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	return val;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__bcm_phy_read_exp);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciint bcm_phy_read_exp(struct phy_device *phydev, u16 reg)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	int rc;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	phy_lock_mdio_bus(phydev);
658c2ecf20Sopenharmony_ci	rc = __bcm_phy_read_exp(phydev, reg);
668c2ecf20Sopenharmony_ci	phy_unlock_mdio_bus(phydev);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	return rc;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_read_exp);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciint __bcm_phy_modify_exp(struct phy_device *phydev, u16 reg, u16 mask, u16 set)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	int new, ret;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	ret = __phy_write(phydev, MII_BCM54XX_EXP_SEL, reg);
778c2ecf20Sopenharmony_ci	if (ret < 0)
788c2ecf20Sopenharmony_ci		return ret;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	ret = __phy_read(phydev, MII_BCM54XX_EXP_DATA);
818c2ecf20Sopenharmony_ci	if (ret < 0)
828c2ecf20Sopenharmony_ci		return ret;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	new = (ret & ~mask) | set;
858c2ecf20Sopenharmony_ci	if (new == ret)
868c2ecf20Sopenharmony_ci		return 0;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return __phy_write(phydev, MII_BCM54XX_EXP_DATA, new);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__bcm_phy_modify_exp);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciint bcm_phy_modify_exp(struct phy_device *phydev, u16 reg, u16 mask, u16 set)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	int ret;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	phy_lock_mdio_bus(phydev);
978c2ecf20Sopenharmony_ci	ret = __bcm_phy_modify_exp(phydev, reg, mask, set);
988c2ecf20Sopenharmony_ci	phy_unlock_mdio_bus(phydev);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return ret;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_modify_exp);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciint bcm54xx_auxctl_read(struct phy_device *phydev, u16 regnum)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	/* The register must be written to both the Shadow Register Select and
1078c2ecf20Sopenharmony_ci	 * the Shadow Read Register Selector
1088c2ecf20Sopenharmony_ci	 */
1098c2ecf20Sopenharmony_ci	phy_write(phydev, MII_BCM54XX_AUX_CTL, MII_BCM54XX_AUXCTL_SHDWSEL_MASK |
1108c2ecf20Sopenharmony_ci		  regnum << MII_BCM54XX_AUXCTL_SHDWSEL_READ_SHIFT);
1118c2ecf20Sopenharmony_ci	return phy_read(phydev, MII_BCM54XX_AUX_CTL);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm54xx_auxctl_read);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciint bcm54xx_auxctl_write(struct phy_device *phydev, u16 regnum, u16 val)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	return phy_write(phydev, MII_BCM54XX_AUX_CTL, regnum | val);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ciEXPORT_SYMBOL(bcm54xx_auxctl_write);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ciint bcm_phy_write_misc(struct phy_device *phydev,
1228c2ecf20Sopenharmony_ci		       u16 reg, u16 chl, u16 val)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	int rc;
1258c2ecf20Sopenharmony_ci	int tmp;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	rc = phy_write(phydev, MII_BCM54XX_AUX_CTL,
1288c2ecf20Sopenharmony_ci		       MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
1298c2ecf20Sopenharmony_ci	if (rc < 0)
1308c2ecf20Sopenharmony_ci		return rc;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	tmp = phy_read(phydev, MII_BCM54XX_AUX_CTL);
1338c2ecf20Sopenharmony_ci	tmp |= MII_BCM54XX_AUXCTL_ACTL_SMDSP_ENA;
1348c2ecf20Sopenharmony_ci	rc = phy_write(phydev, MII_BCM54XX_AUX_CTL, tmp);
1358c2ecf20Sopenharmony_ci	if (rc < 0)
1368c2ecf20Sopenharmony_ci		return rc;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	tmp = (chl * MII_BCM_CHANNEL_WIDTH) | reg;
1398c2ecf20Sopenharmony_ci	rc = bcm_phy_write_exp(phydev, tmp, val);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	return rc;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_write_misc);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ciint bcm_phy_read_misc(struct phy_device *phydev,
1468c2ecf20Sopenharmony_ci		      u16 reg, u16 chl)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	int rc;
1498c2ecf20Sopenharmony_ci	int tmp;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	rc = phy_write(phydev, MII_BCM54XX_AUX_CTL,
1528c2ecf20Sopenharmony_ci		       MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
1538c2ecf20Sopenharmony_ci	if (rc < 0)
1548c2ecf20Sopenharmony_ci		return rc;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	tmp = phy_read(phydev, MII_BCM54XX_AUX_CTL);
1578c2ecf20Sopenharmony_ci	tmp |= MII_BCM54XX_AUXCTL_ACTL_SMDSP_ENA;
1588c2ecf20Sopenharmony_ci	rc = phy_write(phydev, MII_BCM54XX_AUX_CTL, tmp);
1598c2ecf20Sopenharmony_ci	if (rc < 0)
1608c2ecf20Sopenharmony_ci		return rc;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	tmp = (chl * MII_BCM_CHANNEL_WIDTH) | reg;
1638c2ecf20Sopenharmony_ci	rc = bcm_phy_read_exp(phydev, tmp);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return rc;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_read_misc);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciint bcm_phy_ack_intr(struct phy_device *phydev)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	int reg;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* Clear pending interrupts.  */
1748c2ecf20Sopenharmony_ci	reg = phy_read(phydev, MII_BCM54XX_ISR);
1758c2ecf20Sopenharmony_ci	if (reg < 0)
1768c2ecf20Sopenharmony_ci		return reg;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	return 0;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_ack_intr);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ciint bcm_phy_config_intr(struct phy_device *phydev)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	int reg;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	reg = phy_read(phydev, MII_BCM54XX_ECR);
1878c2ecf20Sopenharmony_ci	if (reg < 0)
1888c2ecf20Sopenharmony_ci		return reg;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
1918c2ecf20Sopenharmony_ci		reg &= ~MII_BCM54XX_ECR_IM;
1928c2ecf20Sopenharmony_ci	else
1938c2ecf20Sopenharmony_ci		reg |= MII_BCM54XX_ECR_IM;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	return phy_write(phydev, MII_BCM54XX_ECR, reg);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_config_intr);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ciint bcm_phy_read_shadow(struct phy_device *phydev, u16 shadow)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	phy_write(phydev, MII_BCM54XX_SHD, MII_BCM54XX_SHD_VAL(shadow));
2028c2ecf20Sopenharmony_ci	return MII_BCM54XX_SHD_DATA(phy_read(phydev, MII_BCM54XX_SHD));
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_read_shadow);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciint bcm_phy_write_shadow(struct phy_device *phydev, u16 shadow,
2078c2ecf20Sopenharmony_ci			 u16 val)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	return phy_write(phydev, MII_BCM54XX_SHD,
2108c2ecf20Sopenharmony_ci			 MII_BCM54XX_SHD_WRITE |
2118c2ecf20Sopenharmony_ci			 MII_BCM54XX_SHD_VAL(shadow) |
2128c2ecf20Sopenharmony_ci			 MII_BCM54XX_SHD_DATA(val));
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_write_shadow);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ciint __bcm_phy_read_rdb(struct phy_device *phydev, u16 rdb)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	int val;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	val = __phy_write(phydev, MII_BCM54XX_RDB_ADDR, rdb);
2218c2ecf20Sopenharmony_ci	if (val < 0)
2228c2ecf20Sopenharmony_ci		return val;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return __phy_read(phydev, MII_BCM54XX_RDB_DATA);
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__bcm_phy_read_rdb);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciint bcm_phy_read_rdb(struct phy_device *phydev, u16 rdb)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	int ret;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	phy_lock_mdio_bus(phydev);
2338c2ecf20Sopenharmony_ci	ret = __bcm_phy_read_rdb(phydev, rdb);
2348c2ecf20Sopenharmony_ci	phy_unlock_mdio_bus(phydev);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	return ret;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_read_rdb);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ciint __bcm_phy_write_rdb(struct phy_device *phydev, u16 rdb, u16 val)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	int ret;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	ret = __phy_write(phydev, MII_BCM54XX_RDB_ADDR, rdb);
2458c2ecf20Sopenharmony_ci	if (ret < 0)
2468c2ecf20Sopenharmony_ci		return ret;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	return __phy_write(phydev, MII_BCM54XX_RDB_DATA, val);
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__bcm_phy_write_rdb);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ciint bcm_phy_write_rdb(struct phy_device *phydev, u16 rdb, u16 val)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	int ret;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	phy_lock_mdio_bus(phydev);
2578c2ecf20Sopenharmony_ci	ret = __bcm_phy_write_rdb(phydev, rdb, val);
2588c2ecf20Sopenharmony_ci	phy_unlock_mdio_bus(phydev);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	return ret;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_write_rdb);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ciint __bcm_phy_modify_rdb(struct phy_device *phydev, u16 rdb, u16 mask, u16 set)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	int new, ret;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	ret = __phy_write(phydev, MII_BCM54XX_RDB_ADDR, rdb);
2698c2ecf20Sopenharmony_ci	if (ret < 0)
2708c2ecf20Sopenharmony_ci		return ret;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	ret = __phy_read(phydev, MII_BCM54XX_RDB_DATA);
2738c2ecf20Sopenharmony_ci	if (ret < 0)
2748c2ecf20Sopenharmony_ci		return ret;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	new = (ret & ~mask) | set;
2778c2ecf20Sopenharmony_ci	if (new == ret)
2788c2ecf20Sopenharmony_ci		return 0;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return __phy_write(phydev, MII_BCM54XX_RDB_DATA, new);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__bcm_phy_modify_rdb);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ciint bcm_phy_modify_rdb(struct phy_device *phydev, u16 rdb, u16 mask, u16 set)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	int ret;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	phy_lock_mdio_bus(phydev);
2898c2ecf20Sopenharmony_ci	ret = __bcm_phy_modify_rdb(phydev, rdb, mask, set);
2908c2ecf20Sopenharmony_ci	phy_unlock_mdio_bus(phydev);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return ret;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_modify_rdb);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ciint bcm_phy_enable_apd(struct phy_device *phydev, bool dll_pwr_down)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	int val;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	if (dll_pwr_down) {
3018c2ecf20Sopenharmony_ci		val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_SCR3);
3028c2ecf20Sopenharmony_ci		if (val < 0)
3038c2ecf20Sopenharmony_ci			return val;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci		val |= BCM54XX_SHD_SCR3_DLLAPD_DIS;
3068c2ecf20Sopenharmony_ci		bcm_phy_write_shadow(phydev, BCM54XX_SHD_SCR3, val);
3078c2ecf20Sopenharmony_ci	}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_APD);
3108c2ecf20Sopenharmony_ci	if (val < 0)
3118c2ecf20Sopenharmony_ci		return val;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* Clear APD bits */
3148c2ecf20Sopenharmony_ci	val &= BCM_APD_CLR_MASK;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_ENABLE)
3178c2ecf20Sopenharmony_ci		val |= BCM54XX_SHD_APD_EN;
3188c2ecf20Sopenharmony_ci	else
3198c2ecf20Sopenharmony_ci		val |= BCM_NO_ANEG_APD_EN;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	/* Enable energy detect single link pulse for easy wakeup */
3228c2ecf20Sopenharmony_ci	val |= BCM_APD_SINGLELP_EN;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	/* Enable Auto Power-Down (APD) for the PHY */
3258c2ecf20Sopenharmony_ci	return bcm_phy_write_shadow(phydev, BCM54XX_SHD_APD, val);
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_enable_apd);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ciint bcm_phy_set_eee(struct phy_device *phydev, bool enable)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	int val, mask = 0;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/* Enable EEE at PHY level */
3348c2ecf20Sopenharmony_ci	val = phy_read_mmd(phydev, MDIO_MMD_AN, BRCM_CL45VEN_EEE_CONTROL);
3358c2ecf20Sopenharmony_ci	if (val < 0)
3368c2ecf20Sopenharmony_ci		return val;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	if (enable)
3398c2ecf20Sopenharmony_ci		val |= LPI_FEATURE_EN | LPI_FEATURE_EN_DIG1000X;
3408c2ecf20Sopenharmony_ci	else
3418c2ecf20Sopenharmony_ci		val &= ~(LPI_FEATURE_EN | LPI_FEATURE_EN_DIG1000X);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_AN, BRCM_CL45VEN_EEE_CONTROL, (u32)val);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/* Advertise EEE */
3468c2ecf20Sopenharmony_ci	val = phy_read_mmd(phydev, MDIO_MMD_AN, BCM_CL45VEN_EEE_ADV);
3478c2ecf20Sopenharmony_ci	if (val < 0)
3488c2ecf20Sopenharmony_ci		return val;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
3518c2ecf20Sopenharmony_ci			      phydev->supported))
3528c2ecf20Sopenharmony_ci		mask |= MDIO_EEE_1000T;
3538c2ecf20Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
3548c2ecf20Sopenharmony_ci			      phydev->supported))
3558c2ecf20Sopenharmony_ci		mask |= MDIO_EEE_100TX;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	if (enable)
3588c2ecf20Sopenharmony_ci		val |= mask;
3598c2ecf20Sopenharmony_ci	else
3608c2ecf20Sopenharmony_ci		val &= ~mask;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_AN, BCM_CL45VEN_EEE_ADV, (u32)val);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	return 0;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_set_eee);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ciint bcm_phy_downshift_get(struct phy_device *phydev, u8 *count)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	int val;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	val = bcm54xx_auxctl_read(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
3738c2ecf20Sopenharmony_ci	if (val < 0)
3748c2ecf20Sopenharmony_ci		return val;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	/* Check if wirespeed is enabled or not */
3778c2ecf20Sopenharmony_ci	if (!(val & MII_BCM54XX_AUXCTL_SHDWSEL_MISC_WIRESPEED_EN)) {
3788c2ecf20Sopenharmony_ci		*count = DOWNSHIFT_DEV_DISABLE;
3798c2ecf20Sopenharmony_ci		return 0;
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_SCR2);
3838c2ecf20Sopenharmony_ci	if (val < 0)
3848c2ecf20Sopenharmony_ci		return val;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	/* Downgrade after one link attempt */
3878c2ecf20Sopenharmony_ci	if (val & BCM54XX_SHD_SCR2_WSPD_RTRY_DIS) {
3888c2ecf20Sopenharmony_ci		*count = 1;
3898c2ecf20Sopenharmony_ci	} else {
3908c2ecf20Sopenharmony_ci		/* Downgrade after configured retry count */
3918c2ecf20Sopenharmony_ci		val >>= BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_SHIFT;
3928c2ecf20Sopenharmony_ci		val &= BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_MASK;
3938c2ecf20Sopenharmony_ci		*count = val + BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_OFFSET;
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	return 0;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_downshift_get);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ciint bcm_phy_downshift_set(struct phy_device *phydev, u8 count)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	int val = 0, ret = 0;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	/* Range check the number given */
4058c2ecf20Sopenharmony_ci	if (count - BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_OFFSET >
4068c2ecf20Sopenharmony_ci	    BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_MASK &&
4078c2ecf20Sopenharmony_ci	    count != DOWNSHIFT_DEV_DEFAULT_COUNT) {
4088c2ecf20Sopenharmony_ci		return -ERANGE;
4098c2ecf20Sopenharmony_ci	}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	val = bcm54xx_auxctl_read(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
4128c2ecf20Sopenharmony_ci	if (val < 0)
4138c2ecf20Sopenharmony_ci		return val;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	/* Se the write enable bit */
4168c2ecf20Sopenharmony_ci	val |= MII_BCM54XX_AUXCTL_MISC_WREN;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	if (count == DOWNSHIFT_DEV_DISABLE) {
4198c2ecf20Sopenharmony_ci		val &= ~MII_BCM54XX_AUXCTL_SHDWSEL_MISC_WIRESPEED_EN;
4208c2ecf20Sopenharmony_ci		return bcm54xx_auxctl_write(phydev,
4218c2ecf20Sopenharmony_ci					    MII_BCM54XX_AUXCTL_SHDWSEL_MISC,
4228c2ecf20Sopenharmony_ci					    val);
4238c2ecf20Sopenharmony_ci	} else {
4248c2ecf20Sopenharmony_ci		val |= MII_BCM54XX_AUXCTL_SHDWSEL_MISC_WIRESPEED_EN;
4258c2ecf20Sopenharmony_ci		ret = bcm54xx_auxctl_write(phydev,
4268c2ecf20Sopenharmony_ci					   MII_BCM54XX_AUXCTL_SHDWSEL_MISC,
4278c2ecf20Sopenharmony_ci					   val);
4288c2ecf20Sopenharmony_ci		if (ret < 0)
4298c2ecf20Sopenharmony_ci			return ret;
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_SCR2);
4338c2ecf20Sopenharmony_ci	val &= ~(BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_MASK <<
4348c2ecf20Sopenharmony_ci		 BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_SHIFT |
4358c2ecf20Sopenharmony_ci		 BCM54XX_SHD_SCR2_WSPD_RTRY_DIS);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	switch (count) {
4388c2ecf20Sopenharmony_ci	case 1:
4398c2ecf20Sopenharmony_ci		val |= BCM54XX_SHD_SCR2_WSPD_RTRY_DIS;
4408c2ecf20Sopenharmony_ci		break;
4418c2ecf20Sopenharmony_ci	case DOWNSHIFT_DEV_DEFAULT_COUNT:
4428c2ecf20Sopenharmony_ci		val |= 1 << BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_SHIFT;
4438c2ecf20Sopenharmony_ci		break;
4448c2ecf20Sopenharmony_ci	default:
4458c2ecf20Sopenharmony_ci		val |= (count - BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_OFFSET) <<
4468c2ecf20Sopenharmony_ci			BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_SHIFT;
4478c2ecf20Sopenharmony_ci		break;
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	return bcm_phy_write_shadow(phydev, BCM54XX_SHD_SCR2, val);
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_downshift_set);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistruct bcm_phy_hw_stat {
4558c2ecf20Sopenharmony_ci	const char *string;
4568c2ecf20Sopenharmony_ci	u8 reg;
4578c2ecf20Sopenharmony_ci	u8 shift;
4588c2ecf20Sopenharmony_ci	u8 bits;
4598c2ecf20Sopenharmony_ci};
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci/* Counters freeze at either 0xffff or 0xff, better than nothing */
4628c2ecf20Sopenharmony_cistatic const struct bcm_phy_hw_stat bcm_phy_hw_stats[] = {
4638c2ecf20Sopenharmony_ci	{ "phy_receive_errors", MII_BRCM_CORE_BASE12, 0, 16 },
4648c2ecf20Sopenharmony_ci	{ "phy_serdes_ber_errors", MII_BRCM_CORE_BASE13, 8, 8 },
4658c2ecf20Sopenharmony_ci	{ "phy_false_carrier_sense_errors", MII_BRCM_CORE_BASE13, 0, 8 },
4668c2ecf20Sopenharmony_ci	{ "phy_local_rcvr_nok", MII_BRCM_CORE_BASE14, 8, 8 },
4678c2ecf20Sopenharmony_ci	{ "phy_remote_rcv_nok", MII_BRCM_CORE_BASE14, 0, 8 },
4688c2ecf20Sopenharmony_ci};
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ciint bcm_phy_get_sset_count(struct phy_device *phydev)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	return ARRAY_SIZE(bcm_phy_hw_stats);
4738c2ecf20Sopenharmony_ci}
4748c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_get_sset_count);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_civoid bcm_phy_get_strings(struct phy_device *phydev, u8 *data)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	unsigned int i;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(bcm_phy_hw_stats); i++)
4818c2ecf20Sopenharmony_ci		strlcpy(data + i * ETH_GSTRING_LEN,
4828c2ecf20Sopenharmony_ci			bcm_phy_hw_stats[i].string, ETH_GSTRING_LEN);
4838c2ecf20Sopenharmony_ci}
4848c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_get_strings);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci/* Caller is supposed to provide appropriate storage for the library code to
4878c2ecf20Sopenharmony_ci * access the shadow copy
4888c2ecf20Sopenharmony_ci */
4898c2ecf20Sopenharmony_cistatic u64 bcm_phy_get_stat(struct phy_device *phydev, u64 *shadow,
4908c2ecf20Sopenharmony_ci			    unsigned int i)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	struct bcm_phy_hw_stat stat = bcm_phy_hw_stats[i];
4938c2ecf20Sopenharmony_ci	int val;
4948c2ecf20Sopenharmony_ci	u64 ret;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	val = phy_read(phydev, stat.reg);
4978c2ecf20Sopenharmony_ci	if (val < 0) {
4988c2ecf20Sopenharmony_ci		ret = U64_MAX;
4998c2ecf20Sopenharmony_ci	} else {
5008c2ecf20Sopenharmony_ci		val >>= stat.shift;
5018c2ecf20Sopenharmony_ci		val = val & ((1 << stat.bits) - 1);
5028c2ecf20Sopenharmony_ci		shadow[i] += val;
5038c2ecf20Sopenharmony_ci		ret = shadow[i];
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	return ret;
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_civoid bcm_phy_get_stats(struct phy_device *phydev, u64 *shadow,
5108c2ecf20Sopenharmony_ci		       struct ethtool_stats *stats, u64 *data)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	unsigned int i;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(bcm_phy_hw_stats); i++)
5158c2ecf20Sopenharmony_ci		data[i] = bcm_phy_get_stat(phydev, shadow, i);
5168c2ecf20Sopenharmony_ci}
5178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_get_stats);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_civoid bcm_phy_r_rc_cal_reset(struct phy_device *phydev)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	/* Reset R_CAL/RC_CAL Engine */
5228c2ecf20Sopenharmony_ci	bcm_phy_write_exp_sel(phydev, 0x00b0, 0x0010);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	/* Disable Reset R_AL/RC_CAL Engine */
5258c2ecf20Sopenharmony_ci	bcm_phy_write_exp_sel(phydev, 0x00b0, 0x0000);
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_r_rc_cal_reset);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ciint bcm_phy_28nm_a0b0_afe_config_init(struct phy_device *phydev)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	/* Increase VCO range to prevent unlocking problem of PLL at low
5328c2ecf20Sopenharmony_ci	 * temp
5338c2ecf20Sopenharmony_ci	 */
5348c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, PLL_PLLCTRL_1, 0x0048);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	/* Change Ki to 011 */
5378c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, PLL_PLLCTRL_2, 0x021b);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* Disable loading of TVCO buffer to bandgap, set bandgap trim
5408c2ecf20Sopenharmony_ci	 * to 111
5418c2ecf20Sopenharmony_ci	 */
5428c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, PLL_PLLCTRL_4, 0x0e20);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	/* Adjust bias current trim by -3 */
5458c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, DSP_TAP10, 0x690b);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	/* Switch to CORE_BASE1E */
5488c2ecf20Sopenharmony_ci	phy_write(phydev, MII_BRCM_CORE_BASE1E, 0xd);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	bcm_phy_r_rc_cal_reset(phydev);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/* write AFE_RXCONFIG_0 */
5538c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, AFE_RXCONFIG_0, 0xeb19);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	/* write AFE_RXCONFIG_1 */
5568c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, AFE_RXCONFIG_1, 0x9a3f);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	/* write AFE_RX_LP_COUNTER */
5598c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, AFE_RX_LP_COUNTER, 0x7fc0);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	/* write AFE_HPF_TRIM_OTHERS */
5628c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, AFE_HPF_TRIM_OTHERS, 0x000b);
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/* write AFTE_TX_CONFIG */
5658c2ecf20Sopenharmony_ci	bcm_phy_write_misc(phydev, AFE_TX_CONFIG, 0x0800);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	return 0;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_28nm_a0b0_afe_config_init);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ciint bcm_phy_enable_jumbo(struct phy_device *phydev)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	int ret;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	ret = bcm54xx_auxctl_read(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_AUXCTL);
5768c2ecf20Sopenharmony_ci	if (ret < 0)
5778c2ecf20Sopenharmony_ci		return ret;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	/* Enable extended length packet reception */
5808c2ecf20Sopenharmony_ci	ret = bcm54xx_auxctl_write(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_AUXCTL,
5818c2ecf20Sopenharmony_ci				   ret | MII_BCM54XX_AUXCTL_ACTL_EXT_PKT_LEN);
5828c2ecf20Sopenharmony_ci	if (ret < 0)
5838c2ecf20Sopenharmony_ci		return ret;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	/* Enable the elastic FIFO for raising the transmission limit from
5868c2ecf20Sopenharmony_ci	 * 4.5KB to 10KB, at the expense of an additional 16 ns in propagation
5878c2ecf20Sopenharmony_ci	 * latency.
5888c2ecf20Sopenharmony_ci	 */
5898c2ecf20Sopenharmony_ci	return phy_set_bits(phydev, MII_BCM54XX_ECR, MII_BCM54XX_ECR_FIFOE);
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_enable_jumbo);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_cistatic int __bcm_phy_enable_rdb_access(struct phy_device *phydev)
5948c2ecf20Sopenharmony_ci{
5958c2ecf20Sopenharmony_ci	return __bcm_phy_write_exp(phydev, BCM54XX_EXP_REG7E, 0);
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_cistatic int __bcm_phy_enable_legacy_access(struct phy_device *phydev)
5998c2ecf20Sopenharmony_ci{
6008c2ecf20Sopenharmony_ci	return __bcm_phy_write_rdb(phydev, BCM54XX_RDB_REG0087,
6018c2ecf20Sopenharmony_ci				   BCM54XX_ACCESS_MODE_LEGACY_EN);
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_cistatic int _bcm_phy_cable_test_start(struct phy_device *phydev, bool is_rdb)
6058c2ecf20Sopenharmony_ci{
6068c2ecf20Sopenharmony_ci	u16 mask, set;
6078c2ecf20Sopenharmony_ci	int ret;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	/* Auto-negotiation must be enabled for cable diagnostics to work, but
6108c2ecf20Sopenharmony_ci	 * don't advertise any capabilities.
6118c2ecf20Sopenharmony_ci	 */
6128c2ecf20Sopenharmony_ci	phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
6138c2ecf20Sopenharmony_ci	phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
6148c2ecf20Sopenharmony_ci	phy_write(phydev, MII_CTRL1000, 0);
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	phy_lock_mdio_bus(phydev);
6178c2ecf20Sopenharmony_ci	if (is_rdb) {
6188c2ecf20Sopenharmony_ci		ret = __bcm_phy_enable_legacy_access(phydev);
6198c2ecf20Sopenharmony_ci		if (ret)
6208c2ecf20Sopenharmony_ci			goto out;
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	mask = BCM54XX_ECD_CTRL_CROSS_SHORT_DIS | BCM54XX_ECD_CTRL_UNIT_MASK;
6248c2ecf20Sopenharmony_ci	set = BCM54XX_ECD_CTRL_RUN | BCM54XX_ECD_CTRL_BREAK_LINK |
6258c2ecf20Sopenharmony_ci	      FIELD_PREP(BCM54XX_ECD_CTRL_UNIT_MASK,
6268c2ecf20Sopenharmony_ci			 BCM54XX_ECD_CTRL_UNIT_CM);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	ret = __bcm_phy_modify_exp(phydev, BCM54XX_EXP_ECD_CTRL, mask, set);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ciout:
6318c2ecf20Sopenharmony_ci	/* re-enable the RDB access even if there was an error */
6328c2ecf20Sopenharmony_ci	if (is_rdb)
6338c2ecf20Sopenharmony_ci		ret = __bcm_phy_enable_rdb_access(phydev) ? : ret;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	phy_unlock_mdio_bus(phydev);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	return ret;
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_cistatic int bcm_phy_cable_test_report_trans(int result)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	switch (result) {
6438c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_OK:
6448c2ecf20Sopenharmony_ci		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
6458c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_OPEN:
6468c2ecf20Sopenharmony_ci		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
6478c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_SAME_SHORT:
6488c2ecf20Sopenharmony_ci		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
6498c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_CROSS_SHORT:
6508c2ecf20Sopenharmony_ci		return ETHTOOL_A_CABLE_RESULT_CODE_CROSS_SHORT;
6518c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_INVALID:
6528c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_BUSY:
6538c2ecf20Sopenharmony_ci	default:
6548c2ecf20Sopenharmony_ci		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
6558c2ecf20Sopenharmony_ci	}
6568c2ecf20Sopenharmony_ci}
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic bool bcm_phy_distance_valid(int result)
6598c2ecf20Sopenharmony_ci{
6608c2ecf20Sopenharmony_ci	switch (result) {
6618c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_OPEN:
6628c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_SAME_SHORT:
6638c2ecf20Sopenharmony_ci	case BCM54XX_ECD_FAULT_TYPE_CROSS_SHORT:
6648c2ecf20Sopenharmony_ci		return true;
6658c2ecf20Sopenharmony_ci	}
6668c2ecf20Sopenharmony_ci	return false;
6678c2ecf20Sopenharmony_ci}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_cistatic int bcm_phy_report_length(struct phy_device *phydev, int pair)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	int val;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	val = __bcm_phy_read_exp(phydev,
6748c2ecf20Sopenharmony_ci				 BCM54XX_EXP_ECD_PAIR_A_LENGTH_RESULTS + pair);
6758c2ecf20Sopenharmony_ci	if (val < 0)
6768c2ecf20Sopenharmony_ci		return val;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (val == BCM54XX_ECD_LENGTH_RESULTS_INVALID)
6798c2ecf20Sopenharmony_ci		return 0;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	ethnl_cable_test_fault_length(phydev, pair, val);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	return 0;
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_cistatic int _bcm_phy_cable_test_get_status(struct phy_device *phydev,
6878c2ecf20Sopenharmony_ci					  bool *finished, bool is_rdb)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	int pair_a, pair_b, pair_c, pair_d, ret;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	*finished = false;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	phy_lock_mdio_bus(phydev);
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	if (is_rdb) {
6968c2ecf20Sopenharmony_ci		ret = __bcm_phy_enable_legacy_access(phydev);
6978c2ecf20Sopenharmony_ci		if (ret)
6988c2ecf20Sopenharmony_ci			goto out;
6998c2ecf20Sopenharmony_ci	}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	ret = __bcm_phy_read_exp(phydev, BCM54XX_EXP_ECD_CTRL);
7028c2ecf20Sopenharmony_ci	if (ret < 0)
7038c2ecf20Sopenharmony_ci		goto out;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	if (ret & BCM54XX_ECD_CTRL_IN_PROGRESS) {
7068c2ecf20Sopenharmony_ci		ret = 0;
7078c2ecf20Sopenharmony_ci		goto out;
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	ret = __bcm_phy_read_exp(phydev, BCM54XX_EXP_ECD_FAULT_TYPE);
7118c2ecf20Sopenharmony_ci	if (ret < 0)
7128c2ecf20Sopenharmony_ci		goto out;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	pair_a = FIELD_GET(BCM54XX_ECD_FAULT_TYPE_PAIR_A_MASK, ret);
7158c2ecf20Sopenharmony_ci	pair_b = FIELD_GET(BCM54XX_ECD_FAULT_TYPE_PAIR_B_MASK, ret);
7168c2ecf20Sopenharmony_ci	pair_c = FIELD_GET(BCM54XX_ECD_FAULT_TYPE_PAIR_C_MASK, ret);
7178c2ecf20Sopenharmony_ci	pair_d = FIELD_GET(BCM54XX_ECD_FAULT_TYPE_PAIR_D_MASK, ret);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
7208c2ecf20Sopenharmony_ci				bcm_phy_cable_test_report_trans(pair_a));
7218c2ecf20Sopenharmony_ci	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_B,
7228c2ecf20Sopenharmony_ci				bcm_phy_cable_test_report_trans(pair_b));
7238c2ecf20Sopenharmony_ci	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_C,
7248c2ecf20Sopenharmony_ci				bcm_phy_cable_test_report_trans(pair_c));
7258c2ecf20Sopenharmony_ci	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_D,
7268c2ecf20Sopenharmony_ci				bcm_phy_cable_test_report_trans(pair_d));
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	if (bcm_phy_distance_valid(pair_a))
7298c2ecf20Sopenharmony_ci		bcm_phy_report_length(phydev, 0);
7308c2ecf20Sopenharmony_ci	if (bcm_phy_distance_valid(pair_b))
7318c2ecf20Sopenharmony_ci		bcm_phy_report_length(phydev, 1);
7328c2ecf20Sopenharmony_ci	if (bcm_phy_distance_valid(pair_c))
7338c2ecf20Sopenharmony_ci		bcm_phy_report_length(phydev, 2);
7348c2ecf20Sopenharmony_ci	if (bcm_phy_distance_valid(pair_d))
7358c2ecf20Sopenharmony_ci		bcm_phy_report_length(phydev, 3);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	ret = 0;
7388c2ecf20Sopenharmony_ci	*finished = true;
7398c2ecf20Sopenharmony_ciout:
7408c2ecf20Sopenharmony_ci	/* re-enable the RDB access even if there was an error */
7418c2ecf20Sopenharmony_ci	if (is_rdb)
7428c2ecf20Sopenharmony_ci		ret = __bcm_phy_enable_rdb_access(phydev) ? : ret;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	phy_unlock_mdio_bus(phydev);
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	return ret;
7478c2ecf20Sopenharmony_ci}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ciint bcm_phy_cable_test_start(struct phy_device *phydev)
7508c2ecf20Sopenharmony_ci{
7518c2ecf20Sopenharmony_ci	return _bcm_phy_cable_test_start(phydev, false);
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_cable_test_start);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ciint bcm_phy_cable_test_get_status(struct phy_device *phydev, bool *finished)
7568c2ecf20Sopenharmony_ci{
7578c2ecf20Sopenharmony_ci	return _bcm_phy_cable_test_get_status(phydev, finished, false);
7588c2ecf20Sopenharmony_ci}
7598c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_cable_test_get_status);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci/* We assume that all PHYs which support RDB access can be switched to legacy
7628c2ecf20Sopenharmony_ci * mode. If, in the future, this is not true anymore, we have to re-implement
7638c2ecf20Sopenharmony_ci * this with RDB access.
7648c2ecf20Sopenharmony_ci */
7658c2ecf20Sopenharmony_ciint bcm_phy_cable_test_start_rdb(struct phy_device *phydev)
7668c2ecf20Sopenharmony_ci{
7678c2ecf20Sopenharmony_ci	return _bcm_phy_cable_test_start(phydev, true);
7688c2ecf20Sopenharmony_ci}
7698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_cable_test_start_rdb);
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ciint bcm_phy_cable_test_get_status_rdb(struct phy_device *phydev,
7728c2ecf20Sopenharmony_ci				      bool *finished)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	return _bcm_phy_cable_test_get_status(phydev, finished, true);
7758c2ecf20Sopenharmony_ci}
7768c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcm_phy_cable_test_get_status_rdb);
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Broadcom PHY Library");
7798c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
7808c2ecf20Sopenharmony_ciMODULE_AUTHOR("Broadcom Corporation");
781