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