gentbi.c revision 1.2.2.6       1  1.2.2.6  jdolecek /*	$NetBSD: gentbi.c,v 1.2.2.6 2002/09/06 08:44:59 jdolecek Exp $	*/
      2  1.2.2.2     lukem 
      3  1.2.2.2     lukem /*-
      4  1.2.2.2     lukem  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
      5  1.2.2.2     lukem  * All rights reserved.
      6  1.2.2.2     lukem  *
      7  1.2.2.2     lukem  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.2.2     lukem  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.2.2.2     lukem  * NASA Ames Research Center.
     10  1.2.2.2     lukem  *
     11  1.2.2.2     lukem  * Redistribution and use in source and binary forms, with or without
     12  1.2.2.2     lukem  * modification, are permitted provided that the following conditions
     13  1.2.2.2     lukem  * are met:
     14  1.2.2.2     lukem  * 1. Redistributions of source code must retain the above copyright
     15  1.2.2.2     lukem  *    notice, this list of conditions and the following disclaimer.
     16  1.2.2.2     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.2.2.2     lukem  *    notice, this list of conditions and the following disclaimer in the
     18  1.2.2.2     lukem  *    documentation and/or other materials provided with the distribution.
     19  1.2.2.2     lukem  * 3. All advertising materials mentioning features or use of this software
     20  1.2.2.2     lukem  *    must display the following acknowledgement:
     21  1.2.2.2     lukem  *	This product includes software developed by the NetBSD
     22  1.2.2.2     lukem  *	Foundation, Inc. and its contributors.
     23  1.2.2.2     lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.2.2.2     lukem  *    contributors may be used to endorse or promote products derived
     25  1.2.2.2     lukem  *    from this software without specific prior written permission.
     26  1.2.2.2     lukem  *
     27  1.2.2.2     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.2.2.2     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.2.2.2     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.2.2.2     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.2.2.2     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.2.2.2     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.2.2.2     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.2.2.2     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.2.2.2     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.2.2.2     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.2.2.2     lukem  * POSSIBILITY OF SUCH DAMAGE.
     38  1.2.2.2     lukem  */
     39  1.2.2.2     lukem 
     40  1.2.2.2     lukem /*
     41  1.2.2.2     lukem  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     42  1.2.2.2     lukem  *
     43  1.2.2.2     lukem  * Redistribution and use in source and binary forms, with or without
     44  1.2.2.2     lukem  * modification, are permitted provided that the following conditions
     45  1.2.2.2     lukem  * are met:
     46  1.2.2.2     lukem  * 1. Redistributions of source code must retain the above copyright
     47  1.2.2.2     lukem  *    notice, this list of conditions and the following disclaimer.
     48  1.2.2.2     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.2.2.2     lukem  *    notice, this list of conditions and the following disclaimer in the
     50  1.2.2.2     lukem  *    documentation and/or other materials provided with the distribution.
     51  1.2.2.2     lukem  * 3. All advertising materials mentioning features or use of this software
     52  1.2.2.2     lukem  *    must display the following acknowledgement:
     53  1.2.2.2     lukem  *	This product includes software developed by Manuel Bouyer.
     54  1.2.2.2     lukem  * 4. The name of the author may not be used to endorse or promote products
     55  1.2.2.2     lukem  *    derived from this software without specific prior written permission.
     56  1.2.2.2     lukem  *
     57  1.2.2.2     lukem  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  1.2.2.2     lukem  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  1.2.2.2     lukem  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  1.2.2.2     lukem  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  1.2.2.2     lukem  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  1.2.2.2     lukem  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  1.2.2.2     lukem  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  1.2.2.2     lukem  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  1.2.2.2     lukem  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  1.2.2.2     lukem  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  1.2.2.2     lukem  */
     68  1.2.2.2     lukem 
     69  1.2.2.2     lukem /*
     70  1.2.2.2     lukem  * Driver for generic ten-bit (1000BASE-SX) interfaces, built in to
     71  1.2.2.2     lukem  * many Gigabit Ethernet chips.
     72  1.2.2.2     lukem  *
     73  1.2.2.2     lukem  * All we have to do here is correctly report speed and duplex.
     74  1.2.2.2     lukem  */
     75  1.2.2.2     lukem 
     76  1.2.2.4   thorpej #include <sys/cdefs.h>
     77  1.2.2.6  jdolecek __KERNEL_RCSID(0, "$NetBSD: gentbi.c,v 1.2.2.6 2002/09/06 08:44:59 jdolecek Exp $");
     78  1.2.2.4   thorpej 
     79  1.2.2.2     lukem #include <sys/param.h>
     80  1.2.2.2     lukem #include <sys/systm.h>
     81  1.2.2.2     lukem #include <sys/kernel.h>
     82  1.2.2.2     lukem #include <sys/device.h>
     83  1.2.2.2     lukem #include <sys/socket.h>
     84  1.2.2.2     lukem #include <sys/errno.h>
     85  1.2.2.2     lukem 
     86  1.2.2.2     lukem #include <net/if.h>
     87  1.2.2.2     lukem #include <net/if_media.h>
     88  1.2.2.2     lukem 
     89  1.2.2.2     lukem #include <dev/mii/mii.h>
     90  1.2.2.2     lukem #include <dev/mii/miivar.h>
     91  1.2.2.2     lukem #include <dev/mii/miidevs.h>
     92  1.2.2.2     lukem 
     93  1.2.2.3   thorpej int	gentbimatch(struct device *, struct cfdata *, void *);
     94  1.2.2.3   thorpej void	gentbiattach(struct device *, struct device *, void *);
     95  1.2.2.2     lukem 
     96  1.2.2.2     lukem struct cfattach gentbi_ca = {
     97  1.2.2.2     lukem 	sizeof(struct mii_softc), gentbimatch, gentbiattach,
     98  1.2.2.2     lukem 	    mii_phy_detach, mii_phy_activate
     99  1.2.2.2     lukem };
    100  1.2.2.2     lukem 
    101  1.2.2.3   thorpej int	gentbi_service(struct mii_softc *, struct mii_data *, int);
    102  1.2.2.3   thorpej void	gentbi_status(struct mii_softc *);
    103  1.2.2.2     lukem 
    104  1.2.2.2     lukem const struct mii_phy_funcs gentbi_funcs = {
    105  1.2.2.2     lukem 	gentbi_service, gentbi_status, mii_phy_reset,
    106  1.2.2.2     lukem };
    107  1.2.2.2     lukem 
    108  1.2.2.2     lukem int
    109  1.2.2.3   thorpej gentbimatch(struct device *parent, struct cfdata *match, void *aux)
    110  1.2.2.2     lukem {
    111  1.2.2.2     lukem 	struct mii_attach_args *ma = aux;
    112  1.2.2.2     lukem 	struct mii_data *mii = ma->mii_data;
    113  1.2.2.2     lukem 	int bmsr, extsr;
    114  1.2.2.2     lukem 
    115  1.2.2.2     lukem 	/*
    116  1.2.2.2     lukem 	 * We match as a generic TBI if:
    117  1.2.2.2     lukem 	 *
    118  1.2.2.2     lukem 	 *	- There is no media in the BMSR.
    119  1.2.2.2     lukem 	 *	- EXTSR has only 1000X.
    120  1.2.2.2     lukem 	 */
    121  1.2.2.2     lukem 	bmsr = (*mii->mii_readreg)(parent, ma->mii_phyno, MII_BMSR);
    122  1.2.2.2     lukem 	if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0)
    123  1.2.2.2     lukem 		return (0);
    124  1.2.2.2     lukem 
    125  1.2.2.2     lukem 	extsr = (*mii->mii_readreg)(parent, ma->mii_phyno, MII_EXTSR);
    126  1.2.2.2     lukem 	if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX))
    127  1.2.2.2     lukem 		return (0);
    128  1.2.2.2     lukem 
    129  1.2.2.2     lukem 	if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) {
    130  1.2.2.2     lukem 		/*
    131  1.2.2.2     lukem 		 * We think this is a generic TBI.  Return a match
    132  1.2.2.2     lukem 		 * priority higher than ukphy, but lower than what
    133  1.2.2.2     lukem 		 * specific drivers will return.
    134  1.2.2.2     lukem 		 */
    135  1.2.2.2     lukem 		return (2);
    136  1.2.2.2     lukem 	}
    137  1.2.2.2     lukem 
    138  1.2.2.2     lukem 	return (0);
    139  1.2.2.2     lukem }
    140  1.2.2.2     lukem 
    141  1.2.2.2     lukem void
    142  1.2.2.3   thorpej gentbiattach(struct device *parent, struct device *self, void *aux)
    143  1.2.2.2     lukem {
    144  1.2.2.2     lukem 	struct mii_softc *sc = (struct mii_softc *)self;
    145  1.2.2.2     lukem 	struct mii_attach_args *ma = aux;
    146  1.2.2.2     lukem 	struct mii_data *mii = ma->mii_data;
    147  1.2.2.2     lukem 
    148  1.2.2.2     lukem 	printf(": Generic ten-bit interface, rev. %d\n",
    149  1.2.2.2     lukem 	    MII_REV(ma->mii_id2));
    150  1.2.2.2     lukem 
    151  1.2.2.2     lukem 	sc->mii_inst = mii->mii_instance;
    152  1.2.2.2     lukem 	sc->mii_phy = ma->mii_phyno;
    153  1.2.2.2     lukem 	sc->mii_funcs = &gentbi_funcs;
    154  1.2.2.2     lukem 	sc->mii_pdata = mii;
    155  1.2.2.5  jdolecek 	sc->mii_flags = ma->mii_flags;
    156  1.2.2.2     lukem 	sc->mii_anegticks = 5;
    157  1.2.2.2     lukem 
    158  1.2.2.2     lukem 	PHY_RESET(sc);
    159  1.2.2.2     lukem 
    160  1.2.2.2     lukem 	/*
    161  1.2.2.2     lukem 	 * Mask out all media in the BMSR.  We only are really interested
    162  1.2.2.2     lukem 	 * in "auto".
    163  1.2.2.2     lukem 	 */
    164  1.2.2.2     lukem 	sc->mii_capabilities =
    165  1.2.2.2     lukem 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask & ~BMSR_MEDIAMASK;
    166  1.2.2.2     lukem 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    167  1.2.2.2     lukem 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
    168  1.2.2.2     lukem 
    169  1.2.2.2     lukem 	printf("%s: ", sc->mii_dev.dv_xname);
    170  1.2.2.2     lukem 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    171  1.2.2.2     lukem 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    172  1.2.2.2     lukem 		printf("no media present");
    173  1.2.2.2     lukem 	else
    174  1.2.2.2     lukem 		mii_phy_add_media(sc);
    175  1.2.2.2     lukem 	printf("\n");
    176  1.2.2.2     lukem }
    177  1.2.2.2     lukem 
    178  1.2.2.2     lukem int
    179  1.2.2.3   thorpej gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    180  1.2.2.2     lukem {
    181  1.2.2.2     lukem 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    182  1.2.2.2     lukem 	int reg;
    183  1.2.2.2     lukem 
    184  1.2.2.2     lukem 	switch (cmd) {
    185  1.2.2.2     lukem 	case MII_POLLSTAT:
    186  1.2.2.2     lukem 		/*
    187  1.2.2.2     lukem 		 * If we're not polling our PHY instance, just return.
    188  1.2.2.2     lukem 		 */
    189  1.2.2.2     lukem 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    190  1.2.2.2     lukem 			return (0);
    191  1.2.2.2     lukem 		break;
    192  1.2.2.2     lukem 
    193  1.2.2.2     lukem 	case MII_MEDIACHG:
    194  1.2.2.2     lukem 		/*
    195  1.2.2.2     lukem 		 * If the media indicates a different PHY instance,
    196  1.2.2.2     lukem 		 * isolate ourselves.
    197  1.2.2.2     lukem 		 */
    198  1.2.2.2     lukem 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    199  1.2.2.2     lukem 			reg = PHY_READ(sc, MII_BMCR);
    200  1.2.2.2     lukem 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    201  1.2.2.2     lukem 			return (0);
    202  1.2.2.2     lukem 		}
    203  1.2.2.2     lukem 
    204  1.2.2.2     lukem 		/*
    205  1.2.2.2     lukem 		 * If the interface is not up, don't do anything.
    206  1.2.2.2     lukem 		 */
    207  1.2.2.2     lukem 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    208  1.2.2.2     lukem 			break;
    209  1.2.2.2     lukem 
    210  1.2.2.2     lukem 		mii_phy_setmedia(sc);
    211  1.2.2.2     lukem 		break;
    212  1.2.2.2     lukem 
    213  1.2.2.2     lukem 	case MII_TICK:
    214  1.2.2.2     lukem 		/*
    215  1.2.2.2     lukem 		 * If we're not currently selected, just return.
    216  1.2.2.2     lukem 		 */
    217  1.2.2.2     lukem 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    218  1.2.2.2     lukem 			return (0);
    219  1.2.2.2     lukem 
    220  1.2.2.2     lukem 		if (mii_phy_tick(sc) == EJUSTRETURN)
    221  1.2.2.2     lukem 			return (0);
    222  1.2.2.2     lukem 		break;
    223  1.2.2.2     lukem 
    224  1.2.2.2     lukem 	case MII_DOWN:
    225  1.2.2.2     lukem 		mii_phy_down(sc);
    226  1.2.2.2     lukem 		return (0);
    227  1.2.2.2     lukem 	}
    228  1.2.2.2     lukem 
    229  1.2.2.2     lukem 	/* Update the media status. */
    230  1.2.2.2     lukem 	mii_phy_status(sc);
    231  1.2.2.2     lukem 
    232  1.2.2.2     lukem 	/* Callback if something changed. */
    233  1.2.2.2     lukem 	mii_phy_update(sc, cmd);
    234  1.2.2.2     lukem 	return (0);
    235  1.2.2.2     lukem }
    236  1.2.2.2     lukem 
    237  1.2.2.2     lukem void
    238  1.2.2.3   thorpej gentbi_status(struct mii_softc *sc)
    239  1.2.2.2     lukem {
    240  1.2.2.2     lukem 	struct mii_data *mii = sc->mii_pdata;
    241  1.2.2.2     lukem 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    242  1.2.2.2     lukem 	int bmsr, bmcr, anlpar;
    243  1.2.2.2     lukem 
    244  1.2.2.2     lukem 	mii->mii_media_status = IFM_AVALID;
    245  1.2.2.2     lukem 	mii->mii_media_active = IFM_ETHER;
    246  1.2.2.2     lukem 
    247  1.2.2.2     lukem 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    248  1.2.2.2     lukem 
    249  1.2.2.2     lukem 	if (bmsr & BMSR_LINK)
    250  1.2.2.2     lukem 		mii->mii_media_status |= IFM_ACTIVE;
    251  1.2.2.2     lukem 
    252  1.2.2.2     lukem 	bmcr = PHY_READ(sc, MII_BMCR);
    253  1.2.2.2     lukem 	if (bmcr & BMCR_ISO) {
    254  1.2.2.2     lukem 		mii->mii_media_active |= IFM_NONE;
    255  1.2.2.2     lukem 		mii->mii_media_status = 0;
    256  1.2.2.2     lukem 		return;
    257  1.2.2.2     lukem 	}
    258  1.2.2.2     lukem 
    259  1.2.2.2     lukem 	if (bmcr & BMCR_LOOP)
    260  1.2.2.2     lukem 		mii->mii_media_active |= IFM_LOOP;
    261  1.2.2.2     lukem 
    262  1.2.2.2     lukem 	if (bmcr & BMCR_AUTOEN) {
    263  1.2.2.2     lukem 		/*
    264  1.2.2.2     lukem 		 * The media status bits are only valid of autonegotiation
    265  1.2.2.2     lukem 		 * has completed (or it's disabled).
    266  1.2.2.2     lukem 		 */
    267  1.2.2.2     lukem 		if ((bmsr & BMSR_ACOMP) == 0) {
    268  1.2.2.2     lukem 			/* Erg, still trying, I guess... */
    269  1.2.2.2     lukem 			mii->mii_media_active |= IFM_NONE;
    270  1.2.2.2     lukem 			return;
    271  1.2.2.2     lukem 		}
    272  1.2.2.2     lukem 
    273  1.2.2.2     lukem 		/*
    274  1.2.2.2     lukem 		 * The media is always 1000baseSX.  Check the ANLPAR to
    275  1.2.2.2     lukem 		 * see if we're doing full-duplex.
    276  1.2.2.2     lukem 		 */
    277  1.2.2.2     lukem 		mii->mii_media_active |= IFM_1000_SX;
    278  1.2.2.2     lukem 
    279  1.2.2.2     lukem 		anlpar = PHY_READ(sc, MII_ANLPAR);
    280  1.2.2.2     lukem 		if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 &&
    281  1.2.2.2     lukem 		    (anlpar & ANLPAR_X_FD) != 0)
    282  1.2.2.2     lukem 			mii->mii_media_active |= IFM_FDX;
    283  1.2.2.2     lukem 	} else
    284  1.2.2.2     lukem 		mii->mii_media_active = ife->ifm_media;
    285  1.2.2.2     lukem }
    286