Home | History | Annotate | Line # | Download | only in mii
makphy.c revision 1.42.14.1
      1  1.42.14.1  pgoyette /*	$NetBSD: makphy.c,v 1.42.14.1 2018/06/25 07:25:51 pgoyette Exp $	*/
      2  1.42.14.1  pgoyette /*	$OpenBSD: eephy.c,v 1.56 2015/03/14 03:38:48 jsg Exp $	*/
      3        1.1   thorpej 
      4        1.1   thorpej /*-
      5        1.1   thorpej  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
      6        1.1   thorpej  * All rights reserved.
      7        1.1   thorpej  *
      8        1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      9        1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10        1.1   thorpej  * NASA Ames Research Center.
     11        1.1   thorpej  *
     12        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     13        1.1   thorpej  * modification, are permitted provided that the following conditions
     14        1.1   thorpej  * are met:
     15        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     16        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     17        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     18        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     19        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     20        1.1   thorpej  *
     21        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22        1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     32        1.1   thorpej  */
     33        1.1   thorpej 
     34        1.1   thorpej /*
     35        1.1   thorpej  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     36        1.1   thorpej  *
     37        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     38        1.1   thorpej  * modification, are permitted provided that the following conditions
     39        1.1   thorpej  * are met:
     40        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     41        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     42        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     43        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     44        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     45        1.1   thorpej  *
     46        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     47        1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     48        1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     49        1.1   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     50        1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     51        1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     52        1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     53        1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     54        1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     55        1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     56        1.1   thorpej  */
     57        1.1   thorpej 
     58        1.1   thorpej /*
     59  1.42.14.1  pgoyette  * Principal Author: Parag Patel
     60  1.42.14.1  pgoyette  * Copyright (c) 2001
     61  1.42.14.1  pgoyette  * All rights reserved.
     62  1.42.14.1  pgoyette  *
     63  1.42.14.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     64  1.42.14.1  pgoyette  * modification, are permitted provided that the following conditions
     65  1.42.14.1  pgoyette  * are met:
     66  1.42.14.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     67  1.42.14.1  pgoyette  *    notice unmodified, this list of conditions, and the following
     68  1.42.14.1  pgoyette  *    disclaimer.
     69  1.42.14.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     70  1.42.14.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     71  1.42.14.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     72  1.42.14.1  pgoyette  *
     73  1.42.14.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     74  1.42.14.1  pgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     75  1.42.14.1  pgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     76  1.42.14.1  pgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     77  1.42.14.1  pgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     78  1.42.14.1  pgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     79  1.42.14.1  pgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     80  1.42.14.1  pgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     81  1.42.14.1  pgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     82  1.42.14.1  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     83  1.42.14.1  pgoyette  * SUCH DAMAGE.
     84  1.42.14.1  pgoyette  *
     85  1.42.14.1  pgoyette  * Additonal Copyright (c) 2001 by Traakan Software under same licence.
     86  1.42.14.1  pgoyette  * Secondary Author: Matthew Jacob
     87  1.42.14.1  pgoyette  */
     88  1.42.14.1  pgoyette /*
     89        1.1   thorpej  * Driver for the Marvell 88E1000 ``Alaska'' 10/100/1000 PHY.
     90        1.1   thorpej  */
     91        1.4     lukem 
     92  1.42.14.1  pgoyette /*
     93  1.42.14.1  pgoyette  * Support added for the Marvell 88E1011 (Alaska) 1000/100/10baseTX and
     94  1.42.14.1  pgoyette  * 1000baseSX PHY.
     95  1.42.14.1  pgoyette  * Nathan Binkert <nate (at) openbsd.org>
     96  1.42.14.1  pgoyette  */
     97  1.42.14.1  pgoyette 
     98        1.4     lukem #include <sys/cdefs.h>
     99  1.42.14.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: makphy.c,v 1.42.14.1 2018/06/25 07:25:51 pgoyette Exp $");
    100        1.1   thorpej 
    101        1.1   thorpej #include <sys/param.h>
    102        1.1   thorpej #include <sys/systm.h>
    103        1.1   thorpej #include <sys/kernel.h>
    104        1.1   thorpej #include <sys/device.h>
    105        1.1   thorpej #include <sys/socket.h>
    106        1.1   thorpej #include <sys/errno.h>
    107        1.1   thorpej 
    108        1.1   thorpej #include <net/if.h>
    109        1.1   thorpej #include <net/if_media.h>
    110        1.1   thorpej 
    111        1.1   thorpej #include <dev/mii/mii.h>
    112        1.1   thorpej #include <dev/mii/miivar.h>
    113        1.1   thorpej #include <dev/mii/miidevs.h>
    114        1.1   thorpej 
    115  1.42.14.1  pgoyette #include <dev/mii/e1000phyreg.h>
    116        1.1   thorpej 
    117       1.28   xtraeme static int	makphymatch(device_t, cfdata_t, void *);
    118       1.28   xtraeme static void	makphyattach(device_t, device_t, void *);
    119        1.1   thorpej 
    120       1.28   xtraeme CFATTACH_DECL_NEW(makphy, sizeof(struct mii_softc),
    121       1.11   thorpej     makphymatch, makphyattach, mii_phy_detach, mii_phy_activate);
    122        1.1   thorpej 
    123       1.15   thorpej static int	makphy_service(struct mii_softc *, struct mii_data *, int);
    124       1.15   thorpej static void	makphy_status(struct mii_softc *);
    125       1.23   msaitoh static void	makphy_reset(struct mii_softc *);
    126        1.1   thorpej 
    127  1.42.14.1  pgoyette static const struct mii_phy_funcs eephy_funcs = {
    128       1.23   msaitoh 	makphy_service, makphy_status, makphy_reset,
    129        1.1   thorpej };
    130        1.1   thorpej 
    131  1.42.14.1  pgoyette static const struct mii_phydesc eephys[] = {
    132  1.42.14.1  pgoyette 	{ MII_OUI_MARVELL,		MII_MODEL_MARVELL_E1000_0,
    133  1.42.14.1  pgoyette 	  MII_STR_MARVELL_E1000_0 },
    134  1.42.14.1  pgoyette 
    135  1.42.14.1  pgoyette 	{ MII_OUI_MARVELL,		MII_MODEL_MARVELL_E1000_3,
    136  1.42.14.1  pgoyette 	  MII_STR_MARVELL_E1000_3 },
    137  1.42.14.1  pgoyette 
    138  1.42.14.1  pgoyette 	{ MII_OUI_MARVELL,		MII_MODEL_MARVELL_E1000_5,
    139  1.42.14.1  pgoyette 	  MII_STR_MARVELL_E1000_5 },
    140  1.42.14.1  pgoyette 
    141  1.42.14.1  pgoyette 	{ MII_OUI_MARVELL,		MII_MODEL_MARVELL_E1000_6,
    142  1.42.14.1  pgoyette 	  MII_STR_MARVELL_E1000_6 },
    143  1.42.14.1  pgoyette 
    144        1.5   thorpej 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1000_3,
    145        1.5   thorpej 	  MII_STR_xxMARVELL_E1000_3 },
    146        1.5   thorpej 
    147        1.5   thorpej 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1000_5,
    148        1.5   thorpej 	  MII_STR_xxMARVELL_E1000_5 },
    149        1.8      fvdl 
    150  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1000S,
    151  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_E1000S },
    152  1.42.14.1  pgoyette 
    153        1.8      fvdl 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1011,
    154        1.8      fvdl 	  MII_STR_xxMARVELL_E1011 },
    155        1.1   thorpej 
    156       1.16    briggs 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1111,
    157       1.16    briggs 	  MII_STR_xxMARVELL_E1111 },
    158       1.16    briggs 
    159  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1112,
    160  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_E1112 },
    161  1.42.14.1  pgoyette 
    162       1.25       wiz 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1116,
    163       1.25       wiz 	  MII_STR_xxMARVELL_E1116 },
    164       1.25       wiz 
    165       1.34  kiyohara 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1116R,
    166       1.34  kiyohara 	  MII_STR_xxMARVELL_E1116R },
    167       1.34  kiyohara 
    168       1.36    sekiya 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1116R_29,
    169       1.36    sekiya 	  MII_STR_xxMARVELL_E1116R_29 },
    170       1.36    sekiya 
    171  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1118,
    172  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_E1118 },
    173  1.42.14.1  pgoyette 
    174  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1145,
    175  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_E1145 },
    176  1.42.14.1  pgoyette 
    177  1.42.14.1  pgoyette 	/* XXX: reported not to work on eg. HP XW9400 */
    178  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1149,
    179  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_E1149 },
    180  1.42.14.1  pgoyette 
    181  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1149R,
    182  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_E1149R },
    183  1.42.14.1  pgoyette 
    184       1.38  kiyohara 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1543,
    185       1.38  kiyohara 	  MII_STR_xxMARVELL_E1543 },
    186       1.38  kiyohara 
    187  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E3016,
    188  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_E3016 },
    189  1.42.14.1  pgoyette 
    190  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E3082,
    191  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_E3082 },
    192  1.42.14.1  pgoyette 
    193  1.42.14.1  pgoyette 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_PHYG65G,
    194  1.42.14.1  pgoyette 	  MII_STR_xxMARVELL_PHYG65G },
    195        1.1   thorpej 	{ 0,				0,
    196        1.1   thorpej 	  NULL },
    197        1.1   thorpej };
    198        1.1   thorpej 
    199       1.15   thorpej static int
    200       1.28   xtraeme makphymatch(device_t parent, cfdata_t match, void *aux)
    201        1.1   thorpej {
    202        1.1   thorpej 	struct mii_attach_args *ma = aux;
    203        1.1   thorpej 
    204  1.42.14.1  pgoyette 	if (mii_phy_match(ma, eephys) != NULL)
    205        1.1   thorpej 		return (10);
    206        1.1   thorpej 
    207        1.1   thorpej 	return (0);
    208        1.1   thorpej }
    209        1.1   thorpej 
    210       1.15   thorpej static void
    211       1.28   xtraeme makphyattach(device_t parent, device_t self, void *aux)
    212        1.1   thorpej {
    213       1.18   thorpej 	struct mii_softc *sc = device_private(self);
    214        1.1   thorpej 	struct mii_attach_args *ma = aux;
    215        1.1   thorpej 	struct mii_data *mii = ma->mii_data;
    216        1.1   thorpej 	const struct mii_phydesc *mpd;
    217        1.1   thorpej 
    218  1.42.14.1  pgoyette 	mpd = mii_phy_match(ma, eephys);
    219       1.12   thorpej 	aprint_naive(": Media interface\n");
    220       1.12   thorpej 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    221        1.1   thorpej 
    222       1.28   xtraeme 	sc->mii_dev = self;
    223       1.42   msaitoh 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    224       1.39  christos 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    225       1.42   msaitoh 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    226        1.1   thorpej 	sc->mii_inst = mii->mii_instance;
    227        1.1   thorpej 	sc->mii_phy = ma->mii_phyno;
    228  1.42.14.1  pgoyette 	sc->mii_funcs = &eephy_funcs;
    229        1.1   thorpej 	sc->mii_pdata = mii;
    230        1.6   thorpej 	sc->mii_flags = ma->mii_flags;
    231       1.22  christos 	sc->mii_anegticks = MII_ANEGTICKS;
    232        1.1   thorpej 
    233  1.42.14.1  pgoyette 	/* XXX No loopback support yet, although the hardware can do it. */
    234  1.42.14.1  pgoyette 	sc->mii_flags |= MIIF_NOLOOP;
    235  1.42.14.1  pgoyette 
    236  1.42.14.1  pgoyette 	/* Make sure page 0 is selected. */
    237  1.42.14.1  pgoyette 	PHY_WRITE(sc, E1000_EADR, 0);
    238  1.42.14.1  pgoyette 
    239  1.42.14.1  pgoyette 	/* Switch to copper-only mode if necessary. */
    240  1.42.14.1  pgoyette 	if (sc->mii_mpd_model == MII_MODEL_MARVELL_E1111 &&
    241  1.42.14.1  pgoyette 	    (sc->mii_flags & MIIF_HAVEFIBER) == 0) {
    242       1.39  christos 		/*
    243  1.42.14.1  pgoyette 		 * The onboard 88E1111 PHYs on the Sun X4100 M2 come
    244  1.42.14.1  pgoyette 		 * up with fiber/copper auto-selection enabled, even
    245  1.42.14.1  pgoyette 		 * though the machine only has copper ports.  This
    246  1.42.14.1  pgoyette 		 * makes the chip autoselect to 1000baseX, and makes
    247  1.42.14.1  pgoyette 		 * it impossible to select any other media.  So
    248  1.42.14.1  pgoyette 		 * disable fiber/copper autoselection.
    249       1.39  christos 		 */
    250  1.42.14.1  pgoyette 		int reg = PHY_READ(sc, E1000_ESSR);
    251  1.42.14.1  pgoyette 		if ((reg & E1000_ESSR_HWCFG_MODE) == E1000_ESSR_RGMII_COPPER) {
    252  1.42.14.1  pgoyette 			reg |= E1000_ESSR_DIS_FC;
    253  1.42.14.1  pgoyette 			PHY_WRITE(sc, E1000_ESSR, reg);
    254  1.42.14.1  pgoyette 		}
    255  1.42.14.1  pgoyette 	}
    256  1.42.14.1  pgoyette 
    257  1.42.14.1  pgoyette 	/* Switch to fiber-only mode if necessary. */
    258  1.42.14.1  pgoyette 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1112 &&
    259  1.42.14.1  pgoyette 	    sc->mii_flags & MIIF_HAVEFIBER) {
    260  1.42.14.1  pgoyette 		int page = PHY_READ(sc, E1000_EADR);
    261  1.42.14.1  pgoyette 		PHY_WRITE(sc, E1000_EADR, 2);
    262  1.42.14.1  pgoyette 		int reg = PHY_READ(sc, E1000_SCR);
    263  1.42.14.1  pgoyette 		reg &= ~E1000_SCR_MODE_MASK;
    264  1.42.14.1  pgoyette 		reg |= E1000_SCR_MODE_1000BX;
    265  1.42.14.1  pgoyette 		PHY_WRITE(sc, E1000_SCR, reg);
    266  1.42.14.1  pgoyette 		PHY_WRITE(sc, E1000_EADR, page);
    267       1.39  christos 	}
    268       1.39  christos 
    269        1.1   thorpej 	PHY_RESET(sc);
    270        1.1   thorpej 
    271       1.41   msaitoh 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    272        1.1   thorpej 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    273        1.1   thorpej 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
    274        1.1   thorpej 
    275       1.28   xtraeme 	aprint_normal_dev(self, "");
    276        1.1   thorpej 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    277        1.1   thorpej 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    278       1.12   thorpej 		aprint_error("no media present");
    279        1.1   thorpej 	else
    280        1.1   thorpej 		mii_phy_add_media(sc);
    281       1.12   thorpej 	aprint_normal("\n");
    282        1.1   thorpej }
    283        1.1   thorpej 
    284       1.23   msaitoh static void
    285       1.23   msaitoh makphy_reset(struct mii_softc *sc)
    286       1.23   msaitoh {
    287  1.42.14.1  pgoyette 	int reg, i;
    288  1.42.14.1  pgoyette 
    289  1.42.14.1  pgoyette 	reg = PHY_READ(sc, MII_BMCR);
    290  1.42.14.1  pgoyette 	reg |= BMCR_RESET;
    291  1.42.14.1  pgoyette 	PHY_WRITE(sc, MII_BMCR, reg);
    292  1.42.14.1  pgoyette 
    293  1.42.14.1  pgoyette 	for (i = 0; i < 500; i++) {
    294  1.42.14.1  pgoyette 		DELAY(1);
    295  1.42.14.1  pgoyette 		reg = PHY_READ(sc, MII_BMCR);
    296  1.42.14.1  pgoyette 		if (!(reg & BMCR_RESET))
    297  1.42.14.1  pgoyette 			break;
    298  1.42.14.1  pgoyette 	}
    299  1.42.14.1  pgoyette 
    300  1.42.14.1  pgoyette 	/*
    301  1.42.14.1  pgoyette 	 * Initialize PHY Specific Control Register.
    302  1.42.14.1  pgoyette 	 */
    303  1.42.14.1  pgoyette 	reg = PHY_READ(sc, E1000_SCR);
    304  1.42.14.1  pgoyette 
    305  1.42.14.1  pgoyette 	/* Assert CRS on transmit. */
    306  1.42.14.1  pgoyette 	reg |= E1000_SCR_ASSERT_CRS_ON_TX;
    307       1.23   msaitoh 
    308  1.42.14.1  pgoyette 	/* Enable auto crossover. */
    309  1.42.14.1  pgoyette 	switch (sc->mii_mpd_model) {
    310  1.42.14.1  pgoyette 	case MII_MODEL_xxMARVELL_E3016:
    311  1.42.14.1  pgoyette 	case MII_MODEL_xxMARVELL_E3082:
    312  1.42.14.1  pgoyette 		/* Bits are in a different position.  */
    313  1.42.14.1  pgoyette 		reg |= (E1000_SCR_AUTO_X_MODE >> 1);
    314  1.42.14.1  pgoyette 		break;
    315  1.42.14.1  pgoyette 	default:
    316  1.42.14.1  pgoyette 		/* Automatic crossover causes problems for 1000baseX. */
    317  1.42.14.1  pgoyette 		if (sc->mii_flags & MIIF_IS_1000X)
    318  1.42.14.1  pgoyette 			reg &= ~E1000_SCR_AUTO_X_MODE;
    319  1.42.14.1  pgoyette 		else
    320  1.42.14.1  pgoyette 			reg |= E1000_SCR_AUTO_X_MODE;
    321  1.42.14.1  pgoyette 	}
    322  1.42.14.1  pgoyette 
    323  1.42.14.1  pgoyette 	/* Disable energy detect; only available on some models. */
    324  1.42.14.1  pgoyette 	switch(sc->mii_mpd_model) {
    325  1.42.14.1  pgoyette 	case MII_MODEL_xxMARVELL_E3016:
    326  1.42.14.1  pgoyette 		reg &= ~E3000_SCR_EN_DETECT_MASK;
    327  1.42.14.1  pgoyette 		break;
    328  1.42.14.1  pgoyette 	case MII_MODEL_MARVELL_E1011:
    329  1.42.14.1  pgoyette 	case MII_MODEL_MARVELL_E1111:
    330  1.42.14.1  pgoyette 	case MII_MODEL_xxMARVELL_E1112:
    331  1.42.14.1  pgoyette 	case MII_MODEL_xxMARVELL_PHYG65G:
    332  1.42.14.1  pgoyette 		reg &= ~E1000_SCR_EN_DETECT_MASK;
    333  1.42.14.1  pgoyette 		break;
    334  1.42.14.1  pgoyette 	}
    335  1.42.14.1  pgoyette 
    336  1.42.14.1  pgoyette 	/* Enable scrambler if necessary. */
    337  1.42.14.1  pgoyette 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
    338  1.42.14.1  pgoyette 		reg &= ~E3000_SCR_SCRAMBLER_DISABLE;
    339  1.42.14.1  pgoyette 
    340  1.42.14.1  pgoyette 	/*
    341  1.42.14.1  pgoyette 	 * Store next page in the Link Partner Next Page register for
    342  1.42.14.1  pgoyette 	 * compatibility with 802.3ab.
    343  1.42.14.1  pgoyette 	 */
    344  1.42.14.1  pgoyette 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
    345  1.42.14.1  pgoyette 		reg |= E3000_SCR_REG8_NEXT_PAGE;
    346  1.42.14.1  pgoyette 
    347  1.42.14.1  pgoyette 	PHY_WRITE(sc, E1000_SCR, reg);
    348  1.42.14.1  pgoyette 
    349  1.42.14.1  pgoyette 	/* 25 MHz TX_CLK should always work. */
    350  1.42.14.1  pgoyette 	reg = PHY_READ(sc, E1000_ESCR);
    351  1.42.14.1  pgoyette 	reg |= E1000_ESCR_TX_CLK_25;
    352  1.42.14.1  pgoyette 	PHY_WRITE(sc, E1000_ESCR, reg);
    353  1.42.14.1  pgoyette 
    354  1.42.14.1  pgoyette 	/* Configure LEDs if they were left unconfigured. */
    355  1.42.14.1  pgoyette 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016 &&
    356  1.42.14.1  pgoyette 	    PHY_READ(sc, 0x16) == 0) {
    357  1.42.14.1  pgoyette 		reg = (0x0b << 8) | (0x05 << 4) | 0x04;	/* XXX */
    358  1.42.14.1  pgoyette 		PHY_WRITE(sc, 0x16, reg);
    359  1.42.14.1  pgoyette 	}
    360       1.23   msaitoh 
    361  1.42.14.1  pgoyette 	/*
    362  1.42.14.1  pgoyette 	 * Do a software reset for these settings to take effect.
    363  1.42.14.1  pgoyette 	 * Disable autonegotiation, such that all capabilities get
    364  1.42.14.1  pgoyette 	 * advertised when it is switched back on.
    365  1.42.14.1  pgoyette 	 */
    366  1.42.14.1  pgoyette 	reg = PHY_READ(sc, MII_BMCR);
    367  1.42.14.1  pgoyette 	reg &= ~BMCR_AUTOEN;
    368  1.42.14.1  pgoyette 	PHY_WRITE(sc, MII_BMCR, reg | BMCR_RESET);
    369       1.23   msaitoh }
    370       1.23   msaitoh 
    371       1.15   thorpej static int
    372        1.3   thorpej makphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    373        1.1   thorpej {
    374        1.1   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    375  1.42.14.1  pgoyette 	int bmcr;
    376  1.42.14.1  pgoyette 
    377  1.42.14.1  pgoyette 	if (!device_is_active(sc->mii_dev))
    378  1.42.14.1  pgoyette 		return (ENXIO);
    379        1.1   thorpej 
    380        1.1   thorpej 	switch (cmd) {
    381        1.1   thorpej 	case MII_POLLSTAT:
    382        1.1   thorpej 		/*
    383        1.1   thorpej 		 * If we're not polling our PHY instance, just return.
    384        1.1   thorpej 		 */
    385        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    386        1.1   thorpej 			return (0);
    387        1.1   thorpej 		break;
    388        1.1   thorpej 
    389        1.1   thorpej 	case MII_MEDIACHG:
    390        1.1   thorpej 		/*
    391        1.1   thorpej 		 * If the media indicates a different PHY instance,
    392        1.1   thorpej 		 * isolate ourselves.
    393        1.1   thorpej 		 */
    394        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    395  1.42.14.1  pgoyette 			bmcr = PHY_READ(sc, MII_BMCR);
    396  1.42.14.1  pgoyette 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    397        1.1   thorpej 			return (0);
    398        1.1   thorpej 		}
    399        1.1   thorpej 
    400        1.1   thorpej 		/*
    401        1.1   thorpej 		 * If the interface is not up, don't do anything.
    402        1.1   thorpej 		 */
    403        1.1   thorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    404        1.1   thorpej 			break;
    405        1.1   thorpej 
    406        1.1   thorpej 		mii_phy_setmedia(sc);
    407  1.42.14.1  pgoyette 
    408  1.42.14.1  pgoyette 		/*
    409  1.42.14.1  pgoyette 		 * If autonegitation is not enabled, we need a
    410  1.42.14.1  pgoyette 		 * software reset for the settings to take effect.
    411  1.42.14.1  pgoyette 		 */
    412       1.20    bouyer 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
    413  1.42.14.1  pgoyette 			bmcr = PHY_READ(sc, MII_BMCR);
    414  1.42.14.1  pgoyette 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET);
    415       1.20    bouyer 		}
    416        1.1   thorpej 		break;
    417        1.1   thorpej 
    418        1.1   thorpej 	case MII_TICK:
    419        1.1   thorpej 		/*
    420        1.1   thorpej 		 * If we're not currently selected, just return.
    421        1.1   thorpej 		 */
    422        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    423        1.1   thorpej 			return (0);
    424        1.1   thorpej 
    425        1.1   thorpej 		if (mii_phy_tick(sc) == EJUSTRETURN)
    426        1.1   thorpej 			return (0);
    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.1   thorpej 		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.1   thorpej 	/* Callback if something changed. */
    438        1.1   thorpej 	mii_phy_update(sc, cmd);
    439        1.1   thorpej 	return (0);
    440        1.1   thorpej }
    441        1.1   thorpej 
    442       1.15   thorpej static void
    443        1.3   thorpej makphy_status(struct mii_softc *sc)
    444        1.1   thorpej {
    445        1.1   thorpej 	struct mii_data *mii = sc->mii_pdata;
    446  1.42.14.1  pgoyette 	int bmcr, gsr, ssr;
    447        1.1   thorpej 
    448        1.1   thorpej 	mii->mii_media_status = IFM_AVALID;
    449        1.1   thorpej 	mii->mii_media_active = IFM_ETHER;
    450        1.1   thorpej 
    451  1.42.14.1  pgoyette 	bmcr = PHY_READ(sc, MII_BMCR);
    452  1.42.14.1  pgoyette 	ssr = PHY_READ(sc, E1000_SSR);
    453        1.1   thorpej 
    454  1.42.14.1  pgoyette 	if (ssr & E1000_SSR_LINK)
    455        1.1   thorpej 		mii->mii_media_status |= IFM_ACTIVE;
    456        1.1   thorpej 
    457        1.1   thorpej 	if (bmcr & BMCR_ISO) {
    458        1.1   thorpej 		mii->mii_media_active |= IFM_NONE;
    459        1.1   thorpej 		mii->mii_media_status = 0;
    460        1.1   thorpej 		return;
    461        1.1   thorpej 	}
    462        1.1   thorpej 
    463        1.1   thorpej 	if (bmcr & BMCR_LOOP)
    464        1.1   thorpej 		mii->mii_media_active |= IFM_LOOP;
    465        1.1   thorpej 
    466  1.42.14.1  pgoyette 	if (!(ssr & E1000_SSR_SPD_DPLX_RESOLVED)) {
    467  1.42.14.1  pgoyette 		/* Erg, still trying, I guess... */
    468  1.42.14.1  pgoyette 		mii->mii_media_active |= IFM_NONE;
    469  1.42.14.1  pgoyette 		return;
    470  1.42.14.1  pgoyette 	}
    471        1.1   thorpej 
    472  1.42.14.1  pgoyette 	if (sc->mii_flags & MIIF_IS_1000X) {
    473  1.42.14.1  pgoyette 		mii->mii_media_active |= IFM_1000_SX;
    474  1.42.14.1  pgoyette 	} else {
    475  1.42.14.1  pgoyette 		if (ssr & E1000_SSR_1000MBS)
    476        1.1   thorpej 			mii->mii_media_active |= IFM_1000_T;
    477  1.42.14.1  pgoyette 		else if (ssr & E1000_SSR_100MBS)
    478        1.1   thorpej 			mii->mii_media_active |= IFM_100_TX;
    479  1.42.14.1  pgoyette 		else
    480        1.1   thorpej 			mii->mii_media_active |= IFM_10_T;
    481  1.42.14.1  pgoyette 	}
    482        1.1   thorpej 
    483  1.42.14.1  pgoyette 	if (ssr & E1000_SSR_DUPLEX)
    484  1.42.14.1  pgoyette 		mii->mii_media_active |= mii_phy_flowstatus(sc) | IFM_FDX;
    485  1.42.14.1  pgoyette 	else
    486  1.42.14.1  pgoyette 		mii->mii_media_active |= IFM_HDX;
    487        1.1   thorpej 
    488  1.42.14.1  pgoyette 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
    489  1.42.14.1  pgoyette 		gsr = PHY_READ(sc, MII_100T2SR) | PHY_READ(sc, MII_100T2SR);
    490  1.42.14.1  pgoyette 		if (gsr & GTSR_MS_RES)
    491  1.42.14.1  pgoyette 			mii->mii_media_active |= IFM_ETH_MASTER;
    492  1.42.14.1  pgoyette 	}
    493        1.1   thorpej }
    494