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.
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.
41 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Manuel Bouyer.
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
73 * driver for National Semiconductor's DP83843 `PHYTER' ethernet 10/100 PHY
74 * Data Sheet available from www.national.com
76 * We also support the DP83815 `MacPHYTER' internal PHY since, for our
77 * purposes, they are compatible.
80 #include <sys/param.h>
81 #include <sys/systm.h>
83 #include <sys/errno.h>
84 #include <sys/kernel.h>
85 #include <sys/module.h>
86 #include <sys/socket.h>
89 #include <net/if_media.h>
91 #include <dev/mii/mii.h>
92 #include <dev/mii/miivar.h>
95 #include <dev/mii/nsphyterreg.h>
97 #include "miibus_if.h"
99 static device_probe_t nsphyter_probe;
100 static device_attach_t nsphyter_attach;
102 static device_method_t nsphyter_methods[] = {
103 /* device interface */
104 DEVMETHOD(device_probe, nsphyter_probe),
105 DEVMETHOD(device_attach, nsphyter_attach),
106 DEVMETHOD(device_detach, mii_phy_detach),
107 DEVMETHOD(device_shutdown, bus_generic_shutdown),
111 static devclass_t nsphyter_devclass;
113 static driver_t nsphyter_driver = {
116 sizeof(struct mii_softc)
119 DRIVER_MODULE(nsphyter, miibus, nsphyter_driver, nsphyter_devclass, 0, 0);
121 static int nsphyter_service(struct mii_softc *, struct mii_data *, int);
122 static void nsphyter_status(struct mii_softc *);
123 static void nsphyter_reset(struct mii_softc *);
125 static const struct mii_phydesc nsphys[] = {
126 MII_PHY_DESC(NATSEMI, DP83815),
127 MII_PHY_DESC(NATSEMI, DP83843),
128 MII_PHY_DESC(NATSEMI, DP83847),
133 nsphyter_probe(device_t dev)
136 return (mii_phy_dev_probe(dev, nsphys, BUS_PROBE_DEFAULT));
140 nsphyter_attach(device_t dev)
142 struct mii_softc *sc;
143 struct mii_attach_args *ma;
144 struct mii_data *mii;
147 sc = device_get_softc(dev);
148 ma = device_get_ivars(dev);
149 sc->mii_dev = device_get_parent(dev);
150 mii = device_get_softc(sc->mii_dev);
151 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
153 sc->mii_inst = mii->mii_instance;
154 sc->mii_phy = ma->mii_phyno;
155 sc->mii_service = nsphyter_service;
160 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
162 nic = device_get_name(device_get_parent(sc->mii_dev));
164 * In order for MII loopback to work Am79C971 and greater PCnet
165 * chips additionally need to be placed into external loopback
166 * mode which pcn(4) doesn't do so far.
168 if (strcmp(nic, "pcn") != 0)
170 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
171 sc->mii_inst), MII_MEDIA_100_TX);
173 if (strcmp(nic, "pcn") == 0)
174 sc->mii_flags |= MIIF_NOLOOP;
179 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
180 device_printf(dev, " ");
181 mii_phy_add_media(sc);
186 MIIBUS_MEDIAINIT(sc->mii_dev);
191 nsphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
193 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
199 * If we're not polling our PHY instance, just return.
201 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
207 * If the media indicates a different PHY instance,
210 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
211 reg = PHY_READ(sc, MII_BMCR);
212 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
217 * If the interface is not up, don't do anything.
219 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
222 mii_phy_setmedia(sc);
227 * If we're not currently selected, just return.
229 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
231 if (mii_phy_tick(sc) == EJUSTRETURN)
236 /* Update the media status. */
239 /* Callback if something changed. */
240 mii_phy_update(sc, cmd);
245 nsphyter_status(struct mii_softc *sc)
247 struct mii_data *mii = sc->mii_pdata;
248 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
249 int bmsr, bmcr, physts;
251 mii->mii_media_status = IFM_AVALID;
252 mii->mii_media_active = IFM_ETHER;
254 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
255 physts = PHY_READ(sc, MII_NSPHYTER_PHYSTS);
257 if ((physts & PHYSTS_LINK) != 0)
258 mii->mii_media_status |= IFM_ACTIVE;
260 bmcr = PHY_READ(sc, MII_BMCR);
261 if ((bmcr & BMCR_ISO) != 0) {
262 mii->mii_media_active |= IFM_NONE;
263 mii->mii_media_status = 0;
267 if ((bmcr & BMCR_LOOP) != 0)
268 mii->mii_media_active |= IFM_LOOP;
270 if ((bmcr & BMCR_AUTOEN) != 0) {
272 * The media status bits are only valid if autonegotiation
273 * has completed (or it's disabled).
275 if ((bmsr & BMSR_ACOMP) == 0) {
276 /* Erg, still trying, I guess... */
277 mii->mii_media_active |= IFM_NONE;
281 if ((physts & PHYSTS_SPEED10) != 0)
282 mii->mii_media_active |= IFM_10_T;
284 mii->mii_media_active |= IFM_100_TX;
285 if ((physts & PHYSTS_DUPLEX) != 0)
287 mii->mii_media_active |=
288 IFM_FDX | mii_phy_flowstatus(sc);
290 mii->mii_media_active |= IFM_FDX;
293 mii->mii_media_active |= IFM_HDX;
295 mii->mii_media_active = ife->ifm_media;
299 nsphyter_reset(struct mii_softc *sc)
301 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
304 if ((sc->mii_flags & MIIF_NOISOLATE) != 0)
307 reg = BMCR_RESET | BMCR_ISO;
308 PHY_WRITE(sc, MII_BMCR, reg);
311 * It is best to allow a little time for the reset to settle
312 * in before we start polling the BMCR again. Notably, the
313 * DP8384{3,7} manuals state that there should be a 500us delay
314 * between asserting software reset and attempting MII serial
315 * operations. Be conservative. Also, a DP83815 can get into
316 * a bad state on cable removal and reinsertion if we do not
322 * Wait another 2s for it to complete.
323 * This is only a little overkill as under normal circumstances
324 * the PHY can take up to 1s to complete reset.
325 * This is also a bit odd because after a reset, the BMCR will
326 * clear the reset bit and simply reports 0 even though the reset
327 * is not yet complete.
329 for (i = 0; i < 1000; i++) {
330 reg = PHY_READ(sc, MII_BMCR);
331 if (reg != 0 && (reg & BMCR_RESET) == 0)
336 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
337 if ((ife == NULL && sc->mii_inst != 0) ||
338 (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
339 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);