xref: /netbsd/sys/dev/mca/if_ep_mca.c (revision bf9ec67e)
1 /*	$NetBSD: if_ep_mca.c,v 1.7 2001/11/28 20:56:47 jdolecek 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.7 2001/11/28 20:56:47 jdolecek 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 
109 int ep_mca_match __P((struct device *, struct cfdata *, void *));
110 void ep_mca_attach __P((struct device *, struct device *, void *));
111 
112 struct cfattach ep_mca_ca = {
113 	sizeof(struct ep_softc), ep_mca_match, ep_mca_attach
114 };
115 
116 const struct ep_mca_product {
117 	u_int32_t	epp_prodid;	/* MCA product ID */
118 	const char	*epp_name;	/* device name */
119 } ep_mca_products[] = {
120 	{ MCA_PRODUCT_3C529,	"3C529 Ethernet Adapter" },
121 	{ MCA_PRODUCT_3C529_TP,	"3c529-TP Ethernet Adapter" },
122 	{ MCA_PRODUCT_3C529_TM, "3c529 Ethernet Adapter (test mode)" },
123 	{ MCA_PRODUCT_3C529_2T, "3c529 Ethernet Adapter (10base2/T)" },
124 	{ MCA_PRODUCT_3C529_T,	"3c529 Ethernet Adapter (10baseT)"   },
125 
126 	{ 0,			NULL },
127 };
128 
129 static const struct ep_mca_product *ep_mca_lookup
130     __P((const struct mca_attach_args *));
131 
132 static const struct ep_mca_product *
133 ep_mca_lookup(ma)
134 	const struct mca_attach_args *ma;
135 {
136 	const struct ep_mca_product *epp;
137 
138 	for (epp = ep_mca_products; epp->epp_name != NULL; epp++)
139 		if (ma->ma_id == epp->epp_prodid)
140 			return (epp);
141 
142 	return (NULL);
143 }
144 
145 int
146 ep_mca_match(parent, match, aux)
147 	struct device *parent;
148 	struct cfdata *match;
149 	void *aux;
150 {
151 	struct mca_attach_args *ma = (struct mca_attach_args *) aux;
152 
153 	if (ep_mca_lookup(ma) != NULL)
154 		return (1);
155 
156 	return (0);
157 }
158 
159 void
160 ep_mca_attach(parent, self, aux)
161 	struct device *parent, *self;
162 	void *aux;
163 {
164 	struct ep_softc *sc = (void *)self;
165 	struct mca_attach_args *ma = aux;
166 	bus_space_handle_t ioh;
167 	int pos4, pos5, iobase, irq, media;
168 	const struct ep_mca_product *epp;
169 
170 	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
171 	pos5 = mca_conf_read(ma->ma_mc, ma->ma_slot, 5);
172 
173 	/*
174 	 * POS register 2: (adf pos0)
175 	 * 7 6 5 4 3 2 1 0
176 	 *               \__ enable: 0=adapter disabled, 1=adapter enabled
177 	 *
178 	 * POS register 3: (adf pos1)
179 	 *
180 	 * 7 6 5 4 3 2 1 0
181 	 * \________/
182 	 *          \_______ Boot ROM Address Range: 0=disabled
183 	 *                     X=0xc2000-0xc3fff + (x * 0x2000)
184 	 *
185 	 * POS register 4: (adf pos2)
186 	 *
187 	 * 7 6 5 4 3 2 1 0
188 	 * \________/  \_/
189 	 *          \    \__ Transceiver Type: 00=on-board (RJ45), 01=ext(AUI)
190 	 *           \______ I/O Address Range: 0x200-0x20f + ((x>>2) * 0x400)
191 	 *
192 	 * POS register 5: (adf pos3)
193 	 *
194 	 * 7 6 5 4 3 2 1 0
195 	 *          \____/
196 	 *               \__ Interrupt level
197 	 */
198 
199 	iobase = MCA_CBIO + (((pos4 & 0xfc) >> 2) * 0x400);
200 	irq = (pos5 & 0x0f);
201 
202 	/* map the pio registers */
203 	if (bus_space_map(ma->ma_iot, iobase, MCA_IOSZ, 0, &ioh)) {
204 		printf("%s: unable to map i/o space\n", sc->sc_dev.dv_xname);
205 		return;
206 	}
207 
208 	sc->sc_iot = ma->ma_iot;
209 	sc->sc_ioh = ioh;
210 
211 	epp = ep_mca_lookup(ma);
212 	if (epp == NULL) {
213 		printf("\n");
214 		panic("ep_mca_attach: impossible");
215 	}
216 
217 	printf(" slot %d irq %d: 3Com %s\n", ma->ma_slot + 1,
218 		irq, epp->epp_name);
219 
220 	sc->enable = NULL;
221 	sc->disable = NULL;
222 	sc->enabled = 1;
223 
224 	sc->bustype = ELINK_BUS_MCA;
225 	sc->ep_flags = 0;
226 
227 	if (epconfig(sc, ELINK_CHIPSET_3C509, NULL))
228 		return;
229 
230 	/* Map and establish the interrupt. */
231 	sc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET, epintr, sc);
232 	if (sc->sc_ih == NULL) {
233 		printf("%s: couldn't establish interrupt handler\n",
234 		    sc->sc_dev.dv_xname);
235 		return;
236 	}
237 
238 	/*
239 	 * Set default media to be same as the one selected in POS.
240 	 */
241 	switch (pos4 & 0x03) {
242 	case 0x00:
243 		/* on-board RJ-45 */
244 		media = IFM_10_T;
245 		break;
246 	case 0x01:
247 		/* external AUI */
248 		media = IFM_10_5;
249 		break;
250 	case 0x02:
251 		/* undefined, should never be set */
252 		media = IFM_NONE;
253 		break;
254 	case 0x03:
255 		/* BNC */
256 		media = IFM_10_2;
257 		break;
258 	}
259 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|media);
260 }
261