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