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