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