xref: /freebsd/sys/dev/mii/ukphy_subr.c (revision aa24f48b361effe51163877d84f1b70d32b77e04)
1 /*	$NetBSD: ukphy_subr.c,v 1.2 1998/11/05 04:08:02 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 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /*
37  * Subroutines shared by the ukphy driver and other PHY drivers.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/socket.h>
43 #include <sys/module.h>
44 #include <sys/bus.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 #include "miibus_if.h"
53 
54 /*
55  * Media status subroutine.  If a PHY driver does media detection simply
56  * by decoding the NWay autonegotiation, use this routine.
57  */
58 void
59 ukphy_status(struct mii_softc *phy)
60 {
61 	struct mii_data *mii = phy->mii_pdata;
62 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
63 	int bmsr, bmcr, anlpar, gtcr, gtsr;
64 
65 	mii->mii_media_status = IFM_AVALID;
66 	mii->mii_media_active = IFM_ETHER;
67 
68 	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
69 	if (bmsr & BMSR_LINK)
70 		mii->mii_media_status |= IFM_ACTIVE;
71 
72 	bmcr = PHY_READ(phy, MII_BMCR);
73 	if (bmcr & BMCR_ISO) {
74 		mii->mii_media_active |= IFM_NONE;
75 		mii->mii_media_status = 0;
76 		return;
77 	}
78 
79 	if (bmcr & BMCR_LOOP)
80 		mii->mii_media_active |= IFM_LOOP;
81 
82 	if (bmcr & BMCR_AUTOEN) {
83 		/*
84 		 * NWay autonegotiation takes the highest-order common
85 		 * bit of the ANAR and ANLPAR (i.e. best media advertised
86 		 * both by us and our link partner).
87 		 */
88 		if ((bmsr & BMSR_ACOMP) == 0) {
89 			/* Erg, still trying, I guess... */
90 			mii->mii_media_active |= IFM_NONE;
91 			return;
92 		}
93 
94 		anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
95 		if ((phy->mii_flags & MIIF_HAVE_GTCR) != 0 &&
96 		    (phy->mii_extcapabilities &
97 		    (EXTSR_1000THDX | EXTSR_1000TFDX)) != 0) {
98 			gtcr = PHY_READ(phy, MII_100T2CR);
99 			gtsr = PHY_READ(phy, MII_100T2SR);
100 		} else
101 			gtcr = gtsr = 0;
102 
103 		if ((gtcr & GTCR_ADV_1000TFDX) && (gtsr & GTSR_LP_1000TFDX))
104 			mii->mii_media_active |= IFM_1000_T|IFM_FDX;
105 		else if ((gtcr & GTCR_ADV_1000THDX) &&
106 		    (gtsr & GTSR_LP_1000THDX))
107 			mii->mii_media_active |= IFM_1000_T|IFM_HDX;
108 		else if (anlpar & ANLPAR_TX_FD)
109 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
110 		else if (anlpar & ANLPAR_T4)
111 			mii->mii_media_active |= IFM_100_T4|IFM_HDX;
112 		else if (anlpar & ANLPAR_TX)
113 			mii->mii_media_active |= IFM_100_TX|IFM_HDX;
114 		else if (anlpar & ANLPAR_10_FD)
115 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
116 		else if (anlpar & ANLPAR_10)
117 			mii->mii_media_active |= IFM_10_T|IFM_HDX;
118 		else
119 			mii->mii_media_active |= IFM_NONE;
120 
121 		if ((mii->mii_media_active & IFM_1000_T) != 0 &&
122 		    (gtsr & GTSR_MS_RES) != 0)
123 			mii->mii_media_active |= IFM_ETH_MASTER;
124 
125 		if ((mii->mii_media_active & IFM_FDX) != 0)
126 			mii->mii_media_active |= mii_phy_flowstatus(phy);
127 	} else
128 		mii->mii_media_active = ife->ifm_media;
129 }
130