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