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