xref: /freebsd/sys/dev/mii/bmtphy.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
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  */
69 
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
72 
73 /*
74  * Driver for the Broadcom BCM5201/BCM5202 "Mini-Theta" PHYs.  This also
75  * drives the PHY on the 3Com 3c905C.  The 3c905C's PHY is described in
76  * the 3c905C data sheet.
77  */
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/module.h>
83 #include <sys/socket.h>
84 #include <sys/bus.h>
85 
86 #include <net/if.h>
87 #include <net/if_media.h>
88 
89 #include <dev/mii/mii.h>
90 #include <dev/mii/miivar.h>
91 #include "miidevs.h"
92 
93 #include <dev/mii/bmtphyreg.h>
94 
95 #include "miibus_if.h"
96 
97 static int	bmtphy_probe(device_t);
98 static int	bmtphy_attach(device_t);
99 
100 static device_method_t bmtphy_methods[] = {
101 	/* Device interface */
102 	DEVMETHOD(device_probe,		bmtphy_probe),
103 	DEVMETHOD(device_attach,	bmtphy_attach),
104 	DEVMETHOD(device_detach,	mii_phy_detach),
105 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
106 
107 	{ 0, 0 }
108 };
109 
110 static devclass_t	bmtphy_devclass;
111 
112 static driver_t	bmtphy_driver = {
113 	"bmtphy",
114 	bmtphy_methods,
115 	sizeof(struct mii_softc)
116 };
117 
118 DRIVER_MODULE(bmtphy, miibus, bmtphy_driver, bmtphy_devclass, 0, 0);
119 
120 static int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
121 static void	bmtphy_status(struct mii_softc *);
122 
123 static const struct mii_phydesc bmtphys_dp[] = {
124 	MII_PHY_DESC(BROADCOM, BCM4401),
125 	MII_PHY_DESC(BROADCOM, BCM5201),
126 	MII_PHY_DESC(BROADCOM, BCM5221),
127 	MII_PHY_END
128 };
129 
130 static const struct mii_phydesc bmtphys_lp[] = {
131 	MII_PHY_DESC(BROADCOM, 3C905B),
132 	MII_PHY_DESC(BROADCOM, 3C905C),
133 	MII_PHY_END
134 };
135 
136 static int
137 bmtphy_probe(device_t dev)
138 {
139 	int	rval;
140 
141 	/* Let exphy(4) take precedence for these. */
142 	rval = mii_phy_dev_probe(dev, bmtphys_lp, BUS_PROBE_LOW_PRIORITY);
143 	if (rval <= 0)
144 		return (rval);
145 
146 	return (mii_phy_dev_probe(dev, bmtphys_dp, BUS_PROBE_DEFAULT));
147 }
148 
149 static int
150 bmtphy_attach(device_t dev)
151 {
152 	struct	mii_softc *sc;
153 	struct	mii_attach_args *ma;
154 	struct	mii_data *mii;
155 
156 	sc = device_get_softc(dev);
157 	ma = device_get_ivars(dev);
158 	sc->mii_dev = device_get_parent(dev);
159 	mii = device_get_softc(sc->mii_dev);
160 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
161 
162 	sc->mii_inst = mii->mii_instance;
163 	sc->mii_phy = ma->mii_phyno;
164 	sc->mii_service = bmtphy_service;
165 	sc->mii_pdata = mii;
166 
167 	mii_phy_reset(sc);
168 
169 	mii->mii_instance++;
170 
171 	sc->mii_capabilities =
172 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
173 	device_printf(dev, " ");
174 	mii_phy_add_media(sc);
175 	printf("\n");
176 
177 	MIIBUS_MEDIAINIT(sc->mii_dev);
178 
179 	return (0);
180 }
181 
182 static int
183 bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
184 {
185 	struct	ifmedia_entry *ife;
186 	int	reg;
187 
188 	ife = mii->mii_media.ifm_cur;
189 
190 	switch (cmd) {
191 	case MII_POLLSTAT:
192 		/*
193 		 * If we're not polling our PHY instance, just return.
194 		 */
195 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
196 			return (0);
197 		break;
198 
199 	case MII_MEDIACHG:
200 		/*
201 		 * If the media indicates a different PHY instance,
202 		 * isolate ourselves.
203 		 */
204 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
205 			reg = PHY_READ(sc, MII_BMCR);
206 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
207 			return (0);
208 		}
209 
210 		/*
211 		 * If the interface is not up, don't do anything.
212 		 */
213 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
214 			break;
215 
216 		mii_phy_setmedia(sc);
217 		break;
218 
219 	case MII_TICK:
220 		/*
221 		 * If we're not currently selected, just return.
222 		 */
223 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
224 			return (0);
225 		if (mii_phy_tick(sc) == EJUSTRETURN)
226 			return (0);
227 		break;
228 	}
229 
230 	/* Update the media status. */
231 	bmtphy_status(sc);
232 
233 	/* Callback if something changed. */
234 	mii_phy_update(sc, cmd);
235 
236 	return (0);
237 }
238 
239 static void
240 bmtphy_status(struct mii_softc *sc)
241 {
242 	struct	mii_data *mii;
243 	struct	ifmedia_entry *ife;
244 	int	bmsr, bmcr, aux_csr;
245 
246 	mii = sc->mii_pdata;
247 	ife = mii->mii_media.ifm_cur;
248 
249 	mii->mii_media_status = IFM_AVALID;
250 	mii->mii_media_active = IFM_ETHER;
251 
252 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
253 	aux_csr = PHY_READ(sc, MII_BMTPHY_AUX_CSR);
254 
255 	if (bmsr & BMSR_LINK)
256 		mii->mii_media_status |= IFM_ACTIVE;
257 
258 	bmcr = PHY_READ(sc, MII_BMCR);
259 	if (bmcr & BMCR_ISO) {
260 		mii->mii_media_active |= IFM_NONE;
261 		mii->mii_media_status = 0;
262 		return;
263 	}
264 
265 	if (bmcr & BMCR_LOOP)
266 		mii->mii_media_active |= IFM_LOOP;
267 
268 	if (bmcr & BMCR_AUTOEN) {
269 		/*
270 		 * The media status bits are only valid if autonegotiation
271 		 * has completed (or it's disabled).
272 		 */
273 		if ((bmsr & BMSR_ACOMP) == 0) {
274 			/* Erg, still trying, I guess... */
275 			mii->mii_media_active |= IFM_NONE;
276 			return;
277 		}
278 
279 		if (aux_csr & AUX_CSR_SPEED)
280 			mii->mii_media_active |= IFM_100_TX;
281 		else
282 			mii->mii_media_active |= IFM_10_T;
283 		if (aux_csr & AUX_CSR_FDX)
284 			mii->mii_media_active |= IFM_FDX;
285 	} else
286 		mii->mii_media_active = ife->ifm_media;
287 }
288