18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/* Framework for finding and configuring PHYs.
38c2ecf20Sopenharmony_ci * Also contains generic PHY driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Andy Fleming
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2004 Freescale Semiconductor, Inc.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/bitmap.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/errno.h>
158c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
168c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/io.h>
208c2ecf20Sopenharmony_ci#include <linux/kernel.h>
218c2ecf20Sopenharmony_ci#include <linux/mdio.h>
228c2ecf20Sopenharmony_ci#include <linux/mii.h>
238c2ecf20Sopenharmony_ci#include <linux/mm.h>
248c2ecf20Sopenharmony_ci#include <linux/module.h>
258c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
268c2ecf20Sopenharmony_ci#include <linux/phy.h>
278c2ecf20Sopenharmony_ci#include <linux/phy_led_triggers.h>
288c2ecf20Sopenharmony_ci#include <linux/property.h>
298c2ecf20Sopenharmony_ci#include <linux/sfp.h>
308c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
318c2ecf20Sopenharmony_ci#include <linux/slab.h>
328c2ecf20Sopenharmony_ci#include <linux/string.h>
338c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
348c2ecf20Sopenharmony_ci#include <linux/unistd.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PHY library");
378c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andy Fleming");
388c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
418c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_basic_features);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
448c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_basic_t1_features);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_gbit_features);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_10gbit_features);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
598c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciconst int phy_basic_ports_array[3] = {
628c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_Autoneg_BIT,
638c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_TP_BIT,
648c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_MII_BIT,
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_basic_ports_array);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciconst int phy_fibre_port_array[1] = {
698c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_FIBRE_BIT,
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_fibre_port_array);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciconst int phy_all_ports_features_array[7] = {
748c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_Autoneg_BIT,
758c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_TP_BIT,
768c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_MII_BIT,
778c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_FIBRE_BIT,
788c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_AUI_BIT,
798c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_BNC_BIT,
808c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_Backplane_BIT,
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_all_ports_features_array);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciconst int phy_10_100_features_array[4] = {
858c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_10baseT_Half_BIT,
868c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_10baseT_Full_BIT,
878c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_100baseT_Half_BIT,
888c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_10_100_features_array);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciconst int phy_basic_t1_features_array[2] = {
938c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_TP_BIT,
948c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciconst int phy_gbit_features_array[2] = {
998c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1008c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_gbit_features_array);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciconst int phy_10gbit_features_array[1] = {
1058c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_10gbit_features_array);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic const int phy_10gbit_fec_features_array[1] = {
1108c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
1148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_10gbit_full_features);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic const int phy_10gbit_full_features_array[] = {
1178c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_10baseT_Full_BIT,
1188c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1198c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1208c2ecf20Sopenharmony_ci	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic void features_init(void)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	/* 10/100 half/full*/
1268c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_basic_ports_array,
1278c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_basic_ports_array),
1288c2ecf20Sopenharmony_ci			       phy_basic_features);
1298c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_10_100_features_array,
1308c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_10_100_features_array),
1318c2ecf20Sopenharmony_ci			       phy_basic_features);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* 100 full, TP */
1348c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_basic_t1_features_array,
1358c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_basic_t1_features_array),
1368c2ecf20Sopenharmony_ci			       phy_basic_t1_features);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	/* 10/100 half/full + 1000 half/full */
1398c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_basic_ports_array,
1408c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_basic_ports_array),
1418c2ecf20Sopenharmony_ci			       phy_gbit_features);
1428c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_10_100_features_array,
1438c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_10_100_features_array),
1448c2ecf20Sopenharmony_ci			       phy_gbit_features);
1458c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_gbit_features_array,
1468c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_gbit_features_array),
1478c2ecf20Sopenharmony_ci			       phy_gbit_features);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* 10/100 half/full + 1000 half/full + fibre*/
1508c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_basic_ports_array,
1518c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_basic_ports_array),
1528c2ecf20Sopenharmony_ci			       phy_gbit_fibre_features);
1538c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_10_100_features_array,
1548c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_10_100_features_array),
1558c2ecf20Sopenharmony_ci			       phy_gbit_fibre_features);
1568c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_gbit_features_array,
1578c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_gbit_features_array),
1588c2ecf20Sopenharmony_ci			       phy_gbit_fibre_features);
1598c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_fibre_port_array,
1608c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_fibre_port_array),
1618c2ecf20Sopenharmony_ci			       phy_gbit_fibre_features);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	/* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
1648c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_all_ports_features_array,
1658c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_all_ports_features_array),
1668c2ecf20Sopenharmony_ci			       phy_gbit_all_ports_features);
1678c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_10_100_features_array,
1688c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_10_100_features_array),
1698c2ecf20Sopenharmony_ci			       phy_gbit_all_ports_features);
1708c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_gbit_features_array,
1718c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_gbit_features_array),
1728c2ecf20Sopenharmony_ci			       phy_gbit_all_ports_features);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/* 10/100 half/full + 1000 half/full + 10G full*/
1758c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_all_ports_features_array,
1768c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_all_ports_features_array),
1778c2ecf20Sopenharmony_ci			       phy_10gbit_features);
1788c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_10_100_features_array,
1798c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_10_100_features_array),
1808c2ecf20Sopenharmony_ci			       phy_10gbit_features);
1818c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_gbit_features_array,
1828c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_gbit_features_array),
1838c2ecf20Sopenharmony_ci			       phy_10gbit_features);
1848c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_10gbit_features_array,
1858c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_10gbit_features_array),
1868c2ecf20Sopenharmony_ci			       phy_10gbit_features);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* 10/100/1000/10G full */
1898c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_all_ports_features_array,
1908c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_all_ports_features_array),
1918c2ecf20Sopenharmony_ci			       phy_10gbit_full_features);
1928c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_10gbit_full_features_array,
1938c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_10gbit_full_features_array),
1948c2ecf20Sopenharmony_ci			       phy_10gbit_full_features);
1958c2ecf20Sopenharmony_ci	/* 10G FEC only */
1968c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_10gbit_fec_features_array,
1978c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_10gbit_fec_features_array),
1988c2ecf20Sopenharmony_ci			       phy_10gbit_fec_features);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_civoid phy_device_free(struct phy_device *phydev)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	put_device(&phydev->mdio.dev);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_device_free);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic void phy_mdio_device_free(struct mdio_device *mdiodev)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct phy_device *phydev;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	phydev = container_of(mdiodev, struct phy_device, mdio);
2128c2ecf20Sopenharmony_ci	phy_device_free(phydev);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic void phy_device_release(struct device *dev)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	kfree(to_phy_device(dev));
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic void phy_mdio_device_remove(struct mdio_device *mdiodev)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	struct phy_device *phydev;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	phydev = container_of(mdiodev, struct phy_device, mdio);
2258c2ecf20Sopenharmony_ci	phy_device_remove(phydev);
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic struct phy_driver genphy_driver;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic LIST_HEAD(phy_fixup_list);
2318c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(phy_fixup_lock);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	struct device_driver *drv = phydev->mdio.dev.driver;
2368c2ecf20Sopenharmony_ci	struct phy_driver *phydrv = to_phy_driver(drv);
2378c2ecf20Sopenharmony_ci	struct net_device *netdev = phydev->attached_dev;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (!drv || !phydrv->suspend)
2408c2ecf20Sopenharmony_ci		return false;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* PHY not attached? May suspend if the PHY has not already been
2438c2ecf20Sopenharmony_ci	 * suspended as part of a prior call to phy_disconnect() ->
2448c2ecf20Sopenharmony_ci	 * phy_detach() -> phy_suspend() because the parent netdev might be the
2458c2ecf20Sopenharmony_ci	 * MDIO bus driver and clock gated at this point.
2468c2ecf20Sopenharmony_ci	 */
2478c2ecf20Sopenharmony_ci	if (!netdev)
2488c2ecf20Sopenharmony_ci		goto out;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (netdev->wol_enabled)
2518c2ecf20Sopenharmony_ci		return false;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/* As long as not all affected network drivers support the
2548c2ecf20Sopenharmony_ci	 * wol_enabled flag, let's check for hints that WoL is enabled.
2558c2ecf20Sopenharmony_ci	 * Don't suspend PHY if the attached netdev parent may wake up.
2568c2ecf20Sopenharmony_ci	 * The parent may point to a PCI device, as in tg3 driver.
2578c2ecf20Sopenharmony_ci	 */
2588c2ecf20Sopenharmony_ci	if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
2598c2ecf20Sopenharmony_ci		return false;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* Also don't suspend PHY if the netdev itself may wakeup. This
2628c2ecf20Sopenharmony_ci	 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
2638c2ecf20Sopenharmony_ci	 * e.g. SoC devices.
2648c2ecf20Sopenharmony_ci	 */
2658c2ecf20Sopenharmony_ci	if (device_may_wakeup(&netdev->dev))
2668c2ecf20Sopenharmony_ci		return false;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ciout:
2698c2ecf20Sopenharmony_ci	return !phydev->suspended;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	/* We must stop the state machine manually, otherwise it stops out of
2778c2ecf20Sopenharmony_ci	 * control, possibly with the phydev->lock held. Upon resume, netdev
2788c2ecf20Sopenharmony_ci	 * may call phy routines that try to grab the same lock, and that may
2798c2ecf20Sopenharmony_ci	 * lead to a deadlock.
2808c2ecf20Sopenharmony_ci	 */
2818c2ecf20Sopenharmony_ci	if (phydev->attached_dev && phydev->adjust_link)
2828c2ecf20Sopenharmony_ci		phy_stop_machine(phydev);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	if (!mdio_bus_phy_may_suspend(phydev))
2858c2ecf20Sopenharmony_ci		return 0;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	phydev->suspended_by_mdio_bus = 1;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	return phy_suspend(phydev);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic __maybe_unused int mdio_bus_phy_resume(struct device *dev)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
2958c2ecf20Sopenharmony_ci	int ret;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if (!phydev->suspended_by_mdio_bus)
2988c2ecf20Sopenharmony_ci		goto no_resume;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	phydev->suspended_by_mdio_bus = 0;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	ret = phy_init_hw(phydev);
3038c2ecf20Sopenharmony_ci	if (ret < 0)
3048c2ecf20Sopenharmony_ci		return ret;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	ret = phy_resume(phydev);
3078c2ecf20Sopenharmony_ci	if (ret < 0)
3088c2ecf20Sopenharmony_ci		return ret;
3098c2ecf20Sopenharmony_cino_resume:
3108c2ecf20Sopenharmony_ci	if (phydev->attached_dev && phydev->adjust_link)
3118c2ecf20Sopenharmony_ci		phy_start_machine(phydev);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	return 0;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
3178c2ecf20Sopenharmony_ci			 mdio_bus_phy_resume);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci/**
3208c2ecf20Sopenharmony_ci * phy_register_fixup - creates a new phy_fixup and adds it to the list
3218c2ecf20Sopenharmony_ci * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
3228c2ecf20Sopenharmony_ci * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
3238c2ecf20Sopenharmony_ci *	It can also be PHY_ANY_UID
3248c2ecf20Sopenharmony_ci * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
3258c2ecf20Sopenharmony_ci *	comparison
3268c2ecf20Sopenharmony_ci * @run: The actual code to be run when a matching PHY is found
3278c2ecf20Sopenharmony_ci */
3288c2ecf20Sopenharmony_ciint phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
3298c2ecf20Sopenharmony_ci		       int (*run)(struct phy_device *))
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (!fixup)
3348c2ecf20Sopenharmony_ci		return -ENOMEM;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
3378c2ecf20Sopenharmony_ci	fixup->phy_uid = phy_uid;
3388c2ecf20Sopenharmony_ci	fixup->phy_uid_mask = phy_uid_mask;
3398c2ecf20Sopenharmony_ci	fixup->run = run;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	mutex_lock(&phy_fixup_lock);
3428c2ecf20Sopenharmony_ci	list_add_tail(&fixup->list, &phy_fixup_list);
3438c2ecf20Sopenharmony_ci	mutex_unlock(&phy_fixup_lock);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	return 0;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_register_fixup);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci/* Registers a fixup to be run on any PHY with the UID in phy_uid */
3508c2ecf20Sopenharmony_ciint phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
3518c2ecf20Sopenharmony_ci			       int (*run)(struct phy_device *))
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_register_fixup_for_uid);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci/* Registers a fixup to be run on the PHY with id string bus_id */
3588c2ecf20Sopenharmony_ciint phy_register_fixup_for_id(const char *bus_id,
3598c2ecf20Sopenharmony_ci			      int (*run)(struct phy_device *))
3608c2ecf20Sopenharmony_ci{
3618c2ecf20Sopenharmony_ci	return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_register_fixup_for_id);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci/**
3668c2ecf20Sopenharmony_ci * phy_unregister_fixup - remove a phy_fixup from the list
3678c2ecf20Sopenharmony_ci * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
3688c2ecf20Sopenharmony_ci * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
3698c2ecf20Sopenharmony_ci * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
3708c2ecf20Sopenharmony_ci */
3718c2ecf20Sopenharmony_ciint phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct list_head *pos, *n;
3748c2ecf20Sopenharmony_ci	struct phy_fixup *fixup;
3758c2ecf20Sopenharmony_ci	int ret;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	ret = -ENODEV;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	mutex_lock(&phy_fixup_lock);
3808c2ecf20Sopenharmony_ci	list_for_each_safe(pos, n, &phy_fixup_list) {
3818c2ecf20Sopenharmony_ci		fixup = list_entry(pos, struct phy_fixup, list);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci		if ((!strcmp(fixup->bus_id, bus_id)) &&
3848c2ecf20Sopenharmony_ci		    ((fixup->phy_uid & phy_uid_mask) ==
3858c2ecf20Sopenharmony_ci		     (phy_uid & phy_uid_mask))) {
3868c2ecf20Sopenharmony_ci			list_del(&fixup->list);
3878c2ecf20Sopenharmony_ci			kfree(fixup);
3888c2ecf20Sopenharmony_ci			ret = 0;
3898c2ecf20Sopenharmony_ci			break;
3908c2ecf20Sopenharmony_ci		}
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci	mutex_unlock(&phy_fixup_lock);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	return ret;
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_unregister_fixup);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci/* Unregisters a fixup of any PHY with the UID in phy_uid */
3998c2ecf20Sopenharmony_ciint phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_unregister_fixup_for_uid);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci/* Unregisters a fixup of the PHY with id string bus_id */
4068c2ecf20Sopenharmony_ciint phy_unregister_fixup_for_id(const char *bus_id)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_unregister_fixup_for_id);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
4138c2ecf20Sopenharmony_ci * Fixups can be set to match any in one or more fields.
4148c2ecf20Sopenharmony_ci */
4158c2ecf20Sopenharmony_cistatic int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
4168c2ecf20Sopenharmony_ci{
4178c2ecf20Sopenharmony_ci	if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
4188c2ecf20Sopenharmony_ci		if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
4198c2ecf20Sopenharmony_ci			return 0;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	if ((fixup->phy_uid & fixup->phy_uid_mask) !=
4228c2ecf20Sopenharmony_ci	    (phydev->phy_id & fixup->phy_uid_mask))
4238c2ecf20Sopenharmony_ci		if (fixup->phy_uid != PHY_ANY_UID)
4248c2ecf20Sopenharmony_ci			return 0;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	return 1;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci/* Runs any matching fixups for this phydev */
4308c2ecf20Sopenharmony_cistatic int phy_scan_fixups(struct phy_device *phydev)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	struct phy_fixup *fixup;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	mutex_lock(&phy_fixup_lock);
4358c2ecf20Sopenharmony_ci	list_for_each_entry(fixup, &phy_fixup_list, list) {
4368c2ecf20Sopenharmony_ci		if (phy_needs_fixup(phydev, fixup)) {
4378c2ecf20Sopenharmony_ci			int err = fixup->run(phydev);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci			if (err < 0) {
4408c2ecf20Sopenharmony_ci				mutex_unlock(&phy_fixup_lock);
4418c2ecf20Sopenharmony_ci				return err;
4428c2ecf20Sopenharmony_ci			}
4438c2ecf20Sopenharmony_ci			phydev->has_fixups = true;
4448c2ecf20Sopenharmony_ci		}
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci	mutex_unlock(&phy_fixup_lock);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	return 0;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic int phy_bus_match(struct device *dev, struct device_driver *drv)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
4548c2ecf20Sopenharmony_ci	struct phy_driver *phydrv = to_phy_driver(drv);
4558c2ecf20Sopenharmony_ci	const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
4568c2ecf20Sopenharmony_ci	int i;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
4598c2ecf20Sopenharmony_ci		return 0;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	if (phydrv->match_phy_device)
4628c2ecf20Sopenharmony_ci		return phydrv->match_phy_device(phydev);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	if (phydev->is_c45) {
4658c2ecf20Sopenharmony_ci		for (i = 1; i < num_ids; i++) {
4668c2ecf20Sopenharmony_ci			if (phydev->c45_ids.device_ids[i] == 0xffffffff)
4678c2ecf20Sopenharmony_ci				continue;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci			if ((phydrv->phy_id & phydrv->phy_id_mask) ==
4708c2ecf20Sopenharmony_ci			    (phydev->c45_ids.device_ids[i] &
4718c2ecf20Sopenharmony_ci			     phydrv->phy_id_mask))
4728c2ecf20Sopenharmony_ci				return 1;
4738c2ecf20Sopenharmony_ci		}
4748c2ecf20Sopenharmony_ci		return 0;
4758c2ecf20Sopenharmony_ci	} else {
4768c2ecf20Sopenharmony_ci		return (phydrv->phy_id & phydrv->phy_id_mask) ==
4778c2ecf20Sopenharmony_ci			(phydev->phy_id & phydrv->phy_id_mask);
4788c2ecf20Sopenharmony_ci	}
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic ssize_t
4828c2ecf20Sopenharmony_ciphy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(phy_id);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_cistatic ssize_t
4918c2ecf20Sopenharmony_ciphy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
4948c2ecf20Sopenharmony_ci	const char *mode = NULL;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	if (phy_is_internal(phydev))
4978c2ecf20Sopenharmony_ci		mode = "internal";
4988c2ecf20Sopenharmony_ci	else
4998c2ecf20Sopenharmony_ci		mode = phy_modes(phydev->interface);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	return sprintf(buf, "%s\n", mode);
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(phy_interface);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic ssize_t
5068c2ecf20Sopenharmony_ciphy_has_fixups_show(struct device *dev, struct device_attribute *attr,
5078c2ecf20Sopenharmony_ci		    char *buf)
5088c2ecf20Sopenharmony_ci{
5098c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", phydev->has_fixups);
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(phy_has_fixups);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic struct attribute *phy_dev_attrs[] = {
5168c2ecf20Sopenharmony_ci	&dev_attr_phy_id.attr,
5178c2ecf20Sopenharmony_ci	&dev_attr_phy_interface.attr,
5188c2ecf20Sopenharmony_ci	&dev_attr_phy_has_fixups.attr,
5198c2ecf20Sopenharmony_ci	NULL,
5208c2ecf20Sopenharmony_ci};
5218c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(phy_dev);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_cistatic const struct device_type mdio_bus_phy_type = {
5248c2ecf20Sopenharmony_ci	.name = "PHY",
5258c2ecf20Sopenharmony_ci	.groups = phy_dev_groups,
5268c2ecf20Sopenharmony_ci	.release = phy_device_release,
5278c2ecf20Sopenharmony_ci	.pm = pm_ptr(&mdio_bus_phy_pm_ops),
5288c2ecf20Sopenharmony_ci};
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_cistatic int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
5318c2ecf20Sopenharmony_ci{
5328c2ecf20Sopenharmony_ci	int ret;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
5358c2ecf20Sopenharmony_ci			     MDIO_ID_ARGS(phy_id));
5368c2ecf20Sopenharmony_ci	/* We only check for failures in executing the usermode binary,
5378c2ecf20Sopenharmony_ci	 * not whether a PHY driver module exists for the PHY ID.
5388c2ecf20Sopenharmony_ci	 * Accept -ENOENT because this may occur in case no initramfs exists,
5398c2ecf20Sopenharmony_ci	 * then modprobe isn't available.
5408c2ecf20Sopenharmony_ci	 */
5418c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
5428c2ecf20Sopenharmony_ci		phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
5438c2ecf20Sopenharmony_ci			   ret, (unsigned long)phy_id);
5448c2ecf20Sopenharmony_ci		return ret;
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	return 0;
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistruct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
5518c2ecf20Sopenharmony_ci				     bool is_c45,
5528c2ecf20Sopenharmony_ci				     struct phy_c45_device_ids *c45_ids)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	struct phy_device *dev;
5558c2ecf20Sopenharmony_ci	struct mdio_device *mdiodev;
5568c2ecf20Sopenharmony_ci	int ret = 0;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	/* We allocate the device, and initialize the default values */
5598c2ecf20Sopenharmony_ci	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
5608c2ecf20Sopenharmony_ci	if (!dev)
5618c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	mdiodev = &dev->mdio;
5648c2ecf20Sopenharmony_ci	mdiodev->dev.parent = &bus->dev;
5658c2ecf20Sopenharmony_ci	mdiodev->dev.bus = &mdio_bus_type;
5668c2ecf20Sopenharmony_ci	mdiodev->dev.type = &mdio_bus_phy_type;
5678c2ecf20Sopenharmony_ci	mdiodev->bus = bus;
5688c2ecf20Sopenharmony_ci	mdiodev->bus_match = phy_bus_match;
5698c2ecf20Sopenharmony_ci	mdiodev->addr = addr;
5708c2ecf20Sopenharmony_ci	mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
5718c2ecf20Sopenharmony_ci	mdiodev->device_free = phy_mdio_device_free;
5728c2ecf20Sopenharmony_ci	mdiodev->device_remove = phy_mdio_device_remove;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	dev->speed = SPEED_UNKNOWN;
5758c2ecf20Sopenharmony_ci	dev->duplex = DUPLEX_UNKNOWN;
5768c2ecf20Sopenharmony_ci	dev->pause = 0;
5778c2ecf20Sopenharmony_ci	dev->asym_pause = 0;
5788c2ecf20Sopenharmony_ci	dev->link = 0;
5798c2ecf20Sopenharmony_ci	dev->port = PORT_TP;
5808c2ecf20Sopenharmony_ci	dev->interface = PHY_INTERFACE_MODE_GMII;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	dev->autoneg = AUTONEG_ENABLE;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	dev->is_c45 = is_c45;
5858c2ecf20Sopenharmony_ci	dev->phy_id = phy_id;
5868c2ecf20Sopenharmony_ci	if (c45_ids)
5878c2ecf20Sopenharmony_ci		dev->c45_ids = *c45_ids;
5888c2ecf20Sopenharmony_ci	dev->irq = bus->irq[addr];
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
5918c2ecf20Sopenharmony_ci	device_initialize(&mdiodev->dev);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	dev->state = PHY_DOWN;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	mutex_init(&dev->lock);
5968c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	/* Request the appropriate module unconditionally; don't
5998c2ecf20Sopenharmony_ci	 * bother trying to do so only if it isn't already loaded,
6008c2ecf20Sopenharmony_ci	 * because that gets complicated. A hotplug event would have
6018c2ecf20Sopenharmony_ci	 * done an unconditional modprobe anyway.
6028c2ecf20Sopenharmony_ci	 * We don't do normal hotplug because it won't work for MDIO
6038c2ecf20Sopenharmony_ci	 * -- because it relies on the device staying around for long
6048c2ecf20Sopenharmony_ci	 * enough for the driver to get loaded. With MDIO, the NIC
6058c2ecf20Sopenharmony_ci	 * driver will get bored and give up as soon as it finds that
6068c2ecf20Sopenharmony_ci	 * there's no driver _already_ loaded.
6078c2ecf20Sopenharmony_ci	 */
6088c2ecf20Sopenharmony_ci	if (is_c45 && c45_ids) {
6098c2ecf20Sopenharmony_ci		const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
6108c2ecf20Sopenharmony_ci		int i;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci		for (i = 1; i < num_ids; i++) {
6138c2ecf20Sopenharmony_ci			if (c45_ids->device_ids[i] == 0xffffffff)
6148c2ecf20Sopenharmony_ci				continue;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci			ret = phy_request_driver_module(dev,
6178c2ecf20Sopenharmony_ci						c45_ids->device_ids[i]);
6188c2ecf20Sopenharmony_ci			if (ret)
6198c2ecf20Sopenharmony_ci				break;
6208c2ecf20Sopenharmony_ci		}
6218c2ecf20Sopenharmony_ci	} else {
6228c2ecf20Sopenharmony_ci		ret = phy_request_driver_module(dev, phy_id);
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	if (ret) {
6268c2ecf20Sopenharmony_ci		put_device(&mdiodev->dev);
6278c2ecf20Sopenharmony_ci		dev = ERR_PTR(ret);
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	return dev;
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_device_create);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci/* phy_c45_probe_present - checks to see if a MMD is present in the package
6358c2ecf20Sopenharmony_ci * @bus: the target MII bus
6368c2ecf20Sopenharmony_ci * @prtad: PHY package address on the MII bus
6378c2ecf20Sopenharmony_ci * @devad: PHY device (MMD) address
6388c2ecf20Sopenharmony_ci *
6398c2ecf20Sopenharmony_ci * Read the MDIO_STAT2 register, and check whether a device is responding
6408c2ecf20Sopenharmony_ci * at this address.
6418c2ecf20Sopenharmony_ci *
6428c2ecf20Sopenharmony_ci * Returns: negative error number on bus access error, zero if no device
6438c2ecf20Sopenharmony_ci * is responding, or positive if a device is present.
6448c2ecf20Sopenharmony_ci */
6458c2ecf20Sopenharmony_cistatic int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	int stat2;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2);
6508c2ecf20Sopenharmony_ci	if (stat2 < 0)
6518c2ecf20Sopenharmony_ci		return stat2;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL;
6548c2ecf20Sopenharmony_ci}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
6578c2ecf20Sopenharmony_ci * @bus: the target MII bus
6588c2ecf20Sopenharmony_ci * @addr: PHY address on the MII bus
6598c2ecf20Sopenharmony_ci * @dev_addr: MMD address in the PHY.
6608c2ecf20Sopenharmony_ci * @devices_in_package: where to store the devices in package information.
6618c2ecf20Sopenharmony_ci *
6628c2ecf20Sopenharmony_ci * Description: reads devices in package registers of a MMD at @dev_addr
6638c2ecf20Sopenharmony_ci * from PHY at @addr on @bus.
6648c2ecf20Sopenharmony_ci *
6658c2ecf20Sopenharmony_ci * Returns: 0 on success, -EIO on failure.
6668c2ecf20Sopenharmony_ci */
6678c2ecf20Sopenharmony_cistatic int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
6688c2ecf20Sopenharmony_ci				   u32 *devices_in_package)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	int phy_reg;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
6738c2ecf20Sopenharmony_ci	if (phy_reg < 0)
6748c2ecf20Sopenharmony_ci		return -EIO;
6758c2ecf20Sopenharmony_ci	*devices_in_package = phy_reg << 16;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
6788c2ecf20Sopenharmony_ci	if (phy_reg < 0)
6798c2ecf20Sopenharmony_ci		return -EIO;
6808c2ecf20Sopenharmony_ci	*devices_in_package |= phy_reg;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	return 0;
6838c2ecf20Sopenharmony_ci}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci/**
6868c2ecf20Sopenharmony_ci * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
6878c2ecf20Sopenharmony_ci * @bus: the target MII bus
6888c2ecf20Sopenharmony_ci * @addr: PHY address on the MII bus
6898c2ecf20Sopenharmony_ci * @c45_ids: where to store the c45 ID information.
6908c2ecf20Sopenharmony_ci *
6918c2ecf20Sopenharmony_ci * Read the PHY "devices in package". If this appears to be valid, read
6928c2ecf20Sopenharmony_ci * the PHY identifiers for each device. Return the "devices in package"
6938c2ecf20Sopenharmony_ci * and identifiers in @c45_ids.
6948c2ecf20Sopenharmony_ci *
6958c2ecf20Sopenharmony_ci * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
6968c2ecf20Sopenharmony_ci * the "devices in package" is invalid.
6978c2ecf20Sopenharmony_ci */
6988c2ecf20Sopenharmony_cistatic int get_phy_c45_ids(struct mii_bus *bus, int addr,
6998c2ecf20Sopenharmony_ci			   struct phy_c45_device_ids *c45_ids)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
7028c2ecf20Sopenharmony_ci	u32 devs_in_pkg = 0;
7038c2ecf20Sopenharmony_ci	int i, ret, phy_reg;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	/* Find first non-zero Devices In package. Device zero is reserved
7068c2ecf20Sopenharmony_ci	 * for 802.3 c45 complied PHYs, so don't probe it at first.
7078c2ecf20Sopenharmony_ci	 */
7088c2ecf20Sopenharmony_ci	for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
7098c2ecf20Sopenharmony_ci	     (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {
7108c2ecf20Sopenharmony_ci		if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
7118c2ecf20Sopenharmony_ci			/* Check that there is a device present at this
7128c2ecf20Sopenharmony_ci			 * address before reading the devices-in-package
7138c2ecf20Sopenharmony_ci			 * register to avoid reading garbage from the PHY.
7148c2ecf20Sopenharmony_ci			 * Some PHYs (88x3310) vendor space is not IEEE802.3
7158c2ecf20Sopenharmony_ci			 * compliant.
7168c2ecf20Sopenharmony_ci			 */
7178c2ecf20Sopenharmony_ci			ret = phy_c45_probe_present(bus, addr, i);
7188c2ecf20Sopenharmony_ci			if (ret < 0)
7198c2ecf20Sopenharmony_ci				return -EIO;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci			if (!ret)
7228c2ecf20Sopenharmony_ci				continue;
7238c2ecf20Sopenharmony_ci		}
7248c2ecf20Sopenharmony_ci		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
7258c2ecf20Sopenharmony_ci		if (phy_reg < 0)
7268c2ecf20Sopenharmony_ci			return -EIO;
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
7308c2ecf20Sopenharmony_ci		/* If mostly Fs, there is no device there, then let's probe
7318c2ecf20Sopenharmony_ci		 * MMD 0, as some 10G PHYs have zero Devices In package,
7328c2ecf20Sopenharmony_ci		 * e.g. Cortina CS4315/CS4340 PHY.
7338c2ecf20Sopenharmony_ci		 */
7348c2ecf20Sopenharmony_ci		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
7358c2ecf20Sopenharmony_ci		if (phy_reg < 0)
7368c2ecf20Sopenharmony_ci			return -EIO;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci		/* no device there, let's get out of here */
7398c2ecf20Sopenharmony_ci		if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
7408c2ecf20Sopenharmony_ci			return -ENODEV;
7418c2ecf20Sopenharmony_ci	}
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	/* Now probe Device Identifiers for each device present. */
7448c2ecf20Sopenharmony_ci	for (i = 1; i < num_ids; i++) {
7458c2ecf20Sopenharmony_ci		if (!(devs_in_pkg & (1 << i)))
7468c2ecf20Sopenharmony_ci			continue;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci		if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
7498c2ecf20Sopenharmony_ci			/* Probe the "Device Present" bits for the vendor MMDs
7508c2ecf20Sopenharmony_ci			 * to ignore these if they do not contain IEEE 802.3
7518c2ecf20Sopenharmony_ci			 * registers.
7528c2ecf20Sopenharmony_ci			 */
7538c2ecf20Sopenharmony_ci			ret = phy_c45_probe_present(bus, addr, i);
7548c2ecf20Sopenharmony_ci			if (ret < 0)
7558c2ecf20Sopenharmony_ci				return ret;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci			if (!ret)
7588c2ecf20Sopenharmony_ci				continue;
7598c2ecf20Sopenharmony_ci		}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci		phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1);
7628c2ecf20Sopenharmony_ci		if (phy_reg < 0)
7638c2ecf20Sopenharmony_ci			return -EIO;
7648c2ecf20Sopenharmony_ci		c45_ids->device_ids[i] = phy_reg << 16;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci		phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2);
7678c2ecf20Sopenharmony_ci		if (phy_reg < 0)
7688c2ecf20Sopenharmony_ci			return -EIO;
7698c2ecf20Sopenharmony_ci		c45_ids->device_ids[i] |= phy_reg;
7708c2ecf20Sopenharmony_ci	}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	c45_ids->devices_in_package = devs_in_pkg;
7738c2ecf20Sopenharmony_ci	/* Bit 0 doesn't represent a device, it indicates c22 regs presence */
7748c2ecf20Sopenharmony_ci	c45_ids->mmds_present = devs_in_pkg & ~BIT(0);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	return 0;
7778c2ecf20Sopenharmony_ci}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci/**
7808c2ecf20Sopenharmony_ci * get_phy_c22_id - reads the specified addr for its clause 22 ID.
7818c2ecf20Sopenharmony_ci * @bus: the target MII bus
7828c2ecf20Sopenharmony_ci * @addr: PHY address on the MII bus
7838c2ecf20Sopenharmony_ci * @phy_id: where to store the ID retrieved.
7848c2ecf20Sopenharmony_ci *
7858c2ecf20Sopenharmony_ci * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus,
7868c2ecf20Sopenharmony_ci * placing it in @phy_id. Return zero on successful read and the ID is
7878c2ecf20Sopenharmony_ci * valid, %-EIO on bus access error, or %-ENODEV if no device responds
7888c2ecf20Sopenharmony_ci * or invalid ID.
7898c2ecf20Sopenharmony_ci */
7908c2ecf20Sopenharmony_cistatic int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id)
7918c2ecf20Sopenharmony_ci{
7928c2ecf20Sopenharmony_ci	int phy_reg;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	/* Grab the bits from PHYIR1, and put them in the upper half */
7958c2ecf20Sopenharmony_ci	phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
7968c2ecf20Sopenharmony_ci	if (phy_reg < 0) {
7978c2ecf20Sopenharmony_ci		/* returning -ENODEV doesn't stop bus scanning */
7988c2ecf20Sopenharmony_ci		return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	*phy_id = phy_reg << 16;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	/* Grab the bits from PHYIR2, and put them in the lower half */
8048c2ecf20Sopenharmony_ci	phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
8058c2ecf20Sopenharmony_ci	if (phy_reg < 0) {
8068c2ecf20Sopenharmony_ci		/* returning -ENODEV doesn't stop bus scanning */
8078c2ecf20Sopenharmony_ci		return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
8088c2ecf20Sopenharmony_ci	}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	*phy_id |= phy_reg;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	/* If the phy_id is mostly Fs, there is no device there */
8138c2ecf20Sopenharmony_ci	if ((*phy_id & 0x1fffffff) == 0x1fffffff)
8148c2ecf20Sopenharmony_ci		return -ENODEV;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	return 0;
8178c2ecf20Sopenharmony_ci}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci/**
8208c2ecf20Sopenharmony_ci * get_phy_device - reads the specified PHY device and returns its @phy_device
8218c2ecf20Sopenharmony_ci *		    struct
8228c2ecf20Sopenharmony_ci * @bus: the target MII bus
8238c2ecf20Sopenharmony_ci * @addr: PHY address on the MII bus
8248c2ecf20Sopenharmony_ci * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
8258c2ecf20Sopenharmony_ci *
8268c2ecf20Sopenharmony_ci * Probe for a PHY at @addr on @bus.
8278c2ecf20Sopenharmony_ci *
8288c2ecf20Sopenharmony_ci * When probing for a clause 22 PHY, then read the ID registers. If we find
8298c2ecf20Sopenharmony_ci * a valid ID, allocate and return a &struct phy_device.
8308c2ecf20Sopenharmony_ci *
8318c2ecf20Sopenharmony_ci * When probing for a clause 45 PHY, read the "devices in package" registers.
8328c2ecf20Sopenharmony_ci * If the "devices in package" appears valid, read the ID registers for each
8338c2ecf20Sopenharmony_ci * MMD, allocate and return a &struct phy_device.
8348c2ecf20Sopenharmony_ci *
8358c2ecf20Sopenharmony_ci * Returns an allocated &struct phy_device on success, %-ENODEV if there is
8368c2ecf20Sopenharmony_ci * no PHY present, or %-EIO on bus access error.
8378c2ecf20Sopenharmony_ci */
8388c2ecf20Sopenharmony_cistruct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
8398c2ecf20Sopenharmony_ci{
8408c2ecf20Sopenharmony_ci	struct phy_c45_device_ids c45_ids;
8418c2ecf20Sopenharmony_ci	u32 phy_id = 0;
8428c2ecf20Sopenharmony_ci	int r;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	c45_ids.devices_in_package = 0;
8458c2ecf20Sopenharmony_ci	c45_ids.mmds_present = 0;
8468c2ecf20Sopenharmony_ci	memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	if (is_c45)
8498c2ecf20Sopenharmony_ci		r = get_phy_c45_ids(bus, addr, &c45_ids);
8508c2ecf20Sopenharmony_ci	else
8518c2ecf20Sopenharmony_ci		r = get_phy_c22_id(bus, addr, &phy_id);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	if (r)
8548c2ecf20Sopenharmony_ci		return ERR_PTR(r);
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
8578c2ecf20Sopenharmony_ci}
8588c2ecf20Sopenharmony_ciEXPORT_SYMBOL(get_phy_device);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci/**
8618c2ecf20Sopenharmony_ci * phy_device_register - Register the phy device on the MDIO bus
8628c2ecf20Sopenharmony_ci * @phydev: phy_device structure to be added to the MDIO bus
8638c2ecf20Sopenharmony_ci */
8648c2ecf20Sopenharmony_ciint phy_device_register(struct phy_device *phydev)
8658c2ecf20Sopenharmony_ci{
8668c2ecf20Sopenharmony_ci	int err;
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	err = mdiobus_register_device(&phydev->mdio);
8698c2ecf20Sopenharmony_ci	if (err)
8708c2ecf20Sopenharmony_ci		return err;
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	/* Deassert the reset signal */
8738c2ecf20Sopenharmony_ci	phy_device_reset(phydev, 0);
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	/* Run all of the fixups for this PHY */
8768c2ecf20Sopenharmony_ci	err = phy_scan_fixups(phydev);
8778c2ecf20Sopenharmony_ci	if (err) {
8788c2ecf20Sopenharmony_ci		phydev_err(phydev, "failed to initialize\n");
8798c2ecf20Sopenharmony_ci		goto out;
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	err = device_add(&phydev->mdio.dev);
8838c2ecf20Sopenharmony_ci	if (err) {
8848c2ecf20Sopenharmony_ci		phydev_err(phydev, "failed to add\n");
8858c2ecf20Sopenharmony_ci		goto out;
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	return 0;
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci out:
8918c2ecf20Sopenharmony_ci	/* Assert the reset signal */
8928c2ecf20Sopenharmony_ci	phy_device_reset(phydev, 1);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	mdiobus_unregister_device(&phydev->mdio);
8958c2ecf20Sopenharmony_ci	return err;
8968c2ecf20Sopenharmony_ci}
8978c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_device_register);
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci/**
9008c2ecf20Sopenharmony_ci * phy_device_remove - Remove a previously registered phy device from the MDIO bus
9018c2ecf20Sopenharmony_ci * @phydev: phy_device structure to remove
9028c2ecf20Sopenharmony_ci *
9038c2ecf20Sopenharmony_ci * This doesn't free the phy_device itself, it merely reverses the effects
9048c2ecf20Sopenharmony_ci * of phy_device_register(). Use phy_device_free() to free the device
9058c2ecf20Sopenharmony_ci * after calling this function.
9068c2ecf20Sopenharmony_ci */
9078c2ecf20Sopenharmony_civoid phy_device_remove(struct phy_device *phydev)
9088c2ecf20Sopenharmony_ci{
9098c2ecf20Sopenharmony_ci	if (phydev->mii_ts)
9108c2ecf20Sopenharmony_ci		unregister_mii_timestamper(phydev->mii_ts);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	device_del(&phydev->mdio.dev);
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	/* Assert the reset signal */
9158c2ecf20Sopenharmony_ci	phy_device_reset(phydev, 1);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	mdiobus_unregister_device(&phydev->mdio);
9188c2ecf20Sopenharmony_ci}
9198c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_device_remove);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci/**
9228c2ecf20Sopenharmony_ci * phy_find_first - finds the first PHY device on the bus
9238c2ecf20Sopenharmony_ci * @bus: the target MII bus
9248c2ecf20Sopenharmony_ci */
9258c2ecf20Sopenharmony_cistruct phy_device *phy_find_first(struct mii_bus *bus)
9268c2ecf20Sopenharmony_ci{
9278c2ecf20Sopenharmony_ci	struct phy_device *phydev;
9288c2ecf20Sopenharmony_ci	int addr;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
9318c2ecf20Sopenharmony_ci		phydev = mdiobus_get_phy(bus, addr);
9328c2ecf20Sopenharmony_ci		if (phydev)
9338c2ecf20Sopenharmony_ci			return phydev;
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci	return NULL;
9368c2ecf20Sopenharmony_ci}
9378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_find_first);
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic void phy_link_change(struct phy_device *phydev, bool up)
9408c2ecf20Sopenharmony_ci{
9418c2ecf20Sopenharmony_ci	struct net_device *netdev = phydev->attached_dev;
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	if (up)
9448c2ecf20Sopenharmony_ci		netif_carrier_on(netdev);
9458c2ecf20Sopenharmony_ci	else
9468c2ecf20Sopenharmony_ci		netif_carrier_off(netdev);
9478c2ecf20Sopenharmony_ci	phydev->adjust_link(netdev);
9488c2ecf20Sopenharmony_ci	if (phydev->mii_ts && phydev->mii_ts->link_state)
9498c2ecf20Sopenharmony_ci		phydev->mii_ts->link_state(phydev->mii_ts, phydev);
9508c2ecf20Sopenharmony_ci}
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci/**
9538c2ecf20Sopenharmony_ci * phy_prepare_link - prepares the PHY layer to monitor link status
9548c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
9558c2ecf20Sopenharmony_ci * @handler: callback function for link status change notifications
9568c2ecf20Sopenharmony_ci *
9578c2ecf20Sopenharmony_ci * Description: Tells the PHY infrastructure to handle the
9588c2ecf20Sopenharmony_ci *   gory details on monitoring link status (whether through
9598c2ecf20Sopenharmony_ci *   polling or an interrupt), and to call back to the
9608c2ecf20Sopenharmony_ci *   connected device driver when the link status changes.
9618c2ecf20Sopenharmony_ci *   If you want to monitor your own link state, don't call
9628c2ecf20Sopenharmony_ci *   this function.
9638c2ecf20Sopenharmony_ci */
9648c2ecf20Sopenharmony_cistatic void phy_prepare_link(struct phy_device *phydev,
9658c2ecf20Sopenharmony_ci			     void (*handler)(struct net_device *))
9668c2ecf20Sopenharmony_ci{
9678c2ecf20Sopenharmony_ci	phydev->adjust_link = handler;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci/**
9718c2ecf20Sopenharmony_ci * phy_connect_direct - connect an ethernet device to a specific phy_device
9728c2ecf20Sopenharmony_ci * @dev: the network device to connect
9738c2ecf20Sopenharmony_ci * @phydev: the pointer to the phy device
9748c2ecf20Sopenharmony_ci * @handler: callback function for state change notifications
9758c2ecf20Sopenharmony_ci * @interface: PHY device's interface
9768c2ecf20Sopenharmony_ci */
9778c2ecf20Sopenharmony_ciint phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
9788c2ecf20Sopenharmony_ci		       void (*handler)(struct net_device *),
9798c2ecf20Sopenharmony_ci		       phy_interface_t interface)
9808c2ecf20Sopenharmony_ci{
9818c2ecf20Sopenharmony_ci	int rc;
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	if (!dev)
9848c2ecf20Sopenharmony_ci		return -EINVAL;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
9878c2ecf20Sopenharmony_ci	if (rc)
9888c2ecf20Sopenharmony_ci		return rc;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	phy_prepare_link(phydev, handler);
9918c2ecf20Sopenharmony_ci	if (phy_interrupt_is_valid(phydev))
9928c2ecf20Sopenharmony_ci		phy_request_interrupt(phydev);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	return 0;
9958c2ecf20Sopenharmony_ci}
9968c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_connect_direct);
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci/**
9998c2ecf20Sopenharmony_ci * phy_connect - connect an ethernet device to a PHY device
10008c2ecf20Sopenharmony_ci * @dev: the network device to connect
10018c2ecf20Sopenharmony_ci * @bus_id: the id string of the PHY device to connect
10028c2ecf20Sopenharmony_ci * @handler: callback function for state change notifications
10038c2ecf20Sopenharmony_ci * @interface: PHY device's interface
10048c2ecf20Sopenharmony_ci *
10058c2ecf20Sopenharmony_ci * Description: Convenience function for connecting ethernet
10068c2ecf20Sopenharmony_ci *   devices to PHY devices.  The default behavior is for
10078c2ecf20Sopenharmony_ci *   the PHY infrastructure to handle everything, and only notify
10088c2ecf20Sopenharmony_ci *   the connected driver when the link status changes.  If you
10098c2ecf20Sopenharmony_ci *   don't want, or can't use the provided functionality, you may
10108c2ecf20Sopenharmony_ci *   choose to call only the subset of functions which provide
10118c2ecf20Sopenharmony_ci *   the desired functionality.
10128c2ecf20Sopenharmony_ci */
10138c2ecf20Sopenharmony_cistruct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
10148c2ecf20Sopenharmony_ci			       void (*handler)(struct net_device *),
10158c2ecf20Sopenharmony_ci			       phy_interface_t interface)
10168c2ecf20Sopenharmony_ci{
10178c2ecf20Sopenharmony_ci	struct phy_device *phydev;
10188c2ecf20Sopenharmony_ci	struct device *d;
10198c2ecf20Sopenharmony_ci	int rc;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	/* Search the list of PHY devices on the mdio bus for the
10228c2ecf20Sopenharmony_ci	 * PHY with the requested name
10238c2ecf20Sopenharmony_ci	 */
10248c2ecf20Sopenharmony_ci	d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
10258c2ecf20Sopenharmony_ci	if (!d) {
10268c2ecf20Sopenharmony_ci		pr_err("PHY %s not found\n", bus_id);
10278c2ecf20Sopenharmony_ci		return ERR_PTR(-ENODEV);
10288c2ecf20Sopenharmony_ci	}
10298c2ecf20Sopenharmony_ci	phydev = to_phy_device(d);
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	rc = phy_connect_direct(dev, phydev, handler, interface);
10328c2ecf20Sopenharmony_ci	put_device(d);
10338c2ecf20Sopenharmony_ci	if (rc)
10348c2ecf20Sopenharmony_ci		return ERR_PTR(rc);
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	return phydev;
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_connect);
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci/**
10418c2ecf20Sopenharmony_ci * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
10428c2ecf20Sopenharmony_ci *		    device
10438c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
10448c2ecf20Sopenharmony_ci */
10458c2ecf20Sopenharmony_civoid phy_disconnect(struct phy_device *phydev)
10468c2ecf20Sopenharmony_ci{
10478c2ecf20Sopenharmony_ci	if (phy_is_started(phydev))
10488c2ecf20Sopenharmony_ci		phy_stop(phydev);
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	if (phy_interrupt_is_valid(phydev))
10518c2ecf20Sopenharmony_ci		phy_free_interrupt(phydev);
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	phydev->adjust_link = NULL;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	phy_detach(phydev);
10568c2ecf20Sopenharmony_ci}
10578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_disconnect);
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci/**
10608c2ecf20Sopenharmony_ci * phy_poll_reset - Safely wait until a PHY reset has properly completed
10618c2ecf20Sopenharmony_ci * @phydev: The PHY device to poll
10628c2ecf20Sopenharmony_ci *
10638c2ecf20Sopenharmony_ci * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
10648c2ecf20Sopenharmony_ci *   published in 2008, a PHY reset may take up to 0.5 seconds.  The MII BMCR
10658c2ecf20Sopenharmony_ci *   register must be polled until the BMCR_RESET bit clears.
10668c2ecf20Sopenharmony_ci *
10678c2ecf20Sopenharmony_ci *   Furthermore, any attempts to write to PHY registers may have no effect
10688c2ecf20Sopenharmony_ci *   or even generate MDIO bus errors until this is complete.
10698c2ecf20Sopenharmony_ci *
10708c2ecf20Sopenharmony_ci *   Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
10718c2ecf20Sopenharmony_ci *   standard and do not fully reset after the BMCR_RESET bit is set, and may
10728c2ecf20Sopenharmony_ci *   even *REQUIRE* a soft-reset to properly restart autonegotiation.  In an
10738c2ecf20Sopenharmony_ci *   effort to support such broken PHYs, this function is separate from the
10748c2ecf20Sopenharmony_ci *   standard phy_init_hw() which will zero all the other bits in the BMCR
10758c2ecf20Sopenharmony_ci *   and reapply all driver-specific and board-specific fixups.
10768c2ecf20Sopenharmony_ci */
10778c2ecf20Sopenharmony_cistatic int phy_poll_reset(struct phy_device *phydev)
10788c2ecf20Sopenharmony_ci{
10798c2ecf20Sopenharmony_ci	/* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
10808c2ecf20Sopenharmony_ci	int ret, val;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
10838c2ecf20Sopenharmony_ci				    50000, 600000, true);
10848c2ecf20Sopenharmony_ci	if (ret)
10858c2ecf20Sopenharmony_ci		return ret;
10868c2ecf20Sopenharmony_ci	/* Some chips (smsc911x) may still need up to another 1ms after the
10878c2ecf20Sopenharmony_ci	 * BMCR_RESET bit is cleared before they are usable.
10888c2ecf20Sopenharmony_ci	 */
10898c2ecf20Sopenharmony_ci	msleep(1);
10908c2ecf20Sopenharmony_ci	return 0;
10918c2ecf20Sopenharmony_ci}
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ciint phy_init_hw(struct phy_device *phydev)
10948c2ecf20Sopenharmony_ci{
10958c2ecf20Sopenharmony_ci	int ret = 0;
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	/* Deassert the reset signal */
10988c2ecf20Sopenharmony_ci	phy_device_reset(phydev, 0);
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	if (!phydev->drv)
11018c2ecf20Sopenharmony_ci		return 0;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	if (phydev->drv->soft_reset) {
11048c2ecf20Sopenharmony_ci		ret = phydev->drv->soft_reset(phydev);
11058c2ecf20Sopenharmony_ci		/* see comment in genphy_soft_reset for an explanation */
11068c2ecf20Sopenharmony_ci		if (!ret)
11078c2ecf20Sopenharmony_ci			phydev->suspended = 0;
11088c2ecf20Sopenharmony_ci	}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	if (ret < 0)
11118c2ecf20Sopenharmony_ci		return ret;
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	ret = phy_scan_fixups(phydev);
11148c2ecf20Sopenharmony_ci	if (ret < 0)
11158c2ecf20Sopenharmony_ci		return ret;
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	if (phydev->drv->config_init) {
11188c2ecf20Sopenharmony_ci		ret = phydev->drv->config_init(phydev);
11198c2ecf20Sopenharmony_ci		if (ret < 0)
11208c2ecf20Sopenharmony_ci			return ret;
11218c2ecf20Sopenharmony_ci	}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	if (phydev->drv->config_intr) {
11248c2ecf20Sopenharmony_ci		ret = phydev->drv->config_intr(phydev);
11258c2ecf20Sopenharmony_ci		if (ret < 0)
11268c2ecf20Sopenharmony_ci			return ret;
11278c2ecf20Sopenharmony_ci	}
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	return 0;
11308c2ecf20Sopenharmony_ci}
11318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_init_hw);
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_civoid phy_attached_info(struct phy_device *phydev)
11348c2ecf20Sopenharmony_ci{
11358c2ecf20Sopenharmony_ci	phy_attached_print(phydev, NULL);
11368c2ecf20Sopenharmony_ci}
11378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_attached_info);
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
11408c2ecf20Sopenharmony_cichar *phy_attached_info_irq(struct phy_device *phydev)
11418c2ecf20Sopenharmony_ci{
11428c2ecf20Sopenharmony_ci	char *irq_str;
11438c2ecf20Sopenharmony_ci	char irq_num[8];
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	switch(phydev->irq) {
11468c2ecf20Sopenharmony_ci	case PHY_POLL:
11478c2ecf20Sopenharmony_ci		irq_str = "POLL";
11488c2ecf20Sopenharmony_ci		break;
11498c2ecf20Sopenharmony_ci	case PHY_IGNORE_INTERRUPT:
11508c2ecf20Sopenharmony_ci		irq_str = "IGNORE";
11518c2ecf20Sopenharmony_ci		break;
11528c2ecf20Sopenharmony_ci	default:
11538c2ecf20Sopenharmony_ci		snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
11548c2ecf20Sopenharmony_ci		irq_str = irq_num;
11558c2ecf20Sopenharmony_ci		break;
11568c2ecf20Sopenharmony_ci	}
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	return kasprintf(GFP_KERNEL, "%s", irq_str);
11598c2ecf20Sopenharmony_ci}
11608c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_attached_info_irq);
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_civoid phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
11638c2ecf20Sopenharmony_ci{
11648c2ecf20Sopenharmony_ci	const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
11658c2ecf20Sopenharmony_ci	char *irq_str = phy_attached_info_irq(phydev);
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	if (!fmt) {
11688c2ecf20Sopenharmony_ci		phydev_info(phydev, ATTACHED_FMT "\n",
11698c2ecf20Sopenharmony_ci			 drv_name, phydev_name(phydev),
11708c2ecf20Sopenharmony_ci			 irq_str);
11718c2ecf20Sopenharmony_ci	} else {
11728c2ecf20Sopenharmony_ci		va_list ap;
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci		phydev_info(phydev, ATTACHED_FMT,
11758c2ecf20Sopenharmony_ci			 drv_name, phydev_name(phydev),
11768c2ecf20Sopenharmony_ci			 irq_str);
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci		va_start(ap, fmt);
11798c2ecf20Sopenharmony_ci		vprintk(fmt, ap);
11808c2ecf20Sopenharmony_ci		va_end(ap);
11818c2ecf20Sopenharmony_ci	}
11828c2ecf20Sopenharmony_ci	kfree(irq_str);
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_attached_print);
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_cistatic void phy_sysfs_create_links(struct phy_device *phydev)
11878c2ecf20Sopenharmony_ci{
11888c2ecf20Sopenharmony_ci	struct net_device *dev = phydev->attached_dev;
11898c2ecf20Sopenharmony_ci	int err;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	if (!dev)
11928c2ecf20Sopenharmony_ci		return;
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
11958c2ecf20Sopenharmony_ci				"attached_dev");
11968c2ecf20Sopenharmony_ci	if (err)
11978c2ecf20Sopenharmony_ci		return;
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	err = sysfs_create_link_nowarn(&dev->dev.kobj,
12008c2ecf20Sopenharmony_ci				       &phydev->mdio.dev.kobj,
12018c2ecf20Sopenharmony_ci				       "phydev");
12028c2ecf20Sopenharmony_ci	if (err) {
12038c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "could not add device link to %s err %d\n",
12048c2ecf20Sopenharmony_ci			kobject_name(&phydev->mdio.dev.kobj),
12058c2ecf20Sopenharmony_ci			err);
12068c2ecf20Sopenharmony_ci		/* non-fatal - some net drivers can use one netdevice
12078c2ecf20Sopenharmony_ci		 * with more then one phy
12088c2ecf20Sopenharmony_ci		 */
12098c2ecf20Sopenharmony_ci	}
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	phydev->sysfs_links = true;
12128c2ecf20Sopenharmony_ci}
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_cistatic ssize_t
12158c2ecf20Sopenharmony_ciphy_standalone_show(struct device *dev, struct device_attribute *attr,
12168c2ecf20Sopenharmony_ci		    char *buf)
12178c2ecf20Sopenharmony_ci{
12188c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !phydev->attached_dev);
12218c2ecf20Sopenharmony_ci}
12228c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(phy_standalone);
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci/**
12258c2ecf20Sopenharmony_ci * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
12268c2ecf20Sopenharmony_ci * @upstream: pointer to the phy device
12278c2ecf20Sopenharmony_ci * @bus: sfp bus representing cage being attached
12288c2ecf20Sopenharmony_ci *
12298c2ecf20Sopenharmony_ci * This is used to fill in the sfp_upstream_ops .attach member.
12308c2ecf20Sopenharmony_ci */
12318c2ecf20Sopenharmony_civoid phy_sfp_attach(void *upstream, struct sfp_bus *bus)
12328c2ecf20Sopenharmony_ci{
12338c2ecf20Sopenharmony_ci	struct phy_device *phydev = upstream;
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	if (phydev->attached_dev)
12368c2ecf20Sopenharmony_ci		phydev->attached_dev->sfp_bus = bus;
12378c2ecf20Sopenharmony_ci	phydev->sfp_bus_attached = true;
12388c2ecf20Sopenharmony_ci}
12398c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_sfp_attach);
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci/**
12428c2ecf20Sopenharmony_ci * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
12438c2ecf20Sopenharmony_ci * @upstream: pointer to the phy device
12448c2ecf20Sopenharmony_ci * @bus: sfp bus representing cage being attached
12458c2ecf20Sopenharmony_ci *
12468c2ecf20Sopenharmony_ci * This is used to fill in the sfp_upstream_ops .detach member.
12478c2ecf20Sopenharmony_ci */
12488c2ecf20Sopenharmony_civoid phy_sfp_detach(void *upstream, struct sfp_bus *bus)
12498c2ecf20Sopenharmony_ci{
12508c2ecf20Sopenharmony_ci	struct phy_device *phydev = upstream;
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	if (phydev->attached_dev)
12538c2ecf20Sopenharmony_ci		phydev->attached_dev->sfp_bus = NULL;
12548c2ecf20Sopenharmony_ci	phydev->sfp_bus_attached = false;
12558c2ecf20Sopenharmony_ci}
12568c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_sfp_detach);
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci/**
12598c2ecf20Sopenharmony_ci * phy_sfp_probe - probe for a SFP cage attached to this PHY device
12608c2ecf20Sopenharmony_ci * @phydev: Pointer to phy_device
12618c2ecf20Sopenharmony_ci * @ops: SFP's upstream operations
12628c2ecf20Sopenharmony_ci */
12638c2ecf20Sopenharmony_ciint phy_sfp_probe(struct phy_device *phydev,
12648c2ecf20Sopenharmony_ci		  const struct sfp_upstream_ops *ops)
12658c2ecf20Sopenharmony_ci{
12668c2ecf20Sopenharmony_ci	struct sfp_bus *bus;
12678c2ecf20Sopenharmony_ci	int ret = 0;
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	if (phydev->mdio.dev.fwnode) {
12708c2ecf20Sopenharmony_ci		bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
12718c2ecf20Sopenharmony_ci		if (IS_ERR(bus))
12728c2ecf20Sopenharmony_ci			return PTR_ERR(bus);
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci		phydev->sfp_bus = bus;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci		ret = sfp_bus_add_upstream(bus, phydev, ops);
12778c2ecf20Sopenharmony_ci		sfp_bus_put(bus);
12788c2ecf20Sopenharmony_ci	}
12798c2ecf20Sopenharmony_ci	return ret;
12808c2ecf20Sopenharmony_ci}
12818c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_sfp_probe);
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci/**
12848c2ecf20Sopenharmony_ci * phy_attach_direct - attach a network device to a given PHY device pointer
12858c2ecf20Sopenharmony_ci * @dev: network device to attach
12868c2ecf20Sopenharmony_ci * @phydev: Pointer to phy_device to attach
12878c2ecf20Sopenharmony_ci * @flags: PHY device's dev_flags
12888c2ecf20Sopenharmony_ci * @interface: PHY device's interface
12898c2ecf20Sopenharmony_ci *
12908c2ecf20Sopenharmony_ci * Description: Called by drivers to attach to a particular PHY
12918c2ecf20Sopenharmony_ci *     device. The phy_device is found, and properly hooked up
12928c2ecf20Sopenharmony_ci *     to the phy_driver.  If no driver is attached, then a
12938c2ecf20Sopenharmony_ci *     generic driver is used.  The phy_device is given a ptr to
12948c2ecf20Sopenharmony_ci *     the attaching device, and given a callback for link status
12958c2ecf20Sopenharmony_ci *     change.  The phy_device is returned to the attaching driver.
12968c2ecf20Sopenharmony_ci *     This function takes a reference on the phy device.
12978c2ecf20Sopenharmony_ci */
12988c2ecf20Sopenharmony_ciint phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
12998c2ecf20Sopenharmony_ci		      u32 flags, phy_interface_t interface)
13008c2ecf20Sopenharmony_ci{
13018c2ecf20Sopenharmony_ci	struct mii_bus *bus = phydev->mdio.bus;
13028c2ecf20Sopenharmony_ci	struct device *d = &phydev->mdio.dev;
13038c2ecf20Sopenharmony_ci	struct module *ndev_owner = NULL;
13048c2ecf20Sopenharmony_ci	bool using_genphy = false;
13058c2ecf20Sopenharmony_ci	int err;
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	/* For Ethernet device drivers that register their own MDIO bus, we
13088c2ecf20Sopenharmony_ci	 * will have bus->owner match ndev_mod, so we do not want to increment
13098c2ecf20Sopenharmony_ci	 * our own module->refcnt here, otherwise we would not be able to
13108c2ecf20Sopenharmony_ci	 * unload later on.
13118c2ecf20Sopenharmony_ci	 */
13128c2ecf20Sopenharmony_ci	if (dev)
13138c2ecf20Sopenharmony_ci		ndev_owner = dev->dev.parent->driver->owner;
13148c2ecf20Sopenharmony_ci	if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
13158c2ecf20Sopenharmony_ci		phydev_err(phydev, "failed to get the bus module\n");
13168c2ecf20Sopenharmony_ci		return -EIO;
13178c2ecf20Sopenharmony_ci	}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	get_device(d);
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	/* Assume that if there is no driver, that it doesn't
13228c2ecf20Sopenharmony_ci	 * exist, and we should use the genphy driver.
13238c2ecf20Sopenharmony_ci	 */
13248c2ecf20Sopenharmony_ci	if (!d->driver) {
13258c2ecf20Sopenharmony_ci		if (phydev->is_c45)
13268c2ecf20Sopenharmony_ci			d->driver = &genphy_c45_driver.mdiodrv.driver;
13278c2ecf20Sopenharmony_ci		else
13288c2ecf20Sopenharmony_ci			d->driver = &genphy_driver.mdiodrv.driver;
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci		using_genphy = true;
13318c2ecf20Sopenharmony_ci	}
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	if (!try_module_get(d->driver->owner)) {
13348c2ecf20Sopenharmony_ci		phydev_err(phydev, "failed to get the device driver module\n");
13358c2ecf20Sopenharmony_ci		err = -EIO;
13368c2ecf20Sopenharmony_ci		goto error_put_device;
13378c2ecf20Sopenharmony_ci	}
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	if (using_genphy) {
13408c2ecf20Sopenharmony_ci		err = d->driver->probe(d);
13418c2ecf20Sopenharmony_ci		if (err >= 0)
13428c2ecf20Sopenharmony_ci			err = device_bind_driver(d);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci		if (err)
13458c2ecf20Sopenharmony_ci			goto error_module_put;
13468c2ecf20Sopenharmony_ci	}
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	if (phydev->attached_dev) {
13498c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "PHY already attached\n");
13508c2ecf20Sopenharmony_ci		err = -EBUSY;
13518c2ecf20Sopenharmony_ci		goto error;
13528c2ecf20Sopenharmony_ci	}
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci	phydev->phy_link_change = phy_link_change;
13558c2ecf20Sopenharmony_ci	if (dev) {
13568c2ecf20Sopenharmony_ci		phydev->attached_dev = dev;
13578c2ecf20Sopenharmony_ci		dev->phydev = phydev;
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci		if (phydev->sfp_bus_attached)
13608c2ecf20Sopenharmony_ci			dev->sfp_bus = phydev->sfp_bus;
13618c2ecf20Sopenharmony_ci	}
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	/* Some Ethernet drivers try to connect to a PHY device before
13648c2ecf20Sopenharmony_ci	 * calling register_netdevice() -> netdev_register_kobject() and
13658c2ecf20Sopenharmony_ci	 * does the dev->dev.kobj initialization. Here we only check for
13668c2ecf20Sopenharmony_ci	 * success which indicates that the network device kobject is
13678c2ecf20Sopenharmony_ci	 * ready. Once we do that we still need to keep track of whether
13688c2ecf20Sopenharmony_ci	 * links were successfully set up or not for phy_detach() to
13698c2ecf20Sopenharmony_ci	 * remove them accordingly.
13708c2ecf20Sopenharmony_ci	 */
13718c2ecf20Sopenharmony_ci	phydev->sysfs_links = false;
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_ci	phy_sysfs_create_links(phydev);
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	if (!phydev->attached_dev) {
13768c2ecf20Sopenharmony_ci		err = sysfs_create_file(&phydev->mdio.dev.kobj,
13778c2ecf20Sopenharmony_ci					&dev_attr_phy_standalone.attr);
13788c2ecf20Sopenharmony_ci		if (err)
13798c2ecf20Sopenharmony_ci			phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
13808c2ecf20Sopenharmony_ci	}
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	phydev->dev_flags |= flags;
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	phydev->interface = interface;
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	phydev->state = PHY_READY;
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci	/* Port is set to PORT_TP by default and the actual PHY driver will set
13898c2ecf20Sopenharmony_ci	 * it to different value depending on the PHY configuration. If we have
13908c2ecf20Sopenharmony_ci	 * the generic PHY driver we can't figure it out, thus set the old
13918c2ecf20Sopenharmony_ci	 * legacy PORT_MII value.
13928c2ecf20Sopenharmony_ci	 */
13938c2ecf20Sopenharmony_ci	if (using_genphy)
13948c2ecf20Sopenharmony_ci		phydev->port = PORT_MII;
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	/* Initial carrier state is off as the phy is about to be
13978c2ecf20Sopenharmony_ci	 * (re)initialized.
13988c2ecf20Sopenharmony_ci	 */
13998c2ecf20Sopenharmony_ci	if (dev)
14008c2ecf20Sopenharmony_ci		netif_carrier_off(phydev->attached_dev);
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci	/* Do initial configuration here, now that
14038c2ecf20Sopenharmony_ci	 * we have certain key parameters
14048c2ecf20Sopenharmony_ci	 * (dev_flags and interface)
14058c2ecf20Sopenharmony_ci	 */
14068c2ecf20Sopenharmony_ci	err = phy_init_hw(phydev);
14078c2ecf20Sopenharmony_ci	if (err)
14088c2ecf20Sopenharmony_ci		goto error;
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	err = phy_disable_interrupts(phydev);
14118c2ecf20Sopenharmony_ci	if (err)
14128c2ecf20Sopenharmony_ci		return err;
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci	phy_resume(phydev);
14158c2ecf20Sopenharmony_ci	phy_led_triggers_register(phydev);
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	return err;
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_cierror:
14208c2ecf20Sopenharmony_ci	/* phy_detach() does all of the cleanup below */
14218c2ecf20Sopenharmony_ci	phy_detach(phydev);
14228c2ecf20Sopenharmony_ci	return err;
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_cierror_module_put:
14258c2ecf20Sopenharmony_ci	module_put(d->driver->owner);
14268c2ecf20Sopenharmony_ci	d->driver = NULL;
14278c2ecf20Sopenharmony_cierror_put_device:
14288c2ecf20Sopenharmony_ci	put_device(d);
14298c2ecf20Sopenharmony_ci	if (ndev_owner != bus->owner)
14308c2ecf20Sopenharmony_ci		module_put(bus->owner);
14318c2ecf20Sopenharmony_ci	return err;
14328c2ecf20Sopenharmony_ci}
14338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_attach_direct);
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ci/**
14368c2ecf20Sopenharmony_ci * phy_attach - attach a network device to a particular PHY device
14378c2ecf20Sopenharmony_ci * @dev: network device to attach
14388c2ecf20Sopenharmony_ci * @bus_id: Bus ID of PHY device to attach
14398c2ecf20Sopenharmony_ci * @interface: PHY device's interface
14408c2ecf20Sopenharmony_ci *
14418c2ecf20Sopenharmony_ci * Description: Same as phy_attach_direct() except that a PHY bus_id
14428c2ecf20Sopenharmony_ci *     string is passed instead of a pointer to a struct phy_device.
14438c2ecf20Sopenharmony_ci */
14448c2ecf20Sopenharmony_cistruct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
14458c2ecf20Sopenharmony_ci			      phy_interface_t interface)
14468c2ecf20Sopenharmony_ci{
14478c2ecf20Sopenharmony_ci	struct bus_type *bus = &mdio_bus_type;
14488c2ecf20Sopenharmony_ci	struct phy_device *phydev;
14498c2ecf20Sopenharmony_ci	struct device *d;
14508c2ecf20Sopenharmony_ci	int rc;
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	if (!dev)
14538c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	/* Search the list of PHY devices on the mdio bus for the
14568c2ecf20Sopenharmony_ci	 * PHY with the requested name
14578c2ecf20Sopenharmony_ci	 */
14588c2ecf20Sopenharmony_ci	d = bus_find_device_by_name(bus, NULL, bus_id);
14598c2ecf20Sopenharmony_ci	if (!d) {
14608c2ecf20Sopenharmony_ci		pr_err("PHY %s not found\n", bus_id);
14618c2ecf20Sopenharmony_ci		return ERR_PTR(-ENODEV);
14628c2ecf20Sopenharmony_ci	}
14638c2ecf20Sopenharmony_ci	phydev = to_phy_device(d);
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
14668c2ecf20Sopenharmony_ci	put_device(d);
14678c2ecf20Sopenharmony_ci	if (rc)
14688c2ecf20Sopenharmony_ci		return ERR_PTR(rc);
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	return phydev;
14718c2ecf20Sopenharmony_ci}
14728c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_attach);
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_cistatic bool phy_driver_is_genphy_kind(struct phy_device *phydev,
14758c2ecf20Sopenharmony_ci				      struct device_driver *driver)
14768c2ecf20Sopenharmony_ci{
14778c2ecf20Sopenharmony_ci	struct device *d = &phydev->mdio.dev;
14788c2ecf20Sopenharmony_ci	bool ret = false;
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	if (!phydev->drv)
14818c2ecf20Sopenharmony_ci		return ret;
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci	get_device(d);
14848c2ecf20Sopenharmony_ci	ret = d->driver == driver;
14858c2ecf20Sopenharmony_ci	put_device(d);
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	return ret;
14888c2ecf20Sopenharmony_ci}
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_cibool phy_driver_is_genphy(struct phy_device *phydev)
14918c2ecf20Sopenharmony_ci{
14928c2ecf20Sopenharmony_ci	return phy_driver_is_genphy_kind(phydev,
14938c2ecf20Sopenharmony_ci					 &genphy_driver.mdiodrv.driver);
14948c2ecf20Sopenharmony_ci}
14958c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_driver_is_genphy);
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_cibool phy_driver_is_genphy_10g(struct phy_device *phydev)
14988c2ecf20Sopenharmony_ci{
14998c2ecf20Sopenharmony_ci	return phy_driver_is_genphy_kind(phydev,
15008c2ecf20Sopenharmony_ci					 &genphy_c45_driver.mdiodrv.driver);
15018c2ecf20Sopenharmony_ci}
15028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci/**
15058c2ecf20Sopenharmony_ci * phy_package_join - join a common PHY group
15068c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
15078c2ecf20Sopenharmony_ci * @addr: cookie and PHY address for global register access
15088c2ecf20Sopenharmony_ci * @priv_size: if non-zero allocate this amount of bytes for private data
15098c2ecf20Sopenharmony_ci *
15108c2ecf20Sopenharmony_ci * This joins a PHY group and provides a shared storage for all phydevs in
15118c2ecf20Sopenharmony_ci * this group. This is intended to be used for packages which contain
15128c2ecf20Sopenharmony_ci * more than one PHY, for example a quad PHY transceiver.
15138c2ecf20Sopenharmony_ci *
15148c2ecf20Sopenharmony_ci * The addr parameter serves as a cookie which has to have the same value
15158c2ecf20Sopenharmony_ci * for all members of one group and as a PHY address to access generic
15168c2ecf20Sopenharmony_ci * registers of a PHY package. Usually, one of the PHY addresses of the
15178c2ecf20Sopenharmony_ci * different PHYs in the package provides access to these global registers.
15188c2ecf20Sopenharmony_ci * The address which is given here, will be used in the phy_package_read()
15198c2ecf20Sopenharmony_ci * and phy_package_write() convenience functions. If your PHY doesn't have
15208c2ecf20Sopenharmony_ci * global registers you can just pick any of the PHY addresses.
15218c2ecf20Sopenharmony_ci *
15228c2ecf20Sopenharmony_ci * This will set the shared pointer of the phydev to the shared storage.
15238c2ecf20Sopenharmony_ci * If this is the first call for a this cookie the shared storage will be
15248c2ecf20Sopenharmony_ci * allocated. If priv_size is non-zero, the given amount of bytes are
15258c2ecf20Sopenharmony_ci * allocated for the priv member.
15268c2ecf20Sopenharmony_ci *
15278c2ecf20Sopenharmony_ci * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
15288c2ecf20Sopenharmony_ci * with the same cookie but a different priv_size is an error.
15298c2ecf20Sopenharmony_ci */
15308c2ecf20Sopenharmony_ciint phy_package_join(struct phy_device *phydev, int addr, size_t priv_size)
15318c2ecf20Sopenharmony_ci{
15328c2ecf20Sopenharmony_ci	struct mii_bus *bus = phydev->mdio.bus;
15338c2ecf20Sopenharmony_ci	struct phy_package_shared *shared;
15348c2ecf20Sopenharmony_ci	int ret;
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci	if (addr < 0 || addr >= PHY_MAX_ADDR)
15378c2ecf20Sopenharmony_ci		return -EINVAL;
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_ci	mutex_lock(&bus->shared_lock);
15408c2ecf20Sopenharmony_ci	shared = bus->shared[addr];
15418c2ecf20Sopenharmony_ci	if (!shared) {
15428c2ecf20Sopenharmony_ci		ret = -ENOMEM;
15438c2ecf20Sopenharmony_ci		shared = kzalloc(sizeof(*shared), GFP_KERNEL);
15448c2ecf20Sopenharmony_ci		if (!shared)
15458c2ecf20Sopenharmony_ci			goto err_unlock;
15468c2ecf20Sopenharmony_ci		if (priv_size) {
15478c2ecf20Sopenharmony_ci			shared->priv = kzalloc(priv_size, GFP_KERNEL);
15488c2ecf20Sopenharmony_ci			if (!shared->priv)
15498c2ecf20Sopenharmony_ci				goto err_free;
15508c2ecf20Sopenharmony_ci			shared->priv_size = priv_size;
15518c2ecf20Sopenharmony_ci		}
15528c2ecf20Sopenharmony_ci		shared->addr = addr;
15538c2ecf20Sopenharmony_ci		refcount_set(&shared->refcnt, 1);
15548c2ecf20Sopenharmony_ci		bus->shared[addr] = shared;
15558c2ecf20Sopenharmony_ci	} else {
15568c2ecf20Sopenharmony_ci		ret = -EINVAL;
15578c2ecf20Sopenharmony_ci		if (priv_size && priv_size != shared->priv_size)
15588c2ecf20Sopenharmony_ci			goto err_unlock;
15598c2ecf20Sopenharmony_ci		refcount_inc(&shared->refcnt);
15608c2ecf20Sopenharmony_ci	}
15618c2ecf20Sopenharmony_ci	mutex_unlock(&bus->shared_lock);
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	phydev->shared = shared;
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	return 0;
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_cierr_free:
15688c2ecf20Sopenharmony_ci	kfree(shared);
15698c2ecf20Sopenharmony_cierr_unlock:
15708c2ecf20Sopenharmony_ci	mutex_unlock(&bus->shared_lock);
15718c2ecf20Sopenharmony_ci	return ret;
15728c2ecf20Sopenharmony_ci}
15738c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_package_join);
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci/**
15768c2ecf20Sopenharmony_ci * phy_package_leave - leave a common PHY group
15778c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
15788c2ecf20Sopenharmony_ci *
15798c2ecf20Sopenharmony_ci * This leaves a PHY group created by phy_package_join(). If this phydev
15808c2ecf20Sopenharmony_ci * was the last user of the shared data between the group, this data is
15818c2ecf20Sopenharmony_ci * freed. Resets the phydev->shared pointer to NULL.
15828c2ecf20Sopenharmony_ci */
15838c2ecf20Sopenharmony_civoid phy_package_leave(struct phy_device *phydev)
15848c2ecf20Sopenharmony_ci{
15858c2ecf20Sopenharmony_ci	struct phy_package_shared *shared = phydev->shared;
15868c2ecf20Sopenharmony_ci	struct mii_bus *bus = phydev->mdio.bus;
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_ci	if (!shared)
15898c2ecf20Sopenharmony_ci		return;
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
15928c2ecf20Sopenharmony_ci		bus->shared[shared->addr] = NULL;
15938c2ecf20Sopenharmony_ci		mutex_unlock(&bus->shared_lock);
15948c2ecf20Sopenharmony_ci		kfree(shared->priv);
15958c2ecf20Sopenharmony_ci		kfree(shared);
15968c2ecf20Sopenharmony_ci	}
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_ci	phydev->shared = NULL;
15998c2ecf20Sopenharmony_ci}
16008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(phy_package_leave);
16018c2ecf20Sopenharmony_ci
16028c2ecf20Sopenharmony_cistatic void devm_phy_package_leave(struct device *dev, void *res)
16038c2ecf20Sopenharmony_ci{
16048c2ecf20Sopenharmony_ci	phy_package_leave(*(struct phy_device **)res);
16058c2ecf20Sopenharmony_ci}
16068c2ecf20Sopenharmony_ci
16078c2ecf20Sopenharmony_ci/**
16088c2ecf20Sopenharmony_ci * devm_phy_package_join - resource managed phy_package_join()
16098c2ecf20Sopenharmony_ci * @dev: device that is registering this PHY package
16108c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
16118c2ecf20Sopenharmony_ci * @addr: cookie and PHY address for global register access
16128c2ecf20Sopenharmony_ci * @priv_size: if non-zero allocate this amount of bytes for private data
16138c2ecf20Sopenharmony_ci *
16148c2ecf20Sopenharmony_ci * Managed phy_package_join(). Shared storage fetched by this function,
16158c2ecf20Sopenharmony_ci * phy_package_leave() is automatically called on driver detach. See
16168c2ecf20Sopenharmony_ci * phy_package_join() for more information.
16178c2ecf20Sopenharmony_ci */
16188c2ecf20Sopenharmony_ciint devm_phy_package_join(struct device *dev, struct phy_device *phydev,
16198c2ecf20Sopenharmony_ci			  int addr, size_t priv_size)
16208c2ecf20Sopenharmony_ci{
16218c2ecf20Sopenharmony_ci	struct phy_device **ptr;
16228c2ecf20Sopenharmony_ci	int ret;
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ci	ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
16258c2ecf20Sopenharmony_ci			   GFP_KERNEL);
16268c2ecf20Sopenharmony_ci	if (!ptr)
16278c2ecf20Sopenharmony_ci		return -ENOMEM;
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	ret = phy_package_join(phydev, addr, priv_size);
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci	if (!ret) {
16328c2ecf20Sopenharmony_ci		*ptr = phydev;
16338c2ecf20Sopenharmony_ci		devres_add(dev, ptr);
16348c2ecf20Sopenharmony_ci	} else {
16358c2ecf20Sopenharmony_ci		devres_free(ptr);
16368c2ecf20Sopenharmony_ci	}
16378c2ecf20Sopenharmony_ci
16388c2ecf20Sopenharmony_ci	return ret;
16398c2ecf20Sopenharmony_ci}
16408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_phy_package_join);
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci/**
16438c2ecf20Sopenharmony_ci * phy_detach - detach a PHY device from its network device
16448c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
16458c2ecf20Sopenharmony_ci *
16468c2ecf20Sopenharmony_ci * This detaches the phy device from its network device and the phy
16478c2ecf20Sopenharmony_ci * driver, and drops the reference count taken in phy_attach_direct().
16488c2ecf20Sopenharmony_ci */
16498c2ecf20Sopenharmony_civoid phy_detach(struct phy_device *phydev)
16508c2ecf20Sopenharmony_ci{
16518c2ecf20Sopenharmony_ci	struct net_device *dev = phydev->attached_dev;
16528c2ecf20Sopenharmony_ci	struct module *ndev_owner = NULL;
16538c2ecf20Sopenharmony_ci	struct mii_bus *bus;
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci	if (phydev->sysfs_links) {
16568c2ecf20Sopenharmony_ci		if (dev)
16578c2ecf20Sopenharmony_ci			sysfs_remove_link(&dev->dev.kobj, "phydev");
16588c2ecf20Sopenharmony_ci		sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
16598c2ecf20Sopenharmony_ci	}
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci	if (!phydev->attached_dev)
16628c2ecf20Sopenharmony_ci		sysfs_remove_file(&phydev->mdio.dev.kobj,
16638c2ecf20Sopenharmony_ci				  &dev_attr_phy_standalone.attr);
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_ci	phy_suspend(phydev);
16668c2ecf20Sopenharmony_ci	if (dev) {
16678c2ecf20Sopenharmony_ci		phydev->attached_dev->phydev = NULL;
16688c2ecf20Sopenharmony_ci		phydev->attached_dev = NULL;
16698c2ecf20Sopenharmony_ci	}
16708c2ecf20Sopenharmony_ci	phydev->phylink = NULL;
16718c2ecf20Sopenharmony_ci
16728c2ecf20Sopenharmony_ci	phy_led_triggers_unregister(phydev);
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	if (phydev->mdio.dev.driver)
16758c2ecf20Sopenharmony_ci		module_put(phydev->mdio.dev.driver->owner);
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	/* If the device had no specific driver before (i.e. - it
16788c2ecf20Sopenharmony_ci	 * was using the generic driver), we unbind the device
16798c2ecf20Sopenharmony_ci	 * from the generic driver so that there's a chance a
16808c2ecf20Sopenharmony_ci	 * real driver could be loaded
16818c2ecf20Sopenharmony_ci	 */
16828c2ecf20Sopenharmony_ci	if (phy_driver_is_genphy(phydev) ||
16838c2ecf20Sopenharmony_ci	    phy_driver_is_genphy_10g(phydev))
16848c2ecf20Sopenharmony_ci		device_release_driver(&phydev->mdio.dev);
16858c2ecf20Sopenharmony_ci
16868c2ecf20Sopenharmony_ci	/* Assert the reset signal */
16878c2ecf20Sopenharmony_ci	phy_device_reset(phydev, 1);
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci	/*
16908c2ecf20Sopenharmony_ci	 * The phydev might go away on the put_device() below, so avoid
16918c2ecf20Sopenharmony_ci	 * a use-after-free bug by reading the underlying bus first.
16928c2ecf20Sopenharmony_ci	 */
16938c2ecf20Sopenharmony_ci	bus = phydev->mdio.bus;
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci	put_device(&phydev->mdio.dev);
16968c2ecf20Sopenharmony_ci	if (dev)
16978c2ecf20Sopenharmony_ci		ndev_owner = dev->dev.parent->driver->owner;
16988c2ecf20Sopenharmony_ci	if (ndev_owner != bus->owner)
16998c2ecf20Sopenharmony_ci		module_put(bus->owner);
17008c2ecf20Sopenharmony_ci}
17018c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_detach);
17028c2ecf20Sopenharmony_ci
17038c2ecf20Sopenharmony_ciint phy_suspend(struct phy_device *phydev)
17048c2ecf20Sopenharmony_ci{
17058c2ecf20Sopenharmony_ci	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
17068c2ecf20Sopenharmony_ci	struct net_device *netdev = phydev->attached_dev;
17078c2ecf20Sopenharmony_ci	struct phy_driver *phydrv = phydev->drv;
17088c2ecf20Sopenharmony_ci	int ret;
17098c2ecf20Sopenharmony_ci
17108c2ecf20Sopenharmony_ci	if (phydev->suspended)
17118c2ecf20Sopenharmony_ci		return 0;
17128c2ecf20Sopenharmony_ci
17138c2ecf20Sopenharmony_ci	/* If the device has WOL enabled, we cannot suspend the PHY */
17148c2ecf20Sopenharmony_ci	phy_ethtool_get_wol(phydev, &wol);
17158c2ecf20Sopenharmony_ci	if (wol.wolopts || (netdev && netdev->wol_enabled))
17168c2ecf20Sopenharmony_ci		return -EBUSY;
17178c2ecf20Sopenharmony_ci
17188c2ecf20Sopenharmony_ci	if (!phydrv || !phydrv->suspend)
17198c2ecf20Sopenharmony_ci		return 0;
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	ret = phydrv->suspend(phydev);
17228c2ecf20Sopenharmony_ci	if (!ret)
17238c2ecf20Sopenharmony_ci		phydev->suspended = true;
17248c2ecf20Sopenharmony_ci
17258c2ecf20Sopenharmony_ci	return ret;
17268c2ecf20Sopenharmony_ci}
17278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_suspend);
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ciint __phy_resume(struct phy_device *phydev)
17308c2ecf20Sopenharmony_ci{
17318c2ecf20Sopenharmony_ci	struct phy_driver *phydrv = phydev->drv;
17328c2ecf20Sopenharmony_ci	int ret;
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	WARN_ON(!mutex_is_locked(&phydev->lock));
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_ci	if (!phydrv || !phydrv->resume)
17378c2ecf20Sopenharmony_ci		return 0;
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci	ret = phydrv->resume(phydev);
17408c2ecf20Sopenharmony_ci	if (!ret)
17418c2ecf20Sopenharmony_ci		phydev->suspended = false;
17428c2ecf20Sopenharmony_ci
17438c2ecf20Sopenharmony_ci	return ret;
17448c2ecf20Sopenharmony_ci}
17458c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__phy_resume);
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ciint phy_resume(struct phy_device *phydev)
17488c2ecf20Sopenharmony_ci{
17498c2ecf20Sopenharmony_ci	int ret;
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_ci	mutex_lock(&phydev->lock);
17528c2ecf20Sopenharmony_ci	ret = __phy_resume(phydev);
17538c2ecf20Sopenharmony_ci	mutex_unlock(&phydev->lock);
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	return ret;
17568c2ecf20Sopenharmony_ci}
17578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_resume);
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ciint phy_loopback(struct phy_device *phydev, bool enable)
17608c2ecf20Sopenharmony_ci{
17618c2ecf20Sopenharmony_ci	struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
17628c2ecf20Sopenharmony_ci	int ret = 0;
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci	mutex_lock(&phydev->lock);
17658c2ecf20Sopenharmony_ci
17668c2ecf20Sopenharmony_ci	if (enable && phydev->loopback_enabled) {
17678c2ecf20Sopenharmony_ci		ret = -EBUSY;
17688c2ecf20Sopenharmony_ci		goto out;
17698c2ecf20Sopenharmony_ci	}
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci	if (!enable && !phydev->loopback_enabled) {
17728c2ecf20Sopenharmony_ci		ret = -EINVAL;
17738c2ecf20Sopenharmony_ci		goto out;
17748c2ecf20Sopenharmony_ci	}
17758c2ecf20Sopenharmony_ci
17768c2ecf20Sopenharmony_ci	if (phydev->drv && phydrv->set_loopback)
17778c2ecf20Sopenharmony_ci		ret = phydrv->set_loopback(phydev, enable);
17788c2ecf20Sopenharmony_ci	else
17798c2ecf20Sopenharmony_ci		ret = -EOPNOTSUPP;
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_ci	if (ret)
17828c2ecf20Sopenharmony_ci		goto out;
17838c2ecf20Sopenharmony_ci
17848c2ecf20Sopenharmony_ci	phydev->loopback_enabled = enable;
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ciout:
17878c2ecf20Sopenharmony_ci	mutex_unlock(&phydev->lock);
17888c2ecf20Sopenharmony_ci	return ret;
17898c2ecf20Sopenharmony_ci}
17908c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_loopback);
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci/**
17938c2ecf20Sopenharmony_ci * phy_reset_after_clk_enable - perform a PHY reset if needed
17948c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
17958c2ecf20Sopenharmony_ci *
17968c2ecf20Sopenharmony_ci * Description: Some PHYs are known to need a reset after their refclk was
17978c2ecf20Sopenharmony_ci *   enabled. This function evaluates the flags and perform the reset if it's
17988c2ecf20Sopenharmony_ci *   needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
17998c2ecf20Sopenharmony_ci *   was reset.
18008c2ecf20Sopenharmony_ci */
18018c2ecf20Sopenharmony_ciint phy_reset_after_clk_enable(struct phy_device *phydev)
18028c2ecf20Sopenharmony_ci{
18038c2ecf20Sopenharmony_ci	if (!phydev || !phydev->drv)
18048c2ecf20Sopenharmony_ci		return -ENODEV;
18058c2ecf20Sopenharmony_ci
18068c2ecf20Sopenharmony_ci	if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
18078c2ecf20Sopenharmony_ci		phy_device_reset(phydev, 1);
18088c2ecf20Sopenharmony_ci		phy_device_reset(phydev, 0);
18098c2ecf20Sopenharmony_ci		return 1;
18108c2ecf20Sopenharmony_ci	}
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci	return 0;
18138c2ecf20Sopenharmony_ci}
18148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_reset_after_clk_enable);
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci/* Generic PHY support and helper functions */
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci/**
18198c2ecf20Sopenharmony_ci * genphy_config_advert - sanitize and advertise auto-negotiation parameters
18208c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
18218c2ecf20Sopenharmony_ci *
18228c2ecf20Sopenharmony_ci * Description: Writes MII_ADVERTISE with the appropriate values,
18238c2ecf20Sopenharmony_ci *   after sanitizing the values to make sure we only advertise
18248c2ecf20Sopenharmony_ci *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
18258c2ecf20Sopenharmony_ci *   hasn't changed, and > 0 if it has changed.
18268c2ecf20Sopenharmony_ci */
18278c2ecf20Sopenharmony_cistatic int genphy_config_advert(struct phy_device *phydev)
18288c2ecf20Sopenharmony_ci{
18298c2ecf20Sopenharmony_ci	int err, bmsr, changed = 0;
18308c2ecf20Sopenharmony_ci	u32 adv;
18318c2ecf20Sopenharmony_ci
18328c2ecf20Sopenharmony_ci	/* Only allow advertising what this PHY supports */
18338c2ecf20Sopenharmony_ci	linkmode_and(phydev->advertising, phydev->advertising,
18348c2ecf20Sopenharmony_ci		     phydev->supported);
18358c2ecf20Sopenharmony_ci
18368c2ecf20Sopenharmony_ci	adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
18378c2ecf20Sopenharmony_ci
18388c2ecf20Sopenharmony_ci	/* Setup standard advertisement */
18398c2ecf20Sopenharmony_ci	err = phy_modify_changed(phydev, MII_ADVERTISE,
18408c2ecf20Sopenharmony_ci				 ADVERTISE_ALL | ADVERTISE_100BASE4 |
18418c2ecf20Sopenharmony_ci				 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
18428c2ecf20Sopenharmony_ci				 adv);
18438c2ecf20Sopenharmony_ci	if (err < 0)
18448c2ecf20Sopenharmony_ci		return err;
18458c2ecf20Sopenharmony_ci	if (err > 0)
18468c2ecf20Sopenharmony_ci		changed = 1;
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	bmsr = phy_read(phydev, MII_BMSR);
18498c2ecf20Sopenharmony_ci	if (bmsr < 0)
18508c2ecf20Sopenharmony_ci		return bmsr;
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_ci	/* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
18538c2ecf20Sopenharmony_ci	 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
18548c2ecf20Sopenharmony_ci	 * logical 1.
18558c2ecf20Sopenharmony_ci	 */
18568c2ecf20Sopenharmony_ci	if (!(bmsr & BMSR_ESTATEN))
18578c2ecf20Sopenharmony_ci		return changed;
18588c2ecf20Sopenharmony_ci
18598c2ecf20Sopenharmony_ci	adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
18608c2ecf20Sopenharmony_ci
18618c2ecf20Sopenharmony_ci	err = phy_modify_changed(phydev, MII_CTRL1000,
18628c2ecf20Sopenharmony_ci				 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
18638c2ecf20Sopenharmony_ci				 adv);
18648c2ecf20Sopenharmony_ci	if (err < 0)
18658c2ecf20Sopenharmony_ci		return err;
18668c2ecf20Sopenharmony_ci	if (err > 0)
18678c2ecf20Sopenharmony_ci		changed = 1;
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ci	return changed;
18708c2ecf20Sopenharmony_ci}
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_ci/**
18738c2ecf20Sopenharmony_ci * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
18748c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
18758c2ecf20Sopenharmony_ci *
18768c2ecf20Sopenharmony_ci * Description: Writes MII_ADVERTISE with the appropriate values,
18778c2ecf20Sopenharmony_ci *   after sanitizing the values to make sure we only advertise
18788c2ecf20Sopenharmony_ci *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
18798c2ecf20Sopenharmony_ci *   hasn't changed, and > 0 if it has changed. This function is intended
18808c2ecf20Sopenharmony_ci *   for Clause 37 1000Base-X mode.
18818c2ecf20Sopenharmony_ci */
18828c2ecf20Sopenharmony_cistatic int genphy_c37_config_advert(struct phy_device *phydev)
18838c2ecf20Sopenharmony_ci{
18848c2ecf20Sopenharmony_ci	u16 adv = 0;
18858c2ecf20Sopenharmony_ci
18868c2ecf20Sopenharmony_ci	/* Only allow advertising what this PHY supports */
18878c2ecf20Sopenharmony_ci	linkmode_and(phydev->advertising, phydev->advertising,
18888c2ecf20Sopenharmony_ci		     phydev->supported);
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
18918c2ecf20Sopenharmony_ci			      phydev->advertising))
18928c2ecf20Sopenharmony_ci		adv |= ADVERTISE_1000XFULL;
18938c2ecf20Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
18948c2ecf20Sopenharmony_ci			      phydev->advertising))
18958c2ecf20Sopenharmony_ci		adv |= ADVERTISE_1000XPAUSE;
18968c2ecf20Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
18978c2ecf20Sopenharmony_ci			      phydev->advertising))
18988c2ecf20Sopenharmony_ci		adv |= ADVERTISE_1000XPSE_ASYM;
18998c2ecf20Sopenharmony_ci
19008c2ecf20Sopenharmony_ci	return phy_modify_changed(phydev, MII_ADVERTISE,
19018c2ecf20Sopenharmony_ci				  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
19028c2ecf20Sopenharmony_ci				  ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
19038c2ecf20Sopenharmony_ci				  adv);
19048c2ecf20Sopenharmony_ci}
19058c2ecf20Sopenharmony_ci
19068c2ecf20Sopenharmony_ci/**
19078c2ecf20Sopenharmony_ci * genphy_config_eee_advert - disable unwanted eee mode advertisement
19088c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
19098c2ecf20Sopenharmony_ci *
19108c2ecf20Sopenharmony_ci * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
19118c2ecf20Sopenharmony_ci *   efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
19128c2ecf20Sopenharmony_ci *   changed, and 1 if it has changed.
19138c2ecf20Sopenharmony_ci */
19148c2ecf20Sopenharmony_ciint genphy_config_eee_advert(struct phy_device *phydev)
19158c2ecf20Sopenharmony_ci{
19168c2ecf20Sopenharmony_ci	int err;
19178c2ecf20Sopenharmony_ci
19188c2ecf20Sopenharmony_ci	/* Nothing to disable */
19198c2ecf20Sopenharmony_ci	if (!phydev->eee_broken_modes)
19208c2ecf20Sopenharmony_ci		return 0;
19218c2ecf20Sopenharmony_ci
19228c2ecf20Sopenharmony_ci	err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
19238c2ecf20Sopenharmony_ci				     phydev->eee_broken_modes, 0);
19248c2ecf20Sopenharmony_ci	/* If the call failed, we assume that EEE is not supported */
19258c2ecf20Sopenharmony_ci	return err < 0 ? 0 : err;
19268c2ecf20Sopenharmony_ci}
19278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_config_eee_advert);
19288c2ecf20Sopenharmony_ci
19298c2ecf20Sopenharmony_ci/**
19308c2ecf20Sopenharmony_ci * genphy_setup_forced - configures/forces speed/duplex from @phydev
19318c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
19328c2ecf20Sopenharmony_ci *
19338c2ecf20Sopenharmony_ci * Description: Configures MII_BMCR to force speed/duplex
19348c2ecf20Sopenharmony_ci *   to the values in phydev. Assumes that the values are valid.
19358c2ecf20Sopenharmony_ci *   Please see phy_sanitize_settings().
19368c2ecf20Sopenharmony_ci */
19378c2ecf20Sopenharmony_ciint genphy_setup_forced(struct phy_device *phydev)
19388c2ecf20Sopenharmony_ci{
19398c2ecf20Sopenharmony_ci	u16 ctl = 0;
19408c2ecf20Sopenharmony_ci
19418c2ecf20Sopenharmony_ci	phydev->pause = 0;
19428c2ecf20Sopenharmony_ci	phydev->asym_pause = 0;
19438c2ecf20Sopenharmony_ci
19448c2ecf20Sopenharmony_ci	if (SPEED_1000 == phydev->speed)
19458c2ecf20Sopenharmony_ci		ctl |= BMCR_SPEED1000;
19468c2ecf20Sopenharmony_ci	else if (SPEED_100 == phydev->speed)
19478c2ecf20Sopenharmony_ci		ctl |= BMCR_SPEED100;
19488c2ecf20Sopenharmony_ci
19498c2ecf20Sopenharmony_ci	if (DUPLEX_FULL == phydev->duplex)
19508c2ecf20Sopenharmony_ci		ctl |= BMCR_FULLDPLX;
19518c2ecf20Sopenharmony_ci
19528c2ecf20Sopenharmony_ci	return phy_modify(phydev, MII_BMCR,
19538c2ecf20Sopenharmony_ci			  ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
19548c2ecf20Sopenharmony_ci}
19558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_setup_forced);
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_cistatic int genphy_setup_master_slave(struct phy_device *phydev)
19588c2ecf20Sopenharmony_ci{
19598c2ecf20Sopenharmony_ci	u16 ctl = 0;
19608c2ecf20Sopenharmony_ci
19618c2ecf20Sopenharmony_ci	if (!phydev->is_gigabit_capable)
19628c2ecf20Sopenharmony_ci		return 0;
19638c2ecf20Sopenharmony_ci
19648c2ecf20Sopenharmony_ci	switch (phydev->master_slave_set) {
19658c2ecf20Sopenharmony_ci	case MASTER_SLAVE_CFG_MASTER_PREFERRED:
19668c2ecf20Sopenharmony_ci		ctl |= CTL1000_PREFER_MASTER;
19678c2ecf20Sopenharmony_ci		break;
19688c2ecf20Sopenharmony_ci	case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
19698c2ecf20Sopenharmony_ci		break;
19708c2ecf20Sopenharmony_ci	case MASTER_SLAVE_CFG_MASTER_FORCE:
19718c2ecf20Sopenharmony_ci		ctl |= CTL1000_AS_MASTER;
19728c2ecf20Sopenharmony_ci		fallthrough;
19738c2ecf20Sopenharmony_ci	case MASTER_SLAVE_CFG_SLAVE_FORCE:
19748c2ecf20Sopenharmony_ci		ctl |= CTL1000_ENABLE_MASTER;
19758c2ecf20Sopenharmony_ci		break;
19768c2ecf20Sopenharmony_ci	case MASTER_SLAVE_CFG_UNKNOWN:
19778c2ecf20Sopenharmony_ci	case MASTER_SLAVE_CFG_UNSUPPORTED:
19788c2ecf20Sopenharmony_ci		return 0;
19798c2ecf20Sopenharmony_ci	default:
19808c2ecf20Sopenharmony_ci		phydev_warn(phydev, "Unsupported Master/Slave mode\n");
19818c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
19828c2ecf20Sopenharmony_ci	}
19838c2ecf20Sopenharmony_ci
19848c2ecf20Sopenharmony_ci	return phy_modify_changed(phydev, MII_CTRL1000,
19858c2ecf20Sopenharmony_ci				  (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
19868c2ecf20Sopenharmony_ci				   CTL1000_PREFER_MASTER), ctl);
19878c2ecf20Sopenharmony_ci}
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_cistatic int genphy_read_master_slave(struct phy_device *phydev)
19908c2ecf20Sopenharmony_ci{
19918c2ecf20Sopenharmony_ci	int cfg, state;
19928c2ecf20Sopenharmony_ci	int val;
19938c2ecf20Sopenharmony_ci
19948c2ecf20Sopenharmony_ci	if (!phydev->is_gigabit_capable) {
19958c2ecf20Sopenharmony_ci		phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
19968c2ecf20Sopenharmony_ci		phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
19978c2ecf20Sopenharmony_ci		return 0;
19988c2ecf20Sopenharmony_ci	}
19998c2ecf20Sopenharmony_ci
20008c2ecf20Sopenharmony_ci	phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
20018c2ecf20Sopenharmony_ci	phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_ci	val = phy_read(phydev, MII_CTRL1000);
20048c2ecf20Sopenharmony_ci	if (val < 0)
20058c2ecf20Sopenharmony_ci		return val;
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci	if (val & CTL1000_ENABLE_MASTER) {
20088c2ecf20Sopenharmony_ci		if (val & CTL1000_AS_MASTER)
20098c2ecf20Sopenharmony_ci			cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
20108c2ecf20Sopenharmony_ci		else
20118c2ecf20Sopenharmony_ci			cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
20128c2ecf20Sopenharmony_ci	} else {
20138c2ecf20Sopenharmony_ci		if (val & CTL1000_PREFER_MASTER)
20148c2ecf20Sopenharmony_ci			cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
20158c2ecf20Sopenharmony_ci		else
20168c2ecf20Sopenharmony_ci			cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
20178c2ecf20Sopenharmony_ci	}
20188c2ecf20Sopenharmony_ci
20198c2ecf20Sopenharmony_ci	val = phy_read(phydev, MII_STAT1000);
20208c2ecf20Sopenharmony_ci	if (val < 0)
20218c2ecf20Sopenharmony_ci		return val;
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci	if (val & LPA_1000MSFAIL) {
20248c2ecf20Sopenharmony_ci		state = MASTER_SLAVE_STATE_ERR;
20258c2ecf20Sopenharmony_ci	} else if (phydev->link) {
20268c2ecf20Sopenharmony_ci		/* this bits are valid only for active link */
20278c2ecf20Sopenharmony_ci		if (val & LPA_1000MSRES)
20288c2ecf20Sopenharmony_ci			state = MASTER_SLAVE_STATE_MASTER;
20298c2ecf20Sopenharmony_ci		else
20308c2ecf20Sopenharmony_ci			state = MASTER_SLAVE_STATE_SLAVE;
20318c2ecf20Sopenharmony_ci	} else {
20328c2ecf20Sopenharmony_ci		state = MASTER_SLAVE_STATE_UNKNOWN;
20338c2ecf20Sopenharmony_ci	}
20348c2ecf20Sopenharmony_ci
20358c2ecf20Sopenharmony_ci	phydev->master_slave_get = cfg;
20368c2ecf20Sopenharmony_ci	phydev->master_slave_state = state;
20378c2ecf20Sopenharmony_ci
20388c2ecf20Sopenharmony_ci	return 0;
20398c2ecf20Sopenharmony_ci}
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci/**
20428c2ecf20Sopenharmony_ci * genphy_restart_aneg - Enable and Restart Autonegotiation
20438c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
20448c2ecf20Sopenharmony_ci */
20458c2ecf20Sopenharmony_ciint genphy_restart_aneg(struct phy_device *phydev)
20468c2ecf20Sopenharmony_ci{
20478c2ecf20Sopenharmony_ci	/* Don't isolate the PHY if we're negotiating */
20488c2ecf20Sopenharmony_ci	return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
20498c2ecf20Sopenharmony_ci			  BMCR_ANENABLE | BMCR_ANRESTART);
20508c2ecf20Sopenharmony_ci}
20518c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_restart_aneg);
20528c2ecf20Sopenharmony_ci
20538c2ecf20Sopenharmony_ci/**
20548c2ecf20Sopenharmony_ci * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
20558c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
20568c2ecf20Sopenharmony_ci * @restart: whether aneg restart is requested
20578c2ecf20Sopenharmony_ci *
20588c2ecf20Sopenharmony_ci * Check, and restart auto-negotiation if needed.
20598c2ecf20Sopenharmony_ci */
20608c2ecf20Sopenharmony_ciint genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
20618c2ecf20Sopenharmony_ci{
20628c2ecf20Sopenharmony_ci	int ret;
20638c2ecf20Sopenharmony_ci
20648c2ecf20Sopenharmony_ci	if (!restart) {
20658c2ecf20Sopenharmony_ci		/* Advertisement hasn't changed, but maybe aneg was never on to
20668c2ecf20Sopenharmony_ci		 * begin with?  Or maybe phy was isolated?
20678c2ecf20Sopenharmony_ci		 */
20688c2ecf20Sopenharmony_ci		ret = phy_read(phydev, MII_BMCR);
20698c2ecf20Sopenharmony_ci		if (ret < 0)
20708c2ecf20Sopenharmony_ci			return ret;
20718c2ecf20Sopenharmony_ci
20728c2ecf20Sopenharmony_ci		if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
20738c2ecf20Sopenharmony_ci			restart = true;
20748c2ecf20Sopenharmony_ci	}
20758c2ecf20Sopenharmony_ci
20768c2ecf20Sopenharmony_ci	if (restart)
20778c2ecf20Sopenharmony_ci		return genphy_restart_aneg(phydev);
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_ci	return 0;
20808c2ecf20Sopenharmony_ci}
20818c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_check_and_restart_aneg);
20828c2ecf20Sopenharmony_ci
20838c2ecf20Sopenharmony_ci/**
20848c2ecf20Sopenharmony_ci * __genphy_config_aneg - restart auto-negotiation or write BMCR
20858c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
20868c2ecf20Sopenharmony_ci * @changed: whether autoneg is requested
20878c2ecf20Sopenharmony_ci *
20888c2ecf20Sopenharmony_ci * Description: If auto-negotiation is enabled, we configure the
20898c2ecf20Sopenharmony_ci *   advertising, and then restart auto-negotiation.  If it is not
20908c2ecf20Sopenharmony_ci *   enabled, then we write the BMCR.
20918c2ecf20Sopenharmony_ci */
20928c2ecf20Sopenharmony_ciint __genphy_config_aneg(struct phy_device *phydev, bool changed)
20938c2ecf20Sopenharmony_ci{
20948c2ecf20Sopenharmony_ci	int err;
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_ci	if (genphy_config_eee_advert(phydev))
20978c2ecf20Sopenharmony_ci		changed = true;
20988c2ecf20Sopenharmony_ci
20998c2ecf20Sopenharmony_ci	err = genphy_setup_master_slave(phydev);
21008c2ecf20Sopenharmony_ci	if (err < 0)
21018c2ecf20Sopenharmony_ci		return err;
21028c2ecf20Sopenharmony_ci	else if (err)
21038c2ecf20Sopenharmony_ci		changed = true;
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci	if (AUTONEG_ENABLE != phydev->autoneg)
21068c2ecf20Sopenharmony_ci		return genphy_setup_forced(phydev);
21078c2ecf20Sopenharmony_ci
21088c2ecf20Sopenharmony_ci	err = genphy_config_advert(phydev);
21098c2ecf20Sopenharmony_ci	if (err < 0) /* error */
21108c2ecf20Sopenharmony_ci		return err;
21118c2ecf20Sopenharmony_ci	else if (err)
21128c2ecf20Sopenharmony_ci		changed = true;
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	return genphy_check_and_restart_aneg(phydev, changed);
21158c2ecf20Sopenharmony_ci}
21168c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__genphy_config_aneg);
21178c2ecf20Sopenharmony_ci
21188c2ecf20Sopenharmony_ci/**
21198c2ecf20Sopenharmony_ci * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
21208c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
21218c2ecf20Sopenharmony_ci *
21228c2ecf20Sopenharmony_ci * Description: If auto-negotiation is enabled, we configure the
21238c2ecf20Sopenharmony_ci *   advertising, and then restart auto-negotiation.  If it is not
21248c2ecf20Sopenharmony_ci *   enabled, then we write the BMCR. This function is intended
21258c2ecf20Sopenharmony_ci *   for use with Clause 37 1000Base-X mode.
21268c2ecf20Sopenharmony_ci */
21278c2ecf20Sopenharmony_ciint genphy_c37_config_aneg(struct phy_device *phydev)
21288c2ecf20Sopenharmony_ci{
21298c2ecf20Sopenharmony_ci	int err, changed;
21308c2ecf20Sopenharmony_ci
21318c2ecf20Sopenharmony_ci	if (phydev->autoneg != AUTONEG_ENABLE)
21328c2ecf20Sopenharmony_ci		return genphy_setup_forced(phydev);
21338c2ecf20Sopenharmony_ci
21348c2ecf20Sopenharmony_ci	err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
21358c2ecf20Sopenharmony_ci			 BMCR_SPEED1000);
21368c2ecf20Sopenharmony_ci	if (err)
21378c2ecf20Sopenharmony_ci		return err;
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_ci	changed = genphy_c37_config_advert(phydev);
21408c2ecf20Sopenharmony_ci	if (changed < 0) /* error */
21418c2ecf20Sopenharmony_ci		return changed;
21428c2ecf20Sopenharmony_ci
21438c2ecf20Sopenharmony_ci	if (!changed) {
21448c2ecf20Sopenharmony_ci		/* Advertisement hasn't changed, but maybe aneg was never on to
21458c2ecf20Sopenharmony_ci		 * begin with?  Or maybe phy was isolated?
21468c2ecf20Sopenharmony_ci		 */
21478c2ecf20Sopenharmony_ci		int ctl = phy_read(phydev, MII_BMCR);
21488c2ecf20Sopenharmony_ci
21498c2ecf20Sopenharmony_ci		if (ctl < 0)
21508c2ecf20Sopenharmony_ci			return ctl;
21518c2ecf20Sopenharmony_ci
21528c2ecf20Sopenharmony_ci		if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
21538c2ecf20Sopenharmony_ci			changed = 1; /* do restart aneg */
21548c2ecf20Sopenharmony_ci	}
21558c2ecf20Sopenharmony_ci
21568c2ecf20Sopenharmony_ci	/* Only restart aneg if we are advertising something different
21578c2ecf20Sopenharmony_ci	 * than we were before.
21588c2ecf20Sopenharmony_ci	 */
21598c2ecf20Sopenharmony_ci	if (changed > 0)
21608c2ecf20Sopenharmony_ci		return genphy_restart_aneg(phydev);
21618c2ecf20Sopenharmony_ci
21628c2ecf20Sopenharmony_ci	return 0;
21638c2ecf20Sopenharmony_ci}
21648c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_c37_config_aneg);
21658c2ecf20Sopenharmony_ci
21668c2ecf20Sopenharmony_ci/**
21678c2ecf20Sopenharmony_ci * genphy_aneg_done - return auto-negotiation status
21688c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
21698c2ecf20Sopenharmony_ci *
21708c2ecf20Sopenharmony_ci * Description: Reads the status register and returns 0 either if
21718c2ecf20Sopenharmony_ci *   auto-negotiation is incomplete, or if there was an error.
21728c2ecf20Sopenharmony_ci *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
21738c2ecf20Sopenharmony_ci */
21748c2ecf20Sopenharmony_ciint genphy_aneg_done(struct phy_device *phydev)
21758c2ecf20Sopenharmony_ci{
21768c2ecf20Sopenharmony_ci	int retval = phy_read(phydev, MII_BMSR);
21778c2ecf20Sopenharmony_ci
21788c2ecf20Sopenharmony_ci	return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
21798c2ecf20Sopenharmony_ci}
21808c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_aneg_done);
21818c2ecf20Sopenharmony_ci
21828c2ecf20Sopenharmony_ci/**
21838c2ecf20Sopenharmony_ci * genphy_update_link - update link status in @phydev
21848c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
21858c2ecf20Sopenharmony_ci *
21868c2ecf20Sopenharmony_ci * Description: Update the value in phydev->link to reflect the
21878c2ecf20Sopenharmony_ci *   current link value.  In order to do this, we need to read
21888c2ecf20Sopenharmony_ci *   the status register twice, keeping the second value.
21898c2ecf20Sopenharmony_ci */
21908c2ecf20Sopenharmony_ciint genphy_update_link(struct phy_device *phydev)
21918c2ecf20Sopenharmony_ci{
21928c2ecf20Sopenharmony_ci	int status = 0, bmcr;
21938c2ecf20Sopenharmony_ci
21948c2ecf20Sopenharmony_ci	bmcr = phy_read(phydev, MII_BMCR);
21958c2ecf20Sopenharmony_ci	if (bmcr < 0)
21968c2ecf20Sopenharmony_ci		return bmcr;
21978c2ecf20Sopenharmony_ci
21988c2ecf20Sopenharmony_ci	/* Autoneg is being started, therefore disregard BMSR value and
21998c2ecf20Sopenharmony_ci	 * report link as down.
22008c2ecf20Sopenharmony_ci	 */
22018c2ecf20Sopenharmony_ci	if (bmcr & BMCR_ANRESTART)
22028c2ecf20Sopenharmony_ci		goto done;
22038c2ecf20Sopenharmony_ci
22048c2ecf20Sopenharmony_ci	/* The link state is latched low so that momentary link
22058c2ecf20Sopenharmony_ci	 * drops can be detected. Do not double-read the status
22068c2ecf20Sopenharmony_ci	 * in polling mode to detect such short link drops except
22078c2ecf20Sopenharmony_ci	 * the link was already down.
22088c2ecf20Sopenharmony_ci	 */
22098c2ecf20Sopenharmony_ci	if (!phy_polling_mode(phydev) || !phydev->link) {
22108c2ecf20Sopenharmony_ci		status = phy_read(phydev, MII_BMSR);
22118c2ecf20Sopenharmony_ci		if (status < 0)
22128c2ecf20Sopenharmony_ci			return status;
22138c2ecf20Sopenharmony_ci		else if (status & BMSR_LSTATUS)
22148c2ecf20Sopenharmony_ci			goto done;
22158c2ecf20Sopenharmony_ci	}
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	/* Read link and autonegotiation status */
22188c2ecf20Sopenharmony_ci	status = phy_read(phydev, MII_BMSR);
22198c2ecf20Sopenharmony_ci	if (status < 0)
22208c2ecf20Sopenharmony_ci		return status;
22218c2ecf20Sopenharmony_cidone:
22228c2ecf20Sopenharmony_ci	phydev->link = status & BMSR_LSTATUS ? 1 : 0;
22238c2ecf20Sopenharmony_ci	phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
22248c2ecf20Sopenharmony_ci
22258c2ecf20Sopenharmony_ci	/* Consider the case that autoneg was started and "aneg complete"
22268c2ecf20Sopenharmony_ci	 * bit has been reset, but "link up" bit not yet.
22278c2ecf20Sopenharmony_ci	 */
22288c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
22298c2ecf20Sopenharmony_ci		phydev->link = 0;
22308c2ecf20Sopenharmony_ci
22318c2ecf20Sopenharmony_ci	return 0;
22328c2ecf20Sopenharmony_ci}
22338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_update_link);
22348c2ecf20Sopenharmony_ci
22358c2ecf20Sopenharmony_ciint genphy_read_lpa(struct phy_device *phydev)
22368c2ecf20Sopenharmony_ci{
22378c2ecf20Sopenharmony_ci	int lpa, lpagb;
22388c2ecf20Sopenharmony_ci
22398c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_ENABLE) {
22408c2ecf20Sopenharmony_ci		if (!phydev->autoneg_complete) {
22418c2ecf20Sopenharmony_ci			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
22428c2ecf20Sopenharmony_ci							0);
22438c2ecf20Sopenharmony_ci			mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
22448c2ecf20Sopenharmony_ci			return 0;
22458c2ecf20Sopenharmony_ci		}
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci		if (phydev->is_gigabit_capable) {
22488c2ecf20Sopenharmony_ci			lpagb = phy_read(phydev, MII_STAT1000);
22498c2ecf20Sopenharmony_ci			if (lpagb < 0)
22508c2ecf20Sopenharmony_ci				return lpagb;
22518c2ecf20Sopenharmony_ci
22528c2ecf20Sopenharmony_ci			if (lpagb & LPA_1000MSFAIL) {
22538c2ecf20Sopenharmony_ci				int adv = phy_read(phydev, MII_CTRL1000);
22548c2ecf20Sopenharmony_ci
22558c2ecf20Sopenharmony_ci				if (adv < 0)
22568c2ecf20Sopenharmony_ci					return adv;
22578c2ecf20Sopenharmony_ci
22588c2ecf20Sopenharmony_ci				if (adv & CTL1000_ENABLE_MASTER)
22598c2ecf20Sopenharmony_ci					phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
22608c2ecf20Sopenharmony_ci				else
22618c2ecf20Sopenharmony_ci					phydev_err(phydev, "Master/Slave resolution failed\n");
22628c2ecf20Sopenharmony_ci				return -ENOLINK;
22638c2ecf20Sopenharmony_ci			}
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
22668c2ecf20Sopenharmony_ci							lpagb);
22678c2ecf20Sopenharmony_ci		}
22688c2ecf20Sopenharmony_ci
22698c2ecf20Sopenharmony_ci		lpa = phy_read(phydev, MII_LPA);
22708c2ecf20Sopenharmony_ci		if (lpa < 0)
22718c2ecf20Sopenharmony_ci			return lpa;
22728c2ecf20Sopenharmony_ci
22738c2ecf20Sopenharmony_ci		mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
22748c2ecf20Sopenharmony_ci	} else {
22758c2ecf20Sopenharmony_ci		linkmode_zero(phydev->lp_advertising);
22768c2ecf20Sopenharmony_ci	}
22778c2ecf20Sopenharmony_ci
22788c2ecf20Sopenharmony_ci	return 0;
22798c2ecf20Sopenharmony_ci}
22808c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_read_lpa);
22818c2ecf20Sopenharmony_ci
22828c2ecf20Sopenharmony_ci/**
22838c2ecf20Sopenharmony_ci * genphy_read_status_fixed - read the link parameters for !aneg mode
22848c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
22858c2ecf20Sopenharmony_ci *
22868c2ecf20Sopenharmony_ci * Read the current duplex and speed state for a PHY operating with
22878c2ecf20Sopenharmony_ci * autonegotiation disabled.
22888c2ecf20Sopenharmony_ci */
22898c2ecf20Sopenharmony_ciint genphy_read_status_fixed(struct phy_device *phydev)
22908c2ecf20Sopenharmony_ci{
22918c2ecf20Sopenharmony_ci	int bmcr = phy_read(phydev, MII_BMCR);
22928c2ecf20Sopenharmony_ci
22938c2ecf20Sopenharmony_ci	if (bmcr < 0)
22948c2ecf20Sopenharmony_ci		return bmcr;
22958c2ecf20Sopenharmony_ci
22968c2ecf20Sopenharmony_ci	if (bmcr & BMCR_FULLDPLX)
22978c2ecf20Sopenharmony_ci		phydev->duplex = DUPLEX_FULL;
22988c2ecf20Sopenharmony_ci	else
22998c2ecf20Sopenharmony_ci		phydev->duplex = DUPLEX_HALF;
23008c2ecf20Sopenharmony_ci
23018c2ecf20Sopenharmony_ci	if (bmcr & BMCR_SPEED1000)
23028c2ecf20Sopenharmony_ci		phydev->speed = SPEED_1000;
23038c2ecf20Sopenharmony_ci	else if (bmcr & BMCR_SPEED100)
23048c2ecf20Sopenharmony_ci		phydev->speed = SPEED_100;
23058c2ecf20Sopenharmony_ci	else
23068c2ecf20Sopenharmony_ci		phydev->speed = SPEED_10;
23078c2ecf20Sopenharmony_ci
23088c2ecf20Sopenharmony_ci	return 0;
23098c2ecf20Sopenharmony_ci}
23108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_read_status_fixed);
23118c2ecf20Sopenharmony_ci
23128c2ecf20Sopenharmony_ci/**
23138c2ecf20Sopenharmony_ci * genphy_read_status - check the link status and update current link state
23148c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
23158c2ecf20Sopenharmony_ci *
23168c2ecf20Sopenharmony_ci * Description: Check the link, then figure out the current state
23178c2ecf20Sopenharmony_ci *   by comparing what we advertise with what the link partner
23188c2ecf20Sopenharmony_ci *   advertises.  Start by checking the gigabit possibilities,
23198c2ecf20Sopenharmony_ci *   then move on to 10/100.
23208c2ecf20Sopenharmony_ci */
23218c2ecf20Sopenharmony_ciint genphy_read_status(struct phy_device *phydev)
23228c2ecf20Sopenharmony_ci{
23238c2ecf20Sopenharmony_ci	int err, old_link = phydev->link;
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_ci	/* Update the link, but return if there was an error */
23268c2ecf20Sopenharmony_ci	err = genphy_update_link(phydev);
23278c2ecf20Sopenharmony_ci	if (err)
23288c2ecf20Sopenharmony_ci		return err;
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci	/* why bother the PHY if nothing can have changed */
23318c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
23328c2ecf20Sopenharmony_ci		return 0;
23338c2ecf20Sopenharmony_ci
23348c2ecf20Sopenharmony_ci	phydev->speed = SPEED_UNKNOWN;
23358c2ecf20Sopenharmony_ci	phydev->duplex = DUPLEX_UNKNOWN;
23368c2ecf20Sopenharmony_ci	phydev->pause = 0;
23378c2ecf20Sopenharmony_ci	phydev->asym_pause = 0;
23388c2ecf20Sopenharmony_ci
23398c2ecf20Sopenharmony_ci	err = genphy_read_master_slave(phydev);
23408c2ecf20Sopenharmony_ci	if (err < 0)
23418c2ecf20Sopenharmony_ci		return err;
23428c2ecf20Sopenharmony_ci
23438c2ecf20Sopenharmony_ci	err = genphy_read_lpa(phydev);
23448c2ecf20Sopenharmony_ci	if (err < 0)
23458c2ecf20Sopenharmony_ci		return err;
23468c2ecf20Sopenharmony_ci
23478c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
23488c2ecf20Sopenharmony_ci		phy_resolve_aneg_linkmode(phydev);
23498c2ecf20Sopenharmony_ci	} else if (phydev->autoneg == AUTONEG_DISABLE) {
23508c2ecf20Sopenharmony_ci		err = genphy_read_status_fixed(phydev);
23518c2ecf20Sopenharmony_ci		if (err < 0)
23528c2ecf20Sopenharmony_ci			return err;
23538c2ecf20Sopenharmony_ci	}
23548c2ecf20Sopenharmony_ci
23558c2ecf20Sopenharmony_ci	return 0;
23568c2ecf20Sopenharmony_ci}
23578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_read_status);
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_ci/**
23608c2ecf20Sopenharmony_ci * genphy_c37_read_status - check the link status and update current link state
23618c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
23628c2ecf20Sopenharmony_ci *
23638c2ecf20Sopenharmony_ci * Description: Check the link, then figure out the current state
23648c2ecf20Sopenharmony_ci *   by comparing what we advertise with what the link partner
23658c2ecf20Sopenharmony_ci *   advertises. This function is for Clause 37 1000Base-X mode.
23668c2ecf20Sopenharmony_ci */
23678c2ecf20Sopenharmony_ciint genphy_c37_read_status(struct phy_device *phydev)
23688c2ecf20Sopenharmony_ci{
23698c2ecf20Sopenharmony_ci	int lpa, err, old_link = phydev->link;
23708c2ecf20Sopenharmony_ci
23718c2ecf20Sopenharmony_ci	/* Update the link, but return if there was an error */
23728c2ecf20Sopenharmony_ci	err = genphy_update_link(phydev);
23738c2ecf20Sopenharmony_ci	if (err)
23748c2ecf20Sopenharmony_ci		return err;
23758c2ecf20Sopenharmony_ci
23768c2ecf20Sopenharmony_ci	/* why bother the PHY if nothing can have changed */
23778c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
23788c2ecf20Sopenharmony_ci		return 0;
23798c2ecf20Sopenharmony_ci
23808c2ecf20Sopenharmony_ci	phydev->duplex = DUPLEX_UNKNOWN;
23818c2ecf20Sopenharmony_ci	phydev->pause = 0;
23828c2ecf20Sopenharmony_ci	phydev->asym_pause = 0;
23838c2ecf20Sopenharmony_ci
23848c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
23858c2ecf20Sopenharmony_ci		lpa = phy_read(phydev, MII_LPA);
23868c2ecf20Sopenharmony_ci		if (lpa < 0)
23878c2ecf20Sopenharmony_ci			return lpa;
23888c2ecf20Sopenharmony_ci
23898c2ecf20Sopenharmony_ci		linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
23908c2ecf20Sopenharmony_ci				 phydev->lp_advertising, lpa & LPA_LPACK);
23918c2ecf20Sopenharmony_ci		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
23928c2ecf20Sopenharmony_ci				 phydev->lp_advertising, lpa & LPA_1000XFULL);
23938c2ecf20Sopenharmony_ci		linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
23948c2ecf20Sopenharmony_ci				 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
23958c2ecf20Sopenharmony_ci		linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
23968c2ecf20Sopenharmony_ci				 phydev->lp_advertising,
23978c2ecf20Sopenharmony_ci				 lpa & LPA_1000XPAUSE_ASYM);
23988c2ecf20Sopenharmony_ci
23998c2ecf20Sopenharmony_ci		phy_resolve_aneg_linkmode(phydev);
24008c2ecf20Sopenharmony_ci	} else if (phydev->autoneg == AUTONEG_DISABLE) {
24018c2ecf20Sopenharmony_ci		int bmcr = phy_read(phydev, MII_BMCR);
24028c2ecf20Sopenharmony_ci
24038c2ecf20Sopenharmony_ci		if (bmcr < 0)
24048c2ecf20Sopenharmony_ci			return bmcr;
24058c2ecf20Sopenharmony_ci
24068c2ecf20Sopenharmony_ci		if (bmcr & BMCR_FULLDPLX)
24078c2ecf20Sopenharmony_ci			phydev->duplex = DUPLEX_FULL;
24088c2ecf20Sopenharmony_ci		else
24098c2ecf20Sopenharmony_ci			phydev->duplex = DUPLEX_HALF;
24108c2ecf20Sopenharmony_ci	}
24118c2ecf20Sopenharmony_ci
24128c2ecf20Sopenharmony_ci	return 0;
24138c2ecf20Sopenharmony_ci}
24148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_c37_read_status);
24158c2ecf20Sopenharmony_ci
24168c2ecf20Sopenharmony_ci/**
24178c2ecf20Sopenharmony_ci * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
24188c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
24198c2ecf20Sopenharmony_ci *
24208c2ecf20Sopenharmony_ci * Description: Perform a software PHY reset using the standard
24218c2ecf20Sopenharmony_ci * BMCR_RESET bit and poll for the reset bit to be cleared.
24228c2ecf20Sopenharmony_ci *
24238c2ecf20Sopenharmony_ci * Returns: 0 on success, < 0 on failure
24248c2ecf20Sopenharmony_ci */
24258c2ecf20Sopenharmony_ciint genphy_soft_reset(struct phy_device *phydev)
24268c2ecf20Sopenharmony_ci{
24278c2ecf20Sopenharmony_ci	u16 res = BMCR_RESET;
24288c2ecf20Sopenharmony_ci	int ret;
24298c2ecf20Sopenharmony_ci
24308c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_ENABLE)
24318c2ecf20Sopenharmony_ci		res |= BMCR_ANRESTART;
24328c2ecf20Sopenharmony_ci
24338c2ecf20Sopenharmony_ci	ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
24348c2ecf20Sopenharmony_ci	if (ret < 0)
24358c2ecf20Sopenharmony_ci		return ret;
24368c2ecf20Sopenharmony_ci
24378c2ecf20Sopenharmony_ci	/* Clause 22 states that setting bit BMCR_RESET sets control registers
24388c2ecf20Sopenharmony_ci	 * to their default value. Therefore the POWER DOWN bit is supposed to
24398c2ecf20Sopenharmony_ci	 * be cleared after soft reset.
24408c2ecf20Sopenharmony_ci	 */
24418c2ecf20Sopenharmony_ci	phydev->suspended = 0;
24428c2ecf20Sopenharmony_ci
24438c2ecf20Sopenharmony_ci	ret = phy_poll_reset(phydev);
24448c2ecf20Sopenharmony_ci	if (ret)
24458c2ecf20Sopenharmony_ci		return ret;
24468c2ecf20Sopenharmony_ci
24478c2ecf20Sopenharmony_ci	/* BMCR may be reset to defaults */
24488c2ecf20Sopenharmony_ci	if (phydev->autoneg == AUTONEG_DISABLE)
24498c2ecf20Sopenharmony_ci		ret = genphy_setup_forced(phydev);
24508c2ecf20Sopenharmony_ci
24518c2ecf20Sopenharmony_ci	return ret;
24528c2ecf20Sopenharmony_ci}
24538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_soft_reset);
24548c2ecf20Sopenharmony_ci
24558c2ecf20Sopenharmony_ci/**
24568c2ecf20Sopenharmony_ci * genphy_read_abilities - read PHY abilities from Clause 22 registers
24578c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
24588c2ecf20Sopenharmony_ci *
24598c2ecf20Sopenharmony_ci * Description: Reads the PHY's abilities and populates
24608c2ecf20Sopenharmony_ci * phydev->supported accordingly.
24618c2ecf20Sopenharmony_ci *
24628c2ecf20Sopenharmony_ci * Returns: 0 on success, < 0 on failure
24638c2ecf20Sopenharmony_ci */
24648c2ecf20Sopenharmony_ciint genphy_read_abilities(struct phy_device *phydev)
24658c2ecf20Sopenharmony_ci{
24668c2ecf20Sopenharmony_ci	int val;
24678c2ecf20Sopenharmony_ci
24688c2ecf20Sopenharmony_ci	linkmode_set_bit_array(phy_basic_ports_array,
24698c2ecf20Sopenharmony_ci			       ARRAY_SIZE(phy_basic_ports_array),
24708c2ecf20Sopenharmony_ci			       phydev->supported);
24718c2ecf20Sopenharmony_ci
24728c2ecf20Sopenharmony_ci	val = phy_read(phydev, MII_BMSR);
24738c2ecf20Sopenharmony_ci	if (val < 0)
24748c2ecf20Sopenharmony_ci		return val;
24758c2ecf20Sopenharmony_ci
24768c2ecf20Sopenharmony_ci	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
24778c2ecf20Sopenharmony_ci			 val & BMSR_ANEGCAPABLE);
24788c2ecf20Sopenharmony_ci
24798c2ecf20Sopenharmony_ci	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
24808c2ecf20Sopenharmony_ci			 val & BMSR_100FULL);
24818c2ecf20Sopenharmony_ci	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
24828c2ecf20Sopenharmony_ci			 val & BMSR_100HALF);
24838c2ecf20Sopenharmony_ci	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
24848c2ecf20Sopenharmony_ci			 val & BMSR_10FULL);
24858c2ecf20Sopenharmony_ci	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
24868c2ecf20Sopenharmony_ci			 val & BMSR_10HALF);
24878c2ecf20Sopenharmony_ci
24888c2ecf20Sopenharmony_ci	if (val & BMSR_ESTATEN) {
24898c2ecf20Sopenharmony_ci		val = phy_read(phydev, MII_ESTATUS);
24908c2ecf20Sopenharmony_ci		if (val < 0)
24918c2ecf20Sopenharmony_ci			return val;
24928c2ecf20Sopenharmony_ci
24938c2ecf20Sopenharmony_ci		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
24948c2ecf20Sopenharmony_ci				 phydev->supported, val & ESTATUS_1000_TFULL);
24958c2ecf20Sopenharmony_ci		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
24968c2ecf20Sopenharmony_ci				 phydev->supported, val & ESTATUS_1000_THALF);
24978c2ecf20Sopenharmony_ci		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
24988c2ecf20Sopenharmony_ci				 phydev->supported, val & ESTATUS_1000_XFULL);
24998c2ecf20Sopenharmony_ci	}
25008c2ecf20Sopenharmony_ci
25018c2ecf20Sopenharmony_ci	return 0;
25028c2ecf20Sopenharmony_ci}
25038c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_read_abilities);
25048c2ecf20Sopenharmony_ci
25058c2ecf20Sopenharmony_ci/* This is used for the phy device which doesn't support the MMD extended
25068c2ecf20Sopenharmony_ci * register access, but it does have side effect when we are trying to access
25078c2ecf20Sopenharmony_ci * the MMD register via indirect method.
25088c2ecf20Sopenharmony_ci */
25098c2ecf20Sopenharmony_ciint genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
25108c2ecf20Sopenharmony_ci{
25118c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
25128c2ecf20Sopenharmony_ci}
25138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_read_mmd_unsupported);
25148c2ecf20Sopenharmony_ci
25158c2ecf20Sopenharmony_ciint genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
25168c2ecf20Sopenharmony_ci				 u16 regnum, u16 val)
25178c2ecf20Sopenharmony_ci{
25188c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
25198c2ecf20Sopenharmony_ci}
25208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_write_mmd_unsupported);
25218c2ecf20Sopenharmony_ci
25228c2ecf20Sopenharmony_ciint genphy_suspend(struct phy_device *phydev)
25238c2ecf20Sopenharmony_ci{
25248c2ecf20Sopenharmony_ci	return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
25258c2ecf20Sopenharmony_ci}
25268c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_suspend);
25278c2ecf20Sopenharmony_ci
25288c2ecf20Sopenharmony_ciint genphy_resume(struct phy_device *phydev)
25298c2ecf20Sopenharmony_ci{
25308c2ecf20Sopenharmony_ci	return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
25318c2ecf20Sopenharmony_ci}
25328c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_resume);
25338c2ecf20Sopenharmony_ci
25348c2ecf20Sopenharmony_ciint genphy_loopback(struct phy_device *phydev, bool enable)
25358c2ecf20Sopenharmony_ci{
25368c2ecf20Sopenharmony_ci	return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
25378c2ecf20Sopenharmony_ci			  enable ? BMCR_LOOPBACK : 0);
25388c2ecf20Sopenharmony_ci}
25398c2ecf20Sopenharmony_ciEXPORT_SYMBOL(genphy_loopback);
25408c2ecf20Sopenharmony_ci
25418c2ecf20Sopenharmony_ci/**
25428c2ecf20Sopenharmony_ci * phy_remove_link_mode - Remove a supported link mode
25438c2ecf20Sopenharmony_ci * @phydev: phy_device structure to remove link mode from
25448c2ecf20Sopenharmony_ci * @link_mode: Link mode to be removed
25458c2ecf20Sopenharmony_ci *
25468c2ecf20Sopenharmony_ci * Description: Some MACs don't support all link modes which the PHY
25478c2ecf20Sopenharmony_ci * does.  e.g. a 1G MAC often does not support 1000Half. Add a helper
25488c2ecf20Sopenharmony_ci * to remove a link mode.
25498c2ecf20Sopenharmony_ci */
25508c2ecf20Sopenharmony_civoid phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
25518c2ecf20Sopenharmony_ci{
25528c2ecf20Sopenharmony_ci	linkmode_clear_bit(link_mode, phydev->supported);
25538c2ecf20Sopenharmony_ci	phy_advertise_supported(phydev);
25548c2ecf20Sopenharmony_ci}
25558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_remove_link_mode);
25568c2ecf20Sopenharmony_ci
25578c2ecf20Sopenharmony_cistatic void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
25588c2ecf20Sopenharmony_ci{
25598c2ecf20Sopenharmony_ci	linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
25608c2ecf20Sopenharmony_ci		linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
25618c2ecf20Sopenharmony_ci	linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
25628c2ecf20Sopenharmony_ci		linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
25638c2ecf20Sopenharmony_ci}
25648c2ecf20Sopenharmony_ci
25658c2ecf20Sopenharmony_ci/**
25668c2ecf20Sopenharmony_ci * phy_advertise_supported - Advertise all supported modes
25678c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
25688c2ecf20Sopenharmony_ci *
25698c2ecf20Sopenharmony_ci * Description: Called to advertise all supported modes, doesn't touch
25708c2ecf20Sopenharmony_ci * pause mode advertising.
25718c2ecf20Sopenharmony_ci */
25728c2ecf20Sopenharmony_civoid phy_advertise_supported(struct phy_device *phydev)
25738c2ecf20Sopenharmony_ci{
25748c2ecf20Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(new);
25758c2ecf20Sopenharmony_ci
25768c2ecf20Sopenharmony_ci	linkmode_copy(new, phydev->supported);
25778c2ecf20Sopenharmony_ci	phy_copy_pause_bits(new, phydev->advertising);
25788c2ecf20Sopenharmony_ci	linkmode_copy(phydev->advertising, new);
25798c2ecf20Sopenharmony_ci}
25808c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_advertise_supported);
25818c2ecf20Sopenharmony_ci
25828c2ecf20Sopenharmony_ci/**
25838c2ecf20Sopenharmony_ci * phy_support_sym_pause - Enable support of symmetrical pause
25848c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
25858c2ecf20Sopenharmony_ci *
25868c2ecf20Sopenharmony_ci * Description: Called by the MAC to indicate is supports symmetrical
25878c2ecf20Sopenharmony_ci * Pause, but not asym pause.
25888c2ecf20Sopenharmony_ci */
25898c2ecf20Sopenharmony_civoid phy_support_sym_pause(struct phy_device *phydev)
25908c2ecf20Sopenharmony_ci{
25918c2ecf20Sopenharmony_ci	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
25928c2ecf20Sopenharmony_ci	phy_copy_pause_bits(phydev->advertising, phydev->supported);
25938c2ecf20Sopenharmony_ci}
25948c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_support_sym_pause);
25958c2ecf20Sopenharmony_ci
25968c2ecf20Sopenharmony_ci/**
25978c2ecf20Sopenharmony_ci * phy_support_asym_pause - Enable support of asym pause
25988c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
25998c2ecf20Sopenharmony_ci *
26008c2ecf20Sopenharmony_ci * Description: Called by the MAC to indicate is supports Asym Pause.
26018c2ecf20Sopenharmony_ci */
26028c2ecf20Sopenharmony_civoid phy_support_asym_pause(struct phy_device *phydev)
26038c2ecf20Sopenharmony_ci{
26048c2ecf20Sopenharmony_ci	phy_copy_pause_bits(phydev->advertising, phydev->supported);
26058c2ecf20Sopenharmony_ci}
26068c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_support_asym_pause);
26078c2ecf20Sopenharmony_ci
26088c2ecf20Sopenharmony_ci/**
26098c2ecf20Sopenharmony_ci * phy_set_sym_pause - Configure symmetric Pause
26108c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
26118c2ecf20Sopenharmony_ci * @rx: Receiver Pause is supported
26128c2ecf20Sopenharmony_ci * @tx: Transmit Pause is supported
26138c2ecf20Sopenharmony_ci * @autoneg: Auto neg should be used
26148c2ecf20Sopenharmony_ci *
26158c2ecf20Sopenharmony_ci * Description: Configure advertised Pause support depending on if
26168c2ecf20Sopenharmony_ci * receiver pause and pause auto neg is supported. Generally called
26178c2ecf20Sopenharmony_ci * from the set_pauseparam .ndo.
26188c2ecf20Sopenharmony_ci */
26198c2ecf20Sopenharmony_civoid phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
26208c2ecf20Sopenharmony_ci		       bool autoneg)
26218c2ecf20Sopenharmony_ci{
26228c2ecf20Sopenharmony_ci	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
26238c2ecf20Sopenharmony_ci
26248c2ecf20Sopenharmony_ci	if (rx && tx && autoneg)
26258c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
26268c2ecf20Sopenharmony_ci				 phydev->supported);
26278c2ecf20Sopenharmony_ci
26288c2ecf20Sopenharmony_ci	linkmode_copy(phydev->advertising, phydev->supported);
26298c2ecf20Sopenharmony_ci}
26308c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_set_sym_pause);
26318c2ecf20Sopenharmony_ci
26328c2ecf20Sopenharmony_ci/**
26338c2ecf20Sopenharmony_ci * phy_set_asym_pause - Configure Pause and Asym Pause
26348c2ecf20Sopenharmony_ci * @phydev: target phy_device struct
26358c2ecf20Sopenharmony_ci * @rx: Receiver Pause is supported
26368c2ecf20Sopenharmony_ci * @tx: Transmit Pause is supported
26378c2ecf20Sopenharmony_ci *
26388c2ecf20Sopenharmony_ci * Description: Configure advertised Pause support depending on if
26398c2ecf20Sopenharmony_ci * transmit and receiver pause is supported. If there has been a
26408c2ecf20Sopenharmony_ci * change in adverting, trigger a new autoneg. Generally called from
26418c2ecf20Sopenharmony_ci * the set_pauseparam .ndo.
26428c2ecf20Sopenharmony_ci */
26438c2ecf20Sopenharmony_civoid phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
26448c2ecf20Sopenharmony_ci{
26458c2ecf20Sopenharmony_ci	__ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
26468c2ecf20Sopenharmony_ci
26478c2ecf20Sopenharmony_ci	linkmode_copy(oldadv, phydev->advertising);
26488c2ecf20Sopenharmony_ci	linkmode_set_pause(phydev->advertising, tx, rx);
26498c2ecf20Sopenharmony_ci
26508c2ecf20Sopenharmony_ci	if (!linkmode_equal(oldadv, phydev->advertising) &&
26518c2ecf20Sopenharmony_ci	    phydev->autoneg)
26528c2ecf20Sopenharmony_ci		phy_start_aneg(phydev);
26538c2ecf20Sopenharmony_ci}
26548c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_set_asym_pause);
26558c2ecf20Sopenharmony_ci
26568c2ecf20Sopenharmony_ci/**
26578c2ecf20Sopenharmony_ci * phy_validate_pause - Test if the PHY/MAC support the pause configuration
26588c2ecf20Sopenharmony_ci * @phydev: phy_device struct
26598c2ecf20Sopenharmony_ci * @pp: requested pause configuration
26608c2ecf20Sopenharmony_ci *
26618c2ecf20Sopenharmony_ci * Description: Test if the PHY/MAC combination supports the Pause
26628c2ecf20Sopenharmony_ci * configuration the user is requesting. Returns True if it is
26638c2ecf20Sopenharmony_ci * supported, false otherwise.
26648c2ecf20Sopenharmony_ci */
26658c2ecf20Sopenharmony_cibool phy_validate_pause(struct phy_device *phydev,
26668c2ecf20Sopenharmony_ci			struct ethtool_pauseparam *pp)
26678c2ecf20Sopenharmony_ci{
26688c2ecf20Sopenharmony_ci	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
26698c2ecf20Sopenharmony_ci			       phydev->supported) && pp->rx_pause)
26708c2ecf20Sopenharmony_ci		return false;
26718c2ecf20Sopenharmony_ci
26728c2ecf20Sopenharmony_ci	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
26738c2ecf20Sopenharmony_ci			       phydev->supported) &&
26748c2ecf20Sopenharmony_ci	    pp->rx_pause != pp->tx_pause)
26758c2ecf20Sopenharmony_ci		return false;
26768c2ecf20Sopenharmony_ci
26778c2ecf20Sopenharmony_ci	return true;
26788c2ecf20Sopenharmony_ci}
26798c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_validate_pause);
26808c2ecf20Sopenharmony_ci
26818c2ecf20Sopenharmony_ci/**
26828c2ecf20Sopenharmony_ci * phy_get_pause - resolve negotiated pause modes
26838c2ecf20Sopenharmony_ci * @phydev: phy_device struct
26848c2ecf20Sopenharmony_ci * @tx_pause: pointer to bool to indicate whether transmit pause should be
26858c2ecf20Sopenharmony_ci * enabled.
26868c2ecf20Sopenharmony_ci * @rx_pause: pointer to bool to indicate whether receive pause should be
26878c2ecf20Sopenharmony_ci * enabled.
26888c2ecf20Sopenharmony_ci *
26898c2ecf20Sopenharmony_ci * Resolve and return the flow control modes according to the negotiation
26908c2ecf20Sopenharmony_ci * result. This includes checking that we are operating in full duplex mode.
26918c2ecf20Sopenharmony_ci * See linkmode_resolve_pause() for further details.
26928c2ecf20Sopenharmony_ci */
26938c2ecf20Sopenharmony_civoid phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
26948c2ecf20Sopenharmony_ci{
26958c2ecf20Sopenharmony_ci	if (phydev->duplex != DUPLEX_FULL) {
26968c2ecf20Sopenharmony_ci		*tx_pause = false;
26978c2ecf20Sopenharmony_ci		*rx_pause = false;
26988c2ecf20Sopenharmony_ci		return;
26998c2ecf20Sopenharmony_ci	}
27008c2ecf20Sopenharmony_ci
27018c2ecf20Sopenharmony_ci	return linkmode_resolve_pause(phydev->advertising,
27028c2ecf20Sopenharmony_ci				      phydev->lp_advertising,
27038c2ecf20Sopenharmony_ci				      tx_pause, rx_pause);
27048c2ecf20Sopenharmony_ci}
27058c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_get_pause);
27068c2ecf20Sopenharmony_ci
27078c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_OF_MDIO)
27088c2ecf20Sopenharmony_cistatic int phy_get_int_delay_property(struct device *dev, const char *name)
27098c2ecf20Sopenharmony_ci{
27108c2ecf20Sopenharmony_ci	s32 int_delay;
27118c2ecf20Sopenharmony_ci	int ret;
27128c2ecf20Sopenharmony_ci
27138c2ecf20Sopenharmony_ci	ret = device_property_read_u32(dev, name, &int_delay);
27148c2ecf20Sopenharmony_ci	if (ret)
27158c2ecf20Sopenharmony_ci		return ret;
27168c2ecf20Sopenharmony_ci
27178c2ecf20Sopenharmony_ci	return int_delay;
27188c2ecf20Sopenharmony_ci}
27198c2ecf20Sopenharmony_ci#else
27208c2ecf20Sopenharmony_cistatic int phy_get_int_delay_property(struct device *dev, const char *name)
27218c2ecf20Sopenharmony_ci{
27228c2ecf20Sopenharmony_ci	return -EINVAL;
27238c2ecf20Sopenharmony_ci}
27248c2ecf20Sopenharmony_ci#endif
27258c2ecf20Sopenharmony_ci
27268c2ecf20Sopenharmony_ci/**
27278c2ecf20Sopenharmony_ci * phy_get_delay_index - returns the index of the internal delay
27288c2ecf20Sopenharmony_ci * @phydev: phy_device struct
27298c2ecf20Sopenharmony_ci * @dev: pointer to the devices device struct
27308c2ecf20Sopenharmony_ci * @delay_values: array of delays the PHY supports
27318c2ecf20Sopenharmony_ci * @size: the size of the delay array
27328c2ecf20Sopenharmony_ci * @is_rx: boolean to indicate to get the rx internal delay
27338c2ecf20Sopenharmony_ci *
27348c2ecf20Sopenharmony_ci * Returns the index within the array of internal delay passed in.
27358c2ecf20Sopenharmony_ci * If the device property is not present then the interface type is checked
27368c2ecf20Sopenharmony_ci * if the interface defines use of internal delay then a 1 is returned otherwise
27378c2ecf20Sopenharmony_ci * a 0 is returned.
27388c2ecf20Sopenharmony_ci * The array must be in ascending order. If PHY does not have an ascending order
27398c2ecf20Sopenharmony_ci * array then size = 0 and the value of the delay property is returned.
27408c2ecf20Sopenharmony_ci * Return -EINVAL if the delay is invalid or cannot be found.
27418c2ecf20Sopenharmony_ci */
27428c2ecf20Sopenharmony_cis32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
27438c2ecf20Sopenharmony_ci			   const int *delay_values, int size, bool is_rx)
27448c2ecf20Sopenharmony_ci{
27458c2ecf20Sopenharmony_ci	s32 delay;
27468c2ecf20Sopenharmony_ci	int i;
27478c2ecf20Sopenharmony_ci
27488c2ecf20Sopenharmony_ci	if (is_rx) {
27498c2ecf20Sopenharmony_ci		delay = phy_get_int_delay_property(dev, "rx-internal-delay-ps");
27508c2ecf20Sopenharmony_ci		if (delay < 0 && size == 0) {
27518c2ecf20Sopenharmony_ci			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
27528c2ecf20Sopenharmony_ci			    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
27538c2ecf20Sopenharmony_ci				return 1;
27548c2ecf20Sopenharmony_ci			else
27558c2ecf20Sopenharmony_ci				return 0;
27568c2ecf20Sopenharmony_ci		}
27578c2ecf20Sopenharmony_ci
27588c2ecf20Sopenharmony_ci	} else {
27598c2ecf20Sopenharmony_ci		delay = phy_get_int_delay_property(dev, "tx-internal-delay-ps");
27608c2ecf20Sopenharmony_ci		if (delay < 0 && size == 0) {
27618c2ecf20Sopenharmony_ci			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
27628c2ecf20Sopenharmony_ci			    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
27638c2ecf20Sopenharmony_ci				return 1;
27648c2ecf20Sopenharmony_ci			else
27658c2ecf20Sopenharmony_ci				return 0;
27668c2ecf20Sopenharmony_ci		}
27678c2ecf20Sopenharmony_ci	}
27688c2ecf20Sopenharmony_ci
27698c2ecf20Sopenharmony_ci	if (delay < 0)
27708c2ecf20Sopenharmony_ci		return delay;
27718c2ecf20Sopenharmony_ci
27728c2ecf20Sopenharmony_ci	if (delay && size == 0)
27738c2ecf20Sopenharmony_ci		return delay;
27748c2ecf20Sopenharmony_ci
27758c2ecf20Sopenharmony_ci	if (delay < delay_values[0] || delay > delay_values[size - 1]) {
27768c2ecf20Sopenharmony_ci		phydev_err(phydev, "Delay %d is out of range\n", delay);
27778c2ecf20Sopenharmony_ci		return -EINVAL;
27788c2ecf20Sopenharmony_ci	}
27798c2ecf20Sopenharmony_ci
27808c2ecf20Sopenharmony_ci	if (delay == delay_values[0])
27818c2ecf20Sopenharmony_ci		return 0;
27828c2ecf20Sopenharmony_ci
27838c2ecf20Sopenharmony_ci	for (i = 1; i < size; i++) {
27848c2ecf20Sopenharmony_ci		if (delay == delay_values[i])
27858c2ecf20Sopenharmony_ci			return i;
27868c2ecf20Sopenharmony_ci
27878c2ecf20Sopenharmony_ci		/* Find an approximate index by looking up the table */
27888c2ecf20Sopenharmony_ci		if (delay > delay_values[i - 1] &&
27898c2ecf20Sopenharmony_ci		    delay < delay_values[i]) {
27908c2ecf20Sopenharmony_ci			if (delay - delay_values[i - 1] <
27918c2ecf20Sopenharmony_ci			    delay_values[i] - delay)
27928c2ecf20Sopenharmony_ci				return i - 1;
27938c2ecf20Sopenharmony_ci			else
27948c2ecf20Sopenharmony_ci				return i;
27958c2ecf20Sopenharmony_ci		}
27968c2ecf20Sopenharmony_ci	}
27978c2ecf20Sopenharmony_ci
27988c2ecf20Sopenharmony_ci	phydev_err(phydev, "error finding internal delay index for %d\n",
27998c2ecf20Sopenharmony_ci		   delay);
28008c2ecf20Sopenharmony_ci
28018c2ecf20Sopenharmony_ci	return -EINVAL;
28028c2ecf20Sopenharmony_ci}
28038c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_get_internal_delay);
28048c2ecf20Sopenharmony_ci
28058c2ecf20Sopenharmony_cistatic bool phy_drv_supports_irq(struct phy_driver *phydrv)
28068c2ecf20Sopenharmony_ci{
28078c2ecf20Sopenharmony_ci	return phydrv->config_intr && phydrv->ack_interrupt;
28088c2ecf20Sopenharmony_ci}
28098c2ecf20Sopenharmony_ci
28108c2ecf20Sopenharmony_ci/**
28118c2ecf20Sopenharmony_ci * phy_probe - probe and init a PHY device
28128c2ecf20Sopenharmony_ci * @dev: device to probe and init
28138c2ecf20Sopenharmony_ci *
28148c2ecf20Sopenharmony_ci * Description: Take care of setting up the phy_device structure,
28158c2ecf20Sopenharmony_ci *   set the state to READY (the driver's init function should
28168c2ecf20Sopenharmony_ci *   set it to STARTING if needed).
28178c2ecf20Sopenharmony_ci */
28188c2ecf20Sopenharmony_cistatic int phy_probe(struct device *dev)
28198c2ecf20Sopenharmony_ci{
28208c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
28218c2ecf20Sopenharmony_ci	struct device_driver *drv = phydev->mdio.dev.driver;
28228c2ecf20Sopenharmony_ci	struct phy_driver *phydrv = to_phy_driver(drv);
28238c2ecf20Sopenharmony_ci	int err = 0;
28248c2ecf20Sopenharmony_ci
28258c2ecf20Sopenharmony_ci	phydev->drv = phydrv;
28268c2ecf20Sopenharmony_ci
28278c2ecf20Sopenharmony_ci	/* Disable the interrupt if the PHY doesn't support it
28288c2ecf20Sopenharmony_ci	 * but the interrupt is still a valid one
28298c2ecf20Sopenharmony_ci	 */
28308c2ecf20Sopenharmony_ci	 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
28318c2ecf20Sopenharmony_ci		phydev->irq = PHY_POLL;
28328c2ecf20Sopenharmony_ci
28338c2ecf20Sopenharmony_ci	if (phydrv->flags & PHY_IS_INTERNAL)
28348c2ecf20Sopenharmony_ci		phydev->is_internal = true;
28358c2ecf20Sopenharmony_ci
28368c2ecf20Sopenharmony_ci	/* Deassert the reset signal */
28378c2ecf20Sopenharmony_ci	phy_device_reset(phydev, 0);
28388c2ecf20Sopenharmony_ci
28398c2ecf20Sopenharmony_ci	if (phydev->drv->probe) {
28408c2ecf20Sopenharmony_ci		err = phydev->drv->probe(phydev);
28418c2ecf20Sopenharmony_ci		if (err)
28428c2ecf20Sopenharmony_ci			goto out;
28438c2ecf20Sopenharmony_ci	}
28448c2ecf20Sopenharmony_ci
28458c2ecf20Sopenharmony_ci	/* Start out supporting everything. Eventually,
28468c2ecf20Sopenharmony_ci	 * a controller will attach, and may modify one
28478c2ecf20Sopenharmony_ci	 * or both of these values
28488c2ecf20Sopenharmony_ci	 */
28498c2ecf20Sopenharmony_ci	if (phydrv->features) {
28508c2ecf20Sopenharmony_ci		linkmode_copy(phydev->supported, phydrv->features);
28518c2ecf20Sopenharmony_ci	} else if (phydrv->get_features) {
28528c2ecf20Sopenharmony_ci		err = phydrv->get_features(phydev);
28538c2ecf20Sopenharmony_ci	} else if (phydev->is_c45) {
28548c2ecf20Sopenharmony_ci		err = genphy_c45_pma_read_abilities(phydev);
28558c2ecf20Sopenharmony_ci	} else {
28568c2ecf20Sopenharmony_ci		err = genphy_read_abilities(phydev);
28578c2ecf20Sopenharmony_ci	}
28588c2ecf20Sopenharmony_ci
28598c2ecf20Sopenharmony_ci	if (err)
28608c2ecf20Sopenharmony_ci		goto out;
28618c2ecf20Sopenharmony_ci
28628c2ecf20Sopenharmony_ci	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
28638c2ecf20Sopenharmony_ci			       phydev->supported))
28648c2ecf20Sopenharmony_ci		phydev->autoneg = 0;
28658c2ecf20Sopenharmony_ci
28668c2ecf20Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
28678c2ecf20Sopenharmony_ci			      phydev->supported))
28688c2ecf20Sopenharmony_ci		phydev->is_gigabit_capable = 1;
28698c2ecf20Sopenharmony_ci	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
28708c2ecf20Sopenharmony_ci			      phydev->supported))
28718c2ecf20Sopenharmony_ci		phydev->is_gigabit_capable = 1;
28728c2ecf20Sopenharmony_ci
28738c2ecf20Sopenharmony_ci	of_set_phy_supported(phydev);
28748c2ecf20Sopenharmony_ci	phy_advertise_supported(phydev);
28758c2ecf20Sopenharmony_ci
28768c2ecf20Sopenharmony_ci	/* Get the EEE modes we want to prohibit. We will ask
28778c2ecf20Sopenharmony_ci	 * the PHY stop advertising these mode later on
28788c2ecf20Sopenharmony_ci	 */
28798c2ecf20Sopenharmony_ci	of_set_phy_eee_broken(phydev);
28808c2ecf20Sopenharmony_ci
28818c2ecf20Sopenharmony_ci	/* The Pause Frame bits indicate that the PHY can support passing
28828c2ecf20Sopenharmony_ci	 * pause frames. During autonegotiation, the PHYs will determine if
28838c2ecf20Sopenharmony_ci	 * they should allow pause frames to pass.  The MAC driver should then
28848c2ecf20Sopenharmony_ci	 * use that result to determine whether to enable flow control via
28858c2ecf20Sopenharmony_ci	 * pause frames.
28868c2ecf20Sopenharmony_ci	 *
28878c2ecf20Sopenharmony_ci	 * Normally, PHY drivers should not set the Pause bits, and instead
28888c2ecf20Sopenharmony_ci	 * allow phylib to do that.  However, there may be some situations
28898c2ecf20Sopenharmony_ci	 * (e.g. hardware erratum) where the driver wants to set only one
28908c2ecf20Sopenharmony_ci	 * of these bits.
28918c2ecf20Sopenharmony_ci	 */
28928c2ecf20Sopenharmony_ci	if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
28938c2ecf20Sopenharmony_ci	    !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
28948c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
28958c2ecf20Sopenharmony_ci				 phydev->supported);
28968c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
28978c2ecf20Sopenharmony_ci				 phydev->supported);
28988c2ecf20Sopenharmony_ci	}
28998c2ecf20Sopenharmony_ci
29008c2ecf20Sopenharmony_ci	/* Set the state to READY by default */
29018c2ecf20Sopenharmony_ci	phydev->state = PHY_READY;
29028c2ecf20Sopenharmony_ci
29038c2ecf20Sopenharmony_ciout:
29048c2ecf20Sopenharmony_ci	/* Re-assert the reset signal on error */
29058c2ecf20Sopenharmony_ci	if (err)
29068c2ecf20Sopenharmony_ci		phy_device_reset(phydev, 1);
29078c2ecf20Sopenharmony_ci
29088c2ecf20Sopenharmony_ci	return err;
29098c2ecf20Sopenharmony_ci}
29108c2ecf20Sopenharmony_ci
29118c2ecf20Sopenharmony_cistatic int phy_remove(struct device *dev)
29128c2ecf20Sopenharmony_ci{
29138c2ecf20Sopenharmony_ci	struct phy_device *phydev = to_phy_device(dev);
29148c2ecf20Sopenharmony_ci
29158c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&phydev->state_queue);
29168c2ecf20Sopenharmony_ci
29178c2ecf20Sopenharmony_ci	phydev->state = PHY_DOWN;
29188c2ecf20Sopenharmony_ci
29198c2ecf20Sopenharmony_ci	sfp_bus_del_upstream(phydev->sfp_bus);
29208c2ecf20Sopenharmony_ci	phydev->sfp_bus = NULL;
29218c2ecf20Sopenharmony_ci
29228c2ecf20Sopenharmony_ci	if (phydev->drv && phydev->drv->remove)
29238c2ecf20Sopenharmony_ci		phydev->drv->remove(phydev);
29248c2ecf20Sopenharmony_ci
29258c2ecf20Sopenharmony_ci	/* Assert the reset signal */
29268c2ecf20Sopenharmony_ci	phy_device_reset(phydev, 1);
29278c2ecf20Sopenharmony_ci
29288c2ecf20Sopenharmony_ci	phydev->drv = NULL;
29298c2ecf20Sopenharmony_ci
29308c2ecf20Sopenharmony_ci	return 0;
29318c2ecf20Sopenharmony_ci}
29328c2ecf20Sopenharmony_ci
29338c2ecf20Sopenharmony_ci/**
29348c2ecf20Sopenharmony_ci * phy_driver_register - register a phy_driver with the PHY layer
29358c2ecf20Sopenharmony_ci * @new_driver: new phy_driver to register
29368c2ecf20Sopenharmony_ci * @owner: module owning this PHY
29378c2ecf20Sopenharmony_ci */
29388c2ecf20Sopenharmony_ciint phy_driver_register(struct phy_driver *new_driver, struct module *owner)
29398c2ecf20Sopenharmony_ci{
29408c2ecf20Sopenharmony_ci	int retval;
29418c2ecf20Sopenharmony_ci
29428c2ecf20Sopenharmony_ci	/* Either the features are hard coded, or dynamically
29438c2ecf20Sopenharmony_ci	 * determined. It cannot be both.
29448c2ecf20Sopenharmony_ci	 */
29458c2ecf20Sopenharmony_ci	if (WARN_ON(new_driver->features && new_driver->get_features)) {
29468c2ecf20Sopenharmony_ci		pr_err("%s: features and get_features must not both be set\n",
29478c2ecf20Sopenharmony_ci		       new_driver->name);
29488c2ecf20Sopenharmony_ci		return -EINVAL;
29498c2ecf20Sopenharmony_ci	}
29508c2ecf20Sopenharmony_ci
29518c2ecf20Sopenharmony_ci	new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
29528c2ecf20Sopenharmony_ci	new_driver->mdiodrv.driver.name = new_driver->name;
29538c2ecf20Sopenharmony_ci	new_driver->mdiodrv.driver.bus = &mdio_bus_type;
29548c2ecf20Sopenharmony_ci	new_driver->mdiodrv.driver.probe = phy_probe;
29558c2ecf20Sopenharmony_ci	new_driver->mdiodrv.driver.remove = phy_remove;
29568c2ecf20Sopenharmony_ci	new_driver->mdiodrv.driver.owner = owner;
29578c2ecf20Sopenharmony_ci	new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
29588c2ecf20Sopenharmony_ci
29598c2ecf20Sopenharmony_ci	retval = driver_register(&new_driver->mdiodrv.driver);
29608c2ecf20Sopenharmony_ci	if (retval) {
29618c2ecf20Sopenharmony_ci		pr_err("%s: Error %d in registering driver\n",
29628c2ecf20Sopenharmony_ci		       new_driver->name, retval);
29638c2ecf20Sopenharmony_ci
29648c2ecf20Sopenharmony_ci		return retval;
29658c2ecf20Sopenharmony_ci	}
29668c2ecf20Sopenharmony_ci
29678c2ecf20Sopenharmony_ci	pr_debug("%s: Registered new driver\n", new_driver->name);
29688c2ecf20Sopenharmony_ci
29698c2ecf20Sopenharmony_ci	return 0;
29708c2ecf20Sopenharmony_ci}
29718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_driver_register);
29728c2ecf20Sopenharmony_ci
29738c2ecf20Sopenharmony_ciint phy_drivers_register(struct phy_driver *new_driver, int n,
29748c2ecf20Sopenharmony_ci			 struct module *owner)
29758c2ecf20Sopenharmony_ci{
29768c2ecf20Sopenharmony_ci	int i, ret = 0;
29778c2ecf20Sopenharmony_ci
29788c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++) {
29798c2ecf20Sopenharmony_ci		ret = phy_driver_register(new_driver + i, owner);
29808c2ecf20Sopenharmony_ci		if (ret) {
29818c2ecf20Sopenharmony_ci			while (i-- > 0)
29828c2ecf20Sopenharmony_ci				phy_driver_unregister(new_driver + i);
29838c2ecf20Sopenharmony_ci			break;
29848c2ecf20Sopenharmony_ci		}
29858c2ecf20Sopenharmony_ci	}
29868c2ecf20Sopenharmony_ci	return ret;
29878c2ecf20Sopenharmony_ci}
29888c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_drivers_register);
29898c2ecf20Sopenharmony_ci
29908c2ecf20Sopenharmony_civoid phy_driver_unregister(struct phy_driver *drv)
29918c2ecf20Sopenharmony_ci{
29928c2ecf20Sopenharmony_ci	driver_unregister(&drv->mdiodrv.driver);
29938c2ecf20Sopenharmony_ci}
29948c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_driver_unregister);
29958c2ecf20Sopenharmony_ci
29968c2ecf20Sopenharmony_civoid phy_drivers_unregister(struct phy_driver *drv, int n)
29978c2ecf20Sopenharmony_ci{
29988c2ecf20Sopenharmony_ci	int i;
29998c2ecf20Sopenharmony_ci
30008c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++)
30018c2ecf20Sopenharmony_ci		phy_driver_unregister(drv + i);
30028c2ecf20Sopenharmony_ci}
30038c2ecf20Sopenharmony_ciEXPORT_SYMBOL(phy_drivers_unregister);
30048c2ecf20Sopenharmony_ci
30058c2ecf20Sopenharmony_cistatic struct phy_driver genphy_driver = {
30068c2ecf20Sopenharmony_ci	.phy_id		= 0xffffffff,
30078c2ecf20Sopenharmony_ci	.phy_id_mask	= 0xffffffff,
30088c2ecf20Sopenharmony_ci	.name		= "Generic PHY",
30098c2ecf20Sopenharmony_ci	.get_features	= genphy_read_abilities,
30108c2ecf20Sopenharmony_ci	.suspend	= genphy_suspend,
30118c2ecf20Sopenharmony_ci	.resume		= genphy_resume,
30128c2ecf20Sopenharmony_ci	.set_loopback   = genphy_loopback,
30138c2ecf20Sopenharmony_ci};
30148c2ecf20Sopenharmony_ci
30158c2ecf20Sopenharmony_cistatic const struct ethtool_phy_ops phy_ethtool_phy_ops = {
30168c2ecf20Sopenharmony_ci	.get_sset_count		= phy_ethtool_get_sset_count,
30178c2ecf20Sopenharmony_ci	.get_strings		= phy_ethtool_get_strings,
30188c2ecf20Sopenharmony_ci	.get_stats		= phy_ethtool_get_stats,
30198c2ecf20Sopenharmony_ci	.start_cable_test	= phy_start_cable_test,
30208c2ecf20Sopenharmony_ci	.start_cable_test_tdr	= phy_start_cable_test_tdr,
30218c2ecf20Sopenharmony_ci};
30228c2ecf20Sopenharmony_ci
30238c2ecf20Sopenharmony_cistatic int __init phy_init(void)
30248c2ecf20Sopenharmony_ci{
30258c2ecf20Sopenharmony_ci	int rc;
30268c2ecf20Sopenharmony_ci
30278c2ecf20Sopenharmony_ci	ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
30288c2ecf20Sopenharmony_ci
30298c2ecf20Sopenharmony_ci	rc = mdio_bus_init();
30308c2ecf20Sopenharmony_ci	if (rc)
30318c2ecf20Sopenharmony_ci		goto err_ethtool_phy_ops;
30328c2ecf20Sopenharmony_ci
30338c2ecf20Sopenharmony_ci	features_init();
30348c2ecf20Sopenharmony_ci
30358c2ecf20Sopenharmony_ci	rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
30368c2ecf20Sopenharmony_ci	if (rc)
30378c2ecf20Sopenharmony_ci		goto err_mdio_bus;
30388c2ecf20Sopenharmony_ci
30398c2ecf20Sopenharmony_ci	rc = phy_driver_register(&genphy_driver, THIS_MODULE);
30408c2ecf20Sopenharmony_ci	if (rc)
30418c2ecf20Sopenharmony_ci		goto err_c45;
30428c2ecf20Sopenharmony_ci
30438c2ecf20Sopenharmony_ci	return 0;
30448c2ecf20Sopenharmony_ci
30458c2ecf20Sopenharmony_cierr_c45:
30468c2ecf20Sopenharmony_ci	phy_driver_unregister(&genphy_c45_driver);
30478c2ecf20Sopenharmony_cierr_mdio_bus:
30488c2ecf20Sopenharmony_ci	mdio_bus_exit();
30498c2ecf20Sopenharmony_cierr_ethtool_phy_ops:
30508c2ecf20Sopenharmony_ci	ethtool_set_ethtool_phy_ops(NULL);
30518c2ecf20Sopenharmony_ci
30528c2ecf20Sopenharmony_ci	return rc;
30538c2ecf20Sopenharmony_ci}
30548c2ecf20Sopenharmony_ci
30558c2ecf20Sopenharmony_cistatic void __exit phy_exit(void)
30568c2ecf20Sopenharmony_ci{
30578c2ecf20Sopenharmony_ci	phy_driver_unregister(&genphy_c45_driver);
30588c2ecf20Sopenharmony_ci	phy_driver_unregister(&genphy_driver);
30598c2ecf20Sopenharmony_ci	mdio_bus_exit();
30608c2ecf20Sopenharmony_ci	ethtool_set_ethtool_phy_ops(NULL);
30618c2ecf20Sopenharmony_ci}
30628c2ecf20Sopenharmony_ci
30638c2ecf20Sopenharmony_cisubsys_initcall(phy_init);
30648c2ecf20Sopenharmony_cimodule_exit(phy_exit);
3065