xref: /freebsd/sys/dev/mii/nsgphy.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1098ca2bdSWarner Losh /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3718cf2ccSPedro F. Giffuni  *
4ce4946daSBill Paul  * Copyright (c) 2001 Wind River Systems
5ce4946daSBill Paul  * Copyright (c) 2001
6ce4946daSBill Paul  *	Bill Paul <wpaul@bsdi.com>.  All rights reserved.
778c8c3dbSPoul-Henning Kamp  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
878c8c3dbSPoul-Henning Kamp  * All rights reserved.
978c8c3dbSPoul-Henning Kamp  *
1078c8c3dbSPoul-Henning Kamp  * This code is derived from software contributed to The NetBSD Foundation
1178c8c3dbSPoul-Henning Kamp  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
1278c8c3dbSPoul-Henning Kamp  * NASA Ames Research Center.
13ce4946daSBill Paul  *
14ce4946daSBill Paul  * Redistribution and use in source and binary forms, with or without
15ce4946daSBill Paul  * modification, are permitted provided that the following conditions
16ce4946daSBill Paul  * are met:
17ce4946daSBill Paul  * 1. Redistributions of source code must retain the above copyright
18ce4946daSBill Paul  *    notice, this list of conditions and the following disclaimer.
19ce4946daSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
20ce4946daSBill Paul  *    notice, this list of conditions and the following disclaimer in the
21ce4946daSBill Paul  *    documentation and/or other materials provided with the distribution.
22ce4946daSBill Paul  * 3. All advertising materials mentioning features or use of this software
23ce4946daSBill Paul  *    must display the following acknowledgement:
24ce4946daSBill Paul  *	This product includes software developed by Bill Paul.
25ce4946daSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
26ce4946daSBill Paul  *    may be used to endorse or promote products derived from this software
27ce4946daSBill Paul  *    without specific prior written permission.
28ce4946daSBill Paul  *
29ce4946daSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
30ce4946daSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31ce4946daSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32ce4946daSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
33ce4946daSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34ce4946daSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35ce4946daSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36ce4946daSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37ce4946daSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38ce4946daSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
39ce4946daSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
40ce4946daSBill Paul  */
41ce4946daSBill Paul 
42aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
43ce4946daSBill Paul /*
4421211c45SMarius Strobl  * Driver for the National Semiconductor DP83861, DP83865 and DP83891
45ce4946daSBill Paul  * 10/100/1000 PHYs.
46ce4946daSBill Paul  * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf
4721211c45SMarius Strobl  * and at: http://www.national.com/ds/DP/DP83865.pdf
48ce4946daSBill Paul  *
4921211c45SMarius Strobl  * The DP83891 is the older NS GigE PHY which isn't being sold
50ce4946daSBill Paul  * anymore.  The DP83861 is its replacement, which is an 'enhanced'
51ce4946daSBill Paul  * firmware driven component.  The major difference between the
5221211c45SMarius Strobl  * two is that the DP83891 can't generate interrupts, while the
5321211c45SMarius Strobl  * 83861 can (probably it wasn't originally designed to do this, but
5421211c45SMarius Strobl  * it can now thanks to firmware updates).  The DP83861 also allows
5521211c45SMarius Strobl  * access to its internal RAM via indirect register access.  The
5621211c45SMarius Strobl  * DP83865 is an ultra low power version of the DP83861 and DP83891.
57ce4946daSBill Paul  */
58ce4946daSBill Paul 
59ce4946daSBill Paul #include <sys/param.h>
60ce4946daSBill Paul #include <sys/systm.h>
61ce4946daSBill Paul #include <sys/kernel.h>
6241ee9f1cSPoul-Henning Kamp #include <sys/module.h>
63ce4946daSBill Paul #include <sys/socket.h>
64ce4946daSBill Paul #include <sys/bus.h>
65ce4946daSBill Paul 
66ce4946daSBill Paul #include <net/if.h>
67ce4946daSBill Paul #include <net/if_media.h>
68ce4946daSBill Paul 
69ce4946daSBill Paul #include <dev/mii/mii.h>
70ce4946daSBill Paul #include <dev/mii/miivar.h>
712d3ce713SDavid E. O'Brien #include "miidevs.h"
72ce4946daSBill Paul 
73ce4946daSBill Paul #include <dev/mii/nsgphyreg.h>
74ce4946daSBill Paul 
75ce4946daSBill Paul #include "miibus_if.h"
76ce4946daSBill Paul 
77e51a25f8SAlfred Perlstein static int nsgphy_probe(device_t);
78e51a25f8SAlfred Perlstein static int nsgphy_attach(device_t);
79ce4946daSBill Paul 
80ce4946daSBill Paul static device_method_t nsgphy_methods[] = {
81ce4946daSBill Paul 	/* device interface */
82ce4946daSBill Paul 	DEVMETHOD(device_probe,		nsgphy_probe),
83ce4946daSBill Paul 	DEVMETHOD(device_attach,	nsgphy_attach),
84279fe8d1SPoul-Henning Kamp 	DEVMETHOD(device_detach,	mii_phy_detach),
85ce4946daSBill Paul 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
86604f5f1fSMarius Strobl 	DEVMETHOD_END
87ce4946daSBill Paul };
88ce4946daSBill Paul 
89ce4946daSBill Paul static driver_t nsgphy_driver = {
90ce4946daSBill Paul 	"nsgphy",
91ce4946daSBill Paul 	nsgphy_methods,
92ce4946daSBill Paul 	sizeof(struct mii_softc)
93ce4946daSBill Paul };
94ce4946daSBill Paul 
95*f438c2ffSJohn Baldwin DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, 0, 0);
96ce4946daSBill Paul 
97e51a25f8SAlfred Perlstein static int	nsgphy_service(struct mii_softc *, struct mii_data *,int);
98e51a25f8SAlfred Perlstein static void	nsgphy_status(struct mii_softc *);
99ce4946daSBill Paul 
100a35b9333SMarius Strobl static const struct mii_phydesc nsgphys[] = {
1013fcb7a53SMarius Strobl 	MII_PHY_DESC(xxNATSEMI, DP83861),
1023fcb7a53SMarius Strobl 	MII_PHY_DESC(xxNATSEMI, DP83865),
1033fcb7a53SMarius Strobl 	MII_PHY_DESC(xxNATSEMI, DP83891),
104a1039f82SWarner Losh 	MII_PHY_END
105875525d5SPoul-Henning Kamp };
106875525d5SPoul-Henning Kamp 
1073fcb7a53SMarius Strobl static const struct mii_phy_funcs nsgphy_funcs = {
1083fcb7a53SMarius Strobl 	nsgphy_service,
1093fcb7a53SMarius Strobl 	nsgphy_status,
1103fcb7a53SMarius Strobl 	mii_phy_reset
1113fcb7a53SMarius Strobl };
1123fcb7a53SMarius Strobl 
1133aae18bdSPoul-Henning Kamp static int
nsgphy_probe(device_t dev)1143aae18bdSPoul-Henning Kamp nsgphy_probe(device_t dev)
115ce4946daSBill Paul {
116ce4946daSBill Paul 
117a35b9333SMarius Strobl 	return (mii_phy_dev_probe(dev, nsgphys, BUS_PROBE_DEFAULT));
118ce4946daSBill Paul }
119ce4946daSBill Paul 
1203aae18bdSPoul-Henning Kamp static int
nsgphy_attach(device_t dev)1213aae18bdSPoul-Henning Kamp nsgphy_attach(device_t dev)
122ce4946daSBill Paul {
123ce4946daSBill Paul 	struct mii_softc *sc;
124ce4946daSBill Paul 
125ce4946daSBill Paul 	sc = device_get_softc(dev);
126ce4946daSBill Paul 
1273fcb7a53SMarius Strobl 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &nsgphy_funcs, 0);
128ce4946daSBill Paul 
1293fcb7a53SMarius Strobl 	PHY_RESET(sc);
13021211c45SMarius Strobl 
13121211c45SMarius Strobl 	/*
132142d932bSMarius Strobl 	 * NB: the PHY has the 10BASE-T BMSR bits hard-wired to 0,
133142d932bSMarius Strobl 	 * even though it supports 10BASE-T.
13421211c45SMarius Strobl 	 */
135b7dee1dbSPoul-Henning Kamp 	sc->mii_capabilities = (PHY_READ(sc, MII_BMSR) |
1363fcb7a53SMarius Strobl 	    BMSR_10TFDX | BMSR_10THDX) & sc->mii_capmask;
137142d932bSMarius Strobl 	/*
138142d932bSMarius Strobl 	 * Note that as documented manual 1000BASE-T modes of DP83865 only
139142d932bSMarius Strobl 	 * work together with other National Semiconductor PHYs.
140142d932bSMarius Strobl 	 */
141b7dee1dbSPoul-Henning Kamp 	if (sc->mii_capabilities & BMSR_EXTSTAT)
142b7dee1dbSPoul-Henning Kamp 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
14378c8c3dbSPoul-Henning Kamp 
14478c8c3dbSPoul-Henning Kamp 	mii_phy_add_media(sc);
145ce4946daSBill Paul 	printf("\n");
146ce4946daSBill Paul 
147ce4946daSBill Paul 	MIIBUS_MEDIAINIT(sc->mii_dev);
148ce4946daSBill Paul 	return (0);
149ce4946daSBill Paul }
150ce4946daSBill Paul 
1513aae18bdSPoul-Henning Kamp static int
nsgphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)1523aae18bdSPoul-Henning Kamp nsgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
153ce4946daSBill Paul {
154ce4946daSBill Paul 
155ce4946daSBill Paul 	switch (cmd) {
156ce4946daSBill Paul 	case MII_POLLSTAT:
157ce4946daSBill Paul 		break;
158ce4946daSBill Paul 
159ce4946daSBill Paul 	case MII_MEDIACHG:
16078c8c3dbSPoul-Henning Kamp 		mii_phy_setmedia(sc);
161ce4946daSBill Paul 		break;
162ce4946daSBill Paul 
163ce4946daSBill Paul 	case MII_TICK:
16478c8c3dbSPoul-Henning Kamp 		if (mii_phy_tick(sc) == EJUSTRETURN)
165ce4946daSBill Paul 			return (0);
166ce4946daSBill Paul 		break;
167ce4946daSBill Paul 	}
168ce4946daSBill Paul 
169ce4946daSBill Paul 	/* Update the media status. */
1703fcb7a53SMarius Strobl 	PHY_STATUS(sc);
171ce4946daSBill Paul 
172ce4946daSBill Paul 	/* Callback if something changed. */
173d9730b8bSJonathan Lemon 	mii_phy_update(sc, cmd);
174ce4946daSBill Paul 	return (0);
175ce4946daSBill Paul }
176ce4946daSBill Paul 
177d9730b8bSJonathan Lemon static void
nsgphy_status(struct mii_softc * sc)1783aae18bdSPoul-Henning Kamp nsgphy_status(struct mii_softc *sc)
179ce4946daSBill Paul {
180ce4946daSBill Paul 	struct mii_data *mii = sc->mii_pdata;
18178c8c3dbSPoul-Henning Kamp 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
18278c8c3dbSPoul-Henning Kamp 	int bmsr, bmcr, physup, gtsr;
183ce4946daSBill Paul 
184ce4946daSBill Paul 	mii->mii_media_status = IFM_AVALID;
185ce4946daSBill Paul 	mii->mii_media_active = IFM_ETHER;
186ce4946daSBill Paul 
18778c8c3dbSPoul-Henning Kamp 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
1883aae18bdSPoul-Henning Kamp 
189ce4946daSBill Paul 	physup = PHY_READ(sc, NSGPHY_MII_PHYSUP);
1903aae18bdSPoul-Henning Kamp 
19178c8c3dbSPoul-Henning Kamp 	if (physup & PHY_SUP_LINK)
192ce4946daSBill Paul 		mii->mii_media_status |= IFM_ACTIVE;
193ce4946daSBill Paul 
1943aae18bdSPoul-Henning Kamp 	bmcr = PHY_READ(sc, MII_BMCR);
19578c8c3dbSPoul-Henning Kamp 	if (bmcr & BMCR_ISO) {
19678c8c3dbSPoul-Henning Kamp 		mii->mii_media_active |= IFM_NONE;
19778c8c3dbSPoul-Henning Kamp 		mii->mii_media_status = 0;
19878c8c3dbSPoul-Henning Kamp 		return;
19978c8c3dbSPoul-Henning Kamp 	}
200ce4946daSBill Paul 
2013aae18bdSPoul-Henning Kamp 	if (bmcr & BMCR_LOOP)
202ce4946daSBill Paul 		mii->mii_media_active |= IFM_LOOP;
203ce4946daSBill Paul 
2043aae18bdSPoul-Henning Kamp 	if (bmcr & BMCR_AUTOEN) {
2053aae18bdSPoul-Henning Kamp 		/*
2063aae18bdSPoul-Henning Kamp 		 * The media status bits are only valid if autonegotiation
2073aae18bdSPoul-Henning Kamp 		 * has completed (or it's disabled).
2083aae18bdSPoul-Henning Kamp 		 */
2093aae18bdSPoul-Henning Kamp 		if ((bmsr & BMSR_ACOMP) == 0) {
210ce4946daSBill Paul 			/* Erg, still trying, I guess... */
211ce4946daSBill Paul 			mii->mii_media_active |= IFM_NONE;
212ce4946daSBill Paul 			return;
213ce4946daSBill Paul 		}
214ce4946daSBill Paul 
21578c8c3dbSPoul-Henning Kamp 		switch (physup & (PHY_SUP_SPEED1 | PHY_SUP_SPEED0)) {
21678c8c3dbSPoul-Henning Kamp 		case PHY_SUP_SPEED1:
217b418ad5cSPoul-Henning Kamp 			mii->mii_media_active |= IFM_1000_T;
21878c8c3dbSPoul-Henning Kamp 			gtsr = PHY_READ(sc, MII_100T2SR);
21978c8c3dbSPoul-Henning Kamp 			if (gtsr & GTSR_MS_RES)
22078c8c3dbSPoul-Henning Kamp 				mii->mii_media_active |= IFM_ETH_MASTER;
221ce4946daSBill Paul 			break;
22278c8c3dbSPoul-Henning Kamp 
22378c8c3dbSPoul-Henning Kamp 		case PHY_SUP_SPEED0:
224ce4946daSBill Paul 			mii->mii_media_active |= IFM_100_TX;
225ce4946daSBill Paul 			break;
22678c8c3dbSPoul-Henning Kamp 
22778c8c3dbSPoul-Henning Kamp 		case 0:
228ce4946daSBill Paul 			mii->mii_media_active |= IFM_10_T;
229ce4946daSBill Paul 			break;
23078c8c3dbSPoul-Henning Kamp 
231ce4946daSBill Paul 		default:
23278c8c3dbSPoul-Henning Kamp 			mii->mii_media_active |= IFM_NONE;
23378c8c3dbSPoul-Henning Kamp 			mii->mii_media_status = 0;
23421211c45SMarius Strobl 			return;
235ce4946daSBill Paul 		}
23621211c45SMarius Strobl 
23778c8c3dbSPoul-Henning Kamp 		if (physup & PHY_SUP_DUPLEX)
238991ab941SMarius Strobl 			mii->mii_media_active |=
239991ab941SMarius Strobl 			    IFM_FDX | mii_phy_flowstatus(sc);
24021211c45SMarius Strobl 		else
24121211c45SMarius Strobl 			mii->mii_media_active |= IFM_HDX;
24278c8c3dbSPoul-Henning Kamp 	} else
24378c8c3dbSPoul-Henning Kamp 		mii->mii_media_active = ife->ifm_media;
244ce4946daSBill Paul }
245