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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Driver for the TDK 78Q2120 MII
36 * Datasheet for the 78Q2120 - http://www.tsc.tdk.com/lan/78q2120.pdf
37 * Most of this code stolen from ukphy.c
41 * The TDK 78Q2120 is found on some Xircom X3201 based cardbus cards,
42 * also spotted on some 3C575 cards. It's just like any other normal
43 * phy, except it does auto negotiation in a different way.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/module.h>
55 #include <net/if_media.h>
57 #include <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
61 #include <dev/mii/tdkphyreg.h>
63 #include "miibus_if.h"
65 static int tdkphy_probe(device_t);
66 static int tdkphy_attach(device_t);
68 static device_method_t tdkphy_methods[] = {
69 /* device interface */
70 DEVMETHOD(device_probe, tdkphy_probe),
71 DEVMETHOD(device_attach, tdkphy_attach),
72 DEVMETHOD(device_detach, mii_phy_detach),
73 DEVMETHOD(device_shutdown, bus_generic_shutdown),
77 static devclass_t tdkphy_devclass;
79 static driver_t tdkphy_driver = {
82 sizeof(struct mii_softc)
85 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0);
87 static int tdkphy_service(struct mii_softc *, struct mii_data *, int);
88 static void tdkphy_status(struct mii_softc *);
90 static const struct mii_phydesc tdkphys[] = {
91 MII_PHY_DESC(TDK, 78Q2120),
96 tdkphy_probe(device_t dev)
99 return (mii_phy_dev_probe(dev, tdkphys, BUS_PROBE_DEFAULT));
103 tdkphy_attach(device_t dev)
105 struct mii_softc *sc;
106 struct mii_attach_args *ma;
107 struct mii_data *mii;
108 sc = device_get_softc(dev);
109 ma = device_get_ivars(dev);
110 sc->mii_dev = device_get_parent(dev);
112 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
115 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n",
116 MII_OUI(ma->mii_id1, ma->mii_id2),
117 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
119 sc->mii_flags = miibus_get_flags(dev);
120 sc->mii_inst = mii->mii_instance++;
121 sc->mii_phy = ma->mii_phyno;
122 sc->mii_service = tdkphy_service;
126 * Apparently, we can't do loopback on this PHY.
128 sc->mii_flags |= MIIF_NOLOOP;
132 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
133 device_printf(dev, " ");
134 mii_phy_add_media(sc);
137 MIIBUS_MEDIAINIT(sc->mii_dev);
142 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
151 * If the interface is not up, don't do anything.
153 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
156 mii_phy_setmedia(sc);
160 if (mii_phy_tick(sc) == EJUSTRETURN)
165 /* Update the media status. */
167 if (sc->mii_pdata->mii_media_active & IFM_FDX)
168 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX);
170 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX);
172 /* Callback if something changed. */
173 mii_phy_update(sc, cmd);
178 tdkphy_status(struct mii_softc *phy)
180 struct mii_data *mii = phy->mii_pdata;
181 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
182 int bmsr, bmcr, anlpar, diag;
184 mii->mii_media_status = IFM_AVALID;
185 mii->mii_media_active = IFM_ETHER;
187 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
188 if (bmsr & BMSR_LINK)
189 mii->mii_media_status |= IFM_ACTIVE;
191 bmcr = PHY_READ(phy, MII_BMCR);
192 if (bmcr & BMCR_ISO) {
193 mii->mii_media_active |= IFM_NONE;
194 mii->mii_media_status = 0;
198 if (bmcr & BMCR_LOOP)
199 mii->mii_media_active |= IFM_LOOP;
201 if (bmcr & BMCR_AUTOEN) {
203 * NWay autonegotiation takes the highest-order common
204 * bit of the ANAR and ANLPAR (i.e. best media advertised
205 * both by us and our link partner).
207 if ((bmsr & BMSR_ACOMP) == 0) {
208 /* Erg, still trying, I guess... */
209 mii->mii_media_active |= IFM_NONE;
213 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
215 * ANLPAR doesn't get set on my card, but we check it anyway,
216 * since it is mentioned in the 78Q2120 specs.
218 if (anlpar & ANLPAR_TX_FD)
219 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
220 else if (anlpar & ANLPAR_T4)
221 mii->mii_media_active |= IFM_100_T4|IFM_HDX;
222 else if (anlpar & ANLPAR_TX)
223 mii->mii_media_active |= IFM_100_TX|IFM_HDX;
224 else if (anlpar & ANLPAR_10_FD)
225 mii->mii_media_active |= IFM_10_T|IFM_FDX;
226 else if (anlpar & ANLPAR_10)
227 mii->mii_media_active |= IFM_10_T|IFM_HDX;
230 * ANLPAR isn't set, which leaves two possibilities:
231 * 1) Auto negotiation failed
232 * 2) Auto negotiation completed, but the card forgot
234 * So we check the MII_DIAG(18) register...
236 diag = PHY_READ(phy, MII_DIAG);
237 if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */
238 mii->mii_media_active |= IFM_10_T|IFM_HDX;
240 if (diag & DIAG_DUPLEX)
241 mii->mii_media_active |= IFM_FDX;
243 mii->mii_media_active |= IFM_HDX;
244 if (diag & DIAG_RATE_100)
245 mii->mii_media_active |= IFM_100_TX;
247 mii->mii_media_active |= IFM_10_T;
251 mii->mii_media_active = ife->ifm_media;