xref: /netbsd-src/sys/dev/mii/tlphy.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: tlphy.c,v 1.39 2003/04/29 01:49:34 thorpej 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/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: tlphy.c,v 1.39 2003/04/29 01:49:34 thorpej Exp $");
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/device.h>
80 #include <sys/socket.h>
81 #include <sys/errno.h>
82 
83 #include <machine/bus.h>
84 
85 #include <net/if.h>
86 #include <net/if_media.h>
87 
88 #include <net/if_ether.h>
89 
90 #include <dev/mii/mii.h>
91 #include <dev/mii/miivar.h>
92 #include <dev/mii/miidevs.h>
93 
94 #include <dev/i2c/i2c_bus.h>
95 
96 #include <dev/mii/tlphyreg.h>
97 #include <dev/mii/tlphyvar.h>
98 
99 /* ThunderLAN PHY can only be on a ThunderLAN */
100 #include <dev/pci/if_tlvar.h>
101 
102 struct tlphy_softc {
103 	struct mii_softc sc_mii;		/* generic PHY */
104 	int sc_tlphycap;
105 	int sc_need_acomp;
106 };
107 
108 int	tlphymatch(struct device *, struct cfdata *, void *);
109 void	tlphyattach(struct device *, struct device *, void *);
110 
111 CFATTACH_DECL(tlphy, sizeof(struct tlphy_softc),
112     tlphymatch, tlphyattach, mii_phy_detach, mii_phy_activate);
113 
114 int	tlphy_service(struct mii_softc *, struct mii_data *, int);
115 int	tlphy_auto(struct tlphy_softc *, int);
116 void	tlphy_acomp(struct tlphy_softc *);
117 void	tlphy_status(struct mii_softc *);
118 
119 const struct mii_phy_funcs tlphy_funcs = {
120 	tlphy_service, tlphy_status, mii_phy_reset,
121 };
122 
123 const struct mii_phydesc tlphys[] = {
124 	{ MII_OUI_TI,		MII_MODEL_TI_TLAN10T,
125 	  MII_STR_TI_TLAN10T },
126 
127 	{ 0,			0,
128 	  NULL },
129 };
130 
131 int
132 tlphymatch(struct device *parent, struct cfdata *match, void *aux)
133 {
134 	struct mii_attach_args *ma = aux;
135 
136 	if (mii_phy_match(ma, tlphys) != NULL)
137 		return (10);
138 
139 	return (0);
140 }
141 
142 void
143 tlphyattach(struct device *parent, struct device *self, void *aux)
144 {
145 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
146 	struct tl_softc *tlsc = (struct tl_softc *)self->dv_parent;
147 	struct mii_attach_args *ma = aux;
148 	struct mii_data *mii = ma->mii_data;
149 	const struct mii_phydesc *mpd;
150 	const char *sep = "";
151 
152 	mpd = mii_phy_match(ma, tlphys);
153 	aprint_naive(": Media interface\n");
154 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
155 
156 	sc->sc_mii.mii_inst = mii->mii_instance;
157 	sc->sc_mii.mii_phy = ma->mii_phyno;
158 	sc->sc_mii.mii_funcs = &tlphy_funcs;
159 	sc->sc_mii.mii_pdata = mii;
160 	sc->sc_mii.mii_flags = ma->mii_flags;
161 	sc->sc_mii.mii_anegticks = 5;
162 
163 	PHY_RESET(&sc->sc_mii);
164 
165 	/*
166 	 * Note that if we're on a device that also supports 100baseTX,
167 	 * we are not going to want to use the built-in 10baseT port,
168 	 * since there will be another PHY on the MII wired up to the
169 	 * UTP connector.  The parent indicates this to us by specifying
170 	 * the TLPHY_MEDIA_NO_10_T bit.
171 	 */
172 	sc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
173 	if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
174 		sc->sc_mii.mii_capabilities =
175 		    PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
176 	else
177 		sc->sc_mii.mii_capabilities = 0;
178 
179 
180 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
181 #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
182 
183 	aprint_normal("%s: ", sc->sc_mii.mii_dev.dv_xname);
184 	if (sc->sc_tlphycap) {
185 		if (sc->sc_tlphycap & TLPHY_MEDIA_10_2) {
186 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
187 			    sc->sc_mii.mii_inst), 0);
188 			PRINT("10base2");
189 		} else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5) {
190 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
191 			    sc->sc_mii.mii_inst), 0);
192 			PRINT("10base5");
193 		}
194 	}
195 	if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
196 		aprint_normal("%s", sep);
197 		mii_phy_add_media(&sc->sc_mii);
198 	} else if ((sc->sc_tlphycap &
199 		    (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5)) == 0)
200 		aprint_error("no media present");
201 	aprint_normal("\n");
202 #undef ADD
203 #undef PRINT
204 }
205 
206 int
207 tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd)
208 {
209 	struct tlphy_softc *sc = (struct tlphy_softc *)self;
210 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
211 	int reg;
212 
213 	if ((sc->sc_mii.mii_dev.dv_flags & DVF_ACTIVE) == 0)
214 		return (ENXIO);
215 
216 	if ((sc->sc_mii.mii_flags & MIIF_DOINGAUTO) == 0 && sc->sc_need_acomp)
217 		tlphy_acomp(sc);
218 
219 	switch (cmd) {
220 	case MII_POLLSTAT:
221 		/*
222 		 * If we're not polling our PHY instance, just return.
223 		 */
224 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
225 			return (0);
226 		break;
227 
228 	case MII_MEDIACHG:
229 		/*
230 		 * If the media indicates a different PHY instance,
231 		 * isolate ourselves.
232 		 */
233 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
234 			reg = PHY_READ(&sc->sc_mii, MII_BMCR);
235 			PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
236 			return (0);
237 		}
238 
239 		/*
240 		 * If the interface is not up, don't do anything.
241 		 */
242 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
243 			break;
244 
245 		switch (IFM_SUBTYPE(ife->ifm_media)) {
246 		case IFM_AUTO:
247 			/*
248 			 * The ThunderLAN PHY doesn't self-configure after
249 			 * an autonegotiation cycle, so there's no such
250 			 * thing as "already in auto mode".
251 			 */
252 			(void) tlphy_auto(sc, 1);
253 			break;
254 		case IFM_10_2:
255 		case IFM_10_5:
256 			PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
257 			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
258 			delay(100000);
259 			break;
260 		default:
261 			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
262 			delay(100000);
263 			mii_phy_setmedia(&sc->sc_mii);
264 		}
265 		break;
266 
267 	case MII_TICK:
268 		/*
269 		 * If we're not currently selected, just return.
270 		 */
271 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
272 			return (0);
273 
274 		/*
275 		 * Only used for autonegotiation.
276 		 */
277 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
278 			return (0);
279 
280 		/*
281 		 * Is the interface even up?
282 		 */
283 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
284 			return (0);
285 
286 		/*
287 		 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
288 		 */
289 
290 		if (mii_phy_tick(&sc->sc_mii) == EJUSTRETURN)
291 			return (0);
292 		break;
293 
294 	case MII_DOWN:
295 		mii_phy_down(&sc->sc_mii);
296 		return (0);
297 	}
298 
299 	/* Update the media status. */
300 	mii_phy_status(&sc->sc_mii);
301 
302 	/* Callback if something changed. */
303 	mii_phy_update(&sc->sc_mii, cmd);
304 	return (0);
305 }
306 
307 void
308 tlphy_status(struct mii_softc *physc)
309 {
310 	struct tlphy_softc *sc = (void *) physc;
311 	struct mii_data *mii = sc->sc_mii.mii_pdata;
312 	int bmsr, bmcr, tlctrl;
313 
314 	mii->mii_media_status = IFM_AVALID;
315 	mii->mii_media_active = IFM_ETHER;
316 
317 	bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
318 	if (bmcr & BMCR_ISO) {
319 		mii->mii_media_active |= IFM_NONE;
320 		mii->mii_media_status = 0;
321 		return;
322 	}
323 
324 	tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
325 	if (tlctrl & CTRL_AUISEL) {
326 		if (sc->sc_tlphycap & TLPHY_MEDIA_10_2)
327 			mii->mii_media_active |= IFM_10_2;
328 		else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5)
329 			mii->mii_media_active |= IFM_10_5;
330 		else
331 			printf("%s: AUI selected with no matching media !\n",
332 			    sc->sc_mii.mii_dev.dv_xname);
333 		mii->mii_media_status |= IFM_ACTIVE;
334 		return;
335 	}
336 
337 	bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
338 	    PHY_READ(&sc->sc_mii, MII_BMSR);
339 	if (bmsr & BMSR_LINK)
340 		mii->mii_media_status |= IFM_ACTIVE;
341 
342 	if (bmcr & BMCR_LOOP)
343 		mii->mii_media_active |= IFM_LOOP;
344 
345 	/*
346 	 * Grr, braindead ThunderLAN PHY doesn't have any way to
347 	 * tell which media is actually active.  (Note it also
348 	 * doesn't self-configure after autonegotiation.)  We
349 	 * just have to report what's in the BMCR.
350 	 */
351 	if (bmcr & BMCR_FDX)
352 		mii->mii_media_active |= IFM_FDX;
353 	mii->mii_media_active |= IFM_10_T;
354 }
355 
356 int
357 tlphy_auto(struct tlphy_softc *sc, int waitfor)
358 {
359 	int error;
360 
361 	switch ((error = mii_phy_auto(&sc->sc_mii, waitfor))) {
362 	case EIO:
363 		/*
364 		 * Just assume we're not in full-duplex mode.
365 		 * XXX Check link and try AUI/BNC?
366 		 */
367 		PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
368 		break;
369 
370 	case EJUSTRETURN:
371 		/* Flag that we need to program when it completes. */
372 		sc->sc_need_acomp = 1;
373 		break;
374 
375 	default:
376 		tlphy_acomp(sc);
377 	}
378 
379 	return (error);
380 }
381 
382 void
383 tlphy_acomp(struct tlphy_softc *sc)
384 {
385 	int aner, anlpar;
386 
387 	sc->sc_need_acomp = 0;
388 
389 	/*
390 	 * Grr, braindead ThunderLAN PHY doesn't self-configure
391 	 * after autonegotiation.  We have to do it ourselves
392 	 * based on the link partner status.
393 	 */
394 
395 	aner = PHY_READ(&sc->sc_mii, MII_ANER);
396 	if (aner & ANER_LPAN) {
397 		anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
398 		    PHY_READ(&sc->sc_mii, MII_ANAR);
399 		if (anlpar & ANAR_10_FD) {
400 			PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
401 			return;
402 		}
403 	}
404 	PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
405 }
406