brgphy.c revision 59477
1/*
2 * Copyright (c) 2000
3 *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
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
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $FreeBSD: head/sys/dev/mii/brgphy.c 59477 2000-04-22 01:58:18Z wpaul $
33 */
34
35/*
36 * Driver for the Broadcom BCR5400 1000baseTX PHY. Speed is always
37 * 1000mbps; all we need to negotiate here is full or half duplex.
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/socket.h>
45#include <sys/bus.h>
46
47#include <machine/clock.h>
48
49#include <net/if.h>
50#include <net/if_media.h>
51
52#include <dev/mii/mii.h>
53#include <dev/mii/miivar.h>
54#include <dev/mii/miidevs.h>
55
56#include <dev/mii/brgphyreg.h>
57
58#include "miibus_if.h"
59
60#if !defined(lint)
61static const char rcsid[] =
62  "$FreeBSD: head/sys/dev/mii/brgphy.c 59477 2000-04-22 01:58:18Z wpaul $";
63#endif
64
65static int brgphy_probe		__P((device_t));
66static int brgphy_attach		__P((device_t));
67static int brgphy_detach		__P((device_t));
68
69static device_method_t brgphy_methods[] = {
70	/* device interface */
71	DEVMETHOD(device_probe,		brgphy_probe),
72	DEVMETHOD(device_attach,	brgphy_attach),
73	DEVMETHOD(device_detach,	brgphy_detach),
74	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
75	{ 0, 0 }
76};
77
78static devclass_t brgphy_devclass;
79
80static driver_t brgphy_driver = {
81	"brgphy",
82	brgphy_methods,
83	sizeof(struct mii_softc)
84};
85
86DRIVER_MODULE(brgphy, miibus, brgphy_driver, brgphy_devclass, 0, 0);
87
88int	brgphy_service __P((struct mii_softc *, struct mii_data *, int));
89void	brgphy_status __P((struct mii_softc *));
90
91static int	brgphy_mii_phy_auto __P((struct mii_softc *, int));
92extern void	mii_phy_auto_timeout __P((void *));
93
94static int brgphy_probe(dev)
95	device_t		dev;
96{
97	struct mii_attach_args *ma;
98
99	ma = device_get_ivars(dev);
100
101	if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_xxBROADCOM ||
102	    MII_MODEL(ma->mii_id2) != MII_MODEL_xxBROADCOM_BCM5400)
103		return(ENXIO);
104
105	device_set_desc(dev, MII_STR_xxBROADCOM_BCM5400);
106
107	return(0);
108}
109
110static int brgphy_attach(dev)
111	device_t		dev;
112{
113	struct mii_softc *sc;
114	struct mii_attach_args *ma;
115	struct mii_data *mii;
116	const char *sep = "";
117
118	sc = device_get_softc(dev);
119	ma = device_get_ivars(dev);
120	sc->mii_dev = device_get_parent(dev);
121	mii = device_get_softc(sc->mii_dev);
122	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
123
124	sc->mii_inst = mii->mii_instance;
125	sc->mii_phy = ma->mii_phyno;
126	sc->mii_service = brgphy_service;
127	sc->mii_pdata = mii;
128
129	sc->mii_flags |= MIIF_NOISOLATE;
130	mii->mii_instance++;
131
132#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
133#define PRINT(s)	printf("%s%s", sep, s); sep = ", "
134
135	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
136	    BMCR_ISO);
137#if 0
138	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
139	    BMCR_LOOP|BMCR_S100);
140#endif
141
142	mii_phy_reset(sc);
143
144	device_printf(dev, " ");
145	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst),
146	    BRGPHY_BMCR_FDX);
147	PRINT("1000baseTX");
148	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst), 0);
149	PRINT("1000baseTX-FDX");
150	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
151	PRINT("auto");
152
153	printf("\n");
154#undef ADD
155#undef PRINT
156
157	MIIBUS_MEDIAINIT(sc->mii_dev);
158	return(0);
159}
160
161static int brgphy_detach(dev)
162	device_t		dev;
163{
164	struct mii_softc *sc;
165	struct mii_data *mii;
166
167	sc = device_get_softc(dev);
168	mii = device_get_softc(device_get_parent(dev));
169	sc->mii_dev = NULL;
170	LIST_REMOVE(sc, mii_list);
171
172	return(0);
173}
174int
175brgphy_service(sc, mii, cmd)
176	struct mii_softc *sc;
177	struct mii_data *mii;
178	int cmd;
179{
180	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
181	int reg;
182
183	switch (cmd) {
184	case MII_POLLSTAT:
185		/*
186		 * If we're not polling our PHY instance, just return.
187		 */
188		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
189			return (0);
190		break;
191
192	case MII_MEDIACHG:
193		/*
194		 * If the media indicates a different PHY instance,
195		 * isolate ourselves.
196		 */
197		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
198			reg = PHY_READ(sc, MII_BMCR);
199			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
200			return (0);
201		}
202
203		/*
204		 * If the interface is not up, don't do anything.
205		 */
206		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
207			break;
208
209		PHY_WRITE(sc, BRGPHY_MII_PHY_EXTCTL,
210		    BRGPHY_PHY_EXTCTL_HIGH_LA|BRGPHY_PHY_EXTCTL_EN_LTR);
211		PHY_WRITE(sc, BRGPHY_MII_AUXCTL,
212		    BRGPHY_AUXCTL_LONG_PKT|BRGPHY_AUXCTL_TX_TST);
213		PHY_WRITE(sc, BRGPHY_MII_IMR, 0xFF00);
214
215		switch (IFM_SUBTYPE(ife->ifm_media)) {
216		case IFM_AUTO:
217#ifdef foo
218			/*
219			 * If we're already in auto mode, just return.
220			 */
221			if (PHY_READ(sc, BRGPHY_MII_BMCR) & BRGPHY_BMCR_AUTOEN)
222				return (0);
223#endif
224			(void) brgphy_mii_phy_auto(sc, 1);
225			break;
226		case IFM_1000_TX:
227			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
228				PHY_WRITE(sc, BRGPHY_MII_BMCR,
229				    BRGPHY_BMCR_FDX|BRGPHY_BMCR_SPD1);
230			} else {
231				PHY_WRITE(sc, BRGPHY_MII_BMCR,
232				    BRGPHY_BMCR_SPD1);
233			}
234			PHY_WRITE(sc, BRGPHY_MII_ANAR, BRGPHY_SEL_TYPE);
235
236			/*
237			 * When settning the link manually, one side must
238			 * be the master and the other the slave. However
239			 * ifmedia doesn't give us a good way to specify
240			 * this, so we fake it by using one of the LINK
241			 * flags. If LINK0 is set, we program the PHY to
242			 * be a master, otherwise it's a slave.
243			 */
244			if ((mii->mii_ifp->if_flags & IFF_LINK0)) {
245				PHY_WRITE(sc, BRGPHY_MII_1000CTL,
246				    BRGPHY_1000CTL_MSE|BRGPHY_1000CTL_MSC);
247			} else {
248				PHY_WRITE(sc, BRGPHY_MII_1000CTL,
249				    BRGPHY_1000CTL_MSE);
250			}
251			break;
252		case IFM_100_T4:
253		case IFM_100_TX:
254		case IFM_10_T:
255		default:
256			return (EINVAL);
257		}
258		break;
259
260	case MII_TICK:
261		/*
262		 * If we're not currently selected, just return.
263		 */
264		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
265			return (0);
266
267		/*
268		 * Only used for autonegotiation.
269		 */
270		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
271			return (0);
272
273		/*
274		 * Is the interface even up?
275		 */
276		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
277			return (0);
278
279		/*
280		 * Only retry autonegotiation every 5 seconds.
281		 */
282		if (++sc->mii_ticks != 5)
283			return (0);
284
285		sc->mii_ticks = 0;
286
287		/*
288		 * Check to see if we have link.  If we do, we don't
289		 * need to restart the autonegotiation process.  Read
290		 * the BMSR twice in case it's latched.
291		 */
292		reg = PHY_READ(sc, BRGPHY_MII_AUXSTS);
293		if (reg & BRGPHY_AUXSTS_LINK)
294			break;
295
296		mii_phy_reset(sc);
297		if (brgphy_mii_phy_auto(sc, 0) == EJUSTRETURN)
298			return(0);
299		break;
300	}
301
302	/* Update the media status. */
303	brgphy_status(sc);
304
305	/* Callback if something changed. */
306	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
307		MIIBUS_STATCHG(sc->mii_dev);
308		sc->mii_active = mii->mii_media_active;
309	}
310	return (0);
311}
312
313void
314brgphy_status(sc)
315	struct mii_softc *sc;
316{
317	struct mii_data *mii = sc->mii_pdata;
318	int bmsr, bmcr, anlpar;
319
320	mii->mii_media_status = IFM_AVALID;
321	mii->mii_media_active = IFM_ETHER;
322
323	bmsr = PHY_READ(sc, BRGPHY_MII_BMSR);
324	if (PHY_READ(sc, BRGPHY_MII_AUXSTS) & BRGPHY_AUXSTS_LINK)
325		mii->mii_media_status |= IFM_ACTIVE;
326
327	bmcr = PHY_READ(sc, BRGPHY_MII_BMCR);
328
329	if (bmcr & BRGPHY_BMCR_LOOP)
330		mii->mii_media_active |= IFM_LOOP;
331
332	if (bmcr & BRGPHY_BMCR_AUTOEN) {
333		if ((bmsr & BRGPHY_BMSR_ACOMP) == 0) {
334			/* Erg, still trying, I guess... */
335			mii->mii_media_active |= IFM_NONE;
336			return;
337		}
338
339		mii->mii_media_active |= IFM_1000_TX;
340		anlpar = PHY_READ(sc, BRGPHY_MII_AUXSTS);
341		if ((anlpar & BRGPHY_AUXSTS_AN_RES) == BRGPHY_RES_1000FD)
342			mii->mii_media_active |= IFM_FDX;
343		if ((anlpar & BRGPHY_AUXSTS_AN_RES) == BRGPHY_RES_1000HD)
344			mii->mii_media_active |= IFM_HDX;
345		return;
346	}
347
348	mii->mii_media_active |= IFM_1000_TX;
349	if (bmcr & BRGPHY_BMCR_FDX)
350		mii->mii_media_active |= IFM_FDX;
351	else
352		mii->mii_media_active |= IFM_HDX;
353
354	return;
355}
356
357
358static int
359brgphy_mii_phy_auto(mii, waitfor)
360	struct mii_softc *mii;
361	int waitfor;
362{
363	int bmsr, i;
364
365	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
366		PHY_WRITE(mii, BRGPHY_MII_1000CTL,
367		    BRGPHY_1000CTL_AFD|BRGPHY_1000CTL_AHD);
368		PHY_WRITE(mii, BRGPHY_MII_ANAR, BRGPHY_SEL_TYPE);
369		PHY_WRITE(mii, BRGPHY_MII_BMCR,
370		    BRGPHY_BMCR_AUTOEN | BRGPHY_BMCR_STARTNEG);
371		PHY_WRITE(mii, BRGPHY_MII_IMR, 0xFF00);
372	}
373
374	if (waitfor) {
375		/* Wait 500ms for it to complete. */
376		for (i = 0; i < 500; i++) {
377			if ((bmsr = PHY_READ(mii, BRGPHY_MII_BMSR)) &
378			    BRGPHY_BMSR_ACOMP)
379				return (0);
380			DELAY(1000);
381#if 0
382		if ((bmsr & BMSR_ACOMP) == 0)
383			printf("%s: autonegotiation failed to complete\n",
384			    mii->mii_dev.dv_xname);
385#endif
386		}
387
388		/*
389		 * Don't need to worry about clearing MIIF_DOINGAUTO.
390		 * If that's set, a timeout is pending, and it will
391		 * clear the flag.
392		 */
393		return (EIO);
394	}
395
396	/*
397	 * Just let it finish asynchronously.  This is for the benefit of
398	 * the tick handler driving autonegotiation.  Don't want 500ms
399	 * delays all the time while the system is running!
400	 */
401	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
402		mii->mii_flags |= MIIF_DOINGAUTO;
403		timeout(mii_phy_auto_timeout, mii, hz >> 1);
404	}
405	return (EJUSTRETURN);
406}
407