2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 * $FreeBSD: src/sys/dev/mii/mlphy.c,v 1.2.2.3 2001/02/09 09:50:15 asmodai Exp $
36 * driver for Micro Linear 6692 PHYs
38 * The Micro Linear 6692 is a strange beast, and dealing with it using
39 * this code framework is tricky. The 6692 is actually a 100Mbps-only
40 * device, which means that a second PHY is required to support 10Mbps
41 * modes. However, even though the 6692 does not support 10Mbps modes,
42 * it can still advertise them when performing autonegotiation. If a
43 * 10Mbps mode is negotiated, we must program the registers of the
44 * companion PHY accordingly in addition to programming the registers
47 * This device also does not have vendor/device ID registers.
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/module.h>
58 #include <machine/clock.h>
61 #include <net/if_media.h>
63 #include <dev/mii/mii.h>
64 #include <dev/mii/miivar.h>
66 #include "miibus_if.h"
68 #define ML_STATE_AUTO_SELF 1
69 #define ML_STATE_AUTO_OTHER 2
72 struct mii_softc ml_mii;
77 static int mlphy_probe __P((device_t));
78 static int mlphy_attach __P((device_t));
79 static int mlphy_detach __P((device_t));
81 static device_method_t mlphy_methods[] = {
82 /* device interface */
83 DEVMETHOD(device_probe, mlphy_probe),
84 DEVMETHOD(device_attach, mlphy_attach),
85 DEVMETHOD(device_detach, mlphy_detach),
86 DEVMETHOD(device_shutdown, bus_generic_shutdown),
90 static devclass_t mlphy_devclass;
92 static driver_t mlphy_driver = {
95 sizeof(struct mlphy_softc)
98 DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0);
100 static int mlphy_service __P((struct mii_softc *, struct mii_data *, int));
101 static void mlphy_reset __P((struct mii_softc *));
102 static void mlphy_status __P((struct mii_softc *));
104 static int mlphy_probe(dev)
107 struct mii_attach_args *ma;
110 ma = device_get_ivars(dev);
111 parent = device_get_parent(device_get_parent(dev));
114 * Micro Linear PHY reports oui == 0 model == 0
116 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
117 MII_MODEL(ma->mii_id2) != 0)
121 * Make sure the parent is a `tl'. So far, I have only
122 * encountered the 6692 on an Olicom card with a ThunderLAN
125 if (strcmp(device_get_name(parent), "tl") != 0)
128 device_set_desc(dev, "Micro Linear 6692 media interface");
133 static int mlphy_attach(dev)
136 struct mlphy_softc *msc;
137 struct mii_softc *sc;
138 struct mii_attach_args *ma;
139 struct mii_data *mii;
141 msc = device_get_softc(dev);
143 ma = device_get_ivars(dev);
144 sc->mii_dev = device_get_parent(dev);
145 mii = device_get_softc(sc->mii_dev);
146 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
148 sc->mii_inst = mii->mii_instance;
149 sc->mii_phy = ma->mii_phyno;
150 sc->mii_service = mlphy_service;
155 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
157 #if 0 /* See above. */
158 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
161 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
162 BMCR_LOOP|BMCR_S100);
164 sc->mii_flags &= ~MIIF_NOISOLATE;
166 sc->mii_flags |= MIIF_NOISOLATE;
168 sc->mii_capabilities =
169 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
170 ma->mii_capmask = ~sc->mii_capabilities;
171 device_printf(dev, " ");
172 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
173 printf("no media present");
175 mii_add_media(mii, sc->mii_capabilities,
179 MIIBUS_MEDIAINIT(sc->mii_dev);
183 static int mlphy_detach(dev)
186 struct mlphy_softc *sc;
187 struct mii_data *mii;
189 sc = device_get_softc(dev);
190 mii = device_get_softc(device_get_parent(dev));
191 mii_phy_auto_stop(&sc->ml_mii);
192 sc->ml_mii.mii_dev = NULL;
193 LIST_REMOVE(&sc->ml_mii, mii_list);
199 mlphy_service(xsc, mii, cmd)
200 struct mii_softc *xsc;
201 struct mii_data *mii;
204 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
206 struct mii_softc *other = NULL;
207 struct mlphy_softc *msc = (struct mlphy_softc *)xsc;
208 struct mii_softc *sc = (struct mii_softc *)&msc->ml_mii;
213 * See if there's another PHY on this bus with us.
214 * If so, we may need it for 10Mbps modes.
216 device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
217 for (i = 0; i < devs; i++) {
218 if (strcmp(device_get_name(devlist[i]), "mlphy")) {
219 other = device_get_softc(devlist[i]);
223 free(devlist, M_TEMP);
228 * If we're not polling our PHY instance, just return.
230 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
236 * If the media indicates a different PHY instance,
239 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
240 reg = PHY_READ(sc, MII_BMCR);
241 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
246 * If the interface is not up, don't do anything.
248 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
251 switch (IFM_SUBTYPE(ife->ifm_media)) {
254 * For autonegotiation, reset and isolate the
255 * companion PHY (if any) and then do NWAY
256 * autonegotiation ourselves.
258 msc->ml_state = ML_STATE_AUTO_SELF;
260 mii_phy_reset(other);
261 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
263 (void) mii_phy_auto(sc, 1);
269 * For 10baseT modes, reset and program the
270 * companion PHY (of any), then program ourselves
271 * to match. This will put us in pass-through
272 * mode and let the companion PHY do all the
275 * BMCR data is stored in the ifmedia entry.
278 mii_phy_reset(other);
279 PHY_WRITE(other, MII_BMCR, ife->ifm_data);
281 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
282 PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
287 * For 100baseTX modes, reset and isolate the
288 * companion PHY (if any), then program ourselves
291 * BMCR data is stored in the ifmedia entry.
294 mii_phy_reset(other);
295 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
297 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
298 PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
303 * XXX Not supported as a manual setting right now.
314 * If we're not currently selected, just return.
316 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
320 * Only used for autonegotiation.
322 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
326 * Is the interface even up?
328 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
332 * Only retry autonegotiation every 5 seconds.
334 if (++sc->mii_ticks != 5)
340 * Check to see if we have link. If we do, we don't
341 * need to restart the autonegotiation process. Read
342 * the BMSR twice in case it's latched.
343 * If we're in a 10Mbps mode, check the link of the
344 * 10Mbps PHY. Sometimes the Micro Linear PHY's
345 * linkstat bit will clear while the linkstat bit of
346 * the companion PHY will remain set.
348 if (msc->ml_state == ML_STATE_AUTO_OTHER) {
349 reg = PHY_READ(other, MII_BMSR) |
350 PHY_READ(other, MII_BMSR);
352 reg = PHY_READ(sc, MII_BMSR) |
353 PHY_READ(sc, MII_BMSR);
356 if (reg & BMSR_LINK) {
357 if (!msc->ml_linked) {
366 mii->mii_media_active = IFM_NONE;
368 msc->ml_state = ML_STATE_AUTO_SELF;
370 mii_phy_reset(other);
371 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
373 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
378 /* Update the media status. */
380 if (msc->ml_state == ML_STATE_AUTO_OTHER) {
382 other_inst = other->mii_inst;
383 other->mii_inst = sc->mii_inst;
384 (void) (*other->mii_service)(other, mii, MII_POLLSTAT);
385 other->mii_inst = other_inst;
386 sc->mii_active = other->mii_active;
390 /* Callback if something changed. */
391 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
392 MIIBUS_STATCHG(sc->mii_dev);
393 sc->mii_active = mii->mii_media_active;
400 * The Micro Linear PHY comes out of reset with the 'autoneg
401 * enable' bit set, which we don't want.
403 static void mlphy_reset(sc)
404 struct mii_softc *sc;
409 reg = PHY_READ(sc, MII_BMCR);
411 PHY_WRITE(sc, MII_BMCR, reg);
417 * If we negotiate a 10Mbps mode, we need to check for an alternate
418 * PHY and make sure it's enabled and set correctly.
420 static void mlphy_status(sc)
421 struct mii_softc *sc;
423 struct mlphy_softc *msc = (struct mlphy_softc *)sc;
424 struct mii_data *mii = msc->ml_mii.mii_pdata;
425 struct mii_softc *other = NULL;
429 /* See if there's another PHY on the bus with us. */
430 device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
431 for (i = 0; i < devs; i++) {
432 if (strcmp(device_get_name(devlist[i]), "mlphy")) {
433 other = device_get_softc(devlist[i]);
437 free(devlist, M_TEMP);
444 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
445 msc->ml_state = ML_STATE_AUTO_SELF;
446 mii_phy_reset(other);
447 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
450 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
451 msc->ml_state = ML_STATE_AUTO_OTHER;
452 mlphy_reset(&msc->ml_mii);
453 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO);
454 mii_phy_reset(other);
455 mii_phy_auto(other, 1);