xref: /dragonfly/sys/dev/netif/mii_layer/mlphy.c (revision 9bb2a92d)
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <wpaul@ctr.columbia.edu>.  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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
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: src/sys/dev/mii/mlphy.c,v 1.2.2.3 2001/02/09 09:50:15 asmodai Exp $
33  * $DragonFly: src/sys/dev/netif/mii_layer/mlphy.c,v 1.4 2003/08/27 09:38:31 rob Exp $
34  */
35 
36 /*
37  * driver for Micro Linear 6692 PHYs
38  *
39  * The Micro Linear 6692 is a strange beast, and dealing with it using
40  * this code framework is tricky. The 6692 is actually a 100Mbps-only
41  * device, which means that a second PHY is required to support 10Mbps
42  * modes. However, even though the 6692 does not support 10Mbps modes,
43  * it can still advertise them when performing autonegotiation. If a
44  * 10Mbps mode is negotiated, we must program the registers of the
45  * companion PHY accordingly in addition to programming the registers
46  * of the 6692.
47  *
48  * This device also does not have vendor/device ID registers.
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/socket.h>
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 
59 #include <machine/clock.h>
60 
61 #include <net/if.h>
62 #include <net/if_media.h>
63 
64 #include "mii.h"
65 #include "miivar.h"
66 
67 #include "miibus_if.h"
68 
69 #define ML_STATE_AUTO_SELF	1
70 #define ML_STATE_AUTO_OTHER	2
71 
72 struct mlphy_softc	{
73 	struct mii_softc	ml_mii;
74 	int			ml_state;
75 	int			ml_linked;
76 };
77 
78 static int mlphy_probe		(device_t);
79 static int mlphy_attach		(device_t);
80 static int mlphy_detach		(device_t);
81 
82 static device_method_t mlphy_methods[] = {
83 	/* device interface */
84 	DEVMETHOD(device_probe,		mlphy_probe),
85 	DEVMETHOD(device_attach,	mlphy_attach),
86 	DEVMETHOD(device_detach,	mlphy_detach),
87 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
88 	{ 0, 0 }
89 };
90 
91 static devclass_t mlphy_devclass;
92 
93 static driver_t mlphy_driver = {
94 	"mlphy",
95 	mlphy_methods,
96 	sizeof(struct mlphy_softc)
97 };
98 
99 DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0);
100 
101 static int	mlphy_service (struct mii_softc *, struct mii_data *, int);
102 static void	mlphy_reset (struct mii_softc *);
103 static void	mlphy_status (struct mii_softc *);
104 
105 static int mlphy_probe(dev)
106 	device_t		dev;
107 {
108 	struct mii_attach_args	*ma;
109 	device_t		parent;
110 
111 	ma = device_get_ivars(dev);
112 	parent = device_get_parent(device_get_parent(dev));
113 
114 	/*
115 	 * Micro Linear PHY reports oui == 0 model == 0
116 	 */
117 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
118 	    MII_MODEL(ma->mii_id2) != 0)
119 		return (ENXIO);
120 
121 	/*
122 	 * Make sure the parent is a `tl'. So far, I have only
123 	 * encountered the 6692 on an Olicom card with a ThunderLAN
124 	 * controller chip.
125 	 */
126 	if (strcmp(device_get_name(parent), "tl") != 0)
127 		return (ENXIO);
128 
129 	device_set_desc(dev, "Micro Linear 6692 media interface");
130 
131 	return (0);
132 }
133 
134 static int mlphy_attach(dev)
135 	device_t		dev;
136 {
137 	struct mlphy_softc *msc;
138 	struct mii_softc *sc;
139 	struct mii_attach_args *ma;
140 	struct mii_data *mii;
141 
142 	msc = device_get_softc(dev);
143 	sc = &msc->ml_mii;
144 	ma = device_get_ivars(dev);
145 	sc->mii_dev = device_get_parent(dev);
146 	mii = device_get_softc(sc->mii_dev);
147 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
148 
149 	sc->mii_inst = mii->mii_instance;
150 	sc->mii_phy = ma->mii_phyno;
151 	sc->mii_service = mlphy_service;
152 	sc->mii_pdata = mii;
153 
154 	mii->mii_instance++;
155 
156 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
157 
158 #if 0 /* See above. */
159 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
160 	    BMCR_ISO);
161 #endif
162 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
163 	    BMCR_LOOP|BMCR_S100);
164 
165 	sc->mii_flags &= ~MIIF_NOISOLATE;
166 	mii_phy_reset(sc);
167 	sc->mii_flags |= MIIF_NOISOLATE;
168 
169 	sc->mii_capabilities =
170 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
171 	ma->mii_capmask = ~sc->mii_capabilities;
172 	device_printf(dev, " ");
173 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
174 		printf("no media present");
175 	else
176 		mii_add_media(mii, sc->mii_capabilities,
177 		    sc->mii_inst);
178 	printf("\n");
179 #undef ADD
180 	MIIBUS_MEDIAINIT(sc->mii_dev);
181 	return(0);
182 }
183 
184 static int mlphy_detach(dev)
185 	device_t		dev;
186 {
187 	struct mlphy_softc	*sc;
188 	struct mii_data		*mii;
189 
190 	sc = device_get_softc(dev);
191 	mii = device_get_softc(device_get_parent(dev));
192 	mii_phy_auto_stop(&sc->ml_mii);
193 	sc->ml_mii.mii_dev = NULL;
194 	LIST_REMOVE(&sc->ml_mii, mii_list);
195 
196 	return(0);
197 }
198 
199 static int
200 mlphy_service(xsc, mii, cmd)
201 	struct mii_softc *xsc;
202 	struct mii_data *mii;
203 	int cmd;
204 {
205 	struct ifmedia_entry	*ife = mii->mii_media.ifm_cur;
206 	int			reg;
207 	struct mii_softc	*other = NULL;
208 	struct mlphy_softc	*msc = (struct mlphy_softc *)xsc;
209 	struct mii_softc	*sc = (struct mii_softc *)&msc->ml_mii;
210 	device_t		*devlist;
211 	int			devs, i;
212 
213 	/*
214 	 * See if there's another PHY on this bus with us.
215 	 * If so, we may need it for 10Mbps modes.
216 	 */
217 	device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
218 	for (i = 0; i < devs; i++) {
219 		if (strcmp(device_get_name(devlist[i]), "mlphy")) {
220 			other = device_get_softc(devlist[i]);
221 			break;
222 		}
223 	}
224 	free(devlist, M_TEMP);
225 
226 	switch (cmd) {
227 	case MII_POLLSTAT:
228 		/*
229 		 * If we're not polling our PHY instance, just return.
230 		 */
231 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
232 			return (0);
233 		break;
234 
235 	case MII_MEDIACHG:
236 		/*
237 		 * If the media indicates a different PHY instance,
238 		 * isolate ourselves.
239 		 */
240 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
241 			reg = PHY_READ(sc, MII_BMCR);
242 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
243 			return (0);
244 		}
245 
246 		/*
247 		 * If the interface is not up, don't do anything.
248 		 */
249 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
250 			break;
251 
252 		switch (IFM_SUBTYPE(ife->ifm_media)) {
253 		case IFM_AUTO:
254 			/*
255 			 * For autonegotiation, reset and isolate the
256 			 * companion PHY (if any) and then do NWAY
257 			 * autonegotiation ourselves.
258 			 */
259 			msc->ml_state = ML_STATE_AUTO_SELF;
260 			if (other != NULL) {
261 				mii_phy_reset(other);
262 				PHY_WRITE(other, MII_BMCR, BMCR_ISO);
263 			}
264 			(void) mii_phy_auto(sc, 1);
265 			msc->ml_linked = 0;
266 			return(0);
267 			break;
268 		case IFM_10_T:
269 			/*
270 			 * For 10baseT modes, reset and program the
271 			 * companion PHY (of any), then program ourselves
272 			 * to match. This will put us in pass-through
273 			 * mode and let the companion PHY do all the
274 			 * work.
275 			 *
276 			 * BMCR data is stored in the ifmedia entry.
277 			 */
278 			if (other != NULL) {
279 				mii_phy_reset(other);
280 				PHY_WRITE(other, MII_BMCR, ife->ifm_data);
281 			}
282 			PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
283 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
284 			msc->ml_state = 0;
285 			break;
286 		case IFM_100_TX:
287 			/*
288 			 * For 100baseTX modes, reset and isolate the
289 			 * companion PHY (if any), then program ourselves
290 			 * accordingly.
291 			 *
292 			 * BMCR data is stored in the ifmedia entry.
293 			 */
294 			if (other != NULL) {
295 				mii_phy_reset(other);
296 				PHY_WRITE(other, MII_BMCR, BMCR_ISO);
297 			}
298 			PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
299 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
300 			msc->ml_state = 0;
301 			break;
302 		case IFM_100_T4:
303 			/*
304 			 * XXX Not supported as a manual setting right now.
305 			 */
306 			return (EINVAL);
307 		default:
308 			break;
309 
310 		}
311 		break;
312 
313 	case MII_TICK:
314 		/*
315 		 * If we're not currently selected, just return.
316 		 */
317 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
318 			return (0);
319 
320 		/*
321 		 * Only used for autonegotiation.
322 		 */
323 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
324 			return (0);
325 
326 		/*
327 		 * Is the interface even up?
328 		 */
329 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
330 			return (0);
331 
332 		/*
333 		 * Only retry autonegotiation every 5 seconds.
334 		 */
335 		if (++sc->mii_ticks != 5)
336 			return (0);
337 
338 		sc->mii_ticks = 0;
339 
340 		/*
341 		 * Check to see if we have link.  If we do, we don't
342 		 * need to restart the autonegotiation process.  Read
343 		 * the BMSR twice in case it's latched.
344 		 * If we're in a 10Mbps mode, check the link of the
345 		 * 10Mbps PHY. Sometimes the Micro Linear PHY's
346 		 * linkstat bit will clear while the linkstat bit of
347 		 * the companion PHY will remain set.
348 		 */
349 		if (msc->ml_state == ML_STATE_AUTO_OTHER) {
350 			reg = PHY_READ(other, MII_BMSR) |
351 			    PHY_READ(other, MII_BMSR);
352 		} else {
353 			reg = PHY_READ(sc, MII_BMSR) |
354 			    PHY_READ(sc, MII_BMSR);
355 		}
356 
357 		if (reg & BMSR_LINK) {
358 			if (!msc->ml_linked) {
359 				msc->ml_linked = 1;
360 				mlphy_status(sc);
361 				break;
362 			}
363 			return(0);
364 		}
365 
366 		msc->ml_linked = 0;
367 		mii->mii_media_active = IFM_NONE;
368 		mii_phy_reset(sc);
369 		msc->ml_state = ML_STATE_AUTO_SELF;
370 		if (other != NULL) {
371 			mii_phy_reset(other);
372 			PHY_WRITE(other, MII_BMCR, BMCR_ISO);
373 		}
374 		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
375 			return(0);
376 		break;
377 	}
378 
379 	/* Update the media status. */
380 
381 	if (msc->ml_state == ML_STATE_AUTO_OTHER) {
382 		int			other_inst;
383 		other_inst = other->mii_inst;
384 		other->mii_inst = sc->mii_inst;
385 		(void) (*other->mii_service)(other, mii, MII_POLLSTAT);
386 		other->mii_inst = other_inst;
387 		sc->mii_active = other->mii_active;
388 	} else
389 		ukphy_status(sc);
390 
391 	/* Callback if something changed. */
392 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
393 		MIIBUS_STATCHG(sc->mii_dev);
394 		sc->mii_active = mii->mii_media_active;
395 	}
396 
397 	return (0);
398 }
399 
400 /*
401  * The Micro Linear PHY comes out of reset with the 'autoneg
402  * enable' bit set, which we don't want.
403  */
404 static void mlphy_reset(sc)
405 	struct mii_softc	*sc;
406 {
407 	int			reg;
408 
409 	mii_phy_reset(sc);
410 	reg = PHY_READ(sc, MII_BMCR);
411 	reg &= ~BMCR_AUTOEN;
412 	PHY_WRITE(sc, MII_BMCR, reg);
413 
414 	return;
415 }
416 
417 /*
418  * If we negotiate a 10Mbps mode, we need to check for an alternate
419  * PHY and make sure it's enabled and set correctly.
420  */
421 static void mlphy_status(sc)
422 	struct mii_softc	*sc;
423 {
424 	struct mlphy_softc	*msc = (struct mlphy_softc *)sc;
425 	struct mii_data		*mii = msc->ml_mii.mii_pdata;
426 	struct mii_softc	*other = NULL;
427 	device_t		*devlist;
428 	int			devs, i;
429 
430 	/* See if there's another PHY on the bus with us. */
431 	device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
432 	for (i = 0; i < devs; i++) {
433 		if (strcmp(device_get_name(devlist[i]), "mlphy")) {
434 			other = device_get_softc(devlist[i]);
435 			break;
436 		}
437 	}
438 	free(devlist, M_TEMP);
439 
440 	if (other == NULL)
441 		return;
442 
443 	ukphy_status(sc);
444 
445 	if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
446 		msc->ml_state = ML_STATE_AUTO_SELF;
447 		mii_phy_reset(other);
448 		PHY_WRITE(other, MII_BMCR, BMCR_ISO);
449 	}
450 
451 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
452 		msc->ml_state = ML_STATE_AUTO_OTHER;
453 		mlphy_reset(&msc->ml_mii);
454 		PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO);
455 		mii_phy_reset(other);
456 		mii_phy_auto(other, 1);
457 	}
458 
459 	return;
460 }
461