mii_physubr.c revision 204646
1/*	$NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $	*/
2
3/*-
4 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/mii/mii_physubr.c 204646 2010-03-03 17:55:51Z joel $");
35
36/*
37 * Subroutines common to all PHYs.
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/socket.h>
44#include <sys/errno.h>
45#include <sys/module.h>
46#include <sys/bus.h>
47
48#include <net/if.h>
49#include <net/if_media.h>
50
51#include <dev/mii/mii.h>
52#include <dev/mii/miivar.h>
53
54#include "miibus_if.h"
55
56/*
57 * Media to register setting conversion table.  Order matters.
58 */
59const struct mii_media mii_media_table[MII_NMEDIA] = {
60	/* None */
61	{ BMCR_ISO,		ANAR_CSMA,
62	  0, },
63
64	/* 10baseT */
65	{ BMCR_S10,		ANAR_CSMA|ANAR_10,
66	  0, },
67
68	/* 10baseT-FDX */
69	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD,
70	  0, },
71
72	/* 100baseT4 */
73	{ BMCR_S100,		ANAR_CSMA|ANAR_T4,
74	  0, },
75
76	/* 100baseTX */
77	{ BMCR_S100,		ANAR_CSMA|ANAR_TX,
78	  0, },
79
80	/* 100baseTX-FDX */
81	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD,
82	  0, },
83
84	/* 1000baseX */
85	{ BMCR_S1000,		ANAR_CSMA,
86	  0, },
87
88	/* 1000baseX-FDX */
89	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA,
90	  0, },
91
92	/* 1000baseT */
93	{ BMCR_S1000,		ANAR_CSMA,
94	  GTCR_ADV_1000THDX },
95
96	/* 1000baseT-FDX */
97	{ BMCR_S1000,		ANAR_CSMA,
98	  GTCR_ADV_1000TFDX },
99};
100
101void
102mii_phy_setmedia(struct mii_softc *sc)
103{
104	struct mii_data *mii = sc->mii_pdata;
105	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
106	int bmcr, anar, gtcr;
107
108	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
109		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 ||
110		    (sc->mii_flags & MIIF_FORCEANEG))
111			(void) mii_phy_auto(sc);
112		return;
113	}
114
115	/*
116	 * Table index is stored in the media entry.
117	 */
118
119	KASSERT(ife->ifm_data >=0 && ife->ifm_data < MII_NMEDIA,
120	    ("invalid ife->ifm_data (0x%x) in mii_phy_setmedia",
121	    ife->ifm_data));
122
123	anar = mii_media_table[ife->ifm_data].mm_anar;
124	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
125	gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
126
127	if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
128		switch (IFM_SUBTYPE(ife->ifm_media)) {
129		case IFM_1000_T:
130			gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
131			break;
132
133		default:
134			panic("mii_phy_setmedia: MASTER on wrong media");
135		}
136	}
137
138	if (ife->ifm_media & IFM_LOOP)
139		bmcr |= BMCR_LOOP;
140
141	PHY_WRITE(sc, MII_ANAR, anar);
142	PHY_WRITE(sc, MII_BMCR, bmcr);
143	if (sc->mii_flags & MIIF_HAVE_GTCR)
144		PHY_WRITE(sc, MII_100T2CR, gtcr);
145}
146
147int
148mii_phy_auto(struct mii_softc *sc)
149{
150
151	/*
152	 * Check for 1000BASE-X.  Autonegotiation is a bit
153	 * different on such devices.
154	 */
155	if (sc->mii_flags & MIIF_IS_1000X) {
156		uint16_t anar = 0;
157
158		if (sc->mii_extcapabilities & EXTSR_1000XFDX)
159			anar |= ANAR_X_FD;
160		if (sc->mii_extcapabilities & EXTSR_1000XHDX)
161			anar |= ANAR_X_HD;
162
163		if (sc->mii_flags & MIIF_DOPAUSE) {
164			/* XXX Asymmetric vs. symmetric? */
165			anar |= ANLPAR_X_PAUSE_TOWARDS;
166		}
167
168		PHY_WRITE(sc, MII_ANAR, anar);
169	} else {
170		uint16_t anar;
171
172		anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
173		    ANAR_CSMA;
174		if (sc->mii_flags & MIIF_DOPAUSE)
175			anar |= ANAR_FC;
176		PHY_WRITE(sc, MII_ANAR, anar);
177		if (sc->mii_flags & MIIF_HAVE_GTCR) {
178			uint16_t gtcr = 0;
179
180			if (sc->mii_extcapabilities & EXTSR_1000TFDX)
181				gtcr |= GTCR_ADV_1000TFDX;
182			if (sc->mii_extcapabilities & EXTSR_1000THDX)
183				gtcr |= GTCR_ADV_1000THDX;
184
185			PHY_WRITE(sc, MII_100T2CR, gtcr);
186		}
187	}
188	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
189	return (EJUSTRETURN);
190}
191
192int
193mii_phy_tick(struct mii_softc *sc)
194{
195	struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
196	struct ifnet *ifp = sc->mii_pdata->mii_ifp;
197	int reg;
198
199	/* Just bail now if the interface is down. */
200	if ((ifp->if_flags & IFF_UP) == 0)
201		return (EJUSTRETURN);
202
203	/*
204	 * If we're not doing autonegotiation, we don't need to do
205	 * any extra work here.  However, we need to check the link
206	 * status so we can generate an announcement if the status
207	 * changes.
208	 */
209	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
210		sc->mii_ticks = 0;	/* reset autonegotiation timer. */
211		return (0);
212	}
213
214	/* Read the status register twice; BMSR_LINK is latch-low. */
215	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
216	if (reg & BMSR_LINK) {
217		sc->mii_ticks = 0;	/* reset autonegotiation timer. */
218		/* See above. */
219		return (0);
220	}
221
222	/* Announce link loss right after it happens */
223	if (sc->mii_ticks++ == 0)
224		return (0);
225
226	/* XXX: use default value if phy driver did not set mii_anegticks */
227	if (sc->mii_anegticks == 0)
228		sc->mii_anegticks = MII_ANEGTICKS_GIGE;
229
230	/* Only retry autonegotiation every mii_anegticks ticks. */
231	if (sc->mii_ticks <= sc->mii_anegticks)
232		return (EJUSTRETURN);
233
234	sc->mii_ticks = 0;
235	mii_phy_reset(sc);
236	mii_phy_auto(sc);
237	return (0);
238}
239
240void
241mii_phy_reset(struct mii_softc *sc)
242{
243	struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
244	int reg, i;
245
246	if (sc->mii_flags & MIIF_NOISOLATE)
247		reg = BMCR_RESET;
248	else
249		reg = BMCR_RESET | BMCR_ISO;
250	PHY_WRITE(sc, MII_BMCR, reg);
251
252	/* Wait 100ms for it to complete. */
253	for (i = 0; i < 100; i++) {
254		reg = PHY_READ(sc, MII_BMCR);
255		if ((reg & BMCR_RESET) == 0)
256			break;
257		DELAY(1000);
258	}
259
260	if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
261		if ((ife == NULL && sc->mii_inst != 0) ||
262		    (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
263			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
264	}
265}
266
267void
268mii_phy_down(struct mii_softc *sc)
269{
270
271}
272
273void
274mii_phy_update(struct mii_softc *sc, int cmd)
275{
276	struct mii_data *mii = sc->mii_pdata;
277
278	if (sc->mii_media_active != mii->mii_media_active ||
279	    cmd == MII_MEDIACHG) {
280		MIIBUS_STATCHG(sc->mii_dev);
281		sc->mii_media_active = mii->mii_media_active;
282	}
283	if (sc->mii_media_status != mii->mii_media_status) {
284		MIIBUS_LINKCHG(sc->mii_dev);
285		sc->mii_media_status = mii->mii_media_status;
286	}
287}
288
289/*
290 * Given an ifmedia word, return the corresponding ANAR value.
291 */
292int
293mii_anar(int media)
294{
295	int rv;
296
297	switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
298	case IFM_ETHER|IFM_10_T:
299		rv = ANAR_10|ANAR_CSMA;
300		break;
301	case IFM_ETHER|IFM_10_T|IFM_FDX:
302		rv = ANAR_10_FD|ANAR_CSMA;
303		break;
304	case IFM_ETHER|IFM_100_TX:
305		rv = ANAR_TX|ANAR_CSMA;
306		break;
307	case IFM_ETHER|IFM_100_TX|IFM_FDX:
308		rv = ANAR_TX_FD|ANAR_CSMA;
309		break;
310	case IFM_ETHER|IFM_100_T4:
311		rv = ANAR_T4|ANAR_CSMA;
312		break;
313	default:
314		rv = 0;
315		break;
316	}
317
318	return (rv);
319}
320
321/*
322 * Initialize generic PHY media based on BMSR, called when a PHY is
323 * attached.  We expect to be set up to print a comma-separated list
324 * of media names.  Does not print a newline.
325 */
326void
327mii_add_media(struct mii_softc *sc)
328{
329	const char *sep = "";
330	struct mii_data *mii;
331
332	mii = device_get_softc(sc->mii_dev);
333	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) {
334		printf("no media present");
335		return;
336	}
337
338#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
339#define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
340
341	if (sc->mii_capabilities & BMSR_10THDX) {
342		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0);
343		PRINT("10baseT");
344	}
345	if (sc->mii_capabilities & BMSR_10TFDX) {
346		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
347		    BMCR_FDX);
348		PRINT("10baseT-FDX");
349	}
350	if (sc->mii_capabilities & BMSR_100TXHDX) {
351		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
352		    BMCR_S100);
353		PRINT("100baseTX");
354	}
355	if (sc->mii_capabilities & BMSR_100TXFDX) {
356		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
357		    BMCR_S100|BMCR_FDX);
358		PRINT("100baseTX-FDX");
359	}
360	if (sc->mii_capabilities & BMSR_100T4) {
361		/*
362		 * XXX How do you enable 100baseT4?  I assume we set
363		 * XXX BMCR_S100 and then assume the PHYs will take
364		 * XXX watever action is necessary to switch themselves
365		 * XXX into T4 mode.
366		 */
367		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
368		    BMCR_S100);
369		PRINT("100baseT4");
370	}
371	if (sc->mii_capabilities & BMSR_ANEG) {
372		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
373		    BMCR_AUTOEN);
374		PRINT("auto");
375	}
376
377
378
379#undef ADD
380#undef PRINT
381}
382
383/*
384 * Initialize generic PHY media based on BMSR, called when a PHY is
385 * attached.  We expect to be set up to print a comma-separated list
386 * of media names.  Does not print a newline.
387 */
388void
389mii_phy_add_media(struct mii_softc *sc)
390{
391	struct mii_data *mii = sc->mii_pdata;
392	const char *sep = "";
393
394	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
395	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) {
396		printf("no media present");
397		return;
398	}
399
400	/* Set aneg timer for 10/100 media. Gigabit media handled below. */
401	sc->mii_anegticks = MII_ANEGTICKS;
402
403#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
404#define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
405
406	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
407		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
408		    MII_MEDIA_NONE);
409
410	/*
411	 * There are different interpretations for the bits in
412	 * HomePNA PHYs.  And there is really only one media type
413	 * that is supported.
414	 */
415	if (sc->mii_flags & MIIF_IS_HPNA) {
416		if (sc->mii_capabilities & BMSR_10THDX) {
417			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
418					 sc->mii_inst),
419			    MII_MEDIA_10_T);
420			PRINT("HomePNA1");
421		}
422		return;
423	}
424
425	if (sc->mii_capabilities & BMSR_10THDX) {
426		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
427		    MII_MEDIA_10_T);
428		PRINT("10baseT");
429	}
430	if (sc->mii_capabilities & BMSR_10TFDX) {
431		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
432		    MII_MEDIA_10_T_FDX);
433		PRINT("10baseT-FDX");
434	}
435	if (sc->mii_capabilities & BMSR_100TXHDX) {
436		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
437		    MII_MEDIA_100_TX);
438		PRINT("100baseTX");
439	}
440	if (sc->mii_capabilities & BMSR_100TXFDX) {
441		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
442		    MII_MEDIA_100_TX_FDX);
443		PRINT("100baseTX-FDX");
444	}
445	if (sc->mii_capabilities & BMSR_100T4) {
446		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
447		    MII_MEDIA_100_T4);
448		PRINT("100baseT4");
449	}
450
451	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
452		/*
453		 * XXX Right now only handle 1000SX and 1000TX.  Need
454		 * XXX to handle 1000LX and 1000CX some how.
455		 */
456		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
457			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
458			sc->mii_flags |= MIIF_IS_1000X;
459			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
460			    sc->mii_inst), MII_MEDIA_1000_X);
461			PRINT("1000baseSX");
462		}
463		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
464			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
465			sc->mii_flags |= MIIF_IS_1000X;
466			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
467			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
468			PRINT("1000baseSX-FDX");
469		}
470
471		/*
472		 * 1000baseT media needs to be able to manipulate
473		 * master/slave mode.  We set IFM_ETH_MASTER in
474		 * the "don't care mask" and filter it out when
475		 * the media is set.
476		 *
477		 * All 1000baseT PHYs have a 1000baseT control register.
478		 */
479		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
480			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
481			sc->mii_flags |= MIIF_HAVE_GTCR;
482			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
483			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
484			    sc->mii_inst), MII_MEDIA_1000_T);
485			PRINT("1000baseT");
486		}
487		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
488			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
489			sc->mii_flags |= MIIF_HAVE_GTCR;
490			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
491			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
492			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
493			PRINT("1000baseT-FDX");
494		}
495	}
496
497	if (sc->mii_capabilities & BMSR_ANEG) {
498		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
499		    MII_NMEDIA);	/* intentionally invalid index */
500		PRINT("auto");
501	}
502#undef ADD
503#undef PRINT
504}
505
506int
507mii_phy_detach(device_t dev)
508{
509	struct mii_softc *sc;
510
511	sc = device_get_softc(dev);
512	mii_phy_down(sc);
513	sc->mii_dev = NULL;
514	LIST_REMOVE(sc, mii_list);
515
516	return(0);
517}
518
519const struct mii_phydesc *
520mii_phy_match_gen(const struct mii_attach_args *ma,
521  const struct mii_phydesc *mpd, size_t len)
522{
523
524	for (; mpd->mpd_name != NULL;
525	     mpd = (const struct mii_phydesc *) ((const char *) mpd + len)) {
526		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
527		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
528			return (mpd);
529	}
530	return (NULL);
531}
532
533const struct mii_phydesc *
534mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
535{
536
537	return (mii_phy_match_gen(ma, mpd, sizeof(struct mii_phydesc)));
538}
539
540int
541mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv)
542{
543
544	mpd = mii_phy_match(device_get_ivars(dev), mpd);
545	if (mpd != NULL) {
546		device_set_desc(dev, mpd->mpd_name);
547		return (mrv);
548	}
549
550	return (ENXIO);
551}
552