Home | History | Annotate | Line # | Download | only in mii
urlphy.c revision 1.3
      1  1.3  thorpej /*	$NetBSD: urlphy.c,v 1.3 2002/09/27 02:24:30 thorpej Exp $	*/
      2  1.1   ichiro /*
      3  1.1   ichiro  * Copyright (c) 2001, 2002
      4  1.1   ichiro  *     Shingo WATANABE <nabe (at) nabechan.org>.  All rights reserved.
      5  1.1   ichiro  *
      6  1.1   ichiro  * Redistribution and use in source and binary forms, with or without
      7  1.1   ichiro  * modification, are permitted provided that the following conditions
      8  1.1   ichiro  * are met:
      9  1.1   ichiro  * 1. Redistributions of source code must retain the above copyright
     10  1.1   ichiro  *    notice, this list of conditions and the following disclaimer.
     11  1.1   ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   ichiro  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   ichiro  *    documentation and/or other materials provided with the distribution.
     14  1.1   ichiro  * 3. All advertising materials mentioning features or use of this software
     15  1.1   ichiro  *    must display the following acknowledgement:
     16  1.1   ichiro  *	This product includes software developed by Shingo WATANABE.
     17  1.1   ichiro  * 4. Neither the name of the author nor the names of any co-contributors
     18  1.1   ichiro  *    may be used to endorse or promote products derived from this software
     19  1.1   ichiro  *    without specific prior written permission.
     20  1.1   ichiro  *
     21  1.1   ichiro  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     22  1.1   ichiro  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1   ichiro  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1   ichiro  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     25  1.1   ichiro  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1   ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1   ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1   ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1   ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1   ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1   ichiro  * SUCH DAMAGE.
     32  1.1   ichiro  *
     33  1.1   ichiro  */
     34  1.1   ichiro 
     35  1.1   ichiro /*
     36  1.1   ichiro  * driver for Realtek RL8150L internal phy
     37  1.1   ichiro  */
     38  1.1   ichiro 
     39  1.1   ichiro #include <sys/cdefs.h>
     40  1.3  thorpej __KERNEL_RCSID(0, "$NetBSD: urlphy.c,v 1.3 2002/09/27 02:24:30 thorpej Exp $");
     41  1.1   ichiro 
     42  1.1   ichiro #include <sys/param.h>
     43  1.1   ichiro #include <sys/systm.h>
     44  1.1   ichiro #include <sys/kernel.h>
     45  1.1   ichiro #include <sys/device.h>
     46  1.1   ichiro #include <sys/socket.h>
     47  1.1   ichiro 
     48  1.1   ichiro #include <net/if.h>
     49  1.1   ichiro #include <net/if_media.h>
     50  1.1   ichiro 
     51  1.1   ichiro #include <dev/mii/mii.h>
     52  1.1   ichiro #include <dev/mii/miivar.h>
     53  1.1   ichiro #include <dev/mii/miidevs.h>
     54  1.1   ichiro #include <dev/mii/urlphyreg.h>
     55  1.1   ichiro 
     56  1.1   ichiro #define	URLPHY_DEBUG	0
     57  1.1   ichiro #ifdef URLPHY_DEBUG
     58  1.1   ichiro #define DPRINTF(x)	if (urlphydebug) printf x
     59  1.1   ichiro #define DPRINTFN(n,x)	if (urlphydebug>(n)) printf x
     60  1.1   ichiro int urlphydebug = URLPHY_DEBUG;
     61  1.1   ichiro #else
     62  1.1   ichiro #define DPRINTF(x)
     63  1.1   ichiro #define DPRINTFN(n,x)
     64  1.1   ichiro #endif
     65  1.1   ichiro 
     66  1.1   ichiro int urlphy_match(struct device *, struct cfdata *, void *);
     67  1.1   ichiro void urlphy_attach(struct device *, struct device *, void *);
     68  1.1   ichiro 
     69  1.1   ichiro struct cfattach urlphy_ca = {
     70  1.1   ichiro 	sizeof(struct mii_softc), urlphy_match, urlphy_attach, mii_phy_detach,
     71  1.1   ichiro 	mii_phy_activate
     72  1.1   ichiro };
     73  1.1   ichiro 
     74  1.1   ichiro int urlphy_service(struct mii_softc *, struct mii_data *, int);
     75  1.1   ichiro void urlphy_status(struct mii_softc *);
     76  1.1   ichiro 
     77  1.1   ichiro const struct mii_phy_funcs urlphy_funcs = {
     78  1.1   ichiro 	urlphy_service, urlphy_status, mii_phy_reset,
     79  1.1   ichiro };
     80  1.1   ichiro 
     81  1.1   ichiro int
     82  1.1   ichiro urlphy_match(struct device *parent, struct cfdata *match, void *aux)
     83  1.1   ichiro {
     84  1.1   ichiro 	struct mii_attach_args *ma = aux;
     85  1.1   ichiro 
     86  1.1   ichiro 	/*
     87  1.1   ichiro 	 * RTL8150 reports OUT == 0, MODEL == 0
     88  1.1   ichiro 	 */
     89  1.1   ichiro 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 &&
     90  1.1   ichiro 	    MII_MODEL(ma->mii_id2) != 0)
     91  1.1   ichiro 		return (0);
     92  1.1   ichiro 
     93  1.1   ichiro 	/*
     94  1.1   ichiro 	 * Make sure the parent is an 'url' device.
     95  1.1   ichiro 	 */
     96  1.3  thorpej 	if (strcmp(parent->dv_cfdata->cf_name, "url") != 0)
     97  1.1   ichiro 		return(0);
     98  1.1   ichiro 
     99  1.1   ichiro 	return (10);
    100  1.1   ichiro }
    101  1.1   ichiro 
    102  1.1   ichiro void
    103  1.1   ichiro urlphy_attach(struct device *parent, struct device *self, void *aux)
    104  1.1   ichiro {
    105  1.1   ichiro 	struct mii_softc *sc = (struct mii_softc *)self;
    106  1.1   ichiro 	struct mii_attach_args *ma = aux;
    107  1.1   ichiro 	struct mii_data *mii = ma->mii_data;
    108  1.1   ichiro 
    109  1.1   ichiro 	printf(": Realtek RTL8150L internal media interface\n");
    110  1.1   ichiro 
    111  1.1   ichiro 	DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __FUNCTION__));
    112  1.1   ichiro 
    113  1.1   ichiro 	sc->mii_inst = mii->mii_instance;
    114  1.1   ichiro 	sc->mii_phy = ma->mii_phyno;
    115  1.1   ichiro 	sc->mii_funcs = &urlphy_funcs;
    116  1.1   ichiro 	sc->mii_pdata = mii;
    117  1.1   ichiro 	sc->mii_flags = mii->mii_flags;
    118  1.1   ichiro 	sc->mii_anegticks = 10;
    119  1.1   ichiro 
    120  1.1   ichiro 	/* Don't do loopback on this PHY. */
    121  1.1   ichiro 	sc->mii_flags |= MIIF_NOLOOP;
    122  1.1   ichiro 	/* Don't do isolate on this PHY. */
    123  1.1   ichiro 	sc->mii_flags |= MIIF_NOISOLATE;
    124  1.1   ichiro 
    125  1.1   ichiro 	if (mii->mii_instance != 0) {
    126  1.1   ichiro 		printf("%s: ignoring this PHY, non-zero instance\n",
    127  1.1   ichiro 		       sc->mii_dev.dv_xname);
    128  1.1   ichiro 		return;
    129  1.1   ichiro 	}
    130  1.1   ichiro 	PHY_RESET(sc);
    131  1.1   ichiro 
    132  1.1   ichiro 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    133  1.1   ichiro 	printf("%s: ", sc->mii_dev.dv_xname);
    134  1.1   ichiro 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
    135  1.1   ichiro 		printf("no media present");
    136  1.1   ichiro 	else
    137  1.1   ichiro 		mii_phy_add_media(sc);
    138  1.1   ichiro 	printf("\n");
    139  1.1   ichiro }
    140  1.1   ichiro 
    141  1.1   ichiro int
    142  1.1   ichiro urlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    143  1.1   ichiro {
    144  1.1   ichiro 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    145  1.1   ichiro 	int reg;
    146  1.1   ichiro 
    147  1.1   ichiro 	DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __FUNCTION__));
    148  1.1   ichiro 
    149  1.1   ichiro 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
    150  1.1   ichiro 		return (ENXIO);
    151  1.1   ichiro 
    152  1.1   ichiro 	switch (cmd) {
    153  1.1   ichiro 	case MII_POLLSTAT:
    154  1.1   ichiro 		/*
    155  1.1   ichiro 		 * If we're not polling our PHY instance, just return.
    156  1.1   ichiro 		 */
    157  1.1   ichiro 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    158  1.1   ichiro 			return (0);
    159  1.1   ichiro 		break;
    160  1.1   ichiro 
    161  1.1   ichiro 	case MII_MEDIACHG:
    162  1.1   ichiro 		/*
    163  1.1   ichiro 		 * If we're not currently selected, just return.
    164  1.1   ichiro 		 */
    165  1.1   ichiro 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    166  1.1   ichiro 			return (0);
    167  1.1   ichiro 
    168  1.1   ichiro 		/* If the interface is not up, don't do anything. */
    169  1.1   ichiro 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    170  1.1   ichiro 			break;
    171  1.1   ichiro 
    172  1.1   ichiro 		mii_phy_setmedia(sc);
    173  1.1   ichiro 		break;
    174  1.1   ichiro 
    175  1.1   ichiro 	case MII_TICK:
    176  1.1   ichiro 		/*
    177  1.1   ichiro 		 * If we're not currently selected, just return.
    178  1.1   ichiro 		 */
    179  1.1   ichiro 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    180  1.1   ichiro 			return (0);
    181  1.1   ichiro 
    182  1.1   ichiro 		/* Just bail now if the interface is down. */
    183  1.1   ichiro 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    184  1.1   ichiro 			return (0);
    185  1.1   ichiro 
    186  1.1   ichiro 		/*
    187  1.1   ichiro 		 * If we're not doing autonegotiation, we don't need to do
    188  1.1   ichiro 		 * any extra work here.  However, we need to check the link
    189  1.1   ichiro 		 * status so we can generate an announcement if the status
    190  1.1   ichiro 		 * changes.
    191  1.1   ichiro 		 */
    192  1.1   ichiro 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    193  1.1   ichiro 			return (0);
    194  1.1   ichiro 
    195  1.1   ichiro 		/* Read the status register twice; MSR_LINK is latch-low. */
    196  1.1   ichiro 		reg = PHY_READ(sc, URLPHY_MSR) | PHY_READ(sc, URLPHY_MSR);
    197  1.1   ichiro 		if (reg & URLPHY_MSR_LINK)
    198  1.1   ichiro 			return (0);
    199  1.1   ichiro 
    200  1.1   ichiro 		/*
    201  1.1   ichiro 		 * Only retry autonegotiation every N seconds.
    202  1.1   ichiro 		 */
    203  1.1   ichiro 		KASSERT(sc->mii_anegticks != 0);
    204  1.1   ichiro 		if (++sc->mii_ticks != sc->mii_anegticks)
    205  1.1   ichiro 			return (0);
    206  1.1   ichiro 
    207  1.1   ichiro 		sc->mii_ticks = 0;
    208  1.1   ichiro 		PHY_RESET(sc);
    209  1.1   ichiro 
    210  1.1   ichiro 		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
    211  1.1   ichiro 			return (0);
    212  1.1   ichiro 
    213  1.1   ichiro 		break;
    214  1.1   ichiro 
    215  1.1   ichiro 	case MII_DOWN:
    216  1.1   ichiro 		mii_phy_down(sc);
    217  1.1   ichiro 		return (0);
    218  1.1   ichiro 	}
    219  1.1   ichiro 
    220  1.1   ichiro 	/* Update the media status. */
    221  1.1   ichiro 	mii_phy_status(sc);
    222  1.1   ichiro 
    223  1.1   ichiro 	/* Callback if something changed. */
    224  1.1   ichiro 	mii_phy_update(sc, cmd);
    225  1.1   ichiro 
    226  1.1   ichiro 	return (0);
    227  1.1   ichiro }
    228  1.1   ichiro 
    229  1.1   ichiro void
    230  1.1   ichiro urlphy_status(struct mii_softc *sc)
    231  1.1   ichiro {
    232  1.1   ichiro 	struct mii_data *mii = sc->mii_pdata;
    233  1.1   ichiro 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    234  1.1   ichiro 	int msr, bmsr, bmcr;
    235  1.1   ichiro 
    236  1.1   ichiro 	DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __FUNCTION__));
    237  1.1   ichiro 
    238  1.1   ichiro 	mii->mii_media_status = IFM_AVALID;
    239  1.1   ichiro 	mii->mii_media_active = IFM_ETHER;
    240  1.1   ichiro 
    241  1.1   ichiro 	/*
    242  1.1   ichiro 	 * The link status bit is not exist in the BMSR register,
    243  1.1   ichiro 	 * so we need to read the MSR register to get link status.
    244  1.1   ichiro 	 */
    245  1.1   ichiro 	msr = PHY_READ(sc, URLPHY_MSR) | PHY_READ(sc, URLPHY_MSR);
    246  1.1   ichiro 	if (msr & URLPHY_MSR_LINK)
    247  1.1   ichiro 		mii->mii_media_status |= IFM_ACTIVE;
    248  1.1   ichiro 
    249  1.1   ichiro 	DPRINTF(("%s: %s: link %s\n", sc->mii_dev.dv_xname, __FUNCTION__,
    250  1.1   ichiro 		 mii->mii_media_status & IFM_ACTIVE ? "up" : "down"));
    251  1.1   ichiro 
    252  1.1   ichiro 	bmcr = PHY_READ(sc, MII_BMCR);
    253  1.1   ichiro 	if (bmcr & BMCR_AUTOEN) {
    254  1.1   ichiro 		bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    255  1.1   ichiro 		if ((bmsr & BMSR_ACOMP) == 0) {
    256  1.1   ichiro 			/* Erg, still trying, I guess... */
    257  1.1   ichiro 			mii->mii_media_active |= IFM_NONE;
    258  1.1   ichiro 			return;
    259  1.1   ichiro 		}
    260  1.1   ichiro 
    261  1.1   ichiro 		if (msr & URLPHY_MSR_SPEED_100)
    262  1.1   ichiro 			mii->mii_media_active |= IFM_100_TX;
    263  1.1   ichiro 		else
    264  1.1   ichiro 			mii->mii_media_active |= IFM_10_T;
    265  1.1   ichiro 		if (msr & URLPHY_MSR_DUPLEX)
    266  1.1   ichiro 			mii->mii_media_active |= IFM_FDX;
    267  1.1   ichiro 	} else
    268  1.1   ichiro 		mii->mii_media_active = ife->ifm_media;
    269  1.1   ichiro }
    270