1 /* $NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 thorpej Exp $ */
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
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 THE
37 * POSSIBILITY OF SUCH DAMAGE.
39 * $FreeBSD: src/sys/dev/mii/nsphy.c,v 1.2.2.5 2001/06/08 19:58:33 semenu Exp $
43 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Manuel Bouyer.
56 * 4. The name of the author may not be used to endorse or promote products
57 * derived from this software without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72 * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
73 * Data Sheet available from www.national.com
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/socket.h>
80 #include <sys/errno.h>
81 #include <sys/module.h>
85 #include <net/if_media.h>
93 #include "miibus_if.h"
95 static int nsphy_probe (device_t);
96 static int nsphy_attach (device_t);
98 static device_method_t nsphy_methods[] = {
99 /* device interface */
100 DEVMETHOD(device_probe, nsphy_probe),
101 DEVMETHOD(device_attach, nsphy_attach),
102 DEVMETHOD(device_detach, ukphy_detach),
103 DEVMETHOD(device_shutdown, bus_generic_shutdown),
107 static const struct mii_phydesc nsphys[] = {
108 MII_PHYDESC(NATSEMI, DP83840),
112 static devclass_t nsphy_devclass;
114 static driver_t nsphy_driver = {
117 sizeof(struct mii_softc)
120 DRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, NULL, NULL);
122 static int nsphy_service(struct mii_softc *, struct mii_data *, int);
123 static void nsphy_status(struct mii_softc *);
124 static void nsphy_reset(struct mii_softc *);
127 nsphy_probe(device_t dev)
129 struct mii_attach_args *ma = device_get_ivars(dev);
130 const struct mii_phydesc *mpd;
132 mpd = mii_phy_match(ma, nsphys);
134 device_set_desc(dev, mpd->mpd_name);
141 nsphy_attach(device_t dev)
143 struct mii_softc *sc;
144 struct mii_attach_args *ma;
145 struct mii_data *mii;
147 sc = device_get_softc(dev);
148 ma = device_get_ivars(dev);
149 mii_softc_init(sc, ma);
150 sc->mii_dev = device_get_parent(dev);
151 mii = device_get_softc(sc->mii_dev);
152 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
154 sc->mii_inst = mii->mii_instance;
155 sc->mii_service = nsphy_service;
156 sc->mii_reset = nsphy_reset;
161 * i82557 wedges if all of its PHYs are isolated!
163 if (strcmp(device_get_name(device_get_parent(sc->mii_dev)),
164 "fxp") == 0 && mii->mii_instance == 0)
167 sc->mii_flags |= MIIF_NOISOLATE;
170 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
173 /* Can't do this on the i82557! */
174 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
177 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
182 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
183 device_printf(dev, " ");
184 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
185 kprintf("no media present");
187 mii_phy_add_media(sc);
191 MIIBUS_MEDIAINIT(sc->mii_dev);
196 nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
198 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
204 * If we're not polling our PHY instance, just return.
206 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
212 * If the media indicates a different PHY instance,
215 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
216 reg = PHY_READ(sc, MII_BMCR);
217 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
222 * If the interface is not up, don't do anything.
224 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
227 reg = PHY_READ(sc, MII_NSPHY_PCR);
230 * Set up the PCR to use LED4 to indicate full-duplex
231 * in both 10baseT and 100baseTX modes.
236 * Make sure Carrier Intgrity Monitor function is
237 * disabled (normal for Node operation, but sometimes
243 * Make sure "force link good" is set to normal mode.
244 * It's only intended for debugging.
249 * Mystery bits which are supposedly `reserved',
250 * but we seem to need to set them when the PHY
251 * is connected to some interfaces:
253 * 0x0400 is needed for fxp
254 * (Intel EtherExpress Pro 10+/100B, 82557 chip)
255 * (nsphy with a DP83840 chip)
256 * 0x0100 may be needed for some other card
258 reg |= PCR_CONGCTRL | PCR_TXREADYSEL;
260 if (strcmp(device_get_name(device_get_parent(sc->mii_dev)),
262 PHY_WRITE(sc, MII_NSPHY_PCR, reg);
264 mii_phy_set_media(sc);
269 * If we're not currently selected, just return.
271 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
274 if (mii_phy_tick(sc) == EJUSTRETURN)
279 /* Update the media status. */
282 /* Callback if something changed. */
283 mii_phy_update(sc, cmd);
288 nsphy_status(struct mii_softc *sc)
290 struct mii_data *mii = sc->mii_pdata;
291 int bmsr, bmcr, par, anlpar;
293 mii->mii_media_status = IFM_AVALID;
294 mii->mii_media_active = IFM_ETHER;
296 bmsr = PHY_READ(sc, MII_BMSR) |
297 PHY_READ(sc, MII_BMSR);
298 if (bmsr & BMSR_LINK)
299 mii->mii_media_status |= IFM_ACTIVE;
301 bmcr = PHY_READ(sc, MII_BMCR);
302 if (bmcr & BMCR_ISO) {
303 mii->mii_media_active |= IFM_NONE;
304 mii->mii_media_status = 0;
308 if (bmcr & BMCR_LOOP)
309 mii->mii_media_active |= IFM_LOOP;
311 if (bmcr & BMCR_AUTOEN) {
313 * The PAR status bits are only valid of autonegotiation
314 * has completed (or it's disabled).
316 if ((bmsr & BMSR_ACOMP) == 0) {
317 /* Erg, still trying, I guess... */
318 mii->mii_media_active |= IFM_NONE;
323 * Argh. The PAR doesn't seem to indicate duplex mode
324 * properly! Determine media based on link partner's
325 * advertised capabilities.
327 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
328 anlpar = PHY_READ(sc, MII_ANAR) &
329 PHY_READ(sc, MII_ANLPAR);
330 if (anlpar & ANLPAR_T4)
331 mii->mii_media_active |= IFM_100_T4;
332 else if (anlpar & ANLPAR_TX_FD)
333 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
334 else if (anlpar & ANLPAR_TX)
335 mii->mii_media_active |= IFM_100_TX;
336 else if (anlpar & ANLPAR_10_FD)
337 mii->mii_media_active |= IFM_10_T|IFM_FDX;
338 else if (anlpar & ANLPAR_10)
339 mii->mii_media_active |= IFM_10_T;
341 mii->mii_media_active |= IFM_NONE;
346 * Link partner is not capable of autonegotiation.
347 * We will never be in full-duplex mode if this is
348 * the case, so reading the PAR is OK.
350 par = PHY_READ(sc, MII_NSPHY_PAR);
352 mii->mii_media_active |= IFM_10_T;
354 mii->mii_media_active |= IFM_100_TX;
357 mii->mii_media_active |= IFM_FDX;
360 mii->mii_media_active |= mii->mii_media.ifm_cur->ifm_media;
364 nsphy_reset(struct mii_softc *sc)
369 anar = PHY_READ(sc, MII_ANAR);
370 anar |= BMSR_MEDIA_TO_ANAR(PHY_READ(sc, MII_BMSR));
371 PHY_WRITE(sc, MII_ANAR, anar);