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.3 2003/08/07 21:17:03 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.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
44 #include <machine/clock.h>
47 #include <net/if_media.h>
53 #include "e1000phyreg.h"
55 #include "miibus_if.h"
57 static int e1000phy_probe(device_t);
58 static int e1000phy_attach(device_t);
59 static int e1000phy_detach(device_t);
61 static device_method_t e1000phy_methods[] = {
62 /* device interface */
63 DEVMETHOD(device_probe, e1000phy_probe),
64 DEVMETHOD(device_attach, e1000phy_attach),
65 DEVMETHOD(device_detach, e1000phy_detach),
66 DEVMETHOD(device_shutdown, bus_generic_shutdown),
70 static devclass_t e1000phy_devclass;
71 static driver_t e1000phy_driver = {
72 "e1000phy", e1000phy_methods, sizeof (struct mii_softc)
74 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
76 int e1000phy_service(struct mii_softc *, struct mii_data *, int);
77 void e1000phy_status(struct mii_softc *);
79 static int e1000phy_mii_phy_auto(struct mii_softc *, int);
80 extern void mii_phy_auto_timeout(void *);
81 static void e1000phy_reset(struct mii_softc *);
83 static int e1000phy_debug = 0;
86 e1000phy_probe(device_t dev)
88 struct mii_attach_args *ma;
91 ma = device_get_ivars(dev);
92 id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
94 if (id != E1000_ID_88E1000 && id != E1000_ID_88E1000S) {
98 device_set_desc(dev, MII_STR_MARVELL_E1000);
103 e1000phy_attach(device_t dev)
105 struct mii_softc *sc;
106 struct mii_attach_args *ma;
107 struct mii_data *mii;
108 const char *sep = "";
110 getenv_int("e1000phy_debug", &e1000phy_debug);
112 sc = device_get_softc(dev);
113 ma = device_get_ivars(dev);
114 sc->mii_dev = device_get_parent(dev);
115 mii = device_get_softc(sc->mii_dev);
116 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
118 sc->mii_inst = mii->mii_instance;
119 sc->mii_phy = ma->mii_phyno;
120 sc->mii_service = e1000phy_service;
123 sc->mii_flags |= MIIF_NOISOLATE;
127 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
128 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
131 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
135 device_printf(dev, " ");
136 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst),
137 E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX);
138 PRINT("1000baseTX-FDX");
140 TODO - apparently 1000BT-simplex not supported?
141 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst),
142 E1000_CR_SPEED_1000);
145 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
146 E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX);
147 PRINT("100baseTX-FDX");
148 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
151 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
152 E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX);
153 PRINT("10baseTX-FDX");
154 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
157 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
164 MIIBUS_MEDIAINIT(sc->mii_dev);
169 e1000phy_detach(device_t dev)
171 struct mii_softc *sc;
172 struct mii_data *mii;
174 sc = device_get_softc(dev);
175 mii = device_get_softc(device_get_parent(dev));
177 if (sc->mii_flags & MIIF_DOINGAUTO)
178 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
181 LIST_REMOVE(sc, mii_list);
187 e1000phy_reset(struct mii_softc *sc)
192 /* initialize custom E1000 registers to magic values */
193 reg = PHY_READ(sc, E1000_SCR);
194 reg &= ~E1000_SCR_AUTO_X_MODE;
195 PHY_WRITE(sc, E1000_SCR, reg);
197 /* normal PHY reset */
198 /*mii_phy_reset(sc);*/
199 reg = PHY_READ(sc, E1000_CR);
200 reg |= E1000_CR_RESET;
201 PHY_WRITE(sc, E1000_CR, reg);
203 for (i = 0; i < 500; i++) {
205 reg = PHY_READ(sc, E1000_CR);
206 if (!(reg & E1000_CR_RESET))
210 /* set more custom E1000 registers to magic values */
211 reg = PHY_READ(sc, E1000_SCR);
212 reg |= E1000_SCR_ASSERT_CRS_ON_TX;
213 PHY_WRITE(sc, E1000_SCR, reg);
215 reg = PHY_READ(sc, E1000_ESCR);
216 reg |= E1000_ESCR_TX_CLK_25;
217 PHY_WRITE(sc, E1000_ESCR, reg);
219 /* even more magic to reset DSP? */
220 PHY_WRITE(sc, 29, 0x1d);
221 PHY_WRITE(sc, 30, 0xc1);
222 PHY_WRITE(sc, 30, 0x00);
226 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
228 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
234 * If we're not polling our PHY instance, just return.
236 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
242 * If the media indicates a different PHY instance,
245 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
246 reg = PHY_READ(sc, E1000_CR);
247 PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE);
252 * If the interface is not up, don't do anything.
254 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
258 switch (IFM_SUBTYPE(ife->ifm_media)) {
261 * If we're already in auto mode, just return.
263 if (sc->mii_flags & MIIF_DOINGAUTO) {
267 (void)e1000phy_mii_phy_auto(sc, 1);
271 if (sc->mii_flags & MIIF_DOINGAUTO)
276 /* TODO - any other way to force 1000BT? */
277 (void)e1000phy_mii_phy_auto(sc, 1);
283 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
284 PHY_WRITE(sc, E1000_CR,
285 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100);
286 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD);
288 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
289 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
296 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
297 PHY_WRITE(sc, E1000_CR,
298 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10);
299 PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD);
301 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
302 PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
315 * If we're not currently selected, just return.
317 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
322 * Only used for autonegotiation.
324 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
329 * Is the interface even up?
331 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
336 * Only retry autonegotiation every 5 seconds.
338 if (++(sc->mii_ticks) != 5) {
344 * Check to see if we have link. If we do, we don't
345 * need to restart the autonegotiation process. Read
346 * the BMSR twice in case it's latched.
348 reg = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
350 if (reg & E1000_SR_LINK_STATUS)
355 if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN) {
362 /* Update the media status. */
365 /* Callback if something changed. */
366 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
367 MIIBUS_STATCHG(sc->mii_dev);
368 sc->mii_active = mii->mii_media_active;
375 e1000phy_status(struct mii_softc *sc)
377 struct mii_data *mii = sc->mii_pdata;
378 int bmsr, bmcr, esr, ssr, isr, ar, lpar;
380 mii->mii_media_status = IFM_AVALID;
381 mii->mii_media_active = IFM_ETHER;
383 bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
384 esr = PHY_READ(sc, E1000_ESR);
385 bmcr = PHY_READ(sc, E1000_CR);
386 ssr = PHY_READ(sc, E1000_SSR);
387 isr = PHY_READ(sc, E1000_ISR);
388 ar = PHY_READ(sc, E1000_AR);
389 lpar = PHY_READ(sc, E1000_LPAR);
391 if (bmsr & E1000_SR_LINK_STATUS)
392 mii->mii_media_status |= IFM_ACTIVE;
394 if (bmcr & E1000_CR_LOOPBACK)
395 mii->mii_media_active |= IFM_LOOP;
397 if ((sc->mii_flags & MIIF_DOINGAUTO) &&
398 (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
399 !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
400 /* Erg, still trying, I guess... */
401 mii->mii_media_active |= IFM_NONE;
405 if (ssr & E1000_SSR_1000MBS)
406 mii->mii_media_active |= IFM_1000_TX;
407 else if (ssr & E1000_SSR_100MBS)
408 mii->mii_media_active |= IFM_100_TX;
410 mii->mii_media_active |= IFM_10_T;
412 if (ssr & E1000_SSR_DUPLEX)
413 mii->mii_media_active |= IFM_FDX;
415 mii->mii_media_active |= IFM_HDX;
417 /* FLAG0==rx-flow-control FLAG1==tx-flow-control */
418 if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
419 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
420 } else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
421 (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
422 mii->mii_media_active |= IFM_FLAG1;
423 } else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
424 !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
425 mii->mii_media_active |= IFM_FLAG0;
430 e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor)
434 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
435 PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
436 E1000_AR_100TX | E1000_AR_100TX_FD |
437 E1000_AR_PAUSE | E1000_AR_ASM_DIR);
438 PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD);
439 PHY_WRITE(mii, E1000_CR,
440 E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
444 /* Wait 500ms for it to complete. */
445 for (i = 0; i < 500; i++) {
447 PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR);
449 if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
456 * Don't need to worry about clearing MIIF_DOINGAUTO.
457 * If that's set, a timeout is pending, and it will
458 * clear the flag. [do it anyway]
463 * Just let it finish asynchronously. This is for the benefit of
464 * the tick handler driving autonegotiation. Don't want 500ms
465 * delays all the time while the system is running!
467 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
468 mii->mii_flags |= MIIF_DOINGAUTO;
470 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
472 return (EJUSTRETURN);