xref: /freebsd/sys/dev/mii/bmtphy.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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 /*
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 
85 static int	bmtphy_probe(device_t);
86 static int	bmtphy_attach(device_t);
87 
88 static 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 	DEVMETHOD_END
95 };
96 
97 static driver_t	bmtphy_driver = {
98 	"bmtphy",
99 	bmtphy_methods,
100 	sizeof(struct mii_softc)
101 };
102 
103 DRIVER_MODULE(bmtphy, miibus, bmtphy_driver, 0, 0);
104 
105 static int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
106 static void	bmtphy_status(struct mii_softc *);
107 static void	bmtphy_reset(struct mii_softc *);
108 
109 static const struct mii_phydesc bmtphys_dp[] = {
110 	MII_PHY_DESC(xxBROADCOM, BCM4401),
111 	MII_PHY_DESC(xxBROADCOM, BCM5201),
112 	MII_PHY_DESC(xxBROADCOM, BCM5214),
113 	MII_PHY_DESC(xxBROADCOM, BCM5221),
114 	MII_PHY_DESC(xxBROADCOM, BCM5222),
115 	MII_PHY_END
116 };
117 
118 static const struct mii_phydesc bmtphys_lp[] = {
119 	MII_PHY_DESC(xxBROADCOM, 3C905B),
120 	MII_PHY_DESC(xxBROADCOM, 3C905C),
121 	MII_PHY_END
122 };
123 
124 static const struct mii_phy_funcs bmtphy_funcs = {
125 	bmtphy_service,
126 	bmtphy_status,
127 	bmtphy_reset
128 };
129 
130 static int
131 bmtphy_probe(device_t dev)
132 {
133 	int	rval;
134 
135 	/* Let exphy(4) take precedence for these. */
136 	rval = mii_phy_dev_probe(dev, bmtphys_lp, BUS_PROBE_LOW_PRIORITY);
137 	if (rval <= 0)
138 		return (rval);
139 
140 	return (mii_phy_dev_probe(dev, bmtphys_dp, BUS_PROBE_DEFAULT));
141 }
142 
143 static int
144 bmtphy_attach(device_t dev)
145 {
146 
147 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &bmtphy_funcs, 1);
148 	return (0);
149 }
150 
151 static int
152 bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
153 {
154 
155 	switch (cmd) {
156 	case MII_POLLSTAT:
157 		break;
158 
159 	case MII_MEDIACHG:
160 		mii_phy_setmedia(sc);
161 		break;
162 
163 	case MII_TICK:
164 		if (mii_phy_tick(sc) == EJUSTRETURN)
165 			return (0);
166 		break;
167 	}
168 
169 	/* Update the media status. */
170 	PHY_STATUS(sc);
171 
172 	/* Callback if something changed. */
173 	mii_phy_update(sc, cmd);
174 	return (0);
175 }
176 
177 static void
178 bmtphy_status(struct mii_softc *sc)
179 {
180 	struct mii_data *mii;
181 	struct ifmedia_entry *ife;
182 	int bmsr, bmcr, aux_csr;
183 
184 	mii = sc->mii_pdata;
185 	ife = mii->mii_media.ifm_cur;
186 
187 	mii->mii_media_status = IFM_AVALID;
188 	mii->mii_media_active = IFM_ETHER;
189 
190 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
191 
192 	if (bmsr & BMSR_LINK)
193 		mii->mii_media_status |= IFM_ACTIVE;
194 
195 	bmcr = PHY_READ(sc, MII_BMCR);
196 	if (bmcr & BMCR_ISO) {
197 		mii->mii_media_active |= IFM_NONE;
198 		mii->mii_media_status = 0;
199 		return;
200 	}
201 
202 	if (bmcr & BMCR_LOOP)
203 		mii->mii_media_active |= IFM_LOOP;
204 
205 	if (bmcr & BMCR_AUTOEN) {
206 		/*
207 		 * The media status bits are only valid if autonegotiation
208 		 * has completed (or it's disabled).
209 		 */
210 		if ((bmsr & BMSR_ACOMP) == 0) {
211 			/* Erg, still trying, I guess... */
212 			mii->mii_media_active |= IFM_NONE;
213 			return;
214 		}
215 
216 		aux_csr = PHY_READ(sc, MII_BMTPHY_AUX_CSR);
217 		if (aux_csr & AUX_CSR_SPEED)
218 			mii->mii_media_active |= IFM_100_TX;
219 		else
220 			mii->mii_media_active |= IFM_10_T;
221 		if (aux_csr & AUX_CSR_FDX)
222 			mii->mii_media_active |=
223 			    IFM_FDX | mii_phy_flowstatus(sc);
224 		else
225 			mii->mii_media_active |= IFM_HDX;
226 	} else
227 		mii->mii_media_active = ife->ifm_media;
228 }
229 
230 static void
231 bmtphy_reset(struct mii_softc *sc)
232 {
233 	u_int16_t data;
234 
235 	mii_phy_reset(sc);
236 
237 	if (sc->mii_mpd_model == MII_MODEL_xxBROADCOM_BCM5221) {
238 		/* Enable shadow register mode. */
239 		data = PHY_READ(sc, 0x1f);
240 		PHY_WRITE(sc, 0x1f, data | 0x0080);
241 
242 		/* Enable APD (Auto PowerDetect). */
243 		data = PHY_READ(sc, MII_BMTPHY_AUX2);
244 		PHY_WRITE(sc, MII_BMTPHY_AUX2, data | 0x0020);
245 
246 		/* Enable clocks across APD for Auto-MDIX functionality. */
247 		data = PHY_READ(sc, MII_BMTPHY_INTR);
248 		PHY_WRITE(sc, MII_BMTPHY_INTR, data | 0x0004);
249 
250 		/* Disable shadow register mode. */
251 		data = PHY_READ(sc, 0x1f);
252 		PHY_WRITE(sc, 0x1f, data & ~0x0080);
253 	}
254 }
255