Home | History | Annotate | Line # | Download | only in mii
pnaphy.c revision 1.25
      1 /*	$NetBSD: pnaphy.c,v 1.25 2019/11/27 10:19:21 msaitoh 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.25 2019/11/27 10:19:21 msaitoh 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(device_t, cfdata_t, void *);
     67 static void	pnaphyattach(device_t, device_t, void *);
     68 
     69 CFATTACH_DECL_NEW(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_PHY_DESC(yyAMD, 79c901home),
     81 	MII_PHY_END,
     82 };
     83 
     84 static int
     85 pnaphymatch(device_t parent, cfdata_t match, void *aux)
     86 {
     87 	struct mii_attach_args *ma = aux;
     88 
     89 	if (mii_phy_match(ma, pnaphys) != NULL) {
     90 		/*
     91 		 * Match higher than ukphy, but lower than a specific
     92 		 * driver would match.
     93 		 */
     94 		return 2;
     95 	}
     96 
     97 	return 0;
     98 }
     99 
    100 static void
    101 pnaphyattach(device_t parent, device_t self, void *aux)
    102 {
    103 	struct mii_softc *sc = device_private(self);
    104 	struct mii_attach_args *ma = aux;
    105 	struct mii_data *mii = ma->mii_data;
    106 	const struct mii_phydesc *mpd;
    107 
    108 	mpd = mii_phy_match(ma, pnaphys);
    109 	aprint_naive(": Media interface\n");
    110 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    111 
    112 	sc->mii_dev = self;
    113 	sc->mii_inst = mii->mii_instance;
    114 	sc->mii_phy = ma->mii_phyno;
    115 	sc->mii_funcs = &pnaphy_funcs;
    116 	sc->mii_pdata = mii;
    117 	sc->mii_flags = ma->mii_flags | MIIF_IS_HPNA; /* Force HomePNA */
    118 
    119 	PHY_RESET(sc);
    120 
    121 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
    122 	sc->mii_capabilities &= ma->mii_capmask;
    123 
    124 	mii_phy_add_media(sc);
    125 }
    126 
    127 static int
    128 pnaphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    129 {
    130 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    131 	uint16_t reg;
    132 
    133 	switch (cmd) {
    134 	case MII_POLLSTAT:
    135 		/* If we're not polling our PHY instance, just return. */
    136 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    137 			return 0;
    138 		break;
    139 
    140 	case MII_MEDIACHG:
    141 		/*
    142 		 * If the media indicates a different PHY instance,
    143 		 * isolate ourselves.
    144 		 */
    145 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    146 			PHY_READ(sc, MII_BMCR, &reg);
    147 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    148 			return 0;
    149 		}
    150 
    151 		/* If the interface is not up, don't do anything. */
    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 		/* If we're not currently selected, just return. */
    160 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    161 			return 0;
    162 
    163 		if (mii_phy_tick(sc) == EJUSTRETURN)
    164 			return 0;
    165 		break;
    166 
    167 	case MII_DOWN:
    168 		mii_phy_down(sc);
    169 		return 0;
    170 	}
    171 
    172 	/* Update the media status. */
    173 	mii_phy_status(sc);
    174 
    175 	/* Callback if something changed. */
    176 	mii_phy_update(sc, cmd);
    177 	return 0;
    178 }
    179 
    180 static void
    181 pnaphy_status(struct mii_softc *sc)
    182 {
    183 	struct mii_data *mii = sc->mii_pdata;
    184 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    185 	uint16_t bmsr, bmcr;
    186 
    187 	mii->mii_media_status = IFM_AVALID;
    188 	mii->mii_media_active = IFM_ETHER;
    189 
    190 	PHY_READ(sc, MII_BMSR, &bmsr);
    191 	PHY_READ(sc, MII_BMSR, &bmsr);
    192 
    193 	if (bmsr & BMSR_LINK)
    194 		mii->mii_media_status |= IFM_ACTIVE;
    195 
    196 	PHY_READ(sc, MII_BMCR, &bmcr);
    197 	if (bmcr & BMCR_ISO) {
    198 		mii->mii_media_active |= IFM_NONE;
    199 		mii->mii_media_status = 0;
    200 		return;
    201 	}
    202 
    203 	if (bmcr & BMCR_LOOP)
    204 		mii->mii_media_active |= IFM_LOOP;
    205 
    206 	/*
    207 	 * On HomePNA PHYs, the current media is always the selected
    208 	 * media.
    209 	 *
    210 	 * XXX May not be the case w/ HomePNA 2.
    211 	 */
    212 	mii->mii_media_active = ife->ifm_media;
    213 }
    214