Home | History | Annotate | Line # | Download | only in mii
amhphy.c revision 1.2
      1 /*	$NetBSD: amhphy.c,v 1.2 2001/08/25 18:04:01 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Driver for the 10BASE-T portion of the AMD Am79c901 PHY.
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/device.h>
     46 #include <sys/malloc.h>
     47 #include <sys/socket.h>
     48 #include <sys/errno.h>
     49 
     50 #include <net/if.h>
     51 #include <net/if_media.h>
     52 
     53 #include <dev/mii/mii.h>
     54 #include <dev/mii/miivar.h>
     55 #include <dev/mii/miidevs.h>
     56 
     57 #include <dev/mii/amhphyreg.h>
     58 
     59 int	amhphymatch(struct device *, struct cfdata *, void *);
     60 void	amhphyattach(struct device *, struct device *, void *);
     61 
     62 struct cfattach amhphy_ca = {
     63 	sizeof(struct mii_softc), amhphymatch, amhphyattach,
     64 	    mii_phy_detach, mii_phy_activate
     65 };
     66 
     67 int	amhphy_service(struct mii_softc *, struct mii_data *, int);
     68 void	amhphy_status(struct mii_softc *);
     69 
     70 const struct mii_phy_funcs amhphy_funcs = {
     71 	amhphy_service, amhphy_status, mii_phy_reset,
     72 };
     73 
     74 const struct mii_phydesc amhphys[] = {
     75 	{ MII_OUI_yyAMD,		MII_MODEL_yyAMD_79c901,
     76 	  MII_STR_yyAMD_79c901 },
     77 
     78 	{ 0,				0,
     79 	  NULL },
     80 };
     81 
     82 int
     83 amhphymatch(struct device *parent, struct cfdata *match, void *aux)
     84 {
     85 	struct mii_attach_args *ma = aux;
     86 
     87 	if (mii_phy_match(ma, amhphys) != NULL)
     88 		return (10);
     89 
     90 	return (0);
     91 }
     92 
     93 void
     94 amhphyattach(struct device *parent, struct device *self, void *aux)
     95 {
     96 	struct mii_softc *sc = (struct mii_softc *)self;
     97 	struct mii_attach_args *ma = aux;
     98 	struct mii_data *mii = ma->mii_data;
     99 	const struct mii_phydesc *mpd;
    100 
    101 	mpd = mii_phy_match(ma, amhphys);
    102 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    103 
    104 	sc->mii_inst = mii->mii_instance;
    105 	sc->mii_phy = ma->mii_phyno;
    106 	sc->mii_funcs = &amhphy_funcs;
    107 	sc->mii_pdata = mii;
    108 	sc->mii_flags = mii->mii_flags;
    109 	sc->mii_anegticks = 5;
    110 
    111 	PHY_RESET(sc);
    112 
    113 	sc->mii_capabilities =
    114 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    115 	printf("%s: ", sc->mii_dev.dv_xname);
    116 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
    117 		printf("no media present");
    118 	else
    119 		mii_phy_add_media(sc);
    120 	printf("\n");
    121 }
    122 
    123 int
    124 amhphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    125 {
    126 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    127 	int reg;
    128 
    129 	switch (cmd) {
    130 	case MII_POLLSTAT:
    131 		/*
    132 		 * If we're not polling our PHY instance, just return.
    133 		 */
    134 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    135 			return (0);
    136 		break;
    137 
    138 	case MII_MEDIACHG:
    139 		/*
    140 		 * If the media indicates a different PHY instance,
    141 		 * isolate ourselves.
    142 		 */
    143 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    144 			reg = PHY_READ(sc, MII_BMCR);
    145 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    146 			return (0);
    147 		}
    148 
    149 		/*
    150 		 * If the interface is not up, don't do anything.
    151 		 */
    152 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    153 			break;
    154 
    155 		mii_phy_setmedia(sc);
    156 		break;
    157 
    158 	case MII_TICK:
    159 		/*
    160 		 * If we're not currently selected, just return.
    161 		 */
    162 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    163 			return (0);
    164 
    165 		if (mii_phy_tick(sc) == EJUSTRETURN)
    166 			return (0);
    167 		break;
    168 
    169 	case MII_DOWN:
    170 		mii_phy_down(sc);
    171 		return (0);
    172 	}
    173 
    174 	/* Update the media status. */
    175 	mii_phy_status(sc);
    176 
    177 	/* Callback if something changed. */
    178 	mii_phy_update(sc, cmd);
    179 	return (0);
    180 }
    181 
    182 void
    183 amhphy_status(struct mii_softc *sc)
    184 {
    185 	struct mii_data *mii = sc->mii_pdata;
    186 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    187 	int bmsr, bmcr, ssr;
    188 
    189 	mii->mii_media_status = IFM_AVALID;
    190 	mii->mii_media_active = IFM_ETHER;
    191 
    192 	bmsr = PHY_READ(sc, MII_BMSR);
    193 	ssr = PHY_READ(sc, MII_AMHPHY_SSR);
    194 
    195 	if (ssr & SSR_LS)
    196 		mii->mii_media_status |= IFM_ACTIVE;
    197 
    198 	bmcr = PHY_READ(sc, MII_BMCR);
    199 	if (bmcr & BMCR_ISO) {
    200 		mii->mii_media_active |= IFM_NONE;
    201 		mii->mii_media_status = 0;
    202 		return;
    203 	}
    204 
    205 	if (bmcr & BMCR_LOOP)
    206 		mii->mii_media_active |= IFM_LOOP;
    207 
    208 	if (bmcr & BMCR_AUTOEN) {
    209 		/*
    210 		 * The media status bits are only valid of autonegotiation
    211 		 * has completed (or it's disabled).
    212 		 */
    213 		if ((bmsr & BMSR_ACOMP) == 0) {
    214 			/* Erg, still trying, I guess... */
    215 			mii->mii_media_active |= IFM_NONE;
    216 			return;
    217 		}
    218 
    219 		if (ssr & SSR_S) {
    220 			/*
    221 			 * This should't really ever happen, since it's
    222 			 * a 10BASE-T only PHY.  But the bit exists,
    223 			 * according to the documentation, so we pay
    224 			 * attention to it.
    225 			 */
    226 			mii->mii_media_active |= IFM_100_TX;
    227 		} else
    228 			mii->mii_media_active |= IFM_10_T;
    229 		if (ssr & SSR_FD)
    230 			mii->mii_media_active |= IFM_FDX;
    231 	} else
    232 		mii->mii_media_active = ife->ifm_media;
    233 }
    234