Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.3
      1 /*	$NetBSD: mii_physubr.c,v 1.3 1999/04/23 04:24:32 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Subroutines common to all PHYs.
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/device.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/socket.h>
     49 #include <sys/errno.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_media.h>
     53 
     54 #include <dev/mii/mii.h>
     55 #include <dev/mii/miivar.h>
     56 
     57 void	mii_phy_auto_timeout __P((void *));
     58 
     59 int
     60 mii_phy_auto(mii, waitfor)
     61 	struct mii_softc *mii;
     62 {
     63 	int bmsr, i;
     64 
     65 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
     66 		PHY_WRITE(mii, MII_ANAR,
     67 		    BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
     68 		PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
     69 	}
     70 
     71 	if (waitfor) {
     72 		/* Wait 500ms for it to complete. */
     73 		for (i = 0; i < 500; i++) {
     74 			if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
     75 				return (0);
     76 			delay(1000);
     77 #if 0
     78 		if ((bmsr & BMSR_ACOMP) == 0)
     79 			printf("%s: autonegotiation failed to complete\n",
     80 			    mii->mii_dev.dv_xname);
     81 #endif
     82 		}
     83 
     84 		/*
     85 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
     86 		 * If that's set, a timeout is pending, and it will
     87 		 * clear the flag.
     88 		 */
     89 		return (EIO);
     90 	}
     91 
     92 	/*
     93 	 * Just let it finish asynchronously.  This is for the benefit of
     94 	 * the tick handler driving autonegotiation.  Don't want 500ms
     95 	 * delays all the time while the system is running!
     96 	 */
     97 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
     98 		mii->mii_flags |= MIIF_DOINGAUTO;
     99 		timeout(mii_phy_auto_timeout, mii, hz >> 1);
    100 	}
    101 	return (EJUSTRETURN);
    102 }
    103 
    104 void
    105 mii_phy_auto_timeout(arg)
    106 	void *arg;
    107 {
    108 	struct mii_softc *mii = arg;
    109 	int s, bmsr;
    110 
    111 	s = splnet();
    112 	mii->mii_flags &= ~MIIF_DOINGAUTO;
    113 	bmsr = PHY_READ(mii, MII_BMSR);
    114 #if 0
    115 	if ((bmsr & BMSR_ACOMP) == 0)
    116 		printf("%s: autonegotiation failed to complete\n",
    117 		    sc->sc_dev.dv_xname);
    118 #endif
    119 
    120 	/* Update the media status. */
    121 	(void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
    122 	splx(s);
    123 }
    124 
    125 void
    126 mii_phy_reset(mii)
    127 	struct mii_softc *mii;
    128 {
    129 	int reg, i;
    130 
    131 	if (mii->mii_flags & MIIF_NOISOLATE)
    132 		reg = BMCR_RESET;
    133 	else
    134 		reg = BMCR_RESET | BMCR_ISO;
    135 	PHY_WRITE(mii, MII_BMCR, reg);
    136 
    137 	/* Wait 100ms for it to complete. */
    138 	for (i = 0; i < 100; i++) {
    139 		reg = PHY_READ(mii, MII_BMCR);
    140 		if ((reg & BMCR_RESET) == 0)
    141 			break;
    142 		delay(1000);
    143 	}
    144 
    145 	if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
    146 		PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
    147 }
    148