Home | History | Annotate | Line # | Download | only in mii
      1  1.33   thorpej /*	$NetBSD: atphy.c,v 1.33 2025/10/04 04:14:56 thorpej 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.33   thorpej __KERNEL_RCSID(0, "$NetBSD: atphy.c,v 1.33 2025/10/04 04:14:56 thorpej 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.24   hkenken #define ATPHY_DEBUG_PORT_ADDR		0x1d
     78  1.24   hkenken #define ATPHY_DEBUG_PORT_DATA		0x1e
     79  1.24   hkenken #define ATPHY_RGMII_RX_CLK_DLY		__BIT(15)
     80  1.24   hkenken #define ATPHY_RGMII_TX_CLK_DLY		__BIT(8)
     81  1.24   hkenken 
     82   1.1    cegger static int atphy_match(device_t, cfdata_t, void *);
     83   1.1    cegger static void atphy_attach(device_t, device_t, void *);
     84   1.1    cegger 
     85   1.1    cegger static int atphy_service(struct mii_softc *, struct mii_data *, int);
     86   1.1    cegger static void atphy_reset(struct mii_softc *);
     87   1.1    cegger static void atphy_status(struct mii_softc *);
     88   1.1    cegger static int atphy_mii_phy_auto(struct mii_softc *);
     89  1.11  jmcneill static bool atphy_is_gige(const struct mii_phydesc *);
     90   1.1    cegger 
     91  1.24   hkenken struct atphy_softc {
     92  1.24   hkenken 	struct mii_softc mii_sc;
     93  1.24   hkenken 	int mii_clk_25m;
     94  1.24   hkenken 	bool rgmii_tx_internal_delay;
     95  1.24   hkenken 	bool rgmii_rx_internal_delay;
     96  1.24   hkenken };
     97  1.24   hkenken 
     98  1.24   hkenken CFATTACH_DECL_NEW(atphy, sizeof(struct atphy_softc),
     99   1.1    cegger 	atphy_match, atphy_attach, mii_phy_detach, mii_phy_activate);
    100   1.1    cegger 
    101   1.1    cegger const struct mii_phy_funcs atphy_funcs = {
    102  1.27   msaitoh 	atphy_service, atphy_status, atphy_reset,
    103   1.1    cegger };
    104   1.1    cegger 
    105  1.23   msaitoh static const struct mii_phydesc atphys[] = {
    106  1.20  christos 	MII_PHY_DESC(ATTANSIC, L1),
    107  1.20  christos 	MII_PHY_DESC(ATTANSIC, L2),
    108  1.20  christos 	MII_PHY_DESC(ATTANSIC, AR8021),
    109  1.20  christos 	MII_PHY_DESC(ATTANSIC, AR8035),
    110  1.20  christos 	MII_PHY_END,
    111   1.1    cegger };
    112   1.1    cegger 
    113  1.24   hkenken static void
    114  1.24   hkenken atphy_clk_25m(struct atphy_softc *asc)
    115  1.24   hkenken {
    116  1.24   hkenken 	struct mii_softc *sc = &asc->mii_sc;
    117  1.24   hkenken 	struct {
    118  1.24   hkenken 		uint32_t hz;
    119  1.24   hkenken 		uint16_t data;
    120  1.24   hkenken 	} select_clk[] = {
    121  1.24   hkenken 		{  25000000, 0x0 },
    122  1.24   hkenken 		{  50000000, 0x1 },
    123  1.24   hkenken 		{  62500000, 0x2 },
    124  1.24   hkenken 		{ 125000000, 0x3 }
    125  1.24   hkenken 	};
    126  1.24   hkenken 	uint16_t data = 0;
    127  1.24   hkenken 	uint16_t reg = 0;
    128  1.24   hkenken 
    129  1.24   hkenken 	for (int i = 0; i < __arraycount(select_clk); i++) {
    130  1.24   hkenken 		if (asc->mii_clk_25m <= select_clk[i].hz)
    131  1.24   hkenken 			data = select_clk[i].data;
    132  1.24   hkenken 	}
    133  1.24   hkenken 
    134  1.24   hkenken 	PHY_WRITE(sc, 0x0d, 0x0007);
    135  1.24   hkenken 	PHY_WRITE(sc, 0x0e, 0x8016);
    136  1.24   hkenken 	PHY_WRITE(sc, 0x0d, 0x4007);
    137  1.24   hkenken 	PHY_READ(sc, 0x0e, &reg);
    138  1.24   hkenken 	PHY_WRITE(sc, 0x0e, reg | __SHIFTIN(data, __BITS(4, 3)));
    139  1.24   hkenken }
    140  1.24   hkenken 
    141  1.24   hkenken 
    142  1.11  jmcneill static bool
    143  1.11  jmcneill atphy_is_gige(const struct mii_phydesc *mpd)
    144  1.11  jmcneill {
    145  1.11  jmcneill 	switch (mpd->mpd_oui) {
    146  1.11  jmcneill 	case MII_OUI_ATTANSIC:
    147  1.11  jmcneill 		switch (mpd->mpd_model) {
    148  1.11  jmcneill 		case MII_MODEL_ATTANSIC_L2:
    149  1.11  jmcneill 			return false;
    150  1.11  jmcneill 		}
    151  1.11  jmcneill 	}
    152  1.11  jmcneill 
    153  1.11  jmcneill 	return true;
    154  1.11  jmcneill }
    155  1.11  jmcneill 
    156   1.1    cegger static int
    157   1.1    cegger atphy_match(device_t parent, cfdata_t match, void *aux)
    158   1.1    cegger {
    159   1.1    cegger 	struct mii_attach_args *ma = aux;
    160   1.1    cegger 
    161  1.23   msaitoh 	if (mii_phy_match(ma, atphys) != NULL)
    162   1.1    cegger 		return 10;
    163   1.1    cegger 
    164   1.1    cegger 	return 0;
    165   1.1    cegger }
    166   1.1    cegger 
    167   1.1    cegger void
    168   1.1    cegger atphy_attach(device_t parent, device_t self, void *aux)
    169   1.1    cegger {
    170  1.24   hkenken 	struct atphy_softc *asc = device_private(self);
    171  1.24   hkenken 	prop_dictionary_t prop = device_properties(self);
    172  1.24   hkenken 	struct mii_softc *sc = &asc->mii_sc;
    173   1.1    cegger 	struct mii_attach_args *ma = aux;
    174   1.1    cegger 	struct mii_data *mii = ma->mii_data;
    175   1.1    cegger 	const struct mii_phydesc *mpd;
    176   1.8    cegger 	uint16_t bmsr;
    177   1.1    cegger 
    178  1.23   msaitoh 	mpd = mii_phy_match(ma, atphys);
    179   1.1    cegger 	aprint_naive(": Media interface\n");
    180   1.1    cegger 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    181   1.1    cegger 
    182   1.1    cegger 	sc->mii_dev = self;
    183   1.1    cegger 	sc->mii_inst = mii->mii_instance;
    184   1.1    cegger 	sc->mii_phy = ma->mii_phyno;
    185  1.18   msaitoh 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    186  1.18   msaitoh 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    187  1.18   msaitoh 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    188   1.1    cegger 	sc->mii_funcs = &atphy_funcs;
    189   1.1    cegger 	sc->mii_pdata = mii;
    190   1.1    cegger 	sc->mii_flags = ma->mii_flags;
    191   1.1    cegger 	sc->mii_flags |= MIIF_NOLOOP;
    192   1.1    cegger 
    193  1.33   thorpej 	asc->rgmii_tx_internal_delay = device_getprop_bool(parent,
    194  1.33   thorpej 	    "tx_internal_delay");
    195  1.33   thorpej 	asc->rgmii_rx_internal_delay = device_getprop_bool(parent,
    196  1.33   thorpej 	    "rx_internal_delay");
    197  1.24   hkenken 
    198  1.24   hkenken 	prop_dictionary_get_uint32(prop, "clk_25m", &asc->mii_clk_25m);
    199  1.24   hkenken 	if (asc->mii_clk_25m != 0)
    200  1.24   hkenken 		atphy_clk_25m(asc);
    201  1.24   hkenken 
    202  1.30   thorpej 	mii_lock(mii);
    203  1.30   thorpej 
    204   1.1    cegger 	PHY_RESET(sc);
    205   1.1    cegger 
    206  1.19   msaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
    207  1.19   msaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
    208   1.8    cegger 	sc->mii_capabilities = bmsr & ma->mii_capmask;
    209  1.11  jmcneill 	if (atphy_is_gige(mpd) && (sc->mii_capabilities & BMSR_EXTSTAT))
    210  1.19   msaitoh 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    211   1.1    cegger 
    212  1.30   thorpej 	mii_unlock(mii);
    213  1.30   thorpej 
    214   1.1    cegger 	mii_phy_add_media(sc);
    215   1.1    cegger }
    216   1.1    cegger 
    217   1.1    cegger int
    218   1.1    cegger atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    219   1.1    cegger {
    220   1.1    cegger 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    221   1.1    cegger 	uint16_t anar, bmcr, bmsr;
    222   1.1    cegger 
    223  1.30   thorpej 	KASSERT(mii_locked(mii));
    224  1.30   thorpej 
    225   1.1    cegger 	switch (cmd) {
    226   1.1    cegger 	case MII_POLLSTAT:
    227  1.21   msaitoh 		/* If we're not polling our PHY instance, just return. */
    228   1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    229   1.1    cegger 			return 0;
    230   1.1    cegger 		break;
    231   1.1    cegger 
    232   1.1    cegger 	case MII_MEDIACHG:
    233   1.1    cegger 		/*
    234   1.1    cegger 		 * If the media indicates a different PHY instance,
    235   1.1    cegger 		 * isolate ourselves.
    236   1.1    cegger 		 */
    237   1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    238  1.19   msaitoh 			PHY_READ(sc, MII_BMCR, &bmcr);
    239   1.1    cegger 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    240   1.1    cegger 			return 0;
    241   1.1    cegger 		}
    242   1.1    cegger 
    243  1.21   msaitoh 		/* If the interface is not up, don't do anything. */
    244   1.1    cegger 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    245   1.1    cegger 			break;
    246   1.1    cegger 
    247   1.1    cegger 		bmcr = 0;
    248   1.1    cegger 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    249   1.1    cegger 		case IFM_AUTO:
    250   1.1    cegger 		case IFM_1000_T:
    251   1.1    cegger 			atphy_mii_phy_auto(sc);
    252   1.1    cegger 			goto done;
    253   1.1    cegger 		case IFM_100_TX:
    254   1.1    cegger 			bmcr = BMCR_S100;
    255   1.1    cegger 			break;
    256   1.1    cegger 		case IFM_10_T:
    257   1.1    cegger 			bmcr = BMCR_S10;
    258   1.1    cegger 			break;
    259   1.1    cegger 		case IFM_NONE:
    260  1.19   msaitoh 			PHY_READ(sc, MII_BMCR, &bmcr);
    261   1.1    cegger 			/*
    262   1.1    cegger 			 * XXX
    263   1.1    cegger 			 * Due to an unknown reason powering down PHY resulted
    264   1.6    cegger 			 * in unexpected results such as inaccessibility of
    265   1.1    cegger 			 * hardware of freshly rebooted system. Disable
    266   1.1    cegger 			 * powering down PHY until I got more information for
    267  1.32    andvar 			 * Attansic/Atheros PHY hardware.
    268   1.1    cegger 			 */
    269   1.1    cegger 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    270   1.1    cegger 			goto done;
    271   1.1    cegger 		default:
    272   1.1    cegger 			return EINVAL;
    273   1.1    cegger 		}
    274   1.1    cegger 
    275  1.25   msaitoh 		anar = mii_anar(ife);
    276  1.22   msaitoh 		if ((ife->ifm_media & IFM_FDX) != 0) {
    277   1.1    cegger 			bmcr |= BMCR_FDX;
    278   1.1    cegger 			/* Enable pause. */
    279   1.1    cegger 			if (sc->mii_flags & MIIF_DOPAUSE)
    280  1.15   msaitoh 				anar |= ANAR_PAUSE_TOWARDS;
    281   1.1    cegger 		}
    282   1.1    cegger 
    283   1.1    cegger 		if ((sc->mii_extcapabilities & (EXTSR_1000TFDX |
    284   1.1    cegger 		    EXTSR_1000THDX)) != 0)
    285   1.1    cegger 			PHY_WRITE(sc, MII_100T2CR, 0);
    286   1.1    cegger 		PHY_WRITE(sc, MII_ANAR, anar);
    287   1.1    cegger 
    288  1.21   msaitoh 		/* Start autonegotiation. */
    289   1.8    cegger 		PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
    290   1.1    cegger done:
    291   1.1    cegger 		break;
    292   1.1    cegger 
    293   1.1    cegger 	case MII_TICK:
    294  1.21   msaitoh 		/* If we're not currently selected, just return. */
    295   1.1    cegger 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    296   1.1    cegger 			return 0;
    297   1.1    cegger 
    298  1.21   msaitoh 		/* Is the interface even up? */
    299   1.1    cegger 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    300   1.1    cegger 			return 0;
    301   1.1    cegger 
    302  1.21   msaitoh 		/* Only used for autonegotiation. */
    303  1.14   msaitoh 		if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
    304  1.14   msaitoh 		    (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
    305   1.1    cegger 			sc->mii_ticks = 0;
    306   1.1    cegger 			break;
    307   1.1    cegger 		}
    308   1.1    cegger 
    309   1.1    cegger 		/*
    310   1.1    cegger 		 * Check for link.
    311   1.1    cegger 		 * Read the status register twice; BMSR_LINK is latch-low.
    312   1.1    cegger 		 */
    313  1.19   msaitoh 		PHY_READ(sc, MII_BMSR, &bmsr);
    314  1.19   msaitoh 		PHY_READ(sc, MII_BMSR, &bmsr);
    315   1.1    cegger 		if (bmsr & BMSR_LINK) {
    316   1.1    cegger 			sc->mii_ticks = 0;
    317   1.1    cegger 			break;
    318   1.1    cegger 		}
    319   1.1    cegger 
    320   1.1    cegger 		/* Announce link loss right after it happens. */
    321   1.1    cegger 		if (sc->mii_ticks++ == 0)
    322   1.1    cegger 			break;
    323   1.1    cegger 
    324  1.21   msaitoh 		/* Only retry autonegotiation every mii_anegticks seconds. */
    325  1.31   msaitoh 		if (sc->mii_ticks < sc->mii_anegticks)
    326   1.1    cegger 			break;
    327   1.1    cegger 
    328   1.1    cegger 		atphy_mii_phy_auto(sc);
    329   1.1    cegger 		break;
    330   1.1    cegger 	}
    331   1.1    cegger 
    332   1.1    cegger 	/* Update the media status. */
    333   1.1    cegger 	mii_phy_status(sc);
    334   1.1    cegger 
    335   1.1    cegger 	/* Callback if something changed. */
    336   1.1    cegger 	mii_phy_update(sc, cmd);
    337   1.1    cegger 	return 0;
    338   1.1    cegger }
    339   1.1    cegger 
    340   1.1    cegger static void
    341   1.1    cegger atphy_status(struct mii_softc *sc)
    342   1.1    cegger {
    343   1.1    cegger 	struct mii_data *mii = sc->mii_pdata;
    344  1.19   msaitoh 	uint16_t bmsr, bmcr, gsr, ssr;
    345   1.1    cegger 
    346  1.30   thorpej 	KASSERT(mii_locked(mii));
    347  1.30   thorpej 
    348   1.1    cegger 	mii->mii_media_status = IFM_AVALID;
    349   1.1    cegger 	mii->mii_media_active = IFM_ETHER;
    350   1.1    cegger 
    351  1.19   msaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
    352  1.19   msaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
    353   1.1    cegger 	if (bmsr & BMSR_LINK)
    354   1.1    cegger 		mii->mii_media_status |= IFM_ACTIVE;
    355   1.1    cegger 
    356  1.19   msaitoh 	PHY_READ(sc, MII_BMCR, &bmcr);
    357   1.1    cegger 	if (bmcr & BMCR_ISO) {
    358   1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    359   1.1    cegger 		mii->mii_media_status = 0;
    360   1.1    cegger 		return;
    361   1.1    cegger 	}
    362   1.1    cegger 
    363   1.1    cegger 	if (bmcr & BMCR_LOOP)
    364   1.1    cegger 		mii->mii_media_active |= IFM_LOOP;
    365   1.1    cegger 
    366  1.19   msaitoh 	PHY_READ(sc, ATPHY_SSR, &ssr);
    367   1.1    cegger 	if (!(ssr & ATPHY_SSR_SPD_DPLX_RESOLVED)) {
    368   1.1    cegger 		/* Erg, still trying, I guess... */
    369   1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    370   1.1    cegger 		return;
    371   1.1    cegger 	}
    372   1.1    cegger 
    373   1.1    cegger 	switch (ssr & ATPHY_SSR_SPEED_MASK) {
    374   1.1    cegger 	case ATPHY_SSR_1000MBS:
    375   1.1    cegger 		mii->mii_media_active |= IFM_1000_T;
    376   1.1    cegger 		/*
    377   1.1    cegger 		 * atphy(4) has a valid link so reset mii_ticks.
    378   1.1    cegger 		 * Resetting mii_ticks is needed in order to
    379   1.1    cegger 		 * detect link loss after auto-negotiation.
    380   1.1    cegger 		 */
    381   1.1    cegger 		sc->mii_ticks = 0;
    382   1.1    cegger 		break;
    383   1.1    cegger 	case ATPHY_SSR_100MBS:
    384   1.1    cegger 		mii->mii_media_active |= IFM_100_TX;
    385   1.1    cegger 		sc->mii_ticks = 0;
    386   1.1    cegger 		break;
    387   1.1    cegger 	case ATPHY_SSR_10MBS:
    388   1.1    cegger 		mii->mii_media_active |= IFM_10_T;
    389   1.1    cegger 		sc->mii_ticks = 0;
    390   1.1    cegger 		break;
    391   1.1    cegger 	default:
    392   1.1    cegger 		mii->mii_media_active |= IFM_NONE;
    393   1.1    cegger 		return;
    394   1.1    cegger 	}
    395   1.1    cegger 
    396   1.1    cegger 	if (ssr & ATPHY_SSR_DUPLEX)
    397   1.1    cegger 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
    398   1.1    cegger 	else
    399   1.1    cegger 		mii->mii_media_active |= IFM_HDX;
    400   1.1    cegger 
    401  1.19   msaitoh 	PHY_READ(sc, MII_100T2SR, &gsr);
    402   1.1    cegger 	if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&
    403   1.1    cegger 	    gsr & GTSR_MS_RES)
    404   1.1    cegger 		mii->mii_media_active |= IFM_ETH_MASTER;
    405   1.1    cegger }
    406   1.1    cegger 
    407   1.1    cegger static void
    408   1.1    cegger atphy_reset(struct mii_softc *sc)
    409   1.1    cegger {
    410  1.24   hkenken 	struct atphy_softc *asc = (struct atphy_softc *)sc;
    411  1.19   msaitoh 	uint16_t reg;
    412   1.1    cegger 	int i;
    413   1.1    cegger 
    414  1.30   thorpej 	KASSERT(mii_locked(sc->mii_pdata));
    415  1.30   thorpej 
    416  1.29   msaitoh 	/*
    417  1.29   msaitoh 	 * Take PHY out of power down mode.
    418  1.29   msaitoh 	 *
    419  1.29   msaitoh 	 * XXX AR8021 document has no description about the power saving
    420  1.29   msaitoh 	 * control register. Shouldn't we write it?
    421  1.29   msaitoh 	 */
    422   1.1    cegger 	PHY_WRITE(sc, 29, 0x29);
    423  1.29   msaitoh 	/*
    424  1.29   msaitoh 	 * XXX AR8031 document says the lower 14 bits are reserved and the
    425  1.29   msaitoh 	 * default value is 0x36d0. Shouldn't we clear those bits?
    426  1.29   msaitoh 	 * I have no document neither L1(F1) nor L2(F2).
    427  1.29   msaitoh 	 */
    428   1.1    cegger 	PHY_WRITE(sc, 30, 0);
    429   1.1    cegger 
    430  1.29   msaitoh 	if ((sc->mii_mpd_model == MII_MODEL_ATTANSIC_L2)
    431  1.29   msaitoh 	    && (sc->mii_mpd_rev == 1)) {
    432  1.29   msaitoh 		/*
    433  1.29   msaitoh 		 * On NVIDIA MCP61 with Attansic L2 rev. 1, changing debug
    434  1.29   msaitoh 		 * port 0x29's value makes the next PHY read fail with error.
    435  1.29   msaitoh 		 * This is observed on ASUS M2N-MX SE Plus. Read any register
    436  1.29   msaitoh 		 * to ignore this problem.
    437  1.29   msaitoh 		 */
    438  1.29   msaitoh 		(void)PHY_READ(sc, ATPHY_SCR, &reg);
    439  1.29   msaitoh 	}
    440  1.19   msaitoh 	PHY_READ(sc, ATPHY_SCR, &reg);
    441   1.1    cegger 	/* Enable automatic crossover. */
    442   1.1    cegger 	reg |= ATPHY_SCR_AUTO_X_MODE;
    443   1.1    cegger 	/* Disable power down. */
    444   1.1    cegger 	reg &= ~ATPHY_SCR_MAC_PDOWN;
    445   1.1    cegger 	/* Enable CRS on Tx. */
    446   1.1    cegger 	reg |= ATPHY_SCR_ASSERT_CRS_ON_TX;
    447   1.1    cegger 	/* Auto correction for reversed cable polarity. */
    448   1.1    cegger 	reg |= ATPHY_SCR_POLARITY_REVERSAL;
    449   1.1    cegger 	PHY_WRITE(sc, ATPHY_SCR, reg);
    450   1.1    cegger 
    451   1.8    cegger 	atphy_mii_phy_auto(sc);
    452   1.8    cegger 
    453   1.1    cegger 	/* Workaround F1 bug to reset phy. */
    454  1.19   msaitoh 	PHY_READ(sc, MII_BMCR, &reg);
    455  1.19   msaitoh 	reg |= BMCR_RESET;
    456   1.8    cegger 	PHY_WRITE(sc, MII_BMCR, reg);
    457   1.1    cegger 
    458   1.1    cegger 	for (i = 0; i < 1000; i++) {
    459   1.1    cegger 		DELAY(1);
    460  1.19   msaitoh 		PHY_READ(sc, MII_BMCR, &reg);
    461  1.19   msaitoh 		if ((reg & BMCR_RESET) == 0)
    462   1.1    cegger 			break;
    463   1.1    cegger 	}
    464  1.24   hkenken 
    465  1.24   hkenken 	if (asc->rgmii_tx_internal_delay) {
    466  1.24   hkenken 		PHY_WRITE(sc, ATPHY_DEBUG_PORT_ADDR, 0x05);
    467  1.24   hkenken 		PHY_WRITE(sc, ATPHY_DEBUG_PORT_DATA, ATPHY_RGMII_TX_CLK_DLY);
    468  1.24   hkenken 	}
    469  1.24   hkenken 	if (asc->rgmii_rx_internal_delay) {
    470  1.24   hkenken 		PHY_WRITE(sc, ATPHY_DEBUG_PORT_ADDR, 0x00);
    471  1.24   hkenken 		PHY_WRITE(sc, ATPHY_DEBUG_PORT_DATA, ATPHY_RGMII_RX_CLK_DLY);
    472  1.24   hkenken 	}
    473   1.1    cegger }
    474   1.1    cegger 
    475   1.1    cegger static int
    476   1.1    cegger atphy_mii_phy_auto(struct mii_softc *sc)
    477   1.1    cegger {
    478   1.1    cegger 	uint16_t anar;
    479   1.1    cegger 
    480  1.30   thorpej 	KASSERT(mii_locked(sc->mii_pdata));
    481  1.30   thorpej 
    482  1.13   msaitoh 	sc->mii_ticks = 0;
    483   1.1    cegger 	anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
    484   1.1    cegger 	if (sc->mii_flags & MIIF_DOPAUSE)
    485  1.16   msaitoh 		anar |= ANAR_PAUSE_TOWARDS;
    486   1.1    cegger 	PHY_WRITE(sc, MII_ANAR, anar);
    487   1.1    cegger 	if (sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
    488   1.1    cegger 		PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX |
    489   1.1    cegger 		    GTCR_ADV_1000THDX);
    490   1.8    cegger 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    491   1.1    cegger 
    492   1.1    cegger 	return EJUSTRETURN;
    493   1.1    cegger }
    494