Home | History | Annotate | Line # | Download | only in mii
tlphy.c revision 1.57
      1 /*	$NetBSD: tlphy.c,v 1.57 2008/05/06 11:35:38 tsutsui 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by Manuel Bouyer.
     47  * 4. The name of the author may not be used to endorse or promote products
     48  *    derived from this software without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     53  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     55  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     59  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 /*
     63  * Driver for Texas Instruments's ThunderLAN PHYs
     64  */
     65 
     66 #include <sys/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: tlphy.c,v 1.57 2008/05/06 11:35:38 tsutsui Exp $");
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/kernel.h>
     72 #include <sys/device.h>
     73 #include <sys/socket.h>
     74 #include <sys/errno.h>
     75 
     76 #include <sys/bus.h>
     77 
     78 #include <net/if.h>
     79 #include <net/if_media.h>
     80 
     81 #include <net/if_ether.h>
     82 
     83 #include <dev/mii/mii.h>
     84 #include <dev/mii/miivar.h>
     85 #include <dev/mii/miidevs.h>
     86 
     87 #include <dev/mii/tlphyreg.h>
     88 #include <dev/mii/tlphyvar.h>
     89 
     90 /* ThunderLAN PHY can only be on a ThunderLAN */
     91 #include <dev/pci/if_tlvar.h>
     92 
     93 struct tlphy_softc {
     94 	struct mii_softc sc_mii;		/* generic PHY */
     95 	int sc_tlphycap;
     96 	int sc_need_acomp;
     97 };
     98 
     99 static int	tlphymatch(device_t, cfdata_t, void *);
    100 static void	tlphyattach(device_t, device_t, void *);
    101 
    102 CFATTACH_DECL_NEW(tlphy, sizeof(struct tlphy_softc),
    103     tlphymatch, tlphyattach, mii_phy_detach, mii_phy_activate);
    104 
    105 static int	tlphy_service(struct mii_softc *, struct mii_data *, int);
    106 static int	tlphy_auto(struct tlphy_softc *, int);
    107 static void	tlphy_acomp(struct tlphy_softc *);
    108 static void	tlphy_status(struct mii_softc *);
    109 
    110 static const struct mii_phy_funcs tlphy_funcs = {
    111 	tlphy_service, tlphy_status, mii_phy_reset,
    112 };
    113 
    114 static const struct mii_phydesc tlphys[] = {
    115 	{ MII_OUI_TI,		MII_MODEL_TI_TLAN10T,
    116 	  MII_STR_TI_TLAN10T },
    117 
    118 	{ 0,			0,
    119 	  NULL },
    120 };
    121 
    122 static int
    123 tlphymatch(device_t parent, cfdata_t match, void *aux)
    124 {
    125 	struct mii_attach_args *ma = aux;
    126 
    127 	if (mii_phy_match(ma, tlphys) != NULL)
    128 		return (10);
    129 
    130 	return (0);
    131 }
    132 
    133 static void
    134 tlphyattach(device_t parent, device_t self, void *aux)
    135 {
    136 	struct tlphy_softc *tsc = device_private(self);
    137 	struct mii_softc *sc = &tsc->sc_mii;
    138 	struct tl_softc *tlsc = device_private(device_parent(self));
    139 	struct mii_attach_args *ma = aux;
    140 	struct mii_data *mii = ma->mii_data;
    141 	const struct mii_phydesc *mpd;
    142 	const char *sep = "";
    143 
    144 	mpd = mii_phy_match(ma, tlphys);
    145 	aprint_naive(": Media interface\n");
    146 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    147 
    148 	sc->mii_dev = self;
    149 	sc->mii_inst = mii->mii_instance;
    150 	sc->mii_phy = ma->mii_phyno;
    151 	sc->mii_funcs = &tlphy_funcs;
    152 	sc->mii_pdata = mii;
    153 	sc->mii_flags = ma->mii_flags;
    154 	sc->mii_anegticks = MII_ANEGTICKS;
    155 
    156 	PHY_RESET(sc);
    157 
    158 	/*
    159 	 * Note that if we're on a device that also supports 100baseTX,
    160 	 * we are not going to want to use the built-in 10baseT port,
    161 	 * since there will be another PHY on the MII wired up to the
    162 	 * UTP connector.  The parent indicates this to us by specifying
    163 	 * the TLPHY_MEDIA_NO_10_T bit.
    164 	 */
    165 	tsc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
    166 	if ((tsc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
    167 		sc->mii_capabilities =
    168 		    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    169 	else
    170 		sc->mii_capabilities = 0;
    171 
    172 
    173 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    174 #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
    175 
    176 	aprint_normal_dev(self, "");
    177 	if (tsc->sc_tlphycap) {
    178 		if (tsc->sc_tlphycap & TLPHY_MEDIA_10_2) {
    179 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
    180 			    sc->mii_inst), 0);
    181 			PRINT("10base2");
    182 		} else if (tsc->sc_tlphycap & TLPHY_MEDIA_10_5) {
    183 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
    184 			    sc->mii_inst), 0);
    185 			PRINT("10base5");
    186 		}
    187 	}
    188 	if (sc->mii_capabilities & BMSR_MEDIAMASK) {
    189 		aprint_normal("%s", sep);
    190 		mii_phy_add_media(sc);
    191 	} else if ((tsc->sc_tlphycap &
    192 		    (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5)) == 0)
    193 		aprint_error("no media present");
    194 	aprint_normal("\n");
    195 #undef ADD
    196 #undef PRINT
    197 
    198 	if (!pmf_device_register(self, NULL, mii_phy_resume))
    199 		aprint_error_dev(self, "couldn't establish power handler\n");
    200 }
    201 
    202 static int
    203 tlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    204 {
    205 	struct tlphy_softc *tsc = (struct tlphy_softc *)sc;
    206 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    207 	int reg;
    208 
    209 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0 && tsc->sc_need_acomp)
    210 		tlphy_acomp(tsc);
    211 
    212 	switch (cmd) {
    213 	case MII_POLLSTAT:
    214 		/*
    215 		 * If we're not polling our PHY instance, just return.
    216 		 */
    217 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    218 			return (0);
    219 		break;
    220 
    221 	case MII_MEDIACHG:
    222 		/*
    223 		 * If the media indicates a different PHY instance,
    224 		 * isolate ourselves.
    225 		 */
    226 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    227 			reg = PHY_READ(sc, MII_BMCR);
    228 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    229 			return (0);
    230 		}
    231 
    232 		/*
    233 		 * If the interface is not up, don't do anything.
    234 		 */
    235 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    236 			break;
    237 
    238 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    239 		case IFM_AUTO:
    240 			/*
    241 			 * The ThunderLAN PHY doesn't self-configure after
    242 			 * an autonegotiation cycle, so there's no such
    243 			 * thing as "already in auto mode".
    244 			 */
    245 			(void) tlphy_auto(tsc, 1);
    246 			break;
    247 		case IFM_10_2:
    248 		case IFM_10_5:
    249 			PHY_WRITE(sc, MII_BMCR, 0);
    250 			PHY_WRITE(sc, MII_TLPHY_CTRL, CTRL_AUISEL);
    251 			delay(100000);
    252 			break;
    253 		default:
    254 			PHY_WRITE(sc, MII_TLPHY_CTRL, 0);
    255 			delay(100000);
    256 			mii_phy_setmedia(sc);
    257 		}
    258 		break;
    259 
    260 	case MII_TICK:
    261 		/*
    262 		 * If we're not currently selected, just return.
    263 		 */
    264 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    265 			return (0);
    266 
    267 		/*
    268 		 * Only used for autonegotiation.
    269 		 */
    270 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    271 			return (0);
    272 
    273 		/*
    274 		 * Is the interface even up?
    275 		 */
    276 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    277 			return (0);
    278 
    279 		/*
    280 		 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
    281 		 */
    282 
    283 		if (mii_phy_tick(sc) == EJUSTRETURN)
    284 			return (0);
    285 		break;
    286 
    287 	case MII_DOWN:
    288 		mii_phy_down(sc);
    289 		return (0);
    290 	}
    291 
    292 	/* Update the media status. */
    293 	mii_phy_status(sc);
    294 
    295 	/* Callback if something changed. */
    296 	mii_phy_update(sc, cmd);
    297 	return (0);
    298 }
    299 
    300 static void
    301 tlphy_status(struct mii_softc *sc)
    302 {
    303 	struct tlphy_softc *tsc = (struct tlphy_softc *)sc;
    304 	struct mii_data *mii = sc->mii_pdata;
    305 	int bmsr, bmcr, tlctrl;
    306 
    307 	mii->mii_media_status = IFM_AVALID;
    308 	mii->mii_media_active = IFM_ETHER;
    309 
    310 	bmcr = PHY_READ(sc, MII_BMCR);
    311 	if (bmcr & BMCR_ISO) {
    312 		mii->mii_media_active |= IFM_NONE;
    313 		mii->mii_media_status = 0;
    314 		return;
    315 	}
    316 
    317 	tlctrl = PHY_READ(sc, MII_TLPHY_CTRL);
    318 	if (tlctrl & CTRL_AUISEL) {
    319 		if (tsc->sc_tlphycap & TLPHY_MEDIA_10_2)
    320 			mii->mii_media_active |= IFM_10_2;
    321 		else if (tsc->sc_tlphycap & TLPHY_MEDIA_10_5)
    322 			mii->mii_media_active |= IFM_10_5;
    323 		else
    324 			printf("%s: AUI selected with no matching media !\n",
    325 			    device_xname(sc->mii_dev));
    326 		mii->mii_media_status |= IFM_ACTIVE;
    327 		return;
    328 	}
    329 
    330 	bmsr = PHY_READ(sc, MII_BMSR) |
    331 	    PHY_READ(sc, MII_BMSR);
    332 	if (bmsr & BMSR_LINK)
    333 		mii->mii_media_status |= IFM_ACTIVE;
    334 
    335 	if (bmcr & BMCR_LOOP)
    336 		mii->mii_media_active |= IFM_LOOP;
    337 
    338 	/*
    339 	 * Grr, braindead ThunderLAN PHY doesn't have any way to
    340 	 * tell which media is actually active.  (Note it also
    341 	 * doesn't self-configure after autonegotiation.)  We
    342 	 * just have to report what's in the BMCR.
    343 	 */
    344 	if (bmcr & BMCR_FDX)
    345 		mii->mii_media_active |= IFM_FDX;
    346 	mii->mii_media_active |= IFM_10_T;
    347 }
    348 
    349 static int
    350 tlphy_auto(struct tlphy_softc *tsc, int waitfor)
    351 {
    352 	struct mii_softc *sc = &tsc->sc_mii;
    353 	int error;
    354 
    355 	switch ((error = mii_phy_auto(sc, waitfor))) {
    356 	case EIO:
    357 		/*
    358 		 * Just assume we're not in full-duplex mode.
    359 		 * XXX Check link and try AUI/BNC?
    360 		 */
    361 		PHY_WRITE(sc, MII_BMCR, 0);
    362 		break;
    363 
    364 	case EJUSTRETURN:
    365 		/* Flag that we need to program when it completes. */
    366 		tsc->sc_need_acomp = 1;
    367 		break;
    368 
    369 	default:
    370 		tlphy_acomp(tsc);
    371 	}
    372 
    373 	return (error);
    374 }
    375 
    376 static void
    377 tlphy_acomp(struct tlphy_softc *tsc)
    378 {
    379 	struct mii_softc *sc = &tsc->sc_mii;
    380 	int aner, anlpar;
    381 
    382 	tsc->sc_need_acomp = 0;
    383 
    384 	/*
    385 	 * Grr, braindead ThunderLAN PHY doesn't self-configure
    386 	 * after autonegotiation.  We have to do it ourselves
    387 	 * based on the link partner status.
    388 	 */
    389 
    390 	aner = PHY_READ(sc, MII_ANER);
    391 	if (aner & ANER_LPAN) {
    392 		anlpar = PHY_READ(sc, MII_ANLPAR) &
    393 		    PHY_READ(sc, MII_ANAR);
    394 		if (anlpar & ANAR_10_FD) {
    395 			PHY_WRITE(sc, MII_BMCR, BMCR_FDX);
    396 			return;
    397 		}
    398 	}
    399 	PHY_WRITE(sc, MII_BMCR, 0);
    400 }
    401