ukphy_subr.c revision 129844
1251881Speter/*	$NetBSD: ukphy_subr.c,v 1.2 1998/11/05 04:08:02 thorpej Exp $	*/
2251881Speter
3251881Speter/*-
4251881Speter * Copyright (c) 1998 The NetBSD Foundation, Inc.
5251881Speter * All rights reserved.
6251881Speter *
7251881Speter * This code is derived from software contributed to The NetBSD Foundation
8251881Speter * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9251881Speter * NASA Ames Research Center, and by Frank van der Linden.
10251881Speter *
11251881Speter * Redistribution and use in source and binary forms, with or without
12251881Speter * modification, are permitted provided that the following conditions
13251881Speter * are met:
14251881Speter * 1. Redistributions of source code must retain the above copyright
15251881Speter *    notice, this list of conditions and the following disclaimer.
16251881Speter * 2. Redistributions in binary form must reproduce the above copyright
17251881Speter *    notice, this list of conditions and the following disclaimer in the
18251881Speter *    documentation and/or other materials provided with the distribution.
19251881Speter * 3. All advertising materials mentioning features or use of this software
20251881Speter *    must display the following acknowledgement:
21251881Speter *	This product includes software developed by the NetBSD
22251881Speter *	Foundation, Inc. and its contributors.
23251881Speter * 4. Neither the name of The NetBSD Foundation nor the names of its
24251881Speter *    contributors may be used to endorse or promote products derived
25251881Speter *    from this software without specific prior written permission.
26251881Speter *
27251881Speter * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28251881Speter * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29251881Speter * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30251881Speter * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31251881Speter * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32251881Speter * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33251881Speter * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34251881Speter * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35251881Speter * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36251881Speter * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37251881Speter * POSSIBILITY OF SUCH DAMAGE.
38251881Speter */
39251881Speter
40251881Speter#include <sys/cdefs.h>
41251881Speter__FBSDID("$FreeBSD: head/sys/dev/mii/ukphy_subr.c 129844 2004-05-29 18:09:10Z marius $");
42251881Speter
43251881Speter/*
44251881Speter * Subroutines shared by the ukphy driver and other PHY drivers.
45251881Speter */
46251881Speter
47251881Speter#include <sys/param.h>
48251881Speter#include <sys/systm.h>
49251881Speter#include <sys/socket.h>
50251881Speter#include <sys/module.h>
51251881Speter#include <sys/bus.h>
52251881Speter
53251881Speter#include <net/if.h>
54251881Speter#include <net/if_media.h>
55251881Speter
56251881Speter#include <dev/mii/mii.h>
57251881Speter#include <dev/mii/miivar.h>
58251881Speter
59251881Speter#include "miibus_if.h"
60251881Speter
61251881Speter/*
62251881Speter * Media status subroutine.  If a PHY driver does media detection simply
63251881Speter * by decoding the NWay autonegotiation, use this routine.
64251881Speter */
65251881Spetervoid
66251881Speterukphy_status(struct mii_softc *phy)
67251881Speter{
68251881Speter	struct mii_data *mii = phy->mii_pdata;
69251881Speter	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
70251881Speter	int bmsr, bmcr, anlpar;
71251881Speter
72251881Speter	mii->mii_media_status = IFM_AVALID;
73251881Speter	mii->mii_media_active = IFM_ETHER;
74251881Speter
75251881Speter	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
76251881Speter	if (bmsr & BMSR_LINK)
77251881Speter		mii->mii_media_status |= IFM_ACTIVE;
78251881Speter
79251881Speter	bmcr = PHY_READ(phy, MII_BMCR);
80251881Speter	if (bmcr & BMCR_ISO) {
81251881Speter		mii->mii_media_active |= IFM_NONE;
82251881Speter		mii->mii_media_status = 0;
83251881Speter		return;
84251881Speter	}
85251881Speter
86251881Speter	if (bmcr & BMCR_LOOP)
87251881Speter		mii->mii_media_active |= IFM_LOOP;
88251881Speter
89251881Speter	if (bmcr & BMCR_AUTOEN) {
90251881Speter		/*
91251881Speter		 * NWay autonegotiation takes the highest-order common
92251881Speter		 * bit of the ANAR and ANLPAR (i.e. best media advertised
93251881Speter		 * both by us and our link partner).
94251881Speter		 */
95251881Speter		if ((bmsr & BMSR_ACOMP) == 0) {
96251881Speter			/* Erg, still trying, I guess... */
97251881Speter			mii->mii_media_active |= IFM_NONE;
98251881Speter			return;
99251881Speter		}
100251881Speter
101251881Speter		anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
102251881Speter		if (anlpar & ANLPAR_T4)
103251881Speter			mii->mii_media_active |= IFM_100_T4;
104251881Speter		else if (anlpar & ANLPAR_TX_FD)
105251881Speter			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
106251881Speter		else if (anlpar & ANLPAR_TX)
107251881Speter			mii->mii_media_active |= IFM_100_TX;
108251881Speter		else if (anlpar & ANLPAR_10_FD)
109251881Speter			mii->mii_media_active |= IFM_10_T|IFM_FDX;
110251881Speter		else if (anlpar & ANLPAR_10)
111251881Speter			mii->mii_media_active |= IFM_10_T;
112251881Speter		else
113251881Speter			mii->mii_media_active |= IFM_NONE;
114251881Speter	} else
115251881Speter		mii->mii_media_active = ife->ifm_media;
116251881Speter}
117251881Speter