1 /* $NetBSD: ikphy.c,v 1.8 2008/11/17 03:04:27 dyoung Exp $ */
3 /*******************************************************************************
4 Copyright (c) 2001-2005, Intel Corporation
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
17 3. Neither the name of the Intel Corporation nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
34 * Copyright (c) 2006 Manuel Bouyer. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * driver for Intel's i82563 ethernet 10/100/1000 PHY
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: ikphy.c,v 1.8 2008/11/17 03:04:27 dyoung Exp $");
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/device.h>
68 #include <sys/socket.h>
69 #include <sys/errno.h>
72 #include <net/if_media.h>
74 #include <dev/mii/mii.h>
75 #include <dev/mii/miivar.h>
76 #include <dev/mii/miidevs.h>
78 #include <dev/mii/ikphyreg.h>
80 static int ikphymatch(device_t
, cfdata_t
, void *);
81 static void ikphyattach(device_t
, device_t
, void *);
83 CFATTACH_DECL_NEW(ikphy
, sizeof(struct mii_softc
),
84 ikphymatch
, ikphyattach
, mii_phy_detach
, mii_phy_activate
);
86 static int ikphy_service(struct mii_softc
*, struct mii_data
*, int);
87 static void ikphy_status(struct mii_softc
*);
88 static void ikphy_setmedia(struct mii_softc
*);
90 static const struct mii_phy_funcs ikphy_funcs
= {
91 ikphy_service
, ikphy_status
, mii_phy_reset
,
94 static const struct mii_phydesc ikphys
[] = {
95 { MII_OUI_xxMARVELL
, MII_MODEL_xxMARVELL_I82563
,
96 MII_STR_xxMARVELL_I82563
},
103 ikphymatch(device_t parent
, cfdata_t match
, void *aux
)
105 struct mii_attach_args
*ma
= aux
;
107 if (mii_phy_match(ma
, ikphys
) != NULL
)
114 ikphyattach(device_t parent
, device_t self
, void *aux
)
116 struct mii_softc
*sc
= device_private(self
);
117 struct mii_attach_args
*ma
= aux
;
118 struct mii_data
*mii
= ma
->mii_data
;
119 const struct mii_phydesc
*mpd
;
121 mpd
= mii_phy_match(ma
, ikphys
);
122 aprint_naive(": Media interface\n");
123 aprint_normal(": %s, rev. %d\n", mpd
->mpd_name
, MII_REV(ma
->mii_id2
));
126 sc
->mii_inst
= mii
->mii_instance
;
127 sc
->mii_phy
= ma
->mii_phyno
;
128 sc
->mii_funcs
= &ikphy_funcs
;
130 sc
->mii_flags
= ma
->mii_flags
;
131 sc
->mii_anegticks
= MII_ANEGTICKS
;
135 sc
->mii_capabilities
=
136 PHY_READ(sc
, MII_BMSR
) & ma
->mii_capmask
;
137 if (sc
->mii_capabilities
& BMSR_EXTSTAT
)
138 sc
->mii_extcapabilities
= PHY_READ(sc
, MII_EXTSR
);
139 aprint_normal_dev(self
, "");
140 if ((sc
->mii_capabilities
& BMSR_MEDIAMASK
) == 0 &&
141 (sc
->mii_extcapabilities
& EXTSR_MEDIAMASK
) == 0)
142 aprint_error("no media present");
144 mii_phy_add_media(sc
);
149 ikphy_service(struct mii_softc
*sc
, struct mii_data
*mii
, int cmd
)
151 struct ifmedia_entry
*ife
= mii
->mii_media
.ifm_cur
;
157 * If we're not polling our PHY instance, just return.
159 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
)
165 * If the media indicates a different PHY instance,
168 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
) {
169 reg
= PHY_READ(sc
, MII_BMCR
);
170 PHY_WRITE(sc
, MII_BMCR
, reg
| BMCR_ISO
);
175 * If the interface is not up, don't do anything.
177 if ((mii
->mii_ifp
->if_flags
& IFF_UP
) == 0)
185 * If we're not currently selected, just return.
187 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
)
190 if (mii_phy_tick(sc
) == EJUSTRETURN
)
199 /* Update the media status. */
202 /* Callback if something changed. */
203 mii_phy_update(sc
, cmd
);
208 ikphy_setmedia(struct mii_softc
*sc
)
211 struct mii_data
*mii
= sc
->mii_pdata
;
212 struct ifmedia_entry
*ife
= mii
->mii_media
.ifm_cur
;
214 /* Enable CRS on TX for half-duplex operation. */
215 phy_data
= PHY_READ(sc
, GG82563_PHY_MAC_SPEC_CTRL
);
216 phy_data
|= GG82563_MSCR_ASSERT_CRS_ON_TX
;
217 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
218 phy_data
|= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ
;
219 PHY_WRITE(sc
, GG82563_PHY_MAC_SPEC_CTRL
, phy_data
);
221 /* set mdi/mid-x options */
222 phy_data
= PHY_READ(sc
, GG82563_PHY_SPEC_CTRL
);
223 phy_data
&= ~GG82563_PSCR_CROSSOVER_MODE_MASK
;
224 if (IFM_SUBTYPE(ife
->ifm_media
) == IFM_AUTO
)
225 phy_data
|= GG82563_PSCR_CROSSOVER_MODE_AUTO
;
227 phy_data
|= GG82563_PSCR_CROSSOVER_MODE_MDI
;
228 /* set polarity correction */
229 phy_data
&= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE
;
230 PHY_WRITE(sc
, GG82563_PHY_SPEC_CTRL
, phy_data
);
232 /* SW Reset the PHY so all changes take effect */
236 phy_data
= PHY_READ(sc
, GG82563_PHY_SPEC_CTRL_2
);
237 phy_data
&= ~GG82563_PSCR2_REVERSE_AUTO_NEG
;
238 PHY_WRITE(sc
, GG82563_PHY_SPEC_CTRL_2
, phy_data
);
240 /* Enable Electrical Idle on the PHY */
241 phy_data
= PHY_READ(sc
, GG82563_PHY_PWR_MGMT_CTRL
);
242 phy_data
|= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE
;
243 PHY_WRITE(sc
, GG82563_PHY_PWR_MGMT_CTRL
, phy_data
);
245 phy_data
= PHY_READ(sc
, GG82563_PHY_KMRN_MODE_CTRL
);
246 phy_data
&= ~GG82563_KMCR_PASS_FALSE_CARRIER
;
247 PHY_WRITE(sc
, GG82563_PHY_KMRN_MODE_CTRL
, phy_data
);
250 * Workaround: Disable padding in Kumeran interface in the MAC
251 * and in the PHY to avoid CRC errors.
253 phy_data
= PHY_READ(sc
, GG82563_PHY_INBAND_CTRL
);
254 phy_data
|= GG82563_ICR_DIS_PADDING
;
255 PHY_WRITE(sc
, GG82563_PHY_INBAND_CTRL
, phy_data
);
257 mii_phy_setmedia(sc
);
258 if (IFM_SUBTYPE(ife
->ifm_media
) != IFM_AUTO
) {
260 * when not in auto mode, we need to restart nego
261 * anyway, or a switch from a fixed mode to another
262 * fixed mode may not be seen by the switch.
264 PHY_WRITE(sc
, MII_BMCR
,
265 PHY_READ(sc
, MII_BMCR
) | BMCR_STARTNEG
);
267 phy_data
= PHY_READ(sc
, GG82563_PHY_MAC_SPEC_CTRL
);
268 phy_data
&= ~GG82563_MSCR_TX_CLK_MASK
;
269 switch(IFM_SUBTYPE(ife
->ifm_media
)) {
271 phy_data
|= GG82563_MSCR_TX_CLK_10MBPS_2_5MHZ
;
274 phy_data
|= GG82563_MSCR_TX_CLK_100MBPS_25MHZ
;
277 phy_data
|= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ
;
280 phy_data
|= GG82563_MSCR_ASSERT_CRS_ON_TX
;
281 PHY_WRITE(sc
, GG82563_PHY_MAC_SPEC_CTRL
, phy_data
);
285 ikphy_status(struct mii_softc
*sc
)
287 struct mii_data
*mii
= sc
->mii_pdata
;
288 struct ifmedia_entry
*ife
= mii
->mii_media
.ifm_cur
;
289 int pssr
, bmcr
, gtsr
, kmrn
;
291 mii
->mii_media_status
= IFM_AVALID
;
292 mii
->mii_media_active
= IFM_ETHER
;
294 pssr
= PHY_READ(sc
, GG82563_PHY_SPEC_STATUS
);
296 if (pssr
& GG82563_PSSR_LINK
)
297 mii
->mii_media_status
|= IFM_ACTIVE
;
299 bmcr
= PHY_READ(sc
, MII_BMCR
);
300 if (bmcr
& BMCR_ISO
) {
301 mii
->mii_media_active
|= IFM_NONE
;
302 mii
->mii_media_status
= 0;
306 if (bmcr
& BMCR_LOOP
)
307 mii
->mii_media_active
|= IFM_LOOP
;
309 if (bmcr
& BMCR_AUTOEN
) {
311 * The media status bits are only valid of autonegotiation
312 * has completed (or it's disabled).
314 if ((pssr
& GG82563_PSSR_SPEED_DUPLEX_RESOLVED
) == 0) {
315 /* Erg, still trying, I guess... */
316 mii
->mii_media_active
|= IFM_NONE
;
320 switch (pssr
& GG82563_PSSR_SPEED_MASK
) {
321 case GG82563_PSSR_SPEED_1000MBPS
:
322 mii
->mii_media_active
|= IFM_1000_T
;
323 gtsr
= PHY_READ(sc
, MII_100T2SR
);
324 if (gtsr
& GTSR_MS_RES
)
325 mii
->mii_media_active
|= IFM_ETH_MASTER
;
328 case GG82563_PSSR_SPEED_100MBPS
:
329 mii
->mii_media_active
|= IFM_100_TX
;
332 case GG82563_PSSR_SPEED_10MBPS
:
333 mii
->mii_media_active
|= IFM_10_T
;
337 mii
->mii_media_active
|= IFM_NONE
;
338 mii
->mii_media_status
= 0;
342 if (pssr
& GG82563_PSSR_DUPLEX
)
343 mii
->mii_media_active
|=
344 IFM_FDX
| mii_phy_flowstatus(sc
);
346 mii
->mii_media_active
= ife
->ifm_media
;
347 kmrn
= PHY_READ(sc
, GG82563_PHY_KMRN_MODE_CTRL
);
348 if (mii
->mii_media_active
& IFM_FDX
)
349 kmrn
&= ~GG82563_KMCR_PASS_FALSE_CARRIER
;
351 kmrn
|= GG82563_KMCR_PASS_FALSE_CARRIER
;
352 PHY_WRITE(sc
, GG82563_PHY_KMRN_MODE_CTRL
, kmrn
);