]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/dev/mii/nsphyter.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.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  *
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.
31  */
32
33 /*-
34  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
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.
44  *
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.
55  */
56
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
59
60 /*
61  * driver for National Semiconductor's DP83843 `PHYTER' ethernet 10/100 PHY
62  * Data Sheet available from www.national.com
63  *
64  * We also support the DP83815 `MacPHYTER' internal PHY since, for our
65  * purposes, they are compatible.
66  */
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/bus.h>
71 #include <sys/errno.h>
72 #include <sys/kernel.h>
73 #include <sys/module.h>
74 #include <sys/socket.h>
75
76 #include <net/if.h>
77 #include <net/if_media.h>
78
79 #include <dev/mii/mii.h>
80 #include <dev/mii/miivar.h>
81 #include "miidevs.h"
82
83 #include <dev/mii/nsphyterreg.h>
84
85 #include "miibus_if.h"
86
87 static device_probe_t   nsphyter_probe;
88 static device_attach_t  nsphyter_attach;
89
90 static device_method_t nsphyter_methods[] = {
91         /* device interface */
92         DEVMETHOD(device_probe,         nsphyter_probe),
93         DEVMETHOD(device_attach,        nsphyter_attach),
94         DEVMETHOD(device_detach,        mii_phy_detach),
95         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
96         { 0, 0 }
97 };
98
99 static devclass_t nsphyter_devclass;
100
101 static driver_t nsphyter_driver = {
102         "nsphyter",
103         nsphyter_methods,
104         sizeof(struct mii_softc)
105 };
106
107 DRIVER_MODULE(nsphyter, miibus, nsphyter_driver, nsphyter_devclass, 0, 0);
108
109 static int      nsphyter_service(struct mii_softc *, struct mii_data *, int);
110 static void     nsphyter_status(struct mii_softc *);
111 static void     nsphyter_reset(struct mii_softc *);
112
113 static const struct mii_phydesc nsphys[] = {
114         MII_PHY_DESC(NATSEMI, DP83815),
115         MII_PHY_DESC(NATSEMI, DP83843),
116         MII_PHY_DESC(NATSEMI, DP83847),
117         MII_PHY_END
118 };
119
120 static int
121 nsphyter_probe(device_t dev)
122 {
123
124         return (mii_phy_dev_probe(dev, nsphys, BUS_PROBE_DEFAULT));
125 }
126
127 static int
128 nsphyter_attach(device_t dev)
129 {
130         struct mii_softc *sc;
131         struct mii_attach_args *ma;
132         struct mii_data *mii;
133
134         sc = device_get_softc(dev);
135         ma = device_get_ivars(dev);
136         sc->mii_dev = device_get_parent(dev);
137         mii = ma->mii_data;
138         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
139
140         sc->mii_flags = miibus_get_flags(dev);
141         sc->mii_inst = mii->mii_instance++;
142         sc->mii_phy = ma->mii_phyno;
143         sc->mii_service = nsphyter_service;
144         sc->mii_pdata = mii;
145
146         sc->mii_flags |= MIIF_NOMANPAUSE;
147
148 #if 1
149
150 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
151
152         /*
153          * XXX IFM_LOOP should be handled by mii_phy_add_media() based
154          * on MIIF_NOLOOP.
155          */
156         if ((sc->mii_flags & MIIF_NOLOOP) == 0)
157                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
158                     sc->mii_inst), MII_MEDIA_100_TX);
159
160 #endif
161
162         nsphyter_reset(sc);
163
164         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
165         device_printf(dev, " ");
166         mii_phy_add_media(sc);
167         printf("\n");
168
169         MIIBUS_MEDIAINIT(sc->mii_dev);
170         return (0);
171 }
172
173 static int
174 nsphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
175 {
176
177         switch (cmd) {
178         case MII_POLLSTAT:
179                 break;
180
181         case MII_MEDIACHG:
182                 /*
183                  * If the interface is not up, don't do anything.
184                  */
185                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
186                         break;
187
188                 mii_phy_setmedia(sc);
189                 break;
190
191         case MII_TICK:
192                 if (mii_phy_tick(sc) == EJUSTRETURN)
193                         return (0);
194                 break;
195         }
196
197         /* Update the media status. */
198         nsphyter_status(sc);
199
200         /* Callback if something changed. */
201         mii_phy_update(sc, cmd);
202         return (0);
203 }
204
205 static void
206 nsphyter_status(struct mii_softc *sc)
207 {
208         struct mii_data *mii = sc->mii_pdata;
209         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
210         int bmsr, bmcr, physts;
211
212         mii->mii_media_status = IFM_AVALID;
213         mii->mii_media_active = IFM_ETHER;
214
215         bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
216         physts = PHY_READ(sc, MII_NSPHYTER_PHYSTS);
217
218         if ((physts & PHYSTS_LINK) != 0)
219                 mii->mii_media_status |= IFM_ACTIVE;
220
221         bmcr = PHY_READ(sc, MII_BMCR);
222         if ((bmcr & BMCR_ISO) != 0) {
223                 mii->mii_media_active |= IFM_NONE;
224                 mii->mii_media_status = 0;
225                 return;
226         }
227
228         if ((bmcr & BMCR_LOOP) != 0)
229                 mii->mii_media_active |= IFM_LOOP;
230
231         if ((bmcr & BMCR_AUTOEN) != 0) {
232                 /*
233                  * The media status bits are only valid if autonegotiation
234                  * has completed (or it's disabled).
235                  */
236                 if ((bmsr & BMSR_ACOMP) == 0) {
237                         /* Erg, still trying, I guess... */
238                         mii->mii_media_active |= IFM_NONE;
239                         return;
240                 }
241
242                 if ((physts & PHYSTS_SPEED10) != 0)
243                         mii->mii_media_active |= IFM_10_T;
244                 else
245                         mii->mii_media_active |= IFM_100_TX;
246                 if ((physts & PHYSTS_DUPLEX) != 0)
247                         mii->mii_media_active |=
248                             IFM_FDX | mii_phy_flowstatus(sc);
249                 else
250                         mii->mii_media_active |= IFM_HDX;
251         } else
252                 mii->mii_media_active = ife->ifm_media;
253 }
254
255 static void
256 nsphyter_reset(struct mii_softc *sc)
257 {
258         struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
259         int reg, i;
260
261         if ((sc->mii_flags & MIIF_NOISOLATE) != 0)
262                 reg = BMCR_RESET;
263         else
264                 reg = BMCR_RESET | BMCR_ISO;
265         PHY_WRITE(sc, MII_BMCR, reg);
266
267         /*
268          * It is best to allow a little time for the reset to settle
269          * in before we start polling the BMCR again.  Notably, the
270          * DP8384{3,7} manuals state that there should be a 500us delay
271          * between asserting software reset and attempting MII serial
272          * operations.  Be conservative.  Also, a DP83815 can get into
273          * a bad state on cable removal and reinsertion if we do not
274          * delay here.
275          */
276         DELAY(1000);
277
278         /*
279          * Wait another 2s for it to complete.
280          * This is only a little overkill as under normal circumstances
281          * the PHY can take up to 1s to complete reset.
282          * This is also a bit odd because after a reset, the BMCR will
283          * clear the reset bit and simply reports 0 even though the reset
284          * is not yet complete.
285          */
286         for (i = 0; i < 1000; i++) {
287                 reg = PHY_READ(sc, MII_BMCR);
288                 if (reg != 0 && (reg & BMCR_RESET) == 0)
289                         break;
290                 DELAY(2000);
291         }
292
293         if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
294                 if ((ife == NULL && sc->mii_inst != 0) ||
295                     (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
296                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
297         }
298 }