Home | History | Annotate | Line # | Download | only in mii
tlphy.c revision 1.16
      1 /*	$NetBSD: tlphy.c,v 1.16 1998/11/05 00:19:32 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_tlphycap;
    101 };
    102 
    103 int	tlphymatch __P((struct device *, struct cfdata *, void *));
    104 void	tlphyattach __P((struct device *, struct device *, void *));
    105 
    106 struct cfattach tlphy_ca = {
    107 	sizeof(struct tlphy_softc), tlphymatch, tlphyattach
    108 };
    109 
    110 int	tlphy_service __P((struct mii_softc *, struct mii_data *, int));
    111 void	tlphy_auto __P((struct tlphy_softc *));
    112 void	tlphy_status __P((struct tlphy_softc *));
    113 
    114 int
    115 tlphymatch(parent, match, aux)
    116 	struct device *parent;
    117 	struct cfdata *match;
    118 	void *aux;
    119 {
    120 	struct mii_attach_args *ma = aux;
    121 
    122 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_TI &&
    123 	    MII_MODEL(ma->mii_id2) == MII_MODEL_TI_TLAN10T)
    124 		return (10);
    125 
    126 	return (0);
    127 }
    128 
    129 void
    130 tlphyattach(parent, self, aux)
    131 	struct device *parent, *self;
    132 	void *aux;
    133 {
    134 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
    135 	struct tl_softc *tlsc = (struct tl_softc *)self->dv_parent;
    136 	struct mii_attach_args *ma = aux;
    137 	struct mii_data *mii = ma->mii_data;
    138 	const char *sep = "";
    139 
    140 	printf(": %s, rev. %d\n", MII_STR_TI_TLAN10T,
    141 	    MII_REV(ma->mii_id2));
    142 
    143 	sc->sc_mii.mii_inst = mii->mii_instance;
    144 	sc->sc_mii.mii_phy = ma->mii_phyno;
    145 	sc->sc_mii.mii_service = tlphy_service;
    146 	sc->sc_mii.mii_pdata = mii;
    147 
    148 	mii_phy_reset(&sc->sc_mii);
    149 
    150 	/*
    151 	 * Note that if we're on a device that also supports 100baseTX,
    152 	 * we are not going to want to use the built-in 10baseT port,
    153 	 * since there will be another PHY on the MII wired up to the
    154 	 * UTP connector.  The parent indicates this to us by specifying
    155 	 * the TLPHY_MEDIA_NO_10_T bit.
    156 	 */
    157 	sc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
    158 	if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
    159 		sc->sc_mii.mii_capabilities =
    160 		    PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
    161 	else
    162 		sc->sc_mii.mii_capabilities = 0;
    163 
    164 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    165 
    166 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
    167 	    BMCR_ISO);
    168 
    169 	if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
    170 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
    171 		    sc->sc_mii.mii_inst), BMCR_LOOP);
    172 
    173 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    174 
    175 	printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
    176 	if (sc->sc_tlphycap) {
    177 		if (sc->sc_tlphycap & TLPHY_MEDIA_10_2) {
    178 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
    179 			    sc->sc_mii.mii_inst), 0);
    180 			PRINT("10base2/BNC");
    181 		} else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5) {
    182 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
    183 			    sc->sc_mii.mii_inst), 0);
    184 			PRINT("10base5/AUI");
    185 		}
    186 	}
    187 	if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
    188 		printf(sep);
    189 		mii_add_media(mii, sc->sc_mii.mii_capabilities,
    190 		    sc->sc_mii.mii_inst);
    191 	} else if ((sc->sc_tlphycap & (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5))
    192 	    == 0)
    193 		printf("no media present");
    194 	printf("\n");
    195 #undef ADD
    196 #undef PRINT
    197 }
    198 
    199 int
    200 tlphy_service(self, mii, cmd)
    201 	struct mii_softc *self;
    202 	struct mii_data *mii;
    203 	int cmd;
    204 {
    205 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
    206 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    207 	int reg;
    208 
    209 	switch (cmd) {
    210 	case MII_POLLSTAT:
    211 		/*
    212 		 * If we're not polling our PHY instance, just return.
    213 		 */
    214 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
    215 			return (0);
    216 		break;
    217 
    218 	case MII_MEDIACHG:
    219 		/*
    220 		 * If the media indicates a different PHY instance,
    221 		 * isolate ourselves.
    222 		 */
    223 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
    224 			reg = PHY_READ(&sc->sc_mii, MII_BMCR);
    225 			PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
    226 			return (0);
    227 		}
    228 
    229 		/*
    230 		 * If the interface is not up, don't do anything.
    231 		 */
    232 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    233 			break;
    234 
    235 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    236 		case IFM_AUTO:
    237 			/*
    238 			 * The ThunderLAN PHY doesn't self-configure after
    239 			 * an autonegotiation cycle, so there's no such
    240 			 * thing as "already in auto mode".
    241 			 */
    242 			tlphy_auto(sc);
    243 			break;
    244 		case IFM_10_2:
    245 		case IFM_10_5:
    246 			PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
    247 			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
    248 			delay(100000);
    249 			break;
    250 		default:
    251 			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
    252 			delay(100000);
    253 			PHY_WRITE(&sc->sc_mii, MII_ANAR,
    254 			    mii_anar(ife->ifm_media));
    255 			PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data);
    256 		}
    257 		break;
    258 
    259 	case MII_TICK:
    260 		/*
    261 		 * If we're not currently selected, just return.
    262 		 */
    263 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
    264 			return (0);
    265 
    266 		/*
    267 		 * Only used for autonegotiation.
    268 		 */
    269 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    270 			return (0);
    271 
    272 		/*
    273 		 * Is the interface even up?
    274 		 */
    275 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    276 			return (0);
    277 
    278 		/*
    279 		 * Check to see if we have link.  If we do, we don't
    280 		 * need to restart the autonegotiation process.  Read
    281 		 * the BMSR twice in case it's latched.
    282 		 *
    283 		 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
    284 		 */
    285 		reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
    286 		    PHY_READ(&sc->sc_mii, MII_BMSR);
    287 		if (reg & BMSR_LINK)
    288 			return (0);
    289 
    290 		/*
    291 		 * Only retry autonegotiation every 5 seconds.
    292 		 */
    293 		if (++sc->sc_mii.mii_ticks != 5)
    294 			return (0);
    295 
    296 		sc->sc_mii.mii_ticks = 0;
    297 		mii_phy_reset(&sc->sc_mii);
    298 		tlphy_auto(sc);
    299 		break;
    300 	}
    301 
    302 	/* Update the media status. */
    303 	tlphy_status(sc);
    304 
    305 	/* Callback if something changed. */
    306 	if (sc->sc_mii.mii_active != mii->mii_media_active ||
    307 	    cmd == MII_MEDIACHG) {
    308 		(*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
    309 		sc->sc_mii.mii_active = mii->mii_media_active;
    310 	}
    311 	return (0);
    312 }
    313 
    314 void
    315 tlphy_status(sc)
    316 	struct tlphy_softc *sc;
    317 {
    318 	struct mii_data *mii = sc->sc_mii.mii_pdata;
    319 	struct tl_softc *tlsc = (struct tl_softc *)sc->sc_mii.mii_dev.dv_parent;
    320 	int bmsr, bmcr, tlctrl;
    321 
    322 	mii->mii_media_status = IFM_AVALID;
    323 	mii->mii_media_active = IFM_ETHER;
    324 
    325 	bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
    326 	if (bmcr & BMCR_ISO) {
    327 		mii->mii_media_active |= IFM_NONE;
    328 		mii->mii_media_status = 0;
    329 		return;
    330 	}
    331 
    332 	tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
    333 	if (tlctrl & CTRL_AUISEL) {
    334 		if (sc->sc_tlphycap & TLPHY_MEDIA_10_2)
    335 			mii->mii_media_active |= IFM_10_2;
    336 		else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5)
    337 			mii->mii_media_active |= IFM_10_5;
    338 		else
    339 			printf("%s: AUI selected with no matching media !\n",
    340 			    sc->sc_mii.mii_dev.dv_xname);
    341 		if (tlsc->tl_flags & TL_IFACT)
    342 			mii->mii_media_status |= IFM_ACTIVE;
    343 		return;
    344 	}
    345 
    346 	bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
    347 	    PHY_READ(&sc->sc_mii, MII_BMSR);
    348 	if (bmsr & BMSR_LINK)
    349 		mii->mii_media_status |= IFM_ACTIVE;
    350 
    351 	if (bmcr & BMCR_LOOP)
    352 		mii->mii_media_active |= IFM_LOOP;
    353 
    354 	/*
    355 	 * Grr, braindead ThunderLAN PHY doesn't have any way to
    356 	 * tell which media is actually active.  (Note it also
    357 	 * doesn't self-configure after autonegotiation.)  We
    358 	 * just have to report what's in the BMCR.
    359 	 */
    360 	if (bmcr & BMCR_FDX)
    361 		mii->mii_media_active |= IFM_FDX;
    362 	mii->mii_media_active |= IFM_10_T;
    363 }
    364 
    365 void
    366 tlphy_auto(sc)
    367 	struct tlphy_softc *sc;
    368 {
    369 	int aner, anlpar;
    370 
    371 	if (mii_phy_auto(&sc->sc_mii) == 0)
    372 		goto dflt;
    373 
    374 	/*
    375 	 * Grr, braindead ThunderLAN PHY doesn't self-configure
    376 	 * after autonegotiation.  We have to do it ourselves
    377 	 * based on the link partner status.
    378 	 */
    379 
    380 	aner = PHY_READ(&sc->sc_mii, MII_ANER);
    381 	if (aner & ANER_LPAN) {
    382 		anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
    383 		    PHY_READ(&sc->sc_mii, MII_ANAR);
    384 		if (anlpar & ANAR_10_FD) {
    385 			PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
    386 			return;
    387 		}
    388 	}
    389 
    390  dflt:
    391 	/*
    392 	 * Just assume we're not in full-duplex mode.
    393 	 * XXX Check link and try AUI/BNC?
    394 	 */
    395 	PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
    396 }
    397