Home | History | Annotate | Line # | Download | only in mii
pnaphy.c revision 1.1
      1 /*	$NetBSD: pnaphy.c,v 1.1 2001/08/25 03:21:38 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 generic HomePNA 1.0 PHYs.
     40  *
     41  * HomePNA 1.0 PHYs are pretty simple -- they support exactly one
     42  * media type, and don't do autonegotiation.
     43  *
     44  * HomePNA PHYs often have vendor-specific registers for tuning
     45  * the network connection, but we don't deal with any of that
     46  * at all.
     47  */
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/kernel.h>
     52 #include <sys/device.h>
     53 #include <sys/malloc.h>
     54 #include <sys/socket.h>
     55 #include <sys/errno.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_media.h>
     59 
     60 #include <dev/mii/mii.h>
     61 #include <dev/mii/miivar.h>
     62 #include <dev/mii/miidevs.h>
     63 
     64 int	pnaphymatch(struct device *, struct cfdata *, void *);
     65 void	pnaphyattach(struct device *, struct device *, void *);
     66 
     67 struct cfattach pnaphy_ca = {
     68 	sizeof(struct mii_softc), pnaphymatch, pnaphyattach,
     69 	    mii_phy_detach, mii_phy_activate
     70 };
     71 
     72 int	pnaphy_service(struct mii_softc *, struct mii_data *, int);
     73 void	pnaphy_status(struct mii_softc *);
     74 
     75 const struct mii_phy_funcs pnaphy_funcs = {
     76 	pnaphy_service, pnaphy_status, mii_phy_reset,
     77 };
     78 
     79 const struct mii_phydesc pnaphys[] = {
     80 	{ MII_OUI_yyAMD,		MII_MODEL_yyAMD_79c901home,
     81 	  MII_STR_yyAMD_79c901home },
     82 
     83 	{ 0,				0,
     84 	  NULL },
     85 };
     86 
     87 int
     88 pnaphymatch(parent, match, aux)
     89 	struct device *parent;
     90 	struct cfdata *match;
     91 	void *aux;
     92 {
     93 	struct mii_attach_args *ma = aux;
     94 
     95 	if (mii_phy_match(ma, pnaphys) != NULL) {
     96 		/*
     97 		 * Match higher than ukphy, but lower than a specific
     98 		 * driver would match.
     99 		 */
    100 		return (2);
    101 	}
    102 
    103 	return (0);
    104 }
    105 
    106 void
    107 pnaphyattach(parent, self, aux)
    108 	struct device *parent, *self;
    109 	void *aux;
    110 {
    111 	struct mii_softc *sc = (struct mii_softc *)self;
    112 	struct mii_attach_args *ma = aux;
    113 	struct mii_data *mii = ma->mii_data;
    114 	const struct mii_phydesc *mpd;
    115 
    116 	mpd = mii_phy_match(ma, pnaphys);
    117 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    118 
    119 	sc->mii_inst = mii->mii_instance;
    120 	sc->mii_phy = ma->mii_phyno;
    121 	sc->mii_funcs = &pnaphy_funcs;
    122 	sc->mii_pdata = mii;
    123 	sc->mii_flags = mii->mii_flags;
    124 	sc->mii_anegticks = 5;
    125 
    126 	PHY_RESET(sc);
    127 
    128 	sc->mii_capabilities =
    129 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    130 	printf("%s: ", sc->mii_dev.dv_xname);
    131 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
    132 		printf("no media present");
    133 	else
    134 		mii_phy_add_media(sc);
    135 	printf("\n");
    136 }
    137 
    138 int
    139 pnaphy_service(sc, mii, cmd)
    140 	struct mii_softc *sc;
    141 	struct mii_data *mii;
    142 	int cmd;
    143 {
    144 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    145 	int reg;
    146 
    147 	switch (cmd) {
    148 	case MII_POLLSTAT:
    149 		/*
    150 		 * If we're not polling our PHY instance, just return.
    151 		 */
    152 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    153 			return (0);
    154 		break;
    155 
    156 	case MII_MEDIACHG:
    157 		/*
    158 		 * If the media indicates a different PHY instance,
    159 		 * isolate ourselves.
    160 		 */
    161 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    162 			reg = PHY_READ(sc, MII_BMCR);
    163 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    164 			return (0);
    165 		}
    166 
    167 		/*
    168 		 * If the interface is not up, don't do anything.
    169 		 */
    170 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    171 			break;
    172 
    173 		mii_phy_setmedia(sc);
    174 		break;
    175 
    176 	case MII_TICK:
    177 		/*
    178 		 * If we're not currently selected, just return.
    179 		 */
    180 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    181 			return (0);
    182 
    183 		if (mii_phy_tick(sc) == EJUSTRETURN)
    184 			return (0);
    185 		break;
    186 
    187 	case MII_DOWN:
    188 		mii_phy_down(sc);
    189 		return (0);
    190 	}
    191 
    192 	/* Update the media status. */
    193 	mii_phy_status(sc);
    194 
    195 	/* Callback if something changed. */
    196 	mii_phy_update(sc, cmd);
    197 	return (0);
    198 }
    199 
    200 void
    201 pnaphy_status(sc)
    202 	struct mii_softc *sc;
    203 {
    204 	struct mii_data *mii = sc->mii_pdata;
    205 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    206 	int bmsr, bmcr;
    207 
    208 	mii->mii_media_status = IFM_AVALID;
    209 	mii->mii_media_active = IFM_ETHER;
    210 
    211 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    212 
    213 	if (bmsr & BMSR_LINK)
    214 		mii->mii_media_status |= IFM_ACTIVE;
    215 
    216 	bmcr = PHY_READ(sc, MII_BMCR);
    217 	if (bmcr & BMCR_ISO) {
    218 		mii->mii_media_active |= IFM_NONE;
    219 		mii->mii_media_status = 0;
    220 		return;
    221 	}
    222 
    223 	if (bmcr & BMCR_LOOP)
    224 		mii->mii_media_active |= IFM_LOOP;
    225 
    226 	/*
    227 	 * On HomePNA PHYs, the current media is always the selected
    228 	 * media.
    229 	 *
    230 	 * XXX May not be the case w/ HomePNA 2.
    231 	 */
    232 	mii->mii_media_active = ife->ifm_media;
    233 }
    234