mlphy.c revision 72186
1254721Semaste/*
2254721Semaste * Copyright (c) 1997, 1998, 1999
3254721Semaste *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4254721Semaste *
5254721Semaste * Redistribution and use in source and binary forms, with or without
6254721Semaste * modification, are permitted provided that the following conditions
7254721Semaste * are met:
8254721Semaste * 1. Redistributions of source code must retain the above copyright
9254721Semaste *    notice, this list of conditions and the following disclaimer.
10254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
11254721Semaste *    notice, this list of conditions and the following disclaimer in the
12254721Semaste *    documentation and/or other materials provided with the distribution.
13254721Semaste * 3. All advertising materials mentioning features or use of this software
14254721Semaste *    must display the following acknowledgement:
15254721Semaste *	This product includes software developed by Bill Paul.
16254721Semaste * 4. Neither the name of the author nor the names of any co-contributors
17254721Semaste *    may be used to endorse or promote products derived from this software
18254721Semaste *    without specific prior written permission.
19254721Semaste *
20254721Semaste * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24254721Semaste * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25254721Semaste * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26254721Semaste * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27254721Semaste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28254721Semaste * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29254721Semaste * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30254721Semaste * THE POSSIBILITY OF SUCH DAMAGE.
31254721Semaste *
32254721Semaste * $FreeBSD: head/sys/dev/mii/mlphy.c 72186 2001-02-08 21:53:05Z asmodai $
33254721Semaste */
34254721Semaste
35254721Semaste/*
36254721Semaste * driver for Micro Linear 6692 PHYs
37254721Semaste *
38254721Semaste * The Micro Linear 6692 is a strange beast, and dealing with it using
39254721Semaste * this code framework is tricky. The 6692 is actually a 100Mbps-only
40254721Semaste * device, which means that a second PHY is required to support 10Mbps
41254721Semaste * modes. However, even though the 6692 does not support 10Mbps modes,
42254721Semaste * it can still advertise them when performing autonegotiation. If a
43254721Semaste * 10Mbps mode is negotiated, we must program the registers of the
44254721Semaste * companion PHY accordingly in addition to programming the registers
45254721Semaste * of the 6692.
46254721Semaste *
47254721Semaste * This device also does not have vendor/device ID registers.
48254721Semaste */
49254721Semaste
50254721Semaste#include <sys/param.h>
51254721Semaste#include <sys/systm.h>
52254721Semaste#include <sys/kernel.h>
53254721Semaste#include <sys/socket.h>
54254721Semaste#include <sys/module.h>
55254721Semaste#include <sys/bus.h>
56254721Semaste
57254721Semaste
58254721Semaste#include <net/if.h>
59254721Semaste#include <net/if_media.h>
60254721Semaste
61254721Semaste#include <dev/mii/mii.h>
62254721Semaste#include <dev/mii/miivar.h>
63254721Semaste
64254721Semaste#include "miibus_if.h"
65254721Semaste
66254721Semaste#define ML_STATE_AUTO_SELF	1
67254721Semaste#define ML_STATE_AUTO_OTHER	2
68254721Semaste
69254721Semastestruct mlphy_softc	{
70254721Semaste	struct mii_softc	ml_mii;
71254721Semaste	int			ml_state;
72254721Semaste	int			ml_linked;
73254721Semaste};
74254721Semaste
75254721Semastestatic int mlphy_probe		__P((device_t));
76254721Semastestatic int mlphy_attach		__P((device_t));
77254721Semastestatic int mlphy_detach		__P((device_t));
78254721Semaste
79254721Semastestatic device_method_t mlphy_methods[] = {
80254721Semaste	/* device interface */
81254721Semaste	DEVMETHOD(device_probe,		mlphy_probe),
82254721Semaste	DEVMETHOD(device_attach,	mlphy_attach),
83254721Semaste	DEVMETHOD(device_detach,	mlphy_detach),
84254721Semaste	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
85254721Semaste	{ 0, 0 }
86254721Semaste};
87254721Semaste
88254721Semastestatic devclass_t mlphy_devclass;
89254721Semaste
90254721Semastestatic driver_t mlphy_driver = {
91254721Semaste	"mlphy",
92254721Semaste	mlphy_methods,
93254721Semaste	sizeof(struct mlphy_softc)
94254721Semaste};
95254721Semaste
96254721SemasteDRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0);
97254721Semaste
98254721Semastestatic int	mlphy_service __P((struct mii_softc *, struct mii_data *, int));
99254721Semastestatic void	mlphy_reset __P((struct mii_softc *));
100254721Semastestatic void	mlphy_status __P((struct mii_softc *));
101254721Semaste
102254721Semastestatic int mlphy_probe(dev)
103254721Semaste	device_t		dev;
104254721Semaste{
105254721Semaste	struct mii_attach_args	*ma;
106254721Semaste	device_t		parent;
107254721Semaste
108254721Semaste	ma = device_get_ivars(dev);
109254721Semaste	parent = device_get_parent(device_get_parent(dev));
110254721Semaste
111254721Semaste	/*
112254721Semaste	 * Micro Linear PHY reports oui == 0 model == 0
113254721Semaste	 */
114254721Semaste	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
115254721Semaste	    MII_MODEL(ma->mii_id2) != 0)
116254721Semaste		return (ENXIO);
117254721Semaste
118254721Semaste	/*
119254721Semaste	 * Make sure the parent is a `tl'. So far, I have only
120254721Semaste	 * encountered the 6692 on an Olicom card with a ThunderLAN
121254721Semaste	 * controller chip.
122254721Semaste	 */
123254721Semaste	if (strcmp(device_get_name(parent), "tl") != 0)
124254721Semaste		return (ENXIO);
125254721Semaste
126254721Semaste	device_set_desc(dev, "Micro Linear 6692 media interface");
127254721Semaste
128254721Semaste	return (0);
129254721Semaste}
130254721Semaste
131254721Semastestatic int mlphy_attach(dev)
132254721Semaste	device_t		dev;
133254721Semaste{
134254721Semaste	struct mlphy_softc *msc;
135254721Semaste	struct mii_softc *sc;
136254721Semaste	struct mii_attach_args *ma;
137254721Semaste	struct mii_data *mii;
138254721Semaste
139254721Semaste	msc = device_get_softc(dev);
140254721Semaste	sc = &msc->ml_mii;
141254721Semaste	ma = device_get_ivars(dev);
142254721Semaste	sc->mii_dev = device_get_parent(dev);
143254721Semaste	mii = device_get_softc(sc->mii_dev);
144254721Semaste	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
145254721Semaste
146254721Semaste	sc->mii_inst = mii->mii_instance;
147254721Semaste	sc->mii_phy = ma->mii_phyno;
148254721Semaste	sc->mii_service = mlphy_service;
149254721Semaste	sc->mii_pdata = mii;
150254721Semaste
151254721Semaste	mii->mii_instance++;
152254721Semaste
153254721Semaste#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
154254721Semaste
155254721Semaste#if 0 /* See above. */
156254721Semaste	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
157254721Semaste	    BMCR_ISO);
158254721Semaste#endif
159254721Semaste	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
160254721Semaste	    BMCR_LOOP|BMCR_S100);
161254721Semaste
162254721Semaste	sc->mii_flags &= ~MIIF_NOISOLATE;
163254721Semaste	mii_phy_reset(sc);
164254721Semaste	sc->mii_flags |= MIIF_NOISOLATE;
165254721Semaste
166254721Semaste	sc->mii_capabilities =
167254721Semaste	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
168254721Semaste	ma->mii_capmask = ~sc->mii_capabilities;
169254721Semaste	device_printf(dev, " ");
170254721Semaste	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
171254721Semaste		printf("no media present");
172254721Semaste	else
173254721Semaste		mii_add_media(mii, sc->mii_capabilities,
174254721Semaste		    sc->mii_inst);
175254721Semaste	printf("\n");
176#undef ADD
177	MIIBUS_MEDIAINIT(sc->mii_dev);
178	return(0);
179}
180
181static int mlphy_detach(dev)
182	device_t		dev;
183{
184	struct mlphy_softc	*sc;
185	struct mii_data		*mii;
186
187	sc = device_get_softc(dev);
188	mii = device_get_softc(device_get_parent(dev));
189	mii_phy_auto_stop(&sc->ml_mii);
190	sc->ml_mii.mii_dev = NULL;
191	LIST_REMOVE(&sc->ml_mii, mii_list);
192
193	return(0);
194}
195
196static int
197mlphy_service(xsc, mii, cmd)
198	struct mii_softc *xsc;
199	struct mii_data *mii;
200	int cmd;
201{
202	struct ifmedia_entry	*ife = mii->mii_media.ifm_cur;
203	int			reg;
204	struct mii_softc	*other = NULL;
205	struct mlphy_softc	*msc = (struct mlphy_softc *)xsc;
206	struct mii_softc	*sc = (struct mii_softc *)&msc->ml_mii;
207	device_t		*devlist;
208	int			devs, i;
209
210	/*
211	 * See if there's another PHY on this bus with us.
212	 * If so, we may need it for 10Mbps modes.
213	 */
214	device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
215	for (i = 0; i < devs; i++) {
216		if (strcmp(device_get_name(devlist[i]), "mlphy")) {
217			other = device_get_softc(devlist[i]);
218			break;
219		}
220	}
221	free(devlist, M_TEMP);
222
223	switch (cmd) {
224	case MII_POLLSTAT:
225		/*
226		 * If we're not polling our PHY instance, just return.
227		 */
228		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
229			return (0);
230		break;
231
232	case MII_MEDIACHG:
233		/*
234		 * If the media indicates a different PHY instance,
235		 * isolate ourselves.
236		 */
237		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
238			reg = PHY_READ(sc, MII_BMCR);
239			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
240			return (0);
241		}
242
243		/*
244		 * If the interface is not up, don't do anything.
245		 */
246		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
247			break;
248
249		switch (IFM_SUBTYPE(ife->ifm_media)) {
250		case IFM_AUTO:
251			/*
252			 * For autonegotiation, reset and isolate the
253			 * companion PHY (if any) and then do NWAY
254			 * autonegotiation ourselves.
255			 */
256			msc->ml_state = ML_STATE_AUTO_SELF;
257			if (other != NULL) {
258				mii_phy_reset(other);
259				PHY_WRITE(other, MII_BMCR, BMCR_ISO);
260			}
261			(void) mii_phy_auto(sc, 1);
262			msc->ml_linked = 0;
263			return(0);
264			break;
265		case IFM_10_T:
266			/*
267			 * For 10baseT modes, reset and program the
268			 * companion PHY (of any), then program ourselves
269			 * to match. This will put us in pass-through
270			 * mode and let the companion PHY do all the
271			 * work.
272			 *
273			 * BMCR data is stored in the ifmedia entry.
274			 */
275			if (other != NULL) {
276				mii_phy_reset(other);
277				PHY_WRITE(other, MII_BMCR, ife->ifm_data);
278			}
279			PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
280			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
281			msc->ml_state = 0;
282			break;
283		case IFM_100_TX:
284			/*
285			 * For 100baseTX modes, reset and isolate the
286			 * companion PHY (if any), then program ourselves
287			 * accordingly.
288			 *
289			 * BMCR data is stored in the ifmedia entry.
290			 */
291			if (other != NULL) {
292				mii_phy_reset(other);
293				PHY_WRITE(other, MII_BMCR, BMCR_ISO);
294			}
295			PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
296			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
297			msc->ml_state = 0;
298			break;
299		case IFM_100_T4:
300			/*
301			 * XXX Not supported as a manual setting right now.
302			 */
303			return (EINVAL);
304		default:
305			break;
306
307		}
308		break;
309
310	case MII_TICK:
311		/*
312		 * If we're not currently selected, just return.
313		 */
314		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
315			return (0);
316
317		/*
318		 * Only used for autonegotiation.
319		 */
320		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
321			return (0);
322
323		/*
324		 * Is the interface even up?
325		 */
326		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
327			return (0);
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		 * If we're in a 10Mbps mode, check the link of the
342		 * 10Mbps PHY. Sometimes the Micro Linear PHY's
343		 * linkstat bit will clear while the linkstat bit of
344		 * the companion PHY will remain set.
345		 */
346		if (msc->ml_state == ML_STATE_AUTO_OTHER) {
347			reg = PHY_READ(other, MII_BMSR) |
348			    PHY_READ(other, MII_BMSR);
349		} else {
350			reg = PHY_READ(sc, MII_BMSR) |
351			    PHY_READ(sc, MII_BMSR);
352		}
353
354		if (reg & BMSR_LINK) {
355			if (!msc->ml_linked) {
356				msc->ml_linked = 1;
357				mlphy_status(sc);
358				break;
359			}
360			return(0);
361		}
362
363		msc->ml_linked = 0;
364		mii->mii_media_active = IFM_NONE;
365		mii_phy_reset(sc);
366		msc->ml_state = ML_STATE_AUTO_SELF;
367		if (other != NULL) {
368			mii_phy_reset(other);
369			PHY_WRITE(other, MII_BMCR, BMCR_ISO);
370		}
371		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
372			return(0);
373		break;
374	}
375
376	/* Update the media status. */
377
378	if (msc->ml_state == ML_STATE_AUTO_OTHER) {
379		int			other_inst;
380		other_inst = other->mii_inst;
381		other->mii_inst = sc->mii_inst;
382		(void) (*other->mii_service)(other, mii, MII_POLLSTAT);
383		other->mii_inst = other_inst;
384		sc->mii_active = other->mii_active;
385	} else
386		ukphy_status(sc);
387
388	/* Callback if something changed. */
389	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
390		MIIBUS_STATCHG(sc->mii_dev);
391		sc->mii_active = mii->mii_media_active;
392	}
393
394	return (0);
395}
396
397/*
398 * The Micro Linear PHY comes out of reset with the 'autoneg
399 * enable' bit set, which we don't want.
400 */
401static void mlphy_reset(sc)
402	struct mii_softc	*sc;
403{
404	int			reg;
405
406	mii_phy_reset(sc);
407	reg = PHY_READ(sc, MII_BMCR);
408	reg &= ~BMCR_AUTOEN;
409	PHY_WRITE(sc, MII_BMCR, reg);
410
411	return;
412}
413
414/*
415 * If we negotiate a 10Mbps mode, we need to check for an alternate
416 * PHY and make sure it's enabled and set correctly.
417 */
418static void mlphy_status(sc)
419	struct mii_softc	*sc;
420{
421	struct mlphy_softc	*msc = (struct mlphy_softc *)sc;
422	struct mii_data		*mii = msc->ml_mii.mii_pdata;
423	struct mii_softc	*other = NULL;
424	device_t		*devlist;
425	int			devs, i;
426
427	/* See if there's another PHY on the bus with us. */
428	device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
429	for (i = 0; i < devs; i++) {
430		if (strcmp(device_get_name(devlist[i]), "mlphy")) {
431			other = device_get_softc(devlist[i]);
432			break;
433		}
434	}
435	free(devlist, M_TEMP);
436
437	if (other == NULL)
438		return;
439
440	ukphy_status(sc);
441
442	if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
443		msc->ml_state = ML_STATE_AUTO_SELF;
444		mii_phy_reset(other);
445		PHY_WRITE(other, MII_BMCR, BMCR_ISO);
446	}
447
448	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
449		msc->ml_state = ML_STATE_AUTO_OTHER;
450		mlphy_reset(&msc->ml_mii);
451		PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO);
452		mii_phy_reset(other);
453		mii_phy_auto(other, 1);
454	}
455
456	return;
457}
458