xref: /netbsd/sys/dev/isa/if_ep_isa.c (revision bf9ec67e)
1 /*	$NetBSD: if_ep_isa.c,v 1.30 2002/01/07 21:47:07 thorpej 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.30 2002/01/07 21:47:07 thorpej 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 (ISA_DIRECT_CONFIG(ia))
174 		return (0);
175 
176 	if (ep_isa_probes_initialized == 0) {
177 		LIST_INIT(&ep_isa_all_probes);
178 		ep_isa_probes_initialized = 1;
179 	}
180 
181 	/*
182 	 * Probe this bus if we haven't done so already.
183 	 */
184 	for (er = ep_isa_all_probes.lh_first; er != NULL;
185 	    er = er->er_link.le_next)
186 		if (er->er_bus == parent->dv_unit)
187 			goto bus_probed;
188 
189 	/*
190 	 * Mark this bus so we don't probe it again.
191 	 */
192 	er = (struct ep_isa_done_probe *)
193 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
194 	if (er == NULL)
195 		panic("ep_isa_probe: can't allocate state storage");
196 
197 	er->er_bus = bus;
198 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
199 
200 	/*
201 	 * Map the Etherlink ID port for the probe sequence.
202 	 */
203 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
204 		printf("ep_isa_probe: can't map Etherlink ID port\n");
205 		return 0;
206 	}
207 
208 	for (slot = 0; slot < MAXEPCARDS; slot++) {
209 		elink_reset(iot, ioh, parent->dv_unit);
210 		elink_idseq(iot, ioh, ELINK_509_POLY);
211 
212 		/* Untag all the adapters so they will talk to us. */
213 		if (slot == 0)
214 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
215 
216 		vendor = bswap16(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
217 		if (vendor != MFG_ID)
218 			continue;
219 
220 		model = bswap16(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
221 		/*
222 		 * XXX: Add a new product id to check for other cards
223 		 * (3c515?) and fix the check in ep_isa_attach.
224 		 */
225 		if ((model & 0xfff0) != PROD_ID_3C509) {
226 #ifndef trusted
227 			printf(
228 			 "ep_isa_probe: ignoring model %04x\n", model);
229 #endif
230 			continue;
231 			}
232 
233 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
234 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
235 
236 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
237 		irq >>= 12;
238 
239 		/* XXX Should ignore card if non-ISA(EISA) io address? -chb */
240 
241 		/*
242 		 * Don't attach a 3c509 in PnP mode.
243 		 * PnP mode was added with the 3C509B.
244 		 * Check some EEPROM registers to make sure this is really
245 		 * a 3C509B and test whether it is in PnP mode.
246 		 */
247 		if ((model & 0xfff0) == PROD_ID_3C509) {
248 			u_int16_t cksum, eepromrev, eeprom_cap, eeprom_hi;
249 
250 
251 			/*
252 			 * Fetch all the info we need to ascertain whether
253 			 * the card is  PnP capable and in PnP mode.
254 			 * Skip over PnP cards.
255 			 */
256 
257 			/* secondary configurable data checksum */
258 			cksum = epreadeeprom(iot, ioh, EEPROM_CHECKSUM_EL3)
259 			    & 0xFF;
260 			for (i = EEPROM_CONFIG_HIGH;
261 			    i < EEPROM_CHECKSUM_EL3; i++) {
262 				cksum ^= epreadeeprom(iot, ioh, i);
263 			}
264 			cksum = (cksum & 0xFF) ^ ((cksum >> 8) & 0xFF);
265 
266 			eepromrev = epreadeeprom(iot, ioh, EEPROM_SSI);
267 			eeprom_hi = epreadeeprom(iot, ioh, EEPROM_CONFIG_HIGH);
268 			eeprom_cap = epreadeeprom(iot, ioh, EEPROM_CAP);
269 
270 			/*
271 			 * Stop card responding to contention in future.
272 			 * (NB: stops rsponse to all reads from ID port.)
273 			 */
274 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
275 
276 			if (cksum != 0) {
277 #if 0
278 				printf("ep_isa_probe: cksum mismatch 0x%02x\n",
279 				    (int)cksum);
280 #endif
281 			}
282 			else if ((eepromrev & 0xF) < 1) {
283 				/* 3C509B is adapter revision level 1. */
284 #if 0
285 				printf("ep_isa_probe revision level 0\n");
286 #endif
287 			}
288 			else if (eeprom_cap != 0x2083) {
289 #if 0
290 				printf("ep_isa_probe: capabilities word mismatch0x%03x\n",
291 				    (int)epreadeeprom(iot, ioh, EEPROM_CAP));
292 #endif
293 			}
294 			else
295 			  /*
296 			   * we have a 3c509B with PnP capabilities.
297 			   * Test partly documented bit which toggles when
298 			   * in  PnP mode.
299 			   */
300 			if ((eeprom_hi & 8) != 0) {
301 				printf("3COM 3C509B Ethernet card in PnP mode\n");
302 				continue;
303 			}
304 		}
305 
306 		/*
307 		 * XXX: this should probably not be done here
308 		 * because it enables the drq/irq lines from
309 		 * the board. Perhaps it should be done after
310 		 * we have checked for irq/drq collisions?
311 		 *
312 		 * According to the 3COM docs, this does not enable
313 		 * the irq lines. -chb
314 		 */
315 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
316 
317 		epaddcard(bus, iobase, irq, model);
318 	}
319 	/* XXX should we sort by ethernet address? */
320 
321 	bus_space_unmap(iot, ioh, 1);
322 
323 bus_probed:
324 
325 	if (ia->ia_nio < 1)
326 		return (0);
327 	if (ia->ia_nirq < 1)
328 		return (0);
329 
330 	for (i = 0; i < nepcards; i++) {
331 		if (epcards[i].bus != bus)
332 			continue;
333 		if (epcards[i].available == 0)
334 			continue;
335 
336 		if (ia->ia_io[0].ir_addr != ISACF_PORT_DEFAULT &&
337 		    ia->ia_io[0].ir_addr != epcards[i].iobase)
338 			continue;
339 
340 		if (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT &&
341 		    ia->ia_irq[0].ir_irq != epcards[i].irq)
342 			continue;
343 
344 		goto good;
345 	}
346 	return 0;
347 
348 good:
349 	epcards[i].available = 0;
350 
351 	ia->ia_nio = 1;
352 	ia->ia_io[0].ir_addr = epcards[i].iobase;
353 	ia->ia_io[0].ir_size = 0x10;
354 
355 	ia->ia_nirq = 1;
356 	ia->ia_irq[0].ir_irq = epcards[i].irq;
357 
358 	ia->ia_niomem = 0;
359 	ia->ia_ndrq = 0;
360 
361 	ia->ia_aux = (void *)epcards[i].model;
362 	return 1;
363 }
364 
365 void
366 ep_isa_attach(parent, self, aux)
367 	struct device *parent, *self;
368 	void *aux;
369 {
370 	struct ep_softc *sc = (void *)self;
371 	struct isa_attach_args *ia = aux;
372 	bus_space_tag_t iot = ia->ia_iot;
373 	bus_space_handle_t ioh;
374 	int chipset;
375 
376 	/* Map i/o space. */
377 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, 0x10, 0, &ioh)) {
378 		printf(": can't map i/o space\n");
379 		return;
380 	}
381 
382 	sc->sc_iot = iot;
383 	sc->sc_ioh = ioh;
384 	sc->bustype = ELINK_BUS_ISA;
385 
386 	sc->enable = NULL;
387 	sc->disable = NULL;
388 	sc->enabled = 1;
389 
390 	chipset = (int)(long)ia->ia_aux;
391 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
392 		printf(": 3Com 3C509 Ethernet\n");
393 		epconfig(sc, ELINK_CHIPSET_3C509, NULL);
394 	} else {
395 		/*
396 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
397 		 * at the moment only for a 3c509.
398 		 */
399 		printf(": unknown 3Com Ethernet card: %04x\n", chipset);
400 		return;
401 	}
402 
403 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
404 	    IST_EDGE, IPL_NET, epintr, sc);
405 }
406