inphy.c revision 74377
1/*-
2 * Copyright (c) 2001 Jonathan Lemon
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
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 *	$FreeBSD: head/sys/dev/mii/inphy.c 74377 2001-03-17 02:50:20Z jlemon $
30 */
31
32/*
33 * driver for Intel 82553 and 82555 PHYs
34 */
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/malloc.h>
40#include <sys/socket.h>
41#include <sys/bus.h>
42
43#include <net/if.h>
44#include <net/if_media.h>
45
46#include <dev/mii/mii.h>
47#include <dev/mii/miivar.h>
48#include <dev/mii/miidevs.h>
49
50#include <dev/mii/inphyreg.h>
51
52#include "miibus_if.h"
53
54static int 	inphy_probe(device_t dev);
55static int 	inphy_attach(device_t dev);
56static int 	inphy_detach(device_t dev);
57
58static device_method_t inphy_methods[] = {
59	/* device interface */
60	DEVMETHOD(device_probe,		inphy_probe),
61	DEVMETHOD(device_attach,	inphy_attach),
62	DEVMETHOD(device_detach,	inphy_detach),
63	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
64	{ 0, 0 }
65};
66
67static devclass_t inphy_devclass;
68
69static driver_t inphy_driver = {
70	"inphy",
71	inphy_methods,
72	sizeof(struct mii_softc)
73};
74
75DRIVER_MODULE(inphy, miibus, inphy_driver, inphy_devclass, 0, 0);
76
77int	inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd);
78void	inphy_status(struct mii_softc *sc);
79
80
81static int
82inphy_probe(device_t dev)
83{
84	struct mii_attach_args *ma;
85	device_t parent;
86
87	ma = device_get_ivars(dev);
88	parent = device_get_parent(device_get_parent(dev));
89
90	/* Intel 82555 */
91	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_INTEL &&
92	    MII_MODEL(ma->mii_id2) == MII_MODEL_INTEL_I82555) {
93		device_set_desc(dev, MII_STR_INTEL_I82555);
94		return (0);
95	}
96
97	/* Intel 82553 C stepping */
98	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_INTEL &&
99	    MII_MODEL(ma->mii_id2) == MII_MODEL_INTEL_I82553C) {
100		device_set_desc(dev, MII_STR_INTEL_I82553C);
101		return (0);
102	}
103
104	/* Intel 82553 A/B steppings */
105	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxINTEL &&
106	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxINTEL_I82553AB) {
107		device_set_desc(dev, MII_STR_xxINTEL_I82553AB);
108		return (0);
109	}
110
111	return (ENXIO);
112}
113
114static int
115inphy_attach(device_t dev)
116{
117	struct mii_softc *sc;
118	struct mii_attach_args *ma;
119	struct mii_data *mii;
120
121	sc = device_get_softc(dev);
122	ma = device_get_ivars(dev);
123	sc->mii_dev = device_get_parent(dev);
124	mii = device_get_softc(sc->mii_dev);
125	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
126
127	sc->mii_inst = mii->mii_instance;
128	sc->mii_phy = ma->mii_phyno;
129	sc->mii_service = inphy_service;
130	sc->mii_pdata = mii;
131	mii->mii_instance++;
132
133#if 0
134	sc->mii_flags |= MIIF_NOISOLATE;
135#endif
136
137	ifmedia_add(&mii->mii_media,
138	    IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
139	    BMCR_LOOP|BMCR_S100, NULL);
140
141	mii_phy_reset(sc);
142
143	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
144	device_printf(dev, " ");
145	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
146		printf("no media present");
147	else
148		mii_add_media(mii, sc->mii_capabilities, sc->mii_inst);
149	printf("\n");
150
151	MIIBUS_MEDIAINIT(sc->mii_dev);
152
153	return (0);
154}
155
156static int
157inphy_detach(device_t dev)
158{
159	struct mii_softc *sc;
160	struct mii_data *mii;
161
162	sc = device_get_softc(dev);
163	mii = device_get_softc(device_get_softc(dev));
164	mii_phy_auto_stop(sc);
165	sc->mii_dev = NULL;
166	LIST_REMOVE(sc, mii_list);
167
168	return (0);
169}
170
171int
172inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
173{
174	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
175	int reg;
176
177	switch (cmd) {
178	case MII_POLLSTAT:
179		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
180			return (0);
181		break;
182
183	case MII_MEDIACHG:
184		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
185			reg = PHY_READ(sc, MII_BMCR);
186			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
187			return (0);
188		}
189
190		/*
191		 * If the interface is not up, don't do anything.
192		 */
193		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
194			break;
195
196		switch (IFM_SUBTYPE(ife->ifm_media)) {
197		case IFM_AUTO:
198#if 0
199			/*
200			 * XXX
201			 * we need to differentiate between 'auto media'
202			 * and "NWAY autonegotiate enabled".  For now,
203			 * just re-start full autodetect again.
204			 */
205			/*
206			 * If we're already in auto mode, just return.
207			 */
208			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
209				return (0);
210#endif
211			(void) mii_phy_auto(sc, 0);
212			break;
213		case IFM_100_T4:
214			/*
215			 * XXX Not supported as a manual setting right now.
216			 */
217			return (EINVAL);
218		default:
219			/*
220			 * BMCR data is stored in the ifmedia entry.
221			 */
222			PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
223			PHY_WRITE(sc, MII_BMCR, ife->ifm_data |
224			    (sc->mii_capabilities & BMSR_ANEG ?
225			    BMCR_AUTOEN | BMCR_STARTNEG : 0));
226		}
227		break;
228
229	case MII_TICK:
230		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
231			return (0);
232
233		/*
234		 * Is the interface even up?
235		 */
236		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
237			return (0);
238
239		/*
240		 * Only used for autonegotiation.
241		 */
242		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
243			return (0);
244
245		/*
246		 * check for link.
247        	 * Read the status register twice; BMSR_LINK is latch-low.
248		 */
249        	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
250		if (reg & BMSR_LINK)
251	                return (0);
252
253		/*
254		 * Only retry autonegotiation every 5 seconds.
255		 */
256		if (++sc->mii_ticks != 5)
257			return (0);
258
259		sc->mii_ticks = 0;
260		mii_phy_reset(sc);
261		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
262			return (0);
263		break;
264	}
265
266	/* Update the media status. */
267	inphy_status(sc);
268
269	/* Callback if something changed. */
270	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
271		MIIBUS_STATCHG(sc->mii_dev);
272		sc->mii_active = mii->mii_media_active;
273	}
274	return (0);
275}
276
277void
278inphy_status(struct mii_softc *sc)
279{
280	struct mii_data *mii = sc->mii_pdata;
281	int bmsr, bmcr, scr;
282
283	mii->mii_media_status = IFM_AVALID;
284	mii->mii_media_active = IFM_ETHER;
285
286	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
287	if (bmsr & BMSR_LINK)
288		mii->mii_media_status |= IFM_ACTIVE;
289
290	bmcr = PHY_READ(sc, MII_BMCR);
291	if (bmcr & BMCR_ISO) {
292		mii->mii_media_active |= IFM_NONE;
293		mii->mii_media_status = 0;
294		return;
295	}
296
297	if (bmcr & BMCR_LOOP)
298		mii->mii_media_active |= IFM_LOOP;
299
300	if (bmcr & BMCR_AUTOEN) {
301		if ((bmsr & BMSR_ACOMP) == 0) {
302			mii->mii_media_active |= IFM_NONE;
303			return;
304		}
305
306		scr = PHY_READ(sc, MII_INPHY_SCR);
307		if (scr & SCR_S100)
308			mii->mii_media_active |= IFM_100_TX;
309		else
310			mii->mii_media_active |= IFM_10_T;
311		if (scr & SCR_FDX)
312			mii->mii_media_active |= IFM_FDX;
313	} else
314		mii->mii_media_active |= mii_media_from_bmcr(bmcr);
315}
316