Home | History | Annotate | Line # | Download | only in mii
atphy.c revision 1.18.14.1
      1  1.18.14.1  pgoyette /*	$NetBSD: atphy.c,v 1.18.14.1 2019/01/26 22:00:06 pgoyette 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.18.14.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: atphy.c,v 1.18.14.1 2019/01/26 22:00:06 pgoyette 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.1    cegger 	{ MII_OUI_ATHEROS,	MII_MODEL_ATHEROS_F1,
     95        1.1    cegger 	  MII_STR_ATHEROS_F1 },
     96        1.3    cegger 	{ MII_OUI_ATTANSIC,	MII_MODEL_ATTANSIC_L1,
     97        1.3    cegger 	  MII_STR_ATTANSIC_L1 },
     98       1.10  jmcneill 	{ MII_OUI_ATTANSIC,	MII_MODEL_ATTANSIC_L2,
     99       1.10  jmcneill 	  MII_STR_ATTANSIC_L2 },
    100        1.7      matt 	{ MII_OUI_ATTANSIC,	MII_MODEL_ATTANSIC_AR8021,
    101        1.7      matt 	  MII_STR_ATTANSIC_AR8021 },
    102       1.12      matt 	{ MII_OUI_ATTANSIC,	MII_MODEL_ATTANSIC_AR8035,
    103       1.12      matt 	  MII_STR_ATTANSIC_AR8035 },
    104        1.1    cegger 	{ 0,			0,
    105        1.1    cegger 	  NULL },
    106        1.1    cegger };
    107        1.1    cegger 
    108       1.11  jmcneill static bool
    109       1.11  jmcneill atphy_is_gige(const struct mii_phydesc *mpd)
    110       1.11  jmcneill {
    111       1.11  jmcneill 	switch (mpd->mpd_oui) {
    112       1.11  jmcneill 	case MII_OUI_ATTANSIC:
    113       1.11  jmcneill 		switch (mpd->mpd_model) {
    114       1.11  jmcneill 		case MII_MODEL_ATTANSIC_L2:
    115       1.11  jmcneill 			return false;
    116       1.11  jmcneill 		}
    117       1.11  jmcneill 	}
    118       1.11  jmcneill 
    119       1.11  jmcneill 	return true;
    120       1.11  jmcneill }
    121       1.11  jmcneill 
    122        1.1    cegger static int
    123        1.1    cegger atphy_match(device_t parent, cfdata_t match, void *aux)
    124        1.1    cegger {
    125        1.1    cegger 	struct mii_attach_args *ma = aux;
    126        1.1    cegger 
    127        1.1    cegger 	if (mii_phy_match(ma, etphys) != NULL)
    128        1.1    cegger 		return 10;
    129        1.1    cegger 
    130        1.1    cegger 	return 0;
    131        1.1    cegger }
    132        1.1    cegger 
    133        1.1    cegger void
    134        1.1    cegger atphy_attach(device_t parent, device_t self, void *aux)
    135        1.1    cegger {
    136        1.1    cegger 	struct mii_softc *sc = device_private(self);
    137        1.1    cegger 	struct mii_attach_args *ma = aux;
    138        1.1    cegger 	struct mii_data *mii = ma->mii_data;
    139        1.1    cegger 	const struct mii_phydesc *mpd;
    140        1.8    cegger 	uint16_t bmsr;
    141        1.1    cegger 
    142        1.1    cegger 	mpd = mii_phy_match(ma, etphys);
    143        1.1    cegger 	aprint_naive(": Media interface\n");
    144        1.1    cegger 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    145        1.1    cegger 
    146        1.1    cegger 	sc->mii_dev = self;
    147        1.1    cegger 	sc->mii_inst = mii->mii_instance;
    148        1.1    cegger 	sc->mii_phy = ma->mii_phyno;
    149       1.18   msaitoh 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    150       1.18   msaitoh 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    151       1.18   msaitoh 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    152        1.1    cegger 	sc->mii_funcs = &atphy_funcs;
    153        1.1    cegger 	sc->mii_pdata = mii;
    154        1.1    cegger 	sc->mii_flags = ma->mii_flags;
    155       1.11  jmcneill 	if (atphy_is_gige(mpd))
    156       1.11  jmcneill 		sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    157       1.11  jmcneill 	else
    158       1.11  jmcneill 		sc->mii_anegticks = MII_ANEGTICKS;
    159        1.1    cegger 
    160        1.1    cegger 	sc->mii_flags |= MIIF_NOLOOP;
    161        1.1    cegger 
    162        1.1    cegger 	PHY_RESET(sc);
    163        1.1    cegger 
    164  1.18.14.1  pgoyette 	PHY_READ(sc, MII_BMSR, &bmsr);
    165  1.18.14.1  pgoyette 	PHY_READ(sc, MII_BMSR, &bmsr);
    166        1.8    cegger 	sc->mii_capabilities = bmsr & ma->mii_capmask;
    167       1.11  jmcneill 	if (atphy_is_gige(mpd) && (sc->mii_capabilities & BMSR_EXTSTAT))
    168  1.18.14.1  pgoyette 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    169        1.1    cegger 
    170        1.4    cegger 	aprint_normal_dev(self, "");
    171        1.1    cegger 	mii_phy_add_media(sc);
    172        1.4    cegger 	aprint_normal("\n");
    173        1.1    cegger }
    174        1.1    cegger 
    175        1.1    cegger int
    176        1.1    cegger atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    177        1.1    cegger {
    178        1.1    cegger 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    179        1.1    cegger 	uint16_t anar, bmcr, bmsr;
    180        1.1    cegger 
    181        1.1    cegger 	switch (cmd) {
    182        1.1    cegger 	case MII_POLLSTAT:
    183        1.1    cegger 		/*
    184        1.1    cegger 		 * If we're not polling our PHY instance, just return.
    185        1.1    cegger 		 */
    186        1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    187        1.1    cegger 			return 0;
    188        1.1    cegger 		break;
    189        1.1    cegger 
    190        1.1    cegger 	case MII_MEDIACHG:
    191        1.1    cegger 		/*
    192        1.1    cegger 		 * If the media indicates a different PHY instance,
    193        1.1    cegger 		 * isolate ourselves.
    194        1.1    cegger 		 */
    195        1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    196  1.18.14.1  pgoyette 			PHY_READ(sc, MII_BMCR, &bmcr);
    197        1.1    cegger 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    198        1.1    cegger 			return 0;
    199        1.1    cegger 		}
    200        1.1    cegger 
    201        1.1    cegger 		/*
    202        1.1    cegger 		 * If the interface is not up, don't do anything.
    203        1.1    cegger 		 */
    204        1.1    cegger 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    205        1.1    cegger 			break;
    206        1.1    cegger 
    207        1.1    cegger 		bmcr = 0;
    208        1.1    cegger 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    209        1.1    cegger 		case IFM_AUTO:
    210        1.1    cegger 		case IFM_1000_T:
    211        1.1    cegger 			atphy_mii_phy_auto(sc);
    212        1.1    cegger 			goto done;
    213        1.1    cegger 		case IFM_100_TX:
    214        1.1    cegger 			bmcr = BMCR_S100;
    215        1.1    cegger 			break;
    216        1.1    cegger 		case IFM_10_T:
    217        1.1    cegger 			bmcr = BMCR_S10;
    218        1.1    cegger 			break;
    219        1.1    cegger 		case IFM_NONE:
    220  1.18.14.1  pgoyette 			PHY_READ(sc, MII_BMCR, &bmcr);
    221        1.1    cegger 			/*
    222        1.1    cegger 			 * XXX
    223        1.1    cegger 			 * Due to an unknown reason powering down PHY resulted
    224        1.6    cegger 			 * in unexpected results such as inaccessibility of
    225        1.1    cegger 			 * hardware of freshly rebooted system. Disable
    226        1.1    cegger 			 * powering down PHY until I got more information for
    227        1.1    cegger 			 * Attansic/Atheros PHY hardwares.
    228        1.1    cegger 			 */
    229        1.1    cegger 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    230        1.1    cegger 			goto done;
    231        1.1    cegger 		default:
    232        1.1    cegger 			return EINVAL;
    233        1.1    cegger 		}
    234        1.1    cegger 
    235       1.17   msaitoh 		anar = mii_anar(IFM_SUBTYPE(ife->ifm_media));
    236        1.1    cegger 		if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
    237        1.1    cegger 			bmcr |= BMCR_FDX;
    238        1.1    cegger 			/* Enable pause. */
    239        1.1    cegger 			if (sc->mii_flags & MIIF_DOPAUSE)
    240       1.15   msaitoh 				anar |= ANAR_PAUSE_TOWARDS;
    241        1.1    cegger 		}
    242        1.1    cegger 
    243        1.1    cegger 		if ((sc->mii_extcapabilities & (EXTSR_1000TFDX |
    244        1.1    cegger 		    EXTSR_1000THDX)) != 0)
    245        1.1    cegger 			PHY_WRITE(sc, MII_100T2CR, 0);
    246        1.1    cegger 		PHY_WRITE(sc, MII_ANAR, anar);
    247        1.1    cegger 
    248        1.1    cegger 		/*
    249        1.9    cegger 		 * Start autonegotiation.
    250        1.1    cegger 		 */
    251        1.8    cegger 		PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
    252        1.1    cegger done:
    253        1.1    cegger 		break;
    254        1.1    cegger 
    255        1.1    cegger 	case MII_TICK:
    256        1.1    cegger 		/*
    257        1.1    cegger 		 * If we're not currently selected, just return.
    258        1.1    cegger 		 */
    259        1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    260        1.1    cegger 			return 0;
    261        1.1    cegger 
    262        1.1    cegger 		/*
    263        1.1    cegger 		 * Is the interface even up?
    264        1.1    cegger 		 */
    265        1.1    cegger 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    266        1.1    cegger 			return 0;
    267        1.1    cegger 
    268        1.1    cegger 		/*
    269        1.1    cegger 		 * Only used for autonegotiation.
    270        1.1    cegger 		 */
    271       1.14   msaitoh 		if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
    272       1.14   msaitoh 		    (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
    273        1.1    cegger 			sc->mii_ticks = 0;
    274        1.1    cegger 			break;
    275        1.1    cegger 		}
    276        1.1    cegger 
    277        1.1    cegger 		/*
    278        1.1    cegger 		 * Check for link.
    279        1.1    cegger 		 * Read the status register twice; BMSR_LINK is latch-low.
    280        1.1    cegger 		 */
    281  1.18.14.1  pgoyette 		PHY_READ(sc, MII_BMSR, &bmsr);
    282  1.18.14.1  pgoyette 		PHY_READ(sc, MII_BMSR, &bmsr);
    283        1.1    cegger 		if (bmsr & BMSR_LINK) {
    284        1.1    cegger 			sc->mii_ticks = 0;
    285        1.1    cegger 			break;
    286        1.1    cegger 		}
    287        1.1    cegger 
    288        1.1    cegger 		/* Announce link loss right after it happens. */
    289        1.1    cegger 		if (sc->mii_ticks++ == 0)
    290        1.1    cegger 			break;
    291        1.1    cegger 
    292        1.1    cegger 		/*
    293        1.1    cegger 		 * Only retry autonegotiation every mii_anegticks seconds.
    294        1.1    cegger 		 */
    295        1.1    cegger 		if (sc->mii_ticks <= sc->mii_anegticks)
    296        1.1    cegger 			break;
    297        1.1    cegger 
    298        1.1    cegger 		atphy_mii_phy_auto(sc);
    299        1.1    cegger 		break;
    300        1.1    cegger 	}
    301        1.1    cegger 
    302        1.1    cegger 	/* Update the media status. */
    303        1.1    cegger 	mii_phy_status(sc);
    304        1.1    cegger 
    305        1.1    cegger 	/* Callback if something changed. */
    306        1.1    cegger 	mii_phy_update(sc, cmd);
    307        1.1    cegger 	return 0;
    308        1.1    cegger }
    309        1.1    cegger 
    310        1.1    cegger static void
    311        1.1    cegger atphy_status(struct mii_softc *sc)
    312        1.1    cegger {
    313        1.1    cegger 	struct mii_data *mii = sc->mii_pdata;
    314  1.18.14.1  pgoyette 	uint16_t bmsr, bmcr, gsr, ssr;
    315        1.1    cegger 
    316        1.1    cegger 	mii->mii_media_status = IFM_AVALID;
    317        1.1    cegger 	mii->mii_media_active = IFM_ETHER;
    318        1.1    cegger 
    319  1.18.14.1  pgoyette 	PHY_READ(sc, MII_BMSR, &bmsr);
    320  1.18.14.1  pgoyette 	PHY_READ(sc, MII_BMSR, &bmsr);
    321        1.1    cegger 	if (bmsr & BMSR_LINK)
    322        1.1    cegger 		mii->mii_media_status |= IFM_ACTIVE;
    323        1.1    cegger 
    324  1.18.14.1  pgoyette 	PHY_READ(sc, MII_BMCR, &bmcr);
    325        1.1    cegger 	if (bmcr & BMCR_ISO) {
    326        1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    327        1.1    cegger 		mii->mii_media_status = 0;
    328        1.1    cegger 		return;
    329        1.1    cegger 	}
    330        1.1    cegger 
    331        1.1    cegger 	if (bmcr & BMCR_LOOP)
    332        1.1    cegger 		mii->mii_media_active |= IFM_LOOP;
    333        1.1    cegger 
    334  1.18.14.1  pgoyette 	PHY_READ(sc, ATPHY_SSR, &ssr);
    335        1.1    cegger 	if (!(ssr & ATPHY_SSR_SPD_DPLX_RESOLVED)) {
    336        1.1    cegger 		/* Erg, still trying, I guess... */
    337        1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    338        1.1    cegger 		return;
    339        1.1    cegger 	}
    340        1.1    cegger 
    341        1.1    cegger 	switch (ssr & ATPHY_SSR_SPEED_MASK) {
    342        1.1    cegger 	case ATPHY_SSR_1000MBS:
    343        1.1    cegger 		mii->mii_media_active |= IFM_1000_T;
    344        1.1    cegger 		/*
    345        1.1    cegger 		 * atphy(4) has a valid link so reset mii_ticks.
    346        1.1    cegger 		 * Resetting mii_ticks is needed in order to
    347        1.1    cegger 		 * detect link loss after auto-negotiation.
    348        1.1    cegger 		 */
    349        1.1    cegger 		sc->mii_ticks = 0;
    350        1.1    cegger 		break;
    351        1.1    cegger 	case ATPHY_SSR_100MBS:
    352        1.1    cegger 		mii->mii_media_active |= IFM_100_TX;
    353        1.1    cegger 		sc->mii_ticks = 0;
    354        1.1    cegger 		break;
    355        1.1    cegger 	case ATPHY_SSR_10MBS:
    356        1.1    cegger 		mii->mii_media_active |= IFM_10_T;
    357        1.1    cegger 		sc->mii_ticks = 0;
    358        1.1    cegger 		break;
    359        1.1    cegger 	default:
    360        1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    361        1.1    cegger 		return;
    362        1.1    cegger 	}
    363        1.1    cegger 
    364        1.1    cegger 	if (ssr & ATPHY_SSR_DUPLEX)
    365        1.1    cegger 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
    366        1.1    cegger 	else
    367        1.1    cegger 		mii->mii_media_active |= IFM_HDX;
    368        1.1    cegger 
    369  1.18.14.1  pgoyette 	PHY_READ(sc, MII_100T2SR, &gsr);
    370        1.1    cegger 	if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&
    371        1.1    cegger 	    gsr & GTSR_MS_RES)
    372        1.1    cegger 		mii->mii_media_active |= IFM_ETH_MASTER;
    373        1.1    cegger }
    374        1.1    cegger 
    375        1.1    cegger static void
    376        1.1    cegger atphy_reset(struct mii_softc *sc)
    377        1.1    cegger {
    378  1.18.14.1  pgoyette 	uint16_t reg;
    379        1.1    cegger 	int i;
    380        1.1    cegger 
    381        1.1    cegger 	/* Take PHY out of power down mode. */
    382        1.1    cegger 	PHY_WRITE(sc, 29, 0x29);
    383        1.1    cegger 	PHY_WRITE(sc, 30, 0);
    384        1.1    cegger 
    385  1.18.14.1  pgoyette 	PHY_READ(sc, ATPHY_SCR, &reg);
    386        1.1    cegger 	/* Enable automatic crossover. */
    387        1.1    cegger 	reg |= ATPHY_SCR_AUTO_X_MODE;
    388        1.1    cegger 	/* Disable power down. */
    389        1.1    cegger 	reg &= ~ATPHY_SCR_MAC_PDOWN;
    390        1.1    cegger 	/* Enable CRS on Tx. */
    391        1.1    cegger 	reg |= ATPHY_SCR_ASSERT_CRS_ON_TX;
    392        1.1    cegger 	/* Auto correction for reversed cable polarity. */
    393        1.1    cegger 	reg |= ATPHY_SCR_POLARITY_REVERSAL;
    394        1.1    cegger 	PHY_WRITE(sc, ATPHY_SCR, reg);
    395        1.1    cegger 
    396        1.8    cegger 	atphy_mii_phy_auto(sc);
    397        1.8    cegger 
    398        1.1    cegger 	/* Workaround F1 bug to reset phy. */
    399  1.18.14.1  pgoyette 	PHY_READ(sc, MII_BMCR, &reg);
    400  1.18.14.1  pgoyette 	reg |= BMCR_RESET;
    401        1.8    cegger 	PHY_WRITE(sc, MII_BMCR, reg);
    402        1.1    cegger 
    403        1.1    cegger 	for (i = 0; i < 1000; i++) {
    404        1.1    cegger 		DELAY(1);
    405  1.18.14.1  pgoyette 		PHY_READ(sc, MII_BMCR, &reg);
    406  1.18.14.1  pgoyette 		if ((reg & BMCR_RESET) == 0)
    407        1.1    cegger 			break;
    408        1.1    cegger 	}
    409        1.1    cegger }
    410        1.1    cegger 
    411        1.1    cegger static int
    412        1.1    cegger atphy_mii_phy_auto(struct mii_softc *sc)
    413        1.1    cegger {
    414        1.1    cegger 	uint16_t anar;
    415        1.1    cegger 
    416       1.13   msaitoh 	sc->mii_ticks = 0;
    417        1.1    cegger 	anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
    418        1.1    cegger 	if (sc->mii_flags & MIIF_DOPAUSE)
    419       1.16   msaitoh 		anar |= ANAR_PAUSE_TOWARDS;
    420        1.1    cegger 	PHY_WRITE(sc, MII_ANAR, anar);
    421        1.1    cegger 	if (sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
    422        1.1    cegger 		PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX |
    423        1.1    cegger 		    GTCR_ADV_1000THDX);
    424        1.8    cegger 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    425        1.1    cegger 
    426        1.1    cegger 	return EJUSTRETURN;
    427        1.1    cegger }
    428