xref: /freebsd/sys/dev/mii/e1000phy.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
1 /* $FreeBSD$ */
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/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/bus.h>
43 
44 #include <machine/clock.h>
45 
46 #include <net/if.h>
47 #include <net/if_media.h>
48 
49 #include <dev/mii/mii.h>
50 #include <dev/mii/miivar.h>
51 #include <dev/mii/miidevs.h>
52 
53 #include <dev/mii/e1000phyreg.h>
54 
55 #include "miibus_if.h"
56 
57 static int e1000phy_probe(device_t);
58 static int e1000phy_attach(device_t);
59 static int e1000phy_detach(device_t);
60 
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),
67 	{ 0, 0 }
68 };
69 
70 static devclass_t e1000phy_devclass;
71 static driver_t e1000phy_driver = {
72 	"e1000phy", e1000phy_methods, sizeof (struct mii_softc)
73 };
74 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
75 
76 static int	e1000phy_service(struct mii_softc *, struct mii_data *, int);
77 static void	e1000phy_status(struct mii_softc *);
78 static void	e1000phy_reset(struct mii_softc *);
79 static int	e1000phy_mii_phy_auto(struct mii_softc *, int);
80 
81 extern void	mii_phy_auto_timeout(void *);
82 
83 static int e1000phy_debug = 0;
84 
85 static int
86 e1000phy_probe(device_t	dev)
87 {
88 	struct mii_attach_args *ma;
89 	u_int32_t id;
90 
91 	ma = device_get_ivars(dev);
92 	id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
93 
94 	if (id != E1000_ID_88E1000 && id != E1000_ID_88E1000S) {
95 		return ENXIO;
96 	}
97 
98 	device_set_desc(dev, MII_STR_MARVELL_E1000);
99 	return 0;
100 }
101 
102 static int
103 e1000phy_attach(device_t dev)
104 {
105 	struct mii_softc *sc;
106 	struct mii_attach_args *ma;
107 	struct mii_data *mii;
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 	device_printf(dev, " ");
127 
128 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
129 /*
130 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
131 	    E1000_CR_ISOLATE);
132 */
133 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
134 	    E1000_CR_SPEED_10);
135 	printf("10baseT, ");
136 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
137 	    E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX);
138 	printf("10baseT-FDX, ");
139 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
140 	    E1000_CR_SPEED_100);
141 	printf("100baseTX, ");
142 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
143 	    E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX);
144 	printf("100baseTX-FDX, ");
145 	/*
146 	 * 1000BT-simplex not supported; driver must ignore this entry,
147 	 * but it must be present in order to manually set full-duplex.
148 	 */
149 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst),
150 	    E1000_CR_SPEED_1000);
151 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst),
152 	    E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX);
153 	printf("1000baseTX-FDX, ");
154 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
155 	printf("auto\n");
156 #undef ADD
157 
158 	MIIBUS_MEDIAINIT(sc->mii_dev);
159 	return(0);
160 }
161 
162 static int
163 e1000phy_detach(device_t dev)
164 {
165 	struct mii_softc *sc;
166 	struct mii_data *mii;
167 
168 	sc = device_get_softc(dev);
169 	mii = device_get_softc(device_get_parent(dev));
170 
171 	if (sc->mii_flags & MIIF_DOINGAUTO)
172 		untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
173 
174 	sc->mii_dev = NULL;
175 	LIST_REMOVE(sc, mii_list);
176 
177 	return 0;
178 }
179 
180 static void
181 e1000phy_reset(struct mii_softc *sc)
182 {
183 	u_int32_t reg;
184 	int i;
185 
186 	/* initialize custom E1000 registers to magic values */
187 	reg = PHY_READ(sc, E1000_SCR);
188 	reg &= ~E1000_SCR_AUTO_X_MODE;
189 	PHY_WRITE(sc, E1000_SCR, reg);
190 
191 	/* normal PHY reset */
192 	/*mii_phy_reset(sc);*/
193 	reg = PHY_READ(sc, E1000_CR);
194 	reg |= E1000_CR_RESET;
195 	PHY_WRITE(sc, E1000_CR, reg);
196 
197 	for (i = 0; i < 500; i++) {
198 		DELAY(1);
199 		reg = PHY_READ(sc, E1000_CR);
200 		if (!(reg & E1000_CR_RESET))
201 			break;
202 	}
203 
204 	/* set more custom E1000 registers to magic values */
205 	reg = PHY_READ(sc, E1000_SCR);
206 	reg |= E1000_SCR_ASSERT_CRS_ON_TX;
207 	PHY_WRITE(sc, E1000_SCR, reg);
208 
209 	reg = PHY_READ(sc, E1000_ESCR);
210 	reg |= E1000_ESCR_TX_CLK_25;
211 	PHY_WRITE(sc, E1000_ESCR, reg);
212 
213 	/* even more magic to reset DSP? */
214 	PHY_WRITE(sc, 29, 0x1d);
215 	PHY_WRITE(sc, 30, 0xc1);
216 	PHY_WRITE(sc, 30, 0x00);
217 }
218 
219 static int
220 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
221 {
222 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
223 	int reg;
224 
225 	switch (cmd) {
226 	case MII_POLLSTAT:
227 		/*
228 		 * If we're not polling our PHY instance, just return.
229 		 */
230 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
231 			return (0);
232 		break;
233 
234 	case MII_MEDIACHG:
235 		/*
236 		 * If the media indicates a different PHY instance,
237 		 * isolate ourselves.
238 		 */
239 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
240 			reg = PHY_READ(sc, E1000_CR);
241 			PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE);
242 			return (0);
243 		}
244 
245 		/*
246 		 * If the interface is not up, don't do anything.
247 		 */
248 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
249 			break;
250 		}
251 
252 		switch (IFM_SUBTYPE(ife->ifm_media)) {
253 		case IFM_AUTO:
254 			/*
255 			 * If we're already in auto mode, just return.
256 			 */
257 			if (sc->mii_flags & MIIF_DOINGAUTO) {
258 				return (0);
259 			}
260 			e1000phy_reset(sc);
261 			(void)e1000phy_mii_phy_auto(sc, 1);
262 			break;
263 
264 		case IFM_1000_TX:
265 			if (sc->mii_flags & MIIF_DOINGAUTO)
266 				return (0);
267 
268 			e1000phy_reset(sc);
269 
270 			/* TODO - any other way to force 1000BT? */
271 			(void)e1000phy_mii_phy_auto(sc, 1);
272 			break;
273 
274 		case IFM_100_TX:
275 			e1000phy_reset(sc);
276 
277 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
278 				PHY_WRITE(sc, E1000_CR,
279 				    E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100);
280 				PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD);
281 			} else {
282 				PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
283 				PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
284 			}
285 			break;
286 
287 		case IFM_10_T:
288 			e1000phy_reset(sc);
289 
290 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
291 				PHY_WRITE(sc, E1000_CR,
292 				    E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10);
293 				PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD);
294 			} else {
295 				PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
296 				PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
297 			}
298 
299 			break;
300 
301 		default:
302 			return (EINVAL);
303 		}
304 
305 		break;
306 
307 	case MII_TICK:
308 		/*
309 		 * If we're not currently selected, just return.
310 		 */
311 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
312 			return (0);
313 		}
314 
315 		/*
316 		 * Is the interface even up?
317 		 */
318 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
319 			return (0);
320 
321 		/*
322 		 * Only used for autonegotiation.
323 		 */
324 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
325 			break;
326 
327 		/*
328 		 * check for link.
329 		 * Read the status register twice; BMSR_LINK is latch-low.
330 		 */
331 		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
332 		if (reg & BMSR_LINK)
333 			break;
334 
335 		/*
336 		 * Only retry autonegotiation every 5 seconds.
337 		 */
338 		if (++sc->mii_ticks != 5)
339 			return (0);
340 
341 		sc->mii_ticks = 0;
342 		e1000phy_reset(sc);
343 		if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN)
344 			return (0);
345 		break;
346 	}
347 
348 	/* Update the media status. */
349 	e1000phy_status(sc);
350 
351 	/* Callback if something changed. */
352 	mii_phy_update(sc, cmd);
353 	return (0);
354 }
355 
356 static void
357 e1000phy_status(struct mii_softc *sc)
358 {
359 	struct mii_data *mii = sc->mii_pdata;
360 	int bmsr, bmcr, esr, ssr, isr, ar, lpar;
361 
362 	mii->mii_media_status = IFM_AVALID;
363 	mii->mii_media_active = IFM_ETHER;
364 
365 	bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
366 	esr = PHY_READ(sc, E1000_ESR);
367 	bmcr = PHY_READ(sc, E1000_CR);
368 	ssr = PHY_READ(sc, E1000_SSR);
369 	isr = PHY_READ(sc, E1000_ISR);
370 	ar = PHY_READ(sc, E1000_AR);
371 	lpar = PHY_READ(sc, E1000_LPAR);
372 
373 	if (bmsr & E1000_SR_LINK_STATUS)
374 		mii->mii_media_status |= IFM_ACTIVE;
375 
376 	if (bmcr & E1000_CR_LOOPBACK)
377 		mii->mii_media_active |= IFM_LOOP;
378 
379 	if ((sc->mii_flags & MIIF_DOINGAUTO) &&
380 	    (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
381 	    !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
382 		/* Erg, still trying, I guess... */
383 		mii->mii_media_active |= IFM_NONE;
384 		return;
385 	}
386 
387 	if (ssr & E1000_SSR_1000MBS)
388 		mii->mii_media_active |= IFM_1000_TX;
389 	else if (ssr & E1000_SSR_100MBS)
390 		mii->mii_media_active |= IFM_100_TX;
391 	else
392 		mii->mii_media_active |= IFM_10_T;
393 
394 	if (ssr & E1000_SSR_DUPLEX)
395 		mii->mii_media_active |= IFM_FDX;
396 	else
397 		mii->mii_media_active |= IFM_HDX;
398 
399 	/* FLAG0==rx-flow-control FLAG1==tx-flow-control */
400 	if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
401 		mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
402 	} else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
403 	    (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
404 		mii->mii_media_active |= IFM_FLAG1;
405 	} else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
406 	    !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
407 		mii->mii_media_active |= IFM_FLAG0;
408 	}
409 }
410 
411 static int
412 e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor)
413 {
414 	int bmsr, i;
415 
416 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
417 		PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
418 		    E1000_AR_100TX | E1000_AR_100TX_FD |
419 		    E1000_AR_PAUSE | E1000_AR_ASM_DIR);
420 		PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD);
421 		PHY_WRITE(mii, E1000_CR,
422 		    E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
423 	}
424 
425 	if (waitfor) {
426 		/* Wait 500ms for it to complete. */
427 		for (i = 0; i < 500; i++) {
428 			bmsr =
429 			    PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR);
430 
431 			if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
432 				return (0);
433 			}
434 			DELAY(1000);
435 		}
436 
437 		/*
438 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
439 		 * If that's set, a timeout is pending, and it will
440 		 * clear the flag. [do it anyway]
441 		 */
442 	}
443 
444 	/*
445 	 * Just let it finish asynchronously.  This is for the benefit of
446 	 * the tick handler driving autonegotiation.  Don't want 500ms
447 	 * delays all the time while the system is running!
448 	 */
449 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
450 		mii->mii_flags |= MIIF_DOINGAUTO;
451 		mii->mii_ticks = 0;
452 		mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
453 	}
454 	return (EJUSTRETURN);
455 }
456