2 * Copyright (c) 2001 Wind River Systems
4 * Bill Paul <wpaul@bsdi.com>. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: src/sys/dev/mii/nsgphy.c,v 1.1.2.3 2002/11/08 21:53:49 semenu Exp $
34 * $DragonFly: src/sys/dev/netif/mii_layer/nsgphy.c,v 1.14 2006/12/22 23:26:20 swildner Exp $
38 * Driver for the National Semiconductor DP83891 and DP83861
40 * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf
42 * The DP83891 is the older NatSemi gigE PHY which isn't being sold
43 * anymore. The DP83861 is its replacement, which is an 'enhanced'
44 * firmware driven component. The major difference between the
45 * two is that the 83891 can't generate interrupts, while the
46 * 83861 can. (I think it wasn't originally designed to do this, but
47 * it can now thanks to firmware updates.) The 83861 also allows
48 * access to its internal RAM via indirect register access.
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
57 #include <machine/clock.h>
60 #include <net/if_media.h>
66 #include "nsgphyreg.h"
68 #include "miibus_if.h"
70 static int nsgphy_probe (device_t);
71 static int nsgphy_attach (device_t);
73 static device_method_t nsgphy_methods[] = {
74 /* device interface */
75 DEVMETHOD(device_probe, nsgphy_probe),
76 DEVMETHOD(device_attach, nsgphy_attach),
77 DEVMETHOD(device_shutdown, bus_generic_shutdown),
81 static const struct mii_phydesc nsgphys[] = {
82 MII_PHYDESC(NATSEMI, DP83891),
83 MII_PHYDESC(NATSEMI, DP83861),
87 static devclass_t nsgphy_devclass;
89 static driver_t nsgphy_driver = {
92 sizeof(struct mii_softc)
95 DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0);
97 static int nsgphy_service(struct mii_softc *, struct mii_data *, int);
98 static void nsgphy_status(struct mii_softc *);
101 nsgphy_probe(device_t dev)
103 struct mii_attach_args *ma = device_get_ivars(dev);
104 const struct mii_phydesc *mpd;
106 mpd = mii_phy_match(ma, nsgphys);
108 device_set_desc(dev, mpd->mpd_name);
115 nsgphy_attach(device_t dev)
117 struct mii_softc *sc;
118 struct mii_attach_args *ma;
119 struct mii_data *mii;
121 sc = device_get_softc(dev);
122 ma = device_get_ivars(dev);
123 mii_softc_init(sc, ma);
124 sc->mii_dev = device_get_parent(dev);
125 mii = device_get_softc(sc->mii_dev);
126 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
128 sc->mii_inst = mii->mii_instance;
129 sc->mii_service = nsgphy_service;
131 * Only retry autonegotiation every 17 seconds.
132 * Actually, for gigE PHYs, we should wait longer, since
133 * 5 seconds is the mimimum time the documentation
134 * says to wait for a 1000mbps link to be established.
136 sc->mii_anegticks = 17;
139 sc->mii_flags |= MIIF_NOISOLATE;
142 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
144 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
147 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
153 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
154 if (sc->mii_capabilities & BMSR_EXTSTAT)
155 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
157 device_printf(dev, " ");
158 if ((sc->mii_capabilities & BMSR_MEDIAMASK) ||
159 (sc->mii_extcapabilities & EXTSR_MEDIAMASK))
160 mii_phy_add_media(sc);
162 kprintf("no media present");
167 MIIBUS_MEDIAINIT(sc->mii_dev);
172 nsgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
174 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
180 * If we're not polling our PHY instance, just return.
182 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
188 * If the media indicates a different PHY instance,
191 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
192 reg = PHY_READ(sc, MII_BMCR);
193 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
198 * If the interface is not up, don't do anything.
200 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
204 mii_phy_set_media(sc);
209 * If we're not currently selected, just return.
211 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
214 if (mii_phy_tick(sc) == EJUSTRETURN)
219 /* Update the media status. */
222 /* Callback if something changed. */
223 mii_phy_update(sc, cmd);
228 nsgphy_status(struct mii_softc *sc)
230 struct mii_data *mii = sc->mii_pdata;
231 int bmsr, bmcr, physup, anlpar, gstat;
233 mii->mii_media_status = IFM_AVALID;
234 mii->mii_media_active = IFM_ETHER;
236 bmsr = PHY_READ(sc, NSGPHY_MII_BMSR);
237 physup = PHY_READ(sc, NSGPHY_MII_PHYSUP);
238 if (physup & NSGPHY_PHYSUP_LNKSTS)
239 mii->mii_media_status |= IFM_ACTIVE;
241 bmcr = PHY_READ(sc, NSGPHY_MII_BMCR);
243 if (bmcr & NSGPHY_BMCR_LOOP)
244 mii->mii_media_active |= IFM_LOOP;
246 if (bmcr & NSGPHY_BMCR_AUTOEN) {
247 if ((bmsr & NSGPHY_BMSR_ACOMP) == 0) {
248 /* Erg, still trying, I guess... */
249 mii->mii_media_active |= IFM_NONE;
252 anlpar = PHY_READ(sc, NSGPHY_MII_ANLPAR);
253 gstat = PHY_READ(sc, NSGPHY_MII_1000STS);
254 if (gstat & NSGPHY_1000STS_LPFD)
255 mii->mii_media_active |= IFM_1000_T | IFM_FDX;
256 else if (gstat & NSGPHY_1000STS_LPHD)
257 mii->mii_media_active |= IFM_1000_T | IFM_HDX;
258 else if (anlpar & NSGPHY_ANLPAR_100T4)
259 mii->mii_media_active |= IFM_100_T4;
260 else if (anlpar & NSGPHY_ANLPAR_100FDX)
261 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
262 else if (anlpar & NSGPHY_ANLPAR_100HDX)
263 mii->mii_media_active |= IFM_100_TX;
264 else if (anlpar & NSGPHY_ANLPAR_10FDX)
265 mii->mii_media_active |= IFM_10_T|IFM_FDX;
266 else if (anlpar & NSGPHY_ANLPAR_10HDX)
267 mii->mii_media_active |= IFM_10_T|IFM_HDX;
269 mii->mii_media_active |= IFM_NONE;
273 switch(bmcr & (NSGPHY_BMCR_SPD1|NSGPHY_BMCR_SPD0)) {
275 mii->mii_media_active |= IFM_1000_T;
278 mii->mii_media_active |= IFM_100_TX;
281 mii->mii_media_active |= IFM_10_T;
287 if (bmcr & NSGPHY_BMCR_FDX)
288 mii->mii_media_active |= IFM_FDX;
290 mii->mii_media_active |= IFM_HDX;