dcphy.c revision 1.14
1/*	$OpenBSD: dcphy.c,v 1.14 2005/03/26 04:40:09 krw Exp $	*/
2
3/*
4 * Copyright (c) 1997, 1998, 1999
5 *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * $FreeBSD: src/sys/dev/mii/dcphy.c,v 1.6 2000/10/05 17:36:14 wpaul Exp $
35 */
36
37/*
38 * Pseudo-driver for internal NWAY support on DEC 21143 and workalike
39 * controllers. Technically we're abusing the miibus code to handle
40 * media selection and NWAY support here since there is no MII
41 * interface. However the logical operations are roughly the same,
42 * and the alternative is to create a fake MII interface in the driver,
43 * which is harder to do.
44 */
45
46#include <sys/param.h>
47#include <sys/device.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/socket.h>
51#include <sys/errno.h>
52#include <sys/socket.h>
53
54#include <net/if.h>
55#include <net/if_dl.h>
56#include <net/if_types.h>
57#include <net/if_media.h>
58#include <netinet/in.h>
59#include <netinet/if_ether.h>
60
61#include <dev/mii/mii.h>
62#include <dev/mii/miivar.h>
63#include <dev/mii/miidevs.h>
64
65#include <machine/bus.h>
66
67#include <dev/pci/pcivar.h>
68
69#include <dev/ic/dcreg.h>
70
71#define DC_SETBIT(sc, reg, x)                           \
72        CSR_WRITE_4(sc, reg,                            \
73                CSR_READ_4(sc, reg) | x)
74
75#define DC_CLRBIT(sc, reg, x)                           \
76        CSR_WRITE_4(sc, reg,                            \
77                CSR_READ_4(sc, reg) & ~x)
78
79#define MIIF_AUTOTIMEOUT	0x0004
80
81/*
82 * This is the subsystem ID for the built-in 21143 ethernet
83 * in several Compaq Presario systems. Apparently these are
84 * 10Mbps only, so we need to treat them specially.
85 */
86#define COMPAQ_PRESARIO_ID	0xb0bb0e11
87
88int	dcphy_match(struct device *, void *, void *);
89void	dcphy_attach(struct device *, struct device *, void *);
90
91struct cfattach dcphy_ca = {
92	sizeof(struct mii_softc), dcphy_match, dcphy_attach, mii_phy_detach,
93	    mii_phy_activate
94};
95
96struct cfdriver dcphy_cd = {
97	NULL, "dcphy", DV_DULL
98};
99
100int	dcphy_service(struct mii_softc *, struct mii_data *, int);
101void	dcphy_status(struct mii_softc *);
102int	dcphy_mii_phy_auto(struct mii_softc *, int);
103void	dcphy_reset(struct mii_softc *);
104
105const struct mii_phy_funcs dcphy_funcs = {
106	dcphy_service, dcphy_status, dcphy_reset,
107};
108
109int
110dcphy_match(struct device *parent, void *match, void *aux)
111{
112	struct mii_attach_args *ma = aux;
113
114	/*
115	 * The dc driver will report the 21143 vendor and device
116	 * ID to let us know that it wants us to attach.
117	 */
118	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxDEC &&
119	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxDEC_xxDC)
120		return (10);
121
122	return (0);
123}
124
125void
126dcphy_attach(struct device *parent, struct device *self, void *aux)
127{
128	struct mii_softc *sc = (struct mii_softc *)self;
129	struct mii_attach_args *ma = aux;
130	struct mii_data *mii = ma->mii_data;
131	struct dc_softc *dc_sc;
132
133	printf(": internal PHY\n");
134	sc->mii_inst = mii->mii_instance;
135	sc->mii_phy = ma->mii_phyno;
136	sc->mii_funcs = &dcphy_funcs;
137	sc->mii_pdata = mii;
138	sc->mii_flags = ma->mii_flags;
139	sc->mii_anegticks = 50;
140
141	sc->mii_flags |= MIIF_NOISOLATE;
142
143	mii->mii_instance++;
144
145	dc_sc = mii->mii_ifp->if_softc;
146	CSR_WRITE_4(dc_sc, DC_10BTSTAT, 0);
147	CSR_WRITE_4(dc_sc, DC_10BTCTRL, 0);
148
149#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
150
151	switch(dc_sc->dc_csid) {
152	case COMPAQ_PRESARIO_ID:
153		/* Example of how to only allow 10Mbps modes. */
154		sc->mii_capabilities = BMSR_ANEG|BMSR_10TFDX|BMSR_10THDX;
155		break;
156	default:
157		if (dc_sc->dc_pmode == DC_PMODE_SIA) {
158			sc->mii_capabilities =
159			    BMSR_ANEG|BMSR_10TFDX|BMSR_10THDX;
160		} else {
161			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
162			    sc->mii_inst), BMCR_LOOP|BMCR_S100);
163
164			sc->mii_capabilities =
165			    BMSR_ANEG|BMSR_100TXFDX|BMSR_100TXHDX|
166			    BMSR_10TFDX|BMSR_10THDX;
167		}
168		break;
169	}
170
171	if (dc_sc->dc_type == DC_TYPE_21145)
172		sc->mii_capabilities = BMSR_10THDX;
173
174	sc->mii_capabilities &= ma->mii_capmask;
175	if (sc->mii_capabilities & BMSR_MEDIAMASK)
176		mii_phy_add_media(sc);
177#undef ADD
178}
179
180int
181dcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
182{
183	struct dc_softc *dc_sc;
184	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
185	int reg;
186	u_int32_t mode;
187
188	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
189		return (ENXIO);
190
191	dc_sc = mii->mii_ifp->if_softc;
192
193	switch (cmd) {
194	case MII_POLLSTAT:
195		/*
196		 * If we're not polling our PHY instance, just return.
197		 */
198		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
199			return (0);
200		}
201		break;
202
203	case MII_MEDIACHG:
204		/*
205		 * If the media indicates a different PHY instance,
206		 * isolate ourselves.
207		 */
208		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
209			return (0);
210		}
211
212		/*
213		 * If the interface is not up, don't do anything.
214		 */
215		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
216			break;
217
218		sc->mii_flags = 0;
219		mii->mii_media_active = IFM_NONE;
220		mode = CSR_READ_4(dc_sc, DC_NETCFG);
221		mode &= ~(DC_NETCFG_FULLDUPLEX|DC_NETCFG_PORTSEL|
222		    DC_NETCFG_PCS|DC_NETCFG_SCRAMBLER|DC_NETCFG_SPEEDSEL);
223
224		switch (IFM_SUBTYPE(ife->ifm_media)) {
225		case IFM_AUTO:
226			/*PHY_RESET(sc);*/
227			sc->mii_flags &= ~MIIF_DOINGAUTO;
228			(void) dcphy_mii_phy_auto(sc, 0);
229			break;
230		case IFM_100_T4:
231			/*
232			 * XXX Not supported as a manual setting right now.
233			 */
234			return (EINVAL);
235		case IFM_100_TX:
236			PHY_RESET(sc);
237			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
238			mode |= DC_NETCFG_PORTSEL|DC_NETCFG_PCS|
239			    DC_NETCFG_SCRAMBLER;
240			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
241				mode |= DC_NETCFG_FULLDUPLEX;
242			else
243				mode &= ~DC_NETCFG_FULLDUPLEX;
244			CSR_WRITE_4(dc_sc, DC_NETCFG, mode);
245			break;
246		case IFM_10_T:
247			DC_CLRBIT(dc_sc, DC_SIARESET, DC_SIA_RESET);
248			DC_CLRBIT(dc_sc, DC_10BTCTRL, 0xFFFF);
249			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
250				DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3D);
251			else
252				DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3F);
253			DC_SETBIT(dc_sc, DC_SIARESET, DC_SIA_RESET);
254			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
255			mode &= ~DC_NETCFG_PORTSEL;
256			mode |= DC_NETCFG_SPEEDSEL;
257			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
258				mode |= DC_NETCFG_FULLDUPLEX;
259			else
260				mode &= ~DC_NETCFG_FULLDUPLEX;
261			CSR_WRITE_4(dc_sc, DC_NETCFG, mode);
262			break;
263		default:
264			return(EINVAL);
265		}
266		break;
267
268	case MII_TICK:
269		/*
270		 * If we're not currently selected, just return.
271		 */
272		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
273			return (0);
274
275		/*
276		 * Is the interface even up?
277		 */
278		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
279			return (0);
280
281		/*
282		 * Only used for autonegotiation.
283		 */
284		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
285			break;
286
287		reg = CSR_READ_4(dc_sc, DC_10BTSTAT);
288		if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100))
289			break;
290
291		/*
292		 * Only retry autonegotiation every mii_anegticks seconds.
293		 *
294		 * Otherwise, fall through to calling dcphy_status()
295		 * since real Intel 21143 chips don't show valid link
296		 * status until autonegotiation is switched off, and
297		 * that only happens in dcphy_status().  Without this,
298		 * successful autonegotation is never recognised on
299		 * these chips.
300		 */
301		if (++sc->mii_ticks <= sc->mii_anegticks)
302			break;
303
304		sc->mii_ticks = 0;
305		sc->mii_flags &= ~MIIF_DOINGAUTO;
306		dcphy_mii_phy_auto(sc, 0);
307
308		break;
309	}
310
311	/* Update the media status. */
312	mii_phy_status(sc);
313
314	/* Callback if something changed. */
315	mii_phy_update(sc, cmd);
316	return (0);
317}
318
319void
320dcphy_status(struct mii_softc *sc)
321{
322	struct mii_data *mii = sc->mii_pdata;
323	int reg, anlpar, tstat = 0;
324	struct dc_softc *dc_sc;
325
326	dc_sc = mii->mii_ifp->if_softc;
327
328	mii->mii_media_status = IFM_AVALID;
329	mii->mii_media_active = IFM_ETHER;
330
331	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
332		return;
333
334	reg = CSR_READ_4(dc_sc, DC_10BTSTAT);
335	if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100))
336		mii->mii_media_status |= IFM_ACTIVE;
337
338	if (CSR_READ_4(dc_sc, DC_10BTCTRL) & DC_TCTL_AUTONEGENBL) {
339		/* Erg, still trying, I guess... */
340		tstat = CSR_READ_4(dc_sc, DC_10BTSTAT);
341		if ((tstat & DC_TSTAT_ANEGSTAT) != DC_ASTAT_AUTONEGCMP) {
342			if ((DC_IS_MACRONIX(dc_sc) || DC_IS_PNICII(dc_sc)) &&
343			    (tstat & DC_TSTAT_ANEGSTAT) == DC_ASTAT_DISABLE)
344				goto skip;
345			mii->mii_media_active |= IFM_NONE;
346			return;
347		}
348
349		if (tstat & DC_TSTAT_LP_CAN_NWAY) {
350			anlpar = tstat >> 16;
351			if (anlpar & ANLPAR_T4 &&
352			    sc->mii_capabilities & BMSR_100TXHDX)
353				mii->mii_media_active |= IFM_100_T4;
354			else if (anlpar & ANLPAR_TX_FD &&
355			    sc->mii_capabilities & BMSR_100TXFDX)
356				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
357			else if (anlpar & ANLPAR_TX &&
358			    sc->mii_capabilities & BMSR_100TXHDX)
359				mii->mii_media_active |= IFM_100_TX;
360			else if (anlpar & ANLPAR_10_FD)
361				mii->mii_media_active |= IFM_10_T|IFM_FDX;
362			else if (anlpar & ANLPAR_10)
363				mii->mii_media_active |= IFM_10_T;
364			else
365				mii->mii_media_active |= IFM_NONE;
366			if (DC_IS_INTEL(dc_sc))
367				DC_CLRBIT(dc_sc, DC_10BTCTRL,
368				    DC_TCTL_AUTONEGENBL);
369			return;
370		}
371		/*
372		 * If the other side doesn't support NWAY, then the
373		 * best we can do is determine if we have a 10Mbps or
374		 * 100Mbps link. There's no way to know if the link
375		 * is full or half duplex, so we default to half duplex
376		 * and hope that the user is clever enough to manually
377		 * change the media settings if we're wrong.
378		 */
379		if (!(reg & DC_TSTAT_LS100))
380			mii->mii_media_active |= IFM_100_TX;
381		else if (!(reg & DC_TSTAT_LS10))
382			mii->mii_media_active |= IFM_10_T;
383		else
384			mii->mii_media_active |= IFM_NONE;
385		if (DC_IS_INTEL(dc_sc))
386			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
387		return;
388	}
389
390skip:
391
392	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL)
393		mii->mii_media_active |= IFM_10_T;
394	else
395		mii->mii_media_active |= IFM_100_TX;
396	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX)
397		mii->mii_media_active |= IFM_FDX;
398
399	return;
400}
401
402int
403dcphy_mii_phy_auto(struct mii_softc *mii, int waitfor)
404{
405	int			i;
406	struct dc_softc		*sc;
407
408	sc = mii->mii_pdata->mii_ifp->if_softc;
409
410	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
411		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
412		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
413		DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
414		if (mii->mii_capabilities & BMSR_100TXHDX)
415			CSR_WRITE_4(sc, DC_10BTCTRL, 0x3FFFF);
416		else
417			CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFF);
418		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
419		DC_SETBIT(sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
420		DC_SETBIT(sc, DC_10BTSTAT, DC_ASTAT_TXDISABLE);
421	}
422
423	if (waitfor) {
424		/* Wait 500ms for it to complete. */
425		for (i = 0; i < 500; i++) {
426			if ((CSR_READ_4(sc, DC_10BTSTAT) & DC_TSTAT_ANEGSTAT)
427			    == DC_ASTAT_AUTONEGCMP)
428				return(0);
429			DELAY(1000);
430		}
431		/*
432		 * Don't need to worry about clearing MIIF_DOINGAUTO.
433		 * If that's set, a timeout is pending, and it will
434		 * clear the flag.
435		 */
436		return(EIO);
437	}
438
439	/*
440	 * Just let it finish asynchronously.  This is for the benefit of
441	 * the tick handler driving autonegotiation.  Don't want 500ms
442	 * delays all the time while the system is running!
443	 */
444	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0)
445		mii->mii_flags |= MIIF_DOINGAUTO;
446
447	return(EJUSTRETURN);
448}
449
450void
451dcphy_reset(struct mii_softc *mii)
452{
453	struct dc_softc		*sc;
454
455	sc = mii->mii_pdata->mii_ifp->if_softc;
456
457	DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
458	DELAY(1000);
459	DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
460
461	return;
462}
463