nsgphy.c revision 95724
1/*
2 * Copyright (c) 2001 Wind River Systems
3 * Copyright (c) 2001
4 *	Bill Paul <wpaul@bsdi.com>.  All rights reserved.
5 * Copyright (c) 1998, 1999, 2000, 2001 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 Bill Paul.
23 * 4. Neither the name of the author nor the names of any co-contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37 * THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * $FreeBSD: head/sys/dev/mii/nsgphy.c 95724 2002-04-29 14:09:10Z phk $
40 */
41
42/*
43 * Driver for the National Semiconductor DP83891 and DP83861
44 * 10/100/1000 PHYs.
45 * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf
46 *
47 * The DP83891 is the older NatSemi gigE PHY which isn't being sold
48 * anymore. The DP83861 is its replacement, which is an 'enhanced'
49 * firmware driven component. The major difference between the
50 * two is that the 83891 can't generate interrupts, while the
51 * 83861 can. (I think it wasn't originally designed to do this, but
52 * it can now thanks to firmware updates.) The 83861 also allows
53 * access to its internal RAM via indirect register access.
54 */
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/kernel.h>
59#include <sys/malloc.h>
60#include <sys/socket.h>
61#include <sys/bus.h>
62
63#include <machine/clock.h>
64
65#include <net/if.h>
66#include <net/if_media.h>
67
68#include <dev/mii/mii.h>
69#include <dev/mii/miivar.h>
70#include <dev/mii/miidevs.h>
71
72#include <dev/mii/nsgphyreg.h>
73
74#include "miibus_if.h"
75
76#if !defined(lint)
77static const char rcsid[] =
78  "$FreeBSD: head/sys/dev/mii/nsgphy.c 95724 2002-04-29 14:09:10Z phk $";
79#endif
80
81static int nsgphy_probe		(device_t);
82static int nsgphy_attach	(device_t);
83
84static device_method_t nsgphy_methods[] = {
85	/* device interface */
86	DEVMETHOD(device_probe,		nsgphy_probe),
87	DEVMETHOD(device_attach,	nsgphy_attach),
88	DEVMETHOD(device_detach,	mii_phy_detach),
89	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
90	{ 0, 0 }
91};
92
93static devclass_t nsgphy_devclass;
94
95static driver_t nsgphy_driver = {
96	"nsgphy",
97	nsgphy_methods,
98	sizeof(struct mii_softc)
99};
100
101
102DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0);
103
104static int	nsgphy_service(struct mii_softc *, struct mii_data *,int);
105static void	nsgphy_status(struct mii_softc *);
106extern void	mii_phy_auto_timeout(void *);
107
108const struct mii_phydesc gphyters[] = {
109	{ MII_OUI_NATSEMI,		MII_MODEL_NATSEMI_DP83861,
110	  MII_STR_NATSEMI_DP83861 },
111
112	{ MII_OUI_NATSEMI,		MII_MODEL_NATSEMI_DP83891,
113	  MII_STR_NATSEMI_DP83891 },
114
115	{ 0,				0,
116	  NULL },
117};
118
119static int
120nsgphy_probe(device_t dev)
121{
122	struct mii_attach_args *ma;
123	const struct mii_phydesc *mpd;
124
125	ma = device_get_ivars(dev);
126	mpd = mii_phy_match(ma, gphyters);
127	if (mpd != NULL) {
128		device_set_desc(dev, mpd->mpd_name);
129		return(0);
130	}
131
132	return(ENXIO);
133}
134
135static int
136nsgphy_attach(device_t dev)
137{
138	struct mii_softc *sc;
139	struct mii_attach_args *ma;
140	struct mii_data *mii;
141	const struct mii_phydesc *mpd;
142
143	sc = device_get_softc(dev);
144	ma = device_get_ivars(dev);
145	mpd = mii_phy_match(ma, gphyters);
146	if (bootverbose)
147		device_printf(dev, "<rev. %d>\n", MII_REV(ma->mii_id2));
148	device_printf(dev, " ");
149	sc->mii_dev = device_get_parent(dev);
150	mii = device_get_softc(sc->mii_dev);
151	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
152
153	sc->mii_inst = mii->mii_instance;
154	sc->mii_phy = ma->mii_phyno;
155	sc->mii_service = nsgphy_service;
156	sc->mii_pdata = mii;
157	sc->mii_anegticks = 5;
158
159	mii->mii_instance++;
160
161	sc->mii_capabilities = (PHY_READ(sc, MII_BMSR) |
162	    (BMSR_10TFDX|BMSR_10THDX)) & ma->mii_capmask;
163	if (sc->mii_capabilities & BMSR_EXTSTAT)
164		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
165
166	mii_phy_add_media(sc);
167	printf("\n");
168
169	MIIBUS_MEDIAINIT(sc->mii_dev);
170	return(0);
171}
172
173static int
174nsgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
175{
176	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
177	int reg;
178
179	switch (cmd) {
180	case MII_POLLSTAT:
181		/*
182		 * If we're not polling our PHY instance, just return.
183		 */
184		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
185			return (0);
186		break;
187
188	case MII_MEDIACHG:
189		/*
190		 * If the media indicates a different PHY instance,
191		 * isolate ourselves.
192		 */
193		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
194			reg = PHY_READ(sc, MII_BMCR);
195			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
196			return (0);
197		}
198
199		/*
200		 * If the interface is not up, don't do anything.
201		 */
202		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
203			break;
204
205		mii_phy_setmedia(sc);
206		break;
207
208	case MII_TICK:
209		/*
210		 * If we're not currently selected, just return.
211		 */
212		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
213			return (0);
214
215		if (mii_phy_tick(sc) == EJUSTRETURN)
216			return (0);
217		break;
218	}
219
220	/* Update the media status. */
221	nsgphy_status(sc);
222
223	/* Callback if something changed. */
224	mii_phy_update(sc, cmd);
225	return (0);
226}
227
228static void
229nsgphy_status(struct mii_softc *sc)
230{
231	struct mii_data *mii = sc->mii_pdata;
232	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
233	int bmsr, bmcr, physup, gtsr;
234
235	mii->mii_media_status = IFM_AVALID;
236	mii->mii_media_active = IFM_ETHER;
237
238	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
239
240	physup = PHY_READ(sc, NSGPHY_MII_PHYSUP);
241
242	if (physup & PHY_SUP_LINK)
243		mii->mii_media_status |= IFM_ACTIVE;
244
245	bmcr = PHY_READ(sc, MII_BMCR);
246	if (bmcr & BMCR_ISO) {
247		mii->mii_media_active |= IFM_NONE;
248		mii->mii_media_status = 0;
249		return;
250	}
251
252	if (bmcr & BMCR_LOOP)
253		mii->mii_media_active |= IFM_LOOP;
254
255	if (bmcr & BMCR_AUTOEN) {
256		/*
257		 * The media status bits are only valid if autonegotiation
258		 * has completed (or it's disabled).
259		 */
260		if ((bmsr & BMSR_ACOMP) == 0) {
261			/* Erg, still trying, I guess... */
262			mii->mii_media_active |= IFM_NONE;
263			return;
264		}
265
266		switch (physup & (PHY_SUP_SPEED1|PHY_SUP_SPEED0)) {
267		case PHY_SUP_SPEED1:
268			mii->mii_media_active |= IFM_1000_T;
269			gtsr = PHY_READ(sc, MII_100T2SR);
270			if (gtsr & GTSR_MS_RES)
271				mii->mii_media_active |= IFM_ETH_MASTER;
272			break;
273
274		case PHY_SUP_SPEED0:
275			mii->mii_media_active |= IFM_100_TX;
276			break;
277
278		case 0:
279			mii->mii_media_active |= IFM_10_T;
280			break;
281
282		default:
283			mii->mii_media_active |= IFM_NONE;
284			mii->mii_media_status = 0;
285		}
286		if (physup & PHY_SUP_DUPLEX)
287			mii->mii_media_active |= IFM_FDX;
288	} else
289		mii->mii_media_active = ife->ifm_media;
290}
291