xref: /freebsd/sys/dev/mii/nsphy.c (revision eebd9d53665e1b691ffada910b940b37b6f62cc4)
1d0027533SBill Paul /*	$NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 thorpej Exp $	*/
2d0027533SBill Paul 
3d0027533SBill Paul /*-
4*eebd9d53SWarner 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>
6050aa1061SMarius Strobl __FBSDID("$FreeBSD$");
6150aa1061SMarius Strobl 
62d0027533SBill Paul /*
63d0027533SBill Paul  * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
64d0027533SBill Paul  * Data Sheet available from www.national.com
65d0027533SBill Paul  */
66d0027533SBill Paul 
67d0027533SBill Paul #include <sys/param.h>
68d0027533SBill Paul #include <sys/systm.h>
69d0027533SBill Paul #include <sys/kernel.h>
70d0027533SBill Paul #include <sys/socket.h>
71d0027533SBill Paul #include <sys/errno.h>
72d0027533SBill Paul #include <sys/module.h>
73d0027533SBill Paul #include <sys/bus.h>
74d0027533SBill Paul 
75d0027533SBill Paul #include <net/if.h>
7661a3ac6eSGleb Smirnoff #include <net/if_var.h>
77d0027533SBill Paul #include <net/if_media.h>
78d0027533SBill Paul 
79d0027533SBill Paul #include <dev/mii/mii.h>
80d0027533SBill Paul #include <dev/mii/miivar.h>
812d3ce713SDavid E. O'Brien #include "miidevs.h"
82d0027533SBill Paul 
83d0027533SBill Paul #include <dev/mii/nsphyreg.h>
84d0027533SBill Paul 
85d0027533SBill Paul #include "miibus_if.h"
86d0027533SBill Paul 
87e51a25f8SAlfred Perlstein static int nsphy_probe(device_t);
88e51a25f8SAlfred Perlstein static int nsphy_attach(device_t);
89d0027533SBill Paul 
90d0027533SBill Paul static device_method_t nsphy_methods[] = {
91d0027533SBill Paul 	/* device interface */
92d0027533SBill Paul 	DEVMETHOD(device_probe,		nsphy_probe),
93d0027533SBill Paul 	DEVMETHOD(device_attach,	nsphy_attach),
94279fe8d1SPoul-Henning Kamp 	DEVMETHOD(device_detach,	mii_phy_detach),
95d0027533SBill Paul 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
96604f5f1fSMarius Strobl 	DEVMETHOD_END
97d0027533SBill Paul };
98d0027533SBill Paul 
99d0027533SBill Paul static driver_t nsphy_driver = {
100d0027533SBill Paul 	"nsphy",
101d0027533SBill Paul 	nsphy_methods,
102d0027533SBill Paul 	sizeof(struct mii_softc)
103d0027533SBill Paul };
104d0027533SBill Paul 
105f438c2ffSJohn Baldwin DRIVER_MODULE(nsphy, miibus, nsphy_driver, 0, 0);
106d0027533SBill Paul 
107e51a25f8SAlfred Perlstein static int	nsphy_service(struct mii_softc *, struct mii_data *, int);
108e51a25f8SAlfred Perlstein static void	nsphy_status(struct mii_softc *);
109fb58dc87SMarius Strobl static void	nsphy_reset(struct mii_softc *);
110d0027533SBill Paul 
111a35b9333SMarius Strobl static const struct mii_phydesc nsphys[] = {
1123fcb7a53SMarius Strobl 	MII_PHY_DESC(xxNATSEMI, DP83840),
113a35b9333SMarius Strobl 	MII_PHY_END
114a35b9333SMarius Strobl };
115a35b9333SMarius Strobl 
1163fcb7a53SMarius Strobl static const struct mii_phy_funcs nsphy_funcs = {
1173fcb7a53SMarius Strobl 	nsphy_service,
1183fcb7a53SMarius Strobl 	nsphy_status,
1193fcb7a53SMarius Strobl 	nsphy_reset
1203fcb7a53SMarius Strobl };
1213fcb7a53SMarius Strobl 
1229c1c2e99SAlfred Perlstein static int
1237d830ac9SWarner Losh nsphy_probe(device_t dev)
124d0027533SBill Paul {
125d0027533SBill Paul 
126a35b9333SMarius Strobl 	return (mii_phy_dev_probe(dev, nsphys, BUS_PROBE_DEFAULT));
127d0027533SBill Paul }
128d0027533SBill Paul 
1299c1c2e99SAlfred Perlstein static int
1307d830ac9SWarner Losh nsphy_attach(device_t dev)
131d0027533SBill Paul {
132d0027533SBill Paul 
1332f00fe72SMarius Strobl 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &nsphy_funcs, 1);
134d0027533SBill Paul 	return (0);
135d0027533SBill Paul }
136d0027533SBill Paul 
137d9730b8bSJonathan Lemon static int
1387d830ac9SWarner Losh nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
139d0027533SBill Paul {
140d0027533SBill Paul 	int reg;
141d0027533SBill Paul 
142d0027533SBill Paul 	switch (cmd) {
143d0027533SBill Paul 	case MII_POLLSTAT:
144d0027533SBill Paul 		break;
145d0027533SBill Paul 
146d0027533SBill Paul 	case MII_MEDIACHG:
147d0027533SBill Paul 		reg = PHY_READ(sc, MII_NSPHY_PCR);
148d0027533SBill Paul 
149d0027533SBill Paul 		/*
150d0027533SBill Paul 		 * Set up the PCR to use LED4 to indicate full-duplex
151d0027533SBill Paul 		 * in both 10baseT and 100baseTX modes.
152d0027533SBill Paul 		 */
153d0027533SBill Paul 		reg |= PCR_LED4MODE;
154d0027533SBill Paul 
155d0027533SBill Paul 		/*
1565aa54809SMarius Strobl 		 * Make sure Carrier Integrity Monitor function is
157d0027533SBill Paul 		 * disabled (normal for Node operation, but sometimes
158d0027533SBill Paul 		 * it's not set?!)
159d0027533SBill Paul 		 */
160d0027533SBill Paul 		reg |= PCR_CIMDIS;
161d0027533SBill Paul 
162d0027533SBill Paul 		/*
163d0027533SBill Paul 		 * Make sure "force link good" is set to normal mode.
164d0027533SBill Paul 		 * It's only intended for debugging.
165d0027533SBill Paul 		 */
166d0027533SBill Paul 		reg |= PCR_FLINK100;
167d0027533SBill Paul 
168d0027533SBill Paul 		/*
169d0027533SBill Paul 		 * Mystery bits which are supposedly `reserved',
170d0027533SBill Paul 		 * but we seem to need to set them when the PHY
17132de04d5SJonathan Lemon 		 * is connected to some interfaces:
17232de04d5SJonathan Lemon 		 *
17332de04d5SJonathan Lemon 		 * 0x0400 is needed for fxp
17432de04d5SJonathan Lemon 		 *        (Intel EtherExpress Pro 10+/100B, 82557 chip)
17532de04d5SJonathan Lemon 		 *        (nsphy with a DP83840 chip)
17632de04d5SJonathan Lemon 		 * 0x0100 may be needed for some other card
177d0027533SBill Paul 		 */
178d0027533SBill Paul 		reg |= 0x0100 | 0x0400;
17932de04d5SJonathan Lemon 
1807e310d2dSGleb Smirnoff 		if (mii_phy_mac_match(sc, "fxp"))
181d0027533SBill Paul 			PHY_WRITE(sc, MII_NSPHY_PCR, reg);
18232de04d5SJonathan Lemon 
183fb58dc87SMarius Strobl 		mii_phy_setmedia(sc);
184d0027533SBill Paul 		break;
185d0027533SBill Paul 
186d0027533SBill Paul 	case MII_TICK:
187d9730b8bSJonathan Lemon 		if (mii_phy_tick(sc) == EJUSTRETURN)
188d0027533SBill Paul 			return (0);
189d0027533SBill Paul 		break;
190d0027533SBill Paul 	}
191d0027533SBill Paul 
192d0027533SBill Paul 	/* Update the media status. */
1933fcb7a53SMarius Strobl 	PHY_STATUS(sc);
194d0027533SBill Paul 
195d0027533SBill Paul 	/* Callback if something changed. */
196d9730b8bSJonathan Lemon 	mii_phy_update(sc, cmd);
197d0027533SBill Paul 	return (0);
198d0027533SBill Paul }
199d0027533SBill Paul 
200d9730b8bSJonathan Lemon static void
2017d830ac9SWarner Losh nsphy_status(struct mii_softc *sc)
202d0027533SBill Paul {
203d0027533SBill Paul 	struct mii_data *mii = sc->mii_pdata;
204fb58dc87SMarius Strobl 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
205d0027533SBill Paul 	int bmsr, bmcr, par, anlpar;
206d0027533SBill Paul 
207d0027533SBill Paul 	mii->mii_media_status = IFM_AVALID;
208d0027533SBill Paul 	mii->mii_media_active = IFM_ETHER;
209d0027533SBill Paul 
210d0027533SBill Paul 	bmsr = PHY_READ(sc, MII_BMSR) |
211d0027533SBill Paul 	    PHY_READ(sc, MII_BMSR);
212d0027533SBill Paul 	if (bmsr & BMSR_LINK)
213d0027533SBill Paul 		mii->mii_media_status |= IFM_ACTIVE;
214d0027533SBill Paul 
215d0027533SBill Paul 	bmcr = PHY_READ(sc, MII_BMCR);
216d0027533SBill Paul 	if (bmcr & BMCR_ISO) {
217d0027533SBill Paul 		mii->mii_media_active |= IFM_NONE;
218d0027533SBill Paul 		mii->mii_media_status = 0;
219d0027533SBill Paul 		return;
220d0027533SBill Paul 	}
221d0027533SBill Paul 
222d0027533SBill Paul 	if (bmcr & BMCR_LOOP)
223d0027533SBill Paul 		mii->mii_media_active |= IFM_LOOP;
224d0027533SBill Paul 
225d0027533SBill Paul 	if (bmcr & BMCR_AUTOEN) {
226d0027533SBill Paul 		/*
22782f358ffSMarius Strobl 		 * The PAR status bits are only valid if autonegotiation
228d0027533SBill Paul 		 * has completed (or it's disabled).
229d0027533SBill Paul 		 */
230d0027533SBill Paul 		if ((bmsr & BMSR_ACOMP) == 0) {
231d0027533SBill Paul 			/* Erg, still trying, I guess... */
232d0027533SBill Paul 			mii->mii_media_active |= IFM_NONE;
233d0027533SBill Paul 			return;
234d0027533SBill Paul 		}
235d0027533SBill Paul 
236d0027533SBill Paul 		/*
237d0027533SBill Paul 		 * Argh.  The PAR doesn't seem to indicate duplex mode
238d0027533SBill Paul 		 * properly!  Determine media based on link partner's
239d0027533SBill Paul 		 * advertised capabilities.
240d0027533SBill Paul 		 */
241d0027533SBill Paul 		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
242d0027533SBill Paul 			anlpar = PHY_READ(sc, MII_ANAR) &
243d0027533SBill Paul 			    PHY_READ(sc, MII_ANLPAR);
244d612cc59SPyun YongHyeon 			if (anlpar & ANLPAR_TX_FD)
245d0027533SBill Paul 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
246d612cc59SPyun YongHyeon 			else if (anlpar & ANLPAR_T4)
247d7a9ad56SMarius Strobl 				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
248d0027533SBill Paul 			else if (anlpar & ANLPAR_TX)
249d7a9ad56SMarius Strobl 				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
250d0027533SBill Paul 			else if (anlpar & ANLPAR_10_FD)
251d0027533SBill Paul 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
252d0027533SBill Paul 			else if (anlpar & ANLPAR_10)
253d7a9ad56SMarius Strobl 				mii->mii_media_active |= IFM_10_T|IFM_HDX;
254d0027533SBill Paul 			else
255d0027533SBill Paul 				mii->mii_media_active |= IFM_NONE;
2563fcb7a53SMarius Strobl 			if ((mii->mii_media_active & IFM_FDX) != 0)
2573fcb7a53SMarius Strobl 				mii->mii_media_active |=
2583fcb7a53SMarius Strobl 				    mii_phy_flowstatus(sc);
259d0027533SBill Paul 			return;
260d0027533SBill Paul 		}
261d0027533SBill Paul 
262d0027533SBill Paul 		/*
263d0027533SBill Paul 		 * Link partner is not capable of autonegotiation.
264d0027533SBill Paul 		 * We will never be in full-duplex mode if this is
265d0027533SBill Paul 		 * the case, so reading the PAR is OK.
266d0027533SBill Paul 		 */
267d0027533SBill Paul 		par = PHY_READ(sc, MII_NSPHY_PAR);
268d0027533SBill Paul 		if (par & PAR_10)
269d0027533SBill Paul 			mii->mii_media_active |= IFM_10_T;
270d0027533SBill Paul 		else
271d0027533SBill Paul 			mii->mii_media_active |= IFM_100_TX;
272d7a9ad56SMarius Strobl 		mii->mii_media_active |= IFM_HDX;
273d0027533SBill Paul 	} else
274fb58dc87SMarius Strobl 		mii->mii_media_active = ife->ifm_media;
275fb58dc87SMarius Strobl }
276fb58dc87SMarius Strobl 
277fb58dc87SMarius Strobl static void
278fb58dc87SMarius Strobl nsphy_reset(struct mii_softc *sc)
279fb58dc87SMarius Strobl {
280fb58dc87SMarius Strobl 	struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
281fb58dc87SMarius Strobl 	int reg, i;
282fb58dc87SMarius Strobl 
283fb58dc87SMarius Strobl 	if (sc->mii_flags & MIIF_NOISOLATE)
284fb58dc87SMarius Strobl 		reg = BMCR_RESET;
285fb58dc87SMarius Strobl 	else
286fb58dc87SMarius Strobl 		reg = BMCR_RESET | BMCR_ISO;
287fb58dc87SMarius Strobl 	PHY_WRITE(sc, MII_BMCR, reg);
288fb58dc87SMarius Strobl 
289fb58dc87SMarius Strobl 	/*
29082f358ffSMarius Strobl 	 * It is best to allow a little time for the reset to settle
29182f358ffSMarius Strobl 	 * in before we start polling the BMCR again.  Notably, the
29282f358ffSMarius Strobl 	 * DP83840A manuals state that there should be a 500us delay
29382f358ffSMarius Strobl 	 * between asserting software reset and attempting MII serial
29482f358ffSMarius Strobl 	 * operations.  Be conservative.
295fb58dc87SMarius Strobl 	 */
296fb58dc87SMarius Strobl 	DELAY(1000);
297fb58dc87SMarius Strobl 
298fb58dc87SMarius Strobl 	/*
299fb58dc87SMarius Strobl 	 * Wait another 2s for it to complete.
300fb58dc87SMarius Strobl 	 * This is only a little overkill as under normal circumstances
301fb58dc87SMarius Strobl 	 * the PHY can take up to 1s to complete reset.
302fb58dc87SMarius Strobl 	 * This is also a bit odd because after a reset, the BMCR will
303fb58dc87SMarius Strobl 	 * clear the reset bit and simply reports 0 even though the reset
304fb58dc87SMarius Strobl 	 * is not yet complete.
305fb58dc87SMarius Strobl 	 */
306fb58dc87SMarius Strobl 	for (i = 0; i < 1000; i++) {
307fb58dc87SMarius Strobl 		reg = PHY_READ(sc, MII_BMCR);
308fb58dc87SMarius Strobl 		if (reg != 0 && (reg & BMCR_RESET) == 0)
309fb58dc87SMarius Strobl 			break;
310fb58dc87SMarius Strobl 		DELAY(2000);
311fb58dc87SMarius Strobl 	}
312fb58dc87SMarius Strobl 
313fb58dc87SMarius Strobl 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
314fb58dc87SMarius Strobl 		if ((ife == NULL && sc->mii_inst != 0) ||
315fb58dc87SMarius Strobl 		    (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
316fb58dc87SMarius Strobl 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
317fb58dc87SMarius Strobl 	}
318d0027533SBill Paul }
319