Home | History | Annotate | Line # | Download | only in mii
brgphy.c revision 1.76.28.1
      1  1.76.28.1  christos /*	$NetBSD: brgphy.c,v 1.76.28.1 2019/06/10 22:07:13 christos Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*-
      4        1.1   thorpej  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9        1.1   thorpej  * NASA Ames Research Center.
     10        1.1   thorpej  *
     11        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12        1.1   thorpej  * modification, are permitted provided that the following conditions
     13        1.1   thorpej  * are met:
     14        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19        1.1   thorpej  *
     20        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21        1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1   thorpej  */
     32        1.1   thorpej 
     33        1.1   thorpej /*
     34        1.1   thorpej  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     35        1.1   thorpej  *
     36        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     37        1.1   thorpej  * modification, are permitted provided that the following conditions
     38        1.1   thorpej  * are met:
     39        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     40        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     41        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     42        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     43        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     44        1.1   thorpej  *
     45        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     46        1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     47        1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     48        1.1   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     49        1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     50        1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     51        1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     52        1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     53        1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     54        1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     55        1.1   thorpej  */
     56        1.1   thorpej 
     57        1.1   thorpej /*
     58       1.57       jym  * driver for the Broadcom BCM5400 and BCM5700 Gig-E PHYs.
     59        1.1   thorpej  *
     60        1.1   thorpej  * Programming information for this PHY was gleaned from FreeBSD
     61        1.1   thorpej  * (they were apparently able to get a datasheet from Broadcom).
     62        1.1   thorpej  */
     63        1.5     lukem 
     64        1.5     lukem #include <sys/cdefs.h>
     65  1.76.28.1  christos __KERNEL_RCSID(0, "$NetBSD: brgphy.c,v 1.76.28.1 2019/06/10 22:07:13 christos Exp $");
     66        1.1   thorpej 
     67        1.1   thorpej #include <sys/param.h>
     68        1.1   thorpej #include <sys/systm.h>
     69        1.1   thorpej #include <sys/kernel.h>
     70        1.1   thorpej #include <sys/device.h>
     71        1.1   thorpej #include <sys/socket.h>
     72        1.1   thorpej #include <sys/errno.h>
     73       1.44   msaitoh #include <prop/proplib.h>
     74        1.1   thorpej 
     75        1.1   thorpej #include <net/if.h>
     76        1.1   thorpej #include <net/if_media.h>
     77        1.1   thorpej 
     78        1.1   thorpej #include <dev/mii/mii.h>
     79        1.1   thorpej #include <dev/mii/miivar.h>
     80        1.1   thorpej #include <dev/mii/miidevs.h>
     81        1.1   thorpej #include <dev/mii/brgphyreg.h>
     82        1.1   thorpej 
     83       1.43   msaitoh #include <dev/pci/if_bgereg.h>
     84       1.43   msaitoh #include <dev/pci/if_bnxreg.h>
     85       1.43   msaitoh 
     86       1.39   xtraeme static int	brgphymatch(device_t, cfdata_t, void *);
     87       1.39   xtraeme static void	brgphyattach(device_t, device_t, void *);
     88        1.1   thorpej 
     89       1.44   msaitoh struct brgphy_softc {
     90       1.44   msaitoh 	struct mii_softc sc_mii;
     91       1.54    dyoung 	bool sc_isbge;
     92       1.54    dyoung 	bool sc_isbnx;
     93       1.71   msaitoh 	uint32_t sc_chipid;	/* parent's chipid */
     94       1.71   msaitoh 	uint32_t sc_phyflags;	/* parent's phyflags */
     95       1.71   msaitoh 	uint32_t sc_shared_hwcfg; /* shared hw config */
     96       1.71   msaitoh 	uint32_t sc_port_hwcfg;	/* port specific hw config */
     97       1.44   msaitoh };
     98       1.44   msaitoh 
     99       1.44   msaitoh CFATTACH_DECL3_NEW(brgphy, sizeof(struct brgphy_softc),
    100       1.42    dyoung     brgphymatch, brgphyattach, mii_phy_detach, mii_phy_activate, NULL, NULL,
    101       1.42    dyoung     DVF_DETACH_SHUTDOWN);
    102        1.1   thorpej 
    103       1.21   thorpej static int	brgphy_service(struct mii_softc *, struct mii_data *, int);
    104       1.71   msaitoh static void	brgphy_copper_status(struct mii_softc *);
    105       1.71   msaitoh static void	brgphy_fiber_status(struct mii_softc *);
    106       1.71   msaitoh static void	brgphy_5708s_status(struct mii_softc *);
    107       1.71   msaitoh static void	brgphy_5709s_status(struct mii_softc *);
    108       1.32   msaitoh static int	brgphy_mii_phy_auto(struct mii_softc *);
    109       1.32   msaitoh static void	brgphy_loop(struct mii_softc *);
    110       1.43   msaitoh static void	brgphy_reset(struct mii_softc *);
    111       1.43   msaitoh static void	brgphy_bcm5401_dspcode(struct mii_softc *);
    112       1.43   msaitoh static void	brgphy_bcm5411_dspcode(struct mii_softc *);
    113       1.43   msaitoh static void	brgphy_bcm5421_dspcode(struct mii_softc *);
    114       1.43   msaitoh static void	brgphy_bcm54k2_dspcode(struct mii_softc *);
    115       1.43   msaitoh static void	brgphy_adc_bug(struct mii_softc *);
    116       1.43   msaitoh static void	brgphy_5704_a0_bug(struct mii_softc *);
    117       1.43   msaitoh static void	brgphy_ber_bug(struct mii_softc *);
    118       1.43   msaitoh static void	brgphy_crc_bug(struct mii_softc *);
    119       1.58       jym static void	brgphy_disable_early_dac(struct mii_softc *);
    120       1.52   msaitoh static void	brgphy_jumbo_settings(struct mii_softc *);
    121       1.52   msaitoh static void	brgphy_eth_wirespeed(struct mii_softc *);
    122        1.1   thorpej 
    123       1.10   thorpej 
    124       1.71   msaitoh static const struct mii_phy_funcs brgphy_copper_funcs = {
    125       1.71   msaitoh 	brgphy_service, brgphy_copper_status, brgphy_reset,
    126       1.71   msaitoh };
    127       1.71   msaitoh 
    128       1.71   msaitoh static const struct mii_phy_funcs brgphy_fiber_funcs = {
    129       1.71   msaitoh 	brgphy_service, brgphy_fiber_status, brgphy_reset,
    130       1.71   msaitoh };
    131       1.71   msaitoh 
    132       1.71   msaitoh static const struct mii_phy_funcs brgphy_5708s_funcs = {
    133       1.71   msaitoh 	brgphy_service, brgphy_5708s_status, brgphy_reset,
    134       1.71   msaitoh };
    135       1.71   msaitoh 
    136       1.71   msaitoh static const struct mii_phy_funcs brgphy_5709s_funcs = {
    137       1.71   msaitoh 	brgphy_service, brgphy_5709s_status, brgphy_reset,
    138       1.34     markd };
    139       1.34     markd 
    140       1.21   thorpej static const struct mii_phydesc brgphys[] = {
    141  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5400),
    142  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5401),
    143  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5402),
    144  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5404),
    145  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5411),
    146  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5421),
    147  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5424),
    148  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5461),
    149  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5462),
    150  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5464),
    151  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5466),
    152  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM54K2),
    153  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5701),
    154  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5703),
    155  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5704),
    156  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5705),
    157  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5706),
    158  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5714),
    159  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5750),
    160  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5752),
    161  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5780),
    162  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM, BCM5708C),
    163  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5481),
    164  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5482),
    165  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5708S),
    166  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5709C),
    167  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5709S),
    168  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5709CAX),
    169  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5722),
    170  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5754),
    171  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5755),
    172  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5756),
    173  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5761),
    174  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5784),
    175  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM2, BCM5785),
    176  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM3, BCM5717C),
    177  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM3, BCM5719C),
    178  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM3, BCM5720C),
    179  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM3, BCM57765),
    180  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM3, BCM57780),
    181  1.76.28.1  christos 	MII_PHY_DESC(BROADCOM4, BCM5725C),
    182  1.76.28.1  christos 	MII_PHY_DESC(xxBROADCOM_ALT1, BCM5906),
    183  1.76.28.1  christos 	MII_PHY_END,
    184        1.1   thorpej };
    185        1.1   thorpej 
    186       1.21   thorpej static int
    187       1.48   tsutsui brgphymatch(device_t parent, cfdata_t match, void *aux)
    188        1.1   thorpej {
    189        1.1   thorpej 	struct mii_attach_args *ma = aux;
    190        1.1   thorpej 
    191        1.2   thorpej 	if (mii_phy_match(ma, brgphys) != NULL)
    192  1.76.28.1  christos 		return 10;
    193        1.1   thorpej 
    194  1.76.28.1  christos 	return 0;
    195        1.1   thorpej }
    196        1.1   thorpej 
    197       1.21   thorpej static void
    198       1.46    cegger brgphyattach(device_t parent, device_t self, void *aux)
    199        1.1   thorpej {
    200       1.44   msaitoh 	struct brgphy_softc *bsc = device_private(self);
    201       1.44   msaitoh 	struct mii_softc *sc = &bsc->sc_mii;
    202        1.1   thorpej 	struct mii_attach_args *ma = aux;
    203        1.1   thorpej 	struct mii_data *mii = ma->mii_data;
    204        1.2   thorpej 	const struct mii_phydesc *mpd;
    205       1.44   msaitoh 	prop_dictionary_t dict;
    206        1.1   thorpej 
    207        1.2   thorpej 	mpd = mii_phy_match(ma, brgphys);
    208       1.17   thorpej 	aprint_naive(": Media interface\n");
    209       1.17   thorpej 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    210        1.1   thorpej 
    211       1.39   xtraeme 	sc->mii_dev = self;
    212        1.1   thorpej 	sc->mii_inst = mii->mii_instance;
    213        1.1   thorpej 	sc->mii_phy = ma->mii_phyno;
    214       1.61   msaitoh 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    215       1.32   msaitoh 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    216       1.43   msaitoh 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    217        1.1   thorpej 	sc->mii_pdata = mii;
    218        1.6   thorpej 	sc->mii_flags = ma->mii_flags;
    219       1.30  christos 	sc->mii_anegticks = MII_ANEGTICKS;
    220       1.10   thorpej 
    221       1.58       jym 	if (device_is_a(parent, "bge"))
    222       1.54    dyoung 		bsc->sc_isbge = true;
    223       1.58       jym 	else if (device_is_a(parent, "bnx"))
    224       1.54    dyoung 		bsc->sc_isbnx = true;
    225       1.58       jym 
    226       1.72   msaitoh 	dict = device_properties(parent);
    227       1.58       jym 	if (bsc->sc_isbge || bsc->sc_isbnx) {
    228       1.57       jym 		if (!prop_dictionary_get_uint32(dict, "phyflags",
    229       1.58       jym 		    &bsc->sc_phyflags))
    230       1.58       jym 			aprint_error_dev(self, "failed to get phyflags\n");
    231       1.58       jym 		if (!prop_dictionary_get_uint32(dict, "chipid",
    232       1.58       jym 		    &bsc->sc_chipid))
    233       1.58       jym 			aprint_error_dev(self, "failed to get chipid\n");
    234       1.44   msaitoh 	}
    235       1.57       jym 
    236       1.71   msaitoh 	if (bsc->sc_isbnx) {
    237       1.71   msaitoh 		/* Currently, only bnx use sc_shared_hwcfg and sc_port_hwcfg */
    238       1.71   msaitoh 		if (!prop_dictionary_get_uint32(dict, "shared_hwcfg",
    239       1.71   msaitoh 			&bsc->sc_shared_hwcfg))
    240       1.71   msaitoh 			aprint_error_dev(self, "failed to get shared_hwcfg\n");
    241       1.71   msaitoh 		if (!prop_dictionary_get_uint32(dict, "port_hwcfg",
    242       1.71   msaitoh 			&bsc->sc_port_hwcfg))
    243       1.71   msaitoh 			aprint_error_dev(self, "failed to get port_hwcfg\n");
    244       1.71   msaitoh 	}
    245       1.71   msaitoh 
    246       1.71   msaitoh 	if (sc->mii_flags & MIIF_HAVEFIBER) {
    247       1.74   msaitoh 		if ((sc->mii_mpd_oui == MII_OUI_BROADCOM2)
    248       1.74   msaitoh 		    && sc->mii_mpd_model == MII_MODEL_BROADCOM2_BCM5708S)
    249       1.71   msaitoh 			sc->mii_funcs = &brgphy_5708s_funcs;
    250       1.74   msaitoh 		else if ((sc->mii_mpd_oui == MII_OUI_BROADCOM2)
    251       1.75   msaitoh 		    && (sc->mii_mpd_model ==  MII_MODEL_BROADCOM2_BCM5709S)) {
    252       1.75   msaitoh 			if (bsc->sc_isbnx)
    253       1.75   msaitoh 				sc->mii_funcs = &brgphy_5709s_funcs;
    254       1.75   msaitoh 			else {
    255       1.75   msaitoh 				/*
    256       1.75   msaitoh 				 * XXX
    257       1.75   msaitoh 				 * 5720S and 5709S shares the same PHY id.
    258       1.75   msaitoh 				 * Assume 5720S PHY if parent device is bge(4).
    259       1.75   msaitoh 				 */
    260       1.75   msaitoh 				sc->mii_funcs = &brgphy_5708s_funcs;
    261       1.75   msaitoh 			}
    262       1.75   msaitoh 		} else
    263       1.71   msaitoh 			sc->mii_funcs = &brgphy_fiber_funcs;
    264       1.71   msaitoh 	} else
    265       1.71   msaitoh 		sc->mii_funcs = &brgphy_copper_funcs;
    266       1.71   msaitoh 
    267       1.63   msaitoh 	PHY_RESET(sc);
    268       1.63   msaitoh 
    269  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
    270  1.76.28.1  christos 	sc->mii_capabilities &= ma->mii_capmask;
    271       1.63   msaitoh 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    272  1.76.28.1  christos 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    273       1.63   msaitoh 
    274       1.57       jym 	aprint_normal_dev(self, "");
    275       1.75   msaitoh 	if (sc->mii_flags & MIIF_HAVEFIBER) {
    276       1.75   msaitoh 		sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
    277       1.57       jym 
    278       1.75   msaitoh 		/*
    279       1.75   msaitoh 		 * Set the proper bits for capabilities so that the
    280       1.75   msaitoh 		 * correct media get selected by mii_phy_add_media()
    281       1.75   msaitoh 		 */
    282       1.75   msaitoh 		sc->mii_capabilities |= BMSR_ANEG;
    283       1.75   msaitoh 		sc->mii_capabilities &= ~BMSR_100T4;
    284       1.75   msaitoh 		sc->mii_extcapabilities |= EXTSR_1000XFDX;
    285       1.75   msaitoh 
    286       1.75   msaitoh 		if (bsc->sc_isbnx) {
    287       1.57       jym 			/*
    288       1.75   msaitoh 			 * 2.5Gb support is a software enabled feature
    289       1.75   msaitoh 			 * on the BCM5708S and BCM5709S controllers.
    290       1.57       jym 			 */
    291       1.57       jym #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    292       1.75   msaitoh 			if (bsc->sc_phyflags
    293       1.75   msaitoh 			    & BNX_PHY_2_5G_CAPABLE_FLAG) {
    294       1.75   msaitoh 				ADD(IFM_MAKEWORD(IFM_ETHER, IFM_2500_SX,
    295       1.75   msaitoh 					IFM_FDX, sc->mii_inst), 0);
    296       1.75   msaitoh 				aprint_normal("2500baseSX-FDX, ");
    297       1.57       jym #undef ADD
    298       1.57       jym 			}
    299       1.57       jym 		}
    300       1.57       jym 	}
    301       1.75   msaitoh 	mii_phy_add_media(sc);
    302       1.75   msaitoh 
    303       1.57       jym 	aprint_normal("\n");
    304        1.1   thorpej }
    305        1.1   thorpej 
    306       1.21   thorpej static int
    307        1.4   thorpej brgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    308        1.1   thorpej {
    309        1.1   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    310  1.76.28.1  christos 	uint16_t reg, speed, gig;
    311        1.1   thorpej 
    312        1.1   thorpej 	switch (cmd) {
    313        1.1   thorpej 	case MII_POLLSTAT:
    314       1.73   msaitoh 		/* If we're not polling our PHY instance, just return. */
    315        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    316  1.76.28.1  christos 			return 0;
    317        1.1   thorpej 		break;
    318        1.1   thorpej 
    319        1.1   thorpej 	case MII_MEDIACHG:
    320        1.1   thorpej 		/*
    321        1.1   thorpej 		 * If the media indicates a different PHY instance,
    322        1.1   thorpej 		 * isolate ourselves.
    323        1.1   thorpej 		 */
    324        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    325  1.76.28.1  christos 			PHY_READ(sc, MII_BMCR, &reg);
    326        1.1   thorpej 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    327  1.76.28.1  christos 			return 0;
    328        1.1   thorpej 		}
    329        1.1   thorpej 
    330       1.73   msaitoh 		/* If the interface is not up, don't do anything. */
    331        1.1   thorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    332        1.1   thorpej 			break;
    333        1.1   thorpej 
    334       1.32   msaitoh 		PHY_RESET(sc); /* XXX hardware bug work-around */
    335       1.32   msaitoh 
    336       1.32   msaitoh 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    337       1.32   msaitoh 		case IFM_AUTO:
    338       1.32   msaitoh 			(void) brgphy_mii_phy_auto(sc);
    339       1.32   msaitoh 			break;
    340       1.71   msaitoh 		case IFM_2500_SX:
    341       1.71   msaitoh 			speed = BRGPHY_5708S_BMCR_2500;
    342       1.71   msaitoh 			goto setit;
    343       1.71   msaitoh 		case IFM_1000_SX:
    344       1.32   msaitoh 		case IFM_1000_T:
    345       1.32   msaitoh 			speed = BMCR_S1000;
    346       1.32   msaitoh 			goto setit;
    347       1.32   msaitoh 		case IFM_100_TX:
    348       1.32   msaitoh 			speed = BMCR_S100;
    349       1.32   msaitoh 			goto setit;
    350       1.32   msaitoh 		case IFM_10_T:
    351       1.32   msaitoh 			speed = BMCR_S10;
    352       1.32   msaitoh setit:
    353       1.32   msaitoh 			brgphy_loop(sc);
    354  1.76.28.1  christos 			if ((ife->ifm_media & IFM_FDX) != 0) {
    355       1.32   msaitoh 				speed |= BMCR_FDX;
    356       1.32   msaitoh 				gig = GTCR_ADV_1000TFDX;
    357       1.69   msaitoh 			} else
    358       1.32   msaitoh 				gig = GTCR_ADV_1000THDX;
    359       1.32   msaitoh 
    360       1.32   msaitoh 			PHY_WRITE(sc, MII_100T2CR, 0);
    361       1.51    bouyer 			PHY_WRITE(sc, MII_ANAR, ANAR_CSMA);
    362       1.32   msaitoh 			PHY_WRITE(sc, MII_BMCR, speed);
    363       1.32   msaitoh 
    364       1.71   msaitoh 			if ((IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T) &&
    365       1.71   msaitoh 			    (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_SX) &&
    366       1.71   msaitoh 			    (IFM_SUBTYPE(ife->ifm_media) != IFM_2500_SX))
    367       1.32   msaitoh 				break;
    368       1.32   msaitoh 
    369       1.32   msaitoh 			PHY_WRITE(sc, MII_100T2CR, gig);
    370       1.32   msaitoh 			PHY_WRITE(sc, MII_BMCR,
    371       1.65   msaitoh 			    speed | BMCR_AUTOEN | BMCR_STARTNEG);
    372       1.32   msaitoh 
    373       1.61   msaitoh 			if ((sc->mii_mpd_oui != MII_OUI_BROADCOM)
    374       1.61   msaitoh 			    || (sc->mii_mpd_model != MII_MODEL_BROADCOM_BCM5701))
    375       1.33   msaitoh 				break;
    376       1.32   msaitoh 
    377       1.32   msaitoh 			if (mii->mii_media.ifm_media & IFM_ETH_MASTER)
    378       1.32   msaitoh 				gig |= GTCR_MAN_MS | GTCR_ADV_MS;
    379       1.32   msaitoh 			PHY_WRITE(sc, MII_100T2CR, gig);
    380       1.32   msaitoh 			break;
    381       1.32   msaitoh 		default:
    382  1.76.28.1  christos 			return EINVAL;
    383       1.32   msaitoh 		}
    384        1.1   thorpej 		break;
    385        1.1   thorpej 
    386        1.1   thorpej 	case MII_TICK:
    387       1.73   msaitoh 		/* If we're not currently selected, just return. */
    388        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    389  1.76.28.1  christos 			return 0;
    390        1.1   thorpej 
    391       1.73   msaitoh 		/* Is the interface even up? */
    392       1.67   msaitoh 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    393       1.67   msaitoh 			return 0;
    394       1.67   msaitoh 
    395       1.73   msaitoh 		/* Only used for autonegotiation. */
    396       1.67   msaitoh 		if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
    397       1.67   msaitoh 		    (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
    398       1.67   msaitoh 			sc->mii_ticks = 0;
    399       1.67   msaitoh 			break;
    400       1.67   msaitoh 		}
    401       1.67   msaitoh 
    402       1.67   msaitoh 		/*
    403       1.67   msaitoh 		 * Check for link.
    404       1.67   msaitoh 		 * Read the status register twice; BMSR_LINK is latch-low.
    405       1.67   msaitoh 		 */
    406  1.76.28.1  christos 		PHY_READ(sc, MII_BMSR, &reg);
    407  1.76.28.1  christos 		PHY_READ(sc, MII_BMSR, &reg);
    408       1.67   msaitoh 		if (reg & BMSR_LINK) {
    409       1.67   msaitoh 			sc->mii_ticks = 0;
    410       1.67   msaitoh 			break;
    411       1.67   msaitoh 		}
    412       1.67   msaitoh 
    413       1.67   msaitoh 		/*
    414       1.67   msaitoh 		 * mii_ticks == 0 means it's the first tick after changing the
    415       1.67   msaitoh 		 * media or the link became down since the last tick
    416       1.67   msaitoh 		 * (see above), so break to update the status.
    417       1.67   msaitoh 		 */
    418       1.67   msaitoh 		if (sc->mii_ticks++ == 0)
    419       1.67   msaitoh 			break;
    420       1.67   msaitoh 
    421       1.73   msaitoh 		/* Only retry autonegotiation every mii_anegticks seconds. */
    422       1.67   msaitoh 		KASSERT(sc->mii_anegticks != 0);
    423       1.67   msaitoh 		if (sc->mii_ticks <= sc->mii_anegticks)
    424       1.67   msaitoh 			break;
    425       1.67   msaitoh 
    426       1.67   msaitoh 		brgphy_mii_phy_auto(sc);
    427        1.1   thorpej 		break;
    428        1.1   thorpej 
    429        1.1   thorpej 	case MII_DOWN:
    430        1.1   thorpej 		mii_phy_down(sc);
    431  1.76.28.1  christos 		return 0;
    432        1.1   thorpej 	}
    433        1.1   thorpej 
    434        1.1   thorpej 	/* Update the media status. */
    435        1.1   thorpej 	mii_phy_status(sc);
    436        1.1   thorpej 
    437       1.10   thorpej 	/*
    438       1.32   msaitoh 	 * Callback if something changed. Note that we need to poke the DSP on
    439       1.32   msaitoh 	 * the Broadcom PHYs if the media changes.
    440       1.10   thorpej 	 */
    441       1.23     perry 	if (sc->mii_media_active != mii->mii_media_active ||
    442       1.10   thorpej 	    sc->mii_media_status != mii->mii_media_status ||
    443       1.10   thorpej 	    cmd == MII_MEDIACHG) {
    444       1.61   msaitoh 		switch (sc->mii_mpd_oui) {
    445       1.61   msaitoh 		case MII_OUI_BROADCOM:
    446       1.61   msaitoh 			switch (sc->mii_mpd_model) {
    447       1.61   msaitoh 			case MII_MODEL_BROADCOM_BCM5400:
    448       1.43   msaitoh 				brgphy_bcm5401_dspcode(sc);
    449       1.61   msaitoh 				break;
    450       1.61   msaitoh 			case MII_MODEL_BROADCOM_BCM5401:
    451       1.61   msaitoh 				if (sc->mii_mpd_rev == 1 || sc->mii_mpd_rev == 3)
    452       1.61   msaitoh 					brgphy_bcm5401_dspcode(sc);
    453       1.61   msaitoh 				break;
    454       1.61   msaitoh 			case MII_MODEL_BROADCOM_BCM5411:
    455       1.61   msaitoh 				brgphy_bcm5411_dspcode(sc);
    456       1.61   msaitoh 				break;
    457       1.61   msaitoh 			}
    458       1.43   msaitoh 			break;
    459       1.43   msaitoh 		}
    460       1.10   thorpej 	}
    461       1.43   msaitoh 
    462       1.43   msaitoh 	/* Callback if something changed. */
    463       1.43   msaitoh 	mii_phy_update(sc, cmd);
    464  1.76.28.1  christos 	return 0;
    465        1.1   thorpej }
    466        1.1   thorpej 
    467       1.21   thorpej static void
    468       1.71   msaitoh brgphy_copper_status(struct mii_softc *sc)
    469        1.1   thorpej {
    470        1.1   thorpej 	struct mii_data *mii = sc->mii_pdata;
    471        1.1   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    472  1.76.28.1  christos 	uint16_t bmcr, bmsr, auxsts, gtsr;
    473        1.1   thorpej 
    474        1.1   thorpej 	mii->mii_media_status = IFM_AVALID;
    475        1.1   thorpej 	mii->mii_media_active = IFM_ETHER;
    476        1.1   thorpej 
    477  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    478  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    479       1.57       jym 	if (bmsr & BMSR_LINK)
    480        1.1   thorpej 		mii->mii_media_status |= IFM_ACTIVE;
    481        1.1   thorpej 
    482  1.76.28.1  christos 	PHY_READ(sc, MII_BMCR, &bmcr);
    483        1.1   thorpej 	if (bmcr & BMCR_ISO) {
    484        1.1   thorpej 		mii->mii_media_active |= IFM_NONE;
    485        1.1   thorpej 		mii->mii_media_status = 0;
    486        1.1   thorpej 		return;
    487        1.1   thorpej 	}
    488        1.1   thorpej 
    489        1.1   thorpej 	if (bmcr & BMCR_LOOP)
    490        1.1   thorpej 		mii->mii_media_active |= IFM_LOOP;
    491        1.1   thorpej 
    492        1.1   thorpej 	if (bmcr & BMCR_AUTOEN) {
    493        1.1   thorpej 		/*
    494        1.1   thorpej 		 * The media status bits are only valid of autonegotiation
    495        1.1   thorpej 		 * has completed (or it's disabled).
    496        1.1   thorpej 		 */
    497       1.57       jym 		if ((bmsr & BMSR_ACOMP) == 0) {
    498        1.1   thorpej 			/* Erg, still trying, I guess... */
    499        1.1   thorpej 			mii->mii_media_active |= IFM_NONE;
    500        1.1   thorpej 			return;
    501        1.1   thorpej 		}
    502        1.1   thorpej 
    503  1.76.28.1  christos 		PHY_READ(sc, BRGPHY_MII_AUXSTS, &auxsts);
    504       1.71   msaitoh 
    505       1.71   msaitoh 		switch (auxsts & BRGPHY_AUXSTS_AN_RES) {
    506       1.71   msaitoh 		case BRGPHY_RES_1000FD:
    507       1.71   msaitoh 			mii->mii_media_active |= IFM_1000_T | IFM_FDX;
    508  1.76.28.1  christos 			PHY_READ(sc, MII_100T2SR, &gtsr);
    509       1.71   msaitoh 			if (gtsr & GTSR_MS_RES)
    510       1.71   msaitoh 				mii->mii_media_active |= IFM_ETH_MASTER;
    511       1.71   msaitoh 			break;
    512       1.71   msaitoh 
    513       1.71   msaitoh 		case BRGPHY_RES_1000HD:
    514       1.71   msaitoh 			mii->mii_media_active |= IFM_1000_T | IFM_HDX;
    515  1.76.28.1  christos 			PHY_READ(sc, MII_100T2SR, &gtsr);
    516       1.71   msaitoh 			if (gtsr & GTSR_MS_RES)
    517       1.71   msaitoh 				mii->mii_media_active |= IFM_ETH_MASTER;
    518       1.71   msaitoh 			break;
    519       1.71   msaitoh 
    520       1.71   msaitoh 		case BRGPHY_RES_100FD:
    521       1.71   msaitoh 			mii->mii_media_active |= IFM_100_TX | IFM_FDX;
    522       1.71   msaitoh 			break;
    523       1.71   msaitoh 
    524       1.71   msaitoh 		case BRGPHY_RES_100T4:
    525       1.71   msaitoh 			mii->mii_media_active |= IFM_100_T4 | IFM_HDX;
    526       1.71   msaitoh 			break;
    527       1.71   msaitoh 
    528       1.71   msaitoh 		case BRGPHY_RES_100HD:
    529       1.71   msaitoh 			mii->mii_media_active |= IFM_100_TX | IFM_HDX;
    530       1.71   msaitoh 			break;
    531       1.71   msaitoh 
    532       1.71   msaitoh 		case BRGPHY_RES_10FD:
    533       1.71   msaitoh 			mii->mii_media_active |= IFM_10_T | IFM_FDX;
    534       1.71   msaitoh 			break;
    535       1.71   msaitoh 
    536       1.71   msaitoh 		case BRGPHY_RES_10HD:
    537       1.71   msaitoh 			mii->mii_media_active |= IFM_10_T | IFM_HDX;
    538       1.71   msaitoh 			break;
    539       1.71   msaitoh 
    540       1.71   msaitoh 		default:
    541       1.71   msaitoh 			mii->mii_media_active |= IFM_NONE;
    542       1.71   msaitoh 			mii->mii_media_status = 0;
    543       1.71   msaitoh 		}
    544       1.71   msaitoh 
    545       1.71   msaitoh 		if (mii->mii_media_active & IFM_FDX)
    546       1.71   msaitoh 			mii->mii_media_active |= mii_phy_flowstatus(sc);
    547       1.71   msaitoh 
    548       1.71   msaitoh 	} else
    549       1.71   msaitoh 		mii->mii_media_active = ife->ifm_media;
    550       1.71   msaitoh }
    551       1.71   msaitoh 
    552       1.71   msaitoh void
    553       1.71   msaitoh brgphy_fiber_status(struct mii_softc *sc)
    554       1.71   msaitoh {
    555       1.71   msaitoh 	struct mii_data *mii = sc->mii_pdata;
    556       1.71   msaitoh 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    557  1.76.28.1  christos 	uint16_t bmcr, bmsr, anar, anlpar, result;
    558       1.71   msaitoh 
    559       1.71   msaitoh 	mii->mii_media_status = IFM_AVALID;
    560       1.71   msaitoh 	mii->mii_media_active = IFM_ETHER;
    561       1.71   msaitoh 
    562  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    563  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    564       1.71   msaitoh 	if (bmsr & BMSR_LINK)
    565       1.71   msaitoh 		mii->mii_media_status |= IFM_ACTIVE;
    566       1.57       jym 
    567  1.76.28.1  christos 	PHY_READ(sc, MII_BMCR, &bmcr);
    568       1.71   msaitoh 	if (bmcr & BMCR_LOOP)
    569       1.71   msaitoh 		mii->mii_media_active |= IFM_LOOP;
    570       1.57       jym 
    571       1.71   msaitoh 	if (bmcr & BMCR_AUTOEN) {
    572       1.71   msaitoh 		if ((bmsr & BMSR_ACOMP) == 0) {
    573       1.71   msaitoh 			/* Erg, still trying, I guess... */
    574       1.71   msaitoh 			mii->mii_media_active |= IFM_NONE;
    575       1.71   msaitoh 			return;
    576       1.71   msaitoh 		}
    577       1.57       jym 
    578       1.71   msaitoh 		mii->mii_media_active |= IFM_1000_SX;
    579       1.57       jym 
    580  1.76.28.1  christos 		PHY_READ(sc, MII_ANAR, &anar);
    581  1.76.28.1  christos 		PHY_READ(sc, MII_ANLPAR, &anlpar);
    582  1.76.28.1  christos 		result = anar & anlpar;
    583       1.57       jym 
    584  1.76.28.1  christos 		if (result & ANAR_X_FD)
    585       1.71   msaitoh 			mii->mii_media_active |= IFM_FDX;
    586       1.71   msaitoh 		else
    587       1.71   msaitoh 			mii->mii_media_active |= IFM_HDX;
    588        1.1   thorpej 
    589       1.71   msaitoh 		if (mii->mii_media_active & IFM_FDX)
    590       1.71   msaitoh 			mii->mii_media_active |= mii_phy_flowstatus(sc);
    591       1.71   msaitoh 	} else
    592       1.71   msaitoh 		mii->mii_media_active = ife->ifm_media;
    593       1.71   msaitoh }
    594        1.1   thorpej 
    595       1.71   msaitoh void
    596       1.71   msaitoh brgphy_5708s_status(struct mii_softc *sc)
    597       1.71   msaitoh {
    598       1.71   msaitoh 	struct mii_data *mii = sc->mii_pdata;
    599       1.71   msaitoh 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    600  1.76.28.1  christos 	uint16_t bmcr, bmsr;
    601        1.1   thorpej 
    602       1.71   msaitoh 	mii->mii_media_status = IFM_AVALID;
    603       1.71   msaitoh 	mii->mii_media_active = IFM_ETHER;
    604        1.1   thorpej 
    605  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    606  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    607       1.71   msaitoh 	if (bmsr & BMSR_LINK)
    608       1.71   msaitoh 		mii->mii_media_status |= IFM_ACTIVE;
    609        1.1   thorpej 
    610  1.76.28.1  christos 	PHY_READ(sc, MII_BMCR, &bmcr);
    611       1.71   msaitoh 	if (bmcr & BMCR_LOOP)
    612       1.71   msaitoh 		mii->mii_media_active |= IFM_LOOP;
    613        1.1   thorpej 
    614       1.71   msaitoh 	if (bmcr & BMCR_AUTOEN) {
    615  1.76.28.1  christos 		uint16_t xstat;
    616        1.1   thorpej 
    617       1.71   msaitoh 		if ((bmsr & BMSR_ACOMP) == 0) {
    618       1.71   msaitoh 			PHY_WRITE(sc, BRGPHY_5708S_BLOCK_ADDR,
    619       1.71   msaitoh 			    BRGPHY_5708S_DIG_PG0);
    620  1.76.28.1  christos 			PHY_READ(sc, BRGPHY_5708S_PG0_1000X_STAT1, &xstat);
    621       1.71   msaitoh 			if ((xstat & BRGPHY_5708S_PG0_1000X_STAT1_LINK) == 0) {
    622       1.71   msaitoh 				/* Erg, still trying, I guess... */
    623       1.57       jym 				mii->mii_media_active |= IFM_NONE;
    624       1.71   msaitoh 				return;
    625       1.57       jym 			}
    626        1.1   thorpej 		}
    627       1.57       jym 
    628       1.71   msaitoh 		PHY_WRITE(sc, BRGPHY_5708S_BLOCK_ADDR,
    629       1.71   msaitoh 		    BRGPHY_5708S_DIG_PG0);
    630  1.76.28.1  christos 		PHY_READ(sc, BRGPHY_5708S_PG0_1000X_STAT1, &xstat);
    631       1.71   msaitoh 
    632       1.71   msaitoh 		switch (xstat & BRGPHY_5708S_PG0_1000X_STAT1_SPEED_MASK) {
    633       1.71   msaitoh 		case BRGPHY_5708S_PG0_1000X_STAT1_SPEED_10:
    634       1.71   msaitoh 			mii->mii_media_active |= IFM_10_FL;
    635       1.71   msaitoh 			break;
    636       1.71   msaitoh 		case BRGPHY_5708S_PG0_1000X_STAT1_SPEED_100:
    637       1.71   msaitoh 			mii->mii_media_active |= IFM_100_FX;
    638       1.71   msaitoh 			break;
    639       1.71   msaitoh 		case BRGPHY_5708S_PG0_1000X_STAT1_SPEED_1G:
    640       1.71   msaitoh 			mii->mii_media_active |= IFM_1000_SX;
    641       1.71   msaitoh 			break;
    642       1.71   msaitoh 		case BRGPHY_5708S_PG0_1000X_STAT1_SPEED_25G:
    643       1.71   msaitoh 			mii->mii_media_active |= IFM_2500_SX;
    644       1.71   msaitoh 			break;
    645       1.71   msaitoh 		}
    646       1.71   msaitoh 
    647       1.71   msaitoh 		if (xstat & BRGPHY_5708S_PG0_1000X_STAT1_FDX)
    648       1.71   msaitoh 			mii->mii_media_active |= IFM_FDX;
    649       1.71   msaitoh 		else
    650       1.71   msaitoh 			mii->mii_media_active |= IFM_HDX;
    651       1.71   msaitoh 
    652       1.71   msaitoh 		if (mii->mii_media_active & IFM_FDX) {
    653       1.71   msaitoh 			if (xstat & BRGPHY_5708S_PG0_1000X_STAT1_TX_PAUSE)
    654  1.76.28.1  christos 				mii->mii_media_active
    655  1.76.28.1  christos 				    |= IFM_FLOW | IFM_ETH_TXPAUSE;
    656       1.71   msaitoh 			if (xstat & BRGPHY_5708S_PG0_1000X_STAT1_RX_PAUSE)
    657  1.76.28.1  christos 				mii->mii_media_active
    658  1.76.28.1  christos 				    |= IFM_FLOW | IFM_ETH_RXPAUSE;
    659       1.71   msaitoh 		}
    660       1.71   msaitoh 	} else
    661       1.71   msaitoh 		mii->mii_media_active = ife->ifm_media;
    662       1.71   msaitoh }
    663       1.71   msaitoh 
    664       1.71   msaitoh static void
    665       1.71   msaitoh brgphy_5709s_status(struct mii_softc *sc)
    666       1.71   msaitoh {
    667       1.71   msaitoh 	struct mii_data *mii = sc->mii_pdata;
    668       1.71   msaitoh 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    669  1.76.28.1  christos 	uint16_t bmcr, bmsr, auxsts;
    670       1.71   msaitoh 
    671       1.71   msaitoh 	mii->mii_media_status = IFM_AVALID;
    672       1.71   msaitoh 	mii->mii_media_active = IFM_ETHER;
    673       1.71   msaitoh 
    674  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    675  1.76.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    676       1.71   msaitoh 	if (bmsr & BMSR_LINK)
    677       1.71   msaitoh 		mii->mii_media_status |= IFM_ACTIVE;
    678       1.71   msaitoh 
    679  1.76.28.1  christos 	PHY_READ(sc, MII_BMCR, &bmcr);
    680       1.71   msaitoh 	if (bmcr & BMCR_ISO) {
    681       1.71   msaitoh 		mii->mii_media_active |= IFM_NONE;
    682       1.71   msaitoh 		mii->mii_media_status = 0;
    683       1.71   msaitoh 		return;
    684       1.71   msaitoh 	}
    685       1.71   msaitoh 
    686       1.71   msaitoh 	if (bmcr & BMCR_LOOP)
    687       1.71   msaitoh 		mii->mii_media_active |= IFM_LOOP;
    688       1.71   msaitoh 
    689       1.71   msaitoh 	if (bmcr & BMCR_AUTOEN) {
    690       1.71   msaitoh 		/*
    691       1.71   msaitoh 		 * The media status bits are only valid of autonegotiation
    692       1.71   msaitoh 		 * has completed (or it's disabled).
    693       1.71   msaitoh 		 */
    694       1.71   msaitoh 		if ((bmsr & BMSR_ACOMP) == 0) {
    695       1.71   msaitoh 			/* Erg, still trying, I guess... */
    696       1.71   msaitoh 			mii->mii_media_active |= IFM_NONE;
    697       1.71   msaitoh 			return;
    698       1.71   msaitoh 		}
    699       1.71   msaitoh 
    700       1.71   msaitoh 		/* 5709S has its own general purpose status registers */
    701  1.76.28.1  christos 		PHY_WRITE(sc, BRGPHY_BLOCK_ADDR, BRGPHY_BLOCK_ADDR_GP_STATUS);
    702  1.76.28.1  christos 		PHY_READ(sc, BRGPHY_GP_STATUS_TOP_ANEG_STATUS, &auxsts);
    703       1.71   msaitoh 
    704       1.71   msaitoh 		PHY_WRITE(sc, BRGPHY_BLOCK_ADDR,
    705       1.71   msaitoh 		    BRGPHY_BLOCK_ADDR_COMBO_IEEE0);
    706       1.71   msaitoh 
    707       1.71   msaitoh 		switch (auxsts & BRGPHY_GP_STATUS_TOP_ANEG_SPEED_MASK) {
    708       1.71   msaitoh 		case BRGPHY_GP_STATUS_TOP_ANEG_SPEED_10:
    709       1.71   msaitoh 			mii->mii_media_active |= IFM_10_FL;
    710       1.71   msaitoh 			break;
    711       1.71   msaitoh 		case BRGPHY_GP_STATUS_TOP_ANEG_SPEED_100:
    712       1.71   msaitoh 			mii->mii_media_active |= IFM_100_FX;
    713       1.71   msaitoh 			break;
    714       1.71   msaitoh 		case BRGPHY_GP_STATUS_TOP_ANEG_SPEED_1G:
    715       1.71   msaitoh 			mii->mii_media_active |= IFM_1000_SX;
    716       1.71   msaitoh 			break;
    717       1.71   msaitoh 		case BRGPHY_GP_STATUS_TOP_ANEG_SPEED_25G:
    718       1.71   msaitoh 			mii->mii_media_active |= IFM_2500_SX;
    719       1.71   msaitoh 			break;
    720       1.71   msaitoh 		default:
    721       1.71   msaitoh 			mii->mii_media_active |= IFM_NONE;
    722       1.71   msaitoh 			mii->mii_media_status = 0;
    723       1.71   msaitoh 			break;
    724       1.71   msaitoh 		}
    725       1.71   msaitoh 
    726       1.71   msaitoh 		if (auxsts & BRGPHY_GP_STATUS_TOP_ANEG_FDX)
    727       1.71   msaitoh 			mii->mii_media_active |= IFM_FDX;
    728       1.71   msaitoh 		else
    729       1.71   msaitoh 			mii->mii_media_active |= IFM_HDX;
    730       1.71   msaitoh 
    731       1.19   thorpej 		if (mii->mii_media_active & IFM_FDX)
    732       1.20   thorpej 			mii->mii_media_active |= mii_phy_flowstatus(sc);
    733        1.1   thorpej 	} else
    734        1.1   thorpej 		mii->mii_media_active = ife->ifm_media;
    735       1.10   thorpej }
    736       1.10   thorpej 
    737       1.32   msaitoh int
    738       1.32   msaitoh brgphy_mii_phy_auto(struct mii_softc *sc)
    739       1.32   msaitoh {
    740  1.76.28.1  christos 	uint16_t anar, ktcr = 0;
    741       1.32   msaitoh 
    742       1.64   msaitoh 	sc->mii_ticks = 0;
    743       1.32   msaitoh 	brgphy_loop(sc);
    744       1.32   msaitoh 	PHY_RESET(sc);
    745       1.57       jym 
    746       1.57       jym 	if (sc->mii_flags & MIIF_HAVEFIBER) {
    747       1.57       jym 		anar = ANAR_X_FD | ANAR_X_HD;
    748       1.57       jym 		if (sc->mii_flags & MIIF_DOPAUSE)
    749       1.69   msaitoh 			anar |= ANAR_X_PAUSE_TOWARDS;
    750       1.57       jym 	} else {
    751       1.57       jym 		anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
    752       1.57       jym 		if (sc->mii_flags & MIIF_DOPAUSE)
    753       1.66   msaitoh 			anar |= ANAR_FC | ANAR_PAUSE_ASYM;
    754       1.71   msaitoh 		ktcr = GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
    755       1.71   msaitoh 		if ((sc->mii_mpd_oui == MII_OUI_BROADCOM)
    756       1.71   msaitoh 		    && (sc->mii_mpd_model == MII_MODEL_BROADCOM_BCM5701))
    757       1.71   msaitoh 			ktcr |= GTCR_MAN_MS | GTCR_ADV_MS;
    758       1.71   msaitoh 		PHY_WRITE(sc, MII_100T2CR, ktcr);
    759       1.57       jym 	}
    760       1.32   msaitoh 	PHY_WRITE(sc, MII_ANAR, anar);
    761       1.57       jym 
    762       1.57       jym 	/* Start autonegotiation */
    763       1.69   msaitoh 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    764       1.32   msaitoh 	PHY_WRITE(sc, BRGPHY_MII_IMR, 0xFF00);
    765       1.32   msaitoh 
    766  1.76.28.1  christos 	return EJUSTRETURN;
    767       1.32   msaitoh }
    768       1.32   msaitoh 
    769       1.32   msaitoh void
    770       1.32   msaitoh brgphy_loop(struct mii_softc *sc)
    771       1.32   msaitoh {
    772  1.76.28.1  christos 	uint16_t bmsr;
    773       1.32   msaitoh 	int i;
    774       1.32   msaitoh 
    775       1.32   msaitoh 	PHY_WRITE(sc, MII_BMCR, BMCR_LOOP);
    776       1.33   msaitoh 	for (i = 0; i < 15000; i++) {
    777  1.76.28.1  christos 		PHY_READ(sc, MII_BMSR, &bmsr);
    778       1.32   msaitoh 		if (!(bmsr & BMSR_LINK))
    779       1.32   msaitoh 			break;
    780       1.32   msaitoh 		DELAY(10);
    781       1.32   msaitoh 	}
    782       1.32   msaitoh }
    783       1.32   msaitoh 
    784       1.21   thorpej static void
    785       1.43   msaitoh brgphy_reset(struct mii_softc *sc)
    786       1.10   thorpej {
    787       1.56       jym 	struct brgphy_softc *bsc = device_private(sc->mii_dev);
    788  1.76.28.1  christos 	uint16_t reg;
    789       1.10   thorpej 
    790       1.10   thorpej 	mii_phy_reset(sc);
    791       1.61   msaitoh 	switch (sc->mii_mpd_oui) {
    792       1.61   msaitoh 	case MII_OUI_BROADCOM:
    793       1.61   msaitoh 		switch (sc->mii_mpd_model) {
    794       1.61   msaitoh 		case MII_MODEL_BROADCOM_BCM5400:
    795       1.43   msaitoh 			brgphy_bcm5401_dspcode(sc);
    796       1.61   msaitoh 			break;
    797       1.61   msaitoh 		case MII_MODEL_BROADCOM_BCM5401:
    798       1.61   msaitoh 			if (sc->mii_mpd_rev == 1 || sc->mii_mpd_rev == 3)
    799       1.61   msaitoh 				brgphy_bcm5401_dspcode(sc);
    800       1.61   msaitoh 			break;
    801       1.61   msaitoh 		case MII_MODEL_BROADCOM_BCM5411:
    802       1.61   msaitoh 			brgphy_bcm5411_dspcode(sc);
    803       1.61   msaitoh 			break;
    804       1.61   msaitoh 		case MII_MODEL_BROADCOM_BCM5421:
    805       1.61   msaitoh 			brgphy_bcm5421_dspcode(sc);
    806       1.61   msaitoh 			break;
    807       1.61   msaitoh 		case MII_MODEL_BROADCOM_BCM54K2:
    808       1.61   msaitoh 			brgphy_bcm54k2_dspcode(sc);
    809       1.61   msaitoh 			break;
    810       1.61   msaitoh 		}
    811       1.43   msaitoh 		break;
    812       1.61   msaitoh 	case MII_OUI_BROADCOM3:
    813       1.61   msaitoh 		switch (sc->mii_mpd_model) {
    814       1.61   msaitoh 		case MII_MODEL_BROADCOM3_BCM5717C:
    815       1.61   msaitoh 		case MII_MODEL_BROADCOM3_BCM5719C:
    816       1.61   msaitoh 		case MII_MODEL_BROADCOM3_BCM5720C:
    817       1.61   msaitoh 		case MII_MODEL_BROADCOM3_BCM57765:
    818       1.61   msaitoh 			return;
    819       1.61   msaitoh 		}
    820       1.43   msaitoh 		break;
    821       1.61   msaitoh 	default:
    822       1.43   msaitoh 		break;
    823       1.43   msaitoh 	}
    824       1.15  jonathan 
    825       1.43   msaitoh 	/* Handle any bge (NetXtreme/NetLink) workarounds. */
    826       1.54    dyoung 	if (bsc->sc_isbge) {
    827       1.43   msaitoh 		if (!(sc->mii_flags & MIIF_HAVEFIBER)) {
    828       1.43   msaitoh 
    829       1.68   msaitoh 			if (bsc->sc_phyflags & BGEPHYF_ADC_BUG)
    830       1.43   msaitoh 				brgphy_adc_bug(sc);
    831       1.68   msaitoh 			if (bsc->sc_phyflags & BGEPHYF_5704_A0_BUG)
    832       1.43   msaitoh 				brgphy_5704_a0_bug(sc);
    833       1.68   msaitoh 			if (bsc->sc_phyflags & BGEPHYF_BER_BUG)
    834       1.43   msaitoh 				brgphy_ber_bug(sc);
    835       1.68   msaitoh 			else if (bsc->sc_phyflags & BGEPHYF_JITTER_BUG) {
    836       1.43   msaitoh 				PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x0c00);
    837  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_MII_DSP_ADDR_REG, 0x000a);
    838       1.43   msaitoh 
    839  1.76.28.1  christos 				if (bsc->sc_phyflags
    840       1.68   msaitoh 				    & BGEPHYF_ADJUST_TRIM) {
    841       1.43   msaitoh 					PHY_WRITE(sc, BRGPHY_MII_DSP_RW_PORT,
    842       1.43   msaitoh 					    0x110b);
    843       1.43   msaitoh 					PHY_WRITE(sc, BRGPHY_TEST1,
    844       1.43   msaitoh 					    BRGPHY_TEST1_TRIM_EN | 0x4);
    845       1.43   msaitoh 				} else {
    846       1.43   msaitoh 					PHY_WRITE(sc, BRGPHY_MII_DSP_RW_PORT,
    847       1.43   msaitoh 					    0x010b);
    848       1.43   msaitoh 				}
    849       1.15  jonathan 
    850       1.43   msaitoh 				PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x0400);
    851       1.43   msaitoh 			}
    852       1.68   msaitoh 			if (bsc->sc_phyflags & BGEPHYF_CRC_BUG)
    853       1.43   msaitoh 				brgphy_crc_bug(sc);
    854       1.15  jonathan 
    855       1.43   msaitoh 			/* Set Jumbo frame settings in the PHY. */
    856       1.68   msaitoh 			if (bsc->sc_phyflags & BGEPHYF_JUMBO_CAPABLE)
    857       1.43   msaitoh 				brgphy_jumbo_settings(sc);
    858       1.43   msaitoh 
    859       1.43   msaitoh 			/* Adjust output voltage */
    860       1.61   msaitoh 			if ((sc->mii_mpd_oui == MII_OUI_BROADCOM2)
    861       1.61   msaitoh 			    && (sc->mii_mpd_model == MII_MODEL_BROADCOM2_BCM5906))
    862       1.43   msaitoh 				PHY_WRITE(sc, BRGPHY_MII_EPHY_PTEST, 0x12);
    863       1.43   msaitoh 
    864       1.43   msaitoh 			/* Enable Ethernet@Wirespeed */
    865       1.68   msaitoh 			if (!(bsc->sc_phyflags & BGEPHYF_NO_WIRESPEED))
    866       1.43   msaitoh 				brgphy_eth_wirespeed(sc);
    867       1.43   msaitoh 
    868       1.52   msaitoh #if 0
    869       1.43   msaitoh 			/* Enable Link LED on Dell boxes */
    870       1.68   msaitoh 			if (bsc->sc_phyflags & BGEPHYF_NO_3LED) {
    871  1.76.28.1  christos 				PHY_READ(sc, BRGPHY_MII_PHY_EXTCTL, &reg);
    872  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_MII_PHY_EXTCTL,
    873  1.76.28.1  christos 				    reg & ~BRGPHY_PHY_EXTCTL_3_LED);
    874       1.43   msaitoh 			}
    875       1.43   msaitoh #endif
    876       1.43   msaitoh 		}
    877       1.57       jym 	/* Handle any bnx (NetXtreme II) workarounds. */
    878       1.57       jym 	} else if (bsc->sc_isbnx) {
    879  1.76.28.1  christos 		uint32_t chip_num = _BNX_CHIP_NUM(bsc->sc_chipid);
    880  1.76.28.1  christos 		uint32_t chip_id = _BNX_CHIP_ID(bsc->sc_chipid);
    881  1.76.28.1  christos 		uint32_t chip_rev = _BNX_CHIP_REV(bsc->sc_chipid);
    882  1.76.28.1  christos 
    883       1.61   msaitoh 		if ((sc->mii_mpd_oui == MII_OUI_BROADCOM2)
    884       1.61   msaitoh 		    && sc->mii_mpd_model == MII_MODEL_BROADCOM2_BCM5708S) {
    885  1.76.28.1  christos 			/*
    886  1.76.28.1  christos 			 * Store autoneg capabilities/results in digital block
    887  1.76.28.1  christos 			 * (Page 0)
    888  1.76.28.1  christos 			 */
    889  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_5708S_BLOCK_ADDR,
    890  1.76.28.1  christos 			    BRGPHY_5708S_DIG3_PG2);
    891  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_5708S_PG2_DIGCTL_3_0,
    892  1.76.28.1  christos 			    BRGPHY_5708S_PG2_DIGCTL_3_0_USE_IEEE);
    893  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_5708S_BLOCK_ADDR,
    894  1.76.28.1  christos 			    BRGPHY_5708S_DIG_PG0);
    895       1.43   msaitoh 
    896       1.43   msaitoh 			/* Enable fiber mode and autodetection */
    897  1.76.28.1  christos 			PHY_READ(sc, BRGPHY_5708S_PG0_1000X_CTL1, &reg);
    898  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_5708S_PG0_1000X_CTL1, reg |
    899  1.76.28.1  christos 			    BRGPHY_5708S_PG0_1000X_CTL1_AUTODET_EN |
    900  1.76.28.1  christos 			    BRGPHY_5708S_PG0_1000X_CTL1_FIBER_MODE);
    901       1.43   msaitoh 
    902       1.43   msaitoh 			/* Enable parallel detection */
    903  1.76.28.1  christos 			PHY_READ(sc, BRGPHY_5708S_PG0_1000X_CTL2, &reg);
    904  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_5708S_PG0_1000X_CTL2,
    905  1.76.28.1  christos 			    reg | BRGPHY_5708S_PG0_1000X_CTL2_PAR_DET_EN);
    906  1.76.28.1  christos 
    907  1.76.28.1  christos 			/*
    908  1.76.28.1  christos 			 * Advertise 2.5G support through next page during
    909  1.76.28.1  christos 			 * autoneg
    910  1.76.28.1  christos 			 */
    911  1.76.28.1  christos 			if (bsc->sc_phyflags & BNX_PHY_2_5G_CAPABLE_FLAG) {
    912  1.76.28.1  christos 				PHY_READ(sc, BRGPHY_5708S_ANEG_NXT_PG_XMIT1,
    913  1.76.28.1  christos 				    &reg);
    914  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_5708S_ANEG_NXT_PG_XMIT1,
    915  1.76.28.1  christos 				    reg | BRGPHY_5708S_ANEG_NXT_PG_XMIT1_25G);
    916  1.76.28.1  christos 			}
    917       1.43   msaitoh 
    918       1.43   msaitoh 			/* Increase TX signal amplitude */
    919  1.76.28.1  christos 			if ((chip_id == BNX_CHIP_ID_5708_A0) ||
    920  1.76.28.1  christos 			    (chip_id == BNX_CHIP_ID_5708_B0) ||
    921  1.76.28.1  christos 			    (chip_id == BNX_CHIP_ID_5708_B1)) {
    922  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_5708S_BLOCK_ADDR,
    923       1.43   msaitoh 					BRGPHY_5708S_TX_MISC_PG5);
    924  1.76.28.1  christos 				PHY_READ(sc, BRGPHY_5708S_PG5_TXACTL1, &reg);
    925  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_5708S_PG5_TXACTL1,
    926  1.76.28.1  christos 				    reg & ~BRGPHY_5708S_PG5_TXACTL1_VCM);
    927  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_5708S_BLOCK_ADDR,
    928       1.43   msaitoh 					BRGPHY_5708S_DIG_PG0);
    929       1.43   msaitoh 			}
    930       1.15  jonathan 
    931  1.76.28.1  christos 			/*
    932  1.76.28.1  christos 			 * Backplanes use special
    933  1.76.28.1  christos 			 * driver/pre-driver/pre-emphasis values.
    934  1.76.28.1  christos 			 */
    935       1.71   msaitoh 			if ((bsc->sc_shared_hwcfg & BNX_SHARED_HW_CFG_PHY_BACKPLANE) &&
    936       1.71   msaitoh 			    (bsc->sc_port_hwcfg & BNX_PORT_HW_CFG_CFG_TXCTL3_MASK)) {
    937  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_5708S_BLOCK_ADDR,
    938  1.76.28.1  christos 				    BRGPHY_5708S_TX_MISC_PG5);
    939  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_5708S_PG5_TXACTL3,
    940  1.76.28.1  christos 				    bsc->sc_port_hwcfg &
    941  1.76.28.1  christos 				    BNX_PORT_HW_CFG_CFG_TXCTL3_MASK);
    942  1.76.28.1  christos 				PHY_WRITE(sc, BRGPHY_5708S_BLOCK_ADDR,
    943  1.76.28.1  christos 				    BRGPHY_5708S_DIG_PG0);
    944       1.43   msaitoh 			}
    945       1.71   msaitoh 		} else if ((sc->mii_mpd_oui == MII_OUI_BROADCOM2)
    946       1.61   msaitoh 		    && (sc->mii_mpd_model ==  MII_MODEL_BROADCOM2_BCM5709S)) {
    947       1.57       jym 			/* Select the SerDes Digital block of the AN MMD. */
    948       1.57       jym 			PHY_WRITE(sc, BRGPHY_BLOCK_ADDR,
    949       1.57       jym 			    BRGPHY_BLOCK_ADDR_SERDES_DIG);
    950       1.57       jym 
    951  1.76.28.1  christos 			PHY_READ(sc, BRGPHY_SERDES_DIG_1000X_CTL1, &reg);
    952       1.57       jym 			PHY_WRITE(sc, BRGPHY_SERDES_DIG_1000X_CTL1,
    953  1.76.28.1  christos 			    (reg & ~BRGPHY_SD_DIG_1000X_CTL1_AUTODET) |
    954       1.57       jym 			    BRGPHY_SD_DIG_1000X_CTL1_FIBER);
    955       1.57       jym 
    956       1.58       jym 			if (bsc->sc_phyflags & BNX_PHY_2_5G_CAPABLE_FLAG) {
    957       1.57       jym 				/* Select the Over 1G block of the AN MMD. */
    958       1.57       jym 				PHY_WRITE(sc, BRGPHY_BLOCK_ADDR,
    959       1.57       jym 				    BRGPHY_BLOCK_ADDR_OVER_1G);
    960       1.57       jym 
    961       1.57       jym 				/*
    962       1.57       jym 				 * Enable autoneg "Next Page" to advertise
    963       1.57       jym 				 * 2.5G support.
    964       1.57       jym 				 */
    965  1.76.28.1  christos 				PHY_READ(sc, BRGPHY_OVER_1G_UNFORMAT_PG1,
    966  1.76.28.1  christos 				    &reg);
    967       1.57       jym 				PHY_WRITE(sc, BRGPHY_OVER_1G_UNFORMAT_PG1,
    968  1.76.28.1  christos 				    reg | BRGPHY_5708S_ANEG_NXT_PG_XMIT1_25G);
    969       1.57       jym 			}
    970       1.57       jym 
    971  1.76.28.1  christos 			/*
    972  1.76.28.1  christos 			 * Select the Multi-Rate Backplane Ethernet block of
    973  1.76.28.1  christos 			 * the AN MMD.
    974  1.76.28.1  christos 			 */
    975  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_BLOCK_ADDR,
    976  1.76.28.1  christos 			    BRGPHY_BLOCK_ADDR_MRBE);
    977  1.76.28.1  christos 
    978  1.76.28.1  christos 			/* Enable MRBE speed autoneg. */
    979  1.76.28.1  christos 			PHY_READ(sc, BRGPHY_MRBE_MSG_PG5_NP, &reg);
    980  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_MRBE_MSG_PG5_NP,
    981  1.76.28.1  christos 			    reg | BRGPHY_MRBE_MSG_PG5_NP_MBRE |
    982  1.76.28.1  christos 			    BRGPHY_MRBE_MSG_PG5_NP_T2);
    983  1.76.28.1  christos 
    984  1.76.28.1  christos 			/* Select the Clause 73 User B0 block of the AN MMD. */
    985  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_BLOCK_ADDR,
    986  1.76.28.1  christos 			    BRGPHY_BLOCK_ADDR_CL73_USER_B0);
    987  1.76.28.1  christos 
    988  1.76.28.1  christos 			/* Enable MRBE speed autoneg. */
    989  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_CL73_USER_B0_MBRE_CTL1,
    990  1.76.28.1  christos 			    BRGPHY_CL73_USER_B0_MBRE_CTL1_NP_AFT_BP |
    991  1.76.28.1  christos 			    BRGPHY_CL73_USER_B0_MBRE_CTL1_STA_MGR |
    992  1.76.28.1  christos 			    BRGPHY_CL73_USER_B0_MBRE_CTL1_ANEG);
    993  1.76.28.1  christos 
    994  1.76.28.1  christos 			PHY_WRITE(sc, BRGPHY_BLOCK_ADDR,
    995  1.76.28.1  christos 			    BRGPHY_BLOCK_ADDR_COMBO_IEEE0);
    996  1.76.28.1  christos 
    997  1.76.28.1  christos 		} else if (chip_num == BNX_CHIP_NUM_5709) {
    998  1.76.28.1  christos 			if ((chip_rev == BNX_CHIP_REV_Ax) ||
    999  1.76.28.1  christos 			    (chip_rev == BNX_CHIP_REV_Bx))
   1000       1.58       jym 				brgphy_disable_early_dac(sc);
   1001       1.58       jym 
   1002       1.58       jym 			/* Set Jumbo frame settings in the PHY. */
   1003       1.58       jym 			brgphy_jumbo_settings(sc);
   1004       1.58       jym 
   1005       1.58       jym 			/* Enable Ethernet@Wirespeed */
   1006       1.58       jym 			brgphy_eth_wirespeed(sc);
   1007       1.43   msaitoh 		} else {
   1008       1.43   msaitoh 			if (!(sc->mii_flags & MIIF_HAVEFIBER)) {
   1009       1.43   msaitoh 				brgphy_ber_bug(sc);
   1010       1.18   hannken 
   1011       1.43   msaitoh 				/* Set Jumbo frame settings in the PHY. */
   1012       1.43   msaitoh 				brgphy_jumbo_settings(sc);
   1013       1.18   hannken 
   1014       1.43   msaitoh 				/* Enable Ethernet@Wirespeed */
   1015       1.43   msaitoh 				brgphy_eth_wirespeed(sc);
   1016       1.43   msaitoh 			}
   1017       1.43   msaitoh 		}
   1018       1.43   msaitoh 	}
   1019       1.34     markd }
   1020       1.34     markd 
   1021       1.16  jonathan /* Turn off tap power management on 5401. */
   1022       1.10   thorpej static void
   1023       1.43   msaitoh brgphy_bcm5401_dspcode(struct mii_softc *sc)
   1024       1.10   thorpej {
   1025       1.10   thorpej 	static const struct {
   1026       1.10   thorpej 		int		reg;
   1027       1.10   thorpej 		uint16_t	val;
   1028       1.10   thorpej 	} dspcode[] = {
   1029       1.16  jonathan 		{ BRGPHY_MII_AUXCTL,		0x0c20 },
   1030       1.10   thorpej 		{ BRGPHY_MII_DSP_ADDR_REG,	0x0012 },
   1031       1.10   thorpej 		{ BRGPHY_MII_DSP_RW_PORT,	0x1804 },
   1032       1.10   thorpej 		{ BRGPHY_MII_DSP_ADDR_REG,	0x0013 },
   1033       1.10   thorpej 		{ BRGPHY_MII_DSP_RW_PORT,	0x1204 },
   1034       1.10   thorpej 		{ BRGPHY_MII_DSP_ADDR_REG,	0x8006 },
   1035       1.10   thorpej 		{ BRGPHY_MII_DSP_RW_PORT,	0x0132 },
   1036       1.10   thorpej 		{ BRGPHY_MII_DSP_ADDR_REG,	0x8006 },
   1037       1.10   thorpej 		{ BRGPHY_MII_DSP_RW_PORT,	0x0232 },
   1038       1.10   thorpej 		{ BRGPHY_MII_DSP_ADDR_REG,	0x201f },
   1039       1.10   thorpej 		{ BRGPHY_MII_DSP_RW_PORT,	0x0a20 },
   1040       1.10   thorpej 		{ 0,				0 },
   1041       1.10   thorpej 	};
   1042       1.10   thorpej 	int i;
   1043       1.10   thorpej 
   1044       1.10   thorpej 	for (i = 0; dspcode[i].reg != 0; i++)
   1045       1.10   thorpej 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
   1046       1.61   msaitoh 	delay(40);
   1047       1.10   thorpej }
   1048       1.10   thorpej 
   1049       1.10   thorpej static void
   1050       1.43   msaitoh brgphy_bcm5411_dspcode(struct mii_softc *sc)
   1051       1.10   thorpej {
   1052       1.10   thorpej 	static const struct {
   1053       1.10   thorpej 		int		reg;
   1054       1.10   thorpej 		uint16_t	val;
   1055       1.10   thorpej 	} dspcode[] = {
   1056       1.10   thorpej 		{ 0x1c,				0x8c23 },
   1057       1.10   thorpej 		{ 0x1c,				0x8ca3 },
   1058       1.10   thorpej 		{ 0x1c,				0x8c23 },
   1059       1.15  jonathan 		{ 0,				0 },
   1060       1.15  jonathan 	};
   1061       1.15  jonathan 	int i;
   1062       1.15  jonathan 
   1063       1.15  jonathan 	for (i = 0; dspcode[i].reg != 0; i++)
   1064       1.15  jonathan 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
   1065       1.15  jonathan }
   1066       1.15  jonathan 
   1067       1.43   msaitoh void
   1068       1.43   msaitoh brgphy_bcm5421_dspcode(struct mii_softc *sc)
   1069       1.43   msaitoh {
   1070       1.43   msaitoh 	uint16_t data;
   1071       1.43   msaitoh 
   1072       1.43   msaitoh 	/* Set Class A mode */
   1073       1.43   msaitoh 	PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x1007);
   1074  1.76.28.1  christos 	PHY_READ(sc, BRGPHY_MII_AUXCTL, &data);
   1075       1.43   msaitoh 	PHY_WRITE(sc, BRGPHY_MII_AUXCTL, data | 0x0400);
   1076       1.43   msaitoh 
   1077       1.43   msaitoh 	/* Set FFE gamma override to -0.125 */
   1078       1.43   msaitoh 	PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x0007);
   1079  1.76.28.1  christos 	PHY_READ(sc, BRGPHY_MII_AUXCTL, &data);
   1080       1.43   msaitoh 	PHY_WRITE(sc, BRGPHY_MII_AUXCTL, data | 0x0800);
   1081       1.43   msaitoh 	PHY_WRITE(sc, BRGPHY_MII_DSP_ADDR_REG, 0x000a);
   1082  1.76.28.1  christos 	PHY_READ(sc, BRGPHY_MII_DSP_RW_PORT, &data);
   1083       1.43   msaitoh 	PHY_WRITE(sc, BRGPHY_MII_DSP_RW_PORT, data | 0x0200);
   1084       1.43   msaitoh }
   1085       1.43   msaitoh 
   1086       1.43   msaitoh void
   1087       1.43   msaitoh brgphy_bcm54k2_dspcode(struct mii_softc *sc)
   1088       1.43   msaitoh {
   1089       1.43   msaitoh 	static const struct {
   1090       1.43   msaitoh 		int		reg;
   1091       1.43   msaitoh 		uint16_t	val;
   1092       1.43   msaitoh 	} dspcode[] = {
   1093       1.43   msaitoh 		{ 4,				0x01e1 },
   1094       1.43   msaitoh 		{ 9,				0x0300 },
   1095       1.43   msaitoh 		{ 0,				0 },
   1096       1.43   msaitoh 	};
   1097       1.43   msaitoh 	int i;
   1098       1.43   msaitoh 
   1099       1.43   msaitoh 	for (i = 0; dspcode[i].reg != 0; i++)
   1100       1.43   msaitoh 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
   1101       1.43   msaitoh }
   1102       1.43   msaitoh 
   1103       1.15  jonathan static void
   1104       1.43   msaitoh brgphy_adc_bug(struct mii_softc *sc)
   1105       1.15  jonathan {
   1106       1.15  jonathan 	static const struct {
   1107       1.15  jonathan 		int		reg;
   1108       1.15  jonathan 		uint16_t	val;
   1109       1.15  jonathan 	} dspcode[] = {
   1110       1.15  jonathan 		{ BRGPHY_MII_AUXCTL,		0x0c00 },
   1111       1.15  jonathan 		{ BRGPHY_MII_DSP_ADDR_REG,	0x201f },
   1112       1.15  jonathan 		{ BRGPHY_MII_DSP_RW_PORT,	0x2aaa },
   1113       1.43   msaitoh 		{ BRGPHY_MII_DSP_ADDR_REG,	0x000a },
   1114       1.43   msaitoh 		{ BRGPHY_MII_DSP_RW_PORT,	0x0323 },
   1115       1.43   msaitoh 		{ BRGPHY_MII_AUXCTL,		0x0400 },
   1116       1.15  jonathan 		{ 0,				0 },
   1117       1.15  jonathan 	};
   1118       1.15  jonathan 	int i;
   1119       1.15  jonathan 
   1120       1.15  jonathan 	for (i = 0; dspcode[i].reg != 0; i++)
   1121       1.15  jonathan 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
   1122       1.15  jonathan }
   1123       1.15  jonathan 
   1124       1.15  jonathan static void
   1125       1.43   msaitoh brgphy_5704_a0_bug(struct mii_softc *sc)
   1126       1.15  jonathan {
   1127       1.15  jonathan 	static const struct {
   1128       1.15  jonathan 		int		reg;
   1129       1.15  jonathan 		uint16_t	val;
   1130       1.15  jonathan 	} dspcode[] = {
   1131       1.15  jonathan 		{ 0x1c,				0x8d68 },
   1132       1.33   msaitoh 		{ 0x1c,				0x8d68 },
   1133       1.10   thorpej 		{ 0,				0 },
   1134       1.10   thorpej 	};
   1135       1.10   thorpej 	int i;
   1136       1.10   thorpej 
   1137       1.10   thorpej 	for (i = 0; dspcode[i].reg != 0; i++)
   1138       1.10   thorpej 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
   1139        1.1   thorpej }
   1140       1.22      cube 
   1141       1.22      cube static void
   1142       1.43   msaitoh brgphy_ber_bug(struct mii_softc *sc)
   1143       1.22      cube {
   1144       1.22      cube 	static const struct {
   1145       1.22      cube 		int		reg;
   1146       1.22      cube 		uint16_t	val;
   1147       1.22      cube 	} dspcode[] = {
   1148       1.22      cube 		{ BRGPHY_MII_AUXCTL,		0x0c00 },
   1149       1.22      cube 		{ BRGPHY_MII_DSP_ADDR_REG,	0x000a },
   1150       1.22      cube 		{ BRGPHY_MII_DSP_RW_PORT,	0x310b },
   1151       1.22      cube 		{ BRGPHY_MII_DSP_ADDR_REG,	0x201f },
   1152       1.22      cube 		{ BRGPHY_MII_DSP_RW_PORT,	0x9506 },
   1153       1.22      cube 		{ BRGPHY_MII_DSP_ADDR_REG,	0x401f },
   1154       1.22      cube 		{ BRGPHY_MII_DSP_RW_PORT,	0x14e2 },
   1155       1.22      cube 		{ BRGPHY_MII_AUXCTL,		0x0400 },
   1156       1.22      cube 		{ 0,				0 },
   1157       1.22      cube 	};
   1158       1.22      cube 	int i;
   1159       1.22      cube 
   1160       1.22      cube 	for (i = 0; dspcode[i].reg != 0; i++)
   1161       1.22      cube 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
   1162       1.22      cube }
   1163       1.34     markd 
   1164       1.43   msaitoh /* BCM5701 A0/B0 CRC bug workaround */
   1165       1.43   msaitoh void
   1166       1.43   msaitoh brgphy_crc_bug(struct mii_softc *sc)
   1167       1.34     markd {
   1168       1.34     markd 	static const struct {
   1169       1.34     markd 		int		reg;
   1170       1.34     markd 		uint16_t	val;
   1171       1.34     markd 	} dspcode[] = {
   1172       1.43   msaitoh 		{ BRGPHY_MII_DSP_ADDR_REG,	0x0a75 },
   1173       1.43   msaitoh 		{ 0x1c,				0x8c68 },
   1174       1.43   msaitoh 		{ 0x1c,				0x8d68 },
   1175       1.43   msaitoh 		{ 0x1c,				0x8c68 },
   1176       1.34     markd 		{ 0,				0 },
   1177       1.34     markd 	};
   1178       1.34     markd 	int i;
   1179       1.34     markd 
   1180       1.34     markd 	for (i = 0; dspcode[i].reg != 0; i++)
   1181       1.34     markd 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
   1182       1.34     markd }
   1183       1.52   msaitoh 
   1184       1.52   msaitoh static void
   1185       1.58       jym brgphy_disable_early_dac(struct mii_softc *sc)
   1186       1.58       jym {
   1187  1.76.28.1  christos 	uint16_t val;
   1188       1.58       jym 
   1189       1.58       jym 	PHY_WRITE(sc, BRGPHY_MII_DSP_ADDR_REG, 0x0f08);
   1190  1.76.28.1  christos 	PHY_READ(sc, BRGPHY_MII_DSP_RW_PORT, &val);
   1191       1.58       jym 	val &= ~(1 << 8);
   1192       1.58       jym 	PHY_WRITE(sc, BRGPHY_MII_DSP_RW_PORT, val);
   1193       1.58       jym 
   1194       1.58       jym }
   1195       1.58       jym 
   1196       1.58       jym static void
   1197       1.52   msaitoh brgphy_jumbo_settings(struct mii_softc *sc)
   1198       1.52   msaitoh {
   1199  1.76.28.1  christos 	uint16_t val;
   1200       1.52   msaitoh 
   1201       1.52   msaitoh 	/* Set Jumbo frame settings in the PHY. */
   1202       1.61   msaitoh 	if ((sc->mii_mpd_oui == MII_OUI_BROADCOM)
   1203       1.61   msaitoh 	    && (sc->mii_mpd_model == MII_MODEL_BROADCOM_BCM5401)) {
   1204       1.52   msaitoh 		/* Cannot do read-modify-write on the BCM5401 */
   1205       1.52   msaitoh 		PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x4c20);
   1206       1.52   msaitoh 	} else {
   1207       1.52   msaitoh 		PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x7);
   1208  1.76.28.1  christos 		PHY_READ(sc, BRGPHY_MII_AUXCTL, &val);
   1209       1.52   msaitoh 		PHY_WRITE(sc, BRGPHY_MII_AUXCTL,
   1210  1.76.28.1  christos 		    val & ~(BRGPHY_AUXCTL_LONG_PKT | 0x7));
   1211       1.52   msaitoh 	}
   1212       1.52   msaitoh 
   1213  1.76.28.1  christos 	PHY_READ(sc, BRGPHY_MII_PHY_EXTCTL, &val);
   1214  1.76.28.1  christos 	PHY_WRITE(sc, BRGPHY_MII_PHY_EXTCTL, val & ~BRGPHY_PHY_EXTCTL_HIGH_LA);
   1215       1.52   msaitoh }
   1216       1.52   msaitoh 
   1217       1.52   msaitoh static void
   1218       1.52   msaitoh brgphy_eth_wirespeed(struct mii_softc *sc)
   1219       1.52   msaitoh {
   1220  1.76.28.1  christos 	uint16_t val;
   1221       1.52   msaitoh 
   1222       1.52   msaitoh 	/* Enable Ethernet@Wirespeed */
   1223       1.52   msaitoh 	PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x7007);
   1224  1.76.28.1  christos 	PHY_READ(sc, BRGPHY_MII_AUXCTL, &val);
   1225  1.76.28.1  christos 	PHY_WRITE(sc, BRGPHY_MII_AUXCTL, val | (1 << 15) | (1 << 4));
   1226       1.52   msaitoh }
   1227