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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * driver for Micro Linear 6692 PHYs
40 * The Micro Linear 6692 is a strange beast, and dealing with it using
41 * this code framework is tricky. The 6692 is actually a 100Mbps-only
42 * device, which means that a second PHY is required to support 10Mbps
43 * modes. However, even though the 6692 does not support 10Mbps modes,
44 * it can still advertise them when performing autonegotiation. If a
45 * 10Mbps mode is negotiated, we must program the registers of the
46 * companion PHY accordingly in addition to programming the registers
49 * This device also does not have vendor/device ID registers.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/socket.h>
56 #include <sys/module.h>
58 #include <sys/malloc.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(device_t);
78 static int mlphy_attach(device_t);
80 static device_method_t mlphy_methods[] = {
81 /* device interface */
82 DEVMETHOD(device_probe, mlphy_probe),
83 DEVMETHOD(device_attach, mlphy_attach),
84 DEVMETHOD(device_detach, mii_phy_detach),
85 DEVMETHOD(device_shutdown, bus_generic_shutdown),
89 static devclass_t mlphy_devclass;
91 static driver_t mlphy_driver = {
94 sizeof(struct mlphy_softc)
97 DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0);
99 static int mlphy_service(struct mii_softc *, struct mii_data *, int);
100 static void mlphy_reset(struct mii_softc *);
101 static void mlphy_status(struct mii_softc *);
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");
130 return (BUS_PROBE_DEFAULT);
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 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);
149 sc->mii_inst = mii->mii_instance;
150 sc->mii_phy = ma->mii_phyno;
151 sc->mii_service = mlphy_service;
156 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
158 #if 0 /* See above. */
159 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
162 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
163 BMCR_LOOP|BMCR_S100);
165 sc->mii_flags &= ~MIIF_NOISOLATE;
167 sc->mii_flags |= MIIF_NOISOLATE;
169 sc->mii_capabilities =
170 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
171 ma->mii_capmask = ~sc->mii_capabilities;
172 device_printf(dev, " ");
176 MIIBUS_MEDIAINIT(sc->mii_dev);
181 mlphy_service(xsc, mii, cmd)
182 struct mii_softc *xsc;
183 struct mii_data *mii;
186 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
187 struct mii_softc *other = NULL;
188 struct mlphy_softc *msc = (struct mlphy_softc *)xsc;
189 struct mii_softc *sc = (struct mii_softc *)&msc->ml_mii;
191 int devs, i, other_inst, reg;
194 * See if there's another PHY on this bus with us.
195 * If so, we may need it for 10Mbps modes.
197 device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
198 for (i = 0; i < devs; i++) {
199 if (strcmp(device_get_name(devlist[i]), "mlphy")) {
200 other = device_get_softc(devlist[i]);
204 free(devlist, M_TEMP);
209 * If we're not polling our PHY instance, just return.
211 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
217 * If the media indicates a different PHY instance,
220 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
221 reg = PHY_READ(sc, MII_BMCR);
222 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
227 * If the interface is not up, don't do anything.
229 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
232 switch (IFM_SUBTYPE(ife->ifm_media)) {
235 * For autonegotiation, reset and isolate the
236 * companion PHY (if any) and then do NWAY
237 * autonegotiation ourselves.
239 msc->ml_state = ML_STATE_AUTO_SELF;
241 mii_phy_reset(other);
242 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
244 (void) mii_phy_auto(sc);
249 * For 10baseT modes, reset and program the
250 * companion PHY (of any), then program ourselves
251 * to match. This will put us in pass-through
252 * mode and let the companion PHY do all the
255 * BMCR data is stored in the ifmedia entry.
258 mii_phy_reset(other);
259 PHY_WRITE(other, MII_BMCR, ife->ifm_data);
261 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
262 PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
267 * For 100baseTX modes, reset and isolate the
268 * companion PHY (if any), then program ourselves
271 * BMCR data is stored in the ifmedia entry.
274 mii_phy_reset(other);
275 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
277 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
278 PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
283 * XXX Not supported as a manual setting right now.
294 * If we're not currently selected, just return.
296 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
300 * Is the interface even up?
302 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
306 * Only used for autonegotiation.
308 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
312 * Check to see if we have link. If we do, we don't
313 * need to restart the autonegotiation process. Read
314 * the BMSR twice in case it's latched.
315 * If we're in a 10Mbps mode, check the link of the
316 * 10Mbps PHY. Sometimes the Micro Linear PHY's
317 * linkstat bit will clear while the linkstat bit of
318 * the companion PHY will remain set.
320 if (msc->ml_state == ML_STATE_AUTO_OTHER) {
321 reg = PHY_READ(other, MII_BMSR) |
322 PHY_READ(other, MII_BMSR);
324 reg = PHY_READ(sc, MII_BMSR) |
325 PHY_READ(sc, MII_BMSR);
328 if (reg & BMSR_LINK) {
329 if (!msc->ml_linked) {
337 * Only retry autonegotiation every 5 seconds.
339 if (++sc->mii_ticks <= MII_ANEGTICKS)
344 mii->mii_media_active = IFM_NONE;
346 msc->ml_state = ML_STATE_AUTO_SELF;
348 mii_phy_reset(other);
349 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
355 /* Update the media status. */
357 if (msc->ml_state == ML_STATE_AUTO_OTHER) {
358 other_inst = other->mii_inst;
359 other->mii_inst = sc->mii_inst;
360 (void) (*other->mii_service)(other, mii, MII_POLLSTAT);
361 other->mii_inst = other_inst;
362 sc->mii_media_active = other->mii_media_active;
363 sc->mii_media_status = other->mii_media_status;
367 /* Callback if something changed. */
368 mii_phy_update(sc, cmd);
373 * The Micro Linear PHY comes out of reset with the 'autoneg
374 * enable' bit set, which we don't want.
378 struct mii_softc *sc;
383 reg = PHY_READ(sc, MII_BMCR);
385 PHY_WRITE(sc, MII_BMCR, reg);
389 * If we negotiate a 10Mbps mode, we need to check for an alternate
390 * PHY and make sure it's enabled and set correctly.
394 struct mii_softc *sc;
396 struct mlphy_softc *msc = (struct mlphy_softc *)sc;
397 struct mii_data *mii = msc->ml_mii.mii_pdata;
398 struct mii_softc *other = NULL;
402 /* See if there's another PHY on the bus with us. */
403 device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
404 for (i = 0; i < devs; i++) {
405 if (strcmp(device_get_name(devlist[i]), "mlphy")) {
406 other = device_get_softc(devlist[i]);
410 free(devlist, M_TEMP);
417 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
418 msc->ml_state = ML_STATE_AUTO_SELF;
419 mii_phy_reset(other);
420 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
423 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
424 msc->ml_state = ML_STATE_AUTO_OTHER;
425 mlphy_reset(&msc->ml_mii);
426 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO);
427 mii_phy_reset(other);