]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/mii/nsphyter.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / mii / nsphyter.c
1 /*      $NetBSD: nsphyter.c,v 1.28 2008/01/20 07:58:19 msaitoh Exp $    */
2
3 /*-
4  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
26  *
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.
38  */
39
40 /*-
41  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
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.
56  *
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.
67  */
68
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
71
72 /*
73  * driver for National Semiconductor's DP83843 `PHYTER' ethernet 10/100 PHY
74  * Data Sheet available from www.national.com
75  *
76  * We also support the DP83815 `MacPHYTER' internal PHY since, for our
77  * purposes, they are compatible.
78  */
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/bus.h>
83 #include <sys/errno.h>
84 #include <sys/kernel.h>
85 #include <sys/module.h>
86 #include <sys/socket.h>
87
88 #include <net/if.h>
89 #include <net/if_media.h>
90
91 #include <dev/mii/mii.h>
92 #include <dev/mii/miivar.h>
93 #include "miidevs.h"
94
95 #include <dev/mii/nsphyterreg.h>
96
97 #include "miibus_if.h"
98
99 static device_probe_t   nsphyter_probe;
100 static device_attach_t  nsphyter_attach;
101
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),
108         { 0, 0 }
109 };
110
111 static devclass_t nsphyter_devclass;
112
113 static driver_t nsphyter_driver = {
114         "nsphyter",
115         nsphyter_methods,
116         sizeof(struct mii_softc)
117 };
118
119 DRIVER_MODULE(nsphyter, miibus, nsphyter_driver, nsphyter_devclass, 0, 0);
120
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 *);
124
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),
129         MII_PHY_END
130 };
131
132 static int
133 nsphyter_probe(device_t dev)
134 {
135
136         return (mii_phy_dev_probe(dev, nsphys, BUS_PROBE_DEFAULT));
137 }
138
139 static int
140 nsphyter_attach(device_t dev)
141 {
142         struct mii_softc *sc;
143         struct mii_attach_args *ma;
144         struct mii_data *mii;
145         const char *nic;
146
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);
152
153         sc->mii_inst = mii->mii_instance;
154         sc->mii_phy = ma->mii_phyno;
155         sc->mii_service = nsphyter_service;
156         sc->mii_pdata = mii;
157
158         mii->mii_instance++;
159
160 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
161
162         nic = device_get_name(device_get_parent(sc->mii_dev));
163         /*
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.
167          */
168         if (strcmp(nic, "pcn") != 0)
169 #if 1
170                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
171                     sc->mii_inst), MII_MEDIA_100_TX);
172 #else
173         if (strcmp(nic, "pcn") == 0)
174                 sc->mii_flags |= MIIF_NOLOOP;
175 #endif
176
177         nsphyter_reset(sc);
178
179         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
180         device_printf(dev, " ");
181         mii_phy_add_media(sc);
182         printf("\n");
183
184 #undef ADD
185
186         MIIBUS_MEDIAINIT(sc->mii_dev);
187         return (0);
188 }
189
190 static int
191 nsphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
192 {
193         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
194         int reg;
195
196         switch (cmd) {
197         case MII_POLLSTAT:
198                 /*
199                  * If we're not polling our PHY instance, just return.
200                  */
201                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
202                         return (0);
203                 break;
204
205         case MII_MEDIACHG:
206                 /*
207                  * If the media indicates a different PHY instance,
208                  * isolate ourselves.
209                  */
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);
213                         return (0);
214                 }
215
216                 /*
217                  * If the interface is not up, don't do anything.
218                  */
219                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
220                         break;
221
222                 mii_phy_setmedia(sc);
223                 break;
224
225         case MII_TICK:
226                 /*
227                  * If we're not currently selected, just return.
228                  */
229                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
230                         return (0);
231                 if (mii_phy_tick(sc) == EJUSTRETURN)
232                         return (0);
233                 break;
234         }
235
236         /* Update the media status. */
237         nsphyter_status(sc);
238
239         /* Callback if something changed. */
240         mii_phy_update(sc, cmd);
241         return (0);
242 }
243
244 static void
245 nsphyter_status(struct mii_softc *sc)
246 {
247         struct mii_data *mii = sc->mii_pdata;
248         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
249         int bmsr, bmcr, physts;
250
251         mii->mii_media_status = IFM_AVALID;
252         mii->mii_media_active = IFM_ETHER;
253
254         bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
255         physts = PHY_READ(sc, MII_NSPHYTER_PHYSTS);
256
257         if ((physts & PHYSTS_LINK) != 0)
258                 mii->mii_media_status |= IFM_ACTIVE;
259
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;
264                 return;
265         }
266
267         if ((bmcr & BMCR_LOOP) != 0)
268                 mii->mii_media_active |= IFM_LOOP;
269
270         if ((bmcr & BMCR_AUTOEN) != 0) {
271                 /*
272                  * The media status bits are only valid if autonegotiation
273                  * has completed (or it's disabled).
274                  */
275                 if ((bmsr & BMSR_ACOMP) == 0) {
276                         /* Erg, still trying, I guess... */
277                         mii->mii_media_active |= IFM_NONE;
278                         return;
279                 }
280
281                 if ((physts & PHYSTS_SPEED10) != 0)
282                         mii->mii_media_active |= IFM_10_T;
283                 else
284                         mii->mii_media_active |= IFM_100_TX;
285                 if ((physts & PHYSTS_DUPLEX) != 0)
286 #ifdef notyet
287                         mii->mii_media_active |=
288                             IFM_FDX | mii_phy_flowstatus(sc);
289 #else
290                         mii->mii_media_active |= IFM_FDX;
291 #endif
292                 else
293                         mii->mii_media_active |= IFM_HDX;
294         } else
295                 mii->mii_media_active = ife->ifm_media;
296 }
297
298 static void
299 nsphyter_reset(struct mii_softc *sc)
300 {
301         struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
302         int reg, i;
303
304         if ((sc->mii_flags & MIIF_NOISOLATE) != 0)
305                 reg = BMCR_RESET;
306         else
307                 reg = BMCR_RESET | BMCR_ISO;
308         PHY_WRITE(sc, MII_BMCR, reg);
309
310         /*
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
317          * delay here.
318          */
319         DELAY(1000);
320
321         /*
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.
328          */
329         for (i = 0; i < 1000; i++) {
330                 reg = PHY_READ(sc, MII_BMCR);
331                 if (reg != 0 && (reg & BMCR_RESET) == 0)
332                         break;
333                 DELAY(2000);
334         }
335
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);
340         }
341 }