e1000phy.c revision 84144
1/* $FreeBSD: head/sys/dev/mii/e1000phy.c 84144 2001-09-29 18:59:17Z jlemon $ */
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
57static int e1000phy_probe(device_t);
58static int e1000phy_attach(device_t);
59static int e1000phy_detach(device_t);
60
61static 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
70static devclass_t e1000phy_devclass;
71static driver_t e1000phy_driver = {
72	"e1000phy", e1000phy_methods, sizeof (struct mii_softc)
73};
74DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
75
76int	e1000phy_service(struct mii_softc *, struct mii_data *, int);
77void	e1000phy_status(struct mii_softc *);
78
79static int	e1000phy_mii_phy_auto(struct mii_softc *, int);
80extern void	mii_phy_auto_timeout(void *);
81static void e1000phy_reset(struct mii_softc *);
82
83static int e1000phy_debug = 0;
84
85static int
86e1000phy_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
102static int
103e1000phy_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
162static int
163e1000phy_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
180static void
181e1000phy_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
219int
220e1000phy_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		 * Only used for autonegotiation.
317		 */
318		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
319			return (0);
320		}
321
322		/*
323		 * Is the interface even up?
324		 */
325		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
326			return (0);
327		}
328
329		/*
330		 * Only retry autonegotiation every 5 seconds.
331		 */
332		if (++(sc->mii_ticks) != 5) {
333			return (0);
334		}
335		sc->mii_ticks = 0;
336
337		/*
338		 * Check to see if we have link.  If we do, we don't
339		 * need to restart the autonegotiation process.  Read
340		 * the BMSR twice in case it's latched.
341		 */
342		reg = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
343
344		if (reg & E1000_SR_LINK_STATUS)
345			break;
346
347		e1000phy_reset(sc);
348
349		if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN) {
350			return(0);
351		}
352
353		break;
354	}
355
356	/* Update the media status. */
357	e1000phy_status(sc);
358
359	/* Callback if something changed. */
360	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
361		MIIBUS_STATCHG(sc->mii_dev);
362		sc->mii_active = mii->mii_media_active;
363	}
364
365	return (0);
366}
367
368void
369e1000phy_status(struct mii_softc *sc)
370{
371	struct mii_data *mii = sc->mii_pdata;
372	int bmsr, bmcr, esr, ssr, isr, ar, lpar;
373
374	mii->mii_media_status = IFM_AVALID;
375	mii->mii_media_active = IFM_ETHER;
376
377	bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
378	esr = PHY_READ(sc, E1000_ESR);
379	bmcr = PHY_READ(sc, E1000_CR);
380	ssr = PHY_READ(sc, E1000_SSR);
381	isr = PHY_READ(sc, E1000_ISR);
382	ar = PHY_READ(sc, E1000_AR);
383	lpar = PHY_READ(sc, E1000_LPAR);
384
385	if (bmsr & E1000_SR_LINK_STATUS)
386		mii->mii_media_status |= IFM_ACTIVE;
387
388	if (bmcr & E1000_CR_LOOPBACK)
389		mii->mii_media_active |= IFM_LOOP;
390
391	if ((sc->mii_flags & MIIF_DOINGAUTO) &&
392	    (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
393	    !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
394		/* Erg, still trying, I guess... */
395		mii->mii_media_active |= IFM_NONE;
396		return;
397	}
398
399	if (ssr & E1000_SSR_1000MBS)
400		mii->mii_media_active |= IFM_1000_TX;
401	else if (ssr & E1000_SSR_100MBS)
402		mii->mii_media_active |= IFM_100_TX;
403	else
404		mii->mii_media_active |= IFM_10_T;
405
406	if (ssr & E1000_SSR_DUPLEX)
407		mii->mii_media_active |= IFM_FDX;
408	else
409		mii->mii_media_active |= IFM_HDX;
410
411	/* FLAG0==rx-flow-control FLAG1==tx-flow-control */
412	if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
413		mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
414	} else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
415	    (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
416		mii->mii_media_active |= IFM_FLAG1;
417	} else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
418	    !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
419		mii->mii_media_active |= IFM_FLAG0;
420	}
421}
422
423static int
424e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor)
425{
426	int bmsr, i;
427
428	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
429		PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
430		    E1000_AR_100TX | E1000_AR_100TX_FD |
431		    E1000_AR_PAUSE | E1000_AR_ASM_DIR);
432		PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD);
433		PHY_WRITE(mii, E1000_CR,
434		    E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
435	}
436
437	if (waitfor) {
438		/* Wait 500ms for it to complete. */
439		for (i = 0; i < 500; i++) {
440			bmsr =
441			    PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR);
442
443			if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
444				return (0);
445			}
446			DELAY(1000);
447		}
448
449		/*
450		 * Don't need to worry about clearing MIIF_DOINGAUTO.
451		 * If that's set, a timeout is pending, and it will
452		 * clear the flag. [do it anyway]
453		 */
454	}
455
456	/*
457	 * Just let it finish asynchronously.  This is for the benefit of
458	 * the tick handler driving autonegotiation.  Don't want 500ms
459	 * delays all the time while the system is running!
460	 */
461	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
462		mii->mii_flags |= MIIF_DOINGAUTO;
463		mii->mii_ticks = 0;
464		mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
465	}
466	return (EJUSTRETURN);
467}
468