xref: /freebsd/sys/dev/mii/bmtphy.c (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  *
56  *	from: NetBSD: bmtphy.c,v 1.8 2002/07/03 06:25:50 simonb Exp
57  *
58  */
59 
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 /*
64  * Driver for the Broadcom BCM5201/BCM5202 "Mini-Theta" PHYs.  This also
65  * drives the PHY on the 3Com 3c905C.  The 3c905C's PHY is described in
66  * the 3c905C data sheet.
67  */
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/module.h>
73 #include <sys/socket.h>
74 #include <sys/bus.h>
75 
76 #include <net/if.h>
77 #include <net/if_media.h>
78 
79 #include <dev/mii/mii.h>
80 #include <dev/mii/miivar.h>
81 #include "miidevs.h"
82 
83 #include <dev/mii/bmtphyreg.h>
84 
85 #include "miibus_if.h"
86 
87 static int	bmtphy_probe(device_t);
88 static int	bmtphy_attach(device_t);
89 
90 static device_method_t bmtphy_methods[] = {
91 	/* Device interface */
92 	DEVMETHOD(device_probe,		bmtphy_probe),
93 	DEVMETHOD(device_attach,	bmtphy_attach),
94 	DEVMETHOD(device_detach,	mii_phy_detach),
95 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
96 	DEVMETHOD_END
97 };
98 
99 static driver_t	bmtphy_driver = {
100 	"bmtphy",
101 	bmtphy_methods,
102 	sizeof(struct mii_softc)
103 };
104 
105 DRIVER_MODULE(bmtphy, miibus, bmtphy_driver, 0, 0);
106 
107 static int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
108 static void	bmtphy_status(struct mii_softc *);
109 static void	bmtphy_reset(struct mii_softc *);
110 
111 static const struct mii_phydesc bmtphys_dp[] = {
112 	MII_PHY_DESC(xxBROADCOM, BCM4401),
113 	MII_PHY_DESC(xxBROADCOM, BCM5201),
114 	MII_PHY_DESC(xxBROADCOM, BCM5214),
115 	MII_PHY_DESC(xxBROADCOM, BCM5221),
116 	MII_PHY_DESC(xxBROADCOM, BCM5222),
117 	MII_PHY_END
118 };
119 
120 static const struct mii_phydesc bmtphys_lp[] = {
121 	MII_PHY_DESC(xxBROADCOM, 3C905B),
122 	MII_PHY_DESC(xxBROADCOM, 3C905C),
123 	MII_PHY_END
124 };
125 
126 static const struct mii_phy_funcs bmtphy_funcs = {
127 	bmtphy_service,
128 	bmtphy_status,
129 	bmtphy_reset
130 };
131 
132 static int
133 bmtphy_probe(device_t dev)
134 {
135 	int	rval;
136 
137 	/* Let exphy(4) take precedence for these. */
138 	rval = mii_phy_dev_probe(dev, bmtphys_lp, BUS_PROBE_LOW_PRIORITY);
139 	if (rval <= 0)
140 		return (rval);
141 
142 	return (mii_phy_dev_probe(dev, bmtphys_dp, BUS_PROBE_DEFAULT));
143 }
144 
145 static int
146 bmtphy_attach(device_t dev)
147 {
148 
149 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &bmtphy_funcs, 1);
150 	return (0);
151 }
152 
153 static int
154 bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
155 {
156 
157 	switch (cmd) {
158 	case MII_POLLSTAT:
159 		break;
160 
161 	case MII_MEDIACHG:
162 		mii_phy_setmedia(sc);
163 		break;
164 
165 	case MII_TICK:
166 		if (mii_phy_tick(sc) == EJUSTRETURN)
167 			return (0);
168 		break;
169 	}
170 
171 	/* Update the media status. */
172 	PHY_STATUS(sc);
173 
174 	/* Callback if something changed. */
175 	mii_phy_update(sc, cmd);
176 	return (0);
177 }
178 
179 static void
180 bmtphy_status(struct mii_softc *sc)
181 {
182 	struct mii_data *mii;
183 	struct ifmedia_entry *ife;
184 	int bmsr, bmcr, aux_csr;
185 
186 	mii = sc->mii_pdata;
187 	ife = mii->mii_media.ifm_cur;
188 
189 	mii->mii_media_status = IFM_AVALID;
190 	mii->mii_media_active = IFM_ETHER;
191 
192 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
193 
194 	if (bmsr & BMSR_LINK)
195 		mii->mii_media_status |= IFM_ACTIVE;
196 
197 	bmcr = PHY_READ(sc, MII_BMCR);
198 	if (bmcr & BMCR_ISO) {
199 		mii->mii_media_active |= IFM_NONE;
200 		mii->mii_media_status = 0;
201 		return;
202 	}
203 
204 	if (bmcr & BMCR_LOOP)
205 		mii->mii_media_active |= IFM_LOOP;
206 
207 	if (bmcr & BMCR_AUTOEN) {
208 		/*
209 		 * The media status bits are only valid if autonegotiation
210 		 * has completed (or it's disabled).
211 		 */
212 		if ((bmsr & BMSR_ACOMP) == 0) {
213 			/* Erg, still trying, I guess... */
214 			mii->mii_media_active |= IFM_NONE;
215 			return;
216 		}
217 
218 		aux_csr = PHY_READ(sc, MII_BMTPHY_AUX_CSR);
219 		if (aux_csr & AUX_CSR_SPEED)
220 			mii->mii_media_active |= IFM_100_TX;
221 		else
222 			mii->mii_media_active |= IFM_10_T;
223 		if (aux_csr & AUX_CSR_FDX)
224 			mii->mii_media_active |=
225 			    IFM_FDX | mii_phy_flowstatus(sc);
226 		else
227 			mii->mii_media_active |= IFM_HDX;
228 	} else
229 		mii->mii_media_active = ife->ifm_media;
230 }
231 
232 static void
233 bmtphy_reset(struct mii_softc *sc)
234 {
235 	u_int16_t data;
236 
237 	mii_phy_reset(sc);
238 
239 	if (sc->mii_mpd_model == MII_MODEL_xxBROADCOM_BCM5221) {
240 		/* Enable shadow register mode. */
241 		data = PHY_READ(sc, 0x1f);
242 		PHY_WRITE(sc, 0x1f, data | 0x0080);
243 
244 		/* Enable APD (Auto PowerDetect). */
245 		data = PHY_READ(sc, MII_BMTPHY_AUX2);
246 		PHY_WRITE(sc, MII_BMTPHY_AUX2, data | 0x0020);
247 
248 		/* Enable clocks across APD for Auto-MDIX functionality. */
249 		data = PHY_READ(sc, MII_BMTPHY_INTR);
250 		PHY_WRITE(sc, MII_BMTPHY_INTR, data | 0x0004);
251 
252 		/* Disable shadow register mode. */
253 		data = PHY_READ(sc, 0x1f);
254 		PHY_WRITE(sc, 0x1f, data & ~0x0080);
255 	}
256 }
257