nsphy.c revision 1.7
1/*	$OpenBSD: nsphy.c,v 1.7 1999/12/07 22:01:32 jason Exp $	*/
2/*	$NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 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 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *	This product includes software developed by the NetBSD
23 *	Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 *    contributors may be used to endorse or promote products derived
26 *    from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41/*
42 * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 *    notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 *    notice, this list of conditions and the following disclaimer in the
51 *    documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 *    must display the following acknowledgement:
54 *	This product includes software developed by Manuel Bouyer.
55 * 4. The name of the author may not be used to endorse or promote products
56 *    derived from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 */
69
70/*
71 * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
72 * Data Sheet available from www.national.com
73 */
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/kernel.h>
78#include <sys/device.h>
79#include <sys/malloc.h>
80#include <sys/socket.h>
81#include <sys/errno.h>
82
83#include <net/if.h>
84#include <net/if_media.h>
85
86#include <dev/mii/mii.h>
87#include <dev/mii/miivar.h>
88#include <dev/mii/miidevs.h>
89
90#include <dev/mii/nsphyreg.h>
91
92int	nsphymatch __P((struct device *, void *, void *));
93void	nsphyattach __P((struct device *, struct device *, void *));
94
95struct cfattach nsphy_ca = {
96	sizeof(struct mii_softc), nsphymatch, nsphyattach
97};
98
99struct cfdriver nsphy_cd = {
100	NULL, "nsphy", DV_DULL
101};
102
103int	nsphy_service __P((struct mii_softc *, struct mii_data *, int));
104void	nsphy_status __P((struct mii_softc *));
105void	nsphy_reset __P((struct mii_softc *));
106
107int
108nsphymatch(parent, match, aux)
109	struct device *parent;
110	void *match;
111	void *aux;
112{
113	struct mii_attach_args *ma = aux;
114
115	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI &&
116	    MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840)
117		return (10);
118
119	return (0);
120}
121
122void
123nsphyattach(parent, self, aux)
124	struct device *parent;
125	struct device *self;
126	void *aux;
127{
128	struct mii_softc *sc = (struct mii_softc *)self;
129	struct mii_attach_args *ma = aux;
130	struct mii_data *mii = ma->mii_data;
131
132	printf(": %s, rev. %d\n", MII_STR_NATSEMI_DP83840,
133	    MII_REV(ma->mii_id2));
134
135	sc->mii_inst = mii->mii_instance;
136	sc->mii_phy = ma->mii_phyno;
137	sc->mii_service = nsphy_service;
138	sc->mii_pdata = mii;
139
140	/*
141	 * i82557 wedges if all of its PHYs are isolated!
142	 */
143	if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "fxp") == 0 &&
144	    mii->mii_instance == 0)
145		sc->mii_flags |= MIIF_NOISOLATE;
146
147	nsphy_reset(sc);
148
149	sc->mii_capabilities =
150	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
151	if (sc->mii_capabilities & BMSR_MEDIAMASK)
152		mii_add_media(sc);
153}
154
155int
156nsphy_service(sc, mii, cmd)
157	struct mii_softc *sc;
158	struct mii_data *mii;
159	int cmd;
160{
161	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
162	int reg;
163
164	switch (cmd) {
165	case MII_POLLSTAT:
166		/*
167		 * If we're not polling our PHY instance, just return.
168		 */
169		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
170			return (0);
171		break;
172
173	case MII_MEDIACHG:
174		/*
175		 * If the media indicates a different PHY instance,
176		 * isolate ourselves.
177		 */
178		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
179			reg = PHY_READ(sc, MII_BMCR);
180			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
181			return (0);
182		}
183
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		reg = PHY_READ(sc, MII_NSPHY_PCR);
191
192		/*
193		 * Set up the PCR to use LED4 to indicate full-duplex
194		 * in both 10baseT and 100baseTX modes.
195		 */
196		reg |= PCR_LED4MODE;
197
198		/*
199		 * Make sure Carrier Intgrity Monitor function is
200		 * disabled (normal for Node operation, but sometimes
201		 * it's not set?!)
202		 */
203		reg |= PCR_CIMDIS;
204
205		/*
206		 * Make sure "force link good" is set to normal mode.
207		 * It's only intended for debugging.
208		 */
209		reg |= PCR_FLINK100;
210
211#if 0
212		/*
213		 * Mystery bits which are supposedly `reserved',
214		 * but we seem to need to set them when the PHY
215		 * is connected to some interfaces!
216		 */
217		reg |= 0x0100 | 0x0400;
218#endif
219
220		PHY_WRITE(sc, MII_NSPHY_PCR, reg);
221
222		switch (IFM_SUBTYPE(ife->ifm_media)) {
223		case IFM_AUTO:
224			nsphy_reset(sc);
225			/*
226			 * If we're already in auto mode, just return.
227			 */
228			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
229				return (0);
230			(void) mii_phy_auto(sc, 1);
231			break;
232		default:
233			mii_phy_setmedia(sc);
234		}
235		break;
236
237	case MII_TICK:
238		/*
239		 * If we're not currently selected, just return.
240		 */
241		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
242			return (0);
243
244		/*
245		 * Only used for autonegotiation.
246		 */
247		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
248			return (0);
249
250		/*
251		 * Is the interface even up?
252		 */
253		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
254			return (0);
255
256		/*
257		 * Check to see if we have link.  If we do, we don't
258		 * need to restart the autonegotiation process.  Read
259		 * the BMSR twice in case it's latched.
260		 */
261		reg = PHY_READ(sc, MII_BMSR) |
262		    PHY_READ(sc, MII_BMSR);
263		if (reg & BMSR_LINK)
264			return (0);
265
266		/*
267		 * Only retry autonegotiation every 5 seconds.
268		 */
269		if (++sc->mii_ticks != 5)
270			return (0);
271
272		sc->mii_ticks = 0;
273		nsphy_reset(sc);
274		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
275			return (0);
276		break;
277
278	case MII_DOWN:
279		mii_phy_down(sc);
280		return (0);
281	}
282
283	/* Update the media status. */
284	nsphy_status(sc);
285
286	/* Callback if something changed. */
287	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
288		(*mii->mii_statchg)(sc->mii_dev.dv_parent);
289		sc->mii_active = mii->mii_media_active;
290	}
291	return (0);
292}
293
294void
295nsphy_status(sc)
296	struct mii_softc *sc;
297{
298	struct mii_data *mii = sc->mii_pdata;
299	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
300	int bmsr, bmcr, par, anlpar;
301
302	mii->mii_media_status = IFM_AVALID;
303	mii->mii_media_active = IFM_ETHER;
304
305	bmsr = PHY_READ(sc, MII_BMSR) |
306	    PHY_READ(sc, MII_BMSR);
307	if (bmsr & BMSR_LINK)
308		mii->mii_media_status |= IFM_ACTIVE;
309
310	bmcr = PHY_READ(sc, MII_BMCR);
311	if (bmcr & BMCR_ISO) {
312		mii->mii_media_active |= IFM_NONE;
313		mii->mii_media_status = 0;
314		return;
315	}
316
317	if (bmcr & BMCR_LOOP)
318		mii->mii_media_active |= IFM_LOOP;
319
320	if (bmcr & BMCR_AUTOEN) {
321		/*
322		 * The PAR status bits are only valid of autonegotiation
323		 * has completed (or it's disabled).
324		 */
325		if ((bmsr & BMSR_ACOMP) == 0) {
326			/* Erg, still trying, I guess... */
327			mii->mii_media_active |= IFM_NONE;
328			return;
329		}
330
331		/*
332		 * Argh.  The PAR doesn't seem to indicate duplex mode
333		 * properly!  Determine media based on link partner's
334		 * advertised capabilities.
335		 */
336		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
337			anlpar = PHY_READ(sc, MII_ANAR) &
338			    PHY_READ(sc, MII_ANLPAR);
339			if (anlpar & ANLPAR_T4)
340				mii->mii_media_active |= IFM_100_T4;
341			else if (anlpar & ANLPAR_TX_FD)
342				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
343			else if (anlpar & ANLPAR_TX)
344				mii->mii_media_active |= IFM_100_TX;
345			else if (anlpar & ANLPAR_10_FD)
346				mii->mii_media_active |= IFM_10_T|IFM_FDX;
347			else if (anlpar & ANLPAR_10)
348				mii->mii_media_active |= IFM_10_T;
349			else
350				mii->mii_media_active |= IFM_NONE;
351			return;
352		}
353
354		/*
355		 * Link partner is not capable of autonegotiation.
356		 * We will never be in full-duplex mode if this is
357		 * the case, so reading the PAR is OK.
358		 */
359		par = PHY_READ(sc, MII_NSPHY_PAR);
360		if (par & PAR_10)
361			mii->mii_media_active |= IFM_10_T;
362		else
363			mii->mii_media_active |= IFM_100_TX;
364#if 0
365		if (par & PAR_FDX)
366			mii->mii_media_active |= IFM_FDX;
367#endif
368	} else
369		mii->mii_media_active = ife->ifm_media;
370}
371
372void
373nsphy_reset(sc)
374	struct mii_softc *sc;
375{
376	int anar;
377
378	mii_phy_reset(sc);
379	anar = PHY_READ(sc, MII_ANAR);
380	anar |= BMSR_MEDIA_TO_ANAR(PHY_READ(sc, MII_BMSR));
381	PHY_WRITE(sc, MII_ANAR, anar);
382}
383