1 /* $FreeBSD: src/sys/dev/mii/e1000phy.c,v 1.1.2.2 2002/11/08 21:53:49 semenu Exp $ */
2 /* $DragonFly: src/sys/dev/netif/mii_layer/e1000phy.c,v 1.9 2006/12/22 23:26:20 swildner Exp $ */
3 /* $OpenBSD: eephy.c,v 1.26 2006/06/08 00:27:12 brad Exp $ */
5 * Principal Author: Parag Patel
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Additonal Copyright (c) 2001 by Traakan Software under same licence.
32 * Secondary Author: Matthew Jacob
36 * driver for the Marvell 88E1000 series external 1000/100/10-BT PHY.
40 * Support added for the Marvell 88E1011 (Alaska) 1000/100/10baseTX and
42 * Nathan Binkert <nate@openbsd.org>
43 * Jung-uk Kim <jkim@niksun.com>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
53 #include <net/if_media.h>
59 #include "e1000phyreg.h"
61 #include "miibus_if.h"
63 static int e1000phy_probe(device_t);
64 static int e1000phy_attach(device_t);
65 static int e1000phy_service(struct mii_softc *, struct mii_data *, int);
66 static void e1000phy_status(struct mii_softc *);
67 static int e1000phy_mii_phy_auto(struct mii_softc *, int);
68 static void e1000phy_reset(struct mii_softc *);
70 static device_method_t e1000phy_methods[] = {
71 /* device interface */
72 DEVMETHOD(device_probe, e1000phy_probe),
73 DEVMETHOD(device_attach, e1000phy_attach),
74 DEVMETHOD(device_detach, ukphy_detach),
75 DEVMETHOD(device_shutdown, bus_generic_shutdown),
79 static const struct mii_phydesc e1000phys[] = {
80 MII_PHYDESC(xxMARVELL, E1000_2),
81 MII_PHYDESC(xxMARVELL, E1000_3),
82 MII_PHYDESC(xxMARVELL, E1000_5),
83 MII_PHYDESC(xxMARVELL, E1111),
84 MII_PHYDESC(MARVELL, E1000),
85 MII_PHYDESC(MARVELL, E1011),
86 MII_PHYDESC(MARVELL, E1000_3),
87 MII_PHYDESC(MARVELL, E1000_4),
88 MII_PHYDESC(MARVELL, E1000_5),
89 MII_PHYDESC(MARVELL, E1000_6),
90 MII_PHYDESC(MARVELL, E1111),
91 MII_PHYDESC(MARVELL, E1116),
95 static devclass_t e1000phy_devclass;
97 static driver_t e1000phy_driver = {
100 sizeof(struct mii_softc)
102 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
105 e1000phy_probe(device_t dev)
107 struct mii_attach_args *ma = device_get_ivars(dev);
108 const struct mii_phydesc *mpd;
110 mpd = mii_phy_match(ma, e1000phys);
112 device_set_desc(dev, mpd->mpd_name);
119 e1000phy_attach(device_t dev)
121 struct mii_softc *sc;
122 struct mii_attach_args *ma;
123 struct mii_data *mii;
124 const char *sep = "";
126 sc = device_get_softc(dev);
127 ma = device_get_ivars(dev);
128 mii_softc_init(sc, ma);
129 sc->mii_dev = device_get_parent(dev);
130 mii = device_get_softc(sc->mii_dev);
131 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
133 sc->mii_inst = mii->mii_instance;
134 sc->mii_service = e1000phy_service;
135 sc->mii_reset = e1000phy_reset;
136 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
139 sc->mii_flags |= MIIF_NOISOLATE;
141 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_MARVELL &&
142 MII_MODEL(ma->mii_id2) == MII_MODEL_MARVELL_E1011 &&
143 (PHY_READ(sc, E1000_ESSR) & E1000_ESSR_FIBER_LINK))
144 sc->mii_flags |= MIIF_HAVEFIBER;
150 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
151 #define PRINT(s) kprintf("%s%s", sep, s); sep = ", "
154 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
158 device_printf(dev, " ");
159 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) {
160 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst),
161 E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX);
162 PRINT("1000baseTX-FDX");
164 TODO - apparently 1000BT-simplex not supported?
165 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
166 E1000_CR_SPEED_1000);
169 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
170 E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX);
171 PRINT("100baseTX-FDX");
172 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
175 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
176 E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX);
177 PRINT("10baseTX-FDX");
178 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
182 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst),
183 E1000_CR_SPEED_1000);
184 PRINT("1000baseSX-FDX");
187 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
194 MIIBUS_MEDIAINIT(sc->mii_dev);
199 e1000phy_reset(struct mii_softc *sc)
204 /* initialize custom E1000 registers to magic values */
205 reg = PHY_READ(sc, E1000_SCR);
206 reg &= ~E1000_SCR_AUTO_X_MODE;
207 PHY_WRITE(sc, E1000_SCR, reg);
209 /* normal PHY reset */
210 /*mii_phy_reset(sc);*/
211 reg = PHY_READ(sc, E1000_CR);
212 reg |= E1000_CR_RESET;
213 PHY_WRITE(sc, E1000_CR, reg);
215 for (i = 0; i < 500; i++) {
217 reg = PHY_READ(sc, E1000_CR);
218 if (!(reg & E1000_CR_RESET))
222 /* set more custom E1000 registers to magic values */
223 reg = PHY_READ(sc, E1000_SCR);
224 reg |= E1000_SCR_ASSERT_CRS_ON_TX;
225 PHY_WRITE(sc, E1000_SCR, reg);
227 reg = PHY_READ(sc, E1000_ESCR);
228 reg |= E1000_ESCR_TX_CLK_25;
229 PHY_WRITE(sc, E1000_ESCR, reg);
231 /* even more magic to reset DSP? */
232 PHY_WRITE(sc, 29, 0x1d);
233 PHY_WRITE(sc, 30, 0xc1);
234 PHY_WRITE(sc, 30, 0x00);
238 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
240 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
246 * If we're not polling our PHY instance, just return.
248 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
254 * If the media indicates a different PHY instance,
257 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
258 reg = PHY_READ(sc, E1000_CR);
259 PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE);
264 * If the interface is not up, don't do anything.
266 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
270 switch (IFM_SUBTYPE(ife->ifm_media)) {
273 * If we're already in auto mode, just return.
275 if (sc->mii_flags & MIIF_DOINGAUTO) {
279 e1000phy_mii_phy_auto(sc, 1);
285 PHY_WRITE(sc, E1000_CR,
286 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_1000);
287 PHY_WRITE(sc, E1000_AR, E1000_FA_1000X_FD);
291 if (sc->mii_flags & MIIF_DOINGAUTO)
296 /* TODO - any other way to force 1000BT? */
297 e1000phy_mii_phy_auto(sc, 1);
303 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
304 PHY_WRITE(sc, E1000_CR,
305 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100);
306 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD);
308 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
309 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
316 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
317 PHY_WRITE(sc, E1000_CR,
318 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10);
319 PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD);
321 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
322 PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
335 * If we're not currently selected, just return.
337 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
342 * Is the interface even up?
344 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
349 * Only used for autonegotiation.
351 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
356 * Check to see if we have link. If we do, we don't
357 * need to restart the autonegotiation process. Read
358 * the BMSR twice in case it's latched.
360 reg = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
361 if (reg & E1000_SR_LINK_STATUS)
365 * Only retry autonegotiation every mii_anegticks seconds.
367 if (++(sc->mii_ticks) != sc->mii_anegticks) {
374 if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN) {
381 /* Update the media status. */
384 /* Callback if something changed. */
385 mii_phy_update(sc, cmd);
390 e1000phy_status(struct mii_softc *sc)
392 struct mii_data *mii = sc->mii_pdata;
393 int bmsr, bmcr, esr, ssr, isr, ar, lpar;
395 mii->mii_media_status = IFM_AVALID;
396 mii->mii_media_active = IFM_ETHER;
398 bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
399 esr = PHY_READ(sc, E1000_ESR);
400 bmcr = PHY_READ(sc, E1000_CR);
401 ssr = PHY_READ(sc, E1000_SSR);
402 isr = PHY_READ(sc, E1000_ISR);
403 ar = PHY_READ(sc, E1000_AR);
404 lpar = PHY_READ(sc, E1000_LPAR);
406 if (bmsr & E1000_SR_LINK_STATUS)
407 mii->mii_media_status |= IFM_ACTIVE;
409 if (bmcr & E1000_CR_LOOPBACK)
410 mii->mii_media_active |= IFM_LOOP;
412 if ((sc->mii_flags & MIIF_DOINGAUTO) &&
413 (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
414 !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
415 /* Erg, still trying, I guess... */
416 mii->mii_media_active |= IFM_NONE;
420 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) {
421 if (ssr & E1000_SSR_1000MBS)
422 mii->mii_media_active |= IFM_1000_T;
423 else if (ssr & E1000_SSR_100MBS)
424 mii->mii_media_active |= IFM_100_TX;
426 mii->mii_media_active |= IFM_10_T;
428 if (ssr & E1000_SSR_1000MBS)
429 mii->mii_media_active |= IFM_1000_SX;
432 if (ssr & E1000_SSR_DUPLEX)
433 mii->mii_media_active |= IFM_FDX;
435 mii->mii_media_active |= IFM_HDX;
437 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) {
438 /* FLAG0==rx-flow-control FLAG1==tx-flow-control */
439 if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
440 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
441 } else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
442 (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
443 mii->mii_media_active |= IFM_FLAG1;
444 } else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
445 !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
446 mii->mii_media_active |= IFM_FLAG0;
452 e1000phy_mii_phy_auto(struct mii_softc *sc, int waitfor)
456 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
457 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) {
458 PHY_WRITE(sc, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
459 E1000_AR_100TX | E1000_AR_100TX_FD |
460 E1000_AR_PAUSE | E1000_AR_ASM_DIR);
461 PHY_WRITE(sc, E1000_1GCR, E1000_1GCR_1000T_FD);
463 PHY_WRITE(sc, E1000_AR, E1000_FA_1000X_FD |
464 E1000_FA_SYM_PAUSE | E1000_FA_ASYM_PAUSE);
466 PHY_WRITE(sc, E1000_CR,
467 E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
471 /* Wait 5 seconds for it to complete. */
472 for (i = 0; i < 5000; i++) {
473 bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
475 if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
482 * Don't need to worry about clearing MIIF_DOINGAUTO.
483 * If that's set, a timeout is pending, and it will
484 * clear the flag. [do it anyway]
490 * Just let it finish asynchronously. This is for the benefit of
491 * the tick handler driving autonegotiation. Don't want 500ms
492 * delays all the time while the system is running!
494 if (sc->mii_flags & MIIF_AUTOTSLEEP) {
495 sc->mii_flags |= MIIF_DOINGAUTO;
496 tsleep(&sc->mii_flags, 0, "miiaut", hz >> 1);
497 mii_phy_auto_timeout(sc);
498 } else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
499 sc->mii_flags |= MIIF_DOINGAUTO;
500 callout_reset(&sc->mii_auto_ch, 5 * hz,
501 mii_phy_auto_timeout, sc);
503 return (EJUSTRETURN);