1 /* $NetBSD: nsphyter.c,v 1.28 2008/01/20 07:58:19 msaitoh Exp $ */
4 * Copyright (c) 1998, 1999, 2000, 2001 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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
61 * Driver for the National Semiconductor's DP83843, DP83847 and DP83849
62 * `PHYTER' Ethernet 10/100 PHYs
63 * Data Sheets are available from http://www.national.com
65 * We also support the DP83815 `MacPHYTER' internal PHY since, for our
66 * purposes, they are compatible.
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/errno.h>
73 #include <sys/kernel.h>
74 #include <sys/module.h>
75 #include <sys/socket.h>
78 #include <net/if_media.h>
80 #include <dev/mii/mii.h>
81 #include <dev/mii/miivar.h>
84 #include <dev/mii/nsphyterreg.h>
86 #include "miibus_if.h"
88 static device_probe_t nsphyter_probe;
89 static device_attach_t nsphyter_attach;
91 static device_method_t nsphyter_methods[] = {
92 /* device interface */
93 DEVMETHOD(device_probe, nsphyter_probe),
94 DEVMETHOD(device_attach, nsphyter_attach),
95 DEVMETHOD(device_detach, mii_phy_detach),
96 DEVMETHOD(device_shutdown, bus_generic_shutdown),
100 static devclass_t nsphyter_devclass;
102 static driver_t nsphyter_driver = {
105 sizeof(struct mii_softc)
108 DRIVER_MODULE(nsphyter, miibus, nsphyter_driver, nsphyter_devclass, 0, 0);
110 static int nsphyter_service(struct mii_softc *, struct mii_data *, int);
111 static void nsphyter_status(struct mii_softc *);
112 static void nsphyter_reset(struct mii_softc *);
114 static const struct mii_phydesc nsphyters[] = {
115 MII_PHY_DESC(xxNATSEMI, DP83815),
116 MII_PHY_DESC(xxNATSEMI, DP83843),
117 MII_PHY_DESC(xxNATSEMI, DP83847),
118 MII_PHY_DESC(xxNATSEMI, DP83849),
122 static const struct mii_phy_funcs nsphyter_funcs = {
129 nsphyter_probe(device_t dev)
132 return (mii_phy_dev_probe(dev, nsphyters, BUS_PROBE_DEFAULT));
136 nsphyter_attach(device_t dev)
139 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &nsphyter_funcs, 1);
144 nsphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
152 mii_phy_setmedia(sc);
156 if (mii_phy_tick(sc) == EJUSTRETURN)
161 /* Update the media status. */
164 /* Callback if something changed. */
165 mii_phy_update(sc, cmd);
170 nsphyter_status(struct mii_softc *sc)
172 struct mii_data *mii = sc->mii_pdata;
173 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
174 int bmsr, bmcr, physts;
176 mii->mii_media_status = IFM_AVALID;
177 mii->mii_media_active = IFM_ETHER;
179 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
180 physts = PHY_READ(sc, MII_NSPHYTER_PHYSTS);
182 if ((physts & PHYSTS_LINK) != 0)
183 mii->mii_media_status |= IFM_ACTIVE;
185 bmcr = PHY_READ(sc, MII_BMCR);
186 if ((bmcr & BMCR_ISO) != 0) {
187 mii->mii_media_active |= IFM_NONE;
188 mii->mii_media_status = 0;
192 if ((bmcr & BMCR_LOOP) != 0)
193 mii->mii_media_active |= IFM_LOOP;
195 if ((bmcr & BMCR_AUTOEN) != 0) {
197 * The media status bits are only valid if autonegotiation
198 * has completed (or it's disabled).
200 if ((bmsr & BMSR_ACOMP) == 0) {
201 /* Erg, still trying, I guess... */
202 mii->mii_media_active |= IFM_NONE;
206 if ((physts & PHYSTS_SPEED10) != 0)
207 mii->mii_media_active |= IFM_10_T;
209 mii->mii_media_active |= IFM_100_TX;
210 if ((physts & PHYSTS_DUPLEX) != 0)
211 mii->mii_media_active |=
212 IFM_FDX | mii_phy_flowstatus(sc);
214 mii->mii_media_active |= IFM_HDX;
216 mii->mii_media_active = ife->ifm_media;
220 nsphyter_reset(struct mii_softc *sc)
222 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
225 if ((sc->mii_flags & MIIF_NOISOLATE) != 0)
228 reg = BMCR_RESET | BMCR_ISO;
229 PHY_WRITE(sc, MII_BMCR, reg);
232 * It is best to allow a little time for the reset to settle
233 * in before we start polling the BMCR again. Notably, the
234 * DP8384{3,7} manuals state that there should be a 500us delay
235 * between asserting software reset and attempting MII serial
236 * operations. Be conservative. Also, a DP83815 can get into
237 * a bad state on cable removal and reinsertion if we do not
243 * Wait another 2s for it to complete.
244 * This is only a little overkill as under normal circumstances
245 * the PHY can take up to 1s to complete reset.
246 * This is also a bit odd because after a reset, the BMCR will
247 * clear the reset bit and simply reports 0 even though the reset
248 * is not yet complete.
250 for (i = 0; i < 1000; i++) {
251 reg = PHY_READ(sc, MII_BMCR);
252 if (reg != 0 && (reg & BMCR_RESET) == 0)
257 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
258 if ((ife == NULL && sc->mii_inst != 0) ||
259 (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
260 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);