acphy.c revision 84145
1/*-
2 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7 * NASA Ames Research Center.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the NetBSD
20 *	Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 *    contributors may be used to endorse or promote products derived
23 *    from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 *    notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 *    notice, this list of conditions and the following disclaimer in the
48 *    documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 *    must display the following acknowledgement:
51 *	This product includes software developed by Manuel Bouyer.
52 * 4. The name of the author may not be used to endorse or promote products
53 *    derived from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
56 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
59 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
60 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
64 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 */
66
67/*
68 * Driver for Altima AC101 10/100 PHY
69 */
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/kernel.h>
74#include <sys/socket.h>
75#include <sys/errno.h>
76#include <sys/module.h>
77#include <sys/bus.h>
78
79#include <net/if.h>
80#include <net/if_media.h>
81
82#include <dev/mii/mii.h>
83#include <dev/mii/miivar.h>
84#include <dev/mii/miidevs.h>
85
86#include <dev/mii/acphyreg.h>
87
88#include "miibus_if.h"
89
90#if !defined(lint)
91static const char rcsid[] =
92  "$FreeBSD: head/sys/dev/mii/acphy.c 84145 2001-09-29 19:18:52Z jlemon $";
93#endif
94
95static int acphy_probe		__P((device_t));
96static int acphy_attach		__P((device_t));
97static int acphy_detach		__P((device_t));
98
99static device_method_t acphy_methods[] = {
100	/* device interface */
101	DEVMETHOD(device_probe,		acphy_probe),
102	DEVMETHOD(device_attach,	acphy_attach),
103	DEVMETHOD(device_detach,	acphy_detach),
104	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
105	{ 0, 0 }
106};
107
108static devclass_t acphy_devclass;
109
110static driver_t acphy_driver = {
111	"acphy",
112	acphy_methods,
113	sizeof(struct mii_softc)
114};
115
116DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, 0, 0);
117
118static int	acphy_service __P((struct mii_softc *, struct mii_data *, int));
119static void	acphy_reset __P((struct mii_softc *));
120static void	acphy_status __P((struct mii_softc *));
121
122static int acphy_probe(dev)
123	device_t		dev;
124{
125	struct mii_attach_args *ma;
126
127	ma = device_get_ivars(dev);
128
129	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA &&
130	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101) {
131		device_set_desc(dev, MII_STR_xxALTIMA_AC101);
132	} else
133		return (ENXIO);
134
135	return (0);
136}
137
138static int acphy_attach(dev)
139	device_t		dev;
140{
141	struct mii_softc *sc;
142	struct mii_attach_args *ma;
143	struct mii_data *mii;
144
145	sc = device_get_softc(dev);
146	ma = device_get_ivars(dev);
147	sc->mii_dev = device_get_parent(dev);
148	mii = device_get_softc(sc->mii_dev);
149	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
150
151	sc->mii_inst = mii->mii_instance;
152	sc->mii_phy = ma->mii_phyno;
153	sc->mii_service = acphy_service;
154	sc->mii_pdata = mii;
155	sc->mii_flags |= MIIF_NOISOLATE;
156
157	acphy_reset(sc);
158
159	mii->mii_instance++;
160
161	sc->mii_capabilities =
162	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
163	device_printf(dev, " ");
164	if (sc->mii_capabilities & BMSR_MEDIAMASK)
165		mii_add_media(mii, sc->mii_capabilities,
166		    sc->mii_inst);
167	printf("\n");
168
169	MIIBUS_MEDIAINIT(sc->mii_dev);
170	return (0);
171}
172
173static int acphy_detach(dev)
174	device_t		dev;
175{
176	struct mii_softc *sc;
177	struct mii_data *mii;
178
179	sc = device_get_softc(dev);
180	mii = device_get_softc(device_get_parent(dev));
181	sc->mii_dev = NULL;
182	LIST_REMOVE(sc, mii_list);
183
184	return(0);
185}
186
187static int
188acphy_service(sc, mii, cmd)
189	struct mii_softc *sc;
190	struct mii_data *mii;
191	int cmd;
192{
193	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
194	int reg;
195
196	switch (cmd) {
197	case MII_POLLSTAT:
198		/*
199		 * If we're not polling our PHY instance, just return.
200		 */
201		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
202			return (0);
203		break;
204
205	case MII_MEDIACHG:
206		/*
207		 * If the media indicates a different PHY instance,
208		 * isolate ourselves.
209		 */
210		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
211			reg = PHY_READ(sc, MII_BMCR);
212			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO | BMCR_PDOWN);
213			return (0);
214		}
215
216		/*
217		 * If the interface is not up, don't do anything.
218		 */
219		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
220			break;
221
222		/* Wake & deisolate up is needed */
223		reg = PHY_READ(sc, MII_BMCR);
224		if (reg & (BMCR_ISO | BMCR_PDOWN))
225			PHY_WRITE(sc, MII_BMCR, reg & ~(BMCR_ISO | BMCR_PDOWN));
226
227		switch (IFM_SUBTYPE(ife->ifm_media)) {
228		case IFM_AUTO:
229			/*
230			 * If we're already in auto mode, just return.
231			 */
232			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
233				return (0);
234
235			(void) mii_phy_auto(sc, 1);
236			break;
237
238		default:
239			/*
240			 * BMCR data is stored in the ifmedia entry.
241			 */
242			PHY_WRITE(sc, MII_ANAR,
243			    mii_anar(ife->ifm_media));
244			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
245		}
246		break;
247
248	case MII_TICK:
249		/*
250		 * If we're not currently selected, just return.
251		 */
252		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
253			return (0);
254
255		/*
256		 * Is the interface even up?
257		 */
258		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
259			return (0);
260
261		/*
262		 * Only used for autonegotiation.
263		 */
264		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
265			break;
266
267		/*
268		 * check for link.
269		 * Read the status register twice; BMSR_LINK is latch-low.
270		 */
271		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
272		if (reg & BMSR_LINK)
273			break;
274
275		/*
276		 * Only retry autonegotiation every 5 seconds.
277		 */
278		if (++sc->mii_ticks != 5)
279			return (0);
280
281		sc->mii_ticks = 0;
282		acphy_reset(sc);
283		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
284			return (0);
285		break;
286	}
287
288	/* Update the media status. */
289	acphy_status(sc);
290
291	/* Callback if something changed. */
292	mii_phy_update(sc, cmd);
293	return (0);
294}
295
296static void
297acphy_status(sc)
298	struct mii_softc *sc;
299{
300	struct mii_data *mii = sc->mii_pdata;
301	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
302	int bmsr, bmcr, diag;
303
304	mii->mii_media_status = IFM_AVALID;
305	mii->mii_media_active = IFM_ETHER;
306
307	bmsr = PHY_READ(sc, MII_BMSR) |
308	    PHY_READ(sc, MII_BMSR);
309	if (bmsr & BMSR_LINK)
310		mii->mii_media_status |= IFM_ACTIVE;
311
312	bmcr = PHY_READ(sc, MII_BMCR);
313	if (bmcr & BMCR_ISO) {
314		mii->mii_media_active |= IFM_NONE;
315		mii->mii_media_status = 0;
316		return;
317	}
318
319	if (bmcr & BMCR_LOOP)
320		mii->mii_media_active |= IFM_LOOP;
321
322	if (bmcr & BMCR_AUTOEN) {
323		if ((bmsr & BMSR_ACOMP) == 0) {
324			/* Erg, still trying, I guess... */
325			mii->mii_media_active |= IFM_NONE;
326			return;
327		}
328		diag = PHY_READ(sc, MII_ACPHY_DIAG) |
329		    PHY_READ(sc, MII_ACPHY_DIAG);
330		if (diag & AC_DIAG_SPEED)
331			mii->mii_media_active |= IFM_100_TX;
332		else
333			mii->mii_media_active |= IFM_10_T;
334
335		if (diag & AC_DIAG_DUPLEX)
336			mii->mii_media_active |= IFM_FDX;
337	} else
338		mii->mii_media_active = ife->ifm_media;
339}
340
341static void
342acphy_reset(sc)
343	struct mii_softc *sc;
344{
345
346	mii_phy_reset(sc);
347	PHY_WRITE(sc, MII_ACPHY_INT, 0);
348}
349