xmphy.c revision 77078
1267843Sdelphij/*
2267843Sdelphij * Copyright (c) 2000
3267843Sdelphij *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4267843Sdelphij *
5267843Sdelphij * Redistribution and use in source and binary forms, with or without
6267843Sdelphij * modification, are permitted provided that the following conditions
7267843Sdelphij * are met:
8267843Sdelphij * 1. Redistributions of source code must retain the above copyright
9267843Sdelphij *    notice, this list of conditions and the following disclaimer.
10267843Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11267843Sdelphij *    notice, this list of conditions and the following disclaimer in the
12267843Sdelphij *    documentation and/or other materials provided with the distribution.
13267843Sdelphij * 3. All advertising materials mentioning features or use of this software
14267843Sdelphij *    must display the following acknowledgement:
15267843Sdelphij *	This product includes software developed by Bill Paul.
16267843Sdelphij * 4. Neither the name of the author nor the names of any co-contributors
17267843Sdelphij *    may be used to endorse or promote products derived from this software
18267843Sdelphij *    without specific prior written permission.
19267843Sdelphij *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
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
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $FreeBSD: head/sys/dev/mii/xmphy.c 77078 2001-05-23 22:10:55Z wpaul $
33 */
34
35/*
36 * driver for the XaQti XMAC II's internal PHY. This is sort of
37 * like a 10/100 PHY, except the only thing we're really autoselecting
38 * here is full/half duplex. Speed is always 1000mbps.
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/socket.h>
46#include <sys/bus.h>
47
48
49#include <net/if.h>
50#include <net/if_media.h>
51
52#include <dev/mii/mii.h>
53#include <dev/mii/miivar.h>
54#include <dev/mii/miidevs.h>
55
56#include <dev/mii/xmphyreg.h>
57
58#include "miibus_if.h"
59
60#if !defined(lint)
61static const char rcsid[] =
62  "$FreeBSD: head/sys/dev/mii/xmphy.c 77078 2001-05-23 22:10:55Z wpaul $";
63#endif
64
65static int xmphy_probe		__P((device_t));
66static int xmphy_attach		__P((device_t));
67static int xmphy_detach		__P((device_t));
68
69static device_method_t xmphy_methods[] = {
70	/* device interface */
71	DEVMETHOD(device_probe,		xmphy_probe),
72	DEVMETHOD(device_attach,	xmphy_attach),
73	DEVMETHOD(device_detach,	xmphy_detach),
74	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
75	{ 0, 0 }
76};
77
78static devclass_t xmphy_devclass;
79
80static driver_t xmphy_driver = {
81	"xmphy",
82	xmphy_methods,
83	sizeof(struct mii_softc)
84};
85
86DRIVER_MODULE(xmphy, miibus, xmphy_driver, xmphy_devclass, 0, 0);
87
88int	xmphy_service __P((struct mii_softc *, struct mii_data *, int));
89void	xmphy_status __P((struct mii_softc *));
90
91static int	xmphy_mii_phy_auto __P((struct mii_softc *, int));
92extern void	mii_phy_auto_timeout __P((void *));
93
94static int xmphy_probe(dev)
95	device_t		dev;
96{
97	struct mii_attach_args *ma;
98
99	ma = device_get_ivars(dev);
100
101	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxXAQTI &&
102	    MII_MODEL(ma->mii_id2) == MII_MODEL_XAQTI_XMACII) {
103		device_set_desc(dev, MII_STR_XAQTI_XMACII);
104		return(0);
105	}
106
107	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_JATO &&
108	    MII_MODEL(ma->mii_id2) == MII_MODEL_JATO_BASEX) {
109		device_set_desc(dev, MII_STR_JATO_BASEX);
110		return(0);
111	}
112
113	return(ENXIO);
114}
115
116static int xmphy_attach(dev)
117	device_t		dev;
118{
119	struct mii_softc *sc;
120	struct mii_attach_args *ma;
121	struct mii_data *mii;
122	const char *sep = "";
123
124	sc = device_get_softc(dev);
125	ma = device_get_ivars(dev);
126	sc->mii_dev = device_get_parent(dev);
127	mii = device_get_softc(sc->mii_dev);
128	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
129
130	sc->mii_inst = mii->mii_instance;
131	sc->mii_phy = ma->mii_phyno;
132	sc->mii_service = xmphy_service;
133	sc->mii_pdata = mii;
134
135	sc->mii_flags |= MIIF_NOISOLATE;
136	mii->mii_instance++;
137
138#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
139#define PRINT(s)	printf("%s%s", sep, s); sep = ", "
140
141	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
142	    BMCR_ISO);
143#if 0
144	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
145	    BMCR_LOOP|BMCR_S100);
146#endif
147
148	mii_phy_reset(sc);
149
150	device_printf(dev, " ");
151	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, sc->mii_inst),
152	    XMPHY_BMCR_FDX);
153	PRINT("1000baseSX");
154	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 0);
155	PRINT("1000baseSX-FDX");
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
167static int xmphy_detach(dev)
168	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	if (sc->mii_flags & MIIF_DOINGAUTO)
176		untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
177	sc->mii_dev = NULL;
178	LIST_REMOVE(sc, mii_list);
179
180	return(0);
181}
182int
183xmphy_service(sc, mii, cmd)
184	struct mii_softc *sc;
185	struct mii_data *mii;
186	int cmd;
187{
188	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
189	int reg;
190
191	switch (cmd) {
192	case MII_POLLSTAT:
193		/*
194		 * If we're not polling our PHY instance, just return.
195		 */
196		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
197			return (0);
198		break;
199
200	case MII_MEDIACHG:
201		/*
202		 * If the media indicates a different PHY instance,
203		 * isolate ourselves.
204		 */
205		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
206			reg = PHY_READ(sc, MII_BMCR);
207			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
208			return (0);
209		}
210
211		/*
212		 * If the interface is not up, don't do anything.
213		 */
214		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
215			break;
216
217		switch (IFM_SUBTYPE(ife->ifm_media)) {
218		case IFM_AUTO:
219#ifdef foo
220			/*
221			 * If we're already in auto mode, just return.
222			 */
223			if (PHY_READ(sc, XMPHY_MII_BMCR) & XMPHY_BMCR_AUTOEN)
224				return (0);
225#endif
226			(void) xmphy_mii_phy_auto(sc, 1);
227			break;
228		case IFM_1000_SX:
229			mii_phy_reset(sc);
230			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
231				PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_FDX);
232				PHY_WRITE(sc, XMPHY_MII_BMCR, XMPHY_BMCR_FDX);
233			} else {
234				PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_HDX);
235				PHY_WRITE(sc, XMPHY_MII_BMCR, 0);
236			}
237			break;
238		case IFM_100_T4:
239		case IFM_100_TX:
240		case IFM_10_T:
241		default:
242			return (EINVAL);
243		}
244		break;
245
246	case MII_TICK:
247		/*
248		 * If we're not currently selected, just return.
249		 */
250		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
251			return (0);
252
253		/*
254		 * Only used for autonegotiation.
255		 */
256		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
257			return (0);
258
259		/*
260		 * Is the interface even up?
261		 */
262		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
263			return (0);
264
265		/*
266		 * Only retry autonegotiation every 5 seconds.
267		 */
268		if (++sc->mii_ticks != 5)
269			return (0);
270
271		sc->mii_ticks = 0;
272
273		/*
274		 * Check to see if we have link.  If we do, we don't
275		 * need to restart the autonegotiation process.  Read
276		 * the BMSR twice in case it's latched.
277		 */
278		reg = PHY_READ(sc, XMPHY_MII_BMSR) |
279		    PHY_READ(sc, XMPHY_MII_BMSR);
280		if (reg & XMPHY_BMSR_LINK)
281			break;
282
283		mii_phy_reset(sc);
284		if (xmphy_mii_phy_auto(sc, 0) == EJUSTRETURN)
285			return(0);
286		break;
287	}
288
289	/* Update the media status. */
290	xmphy_status(sc);
291
292	/* Callback if something changed. */
293	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
294		MIIBUS_STATCHG(sc->mii_dev);
295		sc->mii_active = mii->mii_media_active;
296	}
297	return (0);
298}
299
300void
301xmphy_status(sc)
302	struct mii_softc *sc;
303{
304	struct mii_data *mii = sc->mii_pdata;
305	int bmsr, bmcr, anlpar;
306
307	mii->mii_media_status = IFM_AVALID;
308	mii->mii_media_active = IFM_ETHER;
309
310	bmsr = PHY_READ(sc, XMPHY_MII_BMSR) |
311	    PHY_READ(sc, XMPHY_MII_BMSR);
312	if (bmsr & XMPHY_BMSR_LINK)
313		mii->mii_media_status |= IFM_ACTIVE;
314
315	/* Do dummy read of extended status register. */
316	bmcr = PHY_READ(sc, XMPHY_MII_EXTSTS);
317
318	bmcr = PHY_READ(sc, XMPHY_MII_BMCR);
319
320	if (bmcr & XMPHY_BMCR_LOOP)
321		mii->mii_media_active |= IFM_LOOP;
322
323
324	if (bmcr & XMPHY_BMCR_AUTOEN) {
325		if ((bmsr & XMPHY_BMSR_ACOMP) == 0) {
326			if (bmsr & XMPHY_BMSR_LINK) {
327				mii->mii_media_active |= IFM_1000_SX|IFM_HDX;
328				return;
329			}
330			/* Erg, still trying, I guess... */
331			mii->mii_media_active |= IFM_NONE;
332			return;
333		}
334
335		mii->mii_media_active |= IFM_1000_SX;
336		anlpar = PHY_READ(sc, XMPHY_MII_ANAR) &
337		    PHY_READ(sc, XMPHY_MII_ANLPAR);
338		if (anlpar & XMPHY_ANLPAR_FDX)
339			mii->mii_media_active |= IFM_FDX;
340		else
341			mii->mii_media_active |= IFM_HDX;
342		return;
343	}
344
345	mii->mii_media_active |= IFM_1000_SX;
346	if (bmcr & XMPHY_BMCR_FDX)
347		mii->mii_media_active |= IFM_FDX;
348	else
349		mii->mii_media_active |= IFM_HDX;
350
351	return;
352}
353
354
355static int
356xmphy_mii_phy_auto(mii, waitfor)
357	struct mii_softc *mii;
358	int waitfor;
359{
360	int bmsr, anar = 0, i;
361
362	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
363		anar = PHY_READ(mii, XMPHY_MII_ANAR);
364		anar |= XMPHY_ANAR_FDX|XMPHY_ANAR_HDX;
365		PHY_WRITE(mii, XMPHY_MII_ANAR, anar);
366		DELAY(1000);
367		PHY_WRITE(mii, XMPHY_MII_BMCR,
368		    XMPHY_BMCR_AUTOEN | XMPHY_BMCR_STARTNEG);
369	}
370
371	if (waitfor) {
372		/* Wait 500ms for it to complete. */
373		for (i = 0; i < 500; i++) {
374			if ((bmsr = PHY_READ(mii, XMPHY_MII_BMSR)) &
375			    XMPHY_BMSR_ACOMP)
376				return (0);
377			DELAY(1000);
378#if 0
379		if ((bmsr & BMSR_ACOMP) == 0)
380			printf("%s: autonegotiation failed to complete\n",
381			    mii->mii_dev.dv_xname);
382#endif
383		}
384
385		/*
386		 * Don't need to worry about clearing MIIF_DOINGAUTO.
387		 * If that's set, a timeout is pending, and it will
388		 * clear the flag.
389		 */
390		return (EIO);
391	}
392
393	/*
394	 * Just let it finish asynchronously.  This is for the benefit of
395	 * the tick handler driving autonegotiation.  Don't want 500ms
396	 * delays all the time while the system is running!
397	 */
398	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
399		mii->mii_flags |= MIIF_DOINGAUTO;
400		mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
401	}
402	return (EJUSTRETURN);
403}
404