1/* $NetBSD: ukphy_subr.c,v 1.13 2014/06/16 16:48:16 msaitoh 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.13 2014/06/16 16:48:16 msaitoh 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 */
56void
57ukphy_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 int bmsr, bmcr, anlpar, gtcr, gtsr;
62
63 mii->mii_media_status = IFM_AVALID;
64 mii->mii_media_active = IFM_ETHER;
65
66 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
67 if (bmsr & BMSR_LINK)
68 mii->mii_media_status |= IFM_ACTIVE;
69
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;
74 return;
75 }
76
77 if (bmcr & BMCR_LOOP)
78 mii->mii_media_active |= IFM_LOOP;
79
80 if (bmcr & BMCR_AUTOEN) {
81 /*
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).
85 */
86 if ((bmsr & BMSR_ACOMP) == 0) {
87 /* Erg, still trying, I guess... */
88 mii->mii_media_active |= IFM_NONE;
89 return;
90 }
91
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);
98 } else
99 gtcr = gtsr = 0;
100
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|IFM_HDX;
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|IFM_HDX;
110 else if (anlpar & ANLPAR_TX)
111 mii->mii_media_active |= IFM_100_TX|IFM_HDX;
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|IFM_HDX;
116 else
117 mii->mii_media_active |= IFM_NONE;
118
119 if ((mii->mii_media_active & IFM_1000_T) &&
120 (gtsr & GTSR_MS_RES))
121 mii->mii_media_active |= IFM_ETH_MASTER;
122
123 if (mii->mii_media_active & IFM_FDX)
124 mii->mii_media_active |= mii_phy_flowstatus(phy);
125 } else
126 mii->mii_media_active = ife->ifm_media;
127}
128