if_ep_mca.c revision 1.16
1/*	$NetBSD: if_ep_mca.c,v 1.16 2006/10/12 01:31:25 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jaromir Dolecek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the NetBSD
21 *	Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39/*
40 * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org>
41 * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 *    notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 *    notice, this list of conditions and the following disclaimer in the
51 *    documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 *    must display the following acknowledgement:
54 *      This product includes software developed by Herb Peyerl.
55 * 4. The name of Herb Peyerl may not be used to endorse or promote products
56 *    derived from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 */
69
70/*
71 * Driver for 3Com 3c529 cards.
72 *
73 * If you encouter sucky performance, try kernel without DEBUG/DIAGNOSTIC.
74 * This helped on my test machine to change the performance of the card
75 * from like 5KB/s to like 800 KB/s.
76 */
77
78#include <sys/cdefs.h>
79__KERNEL_RCSID(0, "$NetBSD: if_ep_mca.c,v 1.16 2006/10/12 01:31:25 christos Exp $");
80
81#include <sys/param.h>
82#include <sys/systm.h>
83#include <sys/mbuf.h>
84#include <sys/socket.h>
85#include <sys/ioctl.h>
86#include <sys/errno.h>
87#include <sys/device.h>
88
89#include <net/if.h>
90#include <net/if_ether.h>
91#include <net/if_media.h>
92
93#include <machine/bus.h>
94
95#include <dev/mii/miivar.h>
96
97#include <dev/ic/elink3var.h>
98#include <dev/ic/elink3reg.h>
99
100#include <dev/mca/mcavar.h>
101#include <dev/mca/mcadevs.h>
102
103/*
104 * MCA constants.
105 */
106#define MCA_CBIO		0x200	/* Configuration Base IO Address */
107#define MCA_IOSZ		0x10	/* I/O space size */
108
109int ep_mca_match(struct device *, struct cfdata *, void *);
110void ep_mca_attach(struct device *, struct device *, void *);
111
112CFATTACH_DECL(ep_mca, sizeof(struct ep_softc),
113    ep_mca_match, ep_mca_attach, NULL, NULL);
114
115const struct ep_mca_product {
116	u_int32_t	epp_prodid;	/* MCA product ID */
117	const char	*epp_name;	/* device name */
118} ep_mca_products[] = {
119	{ MCA_PRODUCT_3C529,	"3C529 Ethernet Adapter" },
120	{ MCA_PRODUCT_3C529_TP,	"3c529-TP Ethernet Adapter" },
121	{ MCA_PRODUCT_3C529_TM, "3c529 Ethernet Adapter (test mode)" },
122	{ MCA_PRODUCT_3C529_2T, "3c529 Ethernet Adapter (10base2/T)" },
123	{ MCA_PRODUCT_3C529_T,	"3c529 Ethernet Adapter (10baseT)"   },
124
125	{ 0,			NULL },
126};
127
128static const struct ep_mca_product *ep_mca_lookup
129   (const struct mca_attach_args *);
130
131static const struct ep_mca_product *
132ep_mca_lookup(ma)
133	const struct mca_attach_args *ma;
134{
135	const struct ep_mca_product *epp;
136
137	for (epp = ep_mca_products; epp->epp_name != NULL; epp++)
138		if (ma->ma_id == epp->epp_prodid)
139			return (epp);
140
141	return (NULL);
142}
143
144int
145ep_mca_match(struct device *parent __unused, struct cfdata *match __unused,
146    void *aux)
147{
148	struct mca_attach_args *ma = (struct mca_attach_args *) aux;
149
150	if (ep_mca_lookup(ma) != NULL)
151		return (1);
152
153	return (0);
154}
155
156void
157ep_mca_attach(struct device *parent __unused, struct device *self, void *aux)
158{
159	struct ep_softc *sc = device_private(self);
160	struct mca_attach_args *ma = aux;
161	bus_space_handle_t ioh;
162	int pos4, pos5, iobase, irq, media;
163	const struct ep_mca_product *epp;
164
165	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
166	pos5 = mca_conf_read(ma->ma_mc, ma->ma_slot, 5);
167
168	/*
169	 * POS register 2: (adf pos0)
170	 * 7 6 5 4 3 2 1 0
171	 *               \__ enable: 0=adapter disabled, 1=adapter enabled
172	 *
173	 * POS register 3: (adf pos1)
174	 *
175	 * 7 6 5 4 3 2 1 0
176	 * \________/
177	 *          \_______ Boot ROM Address Range: 0=disabled
178	 *                     X=0xc2000-0xc3fff + (x * 0x2000)
179	 *
180	 * POS register 4: (adf pos2)
181	 *
182	 * 7 6 5 4 3 2 1 0
183	 * \________/  \_/
184	 *          \    \__ Transceiver Type: 00=on-board (RJ45), 01=ext(AUI)
185	 *           \______ I/O Address Range: 0x200-0x20f + ((x>>2) * 0x400)
186	 *
187	 * POS register 5: (adf pos3)
188	 *
189	 * 7 6 5 4 3 2 1 0
190	 *          \____/
191	 *               \__ Interrupt level
192	 */
193
194	iobase = MCA_CBIO + (((pos4 & 0xfc) >> 2) * 0x400);
195	irq = (pos5 & 0x0f);
196
197	/* map the pio registers */
198	if (bus_space_map(ma->ma_iot, iobase, MCA_IOSZ, 0, &ioh)) {
199		printf("%s: unable to map i/o space\n", sc->sc_dev.dv_xname);
200		return;
201	}
202
203	sc->sc_iot = ma->ma_iot;
204	sc->sc_ioh = ioh;
205
206	epp = ep_mca_lookup(ma);
207	if (epp == NULL) {
208		printf("\n");
209		panic("ep_mca_attach: impossible");
210	}
211
212	printf(" slot %d irq %d: 3Com %s\n", ma->ma_slot + 1,
213		irq, epp->epp_name);
214
215	sc->enable = NULL;
216	sc->disable = NULL;
217	sc->enabled = 1;
218
219	sc->bustype = ELINK_BUS_MCA;
220	sc->ep_flags = 0;
221
222	if (epconfig(sc, ELINK_CHIPSET_3C509, NULL))
223		return;
224
225	/* Map and establish the interrupt. */
226	sc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET, epintr, sc);
227	if (sc->sc_ih == NULL) {
228		printf("%s: couldn't establish interrupt handler\n",
229		    sc->sc_dev.dv_xname);
230		return;
231	}
232
233	/*
234	 * Set default media to be same as the one selected in POS.
235	 */
236	switch (pos4 & 0x03) {
237	case 0x00:
238		/* on-board RJ-45 */
239		media = IFM_10_T;
240		break;
241	case 0x01:
242		/* external AUI */
243		media = IFM_10_5;
244		break;
245	case 0x02:
246		/* undefined, should never be set */
247		media = IFM_NONE;
248		break;
249	case 0x03:
250		/* BNC */
251		media = IFM_10_2;
252		break;
253	default:
254		printf("%s: cannot determine media\n",
255		    sc->sc_dev.dv_xname);
256		return;
257	}
258	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|media);
259}
260