1 /* $FreeBSD: src/sys/dev/mii/e1000phy.c,v 1.1.2.2 2002/11/08 21:53:49 semenu Exp $ */
3 * Principal Author: Parag Patel
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Additonal Copyright (c) 2001 by Traakan Software under same licence.
30 * Secondary Author: Matthew Jacob
34 * driver for the Marvell 88E1000 series external 1000/100/10-BT PHY.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/socket.h>
43 #include <machine/clock.h>
46 #include <net/if_media.h>
48 #include <dev/mii/mii.h>
49 #include <dev/mii/miivar.h>
50 #include <dev/mii/miidevs.h>
52 #include <dev/mii/e1000phyreg.h>
54 #include "miibus_if.h"
56 static int e1000phy_probe(device_t);
57 static int e1000phy_attach(device_t);
58 static int e1000phy_detach(device_t);
60 static device_method_t e1000phy_methods[] = {
61 /* device interface */
62 DEVMETHOD(device_probe, e1000phy_probe),
63 DEVMETHOD(device_attach, e1000phy_attach),
64 DEVMETHOD(device_detach, e1000phy_detach),
65 DEVMETHOD(device_shutdown, bus_generic_shutdown),
69 static devclass_t e1000phy_devclass;
70 static driver_t e1000phy_driver = {
71 "e1000phy", e1000phy_methods, sizeof (struct mii_softc)
73 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
75 int e1000phy_service(struct mii_softc *, struct mii_data *, int);
76 void e1000phy_status(struct mii_softc *);
78 static int e1000phy_mii_phy_auto(struct mii_softc *, int);
79 extern void mii_phy_auto_timeout(void *);
80 static void e1000phy_reset(struct mii_softc *);
82 static int e1000phy_debug = 0;
85 e1000phy_probe(device_t dev)
87 struct mii_attach_args *ma;
90 ma = device_get_ivars(dev);
91 id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
93 if (id != E1000_ID_88E1000 && id != E1000_ID_88E1000S) {
97 device_set_desc(dev, MII_STR_MARVELL_E1000);
102 e1000phy_attach(device_t dev)
104 struct mii_softc *sc;
105 struct mii_attach_args *ma;
106 struct mii_data *mii;
107 const char *sep = "";
109 getenv_int("e1000phy_debug", &e1000phy_debug);
111 sc = device_get_softc(dev);
112 ma = device_get_ivars(dev);
113 sc->mii_dev = device_get_parent(dev);
114 mii = device_get_softc(sc->mii_dev);
115 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
117 sc->mii_inst = mii->mii_instance;
118 sc->mii_phy = ma->mii_phyno;
119 sc->mii_service = e1000phy_service;
122 sc->mii_flags |= MIIF_NOISOLATE;
126 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
127 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
130 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
134 device_printf(dev, " ");
135 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst),
136 E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX);
137 PRINT("1000baseTX-FDX");
139 TODO - apparently 1000BT-simplex not supported?
140 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst),
141 E1000_CR_SPEED_1000);
144 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
145 E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX);
146 PRINT("100baseTX-FDX");
147 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
150 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
151 E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX);
152 PRINT("10baseTX-FDX");
153 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
156 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
163 MIIBUS_MEDIAINIT(sc->mii_dev);
168 e1000phy_detach(device_t dev)
170 struct mii_softc *sc;
171 struct mii_data *mii;
173 sc = device_get_softc(dev);
174 mii = device_get_softc(device_get_parent(dev));
176 if (sc->mii_flags & MIIF_DOINGAUTO)
177 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
180 LIST_REMOVE(sc, mii_list);
186 e1000phy_reset(struct mii_softc *sc)
191 /* initialize custom E1000 registers to magic values */
192 reg = PHY_READ(sc, E1000_SCR);
193 reg &= ~E1000_SCR_AUTO_X_MODE;
194 PHY_WRITE(sc, E1000_SCR, reg);
196 /* normal PHY reset */
197 /*mii_phy_reset(sc);*/
198 reg = PHY_READ(sc, E1000_CR);
199 reg |= E1000_CR_RESET;
200 PHY_WRITE(sc, E1000_CR, reg);
202 for (i = 0; i < 500; i++) {
204 reg = PHY_READ(sc, E1000_CR);
205 if (!(reg & E1000_CR_RESET))
209 /* set more custom E1000 registers to magic values */
210 reg = PHY_READ(sc, E1000_SCR);
211 reg |= E1000_SCR_ASSERT_CRS_ON_TX;
212 PHY_WRITE(sc, E1000_SCR, reg);
214 reg = PHY_READ(sc, E1000_ESCR);
215 reg |= E1000_ESCR_TX_CLK_25;
216 PHY_WRITE(sc, E1000_ESCR, reg);
218 /* even more magic to reset DSP? */
219 PHY_WRITE(sc, 29, 0x1d);
220 PHY_WRITE(sc, 30, 0xc1);
221 PHY_WRITE(sc, 30, 0x00);
225 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
227 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
233 * If we're not polling our PHY instance, just return.
235 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
241 * If the media indicates a different PHY instance,
244 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
245 reg = PHY_READ(sc, E1000_CR);
246 PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE);
251 * If the interface is not up, don't do anything.
253 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
257 switch (IFM_SUBTYPE(ife->ifm_media)) {
260 * If we're already in auto mode, just return.
262 if (sc->mii_flags & MIIF_DOINGAUTO) {
266 (void)e1000phy_mii_phy_auto(sc, 1);
270 if (sc->mii_flags & MIIF_DOINGAUTO)
275 /* TODO - any other way to force 1000BT? */
276 (void)e1000phy_mii_phy_auto(sc, 1);
282 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
283 PHY_WRITE(sc, E1000_CR,
284 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100);
285 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD);
287 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
288 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
295 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
296 PHY_WRITE(sc, E1000_CR,
297 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10);
298 PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD);
300 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
301 PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
314 * If we're not currently selected, just return.
316 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
321 * Only used for autonegotiation.
323 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
328 * Is the interface even up?
330 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
335 * Only retry autonegotiation every 5 seconds.
337 if (++(sc->mii_ticks) != 5) {
343 * Check to see if we have link. If we do, we don't
344 * need to restart the autonegotiation process. Read
345 * the BMSR twice in case it's latched.
347 reg = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
349 if (reg & E1000_SR_LINK_STATUS)
354 if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN) {
361 /* Update the media status. */
364 /* Callback if something changed. */
365 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
366 MIIBUS_STATCHG(sc->mii_dev);
367 sc->mii_active = mii->mii_media_active;
374 e1000phy_status(struct mii_softc *sc)
376 struct mii_data *mii = sc->mii_pdata;
377 int bmsr, bmcr, esr, ssr, isr, ar, lpar;
379 mii->mii_media_status = IFM_AVALID;
380 mii->mii_media_active = IFM_ETHER;
382 bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
383 esr = PHY_READ(sc, E1000_ESR);
384 bmcr = PHY_READ(sc, E1000_CR);
385 ssr = PHY_READ(sc, E1000_SSR);
386 isr = PHY_READ(sc, E1000_ISR);
387 ar = PHY_READ(sc, E1000_AR);
388 lpar = PHY_READ(sc, E1000_LPAR);
390 if (bmsr & E1000_SR_LINK_STATUS)
391 mii->mii_media_status |= IFM_ACTIVE;
393 if (bmcr & E1000_CR_LOOPBACK)
394 mii->mii_media_active |= IFM_LOOP;
396 if ((sc->mii_flags & MIIF_DOINGAUTO) &&
397 (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
398 !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
399 /* Erg, still trying, I guess... */
400 mii->mii_media_active |= IFM_NONE;
404 if (ssr & E1000_SSR_1000MBS)
405 mii->mii_media_active |= IFM_1000_TX;
406 else if (ssr & E1000_SSR_100MBS)
407 mii->mii_media_active |= IFM_100_TX;
409 mii->mii_media_active |= IFM_10_T;
411 if (ssr & E1000_SSR_DUPLEX)
412 mii->mii_media_active |= IFM_FDX;
414 mii->mii_media_active |= IFM_HDX;
416 /* FLAG0==rx-flow-control FLAG1==tx-flow-control */
417 if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
418 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
419 } else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
420 (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
421 mii->mii_media_active |= IFM_FLAG1;
422 } else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
423 !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
424 mii->mii_media_active |= IFM_FLAG0;
429 e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor)
433 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
434 PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
435 E1000_AR_100TX | E1000_AR_100TX_FD |
436 E1000_AR_PAUSE | E1000_AR_ASM_DIR);
437 PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD);
438 PHY_WRITE(mii, E1000_CR,
439 E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
443 /* Wait 500ms for it to complete. */
444 for (i = 0; i < 500; i++) {
446 PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR);
448 if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
455 * Don't need to worry about clearing MIIF_DOINGAUTO.
456 * If that's set, a timeout is pending, and it will
457 * clear the flag. [do it anyway]
462 * Just let it finish asynchronously. This is for the benefit of
463 * the tick handler driving autonegotiation. Don't want 500ms
464 * delays all the time while the system is running!
466 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
467 mii->mii_flags |= MIIF_DOINGAUTO;
469 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
471 return (EJUSTRETURN);