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.7 2005/10/24 16:55:40 dillon Exp $ */
4 * Principal Author: Parag Patel
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Additonal Copyright (c) 2001 by Traakan Software under same licence.
31 * Secondary Author: Matthew Jacob
35 * driver for the Marvell 88E1000 series external 1000/100/10-BT PHY.
39 * Support added for the Marvell 88E1011 (Alaska) 1000/100/10baseTX and
41 * Nathan Binkert <nate@openbsd.org>
42 * Jung-uk Kim <jkim@niksun.com>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
51 #include <machine/clock.h>
54 #include <net/if_media.h>
60 #include "e1000phyreg.h"
62 #include "miibus_if.h"
64 static int e1000phy_probe(device_t);
65 static int e1000phy_attach(device_t);
66 static int e1000phy_detach(device_t);
68 static device_method_t e1000phy_methods[] = {
69 /* device interface */
70 DEVMETHOD(device_probe, e1000phy_probe),
71 DEVMETHOD(device_attach, e1000phy_attach),
72 DEVMETHOD(device_detach, e1000phy_detach),
73 DEVMETHOD(device_shutdown, bus_generic_shutdown),
77 static devclass_t e1000phy_devclass;
78 static driver_t e1000phy_driver = {
79 "e1000phy", e1000phy_methods, sizeof (struct mii_softc)
81 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
83 int e1000phy_service(struct mii_softc *, struct mii_data *, int);
84 void e1000phy_status(struct mii_softc *);
86 static int e1000phy_mii_phy_auto(struct mii_softc *, int);
87 extern void mii_phy_auto_timeout(void *);
88 static void e1000phy_reset(struct mii_softc *);
90 static int e1000phy_debug = 0;
93 e1000phy_probe(device_t dev)
95 struct mii_attach_args *ma;
98 ma = device_get_ivars(dev);
99 id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
100 if (id != E1000_ID_88E1000
101 && id != E1000_ID_88E1000S
102 && id != E1000_ID_88E1011) {
106 device_set_desc(dev, MII_STR_MARVELL_E1000);
111 e1000phy_attach(device_t dev)
113 struct mii_softc *sc;
114 struct mii_attach_args *ma;
115 struct mii_data *mii;
116 const char *sep = "";
119 getenv_int("e1000phy_debug", &e1000phy_debug);
121 sc = device_get_softc(dev);
122 ma = device_get_ivars(dev);
123 mii_softc_init(sc, ma);
124 sc->mii_dev = device_get_parent(dev);
125 mii = device_get_softc(sc->mii_dev);
126 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
128 sc->mii_inst = mii->mii_instance;
129 sc->mii_service = e1000phy_service;
131 sc->mii_flags |= MIIF_NOISOLATE;
133 id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
134 if (id == E1000_ID_88E1011
135 && (PHY_READ(sc, E1000_ESSR) & E1000_ESSR_FIBER_LINK))
136 sc->mii_flags |= MIIF_HAVEFIBER;
140 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
141 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
144 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
148 device_printf(dev, " ");
149 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) {
150 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst),
151 E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX);
152 PRINT("1000baseTX-FDX");
154 TODO - apparently 1000BT-simplex not supported?
155 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
156 E1000_CR_SPEED_1000);
159 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
160 E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX);
161 PRINT("100baseTX-FDX");
162 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
165 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
166 E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX);
167 PRINT("10baseTX-FDX");
168 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
172 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst),
173 E1000_CR_SPEED_1000);
174 PRINT("1000baseSX-FDX");
176 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
183 MIIBUS_MEDIAINIT(sc->mii_dev);
188 e1000phy_detach(device_t dev)
190 struct mii_softc *sc;
191 struct mii_data *mii;
193 sc = device_get_softc(dev);
194 mii = device_get_softc(device_get_parent(dev));
196 if (sc->mii_flags & MIIF_DOINGAUTO)
197 callout_stop(&sc->mii_auto_ch);
200 LIST_REMOVE(sc, mii_list);
206 e1000phy_reset(struct mii_softc *sc)
211 /* initialize custom E1000 registers to magic values */
212 reg = PHY_READ(sc, E1000_SCR);
213 reg &= ~E1000_SCR_AUTO_X_MODE;
214 PHY_WRITE(sc, E1000_SCR, reg);
216 /* normal PHY reset */
217 /*mii_phy_reset(sc);*/
218 reg = PHY_READ(sc, E1000_CR);
219 reg |= E1000_CR_RESET;
220 PHY_WRITE(sc, E1000_CR, reg);
222 for (i = 0; i < 500; i++) {
224 reg = PHY_READ(sc, E1000_CR);
225 if (!(reg & E1000_CR_RESET))
229 /* set more custom E1000 registers to magic values */
230 reg = PHY_READ(sc, E1000_SCR);
231 reg |= E1000_SCR_ASSERT_CRS_ON_TX;
232 PHY_WRITE(sc, E1000_SCR, reg);
234 reg = PHY_READ(sc, E1000_ESCR);
235 reg |= E1000_ESCR_TX_CLK_25;
236 PHY_WRITE(sc, E1000_ESCR, reg);
238 /* even more magic to reset DSP? */
239 PHY_WRITE(sc, 29, 0x1d);
240 PHY_WRITE(sc, 30, 0xc1);
241 PHY_WRITE(sc, 30, 0x00);
245 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
247 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
253 * If we're not polling our PHY instance, just return.
255 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
261 * If the media indicates a different PHY instance,
264 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
265 reg = PHY_READ(sc, E1000_CR);
266 PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE);
271 * If the interface is not up, don't do anything.
273 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
277 switch (IFM_SUBTYPE(ife->ifm_media)) {
280 * If we're already in auto mode, just return.
282 if (sc->mii_flags & MIIF_DOINGAUTO) {
286 (void)e1000phy_mii_phy_auto(sc, 1);
292 PHY_WRITE(sc, E1000_CR,
293 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_1000);
294 PHY_WRITE(sc, E1000_AR, E1000_FA_1000X_FD);
298 if (sc->mii_flags & MIIF_DOINGAUTO)
303 /* TODO - any other way to force 1000BT? */
304 (void)e1000phy_mii_phy_auto(sc, 1);
310 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
311 PHY_WRITE(sc, E1000_CR,
312 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100);
313 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD);
315 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
316 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
323 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
324 PHY_WRITE(sc, E1000_CR,
325 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10);
326 PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD);
328 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
329 PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
342 * If we're not currently selected, just return.
344 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
349 * Only used for autonegotiation.
351 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
356 * Is the interface even up?
358 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
363 * Only retry autonegotiation every 5 seconds.
365 if (++(sc->mii_ticks) != 5) {
371 * Check to see if we have link. If we do, we don't
372 * need to restart the autonegotiation process. Read
373 * the BMSR twice in case it's latched.
375 reg = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
377 if (reg & E1000_SR_LINK_STATUS)
382 if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN) {
389 /* Update the media status. */
392 /* Callback if something changed. */
393 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
394 MIIBUS_STATCHG(sc->mii_dev);
395 sc->mii_active = mii->mii_media_active;
402 e1000phy_status(struct mii_softc *sc)
404 struct mii_data *mii = sc->mii_pdata;
405 int bmsr, bmcr, esr, ssr, isr, ar, lpar;
407 mii->mii_media_status = IFM_AVALID;
408 mii->mii_media_active = IFM_ETHER;
410 bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
411 esr = PHY_READ(sc, E1000_ESR);
412 bmcr = PHY_READ(sc, E1000_CR);
413 ssr = PHY_READ(sc, E1000_SSR);
414 isr = PHY_READ(sc, E1000_ISR);
415 ar = PHY_READ(sc, E1000_AR);
416 lpar = PHY_READ(sc, E1000_LPAR);
418 if (bmsr & E1000_SR_LINK_STATUS)
419 mii->mii_media_status |= IFM_ACTIVE;
421 if (bmcr & E1000_CR_LOOPBACK)
422 mii->mii_media_active |= IFM_LOOP;
424 if ((sc->mii_flags & MIIF_DOINGAUTO) &&
425 (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
426 !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
427 /* Erg, still trying, I guess... */
428 mii->mii_media_active |= IFM_NONE;
432 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) {
433 if (ssr & E1000_SSR_1000MBS)
434 mii->mii_media_active |= IFM_1000_T;
435 else if (ssr & E1000_SSR_100MBS)
436 mii->mii_media_active |= IFM_100_TX;
438 mii->mii_media_active |= IFM_10_T;
440 if (ssr & E1000_SSR_1000MBS)
441 mii->mii_media_active |= IFM_1000_SX;
444 if (ssr & E1000_SSR_DUPLEX)
445 mii->mii_media_active |= IFM_FDX;
447 mii->mii_media_active |= IFM_HDX;
449 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) {
450 /* FLAG0==rx-flow-control FLAG1==tx-flow-control */
451 if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
452 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
453 } else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
454 (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
455 mii->mii_media_active |= IFM_FLAG1;
456 } else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
457 !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
458 mii->mii_media_active |= IFM_FLAG0;
464 e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor)
468 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
469 if ((mii->mii_flags & MIIF_HAVEFIBER) == 0) {
470 PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
471 E1000_AR_100TX | E1000_AR_100TX_FD |
472 E1000_AR_PAUSE | E1000_AR_ASM_DIR);
473 PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD);
474 PHY_WRITE(mii, E1000_CR,
475 E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
480 /* Wait 5 seconds for it to complete. */
481 for (i = 0; i < 5000; i++) {
483 PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR);
485 if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
492 * Don't need to worry about clearing MIIF_DOINGAUTO.
493 * If that's set, a timeout is pending, and it will
494 * clear the flag. [do it anyway]
499 * Just let it finish asynchronously. This is for the benefit of
500 * the tick handler driving autonegotiation. Don't want 500ms
501 * delays all the time while the system is running!
503 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
504 mii->mii_flags |= MIIF_DOINGAUTO;
506 callout_reset(&mii->mii_auto_ch, 5 * hz,
507 mii_phy_auto_timeout, mii);
509 return (EJUSTRETURN);