xref: /dragonfly/sys/dev/netif/mii_layer/e1000phy.c (revision 984263bc)
1 /* $FreeBSD: src/sys/dev/mii/e1000phy.c,v 1.1.2.2 2002/11/08 21:53:49 semenu Exp $ */
2 /*
3  * Principal Author: Parag Patel
4  * Copyright (c) 2001
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * Additonal Copyright (c) 2001 by Traakan Software under same licence.
30  * Secondary Author: Matthew Jacob
31  */
32 
33 /*
34  * driver for the Marvell 88E1000 series external 1000/100/10-BT PHY.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/socket.h>
41 #include <sys/bus.h>
42 
43 #include <machine/clock.h>
44 
45 #include <net/if.h>
46 #include <net/if_media.h>
47 
48 #include <dev/mii/mii.h>
49 #include <dev/mii/miivar.h>
50 #include <dev/mii/miidevs.h>
51 
52 #include <dev/mii/e1000phyreg.h>
53 
54 #include "miibus_if.h"
55 
56 static int e1000phy_probe(device_t);
57 static int e1000phy_attach(device_t);
58 static int e1000phy_detach(device_t);
59 
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),
66 	{ 0, 0 }
67 };
68 
69 static devclass_t e1000phy_devclass;
70 static driver_t e1000phy_driver = {
71 	"e1000phy", e1000phy_methods, sizeof (struct mii_softc)
72 };
73 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
74 
75 int	e1000phy_service(struct mii_softc *, struct mii_data *, int);
76 void	e1000phy_status(struct mii_softc *);
77 
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 *);
81 
82 static int e1000phy_debug = 0;
83 
84 static int
85 e1000phy_probe(device_t	dev)
86 {
87 	struct mii_attach_args *ma;
88 	u_int32_t id;
89 
90 	ma = device_get_ivars(dev);
91 	id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
92 
93 	if (id != E1000_ID_88E1000 && id != E1000_ID_88E1000S) {
94 		return ENXIO;
95 	}
96 
97 	device_set_desc(dev, MII_STR_MARVELL_E1000);
98 	return 0;
99 }
100 
101 static int
102 e1000phy_attach(device_t dev)
103 {
104 	struct mii_softc *sc;
105 	struct mii_attach_args *ma;
106 	struct mii_data *mii;
107 	const char *sep = "";
108 
109 	getenv_int("e1000phy_debug", &e1000phy_debug);
110 
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);
116 
117 	sc->mii_inst = mii->mii_instance;
118 	sc->mii_phy = ma->mii_phyno;
119 	sc->mii_service = e1000phy_service;
120 	sc->mii_pdata = mii;
121 
122 	sc->mii_flags |= MIIF_NOISOLATE;
123 	mii->mii_instance++;
124 	e1000phy_reset(sc);
125 
126 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
127 #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
128 
129 #if	0
130 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
131 	    E1000_CR_ISOLATE);
132 #endif
133 
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");
138 	/*
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);
142 	PRINT("1000baseTX");
143 	*/
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),
148 			E1000_CR_SPEED_100);
149 	PRINT("100baseTX");
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),
154 			E1000_CR_SPEED_10);
155 	PRINT("10baseTX");
156 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
157 	PRINT("auto");
158 
159 	printf("\n");
160 #undef ADD
161 #undef PRINT
162 
163 	MIIBUS_MEDIAINIT(sc->mii_dev);
164 	return(0);
165 }
166 
167 static int
168 e1000phy_detach(device_t dev)
169 {
170 	struct mii_softc *sc;
171 	struct mii_data *mii;
172 
173 	sc = device_get_softc(dev);
174 	mii = device_get_softc(device_get_parent(dev));
175 
176 	if (sc->mii_flags & MIIF_DOINGAUTO)
177 		untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
178 
179 	sc->mii_dev = NULL;
180 	LIST_REMOVE(sc, mii_list);
181 
182 	return 0;
183 }
184 
185 static void
186 e1000phy_reset(struct mii_softc *sc)
187 {
188 	u_int32_t reg;
189 	int i;
190 
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);
195 
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);
201 
202 	for (i = 0; i < 500; i++) {
203 		DELAY(1);
204 		reg = PHY_READ(sc, E1000_CR);
205 		if (!(reg & E1000_CR_RESET))
206 			break;
207 	}
208 
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);
213 
214 	reg = PHY_READ(sc, E1000_ESCR);
215 	reg |= E1000_ESCR_TX_CLK_25;
216 	PHY_WRITE(sc, E1000_ESCR, reg);
217 
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);
222 }
223 
224 int
225 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
226 {
227 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
228 	int reg;
229 
230 	switch (cmd) {
231 	case MII_POLLSTAT:
232 		/*
233 		 * If we're not polling our PHY instance, just return.
234 		 */
235 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
236 			return (0);
237 		break;
238 
239 	case MII_MEDIACHG:
240 		/*
241 		 * If the media indicates a different PHY instance,
242 		 * isolate ourselves.
243 		 */
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);
247 			return (0);
248 		}
249 
250 		/*
251 		 * If the interface is not up, don't do anything.
252 		 */
253 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
254 			break;
255 		}
256 
257 		switch (IFM_SUBTYPE(ife->ifm_media)) {
258 		case IFM_AUTO:
259 			/*
260 			 * If we're already in auto mode, just return.
261 			 */
262 			if (sc->mii_flags & MIIF_DOINGAUTO) {
263 				return (0);
264 			}
265 			e1000phy_reset(sc);
266 			(void)e1000phy_mii_phy_auto(sc, 1);
267 			break;
268 
269 		case IFM_1000_TX:
270 			if (sc->mii_flags & MIIF_DOINGAUTO)
271 				return (0);
272 
273 			e1000phy_reset(sc);
274 
275 			/* TODO - any other way to force 1000BT? */
276 			(void)e1000phy_mii_phy_auto(sc, 1);
277 			break;
278 
279 		case IFM_100_TX:
280 			e1000phy_reset(sc);
281 
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);
286 			} else {
287 				PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
288 				PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
289 			}
290 			break;
291 
292 		case IFM_10_T:
293 			e1000phy_reset(sc);
294 
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);
299 			} else {
300 				PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
301 				PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
302 			}
303 
304 			break;
305 
306 		default:
307 			return (EINVAL);
308 		}
309 
310 		break;
311 
312 	case MII_TICK:
313 		/*
314 		 * If we're not currently selected, just return.
315 		 */
316 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
317 			return (0);
318 		}
319 
320 		/*
321 		 * Only used for autonegotiation.
322 		 */
323 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
324 			return (0);
325 		}
326 
327 		/*
328 		 * Is the interface even up?
329 		 */
330 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
331 			return (0);
332 		}
333 
334 		/*
335 		 * Only retry autonegotiation every 5 seconds.
336 		 */
337 		if (++(sc->mii_ticks) != 5) {
338 			return (0);
339 		}
340 		sc->mii_ticks = 0;
341 
342 		/*
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.
346 		 */
347 		reg = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
348 
349 		if (reg & E1000_SR_LINK_STATUS)
350 			break;
351 
352 		e1000phy_reset(sc);
353 
354 		if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN) {
355 			return(0);
356 		}
357 
358 		break;
359 	}
360 
361 	/* Update the media status. */
362 	e1000phy_status(sc);
363 
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;
368 	}
369 
370 	return (0);
371 }
372 
373 void
374 e1000phy_status(struct mii_softc *sc)
375 {
376 	struct mii_data *mii = sc->mii_pdata;
377 	int bmsr, bmcr, esr, ssr, isr, ar, lpar;
378 
379 	mii->mii_media_status = IFM_AVALID;
380 	mii->mii_media_active = IFM_ETHER;
381 
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);
389 
390 	if (bmsr & E1000_SR_LINK_STATUS)
391 		mii->mii_media_status |= IFM_ACTIVE;
392 
393 	if (bmcr & E1000_CR_LOOPBACK)
394 		mii->mii_media_active |= IFM_LOOP;
395 
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;
401 		return;
402 	}
403 
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;
408 	else
409 		mii->mii_media_active |= IFM_10_T;
410 
411 	if (ssr & E1000_SSR_DUPLEX)
412 		mii->mii_media_active |= IFM_FDX;
413 	else
414 		mii->mii_media_active |= IFM_HDX;
415 
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;
425 	}
426 }
427 
428 static int
429 e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor)
430 {
431 	int bmsr, i;
432 
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);
440 	}
441 
442 	if (waitfor) {
443 		/* Wait 500ms for it to complete. */
444 		for (i = 0; i < 500; i++) {
445 			bmsr =
446 			    PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR);
447 
448 			if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
449 				return (0);
450 			}
451 			DELAY(1000);
452 		}
453 
454 		/*
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]
458 		 */
459 	}
460 
461 	/*
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!
465 	 */
466 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
467 		mii->mii_flags |= MIIF_DOINGAUTO;
468 		mii->mii_ticks = 0;
469 		mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
470 	}
471 	return (EJUSTRETURN);
472 }
473