nsphy.c revision 50120
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
4050120Swpaul/*
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
6950120Swpaul/*
7050120Swpaul * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
7150120Swpaul * Data Sheet available from www.national.com
7250120Swpaul */
7350120Swpaul
7450120Swpaul#include <sys/param.h>
7550120Swpaul#include <sys/systm.h>
7650120Swpaul#include <sys/kernel.h>
7750120Swpaul#include <sys/malloc.h>
7850120Swpaul#include <sys/socket.h>
7950120Swpaul#include <sys/errno.h>
8050120Swpaul#include <sys/module.h>
8150120Swpaul#include <sys/bus.h>
8250120Swpaul
8350120Swpaul#include <net/if.h>
8450120Swpaul#include <net/if_media.h>
8550120Swpaul
8650120Swpaul#include <dev/mii/mii.h>
8750120Swpaul#include <dev/mii/miivar.h>
8850120Swpaul#include <dev/mii/miidevs.h>
8950120Swpaul
9050120Swpaul#include <dev/mii/nsphyreg.h>
9150120Swpaul
9250120Swpaul#include "miibus_if.h"
9350120Swpaul
9450120Swpaul#if !defined(lint)
9550120Swpaulstatic const char rcsid[] =
9650120Swpaul	"$Id$";
9750120Swpaul#endif
9850120Swpaul
9950120Swpaulstatic int nsphy_probe		__P((device_t));
10050120Swpaulstatic int nsphy_attach		__P((device_t));
10150120Swpaulstatic int nsphy_detach		__P((device_t));
10250120Swpaul
10350120Swpaulstatic device_method_t nsphy_methods[] = {
10450120Swpaul	/* device interface */
10550120Swpaul	DEVMETHOD(device_probe,		nsphy_probe),
10650120Swpaul	DEVMETHOD(device_attach,	nsphy_attach),
10750120Swpaul	DEVMETHOD(device_detach,	nsphy_detach),
10850120Swpaul	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
10950120Swpaul	{ 0, 0 }
11050120Swpaul};
11150120Swpaul
11250120Swpaulstatic devclass_t nsphy_devclass;
11350120Swpaul
11450120Swpaulstatic driver_t nsphy_driver = {
11550120Swpaul	"nsphy",
11650120Swpaul	nsphy_methods,
11750120Swpaul	sizeof(struct mii_softc)
11850120Swpaul};
11950120Swpaul
12050120SwpaulDRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, 0, 0);
12150120Swpaul
12250120Swpaulint	nsphy_service __P((struct mii_softc *, struct mii_data *, int));
12350120Swpaulvoid	nsphy_status __P((struct mii_softc *));
12450120Swpaul
12550120Swpaulstatic int nsphy_probe(dev)
12650120Swpaul	device_t		dev;
12750120Swpaul{
12850120Swpaul	struct mii_attach_args *ma;
12950120Swpaul
13050120Swpaul	ma = device_get_ivars(dev);
13150120Swpaul
13250120Swpaul	if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_NATSEMI ||
13350120Swpaul	    MII_MODEL(ma->mii_id2) != MII_MODEL_NATSEMI_DP83840)
13450120Swpaul		return (ENXIO);
13550120Swpaul
13650120Swpaul	device_set_desc(dev, MII_STR_NATSEMI_DP83840);
13750120Swpaul
13850120Swpaul	return (0);
13950120Swpaul}
14050120Swpaul
14150120Swpaulstatic int nsphy_attach(dev)
14250120Swpaul	device_t		dev;
14350120Swpaul{
14450120Swpaul	struct mii_softc *sc;
14550120Swpaul	struct mii_attach_args *ma;
14650120Swpaul	struct mii_data *mii;
14750120Swpaul
14850120Swpaul	sc = device_get_softc(dev);
14950120Swpaul	ma = device_get_ivars(dev);
15050120Swpaul	sc->mii_dev = device_get_parent(dev);
15150120Swpaul	mii = device_get_softc(sc->mii_dev);
15250120Swpaul	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
15350120Swpaul
15450120Swpaul	sc->mii_inst = mii->mii_instance;
15550120Swpaul	sc->mii_phy = ma->mii_phyno;
15650120Swpaul	sc->mii_service = nsphy_service;
15750120Swpaul	sc->mii_pdata = mii;
15850120Swpaul
15950120Swpaul#ifdef foo
16050120Swpaul	/*
16150120Swpaul	 * i82557 wedges if all of its PHYs are isolated!
16250120Swpaul	 */
16350120Swpaul	if (strcmp(device_get_name(device_get_parent(sc->mii_dev)),
16450120Swpaul	    "fxp") == 0 && mii->mii_instance == 0)
16550120Swpaul#endif
16650120Swpaul
16750120Swpaul	sc->mii_flags |= MIIF_NOISOLATE;
16850120Swpaul	mii->mii_instance++;
16950120Swpaul
17050120Swpaul#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
17150120Swpaul
17250120Swpaul#if 0
17350120Swpaul	/* Can't do this on the i82557! */
17450120Swpaul	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
17550120Swpaul	    BMCR_ISO);
17650120Swpaul#endif
17750120Swpaul	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
17850120Swpaul	    BMCR_LOOP|BMCR_S100);
17950120Swpaul
18050120Swpaul	mii_phy_reset(sc);
18150120Swpaul
18250120Swpaul	sc->mii_capabilities =
18350120Swpaul	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
18450120Swpaul	device_printf(dev, " ");
18550120Swpaul	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
18650120Swpaul		printf("no media present");
18750120Swpaul	else
18850120Swpaul		mii_add_media(mii, sc->mii_capabilities,
18950120Swpaul		    sc->mii_inst);
19050120Swpaul	printf("\n");
19150120Swpaul#undef ADD
19250120Swpaul
19350120Swpaul	MIIBUS_MEDIAINIT(sc->mii_dev);
19450120Swpaul	return(0);
19550120Swpaul}
19650120Swpaul
19750120Swpaulstatic int nsphy_detach(dev)
19850120Swpaul	device_t		dev;
19950120Swpaul{
20050120Swpaul	struct mii_softc *sc;
20150120Swpaul	struct mii_data *mii;
20250120Swpaul
20350120Swpaul	sc = device_get_softc(dev);
20450120Swpaul	mii = device_get_softc(device_get_parent(dev));
20550120Swpaul	sc->mii_dev = NULL;
20650120Swpaul	LIST_REMOVE(sc, mii_list);
20750120Swpaul
20850120Swpaul	return(0);
20950120Swpaul}
21050120Swpaul
21150120Swpaulint
21250120Swpaulnsphy_service(sc, mii, cmd)
21350120Swpaul	struct mii_softc *sc;
21450120Swpaul	struct mii_data *mii;
21550120Swpaul	int cmd;
21650120Swpaul{
21750120Swpaul	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
21850120Swpaul	int reg;
21950120Swpaul
22050120Swpaul	switch (cmd) {
22150120Swpaul	case MII_POLLSTAT:
22250120Swpaul		/*
22350120Swpaul		 * If we're not polling our PHY instance, just return.
22450120Swpaul		 */
22550120Swpaul		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
22650120Swpaul			return (0);
22750120Swpaul		break;
22850120Swpaul
22950120Swpaul	case MII_MEDIACHG:
23050120Swpaul		/*
23150120Swpaul		 * If the media indicates a different PHY instance,
23250120Swpaul		 * isolate ourselves.
23350120Swpaul		 */
23450120Swpaul		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
23550120Swpaul			reg = PHY_READ(sc, MII_BMCR);
23650120Swpaul			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
23750120Swpaul			return (0);
23850120Swpaul		}
23950120Swpaul
24050120Swpaul		/*
24150120Swpaul		 * If the interface is not up, don't do anything.
24250120Swpaul		 */
24350120Swpaul		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
24450120Swpaul			break;
24550120Swpaul
24650120Swpaul		reg = PHY_READ(sc, MII_NSPHY_PCR);
24750120Swpaul
24850120Swpaul		/*
24950120Swpaul		 * Set up the PCR to use LED4 to indicate full-duplex
25050120Swpaul		 * in both 10baseT and 100baseTX modes.
25150120Swpaul		 */
25250120Swpaul		reg |= PCR_LED4MODE;
25350120Swpaul
25450120Swpaul		/*
25550120Swpaul		 * Make sure Carrier Intgrity Monitor function is
25650120Swpaul		 * disabled (normal for Node operation, but sometimes
25750120Swpaul		 * it's not set?!)
25850120Swpaul		 */
25950120Swpaul		reg |= PCR_CIMDIS;
26050120Swpaul
26150120Swpaul		/*
26250120Swpaul		 * Make sure "force link good" is set to normal mode.
26350120Swpaul		 * It's only intended for debugging.
26450120Swpaul		 */
26550120Swpaul		reg |= PCR_FLINK100;
26650120Swpaul
26750120Swpaul#if 0
26850120Swpaul		/*
26950120Swpaul		 * Mystery bits which are supposedly `reserved',
27050120Swpaul		 * but we seem to need to set them when the PHY
27150120Swpaul		 * is connected to some interfaces!
27250120Swpaul		 */
27350120Swpaul		reg |= 0x0100 | 0x0400;
27450120Swpaul#endif
27550120Swpaul/*
27650120Swpaul		PHY_WRITE(sc, MII_NSPHY_PCR, reg);
27750120Swpaul*/
27850120Swpaul		switch (IFM_SUBTYPE(ife->ifm_media)) {
27950120Swpaul		case IFM_AUTO:
28050120Swpaul			/*
28150120Swpaul			 * If we're already in auto mode, just return.
28250120Swpaul			 */
28350120Swpaul			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
28450120Swpaul				return (0);
28550120Swpaul			(void) mii_phy_auto(sc, 1);
28650120Swpaul			break;
28750120Swpaul		case IFM_100_T4:
28850120Swpaul			/*
28950120Swpaul			 * XXX Not supported as a manual setting right now.
29050120Swpaul			 */
29150120Swpaul			return (EINVAL);
29250120Swpaul		default:
29350120Swpaul			/*
29450120Swpaul			 * BMCR data is stored in the ifmedia entry.
29550120Swpaul			 */
29650120Swpaul			PHY_WRITE(sc, MII_ANAR,
29750120Swpaul			    mii_anar(ife->ifm_media));
29850120Swpaul			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
29950120Swpaul		}
30050120Swpaul		break;
30150120Swpaul
30250120Swpaul	case MII_TICK:
30350120Swpaul		/*
30450120Swpaul		 * If we're not currently selected, just return.
30550120Swpaul		 */
30650120Swpaul		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
30750120Swpaul			return (0);
30850120Swpaul
30950120Swpaul		/*
31050120Swpaul		 * Only used for autonegotiation.
31150120Swpaul		 */
31250120Swpaul		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
31350120Swpaul			return (0);
31450120Swpaul
31550120Swpaul		/*
31650120Swpaul		 * Is the interface even up?
31750120Swpaul		 */
31850120Swpaul		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
31950120Swpaul			return (0);
32050120Swpaul
32150120Swpaul		/*
32250120Swpaul		 * Check to see if we have link.  If we do, we don't
32350120Swpaul		 * need to restart the autonegotiation process.  Read
32450120Swpaul		 * the BMSR twice in case it's latched.
32550120Swpaul		 */
32650120Swpaul		reg = PHY_READ(sc, MII_BMSR) |
32750120Swpaul		    PHY_READ(sc, MII_BMSR);
32850120Swpaul		if (reg & BMSR_LINK)
32950120Swpaul			return (0);
33050120Swpaul
33150120Swpaul		/*
33250120Swpaul		 * Only retry autonegotiation every 5 seconds.
33350120Swpaul		 */
33450120Swpaul		if (++sc->mii_ticks != 5)
33550120Swpaul			return (0);
33650120Swpaul
33750120Swpaul		sc->mii_ticks = 0;
33850120Swpaul		mii_phy_reset(sc);
33950120Swpaul		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
34050120Swpaul			return (0);
34150120Swpaul		break;
34250120Swpaul	}
34350120Swpaul
34450120Swpaul	/* Update the media status. */
34550120Swpaul	nsphy_status(sc);
34650120Swpaul
34750120Swpaul	/* Callback if something changed. */
34850120Swpaul	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
34950120Swpaul		MIIBUS_STATCHG(sc->mii_dev);
35050120Swpaul		sc->mii_active = mii->mii_media_active;
35150120Swpaul	}
35250120Swpaul	return (0);
35350120Swpaul}
35450120Swpaul
35550120Swpaulvoid
35650120Swpaulnsphy_status(sc)
35750120Swpaul	struct mii_softc *sc;
35850120Swpaul{
35950120Swpaul	struct mii_data *mii = sc->mii_pdata;
36050120Swpaul	int bmsr, bmcr, par, anlpar;
36150120Swpaul
36250120Swpaul	mii->mii_media_status = IFM_AVALID;
36350120Swpaul	mii->mii_media_active = IFM_ETHER;
36450120Swpaul
36550120Swpaul	bmsr = PHY_READ(sc, MII_BMSR) |
36650120Swpaul	    PHY_READ(sc, MII_BMSR);
36750120Swpaul	if (bmsr & BMSR_LINK)
36850120Swpaul		mii->mii_media_status |= IFM_ACTIVE;
36950120Swpaul
37050120Swpaul	bmcr = PHY_READ(sc, MII_BMCR);
37150120Swpaul	if (bmcr & BMCR_ISO) {
37250120Swpaul		mii->mii_media_active |= IFM_NONE;
37350120Swpaul		mii->mii_media_status = 0;
37450120Swpaul		return;
37550120Swpaul	}
37650120Swpaul
37750120Swpaul	if (bmcr & BMCR_LOOP)
37850120Swpaul		mii->mii_media_active |= IFM_LOOP;
37950120Swpaul
38050120Swpaul	if (bmcr & BMCR_AUTOEN) {
38150120Swpaul		/*
38250120Swpaul		 * The PAR status bits are only valid of autonegotiation
38350120Swpaul		 * has completed (or it's disabled).
38450120Swpaul		 */
38550120Swpaul		if ((bmsr & BMSR_ACOMP) == 0) {
38650120Swpaul			/* Erg, still trying, I guess... */
38750120Swpaul			mii->mii_media_active |= IFM_NONE;
38850120Swpaul			return;
38950120Swpaul		}
39050120Swpaul
39150120Swpaul		/*
39250120Swpaul		 * Argh.  The PAR doesn't seem to indicate duplex mode
39350120Swpaul		 * properly!  Determine media based on link partner's
39450120Swpaul		 * advertised capabilities.
39550120Swpaul		 */
39650120Swpaul		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
39750120Swpaul			anlpar = PHY_READ(sc, MII_ANAR) &
39850120Swpaul			    PHY_READ(sc, MII_ANLPAR);
39950120Swpaul			if (anlpar & ANLPAR_T4)
40050120Swpaul				mii->mii_media_active |= IFM_100_T4;
40150120Swpaul			else if (anlpar & ANLPAR_TX_FD)
40250120Swpaul				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
40350120Swpaul			else if (anlpar & ANLPAR_TX)
40450120Swpaul				mii->mii_media_active |= IFM_100_TX;
40550120Swpaul			else if (anlpar & ANLPAR_10_FD)
40650120Swpaul				mii->mii_media_active |= IFM_10_T|IFM_FDX;
40750120Swpaul			else if (anlpar & ANLPAR_10)
40850120Swpaul				mii->mii_media_active |= IFM_10_T;
40950120Swpaul			else
41050120Swpaul				mii->mii_media_active |= IFM_NONE;
41150120Swpaul			return;
41250120Swpaul		}
41350120Swpaul
41450120Swpaul		/*
41550120Swpaul		 * Link partner is not capable of autonegotiation.
41650120Swpaul		 * We will never be in full-duplex mode if this is
41750120Swpaul		 * the case, so reading the PAR is OK.
41850120Swpaul		 */
41950120Swpaul		par = PHY_READ(sc, MII_NSPHY_PAR);
42050120Swpaul		if (par & PAR_10)
42150120Swpaul			mii->mii_media_active |= IFM_10_T;
42250120Swpaul		else
42350120Swpaul			mii->mii_media_active |= IFM_100_TX;
42450120Swpaul#if 0
42550120Swpaul		if (par & PAR_FDX)
42650120Swpaul			mii->mii_media_active |= IFM_FDX;
42750120Swpaul#endif
42850120Swpaul	} else
42950120Swpaul		mii->mii_media_active = mii_media_from_bmcr(bmcr);
43050120Swpaul}
431