Home | History | Annotate | Line # | Download | only in mii
pnaphy.c revision 1.17
      1 /*	$NetBSD: pnaphy.c,v 1.17 2007/12/09 20:28:04 jmcneill 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/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: pnaphy.c,v 1.17 2007/12/09 20:28:04 jmcneill Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/kernel.h>
     55 #include <sys/device.h>
     56 #include <sys/socket.h>
     57 #include <sys/errno.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_media.h>
     61 
     62 #include <dev/mii/mii.h>
     63 #include <dev/mii/miivar.h>
     64 #include <dev/mii/miidevs.h>
     65 
     66 static int	pnaphymatch(struct device *, struct cfdata *, void *);
     67 static void	pnaphyattach(struct device *, struct device *, void *);
     68 
     69 CFATTACH_DECL(pnaphy, sizeof(struct mii_softc),
     70     pnaphymatch, pnaphyattach, mii_phy_detach, mii_phy_activate);
     71 
     72 static int	pnaphy_service(struct mii_softc *, struct mii_data *, int);
     73 static void	pnaphy_status(struct mii_softc *);
     74 
     75 static const struct mii_phy_funcs pnaphy_funcs = {
     76 	pnaphy_service, pnaphy_status, mii_phy_reset,
     77 };
     78 
     79 static 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 static int
     88 pnaphymatch(struct device *parent, struct cfdata *match,
     89     void *aux)
     90 {
     91 	struct mii_attach_args *ma = aux;
     92 
     93 	if (mii_phy_match(ma, pnaphys) != NULL) {
     94 		/*
     95 		 * Match higher than ukphy, but lower than a specific
     96 		 * driver would match.
     97 		 */
     98 		return (2);
     99 	}
    100 
    101 	return (0);
    102 }
    103 
    104 static void
    105 pnaphyattach(struct device *parent, struct device *self, void *aux)
    106 {
    107 	struct mii_softc *sc = device_private(self);
    108 	struct mii_attach_args *ma = aux;
    109 	struct mii_data *mii = ma->mii_data;
    110 	const struct mii_phydesc *mpd;
    111 
    112 	mpd = mii_phy_match(ma, pnaphys);
    113 	aprint_naive(": Media interface\n");
    114 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    115 
    116 	sc->mii_inst = mii->mii_instance;
    117 	sc->mii_phy = ma->mii_phyno;
    118 	sc->mii_funcs = &pnaphy_funcs;
    119 	sc->mii_pdata = mii;
    120 	sc->mii_flags = ma->mii_flags | MIIF_IS_HPNA; /* force HomePNA */
    121 	sc->mii_anegticks = MII_ANEGTICKS;
    122 
    123 	PHY_RESET(sc);
    124 
    125 	sc->mii_capabilities =
    126 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    127 	aprint_normal("%s: ", sc->mii_dev.dv_xname);
    128 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
    129 		aprint_error("no media present");
    130 	else
    131 		mii_phy_add_media(sc);
    132 	aprint_normal("\n");
    133 
    134 	if (!pmf_device_register(self, NULL, mii_phy_resume))
    135 		aprint_error_dev(self, "couldn't establish power handler\n");
    136 }
    137 
    138 static int
    139 pnaphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    140 {
    141 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    142 	int reg;
    143 
    144 	switch (cmd) {
    145 	case MII_POLLSTAT:
    146 		/*
    147 		 * If we're not polling our PHY instance, just return.
    148 		 */
    149 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    150 			return (0);
    151 		break;
    152 
    153 	case MII_MEDIACHG:
    154 		/*
    155 		 * If the media indicates a different PHY instance,
    156 		 * isolate ourselves.
    157 		 */
    158 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    159 			reg = PHY_READ(sc, MII_BMCR);
    160 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    161 			return (0);
    162 		}
    163 
    164 		/*
    165 		 * If the interface is not up, don't do anything.
    166 		 */
    167 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    168 			break;
    169 
    170 		mii_phy_setmedia(sc);
    171 		break;
    172 
    173 	case MII_TICK:
    174 		/*
    175 		 * If we're not currently selected, just return.
    176 		 */
    177 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    178 			return (0);
    179 
    180 		if (mii_phy_tick(sc) == EJUSTRETURN)
    181 			return (0);
    182 		break;
    183 
    184 	case MII_DOWN:
    185 		mii_phy_down(sc);
    186 		return (0);
    187 	}
    188 
    189 	/* Update the media status. */
    190 	mii_phy_status(sc);
    191 
    192 	/* Callback if something changed. */
    193 	mii_phy_update(sc, cmd);
    194 	return (0);
    195 }
    196 
    197 static void
    198 pnaphy_status(struct mii_softc *sc)
    199 {
    200 	struct mii_data *mii = sc->mii_pdata;
    201 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    202 	int bmsr, bmcr;
    203 
    204 	mii->mii_media_status = IFM_AVALID;
    205 	mii->mii_media_active = IFM_ETHER;
    206 
    207 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    208 
    209 	if (bmsr & BMSR_LINK)
    210 		mii->mii_media_status |= IFM_ACTIVE;
    211 
    212 	bmcr = PHY_READ(sc, MII_BMCR);
    213 	if (bmcr & BMCR_ISO) {
    214 		mii->mii_media_active |= IFM_NONE;
    215 		mii->mii_media_status = 0;
    216 		return;
    217 	}
    218 
    219 	if (bmcr & BMCR_LOOP)
    220 		mii->mii_media_active |= IFM_LOOP;
    221 
    222 	/*
    223 	 * On HomePNA PHYs, the current media is always the selected
    224 	 * media.
    225 	 *
    226 	 * XXX May not be the case w/ HomePNA 2.
    227 	 */
    228 	mii->mii_media_active = ife->ifm_media;
    229 }
    230