1 /* $OpenBSD: nsphy.c,v 1.29 2022/04/06 18:59:29 naddy 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 const 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
nsphymatch(struct device * parent,void * match,void * aux)107 nsphymatch(struct device *parent, void *match, void *aux)
108 {
109 struct mii_attach_args *ma = aux;
110
111 if (mii_phy_match(ma, nsphys) != NULL)
112 return (10);
113
114 return (0);
115 }
116
117 void
nsphyattach(struct device * parent,struct device * self,void * aux)118 nsphyattach(struct device *parent, struct device *self, void *aux)
119 {
120 struct mii_softc *sc = (struct mii_softc *)self;
121 struct mii_attach_args *ma = aux;
122 struct mii_data *mii = ma->mii_data;
123 const struct mii_phydesc *mpd;
124
125 mpd = mii_phy_match(ma, nsphys);
126 printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
127
128 sc->mii_inst = mii->mii_instance;
129 sc->mii_phy = ma->mii_phyno;
130 sc->mii_funcs = &nsphy_funcs;
131 sc->mii_pdata = mii;
132 sc->mii_flags = ma->mii_flags;
133 sc->mii_anegticks = MII_ANEGTICKS;
134
135 PHY_RESET(sc);
136
137 sc->mii_capabilities =
138 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
139 if (sc->mii_capabilities & BMSR_MEDIAMASK)
140 mii_phy_add_media(sc);
141 }
142
143 int
nsphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)144 nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
145 {
146 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
147 int reg;
148
149 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
150 return (ENXIO);
151
152 switch (cmd) {
153 case MII_POLLSTAT:
154 /*
155 * If we're not polling our PHY instance, just return.
156 */
157 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
158 return (0);
159 break;
160
161 case MII_MEDIACHG:
162 /*
163 * If the media indicates a different PHY instance,
164 * isolate ourselves.
165 */
166 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
167 reg = PHY_READ(sc, MII_BMCR);
168 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
169 return (0);
170 }
171
172 /*
173 * If the interface is not up, don't do anything.
174 */
175 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
176 break;
177
178 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 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 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 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 reg |= PCR_CONGCTRL | PCR_TXREADYSEL;
205
206 PHY_WRITE(sc, MII_NSPHY_PCR, reg);
207
208 mii_phy_setmedia(sc);
209 break;
210
211 case MII_TICK:
212 /*
213 * If we're not currently selected, just return.
214 */
215 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
216 return (0);
217
218 if (mii_phy_tick(sc) == EJUSTRETURN)
219 return (0);
220 break;
221
222 case MII_DOWN:
223 mii_phy_down(sc);
224 return (0);
225 }
226
227 /* Update the media status. */
228 mii_phy_status(sc);
229
230 /* Callback if something changed. */
231 mii_phy_update(sc, cmd);
232 return (0);
233 }
234
235 void
nsphy_status(struct mii_softc * sc)236 nsphy_status(struct mii_softc *sc)
237 {
238 struct mii_data *mii = sc->mii_pdata;
239 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
240 int bmsr, bmcr, par, anlpar;
241
242 mii->mii_media_status = IFM_AVALID;
243 mii->mii_media_active = IFM_ETHER;
244
245 bmsr = PHY_READ(sc, MII_BMSR) |
246 PHY_READ(sc, MII_BMSR);
247 if (bmsr & BMSR_LINK)
248 mii->mii_media_status |= IFM_ACTIVE;
249
250 bmcr = PHY_READ(sc, MII_BMCR);
251 if (bmcr & BMCR_ISO) {
252 mii->mii_media_active |= IFM_NONE;
253 mii->mii_media_status = 0;
254 return;
255 }
256
257 if (bmcr & BMCR_LOOP)
258 mii->mii_media_active |= IFM_LOOP;
259
260 if (bmcr & BMCR_AUTOEN) {
261 /*
262 * The PAR status bits are only valid if autonegotiation
263 * has completed (or it's disabled).
264 */
265 if ((bmsr & BMSR_ACOMP) == 0) {
266 /* Erg, still trying, I guess... */
267 mii->mii_media_active |= IFM_NONE;
268 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 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
277 anlpar = PHY_READ(sc, MII_ANAR) &
278 PHY_READ(sc, MII_ANLPAR);
279 if (anlpar & ANLPAR_TX_FD)
280 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
281 else if (anlpar & ANLPAR_T4)
282 mii->mii_media_active |= IFM_100_T4|IFM_HDX;
283 else if (anlpar & ANLPAR_TX)
284 mii->mii_media_active |= IFM_100_TX|IFM_HDX;
285 else if (anlpar & ANLPAR_10_FD)
286 mii->mii_media_active |= IFM_10_T|IFM_FDX;
287 else if (anlpar & ANLPAR_10)
288 mii->mii_media_active |= IFM_10_T|IFM_HDX;
289 else
290 mii->mii_media_active |= IFM_NONE;
291 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 par = PHY_READ(sc, MII_NSPHY_PAR);
300 if (par & PAR_10)
301 mii->mii_media_active |= IFM_10_T;
302 else
303 mii->mii_media_active |= IFM_100_TX;
304 mii->mii_media_active |= IFM_HDX;
305 } else
306 mii->mii_media_active = ife->ifm_media;
307 }
308
309 void
nsphy_reset(struct mii_softc * sc)310 nsphy_reset(struct mii_softc *sc)
311 {
312 int anar;
313
314 mii_phy_reset(sc);
315 anar = PHY_READ(sc, MII_ANAR);
316 anar |= BMSR_MEDIA_TO_ANAR(PHY_READ(sc, MII_BMSR));
317 PHY_WRITE(sc, MII_ANAR, anar);
318 }
319