e2c97745540762c85cddab4ccf5a5475a414c5af
[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  */
24
25 #include "opt_fe.h"
26 #include "opt_inet.h"
27 #include "opt_ipx.h"
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/machintr.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, NULL, NULL);
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                 int intr = irqmap[n];
308
309                 /* Just use the probed value.  */
310                 bus_set_resource(dev, SYS_RES_IRQ, 0, intr, 1,
311                     machintr_legacy_intr_cpuid(intr));
312         } else if (irq != irqmap[n]) {
313                 /* Don't match.  */
314                 sc->stability |= UNSTABLE_IRQ;
315         }
316
317         /* We need an init hook to initialize ASIC before we start.  */
318         sc->init = fe_init_fmv;
319
320         return 0;
321 }
322
323 /*
324  * Fujitsu MB86965 JLI mode probe routines.
325  *
326  * 86965 has a special operating mode called JLI (mode 0), under which
327  * the chip interfaces with ISA bus with a software-programmable
328  * configuration.  (The Fujitsu document calls the feature "Plug and
329  * play," but it is not compatible with the ISA-PnP spec. designed by
330  * Intel and Microsoft.)  Ethernet cards designed to use JLI are
331  * almost same, but there are two things which require board-specific
332  * probe routines: EEPROM layout and IRQ pin connection.
333  *
334  * JLI provides a handy way to access EEPROM which should contains the
335  * chip configuration information (such as I/O port address) as well
336  * as Ethernet station (MAC) address.  The chip configuration info. is
337  * stored on a fixed location.  However, the station address can be
338  * located anywhere in the EEPROM; it is up to the board designer to
339  * determine the location.  (The manual just says "somewhere in the
340  * EEPROM.")  The fe driver must somehow find out the correct
341  * location.
342  *
343  * Another problem resides in the IRQ pin connection.  JLI provides a
344  * user to choose an IRQ from up to four predefined IRQs.  The 86965
345  * chip has a register to select one out of the four possibilities.
346  * However, the selection is against the four IRQ pins on the chip.
347  * (So-called IRQ-A, -B, -C and -D.)  It is (again) up to the board
348  * designer to determine which pin to connect which IRQ line on the
349  * ISA bus.  We need a vendor (or model, for some vendor) specific IRQ
350  * mapping table.
351  * 
352  * The routine fe_probe_jli() provides all probe and initialization
353  * processes which are common to all JLI implementation, and sub-probe
354  * routines supply board-specific actions.
355  *
356  * JLI sub-probe routine has the following template:
357  *
358  *      u_short const * func (struct fe_softc * sc, u_char const * eeprom);
359  *
360  * where eeprom is a pointer to an array of 32 byte data read from the
361  * config EEPROM on the board.  It retuns an IRQ mapping table for the
362  * board, when the corresponding implementation is detected.  It
363  * returns a NULL otherwise.
364  * 
365  * Primary purpose of the functin is to analize the config EEPROM,
366  * determine if it matches with the pattern of that of supported card,
367  * and extract necessary information from it.  One of the information
368  * expected to be extracted from EEPROM is the Ethernet station (MAC)
369  * address, which must be set to the softc table of the interface by
370  * the board-specific routine.
371  */
372
373 /* JLI sub-probe for Allied-Telesyn/Allied-Telesis AT1700/RE2000 series.  */
374 static u_short const *
375 fe_probe_jli_ati(struct fe_softc * sc, u_char const * eeprom)
376 {
377         int i;
378         static u_short const irqmaps_ati [4][4] =
379         {
380                 {  3,  4,  5,  9 },
381                 { 10, 11, 12, 15 },
382                 {  3, 11,  5, 15 },
383                 { 10, 11, 14, 15 },
384         };
385
386         /* Make sure the EEPROM contains Allied-Telesis/Allied-Telesyn
387            bit pattern.  */
388         if (eeprom[1] != 0x00) return NULL;
389         for (i =  2; i <  8; i++) if (eeprom[i] != 0xFF) return NULL;
390         for (i = 14; i < 24; i++) if (eeprom[i] != 0xFF) return NULL;
391
392         /* Get our station address from EEPROM, and make sure the
393            EEPROM contains ATI's address.  */
394         bcopy(eeprom + 8, sc->sc_enaddr, ETHER_ADDR_LEN);
395         if (!valid_Ether_p(sc->sc_enaddr, 0x0000F4))
396                 return NULL;
397
398         /*
399          * The following model identification codes are stolen
400          * from the NetBSD port of the fe driver.  My reviewers
401          * suggested minor revision.
402          */
403
404         /* Determine the card type.  */
405         switch (eeprom[FE_ATI_EEP_MODEL]) {
406           case FE_ATI_MODEL_AT1700T:
407                 sc->typestr = "AT-1700T/RE2001";
408                 sc->mbitmap = MB_HT;
409                 sc->defmedia = MB_HT;
410                 break;
411           case FE_ATI_MODEL_AT1700BT:
412                 sc->typestr = "AT-1700BT/RE2003";
413                 sc->mbitmap = MB_HA | MB_HT | MB_H2;
414                 break;
415           case FE_ATI_MODEL_AT1700FT:
416                 sc->typestr = "AT-1700FT/RE2009";
417                 sc->mbitmap = MB_HA | MB_HT | MB_HF;
418                 break;
419           case FE_ATI_MODEL_AT1700AT:
420                 sc->typestr = "AT-1700AT/RE2005";
421                 sc->mbitmap = MB_HA | MB_HT | MB_H5;
422                 break;
423           default:
424                 sc->typestr = "unknown AT-1700/RE2000";
425                 sc->stability |= UNSTABLE_TYPE | UNSTABLE_IRQ;
426                 break;
427         }
428         sc->type = FE_TYPE_JLI;
429
430 #if 0
431         /* Should we extract default media from eeprom?  Linux driver
432            for AT1700 does it, although previous releases of FreeBSD
433            don't.  FIXME.  */
434         /* Determine the default media selection from the config
435            EEPROM.  The byte at offset EEP_MEDIA is believed to
436            contain BMPR13 value to be set.  We just ignore STP bit or
437            squelch bit, since we don't support those.  (It is
438            intentional.)  */
439         switch (eeprom[FE_ATI_EEP_MEDIA] & FE_B13_PORT) {
440             case FE_B13_AUTO:
441                 sc->defmedia = MB_HA;
442                 break;
443             case FE_B13_TP:
444                 sc->defmedia = MB_HT;
445                 break;
446             case FE_B13_AUI:
447                 sc->defmedia = sc->mbitmap & (MB_H2|MB_H5|MB_H5); /*XXX*/
448                 break;
449             default:        
450                 sc->defmedia = MB_HA;
451                 break;
452         }
453
454         /* Make sure the default media is compatible with the supported
455            ones.  */
456         if ((sc->defmedia & sc->mbitmap) == 0) {
457                 if (sc->defmedia == MB_HA) {
458                         sc->defmedia = MB_HT;
459                 } else {
460                         sc->defmedia = MB_HA;
461                 }
462         }
463 #endif  
464
465         /*
466          * Try to determine IRQ settings.
467          * Different models use different ranges of IRQs.
468          */
469         switch ((eeprom[FE_ATI_EEP_REVISION] & 0xf0)
470                |(eeprom[FE_ATI_EEP_MAGIC]    & 0x04)) {
471             case 0x30: case 0x34: return irqmaps_ati[3];
472             case 0x10: case 0x14:
473             case 0x50: case 0x54: return irqmaps_ati[2];
474             case 0x44: case 0x64: return irqmaps_ati[1];
475             default:              return irqmaps_ati[0];
476         }
477 }
478
479 /* JLI sub-probe and msel hook for ICL Ethernet.  */
480 static void
481 fe_msel_icl(struct fe_softc *sc)
482 {
483         u_char d4;
484
485         /* Switch between UTP and "external tranceiver" as always.  */    
486         fe_msel_965(sc);
487
488         /* The board needs one more bit (on DLCR4) be set appropriately.  */
489         if (IFM_SUBTYPE(sc->media.ifm_media) == IFM_10_5) {
490                 d4 = sc->proto_dlcr4 | FE_D4_CNTRL;
491         } else {
492                 d4 = sc->proto_dlcr4 & ~FE_D4_CNTRL;
493         }
494         fe_outb(sc, FE_DLCR4, d4);
495 }
496
497 static u_short const *
498 fe_probe_jli_icl(struct fe_softc * sc, u_char const * eeprom)
499 {
500         int i;
501         u_short defmedia;
502         u_char d6;
503         static u_short const irqmap_icl [4] = { 9, 10, 5, 15 };
504
505         /* Make sure the EEPROM contains ICL bit pattern.  */
506         for (i = 24; i < 39; i++) {
507             if (eeprom[i] != 0x20 && (eeprom[i] & 0xF0) != 0x30) return NULL;
508         }
509         for (i = 112; i < 122; i++) {
510             if (eeprom[i] != 0x20 && (eeprom[i] & 0xF0) != 0x30) return NULL;
511         }
512
513         /* Make sure the EEPROM contains ICL's permanent station
514            address.  If it isn't, probably this board is not an
515            ICL's.  */
516         if (!valid_Ether_p(eeprom+122, 0x00004B))
517                 return NULL;
518
519         /* Check if the "configured" Ethernet address in the EEPROM is
520            valid.  Use it if it is, or use the "permanent" address instead.  */
521         if (valid_Ether_p(eeprom+4, 0x020000)) {
522                 /* The configured address is valid.  Use it.  */
523                 bcopy(eeprom+4, sc->sc_enaddr, ETHER_ADDR_LEN);
524         } else {
525                 /* The configured address is invalid.  Use permanent.  */
526                 bcopy(eeprom+122, sc->sc_enaddr, ETHER_ADDR_LEN);
527         }
528
529         /* Determine model and supported media.  */
530         switch (eeprom[0x5E]) {
531             case 0:
532                 sc->typestr = "EtherTeam16i/COMBO";
533                 sc->mbitmap = MB_HA | MB_HT | MB_H5 | MB_H2;
534                 break;
535             case 1:
536                 sc->typestr = "EtherTeam16i/TP";
537                 sc->mbitmap = MB_HT;
538                 break;
539             case 2:
540                 sc->typestr = "EtherTeam16i/ErgoPro";
541                 sc->mbitmap = MB_HA | MB_HT | MB_H5;
542                 break;
543             case 4:
544                 sc->typestr = "EtherTeam16i/DUO";
545                 sc->mbitmap = MB_HA | MB_HT | MB_H2;
546                 break;
547             default:
548                 sc->typestr = "EtherTeam16i";
549                 sc->stability |= UNSTABLE_TYPE;
550                 if (bootverbose) {
551                     kprintf("fe%d: unknown model code %02x for EtherTeam16i\n",
552                            sc->sc_unit, eeprom[0x5E]);
553                 }
554                 break;
555         }
556         sc->type = FE_TYPE_JLI;
557
558         /* I'm not sure the following msel hook is required by all
559            models or COMBO only...  FIXME.  */
560         sc->msel = fe_msel_icl;
561
562         /* Make the configured media selection the default media.  */
563         switch (eeprom[0x28]) {
564             case 0: defmedia = MB_HA; break;
565             case 1: defmedia = MB_H5; break;
566             case 2: defmedia = MB_HT; break;
567             case 3: defmedia = MB_H2; break;
568             default: 
569                 if (bootverbose) {
570                         kprintf("fe%d: unknown default media: %02x\n",
571                                sc->sc_unit, eeprom[0x28]);
572                 }
573                 defmedia = MB_HA;
574                 break;
575         }
576
577         /* Make sure the default media is compatible with the
578            supported media.  */
579         if ((defmedia & sc->mbitmap) == 0) {
580                 if (bootverbose) {
581                         kprintf("fe%d: default media adjusted\n", sc->sc_unit);
582                 }
583                 defmedia = sc->mbitmap;
584         }
585
586         /* Keep the determined default media.  */
587         sc->defmedia = defmedia;
588
589         /* ICL has "fat" models.  We have to program 86965 to properly
590            reflect the hardware.  */
591         d6 = sc->proto_dlcr6 & ~(FE_D6_BUFSIZ | FE_D6_BBW);
592         switch ((eeprom[0x61] << 8) | eeprom[0x60]) {
593             case 0x2008: d6 |= FE_D6_BUFSIZ_32KB | FE_D6_BBW_BYTE; break;
594             case 0x4010: d6 |= FE_D6_BUFSIZ_64KB | FE_D6_BBW_WORD; break;
595             default:
596                 /* We can't support it, since we don't know which bits
597                    to set in DLCR6.  */
598                 kprintf("fe%d: unknown SRAM config for ICL\n", sc->sc_unit);
599                 return NULL;
600         }
601         sc->proto_dlcr6 = d6;
602
603         /* Returns the IRQ table for the ICL board.  */
604         return irqmap_icl;
605 }
606
607 /* JLI sub-probe for RATOC REX-5586/5587.  */
608 static u_short const *
609 fe_probe_jli_rex(struct fe_softc * sc, u_char const * eeprom)
610 {
611         int i;
612         static u_short const irqmap_rex [4] = { 3, 4, 5, NO_IRQ };
613
614         /* Make sure the EEPROM contains RATOC's config pattern.  */
615         if (eeprom[1] != eeprom[0]) return NULL;
616         for (i = 8; i < 32; i++) if (eeprom[i] != 0xFF) return NULL;
617
618         /* Get our station address from EEPROM.  Note that RATOC
619            stores it "byte-swapped" in each word.  (I don't know why.)
620            So, we just can't use bcopy().*/
621         sc->sc_enaddr[0] = eeprom[3];
622         sc->sc_enaddr[1] = eeprom[2];
623         sc->sc_enaddr[2] = eeprom[5];
624         sc->sc_enaddr[3] = eeprom[4];
625         sc->sc_enaddr[4] = eeprom[7];
626         sc->sc_enaddr[5] = eeprom[6];
627
628         /* Make sure the EEPROM contains RATOC's station address.  */
629         if (!valid_Ether_p(sc->sc_enaddr, 0x00C0D0))
630                 return NULL;
631
632         /* I don't know any sub-model identification.  */
633         sc->type = FE_TYPE_JLI;
634         sc->typestr = "REX-5586/5587";
635
636         /* Returns the IRQ for the RATOC board.  */
637         return irqmap_rex;
638 }
639
640 /* JLI sub-probe for Unknown board.  */
641 static u_short const *
642 fe_probe_jli_unk(struct fe_softc * sc, u_char const * eeprom)
643 {
644         int i, n, romsize;
645         static u_short const irqmap [4] = { NO_IRQ, NO_IRQ, NO_IRQ, NO_IRQ };
646
647         /* The generic JLI probe considered this board has an 86965
648            in JLI mode, but any other board-specific routines could
649            not find the matching implementation.  So, we "guess" the
650            location by looking for a bit pattern which looks like a
651            MAC address.  */
652
653         /* Determine how large the EEPROM is.  */
654         for (romsize = JLI_EEPROM_SIZE/2; romsize > 16; romsize >>= 1) {
655                 for (i = 0; i < romsize; i++) {
656                         if (eeprom[i] != eeprom[i+romsize])
657                                 break;
658                 }
659                 if (i < romsize)
660                         break;
661         }
662         romsize <<= 1;
663
664         /* Look for a bit pattern which looks like a MAC address.  */
665         for (n = 2; n <= romsize - ETHER_ADDR_LEN; n += 2) {
666                 if (!valid_Ether_p(eeprom + n, 0x000000))
667                         continue;
668         }
669
670         /* If no reasonable address was found, we can't go further.  */
671         if (n > romsize - ETHER_ADDR_LEN)
672                 return NULL;
673
674         /* Extract our (guessed) station address.  */
675         bcopy(eeprom+n, sc->sc_enaddr, ETHER_ADDR_LEN);
676
677         /* We are not sure what type of board it is... */
678         sc->type = FE_TYPE_JLI;
679         sc->typestr = "(unknown JLI)";
680         sc->stability |= UNSTABLE_TYPE | UNSTABLE_MAC;
681
682         /* Returns the totally unknown IRQ mapping table.  */
683         return irqmap;
684 }
685
686 /*
687  * Probe and initialization for all JLI implementations.
688  */
689
690 static int
691 fe_probe_jli(device_t dev)
692 {
693         struct fe_softc *sc = device_get_softc(dev);
694         int i, n, error, xirq;
695         u_long iobase, irq;
696         u_char eeprom [JLI_EEPROM_SIZE];
697         u_short const * irqmap;
698
699         static u_short const baseaddr [8] =
700                 { 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300 };
701         static struct fe_simple_probe_struct const probe_table [] = {
702                 { FE_DLCR1,  0x20, 0x00 },
703                 { FE_DLCR2,  0x50, 0x00 },
704                 { FE_DLCR4,  0x08, 0x00 },
705                 { FE_DLCR5,  0x80, 0x00 },
706 #if 0
707                 { FE_BMPR16, 0x1B, 0x00 },
708                 { FE_BMPR17, 0x7F, 0x00 },
709 #endif
710                 { 0 }
711         };
712
713         /*
714          * See if the specified address is possible for MB86965A JLI mode.
715          */
716         if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
717                 return ENXIO;
718         for (i = 0; i < 8; i++) {
719                 if (baseaddr[i] == iobase)
720                         break;
721         }
722         if (i == 8)
723                 return ENXIO;
724
725         /* 86965 JLI occupies 32 I/O addresses. */
726         if (fe_alloc_port(dev, 32))
727                 return ENXIO;
728
729         /* Fill the softc struct with reasonable default.  */
730         fe_softc_defaults(sc);
731
732         /*
733          * We should test if MB86965A is on the base address now.
734          * Unfortunately, it is very hard to probe it reliably, since
735          * we have no way to reset the chip under software control.
736          * On cold boot, we could check the "signature" bit patterns
737          * described in the Fujitsu document.  On warm boot, however,
738          * we can predict almost nothing about register values.
739          */
740         if (!fe_simple_probe(sc, probe_table))
741                 return ENXIO;
742
743         /* Check if our I/O address matches config info on 86965.  */
744         n = (fe_inb(sc, FE_BMPR19) & FE_B19_ADDR) >> FE_B19_ADDR_SHIFT;
745         if (baseaddr[n] != iobase)
746                 return ENXIO;
747
748         /*
749          * We are now almost sure we have an MB86965 at the given
750          * address.  So, read EEPROM through it.  We have to write
751          * into LSI registers to read from EEPROM.  I want to avoid it
752          * at this stage, but I cannot test the presence of the chip
753          * any further without reading EEPROM.  FIXME.
754          */
755         fe_read_eeprom_jli(sc, eeprom);
756
757         /* Make sure that config info in EEPROM and 86965 agree.  */
758         if (eeprom[FE_EEPROM_CONF] != fe_inb(sc, FE_BMPR19))
759                 return ENXIO;
760
761         /* Use 86965 media selection scheme, unless othewise
762            specified.  It is "AUTO always" and "select with BMPR13."
763            This behaviour covers most of the 86965 based board (as
764            minimum requirements.)  It is backward compatible with
765            previous versions, also.  */
766         sc->mbitmap = MB_HA;
767         sc->defmedia = MB_HA;
768         sc->msel = fe_msel_965;
769
770         /* Perform board-specific probe, one by one.  Note that the
771            order of probe is important and should not be changed
772            arbitrarily.  */
773         if ((irqmap = fe_probe_jli_ati(sc, eeprom)) == NULL
774          && (irqmap = fe_probe_jli_rex(sc, eeprom)) == NULL
775          && (irqmap = fe_probe_jli_icl(sc, eeprom)) == NULL
776          && (irqmap = fe_probe_jli_unk(sc, eeprom)) == NULL)
777                 return ENXIO;
778
779         /* Find the IRQ read from EEPROM.  */
780         n = (fe_inb(sc, FE_BMPR19) & FE_B19_IRQ) >> FE_B19_IRQ_SHIFT;
781         xirq = irqmap[n];
782
783         /* Try to determine IRQ setting.  */
784         error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL);
785         if (error && xirq == NO_IRQ) {
786                 /* The device must be configured with an explicit IRQ.  */
787                 device_printf(dev, "IRQ auto-detection does not work\n");
788                 return ENXIO;
789         } else if (error && xirq != NO_IRQ) {
790                 /* Just use the probed IRQ value.  */
791                 bus_set_resource(dev, SYS_RES_IRQ, 0, xirq, 1,
792                     machintr_legacy_intr_cpuid(xirq));
793         } else if (!error && xirq == NO_IRQ) {
794                 /* No problem.  Go ahead.  */
795         } else if (irq == xirq) {
796                 /* Good.  Go ahead.  */
797         } else {
798                 /* User must be warned in this case.  */
799                 sc->stability |= UNSTABLE_IRQ;
800         }
801
802         /* Setup a hook, which resets te 86965 when the driver is being
803            initialized.  This may solve a nasty bug.  FIXME.  */
804         sc->init = fe_init_jli;
805
806         return 0;
807 }
808
809 /* Probe for TDK LAK-AX031, which is an SSi 78Q8377A based board.  */
810 static int
811 fe_probe_ssi(device_t dev)
812 {
813         struct fe_softc *sc = device_get_softc(dev);
814         u_long iobase, irq;
815
816         u_char eeprom [SSI_EEPROM_SIZE];
817         static struct fe_simple_probe_struct probe_table [] = {
818                 { FE_DLCR2, 0x08, 0x00 },
819                 { FE_DLCR4, 0x08, 0x00 },
820                 { 0 }
821         };
822
823         /* See if the specified I/O address is possible for 78Q8377A.  */
824         if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
825                 return ENXIO;
826         if ((iobase & ~0x3F0) != 0x000)
827                 return ENXIO;
828
829         /* We have 16 registers.  */
830         if (fe_alloc_port(dev, 16))
831                 return ENXIO;
832
833         /* Fill the softc struct with default values.  */
834         fe_softc_defaults(sc);
835
836         /* See if the card is on its address.  */
837         if (!fe_simple_probe(sc, probe_table))
838                 return ENXIO;
839
840         /* We now have to read the config EEPROM.  We should be very
841            careful, since doing so destroys a register.  (Remember, we
842            are not yet sure we have a LAK-AX031 board here.)  Don't
843            remember to select BMPRs bofore reading EEPROM, since other
844            register bank may be selected before the probe() is called.  */
845         fe_read_eeprom_ssi(sc, eeprom);
846
847         /* Make sure the Ethernet (MAC) station address is of TDK's.  */
848         if (!valid_Ether_p(eeprom+FE_SSI_EEP_ADDR, 0x008098))
849                 return ENXIO;
850         bcopy(eeprom + FE_SSI_EEP_ADDR, sc->sc_enaddr, ETHER_ADDR_LEN);
851
852         /* This looks like a TDK-AX031 board.  It requires an explicit
853            IRQ setting in config, since we currently don't know how we
854            can find the IRQ value assigned by ISA PnP manager.  */
855         if (bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL) != 0) {
856                 fe_irq_failure("LAK-AX031", sc->sc_unit, NO_IRQ, NULL);
857                 return ENXIO;
858         }
859
860         /* Fill softc struct accordingly.  */
861         sc->type = FE_TYPE_SSI;
862         sc->typestr = "LAK-AX031";
863         sc->mbitmap = MB_HT;
864         sc->defmedia = MB_HT;
865
866         return 0;
867 }
868
869 /*
870  * Probe and initialization for TDK/LANX LAC-AX012/013 boards.
871  */
872 static int
873 fe_probe_lnx(device_t dev)
874 {
875         struct fe_softc *sc = device_get_softc(dev);
876         u_long iobase, irq;
877
878         u_char eeprom [LNX_EEPROM_SIZE];
879         static struct fe_simple_probe_struct probe_table [] = {
880                 { FE_DLCR2, 0x58, 0x00 },
881                 { FE_DLCR4, 0x08, 0x00 },
882                 { 0 }
883         };
884
885         /* See if the specified I/O address is possible for TDK/LANX boards. */
886         /* 300, 320, 340, and 360 are allowed.  */
887         if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
888                 return ENXIO;
889         if ((iobase & ~0x060) != 0x300)
890                 return ENXIO;
891
892         /* We have 32 registers.  */
893         if (fe_alloc_port(dev, 32))
894                 return ENXIO;
895
896         /* Fill the softc struct with default values.  */
897         fe_softc_defaults(sc);
898
899         /* See if the card is on its address.  */
900         if (!fe_simple_probe(sc, probe_table))
901                 return ENXIO;
902
903         /* We now have to read the config EEPROM.  We should be very
904            careful, since doing so destroys a register.  (Remember, we
905            are not yet sure we have a LAC-AX012/AX013 board here.)  */
906         fe_read_eeprom_lnx(sc, eeprom);
907
908         /* Make sure the Ethernet (MAC) station address is of TDK/LANX's.  */
909         if (!valid_Ether_p(eeprom, 0x008098))
910                 return ENXIO;
911         bcopy(eeprom, sc->sc_enaddr, ETHER_ADDR_LEN);
912
913         /* This looks like a TDK/LANX board.  It requires an
914            explicit IRQ setting in config.  Make sure we have one,
915            determining an appropriate value for the IRQ control
916            register.  */
917         irq = 0;
918         bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL);
919         switch (irq) {
920         case 3: sc->priv_info = 0x40 | LNX_CLK_LO | LNX_SDA_HI; break;
921         case 4: sc->priv_info = 0x20 | LNX_CLK_LO | LNX_SDA_HI; break;
922         case 5: sc->priv_info = 0x10 | LNX_CLK_LO | LNX_SDA_HI; break;
923         case 9: sc->priv_info = 0x80 | LNX_CLK_LO | LNX_SDA_HI; break;
924         default:
925                 fe_irq_failure("LAC-AX012/AX013", sc->sc_unit, irq, "3/4/5/9");
926                 return ENXIO;
927         }
928
929         /* Fill softc struct accordingly.  */
930         sc->type = FE_TYPE_LNX;
931         sc->typestr = "LAC-AX012/AX013";
932         sc->init = fe_init_lnx;
933
934         return 0;
935 }
936
937 /*
938  * Probe and initialization for Gateway Communications' old cards.
939  */
940 static int
941 fe_probe_gwy(device_t dev)
942 {
943         struct fe_softc *sc = device_get_softc(dev);
944         u_long iobase, irq;
945
946         static struct fe_simple_probe_struct probe_table [] = {
947             /*  { FE_DLCR2, 0x70, 0x00 }, */
948                 { FE_DLCR2, 0x58, 0x00 },
949                 { FE_DLCR4, 0x08, 0x00 },
950                 { 0 }
951         };
952
953         /* See if the specified I/O address is possible for Gateway boards.  */
954         if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
955                 return ENXIO;
956         if ((iobase & ~0x1E0) != 0x200)
957                 return ENXIO;
958
959         /* That's all.  The card occupies 32 I/O addresses, as always.  */
960         if (fe_alloc_port(dev, 32))
961                 return ENXIO;
962
963         /* Setup an I/O address mapping table and some others.  */
964         fe_softc_defaults(sc);
965
966         /* See if the card is on its address.  */
967         if (!fe_simple_probe(sc, probe_table))
968                 return ENXIO;
969
970         /* Get our station address from EEPROM. */
971         fe_inblk(sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN);
972
973         /* Make sure it is Gateway Communication's.  */
974         if (!valid_Ether_p(sc->sc_enaddr, 0x000061))
975                 return ENXIO;
976
977         /* Gateway's board requires an explicit IRQ to work, since it
978            is not possible to probe the setting of jumpers.  */
979         if (bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL) != 0) {
980                 fe_irq_failure("Gateway Ethernet", sc->sc_unit, NO_IRQ, NULL);
981                 return ENXIO;
982         }
983
984         /* Fill softc struct accordingly.  */
985         sc->type = FE_TYPE_GWY;
986         sc->typestr = "Gateway Ethernet (Fujitsu chipset)";
987
988         return 0;
989 }
990
991 /* Probe and initialization for Ungermann-Bass Network
992    K.K. "Access/PC" boards.  */
993 static int
994 fe_probe_ubn(device_t dev)
995 {
996         struct fe_softc *sc = device_get_softc(dev);
997         u_long iobase, irq;
998 #if 0
999         u_char sum;
1000 #endif
1001         static struct fe_simple_probe_struct const probe_table [] = {
1002                 { FE_DLCR2, 0x58, 0x00 },
1003                 { FE_DLCR4, 0x08, 0x00 },
1004                 { 0 }
1005         };
1006
1007         /* See if the specified I/O address is possible for AccessPC/ISA.  */
1008         if (bus_get_resource(dev, SYS_RES_IOPORT, 0, &iobase, NULL) != 0)
1009                 return ENXIO;
1010         if ((iobase & ~0x0E0) != 0x300)
1011                 return ENXIO;
1012
1013         /* We have 32 registers.  */
1014         if (fe_alloc_port(dev, 32))
1015                 return ENXIO;
1016
1017         /* Setup an I/O address mapping table and some others.  */
1018         fe_softc_defaults(sc);
1019
1020         /* Simple probe.  */
1021         if (!fe_simple_probe(sc, probe_table))
1022                 return ENXIO;
1023
1024         /* Get our station address form ID ROM and make sure it is UBN's.  */
1025         fe_inblk(sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN);
1026         if (!valid_Ether_p(sc->sc_enaddr, 0x00DD01))
1027                 return ENXIO;
1028 #if 0
1029         /* Calculate checksum.  */
1030         sum = fe_inb(sc, 0x1e);
1031         for (i = 0; i < ETHER_ADDR_LEN; i++) {
1032                 sum ^= sc->sc_enaddr[i];
1033         }
1034         if (sum != 0)
1035                 return ENXIO;
1036 #endif
1037         /* This looks like an AccessPC/ISA board.  It requires an
1038            explicit IRQ setting in config.  Make sure we have one,
1039            determining an appropriate value for the IRQ control
1040            register.  */
1041         irq = 0;
1042         bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, NULL);
1043         switch (irq) {
1044         case 3:  sc->priv_info = 0x02; break;
1045         case 4:  sc->priv_info = 0x04; break;
1046         case 5:  sc->priv_info = 0x08; break;
1047         case 10: sc->priv_info = 0x10; break;
1048         default:
1049                 fe_irq_failure("Access/PC", sc->sc_unit, irq, "3/4/5/10");
1050                 return ENXIO;
1051         }
1052
1053         /* Fill softc struct accordingly.  */
1054         sc->type = FE_TYPE_UBN;
1055         sc->typestr = "Access/PC";
1056         sc->init = fe_init_ubn;
1057
1058         return 0;
1059 }