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