xref: /netbsd-src/sys/dev/mii/tlphy.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: tlphy.c,v 1.31 2001/08/04 02:21:24 bouyer Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999, 2000 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by Manuel Bouyer.
54  * 4. The name of the author may not be used to endorse or promote products
55  *    derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 /*
70  * Driver for Texas Instruments's ThunderLAN PHYs
71  */
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/device.h>
77 #include <sys/socket.h>
78 #include <sys/errno.h>
79 
80 #include <machine/bus.h>
81 
82 #include <net/if.h>
83 #include <net/if_media.h>
84 
85 #include <net/if_ether.h>
86 
87 #include <dev/mii/mii.h>
88 #include <dev/mii/miivar.h>
89 #include <dev/mii/miidevs.h>
90 
91 #include <dev/i2c/i2c_bus.h>
92 
93 #include <dev/mii/tlphyreg.h>
94 #include <dev/mii/tlphyvar.h>
95 
96 /* ThunderLAN PHY can only be on a ThunderLAN */
97 #include <dev/pci/if_tlvar.h>
98 
99 struct tlphy_softc {
100 	struct mii_softc sc_mii;		/* generic PHY */
101 	int sc_tlphycap;
102 	int sc_need_acomp;
103 };
104 
105 int	tlphymatch __P((struct device *, struct cfdata *, void *));
106 void	tlphyattach __P((struct device *, struct device *, void *));
107 
108 struct cfattach tlphy_ca = {
109 	sizeof(struct tlphy_softc), tlphymatch, tlphyattach, mii_phy_detach,
110 	    mii_phy_activate
111 };
112 
113 int	tlphy_service __P((struct mii_softc *, struct mii_data *, int));
114 int	tlphy_auto __P((struct tlphy_softc *, int));
115 void	tlphy_acomp __P((struct tlphy_softc *));
116 void	tlphy_status __P((struct mii_softc *));
117 
118 const struct mii_phy_funcs tlphy_funcs = {
119 	tlphy_service, tlphy_status, mii_phy_reset,
120 };
121 
122 const struct mii_phydesc tlphys[] = {
123 	{ MII_OUI_TI,		MII_MODEL_TI_TLAN10T,
124 	  MII_STR_TI_TLAN10T },
125 
126 	{ 0,			0,
127 	  NULL },
128 };
129 
130 int
131 tlphymatch(parent, match, aux)
132 	struct device *parent;
133 	struct cfdata *match;
134 	void *aux;
135 {
136 	struct mii_attach_args *ma = aux;
137 
138 	if (mii_phy_match(ma, tlphys) != NULL)
139 		return (10);
140 
141 	return (0);
142 }
143 
144 void
145 tlphyattach(parent, self, aux)
146 	struct device *parent, *self;
147 	void *aux;
148 {
149 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
150 	struct tl_softc *tlsc = (struct tl_softc *)self->dv_parent;
151 	struct mii_attach_args *ma = aux;
152 	struct mii_data *mii = ma->mii_data;
153 	const struct mii_phydesc *mpd;
154 	const char *sep = "";
155 
156 	mpd = mii_phy_match(ma, tlphys);
157 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
158 
159 	sc->sc_mii.mii_inst = mii->mii_instance;
160 	sc->sc_mii.mii_phy = ma->mii_phyno;
161 	sc->sc_mii.mii_funcs = &tlphy_funcs;
162 	sc->sc_mii.mii_pdata = mii;
163 	sc->sc_mii.mii_flags = mii->mii_flags;
164 	sc->sc_mii.mii_anegticks = 5;
165 
166 	PHY_RESET(&sc->sc_mii);
167 
168 	/*
169 	 * Note that if we're on a device that also supports 100baseTX,
170 	 * we are not going to want to use the built-in 10baseT port,
171 	 * since there will be another PHY on the MII wired up to the
172 	 * UTP connector.  The parent indicates this to us by specifying
173 	 * the TLPHY_MEDIA_NO_10_T bit.
174 	 */
175 	sc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
176 	if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
177 		sc->sc_mii.mii_capabilities =
178 		    PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
179 	else
180 		sc->sc_mii.mii_capabilities = 0;
181 
182 
183 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
184 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
185 
186 	printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
187 	if (sc->sc_tlphycap) {
188 		if (sc->sc_tlphycap & TLPHY_MEDIA_10_2) {
189 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
190 			    sc->sc_mii.mii_inst), 0);
191 			PRINT("10base2");
192 		} else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5) {
193 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
194 			    sc->sc_mii.mii_inst), 0);
195 			PRINT("10base5");
196 		}
197 	}
198 	if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
199 		printf("%s", sep);
200 		mii_phy_add_media(&sc->sc_mii);
201 	} else if ((sc->sc_tlphycap &
202 		    (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5)) == 0)
203 		printf("no media present");
204 	printf("\n");
205 #undef ADD
206 #undef PRINT
207 }
208 
209 int
210 tlphy_service(self, mii, cmd)
211 	struct mii_softc *self;
212 	struct mii_data *mii;
213 	int cmd;
214 {
215 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
216 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
217 	int reg;
218 
219 	if ((sc->sc_mii.mii_dev.dv_flags & DVF_ACTIVE) == 0)
220 		return (ENXIO);
221 
222 	if ((sc->sc_mii.mii_flags & MIIF_DOINGAUTO) == 0 && sc->sc_need_acomp)
223 		tlphy_acomp(sc);
224 
225 	switch (cmd) {
226 	case MII_POLLSTAT:
227 		/*
228 		 * If we're not polling our PHY instance, just return.
229 		 */
230 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
231 			return (0);
232 		break;
233 
234 	case MII_MEDIACHG:
235 		/*
236 		 * If the media indicates a different PHY instance,
237 		 * isolate ourselves.
238 		 */
239 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
240 			reg = PHY_READ(&sc->sc_mii, MII_BMCR);
241 			PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
242 			return (0);
243 		}
244 
245 		/*
246 		 * If the interface is not up, don't do anything.
247 		 */
248 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
249 			break;
250 
251 		switch (IFM_SUBTYPE(ife->ifm_media)) {
252 		case IFM_AUTO:
253 			/*
254 			 * The ThunderLAN PHY doesn't self-configure after
255 			 * an autonegotiation cycle, so there's no such
256 			 * thing as "already in auto mode".
257 			 */
258 			(void) tlphy_auto(sc, 1);
259 			break;
260 		case IFM_10_2:
261 		case IFM_10_5:
262 			PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
263 			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
264 			delay(100000);
265 			break;
266 		default:
267 			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
268 			delay(100000);
269 			mii_phy_setmedia(&sc->sc_mii);
270 		}
271 		break;
272 
273 	case MII_TICK:
274 		/*
275 		 * If we're not currently selected, just return.
276 		 */
277 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
278 			return (0);
279 
280 		/*
281 		 * Only used for autonegotiation.
282 		 */
283 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
284 			return (0);
285 
286 		/*
287 		 * Is the interface even up?
288 		 */
289 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
290 			return (0);
291 
292 		/*
293 		 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
294 		 */
295 
296 		if (mii_phy_tick(&sc->sc_mii) == EJUSTRETURN)
297 			return (0);
298 		break;
299 
300 	case MII_DOWN:
301 		mii_phy_down(&sc->sc_mii);
302 		return (0);
303 	}
304 
305 	/* Update the media status. */
306 	mii_phy_status(&sc->sc_mii);
307 
308 	/* Callback if something changed. */
309 	mii_phy_update(&sc->sc_mii, cmd);
310 	return (0);
311 }
312 
313 void
314 tlphy_status(physc)
315 	struct mii_softc *physc;
316 {
317 	struct tlphy_softc *sc = (void *) physc;
318 	struct mii_data *mii = sc->sc_mii.mii_pdata;
319 	int bmsr, bmcr, tlctrl;
320 
321 	mii->mii_media_status = IFM_AVALID;
322 	mii->mii_media_active = IFM_ETHER;
323 
324 	bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
325 	if (bmcr & BMCR_ISO) {
326 		mii->mii_media_active |= IFM_NONE;
327 		mii->mii_media_status = 0;
328 		return;
329 	}
330 
331 	tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
332 	if (tlctrl & CTRL_AUISEL) {
333 		if (sc->sc_tlphycap & TLPHY_MEDIA_10_2)
334 			mii->mii_media_active |= IFM_10_2;
335 		else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5)
336 			mii->mii_media_active |= IFM_10_5;
337 		else
338 			printf("%s: AUI selected with no matching media !\n",
339 			    sc->sc_mii.mii_dev.dv_xname);
340 		mii->mii_media_status |= IFM_ACTIVE;
341 		return;
342 	}
343 
344 	bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
345 	    PHY_READ(&sc->sc_mii, MII_BMSR);
346 	if (bmsr & BMSR_LINK)
347 		mii->mii_media_status |= IFM_ACTIVE;
348 
349 	if (bmcr & BMCR_LOOP)
350 		mii->mii_media_active |= IFM_LOOP;
351 
352 	/*
353 	 * Grr, braindead ThunderLAN PHY doesn't have any way to
354 	 * tell which media is actually active.  (Note it also
355 	 * doesn't self-configure after autonegotiation.)  We
356 	 * just have to report what's in the BMCR.
357 	 */
358 	if (bmcr & BMCR_FDX)
359 		mii->mii_media_active |= IFM_FDX;
360 	mii->mii_media_active |= IFM_10_T;
361 }
362 
363 int
364 tlphy_auto(sc, waitfor)
365 	struct tlphy_softc *sc;
366 	int waitfor;
367 {
368 	int error;
369 
370 	switch ((error = mii_phy_auto(&sc->sc_mii, waitfor))) {
371 	case EIO:
372 		/*
373 		 * Just assume we're not in full-duplex mode.
374 		 * XXX Check link and try AUI/BNC?
375 		 */
376 		PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
377 		break;
378 
379 	case EJUSTRETURN:
380 		/* Flag that we need to program when it completes. */
381 		sc->sc_need_acomp = 1;
382 		break;
383 
384 	default:
385 		tlphy_acomp(sc);
386 	}
387 
388 	return (error);
389 }
390 
391 void
392 tlphy_acomp(sc)
393 	struct tlphy_softc *sc;
394 {
395 	int aner, anlpar;
396 
397 	sc->sc_need_acomp = 0;
398 
399 	/*
400 	 * Grr, braindead ThunderLAN PHY doesn't self-configure
401 	 * after autonegotiation.  We have to do it ourselves
402 	 * based on the link partner status.
403 	 */
404 
405 	aner = PHY_READ(&sc->sc_mii, MII_ANER);
406 	if (aner & ANER_LPAN) {
407 		anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
408 		    PHY_READ(&sc->sc_mii, MII_ANAR);
409 		if (anlpar & ANAR_10_FD) {
410 			PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
411 			return;
412 		}
413 	}
414 	PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
415 }
416