Home | History | Annotate | Line # | Download | only in mii
tlphy.c revision 1.33
      1 /*	$NetBSD: tlphy.c,v 1.33 2001/11/13 07:41:37 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000 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/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: tlphy.c,v 1.33 2001/11/13 07:41:37 lukem Exp $");
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/kernel.h>
     79 #include <sys/device.h>
     80 #include <sys/socket.h>
     81 #include <sys/errno.h>
     82 
     83 #include <machine/bus.h>
     84 
     85 #include <net/if.h>
     86 #include <net/if_media.h>
     87 
     88 #include <net/if_ether.h>
     89 
     90 #include <dev/mii/mii.h>
     91 #include <dev/mii/miivar.h>
     92 #include <dev/mii/miidevs.h>
     93 
     94 #include <dev/i2c/i2c_bus.h>
     95 
     96 #include <dev/mii/tlphyreg.h>
     97 #include <dev/mii/tlphyvar.h>
     98 
     99 /* ThunderLAN PHY can only be on a ThunderLAN */
    100 #include <dev/pci/if_tlvar.h>
    101 
    102 struct tlphy_softc {
    103 	struct mii_softc sc_mii;		/* generic PHY */
    104 	int sc_tlphycap;
    105 	int sc_need_acomp;
    106 };
    107 
    108 int	tlphymatch(struct device *, struct cfdata *, void *);
    109 void	tlphyattach(struct device *, struct device *, void *);
    110 
    111 struct cfattach tlphy_ca = {
    112 	sizeof(struct tlphy_softc), tlphymatch, tlphyattach, mii_phy_detach,
    113 	    mii_phy_activate
    114 };
    115 
    116 int	tlphy_service(struct mii_softc *, struct mii_data *, int);
    117 int	tlphy_auto(struct tlphy_softc *, int);
    118 void	tlphy_acomp(struct tlphy_softc *);
    119 void	tlphy_status(struct mii_softc *);
    120 
    121 const struct mii_phy_funcs tlphy_funcs = {
    122 	tlphy_service, tlphy_status, mii_phy_reset,
    123 };
    124 
    125 const struct mii_phydesc tlphys[] = {
    126 	{ MII_OUI_TI,		MII_MODEL_TI_TLAN10T,
    127 	  MII_STR_TI_TLAN10T },
    128 
    129 	{ 0,			0,
    130 	  NULL },
    131 };
    132 
    133 int
    134 tlphymatch(struct device *parent, struct cfdata *match, void *aux)
    135 {
    136 	struct mii_attach_args *ma = aux;
    137 
    138 	if (mii_phy_match(ma, tlphys) != NULL)
    139 		return (10);
    140 
    141 	return (0);
    142 }
    143 
    144 void
    145 tlphyattach(struct device *parent, struct device *self, void *aux)
    146 {
    147 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
    148 	struct tl_softc *tlsc = (struct tl_softc *)self->dv_parent;
    149 	struct mii_attach_args *ma = aux;
    150 	struct mii_data *mii = ma->mii_data;
    151 	const struct mii_phydesc *mpd;
    152 	const char *sep = "";
    153 
    154 	mpd = mii_phy_match(ma, tlphys);
    155 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    156 
    157 	sc->sc_mii.mii_inst = mii->mii_instance;
    158 	sc->sc_mii.mii_phy = ma->mii_phyno;
    159 	sc->sc_mii.mii_funcs = &tlphy_funcs;
    160 	sc->sc_mii.mii_pdata = mii;
    161 	sc->sc_mii.mii_flags = mii->mii_flags;
    162 	sc->sc_mii.mii_anegticks = 5;
    163 
    164 	PHY_RESET(&sc->sc_mii);
    165 
    166 	/*
    167 	 * Note that if we're on a device that also supports 100baseTX,
    168 	 * we are not going to want to use the built-in 10baseT port,
    169 	 * since there will be another PHY on the MII wired up to the
    170 	 * UTP connector.  The parent indicates this to us by specifying
    171 	 * the TLPHY_MEDIA_NO_10_T bit.
    172 	 */
    173 	sc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
    174 	if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
    175 		sc->sc_mii.mii_capabilities =
    176 		    PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
    177 	else
    178 		sc->sc_mii.mii_capabilities = 0;
    179 
    180 
    181 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    182 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    183 
    184 	printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
    185 	if (sc->sc_tlphycap) {
    186 		if (sc->sc_tlphycap & TLPHY_MEDIA_10_2) {
    187 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
    188 			    sc->sc_mii.mii_inst), 0);
    189 			PRINT("10base2");
    190 		} else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5) {
    191 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
    192 			    sc->sc_mii.mii_inst), 0);
    193 			PRINT("10base5");
    194 		}
    195 	}
    196 	if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
    197 		printf("%s", sep);
    198 		mii_phy_add_media(&sc->sc_mii);
    199 	} else if ((sc->sc_tlphycap &
    200 		    (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5)) == 0)
    201 		printf("no media present");
    202 	printf("\n");
    203 #undef ADD
    204 #undef PRINT
    205 }
    206 
    207 int
    208 tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd)
    209 {
    210 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
    211 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    212 	int reg;
    213 
    214 	if ((sc->sc_mii.mii_dev.dv_flags & DVF_ACTIVE) == 0)
    215 		return (ENXIO);
    216 
    217 	if ((sc->sc_mii.mii_flags & MIIF_DOINGAUTO) == 0 && sc->sc_need_acomp)
    218 		tlphy_acomp(sc);
    219 
    220 	switch (cmd) {
    221 	case MII_POLLSTAT:
    222 		/*
    223 		 * If we're not polling our PHY instance, just return.
    224 		 */
    225 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
    226 			return (0);
    227 		break;
    228 
    229 	case MII_MEDIACHG:
    230 		/*
    231 		 * If the media indicates a different PHY instance,
    232 		 * isolate ourselves.
    233 		 */
    234 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
    235 			reg = PHY_READ(&sc->sc_mii, MII_BMCR);
    236 			PHY_WRITE(&sc->sc_mii, 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(ife->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 			(void) tlphy_auto(sc, 1);
    254 			break;
    255 		case IFM_10_2:
    256 		case IFM_10_5:
    257 			PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
    258 			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
    259 			delay(100000);
    260 			break;
    261 		default:
    262 			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
    263 			delay(100000);
    264 			mii_phy_setmedia(&sc->sc_mii);
    265 		}
    266 		break;
    267 
    268 	case MII_TICK:
    269 		/*
    270 		 * If we're not currently selected, just return.
    271 		 */
    272 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
    273 			return (0);
    274 
    275 		/*
    276 		 * Only used for autonegotiation.
    277 		 */
    278 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    279 			return (0);
    280 
    281 		/*
    282 		 * Is the interface even up?
    283 		 */
    284 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    285 			return (0);
    286 
    287 		/*
    288 		 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
    289 		 */
    290 
    291 		if (mii_phy_tick(&sc->sc_mii) == EJUSTRETURN)
    292 			return (0);
    293 		break;
    294 
    295 	case MII_DOWN:
    296 		mii_phy_down(&sc->sc_mii);
    297 		return (0);
    298 	}
    299 
    300 	/* Update the media status. */
    301 	mii_phy_status(&sc->sc_mii);
    302 
    303 	/* Callback if something changed. */
    304 	mii_phy_update(&sc->sc_mii, cmd);
    305 	return (0);
    306 }
    307 
    308 void
    309 tlphy_status(struct mii_softc *physc)
    310 {
    311 	struct tlphy_softc *sc = (void *) physc;
    312 	struct mii_data *mii = sc->sc_mii.mii_pdata;
    313 	int bmsr, bmcr, tlctrl;
    314 
    315 	mii->mii_media_status = IFM_AVALID;
    316 	mii->mii_media_active = IFM_ETHER;
    317 
    318 	bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
    319 	if (bmcr & BMCR_ISO) {
    320 		mii->mii_media_active |= IFM_NONE;
    321 		mii->mii_media_status = 0;
    322 		return;
    323 	}
    324 
    325 	tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
    326 	if (tlctrl & CTRL_AUISEL) {
    327 		if (sc->sc_tlphycap & TLPHY_MEDIA_10_2)
    328 			mii->mii_media_active |= IFM_10_2;
    329 		else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5)
    330 			mii->mii_media_active |= IFM_10_5;
    331 		else
    332 			printf("%s: AUI selected with no matching media !\n",
    333 			    sc->sc_mii.mii_dev.dv_xname);
    334 		mii->mii_media_status |= IFM_ACTIVE;
    335 		return;
    336 	}
    337 
    338 	bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
    339 	    PHY_READ(&sc->sc_mii, MII_BMSR);
    340 	if (bmsr & BMSR_LINK)
    341 		mii->mii_media_status |= IFM_ACTIVE;
    342 
    343 	if (bmcr & BMCR_LOOP)
    344 		mii->mii_media_active |= IFM_LOOP;
    345 
    346 	/*
    347 	 * Grr, braindead ThunderLAN PHY doesn't have any way to
    348 	 * tell which media is actually active.  (Note it also
    349 	 * doesn't self-configure after autonegotiation.)  We
    350 	 * just have to report what's in the BMCR.
    351 	 */
    352 	if (bmcr & BMCR_FDX)
    353 		mii->mii_media_active |= IFM_FDX;
    354 	mii->mii_media_active |= IFM_10_T;
    355 }
    356 
    357 int
    358 tlphy_auto(struct tlphy_softc *sc, int waitfor)
    359 {
    360 	int error;
    361 
    362 	switch ((error = mii_phy_auto(&sc->sc_mii, waitfor))) {
    363 	case EIO:
    364 		/*
    365 		 * Just assume we're not in full-duplex mode.
    366 		 * XXX Check link and try AUI/BNC?
    367 		 */
    368 		PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
    369 		break;
    370 
    371 	case EJUSTRETURN:
    372 		/* Flag that we need to program when it completes. */
    373 		sc->sc_need_acomp = 1;
    374 		break;
    375 
    376 	default:
    377 		tlphy_acomp(sc);
    378 	}
    379 
    380 	return (error);
    381 }
    382 
    383 void
    384 tlphy_acomp(struct tlphy_softc *sc)
    385 {
    386 	int aner, anlpar;
    387 
    388 	sc->sc_need_acomp = 0;
    389 
    390 	/*
    391 	 * Grr, braindead ThunderLAN PHY doesn't self-configure
    392 	 * after autonegotiation.  We have to do it ourselves
    393 	 * based on the link partner status.
    394 	 */
    395 
    396 	aner = PHY_READ(&sc->sc_mii, MII_ANER);
    397 	if (aner & ANER_LPAN) {
    398 		anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
    399 		    PHY_READ(&sc->sc_mii, MII_ANAR);
    400 		if (anlpar & ANAR_10_FD) {
    401 			PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
    402 			return;
    403 		}
    404 	}
    405 	PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
    406 }
    407