xref: /netbsd-src/sys/dev/isa/if_ix.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: if_ix.c,v 1.12 2001/12/06 13:18:22 rafal Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Rafal K. Boni.
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 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_ix.c,v 1.12 2001/12/06 13:18:22 rafal Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/errno.h>
46 #include <sys/device.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/if_media.h>
54 #include <net/if_ether.h>
55 
56 #include <machine/cpu.h>
57 #include <machine/bus.h>
58 #include <machine/intr.h>
59 
60 #include <dev/isa/isareg.h>
61 #include <dev/isa/isavar.h>
62 
63 #include <dev/ic/i82586reg.h>
64 #include <dev/ic/i82586var.h>
65 #include <dev/isa/if_ixreg.h>
66 
67 #ifdef IX_DEBUG
68 #define DPRINTF(x)	printf x
69 #else
70 #define DPRINTF(x)
71 #endif
72 
73 int ix_media[] = {
74 	IFM_ETHER | IFM_10_5,
75 	IFM_ETHER | IFM_10_2,
76 	IFM_ETHER | IFM_10_T,
77 };
78 #define NIX_MEDIA       (sizeof(ix_media) / sizeof(ix_media[0]))
79 
80 struct ix_softc {
81 	struct ie_softc sc_ie;
82 
83 	bus_space_tag_t sc_regt;	/* space tag for registers */
84 	bus_space_handle_t sc_regh;	/* space handle for registers */
85 
86 	u_int8_t	use_pio;	/* use PIO rather than shared mem */
87 	u_int16_t	irq_encoded;	/* encoded IRQ */
88 	void		*sc_ih;		/* interrupt handle */
89 };
90 
91 static void 	ix_reset __P((struct ie_softc *, int));
92 static void 	ix_atten __P((struct ie_softc *, int));
93 static int 	ix_intrhook __P((struct ie_softc *, int));
94 
95 static void     ix_copyin __P((struct ie_softc *, void *, int, size_t));
96 static void     ix_copyout __P((struct ie_softc *, const void *, int, size_t));
97 
98 static void	ix_bus_barrier __P((struct ie_softc *, int, int, int));
99 
100 static u_int16_t ix_read_16 __P((struct ie_softc *, int));
101 static void	ix_write_16 __P((struct ie_softc *, int, u_int16_t));
102 static void	ix_write_24 __P((struct ie_softc *, int, int));
103 static void	ix_zeromem  __P((struct ie_softc *, int, int));
104 
105 static void	ix_mediastatus __P((struct ie_softc *, struct ifmediareq *));
106 
107 static u_int16_t ix_read_eeprom __P((bus_space_tag_t, bus_space_handle_t, int));
108 static void	ix_eeprom_outbits __P((bus_space_tag_t, bus_space_handle_t, int, int));
109 static int	ix_eeprom_inbits  __P((bus_space_tag_t, bus_space_handle_t));
110 static void	ix_eeprom_clock   __P((bus_space_tag_t, bus_space_handle_t, int));
111 
112 int ix_match __P((struct device *, struct cfdata *, void *));
113 void ix_attach __P((struct device *, struct device *, void *));
114 
115 /*
116  * EtherExpress/16 support routines
117  */
118 static void
119 ix_reset(sc, why)
120 	struct ie_softc *sc;
121 	int why;
122 {
123 	struct ix_softc* isc = (struct ix_softc *) sc;
124 
125 	switch (why) {
126 	case CHIP_PROBE:
127 		bus_space_write_1(isc->sc_regt, isc->sc_regh, IX_ECTRL,
128 				  IX_RESET_586);
129 		delay(100);
130 		bus_space_write_1(isc->sc_regt, isc->sc_regh, IX_ECTRL, 0);
131 		delay(100);
132 		break;
133 
134 	case CARD_RESET:
135 		break;
136     }
137 }
138 
139 static void
140 ix_atten(sc, why)
141 	struct ie_softc *sc;
142 	int why;
143 {
144 	struct ix_softc* isc = (struct ix_softc *) sc;
145 	bus_space_write_1(isc->sc_regt, isc->sc_regh, IX_ATTN, 0);
146 }
147 
148 static u_int16_t
149 ix_read_eeprom(iot, ioh, location)
150 	bus_space_tag_t iot;
151 	bus_space_handle_t ioh;
152 	int location;
153 {
154 	int ectrl, edata;
155 
156 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
157 	ectrl &= IX_ECTRL_MASK;
158 	ectrl |= IX_ECTRL_EECS;
159 	bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
160 
161 	ix_eeprom_outbits(iot, ioh, IX_EEPROM_READ, IX_EEPROM_OPSIZE1);
162 	ix_eeprom_outbits(iot, ioh, location, IX_EEPROM_ADDR_SIZE);
163 	edata = ix_eeprom_inbits(iot, ioh);
164 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
165 	ectrl &= ~(IX_RESET_ASIC | IX_ECTRL_EEDI | IX_ECTRL_EECS);
166 	bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
167 	ix_eeprom_clock(iot, ioh, 1);
168 	ix_eeprom_clock(iot, ioh, 0);
169 	return (edata);
170 }
171 
172 static void
173 ix_eeprom_outbits(iot, ioh, edata, count)
174 	bus_space_tag_t iot;
175 	bus_space_handle_t ioh;
176 	int edata, count;
177 {
178 	int ectrl, i;
179 
180 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
181 	ectrl &= ~IX_RESET_ASIC;
182 	for (i = count - 1; i >= 0; i--) {
183 		ectrl &= ~IX_ECTRL_EEDI;
184 		if (edata & (1 << i)) {
185 			ectrl |= IX_ECTRL_EEDI;
186 		}
187 		bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
188 		delay(1);	/* eeprom data must be setup for 0.4 uSec */
189 		ix_eeprom_clock(iot, ioh, 1);
190 		ix_eeprom_clock(iot, ioh, 0);
191 	}
192 	ectrl &= ~IX_ECTRL_EEDI;
193 	bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
194 	delay(1);		/* eeprom data must be held for 0.4 uSec */
195 }
196 
197 static int
198 ix_eeprom_inbits(iot, ioh)
199 	bus_space_tag_t iot;
200 	bus_space_handle_t ioh;
201 {
202 	int ectrl, edata, i;
203 
204 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
205 	ectrl &= ~IX_RESET_ASIC;
206 	for (edata = 0, i = 0; i < 16; i++) {
207 		edata = edata << 1;
208 		ix_eeprom_clock(iot, ioh, 1);
209 		ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
210 		if (ectrl & IX_ECTRL_EEDO) {
211 			edata |= 1;
212 		}
213 		ix_eeprom_clock(iot, ioh, 0);
214 	}
215 	return (edata);
216 }
217 
218 static void
219 ix_eeprom_clock(iot, ioh, state)
220 	bus_space_tag_t iot;
221 	bus_space_handle_t ioh;
222 	int state;
223 {
224 	int ectrl;
225 
226 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
227 	ectrl &= ~(IX_RESET_ASIC | IX_ECTRL_EESK);
228 	if (state) {
229 		ectrl |= IX_ECTRL_EESK;
230 	}
231 	bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
232 	delay(9);		/* EESK must be stable for 8.38 uSec */
233 }
234 
235 static int
236 ix_intrhook(sc, where)
237 	struct ie_softc *sc;
238 	int where;
239 {
240 	struct ix_softc* isc = (struct ix_softc *) sc;
241 
242 	switch (where) {
243 	case INTR_ENTER:
244 		/* entering ISR: disable card interrupts */
245 		bus_space_write_1(isc->sc_regt, isc->sc_regh,
246 				  IX_IRQ, isc->irq_encoded);
247 		break;
248 
249 	case INTR_EXIT:
250 		/* exiting ISR: re-enable card interrupts */
251 		bus_space_write_1(isc->sc_regt, isc->sc_regh, IX_IRQ,
252     				  isc->irq_encoded | IX_IRQ_ENABLE);
253 	break;
254     }
255 
256     return 1;
257 }
258 
259 
260 static void
261 ix_copyin (sc, dst, offset, size)
262         struct ie_softc *sc;
263         void *dst;
264         int offset;
265         size_t size;
266 {
267 	int i, dribble;
268 	u_int8_t* bptr = dst;
269 	u_int16_t* wptr = dst;
270 	struct ix_softc* isc = (struct ix_softc *) sc;
271 
272 	if (isc->use_pio) {
273 		/* Reset read pointer to the specified offset */
274 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
275 						  BUS_SPACE_BARRIER_READ);
276 		bus_space_write_2(sc->bt, sc->bh, IX_READPTR, offset);
277 		bus_space_barrier(sc->bt, sc->bh, IX_READPTR, 2,
278 						  BUS_SPACE_BARRIER_WRITE);
279 	} else {
280 	bus_space_barrier(sc->bt, sc->bh, offset, size,
281 			  BUS_SPACE_BARRIER_READ);
282 	}
283 
284 	if (offset % 2) {
285 		if (isc->use_pio)
286 			*bptr = bus_space_read_1(sc->bt, sc->bh, IX_DATAPORT);
287 		else
288 		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
289 		offset++; bptr++; size--;
290 	}
291 
292 	dribble = size % 2;
293 	wptr = (u_int16_t*) bptr;
294 
295 	if (isc->use_pio) {
296 		for(i = 0; i <  size / 2; i++) {
297 			*wptr = bus_space_read_2(sc->bt, sc->bh, IX_DATAPORT);
298 			wptr++;
299 		}
300 	} else {
301 		bus_space_read_region_2(sc->bt, sc->bh, offset,
302 					(u_int16_t *) bptr, size / 2);
303 	}
304 
305 	if (dribble) {
306 		bptr += size - 1;
307 		offset += size - 1;
308 
309 		if (isc->use_pio)
310 			*bptr = bus_space_read_1(sc->bt, sc->bh, IX_DATAPORT);
311 		else
312 		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
313 	}
314 }
315 
316 static void
317 ix_copyout (sc, src, offset, size)
318         struct ie_softc *sc;
319         const void *src;
320         int offset;
321         size_t size;
322 {
323 	int i, dribble;
324 	int osize = size;
325 	int ooffset = offset;
326 	const u_int8_t* bptr = src;
327 	const u_int16_t* wptr = src;
328 	struct ix_softc* isc = (struct ix_softc *) sc;
329 
330 	if (isc->use_pio) {
331 		/* Reset write pointer to the specified offset */
332 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR, offset);
333 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
334 						  BUS_SPACE_BARRIER_WRITE);
335 	}
336 
337 	if (offset % 2) {
338 		if (isc->use_pio)
339 			bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT, *bptr);
340 		else
341 		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
342 		offset++; bptr++; size--;
343 	}
344 
345 	dribble = size % 2;
346 	wptr = (u_int16_t*) bptr;
347 
348 	if (isc->use_pio) {
349 		for(i = 0; i < size / 2; i++) {
350 			bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT, *wptr);
351 			wptr++;
352 		}
353 	} else {
354 		bus_space_write_region_2(sc->bt, sc->bh, offset,
355 					 (u_int16_t *)bptr, size / 2);
356 	}
357 
358 	if (dribble) {
359 		bptr += size - 1;
360 		offset += size - 1;
361 
362 		if (isc->use_pio)
363 			bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT, *bptr);
364 		else
365 		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
366 	}
367 
368 	if (isc->use_pio)
369 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
370 						  BUS_SPACE_BARRIER_WRITE);
371 	else
372 	bus_space_barrier(sc->bt, sc->bh, ooffset, osize,
373 			  BUS_SPACE_BARRIER_WRITE);
374 }
375 
376 static void
377 ix_bus_barrier(sc, offset, length, flags)
378         struct ie_softc *sc;
379         int offset, length, flags;
380 {
381 	struct ix_softc* isc = (struct ix_softc *) sc;
382 
383 	if (isc->use_pio)
384 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2, flags);
385 	else
386 		bus_space_barrier(sc->bt, sc->bh, offset, length, flags);
387 }
388 
389 static u_int16_t
390 ix_read_16 (sc, offset)
391         struct ie_softc *sc;
392         int offset;
393 {
394 	struct ix_softc* isc = (struct ix_softc *) sc;
395 
396 	if (isc->use_pio) {
397 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
398 						  BUS_SPACE_BARRIER_READ);
399 
400 		/* Reset read pointer to the specified offset */
401 		bus_space_write_2(sc->bt, sc->bh, IX_READPTR, offset);
402 		bus_space_barrier(sc->bt, sc->bh, IX_READPTR, 2,
403 						  BUS_SPACE_BARRIER_WRITE);
404 
405 		return bus_space_read_2(sc->bt, sc->bh, IX_DATAPORT);
406 	} else {
407 		bus_space_barrier(sc->bt, sc->bh, offset, 2,
408 						  BUS_SPACE_BARRIER_READ);
409         return bus_space_read_2(sc->bt, sc->bh, offset);
410 	}
411 }
412 
413 static void
414 ix_write_16 (sc, offset, value)
415         struct ie_softc *sc;
416         int offset;
417         u_int16_t value;
418 {
419 	struct ix_softc* isc = (struct ix_softc *) sc;
420 
421 	if (isc->use_pio) {
422 		/* Reset write pointer to the specified offset */
423 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR, offset);
424 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
425 						  BUS_SPACE_BARRIER_WRITE);
426 
427 		bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT, value);
428 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
429 						  BUS_SPACE_BARRIER_WRITE);
430 	} else {
431         bus_space_write_2(sc->bt, sc->bh, offset, value);
432 		bus_space_barrier(sc->bt, sc->bh, offset, 2,
433 						  BUS_SPACE_BARRIER_WRITE);
434 	}
435 }
436 
437 static void
438 ix_write_24 (sc, offset, addr)
439         struct ie_softc *sc;
440         int offset, addr;
441 {
442 	char* ptr;
443 	struct ix_softc* isc = (struct ix_softc *) sc;
444 	int val = addr + (u_long) sc->sc_maddr - (u_long) sc->sc_iobase;
445 
446 	if (isc->use_pio) {
447 		/* Reset write pointer to the specified offset */
448 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR, offset);
449 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
450 						  BUS_SPACE_BARRIER_WRITE);
451 
452 		ptr = (char*) &val;
453 		bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT,
454 						  *((u_int16_t *)ptr));
455 		bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT,
456 						  *((u_int16_t *)(ptr + 2)));
457 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
458 						  BUS_SPACE_BARRIER_WRITE);
459 	} else {
460         	bus_space_write_4(sc->bt, sc->bh, offset, val);
461 		bus_space_barrier(sc->bt, sc->bh, offset, 4,
462 						  BUS_SPACE_BARRIER_WRITE);
463 	}
464 }
465 
466 static void
467 ix_zeromem(sc, offset, count)
468         struct ie_softc *sc;
469         int offset, count;
470 {
471 	int i;
472 	int dribble;
473 	struct ix_softc* isc = (struct ix_softc *) sc;
474 
475 	if (isc->use_pio) {
476 		/* Reset write pointer to the specified offset */
477 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR, offset);
478 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
479 						  BUS_SPACE_BARRIER_WRITE);
480 
481 		if (offset % 2) {
482 			bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT, 0);
483 			count--;
484 		}
485 
486 	        dribble = count % 2;
487 		for(i = 0; i < count / 2; i++)
488 			bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT, 0);
489 
490 		if (dribble)
491 			bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT, 0);
492 
493 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
494 						  BUS_SPACE_BARRIER_WRITE);
495 	} else {
496 		bus_space_set_region_1(sc->bt, sc->bh, offset, 0, count);
497 		bus_space_barrier(sc->bt, sc->bh, offset, count,
498 						  BUS_SPACE_BARRIER_WRITE);
499 	}
500 }
501 
502 static void
503 ix_mediastatus(sc, ifmr)
504         struct ie_softc *sc;
505         struct ifmediareq *ifmr;
506 {
507         struct ifmedia *ifm = &sc->sc_media;
508 
509         /*
510          * The currently selected media is always the active media.
511          */
512         ifmr->ifm_active = ifm->ifm_cur->ifm_media;
513 }
514 
515 int
516 ix_match(parent, cf, aux)
517 	struct device *parent;
518 	struct cfdata *cf;
519 	void *aux;
520 {
521 	int i;
522 	int rv = 0;
523 	bus_addr_t maddr;
524 	bus_size_t msize;
525 	u_short checksum = 0;
526 	bus_space_handle_t ioh;
527 	bus_space_tag_t iot;
528 	u_int8_t val, bart_config;
529 	u_short pg, adjust, decode, edecode;
530 	u_short board_id, id_var1, id_var2, irq, irq_encoded;
531 	struct isa_attach_args * const ia = aux;
532 	short irq_translate[] = {0, 0x09, 0x03, 0x04, 0x05, 0x0a, 0x0b, 0};
533 
534 	iot = ia->ia_iot;
535 
536 	if (bus_space_map(iot, ia->ia_iobase,
537 			  IX_IOSIZE, 0, &ioh) != 0) {
538 		DPRINTF(("Can't map io space at 0x%x\n", ia->ia_iobase));
539 		return (0);
540 	}
541 
542 	/* XXX: reset any ee16 at the current iobase */
543 	bus_space_write_1(iot, ioh, IX_ECTRL, IX_RESET_ASIC);
544 	bus_space_write_1(iot, ioh, IX_ECTRL, 0);
545 	delay(240);
546 
547 	/* now look for ee16. */
548 	board_id = id_var1 = id_var2 = 0;
549 	for (i = 0; i < 4 ; i++) {
550 		id_var1 = bus_space_read_1(iot, ioh, IX_ID_PORT);
551 		id_var2 = ((id_var1 & 0x03) << 2);
552 		board_id |= (( id_var1 >> 4)  << id_var2);
553 	}
554 
555 	if (board_id != IX_ID) {
556 		DPRINTF(("BART ID mismatch (got 0x%04x, expected 0x%04x)\n",
557 			board_id, IX_ID));
558 		goto out;
559 	}
560 
561 	/*
562 	 * The shared RAM size and location of the EE16 is encoded into
563 	 * EEPROM location 6.  The location of the first set bit tells us
564 	 * the memory address (0xc0000 + (0x4000 * FSB)), where FSB is the
565 	 * number of the first set bit.  The zeroes are then shifted out,
566 	 * and the results is the memory size (1 = 16k, 3 = 32k, 7 = 48k,
567 	 * 0x0f = 64k).
568 	 *
569 	 * Examples:
570 	 *   0x3c -> 64k@0xc8000, 0x70 -> 48k@0xd0000, 0xc0 -> 32k@0xd8000
571 	 *   0x80 -> 16k@0xdc000.
572 	 *
573 	 * Side note: this comes from reading the old driver rather than
574 	 * from a more definitive source, so it could be out-of-whack
575 	 * with what the card can do...
576 	 */
577 
578 	val = ix_read_eeprom(iot, ioh, 6) & 0xff;
579 	for(pg = 0; pg < 8; pg++) {
580 		if (val & 1)
581 			break;
582 		val = val >> 1;
583 	}
584 
585 	if (pg == 8) {
586 		DPRINTF(("Invalid or unsupported memory config\n"));
587 		goto out;
588 	}
589 
590 	maddr = 0xc0000 + (pg * 0x4000);
591 
592 	switch (val) {
593 	case 0x00:
594 		msize = 0;
595 		break;
596 
597 	case 0x01:
598 		msize = 16 * 1024;
599 		break;
600 
601 	case 0x03:
602 		msize = 32 * 1024;
603 		break;
604 
605 	case 0x07:
606 		msize = 48 * 1024;
607 		break;
608 
609 	case 0x0f:
610 		msize = 64 * 1024;
611 		break;
612 
613 	default:
614 		DPRINTF(("invalid memory size %02x\n", val));
615 		goto out;
616 	}
617 
618 	if (ia->ia_maddr == ISACF_IOMEM_DEFAULT)
619 		ia->ia_maddr = maddr;
620 	else if (ia->ia_maddr != maddr) {
621 		DPRINTF((
622 		  "ix_match: memaddr of board @ 0x%x doesn't match config\n",
623 		  ia->ia_iobase));
624 		goto out;
625 	}
626 
627 	if (ia->ia_msize == ISACF_IOSIZ_DEFAULT)
628 		ia->ia_msize = msize;
629 	else if (ia->ia_msize != msize) {
630 		DPRINTF((
631 		   "ix_match: memsize of board @ 0x%x doesn't match config\n",
632 		   ia->ia_iobase));
633 		goto out;
634 	}
635 
636 	/* need to put the 586 in RESET, and leave it */
637 	bus_space_write_1(iot, ioh, IX_ECTRL, IX_RESET_586);
638 
639 	/* read the eeprom and checksum it, should == IX_ID */
640 	for(i = 0; i < 0x40; i++)
641 		checksum += ix_read_eeprom(iot, ioh, i);
642 
643 	if (checksum != IX_ID) {
644 		DPRINTF(("checksum mismatch (got 0x%04x, expected 0x%04x\n",
645 			checksum, IX_ID));
646 		goto out;
647 	}
648 
649 	/*
650 	 * Only do the following bit if using memory-mapped access.  For
651 	 * boards with no mapped memory, we use PIO.  We also use PIO for
652 	 * boards with 16K of mapped memory, as those setups don't seem
653 	 * to work otherwise.
654 	 */
655 	if (msize != 0 && msize != 16384) {
656 		/* Set board up with memory-mapping info */
657 	adjust = IX_MCTRL_FMCS16 | (pg & 0x3) << 2;
658 	decode = ((1 << (ia->ia_msize / 16384)) - 1) << pg;
659 	edecode = ((~decode >> 4) & 0xF0) | (decode >> 8);
660 
661 	bus_space_write_1(iot, ioh, IX_MEMDEC, decode & 0xFF);
662 	bus_space_write_1(iot, ioh, IX_MCTRL, adjust);
663 	bus_space_write_1(iot, ioh, IX_MPCTRL, (~decode & 0xFF));
664 
665 		/* XXX disable Exxx */
666 		bus_space_write_1(iot, ioh, IX_MECTRL, edecode);
667 	}
668 
669 	/*
670 	 * Get the encoded interrupt number from the EEPROM, check it
671 	 * against the passed in IRQ.  Issue a warning if they do not
672 	 * match, and fail the probe.  If irq is 'IRQUNK' then we
673 	 * use the EEPROM irq, and continue.
674 	 */
675 	irq_encoded = ix_read_eeprom(iot, ioh, IX_EEPROM_CONFIG1);
676 	irq_encoded = (irq_encoded & IX_EEPROM_IRQ) >> IX_EEPROM_IRQ_SHIFT;
677 	irq = irq_translate[irq_encoded];
678 	if (ia->ia_irq == ISACF_IRQ_DEFAULT)
679 		ia->ia_irq = irq;
680 	else if (irq != ia->ia_irq) {
681 		DPRINTF(("board IRQ %d does not match config\n", irq));
682 		goto out;
683 	}
684 
685 	/* disable the board interrupts */
686 	bus_space_write_1(iot, ioh, IX_IRQ, irq_encoded);
687 
688 	bart_config = bus_space_read_1(iot, ioh, IX_CONFIG);
689 	bart_config |= IX_BART_LOOPBACK;
690 	bart_config |= IX_BART_MCS16_TEST; /* inb doesn't get bit! */
691 	bus_space_write_1(iot, ioh, IX_CONFIG, bart_config);
692 	bart_config = bus_space_read_1(iot, ioh, IX_CONFIG);
693 
694 	bus_space_write_1(iot, ioh, IX_ECTRL, 0);
695 	delay(100);
696 
697 	rv = 1;
698 	ia->ia_iosize = IX_IOSIZE;
699 	DPRINTF(("ix_match: found board @ 0x%x\n", ia->ia_iobase));
700 
701 out:
702 	bus_space_unmap(iot, ioh, IX_IOSIZE);
703 	return (rv);
704 }
705 
706 void
707 ix_attach(parent, self, aux)
708 	struct device *parent;
709 	struct device *self;
710 	void   *aux;
711 {
712 	struct ix_softc *isc = (void *)self;
713 	struct ie_softc *sc = &isc->sc_ie;
714 	struct isa_attach_args *ia = aux;
715 
716 	int media;
717 	int i, memsize;
718 	u_int8_t bart_config;
719 	bus_space_tag_t iot;
720 	u_int8_t bpat, bval;
721 	u_int16_t wpat, wval;
722 	bus_space_handle_t ioh, memh;
723 	u_short irq_encoded;
724 	u_int8_t ethaddr[ETHER_ADDR_LEN];
725 
726 	iot = ia->ia_iot;
727 
728 	/*
729 	 * Shared memory access seems to fail on 16K mapped boards, so
730 	 * disable shared memory access if the board is in 16K mode.  If
731 	 * no memory is mapped, we have no choice but to use PIO
732 	 */
733 	isc->use_pio = (ia->ia_msize <= (16 * 1024));
734 
735 	if (bus_space_map(iot, ia->ia_iobase,
736 			  ia->ia_iosize, 0, &ioh) != 0) {
737 
738 		DPRINTF(("\n%s: can't map i/o space 0x%x-0x%x\n",
739 			  sc->sc_dev.dv_xname, ia->ia_iobase,
740 			  ia->ia_iobase + ia->ia_iosize - 1));
741 		return;
742 	}
743 
744 	/* We map memory even if using PIO so something else doesn't grab it */
745 	if (ia->ia_msize) {
746 	if (bus_space_map(ia->ia_memt, ia->ia_maddr,
747 			  ia->ia_msize, 0, &memh) != 0) {
748 		DPRINTF(("\n%s: can't map iomem space 0x%x-0x%x\n",
749 			sc->sc_dev.dv_xname, ia->ia_maddr,
750 			ia->ia_maddr + ia->ia_msize - 1));
751 		bus_space_unmap(iot, ioh, ia->ia_iosize);
752 		return;
753 	}
754 	}
755 
756 	isc->sc_regt = iot;
757 	isc->sc_regh = ioh;
758 
759 	/*
760 	 * Get the hardware ethernet address from the EEPROM and
761 	 * save it in the softc for use by the 586 setup code.
762 	 */
763 	wval = ix_read_eeprom(iot, ioh, IX_EEPROM_ENET_HIGH);
764 	ethaddr[1] = wval & 0xFF;
765 	ethaddr[0] = wval >> 8;
766 	wval = ix_read_eeprom(iot, ioh, IX_EEPROM_ENET_MID);
767 	ethaddr[3] = wval & 0xFF;
768 	ethaddr[2] = wval >> 8;
769 	wval = ix_read_eeprom(iot, ioh, IX_EEPROM_ENET_LOW);
770 	ethaddr[5] = wval & 0xFF;
771 	ethaddr[4] = wval >> 8;
772 
773 	sc->hwinit = NULL;
774 	sc->hwreset = ix_reset;
775 	sc->chan_attn = ix_atten;
776 	sc->intrhook = ix_intrhook;
777 
778 	sc->memcopyin = ix_copyin;
779 	sc->memcopyout = ix_copyout;
780 
781 	/* If using PIO, make sure to setup single-byte read/write functions */
782 	if (isc->use_pio) {
783 		sc->ie_bus_barrier = ix_bus_barrier;
784 	} else {
785 		sc->ie_bus_barrier = NULL;
786 	}
787 
788 	sc->ie_bus_read16 = ix_read_16;
789 	sc->ie_bus_write16 = ix_write_16;
790 	sc->ie_bus_write24 = ix_write_24;
791 
792 	sc->do_xmitnopchain = 0;
793 
794 	sc->sc_mediachange = NULL;
795 	sc->sc_mediastatus = ix_mediastatus;
796 
797 	if (isc->use_pio) {
798 		sc->bt = iot;
799 		sc->bh = ioh;
800 
801 		/*
802 		 * If using PIO, the memory size is bounded by on-card memory,
803 		 * not by how much is mapped into the memory-mapped region, so
804 		 * determine how much total memory we have to play with here.
805 		 */
806 		for(memsize = 64 * 1024; memsize; memsize -= 16 * 1024) {
807 			/* warm up shared memory, the zero it all out */
808 			ix_zeromem(sc, 0, 32);
809 			ix_zeromem(sc, 0, memsize);
810 
811 			/* Reset write pointer to the start of RAM */
812 			bus_space_write_2(iot, ioh, IX_WRITEPTR, 0);
813 			bus_space_barrier(iot, ioh, IX_WRITEPTR, 2,
814 						    BUS_SPACE_BARRIER_WRITE);
815 
816 			/* write test pattern */
817 			for(i = 0, wpat = 1; i < memsize; i += 2) {
818 				bus_space_write_2(iot, ioh, IX_DATAPORT, wpat);
819 				wpat += 3;
820 			}
821 
822 			/* Flush all reads & writes to data port */
823 			bus_space_barrier(iot, ioh, IX_DATAPORT, 2,
824 						    BUS_SPACE_BARRIER_READ |
825 						    BUS_SPACE_BARRIER_WRITE);
826 
827 			/* Reset read pointer to beginning of card RAM */
828 			bus_space_write_2(iot, ioh, IX_READPTR, 0);
829 			bus_space_barrier(iot, ioh, IX_READPTR, 2,
830 						    BUS_SPACE_BARRIER_WRITE);
831 
832 			/* read and verify test pattern */
833 			for(i = 0, wpat = 1; i < memsize; i += 2) {
834 				wval = bus_space_read_2(iot, ioh, IX_DATAPORT);
835 
836 				if (wval != wpat)
837 					break;
838 
839 				wpat += 3;
840 			}
841 
842 			/* If we failed, try next size down */
843 			if (i != memsize)
844 				continue;
845 
846 			/* Now try it all with byte reads/writes */
847 			ix_zeromem(sc, 0, 32);
848 			ix_zeromem(sc, 0, memsize);
849 
850 			/* Reset write pointer to start of card RAM */
851 			bus_space_write_2(iot, ioh, IX_WRITEPTR, 0);
852 			bus_space_barrier(iot, ioh, IX_WRITEPTR, 2,
853 						    BUS_SPACE_BARRIER_WRITE);
854 
855 			/* write out test pattern */
856 			for(i = 0, bpat = 1; i < memsize; i++) {
857 				bus_space_write_1(iot, ioh, IX_DATAPORT, bpat);
858 				bpat += 3;
859 			}
860 
861 			/* Flush all reads & writes to data port */
862 			bus_space_barrier(iot, ioh, IX_DATAPORT, 2,
863 						    BUS_SPACE_BARRIER_READ |
864 						    BUS_SPACE_BARRIER_WRITE);
865 
866 			/* Reset read pointer to beginning of card RAM */
867 			bus_space_write_2(iot, ioh, IX_READPTR, 0);
868 			bus_space_barrier(iot, ioh, IX_READPTR, 2,
869 						    BUS_SPACE_BARRIER_WRITE);
870 
871 			/* read and verify test pattern */
872 			for(i = 0, bpat = 1; i < memsize; i++) {
873 				bval = bus_space_read_1(iot, ioh, IX_DATAPORT);
874 
875 				if (bval != bpat)
876 				bpat += 3;
877 			}
878 
879 			/* If we got through all of memory, we're done! */
880 			if (i == memsize)
881 				break;
882 		}
883 
884 		/* Memory tests failed, punt... */
885 		if (memsize == 0)  {
886 			DPRINTF(("\n%s: can't determine size of on-card RAM\n",
887 				sc->sc_dev.dv_xname));
888 			bus_space_unmap(iot, ioh, ia->ia_iosize);
889 			return;
890 		}
891 
892 		sc->bt = iot;
893 		sc->bh = ioh;
894 
895 		sc->sc_msize = memsize;
896 		sc->sc_maddr = (void*) 0;
897 	} else {
898 	sc->bt = ia->ia_memt;
899 	sc->bh = memh;
900 
901 	sc->sc_msize = ia->ia_msize;
902 	sc->sc_maddr = (void *)memh;
903 	}
904 
905 	/* Map i/o space. */
906 	sc->sc_iobase = (char *)sc->sc_maddr + sc->sc_msize - (1 << 24);
907 
908 	/* set up pointers to important on-card control structures */
909 	sc->iscp = 0;
910 	sc->scb = IE_ISCP_SZ;
911 	sc->scp = sc->sc_msize + IE_SCP_ADDR - (1 << 24);
912 
913 	sc->buf_area = sc->scb + IE_SCB_SZ;
914 	sc->buf_area_sz = sc->sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;
915 
916 	/* zero card memory */
917 	ix_zeromem(sc, 0, 32);
918 	ix_zeromem(sc, 0, sc->sc_msize);
919 
920 	/* set card to 16-bit bus mode */
921 	if (isc->use_pio) {
922 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR,
923 				  	    IE_SCP_BUS_USE((u_long)sc->scp));
924 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
925 					          BUS_SPACE_BARRIER_WRITE);
926 
927 		bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT,
928 				  IE_SYSBUS_16BIT);
929 	} else {
930 		bus_space_write_1(sc->bt, sc->bh,
931 				  IE_SCP_BUS_USE((u_long)sc->scp),
932 				  IE_SYSBUS_16BIT);
933 	}
934 
935 	/* set up pointers to key structures */
936 	ix_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp);
937 	ix_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb);
938 	ix_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp);
939 
940 	/* flush setup of pointers, check if chip answers */
941 	if (isc->use_pio) {
942 		bus_space_barrier(sc->bt, sc->bh, 0, IX_IOSIZE,
943 				  BUS_SPACE_BARRIER_WRITE);
944 	} else {
945 	bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize,
946 			  BUS_SPACE_BARRIER_WRITE);
947 	}
948 
949 	if (!i82586_proberam(sc)) {
950 		DPRINTF(("\n%s: Can't talk to i82586!\n",
951 			sc->sc_dev.dv_xname));
952 		bus_space_unmap(iot, ioh, ia->ia_iosize);
953 
954 		if (ia->ia_msize)
955 		bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
956 		return;
957 	}
958 
959 	/* Figure out which media is being used... */
960 	if (ix_read_eeprom(iot, ioh, IX_EEPROM_CONFIG1) &
961 				IX_EEPROM_MEDIA_EXT) {
962 		if (ix_read_eeprom(iot, ioh, IX_EEPROM_MEDIA) &
963 				IX_EEPROM_MEDIA_TP)
964 			media = IFM_ETHER | IFM_10_T;
965 		else
966 			media = IFM_ETHER | IFM_10_2;
967 	} else
968 		media = IFM_ETHER | IFM_10_5;
969 
970 	/* Take the card out of lookback */
971 	bart_config = bus_space_read_1(iot, ioh, IX_CONFIG);
972 	bart_config &= ~IX_BART_LOOPBACK;
973 	bart_config |= IX_BART_MCS16_TEST; /* inb doesn't get bit! */
974 	bus_space_write_1(iot, ioh, IX_CONFIG, bart_config);
975 	bart_config = bus_space_read_1(iot, ioh, IX_CONFIG);
976 
977 	irq_encoded = ix_read_eeprom(iot, ioh,
978 				     IX_EEPROM_CONFIG1);
979 	irq_encoded = (irq_encoded & IX_EEPROM_IRQ) >> IX_EEPROM_IRQ_SHIFT;
980 
981 	/* Enable interrupts */
982 	bus_space_write_1(iot, ioh, IX_IRQ,
983 			  irq_encoded | IX_IRQ_ENABLE);
984 
985 	/* Flush all writes to registers */
986 	bus_space_barrier(iot, ioh, 0, ia->ia_iosize, BUS_SPACE_BARRIER_WRITE);
987 
988 	isc->irq_encoded = irq_encoded;
989 
990 	i82586_attach(sc, "EtherExpress/16", ethaddr,
991 		      ix_media, NIX_MEDIA, media);
992 
993 	if (isc->use_pio)
994 		printf("%s: unsupported memory config, using PIO to access %d bytes of memory\n", sc->sc_dev.dv_xname, sc->sc_msize);
995 
996 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
997 					IPL_NET, i82586_intr, sc);
998 	if (isc->sc_ih == NULL)
999 		DPRINTF(("\n%s: can't establish interrupt\n",
1000 			sc->sc_dev.dv_xname));
1001 }
1002 
1003 struct cfattach ix_ca = {
1004 	sizeof(struct ix_softc), ix_match, ix_attach
1005 };
1006