Home | History | Annotate | Line # | Download | only in mii
acphy.c revision 1.1
      1 /*	$NetBSD: acphy.c,v 1.1 2001/08/24 17:54:33 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 Altima AC101 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/acphyreg.h>
     58 
     59 int	acphymatch(struct device *, struct cfdata *, void *);
     60 void	acphyattach(struct device *, struct device *, void *);
     61 
     62 struct cfattach acphy_ca = {
     63 	sizeof(struct mii_softc), acphymatch, acphyattach,
     64 	    mii_phy_detach, mii_phy_activate
     65 };
     66 
     67 int	acphy_service(struct mii_softc *, struct mii_data *, int);
     68 void	acphy_status(struct mii_softc *);
     69 
     70 const struct mii_phy_funcs acphy_funcs = {
     71 	acphy_service, acphy_status, mii_phy_reset,
     72 };
     73 
     74 const struct mii_phydesc acphys[] = {
     75 	{ MII_OUI_ALTIMA,		MII_MODEL_ALTIMA_AC101,
     76 	  MII_STR_ALTIMA_AC101 },
     77 
     78 	{ 0,				0,
     79 	  NULL },
     80 };
     81 
     82 int
     83 acphymatch(parent, match, aux)
     84 	struct device *parent;
     85 	struct cfdata *match;
     86 	void *aux;
     87 {
     88 	struct mii_attach_args *ma = aux;
     89 
     90 	if (mii_phy_match(ma, acphys) != NULL)
     91 		return (10);
     92 
     93 	return (0);
     94 }
     95 
     96 void
     97 acphyattach(parent, self, aux)
     98 	struct device *parent, *self;
     99 	void *aux;
    100 {
    101 	struct mii_softc *sc = (struct mii_softc *)self;
    102 	struct mii_attach_args *ma = aux;
    103 	struct mii_data *mii = ma->mii_data;
    104 	const struct mii_phydesc *mpd;
    105 
    106 	mpd = mii_phy_match(ma, acphys);
    107 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    108 
    109 	sc->mii_inst = mii->mii_instance;
    110 	sc->mii_phy = ma->mii_phyno;
    111 	sc->mii_funcs = &acphy_funcs;
    112 	sc->mii_pdata = mii;
    113 	sc->mii_flags = mii->mii_flags;
    114 	sc->mii_anegticks = 5;
    115 
    116 	PHY_RESET(sc);
    117 
    118 	/*
    119 	 * XXX Check MCR_FX_SEL to set MIIF_HAVE_FIBER?
    120 	 */
    121 
    122 	sc->mii_capabilities =
    123 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    124 	printf("%s: ", sc->mii_dev.dv_xname);
    125 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
    126 		printf("no media present");
    127 	else
    128 		mii_phy_add_media(sc);
    129 	printf("\n");
    130 }
    131 
    132 int
    133 acphy_service(sc, mii, cmd)
    134 	struct mii_softc *sc;
    135 	struct mii_data *mii;
    136 	int cmd;
    137 {
    138 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    139 	int reg;
    140 
    141 	switch (cmd) {
    142 	case MII_POLLSTAT:
    143 		/*
    144 		 * If we're not polling our PHY instance, just return.
    145 		 */
    146 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    147 			return (0);
    148 		break;
    149 
    150 	case MII_MEDIACHG:
    151 		/*
    152 		 * If the media indicates a different PHY instance,
    153 		 * isolate ourselves.
    154 		 */
    155 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    156 			reg = PHY_READ(sc, MII_BMCR);
    157 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    158 			return (0);
    159 		}
    160 
    161 		/*
    162 		 * If the interface is not up, don't do anything.
    163 		 */
    164 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    165 			break;
    166 
    167 		mii_phy_setmedia(sc);
    168 		break;
    169 
    170 	case MII_TICK:
    171 		/*
    172 		 * If we're not currently selected, just return.
    173 		 */
    174 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    175 			return (0);
    176 
    177 		if (mii_phy_tick(sc) == EJUSTRETURN)
    178 			return (0);
    179 		break;
    180 
    181 	case MII_DOWN:
    182 		mii_phy_down(sc);
    183 		return (0);
    184 	}
    185 
    186 	/* Update the media status. */
    187 	mii_phy_status(sc);
    188 
    189 	/* Callback if something changed. */
    190 	mii_phy_update(sc, cmd);
    191 	return (0);
    192 }
    193 
    194 void
    195 acphy_status(sc)
    196 	struct mii_softc *sc;
    197 {
    198 	struct mii_data *mii = sc->mii_pdata;
    199 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    200 	int bmsr, bmcr, dr;
    201 
    202 	mii->mii_media_status = IFM_AVALID;
    203 	mii->mii_media_active = IFM_ETHER;
    204 
    205 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    206 	dr = PHY_READ(sc, MII_ACPHY_DR);
    207 
    208 	if (bmsr & BMSR_LINK)
    209 		mii->mii_media_status |= IFM_ACTIVE;
    210 
    211 	bmcr = PHY_READ(sc, MII_BMCR);
    212 	if (bmcr & BMCR_ISO) {
    213 		mii->mii_media_active |= IFM_NONE;
    214 		mii->mii_media_status = 0;
    215 		return;
    216 	}
    217 
    218 	if (bmcr & BMCR_LOOP)
    219 		mii->mii_media_active |= IFM_LOOP;
    220 
    221 	if (bmcr & BMCR_AUTOEN) {
    222 		/*
    223 		 * The media status bits are only valid of autonegotiation
    224 		 * has completed (or it's disabled).
    225 		 */
    226 		if ((bmsr & BMSR_ACOMP) == 0) {
    227 			/* Erg, still trying, I guess... */
    228 			mii->mii_media_active |= IFM_NONE;
    229 			return;
    230 		}
    231 
    232 		if (dr & DR_SPEED)
    233 			mii->mii_media_active |= IFM_100_TX;
    234 		else
    235 			mii->mii_media_active |= IFM_10_T;
    236 		if (dr & DR_DPLX)
    237 			mii->mii_media_active |= IFM_FDX;
    238 	} else
    239 		mii->mii_media_active = ife->ifm_media;
    240 }
    241