Merge from vendor branch GCC:
[dragonfly.git] / sys / dev / netif / ed / if_ed_cbus.c
1 /*
2  * Copyright (c) 1995, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/ed/if_ed_cbus.c,v 1.12 2003/10/31 18:31:58 brooks Exp $
28  * $DragonFly: src/sys/dev/netif/ed/Attic/if_ed_cbus.c,v 1.7 2004/02/13 21:15:12 joerg Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/socket.h>
34 #include <sys/kernel.h>
35
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <machine/bus.h>
39 #ifdef PC98
40 #include <sys/rman.h>
41 #include <machine/resource.h>
42 #include <machine/clock.h>
43 #include <machine/md_var.h>
44 #endif
45
46 #include <net/ethernet.h>
47 #include <net/if.h>
48 #include <net/if_arp.h>
49 #include <net/if_mib.h>
50
51 #include <bus/isa/isavar.h>
52
53 #include "if_edvar.h"
54 #ifdef PC98
55 #include "if_edreg.h"
56 #include "if_ed98.h"
57
58 static int ed98_alloc_port      (device_t, int);
59 static int ed98_alloc_memory    (device_t, int);
60 static int ed_pio_testmem       (struct ed_softc *, int, int, int);
61 static int ed_probe_SIC98       (device_t, int, int);
62 static int ed_probe_CNET98      (device_t, int, int);
63 static int ed_probe_CNET98EL    (device_t, int, int);
64 static int ed_probe_NEC77       (device_t, int, int);
65 static int ed_probe_NW98X       (device_t, int, int);
66 static int ed_probe_SB98        (device_t, int, int);
67 static int ed_probe_EZ98        (device_t, int, int);
68 static int ed98_probe_Novell    (device_t, int, int);
69 static int ed98_probe_generic8390       (struct ed_softc *);
70 static void ed_reset_CNET98     (struct ed_softc *, int);
71 static void ed_winsel_CNET98    (struct ed_softc *, u_short);
72 static void ed_get_SB98         (struct ed_softc *);
73 #endif
74
75 static int ed_isa_probe         (device_t);
76 static int ed_isa_attach        (device_t);
77
78 static struct isa_pnp_id ed_ids[] = {
79 #ifdef PC98
80 /* TODO - list up PnP boards for PC-98 */
81         { 0,            NULL }
82 #endif
83 };
84
85 static int
86 ed_isa_probe(dev)
87         device_t dev;
88 {
89         struct ed_softc *sc = device_get_softc(dev);
90         int flags = device_get_flags(dev);
91         int error = 0;
92
93         bzero(sc, sizeof(struct ed_softc));
94 #ifdef PC98
95         sc->type = ED_TYPE98(flags);
96 #ifdef ED_DEBUG
97         device_printf(dev, "ed_isa_probe: sc->type=%x\n", sc->type);
98 #endif
99 #endif
100
101         /* Check isapnp ids */
102         error = ISA_PNP_PROBE(device_get_parent(dev), dev, ed_ids);
103 #ifdef ED_DEBUG
104         device_printf(dev, "ed_isa_probe: ISA_PNP_PROBE returns %d\n", error);
105 #endif
106
107         /* If the card had a PnP ID that didn't match any we know about */
108         if (error == ENXIO) {
109                 goto end;
110         }
111
112         /* If we had some other problem. */
113         if (!(error == 0 || error == ENOENT)) {
114                 goto end;
115         }
116
117         /* Heuristic probes */
118 #ifdef ED_DEBUG
119         device_printf(dev, "ed_isa_probe: Heuristic probes start\n");
120 #endif
121 #ifdef PC98
122         switch (sc->type) {
123         case ED_TYPE98_GENERIC:
124                 /*
125                  * CAUTION!
126                  * sc->type of these boards are overwritten by PC/AT's value.
127                  */
128
129                 /*
130                  * SMC EtherEZ98
131                  */
132                 error = ed_probe_EZ98(dev, 0, flags);
133                 if (error == 0) {
134                         goto end;
135                 }
136
137                 ed_release_resources(dev);
138
139                 /*
140                  * Allied Telesis CenterCom LA-98-T
141                  */
142                 error = ed_probe_Novell(dev, 0, flags);
143                 if (error == 0) {
144                         goto end;
145                 }
146
147                 break;
148
149         /*
150          * NE2000-like boards probe routine
151          */
152         case ED_TYPE98_BDN:
153                 /*
154                  * ELECOM LANEED LD-BDN
155                  * PLANET SMART COM 98 EN-2298
156                  */
157         case ED_TYPE98_LGY:
158                 /*
159                  * MELCO LGY-98, IND-SP, IND-SS
160                  * MACNICA NE2098
161                  */
162         case ED_TYPE98_ICM:
163                 /*
164                  * ICM DT-ET-25, DT-ET-T5, IF-2766ET, IF-2771ET
165                  * D-Link DE-298P, DE-298
166                  */
167         case ED_TYPE98_EGY:
168                 /*
169                  * MELCO EGY-98
170                  * Contec C-NET(98)E-A, C-NET(98)L-A
171                  */
172         case ED_TYPE98_108:
173                 /*
174                  * NEC PC-9801-107,108
175                  */
176         case ED_TYPE98_NC5098:
177                 /*
178                  * NextCom NC5098
179                  */
180
181                 error = ed98_probe_Novell(dev, 0, flags);
182
183                 break;
184
185         /*
186          * other boards with special probe routine
187          */
188         case ED_TYPE98_SIC:
189                 /*
190                  * Allied Telesis SIC-98
191                  */
192                 error = ed_probe_SIC98(dev, 0, flags);
193
194                 break;
195
196         case ED_TYPE98_CNET98EL:
197                 /*
198                  * Contec C-NET(98)E/L
199                  */
200                 error = ed_probe_CNET98EL(dev, 0, flags);
201
202                 break;
203
204         case ED_TYPE98_CNET98:
205                 /*
206                  * Contec C-NET(98)
207                  */
208                 error = ed_probe_CNET98(dev, 0, flags);
209
210                 break;
211
212         case ED_TYPE98_LA98:
213                 /*
214                  * IO-DATA LA/T-98
215                  * NEC PC-9801-77,78
216                  */
217                 error = ed_probe_NEC77(dev, 0, flags);
218
219                 break;
220
221         case ED_TYPE98_NW98X:
222                 /*
223                  * Networld EC/EP-98X
224                  */
225                 error = ed_probe_NW98X(dev, 0, flags);
226
227                 break;
228
229         case ED_TYPE98_SB98:
230                 /*
231                  * Soliton SB-9801
232                  * Fujikura FN-9801
233                  */
234
235                 error = ed_probe_SB98(dev, 0, flags);
236
237                 break;
238         }
239 #endif
240
241 end:
242 #ifdef ED_DEBUG
243         device_printf(dev, "ed_isa_probe: end, error=%d\n", error);
244 #endif
245         if (error == 0)
246                 error = ed_alloc_irq(dev, 0, 0);
247
248         ed_release_resources(dev);
249         return (error);
250 }
251
252 static int
253 ed_isa_attach(dev)
254         device_t dev;
255 {
256         struct ed_softc *sc = device_get_softc(dev);
257         int flags = device_get_flags(dev);
258         int error;
259
260         if (sc->port_used > 0) {
261 #ifdef PC98
262                 if (ED_TYPE98(flags) == ED_TYPE98_GENERIC) {
263                         ed_alloc_port(dev, sc->port_rid, sc->port_used);
264                 } else {
265                         ed98_alloc_port(dev, sc->port_rid);
266                 }
267 #endif
268         }
269         if (sc->mem_used)
270                 ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
271
272         ed_alloc_irq(dev, sc->irq_rid, 0);
273
274         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
275                                edintr, sc, &sc->irq_handle);
276         if (error) {
277                 ed_release_resources(dev);
278                 return (error);
279         }
280
281         return ed_attach(dev);
282 }
283
284 #ifdef PC98
285 /*
286  * Interrupt conversion table for EtherEZ98
287  */
288 static unsigned short ed_EZ98_intr_val[] = {
289         0,
290         3,
291         5,
292         6,
293         0,
294         9,
295         12,
296         13
297 };
298
299 static int
300 ed_probe_EZ98(dev, port_rid, flags)
301         device_t dev;
302         int port_rid;
303         int flags;
304 {
305         struct ed_softc *sc = device_get_softc(dev);
306         int error;
307         static unsigned short *intr_vals[] = {NULL, ed_EZ98_intr_val};
308
309         error = ed_alloc_port(dev, port_rid, ED_EZ98_IO_PORTS);
310         if (error) {
311                 return (error);
312         }
313
314         sc->asic_offset = ED_EZ98_ASIC_OFFSET;
315         sc->nic_offset  = ED_EZ98_NIC_OFFSET;
316
317         return ed_probe_WD80x3_generic(dev, flags, intr_vals);
318 }
319
320 /*
321  * I/O conversion tables
322  */
323
324 /* LGY-98, ICM, C-NET(98)E/L */
325 static  bus_addr_t ed98_ioaddr_generic[] = {
326         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
327 };
328
329 /*
330  *              Definitions for Contec C-NET(98)E/L
331  */
332 #define ED_CNET98EL_ICR         2       /* Interrupt Configuration Register */
333
334 #define ED_CNET98EL_ICR_IRQ3    0x01
335 #define ED_CNET98EL_ICR_IRQ5    0x02
336 #define ED_CNET98EL_ICR_IRQ6    0x04
337 #define ED_CNET98EL_ICR_IRQ12   0x20
338
339 #define ED_CNET98EL_IMR         4       /* Interrupt Mask Register      */
340 #define ED_CNET98EL_ISR         5       /* Interrupt Status Register    */
341
342 /* EGY-98 */
343 static  bus_addr_t ed98_ioaddr_egy98[] = {
344         0,     0x02,  0x04,  0x06,  0x08,  0x0a,  0x0c,  0x0e,
345         0x100, 0x102, 0x104, 0x106, 0x108, 0x10a, 0x10c, 0x10e
346 };
347
348 /* SIC-98 */
349 static  bus_addr_t ed98_ioaddr_sic98[] = {
350         0x0000, 0x0200, 0x0400, 0x0600, 0x0800, 0x0a00, 0x0c00, 0x0e00,
351         0x1000, 0x1200, 0x1400, 0x1600, 0x1800, 0x1a00, 0x1c00, 0x1e00
352 };
353
354 /* LA/T-98, LD-BDN, PC-9801-77, SB-9801 */
355 static  bus_addr_t ed98_ioaddr_la98[] = {
356         0x0000, 0x1000, 0x2000, 0x3000, 0x4000, 0x5000, 0x6000, 0x7000,
357         0x8000, 0x9000, 0xa000, 0xb000, 0xc000, 0xd000, 0xe000, 0xf000,
358         0x0100  /* for NEC 77(see below) */
359 };
360
361 /*
362  *              Definitions for NEC PC-9801-77
363  */
364 #define ED_NEC77_IRQ            16      /* Interrupt Configuration Register */
365
366 #define ED_NEC77_IRQ3           0x04
367 #define ED_NEC77_IRQ5           0x06
368 #define ED_NEC77_IRQ6           0x08
369 #define ED_NEC77_IRQ12          0x0a
370 #define ED_NEC77_IRQ13          0x02
371
372 /*
373  *              Definitions for Soliton SB-9801
374  */
375 #define ED_SB98_CFG             1       /* Board configuration          */
376
377 #define ED_SB98_CFG_IRQ3        0x00
378 #define ED_SB98_CFG_IRQ5        0x04
379 #define ED_SB98_CFG_IRQ6        0x08
380 #define ED_SB98_CFG_IRQ12       0x0c
381 #define ED_SB98_CFG_ALTPORT     0x40            /* use EXTERNAL media   */
382 #define ED_SB98_CFG_ENABLE      0xa0            /* enable configuration */
383
384 #define ED_SB98_EEPENA          2       /* EEPROM access enable         */
385
386 #define ED_SB98_EEPENA_DISABLE  0x00
387 #define ED_SB98_EEPENA_ENABLE   0x01
388
389 #define ED_SB98_EEP             3       /* EEPROM access                */
390
391 #define ED_SB98_EEP_SDA         0x01            /* Serial Data  */
392 #define ED_SB98_EEP_SCL         0x02            /* Serial Clock */
393 #define ED_SB98_EEP_READ        0x01            /* Read Command */
394
395 #define ED_SB98_EEP_DELAY       300
396
397 #define ED_SB98_ADDRESS         0x01            /* Station Address(1-6) */
398
399 #define ED_SB98_POLARITY        4       /* Polarity                     */
400
401 /* PC-9801-108 */
402 static  bus_addr_t ed98_ioaddr_nec108[] = {
403         0x0000, 0x0002, 0x0004, 0x0006, 0x0008, 0x000a, 0x000c, 0x000e,
404         0x1000, 0x1002, 0x1004, 0x1006, 0x1008, 0x100a, 0x100c, 0x100e
405 };
406
407 /* C-NET(98) */
408 static  bus_addr_t ed98_ioaddr_cnet98[] = {
409         0x0000, 0x0002, 0x0004, 0x0006, 0x0008, 0x000a, 0x000c, 0x000e,
410         0x0400, 0x0402, 0x0404, 0x0406, 0x0408, 0x040a, 0x040c, 0x040e
411 };
412
413 /*
414  *              Definitions for Contec C-NET(98)
415  */
416 #define ED_CNET98_MAP_REG0L     0       /* MAPPING register0 Low        */
417 #define ED_CNET98_MAP_REG1L     1       /* MAPPING register1 Low        */
418 #define ED_CNET98_MAP_REG2L     2       /* MAPPING register2 Low        */
419 #define ED_CNET98_MAP_REG3L     3       /* MAPPING register3 Low        */
420 #define ED_CNET98_MAP_REG0H     4       /* MAPPING register0 Hi         */
421 #define ED_CNET98_MAP_REG1H     5       /* MAPPING register1 Hi         */
422 #define ED_CNET98_MAP_REG2H     6       /* MAPPING register2 Hi         */
423 #define ED_CNET98_MAP_REG3H     7       /* MAPPING register3 Hi         */
424 #define ED_CNET98_WIN_REG       8       /* Window register              */
425 #define ED_CNET98_INT_LEV       9       /* Init level register          */
426
427 #define ED_CNET98_INT_IRQ3      0x01            /* INT 0 */
428 #define ED_CNET98_INT_IRQ5      0x02            /* INT 1 */
429 #define ED_CNET98_INT_IRQ6      0x04            /* INT 2 */
430 #define ED_CNET98_INT_IRQ9      0x08            /* INT 3 */
431 #define ED_CNET98_INT_IRQ12     0x20            /* INT 5 */
432 #define ED_CNET98_INT_IRQ13     0x40            /* INT 6 */
433
434 #define ED_CNET98_INT_REQ       10      /* Init request register        */
435 #define ED_CNET98_INT_MASK      11      /* Init mask register           */
436 #define ED_CNET98_INT_STAT      12      /* Init status register         */
437 #define ED_CNET98_INT_CLR       12      /* Init clear register          */
438 #define ED_CNET98_RESERVE1      13
439 #define ED_CNET98_RESERVE2      14
440 #define ED_CNET98_RESERVE3      15
441
442 /* EC/EP-98X, NC5098 */
443 static  bus_addr_t ed98_ioaddr_nw98x[] = {
444         0x0000, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700,
445         0x0800, 0x0900, 0x0a00, 0x0b00, 0x0c00, 0x0d00, 0x0e00, 0x0f00,
446         0x1000  /* for EC/EP-98X(see below) */
447 };
448
449 /*
450  *              Definitions for Networld EC/EP-98X
451  */
452 #define ED_NW98X_IRQ            16      /* Interrupt Configuration Register */
453
454 #define ED_NW98X_IRQ3           0x04
455 #define ED_NW98X_IRQ5           0x06
456 #define ED_NW98X_IRQ6           0x08
457 #define ED_NW98X_IRQ12          0x0a
458 #define ED_NW98X_IRQ13          0x02
459
460 /* NC5098 ASIC */
461 static bus_addr_t ed98_asic_nc5098[] = {
462 /*      DATA    ENADDR                                          RESET   */
463         0x0000, 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x4000,
464              0,      0,      0,      0,      0,      0,      0,      0 
465 };
466
467 /*
468  *              Definitions for NextCom NC5098
469  */
470 #define ED_NC5098_ENADDR        1       /* Station Address(1-6)         */
471
472 /*
473  * Allocate a port resource with the given resource id.
474  */
475 static int
476 ed98_alloc_port(dev, rid)
477         device_t dev;
478         int rid;
479 {
480         struct ed_softc *sc = device_get_softc(dev);
481         struct resource *res;
482         int error;
483         bus_addr_t *io_nic, *io_asic, adj;
484         static bus_addr_t io_res[ED_NOVELL_IO_PORTS + 1];
485         int i, n;
486         int offset, reset, data;
487
488         /* Set i/o table for resource manager */
489         io_nic = io_asic = ed98_ioaddr_generic;
490         offset = ED_NOVELL_ASIC_OFFSET;
491         reset = ED_NOVELL_RESET;
492         data  = ED_NOVELL_DATA;
493         n = ED_NOVELL_IO_PORTS;
494
495         switch (sc->type) {
496         case ED_TYPE98_LGY:
497                 io_asic = ed98_ioaddr_egy98; /* XXX - Yes, we use egy98 */
498                 offset = 0x0200;
499                 reset = 8;
500                 break;
501
502         case ED_TYPE98_EGY:
503                 io_nic = io_asic = ed98_ioaddr_egy98;
504                 offset = 0x0200;
505                 reset = 8;
506                 break;
507
508         case ED_TYPE98_ICM:
509                 offset = 0x0100;
510                 break;
511
512         case ED_TYPE98_BDN:
513                 io_nic = io_asic = ed98_ioaddr_la98;
514                 offset = 0x0100;
515                 reset = 0x0c;
516                 break;
517
518         case ED_TYPE98_SIC:
519                 io_nic = io_asic = ed98_ioaddr_sic98;
520                 offset = 0x2000;
521                 n = 16+1;
522                 break;
523
524         case ED_TYPE98_108:
525                 io_nic = io_asic = ed98_ioaddr_nec108;
526                 offset = 0x0888;        /* XXX - overwritten after */
527                 reset = 1;
528                 n = 16; /* XXX - does not set ASIC i/o here */
529                 break;
530
531         case ED_TYPE98_LA98:
532                 io_nic = io_asic = ed98_ioaddr_la98;
533                 offset = 0x0100;
534                 break;
535
536         case ED_TYPE98_CNET98EL:
537                 offset = 0x0400;
538                 data = 0x0e;
539                 break;
540
541         case ED_TYPE98_CNET98:
542                 /* XXX - Yes, we use generic i/o here */
543                 offset = 0x0400;
544                 break;
545
546         case ED_TYPE98_NW98X:
547                 io_nic = io_asic = ed98_ioaddr_nw98x;
548                 offset = 0x1000;
549                 break;
550
551         case ED_TYPE98_SB98:
552                 io_nic = io_asic = ed98_ioaddr_la98;
553                 offset = 0x0400;
554                 reset = 7;
555                 break;
556
557         case ED_TYPE98_NC5098:
558                 io_nic  = ed98_ioaddr_nw98x;
559                 io_asic = ed98_asic_nc5098;
560                 offset = 0x2000;
561                 reset = 7;
562                 n = 16+8;       /* XXX */
563                 break;
564         }
565
566         bcopy(io_nic, io_res, sizeof(io_nic[0]) * ED_NOVELL_ASIC_OFFSET);
567         for (i = ED_NOVELL_ASIC_OFFSET; i < ED_NOVELL_IO_PORTS; i++) {
568                 io_res[i] = io_asic[i - ED_NOVELL_ASIC_OFFSET] + offset;
569         }
570
571         res = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid,
572                                   io_res, n, RF_ACTIVE);
573         if (!res) {
574                 return (ENOENT);
575         }
576
577         sc->port_rid = rid;
578         sc->port_res = res;
579         sc->port_used = n;
580
581         /* Re-map i/o table if needed */
582         switch (sc->type) {
583         case ED_TYPE98_LA98:
584         case ED_TYPE98_NW98X:
585                 io_res[n] = io_asic[n - ED_NOVELL_ASIC_OFFSET] + offset;
586                 n++;
587                 break;
588
589         case ED_TYPE98_108:
590                 adj = (rman_get_start(res) & 0xf000) / 2;
591                 offset = (offset | adj) - rman_get_start(res);
592
593                 for (n = ED_NOVELL_ASIC_OFFSET; n < ED_NOVELL_IO_PORTS; n++) {
594                         io_res[n] = io_asic[n - ED_NOVELL_ASIC_OFFSET] + offset;
595                 }
596                 break;
597
598         case ED_TYPE98_CNET98:
599                 io_nic = io_asic = ed98_ioaddr_cnet98;
600                 offset = 1;
601
602                 bcopy(io_nic, io_res, sizeof(io_nic[0]) * ED_NOVELL_ASIC_OFFSET);
603                 for (n = ED_NOVELL_ASIC_OFFSET; n < ED_NOVELL_IO_PORTS; n++) {
604                         io_res[n] = io_asic[n - ED_NOVELL_ASIC_OFFSET] + offset;
605                 }
606                 break;
607
608         case ED_TYPE98_NC5098:
609                 n = ED_NOVELL_IO_PORTS;
610                 break;
611         }
612
613         if (reset != ED_NOVELL_RESET) {
614                 io_res[ED_NOVELL_ASIC_OFFSET + ED_NOVELL_RESET] =
615                         io_res[ED_NOVELL_ASIC_OFFSET + reset];
616         }
617         if (data  != ED_NOVELL_DATA) {
618                 io_res[ED_NOVELL_ASIC_OFFSET + ED_NOVELL_DATA] =
619                         io_res[ED_NOVELL_ASIC_OFFSET + data];
620 #if 0
621                 io_res[ED_NOVELL_ASIC_OFFSET + ED_NOVELL_DATA + 1] =
622                         io_res[ED_NOVELL_ASIC_OFFSET + data + 1];
623 #endif
624         }
625
626         error = isa_load_resourcev(res, io_res, n);
627         if (error != 0) {
628                 return (ENOENT);
629         }
630 #ifdef ED_DEBUG
631         device_printf(dev, "ed98_alloc_port: i/o ports = %d\n", n);
632         for (i = 0; i < n; i++) {
633                 printf("%x,", io_res[i]);
634         }
635         printf("\n");
636 #endif
637         return (0);
638 }
639
640 static int
641 ed98_alloc_memory(dev, rid)
642         device_t dev;
643         int rid;
644 {
645         struct ed_softc *sc = device_get_softc(dev);
646         int error;
647         u_long conf_maddr, conf_msize;
648
649         error = bus_get_resource(dev, SYS_RES_MEMORY, 0,
650                                  &conf_maddr, &conf_msize);
651         if (error) {
652                 return (error);
653         }
654
655         if ((conf_maddr == 0) || (conf_msize == 0)) {
656                 return (ENXIO);
657         }
658
659         error = ed_alloc_memory(dev, rid, (int) conf_msize);
660         if (error) {
661                 return (error);
662         }
663
664         sc->mem_start = (caddr_t) rman_get_virtual(sc->mem_res);
665         sc->mem_size  = conf_msize;
666
667         return (0);
668 }
669
670 /*
671  * Generic probe routine for testing for the existance of a DS8390.
672  *      Must be called after the NIC has just been reset. This routine
673  *      works by looking at certain register values that are guaranteed
674  *      to be initialized a certain way after power-up or reset. Seems
675  *      not to currently work on the 83C690.
676  *
677  * Specifically:
678  *
679  *      Register                        reset bits      set bits
680  *      Command Register (CR)           TXP, STA        RD2, STP
681  *      Interrupt Status (ISR)                          RST
682  *      Interrupt Mask (IMR)            All bits
683  *      Data Control (DCR)                              LAS
684  *      Transmit Config. (TCR)          LB1, LB0
685  *
686  * XXX - We only check the CR register.
687  *
688  * Return 1 if 8390 was found, 0 if not.
689  */
690
691 static int
692 ed98_probe_generic8390(sc)
693         struct ed_softc *sc;
694 {
695         u_char tmp = ed_nic_inb(sc, ED_P0_CR);
696 #ifdef DIAGNOSTIC
697         printf("ed?: inb(ED_P0_CR)=%x\n", tmp);
698 #endif
699         if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
700             (ED_CR_RD2 | ED_CR_STP)) {
701                 return (0);
702         }
703
704         (void) ed_nic_inb(sc, ED_P0_ISR);
705
706         return (1);
707 }
708
709 static int
710 ed98_probe_Novell(dev, port_rid, flags)
711         device_t dev;
712         int port_rid;
713         int flags;
714 {
715         struct ed_softc *sc = device_get_softc(dev);
716         int error;
717         int n;
718         u_char romdata[ETHER_ADDR_LEN * 2], tmp;
719
720 #ifdef ED_DEBUG
721         device_printf(dev, "ed98_probe_Novell: start\n");
722 #endif
723         error = ed98_alloc_port(dev, port_rid);
724         if (error) {
725                 return (error);
726         }
727
728         sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
729         sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
730
731         /* Reset the board */
732 #ifdef ED_DEBUG
733         device_printf(dev, "ed98_probe_Novell: reset\n");
734 #endif
735         switch (sc->type) {
736 #if 1   /* XXX - I'm not sure this is really necessary... */
737         case ED_TYPE98_BDN:
738                 tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
739                 ed_asic_outb(sc, ED_NOVELL_RESET, (tmp & 0xf0) | 0x08);
740                 ed_nic_outb(sc, 0x04, tmp);
741                 (void) ed_asic_inb(sc, 0x08);
742                 ed_asic_outb(sc, 0x08, tmp);
743                 ed_asic_outb(sc, 0x08, tmp & 0x7f);
744                 break;
745 #endif
746         case ED_TYPE98_NC5098:
747                 ed_asic_outb(sc, ED_NOVELL_RESET, 0x00);
748                 DELAY(5000);
749                 ed_asic_outb(sc, ED_NOVELL_RESET, 0x01);
750                 break;
751
752         default:
753                 tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
754
755         /*
756          * I don't know if this is necessary; probably cruft leftover from
757          * Clarkson packet driver code. Doesn't do a thing on the boards I've
758          * tested. -DG [note that an outb(0x84, 0) seems to work here, and is
759          * non-invasive...but some boards don't seem to reset and I don't have
760          * complete documentation on what the 'right' thing to do is...so we
761          * do the invasive thing for now. Yuck.]
762          */
763                 ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
764                 break;
765         }
766         DELAY(5000);
767
768         /*
769          * This is needed because some NE clones apparently don't reset the
770          * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX
771          * - this makes the probe invasive! ...Done against my better
772          * judgement. -DLG
773          */
774         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
775         DELAY(5000);
776
777         /* Make sure that we really have an 8390 based board */
778         if (!ed98_probe_generic8390(sc)) {
779                 return (ENXIO);
780         }
781
782         /* Test memory via PIO */
783 #ifdef ED_DEBUG
784         device_printf(dev, "ed98_probe_Novell: test memory\n");
785 #endif
786         sc->cr_proto = ED_CR_RD2;
787         if (!ed_pio_testmem(sc,  8192, 0, flags)
788         &&  !ed_pio_testmem(sc, 16384, 1, flags)) {
789                 return (ENXIO);
790         }
791
792         /* Setup the board type */
793 #ifdef ED_DEBUG
794         device_printf(dev, "ed98_probe_Novell: board type\n");
795 #endif
796         switch (sc->type) {
797         case ED_TYPE98_BDN:
798                 sc->type_str = "LD-BDN";
799                 break;
800         case ED_TYPE98_EGY:
801                 sc->type_str = "EGY-98";
802                 break;
803         case ED_TYPE98_LGY:
804                 sc->type_str = "LGY-98";
805                 break;
806         case ED_TYPE98_ICM:
807                 sc->type_str = "ICM";
808                 break;
809         case ED_TYPE98_108:
810                 sc->type_str = "PC-9801-108";
811                 break;
812         case ED_TYPE98_LA98:
813                 sc->type_str = "LA-98";
814                 break;
815         case ED_TYPE98_NW98X:
816                 sc->type_str = "NW98X";
817                 break;
818         case ED_TYPE98_NC5098:
819                 sc->type_str = "NC5098";
820                 break;
821         default:
822                 sc->type_str = NULL;
823                 break;
824         }
825
826         /* Get station address */
827         switch (sc->type) {
828         case ED_TYPE98_NC5098:
829                 for (n = 0; n < ETHER_ADDR_LEN; n++) {
830                         sc->arpcom.ac_enaddr[n] =
831                                 ed_asic_inb(sc, ED_NC5098_ENADDR + n);
832                 }
833                 break;
834
835         default:
836                 ed_pio_readmem(sc, 0, romdata, sizeof(romdata));
837                 for (n = 0; n < ETHER_ADDR_LEN; n++) {
838                         sc->arpcom.ac_enaddr[n] =
839                                 romdata[n * (sc->isa16bit + 1)];
840                 }
841                 break;
842         }
843
844         /* clear any pending interrupts that might have occurred above */
845         ed_nic_outb(sc, ED_P0_ISR, 0xff);
846
847         return (0);
848 }
849
850 /*
851  * Probe and vendor-specific initialization routine for SIC-98 boards
852  */
853 static int
854 ed_probe_SIC98(dev, port_rid, flags)
855         device_t dev;
856         int port_rid;
857         int flags;
858 {
859         struct ed_softc *sc = device_get_softc(dev);
860         int error;
861         int i;
862         u_char sum;
863
864         /*
865          * Setup card RAM and I/O address
866          * Kernel Virtual to segment C0000-DFFFF????
867          */
868         error = ed98_alloc_port(dev, port_rid);
869         if (error) {
870                 return (error);
871         }
872
873         sc->asic_offset = ED_SIC_ASIC_OFFSET;
874         sc->nic_offset  = ED_SIC_NIC_OFFSET;
875
876         error = ed98_alloc_memory(dev, 0);
877         if (error) {
878                 return (error);
879         }
880
881         /* Reset card to force it into a known state. */
882         ed_asic_outb(sc, 0, 0x00);
883         DELAY(100);
884         if (ED_TYPE98SUB(flags) == 0) {
885                 /* SIC-98/SIU-98 */
886                 ed_asic_outb(sc, 0, 0x94);
887                 DELAY(100);
888                 ed_asic_outb(sc, 0, 0x94);
889         } else {
890                 /* SIU-98-D */
891                 ed_asic_outb(sc, 0, 0x80);
892                 DELAY(100);
893                 ed_asic_outb(sc, 0, 0x94);
894                 DELAY(100);
895                 ed_asic_outb(sc, 0, 0x9e);
896         }
897         DELAY(100);
898
899         /*
900          * Here we check the card ROM, if the checksum passes, and the
901          * type code and ethernet address check out, then we know we have
902          * an SIC card.
903          */
904         sum = sc->mem_start[6 * 2];
905         for (i = 0; i < ETHER_ADDR_LEN; i++) {
906                 sum ^= (sc->arpcom.ac_enaddr[i] = sc->mem_start[i * 2]);
907         }
908 #ifdef ED_DEBUG
909         device_printf(dev, "ed_probe_sic98: got address %6D\n",
910                       sc->arpcom.ac_enaddr, ":");
911 #endif
912         if (sum != 0) {
913                 return (ENXIO);
914         }
915         if ((sc->arpcom.ac_enaddr[0] | sc->arpcom.ac_enaddr[1] |
916              sc->arpcom.ac_enaddr[2]) == 0) {
917                 return (ENXIO);
918         }
919
920         sc->vendor   = ED_VENDOR_SIC;
921         sc->type_str = "SIC98";
922         sc->isa16bit = 1;
923         sc->cr_proto = 0;
924
925         /*
926          * SIC RAM page 0x0000-0x3fff(or 0x7fff)
927          */
928         if (ED_TYPE98SUB(flags) == 0) {
929                 ed_asic_outb(sc, 0, 0x90);
930         } else {
931                 ed_asic_outb(sc, 0, 0x8e);
932         }
933         DELAY(100);
934
935         /*
936          * clear interface memory, then sum to make sure its valid
937          */
938         bzero(sc->mem_start, sc->mem_size);
939
940         for (i = 0; i < sc->mem_size; i++) {
941                 if (sc->mem_start[i]) {
942                         device_printf(dev, "failed to clear shared memory "
943                                 "at %x - check configuration\n",
944                                 kvtop(sc->mem_start + i));
945
946                         return (ENXIO);
947                 }
948         }
949
950         sc->mem_shared = 1;
951         sc->mem_end = sc->mem_start + sc->mem_size;
952
953         /*
954          * allocate one xmit buffer if < 16k, two buffers otherwise
955          */
956         if ((sc->mem_size < 16384) || (flags & ED_FLAGS_NO_MULTI_BUFFERING)) {
957                 sc->txb_cnt = 1;
958         } else {
959                 sc->txb_cnt = 2;
960         }
961         sc->tx_page_start = 0;
962
963         sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE * sc->txb_cnt;
964         sc->rec_page_stop = sc->tx_page_start + sc->mem_size / ED_PAGE_SIZE;
965
966         sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
967
968         return (0);
969 }
970
971 /*
972  * Contec C-NET(98) series support routines
973  */
974 static void
975 ed_reset_CNET98(sc, flags)
976         struct ed_softc *sc;
977         int flags;
978 {
979         u_int   init_addr = ED_CNET98_INIT;
980         u_char tmp;
981
982         /* Choose initial register address */
983         if (ED_TYPE98SUB(flags) != 0) {
984                 init_addr = ED_CNET98_INIT2;
985         }
986 #ifdef ED_DEBUG
987         printf("ed?: initial register=%x\n", init_addr);
988 #endif
989         /*
990          * Reset the board to force it into a known state.
991          */
992         outb(init_addr, 0x00);  /* request */
993         DELAY(5000);
994         outb(init_addr, 0x01);  /* cancel */
995         DELAY(5000);
996
997         /*
998          * Set I/O address(A15-12) and cpu type
999          *
1000          *   AAAAIXXC(8bit)
1001          *   AAAA: A15-A12,  I: I/O enable, XX: reserved, C: CPU type
1002          *
1003          * CPU type is 1:80286 or higher, 0:not.
1004          * But FreeBSD runs under i386 or higher, thus it must be 1.
1005          */
1006         tmp = (rman_get_start(sc->port_res) & 0xf000) >> 8;
1007         tmp |= (0x08 | 0x01);
1008 #ifdef ED_DEBUG
1009         printf("ed?: outb(%x, %x)\n", init_addr + 2, tmp);
1010 #endif
1011         outb(init_addr + 2, tmp);
1012         DELAY(5000);
1013
1014         /*
1015          * This is needed because some NE clones apparently don't reset the
1016          * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX
1017          * - this makes the probe invasive! ...Done against my better
1018          * judgement. -DLG
1019          */
1020         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
1021         DELAY(5000);
1022 }
1023
1024 static void
1025 ed_winsel_CNET98(sc, bank)
1026         struct ed_softc *sc;
1027         u_short bank;
1028 {
1029         u_char mem = (kvtop(sc->mem_start) >> 12) & 0xff;
1030
1031         /*
1032          * Disable window memory
1033          *    bit7 is 0:disable
1034          */
1035         ed_asic_outb(sc, ED_CNET98_WIN_REG, mem & 0x7f);
1036         DELAY(10);
1037
1038         /*
1039          * Select window address
1040          *    FreeBSD address 0xf00xxxxx
1041          */
1042         ed_asic_outb(sc, ED_CNET98_MAP_REG0L, bank & 0xff);
1043         DELAY(10);
1044         ed_asic_outb(sc, ED_CNET98_MAP_REG0H, (bank >> 8) & 0xff);
1045         DELAY(10);
1046         ed_asic_outb(sc, ED_CNET98_MAP_REG1L, 0x00);
1047         DELAY(10);
1048         ed_asic_outb(sc, ED_CNET98_MAP_REG1H, 0x41);
1049         DELAY(10);
1050         ed_asic_outb(sc, ED_CNET98_MAP_REG2L, 0x00);
1051         DELAY(10);
1052         ed_asic_outb(sc, ED_CNET98_MAP_REG2H, 0x42);
1053         DELAY(10);
1054         ed_asic_outb(sc, ED_CNET98_MAP_REG3L, 0x00);
1055         DELAY(10);
1056         ed_asic_outb(sc, ED_CNET98_MAP_REG3H, 0x43);
1057         DELAY(10);
1058
1059         /*
1060          * Enable window memory(16Kbyte)
1061          *    bit7 is 1:enable
1062          */
1063 #ifdef ED_DEBUG
1064         printf("ed?: window start address=%x\n", mem);
1065 #endif
1066         ed_asic_outb(sc, ED_CNET98_WIN_REG, mem);
1067         DELAY(10);
1068 }
1069
1070 /*
1071  * Probe and vendor-specific initialization routine for C-NET(98) boards
1072  */
1073 static int
1074 ed_probe_CNET98(dev, port_rid, flags)
1075         device_t dev;
1076         int port_rid;
1077         int flags;
1078 {
1079         struct ed_softc *sc = device_get_softc(dev);
1080         int error;
1081         u_char tmp;
1082         u_long conf_irq, junk;
1083         int i;
1084 #ifdef DIAGNOSTIC
1085         u_char tmp_s;
1086 #endif
1087
1088         error = ed98_alloc_port(dev, port_rid);
1089         if (error) {
1090                 return (error);
1091         }
1092
1093         sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
1094         sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
1095
1096         error = ed98_alloc_memory(dev, 0);
1097         if (error) {
1098                 return (error);
1099         }
1100
1101         /* Check I/O address. 0x[a-f]3d0 are allowed. */
1102         if (((rman_get_start(sc->port_res) & 0x0fff) != 0x03d0)
1103         ||  ((rman_get_start(sc->port_res) & 0xf000) < (u_short) 0xa000)) {
1104 #ifdef DIAGNOSTIC
1105                 device_printf(dev, "Invalid i/o port configuration (0x%lx) "
1106                         "must be %s for %s\n", rman_get_start(sc->port_res),
1107                         "0x[a-f]3d0", "CNET98");
1108 #endif
1109                 return (ENXIO);
1110         }
1111
1112 #ifdef DIAGNOSTIC
1113         /* Check window area address */
1114         tmp_s = kvtop(sc->mem_start) >> 12;
1115         if (tmp_s < 0x80) {
1116                 device_printf(dev, "Please change window address(0x%x)\n",
1117                         kvtop(sc->mem_start));
1118                 return (ENXIO);
1119         }
1120
1121         tmp_s &= 0x0f;
1122         tmp    = rman_get_start(sc->port_res) >> 12;
1123         if ((tmp_s <= tmp) && (tmp < (tmp_s + 4))) {
1124                 device_printf(dev, "Please change iobase address(0x%lx) "
1125                         "or window address(0x%x)\n",
1126                         rman_get_start(sc->port_res), kvtop(sc->mem_start));
1127                 return (ENXIO);
1128         }
1129 #endif
1130         /* Reset the board */
1131         ed_reset_CNET98(sc, flags);
1132
1133         /*
1134          * This is needed because some NE clones apparently don't reset the
1135          * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX
1136          * - this makes the probe invasive! ...Done against my better
1137          * judgement. -DLG
1138          */
1139         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
1140         DELAY(5000);
1141
1142         /* Make sure that we really have an 8390 based board */
1143         if (!ed98_probe_generic8390(sc)) {
1144                 return (ENXIO);
1145         }
1146
1147         /*
1148          *  Set window ethernet address area
1149          *    board memory base 0x480000  data 256byte
1150          */
1151         ed_winsel_CNET98(sc, 0x4800);
1152
1153         /*
1154          * Get station address from on-board ROM
1155          */
1156         bcopy(sc->mem_start, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
1157
1158         sc->vendor    = ED_VENDOR_MISC;
1159         sc->type_str  = "CNET98";
1160         sc->isa16bit  = 0;
1161         sc->cr_proto  = ED_CR_RD2;
1162
1163         /*
1164          * Set window buffer memory area
1165          *    board memory base 0x400000  data 16kbyte
1166          */
1167         ed_winsel_CNET98(sc, 0x4000);
1168
1169         /*
1170          * clear interface memory, then sum to make sure its valid
1171          */
1172         bzero(sc->mem_start, sc->mem_size);
1173
1174         for (i = 0; i < sc->mem_size; i++) {
1175                 if (sc->mem_start[i]) {
1176                         device_printf(dev, "failed to clear shared memory "
1177                                 "at %x - check configuration\n",
1178                                 kvtop(sc->mem_start + i));
1179
1180                         return (ENXIO);
1181                 }
1182         }
1183
1184         sc->mem_shared = 1;
1185         sc->mem_end = sc->mem_start + sc->mem_size;
1186
1187         sc->txb_cnt = 1;        /* XXX */
1188         sc->tx_page_start = 0;
1189
1190         sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE;
1191         sc->rec_page_stop = sc->tx_page_start + sc->mem_size / ED_PAGE_SIZE;
1192
1193         sc->mem_ring = sc->mem_start + ED_PAGE_SIZE * ED_TXBUF_SIZE;
1194
1195         /*
1196          *   Set interrupt level
1197          */
1198         error = bus_get_resource(dev, SYS_RES_IRQ, 0,
1199                                  &conf_irq, &junk);
1200         if (error)
1201                 return (error);
1202
1203         switch (conf_irq) {
1204         case 3:
1205                 tmp = ED_CNET98_INT_IRQ3;
1206                 break;
1207         case 5:
1208                 tmp = ED_CNET98_INT_IRQ5;
1209                 break;
1210         case 6:
1211                 tmp = ED_CNET98_INT_IRQ6;
1212                 break;
1213         case 9:
1214                 tmp = ED_CNET98_INT_IRQ9;
1215                 break;
1216         case 12:
1217                 tmp = ED_CNET98_INT_IRQ12;
1218                 break;
1219         case 13:
1220                 tmp = ED_CNET98_INT_IRQ13;
1221                 break;
1222         default:
1223                 device_printf(dev, "Invalid irq configuration (%ld) must be "
1224                         "%s for %s\n", conf_irq, "3,5,6,9,12,13", "CNET98");
1225                 return (ENXIO);
1226         }
1227         ed_asic_outb(sc, ED_CNET98_INT_LEV, tmp);
1228         DELAY(1000);
1229         /*
1230          *   Set interrupt mask.
1231          *     bit7:1 all interrupt mask
1232          *     bit1:1 timer interrupt mask
1233          *     bit0:0 NS controler interrupt enable
1234          */
1235         ed_asic_outb(sc, ED_CNET98_INT_MASK, 0x7e);
1236         DELAY(1000);
1237
1238         return (0);
1239 }
1240
1241 /*
1242  * Probe and vendor-specific initialization routine for C-NET(98)E/L boards
1243  */
1244 static int
1245 ed_probe_CNET98EL(dev, port_rid, flags)
1246         device_t dev;
1247         int port_rid;
1248         int flags;
1249 {
1250         struct ed_softc *sc = device_get_softc(dev);
1251         int error;
1252         int i;
1253         u_char romdata[ETHER_ADDR_LEN * 2], tmp;
1254         u_long conf_irq, junk;
1255
1256         error = ed98_alloc_port(dev, port_rid);
1257         if (error) {
1258                 return (error);
1259         }
1260
1261         sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
1262         sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
1263
1264         /* Check I/O address. 0x[0-f]3d0 are allowed. */
1265         if ((rman_get_start(sc->port_res) & 0x0fff) != 0x03d0) {
1266 #ifdef DIAGNOSTIC
1267                 device_printf(dev, "Invalid i/o port configuration (0x%lx) "
1268                         "must be %s for %s\n", rman_get_start(sc->port_res),
1269                         "0x?3d0", "CNET98E/L");
1270 #endif
1271                 return (ENXIO);
1272         }
1273
1274         /* Reset the board */
1275         ed_reset_CNET98(sc, flags);
1276
1277         /*
1278          * This is needed because some NE clones apparently don't reset the
1279          * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX
1280          * - this makes the probe invasive! ...Done against my better
1281          * judgement. -DLG
1282          */
1283         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
1284         DELAY(5000);
1285
1286         /* Make sure that we really have an 8390 based board */
1287         if (!ed98_probe_generic8390(sc)) {
1288                 return (ENXIO);
1289         }
1290
1291         /* Test memory via PIO */
1292         sc->cr_proto = ED_CR_RD2;
1293         if (!ed_pio_testmem(sc, ED_CNET98EL_PAGE_OFFSET, 1, flags)) {
1294                 return (ENXIO);
1295         }
1296
1297         /* This looks like a C-NET(98)E/L board. */
1298         sc->type_str = "CNET98E/L";
1299
1300         /*
1301          * Set IRQ. C-NET(98)E/L only allows a choice of irq 3,5,6.
1302          */
1303         error = bus_get_resource(dev, SYS_RES_IRQ, 0,
1304                                  &conf_irq, &junk);
1305         if (error) {
1306                 return (error);
1307         }
1308
1309         switch (conf_irq) {
1310         case 3:
1311                 tmp = ED_CNET98EL_ICR_IRQ3;
1312                 break;
1313         case 5:
1314                 tmp = ED_CNET98EL_ICR_IRQ5;
1315                 break;
1316         case 6:
1317                 tmp = ED_CNET98EL_ICR_IRQ6;
1318                 break;
1319 #if 0
1320         case 12:
1321                 tmp = ED_CNET98EL_ICR_IRQ12;
1322                 break;
1323 #endif
1324         default:
1325                 device_printf(dev, "Invalid irq configuration (%ld) must be "
1326                         "%s for %s\n", conf_irq, "3,5,6", "CNET98E/L");
1327                 return (ENXIO);
1328         }
1329         ed_asic_outb(sc, ED_CNET98EL_ICR, tmp);
1330         ed_asic_outb(sc, ED_CNET98EL_IMR, 0x7e);
1331
1332         /* Get station address from on-board ROM */
1333         ed_pio_readmem(sc, 16384, romdata, sizeof(romdata));
1334         for (i = 0; i < ETHER_ADDR_LEN; i++) {
1335                 sc->arpcom.ac_enaddr[i] = romdata[i * 2];
1336         }
1337
1338         /* clear any pending interrupts that might have occurred above */
1339         ed_nic_outb(sc, ED_P0_ISR, 0xff);
1340
1341         return (0);
1342 }
1343
1344 /*
1345  * Probe and vendor-specific initialization routine for PC-9801-77 boards
1346  */
1347 static int
1348 ed_probe_NEC77(dev, port_rid, flags)
1349         device_t dev;
1350         int port_rid;
1351         int flags;
1352 {
1353         struct ed_softc *sc = device_get_softc(dev);
1354         int error;
1355         u_char tmp;
1356         u_long conf_irq, junk;
1357
1358         error = ed98_probe_Novell(dev, port_rid, flags);
1359         if (error) {
1360                 return (error);
1361         }
1362
1363         /* LA/T-98 does not need IRQ setting. */
1364         if (ED_TYPE98SUB(flags) == 0) {
1365                 return (0);
1366         }
1367
1368         /*
1369          * Set IRQ. PC-9801-77 only allows a choice of irq 3,5,6,12,13.
1370          */
1371         error = bus_get_resource(dev, SYS_RES_IRQ, 0,
1372                                  &conf_irq, &junk);
1373         if (error) {
1374                 return (error);
1375         }
1376
1377         switch (conf_irq) {
1378         case 3:
1379                 tmp = ED_NEC77_IRQ3;
1380                 break;
1381         case 5:
1382                 tmp = ED_NEC77_IRQ5;
1383                 break;
1384         case 6:
1385                 tmp = ED_NEC77_IRQ6;
1386                 break;
1387         case 12:
1388                 tmp = ED_NEC77_IRQ12;
1389                 break;
1390         case 13:
1391                 tmp = ED_NEC77_IRQ13;
1392                 break;
1393         default:
1394                 device_printf(dev, "Invalid irq configuration (%ld) must be "
1395                         "%s for %s\n", conf_irq, "3,5,6,12,13", "PC-9801-77");
1396                 return (ENXIO);
1397         }
1398         ed_asic_outb(sc, ED_NEC77_IRQ, tmp);
1399
1400         return (0);
1401 }
1402
1403 /*
1404  * Probe and vendor-specific initialization routine for EC/EP-98X boards
1405  */
1406 static int
1407 ed_probe_NW98X(dev, port_rid, flags)
1408         device_t dev;
1409         int port_rid;
1410         int flags;
1411 {
1412         struct ed_softc *sc = device_get_softc(dev);
1413         int error;
1414         u_char tmp;
1415         u_long conf_irq, junk;
1416
1417         error = ed98_probe_Novell(dev, port_rid, flags);
1418         if (error) {
1419                 return (error);
1420         }
1421
1422         /* Networld 98X3 does not need IRQ setting. */
1423         if (ED_TYPE98SUB(flags) == 0) {
1424                 return (0);
1425         }
1426
1427         /*
1428          * Set IRQ. EC/EP-98X only allows a choice of irq 3,5,6,12,13.
1429          */
1430         error = bus_get_resource(dev, SYS_RES_IRQ, 0,
1431                                  &conf_irq, &junk);
1432         if (error) {
1433                 return (error);
1434         }
1435
1436         switch (conf_irq) {
1437         case 3:
1438                 tmp = ED_NW98X_IRQ3;
1439                 break;
1440         case 5:
1441                 tmp = ED_NW98X_IRQ5;
1442                 break;
1443         case 6:
1444                 tmp = ED_NW98X_IRQ6;
1445                 break;
1446         case 12:
1447                 tmp = ED_NW98X_IRQ12;
1448                 break;
1449         case 13:
1450                 tmp = ED_NW98X_IRQ13;
1451                 break;
1452         default:
1453                 device_printf(dev, "Invalid irq configuration (%ld) must be "
1454                         "%s for %s\n", conf_irq, "3,5,6,12,13", "EC/EP-98X");
1455                 return (ENXIO);
1456         }
1457         ed_asic_outb(sc, ED_NW98X_IRQ, tmp);
1458
1459         return (0);
1460 }
1461
1462 /*
1463  * Read SB-9801 station address from Serial Two-Wire EEPROM
1464  */
1465 static void
1466 ed_get_SB98(sc)
1467         struct ed_softc *sc;
1468 {
1469         int i, j;
1470         u_char mask, val;
1471
1472         /* enable EEPROM acceess */
1473         ed_asic_outb(sc, ED_SB98_EEPENA, ED_SB98_EEPENA_ENABLE);
1474
1475         /* output start command */
1476         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SDA | ED_SB98_EEP_SCL);
1477         DELAY(ED_SB98_EEP_DELAY);
1478         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SCL);
1479         DELAY(ED_SB98_EEP_DELAY);
1480
1481         /* output address (7bit) */
1482         for (mask = 0x40; mask != 0; mask >>= 1) {
1483                 val = 0;
1484                 if (ED_SB98_ADDRESS & mask)
1485                         val = ED_SB98_EEP_SDA;
1486                 ed_asic_outb(sc, ED_SB98_EEP, val);
1487                 DELAY(ED_SB98_EEP_DELAY);
1488                 ed_asic_outb(sc, ED_SB98_EEP, val | ED_SB98_EEP_SCL);
1489                 DELAY(ED_SB98_EEP_DELAY);
1490         }
1491
1492         /* output READ command */
1493         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_READ);
1494         DELAY(ED_SB98_EEP_DELAY);
1495         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_READ | ED_SB98_EEP_SCL);
1496         DELAY(ED_SB98_EEP_DELAY);
1497
1498         /* read station address */
1499         for (i = 0; i < ETHER_ADDR_LEN; i++) {
1500                 /* output ACK */
1501                 ed_asic_outb(sc, ED_SB98_EEP, 0);
1502                 DELAY(ED_SB98_EEP_DELAY);
1503                 ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SCL);
1504                 DELAY(ED_SB98_EEP_DELAY);
1505
1506                 val = 0;
1507                 for (j = 0; j < 8; j++) {
1508                         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SDA);
1509                         DELAY(ED_SB98_EEP_DELAY);
1510                         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SDA | ED_SB98_EEP_SCL);
1511                         DELAY(ED_SB98_EEP_DELAY);
1512                         val <<= 1;
1513                         val |= (ed_asic_inb(sc, ED_SB98_EEP) & ED_SB98_EEP_SDA);
1514                         DELAY(ED_SB98_EEP_DELAY);
1515                 }
1516                 sc->arpcom.ac_enaddr[i] = val;
1517         }
1518
1519         /* output Last ACK */
1520         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SDA);
1521         DELAY(ED_SB98_EEP_DELAY);
1522         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SDA | ED_SB98_EEP_SCL);
1523         DELAY(ED_SB98_EEP_DELAY);
1524
1525         /* output stop command */
1526         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SCL);
1527         DELAY(ED_SB98_EEP_DELAY);
1528         ed_asic_outb(sc, ED_SB98_EEP, ED_SB98_EEP_SDA | ED_SB98_EEP_SCL);
1529         DELAY(ED_SB98_EEP_DELAY);
1530
1531         /* disable EEPROM access */
1532         ed_asic_outb(sc, ED_SB98_EEPENA, ED_SB98_EEPENA_DISABLE);
1533 }
1534
1535 /*
1536  * Probe and vendor-specific initialization routine for SB-9801 boards
1537  */
1538 static int
1539 ed_probe_SB98(dev, port_rid, flags)
1540         device_t dev;
1541         int port_rid;
1542         int flags;
1543 {
1544         struct ed_softc *sc = device_get_softc(dev);
1545         int error;
1546         u_char tmp;
1547         u_long conf_irq, junk;
1548
1549         error = ed98_alloc_port(dev, port_rid);
1550         if (error) {
1551                 return (error);
1552         }
1553
1554         sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
1555         sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
1556
1557         /* Check I/O address. 00d[02468ace] are allowed. */
1558         if ((rman_get_start(sc->port_res) & ~0x000e) != 0x00d0) {
1559 #ifdef DIAGNOSTIC
1560                 device_printf(dev, "Invalid i/o port configuration (0x%lx) "
1561                         "must be %s for %s\n", rman_get_start(sc->port_res),
1562                         "0xd?", "SB9801");
1563 #endif
1564                 return (ENXIO);
1565         }
1566
1567         /* Write I/O port address and read 4 times */
1568         outb(ED_SB98_IO_INHIBIT, rman_get_start(sc->port_res) & 0xff);
1569         (void) inb(ED_SB98_IO_INHIBIT); DELAY(300);
1570         (void) inb(ED_SB98_IO_INHIBIT); DELAY(300);
1571         (void) inb(ED_SB98_IO_INHIBIT); DELAY(300);
1572         (void) inb(ED_SB98_IO_INHIBIT); DELAY(300);
1573
1574         /*
1575          * Check IRQ. Soliton SB-9801 only allows a choice of
1576          * irq 3,5,6,12
1577          */
1578         error = bus_get_resource(dev, SYS_RES_IRQ, 0,
1579                                  &conf_irq, &junk);
1580         if (error) {
1581                 return (error);
1582         }
1583
1584         switch (conf_irq) {
1585         case 3:
1586                 tmp = ED_SB98_CFG_IRQ3;
1587                 break;
1588         case 5:
1589                 tmp = ED_SB98_CFG_IRQ5;
1590                 break;
1591         case 6:
1592                 tmp = ED_SB98_CFG_IRQ6;
1593                 break;
1594         case 12:
1595                 tmp = ED_SB98_CFG_IRQ12;
1596                 break;
1597         default:
1598                 device_printf(dev, "Invalid irq configuration (%ld) must be "
1599                         "%s for %s\n", conf_irq, "3,5,6,12", "SB9801");
1600                 return (ENXIO);
1601         }
1602
1603         if (flags & ED_FLAGS_DISABLE_TRANCEIVER) {
1604                 tmp |= ED_SB98_CFG_ALTPORT;
1605         }
1606         ed_asic_outb(sc, ED_SB98_CFG, ED_SB98_CFG_ENABLE | tmp);
1607         ed_asic_outb(sc, ED_SB98_POLARITY, 0x01);
1608
1609         /* Reset the board. */
1610         ed_asic_outb(sc, ED_NOVELL_RESET, 0x7a);
1611         DELAY(300);
1612         ed_asic_outb(sc, ED_NOVELL_RESET, 0x79);
1613         DELAY(300);
1614
1615         /*
1616          * This is needed because some NE clones apparently don't reset the
1617          * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX
1618          * - this makes the probe invasive! ...Done against my better
1619          * judgement. -DLG
1620          */
1621         ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
1622         DELAY(5000);
1623
1624         /* Make sure that we really have an 8390 based board */
1625         if (!ed98_probe_generic8390(sc)) {
1626                 return (ENXIO);
1627         }
1628
1629         /* Test memory via PIO */
1630         sc->cr_proto = ED_CR_RD2;
1631         if (!ed_pio_testmem(sc, 16384, 1, flags)) {
1632                 return (ENXIO);
1633         }
1634
1635         /* This looks like an SB9801 board. */
1636         sc->type_str = "SB9801";
1637
1638         /* Get station address */
1639         ed_get_SB98(sc);
1640
1641         /* clear any pending interrupts that might have occurred above */
1642         ed_nic_outb(sc, ED_P0_ISR, 0xff);
1643
1644         return (0);
1645 }
1646
1647 /*
1648  * Test the ability to read and write to the NIC memory.
1649  */
1650 static int
1651 ed_pio_testmem(sc, page_offset, isa16bit, flags)
1652         struct ed_softc *sc;
1653         int page_offset;
1654         int isa16bit;
1655         int flags;
1656 {
1657         u_long memsize;
1658         static char test_pattern[32] = "THIS is A memory TEST pattern";
1659         char test_buffer[32];
1660 #ifdef DIAGNOSTIC
1661         int page_end;
1662 #endif
1663
1664         sc->vendor = ED_VENDOR_NOVELL;
1665         sc->mem_shared = 0;
1666         sc->isa16bit = isa16bit;
1667
1668         /* 8k of memory plus an additional 8k if 16bit */
1669         memsize = (isa16bit ? 16384 : 8192);
1670
1671         /*
1672          * This prevents packets from being stored in the NIC memory when the
1673          * readmem routine turns on the start bit in the CR.
1674          */
1675         ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON);
1676
1677         /* Initialize DCR for byte/word operations */
1678         if (isa16bit) {
1679                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
1680         } else {
1681                 ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
1682         }
1683         ed_nic_outb(sc, ED_P0_PSTART, page_offset / ED_PAGE_SIZE);
1684         ed_nic_outb(sc, ED_P0_PSTOP, (page_offset + memsize) / ED_PAGE_SIZE);
1685 #ifdef ED_DEBUG
1686         printf("ed?: ed_pio_testmem: page start=%x, end=%x",
1687                       page_offset, page_offset + memsize);
1688 #endif
1689
1690         /*
1691          * Write a test pattern. If this fails, then we don't know
1692          * what this board is.
1693          */
1694         ed_pio_writemem(sc, test_pattern, page_offset, sizeof(test_pattern));
1695         ed_pio_readmem(sc, page_offset, test_buffer, sizeof(test_pattern));
1696
1697         if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
1698 #ifdef ED_DEBUG
1699                 printf("ed?: ed_pio_testmem: bcmp(page %x) NG",
1700                               page_offset);
1701 #endif
1702                 return (0);
1703         }
1704
1705 #ifdef DIAGNOSTIC
1706         /* Check the bottom. */
1707         page_end = page_offset + memsize - ED_PAGE_SIZE;
1708         ed_pio_writemem(sc, test_pattern, page_end, sizeof(test_pattern));
1709         ed_pio_readmem(sc, page_end, test_buffer, sizeof(test_pattern));
1710
1711         if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
1712 #ifdef ED_DEBUG
1713                 printf("ed?: ed_pio_testmem: bcmp(page %x) NG",
1714                               page_end);
1715 #endif
1716                 return (0);
1717         }
1718 #endif
1719         sc->mem_size = memsize;
1720         sc->mem_start = (char *) page_offset;
1721         sc->mem_end   = sc->mem_start + memsize;
1722         sc->tx_page_start = page_offset / ED_PAGE_SIZE;
1723
1724         /*
1725          * Use one xmit buffer if < 16k, two buffers otherwise (if not told
1726          * otherwise).
1727          */
1728         if ((memsize < 16384) || (flags & ED_FLAGS_NO_MULTI_BUFFERING)) {
1729                 sc->txb_cnt = 1;
1730         } else {
1731                 sc->txb_cnt = 2;
1732         }
1733
1734         sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
1735         sc->rec_page_stop  = sc->tx_page_start + memsize / ED_PAGE_SIZE;
1736
1737         sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
1738
1739         return (1);
1740 }
1741 #endif  /* PC98 */
1742
1743 static device_method_t ed_isa_methods[] = {
1744         /* Device interface */
1745         DEVMETHOD(device_probe,         ed_isa_probe),
1746         DEVMETHOD(device_attach,        ed_isa_attach),
1747
1748         { 0, 0 }
1749 };
1750
1751 static driver_t ed_isa_driver = {
1752         "ed",
1753         ed_isa_methods,
1754         sizeof(struct ed_softc)
1755 };
1756
1757 DRIVER_MODULE(if_ed, isa, ed_isa_driver, ed_devclass, 0, 0);
1758 MODULE_DEPEND(if_ed, isa, 1, 1, 1);
1759 MODULE_DEPEND(if_ed, ether, 1, 1, 1);