Home | History | Annotate | Line # | Download | only in mii
tlphy.c revision 1.8
      1 /*	$NetBSD: tlphy.c,v 1.8 1998/08/12 20:46:47 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 Texas Instruments's ThunderLAN PHYs
     71  */
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/kernel.h>
     76 #include <sys/device.h>
     77 #include <sys/socket.h>
     78 
     79 #include <machine/bus.h>
     80 
     81 #include <net/if.h>
     82 #include <net/if_media.h>
     83 
     84 #include <net/if_ether.h>
     85 
     86 #include <dev/mii/mii.h>
     87 #include <dev/mii/miivar.h>
     88 #include <dev/mii/miidevs.h>
     89 
     90 #include <dev/i2c/i2c_bus.h>
     91 
     92 #include <dev/mii/tlphyreg.h>
     93 #include <dev/mii/tlphyvar.h>
     94 
     95 /* ThunderLAN PHY can only be on a ThunderLAN */
     96 #include <dev/pci/if_tlvar.h>
     97 
     98 struct tlphy_softc {
     99 	struct mii_softc sc_mii;		/* generic PHY */
    100 	int sc_capabilities;
    101 	int sc_ticks;
    102 	int sc_tlphycap;
    103 	int sc_active;
    104 };
    105 
    106 int	tlphymatch __P((struct device *, struct cfdata *, void *));
    107 void	tlphyattach __P((struct device *, struct device *, void *));
    108 
    109 struct cfattach tlphy_ca = {
    110 	sizeof(struct tlphy_softc), tlphymatch, tlphyattach
    111 };
    112 
    113 #define	TLPHY_READ(sc, reg) \
    114     (*(sc)->sc_mii.mii_pdata->mii_readreg)((sc)->sc_mii.mii_dev.dv_parent, \
    115 	(sc)->sc_mii.mii_phy, (reg))
    116 
    117 #define	TLPHY_WRITE(sc, reg, val) \
    118     (*(sc)->sc_mii.mii_pdata->mii_writereg)((sc)->sc_mii.mii_dev.dv_parent, \
    119 	(sc)->sc_mii.mii_phy, (reg), (val))
    120 
    121 int	tlphy_service __P((struct mii_softc *, struct mii_data *, int));
    122 void	tlphy_reset __P((struct tlphy_softc *));
    123 void	tlphy_auto __P((struct tlphy_softc *));
    124 void	tlphy_status __P((struct tlphy_softc *));
    125 
    126 int
    127 tlphymatch(parent, match, aux)
    128 	struct device *parent;
    129 	struct cfdata *match;
    130 	void *aux;
    131 {
    132 	struct mii_attach_args *ma = aux;
    133 
    134 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_TI &&
    135 	    MII_MODEL(ma->mii_id2) == MII_MODEL_TI_TLAN10T)
    136 		return (1);
    137 
    138 	return (0);
    139 }
    140 
    141 void
    142 tlphyattach(parent, self, aux)
    143 	struct device *parent, *self;
    144 	void *aux;
    145 {
    146 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
    147 	struct tl_softc *tlsc = (struct tl_softc *)self->dv_parent;
    148 	struct mii_attach_args *ma = aux;
    149 	struct mii_data *mii = ma->mii_data;
    150 	const char *sep = "";
    151 
    152 	printf(": %s, rev. %d\n", MII_STR_TI_TLAN10T,
    153 	    MII_REV(ma->mii_id2));
    154 
    155 	sc->sc_mii.mii_inst = mii->mii_instance;
    156 	sc->sc_mii.mii_phy = ma->mii_phyno;
    157 	sc->sc_mii.mii_service = tlphy_service;
    158 	sc->sc_mii.mii_pdata = mii;
    159 
    160 	tlphy_reset(sc);
    161 
    162 	/*
    163 	 * Note that if we're on a device that also supports 100baseTX,
    164 	 * we are not going to want to use the built-in 10baseT port,
    165 	 * since there will be another PHY on the MII wired up to the
    166 	 * UTP connector.  The parent indicates this to us by specifying
    167 	 * the TLPHY_MEDIA_NO_10_T bit.
    168 	 */
    169 	sc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
    170 	if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
    171 		sc->sc_capabilities =
    172 		    TLPHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    173 	else
    174 		sc->sc_capabilities = 0;
    175 
    176 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    177 
    178 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
    179 	    BMCR_ISO);
    180 
    181 	if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
    182 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
    183 		    sc->sc_mii.mii_inst), BMCR_LOOP);
    184 
    185 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    186 
    187 	printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
    188 	if (sc->sc_tlphycap) {
    189 		if (sc->sc_tlphycap == TLPHY_MEDIA_10_2) {
    190 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
    191 			    sc->sc_mii.mii_inst), 0);
    192 			PRINT("10base2/BNC");
    193 		} else {
    194 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
    195 			    sc->sc_mii.mii_inst), 0);
    196 			PRINT("10base5/AUI");
    197 		}
    198 	}
    199 	if (sc->sc_capabilities & BMSR_MEDIAMASK) {
    200 		printf(sep);
    201 		mii_add_media(mii, sc->sc_capabilities, sc->sc_mii.mii_inst);
    202 	} else if (sc->sc_tlphycap == 0)
    203 		printf("no media present");
    204 	printf("\n");
    205 #undef ADD
    206 #undef PRINT
    207 }
    208 
    209 int
    210 tlphy_service(self, mii, cmd)
    211 	struct mii_softc *self;
    212 	struct mii_data *mii;
    213 	int cmd;
    214 {
    215 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
    216 	int reg;
    217 
    218 	switch (cmd) {
    219 	case MII_POLLSTAT:
    220 		/*
    221 		 * If we're not polling our PHY instance, just return.
    222 		 */
    223 		if (IFM_INST(mii->mii_media.ifm_media) !=
    224 		    sc->sc_mii.mii_inst)
    225 			return (0);
    226 		break;
    227 
    228 	case MII_MEDIACHG:
    229 		/*
    230 		 * If the media indicates a different PHY instance,
    231 		 * isolate ourselves.
    232 		 */
    233 		if (IFM_INST(mii->mii_media.ifm_media) !=
    234 		    sc->sc_mii.mii_inst) {
    235 			reg = TLPHY_READ(sc, MII_BMCR);
    236 			TLPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    237 			return (0);
    238 		}
    239 
    240 		/*
    241 		 * If the interface is not up, don't do anything.
    242 		 */
    243 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    244 			break;
    245 
    246 		switch (IFM_SUBTYPE(mii->mii_media.ifm_media)) {
    247 		case IFM_AUTO:
    248 			/*
    249 			 * The ThunderLAN PHY doesn't self-configure after
    250 			 * an autonegotiation cycle, so there's no such
    251 			 * thing as "already in auto mode".
    252 			 */
    253 			tlphy_auto(sc);
    254 			break;
    255 		case IFM_10_2:
    256 		case IFM_10_5:
    257 			TLPHY_WRITE(sc, MII_BMCR, 0);
    258 			TLPHY_WRITE(sc, MII_TLPHY_CTRL, CTRL_AUISEL);
    259 			delay(100000);
    260 			break;
    261 		default:
    262 			TLPHY_WRITE(sc, MII_TLPHY_CTRL, 0);
    263 			delay(100000);
    264 			TLPHY_WRITE(sc, MII_ANAR,
    265 			    mii_anar(mii->mii_media.ifm_media));
    266 			TLPHY_WRITE(sc, MII_BMCR,
    267 			    mii->mii_media.ifm_cur->ifm_data);
    268 		}
    269 		break;
    270 
    271 	case MII_TICK:
    272 		/*
    273 		 * If we're not currently selected, just return.
    274 		 */
    275 		if (IFM_INST(mii->mii_media.ifm_media) !=
    276 		    sc->sc_mii.mii_inst)
    277 			return (0);
    278 
    279 		/*
    280 		 * Only used for autonegotiation.
    281 		 */
    282 		if (IFM_SUBTYPE(mii->mii_media.ifm_media) != IFM_AUTO)
    283 			return (0);
    284 
    285 		/*
    286 		 * Is the interface even up?
    287 		 */
    288 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    289 			return (0);
    290 
    291 		/*
    292 		 * Check to see if we have link.  If we do, we don't
    293 		 * need to restart the autonegotiation process.  Read
    294 		 * the BMSR twice in case it's latched.
    295 		 *
    296 		 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
    297 		 */
    298 		reg = TLPHY_READ(sc, MII_BMSR) | TLPHY_READ(sc, MII_BMSR);
    299 		if (reg & BMSR_LINK)
    300 			return (0);
    301 
    302 		/*
    303 		 * Only retry autonegotiation every 5 seconds.
    304 		 */
    305 		if (++sc->sc_ticks != 5)
    306 			return (0);
    307 
    308 		sc->sc_ticks = 0;
    309 		tlphy_reset(sc);
    310 		tlphy_auto(sc);
    311 		break;
    312 	}
    313 
    314 	/* Update the media status. */
    315 	tlphy_status(sc);
    316 
    317 	/* Callback if something changed. */
    318 	if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
    319 		(*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
    320 		sc->sc_active = mii->mii_media_active;
    321 	}
    322 	return (0);
    323 }
    324 
    325 void
    326 tlphy_status(sc)
    327 	struct tlphy_softc *sc;
    328 {
    329 	struct mii_data *mii = sc->sc_mii.mii_pdata;
    330 	int bmsr, bmcr, tlctrl;
    331 
    332 	mii->mii_media_status = IFM_AVALID;
    333 	mii->mii_media_active = IFM_ETHER;
    334 
    335 	bmsr = TLPHY_READ(sc, MII_BMSR) | TLPHY_READ(sc, MII_BMSR);
    336 	if (bmsr & BMSR_LINK)
    337 		mii->mii_media_status |= IFM_ACTIVE;
    338 
    339 	bmcr = TLPHY_READ(sc, MII_BMCR);
    340 	if (bmcr & BMCR_ISO) {
    341 		mii->mii_media_active |= IFM_NONE;
    342 		mii->mii_media_status = 0;
    343 		return;
    344 	}
    345 
    346 	tlctrl = TLPHY_READ(sc, MII_TLPHY_CTRL);
    347 	if (tlctrl & CTRL_AUISEL) {
    348 		if (sc->sc_tlphycap == TLPHY_MEDIA_10_2)
    349 			mii->mii_media_status |= IFM_10_2;
    350 		else
    351 			mii->mii_media_status |= IFM_10_5;
    352 		/* XXX Can we sense carrier here? */
    353 		mii->mii_media_status = 0;
    354 		return;
    355 	}
    356 
    357 	if (bmcr & BMCR_LOOP)
    358 		mii->mii_media_active |= IFM_LOOP;
    359 
    360 	/*
    361 	 * Grr, braindead ThunderLAN PHY doesn't have any way to
    362 	 * tell which media is actually active.  (Note it also
    363 	 * doesn't self-configure after autonegotiation.)  We
    364 	 * just have to report what's in the BMCR.
    365 	 */
    366 	if (bmcr & BMCR_FDX)
    367 		mii->mii_media_active |= IFM_FDX;
    368 	mii->mii_media_active |= IFM_10_T;
    369 }
    370 
    371 void
    372 tlphy_auto(sc)
    373 	struct tlphy_softc *sc;
    374 {
    375 	int aner, anlpar, bmsr, i;
    376 
    377 	TLPHY_WRITE(sc, MII_ANAR,
    378 	    BMSR_MEDIA_TO_ANAR(sc->sc_capabilities) | ANAR_CSMA);
    379 	TLPHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    380 
    381 	/* Wait 500ms for it to complete. */
    382 	for (i = 0; i < 500; i++) {
    383 		if ((bmsr = TLPHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    384 			return;
    385 		delay(1000);
    386 	}
    387 	if ((bmsr & BMSR_ACOMP) == 0) {
    388 #if 0
    389 		printf("%s: autonegotiation failed to complete\n",
    390 		    sc->sc_mii.mii_dev.dv_xname);
    391 #endif
    392 		goto default;
    393 	}
    394 
    395 	/*
    396 	 * Grr, braindead ThunderLAN PHY doesn't self-configure
    397 	 * after autonegotiation.  We have to do it ourselves
    398 	 * based on the link partner status.
    399 	 */
    400 
    401 	aner = TLPHY_READ(sc, MII_ANER);
    402 	if (aner & ANER_LPAN) {
    403 		anlpar = TLPHY_READ(sc, MII_ANLPAR) &
    404 		    TLPHY_READ(sc, MII_ANAR);
    405 		if (anlpar & ANAR_10_FD) {
    406 			TLPHY_WRITE(sc, MII_BMCR, BMCR_FDX);
    407 			return;
    408 		}
    409 	}
    410 
    411  default:
    412 	/*
    413 	 * Just assume we're not in full-duplex mode.
    414 	 * XXX Check link and try AUI/BNC?
    415 	 */
    416 	TLPHY_WRITE(sc, MII_BMCR, 0);
    417 }
    418 
    419 void
    420 tlphy_reset(sc)
    421 	struct tlphy_softc *sc;
    422 {
    423 	int reg, i;
    424 
    425 	TLPHY_WRITE(sc, MII_BMCR, BMCR_RESET|BMCR_ISO);
    426 
    427 	/* Wait 100ms for it to complete. */
    428 	for (i = 0; i < 100; i++) {
    429 		reg = TLPHY_READ(sc, MII_BMCR);
    430 		if ((reg & BMCR_RESET) == 0)
    431 			break;
    432 		delay(1000);
    433 	}
    434 
    435 	/* Make sure the PHY is isolated. */
    436 	if (sc->sc_mii.mii_inst != 0)
    437 		TLPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    438 }
    439