xref: /NextBSD/sys/dev/fe/if_fe_isa.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*-
2  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
3  *
4  * This software may be used, modified, copied, distributed, and sold, in
5  * both source and binary form provided that the above copyright, these
6  * terms and the following disclaimer are retained.  The name of the author
7  * and/or the contributor may not be used to endorse or promote products
8  * derived from this software without specific prior written permission.
9  *
10  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
11  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
14  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
16  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
17  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20  * SUCH DAMAGE.
21  *
22  */
23 
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
26 
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/socket.h>
31 #include <sys/module.h>
32 
33 #include <sys/bus.h>
34 #include <machine/bus.h>
35 #include <sys/rman.h>
36 
37 #include <net/ethernet.h>
38 #include <net/if.h>
39 #include <net/if_mib.h>
40 #include <net/if_media.h>
41 
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
44 
45 #include <dev/fe/mb86960.h>
46 #include <dev/fe/if_fereg.h>
47 #include <dev/fe/if_fevar.h>
48 
49 #include <isa/isavar.h>
50 
51 /*
52  *	ISA specific code.
53  */
54 static int fe_isa_probe(device_t);
55 static int fe_isa_attach(device_t);
56 
57 static device_method_t fe_isa_methods[] = {
58 	/* Device interface */
59 	DEVMETHOD(device_probe,		fe_isa_probe),
60 	DEVMETHOD(device_attach,	fe_isa_attach),
61 
62 	{ 0, 0 }
63 };
64 
65 static driver_t fe_isa_driver = {
66 	"fe",
67 	fe_isa_methods,
68 	sizeof (struct fe_softc)
69 };
70 
71 DRIVER_MODULE(fe, isa, fe_isa_driver, fe_devclass, 0, 0);
72 
73 
74 static int fe_probe_ssi(device_t);
75 static int fe_probe_jli(device_t);
76 static int fe_probe_fmv(device_t);
77 static int fe_probe_lnx(device_t);
78 static int fe_probe_gwy(device_t);
79 static int fe_probe_ubn(device_t);
80 
81 /*
82  * Determine if the device is present at a specified I/O address.  The
83  * main entry to the driver.
84  */
85 static int
fe_isa_probe(device_t dev)86 fe_isa_probe(device_t dev)
87 {
88 	struct fe_softc *sc;
89 	int error;
90 
91 	/* Check isapnp ids */
92 	if (isa_get_vendorid(dev))
93 		return (ENXIO);
94 
95 	/* Prepare for the softc struct.  */
96 	sc = device_get_softc(dev);
97 	sc->sc_unit = device_get_unit(dev);
98 
99 	/* Probe for supported boards.  */
100 	if ((error = fe_probe_ssi(dev)) == 0)
101 		goto end;
102 	fe_release_resource(dev);
103 
104 	if ((error = fe_probe_jli(dev)) == 0)
105 		goto end;
106 	fe_release_resource(dev);
107 
108 	if ((error = fe_probe_fmv(dev)) == 0)
109 		goto end;
110 	fe_release_resource(dev);
111 
112 	if ((error = fe_probe_lnx(dev)) == 0)
113 		goto end;
114 	fe_release_resource(dev);
115 
116 	if ((error = fe_probe_ubn(dev)) == 0)
117 		goto end;
118 	fe_release_resource(dev);
119 
120 	if ((error = fe_probe_gwy(dev)) == 0)
121 		goto end;
122 	fe_release_resource(dev);
123 
124 end:
125 	if (error == 0)
126 		error = fe_alloc_irq(dev, 0);
127 
128 	fe_release_resource(dev);
129 	return (error);
130 }
131 
132 static int
fe_isa_attach(device_t dev)133 fe_isa_attach(device_t dev)
134 {
135 	struct fe_softc *sc = device_get_softc(dev);
136 	int error = 0;
137 
138 	/*
139 	 * Note: these routines aren't expected to fail since we also call
140 	 * them in the probe routine.  But coverity complains, so we'll honor
141 	 * that complaint since the intention here was never to ignore them..
142 	 */
143 	if (sc->port_used) {
144 		error = fe_alloc_port(dev, sc->port_used);
145 		if (error != 0)
146 			return (error);
147 	}
148 	error = fe_alloc_irq(dev, 0);
149 	if (error != 0)
150 		return (error);
151 
152 	return fe_attach(dev);
153 }
154 
155 
156 /*
157  * Probe and initialization for Fujitsu FMV-180 series boards
158  */
159 
160 static void
fe_init_fmv(struct fe_softc * sc)161 fe_init_fmv(struct fe_softc *sc)
162 {
163 	/* Initialize ASIC.  */
164 	fe_outb(sc, FE_FMV3, 0);
165 	fe_outb(sc, FE_FMV10, 0);
166 
167 #if 0
168 	/* "Refresh" hardware configuration.  FIXME.  */
169 	fe_outb(sc, FE_FMV2, fe_inb(sc, FE_FMV2));
170 #endif
171 
172 	/* Turn the "master interrupt control" flag of ASIC on.  */
173 	fe_outb(sc, FE_FMV3, FE_FMV3_IRQENB);
174 }
175 
176 static void
fe_msel_fmv184(struct fe_softc * sc)177 fe_msel_fmv184(struct fe_softc *sc)
178 {
179 	u_char port;
180 
181 	/* FMV-184 has a special "register" to switch between AUI/BNC.
182 	   Determine the value to write into the register, based on the
183 	   user-specified media selection.  */
184 	port = (IFM_SUBTYPE(sc->media.ifm_media) == IFM_10_2) ? 0x00 : 0x01;
185 
186 	/* The register is #5 on exntesion register bank...
187 	   (Details of the register layout is not yet discovered.)  */
188 	fe_outb(sc, 0x1B, 0x46);	/* ??? */
189 	fe_outb(sc, 0x1E, 0x04);	/* select ex-reg #4.  */
190 	fe_outb(sc, 0x1F, 0xC8);	/* ??? */
191 	fe_outb(sc, 0x1E, 0x05);	/* select ex-reg #5.  */
192 	fe_outb(sc, 0x1F, port);	/* Switch the media.  */
193 	fe_outb(sc, 0x1E, 0x04);	/* select ex-reg #4.  */
194 	fe_outb(sc, 0x1F, 0x00);	/* ??? */
195 	fe_outb(sc, 0x1B, 0x00);	/* ??? */
196 
197 	/* Make sure to select "external tranceiver" on MB86964.  */
198 	fe_outb(sc, FE_BMPR13, sc->proto_bmpr13 | FE_B13_PORT_AUI);
199 }
200 
201 static int
fe_probe_fmv(device_t dev)202 fe_probe_fmv(device_t dev)
203 {
204 	struct fe_softc *sc = device_get_softc(dev);
205 	int n;
206 	u_long iobase, irq;
207 
208 	static u_short const irqmap [ 4 ] = { 3, 7, 10, 15 };
209 
210 	static struct fe_simple_probe_struct const probe_table [] = {
211 		{ FE_DLCR2, 0x71, 0x00 },
212 		{ FE_DLCR4, 0x08, 0x00 },
213 
214 		{ FE_FMV0, 0x78, 0x50 },	/* ERRDY+PRRDY */
215 		{ FE_FMV1, 0xB0, 0x00 },	/* FMV-183/4 has 0x48 bits. */
216 		{ FE_FMV3, 0x7F, 0x00 },
217 
218 		{ 0 }
219 	};
220 
221 	/* Board subtypes; it lists known FMV-180 variants.  */
222 	struct subtype {
223 		u_short mcode;
224 		u_short mbitmap;
225 		u_short defmedia;
226 		char const * str;
227 	};
228 	static struct subtype const typelist [] = {
229 	    { 0x0005, MB_HA|MB_HT|MB_H5, MB_HA, "FMV-181"		},
230 	    { 0x0105, MB_HA|MB_HT|MB_H5, MB_HA, "FMV-181A"		},
231 	    { 0x0003, MB_HM,             MB_HM, "FMV-182"		},
232 	    { 0x0103, MB_HM,             MB_HM, "FMV-182A"		},
233 	    { 0x0804, MB_HT,             MB_HT, "FMV-183"		},
234 	    { 0x0C04, MB_HT,             MB_HT, "FMV-183 (on-board)"	},
235 	    { 0x0803, MB_H2|MB_H5,       MB_H2, "FMV-184"		},
236 	    { 0,      MB_HA,             MB_HA, "unknown FMV-180 (?)"	},
237 	};
238 	struct subtype const * type;
239 
240 	/* Media indicator and "Hardware revision ID"  */
241 	u_short mcode;
242 
243 	/* See if the specified address is possible for FMV-180
244            series.  220, 240, 260, 280, 2A0, 2C0, 300, and 340 are
245            allowed for all boards, and 200, 2E0, 320, 360, 380, 3A0,
246            3C0, and 3E0 for PnP boards.  */
247 	if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
248 		return ENXIO;
249 	if ((iobase & ~0x1E0) != 0x200)
250 		return ENXIO;
251 
252 	/* FMV-180 occupies 32 I/O addresses. */
253 	if (fe_alloc_port(dev, 32))
254 		return ENXIO;
255 
256 	/* Setup an I/O address mapping table and some others.  */
257 	fe_softc_defaults(sc);
258 
259 	/* Simple probe.  */
260 	if (!fe_simple_probe(sc, probe_table))
261 		return ENXIO;
262 
263 	/* Get our station address from EEPROM, and make sure it is
264            Fujitsu's.  */
265 	fe_inblk(sc, FE_FMV4, sc->enaddr, ETHER_ADDR_LEN);
266 	if (!fe_valid_Ether_p(sc->enaddr, 0x00000E))
267 		return ENXIO;
268 
269 	/* Find the supported media and "hardware revision" to know
270            the model identification.  */
271 	mcode = (fe_inb(sc, FE_FMV0) & FE_FMV0_MEDIA)
272 	     | ((fe_inb(sc, FE_FMV1) & FE_FMV1_REV) << 8);
273 
274 	/* Determine the card type.  */
275 	for (type = typelist; type->mcode != 0; type++) {
276 		if (type->mcode == mcode)
277 			break;
278 	}
279 	if (type->mcode == 0) {
280 	  	/* Unknown card type...  Hope the driver works.  */
281 		sc->stability |= UNSTABLE_TYPE;
282 		if (bootverbose) {
283 			device_printf(dev, "unknown config: %x-%x-%x-%x\n",
284 				      fe_inb(sc, FE_FMV0),
285 				      fe_inb(sc, FE_FMV1),
286 				      fe_inb(sc, FE_FMV2),
287 				      fe_inb(sc, FE_FMV3));
288 		}
289 	}
290 
291 	/* Setup the board type and media information.  */
292 	sc->type = FE_TYPE_FMV;
293 	sc->typestr = type->str;
294 	sc->mbitmap = type->mbitmap;
295 	sc->defmedia = type->defmedia;
296 	sc->msel = fe_msel_965;
297 
298 	if (type->mbitmap == (MB_H2 | MB_H5)) {
299 		/* FMV184 requires a special media selection procedure.  */
300 		sc->msel = fe_msel_fmv184;
301 	}
302 
303 	/*
304 	 * An FMV-180 has been probed.
305 	 * Determine which IRQ to be used.
306 	 *
307 	 * In this version, we give a priority to the kernel config file.
308 	 * If the EEPROM and config don't match, say it to the user for
309 	 * an attention.
310 	 */
311 	n = (fe_inb(sc, FE_FMV2) & FE_FMV2_IRS)	>> FE_FMV2_IRS_SHIFT;
312 
313 	irq = 0;
314 	bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL);
315 	if (irq == NO_IRQ) {
316 		/* Just use the probed value.  */
317 		bus_set_resource(dev, SYS_RES_IRQ, 0, irqmap[n], 1);
318 	} else if (irq != irqmap[n]) {
319 		/* Don't match.  */
320 		sc->stability |= UNSTABLE_IRQ;
321 	}
322 
323 	/* We need an init hook to initialize ASIC before we start.  */
324 	sc->init = fe_init_fmv;
325 
326 	return 0;
327 }
328 
329 /*
330  * Fujitsu MB86965 JLI mode probe routines.
331  *
332  * 86965 has a special operating mode called JLI (mode 0), under which
333  * the chip interfaces with ISA bus with a software-programmable
334  * configuration.  (The Fujitsu document calls the feature "Plug and
335  * play," but it is not compatible with the ISA-PnP spec. designed by
336  * Intel and Microsoft.)  Ethernet cards designed to use JLI are
337  * almost same, but there are two things which require board-specific
338  * probe routines: EEPROM layout and IRQ pin connection.
339  *
340  * JLI provides a handy way to access EEPROM which should contains the
341  * chip configuration information (such as I/O port address) as well
342  * as Ethernet station (MAC) address.  The chip configuration info. is
343  * stored on a fixed location.  However, the station address can be
344  * located anywhere in the EEPROM; it is up to the board designer to
345  * determine the location.  (The manual just says "somewhere in the
346  * EEPROM.")  The fe driver must somehow find out the correct
347  * location.
348  *
349  * Another problem resides in the IRQ pin connection.  JLI provides a
350  * user to choose an IRQ from up to four predefined IRQs.  The 86965
351  * chip has a register to select one out of the four possibilities.
352  * However, the selection is against the four IRQ pins on the chip.
353  * (So-called IRQ-A, -B, -C and -D.)  It is (again) up to the board
354  * designer to determine which pin to connect which IRQ line on the
355  * ISA bus.  We need a vendor (or model, for some vendor) specific IRQ
356  * mapping table.
357  *
358  * The routine fe_probe_jli() provides all probe and initialization
359  * processes which are common to all JLI implementation, and sub-probe
360  * routines supply board-specific actions.
361  *
362  * JLI sub-probe routine has the following template:
363  *
364  *	u_short const * func (struct fe_softc * sc, u_char const * eeprom);
365  *
366  * where eeprom is a pointer to an array of 32 byte data read from the
367  * config EEPROM on the board.  It retuns an IRQ mapping table for the
368  * board, when the corresponding implementation is detected.  It
369  * returns a NULL otherwise.
370  *
371  * Primary purpose of the functin is to analize the config EEPROM,
372  * determine if it matches with the pattern of that of supported card,
373  * and extract necessary information from it.  One of the information
374  * expected to be extracted from EEPROM is the Ethernet station (MAC)
375  * address, which must be set to the softc table of the interface by
376  * the board-specific routine.
377  */
378 
379 /* JLI sub-probe for Allied-Telesyn/Allied-Telesis AT1700/RE2000 series.  */
380 static u_short const *
fe_probe_jli_ati(struct fe_softc * sc,u_char const * eeprom)381 fe_probe_jli_ati(struct fe_softc * sc, u_char const * eeprom)
382 {
383 	int i;
384 	static u_short const irqmaps_ati [4][4] =
385 	{
386 		{  3,  4,  5,  9 },
387 		{ 10, 11, 12, 15 },
388 		{  3, 11,  5, 15 },
389 		{ 10, 11, 14, 15 },
390 	};
391 
392 	/* Make sure the EEPROM contains Allied-Telesis/Allied-Telesyn
393 	   bit pattern.  */
394 	if (eeprom[1] != 0x00) return NULL;
395 	for (i =  2; i <  8; i++) if (eeprom[i] != 0xFF) return NULL;
396 	for (i = 14; i < 24; i++) if (eeprom[i] != 0xFF) return NULL;
397 
398 	/* Get our station address from EEPROM, and make sure the
399            EEPROM contains ATI's address.  */
400 	bcopy(eeprom + 8, sc->enaddr, ETHER_ADDR_LEN);
401 	if (!fe_valid_Ether_p(sc->enaddr, 0x0000F4))
402 		return NULL;
403 
404 	/*
405 	 * The following model identification codes are stolen
406 	 * from the NetBSD port of the fe driver.  My reviewers
407 	 * suggested minor revision.
408 	 */
409 
410 	/* Determine the card type.  */
411 	switch (eeprom[FE_ATI_EEP_MODEL]) {
412 	  case FE_ATI_MODEL_AT1700T:
413 		sc->typestr = "AT-1700T/RE2001";
414 		sc->mbitmap = MB_HT;
415 		sc->defmedia = MB_HT;
416 		break;
417 	  case FE_ATI_MODEL_AT1700BT:
418 		sc->typestr = "AT-1700BT/RE2003";
419 		sc->mbitmap = MB_HA | MB_HT | MB_H2;
420 		break;
421 	  case FE_ATI_MODEL_AT1700FT:
422 		sc->typestr = "AT-1700FT/RE2009";
423 		sc->mbitmap = MB_HA | MB_HT | MB_HF;
424 		break;
425 	  case FE_ATI_MODEL_AT1700AT:
426 		sc->typestr = "AT-1700AT/RE2005";
427 		sc->mbitmap = MB_HA | MB_HT | MB_H5;
428 		break;
429 	  default:
430 		sc->typestr = "unknown AT-1700/RE2000";
431 		sc->stability |= UNSTABLE_TYPE | UNSTABLE_IRQ;
432 		break;
433 	}
434 	sc->type = FE_TYPE_JLI;
435 
436 #if 0
437 	/* Should we extract default media from eeprom?  Linux driver
438 	   for AT1700 does it, although previous releases of FreeBSD
439 	   don't.  FIXME.  */
440 	/* Determine the default media selection from the config
441            EEPROM.  The byte at offset EEP_MEDIA is believed to
442            contain BMPR13 value to be set.  We just ignore STP bit or
443            squelch bit, since we don't support those.  (It is
444            intentional.)  */
445 	switch (eeprom[FE_ATI_EEP_MEDIA] & FE_B13_PORT) {
446 	    case FE_B13_AUTO:
447 		sc->defmedia = MB_HA;
448 		break;
449 	    case FE_B13_TP:
450 		sc->defmedia = MB_HT;
451 		break;
452 	    case FE_B13_AUI:
453 		sc->defmedia = sc->mbitmap & (MB_H2|MB_H5|MB_H5); /*XXX*/
454 		break;
455 	    default:
456 		sc->defmedia = MB_HA;
457 		break;
458 	}
459 
460 	/* Make sure the default media is compatible with the supported
461 	   ones.  */
462 	if ((sc->defmedia & sc->mbitmap) == 0) {
463 		if (sc->defmedia == MB_HA) {
464 			sc->defmedia = MB_HT;
465 		} else {
466 			sc->defmedia = MB_HA;
467 		}
468 	}
469 #endif
470 
471 	/*
472 	 * Try to determine IRQ settings.
473 	 * Different models use different ranges of IRQs.
474 	 */
475 	switch ((eeprom[FE_ATI_EEP_REVISION] & 0xf0)
476 	       |(eeprom[FE_ATI_EEP_MAGIC]    & 0x04)) {
477 	    case 0x30: case 0x34: return irqmaps_ati[3];
478 	    case 0x10: case 0x14:
479 	    case 0x50: case 0x54: return irqmaps_ati[2];
480 	    case 0x44: case 0x64: return irqmaps_ati[1];
481 	    default:		  return irqmaps_ati[0];
482 	}
483 }
484 
485 /* JLI sub-probe and msel hook for ICL Ethernet.  */
486 static void
fe_msel_icl(struct fe_softc * sc)487 fe_msel_icl(struct fe_softc *sc)
488 {
489 	u_char d4;
490 
491 	/* Switch between UTP and "external tranceiver" as always.  */
492 	fe_msel_965(sc);
493 
494 	/* The board needs one more bit (on DLCR4) be set appropriately.  */
495 	if (IFM_SUBTYPE(sc->media.ifm_media) == IFM_10_5) {
496 		d4 = sc->proto_dlcr4 | FE_D4_CNTRL;
497 	} else {
498 		d4 = sc->proto_dlcr4 & ~FE_D4_CNTRL;
499 	}
500 	fe_outb(sc, FE_DLCR4, d4);
501 }
502 
503 static u_short const *
fe_probe_jli_icl(struct fe_softc * sc,u_char const * eeprom)504 fe_probe_jli_icl(struct fe_softc * sc, u_char const * eeprom)
505 {
506 	int i;
507 	u_short defmedia;
508 	u_char d6;
509 	static u_short const irqmap_icl [4] = { 9, 10, 5, 15 };
510 
511 	/* Make sure the EEPROM contains ICL bit pattern.  */
512 	for (i = 24; i < 39; i++) {
513 	    if (eeprom[i] != 0x20 && (eeprom[i] & 0xF0) != 0x30) return NULL;
514 	}
515 	for (i = 112; i < 122; i++) {
516 	    if (eeprom[i] != 0x20 && (eeprom[i] & 0xF0) != 0x30) return NULL;
517 	}
518 
519 	/* Make sure the EEPROM contains ICL's permanent station
520            address.  If it isn't, probably this board is not an
521            ICL's.  */
522 	if (!fe_valid_Ether_p(eeprom+122, 0x00004B))
523 		return NULL;
524 
525 	/* Check if the "configured" Ethernet address in the EEPROM is
526 	   valid.  Use it if it is, or use the "permanent" address instead.  */
527 	if (fe_valid_Ether_p(eeprom+4, 0x020000)) {
528 		/* The configured address is valid.  Use it.  */
529 		bcopy(eeprom+4, sc->enaddr, ETHER_ADDR_LEN);
530 	} else {
531 		/* The configured address is invalid.  Use permanent.  */
532 		bcopy(eeprom+122, sc->enaddr, ETHER_ADDR_LEN);
533 	}
534 
535 	/* Determine model and supported media.  */
536 	switch (eeprom[0x5E]) {
537 	    case 0:
538 	        sc->typestr = "EtherTeam16i/COMBO";
539 	        sc->mbitmap = MB_HA | MB_HT | MB_H5 | MB_H2;
540 		break;
541 	    case 1:
542 		sc->typestr = "EtherTeam16i/TP";
543 	        sc->mbitmap = MB_HT;
544 		break;
545 	    case 2:
546 		sc->typestr = "EtherTeam16i/ErgoPro";
547 		sc->mbitmap = MB_HA | MB_HT | MB_H5;
548 		break;
549 	    case 4:
550 		sc->typestr = "EtherTeam16i/DUO";
551 		sc->mbitmap = MB_HA | MB_HT | MB_H2;
552 		break;
553 	    default:
554 		sc->typestr = "EtherTeam16i";
555 		sc->stability |= UNSTABLE_TYPE;
556 		if (bootverbose) {
557 		    printf("fe%d: unknown model code %02x for EtherTeam16i\n",
558 			   sc->sc_unit, eeprom[0x5E]);
559 		}
560 		break;
561 	}
562 	sc->type = FE_TYPE_JLI;
563 
564 	/* I'm not sure the following msel hook is required by all
565            models or COMBO only...  FIXME.  */
566 	sc->msel = fe_msel_icl;
567 
568 	/* Make the configured media selection the default media.  */
569 	switch (eeprom[0x28]) {
570 	    case 0: defmedia = MB_HA; break;
571 	    case 1: defmedia = MB_H5; break;
572 	    case 2: defmedia = MB_HT; break;
573 	    case 3: defmedia = MB_H2; break;
574 	    default:
575 		if (bootverbose) {
576 			printf("fe%d: unknown default media: %02x\n",
577 			       sc->sc_unit, eeprom[0x28]);
578 		}
579 		defmedia = MB_HA;
580 		break;
581 	}
582 
583 	/* Make sure the default media is compatible with the
584 	   supported media.  */
585 	if ((defmedia & sc->mbitmap) == 0) {
586 		if (bootverbose) {
587 			printf("fe%d: default media adjusted\n", sc->sc_unit);
588 		}
589 		defmedia = sc->mbitmap;
590 	}
591 
592 	/* Keep the determined default media.  */
593 	sc->defmedia = defmedia;
594 
595 	/* ICL has "fat" models.  We have to program 86965 to properly
596 	   reflect the hardware.  */
597 	d6 = sc->proto_dlcr6 & ~(FE_D6_BUFSIZ | FE_D6_BBW);
598 	switch ((eeprom[0x61] << 8) | eeprom[0x60]) {
599 	    case 0x2008: d6 |= FE_D6_BUFSIZ_32KB | FE_D6_BBW_BYTE; break;
600 	    case 0x4010: d6 |= FE_D6_BUFSIZ_64KB | FE_D6_BBW_WORD; break;
601 	    default:
602 		/* We can't support it, since we don't know which bits
603 		   to set in DLCR6.  */
604 		printf("fe%d: unknown SRAM config for ICL\n", sc->sc_unit);
605 		return NULL;
606 	}
607 	sc->proto_dlcr6 = d6;
608 
609 	/* Returns the IRQ table for the ICL board.  */
610 	return irqmap_icl;
611 }
612 
613 /* JLI sub-probe for RATOC REX-5586/5587.  */
614 static u_short const *
fe_probe_jli_rex(struct fe_softc * sc,u_char const * eeprom)615 fe_probe_jli_rex(struct fe_softc * sc, u_char const * eeprom)
616 {
617 	int i;
618 	static u_short const irqmap_rex [4] = { 3, 4, 5, NO_IRQ };
619 
620 	/* Make sure the EEPROM contains RATOC's config pattern.  */
621 	if (eeprom[1] != eeprom[0]) return NULL;
622 	for (i = 8; i < 32; i++) if (eeprom[i] != 0xFF) return NULL;
623 
624 	/* Get our station address from EEPROM.  Note that RATOC
625 	   stores it "byte-swapped" in each word.  (I don't know why.)
626 	   So, we just can't use bcopy().*/
627 	sc->enaddr[0] = eeprom[3];
628 	sc->enaddr[1] = eeprom[2];
629 	sc->enaddr[2] = eeprom[5];
630 	sc->enaddr[3] = eeprom[4];
631 	sc->enaddr[4] = eeprom[7];
632 	sc->enaddr[5] = eeprom[6];
633 
634 	/* Make sure the EEPROM contains RATOC's station address.  */
635 	if (!fe_valid_Ether_p(sc->enaddr, 0x00C0D0))
636 		return NULL;
637 
638 	/* I don't know any sub-model identification.  */
639 	sc->type = FE_TYPE_JLI;
640 	sc->typestr = "REX-5586/5587";
641 
642 	/* Returns the IRQ for the RATOC board.  */
643 	return irqmap_rex;
644 }
645 
646 /* JLI sub-probe for Unknown board.  */
647 static u_short const *
fe_probe_jli_unk(struct fe_softc * sc,u_char const * eeprom)648 fe_probe_jli_unk(struct fe_softc * sc, u_char const * eeprom)
649 {
650 	int i, n, romsize;
651 	static u_short const irqmap [4] = { NO_IRQ, NO_IRQ, NO_IRQ, NO_IRQ };
652 
653 	/* The generic JLI probe considered this board has an 86965
654 	   in JLI mode, but any other board-specific routines could
655 	   not find the matching implementation.  So, we "guess" the
656 	   location by looking for a bit pattern which looks like a
657 	   MAC address.  */
658 
659 	/* Determine how large the EEPROM is.  */
660 	for (romsize = JLI_EEPROM_SIZE/2; romsize > 16; romsize >>= 1) {
661 		for (i = 0; i < romsize; i++) {
662 			if (eeprom[i] != eeprom[i+romsize])
663 				break;
664 		}
665 		if (i < romsize)
666 			break;
667 	}
668 	romsize <<= 1;
669 
670 	/* Look for a bit pattern which looks like a MAC address.  */
671 	for (n = 2; n <= romsize - ETHER_ADDR_LEN; n += 2) {
672 		if (!fe_valid_Ether_p(eeprom + n, 0x000000))
673 			continue;
674 	}
675 
676 	/* If no reasonable address was found, we can't go further.  */
677 	if (n > romsize - ETHER_ADDR_LEN)
678 		return NULL;
679 
680 	/* Extract our (guessed) station address.  */
681 	bcopy(eeprom+n, sc->enaddr, ETHER_ADDR_LEN);
682 
683 	/* We are not sure what type of board it is... */
684 	sc->type = FE_TYPE_JLI;
685 	sc->typestr = "(unknown JLI)";
686 	sc->stability |= UNSTABLE_TYPE | UNSTABLE_MAC;
687 
688 	/* Returns the totally unknown IRQ mapping table.  */
689 	return irqmap;
690 }
691 
692 /*
693  * Probe and initialization for all JLI implementations.
694  */
695 
696 static int
fe_probe_jli(device_t dev)697 fe_probe_jli(device_t dev)
698 {
699 	struct fe_softc *sc = device_get_softc(dev);
700 	int i, n, error, xirq;
701 	u_long iobase, irq;
702 	u_char eeprom [JLI_EEPROM_SIZE];
703 	u_short const * irqmap;
704 
705 	static u_short const baseaddr [8] =
706 		{ 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300 };
707 	static struct fe_simple_probe_struct const probe_table [] = {
708 		{ FE_DLCR1,  0x20, 0x00 },
709 		{ FE_DLCR2,  0x50, 0x00 },
710 		{ FE_DLCR4,  0x08, 0x00 },
711 		{ FE_DLCR5,  0x80, 0x00 },
712 #if 0
713 		{ FE_BMPR16, 0x1B, 0x00 },
714 		{ FE_BMPR17, 0x7F, 0x00 },
715 #endif
716 		{ 0 }
717 	};
718 
719 	/*
720 	 * See if the specified address is possible for MB86965A JLI mode.
721 	 */
722 	if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
723 		return ENXIO;
724 	for (i = 0; i < 8; i++) {
725 		if (baseaddr[i] == iobase)
726 			break;
727 	}
728 	if (i == 8)
729 		return ENXIO;
730 
731 	/* 86965 JLI occupies 32 I/O addresses. */
732 	if (fe_alloc_port(dev, 32))
733 		return ENXIO;
734 
735 	/* Fill the softc struct with reasonable default.  */
736 	fe_softc_defaults(sc);
737 
738 	/*
739 	 * We should test if MB86965A is on the base address now.
740 	 * Unfortunately, it is very hard to probe it reliably, since
741 	 * we have no way to reset the chip under software control.
742 	 * On cold boot, we could check the "signature" bit patterns
743 	 * described in the Fujitsu document.  On warm boot, however,
744 	 * we can predict almost nothing about register values.
745 	 */
746 	if (!fe_simple_probe(sc, probe_table))
747 		return ENXIO;
748 
749 	/* Check if our I/O address matches config info on 86965.  */
750 	n = (fe_inb(sc, FE_BMPR19) & FE_B19_ADDR) >> FE_B19_ADDR_SHIFT;
751 	if (baseaddr[n] != iobase)
752 		return ENXIO;
753 
754 	/*
755 	 * We are now almost sure we have an MB86965 at the given
756 	 * address.  So, read EEPROM through it.  We have to write
757 	 * into LSI registers to read from EEPROM.  I want to avoid it
758 	 * at this stage, but I cannot test the presence of the chip
759 	 * any further without reading EEPROM.  FIXME.
760 	 */
761 	fe_read_eeprom_jli(sc, eeprom);
762 
763 	/* Make sure that config info in EEPROM and 86965 agree.  */
764 	if (eeprom[FE_EEPROM_CONF] != fe_inb(sc, FE_BMPR19))
765 		return ENXIO;
766 
767 	/* Use 86965 media selection scheme, unless othewise
768            specified.  It is "AUTO always" and "select with BMPR13."
769            This behaviour covers most of the 86965 based board (as
770            minimum requirements.)  It is backward compatible with
771            previous versions, also.  */
772 	sc->mbitmap = MB_HA;
773 	sc->defmedia = MB_HA;
774 	sc->msel = fe_msel_965;
775 
776 	/* Perform board-specific probe, one by one.  Note that the
777            order of probe is important and should not be changed
778            arbitrarily.  */
779 	if ((irqmap = fe_probe_jli_ati(sc, eeprom)) == NULL
780 	 && (irqmap = fe_probe_jli_rex(sc, eeprom)) == NULL
781 	 && (irqmap = fe_probe_jli_icl(sc, eeprom)) == NULL
782 	 && (irqmap = fe_probe_jli_unk(sc, eeprom)) == NULL)
783 		return ENXIO;
784 
785 	/* Find the IRQ read from EEPROM.  */
786 	n = (fe_inb(sc, FE_BMPR19) & FE_B19_IRQ) >> FE_B19_IRQ_SHIFT;
787 	xirq = irqmap[n];
788 
789 	/* Try to determine IRQ setting.  */
790 	error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL);
791 	if (error && xirq == NO_IRQ) {
792 		/* The device must be configured with an explicit IRQ.  */
793 		device_printf(dev, "IRQ auto-detection does not work\n");
794 		return ENXIO;
795 	} else if (error && xirq != NO_IRQ) {
796 		/* Just use the probed IRQ value.  */
797 		bus_set_resource(dev, SYS_RES_IRQ, 0, xirq, 1);
798 	} else if (!error && xirq == NO_IRQ) {
799 		/* No problem.  Go ahead.  */
800 	} else if (irq == xirq) {
801 		/* Good.  Go ahead.  */
802 	} else {
803 		/* User must be warned in this case.  */
804 		sc->stability |= UNSTABLE_IRQ;
805 	}
806 
807 	/* Setup a hook, which resets te 86965 when the driver is being
808            initialized.  This may solve a nasty bug.  FIXME.  */
809 	sc->init = fe_init_jli;
810 
811 	return 0;
812 }
813 
814 /* Probe for TDK LAK-AX031, which is an SSi 78Q8377A based board.  */
815 static int
fe_probe_ssi(device_t dev)816 fe_probe_ssi(device_t dev)
817 {
818 	struct fe_softc *sc = device_get_softc(dev);
819 	u_long iobase, irq;
820 
821 	u_char eeprom [SSI_EEPROM_SIZE];
822 	static struct fe_simple_probe_struct probe_table [] = {
823 		{ FE_DLCR2, 0x08, 0x00 },
824 		{ FE_DLCR4, 0x08, 0x00 },
825 		{ 0 }
826 	};
827 
828 	/* See if the specified I/O address is possible for 78Q8377A.  */
829 	if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
830 		return ENXIO;
831 	if ((iobase & ~0x3F0) != 0x000)
832                 return ENXIO;
833 
834 	/* We have 16 registers.  */
835 	if (fe_alloc_port(dev, 16))
836 		return ENXIO;
837 
838 	/* Fill the softc struct with default values.  */
839 	fe_softc_defaults(sc);
840 
841 	/* See if the card is on its address.  */
842 	if (!fe_simple_probe(sc, probe_table))
843 		return ENXIO;
844 
845 	/* We now have to read the config EEPROM.  We should be very
846            careful, since doing so destroys a register.  (Remember, we
847            are not yet sure we have a LAK-AX031 board here.)  Don't
848            remember to select BMPRs bofore reading EEPROM, since other
849            register bank may be selected before the probe() is called.  */
850 	fe_read_eeprom_ssi(sc, eeprom);
851 
852 	/* Make sure the Ethernet (MAC) station address is of TDK's.  */
853 	if (!fe_valid_Ether_p(eeprom+FE_SSI_EEP_ADDR, 0x008098))
854 		return ENXIO;
855 	bcopy(eeprom + FE_SSI_EEP_ADDR, sc->enaddr, ETHER_ADDR_LEN);
856 
857 	/* This looks like a TDK-AX031 board.  It requires an explicit
858 	   IRQ setting in config, since we currently don't know how we
859 	   can find the IRQ value assigned by ISA PnP manager.  */
860 	if (bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL) != 0) {
861 		fe_irq_failure("LAK-AX031", sc->sc_unit, NO_IRQ, NULL);
862 		return ENXIO;
863 	}
864 
865 	/* Fill softc struct accordingly.  */
866 	sc->type = FE_TYPE_SSI;
867 	sc->typestr = "LAK-AX031";
868 	sc->mbitmap = MB_HT;
869 	sc->defmedia = MB_HT;
870 
871 	return 0;
872 }
873 
874 /*
875  * Probe and initialization for TDK/LANX LAC-AX012/013 boards.
876  */
877 static int
fe_probe_lnx(device_t dev)878 fe_probe_lnx(device_t dev)
879 {
880 	struct fe_softc *sc = device_get_softc(dev);
881 	u_long iobase, irq;
882 
883 	u_char eeprom [LNX_EEPROM_SIZE];
884 	static struct fe_simple_probe_struct probe_table [] = {
885 		{ FE_DLCR2, 0x58, 0x00 },
886 		{ FE_DLCR4, 0x08, 0x00 },
887 		{ 0 }
888 	};
889 
890 	/* See if the specified I/O address is possible for TDK/LANX boards. */
891 	/* 300, 320, 340, and 360 are allowed.  */
892 	if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
893 		return ENXIO;
894 	if ((iobase & ~0x060) != 0x300)
895                 return ENXIO;
896 
897 	/* We have 32 registers.  */
898 	if (fe_alloc_port(dev, 32))
899 		return ENXIO;
900 
901 	/* Fill the softc struct with default values.  */
902 	fe_softc_defaults(sc);
903 
904 	/* See if the card is on its address.  */
905 	if (!fe_simple_probe(sc, probe_table))
906 		return ENXIO;
907 
908 	/* We now have to read the config EEPROM.  We should be very
909            careful, since doing so destroys a register.  (Remember, we
910            are not yet sure we have a LAC-AX012/AX013 board here.)  */
911 	fe_read_eeprom_lnx(sc, eeprom);
912 
913 	/* Make sure the Ethernet (MAC) station address is of TDK/LANX's.  */
914 	if (!fe_valid_Ether_p(eeprom, 0x008098))
915 		return ENXIO;
916 	bcopy(eeprom, sc->enaddr, ETHER_ADDR_LEN);
917 
918 	/* This looks like a TDK/LANX board.  It requires an
919 	   explicit IRQ setting in config.  Make sure we have one,
920 	   determining an appropriate value for the IRQ control
921 	   register.  */
922 	irq = 0;
923 	bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL);
924 	switch (irq) {
925 	case 3: sc->priv_info = 0x40 | LNX_CLK_LO | LNX_SDA_HI; break;
926 	case 4: sc->priv_info = 0x20 | LNX_CLK_LO | LNX_SDA_HI; break;
927 	case 5: sc->priv_info = 0x10 | LNX_CLK_LO | LNX_SDA_HI; break;
928 	case 9: sc->priv_info = 0x80 | LNX_CLK_LO | LNX_SDA_HI; break;
929 	default:
930 		fe_irq_failure("LAC-AX012/AX013", sc->sc_unit, irq, "3/4/5/9");
931 		return ENXIO;
932 	}
933 
934 	/* Fill softc struct accordingly.  */
935 	sc->type = FE_TYPE_LNX;
936 	sc->typestr = "LAC-AX012/AX013";
937 	sc->init = fe_init_lnx;
938 
939 	return 0;
940 }
941 
942 /*
943  * Probe and initialization for Gateway Communications' old cards.
944  */
945 static int
fe_probe_gwy(device_t dev)946 fe_probe_gwy(device_t dev)
947 {
948 	struct fe_softc *sc = device_get_softc(dev);
949 	u_long iobase, irq;
950 
951 	static struct fe_simple_probe_struct probe_table [] = {
952 	    /*	{ FE_DLCR2, 0x70, 0x00 }, */
953 		{ FE_DLCR2, 0x58, 0x00 },
954 		{ FE_DLCR4, 0x08, 0x00 },
955 		{ 0 }
956 	};
957 
958 	/* See if the specified I/O address is possible for Gateway boards.  */
959 	if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
960 		return ENXIO;
961 	if ((iobase & ~0x1E0) != 0x200)
962 		return ENXIO;
963 
964 	/* That's all.  The card occupies 32 I/O addresses, as always.  */
965 	if (fe_alloc_port(dev, 32))
966 		return ENXIO;
967 
968 	/* Setup an I/O address mapping table and some others.  */
969 	fe_softc_defaults(sc);
970 
971 	/* See if the card is on its address.  */
972 	if (!fe_simple_probe(sc, probe_table))
973 		return ENXIO;
974 
975 	/* Get our station address from EEPROM. */
976 	fe_inblk(sc, 0x18, sc->enaddr, ETHER_ADDR_LEN);
977 
978 	/* Make sure it is Gateway Communication's.  */
979 	if (!fe_valid_Ether_p(sc->enaddr, 0x000061))
980 		return ENXIO;
981 
982 	/* Gateway's board requires an explicit IRQ to work, since it
983 	   is not possible to probe the setting of jumpers.  */
984 	if (bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL) != 0) {
985 		fe_irq_failure("Gateway Ethernet", sc->sc_unit, NO_IRQ, NULL);
986 		return ENXIO;
987 	}
988 
989 	/* Fill softc struct accordingly.  */
990 	sc->type = FE_TYPE_GWY;
991 	sc->typestr = "Gateway Ethernet (Fujitsu chipset)";
992 
993 	return 0;
994 }
995 
996 /* Probe and initialization for Ungermann-Bass Network
997    K.K. "Access/PC" boards.  */
998 static int
fe_probe_ubn(device_t dev)999 fe_probe_ubn(device_t dev)
1000 {
1001 	struct fe_softc *sc = device_get_softc(dev);
1002 	u_long iobase, irq;
1003 #if 0
1004 	u_char sum;
1005 #endif
1006 	static struct fe_simple_probe_struct const probe_table [] = {
1007 		{ FE_DLCR2, 0x58, 0x00 },
1008 		{ FE_DLCR4, 0x08, 0x00 },
1009 		{ 0 }
1010 	};
1011 
1012 	/* See if the specified I/O address is possible for AccessPC/ISA.  */
1013 	if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
1014 		return ENXIO;
1015 	if ((iobase & ~0x0E0) != 0x300)
1016 		return ENXIO;
1017 
1018 	/* We have 32 registers.  */
1019 	if (fe_alloc_port(dev, 32))
1020 		return ENXIO;
1021 
1022 	/* Setup an I/O address mapping table and some others.  */
1023 	fe_softc_defaults(sc);
1024 
1025 	/* Simple probe.  */
1026 	if (!fe_simple_probe(sc, probe_table))
1027 		return ENXIO;
1028 
1029 	/* Get our station address form ID ROM and make sure it is UBN's.  */
1030 	fe_inblk(sc, 0x18, sc->enaddr, ETHER_ADDR_LEN);
1031 	if (!fe_valid_Ether_p(sc->enaddr, 0x00DD01))
1032 		return ENXIO;
1033 #if 0
1034 	/* Calculate checksum.  */
1035 	sum = fe_inb(sc, 0x1e);
1036 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1037 		sum ^= sc->enaddr[i];
1038 	}
1039 	if (sum != 0)
1040 		return ENXIO;
1041 #endif
1042 	/* This looks like an AccessPC/ISA board.  It requires an
1043 	   explicit IRQ setting in config.  Make sure we have one,
1044 	   determining an appropriate value for the IRQ control
1045 	   register.  */
1046 	irq = 0;
1047 	bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL);
1048 	switch (irq) {
1049 	case 3:  sc->priv_info = 0x02; break;
1050 	case 4:  sc->priv_info = 0x04; break;
1051 	case 5:  sc->priv_info = 0x08; break;
1052 	case 10: sc->priv_info = 0x10; break;
1053 	default:
1054 		fe_irq_failure("Access/PC", sc->sc_unit, irq, "3/4/5/10");
1055 		return ENXIO;
1056 	}
1057 
1058 	/* Fill softc struct accordingly.  */
1059 	sc->type = FE_TYPE_UBN;
1060 	sc->typestr = "Access/PC";
1061 	sc->init = fe_init_ubn;
1062 
1063 	return 0;
1064 }
1065