1 /* $NetBSD: igphy.c,v 1.19 2009/12/16 04:50:35 msaitoh Exp $ */
4 * The Intel copyright applies to the analog register setup, and the
5 * (currently disabled) SmartSpeed workaround code.
8 /*******************************************************************************
10 Copyright (c) 2001-2003, Intel Corporation
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
16 1. Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
23 3. Neither the name of the Intel Corporation nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
39 *******************************************************************************/
43 * Copyright (c) 1998, 1999, 2000, 2003 The NetBSD Foundation, Inc.
44 * All rights reserved.
46 * This code is derived from software contributed to The NetBSD Foundation
47 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
48 * NASA Ames Research Center, and by Frank van der Linden.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: igphy.c,v 1.19 2009/12/16 04:50:35 msaitoh Exp $");
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/device.h>
81 #include <sys/socket.h>
82 #include <sys/errno.h>
85 #include <net/if_media.h>
87 #include <dev/mii/mii.h>
88 #include <dev/mii/miivar.h>
89 #include <dev/mii/miidevs.h>
90 #include <dev/mii/igphyreg.h>
91 #include <dev/pci/if_wmvar.h>
94 struct mii_softc sc_mii
;
100 static void igphy_reset(struct mii_softc
*);
101 static void igphy_load_dspcode(struct mii_softc
*);
102 static void igphy_load_dspcode_igp3(struct mii_softc
*);
103 static void igphy_smartspeed_workaround(struct mii_softc
*sc
);
105 static int igphymatch(device_t
, cfdata_t
, void *);
106 static void igphyattach(device_t
, device_t
, void *);
108 CFATTACH_DECL_NEW(igphy
, sizeof(struct igphy_softc
),
109 igphymatch
, igphyattach
, mii_phy_detach
, mii_phy_activate
);
111 static int igphy_service(struct mii_softc
*, struct mii_data
*, int);
112 static void igphy_status(struct mii_softc
*);
114 static const struct mii_phy_funcs igphy_funcs
= {
115 igphy_service
, igphy_status
, igphy_reset
,
118 static const struct mii_phydesc igphys
[] = {
119 { MII_OUI_yyINTEL
, MII_MODEL_yyINTEL_IGP01E1000
,
120 MII_STR_yyINTEL_IGP01E1000
},
122 { MII_OUI_yyINTEL
, MII_MODEL_yyINTEL_I82566
,
123 MII_STR_yyINTEL_I82566
},
130 igphymatch(device_t parent
, cfdata_t match
, void *aux
)
132 struct mii_attach_args
*ma
= aux
;
134 if (mii_phy_match(ma
, igphys
) != NULL
)
141 igphyattach(device_t parent
, device_t self
, void *aux
)
143 struct mii_softc
*sc
= device_private(self
);
144 struct mii_attach_args
*ma
= aux
;
145 struct mii_data
*mii
= ma
->mii_data
;
146 const struct mii_phydesc
*mpd
;
147 struct igphy_softc
*igsc
= (struct igphy_softc
*) sc
;
148 prop_dictionary_t dict
;
150 mpd
= mii_phy_match(ma
, igphys
);
151 aprint_naive(": Media interface\n");
152 aprint_normal(": %s, rev. %d\n", mpd
->mpd_name
, MII_REV(ma
->mii_id2
));
154 dict
= device_properties(parent
);
155 if (!prop_dictionary_get_uint32(dict
, "mactype", &igsc
->sc_mactype
))
156 aprint_error("WARNING! Failed to get mactype\n");
157 if (!prop_dictionary_get_uint32(dict
, "macflags", &igsc
->sc_macflags
))
158 aprint_error("WARNING! Failed to get macflags\n");
161 sc
->mii_inst
= mii
->mii_instance
;
162 sc
->mii_phy
= ma
->mii_phyno
;
163 sc
->mii_funcs
= &igphy_funcs
;
165 sc
->mii_flags
= ma
->mii_flags
;
166 sc
->mii_anegticks
= MII_ANEGTICKS_GIGE
;
170 sc
->mii_capabilities
=
171 PHY_READ(sc
, MII_BMSR
) & ma
->mii_capmask
;
172 if (sc
->mii_capabilities
& BMSR_EXTSTAT
)
173 sc
->mii_extcapabilities
= PHY_READ(sc
, MII_EXTSR
);
174 aprint_normal_dev(self
, "");
175 if ((sc
->mii_capabilities
& BMSR_MEDIAMASK
) == 0 &&
176 (sc
->mii_extcapabilities
& EXTSR_MEDIAMASK
) == 0)
177 aprint_error("no media present");
179 mii_phy_add_media(sc
);
188 static const dspcode igp1code
[] = {
201 static const dspcode igp1code_r2
[] = {
206 static const dspcode igp3code
[] = {
242 /* DSP patch for igp1 and igp2 */
244 igphy_load_dspcode(struct mii_softc
*sc
)
246 struct igphy_softc
*igsc
= (struct igphy_softc
*) sc
;
251 /* This workaround is only for 82541 and 82547 */
252 switch (igsc
->sc_mactype
) {
265 /* Delay after phy reset to enable NVM configuration to load */
269 * Save off the current value of register 0x2F5B to be restored at
270 * the end of this routine.
272 reg
= IGPHY_READ(sc
, 0x2f5b);
274 /* Disabled the PHY transmitter */
275 IGPHY_WRITE(sc
, 0x2f5b, 0x0003);
279 PHY_WRITE(sc
, MII_IGPHY_PAGE_SELECT
, 0x0000);
280 PHY_WRITE(sc
, 0x0000, 0x0140);
284 for (i
= 0; !((code
[i
].reg
== 0) && (code
[i
].val
== 0)); i
++)
285 IGPHY_WRITE(sc
, code
[i
].reg
, code
[i
].val
);
287 PHY_WRITE(sc
, MII_IGPHY_PAGE_SELECT
,0x0000);
288 PHY_WRITE(sc
, 0x0000, 0x3300);
292 /* Now enable the transmitter */
293 IGPHY_WRITE(sc
, 0x2f5b, reg
);
297 igphy_load_dspcode_igp3(struct mii_softc
*sc
)
299 const dspcode
*code
= igp3code
;
302 for (i
= 0; !((code
[i
].reg
== 0) && (code
[i
].val
== 0)); i
++)
303 IGPHY_WRITE(sc
, code
[i
].reg
, code
[i
].val
);
307 igphy_reset(struct mii_softc
*sc
)
309 struct igphy_softc
*igsc
= (struct igphy_softc
*) sc
;
310 uint16_t fused
, fine
, coarse
;
315 switch (igsc
->sc_mactype
) {
320 igphy_load_dspcode(sc
);
324 if ((igsc
->sc_macflags
& WM_F_EEPROM_INVALID
) != 0)
325 igphy_load_dspcode_igp3(sc
);
327 default: /* Not for ICH10, PCH and 8257[12] */
331 if (igsc
->sc_mactype
== WM_T_82547
) {
332 fused
= IGPHY_READ(sc
, MII_IGPHY_ANALOG_SPARE_FUSE_STATUS
);
333 if ((fused
& ANALOG_SPARE_FUSE_ENABLED
) == 0) {
334 fused
= IGPHY_READ(sc
, MII_IGPHY_ANALOG_FUSE_STATUS
);
336 fine
= fused
& ANALOG_FUSE_FINE_MASK
;
337 coarse
= fused
& ANALOG_FUSE_COARSE_MASK
;
339 if (coarse
> ANALOG_FUSE_COARSE_THRESH
) {
340 coarse
-= ANALOG_FUSE_COARSE_10
;
341 fine
-= ANALOG_FUSE_FINE_1
;
342 } else if (coarse
== ANALOG_FUSE_COARSE_THRESH
)
343 fine
-= ANALOG_FUSE_FINE_10
;
345 fused
= (fused
& ANALOG_FUSE_POLY_MASK
) |
346 (fine
& ANALOG_FUSE_FINE_MASK
) |
347 (coarse
& ANALOG_FUSE_COARSE_MASK
);
349 IGPHY_WRITE(sc
, MII_IGPHY_ANALOG_FUSE_CONTROL
, fused
);
350 IGPHY_WRITE(sc
, MII_IGPHY_ANALOG_FUSE_BYPASS
,
351 ANALOG_FUSE_ENABLE_SW_CONTROL
);
354 PHY_WRITE(sc
, MII_IGPHY_PAGE_SELECT
, 0x0000);
359 igphy_service(struct mii_softc
*sc
, struct mii_data
*mii
, int cmd
)
361 struct ifmedia_entry
*ife
= mii
->mii_media
.ifm_cur
;
367 * If we're not polling our PHY instance, just return.
369 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
)
375 * If the media indicates a different PHY instance,
378 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
) {
379 reg
= PHY_READ(sc
, MII_BMCR
);
380 PHY_WRITE(sc
, MII_BMCR
, reg
| BMCR_ISO
);
385 * If the interface is not up, don't do anything.
387 if ((mii
->mii_ifp
->if_flags
& IFF_UP
) == 0)
390 reg
= PHY_READ(sc
, MII_IGPHY_PORT_CTRL
);
391 if (IFM_SUBTYPE(ife
->ifm_media
) == IFM_AUTO
) {
392 reg
|= PSCR_AUTO_MDIX
;
393 reg
&= ~PSCR_FORCE_MDI_MDIX
;
394 PHY_WRITE(sc
, MII_IGPHY_PORT_CTRL
, reg
);
396 reg
&= ~(PSCR_AUTO_MDIX
| PSCR_FORCE_MDI_MDIX
);
397 PHY_WRITE(sc
, MII_IGPHY_PORT_CTRL
, reg
);
400 mii_phy_setmedia(sc
);
405 * If we're not currently selected, just return.
407 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
)
410 igphy_smartspeed_workaround(sc
);
412 if (mii_phy_tick(sc
) == EJUSTRETURN
)
421 /* Update the media status. */
424 /* Callback if something changed. */
425 mii_phy_update(sc
, cmd
);
431 igphy_status(struct mii_softc
*sc
)
433 struct mii_data
*mii
= sc
->mii_pdata
;
434 struct ifmedia_entry
*ife
= mii
->mii_media
.ifm_cur
;
435 uint16_t bmcr
, pssr
, gtsr
, bmsr
;
437 mii
->mii_media_status
= IFM_AVALID
;
438 mii
->mii_media_active
= IFM_ETHER
;
440 pssr
= PHY_READ(sc
, MII_IGPHY_PORT_STATUS
);
442 if (pssr
& PSSR_LINK_UP
)
443 mii
->mii_media_status
|= IFM_ACTIVE
;
445 bmcr
= PHY_READ(sc
, MII_BMCR
);
446 if (bmcr
& BMCR_ISO
) {
447 mii
->mii_media_active
|= IFM_NONE
;
448 mii
->mii_media_status
= 0;
452 if (bmcr
& BMCR_LOOP
)
453 mii
->mii_media_active
|= IFM_LOOP
;
455 bmsr
= PHY_READ(sc
, MII_BMSR
) | PHY_READ(sc
, MII_BMSR
);
458 * XXX can't check if the info is valid, no
459 * 'negotiation done' bit?
461 if (bmcr
& BMCR_AUTOEN
) {
462 if ((bmsr
& BMSR_ACOMP
) == 0) {
463 mii
->mii_media_active
|= IFM_NONE
;
466 switch (pssr
& PSSR_SPEED_MASK
) {
467 case PSSR_SPEED_1000MBPS
:
468 mii
->mii_media_active
|= IFM_1000_T
;
469 gtsr
= PHY_READ(sc
, MII_100T2SR
);
470 if (gtsr
& GTSR_MS_RES
)
471 mii
->mii_media_active
|= IFM_ETH_MASTER
;
474 case PSSR_SPEED_100MBPS
:
475 mii
->mii_media_active
|= IFM_100_TX
;
478 case PSSR_SPEED_10MBPS
:
479 mii
->mii_media_active
|= IFM_10_T
;
483 mii
->mii_media_active
|= IFM_NONE
;
484 mii
->mii_media_status
= 0;
488 if (pssr
& PSSR_FULL_DUPLEX
)
489 mii
->mii_media_active
|=
490 IFM_FDX
| mii_phy_flowstatus(sc
);
492 mii
->mii_media_active
= ife
->ifm_media
;
496 igphy_smartspeed_workaround(struct mii_softc
*sc
)
498 struct igphy_softc
*igsc
= (struct igphy_softc
*) sc
;
499 uint16_t reg
, gtsr
, gtcr
;
502 /* This workaround is only for 82541 and 82547 */
503 switch (igsc
->sc_mactype
) {
514 if ((PHY_READ(sc
, MII_BMCR
) & BMCR_AUTOEN
) == 0)
517 /* XXX Assume 1000TX-FDX is advertized if doing autonegotiation. */
519 reg
= PHY_READ(sc
, MII_BMSR
) | PHY_READ(sc
, MII_BMSR
);
520 if ((reg
& BMSR_LINK
) == 0) {
521 switch (igsc
->sc_smartspeed
) {
523 gtsr
= PHY_READ(sc
, MII_100T2SR
);
524 if (!(gtsr
& GTSR_MAN_MS_FLT
))
526 gtsr
= PHY_READ(sc
, MII_100T2SR
);
527 if (gtsr
& GTSR_MAN_MS_FLT
) {
528 gtcr
= PHY_READ(sc
, MII_100T2CR
);
529 if (gtcr
& GTCR_MAN_MS
) {
530 gtcr
&= ~GTCR_MAN_MS
;
531 PHY_WRITE(sc
, MII_100T2CR
,
537 case IGPHY_TICK_DOWNSHIFT
:
538 gtcr
= PHY_READ(sc
, MII_100T2CR
);
540 PHY_WRITE(sc
, MII_100T2CR
, gtcr
);
546 if (igsc
->sc_smartspeed
++ == IGPHY_TICK_MAX
)
547 igsc
->sc_smartspeed
= 0;
549 igsc
->sc_smartspeed
= 0;