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