nsphy.c revision 92739
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/socket.h>
7850120Swpaul#include <sys/errno.h>
7950120Swpaul#include <sys/module.h>
8050120Swpaul#include <sys/bus.h>
8150120Swpaul
8250120Swpaul#include <net/if.h>
8350120Swpaul#include <net/if_media.h>
8450120Swpaul
8550120Swpaul#include <dev/mii/mii.h>
8650120Swpaul#include <dev/mii/miivar.h>
8750120Swpaul#include <dev/mii/miidevs.h>
8850120Swpaul
8950120Swpaul#include <dev/mii/nsphyreg.h>
9050120Swpaul
9150120Swpaul#include "miibus_if.h"
9250120Swpaul
9350120Swpaul#if !defined(lint)
9450120Swpaulstatic const char rcsid[] =
9550477Speter  "$FreeBSD: head/sys/dev/mii/nsphy.c 92739 2002-03-20 02:08:01Z alfred $";
9650120Swpaul#endif
9750120Swpaul
9892739Salfredstatic int nsphy_probe		(device_t);
9992739Salfredstatic int nsphy_attach		(device_t);
10092739Salfredstatic int nsphy_detach		(device_t);
10150120Swpaul
10250120Swpaulstatic device_method_t nsphy_methods[] = {
10350120Swpaul	/* device interface */
10450120Swpaul	DEVMETHOD(device_probe,		nsphy_probe),
10550120Swpaul	DEVMETHOD(device_attach,	nsphy_attach),
10650120Swpaul	DEVMETHOD(device_detach,	nsphy_detach),
10750120Swpaul	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
10850120Swpaul	{ 0, 0 }
10950120Swpaul};
11050120Swpaul
11150120Swpaulstatic devclass_t nsphy_devclass;
11250120Swpaul
11350120Swpaulstatic driver_t nsphy_driver = {
11450120Swpaul	"nsphy",
11550120Swpaul	nsphy_methods,
11650120Swpaul	sizeof(struct mii_softc)
11750120Swpaul};
11850120Swpaul
11950120SwpaulDRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, 0, 0);
12050120Swpaul
12192739Salfredstatic int	nsphy_service(struct mii_softc *, struct mii_data *, int);
12292739Salfredstatic void	nsphy_status(struct mii_softc *);
12350120Swpaul
12450120Swpaulstatic int nsphy_probe(dev)
12550120Swpaul	device_t		dev;
12650120Swpaul{
12750120Swpaul	struct mii_attach_args *ma;
12850120Swpaul
12950120Swpaul	ma = device_get_ivars(dev);
13050120Swpaul
13159153Ssemenu	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI &&
13259153Ssemenu	    MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840) {
13359153Ssemenu		device_set_desc(dev, MII_STR_NATSEMI_DP83840);
13459153Ssemenu	} else
13550120Swpaul		return (ENXIO);
13650120Swpaul
13750120Swpaul	return (0);
13850120Swpaul}
13950120Swpaul
14050120Swpaulstatic int nsphy_attach(dev)
14150120Swpaul	device_t		dev;
14250120Swpaul{
14350120Swpaul	struct mii_softc *sc;
14450120Swpaul	struct mii_attach_args *ma;
14550120Swpaul	struct mii_data *mii;
14650120Swpaul
14750120Swpaul	sc = device_get_softc(dev);
14850120Swpaul	ma = device_get_ivars(dev);
14950120Swpaul	sc->mii_dev = device_get_parent(dev);
15050120Swpaul	mii = device_get_softc(sc->mii_dev);
15150120Swpaul	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
15250120Swpaul
15350120Swpaul	sc->mii_inst = mii->mii_instance;
15450120Swpaul	sc->mii_phy = ma->mii_phyno;
15550120Swpaul	sc->mii_service = nsphy_service;
15650120Swpaul	sc->mii_pdata = mii;
15750120Swpaul
15850120Swpaul#ifdef foo
15950120Swpaul	/*
16050120Swpaul	 * i82557 wedges if all of its PHYs are isolated!
16150120Swpaul	 */
16250120Swpaul	if (strcmp(device_get_name(device_get_parent(sc->mii_dev)),
16350120Swpaul	    "fxp") == 0 && mii->mii_instance == 0)
16450120Swpaul#endif
16550120Swpaul
16650120Swpaul	sc->mii_flags |= MIIF_NOISOLATE;
16750120Swpaul	mii->mii_instance++;
16850120Swpaul
16950120Swpaul#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
17050120Swpaul
17150120Swpaul#if 0
17250120Swpaul	/* Can't do this on the i82557! */
17350120Swpaul	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
17450120Swpaul	    BMCR_ISO);
17550120Swpaul#endif
17650120Swpaul	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
17750120Swpaul	    BMCR_LOOP|BMCR_S100);
17850120Swpaul
17950120Swpaul	mii_phy_reset(sc);
18050120Swpaul
18150120Swpaul	sc->mii_capabilities =
18250120Swpaul	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
18350120Swpaul	device_printf(dev, " ");
18450120Swpaul	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
18550120Swpaul		printf("no media present");
18650120Swpaul	else
18750120Swpaul		mii_add_media(mii, sc->mii_capabilities,
18850120Swpaul		    sc->mii_inst);
18950120Swpaul	printf("\n");
19050120Swpaul#undef ADD
19150120Swpaul
19250120Swpaul	MIIBUS_MEDIAINIT(sc->mii_dev);
19350120Swpaul	return(0);
19450120Swpaul}
19550120Swpaul
19650120Swpaulstatic int nsphy_detach(dev)
19750120Swpaul	device_t		dev;
19850120Swpaul{
19950120Swpaul	struct mii_softc *sc;
20050120Swpaul	struct mii_data *mii;
20150120Swpaul
20250120Swpaul	sc = device_get_softc(dev);
20350120Swpaul	mii = device_get_softc(device_get_parent(dev));
20469925Swpaul	mii_phy_auto_stop(sc);
20550120Swpaul	sc->mii_dev = NULL;
20650120Swpaul	LIST_REMOVE(sc, mii_list);
20750120Swpaul
20850120Swpaul	return(0);
20950120Swpaul}
21050120Swpaul
21184145Sjlemonstatic int
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		/*
26850120Swpaul		 * Mystery bits which are supposedly `reserved',
26950120Swpaul		 * but we seem to need to set them when the PHY
27074353Sjlemon		 * is connected to some interfaces:
27174353Sjlemon		 *
27274353Sjlemon		 * 0x0400 is needed for fxp
27374353Sjlemon		 *        (Intel EtherExpress Pro 10+/100B, 82557 chip)
27474353Sjlemon		 *        (nsphy with a DP83840 chip)
27574353Sjlemon		 * 0x0100 may be needed for some other card
27650120Swpaul		 */
27750120Swpaul		reg |= 0x0100 | 0x0400;
27874353Sjlemon
27977634Sjlemon		if (strcmp(device_get_name(device_get_parent(sc->mii_dev)),
28077634Sjlemon		    "fxp") == 0)
28177634Sjlemon			PHY_WRITE(sc, MII_NSPHY_PCR, reg);
28274353Sjlemon
28350120Swpaul		switch (IFM_SUBTYPE(ife->ifm_media)) {
28450120Swpaul		case IFM_AUTO:
28550120Swpaul			/*
28650120Swpaul			 * If we're already in auto mode, just return.
28750120Swpaul			 */
28850120Swpaul			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
28950120Swpaul				return (0);
29050120Swpaul			(void) mii_phy_auto(sc, 1);
29150120Swpaul			break;
29250120Swpaul		case IFM_100_T4:
29350120Swpaul			/*
29450120Swpaul			 * XXX Not supported as a manual setting right now.
29550120Swpaul			 */
29650120Swpaul			return (EINVAL);
29750120Swpaul		default:
29850120Swpaul			/*
29950120Swpaul			 * BMCR data is stored in the ifmedia entry.
30050120Swpaul			 */
30150120Swpaul			PHY_WRITE(sc, MII_ANAR,
30250120Swpaul			    mii_anar(ife->ifm_media));
30350120Swpaul			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
30450120Swpaul		}
30550120Swpaul		break;
30650120Swpaul
30750120Swpaul	case MII_TICK:
30850120Swpaul		/*
30950120Swpaul		 * If we're not currently selected, just return.
31050120Swpaul		 */
31150120Swpaul		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
31250120Swpaul			return (0);
31384145Sjlemon		if (mii_phy_tick(sc) == EJUSTRETURN)
31450120Swpaul			return (0);
31550120Swpaul		break;
31650120Swpaul	}
31750120Swpaul
31850120Swpaul	/* Update the media status. */
31950120Swpaul	nsphy_status(sc);
32050120Swpaul
32150120Swpaul	/* Callback if something changed. */
32284145Sjlemon	mii_phy_update(sc, cmd);
32350120Swpaul	return (0);
32450120Swpaul}
32550120Swpaul
32684145Sjlemonstatic void
32750120Swpaulnsphy_status(sc)
32850120Swpaul	struct mii_softc *sc;
32950120Swpaul{
33050120Swpaul	struct mii_data *mii = sc->mii_pdata;
33150120Swpaul	int bmsr, bmcr, par, anlpar;
33250120Swpaul
33350120Swpaul	mii->mii_media_status = IFM_AVALID;
33450120Swpaul	mii->mii_media_active = IFM_ETHER;
33550120Swpaul
33650120Swpaul	bmsr = PHY_READ(sc, MII_BMSR) |
33750120Swpaul	    PHY_READ(sc, MII_BMSR);
33850120Swpaul	if (bmsr & BMSR_LINK)
33950120Swpaul		mii->mii_media_status |= IFM_ACTIVE;
34050120Swpaul
34150120Swpaul	bmcr = PHY_READ(sc, MII_BMCR);
34250120Swpaul	if (bmcr & BMCR_ISO) {
34350120Swpaul		mii->mii_media_active |= IFM_NONE;
34450120Swpaul		mii->mii_media_status = 0;
34550120Swpaul		return;
34650120Swpaul	}
34750120Swpaul
34850120Swpaul	if (bmcr & BMCR_LOOP)
34950120Swpaul		mii->mii_media_active |= IFM_LOOP;
35050120Swpaul
35150120Swpaul	if (bmcr & BMCR_AUTOEN) {
35250120Swpaul		/*
35350120Swpaul		 * The PAR status bits are only valid of autonegotiation
35450120Swpaul		 * has completed (or it's disabled).
35550120Swpaul		 */
35650120Swpaul		if ((bmsr & BMSR_ACOMP) == 0) {
35750120Swpaul			/* Erg, still trying, I guess... */
35850120Swpaul			mii->mii_media_active |= IFM_NONE;
35950120Swpaul			return;
36050120Swpaul		}
36150120Swpaul
36250120Swpaul		/*
36350120Swpaul		 * Argh.  The PAR doesn't seem to indicate duplex mode
36450120Swpaul		 * properly!  Determine media based on link partner's
36550120Swpaul		 * advertised capabilities.
36650120Swpaul		 */
36750120Swpaul		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
36850120Swpaul			anlpar = PHY_READ(sc, MII_ANAR) &
36950120Swpaul			    PHY_READ(sc, MII_ANLPAR);
37050120Swpaul			if (anlpar & ANLPAR_T4)
37150120Swpaul				mii->mii_media_active |= IFM_100_T4;
37250120Swpaul			else if (anlpar & ANLPAR_TX_FD)
37350120Swpaul				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
37450120Swpaul			else if (anlpar & ANLPAR_TX)
37550120Swpaul				mii->mii_media_active |= IFM_100_TX;
37650120Swpaul			else if (anlpar & ANLPAR_10_FD)
37750120Swpaul				mii->mii_media_active |= IFM_10_T|IFM_FDX;
37850120Swpaul			else if (anlpar & ANLPAR_10)
37950120Swpaul				mii->mii_media_active |= IFM_10_T;
38050120Swpaul			else
38150120Swpaul				mii->mii_media_active |= IFM_NONE;
38250120Swpaul			return;
38350120Swpaul		}
38450120Swpaul
38550120Swpaul		/*
38650120Swpaul		 * Link partner is not capable of autonegotiation.
38750120Swpaul		 * We will never be in full-duplex mode if this is
38850120Swpaul		 * the case, so reading the PAR is OK.
38950120Swpaul		 */
39050120Swpaul		par = PHY_READ(sc, MII_NSPHY_PAR);
39150120Swpaul		if (par & PAR_10)
39250120Swpaul			mii->mii_media_active |= IFM_10_T;
39350120Swpaul		else
39450120Swpaul			mii->mii_media_active |= IFM_100_TX;
39550120Swpaul#if 0
39650120Swpaul		if (par & PAR_FDX)
39750120Swpaul			mii->mii_media_active |= IFM_FDX;
39850120Swpaul#endif
39950120Swpaul	} else
40059153Ssemenu		mii->mii_media_active |= mii_media_from_bmcr(bmcr);
40150120Swpaul}
402