Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.5
      1  1.5  drochner /*	$NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*-
      4  1.3   thorpej  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
      5  1.1   thorpej  * All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1   thorpej  * NASA Ames Research Center.
     10  1.1   thorpej  *
     11  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.1   thorpej  * modification, are permitted provided that the following conditions
     13  1.1   thorpej  * are met:
     14  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.1   thorpej  *    must display the following acknowledgement:
     21  1.1   thorpej  *	This product includes software developed by the NetBSD
     22  1.1   thorpej  *	Foundation, Inc. and its contributors.
     23  1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.1   thorpej  *    from this software without specific prior written permission.
     26  1.1   thorpej  *
     27  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1   thorpej  */
     39  1.1   thorpej 
     40  1.1   thorpej /*
     41  1.1   thorpej  * Subroutines common to all PHYs.
     42  1.1   thorpej  */
     43  1.1   thorpej 
     44  1.1   thorpej #include <sys/param.h>
     45  1.1   thorpej #include <sys/device.h>
     46  1.1   thorpej #include <sys/systm.h>
     47  1.3   thorpej #include <sys/kernel.h>
     48  1.1   thorpej #include <sys/socket.h>
     49  1.3   thorpej #include <sys/errno.h>
     50  1.1   thorpej 
     51  1.1   thorpej #include <net/if.h>
     52  1.1   thorpej #include <net/if_media.h>
     53  1.1   thorpej 
     54  1.1   thorpej #include <dev/mii/mii.h>
     55  1.1   thorpej #include <dev/mii/miivar.h>
     56  1.1   thorpej 
     57  1.3   thorpej void	mii_phy_auto_timeout __P((void *));
     58  1.3   thorpej 
     59  1.1   thorpej int
     60  1.3   thorpej mii_phy_auto(mii, waitfor)
     61  1.1   thorpej 	struct mii_softc *mii;
     62  1.4    kleink 	int waitfor;
     63  1.1   thorpej {
     64  1.1   thorpej 	int bmsr, i;
     65  1.1   thorpej 
     66  1.3   thorpej 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
     67  1.3   thorpej 		PHY_WRITE(mii, MII_ANAR,
     68  1.3   thorpej 		    BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
     69  1.3   thorpej 		PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
     70  1.3   thorpej 	}
     71  1.3   thorpej 
     72  1.3   thorpej 	if (waitfor) {
     73  1.3   thorpej 		/* Wait 500ms for it to complete. */
     74  1.3   thorpej 		for (i = 0; i < 500; i++) {
     75  1.3   thorpej 			if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
     76  1.3   thorpej 				return (0);
     77  1.3   thorpej 			delay(1000);
     78  1.3   thorpej #if 0
     79  1.3   thorpej 		if ((bmsr & BMSR_ACOMP) == 0)
     80  1.3   thorpej 			printf("%s: autonegotiation failed to complete\n",
     81  1.3   thorpej 			    mii->mii_dev.dv_xname);
     82  1.3   thorpej #endif
     83  1.3   thorpej 		}
     84  1.3   thorpej 
     85  1.3   thorpej 		/*
     86  1.3   thorpej 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
     87  1.3   thorpej 		 * If that's set, a timeout is pending, and it will
     88  1.3   thorpej 		 * clear the flag.
     89  1.3   thorpej 		 */
     90  1.3   thorpej 		return (EIO);
     91  1.1   thorpej 	}
     92  1.3   thorpej 
     93  1.3   thorpej 	/*
     94  1.3   thorpej 	 * Just let it finish asynchronously.  This is for the benefit of
     95  1.3   thorpej 	 * the tick handler driving autonegotiation.  Don't want 500ms
     96  1.3   thorpej 	 * delays all the time while the system is running!
     97  1.3   thorpej 	 */
     98  1.3   thorpej 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
     99  1.3   thorpej 		mii->mii_flags |= MIIF_DOINGAUTO;
    100  1.3   thorpej 		timeout(mii_phy_auto_timeout, mii, hz >> 1);
    101  1.3   thorpej 	}
    102  1.3   thorpej 	return (EJUSTRETURN);
    103  1.3   thorpej }
    104  1.3   thorpej 
    105  1.3   thorpej void
    106  1.3   thorpej mii_phy_auto_timeout(arg)
    107  1.3   thorpej 	void *arg;
    108  1.3   thorpej {
    109  1.3   thorpej 	struct mii_softc *mii = arg;
    110  1.3   thorpej 	int s, bmsr;
    111  1.3   thorpej 
    112  1.3   thorpej 	s = splnet();
    113  1.3   thorpej 	mii->mii_flags &= ~MIIF_DOINGAUTO;
    114  1.3   thorpej 	bmsr = PHY_READ(mii, MII_BMSR);
    115  1.1   thorpej #if 0
    116  1.1   thorpej 	if ((bmsr & BMSR_ACOMP) == 0)
    117  1.1   thorpej 		printf("%s: autonegotiation failed to complete\n",
    118  1.3   thorpej 		    sc->sc_dev.dv_xname);
    119  1.1   thorpej #endif
    120  1.3   thorpej 
    121  1.3   thorpej 	/* Update the media status. */
    122  1.3   thorpej 	(void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
    123  1.3   thorpej 	splx(s);
    124  1.2   thorpej }
    125  1.2   thorpej 
    126  1.2   thorpej void
    127  1.2   thorpej mii_phy_reset(mii)
    128  1.2   thorpej 	struct mii_softc *mii;
    129  1.2   thorpej {
    130  1.2   thorpej 	int reg, i;
    131  1.2   thorpej 
    132  1.2   thorpej 	if (mii->mii_flags & MIIF_NOISOLATE)
    133  1.2   thorpej 		reg = BMCR_RESET;
    134  1.2   thorpej 	else
    135  1.2   thorpej 		reg = BMCR_RESET | BMCR_ISO;
    136  1.2   thorpej 	PHY_WRITE(mii, MII_BMCR, reg);
    137  1.2   thorpej 
    138  1.2   thorpej 	/* Wait 100ms for it to complete. */
    139  1.2   thorpej 	for (i = 0; i < 100; i++) {
    140  1.2   thorpej 		reg = PHY_READ(mii, MII_BMCR);
    141  1.2   thorpej 		if ((reg & BMCR_RESET) == 0)
    142  1.2   thorpej 			break;
    143  1.2   thorpej 		delay(1000);
    144  1.2   thorpej 	}
    145  1.2   thorpej 
    146  1.2   thorpej 	if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
    147  1.2   thorpej 		PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
    148  1.5  drochner }
    149  1.5  drochner 
    150  1.5  drochner /*
    151  1.5  drochner  * Given an ifmedia word, return the corresponding ANAR value.
    152  1.5  drochner  */
    153  1.5  drochner int
    154  1.5  drochner mii_anar(media)
    155  1.5  drochner 	int media;
    156  1.5  drochner {
    157  1.5  drochner 	int rv;
    158  1.5  drochner 
    159  1.5  drochner 	switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
    160  1.5  drochner 	case IFM_ETHER|IFM_10_T:
    161  1.5  drochner 		rv = ANAR_10|ANAR_CSMA;
    162  1.5  drochner 		break;
    163  1.5  drochner 	case IFM_ETHER|IFM_10_T|IFM_FDX:
    164  1.5  drochner 		rv = ANAR_10_FD|ANAR_CSMA;
    165  1.5  drochner 		break;
    166  1.5  drochner 	case IFM_ETHER|IFM_100_TX:
    167  1.5  drochner 		rv = ANAR_TX|ANAR_CSMA;
    168  1.5  drochner 		break;
    169  1.5  drochner 	case IFM_ETHER|IFM_100_TX|IFM_FDX:
    170  1.5  drochner 		rv = ANAR_TX_FD|ANAR_CSMA;
    171  1.5  drochner 		break;
    172  1.5  drochner 	case IFM_ETHER|IFM_100_T4:
    173  1.5  drochner 		rv = ANAR_T4|ANAR_CSMA;
    174  1.5  drochner 		break;
    175  1.5  drochner 	default:
    176  1.5  drochner 		rv = 0;
    177  1.5  drochner 		break;
    178  1.5  drochner 	}
    179  1.5  drochner 
    180  1.5  drochner 	return (rv);
    181  1.5  drochner }
    182  1.5  drochner 
    183  1.5  drochner /*
    184  1.5  drochner  * Given a BMCR value, return the corresponding ifmedia word.
    185  1.5  drochner  */
    186  1.5  drochner int
    187  1.5  drochner mii_media_from_bmcr(bmcr)
    188  1.5  drochner 	int bmcr;
    189  1.5  drochner {
    190  1.5  drochner 	int rv = IFM_ETHER;
    191  1.5  drochner 
    192  1.5  drochner 	if (bmcr & BMCR_S100)
    193  1.5  drochner 		rv |= IFM_100_TX;
    194  1.5  drochner 	else
    195  1.5  drochner 		rv |= IFM_10_T;
    196  1.5  drochner 	if (bmcr & BMCR_FDX)
    197  1.5  drochner 		rv |= IFM_FDX;
    198  1.5  drochner 
    199  1.5  drochner 	return (rv);
    200  1.5  drochner }
    201  1.5  drochner 
    202  1.5  drochner /*
    203  1.5  drochner  * Initialize generic PHY media based on BMSR, called when a PHY is
    204  1.5  drochner  * attached.  We expect to be set up to print a comma-separated list
    205  1.5  drochner  * of media names.  Does not print a newline.
    206  1.5  drochner  */
    207  1.5  drochner void
    208  1.5  drochner mii_add_media(mii, bmsr, instance)
    209  1.5  drochner 	struct mii_data *mii;
    210  1.5  drochner 	int bmsr, instance;
    211  1.5  drochner {
    212  1.5  drochner 	const char *sep = "";
    213  1.5  drochner 
    214  1.5  drochner #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    215  1.5  drochner #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    216  1.5  drochner 
    217  1.5  drochner 	if (bmsr & BMSR_10THDX) {
    218  1.5  drochner 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
    219  1.5  drochner 		PRINT("10baseT");
    220  1.5  drochner 	}
    221  1.5  drochner 	if (bmsr & BMSR_10TFDX) {
    222  1.5  drochner 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
    223  1.5  drochner 		    BMCR_FDX);
    224  1.5  drochner 		PRINT("10baseT-FDX");
    225  1.5  drochner 	}
    226  1.5  drochner 	if (bmsr & BMSR_100TXHDX) {
    227  1.5  drochner 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
    228  1.5  drochner 		    BMCR_S100);
    229  1.5  drochner 		PRINT("100baseTX");
    230  1.5  drochner 	}
    231  1.5  drochner 	if (bmsr & BMSR_100TXFDX) {
    232  1.5  drochner 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
    233  1.5  drochner 		    BMCR_S100|BMCR_FDX);
    234  1.5  drochner 		PRINT("100baseTX-FDX");
    235  1.5  drochner 	}
    236  1.5  drochner 	if (bmsr & BMSR_100T4) {
    237  1.5  drochner 		/*
    238  1.5  drochner 		 * XXX How do you enable 100baseT4?  I assume we set
    239  1.5  drochner 		 * XXX BMCR_S100 and then assume the PHYs will take
    240  1.5  drochner 		 * XXX watever action is necessary to switch themselves
    241  1.5  drochner 		 * XXX into T4 mode.
    242  1.5  drochner 		 */
    243  1.5  drochner 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
    244  1.5  drochner 		    BMCR_S100);
    245  1.5  drochner 		PRINT("100baseT4");
    246  1.5  drochner 	}
    247  1.5  drochner 	if (bmsr & BMSR_ANEG) {
    248  1.5  drochner 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
    249  1.5  drochner 		    BMCR_AUTOEN);
    250  1.5  drochner 		PRINT("auto");
    251  1.5  drochner 	}
    252  1.5  drochner #undef ADD
    253  1.5  drochner #undef PRINT
    254  1.1   thorpej }
    255