Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.5.4.1
      1  1.5.4.1      fvdl /*	$NetBSD: mii_physubr.c,v 1.5.4.1 1999/11/15 00:40:55 fvdl 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.5.4.1      fvdl /*
     58  1.5.4.1      fvdl  * Media to register setting conversion table.  Order matters.
     59  1.5.4.1      fvdl  */
     60  1.5.4.1      fvdl const struct mii_media mii_media_table[] = {
     61  1.5.4.1      fvdl 	{ BMCR_ISO,		ANAR_CSMA },		/* None */
     62  1.5.4.1      fvdl 	{ 0,			ANAR_CSMA|ANAR_10 },	/* 10baseT */
     63  1.5.4.1      fvdl 	{ BMCR_FDX,		ANAR_CSMA|ANAR_10_FD },	/* 10baseT-FDX */
     64  1.5.4.1      fvdl 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4 },	/* 100baseT4 */
     65  1.5.4.1      fvdl 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX },	/* 100baseTX */
     66  1.5.4.1      fvdl 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD },	/* 100baseTX-FDX */
     67  1.5.4.1      fvdl };
     68  1.5.4.1      fvdl 
     69      1.3   thorpej void	mii_phy_auto_timeout __P((void *));
     70      1.3   thorpej 
     71  1.5.4.1      fvdl void
     72  1.5.4.1      fvdl mii_phy_setmedia(sc)
     73  1.5.4.1      fvdl 	struct mii_softc *sc;
     74  1.5.4.1      fvdl {
     75  1.5.4.1      fvdl 	struct mii_data *mii = sc->mii_pdata;
     76  1.5.4.1      fvdl 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
     77  1.5.4.1      fvdl 	int bmcr, anar;
     78  1.5.4.1      fvdl 
     79  1.5.4.1      fvdl 	/*
     80  1.5.4.1      fvdl 	 * Table index is stored in the media entry.
     81  1.5.4.1      fvdl 	 */
     82  1.5.4.1      fvdl 
     83  1.5.4.1      fvdl #ifdef DIAGNOSTIC
     84  1.5.4.1      fvdl 	if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
     85  1.5.4.1      fvdl 		panic("mii_phy_setmedia");
     86  1.5.4.1      fvdl #endif
     87  1.5.4.1      fvdl 
     88  1.5.4.1      fvdl 	anar = mii_media_table[ife->ifm_data].mm_anar;
     89  1.5.4.1      fvdl 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
     90  1.5.4.1      fvdl 
     91  1.5.4.1      fvdl 	if (ife->ifm_media & IFM_LOOP)
     92  1.5.4.1      fvdl 		bmcr |= BMCR_LOOP;
     93  1.5.4.1      fvdl 
     94  1.5.4.1      fvdl 	PHY_WRITE(sc, MII_ANAR, anar);
     95  1.5.4.1      fvdl 	PHY_WRITE(sc, MII_BMCR, bmcr);
     96  1.5.4.1      fvdl }
     97  1.5.4.1      fvdl 
     98      1.1   thorpej int
     99  1.5.4.1      fvdl mii_phy_auto(sc, waitfor)
    100  1.5.4.1      fvdl 	struct mii_softc *sc;
    101      1.4    kleink 	int waitfor;
    102      1.1   thorpej {
    103      1.1   thorpej 	int bmsr, i;
    104      1.1   thorpej 
    105  1.5.4.1      fvdl 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    106  1.5.4.1      fvdl 		PHY_WRITE(sc, MII_ANAR,
    107  1.5.4.1      fvdl 		    BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
    108  1.5.4.1      fvdl 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    109      1.3   thorpej 	}
    110      1.3   thorpej 
    111      1.3   thorpej 	if (waitfor) {
    112      1.3   thorpej 		/* Wait 500ms for it to complete. */
    113      1.3   thorpej 		for (i = 0; i < 500; i++) {
    114  1.5.4.1      fvdl 			if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    115      1.3   thorpej 				return (0);
    116      1.3   thorpej 			delay(1000);
    117      1.3   thorpej 		}
    118      1.3   thorpej 
    119      1.3   thorpej 		/*
    120      1.3   thorpej 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    121      1.3   thorpej 		 * If that's set, a timeout is pending, and it will
    122      1.3   thorpej 		 * clear the flag.
    123      1.3   thorpej 		 */
    124      1.3   thorpej 		return (EIO);
    125      1.1   thorpej 	}
    126      1.3   thorpej 
    127      1.3   thorpej 	/*
    128      1.3   thorpej 	 * Just let it finish asynchronously.  This is for the benefit of
    129      1.3   thorpej 	 * the tick handler driving autonegotiation.  Don't want 500ms
    130      1.3   thorpej 	 * delays all the time while the system is running!
    131      1.3   thorpej 	 */
    132  1.5.4.1      fvdl 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    133  1.5.4.1      fvdl 		sc->mii_flags |= MIIF_DOINGAUTO;
    134  1.5.4.1      fvdl 		timeout(mii_phy_auto_timeout, sc, hz >> 1);
    135      1.3   thorpej 	}
    136      1.3   thorpej 	return (EJUSTRETURN);
    137      1.3   thorpej }
    138      1.3   thorpej 
    139      1.3   thorpej void
    140      1.3   thorpej mii_phy_auto_timeout(arg)
    141      1.3   thorpej 	void *arg;
    142      1.3   thorpej {
    143  1.5.4.1      fvdl 	struct mii_softc *sc = arg;
    144      1.3   thorpej 	int s, bmsr;
    145      1.3   thorpej 
    146      1.3   thorpej 	s = splnet();
    147  1.5.4.1      fvdl 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    148  1.5.4.1      fvdl 	bmsr = PHY_READ(sc, MII_BMSR);
    149      1.3   thorpej 
    150      1.3   thorpej 	/* Update the media status. */
    151  1.5.4.1      fvdl 	(void) (*sc->mii_service)(sc, sc->mii_pdata, MII_POLLSTAT);
    152      1.3   thorpej 	splx(s);
    153      1.2   thorpej }
    154      1.2   thorpej 
    155      1.2   thorpej void
    156  1.5.4.1      fvdl mii_phy_reset(sc)
    157  1.5.4.1      fvdl 	struct mii_softc *sc;
    158      1.2   thorpej {
    159      1.2   thorpej 	int reg, i;
    160      1.2   thorpej 
    161  1.5.4.1      fvdl 	if (sc->mii_flags & MIIF_NOISOLATE)
    162      1.2   thorpej 		reg = BMCR_RESET;
    163      1.2   thorpej 	else
    164      1.2   thorpej 		reg = BMCR_RESET | BMCR_ISO;
    165  1.5.4.1      fvdl 	PHY_WRITE(sc, MII_BMCR, reg);
    166      1.2   thorpej 
    167      1.2   thorpej 	/* Wait 100ms for it to complete. */
    168      1.2   thorpej 	for (i = 0; i < 100; i++) {
    169  1.5.4.1      fvdl 		reg = PHY_READ(sc, MII_BMCR);
    170      1.2   thorpej 		if ((reg & BMCR_RESET) == 0)
    171      1.2   thorpej 			break;
    172      1.2   thorpej 		delay(1000);
    173      1.2   thorpej 	}
    174      1.2   thorpej 
    175  1.5.4.1      fvdl 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    176  1.5.4.1      fvdl 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    177      1.5  drochner }
    178      1.5  drochner 
    179  1.5.4.1      fvdl void
    180  1.5.4.1      fvdl mii_phy_down(sc)
    181  1.5.4.1      fvdl 	struct mii_softc *sc;
    182      1.5  drochner {
    183      1.5  drochner 
    184  1.5.4.1      fvdl 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    185  1.5.4.1      fvdl 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    186  1.5.4.1      fvdl 		untimeout(mii_phy_auto_timeout, sc);
    187      1.5  drochner 	}
    188      1.5  drochner }
    189      1.5  drochner 
    190      1.5  drochner /*
    191      1.5  drochner  * Initialize generic PHY media based on BMSR, called when a PHY is
    192      1.5  drochner  * attached.  We expect to be set up to print a comma-separated list
    193      1.5  drochner  * of media names.  Does not print a newline.
    194      1.5  drochner  */
    195      1.5  drochner void
    196  1.5.4.1      fvdl mii_add_media(sc)
    197  1.5.4.1      fvdl 	struct mii_softc *sc;
    198      1.5  drochner {
    199  1.5.4.1      fvdl 	struct mii_data *mii = sc->mii_pdata;
    200      1.5  drochner 	const char *sep = "";
    201      1.5  drochner 
    202      1.5  drochner #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    203      1.5  drochner #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    204      1.5  drochner 
    205  1.5.4.1      fvdl 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    206  1.5.4.1      fvdl 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    207  1.5.4.1      fvdl 		    MII_MEDIA_NONE);
    208  1.5.4.1      fvdl 
    209  1.5.4.1      fvdl 	if (sc->mii_capabilities & BMSR_10THDX) {
    210  1.5.4.1      fvdl 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    211  1.5.4.1      fvdl 		    MII_MEDIA_10_T);
    212  1.5.4.1      fvdl #if 0
    213  1.5.4.1      fvdl 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    214  1.5.4.1      fvdl 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
    215  1.5.4.1      fvdl 			    sc->mii_inst), MII_MEDIA_10_T);
    216  1.5.4.1      fvdl #endif
    217      1.5  drochner 		PRINT("10baseT");
    218      1.5  drochner 	}
    219  1.5.4.1      fvdl 	if (sc->mii_capabilities & BMSR_10TFDX) {
    220  1.5.4.1      fvdl 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    221  1.5.4.1      fvdl 		    MII_MEDIA_10_T_FDX);
    222      1.5  drochner 		PRINT("10baseT-FDX");
    223      1.5  drochner 	}
    224  1.5.4.1      fvdl 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    225  1.5.4.1      fvdl 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    226  1.5.4.1      fvdl 		    MII_MEDIA_100_TX);
    227  1.5.4.1      fvdl #if 0
    228  1.5.4.1      fvdl 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    229  1.5.4.1      fvdl 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
    230  1.5.4.1      fvdl 			    sc->mii_inst), MII_MEDIA_100_TX);
    231  1.5.4.1      fvdl #endif
    232      1.5  drochner 		PRINT("100baseTX");
    233      1.5  drochner 	}
    234  1.5.4.1      fvdl 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    235  1.5.4.1      fvdl 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    236  1.5.4.1      fvdl 		    MII_MEDIA_100_TX_FDX);
    237      1.5  drochner 		PRINT("100baseTX-FDX");
    238      1.5  drochner 	}
    239  1.5.4.1      fvdl 	if (sc->mii_capabilities & BMSR_100T4) {
    240  1.5.4.1      fvdl 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    241  1.5.4.1      fvdl 		    MII_MEDIA_100_T4);
    242  1.5.4.1      fvdl #if 0
    243  1.5.4.1      fvdl 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    244  1.5.4.1      fvdl 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, IFM_LOOP,
    245  1.5.4.1      fvdl 			    sc->mii_inst), MII_MEDIA_100_T4);
    246  1.5.4.1      fvdl #endif
    247      1.5  drochner 		PRINT("100baseT4");
    248      1.5  drochner 	}
    249  1.5.4.1      fvdl 	if (sc->mii_capabilities & BMSR_ANEG) {
    250  1.5.4.1      fvdl 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    251  1.5.4.1      fvdl 		    MII_NMEDIA);	/* intentionally invalid index */
    252      1.5  drochner 		PRINT("auto");
    253      1.5  drochner 	}
    254      1.5  drochner #undef ADD
    255      1.5  drochner #undef PRINT
    256      1.1   thorpej }
    257