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