xref: /netbsd-src/sys/dev/isa/if_ep_isa.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: if_ep_isa.c,v 1.29 2001/11/13 08:01:18 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org>
42  * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *      This product includes software developed by Herb Peyerl.
56  * 4. The name of Herb Peyerl may not be used to endorse or promote products
57  *    derived from this software without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69  */
70 
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: if_ep_isa.c,v 1.29 2001/11/13 08:01:18 lukem Exp $");
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/mbuf.h>
77 #include <sys/socket.h>
78 #include <sys/ioctl.h>
79 #include <sys/errno.h>
80 #include <sys/syslog.h>
81 #include <sys/select.h>
82 #include <sys/device.h>
83 #include <sys/queue.h>
84 
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_ether.h>
88 #include <net/if_media.h>
89 
90 #include <machine/cpu.h>
91 #include <machine/bus.h>
92 #include <machine/intr.h>
93 
94 #include <dev/mii/miivar.h>
95 
96 #include <dev/ic/elink3var.h>
97 #include <dev/ic/elink3reg.h>
98 
99 #include <dev/isa/isavar.h>
100 #include <dev/isa/elink.h>
101 
102 int ep_isa_probe __P((struct device *, struct cfdata *, void *));
103 void ep_isa_attach __P((struct device *, struct device *, void *));
104 
105 struct cfattach ep_isa_ca = {
106 	sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
107 };
108 
109 static	void epaddcard __P((int, int, int, int));
110 
111 /*
112  * This keeps track of which ISAs have been through an ep probe sequence.
113  * A simple static variable isn't enough, since it's conceivable that
114  * a system might have more than one ISA bus.
115  *
116  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
117  * for "isa0".
118  */
119 struct ep_isa_done_probe {
120 	LIST_ENTRY(ep_isa_done_probe)	er_link;
121 	int				er_bus;
122 };
123 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
124 static int ep_isa_probes_initialized;
125 
126 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
127 
128 static struct epcard {
129 	int	bus;
130 	int	iobase;
131 	int	irq;
132 	char	available;
133 	long	model;
134 } epcards[MAXEPCARDS];
135 static int nepcards;
136 
137 static void
138 epaddcard(bus, iobase, irq, model)
139 	int bus, iobase, irq, model;
140 {
141 
142 	if (nepcards >= MAXEPCARDS)
143 		return;
144 	epcards[nepcards].bus = bus;
145 	epcards[nepcards].iobase = iobase;
146 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
147 	epcards[nepcards].model = model;
148 	epcards[nepcards].available = 1;
149 	nepcards++;
150 }
151 
152 /*
153  * 3c509 cards on the ISA bus are probed in ethernet address order.
154  * The probe sequence requires careful orchestration, and we'd like
155  * like to allow the irq and base address to be wildcarded. So, we
156  * probe all the cards the first time epprobe() is called. On subsequent
157  * calls we look for matching cards.
158  */
159 int
160 ep_isa_probe(parent, match, aux)
161 	struct device *parent;
162 	struct cfdata *match;
163 	void *aux;
164 {
165 	struct isa_attach_args *ia = aux;
166 	bus_space_tag_t iot = ia->ia_iot;
167 	bus_space_handle_t ioh;
168 	int slot, iobase, irq, i;
169 	u_int16_t vendor, model;
170 	struct ep_isa_done_probe *er;
171 	int bus = parent->dv_unit;
172 
173 	if (ep_isa_probes_initialized == 0) {
174 		LIST_INIT(&ep_isa_all_probes);
175 		ep_isa_probes_initialized = 1;
176 	}
177 
178 	/*
179 	 * Probe this bus if we haven't done so already.
180 	 */
181 	for (er = ep_isa_all_probes.lh_first; er != NULL;
182 	    er = er->er_link.le_next)
183 		if (er->er_bus == parent->dv_unit)
184 			goto bus_probed;
185 
186 	/*
187 	 * Mark this bus so we don't probe it again.
188 	 */
189 	er = (struct ep_isa_done_probe *)
190 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
191 	if (er == NULL)
192 		panic("ep_isa_probe: can't allocate state storage");
193 
194 	er->er_bus = bus;
195 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
196 
197 	/*
198 	 * Map the Etherlink ID port for the probe sequence.
199 	 */
200 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
201 		printf("ep_isa_probe: can't map Etherlink ID port\n");
202 		return 0;
203 	}
204 
205 	for (slot = 0; slot < MAXEPCARDS; slot++) {
206 		elink_reset(iot, ioh, parent->dv_unit);
207 		elink_idseq(iot, ioh, ELINK_509_POLY);
208 
209 		/* Untag all the adapters so they will talk to us. */
210 		if (slot == 0)
211 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
212 
213 		vendor = bswap16(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
214 		if (vendor != MFG_ID)
215 			continue;
216 
217 		model = bswap16(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
218 		/*
219 		 * XXX: Add a new product id to check for other cards
220 		 * (3c515?) and fix the check in ep_isa_attach.
221 		 */
222 		if ((model & 0xfff0) != PROD_ID_3C509) {
223 #ifndef trusted
224 			printf(
225 			 "ep_isa_probe: ignoring model %04x\n", model);
226 #endif
227 			continue;
228 			}
229 
230 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
231 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
232 
233 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
234 		irq >>= 12;
235 
236 		/* XXX Should ignore card if non-ISA(EISA) io address? -chb */
237 
238 		/*
239 		 * Don't attach a 3c509 in PnP mode.
240 		 * PnP mode was added with the 3C509B.
241 		 * Check some EEPROM registers to make sure this is really
242 		 * a 3C509B and test whether it is in PnP mode.
243 		 */
244 		if ((model & 0xfff0) == PROD_ID_3C509) {
245 			u_int16_t cksum, eepromrev, eeprom_cap, eeprom_hi;
246 
247 
248 			/*
249 			 * Fetch all the info we need to ascertain whether
250 			 * the card is  PnP capable and in PnP mode.
251 			 * Skip over PnP cards.
252 			 */
253 
254 			/* secondary configurable data checksum */
255 			cksum = epreadeeprom(iot, ioh, EEPROM_CHECKSUM_EL3)
256 			    & 0xFF;
257 			for (i = EEPROM_CONFIG_HIGH;
258 			    i < EEPROM_CHECKSUM_EL3; i++) {
259 				cksum ^= epreadeeprom(iot, ioh, i);
260 			}
261 			cksum = (cksum & 0xFF) ^ ((cksum >> 8) & 0xFF);
262 
263 			eepromrev = epreadeeprom(iot, ioh, EEPROM_SSI);
264 			eeprom_hi = epreadeeprom(iot, ioh, EEPROM_CONFIG_HIGH);
265 			eeprom_cap = epreadeeprom(iot, ioh, EEPROM_CAP);
266 
267 			/*
268 			 * Stop card responding to contention in future.
269 			 * (NB: stops rsponse to all reads from ID port.)
270 			 */
271 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
272 
273 			if (cksum != 0) {
274 #if 0
275 				printf("ep_isa_probe: cksum mismatch 0x%02x\n",
276 				    (int)cksum);
277 #endif
278 			}
279 			else if ((eepromrev & 0xF) < 1) {
280 				/* 3C509B is adapter revision level 1. */
281 #if 0
282 				printf("ep_isa_probe revision level 0\n");
283 #endif
284 			}
285 			else if (eeprom_cap != 0x2083) {
286 #if 0
287 				printf("ep_isa_probe: capabilities word mismatch0x%03x\n",
288 				    (int)epreadeeprom(iot, ioh, EEPROM_CAP));
289 #endif
290 			}
291 			else
292 			  /*
293 			   * we have a 3c509B with PnP capabilities.
294 			   * Test partly documented bit which toggles when
295 			   * in  PnP mode.
296 			   */
297 			if ((eeprom_hi & 8) != 0) {
298 				printf("3COM 3C509B Ethernet card in PnP mode\n");
299 				continue;
300 			}
301 		}
302 
303 		/*
304 		 * XXX: this should probably not be done here
305 		 * because it enables the drq/irq lines from
306 		 * the board. Perhaps it should be done after
307 		 * we have checked for irq/drq collisions?
308 		 *
309 		 * According to the 3COM docs, this does not enable
310 		 * the irq lines. -chb
311 		 */
312 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
313 
314 		epaddcard(bus, iobase, irq, model);
315 	}
316 	/* XXX should we sort by ethernet address? */
317 
318 	bus_space_unmap(iot, ioh, 1);
319 
320 bus_probed:
321 
322 	for (i = 0; i < nepcards; i++) {
323 		if (epcards[i].bus != bus)
324 			continue;
325 		if (epcards[i].available == 0)
326 			continue;
327 		if (ia->ia_iobase != IOBASEUNK &&
328 		    ia->ia_iobase != epcards[i].iobase)
329 			continue;
330 		if (ia->ia_irq != IRQUNK &&
331 		    ia->ia_irq != epcards[i].irq)
332 			continue;
333 		goto good;
334 	}
335 	return 0;
336 
337 good:
338 	epcards[i].available = 0;
339 	ia->ia_iobase = epcards[i].iobase;
340 	ia->ia_irq = epcards[i].irq;
341 	ia->ia_iosize = 0x10;
342 	ia->ia_msize = 0;
343 	ia->ia_aux = (void *)epcards[i].model;
344 	return 1;
345 }
346 
347 void
348 ep_isa_attach(parent, self, aux)
349 	struct device *parent, *self;
350 	void *aux;
351 {
352 	struct ep_softc *sc = (void *)self;
353 	struct isa_attach_args *ia = aux;
354 	bus_space_tag_t iot = ia->ia_iot;
355 	bus_space_handle_t ioh;
356 	int chipset;
357 
358 	/* Map i/o space. */
359 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
360 		printf(": can't map i/o space\n");
361 		return;
362 	}
363 
364 	sc->sc_iot = iot;
365 	sc->sc_ioh = ioh;
366 	sc->bustype = ELINK_BUS_ISA;
367 
368 	sc->enable = NULL;
369 	sc->disable = NULL;
370 	sc->enabled = 1;
371 
372 	chipset = (int)(long)ia->ia_aux;
373 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
374 		printf(": 3Com 3C509 Ethernet\n");
375 		epconfig(sc, ELINK_CHIPSET_3C509, NULL);
376 	} else {
377 		/*
378 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
379 		 * at the moment only for a 3c509.
380 		 */
381 		printf(": unknown 3Com Ethernet card: %04x\n", chipset);
382 		return;
383 	}
384 
385 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
386 	    IPL_NET, epintr, sc);
387 }
388