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