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 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32 * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * 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$");
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	DEVMETHOD_END
95};
96
97static devclass_t	bmtphy_devclass;
98
99static driver_t	bmtphy_driver = {
100	"bmtphy",
101	bmtphy_methods,
102	sizeof(struct mii_softc)
103};
104
105DRIVER_MODULE(bmtphy, miibus, bmtphy_driver, bmtphy_devclass, 0, 0);
106
107static int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
108static void	bmtphy_status(struct mii_softc *);
109static void	bmtphy_reset(struct mii_softc *);
110
111static 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
120static const struct mii_phydesc bmtphys_lp[] = {
121	MII_PHY_DESC(xxBROADCOM, 3C905B),
122	MII_PHY_DESC(xxBROADCOM, 3C905C),
123	MII_PHY_END
124};
125
126static const struct mii_phy_funcs bmtphy_funcs = {
127	bmtphy_service,
128	bmtphy_status,
129	bmtphy_reset
130};
131
132static int
133bmtphy_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#ifndef __HAIKU__
140	if (rval <= 0)
141#else
142	if (rval <= 0 && rval != ENXIO)
143		return (rval);
144#endif
145
146	return (mii_phy_dev_probe(dev, bmtphys_dp, BUS_PROBE_DEFAULT));
147}
148
149static int
150bmtphy_attach(device_t dev)
151{
152
153	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &bmtphy_funcs, 1);
154	return (0);
155}
156
157static int
158bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
159{
160
161	switch (cmd) {
162	case MII_POLLSTAT:
163		break;
164
165	case MII_MEDIACHG:
166		/*
167		 * If the interface is not up, don't do anything.
168		 */
169		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
170			break;
171
172		mii_phy_setmedia(sc);
173		break;
174
175	case MII_TICK:
176		if (mii_phy_tick(sc) == EJUSTRETURN)
177			return (0);
178		break;
179	}
180
181	/* Update the media status. */
182	PHY_STATUS(sc);
183
184	/* Callback if something changed. */
185	mii_phy_update(sc, cmd);
186	return (0);
187}
188
189static void
190bmtphy_status(struct mii_softc *sc)
191{
192	struct mii_data *mii;
193	struct ifmedia_entry *ife;
194	int bmsr, bmcr, aux_csr;
195
196	mii = sc->mii_pdata;
197	ife = mii->mii_media.ifm_cur;
198
199	mii->mii_media_status = IFM_AVALID;
200	mii->mii_media_active = IFM_ETHER;
201
202	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
203
204	if (bmsr & BMSR_LINK)
205		mii->mii_media_status |= IFM_ACTIVE;
206
207	bmcr = PHY_READ(sc, MII_BMCR);
208	if (bmcr & BMCR_ISO) {
209		mii->mii_media_active |= IFM_NONE;
210		mii->mii_media_status = 0;
211		return;
212	}
213
214	if (bmcr & BMCR_LOOP)
215		mii->mii_media_active |= IFM_LOOP;
216
217	if (bmcr & BMCR_AUTOEN) {
218		/*
219		 * The media status bits are only valid if autonegotiation
220		 * has completed (or it's disabled).
221		 */
222		if ((bmsr & BMSR_ACOMP) == 0) {
223			/* Erg, still trying, I guess... */
224			mii->mii_media_active |= IFM_NONE;
225			return;
226		}
227
228		aux_csr = PHY_READ(sc, MII_BMTPHY_AUX_CSR);
229		if (aux_csr & AUX_CSR_SPEED)
230			mii->mii_media_active |= IFM_100_TX;
231		else
232			mii->mii_media_active |= IFM_10_T;
233		if (aux_csr & AUX_CSR_FDX)
234			mii->mii_media_active |=
235			    IFM_FDX | mii_phy_flowstatus(sc);
236		else
237			mii->mii_media_active |= IFM_HDX;
238	} else
239		mii->mii_media_active = ife->ifm_media;
240}
241
242static void
243bmtphy_reset(struct mii_softc *sc)
244{
245	u_int16_t data;
246
247	mii_phy_reset(sc);
248
249	if (sc->mii_mpd_model == MII_MODEL_xxBROADCOM_BCM5221) {
250		/* Enable shadow register mode. */
251		data = PHY_READ(sc, 0x1f);
252		PHY_WRITE(sc, 0x1f, data | 0x0080);
253
254		/* Enable APD (Auto PowerDetect). */
255		data = PHY_READ(sc, MII_BMTPHY_AUX2);
256		PHY_WRITE(sc, MII_BMTPHY_AUX2, data | 0x0020);
257
258		/* Enable clocks across APD for Auto-MDIX functionality. */
259		data = PHY_READ(sc, MII_BMTPHY_INTR);
260		PHY_WRITE(sc, MII_BMTPHY_INTR, data | 0x0004);
261
262		/* Disable shadow register mode. */
263		data = PHY_READ(sc, 0x1f);
264		PHY_WRITE(sc, 0x1f, data & ~0x0080);
265	}
266}
267