nsphy.c revision 150763
150120Swpaul/*	$NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 thorpej Exp $	*/
250120Swpaul
350120Swpaul/*-
450120Swpaul * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
550120Swpaul * All rights reserved.
650120Swpaul *
750120Swpaul * This code is derived from software contributed to The NetBSD Foundation
850120Swpaul * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
950120Swpaul * NASA Ames Research Center.
1050120Swpaul *
1150120Swpaul * Redistribution and use in source and binary forms, with or without
1250120Swpaul * modification, are permitted provided that the following conditions
1350120Swpaul * are met:
1450120Swpaul * 1. Redistributions of source code must retain the above copyright
1550120Swpaul *    notice, this list of conditions and the following disclaimer.
1650120Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1750120Swpaul *    notice, this list of conditions and the following disclaimer in the
1850120Swpaul *    documentation and/or other materials provided with the distribution.
1950120Swpaul * 3. All advertising materials mentioning features or use of this software
2050120Swpaul *    must display the following acknowledgement:
2150120Swpaul *	This product includes software developed by the NetBSD
2250120Swpaul *	Foundation, Inc. and its contributors.
2350120Swpaul * 4. Neither the name of The NetBSD Foundation nor the names of its
2450120Swpaul *    contributors may be used to endorse or promote products derived
2550120Swpaul *    from this software without specific prior written permission.
2650120Swpaul *
2750120Swpaul * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2850120Swpaul * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2950120Swpaul * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3050120Swpaul * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3150120Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3250120Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3350120Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3450120Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3550120Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3650120Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3750120Swpaul * POSSIBILITY OF SUCH DAMAGE.
3850120Swpaul */
3950120Swpaul
40139749Simp/*-
4150120Swpaul * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
4250120Swpaul *
4350120Swpaul * Redistribution and use in source and binary forms, with or without
4450120Swpaul * modification, are permitted provided that the following conditions
4550120Swpaul * are met:
4650120Swpaul * 1. Redistributions of source code must retain the above copyright
4750120Swpaul *    notice, this list of conditions and the following disclaimer.
4850120Swpaul * 2. Redistributions in binary form must reproduce the above copyright
4950120Swpaul *    notice, this list of conditions and the following disclaimer in the
5050120Swpaul *    documentation and/or other materials provided with the distribution.
5150120Swpaul * 3. All advertising materials mentioning features or use of this software
5250120Swpaul *    must display the following acknowledgement:
5350120Swpaul *	This product includes software developed by Manuel Bouyer.
5450120Swpaul * 4. The name of the author may not be used to endorse or promote products
5550120Swpaul *    derived from this software without specific prior written permission.
5650120Swpaul *
5750120Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5850120Swpaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5950120Swpaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
6050120Swpaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
6150120Swpaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
6250120Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
6350120Swpaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
6450120Swpaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6550120Swpaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
6650120Swpaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6750120Swpaul */
6850120Swpaul
69129844Smarius#include <sys/cdefs.h>
70129844Smarius__FBSDID("$FreeBSD: head/sys/dev/mii/nsphy.c 150763 2005-09-30 19:39:27Z imp $");
71129844Smarius
7250120Swpaul/*
7350120Swpaul * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
7450120Swpaul * Data Sheet available from www.national.com
7550120Swpaul */
7650120Swpaul
7750120Swpaul#include <sys/param.h>
7850120Swpaul#include <sys/systm.h>
7950120Swpaul#include <sys/kernel.h>
8050120Swpaul#include <sys/socket.h>
8150120Swpaul#include <sys/errno.h>
8250120Swpaul#include <sys/module.h>
8350120Swpaul#include <sys/bus.h>
8450120Swpaul
8550120Swpaul#include <net/if.h>
8650120Swpaul#include <net/if_media.h>
8750120Swpaul
8850120Swpaul#include <dev/mii/mii.h>
8950120Swpaul#include <dev/mii/miivar.h>
90109514Sobrien#include "miidevs.h"
9150120Swpaul
9250120Swpaul#include <dev/mii/nsphyreg.h>
9350120Swpaul
9450120Swpaul#include "miibus_if.h"
9550120Swpaul
96105135Salfredstatic int nsphy_probe(device_t);
97105135Salfredstatic int nsphy_attach(device_t);
9850120Swpaul
9950120Swpaulstatic device_method_t nsphy_methods[] = {
10050120Swpaul	/* device interface */
10150120Swpaul	DEVMETHOD(device_probe,		nsphy_probe),
10250120Swpaul	DEVMETHOD(device_attach,	nsphy_attach),
10395722Sphk	DEVMETHOD(device_detach,	mii_phy_detach),
10450120Swpaul	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
10550120Swpaul	{ 0, 0 }
10650120Swpaul};
10750120Swpaul
10850120Swpaulstatic devclass_t nsphy_devclass;
10950120Swpaul
11050120Swpaulstatic driver_t nsphy_driver = {
11150120Swpaul	"nsphy",
11250120Swpaul	nsphy_methods,
11350120Swpaul	sizeof(struct mii_softc)
11450120Swpaul};
11550120Swpaul
11650120SwpaulDRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, 0, 0);
11750120Swpaul
11892739Salfredstatic int	nsphy_service(struct mii_softc *, struct mii_data *, int);
11992739Salfredstatic void	nsphy_status(struct mii_softc *);
12050120Swpaul
121105135Salfredstatic int
122150763Simpnsphy_probe(device_t dev)
12350120Swpaul{
12450120Swpaul	struct mii_attach_args *ma;
12550120Swpaul
12650120Swpaul	ma = device_get_ivars(dev);
12750120Swpaul
12859153Ssemenu	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI &&
12959153Ssemenu	    MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840) {
13059153Ssemenu		device_set_desc(dev, MII_STR_NATSEMI_DP83840);
13159153Ssemenu	} else
13250120Swpaul		return (ENXIO);
13350120Swpaul
13450120Swpaul	return (0);
13550120Swpaul}
13650120Swpaul
137105135Salfredstatic int
138150763Simpnsphy_attach(device_t dev)
13950120Swpaul{
14050120Swpaul	struct mii_softc *sc;
14150120Swpaul	struct mii_attach_args *ma;
14250120Swpaul	struct mii_data *mii;
14350120Swpaul
14450120Swpaul	sc = device_get_softc(dev);
14550120Swpaul	ma = device_get_ivars(dev);
14650120Swpaul	sc->mii_dev = device_get_parent(dev);
14750120Swpaul	mii = device_get_softc(sc->mii_dev);
14850120Swpaul	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
14950120Swpaul
15050120Swpaul	sc->mii_inst = mii->mii_instance;
15150120Swpaul	sc->mii_phy = ma->mii_phyno;
15250120Swpaul	sc->mii_service = nsphy_service;
15350120Swpaul	sc->mii_pdata = mii;
15450120Swpaul
15550120Swpaul#ifdef foo
15650120Swpaul	/*
15750120Swpaul	 * i82557 wedges if all of its PHYs are isolated!
15850120Swpaul	 */
15950120Swpaul	if (strcmp(device_get_name(device_get_parent(sc->mii_dev)),
16050120Swpaul	    "fxp") == 0 && mii->mii_instance == 0)
16150120Swpaul#endif
16250120Swpaul
16350120Swpaul	sc->mii_flags |= MIIF_NOISOLATE;
16450120Swpaul	mii->mii_instance++;
16550120Swpaul
16650120Swpaul#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
16750120Swpaul
16850120Swpaul#if 0
16950120Swpaul	/* Can't do this on the i82557! */
17050120Swpaul	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
17150120Swpaul	    BMCR_ISO);
17250120Swpaul#endif
17350120Swpaul	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
17450120Swpaul	    BMCR_LOOP|BMCR_S100);
17550120Swpaul
17650120Swpaul	mii_phy_reset(sc);
17750120Swpaul
17850120Swpaul	sc->mii_capabilities =
17950120Swpaul	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
18050120Swpaul	device_printf(dev, " ");
18195667Sphk	mii_add_media(sc);
18250120Swpaul	printf("\n");
18350120Swpaul#undef ADD
18450120Swpaul
18550120Swpaul	MIIBUS_MEDIAINIT(sc->mii_dev);
18650120Swpaul	return(0);
18750120Swpaul}
18850120Swpaul
18984145Sjlemonstatic int
190150763Simpnsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
19150120Swpaul{
19250120Swpaul	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
19350120Swpaul	int reg;
194129846Smarius	device_t parent;
19550120Swpaul
19650120Swpaul	switch (cmd) {
19750120Swpaul	case MII_POLLSTAT:
19850120Swpaul		/*
19950120Swpaul		 * If we're not polling our PHY instance, just return.
20050120Swpaul		 */
20150120Swpaul		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
20250120Swpaul			return (0);
20350120Swpaul		break;
20450120Swpaul
20550120Swpaul	case MII_MEDIACHG:
20650120Swpaul		/*
20750120Swpaul		 * If the media indicates a different PHY instance,
20850120Swpaul		 * isolate ourselves.
20950120Swpaul		 */
21050120Swpaul		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
21150120Swpaul			reg = PHY_READ(sc, MII_BMCR);
21250120Swpaul			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
21350120Swpaul			return (0);
21450120Swpaul		}
21550120Swpaul
21650120Swpaul		/*
21750120Swpaul		 * If the interface is not up, don't do anything.
21850120Swpaul		 */
21950120Swpaul		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
22050120Swpaul			break;
22150120Swpaul
222129846Smarius		parent = device_get_parent(sc->mii_dev);
223129846Smarius
22450120Swpaul		reg = PHY_READ(sc, MII_NSPHY_PCR);
22550120Swpaul
22650120Swpaul		/*
22750120Swpaul		 * Set up the PCR to use LED4 to indicate full-duplex
22850120Swpaul		 * in both 10baseT and 100baseTX modes.
22950120Swpaul		 */
23050120Swpaul		reg |= PCR_LED4MODE;
23150120Swpaul
23250120Swpaul		/*
233129842Smarius		 * Make sure Carrier Integrity Monitor function is
23450120Swpaul		 * disabled (normal for Node operation, but sometimes
23550120Swpaul		 * it's not set?!)
23650120Swpaul		 */
23750120Swpaul		reg |= PCR_CIMDIS;
23850120Swpaul
23950120Swpaul		/*
24050120Swpaul		 * Make sure "force link good" is set to normal mode.
24150120Swpaul		 * It's only intended for debugging.
24250120Swpaul		 */
24350120Swpaul		reg |= PCR_FLINK100;
24450120Swpaul
24550120Swpaul		/*
24650120Swpaul		 * Mystery bits which are supposedly `reserved',
24750120Swpaul		 * but we seem to need to set them when the PHY
24874353Sjlemon		 * is connected to some interfaces:
24974353Sjlemon		 *
25074353Sjlemon		 * 0x0400 is needed for fxp
25174353Sjlemon		 *        (Intel EtherExpress Pro 10+/100B, 82557 chip)
25274353Sjlemon		 *        (nsphy with a DP83840 chip)
25374353Sjlemon		 * 0x0100 may be needed for some other card
25450120Swpaul		 */
25550120Swpaul		reg |= 0x0100 | 0x0400;
25674353Sjlemon
257129846Smarius		if (strcmp(device_get_name(parent), "fxp") == 0)
25877634Sjlemon			PHY_WRITE(sc, MII_NSPHY_PCR, reg);
25974353Sjlemon
26050120Swpaul		switch (IFM_SUBTYPE(ife->ifm_media)) {
26150120Swpaul		case IFM_AUTO:
26250120Swpaul			/*
263129846Smarius			 * If we're already in auto mode and don't hang off
264129846Smarius			 * of a hme(4), just return. DP83840A on hme(4) don't
265129846Smarius			 * advertise their media capabilities themselves
266129846Smarius			 * properly so force writing the ANAR according to
267129846Smarius			 * the BMSR by mii_phy_auto() every time.
26850120Swpaul			 */
269129846Smarius			if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 &&
270129846Smarius			    strcmp(device_get_name(parent), "hme") != 0)
27150120Swpaul				return (0);
27296026Sphk			(void) mii_phy_auto(sc);
27350120Swpaul			break;
27450120Swpaul		case IFM_100_T4:
27550120Swpaul			/*
27650120Swpaul			 * XXX Not supported as a manual setting right now.
27750120Swpaul			 */
27850120Swpaul			return (EINVAL);
27950120Swpaul		default:
28050120Swpaul			/*
28150120Swpaul			 * BMCR data is stored in the ifmedia entry.
28250120Swpaul			 */
28350120Swpaul			PHY_WRITE(sc, MII_ANAR,
28450120Swpaul			    mii_anar(ife->ifm_media));
28550120Swpaul			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
28650120Swpaul		}
28750120Swpaul		break;
28850120Swpaul
28950120Swpaul	case MII_TICK:
29050120Swpaul		/*
29150120Swpaul		 * If we're not currently selected, just return.
29250120Swpaul		 */
29350120Swpaul		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
29450120Swpaul			return (0);
29584145Sjlemon		if (mii_phy_tick(sc) == EJUSTRETURN)
29650120Swpaul			return (0);
29750120Swpaul		break;
29850120Swpaul	}
29950120Swpaul
30050120Swpaul	/* Update the media status. */
30150120Swpaul	nsphy_status(sc);
30250120Swpaul
30350120Swpaul	/* Callback if something changed. */
30484145Sjlemon	mii_phy_update(sc, cmd);
30550120Swpaul	return (0);
30650120Swpaul}
30750120Swpaul
30884145Sjlemonstatic void
309150763Simpnsphy_status(struct mii_softc *sc)
31050120Swpaul{
31150120Swpaul	struct mii_data *mii = sc->mii_pdata;
31250120Swpaul	int bmsr, bmcr, par, anlpar;
31350120Swpaul
31450120Swpaul	mii->mii_media_status = IFM_AVALID;
31550120Swpaul	mii->mii_media_active = IFM_ETHER;
31650120Swpaul
31750120Swpaul	bmsr = PHY_READ(sc, MII_BMSR) |
31850120Swpaul	    PHY_READ(sc, MII_BMSR);
31950120Swpaul	if (bmsr & BMSR_LINK)
32050120Swpaul		mii->mii_media_status |= IFM_ACTIVE;
32150120Swpaul
32250120Swpaul	bmcr = PHY_READ(sc, MII_BMCR);
32350120Swpaul	if (bmcr & BMCR_ISO) {
32450120Swpaul		mii->mii_media_active |= IFM_NONE;
32550120Swpaul		mii->mii_media_status = 0;
32650120Swpaul		return;
32750120Swpaul	}
32850120Swpaul
32950120Swpaul	if (bmcr & BMCR_LOOP)
33050120Swpaul		mii->mii_media_active |= IFM_LOOP;
33150120Swpaul
33250120Swpaul	if (bmcr & BMCR_AUTOEN) {
33350120Swpaul		/*
33450120Swpaul		 * The PAR status bits are only valid of autonegotiation
33550120Swpaul		 * has completed (or it's disabled).
33650120Swpaul		 */
33750120Swpaul		if ((bmsr & BMSR_ACOMP) == 0) {
33850120Swpaul			/* Erg, still trying, I guess... */
33950120Swpaul			mii->mii_media_active |= IFM_NONE;
34050120Swpaul			return;
34150120Swpaul		}
34250120Swpaul
34350120Swpaul		/*
34450120Swpaul		 * Argh.  The PAR doesn't seem to indicate duplex mode
34550120Swpaul		 * properly!  Determine media based on link partner's
34650120Swpaul		 * advertised capabilities.
34750120Swpaul		 */
34850120Swpaul		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
34950120Swpaul			anlpar = PHY_READ(sc, MII_ANAR) &
35050120Swpaul			    PHY_READ(sc, MII_ANLPAR);
35150120Swpaul			if (anlpar & ANLPAR_T4)
35250120Swpaul				mii->mii_media_active |= IFM_100_T4;
35350120Swpaul			else if (anlpar & ANLPAR_TX_FD)
35450120Swpaul				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
35550120Swpaul			else if (anlpar & ANLPAR_TX)
35650120Swpaul				mii->mii_media_active |= IFM_100_TX;
35750120Swpaul			else if (anlpar & ANLPAR_10_FD)
35850120Swpaul				mii->mii_media_active |= IFM_10_T|IFM_FDX;
35950120Swpaul			else if (anlpar & ANLPAR_10)
36050120Swpaul				mii->mii_media_active |= IFM_10_T;
36150120Swpaul			else
36250120Swpaul				mii->mii_media_active |= IFM_NONE;
36350120Swpaul			return;
36450120Swpaul		}
36550120Swpaul
36650120Swpaul		/*
36750120Swpaul		 * Link partner is not capable of autonegotiation.
36850120Swpaul		 * We will never be in full-duplex mode if this is
36950120Swpaul		 * the case, so reading the PAR is OK.
37050120Swpaul		 */
37150120Swpaul		par = PHY_READ(sc, MII_NSPHY_PAR);
37250120Swpaul		if (par & PAR_10)
37350120Swpaul			mii->mii_media_active |= IFM_10_T;
37450120Swpaul		else
37550120Swpaul			mii->mii_media_active |= IFM_100_TX;
37650120Swpaul#if 0
37750120Swpaul		if (par & PAR_FDX)
37850120Swpaul			mii->mii_media_active |= IFM_FDX;
37950120Swpaul#endif
38050120Swpaul	} else
38159153Ssemenu		mii->mii_media_active |= mii_media_from_bmcr(bmcr);
38250120Swpaul}
383