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