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