Line data Source code
1 : /* $OpenBSD: nsphy.c,v 1.28 2015/03/14 03:38:48 jsg Exp $ */
2 : /* $NetBSD: nsphy.c,v 1.25 2000/02/02 23:34:57 thorpej Exp $ */
3 :
4 : /*-
5 : * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6 : * All rights reserved.
7 : *
8 : * This code is derived from software contributed to The NetBSD Foundation
9 : * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 : * NASA Ames Research Center.
11 : *
12 : * Redistribution and use in source and binary forms, with or without
13 : * modification, are permitted provided that the following conditions
14 : * are met:
15 : * 1. Redistributions of source code must retain the above copyright
16 : * notice, this list of conditions and the following disclaimer.
17 : * 2. Redistributions in binary form must reproduce the above copyright
18 : * notice, this list of conditions and the following disclaimer in the
19 : * documentation and/or other materials provided with the distribution.
20 : *
21 : * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 : * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 : * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 : * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 : * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 : * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 : * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 : * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 : * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 : * POSSIBILITY OF SUCH DAMAGE.
32 : */
33 :
34 : /*
35 : * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
36 : *
37 : * Redistribution and use in source and binary forms, with or without
38 : * modification, are permitted provided that the following conditions
39 : * are met:
40 : * 1. Redistributions of source code must retain the above copyright
41 : * notice, this list of conditions and the following disclaimer.
42 : * 2. Redistributions in binary form must reproduce the above copyright
43 : * notice, this list of conditions and the following disclaimer in the
44 : * documentation and/or other materials provided with the distribution.
45 : *
46 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 : * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50 : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51 : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 : */
57 :
58 : /*
59 : * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
60 : * Data Sheet available from www.national.com
61 : */
62 :
63 : #include <sys/param.h>
64 : #include <sys/systm.h>
65 : #include <sys/device.h>
66 : #include <sys/socket.h>
67 : #include <sys/errno.h>
68 :
69 : #include <net/if.h>
70 : #include <net/if_var.h>
71 : #include <net/if_media.h>
72 :
73 : #include <dev/mii/mii.h>
74 : #include <dev/mii/miivar.h>
75 : #include <dev/mii/miidevs.h>
76 :
77 : #include <dev/mii/nsphyreg.h>
78 :
79 : int nsphymatch(struct device *, void *, void *);
80 : void nsphyattach(struct device *, struct device *, void *);
81 :
82 : struct cfattach nsphy_ca = {
83 : sizeof(struct mii_softc), nsphymatch, nsphyattach, mii_phy_detach
84 : };
85 :
86 : struct cfdriver nsphy_cd = {
87 : NULL, "nsphy", DV_DULL
88 : };
89 :
90 : int nsphy_service(struct mii_softc *, struct mii_data *, int);
91 : void nsphy_status(struct mii_softc *);
92 : void nsphy_reset(struct mii_softc *);
93 :
94 : const struct mii_phy_funcs nsphy_funcs = {
95 : nsphy_service, nsphy_status, nsphy_reset,
96 : };
97 :
98 : static const struct mii_phydesc nsphys[] = {
99 : { MII_OUI_NATSEMI, MII_MODEL_NATSEMI_DP83840,
100 : MII_STR_NATSEMI_DP83840 },
101 :
102 : { 0, 0,
103 : NULL },
104 : };
105 :
106 : int
107 0 : nsphymatch(struct device *parent, void *match, void *aux)
108 : {
109 0 : struct mii_attach_args *ma = aux;
110 :
111 0 : if (mii_phy_match(ma, nsphys) != NULL)
112 0 : return (10);
113 :
114 0 : return (0);
115 0 : }
116 :
117 : void
118 0 : nsphyattach(struct device *parent, struct device *self, void *aux)
119 : {
120 0 : struct mii_softc *sc = (struct mii_softc *)self;
121 0 : struct mii_attach_args *ma = aux;
122 0 : struct mii_data *mii = ma->mii_data;
123 : const struct mii_phydesc *mpd;
124 :
125 0 : mpd = mii_phy_match(ma, nsphys);
126 0 : printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
127 :
128 0 : sc->mii_inst = mii->mii_instance;
129 0 : sc->mii_phy = ma->mii_phyno;
130 0 : sc->mii_funcs = &nsphy_funcs;
131 0 : sc->mii_pdata = mii;
132 0 : sc->mii_flags = ma->mii_flags;
133 0 : sc->mii_anegticks = MII_ANEGTICKS;
134 :
135 0 : PHY_RESET(sc);
136 :
137 0 : sc->mii_capabilities =
138 0 : PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
139 0 : if (sc->mii_capabilities & BMSR_MEDIAMASK)
140 0 : mii_phy_add_media(sc);
141 0 : }
142 :
143 : int
144 0 : nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
145 : {
146 0 : struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
147 : int reg;
148 :
149 0 : if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
150 0 : return (ENXIO);
151 :
152 0 : switch (cmd) {
153 : case MII_POLLSTAT:
154 : /*
155 : * If we're not polling our PHY instance, just return.
156 : */
157 0 : if (IFM_INST(ife->ifm_media) != sc->mii_inst)
158 0 : return (0);
159 : break;
160 :
161 : case MII_MEDIACHG:
162 : /*
163 : * If the media indicates a different PHY instance,
164 : * isolate ourselves.
165 : */
166 0 : if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
167 0 : reg = PHY_READ(sc, MII_BMCR);
168 0 : PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
169 0 : return (0);
170 : }
171 :
172 : /*
173 : * If the interface is not up, don't do anything.
174 : */
175 0 : if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
176 : break;
177 :
178 0 : reg = PHY_READ(sc, MII_NSPHY_PCR);
179 :
180 : /*
181 : * Set up the PCR to use LED4 to indicate full-duplex
182 : * in both 10baseT and 100baseTX modes.
183 : */
184 0 : reg |= PCR_LED4MODE;
185 :
186 : /*
187 : * Make sure Carrier Integrity Monitor function is
188 : * disabled (normal for Node operation, but sometimes
189 : * it's not set?!)
190 : */
191 0 : reg |= PCR_CIMDIS;
192 :
193 : /*
194 : * Make sure "force link good" is set to normal mode.
195 : * It's only intended for debugging.
196 : */
197 0 : reg |= PCR_FLINK100;
198 :
199 : /*
200 : * Mystery bits which are supposedly `reserved',
201 : * but we seem to need to set them when the PHY
202 : * is connected to some interfaces!
203 : */
204 0 : reg |= PCR_CONGCTRL | PCR_TXREADYSEL;
205 :
206 0 : PHY_WRITE(sc, MII_NSPHY_PCR, reg);
207 :
208 0 : mii_phy_setmedia(sc);
209 0 : break;
210 :
211 : case MII_TICK:
212 : /*
213 : * If we're not currently selected, just return.
214 : */
215 0 : if (IFM_INST(ife->ifm_media) != sc->mii_inst)
216 0 : return (0);
217 :
218 0 : if (mii_phy_tick(sc) == EJUSTRETURN)
219 0 : return (0);
220 : break;
221 :
222 : case MII_DOWN:
223 0 : mii_phy_down(sc);
224 0 : return (0);
225 : }
226 :
227 : /* Update the media status. */
228 0 : mii_phy_status(sc);
229 :
230 : /* Callback if something changed. */
231 0 : mii_phy_update(sc, cmd);
232 0 : return (0);
233 0 : }
234 :
235 : void
236 0 : nsphy_status(struct mii_softc *sc)
237 : {
238 0 : struct mii_data *mii = sc->mii_pdata;
239 0 : struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
240 : int bmsr, bmcr, par, anlpar;
241 :
242 0 : mii->mii_media_status = IFM_AVALID;
243 0 : mii->mii_media_active = IFM_ETHER;
244 :
245 0 : bmsr = PHY_READ(sc, MII_BMSR) |
246 0 : PHY_READ(sc, MII_BMSR);
247 0 : if (bmsr & BMSR_LINK)
248 0 : mii->mii_media_status |= IFM_ACTIVE;
249 :
250 0 : bmcr = PHY_READ(sc, MII_BMCR);
251 0 : if (bmcr & BMCR_ISO) {
252 0 : mii->mii_media_active |= IFM_NONE;
253 0 : mii->mii_media_status = 0;
254 0 : return;
255 : }
256 :
257 0 : if (bmcr & BMCR_LOOP)
258 0 : mii->mii_media_active |= IFM_LOOP;
259 :
260 0 : if (bmcr & BMCR_AUTOEN) {
261 : /*
262 : * The PAR status bits are only valid if autonegotiation
263 : * has completed (or it's disabled).
264 : */
265 0 : if ((bmsr & BMSR_ACOMP) == 0) {
266 : /* Erg, still trying, I guess... */
267 0 : mii->mii_media_active |= IFM_NONE;
268 0 : return;
269 : }
270 :
271 : /*
272 : * Argh. The PAR doesn't seem to indicate duplex mode
273 : * properly! Determine media based on link partner's
274 : * advertised capabilities.
275 : */
276 0 : if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
277 0 : anlpar = PHY_READ(sc, MII_ANAR) &
278 0 : PHY_READ(sc, MII_ANLPAR);
279 0 : if (anlpar & ANLPAR_TX_FD)
280 0 : mii->mii_media_active |= IFM_100_TX|IFM_FDX;
281 0 : else if (anlpar & ANLPAR_T4)
282 0 : mii->mii_media_active |= IFM_100_T4|IFM_HDX;
283 0 : else if (anlpar & ANLPAR_TX)
284 0 : mii->mii_media_active |= IFM_100_TX|IFM_HDX;
285 0 : else if (anlpar & ANLPAR_10_FD)
286 0 : mii->mii_media_active |= IFM_10_T|IFM_FDX;
287 0 : else if (anlpar & ANLPAR_10)
288 0 : mii->mii_media_active |= IFM_10_T|IFM_HDX;
289 : else
290 0 : mii->mii_media_active |= IFM_NONE;
291 0 : return;
292 : }
293 :
294 : /*
295 : * Link partner is not capable of autonegotiation.
296 : * We will never be in full-duplex mode if this is
297 : * the case, so reading the PAR is OK.
298 : */
299 0 : par = PHY_READ(sc, MII_NSPHY_PAR);
300 0 : if (par & PAR_10)
301 0 : mii->mii_media_active |= IFM_10_T;
302 : else
303 0 : mii->mii_media_active |= IFM_100_TX;
304 0 : mii->mii_media_active |= IFM_HDX;
305 0 : } else
306 0 : mii->mii_media_active = ife->ifm_media;
307 0 : }
308 :
309 : void
310 0 : nsphy_reset(struct mii_softc *sc)
311 : {
312 : int anar;
313 :
314 0 : mii_phy_reset(sc);
315 0 : anar = PHY_READ(sc, MII_ANAR);
316 0 : anar |= BMSR_MEDIA_TO_ANAR(PHY_READ(sc, MII_BMSR));
317 0 : PHY_WRITE(sc, MII_ANAR, anar);
318 0 : }
|