tlphy.c revision 182064
1/*	$NetBSD: tlphy.c,v 1.18 1999/05/14 11:40:28 drochner Exp $	*/
2
3/*-
4 * Copyright (c) 1998, 1999 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#include <sys/cdefs.h>
70__FBSDID("$FreeBSD: head/sys/dev/mii/tlphy.c 182064 2008-08-23 15:29:28Z imp $");
71
72/*
73 * Driver for Texas Instruments's ThunderLAN PHYs
74 */
75
76#include <sys/param.h>
77#include <sys/systm.h>
78#include <sys/kernel.h>
79#include <sys/socket.h>
80#include <sys/errno.h>
81#include <sys/module.h>
82#include <sys/bus.h>
83#include <sys/malloc.h>
84
85#include <machine/bus.h>
86
87#include <net/if.h>
88#include <net/if_media.h>
89
90#include <dev/mii/mii.h>
91#include <dev/mii/miivar.h>
92#include "miidevs.h"
93
94#include <dev/mii/tlphyreg.h>
95
96#include "miibus_if.h"
97
98struct tlphy_softc {
99	struct mii_softc sc_mii;		/* generic PHY */
100	int sc_need_acomp;
101};
102
103static int tlphy_probe(device_t);
104static int tlphy_attach(device_t);
105
106static device_method_t tlphy_methods[] = {
107	/* device interface */
108	DEVMETHOD(device_probe,		tlphy_probe),
109	DEVMETHOD(device_attach,	tlphy_attach),
110	DEVMETHOD(device_detach,	mii_phy_detach),
111	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
112	{ 0, 0 }
113};
114
115static devclass_t tlphy_devclass;
116
117static driver_t tlphy_driver = {
118	"tlphy",
119	tlphy_methods,
120	sizeof(struct tlphy_softc)
121};
122
123DRIVER_MODULE(tlphy, miibus, tlphy_driver, tlphy_devclass, 0, 0);
124
125static int	tlphy_service(struct mii_softc *, struct mii_data *, int);
126static int	tlphy_auto(struct tlphy_softc *);
127static void	tlphy_acomp(struct tlphy_softc *);
128static void	tlphy_status(struct tlphy_softc *);
129
130static const struct mii_phydesc tlphys[] = {
131	MII_PHY_DESC(xxTI, TLAN10T),
132	MII_PHY_END
133};
134
135static int
136tlphy_probe(device_t dev)
137{
138
139	return (mii_phy_dev_probe(dev, tlphys, BUS_PROBE_DEFAULT));
140}
141
142static int
143tlphy_attach(device_t dev)
144{
145	device_t *devlist;
146	struct tlphy_softc *sc;
147	struct mii_softc *other;
148	struct mii_attach_args *ma;
149	struct mii_data *mii;
150	const char *sep = "";
151	int capmask, devs, i;
152
153	sc = device_get_softc(dev);
154	ma = device_get_ivars(dev);
155	sc->sc_mii.mii_dev = device_get_parent(dev);
156	mii = device_get_softc(sc->sc_mii.mii_dev);
157	LIST_INSERT_HEAD(&mii->mii_phys, &sc->sc_mii, mii_list);
158
159	sc->sc_mii.mii_inst = mii->mii_instance;
160	sc->sc_mii.mii_phy = ma->mii_phyno;
161	sc->sc_mii.mii_service = tlphy_service;
162	sc->sc_mii.mii_pdata = mii;
163
164	capmask = 0xFFFFFFFF;
165	if (mii->mii_instance &&
166	    device_get_children(sc->sc_mii.mii_dev, &devlist, &devs) == 0) {
167		for (i = 0; i < devs; i++) {
168			if (strcmp(device_get_name(devlist[i]), "tlphy")) {
169				other = device_get_softc(devlist[i]);
170				capmask &= ~other->mii_capabilities;
171				break;
172			}
173		}
174		free(devlist, M_TEMP);
175	}
176
177	mii->mii_instance++;
178
179	sc->sc_mii.mii_flags &= ~MIIF_NOISOLATE;
180	mii_phy_reset(&sc->sc_mii);
181	sc->sc_mii.mii_flags |= MIIF_NOISOLATE;
182
183	/*
184	 * Note that if we're on a device that also supports 100baseTX,
185	 * we are not going to want to use the built-in 10baseT port,
186	 * since there will be another PHY on the MII wired up to the
187	 * UTP connector.  The parent indicates this to us by specifying
188	 * the TLPHY_MEDIA_NO_10_T bit.
189	 */
190	sc->sc_mii.mii_capabilities =
191	    PHY_READ(&sc->sc_mii, MII_BMSR) & capmask /*ma->mii_capmask*/;
192
193#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
194
195	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
196	    BMCR_ISO);
197
198	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
199	    sc->sc_mii.mii_inst), BMCR_LOOP);
200
201#define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
202
203	device_printf(dev, " ");
204	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->sc_mii.mii_inst), 0);
205	PRINT("10base2/BNC");
206	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, sc->sc_mii.mii_inst), 0);
207	PRINT("10base5/AUI");
208
209	if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
210		printf("%s", sep);
211		mii_add_media(&sc->sc_mii);
212	}
213
214	printf("\n");
215#undef ADD
216#undef PRINT
217	MIIBUS_MEDIAINIT(sc->sc_mii.mii_dev);
218	return (0);
219}
220
221static int
222tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd)
223{
224	struct tlphy_softc *sc = (struct tlphy_softc *)self;
225	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
226	int reg;
227
228	if (sc->sc_need_acomp)
229		tlphy_acomp(sc);
230
231	switch (cmd) {
232	case MII_POLLSTAT:
233		/*
234		 * If we're not polling our PHY instance, just return.
235		 */
236		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
237			return (0);
238		break;
239
240	case MII_MEDIACHG:
241		/*
242		 * If the media indicates a different PHY instance,
243		 * isolate ourselves.
244		 */
245		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
246			reg = PHY_READ(&sc->sc_mii, MII_BMCR);
247			PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
248			return (0);
249		}
250
251		/*
252		 * If the interface is not up, don't do anything.
253		 */
254		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
255			break;
256
257		switch (IFM_SUBTYPE(ife->ifm_media)) {
258		case IFM_AUTO:
259			/*
260			 * The ThunderLAN PHY doesn't self-configure after
261			 * an autonegotiation cycle, so there's no such
262			 * thing as "already in auto mode".
263			 */
264			(void) tlphy_auto(sc);
265			break;
266		case IFM_10_2:
267		case IFM_10_5:
268			PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
269			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
270			DELAY(100000);
271			break;
272		default:
273			PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
274			DELAY(100000);
275			PHY_WRITE(&sc->sc_mii, MII_ANAR,
276			    mii_anar(ife->ifm_media));
277			PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data);
278		}
279		break;
280
281	case MII_TICK:
282		/*
283		 * If we're not currently selected, just return.
284		 */
285		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
286			return (0);
287
288		/*
289		 * Is the interface even up?
290		 */
291		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
292			return (0);
293
294		/*
295		 * Only used for autonegotiation.
296		 */
297		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
298			break;
299
300		/*
301		 * Check to see if we have link.  If we do, we don't
302		 * need to restart the autonegotiation process.  Read
303		 * the BMSR twice in case it's latched.
304		 *
305		 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
306		 */
307		reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
308		    PHY_READ(&sc->sc_mii, MII_BMSR);
309		if (reg & BMSR_LINK)
310			break;
311
312		/*
313		 * Only retry autonegotiation every 5 seconds.
314		 */
315		if (++sc->sc_mii.mii_ticks <= MII_ANEGTICKS)
316			break;
317
318		sc->sc_mii.mii_ticks = 0;
319		mii_phy_reset(&sc->sc_mii);
320		tlphy_auto(sc);
321		return (0);
322	}
323
324	/* Update the media status. */
325	tlphy_status(sc);
326
327	/* Callback if something changed. */
328	mii_phy_update(&sc->sc_mii, cmd);
329	return (0);
330}
331
332static void
333tlphy_status(struct tlphy_softc *sc)
334{
335	struct mii_data *mii = sc->sc_mii.mii_pdata;
336	int bmsr, bmcr, tlctrl;
337
338	mii->mii_media_status = IFM_AVALID;
339	mii->mii_media_active = IFM_ETHER;
340
341	bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
342	if (bmcr & BMCR_ISO) {
343		mii->mii_media_active |= IFM_NONE;
344		mii->mii_media_status = 0;
345		return;
346	}
347
348	tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
349	if (tlctrl & CTRL_AUISEL) {
350		mii->mii_media_status = 0;
351		mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;
352		return;
353	}
354
355	bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
356	    PHY_READ(&sc->sc_mii, MII_BMSR);
357	if (bmsr & BMSR_LINK)
358		mii->mii_media_status |= IFM_ACTIVE;
359
360	if (bmcr & BMCR_LOOP)
361		mii->mii_media_active |= IFM_LOOP;
362
363	/*
364	 * Grr, braindead ThunderLAN PHY doesn't have any way to
365	 * tell which media is actually active.  (Note it also
366	 * doesn't self-configure after autonegotiation.)  We
367	 * just have to report what's in the BMCR.
368	 */
369	if (bmcr & BMCR_FDX)
370		mii->mii_media_active |= IFM_FDX;
371	mii->mii_media_active |= IFM_10_T;
372}
373
374static int
375tlphy_auto(struct tlphy_softc *sc)
376{
377	int error;
378
379	switch ((error = mii_phy_auto(&sc->sc_mii))) {
380	case EIO:
381		/*
382		 * Just assume we're not in full-duplex mode.
383		 * XXX Check link and try AUI/BNC?
384		 */
385		PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
386		break;
387
388	case EJUSTRETURN:
389		/* Flag that we need to program when it completes. */
390		sc->sc_need_acomp = 1;
391		break;
392
393	default:
394		tlphy_acomp(sc);
395	}
396
397	return (error);
398}
399
400static void
401tlphy_acomp(struct tlphy_softc *sc)
402{
403	int aner, anlpar;
404
405	sc->sc_need_acomp = 0;
406
407	/*
408	 * Grr, braindead ThunderLAN PHY doesn't self-configure
409	 * after autonegotiation.  We have to do it ourselves
410	 * based on the link partner status.
411	 */
412
413	aner = PHY_READ(&sc->sc_mii, MII_ANER);
414	if (aner & ANER_LPAN) {
415		anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
416		    PHY_READ(&sc->sc_mii, MII_ANAR);
417		if (anlpar & ANAR_10_FD) {
418			PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
419			return;
420		}
421	}
422	PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
423}
424