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