Home | History | Annotate | Line # | Download | only in mii
nsphy.c revision 1.30
      1  1.30   thorpej /*	$NetBSD: nsphy.c,v 1.30 2001/05/31 16:02:29 thorpej Exp $	*/
      2   1.5   thorpej 
      3   1.5   thorpej /*-
      4  1.27   thorpej  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      5   1.5   thorpej  * All rights reserved.
      6   1.5   thorpej  *
      7   1.5   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.5   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.5   thorpej  * NASA Ames Research Center.
     10   1.5   thorpej  *
     11   1.5   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.5   thorpej  * modification, are permitted provided that the following conditions
     13   1.5   thorpej  * are met:
     14   1.5   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.5   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.5   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.5   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.5   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.5   thorpej  * 3. All advertising materials mentioning features or use of this software
     20   1.5   thorpej  *    must display the following acknowledgement:
     21   1.5   thorpej  *	This product includes software developed by the NetBSD
     22   1.5   thorpej  *	Foundation, Inc. and its contributors.
     23   1.5   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.5   thorpej  *    contributors may be used to endorse or promote products derived
     25   1.5   thorpej  *    from this software without specific prior written permission.
     26   1.5   thorpej  *
     27   1.5   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.5   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.5   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.5   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.5   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.5   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.5   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.5   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.5   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.5   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.5   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38   1.5   thorpej  */
     39   1.5   thorpej 
     40   1.1    bouyer /*
     41   1.1    bouyer  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     42   1.1    bouyer  *
     43   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     44   1.1    bouyer  * modification, are permitted provided that the following conditions
     45   1.1    bouyer  * are met:
     46   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     47   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     48   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     50   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     51   1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     52   1.1    bouyer  *    must display the following acknowledgement:
     53   1.2   thorpej  *	This product includes software developed by Manuel Bouyer.
     54   1.1    bouyer  * 4. The name of the author may not be used to endorse or promote products
     55   1.1    bouyer  *    derived from this software without specific prior written permission.
     56   1.1    bouyer  *
     57   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58   1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59   1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60   1.1    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61   1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62   1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63   1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64   1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65   1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66   1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67   1.1    bouyer  */
     68   1.1    bouyer 
     69   1.5   thorpej /*
     70   1.5   thorpej  * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
     71   1.5   thorpej  * Data Sheet available from www.national.com
     72   1.5   thorpej  */
     73   1.1    bouyer 
     74   1.1    bouyer #include <sys/param.h>
     75   1.1    bouyer #include <sys/systm.h>
     76   1.1    bouyer #include <sys/kernel.h>
     77   1.1    bouyer #include <sys/device.h>
     78   1.1    bouyer #include <sys/malloc.h>
     79   1.1    bouyer #include <sys/socket.h>
     80  1.17   thorpej #include <sys/errno.h>
     81   1.5   thorpej 
     82   1.1    bouyer #include <net/if.h>
     83   1.1    bouyer #include <net/if_media.h>
     84   1.1    bouyer 
     85   1.5   thorpej #include <dev/mii/mii.h>
     86   1.5   thorpej #include <dev/mii/miivar.h>
     87   1.5   thorpej #include <dev/mii/miidevs.h>
     88   1.5   thorpej 
     89   1.5   thorpej #include <dev/mii/nsphyreg.h>
     90   1.5   thorpej 
     91   1.2   thorpej int	nsphymatch __P((struct device *, struct cfdata *, void *));
     92   1.2   thorpej void	nsphyattach __P((struct device *, struct device *, void *));
     93   1.1    bouyer 
     94   1.1    bouyer struct cfattach nsphy_ca = {
     95  1.22   thorpej 	sizeof(struct mii_softc), nsphymatch, nsphyattach, mii_phy_detach,
     96  1.22   thorpej 	    mii_phy_activate
     97   1.1    bouyer };
     98   1.1    bouyer 
     99   1.5   thorpej int	nsphy_service __P((struct mii_softc *, struct mii_data *, int));
    100  1.15   thorpej void	nsphy_status __P((struct mii_softc *));
    101   1.5   thorpej 
    102  1.27   thorpej const struct mii_phy_funcs nsphy_funcs = {
    103  1.27   thorpej 	nsphy_service, nsphy_status, mii_phy_reset,
    104  1.27   thorpej };
    105  1.27   thorpej 
    106   1.1    bouyer int
    107   1.1    bouyer nsphymatch(parent, match, aux)
    108   1.2   thorpej 	struct device *parent;
    109   1.1    bouyer 	struct cfdata *match;
    110   1.1    bouyer 	void *aux;
    111   1.1    bouyer {
    112   1.5   thorpej 	struct mii_attach_args *ma = aux;
    113   1.5   thorpej 
    114  1.29  drochner 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxNATSEMI &&
    115  1.29  drochner 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxNATSEMI_DP83840)
    116  1.14   thorpej 		return (10);
    117   1.1    bouyer 
    118   1.5   thorpej 	return (0);
    119   1.1    bouyer }
    120   1.1    bouyer 
    121   1.1    bouyer void
    122   1.1    bouyer nsphyattach(parent, self, aux)
    123   1.2   thorpej 	struct device *parent, *self;
    124   1.1    bouyer 	void *aux;
    125   1.1    bouyer {
    126  1.15   thorpej 	struct mii_softc *sc = (struct mii_softc *)self;
    127   1.5   thorpej 	struct mii_attach_args *ma = aux;
    128   1.5   thorpej 	struct mii_data *mii = ma->mii_data;
    129   1.5   thorpej 
    130  1.29  drochner 	printf(": %s, rev. %d\n", MII_STR_xxNATSEMI_DP83840,
    131   1.5   thorpej 	    MII_REV(ma->mii_id2));
    132   1.5   thorpej 
    133  1.15   thorpej 	sc->mii_inst = mii->mii_instance;
    134  1.15   thorpej 	sc->mii_phy = ma->mii_phyno;
    135  1.27   thorpej 	sc->mii_funcs = &nsphy_funcs;
    136  1.15   thorpej 	sc->mii_pdata = mii;
    137  1.23   thorpej 	sc->mii_flags = mii->mii_flags;
    138  1.30   thorpej 	sc->mii_anegticks = 5;
    139  1.13   thorpej 
    140  1.27   thorpej 	PHY_RESET(sc);
    141   1.5   thorpej 
    142  1.15   thorpej 	sc->mii_capabilities =
    143  1.15   thorpej 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    144  1.15   thorpej 	printf("%s: ", sc->mii_dev.dv_xname);
    145  1.15   thorpej 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
    146   1.5   thorpej 		printf("no media present");
    147   1.5   thorpej 	else
    148  1.22   thorpej 		mii_phy_add_media(sc);
    149   1.5   thorpej 	printf("\n");
    150   1.5   thorpej }
    151   1.5   thorpej 
    152   1.5   thorpej int
    153  1.15   thorpej nsphy_service(sc, mii, cmd)
    154  1.15   thorpej 	struct mii_softc *sc;
    155   1.5   thorpej 	struct mii_data *mii;
    156   1.5   thorpej 	int cmd;
    157   1.5   thorpej {
    158  1.10   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    159   1.5   thorpej 	int reg;
    160  1.21   thorpej 
    161  1.21   thorpej 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
    162  1.21   thorpej 		return (ENXIO);
    163   1.5   thorpej 
    164   1.5   thorpej 	switch (cmd) {
    165   1.5   thorpej 	case MII_POLLSTAT:
    166   1.5   thorpej 		/*
    167   1.5   thorpej 		 * If we're not polling our PHY instance, just return.
    168   1.5   thorpej 		 */
    169  1.15   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    170   1.5   thorpej 			return (0);
    171   1.5   thorpej 		break;
    172   1.5   thorpej 
    173   1.5   thorpej 	case MII_MEDIACHG:
    174   1.5   thorpej 		/*
    175   1.5   thorpej 		 * If the media indicates a different PHY instance,
    176   1.5   thorpej 		 * isolate ourselves.
    177   1.5   thorpej 		 */
    178  1.15   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    179  1.15   thorpej 			reg = PHY_READ(sc, MII_BMCR);
    180  1.15   thorpej 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    181   1.5   thorpej 			return (0);
    182   1.5   thorpej 		}
    183   1.5   thorpej 
    184   1.5   thorpej 		/*
    185   1.5   thorpej 		 * If the interface is not up, don't do anything.
    186   1.5   thorpej 		 */
    187   1.5   thorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    188   1.5   thorpej 			break;
    189   1.5   thorpej 
    190  1.15   thorpej 		reg = PHY_READ(sc, MII_NSPHY_PCR);
    191   1.9   thorpej 
    192   1.5   thorpej 		/*
    193   1.5   thorpej 		 * Set up the PCR to use LED4 to indicate full-duplex
    194   1.5   thorpej 		 * in both 10baseT and 100baseTX modes.
    195   1.5   thorpej 		 */
    196   1.9   thorpej 		reg |= PCR_LED4MODE;
    197   1.9   thorpej 
    198   1.9   thorpej 		/*
    199   1.9   thorpej 		 * Make sure Carrier Intgrity Monitor function is
    200   1.9   thorpej 		 * disabled (normal for Node operation, but sometimes
    201   1.9   thorpej 		 * it's not set?!)
    202   1.9   thorpej 		 */
    203   1.9   thorpej 		reg |= PCR_CIMDIS;
    204   1.9   thorpej 
    205   1.9   thorpej 		/*
    206  1.18   thorpej 		 * Make sure "force link good" is set to normal mode.
    207  1.18   thorpej 		 * It's only intended for debugging.
    208   1.9   thorpej 		 */
    209  1.18   thorpej 		reg |= PCR_FLINK100;
    210   1.9   thorpej 
    211   1.9   thorpej 		/*
    212   1.9   thorpej 		 * Mystery bits which are supposedly `reserved',
    213   1.9   thorpej 		 * but we seem to need to set them when the PHY
    214  1.28    martin 		 * is connected to some interfaces:
    215  1.28    martin 		 *
    216  1.28    martin 		 * 0x0400 is needed for fxp
    217  1.28    martin 		 *        (Intel EtherExpress Pro 10+/100B, 82557 chip)
    218  1.28    martin 		 *        (nsphy with a DP83840 chip)
    219  1.28    martin 		 * 0x0100 may be needed for some other card
    220   1.9   thorpej 		 */
    221   1.9   thorpej 		reg |= 0x0100 | 0x0400;
    222   1.9   thorpej 
    223  1.15   thorpej 		PHY_WRITE(sc, MII_NSPHY_PCR, reg);
    224   1.5   thorpej 
    225  1.25   thorpej 		mii_phy_setmedia(sc);
    226   1.5   thorpej 		break;
    227   1.5   thorpej 
    228   1.5   thorpej 	case MII_TICK:
    229   1.5   thorpej 		/*
    230   1.5   thorpej 		 * If we're not currently selected, just return.
    231   1.5   thorpej 		 */
    232  1.15   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    233   1.5   thorpej 			return (0);
    234   1.5   thorpej 
    235  1.26   thorpej 		if (mii_phy_tick(sc) == EJUSTRETURN)
    236  1.17   thorpej 			return (0);
    237   1.5   thorpej 		break;
    238  1.20   thorpej 
    239  1.20   thorpej 	case MII_DOWN:
    240  1.20   thorpej 		mii_phy_down(sc);
    241  1.20   thorpej 		return (0);
    242   1.5   thorpej 	}
    243   1.5   thorpej 
    244   1.5   thorpej 	/* Update the media status. */
    245  1.24   thorpej 	mii_phy_status(sc);
    246   1.5   thorpej 
    247   1.5   thorpej 	/* Callback if something changed. */
    248  1.26   thorpej 	mii_phy_update(sc, cmd);
    249   1.5   thorpej 	return (0);
    250   1.5   thorpej }
    251   1.5   thorpej 
    252   1.5   thorpej void
    253   1.5   thorpej nsphy_status(sc)
    254  1.15   thorpej 	struct mii_softc *sc;
    255   1.5   thorpej {
    256  1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    257  1.19   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    258   1.8   thorpej 	int bmsr, bmcr, par, anlpar;
    259   1.5   thorpej 
    260   1.5   thorpej 	mii->mii_media_status = IFM_AVALID;
    261   1.5   thorpej 	mii->mii_media_active = IFM_ETHER;
    262   1.1    bouyer 
    263  1.15   thorpej 	bmsr = PHY_READ(sc, MII_BMSR) |
    264  1.15   thorpej 	    PHY_READ(sc, MII_BMSR);
    265   1.5   thorpej 	if (bmsr & BMSR_LINK)
    266   1.5   thorpej 		mii->mii_media_status |= IFM_ACTIVE;
    267   1.5   thorpej 
    268  1.15   thorpej 	bmcr = PHY_READ(sc, MII_BMCR);
    269   1.5   thorpej 	if (bmcr & BMCR_ISO) {
    270   1.5   thorpej 		mii->mii_media_active |= IFM_NONE;
    271   1.5   thorpej 		mii->mii_media_status = 0;
    272   1.1    bouyer 		return;
    273   1.1    bouyer 	}
    274   1.5   thorpej 
    275   1.5   thorpej 	if (bmcr & BMCR_LOOP)
    276   1.5   thorpej 		mii->mii_media_active |= IFM_LOOP;
    277   1.5   thorpej 
    278   1.5   thorpej 	if (bmcr & BMCR_AUTOEN) {
    279   1.5   thorpej 		/*
    280   1.5   thorpej 		 * The PAR status bits are only valid of autonegotiation
    281   1.5   thorpej 		 * has completed (or it's disabled).
    282   1.5   thorpej 		 */
    283   1.5   thorpej 		if ((bmsr & BMSR_ACOMP) == 0) {
    284   1.7   thorpej 			/* Erg, still trying, I guess... */
    285   1.7   thorpej 			mii->mii_media_active |= IFM_NONE;
    286   1.5   thorpej 			return;
    287   1.5   thorpej 		}
    288   1.8   thorpej 
    289   1.8   thorpej 		/*
    290   1.8   thorpej 		 * Argh.  The PAR doesn't seem to indicate duplex mode
    291   1.8   thorpej 		 * properly!  Determine media based on link partner's
    292   1.8   thorpej 		 * advertised capabilities.
    293   1.8   thorpej 		 */
    294  1.15   thorpej 		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
    295  1.15   thorpej 			anlpar = PHY_READ(sc, MII_ANAR) &
    296  1.15   thorpej 			    PHY_READ(sc, MII_ANLPAR);
    297   1.8   thorpej 			if (anlpar & ANLPAR_T4)
    298   1.8   thorpej 				mii->mii_media_active |= IFM_100_T4;
    299   1.8   thorpej 			else if (anlpar & ANLPAR_TX_FD)
    300   1.8   thorpej 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
    301   1.8   thorpej 			else if (anlpar & ANLPAR_TX)
    302   1.8   thorpej 				mii->mii_media_active |= IFM_100_TX;
    303   1.8   thorpej 			else if (anlpar & ANLPAR_10_FD)
    304   1.8   thorpej 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
    305   1.8   thorpej 			else if (anlpar & ANLPAR_10)
    306   1.8   thorpej 				mii->mii_media_active |= IFM_10_T;
    307   1.8   thorpej 			else
    308   1.8   thorpej 				mii->mii_media_active |= IFM_NONE;
    309   1.8   thorpej 			return;
    310   1.8   thorpej 		}
    311   1.8   thorpej 
    312   1.8   thorpej 		/*
    313   1.8   thorpej 		 * Link partner is not capable of autonegotiation.
    314   1.8   thorpej 		 * We will never be in full-duplex mode if this is
    315   1.8   thorpej 		 * the case, so reading the PAR is OK.
    316   1.8   thorpej 		 */
    317  1.15   thorpej 		par = PHY_READ(sc, MII_NSPHY_PAR);
    318   1.5   thorpej 		if (par & PAR_10)
    319   1.5   thorpej 			mii->mii_media_active |= IFM_10_T;
    320   1.5   thorpej 		else
    321   1.5   thorpej 			mii->mii_media_active |= IFM_100_TX;
    322   1.8   thorpej #if 0
    323   1.5   thorpej 		if (par & PAR_FDX)
    324   1.5   thorpej 			mii->mii_media_active |= IFM_FDX;
    325   1.8   thorpej #endif
    326  1.16   thorpej 	} else
    327  1.19   thorpej 		mii->mii_media_active = ife->ifm_media;
    328   1.1    bouyer }
    329