Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.4
      1 /*	$NetBSD: mii_physubr.c,v 1.4 1999/04/26 14:48:57 kleink 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 	int waitfor;
     63 {
     64 	int bmsr, i;
     65 
     66 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
     67 		PHY_WRITE(mii, MII_ANAR,
     68 		    BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
     69 		PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
     70 	}
     71 
     72 	if (waitfor) {
     73 		/* Wait 500ms for it to complete. */
     74 		for (i = 0; i < 500; i++) {
     75 			if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
     76 				return (0);
     77 			delay(1000);
     78 #if 0
     79 		if ((bmsr & BMSR_ACOMP) == 0)
     80 			printf("%s: autonegotiation failed to complete\n",
     81 			    mii->mii_dev.dv_xname);
     82 #endif
     83 		}
     84 
     85 		/*
     86 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
     87 		 * If that's set, a timeout is pending, and it will
     88 		 * clear the flag.
     89 		 */
     90 		return (EIO);
     91 	}
     92 
     93 	/*
     94 	 * Just let it finish asynchronously.  This is for the benefit of
     95 	 * the tick handler driving autonegotiation.  Don't want 500ms
     96 	 * delays all the time while the system is running!
     97 	 */
     98 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
     99 		mii->mii_flags |= MIIF_DOINGAUTO;
    100 		timeout(mii_phy_auto_timeout, mii, hz >> 1);
    101 	}
    102 	return (EJUSTRETURN);
    103 }
    104 
    105 void
    106 mii_phy_auto_timeout(arg)
    107 	void *arg;
    108 {
    109 	struct mii_softc *mii = arg;
    110 	int s, bmsr;
    111 
    112 	s = splnet();
    113 	mii->mii_flags &= ~MIIF_DOINGAUTO;
    114 	bmsr = PHY_READ(mii, MII_BMSR);
    115 #if 0
    116 	if ((bmsr & BMSR_ACOMP) == 0)
    117 		printf("%s: autonegotiation failed to complete\n",
    118 		    sc->sc_dev.dv_xname);
    119 #endif
    120 
    121 	/* Update the media status. */
    122 	(void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
    123 	splx(s);
    124 }
    125 
    126 void
    127 mii_phy_reset(mii)
    128 	struct mii_softc *mii;
    129 {
    130 	int reg, i;
    131 
    132 	if (mii->mii_flags & MIIF_NOISOLATE)
    133 		reg = BMCR_RESET;
    134 	else
    135 		reg = BMCR_RESET | BMCR_ISO;
    136 	PHY_WRITE(mii, MII_BMCR, reg);
    137 
    138 	/* Wait 100ms for it to complete. */
    139 	for (i = 0; i < 100; i++) {
    140 		reg = PHY_READ(mii, MII_BMCR);
    141 		if ((reg & BMCR_RESET) == 0)
    142 			break;
    143 		delay(1000);
    144 	}
    145 
    146 	if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
    147 		PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
    148 }
    149