2 * Copyright (c) 2000,2001 Jonathan Chen.
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 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Driver for the TDK 78Q2120 MII
35 * Datasheet for the 78Q2120 - http://www.tsc.tdk.com/lan/78q2120.pdf
36 * Most of this code stolen from ukphy.c
40 * The TDK 78Q2120 is found on some Xircom X3201 based cardbus cards. It's just
41 * like any other normal phy, except it does auto negotiation in a different
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/errno.h>
50 #include <sys/module.h>
54 #include <net/if_media.h>
56 #include <machine/clock.h>
58 #include <dev/mii/mii.h>
59 #include <dev/mii/miivar.h>
60 #include <dev/mii/miidevs.h>
62 #include <dev/mii/tdkphyreg.h>
64 #include "miibus_if.h"
67 static const char rcsid[] =
68 "$Id: tdkphy.c,v 1.3 2000/10/14 06:20:56 jon Exp $";
71 static int tdkphy_probe(device_t);
72 static int tdkphy_attach(device_t);
74 static device_method_t tdkphy_methods[] = {
75 /* device interface */
76 DEVMETHOD(device_probe, tdkphy_probe),
77 DEVMETHOD(device_attach, tdkphy_attach),
78 DEVMETHOD(device_detach, mii_phy_detach),
79 DEVMETHOD(device_shutdown, bus_generic_shutdown),
83 static devclass_t tdkphy_devclass;
85 static driver_t tdkphy_driver = {
88 sizeof(struct mii_softc)
91 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0);
93 static int tdkphy_service(struct mii_softc *, struct mii_data *, int);
94 static void tdkphy_status(struct mii_softc *);
97 tdkphy_probe(device_t dev)
99 struct mii_attach_args *ma;
100 ma = device_get_ivars(dev);
101 if ((MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_TDK ||
102 MII_MODEL(ma->mii_id2) != MII_MODEL_TDK_78Q2120))
105 device_set_desc(dev, MII_STR_TDK_78Q2120);
110 tdkphy_attach(device_t dev)
112 struct mii_softc *sc;
113 struct mii_attach_args *ma;
114 struct mii_data *mii;
115 sc = device_get_softc(dev);
116 ma = device_get_ivars(dev);
117 sc->mii_dev = device_get_parent(dev);
118 mii = device_get_softc(sc->mii_dev);
119 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
122 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n",
123 MII_OUI(ma->mii_id1, ma->mii_id2),
124 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
126 sc->mii_inst = mii->mii_instance;
127 sc->mii_phy = ma->mii_phyno;
128 sc->mii_service = tdkphy_service;
133 sc->mii_flags |= MIIF_NOISOLATE;
135 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
137 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
140 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
141 BMCR_LOOP|BMCR_S100);
146 sc->mii_capabilities =
147 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
148 device_printf(dev, " ");
153 MIIBUS_MEDIAINIT(sc->mii_dev);
159 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
161 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
167 * If we're not polling our PHY instance, just return.
169 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
175 * If the media indicates a different PHY instance,
178 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
179 reg = PHY_READ(sc, MII_BMCR);
180 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
185 * If the interface is not up, don't do anything.
187 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
190 switch (IFM_SUBTYPE(ife->ifm_media)) {
193 * If we're already in auto mode, just return.
195 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
197 (void) mii_phy_auto(sc);
201 * Not supported on MII
206 * BMCR data is stored in the ifmedia entry.
208 PHY_WRITE(sc, MII_ANAR,
209 mii_anar(ife->ifm_media));
210 PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
216 * If we're not currently selected, just return.
218 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
220 if (mii_phy_tick(sc) == EJUSTRETURN)
225 /* Update the media status. */
227 if (sc->mii_pdata->mii_media_active & IFM_FDX)
228 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX);
230 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX);
232 /* Callback if something changed. */
233 mii_phy_update(sc, cmd);
238 tdkphy_status(struct mii_softc *phy)
240 struct mii_data *mii = phy->mii_pdata;
241 int bmsr, bmcr, anlpar, diag;
243 mii->mii_media_status = IFM_AVALID;
244 mii->mii_media_active = IFM_ETHER;
246 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
247 if (bmsr & BMSR_LINK)
248 mii->mii_media_status |= IFM_ACTIVE;
250 bmcr = PHY_READ(phy, MII_BMCR);
251 if (bmcr & BMCR_ISO) {
252 mii->mii_media_active |= IFM_NONE;
253 mii->mii_media_status = 0;
257 if (bmcr & BMCR_LOOP)
258 mii->mii_media_active |= IFM_LOOP;
260 if (bmcr & BMCR_AUTOEN) {
262 * NWay autonegotiation takes the highest-order common
263 * bit of the ANAR and ANLPAR (i.e. best media advertised
264 * both by us and our link partner).
266 if ((bmsr & BMSR_ACOMP) == 0) {
267 /* Erg, still trying, I guess... */
268 mii->mii_media_active |= IFM_NONE;
272 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
274 * ANLPAR doesn't get set on my card, but we check it anyway,
275 * since it is mentioned in the 78Q2120 specs.
277 if (anlpar & ANLPAR_T4)
278 mii->mii_media_active |= IFM_100_T4;
279 else if (anlpar & ANLPAR_TX_FD)
280 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
281 else if (anlpar & ANLPAR_TX)
282 mii->mii_media_active |= IFM_100_TX;
283 else if (anlpar & ANLPAR_10_FD)
284 mii->mii_media_active |= IFM_10_T|IFM_FDX;
285 else if (anlpar & ANLPAR_10)
286 mii->mii_media_active |= IFM_10_T;
289 * ANLPAR isn't set, which leaves two possibilities:
290 * 1) Auto negotiation failed
291 * 2) Auto negotiation completed, but the card forgot
293 * So we check the MII_DIAG(18) register...
295 diag = PHY_READ(phy, MII_DIAG);
296 if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */
297 mii->mii_media_active |= IFM_10_T;
299 if (diag & DIAG_DUPLEX)
300 mii->mii_media_active |= IFM_FDX;
301 if (diag & DIAG_RATE_100)
302 mii->mii_media_active |= IFM_100_TX;
304 mii->mii_media_active |= IFM_10_T;
308 mii->mii_media_active = mii_media_from_bmcr(bmcr);