1 /* $NetBSD: ukphy_subr.c,v 1.10 2008/04/28 20:23:53 martin Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
34 * Subroutines shared by the ukphy driver and other PHY drivers.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ukphy_subr.c,v 1.10 2008/04/28 20:23:53 martin Exp $");
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>
47 #include <net/if_media.h>
49 #include <dev/mii/mii.h>
50 #include <dev/mii/miivar.h>
53 * Media status subroutine. If a PHY driver does media detection simply
54 * by decoding the NWay autonegotiation, use this routine.
57 ukphy_status(struct mii_softc
*phy
)
59 struct mii_data
*mii
= phy
->mii_pdata
;
60 struct ifmedia_entry
*ife
= mii
->mii_media
.ifm_cur
;
61 int bmsr
, bmcr
, anlpar
, gtcr
, gtsr
;
63 mii
->mii_media_status
= IFM_AVALID
;
64 mii
->mii_media_active
= IFM_ETHER
;
66 bmsr
= PHY_READ(phy
, MII_BMSR
) | PHY_READ(phy
, MII_BMSR
);
68 mii
->mii_media_status
|= IFM_ACTIVE
;
70 bmcr
= PHY_READ(phy
, MII_BMCR
);
71 if (bmcr
& BMCR_ISO
) {
72 mii
->mii_media_active
|= IFM_NONE
;
73 mii
->mii_media_status
= 0;
78 mii
->mii_media_active
|= IFM_LOOP
;
80 if (bmcr
& BMCR_AUTOEN
) {
82 * NWay autonegotiation takes the highest-order common
83 * bit of the ANAR and ANLPAR (i.e. best media advertised
84 * both by us and our link partner).
86 if ((bmsr
& BMSR_ACOMP
) == 0) {
87 /* Erg, still trying, I guess... */
88 mii
->mii_media_active
|= IFM_NONE
;
92 anlpar
= PHY_READ(phy
, MII_ANAR
) & PHY_READ(phy
, MII_ANLPAR
);
93 if ((phy
->mii_flags
& MIIF_HAVE_GTCR
) != 0 &&
94 (phy
->mii_extcapabilities
&
95 (EXTSR_1000THDX
|EXTSR_1000TFDX
)) != 0) {
96 gtcr
= PHY_READ(phy
, MII_100T2CR
);
97 gtsr
= PHY_READ(phy
, MII_100T2SR
);
101 if ((gtcr
& GTCR_ADV_1000TFDX
) && (gtsr
& GTSR_LP_1000TFDX
))
102 mii
->mii_media_active
|= IFM_1000_T
|IFM_FDX
;
103 else if ((gtcr
& GTCR_ADV_1000THDX
) &&
104 (gtsr
& GTSR_LP_1000THDX
))
105 mii
->mii_media_active
|= IFM_1000_T
;
106 else if (anlpar
& ANLPAR_TX_FD
)
107 mii
->mii_media_active
|= IFM_100_TX
|IFM_FDX
;
108 else if (anlpar
& ANLPAR_T4
)
109 mii
->mii_media_active
|= IFM_100_T4
;
110 else if (anlpar
& ANLPAR_TX
)
111 mii
->mii_media_active
|= IFM_100_TX
;
112 else if (anlpar
& ANLPAR_10_FD
)
113 mii
->mii_media_active
|= IFM_10_T
|IFM_FDX
;
114 else if (anlpar
& ANLPAR_10
)
115 mii
->mii_media_active
|= IFM_10_T
;
117 mii
->mii_media_active
|= IFM_NONE
;
119 if ((mii
->mii_media_active
& IFM_1000_T
) &&
120 (gtsr
& GTSR_MS_RES
))
121 mii
->mii_media_active
|= IFM_ETH_MASTER
;
123 if (mii
->mii_media_active
& IFM_FDX
)
124 mii
->mii_media_active
|= mii_phy_flowstatus(phy
);
126 mii
->mii_media_active
= ife
->ifm_media
;