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