bmtphy.c revision 213893
1193323Sed/*-
2193323Sed * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * This code is derived from software contributed to The NetBSD Foundation
6193323Sed * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7193323Sed * NASA Ames Research Center.
8193323Sed *
9193323Sed * Redistribution and use in source and binary forms, with or without
10193323Sed * modification, are permitted provided that the following conditions
11193323Sed * are met:
12193323Sed * 1. Redistributions of source code must retain the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer.
14193323Sed * 2. Redistributions in binary form must reproduce the above copyright
15198090Srdivacky *    notice, this list of conditions and the following disclaimer in the
16193323Sed *    documentation and/or other materials provided with the distribution.
17193323Sed *
18193323Sed * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19193323Sed * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20193323Sed * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21193323Sed * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22193323Sed * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23193323Sed * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24193323Sed * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25210299Sed * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26198090Srdivacky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27193323Sed * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28193323Sed * POSSIBILITY OF SUCH DAMAGE.
29193323Sed */
30276479Sdim
31193323Sed/*
32193323Sed * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
33193323Sed *
34193323Sed * Redistribution and use in source and binary forms, with or without
35193323Sed * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 *    notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 *    notice, this list of conditions and the following disclaimer in the
41 *    documentation and/or other materials provided with the distribution.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
47 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 *
54 *	from: NetBSD: bmtphy.c,v 1.8 2002/07/03 06:25:50 simonb Exp
55 *
56 */
57
58#include <sys/cdefs.h>
59__FBSDID("$FreeBSD: head/sys/dev/mii/bmtphy.c 213893 2010-10-15 14:52:11Z marius $");
60
61/*
62 * Driver for the Broadcom BCM5201/BCM5202 "Mini-Theta" PHYs.  This also
63 * drives the PHY on the 3Com 3c905C.  The 3c905C's PHY is described in
64 * the 3c905C data sheet.
65 */
66
67#include <sys/param.h>
68#include <sys/systm.h>
69#include <sys/kernel.h>
70#include <sys/module.h>
71#include <sys/socket.h>
72#include <sys/bus.h>
73
74#include <net/if.h>
75#include <net/if_media.h>
76
77#include <dev/mii/mii.h>
78#include <dev/mii/miivar.h>
79#include "miidevs.h"
80
81#include <dev/mii/bmtphyreg.h>
82
83#include "miibus_if.h"
84
85static int	bmtphy_probe(device_t);
86static int	bmtphy_attach(device_t);
87
88static device_method_t bmtphy_methods[] = {
89	/* Device interface */
90	DEVMETHOD(device_probe,		bmtphy_probe),
91	DEVMETHOD(device_attach,	bmtphy_attach),
92	DEVMETHOD(device_detach,	mii_phy_detach),
93	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
94
95	{ 0, 0 }
96};
97
98static devclass_t	bmtphy_devclass;
99
100static driver_t	bmtphy_driver = {
101	"bmtphy",
102	bmtphy_methods,
103	sizeof(struct mii_softc)
104};
105
106DRIVER_MODULE(bmtphy, miibus, bmtphy_driver, bmtphy_devclass, 0, 0);
107
108static int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
109static void	bmtphy_status(struct mii_softc *);
110
111static const struct mii_phydesc bmtphys_dp[] = {
112	MII_PHY_DESC(BROADCOM, BCM4401),
113	MII_PHY_DESC(BROADCOM, BCM5201),
114	MII_PHY_DESC(BROADCOM, BCM5221),
115	MII_PHY_END
116};
117
118static const struct mii_phydesc bmtphys_lp[] = {
119	MII_PHY_DESC(BROADCOM, 3C905B),
120	MII_PHY_DESC(BROADCOM, 3C905C),
121	MII_PHY_END
122};
123
124static int
125bmtphy_probe(device_t dev)
126{
127	int	rval;
128
129	/* Let exphy(4) take precedence for these. */
130	rval = mii_phy_dev_probe(dev, bmtphys_lp, BUS_PROBE_LOW_PRIORITY);
131	if (rval <= 0)
132		return (rval);
133
134	return (mii_phy_dev_probe(dev, bmtphys_dp, BUS_PROBE_DEFAULT));
135}
136
137static int
138bmtphy_attach(device_t dev)
139{
140	struct	mii_softc *sc;
141	struct	mii_attach_args *ma;
142	struct	mii_data *mii;
143
144	sc = device_get_softc(dev);
145	ma = device_get_ivars(dev);
146	sc->mii_dev = device_get_parent(dev);
147	mii = ma->mii_data;
148	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
149
150	sc->mii_flags = miibus_get_flags(dev);
151	sc->mii_inst = mii->mii_instance++;
152	sc->mii_phy = ma->mii_phyno;
153	sc->mii_service = bmtphy_service;
154	sc->mii_pdata = mii;
155
156	mii_phy_reset(sc);
157
158	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
159	device_printf(dev, " ");
160	mii_phy_add_media(sc);
161	printf("\n");
162
163	MIIBUS_MEDIAINIT(sc->mii_dev);
164
165	return (0);
166}
167
168static int
169bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
170{
171
172	switch (cmd) {
173	case MII_POLLSTAT:
174		break;
175
176	case MII_MEDIACHG:
177		/*
178		 * If the interface is not up, don't do anything.
179		 */
180		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
181			break;
182
183		mii_phy_setmedia(sc);
184		break;
185
186	case MII_TICK:
187		if (mii_phy_tick(sc) == EJUSTRETURN)
188			return (0);
189		break;
190	}
191
192	/* Update the media status. */
193	bmtphy_status(sc);
194
195	/* Callback if something changed. */
196	mii_phy_update(sc, cmd);
197
198	return (0);
199}
200
201static void
202bmtphy_status(struct mii_softc *sc)
203{
204	struct	mii_data *mii;
205	struct	ifmedia_entry *ife;
206	int	bmsr, bmcr, aux_csr;
207
208	mii = sc->mii_pdata;
209	ife = mii->mii_media.ifm_cur;
210
211	mii->mii_media_status = IFM_AVALID;
212	mii->mii_media_active = IFM_ETHER;
213
214	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
215	aux_csr = PHY_READ(sc, MII_BMTPHY_AUX_CSR);
216
217	if (bmsr & BMSR_LINK)
218		mii->mii_media_status |= IFM_ACTIVE;
219
220	bmcr = PHY_READ(sc, MII_BMCR);
221	if (bmcr & BMCR_ISO) {
222		mii->mii_media_active |= IFM_NONE;
223		mii->mii_media_status = 0;
224		return;
225	}
226
227	if (bmcr & BMCR_LOOP)
228		mii->mii_media_active |= IFM_LOOP;
229
230	if (bmcr & BMCR_AUTOEN) {
231		/*
232		 * The media status bits are only valid if autonegotiation
233		 * has completed (or it's disabled).
234		 */
235		if ((bmsr & BMSR_ACOMP) == 0) {
236			/* Erg, still trying, I guess... */
237			mii->mii_media_active |= IFM_NONE;
238			return;
239		}
240
241		if (aux_csr & AUX_CSR_SPEED)
242			mii->mii_media_active |= IFM_100_TX;
243		else
244			mii->mii_media_active |= IFM_10_T;
245		if (aux_csr & AUX_CSR_FDX)
246			mii->mii_media_active |= IFM_FDX;
247		else
248			mii->mii_media_active |= IFM_HDX;
249	} else
250		mii->mii_media_active = ife->ifm_media;
251}
252