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