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 $
33 * $DragonFly: src/sys/dev/netif/mii_layer/mlphy.c,v 1.11 2006/12/22 23:26:20 swildner Exp $
37 * driver for Micro Linear 6692 PHYs
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
48 * This device also does not have vendor/device ID registers.
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>
59 #include <machine/clock.h>
62 #include <net/if_media.h>
67 #include "miibus_if.h"
69 #define ML_STATE_AUTO_SELF 1
70 #define ML_STATE_AUTO_OTHER 2
73 struct mii_softc ml_mii;
78 static int mlphy_probe (device_t);
79 static int mlphy_attach (device_t);
80 static int mlphy_detach (device_t);
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),
91 static devclass_t mlphy_devclass;
93 static driver_t mlphy_driver = {
96 sizeof(struct mlphy_softc)
99 DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0);
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 *);
106 mlphy_probe(device_t dev)
108 struct mii_attach_args *ma;
111 ma = device_get_ivars(dev);
112 parent = device_get_parent(device_get_parent(dev));
115 * Micro Linear PHY reports oui == 0 model == 0
117 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
118 MII_MODEL(ma->mii_id2) != 0)
122 * Make sure the parent is a `tl'. So far, I have only
123 * encountered the 6692 on an Olicom card with a ThunderLAN
126 if (strcmp(device_get_name(parent), "tl") != 0)
129 device_set_desc(dev, "Micro Linear 6692 media interface");
135 mlphy_attach(device_t dev)
137 struct mlphy_softc *msc;
138 struct mii_softc *sc;
139 struct mii_attach_args *ma;
140 struct mii_data *mii;
142 msc = device_get_softc(dev);
144 ma = device_get_ivars(dev);
145 mii_softc_init(sc, ma);
146 sc->mii_dev = device_get_parent(dev);
147 mii = device_get_softc(sc->mii_dev);
148 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
150 sc->mii_inst = mii->mii_instance;
151 sc->mii_service = mlphy_service;
152 sc->mii_reset = mlphy_reset;
157 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
159 #if 0 /* See above. */
160 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
163 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
166 sc->mii_flags &= ~MIIF_NOISOLATE;
168 sc->mii_flags |= MIIF_NOISOLATE;
170 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
172 device_printf(dev, " ");
173 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
174 kprintf("no media present");
176 mii_phy_add_media(sc);
179 MIIBUS_MEDIAINIT(sc->mii_dev);
184 mlphy_detach(device_t dev)
186 struct mlphy_softc *sc;
188 sc = device_get_softc(dev);
189 mii_phy_auto_stop(&sc->ml_mii);
190 sc->ml_mii.mii_dev = NULL;
191 LIST_REMOVE(&sc->ml_mii, mii_list);
197 mlphy_service(struct mii_softc *xsc, struct mii_data *mii, int cmd)
199 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
201 struct mii_softc *other = NULL;
202 struct mlphy_softc *msc = (struct mlphy_softc *)xsc;
203 struct mii_softc *sc = (struct mii_softc *)&msc->ml_mii;
206 const struct mii_media *mm;
209 * See if there's another PHY on this bus with us.
210 * If so, we may need it for 10Mbps modes.
212 device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
213 for (i = 0; i < devs; i++) {
214 if (strcmp(device_get_name(devlist[i]), "mlphy")) {
215 other = device_get_softc(devlist[i]);
219 kfree(devlist, M_TEMP);
221 KKASSERT(ife->ifm_data >= 0 && ife->ifm_data < MII_NMEDIA);
222 mm = &mii_media_table[ife->ifm_data];
227 * If we're not polling our PHY instance, just return.
229 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
235 * If the media indicates a different PHY instance,
238 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
239 reg = PHY_READ(sc, MII_BMCR);
240 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
245 * If the interface is not up, don't do anything.
247 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
250 switch (IFM_SUBTYPE(ife->ifm_media)) {
253 * For autonegotiation, reset and isolate the
254 * companion PHY (if any) and then do NWAY
255 * autonegotiation ourselves.
257 msc->ml_state = ML_STATE_AUTO_SELF;
259 mii_phy_reset(other);
260 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
268 * For 10baseT modes, reset and program the
269 * companion PHY (of any), then program ourselves
270 * to match. This will put us in pass-through
271 * mode and let the companion PHY do all the
275 mii_phy_reset(other);
276 PHY_WRITE(other, MII_BMCR, mm->mm_bmcr);
278 PHY_WRITE(sc, MII_ANAR, mm->mm_anar);
279 PHY_WRITE(sc, MII_BMCR, mm->mm_bmcr);
285 * For 100baseTX modes, reset and isolate the
286 * companion PHY (if any), then program ourselves
290 mii_phy_reset(other);
291 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
293 PHY_WRITE(sc, MII_ANAR, mm->mm_anar);
294 PHY_WRITE(sc, MII_BMCR, mm->mm_bmcr);
300 * XXX Not supported as a manual setting right now.
310 * If we're not currently selected, just return.
312 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
316 * Is the interface even up?
318 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
322 * Only used for autonegotiation.
324 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
328 * Only retry autonegotiation every 5 seconds.
330 if (++sc->mii_ticks <= sc->mii_anegticks)
336 * Check to see if we have link. If we do, we don't
337 * need to restart the autonegotiation process. Read
338 * the BMSR twice in case it's latched.
339 * If we're in a 10Mbps mode, check the link of the
340 * 10Mbps PHY. Sometimes the Micro Linear PHY's
341 * linkstat bit will clear while the linkstat bit of
342 * the companion PHY will remain set.
344 if (msc->ml_state == ML_STATE_AUTO_OTHER) {
345 reg = PHY_READ(other, MII_BMSR) |
346 PHY_READ(other, MII_BMSR);
348 reg = PHY_READ(sc, MII_BMSR) |
349 PHY_READ(sc, MII_BMSR);
352 if (reg & BMSR_LINK) {
353 if (!msc->ml_linked) {
362 mii->mii_media_active = IFM_NONE;
364 msc->ml_state = ML_STATE_AUTO_SELF;
366 mii_phy_reset(other);
367 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
369 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
374 /* Update the media status. */
375 if (msc->ml_state == ML_STATE_AUTO_OTHER) {
378 other_inst = other->mii_inst;
379 other->mii_inst = sc->mii_inst;
380 other->mii_service(other, mii, MII_POLLSTAT);
381 other->mii_inst = other_inst;
382 sc->mii_media_active = other->mii_media_active;
383 sc->mii_media_status = other->mii_media_status;
388 /* Callback if something changed. */
389 mii_phy_update(sc, cmd);
394 * The Micro Linear PHY comes out of reset with the 'autoneg
395 * enable' bit set, which we don't want.
398 mlphy_reset(struct mii_softc *sc)
403 reg = PHY_READ(sc, MII_BMCR);
405 PHY_WRITE(sc, MII_BMCR, reg);
409 * If we negotiate a 10Mbps mode, we need to check for an alternate
410 * PHY and make sure it's enabled and set correctly.
413 mlphy_status(struct mii_softc *sc)
415 struct mlphy_softc *msc = (struct mlphy_softc *)sc;
416 struct mii_data *mii = msc->ml_mii.mii_pdata;
417 struct mii_softc *other = NULL;
421 /* See if there's another PHY on the bus with us. */
422 device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
423 for (i = 0; i < devs; i++) {
424 if (strcmp(device_get_name(devlist[i]), "mlphy")) {
425 other = device_get_softc(devlist[i]);
429 kfree(devlist, M_TEMP);
436 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
437 msc->ml_state = ML_STATE_AUTO_SELF;
438 mii_phy_reset(other);
439 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
442 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
443 msc->ml_state = ML_STATE_AUTO_OTHER;
444 mlphy_reset(&msc->ml_mii);
445 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO);
446 mii_phy_reset(other);
447 mii_phy_auto(other, 1);