2 * Copyright (c) 2001 Wind River Systems
4 * Bill Paul <wpaul@bsdi.com>. All rights reserved.
5 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Bill Paul.
23 * 4. Neither the name of the author nor the names of any co-contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37 * THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * Driver for the National Semiconductor DP83891 and DP83861
46 * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf
48 * The DP83891 is the older NatSemi gigE PHY which isn't being sold
49 * anymore. The DP83861 is its replacement, which is an 'enhanced'
50 * firmware driven component. The major difference between the
51 * two is that the 83891 can't generate interrupts, while the
52 * 83861 can. (I think it wasn't originally designed to do this, but
53 * it can now thanks to firmware updates.) The 83861 also allows
54 * access to its internal RAM via indirect register access.
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/socket.h>
66 #include <machine/clock.h>
69 #include <net/if_media.h>
71 #include <dev/mii/mii.h>
72 #include <dev/mii/miivar.h>
75 #include <dev/mii/nsgphyreg.h>
77 #include "miibus_if.h"
79 static int nsgphy_probe(device_t);
80 static int nsgphy_attach(device_t);
82 static device_method_t nsgphy_methods[] = {
83 /* device interface */
84 DEVMETHOD(device_probe, nsgphy_probe),
85 DEVMETHOD(device_attach, nsgphy_attach),
86 DEVMETHOD(device_detach, mii_phy_detach),
87 DEVMETHOD(device_shutdown, bus_generic_shutdown),
91 static devclass_t nsgphy_devclass;
93 static driver_t nsgphy_driver = {
96 sizeof(struct mii_softc)
100 DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0);
102 static int nsgphy_service(struct mii_softc *, struct mii_data *,int);
103 static void nsgphy_status(struct mii_softc *);
105 const struct mii_phydesc gphyters[] = {
106 { MII_OUI_NATSEMI, MII_MODEL_NATSEMI_DP83861,
107 MII_STR_NATSEMI_DP83861 },
109 { MII_OUI_NATSEMI, MII_MODEL_NATSEMI_DP83891,
110 MII_STR_NATSEMI_DP83891 },
117 nsgphy_probe(device_t dev)
119 struct mii_attach_args *ma;
120 const struct mii_phydesc *mpd;
122 ma = device_get_ivars(dev);
123 mpd = mii_phy_match(ma, gphyters);
125 device_set_desc(dev, mpd->mpd_name);
133 nsgphy_attach(device_t dev)
135 struct mii_softc *sc;
136 struct mii_attach_args *ma;
137 struct mii_data *mii;
139 sc = device_get_softc(dev);
140 ma = device_get_ivars(dev);
142 device_printf(dev, "<rev. %d>\n", MII_REV(ma->mii_id2));
143 device_printf(dev, " ");
144 sc->mii_dev = device_get_parent(dev);
145 mii = device_get_softc(sc->mii_dev);
146 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
148 sc->mii_inst = mii->mii_instance;
149 sc->mii_phy = ma->mii_phyno;
150 sc->mii_service = nsgphy_service;
152 sc->mii_anegticks = 5;
156 sc->mii_capabilities = (PHY_READ(sc, MII_BMSR) |
157 (BMSR_10TFDX|BMSR_10THDX)) & ma->mii_capmask;
158 if (sc->mii_capabilities & BMSR_EXTSTAT)
159 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
161 mii_phy_add_media(sc);
164 MIIBUS_MEDIAINIT(sc->mii_dev);
169 nsgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
171 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
177 * If we're not polling our PHY instance, just return.
179 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
185 * If the media indicates a different PHY instance,
188 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
189 reg = PHY_READ(sc, MII_BMCR);
190 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
195 * If the interface is not up, don't do anything.
197 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
200 mii_phy_setmedia(sc);
205 * If we're not currently selected, just return.
207 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
210 if (mii_phy_tick(sc) == EJUSTRETURN)
215 /* Update the media status. */
218 /* Callback if something changed. */
219 mii_phy_update(sc, cmd);
224 nsgphy_status(struct mii_softc *sc)
226 struct mii_data *mii = sc->mii_pdata;
227 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
228 int bmsr, bmcr, physup, gtsr;
230 mii->mii_media_status = IFM_AVALID;
231 mii->mii_media_active = IFM_ETHER;
233 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
235 physup = PHY_READ(sc, NSGPHY_MII_PHYSUP);
237 if (physup & PHY_SUP_LINK)
238 mii->mii_media_status |= IFM_ACTIVE;
240 bmcr = PHY_READ(sc, MII_BMCR);
241 if (bmcr & BMCR_ISO) {
242 mii->mii_media_active |= IFM_NONE;
243 mii->mii_media_status = 0;
247 if (bmcr & BMCR_LOOP)
248 mii->mii_media_active |= IFM_LOOP;
250 if (bmcr & BMCR_AUTOEN) {
252 * The media status bits are only valid if autonegotiation
253 * has completed (or it's disabled).
255 if ((bmsr & BMSR_ACOMP) == 0) {
256 /* Erg, still trying, I guess... */
257 mii->mii_media_active |= IFM_NONE;
261 switch (physup & (PHY_SUP_SPEED1|PHY_SUP_SPEED0)) {
263 mii->mii_media_active |= IFM_1000_T;
264 gtsr = PHY_READ(sc, MII_100T2SR);
265 if (gtsr & GTSR_MS_RES)
266 mii->mii_media_active |= IFM_ETH_MASTER;
270 mii->mii_media_active |= IFM_100_TX;
274 mii->mii_media_active |= IFM_10_T;
278 mii->mii_media_active |= IFM_NONE;
279 mii->mii_media_status = 0;
281 if (physup & PHY_SUP_DUPLEX)
282 mii->mii_media_active |= IFM_FDX;
284 mii->mii_media_active = ife->ifm_media;