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