bmtphy.c revision 99440
1/*
2 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7 * NASA Ames Research Center.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *      This product includes software developed by the NetBSD
20 *      Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 *    contributors may be used to endorse or promote products derived
23 *    from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 *    notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 *    notice, this list of conditions and the following disclaimer in the
48 *    documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 *    must display the following acknowledgement:
51 *      This product includes software developed by Manuel Bouyer.
52 * 4. The name of the author may not be used to endorse or promote products
53 *    derived from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
56 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
59 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
60 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
64 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 *	from: NetBSD: bmtphy.c,v 1.8 2002/07/03 06:25:50 simonb Exp
67 *
68 * $FreeBSD: head/sys/dev/mii/bmtphy.c 99440 2002-07-05 11:07:24Z benno $
69 */
70
71/*
72 * Driver for the Broadcom BCM5201/BCM5202 "Mini-Theta" PHYs.  This also
73 * drives the PHY on the 3Com 3c905C.  The 3c905C's PHY is described in
74 * the 3c905C data sheet.
75 */
76
77#include <sys/param.h>
78#include <sys/systm.h>
79#include <sys/kernel.h>
80#include <sys/malloc.h>
81#include <sys/socket.h>
82#include <sys/bus.h>
83
84#include <net/if.h>
85#include <net/if_media.h>
86
87#include <dev/mii/mii.h>
88#include <dev/mii/miivar.h>
89#include <dev/mii/miidevs.h>
90
91#include <dev/mii/bmtphyreg.h>
92
93#include "miibus_if.h"
94
95static int	bmtphy_probe(device_t);
96static int	bmtphy_attach(device_t);
97
98static device_method_t bmtphy_methods[] = {
99	/* Device interface */
100	DEVMETHOD(device_probe,		bmtphy_probe),
101	DEVMETHOD(device_attach,	bmtphy_attach),
102	DEVMETHOD(device_detach,	mii_phy_detach),
103	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
104
105	{ 0, 0 }
106};
107
108static devclass_t	bmtphy_devclass;
109
110static driver_t	bmtphy_driver = {
111	"bmtphy",
112	bmtphy_methods,
113	sizeof(struct mii_softc)
114};
115
116DRIVER_MODULE(bmtphy, miibus, bmtphy_driver, bmtphy_devclass, 0, 0);
117
118static int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
119static void	bmtphy_status(struct mii_softc *);
120
121static int
122bmtphy_probe(device_t dev)
123{
124	struct	mii_attach_args *ma;
125	int	rval;
126
127	ma = device_get_ivars(dev);
128	rval = 0;
129
130	if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_BROADCOM)
131		return (ENXIO);
132
133	switch (MII_MODEL(ma->mii_id2)) {
134	case MII_MODEL_BROADCOM_3C905B:
135		device_set_desc(dev, MII_STR_BROADCOM_3C905B);
136		rval = -10;	/* Let exphy take precedence. */
137		break;
138	case MII_MODEL_BROADCOM_3C905C:
139		device_set_desc(dev, MII_STR_BROADCOM_3C905C);
140		rval = -10;	/* Let exphy take precedence. */
141		break;
142	case MII_MODEL_BROADCOM_BCM5201:
143		device_set_desc(dev, MII_STR_BROADCOM_BCM5201);
144		break;
145	case MII_MODEL_BROADCOM_BCM5221:
146		device_set_desc(dev, MII_STR_BROADCOM_BCM5221);
147		break;
148	default:
149		return (ENXIO);
150		break;
151	}
152
153	return (rval);
154}
155
156static int
157bmtphy_attach(device_t dev)
158{
159	struct	mii_softc *sc;
160	struct	mii_attach_args *ma;
161	struct	mii_data *mii;
162
163	sc = device_get_softc(dev);
164	ma = device_get_ivars(dev);
165	sc->mii_dev = device_get_parent(dev);
166	mii = device_get_softc(sc->mii_dev);
167	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
168
169	sc->mii_inst = mii->mii_instance;
170	sc->mii_phy = ma->mii_phyno;
171	sc->mii_service = bmtphy_service;
172	sc->mii_pdata = mii;
173
174	mii_phy_reset(sc);
175
176	mii->mii_instance++;
177
178	sc->mii_capabilities =
179	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
180	device_printf(dev, " ");
181	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
182		printf("no media present");
183	else
184		mii_phy_add_media(sc);
185
186	printf("\n");
187
188	MIIBUS_MEDIAINIT(sc->mii_dev);
189
190	return (0);
191}
192
193static int
194bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
195{
196	struct	ifmedia_entry *ife;
197	int	reg;
198
199	ife = mii->mii_media.ifm_cur;
200
201	switch (cmd) {
202	case MII_POLLSTAT:
203		/*
204		 * If we're not polling our PHY instance, just return.
205		 */
206		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
207			return (0);
208		break;
209
210	case MII_MEDIACHG:
211		/*
212		 * If the media indicates a different PHY instance,
213		 * isolate ourselves.
214		 */
215		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
216			reg = PHY_READ(sc, MII_BMCR);
217			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
218			return (0);
219		}
220
221		/*
222		 * If the interface is not up, don't do anything.
223		 */
224		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
225			break;
226
227		mii_phy_setmedia(sc);
228		break;
229
230	case MII_TICK:
231		/*
232		 * If we're not currently selected, just return.
233		 */
234		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
235			return (0);
236		if (mii_phy_tick(sc) == EJUSTRETURN)
237			return (0);
238		break;
239	}
240
241	/* Update the media status. */
242	bmtphy_status(sc);
243
244	/* Callback if something changed. */
245	mii_phy_update(sc, cmd);
246
247	return (0);
248}
249
250static void
251bmtphy_status(struct mii_softc *sc)
252{
253	struct	mii_data *mii;
254	struct	ifmedia_entry *ife;
255	int	bmsr, bmcr, aux_csr;
256
257	mii = sc->mii_pdata;
258	ife = mii->mii_media.ifm_cur;
259
260	mii->mii_media_status = IFM_AVALID;
261	mii->mii_media_active = IFM_ETHER;
262
263	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
264	aux_csr = PHY_READ(sc, MII_BMTPHY_AUX_CSR);
265
266	if (bmsr & BMSR_LINK)
267		mii->mii_media_status |= IFM_ACTIVE;
268
269	bmcr = PHY_READ(sc, MII_BMCR);
270	if (bmcr & BMCR_ISO) {
271		mii->mii_media_active |= IFM_NONE;
272		mii->mii_media_status = 0;
273		return;
274	}
275
276	if (bmcr & BMCR_LOOP)
277		mii->mii_media_active |= IFM_LOOP;
278
279	if (bmcr & BMCR_AUTOEN) {
280		/*
281		 * The media status bits are only valid if autonegotiation
282		 * has completed (or it's disabled).
283		 */
284		if ((bmsr & BMSR_ACOMP) == 0) {
285			/* Erg, still trying, I guess... */
286			mii->mii_media_active |= IFM_NONE;
287			return;
288		}
289
290		if (aux_csr & AUX_CSR_SPEED)
291			mii->mii_media_active |= IFM_100_TX;
292		else
293			mii->mii_media_active |= IFM_10_T;
294		if (aux_csr & AUX_CSR_FDX)
295			mii->mii_media_active |= IFM_FDX;
296	} else
297		mii->mii_media_active = ife->ifm_media;
298}
299