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