Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /* Driver for Asix PHYs
0003  *
0004  * Author: Michael Schmitz <schmitzmic@gmail.com>
0005  */
0006 #include <linux/kernel.h>
0007 #include <linux/errno.h>
0008 #include <linux/init.h>
0009 #include <linux/module.h>
0010 #include <linux/mii.h>
0011 #include <linux/phy.h>
0012 
0013 #define PHY_ID_ASIX_AX88772A        0x003b1861
0014 #define PHY_ID_ASIX_AX88772C        0x003b1881
0015 #define PHY_ID_ASIX_AX88796B        0x003b1841
0016 
0017 MODULE_DESCRIPTION("Asix PHY driver");
0018 MODULE_AUTHOR("Michael Schmitz <schmitzmic@gmail.com>");
0019 MODULE_LICENSE("GPL");
0020 
0021 /**
0022  * asix_soft_reset - software reset the PHY via BMCR_RESET bit
0023  * @phydev: target phy_device struct
0024  *
0025  * Description: Perform a software PHY reset using the standard
0026  * BMCR_RESET bit and poll for the reset bit to be cleared.
0027  * Toggle BMCR_RESET bit off to accommodate broken AX8796B PHY implementation
0028  * such as used on the Individual Computers' X-Surf 100 Zorro card.
0029  *
0030  * Returns: 0 on success, < 0 on failure
0031  */
0032 static int asix_soft_reset(struct phy_device *phydev)
0033 {
0034     int ret;
0035 
0036     /* Asix PHY won't reset unless reset bit toggles */
0037     ret = phy_write(phydev, MII_BMCR, 0);
0038     if (ret < 0)
0039         return ret;
0040 
0041     return genphy_soft_reset(phydev);
0042 }
0043 
0044 /* AX88772A is not working properly with some old switches (NETGEAR EN 108TP):
0045  * after autoneg is done and the link status is reported as active, the MII_LPA
0046  * register is 0. This issue is not reproducible on AX88772C.
0047  */
0048 static int asix_ax88772a_read_status(struct phy_device *phydev)
0049 {
0050     int ret, val;
0051 
0052     ret = genphy_update_link(phydev);
0053     if (ret)
0054         return ret;
0055 
0056     if (!phydev->link)
0057         return 0;
0058 
0059     /* If MII_LPA is 0, phy_resolve_aneg_linkmode() will fail to resolve
0060      * linkmode so use MII_BMCR as default values.
0061      */
0062     val = phy_read(phydev, MII_BMCR);
0063     if (val < 0)
0064         return val;
0065 
0066     if (val & BMCR_SPEED100)
0067         phydev->speed = SPEED_100;
0068     else
0069         phydev->speed = SPEED_10;
0070 
0071     if (val & BMCR_FULLDPLX)
0072         phydev->duplex = DUPLEX_FULL;
0073     else
0074         phydev->duplex = DUPLEX_HALF;
0075 
0076     ret = genphy_read_lpa(phydev);
0077     if (ret < 0)
0078         return ret;
0079 
0080     if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
0081         phy_resolve_aneg_linkmode(phydev);
0082 
0083     return 0;
0084 }
0085 
0086 static void asix_ax88772a_link_change_notify(struct phy_device *phydev)
0087 {
0088     /* Reset PHY, otherwise MII_LPA will provide outdated information.
0089      * This issue is reproducible only with some link partner PHYs
0090      */
0091     if (phydev->state == PHY_NOLINK) {
0092         phy_init_hw(phydev);
0093         phy_start_aneg(phydev);
0094     }
0095 }
0096 
0097 static struct phy_driver asix_driver[] = {
0098 {
0099     PHY_ID_MATCH_EXACT(PHY_ID_ASIX_AX88772A),
0100     .name       = "Asix Electronics AX88772A",
0101     .flags      = PHY_IS_INTERNAL,
0102     .read_status    = asix_ax88772a_read_status,
0103     .suspend    = genphy_suspend,
0104     .resume     = genphy_resume,
0105     .soft_reset = asix_soft_reset,
0106     .link_change_notify = asix_ax88772a_link_change_notify,
0107 }, {
0108     PHY_ID_MATCH_EXACT(PHY_ID_ASIX_AX88772C),
0109     .name       = "Asix Electronics AX88772C",
0110     .flags      = PHY_IS_INTERNAL,
0111     .suspend    = genphy_suspend,
0112     .resume     = genphy_resume,
0113     .soft_reset = asix_soft_reset,
0114 }, {
0115     .phy_id     = PHY_ID_ASIX_AX88796B,
0116     .name       = "Asix Electronics AX88796B",
0117     .phy_id_mask    = 0xfffffff0,
0118     /* PHY_BASIC_FEATURES */
0119     .soft_reset = asix_soft_reset,
0120 } };
0121 
0122 module_phy_driver(asix_driver);
0123 
0124 static struct mdio_device_id __maybe_unused asix_tbl[] = {
0125     { PHY_ID_MATCH_EXACT(PHY_ID_ASIX_AX88772A) },
0126     { PHY_ID_MATCH_EXACT(PHY_ID_ASIX_AX88772C) },
0127     { PHY_ID_ASIX_AX88796B, 0xfffffff0 },
0128     { }
0129 };
0130 
0131 MODULE_DEVICE_TABLE(mdio, asix_tbl);