Home | History | Annotate | Line # | Download | only in mii
igphy.c revision 1.16.14.1
      1  1.16.14.1      matt /*	$NetBSD: igphy.c,v 1.16.14.1 2010/04/21 00:27:39 matt Exp $	*/
      2        1.1      fvdl 
      3        1.1      fvdl /*
      4        1.1      fvdl  * The Intel copyright applies to the analog register setup, and the
      5        1.1      fvdl  * (currently disabled) SmartSpeed workaround code.
      6        1.1      fvdl  */
      7        1.1      fvdl 
      8        1.1      fvdl /*******************************************************************************
      9        1.1      fvdl 
     10        1.1      fvdl   Copyright (c) 2001-2003, Intel Corporation
     11        1.1      fvdl   All rights reserved.
     12        1.1      fvdl 
     13        1.1      fvdl   Redistribution and use in source and binary forms, with or without
     14        1.1      fvdl   modification, are permitted provided that the following conditions are met:
     15        1.1      fvdl 
     16        1.1      fvdl    1. Redistributions of source code must retain the above copyright notice,
     17        1.1      fvdl       this list of conditions and the following disclaimer.
     18        1.1      fvdl 
     19        1.1      fvdl    2. Redistributions in binary form must reproduce the above copyright
     20        1.1      fvdl       notice, this list of conditions and the following disclaimer in the
     21        1.1      fvdl       documentation and/or other materials provided with the distribution.
     22        1.1      fvdl 
     23        1.1      fvdl    3. Neither the name of the Intel Corporation nor the names of its
     24        1.1      fvdl       contributors may be used to endorse or promote products derived from
     25        1.1      fvdl       this software without specific prior written permission.
     26        1.1      fvdl 
     27        1.1      fvdl   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     28        1.1      fvdl   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29        1.1      fvdl   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30        1.1      fvdl   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     31        1.1      fvdl   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32        1.1      fvdl   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33        1.1      fvdl   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34        1.1      fvdl   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35        1.1      fvdl   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36        1.1      fvdl   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37        1.1      fvdl   POSSIBILITY OF SUCH DAMAGE.
     38        1.1      fvdl 
     39        1.1      fvdl *******************************************************************************/
     40        1.1      fvdl 
     41        1.1      fvdl 
     42        1.1      fvdl /*-
     43        1.1      fvdl  * Copyright (c) 1998, 1999, 2000, 2003 The NetBSD Foundation, Inc.
     44        1.1      fvdl  * All rights reserved.
     45        1.1      fvdl  *
     46        1.1      fvdl  * This code is derived from software contributed to The NetBSD Foundation
     47        1.1      fvdl  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     48        1.1      fvdl  * NASA Ames Research Center, and by Frank van der Linden.
     49        1.1      fvdl  *
     50        1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     51        1.1      fvdl  * modification, are permitted provided that the following conditions
     52        1.1      fvdl  * are met:
     53        1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     54        1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     55        1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     56        1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     57        1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     58        1.1      fvdl  *
     59        1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     60        1.1      fvdl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61        1.1      fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62        1.1      fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     63        1.1      fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64        1.1      fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65        1.1      fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66        1.1      fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67        1.1      fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68        1.1      fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69        1.1      fvdl  * POSSIBILITY OF SUCH DAMAGE.
     70        1.1      fvdl  */
     71        1.1      fvdl 
     72        1.1      fvdl #include <sys/cdefs.h>
     73  1.16.14.1      matt __KERNEL_RCSID(0, "$NetBSD: igphy.c,v 1.16.14.1 2010/04/21 00:27:39 matt Exp $");
     74        1.1      fvdl 
     75        1.1      fvdl #include "opt_mii.h"
     76        1.1      fvdl 
     77        1.1      fvdl #include <sys/param.h>
     78        1.1      fvdl #include <sys/systm.h>
     79        1.1      fvdl #include <sys/kernel.h>
     80        1.1      fvdl #include <sys/device.h>
     81        1.1      fvdl #include <sys/socket.h>
     82        1.1      fvdl #include <sys/errno.h>
     83        1.1      fvdl 
     84        1.1      fvdl #include <net/if.h>
     85        1.1      fvdl #include <net/if_media.h>
     86        1.1      fvdl 
     87        1.1      fvdl #include <dev/mii/mii.h>
     88        1.1      fvdl #include <dev/mii/miivar.h>
     89        1.1      fvdl #include <dev/mii/miidevs.h>
     90        1.1      fvdl #include <dev/mii/igphyreg.h>
     91  1.16.14.1      matt #include <dev/pci/if_wmvar.h>
     92        1.1      fvdl 
     93        1.5   thorpej struct igphy_softc {
     94        1.5   thorpej 	struct mii_softc sc_mii;
     95        1.5   thorpej 	int sc_smartspeed;
     96  1.16.14.1      matt 	uint32_t sc_mactype;
     97  1.16.14.1      matt 	uint32_t sc_macflags;
     98        1.5   thorpej };
     99        1.5   thorpej 
    100        1.1      fvdl static void igphy_reset(struct mii_softc *);
    101        1.1      fvdl static void igphy_load_dspcode(struct mii_softc *);
    102  1.16.14.1      matt static void igphy_load_dspcode_igp3(struct mii_softc *);
    103        1.1      fvdl static void igphy_smartspeed_workaround(struct mii_softc *sc);
    104        1.1      fvdl 
    105       1.16   xtraeme static int	igphymatch(device_t, cfdata_t, void *);
    106       1.16   xtraeme static void	igphyattach(device_t, device_t, void *);
    107        1.1      fvdl 
    108       1.16   xtraeme CFATTACH_DECL_NEW(igphy, sizeof(struct igphy_softc),
    109        1.1      fvdl     igphymatch, igphyattach, mii_phy_detach, mii_phy_activate);
    110        1.1      fvdl 
    111        1.4   thorpej static int	igphy_service(struct mii_softc *, struct mii_data *, int);
    112        1.4   thorpej static void	igphy_status(struct mii_softc *);
    113        1.1      fvdl 
    114        1.4   thorpej static const struct mii_phy_funcs igphy_funcs = {
    115        1.1      fvdl 	igphy_service, igphy_status, igphy_reset,
    116        1.1      fvdl };
    117        1.1      fvdl 
    118        1.4   thorpej static const struct mii_phydesc igphys[] = {
    119        1.1      fvdl 	{ MII_OUI_yyINTEL,		MII_MODEL_yyINTEL_IGP01E1000,
    120        1.1      fvdl 	  MII_STR_yyINTEL_IGP01E1000 },
    121        1.1      fvdl 
    122       1.12   msaitoh 	{ MII_OUI_yyINTEL,		MII_MODEL_yyINTEL_I82566,
    123       1.12   msaitoh 	  MII_STR_yyINTEL_I82566 },
    124       1.12   msaitoh 
    125        1.1      fvdl 	{0,				0,
    126        1.1      fvdl 	 NULL },
    127        1.1      fvdl };
    128        1.1      fvdl 
    129        1.4   thorpej static int
    130       1.16   xtraeme igphymatch(device_t parent, cfdata_t match, void *aux)
    131        1.1      fvdl {
    132        1.1      fvdl 	struct mii_attach_args *ma = aux;
    133        1.1      fvdl 
    134        1.1      fvdl 	if (mii_phy_match(ma, igphys) != NULL)
    135        1.1      fvdl 		return 10;
    136        1.1      fvdl 
    137        1.1      fvdl 	return 0;
    138        1.1      fvdl }
    139        1.1      fvdl 
    140        1.4   thorpej static void
    141       1.16   xtraeme igphyattach(device_t parent, device_t self, void *aux)
    142        1.1      fvdl {
    143        1.7   thorpej 	struct mii_softc *sc = device_private(self);
    144        1.1      fvdl 	struct mii_attach_args *ma = aux;
    145        1.1      fvdl 	struct mii_data *mii = ma->mii_data;
    146        1.1      fvdl 	const struct mii_phydesc *mpd;
    147  1.16.14.1      matt 	struct igphy_softc *igsc = (struct igphy_softc *) sc;
    148  1.16.14.1      matt 	prop_dictionary_t dict;
    149        1.1      fvdl 
    150        1.1      fvdl 	mpd = mii_phy_match(ma, igphys);
    151        1.1      fvdl 	aprint_naive(": Media interface\n");
    152        1.1      fvdl 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    153        1.1      fvdl 
    154  1.16.14.1      matt 	dict = device_properties(parent);
    155  1.16.14.1      matt 	if (!prop_dictionary_get_uint32(dict, "mactype", &igsc->sc_mactype))
    156  1.16.14.1      matt 		aprint_error("WARNING! Failed to get mactype\n");
    157  1.16.14.1      matt 	if (!prop_dictionary_get_uint32(dict, "macflags", &igsc->sc_macflags))
    158  1.16.14.1      matt 		aprint_error("WARNING! Failed to get macflags\n");
    159  1.16.14.1      matt 
    160       1.16   xtraeme 	sc->mii_dev = self;
    161        1.1      fvdl 	sc->mii_inst = mii->mii_instance;
    162        1.1      fvdl 	sc->mii_phy = ma->mii_phyno;
    163        1.1      fvdl 	sc->mii_funcs = &igphy_funcs;
    164        1.1      fvdl 	sc->mii_pdata = mii;
    165        1.1      fvdl 	sc->mii_flags = ma->mii_flags;
    166       1.10  christos 	sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    167        1.1      fvdl 
    168        1.1      fvdl 	PHY_RESET(sc);
    169        1.1      fvdl 
    170        1.1      fvdl 	sc->mii_capabilities =
    171        1.1      fvdl 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    172        1.1      fvdl 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    173        1.1      fvdl 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
    174       1.16   xtraeme 	aprint_normal_dev(self, "");
    175        1.1      fvdl 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    176        1.1      fvdl 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    177        1.1      fvdl 		aprint_error("no media present");
    178        1.1      fvdl 	else
    179        1.1      fvdl 		mii_phy_add_media(sc);
    180        1.1      fvdl 	aprint_normal("\n");
    181       1.13  jmcneill 
    182       1.13  jmcneill 	if (!pmf_device_register(self, NULL, mii_phy_resume))
    183       1.13  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    184        1.1      fvdl }
    185        1.1      fvdl 
    186  1.16.14.1      matt typedef struct {
    187  1.16.14.1      matt 	int reg;
    188  1.16.14.1      matt 	uint16_t val;
    189  1.16.14.1      matt } dspcode;
    190  1.16.14.1      matt 
    191  1.16.14.1      matt static const dspcode igp1code[] = {
    192  1.16.14.1      matt 	{ 0x1f95, 0x0001 },
    193  1.16.14.1      matt 	{ 0x1f71, 0xbd21 },
    194  1.16.14.1      matt 	{ 0x1f79, 0x0018 },
    195  1.16.14.1      matt 	{ 0x1f30, 0x1600 },
    196  1.16.14.1      matt 	{ 0x1f31, 0x0014 },
    197  1.16.14.1      matt 	{ 0x1f32, 0x161c },
    198  1.16.14.1      matt 	{ 0x1f94, 0x0003 },
    199  1.16.14.1      matt 	{ 0x1f96, 0x003f },
    200  1.16.14.1      matt 	{ 0x2010, 0x0008 },
    201  1.16.14.1      matt 	{ 0, 0 },
    202  1.16.14.1      matt };
    203  1.16.14.1      matt 
    204  1.16.14.1      matt static const dspcode igp1code_r2[] = {
    205  1.16.14.1      matt 	{ 0x1f73, 0x0099 },
    206  1.16.14.1      matt 	{ 0, 0 },
    207  1.16.14.1      matt };
    208  1.16.14.1      matt 
    209  1.16.14.1      matt static const dspcode igp3code[] = {
    210  1.16.14.1      matt 	{ 0x2f5b, 0x9018},
    211  1.16.14.1      matt 	{ 0x2f52, 0x0000},
    212  1.16.14.1      matt 	{ 0x2fb1, 0x8b24},
    213  1.16.14.1      matt 	{ 0x2fb2, 0xf8f0},
    214  1.16.14.1      matt 	{ 0x2010, 0x10b0},
    215  1.16.14.1      matt 	{ 0x2011, 0x0000},
    216  1.16.14.1      matt 	{ 0x20dd, 0x249a},
    217  1.16.14.1      matt 	{ 0x20de, 0x00d3},
    218  1.16.14.1      matt 	{ 0x28b4, 0x04ce},
    219  1.16.14.1      matt 	{ 0x2f70, 0x29e4},
    220  1.16.14.1      matt 	{ 0x0000, 0x0140},
    221  1.16.14.1      matt 	{ 0x1f30, 0x1606},
    222  1.16.14.1      matt 	{ 0x1f31, 0xb814},
    223  1.16.14.1      matt 	{ 0x1f35, 0x002a},
    224  1.16.14.1      matt 	{ 0x1f3e, 0x0067},
    225  1.16.14.1      matt 	{ 0x1f54, 0x0065},
    226  1.16.14.1      matt 	{ 0x1f55, 0x002a},
    227  1.16.14.1      matt 	{ 0x1f56, 0x002a},
    228  1.16.14.1      matt 	{ 0x1f72, 0x3fb0},
    229  1.16.14.1      matt 	{ 0x1f76, 0xc0ff},
    230  1.16.14.1      matt 	{ 0x1f77, 0x1dec},
    231  1.16.14.1      matt 	{ 0x1f78, 0xf9ef},
    232  1.16.14.1      matt 	{ 0x1f79, 0x0210},
    233  1.16.14.1      matt 	{ 0x1895, 0x0003},
    234  1.16.14.1      matt 	{ 0x1796, 0x0008},
    235  1.16.14.1      matt 	{ 0x1798, 0xd008},
    236  1.16.14.1      matt 	{ 0x1898, 0xd918},
    237  1.16.14.1      matt 	{ 0x187a, 0x0800},
    238  1.16.14.1      matt 	{ 0x0019, 0x008d},
    239  1.16.14.1      matt 	{ 0x001b, 0x2080},
    240  1.16.14.1      matt 	{ 0x0014, 0x0045},
    241  1.16.14.1      matt 	{ 0x0000, 0x1340},
    242  1.16.14.1      matt 	{ 0, 0 },
    243  1.16.14.1      matt };
    244  1.16.14.1      matt 
    245  1.16.14.1      matt /* DSP patch for igp1 and igp2 */
    246        1.1      fvdl static void
    247        1.1      fvdl igphy_load_dspcode(struct mii_softc *sc)
    248        1.1      fvdl {
    249  1.16.14.1      matt 	struct igphy_softc *igsc = (struct igphy_softc *) sc;
    250  1.16.14.1      matt 	const dspcode *code;
    251  1.16.14.1      matt 	uint16_t reg;
    252        1.1      fvdl 	int i;
    253        1.1      fvdl 
    254  1.16.14.1      matt 	/* This workaround is only for 82541 and 82547 */
    255  1.16.14.1      matt 	switch (igsc->sc_mactype) {
    256  1.16.14.1      matt 	case WM_T_82541:
    257  1.16.14.1      matt 	case WM_T_82547:
    258  1.16.14.1      matt 		code = igp1code;
    259  1.16.14.1      matt 		break;
    260  1.16.14.1      matt 	case WM_T_82541_2:
    261  1.16.14.1      matt 	case WM_T_82547_2:
    262  1.16.14.1      matt 		code = igp1code_r2;
    263  1.16.14.1      matt 		break;
    264  1.16.14.1      matt 	default:
    265  1.16.14.1      matt 		return;	/* byebye */
    266  1.16.14.1      matt 	}
    267  1.16.14.1      matt 
    268  1.16.14.1      matt 	/* Delay after phy reset to enable NVM configuration to load */
    269  1.16.14.1      matt 	delay(20000);
    270  1.16.14.1      matt 
    271  1.16.14.1      matt 	/*
    272  1.16.14.1      matt 	 * Save off the current value of register 0x2F5B to be restored at
    273  1.16.14.1      matt 	 * the end of this routine.
    274  1.16.14.1      matt 	 */
    275  1.16.14.1      matt 	reg = IGPHY_READ(sc, 0x2f5b);
    276  1.16.14.1      matt 
    277  1.16.14.1      matt 	/* Disabled the PHY transmitter */
    278  1.16.14.1      matt 	IGPHY_WRITE(sc, 0x2f5b, 0x0003);
    279  1.16.14.1      matt 
    280  1.16.14.1      matt 	delay(20000);
    281        1.1      fvdl 
    282        1.1      fvdl 	PHY_WRITE(sc, MII_IGPHY_PAGE_SELECT, 0x0000);
    283        1.1      fvdl 	PHY_WRITE(sc, 0x0000, 0x0140);
    284        1.1      fvdl 
    285  1.16.14.1      matt 	delay(5000);
    286        1.1      fvdl 
    287  1.16.14.1      matt 	for (i = 0; !((code[i].reg == 0) && (code[i].val == 0)); i++)
    288  1.16.14.1      matt 		IGPHY_WRITE(sc, code[i].reg, code[i].val);
    289        1.1      fvdl 
    290        1.1      fvdl 	PHY_WRITE(sc, MII_IGPHY_PAGE_SELECT,0x0000);
    291        1.1      fvdl 	PHY_WRITE(sc, 0x0000, 0x3300);
    292  1.16.14.1      matt 
    293  1.16.14.1      matt 	delay(20000);
    294  1.16.14.1      matt 
    295  1.16.14.1      matt 	/* Now enable the transmitter */
    296  1.16.14.1      matt 	IGPHY_WRITE(sc, 0x2f5b, reg);
    297  1.16.14.1      matt }
    298  1.16.14.1      matt 
    299  1.16.14.1      matt static void
    300  1.16.14.1      matt igphy_load_dspcode_igp3(struct mii_softc *sc)
    301  1.16.14.1      matt {
    302  1.16.14.1      matt 	const dspcode *code = igp3code;
    303  1.16.14.1      matt 	int i;
    304  1.16.14.1      matt 
    305  1.16.14.1      matt 	for (i = 0; !((code[i].reg == 0) && (code[i].val == 0)); i++)
    306  1.16.14.1      matt 		IGPHY_WRITE(sc, code[i].reg, code[i].val);
    307        1.1      fvdl }
    308        1.1      fvdl 
    309        1.1      fvdl static void
    310        1.1      fvdl igphy_reset(struct mii_softc *sc)
    311        1.1      fvdl {
    312  1.16.14.1      matt 	struct igphy_softc *igsc = (struct igphy_softc *) sc;
    313        1.1      fvdl 	uint16_t fused, fine, coarse;
    314        1.1      fvdl 
    315        1.1      fvdl 	mii_phy_reset(sc);
    316  1.16.14.1      matt 	delay(150);
    317        1.1      fvdl 
    318  1.16.14.1      matt 	switch (igsc->sc_mactype) {
    319  1.16.14.1      matt 	case WM_T_82541:
    320  1.16.14.1      matt 	case WM_T_82547:
    321  1.16.14.1      matt 	case WM_T_82541_2:
    322  1.16.14.1      matt 	case WM_T_82547_2:
    323  1.16.14.1      matt 		igphy_load_dspcode(sc);
    324  1.16.14.1      matt 		break;
    325  1.16.14.1      matt 	case WM_T_ICH8:
    326  1.16.14.1      matt 	case WM_T_ICH9:
    327  1.16.14.1      matt 		if ((igsc->sc_macflags & WM_F_EEPROM_INVALID) != 0)
    328  1.16.14.1      matt 			igphy_load_dspcode_igp3(sc);
    329  1.16.14.1      matt 		break;
    330  1.16.14.1      matt 	default:	/* Not for ICH10, PCH and 8257[12] */
    331  1.16.14.1      matt 		break;
    332        1.1      fvdl 	}
    333  1.16.14.1      matt 
    334  1.16.14.1      matt 	if (igsc->sc_mactype == WM_T_82547) {
    335  1.16.14.1      matt 		fused = IGPHY_READ(sc, MII_IGPHY_ANALOG_SPARE_FUSE_STATUS);
    336  1.16.14.1      matt 		if ((fused & ANALOG_SPARE_FUSE_ENABLED) == 0) {
    337  1.16.14.1      matt 			fused = IGPHY_READ(sc, MII_IGPHY_ANALOG_FUSE_STATUS);
    338  1.16.14.1      matt 
    339  1.16.14.1      matt 			fine = fused & ANALOG_FUSE_FINE_MASK;
    340  1.16.14.1      matt 			coarse = fused & ANALOG_FUSE_COARSE_MASK;
    341  1.16.14.1      matt 
    342  1.16.14.1      matt 			if (coarse > ANALOG_FUSE_COARSE_THRESH) {
    343  1.16.14.1      matt 				coarse -= ANALOG_FUSE_COARSE_10;
    344  1.16.14.1      matt 				fine -= ANALOG_FUSE_FINE_1;
    345  1.16.14.1      matt 			} else if (coarse == ANALOG_FUSE_COARSE_THRESH)
    346  1.16.14.1      matt 				fine -= ANALOG_FUSE_FINE_10;
    347  1.16.14.1      matt 
    348  1.16.14.1      matt 			fused = (fused & ANALOG_FUSE_POLY_MASK) |
    349  1.16.14.1      matt 			    (fine & ANALOG_FUSE_FINE_MASK) |
    350  1.16.14.1      matt 			    (coarse & ANALOG_FUSE_COARSE_MASK);
    351  1.16.14.1      matt 
    352  1.16.14.1      matt 			IGPHY_WRITE(sc, MII_IGPHY_ANALOG_FUSE_CONTROL, fused);
    353  1.16.14.1      matt 			IGPHY_WRITE(sc, MII_IGPHY_ANALOG_FUSE_BYPASS,
    354  1.16.14.1      matt 			    ANALOG_FUSE_ENABLE_SW_CONTROL);
    355  1.16.14.1      matt 		}
    356  1.16.14.1      matt 	}
    357  1.16.14.1      matt 	PHY_WRITE(sc, MII_IGPHY_PAGE_SELECT, 0x0000);
    358        1.1      fvdl }
    359        1.1      fvdl 
    360        1.1      fvdl 
    361        1.4   thorpej static int
    362        1.1      fvdl igphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    363        1.1      fvdl {
    364        1.1      fvdl 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    365        1.1      fvdl 	uint16_t reg;
    366        1.1      fvdl 
    367        1.1      fvdl 	switch (cmd) {
    368        1.1      fvdl 	case MII_POLLSTAT:
    369        1.1      fvdl 		/*
    370        1.1      fvdl 		 * If we're not polling our PHY instance, just return.
    371        1.1      fvdl 		 */
    372        1.1      fvdl 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    373        1.1      fvdl 			return (0);
    374        1.1      fvdl 		break;
    375        1.1      fvdl 
    376        1.1      fvdl 	case MII_MEDIACHG:
    377        1.1      fvdl 		/*
    378        1.1      fvdl 		 * If the media indicates a different PHY instance,
    379        1.1      fvdl 		 * isolate ourselves.
    380        1.1      fvdl 		 */
    381        1.1      fvdl 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    382        1.1      fvdl 			reg = PHY_READ(sc, MII_BMCR);
    383        1.1      fvdl 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    384        1.1      fvdl 			return (0);
    385        1.1      fvdl 		}
    386        1.1      fvdl 
    387        1.1      fvdl 		/*
    388        1.1      fvdl 		 * If the interface is not up, don't do anything.
    389        1.1      fvdl 		 */
    390        1.1      fvdl 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    391        1.1      fvdl 			break;
    392        1.1      fvdl 
    393       1.11   msaitoh 		reg = PHY_READ(sc, MII_IGPHY_PORT_CTRL);
    394       1.11   msaitoh 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
    395       1.11   msaitoh 			reg |= PSCR_AUTO_MDIX;
    396       1.11   msaitoh 			reg &= ~PSCR_FORCE_MDI_MDIX;
    397       1.11   msaitoh 			PHY_WRITE(sc, MII_IGPHY_PORT_CTRL, reg);
    398       1.11   msaitoh 		} else {
    399       1.11   msaitoh 			reg &= ~(PSCR_AUTO_MDIX | PSCR_FORCE_MDI_MDIX);
    400       1.11   msaitoh 			PHY_WRITE(sc, MII_IGPHY_PORT_CTRL, reg);
    401       1.11   msaitoh 		}
    402       1.11   msaitoh 
    403        1.1      fvdl 		mii_phy_setmedia(sc);
    404        1.1      fvdl 		break;
    405        1.1      fvdl 
    406        1.1      fvdl 	case MII_TICK:
    407        1.1      fvdl 		/*
    408        1.1      fvdl 		 * If we're not currently selected, just return.
    409        1.1      fvdl 		 */
    410        1.1      fvdl 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    411        1.1      fvdl 			return (0);
    412        1.1      fvdl 
    413        1.1      fvdl 		igphy_smartspeed_workaround(sc);
    414        1.1      fvdl 
    415        1.1      fvdl 		if (mii_phy_tick(sc) == EJUSTRETURN)
    416        1.1      fvdl 			return (0);
    417        1.1      fvdl 		break;
    418        1.1      fvdl 
    419        1.1      fvdl 	case MII_DOWN:
    420        1.1      fvdl 		mii_phy_down(sc);
    421        1.1      fvdl 		return (0);
    422        1.1      fvdl 	}
    423        1.1      fvdl 
    424        1.1      fvdl 	/* Update the media status. */
    425        1.1      fvdl 	mii_phy_status(sc);
    426        1.1      fvdl 
    427        1.1      fvdl 	/* Callback if something changed. */
    428        1.1      fvdl 	mii_phy_update(sc, cmd);
    429        1.1      fvdl 	return (0);
    430        1.1      fvdl }
    431        1.1      fvdl 
    432        1.1      fvdl 
    433        1.4   thorpej static void
    434        1.1      fvdl igphy_status(struct mii_softc *sc)
    435        1.1      fvdl {
    436        1.1      fvdl 	struct mii_data *mii = sc->mii_pdata;
    437        1.1      fvdl 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    438        1.1      fvdl 	uint16_t bmcr, pssr, gtsr, bmsr;
    439        1.1      fvdl 
    440        1.1      fvdl 	mii->mii_media_status = IFM_AVALID;
    441        1.1      fvdl 	mii->mii_media_active = IFM_ETHER;
    442        1.1      fvdl 
    443        1.1      fvdl 	pssr = PHY_READ(sc, MII_IGPHY_PORT_STATUS);
    444        1.1      fvdl 
    445        1.1      fvdl 	if (pssr & PSSR_LINK_UP)
    446        1.1      fvdl 		mii->mii_media_status |= IFM_ACTIVE;
    447        1.1      fvdl 
    448        1.1      fvdl 	bmcr = PHY_READ(sc, MII_BMCR);
    449        1.1      fvdl 	if (bmcr & BMCR_ISO) {
    450        1.1      fvdl 		mii->mii_media_active |= IFM_NONE;
    451        1.1      fvdl 		mii->mii_media_status = 0;
    452        1.1      fvdl 		return;
    453        1.1      fvdl 	}
    454        1.1      fvdl 
    455        1.1      fvdl 	if (bmcr & BMCR_LOOP)
    456        1.1      fvdl 		mii->mii_media_active |= IFM_LOOP;
    457        1.1      fvdl 
    458        1.1      fvdl 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    459        1.1      fvdl 
    460        1.1      fvdl 	/*
    461        1.1      fvdl 	 * XXX can't check if the info is valid, no
    462        1.1      fvdl 	 * 'negotiation done' bit?
    463        1.1      fvdl 	 */
    464        1.1      fvdl 	if (bmcr & BMCR_AUTOEN) {
    465        1.1      fvdl 		if ((bmsr & BMSR_ACOMP) == 0) {
    466        1.1      fvdl 			mii->mii_media_active |= IFM_NONE;
    467        1.1      fvdl 			return;
    468        1.1      fvdl 		}
    469        1.1      fvdl 		switch (pssr & PSSR_SPEED_MASK) {
    470        1.1      fvdl 		case PSSR_SPEED_1000MBPS:
    471        1.1      fvdl 			mii->mii_media_active |= IFM_1000_T;
    472        1.1      fvdl 			gtsr = PHY_READ(sc, MII_100T2SR);
    473        1.1      fvdl 			if (gtsr & GTSR_MS_RES)
    474        1.1      fvdl 				mii->mii_media_active |= IFM_ETH_MASTER;
    475        1.1      fvdl 			break;
    476        1.1      fvdl 
    477        1.1      fvdl 		case PSSR_SPEED_100MBPS:
    478        1.1      fvdl 			mii->mii_media_active |= IFM_100_TX;
    479        1.1      fvdl 			break;
    480        1.1      fvdl 
    481        1.1      fvdl 		case PSSR_SPEED_10MBPS:
    482        1.1      fvdl 			mii->mii_media_active |= IFM_10_T;
    483        1.1      fvdl 			break;
    484        1.1      fvdl 
    485        1.1      fvdl 		default:
    486        1.1      fvdl 			mii->mii_media_active |= IFM_NONE;
    487        1.1      fvdl 			mii->mii_media_status = 0;
    488        1.1      fvdl 			return;
    489        1.1      fvdl 		}
    490        1.1      fvdl 
    491        1.1      fvdl 		if (pssr & PSSR_FULL_DUPLEX)
    492        1.2   thorpej 			mii->mii_media_active |=
    493        1.3   thorpej 			    IFM_FDX | mii_phy_flowstatus(sc);
    494        1.1      fvdl 	} else
    495        1.1      fvdl 		mii->mii_media_active = ife->ifm_media;
    496        1.1      fvdl }
    497        1.1      fvdl 
    498        1.1      fvdl static void
    499        1.1      fvdl igphy_smartspeed_workaround(struct mii_softc *sc)
    500        1.1      fvdl {
    501        1.5   thorpej 	struct igphy_softc *igsc = (struct igphy_softc *) sc;
    502        1.5   thorpej 	uint16_t reg, gtsr, gtcr;
    503        1.5   thorpej 
    504  1.16.14.1      matt 
    505  1.16.14.1      matt 	/* This workaround is only for 82541 and 82547 */
    506  1.16.14.1      matt 	switch (igsc->sc_mactype) {
    507  1.16.14.1      matt 	case WM_T_82541:
    508  1.16.14.1      matt 	case WM_T_82541_2:
    509  1.16.14.1      matt 	case WM_T_82547:
    510  1.16.14.1      matt 	case WM_T_82547_2:
    511  1.16.14.1      matt 		break;
    512  1.16.14.1      matt 	default:
    513  1.16.14.1      matt 		/* byebye */
    514  1.16.14.1      matt 		return;
    515  1.16.14.1      matt 	}
    516  1.16.14.1      matt 
    517        1.5   thorpej 	if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
    518        1.5   thorpej 		return;
    519        1.5   thorpej 
    520        1.5   thorpej 	/* XXX Assume 1000TX-FDX is advertized if doing autonegotiation. */
    521        1.1      fvdl 
    522        1.1      fvdl 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    523        1.5   thorpej 	if ((reg & BMSR_LINK) == 0) {
    524        1.5   thorpej 		switch (igsc->sc_smartspeed) {
    525        1.1      fvdl 		case 0:
    526        1.1      fvdl 			gtsr = PHY_READ(sc, MII_100T2SR);
    527        1.1      fvdl 			if (!(gtsr & GTSR_MAN_MS_FLT))
    528        1.1      fvdl 				break;
    529        1.1      fvdl 			gtsr = PHY_READ(sc, MII_100T2SR);
    530        1.1      fvdl 			if (gtsr & GTSR_MAN_MS_FLT) {
    531        1.1      fvdl 				gtcr = PHY_READ(sc, MII_100T2CR);
    532        1.1      fvdl 				if (gtcr & GTCR_MAN_MS) {
    533        1.1      fvdl 					gtcr &= ~GTCR_MAN_MS;
    534        1.1      fvdl 					PHY_WRITE(sc, MII_100T2CR,
    535        1.1      fvdl 					    gtcr);
    536        1.1      fvdl 				}
    537        1.1      fvdl 				mii_phy_auto(sc, 0);
    538        1.1      fvdl 			}
    539        1.1      fvdl 			break;
    540        1.1      fvdl 		case IGPHY_TICK_DOWNSHIFT:
    541        1.1      fvdl 			gtcr = PHY_READ(sc, MII_100T2CR);
    542        1.1      fvdl 			gtcr |= GTCR_MAN_MS;
    543        1.1      fvdl 			PHY_WRITE(sc, MII_100T2CR, gtcr);
    544        1.1      fvdl 			mii_phy_auto(sc, 0);
    545        1.1      fvdl 			break;
    546        1.1      fvdl 		default:
    547        1.1      fvdl 			break;
    548        1.1      fvdl 		}
    549        1.5   thorpej 		if (igsc->sc_smartspeed++ == IGPHY_TICK_MAX)
    550        1.5   thorpej 			igsc->sc_smartspeed = 0;
    551        1.5   thorpej 	} else
    552        1.5   thorpej 		igsc->sc_smartspeed = 0;
    553        1.1      fvdl }
    554