xref: /freebsd/sys/dev/mii/tdkphy.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
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$
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/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/module.h>
52 #include <sys/bus.h>
53 
54 #include <net/if.h>
55 #include <net/if_media.h>
56 
57 #include <machine/clock.h>
58 
59 #include <dev/mii/mii.h>
60 #include <dev/mii/miivar.h>
61 #include <dev/mii/miidevs.h>
62 
63 #include <dev/mii/tdkphyreg.h>
64 
65 #include "miibus_if.h"
66 
67 #if !defined(lint)
68 static const char rcsid[] =
69   "$Id: tdkphy.c,v 1.3 2000/10/14 06:20:56 jon Exp $";
70 #endif
71 
72 static int tdkphy_probe			__P((device_t));
73 static int tdkphy_attach		__P((device_t));
74 static int tdkphy_detach		__P((device_t));
75 
76 static device_method_t tdkphy_methods[] = {
77 	/* device interface */
78 	DEVMETHOD(device_probe,		tdkphy_probe),
79 	DEVMETHOD(device_attach,	tdkphy_attach),
80 	DEVMETHOD(device_detach,	tdkphy_detach),
81 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
82 	{ 0, 0 }
83 };
84 
85 static devclass_t tdkphy_devclass;
86 
87 static driver_t tdkphy_driver = {
88 	"tdkphy",
89 	tdkphy_methods,
90 	sizeof(struct mii_softc)
91 };
92 
93 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0);
94 
95 int	tdkphy_service __P((struct mii_softc *, struct mii_data *, int));
96 void	tdkphy_status __P((struct mii_softc *));
97 
98 static int
99 tdkphy_probe(device_t dev)
100 {
101         struct mii_attach_args *ma;
102         ma = device_get_ivars(dev);
103  	if ((MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_TDK ||
104 	     MII_MODEL(ma->mii_id2) != MII_MODEL_TDK_78Q2120))
105 		return (ENXIO);
106 
107 	device_set_desc(dev, MII_STR_TDK_78Q2120);
108 	return (0);
109 }
110 
111 static int
112 tdkphy_attach(device_t dev)
113 {
114 	struct mii_softc *sc;
115 	struct mii_attach_args *ma;
116 	struct mii_data *mii;
117 	sc = device_get_softc(dev);
118 	ma = device_get_ivars(dev);
119 	sc->mii_dev = device_get_parent(dev);
120 	mii = device_get_softc(sc->mii_dev);
121 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
122 
123 	if (bootverbose)
124 		device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n",
125 		    MII_OUI(ma->mii_id1, ma->mii_id2),
126 		    MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
127 
128 	sc->mii_inst = mii->mii_instance;
129 	sc->mii_phy = ma->mii_phyno;
130 	sc->mii_service = tdkphy_service;
131 	sc->mii_pdata = mii;
132 
133 	mii->mii_instance++;
134 
135 	sc->mii_flags |= MIIF_NOISOLATE;
136 
137 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
138 
139 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
140 	    BMCR_ISO);
141 #if 0
142 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
143 	    BMCR_LOOP|BMCR_S100);
144 #endif
145 
146 	mii_phy_reset(sc);
147 
148 	sc->mii_capabilities =
149 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
150 	device_printf(dev, " ");
151 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
152 		printf("no media present");
153 	else
154 		mii_add_media(mii, sc->mii_capabilities,
155 		    sc->mii_inst);
156 	printf("\n");
157 #undef ADD
158 
159 	MIIBUS_MEDIAINIT(sc->mii_dev);
160 
161 	return(0);
162 }
163 
164 static int tdkphy_detach(device_t dev)
165 {
166 	struct mii_softc *sc;
167 	struct mii_data *mii;
168 
169 	sc = device_get_softc(dev);
170 	mii = device_get_softc(device_get_parent(dev));
171 	mii_phy_auto_stop(sc);
172 	sc->mii_dev = NULL;
173 	LIST_REMOVE(sc, mii_list);
174 
175 	return(0);
176 }
177 
178 int
179 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
180 {
181 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
182 	int reg;
183 
184 	switch (cmd) {
185 	case MII_POLLSTAT:
186 		/*
187 		 * If we're not polling our PHY instance, just return.
188 		 */
189 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
190 			return (0);
191 		break;
192 
193 	case MII_MEDIACHG:
194 		/*
195 		 * If the media indicates a different PHY instance,
196 		 * isolate ourselves.
197 		 */
198 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
199 			reg = PHY_READ(sc, MII_BMCR);
200 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
201 			return (0);
202 		}
203 
204 		/*
205 		 * If the interface is not up, don't do anything.
206 		 */
207 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
208 			break;
209 
210 		switch (IFM_SUBTYPE(ife->ifm_media)) {
211 		case IFM_AUTO:
212 			/*
213 			 * If we're already in auto mode, just return.
214 			 */
215 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
216 				return (0);
217 			(void) mii_phy_auto(sc, 1);
218 			break;
219 		case IFM_100_T4:
220 			/*
221 			 * Not supported on MII
222 			 */
223 			return (EINVAL);
224 		default:
225 			/*
226 			 * BMCR data is stored in the ifmedia entry.
227 			 */
228 			PHY_WRITE(sc, MII_ANAR,
229 			    mii_anar(ife->ifm_media));
230 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
231 		}
232 		break;
233 
234 	case MII_TICK:
235 		/*
236 		 * If we're not currently selected, just return.
237 		 */
238 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
239 			return (0);
240 
241 		/*
242 		 * Only used for autonegotiation.
243 		 */
244 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
245 			return (0);
246 
247 		/*
248 		 * Is the interface even up?
249 		 */
250 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
251 			return (0);
252 
253 		/*
254 		 * Check to see if we have link.  If we do, we don't
255 		 * need to restart the autonegotiation process.  Read
256 		 * the BMSR twice in case it's latched.
257 		 */
258 		reg = PHY_READ(sc, MII_BMSR) |
259 		    PHY_READ(sc, MII_BMSR);
260 		if (reg & BMSR_LINK)
261 			return (0);
262 
263 		/*
264 		 * Only retry autonegotiation every 5 seconds.
265 		 */
266 		if (++sc->mii_ticks != 5)
267 			return (0);
268 
269 		sc->mii_ticks = 0;
270 		mii_phy_reset(sc);
271 		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
272 			return (0);
273 		break;
274 	}
275 
276 	/* Update the media status. */
277 	tdkphy_status(sc);
278 	if (sc->mii_pdata->mii_media_active & IFM_FDX)
279 		PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX);
280 	else
281 		PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX);
282 	/* Callback if something changed. */
283 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
284 		MIIBUS_STATCHG(sc->mii_dev);
285 		sc->mii_active = mii->mii_media_active;
286 	}
287 	return (0);
288 }
289 
290 void
291 tdkphy_status(struct mii_softc *phy)
292 {
293 	struct mii_data *mii = phy->mii_pdata;
294 	int bmsr, bmcr, anlpar, diag;
295 
296 	mii->mii_media_status = IFM_AVALID;
297 	mii->mii_media_active = IFM_ETHER;
298 
299 	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
300 	if (bmsr & BMSR_LINK)
301 		mii->mii_media_status |= IFM_ACTIVE;
302 
303 	bmcr = PHY_READ(phy, MII_BMCR);
304 	if (bmcr & BMCR_ISO) {
305 		mii->mii_media_active |= IFM_NONE;
306 		mii->mii_media_status = 0;
307 		return;
308 	}
309 
310 	if (bmcr & BMCR_LOOP)
311 		mii->mii_media_active |= IFM_LOOP;
312 
313 	if (bmcr & BMCR_AUTOEN) {
314 		/*
315 		 * NWay autonegotiation takes the highest-order common
316 		 * bit of the ANAR and ANLPAR (i.e. best media advertised
317 		 * both by us and our link partner).
318 		 */
319 		if ((bmsr & BMSR_ACOMP) == 0) {
320 			/* Erg, still trying, I guess... */
321 			mii->mii_media_active |= IFM_NONE;
322 			return;
323 		}
324 
325 		anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
326 		/*
327 		 * ANLPAR doesn't get set on my card, but we check it anyway,
328 		 * since it is mentioned in the 78Q2120 specs.
329 		 */
330 		if (anlpar & ANLPAR_T4)
331 			mii->mii_media_active |= IFM_100_T4;
332 		else if (anlpar & ANLPAR_TX_FD)
333 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
334 		else if (anlpar & ANLPAR_TX)
335 			mii->mii_media_active |= IFM_100_TX;
336 		else if (anlpar & ANLPAR_10_FD)
337 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
338 		else if (anlpar & ANLPAR_10)
339 			mii->mii_media_active |= IFM_10_T;
340 		else {
341 			/*
342 			 * ANLPAR isn't set, which leaves two possibilities:
343 			 * 1) Auto negotiation failed
344 			 * 2) Auto negotiation completed, but the card forgot
345 			 *    to set ANLPAR.
346 			 * So we check the MII_DIAG(18) register...
347 			 */
348 			diag = PHY_READ(phy, MII_DIAG);
349 			if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */
350 				mii->mii_media_active |= IFM_10_T;
351 			else {
352 				if (diag & DIAG_DUPLEX)
353 					mii->mii_media_active |= IFM_FDX;
354 				if (diag & DIAG_RATE_100)
355 					mii->mii_media_active |= IFM_100_TX;
356 				else
357 					mii->mii_media_active |= IFM_10_T;
358 			}
359 		}
360 	} else {
361 		mii->mii_media_active = mii_media_from_bmcr(bmcr);
362 	}
363 }
364