/*      $NetBSD: ukphy_subr.c,v 1.17 2020/03/15 23:04:50 thorpej Exp $  */

/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center, and by Frank van der Linden.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/*
* Subroutines shared by the ukphy driver and other PHY drivers.
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ukphy_subr.c,v 1.17 2020/03/15 23:04:50 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_media.h>

#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>

/*
* Media status subroutine.  If a PHY driver does media detection simply
* by decoding the NWay autonegotiation, use this routine.
*/
void
ukphy_status(struct mii_softc *phy)
{
       struct mii_data *mii = phy->mii_pdata;
       struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
       uint16_t bmsr, bmcr, anar, anlpar, gtcr, gtsr, result;

       KASSERT(mii_locked(mii));

       mii->mii_media_status = IFM_AVALID;
       mii->mii_media_active = IFM_ETHER;

       PHY_READ(phy, MII_BMSR, &bmsr);
       PHY_READ(phy, MII_BMSR, &bmsr);
       if (bmsr & BMSR_LINK)
               mii->mii_media_status |= IFM_ACTIVE;

       PHY_READ(phy, MII_BMCR, &bmcr);
       if (bmcr & BMCR_ISO) {
               mii->mii_media_active |= IFM_NONE;
               mii->mii_media_status = 0;
               return;
       }

       if (bmcr & BMCR_LOOP)
               mii->mii_media_active |= IFM_LOOP;

       if (bmcr & BMCR_AUTOEN) {
               /*
                * NWay autonegotiation takes the highest-order common
                * bit of the ANAR and ANLPAR (i.e. best media advertised
                * both by us and our link partner).
                */
               if ((bmsr & BMSR_ACOMP) == 0) {
                       /* Erg, still trying, I guess... */
                       mii->mii_media_active |= IFM_NONE;
                       return;
               }

               PHY_READ(phy, MII_ANAR, &anar);
               PHY_READ(phy, MII_ANLPAR, &anlpar);
               result = anar & anlpar;
               if ((phy->mii_flags & MIIF_HAVE_GTCR) != 0 &&
                   (phy->mii_extcapabilities &
                       (EXTSR_1000THDX | EXTSR_1000TFDX)) != 0) {
                       PHY_READ(phy, MII_100T2CR, &gtcr);
                       PHY_READ(phy, MII_100T2SR, &gtsr);
               } else
                       gtcr = gtsr = 0;

               if ((gtcr & GTCR_ADV_1000TFDX) && (gtsr & GTSR_LP_1000TFDX))
                       mii->mii_media_active |= IFM_1000_T | IFM_FDX;
               else if ((gtcr & GTCR_ADV_1000THDX) &&
                        (gtsr & GTSR_LP_1000THDX))
                       mii->mii_media_active |= IFM_1000_T | IFM_HDX;
               else if (result & ANLPAR_TX_FD)
                       mii->mii_media_active |= IFM_100_TX | IFM_FDX;
               else if (result & ANLPAR_T4)
                       mii->mii_media_active |= IFM_100_T4 | IFM_HDX;
               else if (result & ANLPAR_TX)
                       mii->mii_media_active |= IFM_100_TX | IFM_HDX;
               else if (result & ANLPAR_10_FD)
                       mii->mii_media_active |= IFM_10_T | IFM_FDX;
               else if (result & ANLPAR_10)
                       mii->mii_media_active |= IFM_10_T | IFM_HDX;
               else
                       mii->mii_media_active |= IFM_NONE;

               if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&
                   (gtsr & GTSR_MS_RES))
                       mii->mii_media_active |= IFM_ETH_MASTER;

               if (mii->mii_media_active & IFM_FDX)
                       mii->mii_media_active |= mii_phy_flowstatus(phy);
       } else
               mii->mii_media_active = ife->ifm_media;
}