Home | History | Annotate | Line # | Download | only in mii
atphy.c revision 1.22
      1  1.22   msaitoh /*	$NetBSD: atphy.c,v 1.22 2019/04/11 09:00:34 msaitoh Exp $ */
      2   1.1    cegger /*	$OpenBSD: atphy.c,v 1.1 2008/09/25 20:47:16 brad Exp $	*/
      3   1.1    cegger 
      4   1.1    cegger /*-
      5   1.1    cegger  * Copyright (c) 2008, Pyun YongHyeon <yongari (at) FreeBSD.org>
      6   1.1    cegger  * All rights reserved.
      7   1.1    cegger  *
      8   1.1    cegger  * Redistribution and use in source and binary forms, with or without
      9   1.1    cegger  * modification, are permitted provided that the following conditions
     10   1.1    cegger  * are met:
     11   1.1    cegger  * 1. Redistributions of source code must retain the above copyright
     12   1.1    cegger  *    notice unmodified, this list of conditions, and the following
     13   1.1    cegger  *    disclaimer.
     14   1.1    cegger  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1    cegger  *    notice, this list of conditions and the following disclaimer in the
     16   1.1    cegger  *    documentation and/or other materials provided with the distribution.
     17   1.1    cegger  *
     18   1.1    cegger  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19   1.1    cegger  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20   1.1    cegger  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21   1.1    cegger  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22   1.1    cegger  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1    cegger  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24   1.1    cegger  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1    cegger  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1    cegger  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1    cegger  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1    cegger  * SUCH DAMAGE.
     29   1.1    cegger  */
     30   1.1    cegger 
     31   1.1    cegger /*
     32   1.1    cegger  * Driver for the Attansic F1 10/100/1000 PHY.
     33   1.1    cegger  */
     34   1.1    cegger 
     35   1.2    cegger #include <sys/cdefs.h>
     36  1.22   msaitoh __KERNEL_RCSID(0, "$NetBSD: atphy.c,v 1.22 2019/04/11 09:00:34 msaitoh Exp $");
     37   1.2    cegger 
     38   1.1    cegger #include <sys/param.h>
     39   1.1    cegger #include <sys/systm.h>
     40   1.1    cegger #include <sys/kernel.h>
     41   1.1    cegger #include <sys/device.h>
     42   1.1    cegger #include <sys/socket.h>
     43   1.1    cegger 
     44   1.1    cegger #include <net/if.h>
     45   1.1    cegger #include <net/if_media.h>
     46   1.1    cegger 
     47   1.1    cegger #include <dev/mii/mii.h>
     48   1.1    cegger #include <dev/mii/miivar.h>
     49   1.1    cegger #include <dev/mii/miidevs.h>
     50   1.1    cegger 
     51   1.1    cegger /* Special Control Register */
     52   1.1    cegger #define ATPHY_SCR			0x10
     53   1.1    cegger #define ATPHY_SCR_JABBER_DISABLE	0x0001
     54   1.1    cegger #define ATPHY_SCR_POLARITY_REVERSAL	0x0002
     55   1.1    cegger #define ATPHY_SCR_SQE_TEST		0x0004
     56   1.1    cegger #define ATPHY_SCR_MAC_PDOWN		0x0008
     57   1.1    cegger #define ATPHY_SCR_CLK125_DISABLE	0x0010
     58   1.1    cegger #define ATPHY_SCR_MDI_MANUAL_MODE	0x0000
     59   1.1    cegger #define ATPHY_SCR_MDIX_MANUAL_MODE	0x0020
     60   1.1    cegger #define ATPHY_SCR_AUTO_X_1000T		0x0040
     61   1.1    cegger #define ATPHY_SCR_AUTO_X_MODE		0x0060
     62   1.1    cegger #define ATPHY_SCR_10BT_EXT_ENABLE	0x0080
     63   1.1    cegger #define ATPHY_SCR_MII_5BIT_ENABLE	0x0100
     64   1.1    cegger #define ATPHY_SCR_SCRAMBLER_DISABLE	0x0200
     65   1.1    cegger #define ATPHY_SCR_FORCE_LINK_GOOD	0x0400
     66   1.1    cegger #define ATPHY_SCR_ASSERT_CRS_ON_TX	0x0800
     67   1.1    cegger 
     68   1.1    cegger /* Special Status Register. */
     69   1.1    cegger #define ATPHY_SSR			0x11
     70   1.1    cegger #define ATPHY_SSR_SPD_DPLX_RESOLVED	0x0800
     71   1.1    cegger #define ATPHY_SSR_DUPLEX		0x2000
     72   1.1    cegger #define ATPHY_SSR_SPEED_MASK		0xC000
     73   1.1    cegger #define ATPHY_SSR_10MBS			0x0000
     74   1.1    cegger #define ATPHY_SSR_100MBS		0x4000
     75   1.1    cegger #define ATPHY_SSR_1000MBS		0x8000
     76   1.1    cegger 
     77   1.1    cegger static int atphy_match(device_t, cfdata_t, void *);
     78   1.1    cegger static void atphy_attach(device_t, device_t, void *);
     79   1.1    cegger 
     80   1.1    cegger static int atphy_service(struct mii_softc *, struct mii_data *, int);
     81   1.1    cegger static void atphy_reset(struct mii_softc *);
     82   1.1    cegger static void atphy_status(struct mii_softc *);
     83   1.1    cegger static int atphy_mii_phy_auto(struct mii_softc *);
     84  1.11  jmcneill static bool atphy_is_gige(const struct mii_phydesc *);
     85   1.1    cegger 
     86   1.1    cegger CFATTACH_DECL_NEW(atphy, sizeof(struct mii_softc),
     87   1.1    cegger 	atphy_match, atphy_attach, mii_phy_detach, mii_phy_activate);
     88   1.1    cegger 
     89   1.1    cegger const struct mii_phy_funcs atphy_funcs = {
     90   1.1    cegger         atphy_service, atphy_status, atphy_reset,
     91   1.1    cegger };
     92   1.1    cegger 
     93   1.1    cegger static const struct mii_phydesc etphys[] = {
     94  1.20  christos 	MII_PHY_DESC(ATHEROS, F1),
     95  1.20  christos 	MII_PHY_DESC(ATTANSIC, L1),
     96  1.20  christos 	MII_PHY_DESC(ATTANSIC, L2),
     97  1.20  christos 	MII_PHY_DESC(ATTANSIC, AR8021),
     98  1.20  christos 	MII_PHY_DESC(ATTANSIC, AR8035),
     99  1.20  christos 	MII_PHY_END,
    100   1.1    cegger };
    101   1.1    cegger 
    102  1.11  jmcneill static bool
    103  1.11  jmcneill atphy_is_gige(const struct mii_phydesc *mpd)
    104  1.11  jmcneill {
    105  1.11  jmcneill 	switch (mpd->mpd_oui) {
    106  1.11  jmcneill 	case MII_OUI_ATTANSIC:
    107  1.11  jmcneill 		switch (mpd->mpd_model) {
    108  1.11  jmcneill 		case MII_MODEL_ATTANSIC_L2:
    109  1.11  jmcneill 			return false;
    110  1.11  jmcneill 		}
    111  1.11  jmcneill 	}
    112  1.11  jmcneill 
    113  1.11  jmcneill 	return true;
    114  1.11  jmcneill }
    115  1.11  jmcneill 
    116   1.1    cegger static int
    117   1.1    cegger atphy_match(device_t parent, cfdata_t match, void *aux)
    118   1.1    cegger {
    119   1.1    cegger 	struct mii_attach_args *ma = aux;
    120   1.1    cegger 
    121   1.1    cegger 	if (mii_phy_match(ma, etphys) != NULL)
    122   1.1    cegger 		return 10;
    123   1.1    cegger 
    124   1.1    cegger 	return 0;
    125   1.1    cegger }
    126   1.1    cegger 
    127   1.1    cegger void
    128   1.1    cegger atphy_attach(device_t parent, device_t self, void *aux)
    129   1.1    cegger {
    130   1.1    cegger 	struct mii_softc *sc = device_private(self);
    131   1.1    cegger 	struct mii_attach_args *ma = aux;
    132   1.1    cegger 	struct mii_data *mii = ma->mii_data;
    133   1.1    cegger 	const struct mii_phydesc *mpd;
    134   1.8    cegger 	uint16_t bmsr;
    135   1.1    cegger 
    136   1.1    cegger 	mpd = mii_phy_match(ma, etphys);
    137   1.1    cegger 	aprint_naive(": Media interface\n");
    138   1.1    cegger 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    139   1.1    cegger 
    140   1.1    cegger 	sc->mii_dev = self;
    141   1.1    cegger 	sc->mii_inst = mii->mii_instance;
    142   1.1    cegger 	sc->mii_phy = ma->mii_phyno;
    143  1.18   msaitoh 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    144  1.18   msaitoh 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    145  1.18   msaitoh 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    146   1.1    cegger 	sc->mii_funcs = &atphy_funcs;
    147   1.1    cegger 	sc->mii_pdata = mii;
    148   1.1    cegger 	sc->mii_flags = ma->mii_flags;
    149  1.11  jmcneill 	if (atphy_is_gige(mpd))
    150  1.11  jmcneill 		sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    151  1.11  jmcneill 	else
    152  1.11  jmcneill 		sc->mii_anegticks = MII_ANEGTICKS;
    153   1.1    cegger 
    154   1.1    cegger 	sc->mii_flags |= MIIF_NOLOOP;
    155   1.1    cegger 
    156   1.1    cegger 	PHY_RESET(sc);
    157   1.1    cegger 
    158  1.19   msaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
    159  1.19   msaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
    160   1.8    cegger 	sc->mii_capabilities = bmsr & ma->mii_capmask;
    161  1.11  jmcneill 	if (atphy_is_gige(mpd) && (sc->mii_capabilities & BMSR_EXTSTAT))
    162  1.19   msaitoh 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    163   1.1    cegger 
    164   1.4    cegger 	aprint_normal_dev(self, "");
    165   1.1    cegger 	mii_phy_add_media(sc);
    166   1.4    cegger 	aprint_normal("\n");
    167   1.1    cegger }
    168   1.1    cegger 
    169   1.1    cegger int
    170   1.1    cegger atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    171   1.1    cegger {
    172   1.1    cegger 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    173   1.1    cegger 	uint16_t anar, bmcr, bmsr;
    174   1.1    cegger 
    175   1.1    cegger 	switch (cmd) {
    176   1.1    cegger 	case MII_POLLSTAT:
    177  1.21   msaitoh 		/* If we're not polling our PHY instance, just return. */
    178   1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    179   1.1    cegger 			return 0;
    180   1.1    cegger 		break;
    181   1.1    cegger 
    182   1.1    cegger 	case MII_MEDIACHG:
    183   1.1    cegger 		/*
    184   1.1    cegger 		 * If the media indicates a different PHY instance,
    185   1.1    cegger 		 * isolate ourselves.
    186   1.1    cegger 		 */
    187   1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    188  1.19   msaitoh 			PHY_READ(sc, MII_BMCR, &bmcr);
    189   1.1    cegger 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    190   1.1    cegger 			return 0;
    191   1.1    cegger 		}
    192   1.1    cegger 
    193  1.21   msaitoh 		/* If the interface is not up, don't do anything. */
    194   1.1    cegger 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    195   1.1    cegger 			break;
    196   1.1    cegger 
    197   1.1    cegger 		bmcr = 0;
    198   1.1    cegger 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    199   1.1    cegger 		case IFM_AUTO:
    200   1.1    cegger 		case IFM_1000_T:
    201   1.1    cegger 			atphy_mii_phy_auto(sc);
    202   1.1    cegger 			goto done;
    203   1.1    cegger 		case IFM_100_TX:
    204   1.1    cegger 			bmcr = BMCR_S100;
    205   1.1    cegger 			break;
    206   1.1    cegger 		case IFM_10_T:
    207   1.1    cegger 			bmcr = BMCR_S10;
    208   1.1    cegger 			break;
    209   1.1    cegger 		case IFM_NONE:
    210  1.19   msaitoh 			PHY_READ(sc, MII_BMCR, &bmcr);
    211   1.1    cegger 			/*
    212   1.1    cegger 			 * XXX
    213   1.1    cegger 			 * Due to an unknown reason powering down PHY resulted
    214   1.6    cegger 			 * in unexpected results such as inaccessibility of
    215   1.1    cegger 			 * hardware of freshly rebooted system. Disable
    216   1.1    cegger 			 * powering down PHY until I got more information for
    217   1.1    cegger 			 * Attansic/Atheros PHY hardwares.
    218   1.1    cegger 			 */
    219   1.1    cegger 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    220   1.1    cegger 			goto done;
    221   1.1    cegger 		default:
    222   1.1    cegger 			return EINVAL;
    223   1.1    cegger 		}
    224   1.1    cegger 
    225  1.17   msaitoh 		anar = mii_anar(IFM_SUBTYPE(ife->ifm_media));
    226  1.22   msaitoh 		if ((ife->ifm_media & IFM_FDX) != 0) {
    227   1.1    cegger 			bmcr |= BMCR_FDX;
    228   1.1    cegger 			/* Enable pause. */
    229   1.1    cegger 			if (sc->mii_flags & MIIF_DOPAUSE)
    230  1.15   msaitoh 				anar |= ANAR_PAUSE_TOWARDS;
    231   1.1    cegger 		}
    232   1.1    cegger 
    233   1.1    cegger 		if ((sc->mii_extcapabilities & (EXTSR_1000TFDX |
    234   1.1    cegger 		    EXTSR_1000THDX)) != 0)
    235   1.1    cegger 			PHY_WRITE(sc, MII_100T2CR, 0);
    236   1.1    cegger 		PHY_WRITE(sc, MII_ANAR, anar);
    237   1.1    cegger 
    238  1.21   msaitoh 		/* Start autonegotiation. */
    239   1.8    cegger 		PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
    240   1.1    cegger done:
    241   1.1    cegger 		break;
    242   1.1    cegger 
    243   1.1    cegger 	case MII_TICK:
    244  1.21   msaitoh 		/* If we're not currently selected, just return. */
    245   1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    246   1.1    cegger 			return 0;
    247   1.1    cegger 
    248  1.21   msaitoh 		/* Is the interface even up? */
    249   1.1    cegger 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    250   1.1    cegger 			return 0;
    251   1.1    cegger 
    252  1.21   msaitoh 		/* Only used for autonegotiation. */
    253  1.14   msaitoh 		if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
    254  1.14   msaitoh 		    (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
    255   1.1    cegger 			sc->mii_ticks = 0;
    256   1.1    cegger 			break;
    257   1.1    cegger 		}
    258   1.1    cegger 
    259   1.1    cegger 		/*
    260   1.1    cegger 		 * Check for link.
    261   1.1    cegger 		 * Read the status register twice; BMSR_LINK is latch-low.
    262   1.1    cegger 		 */
    263  1.19   msaitoh 		PHY_READ(sc, MII_BMSR, &bmsr);
    264  1.19   msaitoh 		PHY_READ(sc, MII_BMSR, &bmsr);
    265   1.1    cegger 		if (bmsr & BMSR_LINK) {
    266   1.1    cegger 			sc->mii_ticks = 0;
    267   1.1    cegger 			break;
    268   1.1    cegger 		}
    269   1.1    cegger 
    270   1.1    cegger 		/* Announce link loss right after it happens. */
    271   1.1    cegger 		if (sc->mii_ticks++ == 0)
    272   1.1    cegger 			break;
    273   1.1    cegger 
    274  1.21   msaitoh 		/* Only retry autonegotiation every mii_anegticks seconds. */
    275   1.1    cegger 		if (sc->mii_ticks <= sc->mii_anegticks)
    276   1.1    cegger 			break;
    277   1.1    cegger 
    278   1.1    cegger 		atphy_mii_phy_auto(sc);
    279   1.1    cegger 		break;
    280   1.1    cegger 	}
    281   1.1    cegger 
    282   1.1    cegger 	/* Update the media status. */
    283   1.1    cegger 	mii_phy_status(sc);
    284   1.1    cegger 
    285   1.1    cegger 	/* Callback if something changed. */
    286   1.1    cegger 	mii_phy_update(sc, cmd);
    287   1.1    cegger 	return 0;
    288   1.1    cegger }
    289   1.1    cegger 
    290   1.1    cegger static void
    291   1.1    cegger atphy_status(struct mii_softc *sc)
    292   1.1    cegger {
    293   1.1    cegger 	struct mii_data *mii = sc->mii_pdata;
    294  1.19   msaitoh 	uint16_t bmsr, bmcr, gsr, ssr;
    295   1.1    cegger 
    296   1.1    cegger 	mii->mii_media_status = IFM_AVALID;
    297   1.1    cegger 	mii->mii_media_active = IFM_ETHER;
    298   1.1    cegger 
    299  1.19   msaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
    300  1.19   msaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
    301   1.1    cegger 	if (bmsr & BMSR_LINK)
    302   1.1    cegger 		mii->mii_media_status |= IFM_ACTIVE;
    303   1.1    cegger 
    304  1.19   msaitoh 	PHY_READ(sc, MII_BMCR, &bmcr);
    305   1.1    cegger 	if (bmcr & BMCR_ISO) {
    306   1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    307   1.1    cegger 		mii->mii_media_status = 0;
    308   1.1    cegger 		return;
    309   1.1    cegger 	}
    310   1.1    cegger 
    311   1.1    cegger 	if (bmcr & BMCR_LOOP)
    312   1.1    cegger 		mii->mii_media_active |= IFM_LOOP;
    313   1.1    cegger 
    314  1.19   msaitoh 	PHY_READ(sc, ATPHY_SSR, &ssr);
    315   1.1    cegger 	if (!(ssr & ATPHY_SSR_SPD_DPLX_RESOLVED)) {
    316   1.1    cegger 		/* Erg, still trying, I guess... */
    317   1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    318   1.1    cegger 		return;
    319   1.1    cegger 	}
    320   1.1    cegger 
    321   1.1    cegger 	switch (ssr & ATPHY_SSR_SPEED_MASK) {
    322   1.1    cegger 	case ATPHY_SSR_1000MBS:
    323   1.1    cegger 		mii->mii_media_active |= IFM_1000_T;
    324   1.1    cegger 		/*
    325   1.1    cegger 		 * atphy(4) has a valid link so reset mii_ticks.
    326   1.1    cegger 		 * Resetting mii_ticks is needed in order to
    327   1.1    cegger 		 * detect link loss after auto-negotiation.
    328   1.1    cegger 		 */
    329   1.1    cegger 		sc->mii_ticks = 0;
    330   1.1    cegger 		break;
    331   1.1    cegger 	case ATPHY_SSR_100MBS:
    332   1.1    cegger 		mii->mii_media_active |= IFM_100_TX;
    333   1.1    cegger 		sc->mii_ticks = 0;
    334   1.1    cegger 		break;
    335   1.1    cegger 	case ATPHY_SSR_10MBS:
    336   1.1    cegger 		mii->mii_media_active |= IFM_10_T;
    337   1.1    cegger 		sc->mii_ticks = 0;
    338   1.1    cegger 		break;
    339   1.1    cegger 	default:
    340   1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    341   1.1    cegger 		return;
    342   1.1    cegger 	}
    343   1.1    cegger 
    344   1.1    cegger 	if (ssr & ATPHY_SSR_DUPLEX)
    345   1.1    cegger 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
    346   1.1    cegger 	else
    347   1.1    cegger 		mii->mii_media_active |= IFM_HDX;
    348   1.1    cegger 
    349  1.19   msaitoh 	PHY_READ(sc, MII_100T2SR, &gsr);
    350   1.1    cegger 	if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&
    351   1.1    cegger 	    gsr & GTSR_MS_RES)
    352   1.1    cegger 		mii->mii_media_active |= IFM_ETH_MASTER;
    353   1.1    cegger }
    354   1.1    cegger 
    355   1.1    cegger static void
    356   1.1    cegger atphy_reset(struct mii_softc *sc)
    357   1.1    cegger {
    358  1.19   msaitoh 	uint16_t reg;
    359   1.1    cegger 	int i;
    360   1.1    cegger 
    361   1.1    cegger 	/* Take PHY out of power down mode. */
    362   1.1    cegger 	PHY_WRITE(sc, 29, 0x29);
    363   1.1    cegger 	PHY_WRITE(sc, 30, 0);
    364   1.1    cegger 
    365  1.19   msaitoh 	PHY_READ(sc, ATPHY_SCR, &reg);
    366   1.1    cegger 	/* Enable automatic crossover. */
    367   1.1    cegger 	reg |= ATPHY_SCR_AUTO_X_MODE;
    368   1.1    cegger 	/* Disable power down. */
    369   1.1    cegger 	reg &= ~ATPHY_SCR_MAC_PDOWN;
    370   1.1    cegger 	/* Enable CRS on Tx. */
    371   1.1    cegger 	reg |= ATPHY_SCR_ASSERT_CRS_ON_TX;
    372   1.1    cegger 	/* Auto correction for reversed cable polarity. */
    373   1.1    cegger 	reg |= ATPHY_SCR_POLARITY_REVERSAL;
    374   1.1    cegger 	PHY_WRITE(sc, ATPHY_SCR, reg);
    375   1.1    cegger 
    376   1.8    cegger 	atphy_mii_phy_auto(sc);
    377   1.8    cegger 
    378   1.1    cegger 	/* Workaround F1 bug to reset phy. */
    379  1.19   msaitoh 	PHY_READ(sc, MII_BMCR, &reg);
    380  1.19   msaitoh 	reg |= BMCR_RESET;
    381   1.8    cegger 	PHY_WRITE(sc, MII_BMCR, reg);
    382   1.1    cegger 
    383   1.1    cegger 	for (i = 0; i < 1000; i++) {
    384   1.1    cegger 		DELAY(1);
    385  1.19   msaitoh 		PHY_READ(sc, MII_BMCR, &reg);
    386  1.19   msaitoh 		if ((reg & BMCR_RESET) == 0)
    387   1.1    cegger 			break;
    388   1.1    cegger 	}
    389   1.1    cegger }
    390   1.1    cegger 
    391   1.1    cegger static int
    392   1.1    cegger atphy_mii_phy_auto(struct mii_softc *sc)
    393   1.1    cegger {
    394   1.1    cegger 	uint16_t anar;
    395   1.1    cegger 
    396  1.13   msaitoh 	sc->mii_ticks = 0;
    397   1.1    cegger 	anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
    398   1.1    cegger 	if (sc->mii_flags & MIIF_DOPAUSE)
    399  1.16   msaitoh 		anar |= ANAR_PAUSE_TOWARDS;
    400   1.1    cegger 	PHY_WRITE(sc, MII_ANAR, anar);
    401   1.1    cegger 	if (sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
    402   1.1    cegger 		PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX |
    403   1.1    cegger 		    GTCR_ADV_1000THDX);
    404   1.8    cegger 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    405   1.1    cegger 
    406   1.1    cegger 	return EJUSTRETURN;
    407   1.1    cegger }
    408