xref: /netbsd-src/sys/dev/mii/nsphyter.c (revision 68e9e41ed4a55e6a93af624260f4c25b4d6dff9e)
1*68e9e41eSthorpej /*	$NetBSD: nsphyter.c,v 1.46 2020/03/28 18:37:18 thorpej Exp $	*/
2b67d4809Sthorpej 
3b67d4809Sthorpej /*-
4996df042Sthorpej  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5b67d4809Sthorpej  * All rights reserved.
6b67d4809Sthorpej  *
7b67d4809Sthorpej  * This code is derived from software contributed to The NetBSD Foundation
8b67d4809Sthorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9b67d4809Sthorpej  * NASA Ames Research Center.
10b67d4809Sthorpej  *
11b67d4809Sthorpej  * Redistribution and use in source and binary forms, with or without
12b67d4809Sthorpej  * modification, are permitted provided that the following conditions
13b67d4809Sthorpej  * are met:
14b67d4809Sthorpej  * 1. Redistributions of source code must retain the above copyright
15b67d4809Sthorpej  *    notice, this list of conditions and the following disclaimer.
16b67d4809Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
17b67d4809Sthorpej  *    notice, this list of conditions and the following disclaimer in the
18b67d4809Sthorpej  *    documentation and/or other materials provided with the distribution.
19b67d4809Sthorpej  *
20b67d4809Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21b67d4809Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22b67d4809Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23b67d4809Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24b67d4809Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25b67d4809Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26b67d4809Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27b67d4809Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28b67d4809Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29b67d4809Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30b67d4809Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
31b67d4809Sthorpej  */
32b67d4809Sthorpej 
33b67d4809Sthorpej /*
34b67d4809Sthorpej  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
35b67d4809Sthorpej  *
36b67d4809Sthorpej  * Redistribution and use in source and binary forms, with or without
37b67d4809Sthorpej  * modification, are permitted provided that the following conditions
38b67d4809Sthorpej  * are met:
39b67d4809Sthorpej  * 1. Redistributions of source code must retain the above copyright
40b67d4809Sthorpej  *    notice, this list of conditions and the following disclaimer.
41b67d4809Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
42b67d4809Sthorpej  *    notice, this list of conditions and the following disclaimer in the
43b67d4809Sthorpej  *    documentation and/or other materials provided with the distribution.
44b67d4809Sthorpej  *
45b67d4809Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46b67d4809Sthorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47b67d4809Sthorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48b67d4809Sthorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49b67d4809Sthorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50b67d4809Sthorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51b67d4809Sthorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52b67d4809Sthorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53b67d4809Sthorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54b67d4809Sthorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55b67d4809Sthorpej  */
56b67d4809Sthorpej 
57b67d4809Sthorpej /*
58b67d4809Sthorpej  * driver for National Semiconductor's DP83843 `PHYTER' ethernet 10/100 PHY
59b67d4809Sthorpej  * Data Sheet available from www.national.com
60996df042Sthorpej  *
61996df042Sthorpej  * We also support the DP83815 MacPHYER internal PHY since, for our
62996df042Sthorpej  * purposes, they are compatible.
63b67d4809Sthorpej  */
64b67d4809Sthorpej 
658b7bb912Slukem #include <sys/cdefs.h>
66*68e9e41eSthorpej __KERNEL_RCSID(0, "$NetBSD: nsphyter.c,v 1.46 2020/03/28 18:37:18 thorpej Exp $");
678b7bb912Slukem 
68b67d4809Sthorpej #include <sys/param.h>
69b67d4809Sthorpej #include <sys/systm.h>
70b67d4809Sthorpej #include <sys/kernel.h>
71b67d4809Sthorpej #include <sys/device.h>
72b67d4809Sthorpej #include <sys/socket.h>
73b67d4809Sthorpej #include <sys/errno.h>
74b67d4809Sthorpej 
75b67d4809Sthorpej #include <net/if.h>
76b67d4809Sthorpej #include <net/if_media.h>
77b67d4809Sthorpej 
78b67d4809Sthorpej #include <dev/mii/mii.h>
79b67d4809Sthorpej #include <dev/mii/miivar.h>
80b67d4809Sthorpej #include <dev/mii/miidevs.h>
81b67d4809Sthorpej 
82b67d4809Sthorpej #include <dev/mii/nsphyterreg.h>
83b67d4809Sthorpej 
847db0e577Sxtraeme static int	nsphytermatch(device_t, cfdata_t, void *);
857db0e577Sxtraeme static void	nsphyterattach(device_t, device_t, void *);
86b67d4809Sthorpej 
87*68e9e41eSthorpej CFATTACH_DECL_NEW(nsphyter, sizeof(struct mii_softc),
88*68e9e41eSthorpej     nsphytermatch, nsphyterattach, mii_phy_detach, mii_phy_activate);
89b67d4809Sthorpej 
901efb3da0Sthorpej static int	nsphyter_service(struct mii_softc *, struct mii_data *, int);
911efb3da0Sthorpej static void	nsphyter_status(struct mii_softc *);
92fae0af79Smsaitoh static void	nsphyter_reset(struct mii_softc *);
93b67d4809Sthorpej 
941efb3da0Sthorpej static const struct mii_phy_funcs nsphyter_funcs = {
95fae0af79Smsaitoh 	nsphyter_service, nsphyter_status, nsphyter_reset,
9649014bf5Sthorpej };
9749014bf5Sthorpej 
981efb3da0Sthorpej static const struct mii_phydesc nsphyters[] = {
997b43da1bSchristos 	MII_PHY_DESC(xxNATSEMI, DP83843),
1007b43da1bSchristos 	MII_PHY_DESC(xxNATSEMI, DP83847),
1017b43da1bSchristos 	MII_PHY_DESC(xxNATSEMI, DP83849),
1027b43da1bSchristos 	MII_PHY_DESC(xxNATSEMI, DP83815),
1037b43da1bSchristos 	MII_PHY_END,
104424f7a1eSthorpej };
105424f7a1eSthorpej 
1061efb3da0Sthorpej static int
nsphytermatch(device_t parent,cfdata_t match,void * aux)1077db0e577Sxtraeme nsphytermatch(device_t parent, cfdata_t match, void *aux)
108b67d4809Sthorpej {
109b67d4809Sthorpej 	struct mii_attach_args *ma = aux;
110b67d4809Sthorpej 
111424f7a1eSthorpej 	if (mii_phy_match(ma, nsphyters) != NULL)
1128e65e831Smsaitoh 		return 10;
113996df042Sthorpej 
1148e65e831Smsaitoh 	return 0;
115b67d4809Sthorpej }
116b67d4809Sthorpej 
1171efb3da0Sthorpej static void
nsphyterattach(device_t parent,device_t self,void * aux)1187db0e577Sxtraeme nsphyterattach(device_t parent, device_t self, void *aux)
119b67d4809Sthorpej {
12044b2bb4cStsutsui 	struct mii_softc *sc = device_private(self);
121b67d4809Sthorpej 	struct mii_attach_args *ma = aux;
122b67d4809Sthorpej 	struct mii_data *mii = ma->mii_data;
123424f7a1eSthorpej 	const struct mii_phydesc *mpd;
124b67d4809Sthorpej 
125424f7a1eSthorpej 	mpd = mii_phy_match(ma, nsphyters);
126c31f87a5Sthorpej 	aprint_naive(": Media interface\n");
127c31f87a5Sthorpej 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
128b67d4809Sthorpej 
1297db0e577Sxtraeme 	sc->mii_dev = self;
130b67d4809Sthorpej 	sc->mii_inst = mii->mii_instance;
131b67d4809Sthorpej 	sc->mii_phy = ma->mii_phyno;
13249014bf5Sthorpej 	sc->mii_funcs = &nsphyter_funcs;
133b67d4809Sthorpej 	sc->mii_pdata = mii;
134b0178985Sthorpej 	sc->mii_flags = ma->mii_flags;
135b67d4809Sthorpej 
1367a9a30c5Sthorpej 	mii_lock(mii);
1377a9a30c5Sthorpej 
13849014bf5Sthorpej 	PHY_RESET(sc);
139b67d4809Sthorpej 
140a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
141a5cdd4b4Smsaitoh 	sc->mii_capabilities &= ma->mii_capmask;
142509697f3Smsaitoh 
1437a9a30c5Sthorpej 	mii_unlock(mii);
1447a9a30c5Sthorpej 
14584dc99fdSthorpej 	mii_phy_add_media(sc);
146b67d4809Sthorpej }
147b67d4809Sthorpej 
1481efb3da0Sthorpej static int
nsphyter_service(struct mii_softc * sc,struct mii_data * mii,int cmd)14989893e42Sthorpej nsphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
150b67d4809Sthorpej {
151b67d4809Sthorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
152a5cdd4b4Smsaitoh 	uint16_t reg;
153b67d4809Sthorpej 
1547a9a30c5Sthorpej 	KASSERT(mii_locked(mii));
1557a9a30c5Sthorpej 
156b67d4809Sthorpej 	switch (cmd) {
157b67d4809Sthorpej 	case MII_POLLSTAT:
1588e65e831Smsaitoh 		/* If we're not polling our PHY instance, just return. */
159b67d4809Sthorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
1608e65e831Smsaitoh 			return 0;
161b67d4809Sthorpej 		break;
162b67d4809Sthorpej 
163b67d4809Sthorpej 	case MII_MEDIACHG:
164b67d4809Sthorpej 		/*
165b67d4809Sthorpej 		 * If the media indicates a different PHY instance,
166b67d4809Sthorpej 		 * isolate ourselves.
167b67d4809Sthorpej 		 */
168b67d4809Sthorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
169a5cdd4b4Smsaitoh 			PHY_READ(sc, MII_BMCR, &reg);
170b67d4809Sthorpej 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
1718e65e831Smsaitoh 			return 0;
172b67d4809Sthorpej 		}
173b67d4809Sthorpej 
1748e65e831Smsaitoh 		/* If the interface is not up, don't do anything. */
175b67d4809Sthorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
176b67d4809Sthorpej 			break;
177b67d4809Sthorpej 
178b67d4809Sthorpej 		mii_phy_setmedia(sc);
179b67d4809Sthorpej 		break;
180b67d4809Sthorpej 
181b67d4809Sthorpej 	case MII_TICK:
1828e65e831Smsaitoh 		/* If we're not currently selected, just return. */
183b67d4809Sthorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
1848e65e831Smsaitoh 			return 0;
185b67d4809Sthorpej 
186ad61d101Sthorpej 		if (mii_phy_tick(sc) == EJUSTRETURN)
1878e65e831Smsaitoh 			return 0;
188b67d4809Sthorpej 		break;
189b67d4809Sthorpej 
190b67d4809Sthorpej 	case MII_DOWN:
191b67d4809Sthorpej 		mii_phy_down(sc);
1928e65e831Smsaitoh 		return 0;
193b67d4809Sthorpej 	}
194b67d4809Sthorpej 
195b67d4809Sthorpej 	/* Update the media status. */
1968923ca0bSthorpej 	mii_phy_status(sc);
197b67d4809Sthorpej 
198b67d4809Sthorpej 	/* Callback if something changed. */
199ad61d101Sthorpej 	mii_phy_update(sc, cmd);
2008e65e831Smsaitoh 	return 0;
201b67d4809Sthorpej }
202b67d4809Sthorpej 
2031efb3da0Sthorpej static void
nsphyter_status(struct mii_softc * sc)20489893e42Sthorpej nsphyter_status(struct mii_softc *sc)
205b67d4809Sthorpej {
206b67d4809Sthorpej 	struct mii_data *mii = sc->mii_pdata;
207b67d4809Sthorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
208a5cdd4b4Smsaitoh 	uint16_t bmsr, bmcr, physts;
209b67d4809Sthorpej 
2107a9a30c5Sthorpej 	KASSERT(mii_locked(mii));
2117a9a30c5Sthorpej 
212b67d4809Sthorpej 	mii->mii_media_status = IFM_AVALID;
213b67d4809Sthorpej 	mii->mii_media_active = IFM_ETHER;
214b67d4809Sthorpej 
215a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
216a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
217a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_NSPHYTER_PHYSTS, &physts);
218b67d4809Sthorpej 
219b67d4809Sthorpej 	if (physts & PHYSTS_LINK)
220b67d4809Sthorpej 		mii->mii_media_status |= IFM_ACTIVE;
221b67d4809Sthorpej 
222a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_BMCR, &bmcr);
223b67d4809Sthorpej 	if (bmcr & BMCR_ISO) {
224b67d4809Sthorpej 		mii->mii_media_active |= IFM_NONE;
225b67d4809Sthorpej 		mii->mii_media_status = 0;
226b67d4809Sthorpej 		return;
227b67d4809Sthorpej 	}
228b67d4809Sthorpej 
229b67d4809Sthorpej 	if (bmcr & BMCR_LOOP)
230b67d4809Sthorpej 		mii->mii_media_active |= IFM_LOOP;
231b67d4809Sthorpej 
232b67d4809Sthorpej 	if (bmcr & BMCR_AUTOEN) {
233b67d4809Sthorpej 		/*
2344a418e8cSmsaitoh 		 * The media status bits are only valid if autonegotiation
235b67d4809Sthorpej 		 * has completed (or it's disabled).
236b67d4809Sthorpej 		 */
237b67d4809Sthorpej 		if ((bmsr & BMSR_ACOMP) == 0) {
238b67d4809Sthorpej 			/* Erg, still trying, I guess... */
239b67d4809Sthorpej 			mii->mii_media_active |= IFM_NONE;
240b67d4809Sthorpej 			return;
241b67d4809Sthorpej 		}
242b67d4809Sthorpej 
243b67d4809Sthorpej 		if (physts & PHYSTS_SPEED10)
244b67d4809Sthorpej 			mii->mii_media_active |= IFM_10_T;
245b67d4809Sthorpej 		else
246b67d4809Sthorpej 			mii->mii_media_active |= IFM_100_TX;
247b211437bSmsaitoh 
248b67d4809Sthorpej 		if (physts & PHYSTS_DUPLEX)
24974543d48Sthorpej 			mii->mii_media_active |=
25074543d48Sthorpej 			    IFM_FDX | mii_phy_flowstatus(sc);
251b211437bSmsaitoh 		else
252b211437bSmsaitoh 			mii->mii_media_active |= IFM_HDX;
253b67d4809Sthorpej 	} else
254b67d4809Sthorpej 		mii->mii_media_active = ife->ifm_media;
255b67d4809Sthorpej }
256fae0af79Smsaitoh 
257a74cc5cdSmsaitoh static void
nsphyter_reset(struct mii_softc * sc)258fae0af79Smsaitoh nsphyter_reset(struct mii_softc *sc)
259fae0af79Smsaitoh {
260a5cdd4b4Smsaitoh 	int i;
261a5cdd4b4Smsaitoh 	uint16_t reg;
262fae0af79Smsaitoh 
2637a9a30c5Sthorpej 	KASSERT(mii_locked(sc->mii_pdata));
2647a9a30c5Sthorpej 
265fae0af79Smsaitoh 	if (sc->mii_flags & MIIF_NOISOLATE)
266fae0af79Smsaitoh 		reg = BMCR_RESET;
267fae0af79Smsaitoh 	else
268fae0af79Smsaitoh 		reg = BMCR_RESET | BMCR_ISO;
269fae0af79Smsaitoh 	PHY_WRITE(sc, MII_BMCR, reg);
270fae0af79Smsaitoh 
271fae0af79Smsaitoh 	/*
272fae0af79Smsaitoh 	 * It is best to allow a little time for the reset to settle
273fae0af79Smsaitoh 	 * in before we start polling the BMCR again.  Notably, the
274fae0af79Smsaitoh 	 * DP83840A manual states that there should be a 500us delay
275fae0af79Smsaitoh 	 * between asserting software reset and attempting MII serial
276fae0af79Smsaitoh 	 * operations.  Also, a DP83815 can get into a bad state on
277fae0af79Smsaitoh 	 * cable removal and reinsertion if we do not delay here.
278fae0af79Smsaitoh 	 */
279fae0af79Smsaitoh 	delay(500);
280fae0af79Smsaitoh 
281fae0af79Smsaitoh 	/* Wait another 100ms for it to complete. */
282fae0af79Smsaitoh 	for (i = 0; i < 100; i++) {
283a5cdd4b4Smsaitoh 		PHY_READ(sc, MII_BMCR, &reg);
284fae0af79Smsaitoh 		if ((reg & BMCR_RESET) == 0)
285fae0af79Smsaitoh 			break;
286fae0af79Smsaitoh 		delay(1000);
287fae0af79Smsaitoh 	}
288fae0af79Smsaitoh 
289fae0af79Smsaitoh 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
290fae0af79Smsaitoh 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
291fae0af79Smsaitoh }
292