Home | History | Annotate | Line # | Download | only in mii
sqphy.c revision 1.1
      1 /*	$NetBSD: sqphy.c,v 1.1 1998/11/03 23:51:29 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Manuel Bouyer.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * driver for Seeq 80220/80221 and 80223 10/100 ethernet PHYs
     71  * datasheet from www.seeq.com
     72  */
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/kernel.h>
     77 #include <sys/device.h>
     78 #include <sys/malloc.h>
     79 #include <sys/socket.h>
     80 
     81 #include <net/if.h>
     82 #include <net/if_media.h>
     83 
     84 #include <dev/mii/mii.h>
     85 #include <dev/mii/miivar.h>
     86 #include <dev/mii/miidevs.h>
     87 
     88 #include <dev/mii/sqphyreg.h>
     89 
     90 struct sqphy_softc {
     91 	struct mii_softc sc_mii;		/* generic PHY */
     92 	int sc_capabilities;
     93 	int sc_ticks;
     94 	int sc_active;
     95 };
     96 
     97 int	sqphymatch __P((struct device *, struct cfdata *, void *));
     98 void	sqphyattach __P((struct device *, struct device *, void *));
     99 
    100 struct cfattach sqphy_ca = {
    101 	sizeof(struct sqphy_softc), sqphymatch, sqphyattach
    102 };
    103 
    104 #define	SQPHY_READ(sc, reg) \
    105     (*(sc)->sc_mii.mii_pdata->mii_readreg)((sc)->sc_mii.mii_dev.dv_parent, \
    106 	(sc)->sc_mii.mii_phy, (reg))
    107 
    108 #define	SQPHY_WRITE(sc, reg, val) \
    109     (*(sc)->sc_mii.mii_pdata->mii_writereg)((sc)->sc_mii.mii_dev.dv_parent, \
    110 	(sc)->sc_mii.mii_phy, (reg), (val))
    111 
    112 int	sqphy_service __P((struct mii_softc *, struct mii_data *, int));
    113 void	sqphy_reset __P((struct sqphy_softc *));
    114 void	sqphy_auto __P((struct sqphy_softc *));
    115 void	sqphy_status __P((struct sqphy_softc *));
    116 
    117 int
    118 sqphymatch(parent, match, aux)
    119 	struct device *parent;
    120 	struct cfdata *match;
    121 	void *aux;
    122 {
    123 	struct mii_attach_args *ma = aux;
    124 
    125 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_SEEQ &&
    126 	    MII_MODEL(ma->mii_id2) == MII_MODEL_SEEQ_80220)
    127 		return (1);
    128 
    129 	return (0);
    130 }
    131 
    132 void
    133 sqphyattach(parent, self, aux)
    134 	struct device *parent, *self;
    135 	void *aux;
    136 {
    137 	struct sqphy_softc *sc = (struct sqphy_softc *)self;
    138 	struct mii_attach_args *ma = aux;
    139 	struct mii_data *mii = ma->mii_data;
    140 
    141 	printf(": %s, rev. %d\n", MII_STR_SEEQ_80220,
    142 	    MII_REV(ma->mii_id2));
    143 
    144 	sc->sc_mii.mii_inst = mii->mii_instance;
    145 	sc->sc_mii.mii_phy = ma->mii_phyno;
    146 	sc->sc_mii.mii_service = sqphy_service;
    147 	sc->sc_mii.mii_pdata = mii;
    148 
    149 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    150 
    151 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
    152 	    BMCR_ISO);
    153 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->sc_mii.mii_inst),
    154 	    BMCR_LOOP|BMCR_S100);
    155 
    156 	sqphy_reset(sc);
    157 
    158 	sc->sc_capabilities = SQPHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    159 	printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
    160 	if ((sc->sc_capabilities & BMSR_MEDIAMASK) == 0)
    161 		printf("no media present");
    162 	else
    163 		mii_add_media(mii, sc->sc_capabilities, sc->sc_mii.mii_inst);
    164 	printf("\n");
    165 #undef ADD
    166 }
    167 
    168 int
    169 sqphy_service(self, mii, cmd)
    170 	struct mii_softc *self;
    171 	struct mii_data *mii;
    172 	int cmd;
    173 {
    174 	struct sqphy_softc *sc = (struct sqphy_softc *)self;
    175 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    176 	int reg;
    177 
    178 	switch (cmd) {
    179 	case MII_POLLSTAT:
    180 		/*
    181 		 * If we're not polling our PHY instance, just return.
    182 		 */
    183 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
    184 			return (0);
    185 		break;
    186 
    187 	case MII_MEDIACHG:
    188 		/*
    189 		 * If the media indicates a different PHY instance,
    190 		 * isolate ourselves.
    191 		 */
    192 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
    193 			reg = SQPHY_READ(sc, MII_BMCR);
    194 			SQPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    195 			return (0);
    196 		}
    197 
    198 		/*
    199 		 * If the interface is not up, don't do anything.
    200 		 */
    201 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    202 			break;
    203 
    204 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    205 		case IFM_AUTO:
    206 			/*
    207 			 * If we're already in auto mode, just return.
    208 			 */
    209 			if (SQPHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
    210 				return (0);
    211 			sqphy_auto(sc);
    212 			break;
    213 		case IFM_100_T4:
    214 			/*
    215 			 * XXX Not supported as a manual setting right now.
    216 			 */
    217 			return (EINVAL);
    218 		default:
    219 			/*
    220 			 * BMCR data is stored in the ifmedia entry.
    221 			 */
    222 			SQPHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
    223 			SQPHY_WRITE(sc, MII_BMCR, ife->ifm_data);
    224 		}
    225 		break;
    226 
    227 	case MII_TICK:
    228 		/*
    229 		 * If we're not currently selected, just return.
    230 		 */
    231 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
    232 			return (0);
    233 
    234 		/*
    235 		 * Only used for autonegotiation.
    236 		 */
    237 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    238 			return (0);
    239 
    240 		/*
    241 		 * Is the interface even up?
    242 		 */
    243 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    244 			return (0);
    245 
    246 		/*
    247 		 * Check to see if we have link.  If we do, we don't
    248 		 * need to restart the autonegotiation process.  Read
    249 		 * the BMSR twice in case it's latched.
    250 		 */
    251 		reg = SQPHY_READ(sc, MII_BMSR) | SQPHY_READ(sc, MII_BMSR);
    252 		if (reg & BMSR_LINK)
    253 			return (0);
    254 
    255 		/*
    256 		 * Only retry autonegotiation every 5 seconds.
    257 		 */
    258 		if (++sc->sc_ticks != 5)
    259 			return (0);
    260 
    261 		sc->sc_ticks = 0;
    262 		sqphy_reset(sc);
    263 		sqphy_auto(sc);
    264 		break;
    265 	}
    266 
    267 	/* Update the media status. */
    268 	sqphy_status(sc);
    269 
    270 	/* Callback if something changed. */
    271 	if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
    272 		(*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
    273 		sc->sc_active = mii->mii_media_active;
    274 	}
    275 	return (0);
    276 }
    277 
    278 void
    279 sqphy_status(sc)
    280 	struct sqphy_softc *sc;
    281 {
    282 	struct mii_data *mii = sc->sc_mii.mii_pdata;
    283 	int bmsr, bmcr, status;
    284 
    285 	mii->mii_media_status = IFM_AVALID;
    286 	mii->mii_media_active = IFM_ETHER;
    287 
    288 	bmsr = SQPHY_READ(sc, MII_BMSR) | SQPHY_READ(sc, MII_BMSR);
    289 	if (bmsr & BMSR_LINK)
    290 		mii->mii_media_status |= IFM_ACTIVE;
    291 
    292 	bmcr = SQPHY_READ(sc, MII_BMCR);
    293 	if (bmcr & BMCR_ISO) {
    294 		mii->mii_media_active |= IFM_NONE;
    295 		mii->mii_media_status = 0;
    296 		return;
    297 	}
    298 
    299 	if (bmcr & BMCR_LOOP)
    300 		mii->mii_media_active |= IFM_LOOP;
    301 
    302 	if ((bmcr & BMCR_AUTOEN) && (bmsr & BMSR_ACOMP) == 0) {
    303 		/* Erg, still trying, I guess... */
    304 		mii->mii_media_active |= IFM_NONE;
    305 		return;
    306 	}
    307 
    308 	status = SQPHY_READ(sc, MII_SQPHY_STATUS);
    309 	if (status & STATUS_SPD_DET)
    310 		mii->mii_media_active |= IFM_100_TX;
    311 	else
    312 		mii->mii_media_active |= IFM_10_T;
    313 	if (status & STATUS_DPLX_DET)
    314 		mii->mii_media_active |= IFM_FDX;
    315 }
    316 
    317 void
    318 sqphy_auto(sc)
    319 	struct sqphy_softc *sc;
    320 {
    321 	int bmsr, i;
    322 
    323 	SQPHY_WRITE(sc, MII_ANAR,
    324 	    BMSR_MEDIA_TO_ANAR(sc->sc_capabilities) | ANAR_CSMA);
    325 	SQPHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    326 
    327 	/* Wait 500ms for it to complete. */
    328 	for (i = 0; i < 500; i++) {
    329 		if ((bmsr = SQPHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    330 			return;
    331 		delay(1000);
    332 	}
    333 #if 0
    334 	if ((bmsr & BMSR_ACOMP) == 0)
    335 		printf("%s: autonegotiation failed to complete\n",
    336 		    sc->sc_mii.mii_dev.dv_xname);
    337 #endif
    338 }
    339 
    340 void
    341 sqphy_reset(sc)
    342 	struct sqphy_softc *sc;
    343 {
    344 	int reg, i;
    345 
    346 	SQPHY_WRITE(sc, MII_BMCR, BMCR_RESET|BMCR_ISO);
    347 
    348 	/* Wait 100ms for it to complete. */
    349 	for (i = 0; i < 100; i++) {
    350 		reg = SQPHY_READ(sc, MII_BMCR);
    351 		if ((reg & BMCR_RESET) == 0)
    352 			break;
    353 		delay(1000);
    354 	}
    355 
    356 	/* Make sure the PHY is isolated. */
    357 	if (sc->sc_mii.mii_inst != 0)
    358 		SQPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    359 }
    360