tdkphy.c revision 106107
1/*
2 * Copyright (c) 2000,2001 Jonathan Chen.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in
13 *    the documentation and/or other materials provided with the
14 *    distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/mii/tdkphy.c 106107 2002-10-29 00:20:47Z semenu $
29 */
30
31/*
32 * Driver for the TDK 78Q2120 MII
33 *
34 * References:
35 *   Datasheet for the 78Q2120 - http://www.tsc.tdk.com/lan/78q2120.pdf
36 *   Most of this code stolen from ukphy.c
37 */
38
39/*
40 * The TDK 78Q2120 is found on some Xircom X3201 based cardbus cards.  It's just
41 * like any other normal phy, except it does auto negotiation in a different
42 * way.
43 */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/socket.h>
49#include <sys/errno.h>
50#include <sys/module.h>
51#include <sys/bus.h>
52
53#include <net/if.h>
54#include <net/if_media.h>
55
56#include <machine/clock.h>
57
58#include <dev/mii/mii.h>
59#include <dev/mii/miivar.h>
60#include <dev/mii/miidevs.h>
61
62#include <dev/mii/tdkphyreg.h>
63
64#include "miibus_if.h"
65
66#if !defined(lint)
67static const char rcsid[] =
68  "$Id: tdkphy.c,v 1.3 2000/10/14 06:20:56 jon Exp $";
69#endif
70
71static int tdkphy_probe(device_t);
72static int tdkphy_attach(device_t);
73
74static device_method_t tdkphy_methods[] = {
75	/* device interface */
76	DEVMETHOD(device_probe,		tdkphy_probe),
77	DEVMETHOD(device_attach,	tdkphy_attach),
78	DEVMETHOD(device_detach,	mii_phy_detach),
79	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
80	{ 0, 0 }
81};
82
83static devclass_t tdkphy_devclass;
84
85static driver_t tdkphy_driver = {
86	"tdkphy",
87	tdkphy_methods,
88	sizeof(struct mii_softc)
89};
90
91DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0);
92
93static int tdkphy_service(struct mii_softc *, struct mii_data *, int);
94static void tdkphy_status(struct mii_softc *);
95
96static int
97tdkphy_probe(device_t dev)
98{
99        struct mii_attach_args *ma;
100        ma = device_get_ivars(dev);
101 	if ((MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_TDK ||
102	     MII_MODEL(ma->mii_id2) != MII_MODEL_TDK_78Q2120))
103		return (ENXIO);
104
105	device_set_desc(dev, MII_STR_TDK_78Q2120);
106	return (0);
107}
108
109static int
110tdkphy_attach(device_t dev)
111{
112	struct mii_softc *sc;
113	struct mii_attach_args *ma;
114	struct mii_data *mii;
115	sc = device_get_softc(dev);
116	ma = device_get_ivars(dev);
117	sc->mii_dev = device_get_parent(dev);
118	mii = device_get_softc(sc->mii_dev);
119	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
120
121	if (bootverbose)
122		device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n",
123		    MII_OUI(ma->mii_id1, ma->mii_id2),
124		    MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
125
126	sc->mii_inst = mii->mii_instance;
127	sc->mii_phy = ma->mii_phyno;
128	sc->mii_service = tdkphy_service;
129	sc->mii_pdata = mii;
130
131	mii->mii_instance++;
132
133	sc->mii_flags |= MIIF_NOISOLATE;
134
135#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
136
137	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
138	    BMCR_ISO);
139#if 0
140	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
141	    BMCR_LOOP|BMCR_S100);
142#endif
143
144	mii_phy_reset(sc);
145
146	sc->mii_capabilities =
147	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
148	device_printf(dev, " ");
149	mii_add_media(sc);
150	printf("\n");
151#undef ADD
152
153	MIIBUS_MEDIAINIT(sc->mii_dev);
154
155	return(0);
156}
157
158static int
159tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
160{
161	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
162	int reg;
163
164	switch (cmd) {
165	case MII_POLLSTAT:
166		/*
167		 * If we're not polling our PHY instance, just return.
168		 */
169		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
170			return (0);
171		break;
172
173	case MII_MEDIACHG:
174		/*
175		 * If the media indicates a different PHY instance,
176		 * isolate ourselves.
177		 */
178		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
179			reg = PHY_READ(sc, MII_BMCR);
180			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
181			return (0);
182		}
183
184		/*
185		 * If the interface is not up, don't do anything.
186		 */
187		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
188			break;
189
190		switch (IFM_SUBTYPE(ife->ifm_media)) {
191		case IFM_AUTO:
192			/*
193			 * If we're already in auto mode, just return.
194			 */
195			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
196				return (0);
197			(void) mii_phy_auto(sc);
198			break;
199		case IFM_100_T4:
200			/*
201			 * Not supported on MII
202			 */
203			return (EINVAL);
204		default:
205			/*
206			 * BMCR data is stored in the ifmedia entry.
207			 */
208			PHY_WRITE(sc, MII_ANAR,
209			    mii_anar(ife->ifm_media));
210			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
211		}
212		break;
213
214	case MII_TICK:
215		/*
216		 * If we're not currently selected, just return.
217		 */
218		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
219			return (0);
220		if (mii_phy_tick(sc) == EJUSTRETURN)
221			return (0);
222		break;
223	}
224
225	/* Update the media status. */
226	tdkphy_status(sc);
227	if (sc->mii_pdata->mii_media_active & IFM_FDX)
228		PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX);
229	else
230		PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX);
231
232	/* Callback if something changed. */
233	mii_phy_update(sc, cmd);
234	return (0);
235}
236
237static void
238tdkphy_status(struct mii_softc *phy)
239{
240	struct mii_data *mii = phy->mii_pdata;
241	int bmsr, bmcr, anlpar, diag;
242
243	mii->mii_media_status = IFM_AVALID;
244	mii->mii_media_active = IFM_ETHER;
245
246	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
247	if (bmsr & BMSR_LINK)
248		mii->mii_media_status |= IFM_ACTIVE;
249
250	bmcr = PHY_READ(phy, MII_BMCR);
251	if (bmcr & BMCR_ISO) {
252		mii->mii_media_active |= IFM_NONE;
253		mii->mii_media_status = 0;
254		return;
255	}
256
257	if (bmcr & BMCR_LOOP)
258		mii->mii_media_active |= IFM_LOOP;
259
260	if (bmcr & BMCR_AUTOEN) {
261		/*
262		 * NWay autonegotiation takes the highest-order common
263		 * bit of the ANAR and ANLPAR (i.e. best media advertised
264		 * both by us and our link partner).
265		 */
266		if ((bmsr & BMSR_ACOMP) == 0) {
267			/* Erg, still trying, I guess... */
268			mii->mii_media_active |= IFM_NONE;
269			return;
270		}
271
272		anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
273		/*
274		 * ANLPAR doesn't get set on my card, but we check it anyway,
275		 * since it is mentioned in the 78Q2120 specs.
276		 */
277		if (anlpar & ANLPAR_T4)
278			mii->mii_media_active |= IFM_100_T4;
279		else if (anlpar & ANLPAR_TX_FD)
280			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
281		else if (anlpar & ANLPAR_TX)
282			mii->mii_media_active |= IFM_100_TX;
283		else if (anlpar & ANLPAR_10_FD)
284			mii->mii_media_active |= IFM_10_T|IFM_FDX;
285		else if (anlpar & ANLPAR_10)
286			mii->mii_media_active |= IFM_10_T;
287		else {
288			/*
289			 * ANLPAR isn't set, which leaves two possibilities:
290			 * 1) Auto negotiation failed
291			 * 2) Auto negotiation completed, but the card forgot
292			 *    to set ANLPAR.
293			 * So we check the MII_DIAG(18) register...
294			 */
295			diag = PHY_READ(phy, MII_DIAG);
296			if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */
297				mii->mii_media_active |= IFM_10_T;
298			else {
299				if (diag & DIAG_DUPLEX)
300					mii->mii_media_active |= IFM_FDX;
301				if (diag & DIAG_RATE_100)
302					mii->mii_media_active |= IFM_100_TX;
303				else
304					mii->mii_media_active |= IFM_10_T;
305			}
306		}
307	} else {
308		mii->mii_media_active = mii_media_from_bmcr(bmcr);
309	}
310}
311