18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Driver for the Texas Instruments DP83869 PHY
38c2ecf20Sopenharmony_ci * Copyright (C) 2019 Texas Instruments Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
78c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/mii.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/of.h>
128c2ecf20Sopenharmony_ci#include <linux/phy.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <dt-bindings/net/ti-dp83869.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define DP83869_PHY_ID		0x2000a0f1
198c2ecf20Sopenharmony_ci#define DP83869_DEVADDR		0x1f
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define MII_DP83869_PHYCTRL	0x10
228c2ecf20Sopenharmony_ci#define MII_DP83869_MICR	0x12
238c2ecf20Sopenharmony_ci#define MII_DP83869_ISR		0x13
248c2ecf20Sopenharmony_ci#define DP83869_CFG2		0x14
258c2ecf20Sopenharmony_ci#define DP83869_CTRL		0x1f
268c2ecf20Sopenharmony_ci#define DP83869_CFG4		0x1e
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Extended Registers */
298c2ecf20Sopenharmony_ci#define DP83869_GEN_CFG3        0x0031
308c2ecf20Sopenharmony_ci#define DP83869_RGMIICTL	0x0032
318c2ecf20Sopenharmony_ci#define DP83869_STRAP_STS1	0x006e
328c2ecf20Sopenharmony_ci#define DP83869_RGMIIDCTL	0x0086
338c2ecf20Sopenharmony_ci#define DP83869_RXFCFG		0x0134
348c2ecf20Sopenharmony_ci#define DP83869_RXFPMD1		0x0136
358c2ecf20Sopenharmony_ci#define DP83869_RXFPMD2		0x0137
368c2ecf20Sopenharmony_ci#define DP83869_RXFPMD3		0x0138
378c2ecf20Sopenharmony_ci#define DP83869_RXFSOP1		0x0139
388c2ecf20Sopenharmony_ci#define DP83869_RXFSOP2		0x013A
398c2ecf20Sopenharmony_ci#define DP83869_RXFSOP3		0x013B
408c2ecf20Sopenharmony_ci#define DP83869_IO_MUX_CFG	0x0170
418c2ecf20Sopenharmony_ci#define DP83869_OP_MODE		0x01df
428c2ecf20Sopenharmony_ci#define DP83869_FX_CTRL		0x0c00
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define DP83869_SW_RESET	BIT(15)
458c2ecf20Sopenharmony_ci#define DP83869_SW_RESTART	BIT(14)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* MICR Interrupt bits */
488c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_AN_ERR_INT_EN		BIT(15)
498c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_SPEED_CHNG_INT_EN	BIT(14)
508c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_DUP_MODE_CHNG_INT_EN	BIT(13)
518c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_PAGE_RXD_INT_EN	BIT(12)
528c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_AUTONEG_COMP_INT_EN	BIT(11)
538c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_LINK_STS_CHNG_INT_EN	BIT(10)
548c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_FALSE_CARRIER_INT_EN	BIT(8)
558c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_SLEEP_MODE_CHNG_INT_EN	BIT(4)
568c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_WOL_INT_EN		BIT(3)
578c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_XGMII_ERR_INT_EN	BIT(2)
588c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_POL_CHNG_INT_EN	BIT(1)
598c2ecf20Sopenharmony_ci#define MII_DP83869_MICR_JABBER_INT_EN		BIT(0)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define MII_DP83869_BMCR_DEFAULT	(BMCR_ANENABLE | \
628c2ecf20Sopenharmony_ci					 BMCR_FULLDPLX | \
638c2ecf20Sopenharmony_ci					 BMCR_SPEED1000)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define MII_DP83869_FIBER_ADVERTISE    (ADVERTISED_FIBRE | \
668c2ecf20Sopenharmony_ci					ADVERTISED_Pause | \
678c2ecf20Sopenharmony_ci					ADVERTISED_Asym_Pause)
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* This is the same bit mask as the BMCR so re-use the BMCR default */
708c2ecf20Sopenharmony_ci#define DP83869_FX_CTRL_DEFAULT	MII_DP83869_BMCR_DEFAULT
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* CFG1 bits */
738c2ecf20Sopenharmony_ci#define DP83869_CFG1_DEFAULT	(ADVERTISE_1000HALF | \
748c2ecf20Sopenharmony_ci				 ADVERTISE_1000FULL | \
758c2ecf20Sopenharmony_ci				 CTL1000_AS_MASTER)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/* RGMIICTL bits */
788c2ecf20Sopenharmony_ci#define DP83869_RGMII_TX_CLK_DELAY_EN		BIT(1)
798c2ecf20Sopenharmony_ci#define DP83869_RGMII_RX_CLK_DELAY_EN		BIT(0)
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/* RGMIIDCTL */
828c2ecf20Sopenharmony_ci#define DP83869_RGMII_CLK_DELAY_SHIFT		4
838c2ecf20Sopenharmony_ci#define DP83869_CLK_DELAY_DEF			7
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* STRAP_STS1 bits */
868c2ecf20Sopenharmony_ci#define DP83869_STRAP_OP_MODE_MASK		GENMASK(2, 0)
878c2ecf20Sopenharmony_ci#define DP83869_STRAP_STS1_RESERVED		BIT(11)
888c2ecf20Sopenharmony_ci#define DP83869_STRAP_MIRROR_ENABLED           BIT(12)
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* PHYCTRL bits */
918c2ecf20Sopenharmony_ci#define DP83869_RX_FIFO_SHIFT	12
928c2ecf20Sopenharmony_ci#define DP83869_TX_FIFO_SHIFT	14
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* PHY_CTRL lower bytes 0x48 are declared as reserved */
958c2ecf20Sopenharmony_ci#define DP83869_PHY_CTRL_DEFAULT	0x48
968c2ecf20Sopenharmony_ci#define DP83869_PHYCR_FIFO_DEPTH_MASK	GENMASK(15, 12)
978c2ecf20Sopenharmony_ci#define DP83869_PHYCR_RESERVED_MASK	BIT(11)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* IO_MUX_CFG bits */
1008c2ecf20Sopenharmony_ci#define DP83869_IO_MUX_CFG_IO_IMPEDANCE_CTRL	0x1f
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define DP83869_IO_MUX_CFG_IO_IMPEDANCE_MAX	0x0
1038c2ecf20Sopenharmony_ci#define DP83869_IO_MUX_CFG_IO_IMPEDANCE_MIN	0x1f
1048c2ecf20Sopenharmony_ci#define DP83869_IO_MUX_CFG_CLK_O_SEL_MASK	(0x1f << 8)
1058c2ecf20Sopenharmony_ci#define DP83869_IO_MUX_CFG_CLK_O_SEL_SHIFT	8
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/* CFG3 bits */
1088c2ecf20Sopenharmony_ci#define DP83869_CFG3_PORT_MIRROR_EN              BIT(0)
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* CFG4 bits */
1118c2ecf20Sopenharmony_ci#define DP83869_INT_OE	BIT(7)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* OP MODE */
1148c2ecf20Sopenharmony_ci#define DP83869_OP_MODE_MII			BIT(5)
1158c2ecf20Sopenharmony_ci#define DP83869_SGMII_RGMII_BRIDGE		BIT(6)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* RXFCFG bits*/
1188c2ecf20Sopenharmony_ci#define DP83869_WOL_MAGIC_EN		BIT(0)
1198c2ecf20Sopenharmony_ci#define DP83869_WOL_PATTERN_EN		BIT(1)
1208c2ecf20Sopenharmony_ci#define DP83869_WOL_BCAST_EN		BIT(2)
1218c2ecf20Sopenharmony_ci#define DP83869_WOL_UCAST_EN		BIT(4)
1228c2ecf20Sopenharmony_ci#define DP83869_WOL_SEC_EN		BIT(5)
1238c2ecf20Sopenharmony_ci#define DP83869_WOL_ENH_MAC		BIT(7)
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* CFG2 bits */
1268c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_EN		(BIT(8) | BIT(9))
1278c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_ATTEMPT_MASK	(BIT(10) | BIT(11))
1288c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_1_COUNT_VAL	0
1298c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_2_COUNT_VAL	1
1308c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_4_COUNT_VAL	2
1318c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_8_COUNT_VAL	3
1328c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_1_COUNT	1
1338c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_2_COUNT	2
1348c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_4_COUNT	4
1358c2ecf20Sopenharmony_ci#define DP83869_DOWNSHIFT_8_COUNT	8
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cienum {
1388c2ecf20Sopenharmony_ci	DP83869_PORT_MIRRORING_KEEP,
1398c2ecf20Sopenharmony_ci	DP83869_PORT_MIRRORING_EN,
1408c2ecf20Sopenharmony_ci	DP83869_PORT_MIRRORING_DIS,
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistruct dp83869_private {
1448c2ecf20Sopenharmony_ci	int tx_fifo_depth;
1458c2ecf20Sopenharmony_ci	int rx_fifo_depth;
1468c2ecf20Sopenharmony_ci	s32 rx_int_delay;
1478c2ecf20Sopenharmony_ci	s32 tx_int_delay;
1488c2ecf20Sopenharmony_ci	int io_impedance;
1498c2ecf20Sopenharmony_ci	int port_mirroring;
1508c2ecf20Sopenharmony_ci	bool rxctrl_strap_quirk;
1518c2ecf20Sopenharmony_ci	int clk_output_sel;
1528c2ecf20Sopenharmony_ci	int mode;
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic int dp83869_read_status(struct phy_device *phydev)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct dp83869_private *dp83869 = phydev->priv;
1588c2ecf20Sopenharmony_ci	int ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	ret = genphy_read_status(phydev);
1618c2ecf20Sopenharmony_ci	if (ret)
1628c2ecf20Sopenharmony_ci		return ret;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, phydev->supported)) {
1658c2ecf20Sopenharmony_ci		if (phydev->link) {
1668c2ecf20Sopenharmony_ci			if (dp83869->mode == DP83869_RGMII_100_BASE)
1678c2ecf20Sopenharmony_ci				phydev->speed = SPEED_100;
1688c2ecf20Sopenharmony_ci		} else {
1698c2ecf20Sopenharmony_ci			phydev->speed = SPEED_UNKNOWN;
1708c2ecf20Sopenharmony_ci			phydev->duplex = DUPLEX_UNKNOWN;
1718c2ecf20Sopenharmony_ci		}
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	return 0;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic int dp83869_ack_interrupt(struct phy_device *phydev)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	int err = phy_read(phydev, MII_DP83869_ISR);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (err < 0)
1828c2ecf20Sopenharmony_ci		return err;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return 0;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int dp83869_config_intr(struct phy_device *phydev)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	int micr_status = 0;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1928c2ecf20Sopenharmony_ci		micr_status = phy_read(phydev, MII_DP83869_MICR);
1938c2ecf20Sopenharmony_ci		if (micr_status < 0)
1948c2ecf20Sopenharmony_ci			return micr_status;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		micr_status |=
1978c2ecf20Sopenharmony_ci			(MII_DP83869_MICR_AN_ERR_INT_EN |
1988c2ecf20Sopenharmony_ci			MII_DP83869_MICR_SPEED_CHNG_INT_EN |
1998c2ecf20Sopenharmony_ci			MII_DP83869_MICR_AUTONEG_COMP_INT_EN |
2008c2ecf20Sopenharmony_ci			MII_DP83869_MICR_LINK_STS_CHNG_INT_EN |
2018c2ecf20Sopenharmony_ci			MII_DP83869_MICR_DUP_MODE_CHNG_INT_EN |
2028c2ecf20Sopenharmony_ci			MII_DP83869_MICR_SLEEP_MODE_CHNG_INT_EN);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		return phy_write(phydev, MII_DP83869_MICR, micr_status);
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return phy_write(phydev, MII_DP83869_MICR, micr_status);
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic int dp83869_set_wol(struct phy_device *phydev,
2118c2ecf20Sopenharmony_ci			   struct ethtool_wolinfo *wol)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	struct net_device *ndev = phydev->attached_dev;
2148c2ecf20Sopenharmony_ci	int val_rxcfg, val_micr;
2158c2ecf20Sopenharmony_ci	u8 *mac;
2168c2ecf20Sopenharmony_ci	int ret;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	val_rxcfg = phy_read_mmd(phydev, DP83869_DEVADDR, DP83869_RXFCFG);
2198c2ecf20Sopenharmony_ci	if (val_rxcfg < 0)
2208c2ecf20Sopenharmony_ci		return val_rxcfg;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	val_micr = phy_read(phydev, MII_DP83869_MICR);
2238c2ecf20Sopenharmony_ci	if (val_micr < 0)
2248c2ecf20Sopenharmony_ci		return val_micr;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	if (wol->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_UCAST |
2278c2ecf20Sopenharmony_ci			    WAKE_BCAST)) {
2288c2ecf20Sopenharmony_ci		val_rxcfg |= DP83869_WOL_ENH_MAC;
2298c2ecf20Sopenharmony_ci		val_micr |= MII_DP83869_MICR_WOL_INT_EN;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci		if (wol->wolopts & WAKE_MAGIC ||
2328c2ecf20Sopenharmony_ci		    wol->wolopts & WAKE_MAGICSECURE) {
2338c2ecf20Sopenharmony_ci			mac = (u8 *)ndev->dev_addr;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci			if (!is_valid_ether_addr(mac))
2368c2ecf20Sopenharmony_ci				return -EINVAL;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci			ret = phy_write_mmd(phydev, DP83869_DEVADDR,
2398c2ecf20Sopenharmony_ci					    DP83869_RXFPMD1,
2408c2ecf20Sopenharmony_ci					    mac[1] << 8 | mac[0]);
2418c2ecf20Sopenharmony_ci			if (ret)
2428c2ecf20Sopenharmony_ci				return ret;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci			ret = phy_write_mmd(phydev, DP83869_DEVADDR,
2458c2ecf20Sopenharmony_ci					    DP83869_RXFPMD2,
2468c2ecf20Sopenharmony_ci					    mac[3] << 8 | mac[2]);
2478c2ecf20Sopenharmony_ci			if (ret)
2488c2ecf20Sopenharmony_ci				return ret;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci			ret = phy_write_mmd(phydev, DP83869_DEVADDR,
2518c2ecf20Sopenharmony_ci					    DP83869_RXFPMD3,
2528c2ecf20Sopenharmony_ci					    mac[5] << 8 | mac[4]);
2538c2ecf20Sopenharmony_ci			if (ret)
2548c2ecf20Sopenharmony_ci				return ret;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci			val_rxcfg |= DP83869_WOL_MAGIC_EN;
2578c2ecf20Sopenharmony_ci		} else {
2588c2ecf20Sopenharmony_ci			val_rxcfg &= ~DP83869_WOL_MAGIC_EN;
2598c2ecf20Sopenharmony_ci		}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci		if (wol->wolopts & WAKE_MAGICSECURE) {
2628c2ecf20Sopenharmony_ci			ret = phy_write_mmd(phydev, DP83869_DEVADDR,
2638c2ecf20Sopenharmony_ci					    DP83869_RXFSOP1,
2648c2ecf20Sopenharmony_ci					    (wol->sopass[1] << 8) | wol->sopass[0]);
2658c2ecf20Sopenharmony_ci			if (ret)
2668c2ecf20Sopenharmony_ci				return ret;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci			ret = phy_write_mmd(phydev, DP83869_DEVADDR,
2698c2ecf20Sopenharmony_ci					    DP83869_RXFSOP2,
2708c2ecf20Sopenharmony_ci					    (wol->sopass[3] << 8) | wol->sopass[2]);
2718c2ecf20Sopenharmony_ci			if (ret)
2728c2ecf20Sopenharmony_ci				return ret;
2738c2ecf20Sopenharmony_ci			ret = phy_write_mmd(phydev, DP83869_DEVADDR,
2748c2ecf20Sopenharmony_ci					    DP83869_RXFSOP3,
2758c2ecf20Sopenharmony_ci					    (wol->sopass[5] << 8) | wol->sopass[4]);
2768c2ecf20Sopenharmony_ci			if (ret)
2778c2ecf20Sopenharmony_ci				return ret;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci			val_rxcfg |= DP83869_WOL_SEC_EN;
2808c2ecf20Sopenharmony_ci		} else {
2818c2ecf20Sopenharmony_ci			val_rxcfg &= ~DP83869_WOL_SEC_EN;
2828c2ecf20Sopenharmony_ci		}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci		if (wol->wolopts & WAKE_UCAST)
2858c2ecf20Sopenharmony_ci			val_rxcfg |= DP83869_WOL_UCAST_EN;
2868c2ecf20Sopenharmony_ci		else
2878c2ecf20Sopenharmony_ci			val_rxcfg &= ~DP83869_WOL_UCAST_EN;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci		if (wol->wolopts & WAKE_BCAST)
2908c2ecf20Sopenharmony_ci			val_rxcfg |= DP83869_WOL_BCAST_EN;
2918c2ecf20Sopenharmony_ci		else
2928c2ecf20Sopenharmony_ci			val_rxcfg &= ~DP83869_WOL_BCAST_EN;
2938c2ecf20Sopenharmony_ci	} else {
2948c2ecf20Sopenharmony_ci		val_rxcfg &= ~DP83869_WOL_ENH_MAC;
2958c2ecf20Sopenharmony_ci		val_micr &= ~MII_DP83869_MICR_WOL_INT_EN;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	ret = phy_write_mmd(phydev, DP83869_DEVADDR, DP83869_RXFCFG, val_rxcfg);
2998c2ecf20Sopenharmony_ci	if (ret)
3008c2ecf20Sopenharmony_ci		return ret;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	return phy_write(phydev, MII_DP83869_MICR, val_micr);
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic void dp83869_get_wol(struct phy_device *phydev,
3068c2ecf20Sopenharmony_ci			    struct ethtool_wolinfo *wol)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	int value, sopass_val;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	wol->supported = (WAKE_UCAST | WAKE_BCAST | WAKE_MAGIC |
3118c2ecf20Sopenharmony_ci			WAKE_MAGICSECURE);
3128c2ecf20Sopenharmony_ci	wol->wolopts = 0;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	value = phy_read_mmd(phydev, DP83869_DEVADDR, DP83869_RXFCFG);
3158c2ecf20Sopenharmony_ci	if (value < 0) {
3168c2ecf20Sopenharmony_ci		phydev_err(phydev, "Failed to read RX CFG\n");
3178c2ecf20Sopenharmony_ci		return;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (value & DP83869_WOL_UCAST_EN)
3218c2ecf20Sopenharmony_ci		wol->wolopts |= WAKE_UCAST;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (value & DP83869_WOL_BCAST_EN)
3248c2ecf20Sopenharmony_ci		wol->wolopts |= WAKE_BCAST;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	if (value & DP83869_WOL_MAGIC_EN)
3278c2ecf20Sopenharmony_ci		wol->wolopts |= WAKE_MAGIC;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (value & DP83869_WOL_SEC_EN) {
3308c2ecf20Sopenharmony_ci		sopass_val = phy_read_mmd(phydev, DP83869_DEVADDR,
3318c2ecf20Sopenharmony_ci					  DP83869_RXFSOP1);
3328c2ecf20Sopenharmony_ci		if (sopass_val < 0) {
3338c2ecf20Sopenharmony_ci			phydev_err(phydev, "Failed to read RX SOP 1\n");
3348c2ecf20Sopenharmony_ci			return;
3358c2ecf20Sopenharmony_ci		}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		wol->sopass[0] = (sopass_val & 0xff);
3388c2ecf20Sopenharmony_ci		wol->sopass[1] = (sopass_val >> 8);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci		sopass_val = phy_read_mmd(phydev, DP83869_DEVADDR,
3418c2ecf20Sopenharmony_ci					  DP83869_RXFSOP2);
3428c2ecf20Sopenharmony_ci		if (sopass_val < 0) {
3438c2ecf20Sopenharmony_ci			phydev_err(phydev, "Failed to read RX SOP 2\n");
3448c2ecf20Sopenharmony_ci			return;
3458c2ecf20Sopenharmony_ci		}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci		wol->sopass[2] = (sopass_val & 0xff);
3488c2ecf20Sopenharmony_ci		wol->sopass[3] = (sopass_val >> 8);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci		sopass_val = phy_read_mmd(phydev, DP83869_DEVADDR,
3518c2ecf20Sopenharmony_ci					  DP83869_RXFSOP3);
3528c2ecf20Sopenharmony_ci		if (sopass_val < 0) {
3538c2ecf20Sopenharmony_ci			phydev_err(phydev, "Failed to read RX SOP 3\n");
3548c2ecf20Sopenharmony_ci			return;
3558c2ecf20Sopenharmony_ci		}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci		wol->sopass[4] = (sopass_val & 0xff);
3588c2ecf20Sopenharmony_ci		wol->sopass[5] = (sopass_val >> 8);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci		wol->wolopts |= WAKE_MAGICSECURE;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (!(value & DP83869_WOL_ENH_MAC))
3648c2ecf20Sopenharmony_ci		wol->wolopts = 0;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistatic int dp83869_get_downshift(struct phy_device *phydev, u8 *data)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	int val, cnt, enable, count;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	val = phy_read(phydev, DP83869_CFG2);
3728c2ecf20Sopenharmony_ci	if (val < 0)
3738c2ecf20Sopenharmony_ci		return val;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	enable = FIELD_GET(DP83869_DOWNSHIFT_EN, val);
3768c2ecf20Sopenharmony_ci	cnt = FIELD_GET(DP83869_DOWNSHIFT_ATTEMPT_MASK, val);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	switch (cnt) {
3798c2ecf20Sopenharmony_ci	case DP83869_DOWNSHIFT_1_COUNT_VAL:
3808c2ecf20Sopenharmony_ci		count = DP83869_DOWNSHIFT_1_COUNT;
3818c2ecf20Sopenharmony_ci		break;
3828c2ecf20Sopenharmony_ci	case DP83869_DOWNSHIFT_2_COUNT_VAL:
3838c2ecf20Sopenharmony_ci		count = DP83869_DOWNSHIFT_2_COUNT;
3848c2ecf20Sopenharmony_ci		break;
3858c2ecf20Sopenharmony_ci	case DP83869_DOWNSHIFT_4_COUNT_VAL:
3868c2ecf20Sopenharmony_ci		count = DP83869_DOWNSHIFT_4_COUNT;
3878c2ecf20Sopenharmony_ci		break;
3888c2ecf20Sopenharmony_ci	case DP83869_DOWNSHIFT_8_COUNT_VAL:
3898c2ecf20Sopenharmony_ci		count = DP83869_DOWNSHIFT_8_COUNT;
3908c2ecf20Sopenharmony_ci		break;
3918c2ecf20Sopenharmony_ci	default:
3928c2ecf20Sopenharmony_ci		return -EINVAL;
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	*data = enable ? count : DOWNSHIFT_DEV_DISABLE;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	return 0;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic int dp83869_set_downshift(struct phy_device *phydev, u8 cnt)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	int val, count;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	if (cnt > DP83869_DOWNSHIFT_8_COUNT)
4058c2ecf20Sopenharmony_ci		return -EINVAL;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	if (!cnt)
4088c2ecf20Sopenharmony_ci		return phy_clear_bits(phydev, DP83869_CFG2,
4098c2ecf20Sopenharmony_ci				      DP83869_DOWNSHIFT_EN);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	switch (cnt) {
4128c2ecf20Sopenharmony_ci	case DP83869_DOWNSHIFT_1_COUNT:
4138c2ecf20Sopenharmony_ci		count = DP83869_DOWNSHIFT_1_COUNT_VAL;
4148c2ecf20Sopenharmony_ci		break;
4158c2ecf20Sopenharmony_ci	case DP83869_DOWNSHIFT_2_COUNT:
4168c2ecf20Sopenharmony_ci		count = DP83869_DOWNSHIFT_2_COUNT_VAL;
4178c2ecf20Sopenharmony_ci		break;
4188c2ecf20Sopenharmony_ci	case DP83869_DOWNSHIFT_4_COUNT:
4198c2ecf20Sopenharmony_ci		count = DP83869_DOWNSHIFT_4_COUNT_VAL;
4208c2ecf20Sopenharmony_ci		break;
4218c2ecf20Sopenharmony_ci	case DP83869_DOWNSHIFT_8_COUNT:
4228c2ecf20Sopenharmony_ci		count = DP83869_DOWNSHIFT_8_COUNT_VAL;
4238c2ecf20Sopenharmony_ci		break;
4248c2ecf20Sopenharmony_ci	default:
4258c2ecf20Sopenharmony_ci		phydev_err(phydev,
4268c2ecf20Sopenharmony_ci			   "Downshift count must be 1, 2, 4 or 8\n");
4278c2ecf20Sopenharmony_ci		return -EINVAL;
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	val = DP83869_DOWNSHIFT_EN;
4318c2ecf20Sopenharmony_ci	val |= FIELD_PREP(DP83869_DOWNSHIFT_ATTEMPT_MASK, count);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	return phy_modify(phydev, DP83869_CFG2,
4348c2ecf20Sopenharmony_ci			  DP83869_DOWNSHIFT_EN | DP83869_DOWNSHIFT_ATTEMPT_MASK,
4358c2ecf20Sopenharmony_ci			  val);
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistatic int dp83869_get_tunable(struct phy_device *phydev,
4398c2ecf20Sopenharmony_ci			       struct ethtool_tunable *tuna, void *data)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	switch (tuna->id) {
4428c2ecf20Sopenharmony_ci	case ETHTOOL_PHY_DOWNSHIFT:
4438c2ecf20Sopenharmony_ci		return dp83869_get_downshift(phydev, data);
4448c2ecf20Sopenharmony_ci	default:
4458c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic int dp83869_set_tunable(struct phy_device *phydev,
4508c2ecf20Sopenharmony_ci			       struct ethtool_tunable *tuna, const void *data)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	switch (tuna->id) {
4538c2ecf20Sopenharmony_ci	case ETHTOOL_PHY_DOWNSHIFT:
4548c2ecf20Sopenharmony_ci		return dp83869_set_downshift(phydev, *(const u8 *)data);
4558c2ecf20Sopenharmony_ci	default:
4568c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cistatic int dp83869_config_port_mirroring(struct phy_device *phydev)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	struct dp83869_private *dp83869 = phydev->priv;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	if (dp83869->port_mirroring == DP83869_PORT_MIRRORING_EN)
4658c2ecf20Sopenharmony_ci		return phy_set_bits_mmd(phydev, DP83869_DEVADDR,
4668c2ecf20Sopenharmony_ci					DP83869_GEN_CFG3,
4678c2ecf20Sopenharmony_ci					DP83869_CFG3_PORT_MIRROR_EN);
4688c2ecf20Sopenharmony_ci	else
4698c2ecf20Sopenharmony_ci		return phy_clear_bits_mmd(phydev, DP83869_DEVADDR,
4708c2ecf20Sopenharmony_ci					  DP83869_GEN_CFG3,
4718c2ecf20Sopenharmony_ci					  DP83869_CFG3_PORT_MIRROR_EN);
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic int dp83869_set_strapped_mode(struct phy_device *phydev)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	struct dp83869_private *dp83869 = phydev->priv;
4778c2ecf20Sopenharmony_ci	int val;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	val = phy_read_mmd(phydev, DP83869_DEVADDR, DP83869_STRAP_STS1);
4808c2ecf20Sopenharmony_ci	if (val < 0)
4818c2ecf20Sopenharmony_ci		return val;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	dp83869->mode = val & DP83869_STRAP_OP_MODE_MASK;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	return 0;
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_OF_MDIO)
4898c2ecf20Sopenharmony_cistatic const int dp83869_internal_delay[] = {250, 500, 750, 1000, 1250, 1500,
4908c2ecf20Sopenharmony_ci					     1750, 2000, 2250, 2500, 2750, 3000,
4918c2ecf20Sopenharmony_ci					     3250, 3500, 3750, 4000};
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_cistatic int dp83869_of_init(struct phy_device *phydev)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	struct dp83869_private *dp83869 = phydev->priv;
4968c2ecf20Sopenharmony_ci	struct device *dev = &phydev->mdio.dev;
4978c2ecf20Sopenharmony_ci	struct device_node *of_node = dev->of_node;
4988c2ecf20Sopenharmony_ci	int delay_size = ARRAY_SIZE(dp83869_internal_delay);
4998c2ecf20Sopenharmony_ci	int ret;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	if (!of_node)
5028c2ecf20Sopenharmony_ci		return -ENODEV;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	dp83869->io_impedance = -EINVAL;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* Optional configuration */
5078c2ecf20Sopenharmony_ci	ret = of_property_read_u32(of_node, "ti,clk-output-sel",
5088c2ecf20Sopenharmony_ci				   &dp83869->clk_output_sel);
5098c2ecf20Sopenharmony_ci	if (ret || dp83869->clk_output_sel > DP83869_CLK_O_SEL_REF_CLK)
5108c2ecf20Sopenharmony_ci		dp83869->clk_output_sel = DP83869_CLK_O_SEL_REF_CLK;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	ret = of_property_read_u32(of_node, "ti,op-mode", &dp83869->mode);
5138c2ecf20Sopenharmony_ci	if (ret == 0) {
5148c2ecf20Sopenharmony_ci		if (dp83869->mode < DP83869_RGMII_COPPER_ETHERNET ||
5158c2ecf20Sopenharmony_ci		    dp83869->mode > DP83869_SGMII_COPPER_ETHERNET)
5168c2ecf20Sopenharmony_ci			return -EINVAL;
5178c2ecf20Sopenharmony_ci	} else {
5188c2ecf20Sopenharmony_ci		ret = dp83869_set_strapped_mode(phydev);
5198c2ecf20Sopenharmony_ci		if (ret)
5208c2ecf20Sopenharmony_ci			return ret;
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	if (of_property_read_bool(of_node, "ti,max-output-impedance"))
5248c2ecf20Sopenharmony_ci		dp83869->io_impedance = DP83869_IO_MUX_CFG_IO_IMPEDANCE_MAX;
5258c2ecf20Sopenharmony_ci	else if (of_property_read_bool(of_node, "ti,min-output-impedance"))
5268c2ecf20Sopenharmony_ci		dp83869->io_impedance = DP83869_IO_MUX_CFG_IO_IMPEDANCE_MIN;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	if (of_property_read_bool(of_node, "enet-phy-lane-swap")) {
5298c2ecf20Sopenharmony_ci		dp83869->port_mirroring = DP83869_PORT_MIRRORING_EN;
5308c2ecf20Sopenharmony_ci	} else {
5318c2ecf20Sopenharmony_ci		/* If the lane swap is not in the DT then check the straps */
5328c2ecf20Sopenharmony_ci		ret = phy_read_mmd(phydev, DP83869_DEVADDR, DP83869_STRAP_STS1);
5338c2ecf20Sopenharmony_ci		if (ret < 0)
5348c2ecf20Sopenharmony_ci			return ret;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci		if (ret & DP83869_STRAP_MIRROR_ENABLED)
5378c2ecf20Sopenharmony_ci			dp83869->port_mirroring = DP83869_PORT_MIRRORING_EN;
5388c2ecf20Sopenharmony_ci		else
5398c2ecf20Sopenharmony_ci			dp83869->port_mirroring = DP83869_PORT_MIRRORING_DIS;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci		ret = 0;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	if (of_property_read_u32(of_node, "rx-fifo-depth",
5458c2ecf20Sopenharmony_ci				 &dp83869->rx_fifo_depth))
5468c2ecf20Sopenharmony_ci		dp83869->rx_fifo_depth = DP83869_PHYCR_FIFO_DEPTH_4_B_NIB;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	if (of_property_read_u32(of_node, "tx-fifo-depth",
5498c2ecf20Sopenharmony_ci				 &dp83869->tx_fifo_depth))
5508c2ecf20Sopenharmony_ci		dp83869->tx_fifo_depth = DP83869_PHYCR_FIFO_DEPTH_4_B_NIB;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	dp83869->rx_int_delay = phy_get_internal_delay(phydev, dev,
5538c2ecf20Sopenharmony_ci						       &dp83869_internal_delay[0],
5548c2ecf20Sopenharmony_ci						       delay_size, true);
5558c2ecf20Sopenharmony_ci	if (dp83869->rx_int_delay < 0)
5568c2ecf20Sopenharmony_ci		dp83869->rx_int_delay = DP83869_CLK_DELAY_DEF;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	dp83869->tx_int_delay = phy_get_internal_delay(phydev, dev,
5598c2ecf20Sopenharmony_ci						       &dp83869_internal_delay[0],
5608c2ecf20Sopenharmony_ci						       delay_size, false);
5618c2ecf20Sopenharmony_ci	if (dp83869->tx_int_delay < 0)
5628c2ecf20Sopenharmony_ci		dp83869->tx_int_delay = DP83869_CLK_DELAY_DEF;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	return ret;
5658c2ecf20Sopenharmony_ci}
5668c2ecf20Sopenharmony_ci#else
5678c2ecf20Sopenharmony_cistatic int dp83869_of_init(struct phy_device *phydev)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	return dp83869_set_strapped_mode(phydev);
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci#endif /* CONFIG_OF_MDIO */
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic int dp83869_configure_rgmii(struct phy_device *phydev,
5748c2ecf20Sopenharmony_ci				   struct dp83869_private *dp83869)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	int ret = 0, val;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	if (phy_interface_is_rgmii(phydev)) {
5798c2ecf20Sopenharmony_ci		val = phy_read(phydev, MII_DP83869_PHYCTRL);
5808c2ecf20Sopenharmony_ci		if (val < 0)
5818c2ecf20Sopenharmony_ci			return val;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci		val &= ~DP83869_PHYCR_FIFO_DEPTH_MASK;
5848c2ecf20Sopenharmony_ci		val |= (dp83869->tx_fifo_depth << DP83869_TX_FIFO_SHIFT);
5858c2ecf20Sopenharmony_ci		val |= (dp83869->rx_fifo_depth << DP83869_RX_FIFO_SHIFT);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci		ret = phy_write(phydev, MII_DP83869_PHYCTRL, val);
5888c2ecf20Sopenharmony_ci		if (ret)
5898c2ecf20Sopenharmony_ci			return ret;
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	if (dp83869->io_impedance >= 0)
5938c2ecf20Sopenharmony_ci		ret = phy_modify_mmd(phydev, DP83869_DEVADDR,
5948c2ecf20Sopenharmony_ci				     DP83869_IO_MUX_CFG,
5958c2ecf20Sopenharmony_ci				     DP83869_IO_MUX_CFG_IO_IMPEDANCE_CTRL,
5968c2ecf20Sopenharmony_ci				     dp83869->io_impedance &
5978c2ecf20Sopenharmony_ci				     DP83869_IO_MUX_CFG_IO_IMPEDANCE_CTRL);
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	return ret;
6008c2ecf20Sopenharmony_ci}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic int dp83869_configure_fiber(struct phy_device *phydev,
6038c2ecf20Sopenharmony_ci				   struct dp83869_private *dp83869)
6048c2ecf20Sopenharmony_ci{
6058c2ecf20Sopenharmony_ci	int bmcr;
6068c2ecf20Sopenharmony_ci	int ret;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	/* Only allow advertising what this PHY supports */
6098c2ecf20Sopenharmony_ci	linkmode_and(phydev->advertising, phydev->advertising,
6108c2ecf20Sopenharmony_ci		     phydev->supported);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, phydev->supported);
6138c2ecf20Sopenharmony_ci	linkmode_set_bit(ADVERTISED_FIBRE, phydev->advertising);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	if (dp83869->mode == DP83869_RGMII_1000_BASE) {
6168c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
6178c2ecf20Sopenharmony_ci				 phydev->supported);
6188c2ecf20Sopenharmony_ci	} else {
6198c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT,
6208c2ecf20Sopenharmony_ci				 phydev->supported);
6218c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseFX_Half_BIT,
6228c2ecf20Sopenharmony_ci				 phydev->supported);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci		/* Auto neg is not supported in 100base FX mode */
6258c2ecf20Sopenharmony_ci		bmcr = phy_read(phydev, MII_BMCR);
6268c2ecf20Sopenharmony_ci		if (bmcr < 0)
6278c2ecf20Sopenharmony_ci			return bmcr;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci		phydev->autoneg = AUTONEG_DISABLE;
6308c2ecf20Sopenharmony_ci		linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported);
6318c2ecf20Sopenharmony_ci		linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->advertising);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci		if (bmcr & BMCR_ANENABLE) {
6348c2ecf20Sopenharmony_ci			ret =  phy_modify(phydev, MII_BMCR, BMCR_ANENABLE, 0);
6358c2ecf20Sopenharmony_ci			if (ret < 0)
6368c2ecf20Sopenharmony_ci				return ret;
6378c2ecf20Sopenharmony_ci		}
6388c2ecf20Sopenharmony_ci	}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	/* Update advertising from supported */
6418c2ecf20Sopenharmony_ci	linkmode_or(phydev->advertising, phydev->advertising,
6428c2ecf20Sopenharmony_ci		    phydev->supported);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	return 0;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic int dp83869_configure_mode(struct phy_device *phydev,
6488c2ecf20Sopenharmony_ci				  struct dp83869_private *dp83869)
6498c2ecf20Sopenharmony_ci{
6508c2ecf20Sopenharmony_ci	int phy_ctrl_val;
6518c2ecf20Sopenharmony_ci	int ret;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	if (dp83869->mode < DP83869_RGMII_COPPER_ETHERNET ||
6548c2ecf20Sopenharmony_ci	    dp83869->mode > DP83869_SGMII_COPPER_ETHERNET)
6558c2ecf20Sopenharmony_ci		return -EINVAL;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	/* Below init sequence for each operational mode is defined in
6588c2ecf20Sopenharmony_ci	 * section 9.4.8 of the datasheet.
6598c2ecf20Sopenharmony_ci	 */
6608c2ecf20Sopenharmony_ci	ret = phy_write_mmd(phydev, DP83869_DEVADDR, DP83869_OP_MODE,
6618c2ecf20Sopenharmony_ci			    dp83869->mode);
6628c2ecf20Sopenharmony_ci	if (ret)
6638c2ecf20Sopenharmony_ci		return ret;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	ret = phy_write(phydev, MII_BMCR, MII_DP83869_BMCR_DEFAULT);
6668c2ecf20Sopenharmony_ci	if (ret)
6678c2ecf20Sopenharmony_ci		return ret;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	phy_ctrl_val = (dp83869->rx_fifo_depth << DP83869_RX_FIFO_SHIFT |
6708c2ecf20Sopenharmony_ci			dp83869->tx_fifo_depth << DP83869_TX_FIFO_SHIFT |
6718c2ecf20Sopenharmony_ci			DP83869_PHY_CTRL_DEFAULT);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	switch (dp83869->mode) {
6748c2ecf20Sopenharmony_ci	case DP83869_RGMII_COPPER_ETHERNET:
6758c2ecf20Sopenharmony_ci		ret = phy_write(phydev, MII_DP83869_PHYCTRL,
6768c2ecf20Sopenharmony_ci				phy_ctrl_val);
6778c2ecf20Sopenharmony_ci		if (ret)
6788c2ecf20Sopenharmony_ci			return ret;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci		ret = phy_write(phydev, MII_CTRL1000, DP83869_CFG1_DEFAULT);
6818c2ecf20Sopenharmony_ci		if (ret)
6828c2ecf20Sopenharmony_ci			return ret;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci		ret = dp83869_configure_rgmii(phydev, dp83869);
6858c2ecf20Sopenharmony_ci		if (ret)
6868c2ecf20Sopenharmony_ci			return ret;
6878c2ecf20Sopenharmony_ci		break;
6888c2ecf20Sopenharmony_ci	case DP83869_RGMII_SGMII_BRIDGE:
6898c2ecf20Sopenharmony_ci		ret = phy_modify_mmd(phydev, DP83869_DEVADDR, DP83869_OP_MODE,
6908c2ecf20Sopenharmony_ci				     DP83869_SGMII_RGMII_BRIDGE,
6918c2ecf20Sopenharmony_ci				     DP83869_SGMII_RGMII_BRIDGE);
6928c2ecf20Sopenharmony_ci		if (ret)
6938c2ecf20Sopenharmony_ci			return ret;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci		ret = phy_write_mmd(phydev, DP83869_DEVADDR,
6968c2ecf20Sopenharmony_ci				    DP83869_FX_CTRL, DP83869_FX_CTRL_DEFAULT);
6978c2ecf20Sopenharmony_ci		if (ret)
6988c2ecf20Sopenharmony_ci			return ret;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci		break;
7018c2ecf20Sopenharmony_ci	case DP83869_1000M_MEDIA_CONVERT:
7028c2ecf20Sopenharmony_ci		ret = phy_write(phydev, MII_DP83869_PHYCTRL,
7038c2ecf20Sopenharmony_ci				phy_ctrl_val);
7048c2ecf20Sopenharmony_ci		if (ret)
7058c2ecf20Sopenharmony_ci			return ret;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci		ret = phy_write_mmd(phydev, DP83869_DEVADDR,
7088c2ecf20Sopenharmony_ci				    DP83869_FX_CTRL, DP83869_FX_CTRL_DEFAULT);
7098c2ecf20Sopenharmony_ci		if (ret)
7108c2ecf20Sopenharmony_ci			return ret;
7118c2ecf20Sopenharmony_ci		break;
7128c2ecf20Sopenharmony_ci	case DP83869_100M_MEDIA_CONVERT:
7138c2ecf20Sopenharmony_ci		ret = phy_write(phydev, MII_DP83869_PHYCTRL,
7148c2ecf20Sopenharmony_ci				phy_ctrl_val);
7158c2ecf20Sopenharmony_ci		if (ret)
7168c2ecf20Sopenharmony_ci			return ret;
7178c2ecf20Sopenharmony_ci		break;
7188c2ecf20Sopenharmony_ci	case DP83869_SGMII_COPPER_ETHERNET:
7198c2ecf20Sopenharmony_ci		ret = phy_write(phydev, MII_DP83869_PHYCTRL,
7208c2ecf20Sopenharmony_ci				phy_ctrl_val);
7218c2ecf20Sopenharmony_ci		if (ret)
7228c2ecf20Sopenharmony_ci			return ret;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci		ret = phy_write(phydev, MII_CTRL1000, DP83869_CFG1_DEFAULT);
7258c2ecf20Sopenharmony_ci		if (ret)
7268c2ecf20Sopenharmony_ci			return ret;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci		ret = phy_write_mmd(phydev, DP83869_DEVADDR,
7298c2ecf20Sopenharmony_ci				    DP83869_FX_CTRL, DP83869_FX_CTRL_DEFAULT);
7308c2ecf20Sopenharmony_ci		if (ret)
7318c2ecf20Sopenharmony_ci			return ret;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		break;
7348c2ecf20Sopenharmony_ci	case DP83869_RGMII_1000_BASE:
7358c2ecf20Sopenharmony_ci	case DP83869_RGMII_100_BASE:
7368c2ecf20Sopenharmony_ci		ret = dp83869_configure_fiber(phydev, dp83869);
7378c2ecf20Sopenharmony_ci		break;
7388c2ecf20Sopenharmony_ci	default:
7398c2ecf20Sopenharmony_ci		return -EINVAL;
7408c2ecf20Sopenharmony_ci	}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	return ret;
7438c2ecf20Sopenharmony_ci}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_cistatic int dp83869_config_init(struct phy_device *phydev)
7468c2ecf20Sopenharmony_ci{
7478c2ecf20Sopenharmony_ci	struct dp83869_private *dp83869 = phydev->priv;
7488c2ecf20Sopenharmony_ci	int ret, val;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	/* Force speed optimization for the PHY even if it strapped */
7518c2ecf20Sopenharmony_ci	ret = phy_modify(phydev, DP83869_CFG2, DP83869_DOWNSHIFT_EN,
7528c2ecf20Sopenharmony_ci			 DP83869_DOWNSHIFT_EN);
7538c2ecf20Sopenharmony_ci	if (ret)
7548c2ecf20Sopenharmony_ci		return ret;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	ret = dp83869_configure_mode(phydev, dp83869);
7578c2ecf20Sopenharmony_ci	if (ret)
7588c2ecf20Sopenharmony_ci		return ret;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/* Enable Interrupt output INT_OE in CFG4 register */
7618c2ecf20Sopenharmony_ci	if (phy_interrupt_is_valid(phydev)) {
7628c2ecf20Sopenharmony_ci		val = phy_read(phydev, DP83869_CFG4);
7638c2ecf20Sopenharmony_ci		val |= DP83869_INT_OE;
7648c2ecf20Sopenharmony_ci		phy_write(phydev, DP83869_CFG4, val);
7658c2ecf20Sopenharmony_ci	}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	if (dp83869->port_mirroring != DP83869_PORT_MIRRORING_KEEP)
7688c2ecf20Sopenharmony_ci		dp83869_config_port_mirroring(phydev);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	/* Clock output selection if muxing property is set */
7718c2ecf20Sopenharmony_ci	if (dp83869->clk_output_sel != DP83869_CLK_O_SEL_REF_CLK)
7728c2ecf20Sopenharmony_ci		ret = phy_modify_mmd(phydev,
7738c2ecf20Sopenharmony_ci				     DP83869_DEVADDR, DP83869_IO_MUX_CFG,
7748c2ecf20Sopenharmony_ci				     DP83869_IO_MUX_CFG_CLK_O_SEL_MASK,
7758c2ecf20Sopenharmony_ci				     dp83869->clk_output_sel <<
7768c2ecf20Sopenharmony_ci				     DP83869_IO_MUX_CFG_CLK_O_SEL_SHIFT);
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	if (phy_interface_is_rgmii(phydev)) {
7798c2ecf20Sopenharmony_ci		ret = phy_write_mmd(phydev, DP83869_DEVADDR, DP83869_RGMIIDCTL,
7808c2ecf20Sopenharmony_ci				    dp83869->rx_int_delay |
7818c2ecf20Sopenharmony_ci			dp83869->tx_int_delay << DP83869_RGMII_CLK_DELAY_SHIFT);
7828c2ecf20Sopenharmony_ci		if (ret)
7838c2ecf20Sopenharmony_ci			return ret;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci		val = phy_read_mmd(phydev, DP83869_DEVADDR, DP83869_RGMIICTL);
7868c2ecf20Sopenharmony_ci		val |= (DP83869_RGMII_TX_CLK_DELAY_EN |
7878c2ecf20Sopenharmony_ci			DP83869_RGMII_RX_CLK_DELAY_EN);
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
7908c2ecf20Sopenharmony_ci			val &= ~(DP83869_RGMII_TX_CLK_DELAY_EN |
7918c2ecf20Sopenharmony_ci				 DP83869_RGMII_RX_CLK_DELAY_EN);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
7948c2ecf20Sopenharmony_ci			val &= ~DP83869_RGMII_TX_CLK_DELAY_EN;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
7978c2ecf20Sopenharmony_ci			val &= ~DP83869_RGMII_RX_CLK_DELAY_EN;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci		ret = phy_write_mmd(phydev, DP83869_DEVADDR, DP83869_RGMIICTL,
8008c2ecf20Sopenharmony_ci				    val);
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	return ret;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_cistatic int dp83869_probe(struct phy_device *phydev)
8078c2ecf20Sopenharmony_ci{
8088c2ecf20Sopenharmony_ci	struct dp83869_private *dp83869;
8098c2ecf20Sopenharmony_ci	int ret;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	dp83869 = devm_kzalloc(&phydev->mdio.dev, sizeof(*dp83869),
8128c2ecf20Sopenharmony_ci			       GFP_KERNEL);
8138c2ecf20Sopenharmony_ci	if (!dp83869)
8148c2ecf20Sopenharmony_ci		return -ENOMEM;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	phydev->priv = dp83869;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	ret = dp83869_of_init(phydev);
8198c2ecf20Sopenharmony_ci	if (ret)
8208c2ecf20Sopenharmony_ci		return ret;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	if (dp83869->mode == DP83869_RGMII_100_BASE ||
8238c2ecf20Sopenharmony_ci	    dp83869->mode == DP83869_RGMII_1000_BASE)
8248c2ecf20Sopenharmony_ci		phydev->port = PORT_FIBRE;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	return dp83869_config_init(phydev);
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_cistatic int dp83869_phy_reset(struct phy_device *phydev)
8308c2ecf20Sopenharmony_ci{
8318c2ecf20Sopenharmony_ci	int ret;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	ret = phy_write(phydev, DP83869_CTRL, DP83869_SW_RESET);
8348c2ecf20Sopenharmony_ci	if (ret < 0)
8358c2ecf20Sopenharmony_ci		return ret;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	usleep_range(10, 20);
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	/* Global sw reset sets all registers to default.
8408c2ecf20Sopenharmony_ci	 * Need to set the registers in the PHY to the right config.
8418c2ecf20Sopenharmony_ci	 */
8428c2ecf20Sopenharmony_ci	return dp83869_config_init(phydev);
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic struct phy_driver dp83869_driver[] = {
8468c2ecf20Sopenharmony_ci	{
8478c2ecf20Sopenharmony_ci		PHY_ID_MATCH_MODEL(DP83869_PHY_ID),
8488c2ecf20Sopenharmony_ci		.name		= "TI DP83869",
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci		.probe          = dp83869_probe,
8518c2ecf20Sopenharmony_ci		.config_init	= dp83869_config_init,
8528c2ecf20Sopenharmony_ci		.soft_reset	= dp83869_phy_reset,
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci		/* IRQ related */
8558c2ecf20Sopenharmony_ci		.ack_interrupt	= dp83869_ack_interrupt,
8568c2ecf20Sopenharmony_ci		.config_intr	= dp83869_config_intr,
8578c2ecf20Sopenharmony_ci		.read_status	= dp83869_read_status,
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci		.get_tunable	= dp83869_get_tunable,
8608c2ecf20Sopenharmony_ci		.set_tunable	= dp83869_set_tunable,
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci		.get_wol	= dp83869_get_wol,
8638c2ecf20Sopenharmony_ci		.set_wol	= dp83869_set_wol,
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci		.suspend	= genphy_suspend,
8668c2ecf20Sopenharmony_ci		.resume		= genphy_resume,
8678c2ecf20Sopenharmony_ci	},
8688c2ecf20Sopenharmony_ci};
8698c2ecf20Sopenharmony_cimodule_phy_driver(dp83869_driver);
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_cistatic struct mdio_device_id __maybe_unused dp83869_tbl[] = {
8728c2ecf20Sopenharmony_ci	{ PHY_ID_MATCH_MODEL(DP83869_PHY_ID) },
8738c2ecf20Sopenharmony_ci	{ }
8748c2ecf20Sopenharmony_ci};
8758c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(mdio, dp83869_tbl);
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Texas Instruments DP83869 PHY driver");
8788c2ecf20Sopenharmony_ciMODULE_AUTHOR("Dan Murphy <dmurphy@ti.com");
8798c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
880