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