Home | History | Annotate | Line # | Download | only in mii
rlphy.c revision 1.13
      1 /*	$NetBSD: rlphy.c,v 1.13 2007/03/19 12:23:30 tsutsui Exp $	*/
      2 /*	$OpenBSD: rlphy.c,v 1.20 2005/07/31 05:27:30 pvalchev Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998, 1999 Jason L. Wright (jason (at) thought.net)
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Driver for the internal PHY found on RTL8139 based nics, based
     32  * on drivers for the 'exphy' (Internal 3Com phys) and 'nsphy'
     33  * (National Semiconductor DP83840).
     34  */
     35 
     36 /*
     37  * Ported to NetBSD by Juan Romero Pardines <xtraeme (at) NetBSD.org>
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: rlphy.c,v 1.13 2007/03/19 12:23:30 tsutsui Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.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 #include <machine/bus.h>
     57 #include <dev/ic/rtl81x9reg.h>
     58 
     59 int	rlphymatch(struct device *, struct cfdata *, void *);
     60 void	rlphyattach(struct device *, struct device *, void *);
     61 
     62 CFATTACH_DECL(rlphy, sizeof(struct mii_softc),
     63     rlphymatch, rlphyattach, mii_phy_detach, mii_phy_activate);
     64 
     65 int	rlphy_service(struct mii_softc *, struct mii_data *, int);
     66 void	rlphy_status(struct mii_softc *);
     67 
     68 static void rlphy_reset(struct mii_softc *);
     69 
     70 const struct mii_phy_funcs rlphy_funcs = {
     71 	rlphy_service, rlphy_status, rlphy_reset,
     72 };
     73 
     74 static const struct mii_phydesc rlphys[] = {
     75 	{ MII_OUI_yyREALTEK,		MII_MODEL_yyREALTEK_RTL8201L,
     76           MII_STR_yyREALTEK_RTL8201L },
     77 	{ MII_OUI_ICPLUS,		MII_MODEL_ICPLUS_IP101,
     78 	  MII_STR_ICPLUS_IP101 },
     79 
     80 	{ 0,				0,
     81 	  NULL },
     82 };
     83 
     84 int
     85 rlphymatch(struct device *parent, struct cfdata *match, void *aux)
     86 {
     87 	struct mii_attach_args *ma = aux;
     88 
     89 	if (mii_phy_match(ma, rlphys) != NULL)
     90 		return (10);
     91 
     92 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
     93 	    MII_MODEL(ma->mii_id2) != 0)
     94 		return 0;
     95 
     96 	if (!device_is_a(parent, "rtk") && !device_is_a(parent, "re"))
     97 		return 0;
     98 
     99 	/*
    100 	 * A "real" phy should get preference, but on the 8139 there
    101 	 * is no phyid register.
    102 	 */
    103 	return 5;
    104 }
    105 
    106 void
    107 rlphyattach(struct device *parent, struct device *self, void *aux)
    108 {
    109 	struct mii_softc *sc = device_private(self);
    110 	struct mii_attach_args *ma = aux;
    111 	struct mii_data *mii = ma->mii_data;
    112 
    113 	if (MII_MODEL(ma->mii_id2) == MII_MODEL_yyREALTEK_RTL8201L) {
    114 		aprint_normal(": %s, rev. %d\n", MII_STR_yyREALTEK_RTL8201L,
    115 		    MII_REV(ma->mii_id2));
    116 	} else
    117 		aprint_normal(": Realtek internal PHY\n");
    118 
    119 	sc->mii_inst = mii->mii_instance;
    120 	sc->mii_phy = ma->mii_phyno;
    121 	sc->mii_funcs = &rlphy_funcs;
    122 	sc->mii_pdata = mii;
    123 	sc->mii_flags = ma->mii_flags;
    124 
    125 	sc->mii_flags |= MIIF_NOISOLATE;
    126 
    127 	PHY_RESET(sc);
    128 
    129 	aprint_normal("%s: ", sc->mii_dev.dv_xname);
    130 	sc->mii_capabilities =
    131 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    132 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
    133 		mii_phy_add_media(sc);
    134 	aprint_normal("\n");
    135 }
    136 
    137 int
    138 rlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    139 {
    140 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    141 
    142 	int rv;
    143 
    144 	if (!device_is_active(&sc->mii_dev))
    145 		return ENXIO;
    146 
    147 	/*
    148 	 * Can't isolate the RTL8139 phy, so it has to be the only one.
    149 	 */
    150 	if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    151 		panic("rlphy_service: attempt to isolate phy");
    152 
    153 	switch (cmd) {
    154 	case MII_POLLSTAT:
    155 		break;
    156 
    157 	case MII_MEDIACHG:
    158 		/*
    159 		 * If the interface is not up, don't do anything.
    160 		 */
    161 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    162 			break;
    163 
    164 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    165 		case IFM_AUTO:
    166 			/*
    167 			 * If we're already in auto mode, just return.
    168 			 */
    169 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
    170 				return (0);
    171 			(void) mii_phy_auto(sc, 0);
    172 			break;
    173 		case IFM_100_T4:
    174 			/*
    175 			 * XXX Not supported as a manual setting right now.
    176 			 */
    177 			return (EINVAL);
    178 		default:
    179 			/*
    180 			 * BMCR data is stored in the ifmedia entry.
    181 			 */
    182 			switch (ife->ifm_media &
    183 			    (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
    184 				case IFM_ETHER|IFM_10_T:
    185 					rv = ANAR_10|ANAR_CSMA;
    186 					break;
    187 				case IFM_ETHER|IFM_10_T|IFM_FDX:
    188 					rv = ANAR_10_FD|ANAR_CSMA;
    189 					break;
    190 				case IFM_ETHER|IFM_100_TX:
    191 					rv = ANAR_TX|ANAR_CSMA;
    192 					break;
    193 				case IFM_ETHER|IFM_100_TX|IFM_FDX:
    194 					rv = ANAR_TX_FD|ANAR_CSMA;
    195 					break;
    196 				case IFM_ETHER|IFM_100_T4:
    197 					rv = ANAR_T4|ANAR_CSMA;
    198 					break;
    199 				default:
    200 					rv = 0;
    201 					break;
    202 			}
    203 
    204 			PHY_WRITE(sc, MII_ANAR, rv);
    205 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
    206 		}
    207 		break;
    208 
    209 	case MII_TICK:
    210 		/*
    211 		 * Is the interface even up?
    212 		 */
    213 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    214 			return (0);
    215 
    216 		/*
    217 		 * Only used for autonegotiation.
    218 		 */
    219 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    220 			break;
    221 
    222 		/*
    223 		 * The RealTek PHY's autonegotiation doesn't need to be
    224 		 * kicked; it continues in the background.
    225 		 */
    226 		break;
    227 
    228 	case MII_DOWN:
    229 		mii_phy_down(sc);
    230 		return (0);
    231 	}
    232 
    233 	/* Update the media status. */
    234 	mii_phy_status(sc);
    235 
    236 	/* Callback if something changed. */
    237 	mii_phy_update(sc, cmd);
    238 	return (0);
    239 }
    240 
    241 void
    242 rlphy_status(struct mii_softc *sc)
    243 {
    244 	struct mii_data *mii = sc->mii_pdata;
    245 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    246 	int bmsr, bmcr, anlpar;
    247 
    248 	mii->mii_media_status = IFM_AVALID;
    249 	mii->mii_media_active = IFM_ETHER;
    250 
    251 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    252 	if (bmsr & BMSR_LINK)
    253 		mii->mii_media_status |= IFM_ACTIVE;
    254 
    255 	bmcr = PHY_READ(sc, MII_BMCR);
    256 	if (bmcr & BMCR_ISO) {
    257 		mii->mii_media_active |= IFM_NONE;
    258 		mii->mii_media_status = 0;
    259 		return;
    260 	}
    261 
    262 	if (bmcr & BMCR_LOOP)
    263 		mii->mii_media_active |= IFM_LOOP;
    264 
    265 	if (bmcr & BMCR_AUTOEN) {
    266 		/*
    267 		 * NWay autonegotiation takes the highest-order common
    268 		 * bit of the ANAR and ANLPAR (i.e. best media advertised
    269 		 * both by us and our link partner).
    270 		 */
    271 		if ((bmsr & BMSR_ACOMP) == 0) {
    272 			/* Erg, still trying, I guess... */
    273 			mii->mii_media_active |= IFM_NONE;
    274 			return;
    275 		}
    276 
    277 		if ((anlpar = PHY_READ(sc, MII_ANAR) &
    278 		    PHY_READ(sc, MII_ANLPAR))) {
    279 			if (anlpar & ANLPAR_T4)
    280 				mii->mii_media_active |= IFM_100_T4;
    281 			else if (anlpar & ANLPAR_TX_FD)
    282 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
    283 			else if (anlpar & ANLPAR_TX)
    284 				mii->mii_media_active |= IFM_100_TX;
    285 			else if (anlpar & ANLPAR_10_FD)
    286 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
    287 			else if (anlpar & ANLPAR_10)
    288 				mii->mii_media_active |= IFM_10_T;
    289 			else
    290 				mii->mii_media_active |= IFM_NONE;
    291 			return;
    292 		}
    293 
    294 		/*
    295 		 * If the other side doesn't support NWAY, then the
    296 		 * best we can do is determine if we have a 10Mbps or
    297 		 * 100Mbps link. There's no way to know if the link
    298 		 * is full or half duplex, so we default to half duplex
    299 		 * and hope that the user is clever enough to manually
    300 		 * change the media settings if we're wrong.
    301 		 */
    302 
    303 		/*
    304 		 * The RealTek PHY supports non-NWAY link speed
    305 		 * detection, however it does not report the link
    306 		 * detection results via the ANLPAR or BMSR registers.
    307 		 * (What? RealTek doesn't do things the way everyone
    308 		 * else does? I'm just shocked, shocked I tell you.)
    309 		 * To determine the link speed, we have to do one
    310 		 * of two things:
    311 		 *
    312 		 * - If this is a standalone RealTek RTL8201(L) PHY,
    313 		 *   we can determine the link speed by testing bit 0
    314 		 *   in the magic, vendor-specific register at offset
    315 		 *   0x19.
    316 		 *
    317 		 * - If this is a RealTek MAC with integrated PHY, we
    318 		 *   can test the 'SPEED10' bit of the MAC's media status
    319 		 *   register.
    320 		 */
    321 		if (device_is_a(device_parent(&sc->mii_dev), "rtk")) {
    322 			if (PHY_READ(sc, RTK_MEDIASTAT) & RTK_MEDIASTAT_SPEED10)
    323 				mii->mii_media_active |= IFM_10_T;
    324 			else
    325 				mii->mii_media_active |= IFM_100_TX;
    326 		} else {
    327 			if (PHY_READ(sc, 0x0019) & 0x01)
    328 				mii->mii_media_active |= IFM_100_TX;
    329 			else
    330 				mii->mii_media_active |= IFM_10_T;
    331 		}
    332 
    333 	} else
    334 		mii->mii_media_active = ife->ifm_media;
    335 }
    336 
    337 static void
    338 rlphy_reset(struct mii_softc *sc)
    339 {
    340 
    341 	mii_phy_reset(sc);
    342 
    343 	/*
    344 	 * XXX RealTek PHY doesn't set the BMCR properly after
    345 	 * XXX reset, which breaks autonegotiation.
    346 	 */
    347 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN);
    348 }
    349