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