xref: /netbsd-src/sys/dev/isa/if_ep_isa.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_ep_isa.c,v 1.41 2007/10/19 12:00:17 ad 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.41 2007/10/19 12:00:17 ad 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 <sys/cpu.h>
91 #include <sys/bus.h>
92 #include <sys/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(struct device *, struct cfdata *, void *);
103 void ep_isa_attach(struct device *, struct device *, void *);
104 
105 CFATTACH_DECL(ep_isa, sizeof(struct ep_softc),
106     ep_isa_probe, ep_isa_attach, NULL, NULL);
107 
108 static	void epaddcard(int, int, int, int);
109 
110 /*
111  * This keeps track of which ISAs have been through an ep probe sequence.
112  * A simple static variable isn't enough, since it's conceivable that
113  * a system might have more than one ISA bus.
114  *
115  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
116  * for "isa0".
117  */
118 struct ep_isa_done_probe {
119 	LIST_ENTRY(ep_isa_done_probe)	er_link;
120 	int				er_bus;
121 };
122 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
123 static int ep_isa_probes_initialized;
124 
125 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
126 
127 static struct epcard {
128 	int	bus;
129 	int	iobase;
130 	int	irq;
131 	char	available;
132 	long	model;
133 } epcards[MAXEPCARDS];
134 static int nepcards;
135 
136 static void
137 epaddcard(bus, iobase, irq, model)
138 	int bus, iobase, irq, model;
139 {
140 
141 	if (nepcards >= MAXEPCARDS)
142 		return;
143 	epcards[nepcards].bus = bus;
144 	epcards[nepcards].iobase = iobase;
145 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
146 	epcards[nepcards].model = model;
147 	epcards[nepcards].available = 1;
148 	nepcards++;
149 }
150 
151 /*
152  * 3c509 cards on the ISA bus are probed in ethernet address order.
153  * The probe sequence requires careful orchestration, and we'd like
154  * like to allow the irq and base address to be wildcarded. So, we
155  * probe all the cards the first time epprobe() is called. On subsequent
156  * calls we look for matching cards.
157  */
158 int
159 ep_isa_probe(struct device *parent, struct cfdata *match,
160     void *aux)
161 {
162 	struct isa_attach_args *ia = aux;
163 	bus_space_tag_t iot = ia->ia_iot;
164 	bus_space_handle_t ioh;
165 	int slot, iobase, irq, i;
166 	u_int16_t vendor, model, eeprom_addr_cfg;
167 	struct ep_isa_done_probe *er;
168 	int bus = device_unit(parent);
169 
170 	if (ISA_DIRECT_CONFIG(ia))
171 		return (0);
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 == device_unit(parent))
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, device_unit(parent));
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 		eeprom_addr_cfg = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
231 		iobase = (eeprom_addr_cfg & 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 bits which toggle when
295 			   * in PnP mode.
296 			   */
297 			if ((eeprom_hi & 0x8) != 0 || ((eeprom_hi & 0xc) == 0 &&
298 			    (eeprom_addr_cfg & 0x80) != 0)) {
299 				printf("3COM 3C509B Ethernet card in PnP mode\n");
300 				continue;
301 			}
302 		}
303 
304 		/*
305 		 * XXX: this should probably not be done here
306 		 * because it enables the drq/irq lines from
307 		 * the board. Perhaps it should be done after
308 		 * we have checked for irq/drq collisions?
309 		 *
310 		 * According to the 3COM docs, this does not enable
311 		 * the irq lines. -chb
312 		 */
313 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
314 
315 		epaddcard(bus, iobase, irq, model);
316 	}
317 	/* XXX should we sort by ethernet address? */
318 
319 	bus_space_unmap(iot, ioh, 1);
320 
321 bus_probed:
322 
323 	if (ia->ia_nio < 1)
324 		return (0);
325 	if (ia->ia_nirq < 1)
326 		return (0);
327 
328 	for (i = 0; i < nepcards; i++) {
329 		if (epcards[i].bus != bus)
330 			continue;
331 		if (epcards[i].available == 0)
332 			continue;
333 
334 		if (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
335 		    ia->ia_io[0].ir_addr != epcards[i].iobase)
336 			continue;
337 
338 		if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
339 		    ia->ia_irq[0].ir_irq != epcards[i].irq)
340 			continue;
341 
342 		goto good;
343 	}
344 	return 0;
345 
346 good:
347 	epcards[i].available = 0;
348 
349 	ia->ia_nio = 1;
350 	ia->ia_io[0].ir_addr = epcards[i].iobase;
351 	ia->ia_io[0].ir_size = 0x10;
352 
353 	ia->ia_nirq = 1;
354 	ia->ia_irq[0].ir_irq = epcards[i].irq;
355 
356 	ia->ia_niomem = 0;
357 	ia->ia_ndrq = 0;
358 
359 	ia->ia_aux = (void *)epcards[i].model;
360 	return 1;
361 }
362 
363 void
364 ep_isa_attach(struct device *parent, struct device *self, void *aux)
365 {
366 	struct ep_softc *sc = (void *)self;
367 	struct isa_attach_args *ia = aux;
368 	bus_space_tag_t iot = ia->ia_iot;
369 	bus_space_handle_t ioh;
370 	int chipset;
371 
372 	/* Map i/o space. */
373 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, 0x10, 0, &ioh)) {
374 		printf(": can't map i/o space\n");
375 		return;
376 	}
377 
378 	sc->sc_iot = iot;
379 	sc->sc_ioh = ioh;
380 	sc->bustype = ELINK_BUS_ISA;
381 
382 	sc->enable = NULL;
383 	sc->disable = NULL;
384 	sc->enabled = 1;
385 
386 	chipset = (int)(long)ia->ia_aux;
387 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
388 		printf(": 3Com 3C509 Ethernet\n");
389 		epconfig(sc, ELINK_CHIPSET_3C509, NULL);
390 	} else {
391 		/*
392 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
393 		 * at the moment only for a 3c509.
394 		 */
395 		printf(": unknown 3Com Ethernet card: %04x\n", chipset);
396 		return;
397 	}
398 
399 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
400 	    IST_EDGE, IPL_NET, epintr, sc);
401 }
402