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