xref: /netbsd-src/sys/dev/mii/tlphy.c (revision 9589449d9e194dea63f52e004001e8b07924dec4)
1 /*	$NetBSD: tlphy.c,v 1.72 2023/02/22 08:09:09 msaitoh 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.72 2023/02/22 08:09:09 msaitoh 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 *);
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_PHY_DESC(TI, TLAN10T),
111 	MII_PHY_END,
112 };
113 
114 static int
tlphymatch(device_t parent,cfdata_t match,void * aux)115 tlphymatch(device_t parent, cfdata_t match, void *aux)
116 {
117 	struct mii_attach_args *ma = aux;
118 
119 	if (mii_phy_match(ma, tlphys) != NULL)
120 		return 10;
121 
122 	return 0;
123 }
124 
125 static void
tlphyattach(device_t parent,device_t self,void * aux)126 tlphyattach(device_t parent, device_t self, void *aux)
127 {
128 	struct tlphy_softc *tsc = device_private(self);
129 	struct mii_softc *sc = &tsc->sc_mii;
130 	struct tl_softc *tlsc = device_private(device_parent(self));
131 	struct mii_attach_args *ma = aux;
132 	struct mii_data *mii = ma->mii_data;
133 	const struct mii_phydesc *mpd;
134 	const char *sep = "";
135 
136 	mpd = mii_phy_match(ma, tlphys);
137 	aprint_naive(": Media interface\n");
138 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
139 
140 	sc->mii_dev = self;
141 	sc->mii_inst = mii->mii_instance;
142 	sc->mii_phy = ma->mii_phyno;
143 	sc->mii_funcs = &tlphy_funcs;
144 	sc->mii_pdata = mii;
145 	sc->mii_flags = ma->mii_flags;
146 
147 	mii_lock(mii);
148 
149 	PHY_RESET(sc);
150 
151 	/*
152 	 * Note that if we're on a device that also supports 100baseTX, we are
153 	 * not going to want to use the built-in 10baseT port, since there will
154 	 * be another PHY on the MII wired up to the UTP connector.  The parent
155 	 * indicates this to us by specifying the TLPHY_MEDIA_NO_10_T bit.
156 	 */
157 	tsc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
158 	if ((tsc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0) {
159 		PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
160 		sc->mii_capabilities &= ma->mii_capmask;
161 	} else
162 		sc->mii_capabilities = 0;
163 
164 	mii_unlock(mii);
165 
166 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
167 #define	PRINT(str)					     \
168 	do {						     \
169 		aprint_normal("%s%s", sep, str);	     \
170 		sep = ", ";				     \
171 	} while (/* CONSTCOND */0)
172 
173 	if (tsc->sc_tlphycap) {
174 		mii_lock(mii);
175 		sc->mii_anegticks = MII_ANEGTICKS;
176 		mii_unlock(mii);
177 		aprint_normal_dev(self, "");
178 		if (tsc->sc_tlphycap & TLPHY_MEDIA_10_2) {
179 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->mii_inst),
180 			    0);
181 			PRINT("10base2");
182 		} else if (tsc->sc_tlphycap & TLPHY_MEDIA_10_5) {
183 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, sc->mii_inst),
184 			    0);
185 			PRINT("10base5");
186 		} else
187 			PRINT("no media present");
188 		aprint_normal("\n");
189 	}
190 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
191 		mii_phy_add_media(sc);
192 	else {
193 		/*
194 		 * mii_phy_add_media() automatically install power handler,
195 		 * but if_media_add() doesn't. Do it now.
196 		 */
197 		if (!pmf_device_register(self, NULL, mii_phy_resume)) {
198 			aprint_error_dev(self,
199 			    "couldn't establish power handler\n");
200 		}
201 	}
202 #undef ADD
203 #undef PRINT
204 }
205 
206 static int
tlphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)207 tlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
208 {
209 	struct tlphy_softc *tsc = (struct tlphy_softc *)sc;
210 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
211 	uint16_t reg;
212 
213 	KASSERT(mii_locked(mii));
214 
215 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0 && tsc->sc_need_acomp)
216 		tlphy_acomp(tsc);
217 
218 	switch (cmd) {
219 	case MII_POLLSTAT:
220 		/* If we're not polling our PHY instance, just return. */
221 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
222 			return 0;
223 		break;
224 
225 	case MII_MEDIACHG:
226 		/*
227 		 * If the media indicates a different PHY instance,
228 		 * isolate ourselves.
229 		 */
230 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
231 			PHY_READ(sc, MII_BMCR, &reg);
232 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
233 			return 0;
234 		}
235 
236 		/* If the interface is not up, don't do anything. */
237 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
238 			break;
239 
240 		switch (IFM_SUBTYPE(ife->ifm_media)) {
241 		case IFM_AUTO:
242 			(void) tlphy_auto(tsc);
243 			break;
244 		case IFM_10_2:
245 		case IFM_10_5:
246 			PHY_WRITE(sc, MII_BMCR, 0);
247 			PHY_WRITE(sc, MII_TLPHY_CTRL, CTRL_AUISEL);
248 			delay(100000);
249 			break;
250 		default:
251 			PHY_WRITE(sc, MII_TLPHY_CTRL, 0);
252 			delay(100000);
253 			mii_phy_setmedia(sc);
254 		}
255 		break;
256 
257 	case MII_TICK:
258 		/* If we're not currently selected, just return. */
259 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
260 			return 0;
261 
262 		/*
263 		 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
264 		 */
265 
266 		/* Only used for autonegotiation. */
267 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
268 			sc->mii_ticks = 0;
269 			break;
270 		}
271 
272 		/*
273 		 * Check for link.
274 		 * Read the status register twice; BMSR_LINK is latch-low.
275 		 */
276 		PHY_READ(sc, MII_BMSR, &reg);
277 		PHY_READ(sc, MII_BMSR, &reg);
278 		if (reg & BMSR_LINK) {
279 			sc->mii_ticks = 0;
280 			break;
281 		}
282 
283 		/*
284 		 * mii_ticks == 0 means it's the first tick after changing the
285 		 * media or the link became down since the last tick
286 		 * (see above), so break to update the status.
287 		 */
288 		if (sc->mii_ticks++ == 0)
289 			break;
290 
291 		/* Only retry autonegotiation every mii_anegticks seconds. */
292 		KASSERT(sc->mii_anegticks != 0);
293 		if (sc->mii_ticks < sc->mii_anegticks)
294 			break;
295 
296 		tlphy_auto(tsc);
297 		break;
298 
299 	case MII_DOWN:
300 		mii_phy_down(sc);
301 		return 0;
302 	}
303 
304 	/* Update the media status. */
305 	mii_phy_status(sc);
306 
307 	/* Callback if something changed. */
308 	mii_phy_update(sc, cmd);
309 	return 0;
310 }
311 
312 static void
tlphy_status(struct mii_softc * sc)313 tlphy_status(struct mii_softc *sc)
314 {
315 	struct tlphy_softc *tsc = (struct tlphy_softc *)sc;
316 	struct mii_data *mii = sc->mii_pdata;
317 	uint16_t bmsr, bmcr, tlctrl;
318 
319 	KASSERT(mii_locked(mii));
320 
321 	mii->mii_media_status = IFM_AVALID;
322 	mii->mii_media_active = IFM_ETHER;
323 
324 	PHY_READ(sc, MII_BMCR, &bmcr);
325 	if (bmcr & BMCR_ISO) {
326 		mii->mii_media_active |= IFM_NONE;
327 		mii->mii_media_status = 0;
328 		return;
329 	}
330 
331 	PHY_READ(sc, MII_TLPHY_CTRL, &tlctrl);
332 	if (tlctrl & CTRL_AUISEL) {
333 		if (tsc->sc_tlphycap & TLPHY_MEDIA_10_2)
334 			mii->mii_media_active |= IFM_10_2;
335 		else if (tsc->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 			    device_xname(sc->mii_dev));
340 		mii->mii_media_status |= IFM_ACTIVE;
341 		return;
342 	}
343 
344 	PHY_READ(sc, MII_BMSR, &bmsr);
345 	PHY_READ(sc, MII_BMSR, &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 tell which
354 	 * media is actually active.  (Note it also doesn't self-configure
355 	 * after autonegotiation.)  We just have to report what's in the BMCR.
356 	 */
357 	if (bmcr & BMCR_FDX)
358 		mii->mii_media_active |= IFM_FDX;
359 	else
360 		mii->mii_media_active |= IFM_HDX;
361 	mii->mii_media_active |= IFM_10_T;
362 }
363 
364 static int
tlphy_auto(struct tlphy_softc * tsc)365 tlphy_auto(struct tlphy_softc *tsc)
366 {
367 	struct mii_softc *sc = &tsc->sc_mii;
368 	int error;
369 
370 	switch ((error = mii_phy_auto(sc))) {
371 	case EJUSTRETURN:
372 		/* Flag that we need to program when it completes. */
373 		tsc->sc_need_acomp = 1;
374 		break;
375 
376 	default:
377 		tlphy_acomp(tsc);
378 	}
379 
380 	return error;
381 }
382 
383 static void
tlphy_acomp(struct tlphy_softc * tsc)384 tlphy_acomp(struct tlphy_softc *tsc)
385 {
386 	struct mii_softc *sc = &tsc->sc_mii;
387 	uint16_t aner, anar, anlpar, result;
388 
389 	tsc->sc_need_acomp = 0;
390 
391 	/*
392 	 * Grr, braindead ThunderLAN PHY doesn't self-configure
393 	 * after autonegotiation.  We have to do it ourselves
394 	 * based on the link partner status.
395 	 */
396 
397 	PHY_READ(sc, MII_ANER, &aner);
398 	if (aner & ANER_LPAN) {
399 		PHY_READ(sc, MII_ANAR, &anar);
400 		PHY_READ(sc, MII_ANLPAR, &anlpar);
401 		result = anar & anlpar;
402 		if (result & ANAR_10_FD) {
403 			PHY_WRITE(sc, MII_BMCR, BMCR_FDX);
404 			return;
405 		}
406 	}
407 	PHY_WRITE(sc, MII_BMCR, 0);
408 }
409