xref: /freebsd/sys/dev/mii/nsphy.c (revision 685dc743)
1d0027533SBill Paul /*	$NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 thorpej Exp $	*/
2d0027533SBill Paul 
3d0027533SBill Paul /*-
4eebd9d53SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5718cf2ccSPedro F. Giffuni  *
6d0027533SBill Paul  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
7d0027533SBill Paul  * All rights reserved.
8d0027533SBill Paul  *
9d0027533SBill Paul  * This code is derived from software contributed to The NetBSD Foundation
10d0027533SBill Paul  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11d0027533SBill Paul  * NASA Ames Research Center.
12d0027533SBill Paul  *
13d0027533SBill Paul  * Redistribution and use in source and binary forms, with or without
14d0027533SBill Paul  * modification, are permitted provided that the following conditions
15d0027533SBill Paul  * are met:
16d0027533SBill Paul  * 1. Redistributions of source code must retain the above copyright
17d0027533SBill Paul  *    notice, this list of conditions and the following disclaimer.
18d0027533SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
19d0027533SBill Paul  *    notice, this list of conditions and the following disclaimer in the
20d0027533SBill Paul  *    documentation and/or other materials provided with the distribution.
21d0027533SBill Paul  *
22d0027533SBill Paul  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23d0027533SBill Paul  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24d0027533SBill Paul  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25d0027533SBill Paul  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26d0027533SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27d0027533SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28d0027533SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29d0027533SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30d0027533SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31d0027533SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32d0027533SBill Paul  * POSSIBILITY OF SUCH DAMAGE.
33d0027533SBill Paul  */
34d0027533SBill Paul 
35098ca2bdSWarner Losh /*-
36d0027533SBill Paul  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
37d0027533SBill Paul  *
38d0027533SBill Paul  * Redistribution and use in source and binary forms, with or without
39d0027533SBill Paul  * modification, are permitted provided that the following conditions
40d0027533SBill Paul  * are met:
41d0027533SBill Paul  * 1. Redistributions of source code must retain the above copyright
42d0027533SBill Paul  *    notice, this list of conditions and the following disclaimer.
43d0027533SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
44d0027533SBill Paul  *    notice, this list of conditions and the following disclaimer in the
45d0027533SBill Paul  *    documentation and/or other materials provided with the distribution.
46d0027533SBill Paul  *
47d0027533SBill Paul  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48d0027533SBill Paul  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49d0027533SBill Paul  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50d0027533SBill Paul  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51d0027533SBill Paul  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52d0027533SBill Paul  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53d0027533SBill Paul  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54d0027533SBill Paul  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55d0027533SBill Paul  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56d0027533SBill Paul  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57d0027533SBill Paul  */
58d0027533SBill Paul 
5950aa1061SMarius Strobl #include <sys/cdefs.h>
60d0027533SBill Paul /*
61d0027533SBill Paul  * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
62d0027533SBill Paul  * Data Sheet available from www.national.com
63d0027533SBill Paul  */
64d0027533SBill Paul 
65d0027533SBill Paul #include <sys/param.h>
66d0027533SBill Paul #include <sys/systm.h>
67d0027533SBill Paul #include <sys/kernel.h>
68d0027533SBill Paul #include <sys/socket.h>
69d0027533SBill Paul #include <sys/errno.h>
70d0027533SBill Paul #include <sys/module.h>
71d0027533SBill Paul #include <sys/bus.h>
72d0027533SBill Paul 
73d0027533SBill Paul #include <net/if.h>
7461a3ac6eSGleb Smirnoff #include <net/if_var.h>
75d0027533SBill Paul #include <net/if_media.h>
76d0027533SBill Paul 
77d0027533SBill Paul #include <dev/mii/mii.h>
78d0027533SBill Paul #include <dev/mii/miivar.h>
792d3ce713SDavid E. O'Brien #include "miidevs.h"
80d0027533SBill Paul 
81d0027533SBill Paul #include <dev/mii/nsphyreg.h>
82d0027533SBill Paul 
83d0027533SBill Paul #include "miibus_if.h"
84d0027533SBill Paul 
85e51a25f8SAlfred Perlstein static int nsphy_probe(device_t);
86e51a25f8SAlfred Perlstein static int nsphy_attach(device_t);
87d0027533SBill Paul 
88d0027533SBill Paul static device_method_t nsphy_methods[] = {
89d0027533SBill Paul 	/* device interface */
90d0027533SBill Paul 	DEVMETHOD(device_probe,		nsphy_probe),
91d0027533SBill Paul 	DEVMETHOD(device_attach,	nsphy_attach),
92279fe8d1SPoul-Henning Kamp 	DEVMETHOD(device_detach,	mii_phy_detach),
93d0027533SBill Paul 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
94604f5f1fSMarius Strobl 	DEVMETHOD_END
95d0027533SBill Paul };
96d0027533SBill Paul 
97d0027533SBill Paul static driver_t nsphy_driver = {
98d0027533SBill Paul 	"nsphy",
99d0027533SBill Paul 	nsphy_methods,
100d0027533SBill Paul 	sizeof(struct mii_softc)
101d0027533SBill Paul };
102d0027533SBill Paul 
103f438c2ffSJohn Baldwin DRIVER_MODULE(nsphy, miibus, nsphy_driver, 0, 0);
104d0027533SBill Paul 
105e51a25f8SAlfred Perlstein static int	nsphy_service(struct mii_softc *, struct mii_data *, int);
106e51a25f8SAlfred Perlstein static void	nsphy_status(struct mii_softc *);
107fb58dc87SMarius Strobl static void	nsphy_reset(struct mii_softc *);
108d0027533SBill Paul 
109a35b9333SMarius Strobl static const struct mii_phydesc nsphys[] = {
1103fcb7a53SMarius Strobl 	MII_PHY_DESC(xxNATSEMI, DP83840),
111a35b9333SMarius Strobl 	MII_PHY_END
112a35b9333SMarius Strobl };
113a35b9333SMarius Strobl 
1143fcb7a53SMarius Strobl static const struct mii_phy_funcs nsphy_funcs = {
1153fcb7a53SMarius Strobl 	nsphy_service,
1163fcb7a53SMarius Strobl 	nsphy_status,
1173fcb7a53SMarius Strobl 	nsphy_reset
1183fcb7a53SMarius Strobl };
1193fcb7a53SMarius Strobl 
1209c1c2e99SAlfred Perlstein static int
nsphy_probe(device_t dev)1217d830ac9SWarner Losh nsphy_probe(device_t dev)
122d0027533SBill Paul {
123d0027533SBill Paul 
124a35b9333SMarius Strobl 	return (mii_phy_dev_probe(dev, nsphys, BUS_PROBE_DEFAULT));
125d0027533SBill Paul }
126d0027533SBill Paul 
1279c1c2e99SAlfred Perlstein static int
nsphy_attach(device_t dev)1287d830ac9SWarner Losh nsphy_attach(device_t dev)
129d0027533SBill Paul {
130d0027533SBill Paul 
1312f00fe72SMarius Strobl 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &nsphy_funcs, 1);
132d0027533SBill Paul 	return (0);
133d0027533SBill Paul }
134d0027533SBill Paul 
135d9730b8bSJonathan Lemon static int
nsphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)1367d830ac9SWarner Losh nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
137d0027533SBill Paul {
138d0027533SBill Paul 	int reg;
139d0027533SBill Paul 
140d0027533SBill Paul 	switch (cmd) {
141d0027533SBill Paul 	case MII_POLLSTAT:
142d0027533SBill Paul 		break;
143d0027533SBill Paul 
144d0027533SBill Paul 	case MII_MEDIACHG:
145d0027533SBill Paul 		reg = PHY_READ(sc, MII_NSPHY_PCR);
146d0027533SBill Paul 
147d0027533SBill Paul 		/*
148d0027533SBill Paul 		 * Set up the PCR to use LED4 to indicate full-duplex
149d0027533SBill Paul 		 * in both 10baseT and 100baseTX modes.
150d0027533SBill Paul 		 */
151d0027533SBill Paul 		reg |= PCR_LED4MODE;
152d0027533SBill Paul 
153d0027533SBill Paul 		/*
1545aa54809SMarius Strobl 		 * Make sure Carrier Integrity Monitor function is
155d0027533SBill Paul 		 * disabled (normal for Node operation, but sometimes
156d0027533SBill Paul 		 * it's not set?!)
157d0027533SBill Paul 		 */
158d0027533SBill Paul 		reg |= PCR_CIMDIS;
159d0027533SBill Paul 
160d0027533SBill Paul 		/*
161d0027533SBill Paul 		 * Make sure "force link good" is set to normal mode.
162d0027533SBill Paul 		 * It's only intended for debugging.
163d0027533SBill Paul 		 */
164d0027533SBill Paul 		reg |= PCR_FLINK100;
165d0027533SBill Paul 
166d0027533SBill Paul 		/*
167d0027533SBill Paul 		 * Mystery bits which are supposedly `reserved',
168d0027533SBill Paul 		 * but we seem to need to set them when the PHY
16932de04d5SJonathan Lemon 		 * is connected to some interfaces:
17032de04d5SJonathan Lemon 		 *
17132de04d5SJonathan Lemon 		 * 0x0400 is needed for fxp
17232de04d5SJonathan Lemon 		 *        (Intel EtherExpress Pro 10+/100B, 82557 chip)
17332de04d5SJonathan Lemon 		 *        (nsphy with a DP83840 chip)
17432de04d5SJonathan Lemon 		 * 0x0100 may be needed for some other card
175d0027533SBill Paul 		 */
176d0027533SBill Paul 		reg |= 0x0100 | 0x0400;
17732de04d5SJonathan Lemon 
1787e310d2dSGleb Smirnoff 		if (mii_phy_mac_match(sc, "fxp"))
179d0027533SBill Paul 			PHY_WRITE(sc, MII_NSPHY_PCR, reg);
18032de04d5SJonathan Lemon 
181fb58dc87SMarius Strobl 		mii_phy_setmedia(sc);
182d0027533SBill Paul 		break;
183d0027533SBill Paul 
184d0027533SBill Paul 	case MII_TICK:
185d9730b8bSJonathan Lemon 		if (mii_phy_tick(sc) == EJUSTRETURN)
186d0027533SBill Paul 			return (0);
187d0027533SBill Paul 		break;
188d0027533SBill Paul 	}
189d0027533SBill Paul 
190d0027533SBill Paul 	/* Update the media status. */
1913fcb7a53SMarius Strobl 	PHY_STATUS(sc);
192d0027533SBill Paul 
193d0027533SBill Paul 	/* Callback if something changed. */
194d9730b8bSJonathan Lemon 	mii_phy_update(sc, cmd);
195d0027533SBill Paul 	return (0);
196d0027533SBill Paul }
197d0027533SBill Paul 
198d9730b8bSJonathan Lemon static void
nsphy_status(struct mii_softc * sc)1997d830ac9SWarner Losh nsphy_status(struct mii_softc *sc)
200d0027533SBill Paul {
201d0027533SBill Paul 	struct mii_data *mii = sc->mii_pdata;
202fb58dc87SMarius Strobl 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
203d0027533SBill Paul 	int bmsr, bmcr, par, anlpar;
204d0027533SBill Paul 
205d0027533SBill Paul 	mii->mii_media_status = IFM_AVALID;
206d0027533SBill Paul 	mii->mii_media_active = IFM_ETHER;
207d0027533SBill Paul 
208d0027533SBill Paul 	bmsr = PHY_READ(sc, MII_BMSR) |
209d0027533SBill Paul 	    PHY_READ(sc, MII_BMSR);
210d0027533SBill Paul 	if (bmsr & BMSR_LINK)
211d0027533SBill Paul 		mii->mii_media_status |= IFM_ACTIVE;
212d0027533SBill Paul 
213d0027533SBill Paul 	bmcr = PHY_READ(sc, MII_BMCR);
214d0027533SBill Paul 	if (bmcr & BMCR_ISO) {
215d0027533SBill Paul 		mii->mii_media_active |= IFM_NONE;
216d0027533SBill Paul 		mii->mii_media_status = 0;
217d0027533SBill Paul 		return;
218d0027533SBill Paul 	}
219d0027533SBill Paul 
220d0027533SBill Paul 	if (bmcr & BMCR_LOOP)
221d0027533SBill Paul 		mii->mii_media_active |= IFM_LOOP;
222d0027533SBill Paul 
223d0027533SBill Paul 	if (bmcr & BMCR_AUTOEN) {
224d0027533SBill Paul 		/*
22582f358ffSMarius Strobl 		 * The PAR status bits are only valid if autonegotiation
226d0027533SBill Paul 		 * has completed (or it's disabled).
227d0027533SBill Paul 		 */
228d0027533SBill Paul 		if ((bmsr & BMSR_ACOMP) == 0) {
229d0027533SBill Paul 			/* Erg, still trying, I guess... */
230d0027533SBill Paul 			mii->mii_media_active |= IFM_NONE;
231d0027533SBill Paul 			return;
232d0027533SBill Paul 		}
233d0027533SBill Paul 
234d0027533SBill Paul 		/*
235d0027533SBill Paul 		 * Argh.  The PAR doesn't seem to indicate duplex mode
236d0027533SBill Paul 		 * properly!  Determine media based on link partner's
237d0027533SBill Paul 		 * advertised capabilities.
238d0027533SBill Paul 		 */
239d0027533SBill Paul 		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
240d0027533SBill Paul 			anlpar = PHY_READ(sc, MII_ANAR) &
241d0027533SBill Paul 			    PHY_READ(sc, MII_ANLPAR);
242d612cc59SPyun YongHyeon 			if (anlpar & ANLPAR_TX_FD)
243d0027533SBill Paul 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
244d612cc59SPyun YongHyeon 			else if (anlpar & ANLPAR_T4)
245d7a9ad56SMarius Strobl 				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
246d0027533SBill Paul 			else if (anlpar & ANLPAR_TX)
247d7a9ad56SMarius Strobl 				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
248d0027533SBill Paul 			else if (anlpar & ANLPAR_10_FD)
249d0027533SBill Paul 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
250d0027533SBill Paul 			else if (anlpar & ANLPAR_10)
251d7a9ad56SMarius Strobl 				mii->mii_media_active |= IFM_10_T|IFM_HDX;
252d0027533SBill Paul 			else
253d0027533SBill Paul 				mii->mii_media_active |= IFM_NONE;
2543fcb7a53SMarius Strobl 			if ((mii->mii_media_active & IFM_FDX) != 0)
2553fcb7a53SMarius Strobl 				mii->mii_media_active |=
2563fcb7a53SMarius Strobl 				    mii_phy_flowstatus(sc);
257d0027533SBill Paul 			return;
258d0027533SBill Paul 		}
259d0027533SBill Paul 
260d0027533SBill Paul 		/*
261d0027533SBill Paul 		 * Link partner is not capable of autonegotiation.
262d0027533SBill Paul 		 * We will never be in full-duplex mode if this is
263d0027533SBill Paul 		 * the case, so reading the PAR is OK.
264d0027533SBill Paul 		 */
265d0027533SBill Paul 		par = PHY_READ(sc, MII_NSPHY_PAR);
266d0027533SBill Paul 		if (par & PAR_10)
267d0027533SBill Paul 			mii->mii_media_active |= IFM_10_T;
268d0027533SBill Paul 		else
269d0027533SBill Paul 			mii->mii_media_active |= IFM_100_TX;
270d7a9ad56SMarius Strobl 		mii->mii_media_active |= IFM_HDX;
271d0027533SBill Paul 	} else
272fb58dc87SMarius Strobl 		mii->mii_media_active = ife->ifm_media;
273fb58dc87SMarius Strobl }
274fb58dc87SMarius Strobl 
275fb58dc87SMarius Strobl static void
nsphy_reset(struct mii_softc * sc)276fb58dc87SMarius Strobl nsphy_reset(struct mii_softc *sc)
277fb58dc87SMarius Strobl {
278fb58dc87SMarius Strobl 	struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
279fb58dc87SMarius Strobl 	int reg, i;
280fb58dc87SMarius Strobl 
281fb58dc87SMarius Strobl 	if (sc->mii_flags & MIIF_NOISOLATE)
282fb58dc87SMarius Strobl 		reg = BMCR_RESET;
283fb58dc87SMarius Strobl 	else
284fb58dc87SMarius Strobl 		reg = BMCR_RESET | BMCR_ISO;
285fb58dc87SMarius Strobl 	PHY_WRITE(sc, MII_BMCR, reg);
286fb58dc87SMarius Strobl 
287fb58dc87SMarius Strobl 	/*
28882f358ffSMarius Strobl 	 * It is best to allow a little time for the reset to settle
28982f358ffSMarius Strobl 	 * in before we start polling the BMCR again.  Notably, the
29082f358ffSMarius Strobl 	 * DP83840A manuals state that there should be a 500us delay
29182f358ffSMarius Strobl 	 * between asserting software reset and attempting MII serial
29282f358ffSMarius Strobl 	 * operations.  Be conservative.
293fb58dc87SMarius Strobl 	 */
294fb58dc87SMarius Strobl 	DELAY(1000);
295fb58dc87SMarius Strobl 
296fb58dc87SMarius Strobl 	/*
297fb58dc87SMarius Strobl 	 * Wait another 2s for it to complete.
298fb58dc87SMarius Strobl 	 * This is only a little overkill as under normal circumstances
299fb58dc87SMarius Strobl 	 * the PHY can take up to 1s to complete reset.
300fb58dc87SMarius Strobl 	 * This is also a bit odd because after a reset, the BMCR will
301fb58dc87SMarius Strobl 	 * clear the reset bit and simply reports 0 even though the reset
302fb58dc87SMarius Strobl 	 * is not yet complete.
303fb58dc87SMarius Strobl 	 */
304fb58dc87SMarius Strobl 	for (i = 0; i < 1000; i++) {
305fb58dc87SMarius Strobl 		reg = PHY_READ(sc, MII_BMCR);
306fb58dc87SMarius Strobl 		if (reg != 0 && (reg & BMCR_RESET) == 0)
307fb58dc87SMarius Strobl 			break;
308fb58dc87SMarius Strobl 		DELAY(2000);
309fb58dc87SMarius Strobl 	}
310fb58dc87SMarius Strobl 
311fb58dc87SMarius Strobl 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
312fb58dc87SMarius Strobl 		if ((ife == NULL && sc->mii_inst != 0) ||
313fb58dc87SMarius Strobl 		    (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
314fb58dc87SMarius Strobl 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
315fb58dc87SMarius Strobl 	}
316d0027533SBill Paul }
317