Home | History | Annotate | Line # | Download | only in mii
      1 /*	$NetBSD: ukphy_subr.c,v 1.17 2020/03/15 23:04:50 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Frank van der Linden.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Subroutines shared by the ukphy driver and other PHY drivers.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: ukphy_subr.c,v 1.17 2020/03/15 23:04:50 thorpej Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/device.h>
     44 #include <sys/socket.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_media.h>
     48 
     49 #include <dev/mii/mii.h>
     50 #include <dev/mii/miivar.h>
     51 
     52 /*
     53  * Media status subroutine.  If a PHY driver does media detection simply
     54  * by decoding the NWay autonegotiation, use this routine.
     55  */
     56 void
     57 ukphy_status(struct mii_softc *phy)
     58 {
     59 	struct mii_data *mii = phy->mii_pdata;
     60 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
     61 	uint16_t bmsr, bmcr, anar, anlpar, gtcr, gtsr, result;
     62 
     63 	KASSERT(mii_locked(mii));
     64 
     65 	mii->mii_media_status = IFM_AVALID;
     66 	mii->mii_media_active = IFM_ETHER;
     67 
     68 	PHY_READ(phy, MII_BMSR, &bmsr);
     69 	PHY_READ(phy, MII_BMSR, &bmsr);
     70 	if (bmsr & BMSR_LINK)
     71 		mii->mii_media_status |= IFM_ACTIVE;
     72 
     73 	PHY_READ(phy, MII_BMCR, &bmcr);
     74 	if (bmcr & BMCR_ISO) {
     75 		mii->mii_media_active |= IFM_NONE;
     76 		mii->mii_media_status = 0;
     77 		return;
     78 	}
     79 
     80 	if (bmcr & BMCR_LOOP)
     81 		mii->mii_media_active |= IFM_LOOP;
     82 
     83 	if (bmcr & BMCR_AUTOEN) {
     84 		/*
     85 		 * NWay autonegotiation takes the highest-order common
     86 		 * bit of the ANAR and ANLPAR (i.e. best media advertised
     87 		 * both by us and our link partner).
     88 		 */
     89 		if ((bmsr & BMSR_ACOMP) == 0) {
     90 			/* Erg, still trying, I guess... */
     91 			mii->mii_media_active |= IFM_NONE;
     92 			return;
     93 		}
     94 
     95 		PHY_READ(phy, MII_ANAR, &anar);
     96 		PHY_READ(phy, MII_ANLPAR, &anlpar);
     97 		result = anar & anlpar;
     98 		if ((phy->mii_flags & MIIF_HAVE_GTCR) != 0 &&
     99 		    (phy->mii_extcapabilities &
    100 			(EXTSR_1000THDX | EXTSR_1000TFDX)) != 0) {
    101 			PHY_READ(phy, MII_100T2CR, &gtcr);
    102 			PHY_READ(phy, MII_100T2SR, &gtsr);
    103 		} else
    104 			gtcr = gtsr = 0;
    105 
    106 		if ((gtcr & GTCR_ADV_1000TFDX) && (gtsr & GTSR_LP_1000TFDX))
    107 			mii->mii_media_active |= IFM_1000_T | IFM_FDX;
    108 		else if ((gtcr & GTCR_ADV_1000THDX) &&
    109 			 (gtsr & GTSR_LP_1000THDX))
    110 			mii->mii_media_active |= IFM_1000_T | IFM_HDX;
    111 		else if (result & ANLPAR_TX_FD)
    112 			mii->mii_media_active |= IFM_100_TX | IFM_FDX;
    113 		else if (result & ANLPAR_T4)
    114 			mii->mii_media_active |= IFM_100_T4 | IFM_HDX;
    115 		else if (result & ANLPAR_TX)
    116 			mii->mii_media_active |= IFM_100_TX | IFM_HDX;
    117 		else if (result & ANLPAR_10_FD)
    118 			mii->mii_media_active |= IFM_10_T | IFM_FDX;
    119 		else if (result & ANLPAR_10)
    120 			mii->mii_media_active |= IFM_10_T | IFM_HDX;
    121 		else
    122 			mii->mii_media_active |= IFM_NONE;
    123 
    124 		if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&
    125 		    (gtsr & GTSR_MS_RES))
    126 			mii->mii_media_active |= IFM_ETH_MASTER;
    127 
    128 		if (mii->mii_media_active & IFM_FDX)
    129 			mii->mii_media_active |= mii_phy_flowstatus(phy);
    130 	} else
    131 		mii->mii_media_active = ife->ifm_media;
    132 }
    133