ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / ne2k_cbus.c
1 /*
2
3   ne2k_cbus.c: A driver for the NE2000 like ethernet on NEC PC-9800.
4
5         This is a copy of the 2.5.66 Linux ISA NE2000 driver "ne.c" 
6         (Donald Becker/Paul Gortmaker) with the NEC PC-9800 specific
7         changes added by Osamu Tomita. 
8
9 From ne.c:
10 -----------
11     Copyright 1993 United States Government as represented by the
12     Director, National Security Agency.
13
14     This software may be used and distributed according to the terms
15     of the GNU General Public License, incorporated herein by reference.
16 -----------
17
18 */
19
20 /* Routines for the NatSemi-based designs (NE[12]000). */
21
22 static const char version[] =
23 "ne2k_cbus.c:v1.0 3/24/03 Osamu Tomita\n";
24
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/isapnp.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34
35 #include <asm/system.h>
36 #include <asm/io.h>
37
38 #include "8390.h"
39
40 /* Some defines that people can play with if so inclined. */
41
42 /* Do we support clones that don't adhere to 14,15 of the SAprom ? */
43 #define SUPPORT_NE_BAD_CLONES
44
45 /* Do we perform extra sanity checks on stuff ? */
46 /* #define NE_SANITY_CHECK */
47
48 /* Do we implement the read before write bugfix ? */
49 /* #define NE_RW_BUGFIX */
50
51 /* Do we have a non std. amount of memory? (in units of 256 byte pages) */
52 /* #define PACKETBUF_MEMSIZE    0x40 */
53
54 #ifdef SUPPORT_NE_BAD_CLONES
55 /* A list of bad clones that we none-the-less recognize. */
56 static struct { const char *name8, *name16; unsigned char SAprefix[4];}
57 bad_clone_list[] __initdata = {
58     {"LA/T-98?", "LA/T-98", {0x00, 0xa0, 0xb0}},        /* I/O Data */
59     {"EGY-98?", "EGY-98", {0x00, 0x40, 0x26}},          /* Melco EGY98 */
60     {"ICM?", "ICM-27xx-ET", {0x00, 0x80, 0xc8}},        /* ICM IF-27xx-ET */
61     {"CNET-98/EL?", "CNET(98)E/L", {0x00, 0x80, 0x4C}}, /* Contec CNET-98/EL */
62     {0,}
63 };
64 #endif
65
66 /* ---- No user-serviceable parts below ---- */
67
68 #define NE_BASE  (dev->base_addr)
69 #define NE_CMD          EI_SHIFT(0x00)
70 #define NE_DATAPORT     EI_SHIFT(0x10)  /* NatSemi-defined port window offset. */
71 #define NE_RESET        EI_SHIFT(0x1f) /* Issue a read to reset, a write to clear. */
72 #define NE_IO_EXTENT    0x20
73
74 #define NE1SM_START_PG  0x20    /* First page of TX buffer */
75 #define NE1SM_STOP_PG   0x40    /* Last page +1 of RX ring */
76 #define NESM_START_PG   0x40    /* First page of TX buffer */
77 #define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
78
79 #include "ne2k_cbus.h"
80
81 static int ne_probe1(struct net_device *dev, int ioaddr);
82 static int ne_open(struct net_device *dev);
83 static int ne_close(struct net_device *dev);
84
85 static void ne_reset_8390(struct net_device *dev);
86 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
87                           int ring_page);
88 static void ne_block_input(struct net_device *dev, int count,
89                           struct sk_buff *skb, int ring_offset);
90 static void ne_block_output(struct net_device *dev, const int count,
91                 const unsigned char *buf, const int start_page);
92
93 \f
94 /*  Probe for various non-shared-memory ethercards.
95
96    NEx000-clone boards have a Station Address PROM (SAPROM) in the packet
97    buffer memory space.  NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of
98    the SAPROM, while other supposed NE2000 clones must be detected by their
99    SA prefix.
100
101    Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
102    mode results in doubled values, which can be detected and compensated for.
103
104    The probe is also responsible for initializing the card and filling
105    in the 'dev' and 'ei_status' structures.
106
107    We use the minimum memory size for some ethercard product lines, iff we can't
108    distinguish models.  You can increase the packet buffer size by setting
109    PACKETBUF_MEMSIZE.  Reported Cabletron packet buffer locations are:
110         E1010   starts at 0x100 and ends at 0x2000.
111         E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory")
112         E2010    starts at 0x100 and ends at 0x4000.
113         E2010-x starts at 0x100 and ends at 0xffff.  */
114
115 static int __init do_ne_probe(struct net_device *dev)
116 {
117         unsigned int base_addr = dev->base_addr;
118         int irq = dev->irq;
119
120         SET_MODULE_OWNER(dev);
121
122         if (ei_debug > 2)
123                 printk(KERN_DEBUG "ne_probe(): entered.\n");
124
125         /* If CONFIG_NET_CBUS,
126            we need dev->priv->reg_offset BEFORE to probe */
127         if (ne2k_cbus_init(dev) != 0)
128                 return -ENOMEM;
129
130         /* First check any supplied i/o locations. User knows best. <cough> */
131         if (base_addr > 0) {
132                 int result;
133                 const struct ne2k_cbus_hwinfo *hw = ne2k_cbus_get_hwinfo((int)(dev->mem_start & NE2K_CBUS_HARDWARE_TYPE_MASK));
134
135                 if (ei_debug > 2)
136                         printk(KERN_DEBUG "ne_probe(): call ne_probe_cbus(base_addr=0x%x)\n", base_addr);
137
138                 result = ne_probe_cbus(dev, hw, base_addr, irq);
139                 if (result != 0)
140                         ne2k_cbus_destroy(dev);
141
142                 return result;
143         }
144
145         if (ei_debug > 2)
146                 printk(KERN_DEBUG "ne_probe(): base_addr is not specified.\n");
147
148 #ifndef MODULE
149         /* Last resort. The semi-risky C-Bus auto-probe. */
150         if (ei_debug > 2)
151                 printk(KERN_DEBUG "ne_probe(): auto-probe start.\n");
152
153         {
154                 const struct ne2k_cbus_hwinfo *hw = ne2k_cbus_get_hwinfo((int)(dev->mem_start & NE2K_CBUS_HARDWARE_TYPE_MASK));
155
156                 if (hw && hw->hwtype) {
157                         const unsigned short *plist;
158                         for (plist = hw->portlist; *plist; plist++)
159                                 if (ne_probe_cbus(dev, hw, *plist, irq) == 0)
160                                         return 0;
161                 } else {
162                         for (hw = &ne2k_cbus_hwinfo_list[0]; hw->hwtype; hw++) {
163                                 const unsigned short *plist;
164                                 for (plist = hw->portlist; *plist; plist++)
165                                         if (ne_probe_cbus(dev, hw, *plist, irq) == 0)
166                                                 return 0;
167                         }
168                 }
169         }
170 #endif
171
172         ne2k_cbus_destroy(dev);
173
174         return -ENODEV;
175 }
176
177 static void cleanup_card(struct net_device *dev)
178 {
179         const struct ne2k_cbus_region *rlist;
180         const struct ne2k_cbus_hwinfo *hw = ne2k_cbus_get_hwinfo((int)(dev->mem_start & NE2K_CBUS_HARDWARE_TYPE_MASK));
181
182         free_irq(dev->irq, dev);
183         for (rlist = hw->regionlist; rlist->range; rlist++) {
184                 release_region(dev->base_addr + rlist->start,
185                                 rlist->range);
186         }
187         ne2k_cbus_destroy(dev);
188 }
189
190 struct net_device * __init ne_probe(int unit)
191 {
192         struct net_device *dev = alloc_ei_netdev();
193         int err;
194
195         if (!dev)
196                 return ERR_PTR(-ENOMEM);
197
198         sprintf(dev->name, "eth%d", unit);
199         netdev_boot_setup_check(dev);
200
201         err = do_ne_probe(dev);
202         if (err)
203                 goto out;
204         err = register_netdev(dev);
205         if (err)
206                 goto out1;
207         return dev;
208 out1:
209         cleanup_card(dev);
210 out:
211         free_netdev(dev);
212         return ERR_PTR(err);
213 }
214
215 static int __init ne_probe_cbus(struct net_device *dev, const struct ne2k_cbus_hwinfo *hw, int ioaddr, int irq)
216 {
217         if (ei_debug > 2)
218                 printk(KERN_DEBUG "ne_probe_cbus(): entered. (called from %p)\n",
219                        __builtin_return_address(0));
220
221         if (hw && hw->hwtype) {
222                 ne2k_cbus_set_hwtype(dev, hw, ioaddr);
223                 dev->irq = irq;
224                 return ne_probe1(dev, ioaddr);
225         } else {
226                 /* auto detect */
227
228                 printk(KERN_DEBUG "ne_probe_cbus(): try to determine hardware types.\n");
229                 for (hw = &ne2k_cbus_hwinfo_list[0]; hw->hwtype; hw++) {
230                         ne2k_cbus_set_hwtype(dev, hw, ioaddr);
231                         dev->irq = irq;
232                         if (ne_probe1(dev, ioaddr) == 0)
233                                 return 0;
234                 }
235         }
236         return -ENODEV;
237 }
238
239 static int __init ne_probe1(struct net_device *dev, int ioaddr)
240 {
241         int i;
242         unsigned char SA_prom[32];
243         int wordlength = 2;
244         const char *name = NULL;
245         int start_page, stop_page;
246         int neX000, bad_card;
247         int reg0, ret;
248         static unsigned version_printed;
249         const struct ne2k_cbus_region *rlist;
250         const struct ne2k_cbus_hwinfo *hw = ne2k_cbus_get_hwinfo((int)(dev->mem_start & NE2K_CBUS_HARDWARE_TYPE_MASK));
251         struct ei_device *ei_local = (struct ei_device *)(dev->priv);
252
253 #ifdef CONFIG_NE2K_CBUS_CNET98EL
254         if (hw->hwtype == NE2K_CBUS_HARDWARE_TYPE_CNET98EL) {
255                 outb_p(0, CONFIG_NE2K_CBUS_CNET98EL_IO_BASE);
256                 /* udelay(5000);        */
257                 outb_p(1, CONFIG_NE2K_CBUS_CNET98EL_IO_BASE);
258                 /* udelay(5000);        */
259                 outb_p((ioaddr & 0xf000) >> 8 | 0x08 | 0x01, CONFIG_NE2K_CBUS_CNET98EL_IO_BASE + 2);
260                 /* udelay(5000); */
261         }
262 #endif
263
264         for (rlist = hw->regionlist; rlist->range; rlist++)
265                 if (!request_region(ioaddr + rlist->start,
266                                         rlist->range, dev->name)) {
267                         ret = -EBUSY;
268                         goto err_out;
269                 }
270
271         reg0 = inb_p(ioaddr + EI_SHIFT(0));
272         if (reg0 == 0xFF) {
273                 ret = -ENODEV;
274                 goto err_out;
275         }
276
277         /* Do a preliminary verification that we have a 8390. */
278 #ifdef CONFIG_NE2K_CBUS_CNET98EL
279         if (hw->hwtype != NE2K_CBUS_HARDWARE_TYPE_CNET98EL)
280 #endif
281         {
282                 int regd;
283                 outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
284                 regd = inb_p(ioaddr + EI_SHIFT(0x0d));
285                 outb_p(0xff, ioaddr + EI_SHIFT(0x0d));
286                 outb_p(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
287                 inb_p(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
288                 if (inb_p(ioaddr + EN0_COUNTER0) != 0) {
289                         outb_p(reg0, ioaddr);
290                         outb_p(regd, ioaddr + EI_SHIFT(0x0d));  /* Restore the old values. */
291                         ret = -ENODEV;
292                         goto err_out;
293                 }
294         }
295
296         if (ei_debug  &&  version_printed++ == 0)
297                 printk(KERN_INFO "%s", version);
298
299         printk(KERN_INFO "NE*000 ethercard probe at %#3x:", ioaddr);
300
301         /* A user with a poor card that fails to ack the reset, or that
302            does not have a valid 0x57,0x57 signature can still use this
303            without having to recompile. Specifying an i/o address along
304            with an otherwise unused dev->mem_end value of "0xBAD" will
305            cause the driver to skip these parts of the probe. */
306
307         bad_card = ((dev->base_addr != 0) && (dev->mem_end == 0xbad));
308
309         /* Reset card. Who knows what dain-bramaged state it was left in. */
310
311         {
312                 unsigned long reset_start_time = jiffies;
313
314                 /* derived from CNET98EL-patch for bad clones */
315                 outb_p(E8390_NODMA | E8390_STOP, ioaddr + E8390_CMD);
316
317                 /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
318                 outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
319
320                 while ((inb_p(ioaddr + EN0_ISR) & ENISR_RESET) == 0)
321                 if (jiffies - reset_start_time > 2*HZ/100) {
322                         if (bad_card) {
323                                 printk(" (warning: no reset ack)");
324                                 break;
325                         } else {
326                                 printk(" not found (no reset ack).\n");
327                                 ret = -ENODEV;
328                                 goto err_out;
329                         }
330                 }
331
332                 outb_p(0xff, ioaddr + EN0_ISR);         /* Ack all intr. */
333         }
334
335 #ifdef CONFIG_NE2K_CBUS_CNET98EL
336         if (hw->hwtype == NE2K_CBUS_HARDWARE_TYPE_CNET98EL) {
337                 static const char pat[32] ="AbcdeFghijKlmnoPqrstUvwxyZ789012";
338                 char buf[32];
339                 int maxwait = 200;
340
341                 if (ei_debug > 2)
342                         printk(" [CNET98EL-specific initialize...");
343                 outb_p(E8390_NODMA | E8390_STOP, ioaddr + E8390_CMD); /* 0x20|0x1 */
344                 ret = -ENODEV;
345                 i = inb(ioaddr);
346                 if ((i & ~0x2) != (0x20 | 0x01))
347                         goto err_out;
348                 if ((inb(ioaddr + 0x7) & 0x80) != 0x80)
349                         goto err_out;
350                 outb_p(E8390_RXOFF, ioaddr + EN0_RXCR); /* out(ioaddr+0xc, 0x20) */
351                 /* outb_p(ENDCFG_WTS|ENDCFG_FT1|ENDCFG_LS, ioaddr+EN0_DCFG); */
352                 outb_p(ENDCFG_WTS | 0x48, ioaddr + EN0_DCFG); /* 0x49 */
353                 outb_p(CNET98EL_START_PG, ioaddr + EN0_STARTPG);
354                 outb_p(CNET98EL_STOP_PG, ioaddr + EN0_STOPPG);
355                 if (ei_debug > 2)
356                         printk("memory check");
357                 for (i = 0; i < 65536; i += 1024) {
358                         if (ei_debug > 2)
359                                 printk(" %04x", i);
360                         ne2k_cbus_writemem(dev, ioaddr, i, pat, 32);
361                         while (((inb(ioaddr + EN0_ISR) & ENISR_RDC) != ENISR_RDC) && --maxwait)
362                                 ;
363                         ne2k_cbus_readmem(dev, ioaddr, i, buf, 32);
364                         if (memcmp(pat, buf, 32)) {
365                                 if (ei_debug > 2)
366                                         printk(" failed.");
367                                 break;
368                         }
369                 }
370                 if (i != 16384) {
371                         if (ei_debug > 2)
372                                 printk("] ");
373                         printk("memory failure at %x\n", i);
374                         goto err_out;
375                 }
376                 if (ei_debug > 2)
377                         printk(" good...");
378                 if (!dev->irq) {
379                         if (ei_debug > 2)
380                                 printk("] ");
381                         printk("IRQ must be specified for C-NET(98)E/L. probe failed.\n");
382                         goto err_out;
383                 }
384                 outb((dev->irq > 5) ? (dev->irq & 4):(dev->irq >> 1), ioaddr + (0x2 | 0x400));
385                 outb(0x7e, ioaddr + (0x4 | 0x400));
386                 ne2k_cbus_readmem(dev, ioaddr, 16384, SA_prom, 32);
387                 outb(0xff, ioaddr + EN0_ISR);
388                 if (ei_debug > 2)
389                         printk("done]");
390         } else
391 #endif /* CONFIG_NE2K_CBUS_CNET98EL */
392         /* Read the 16 bytes of station address PROM.
393            We must first initialize registers, similar to NS8390_init(eifdev, 0).
394            We can't reliably read the SAPROM address without this.
395            (I learned the hard way!). */
396         {
397                 struct {unsigned char value; unsigned short offset;} program_seq[] = 
398                 {
399                         {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
400                         /* NEC PC-9800: some board can only handle word-wide access? */
401                         {0x48 | ENDCFG_WTS,     EN0_DCFG},      /* Set word-wide (0x48) access. */
402                         {16384 / 256, EN0_STARTPG},
403                         {32768 / 256, EN0_STOPPG},
404                         {0x00,  EN0_RCNTLO},    /* Clear the count regs. */
405                         {0x00,  EN0_RCNTHI},
406                         {0x00,  EN0_IMR},       /* Mask completion irq. */
407                         {0xFF,  EN0_ISR},
408                         {E8390_RXOFF, EN0_RXCR},        /* 0x20  Set to monitor */
409                         {E8390_TXOFF, EN0_TXCR},        /* 0x02  and loopback mode. */
410                         {32,    EN0_RCNTLO},
411                         {0x00,  EN0_RCNTHI},
412                         {0x00,  EN0_RSARLO},    /* DMA starting at 0x0000. */
413                         {0x00,  EN0_RSARHI},
414                         {E8390_RREAD+E8390_START, E8390_CMD},
415                 };
416
417                 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
418                         outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
419                 insw(ioaddr + NE_DATAPORT, SA_prom, 32 >> 1);
420
421         }
422
423         if (wordlength == 2)
424         {
425                 for (i = 0; i < 16; i++)
426                         SA_prom[i] = SA_prom[i+i];
427                 start_page = NESM_START_PG;
428                 stop_page = NESM_STOP_PG;
429 #ifdef CONFIG_NE2K_CBUS_CNET98EL
430                 if (hw->hwtype == NE2K_CBUS_HARDWARE_TYPE_CNET98EL) {
431                         start_page = CNET98EL_START_PG;
432                         stop_page = CNET98EL_STOP_PG;
433                 }
434 #endif
435         } else {
436                 start_page = NE1SM_START_PG;
437                 stop_page = NE1SM_STOP_PG;
438         }
439
440         neX000 = (SA_prom[14] == 0x57  &&  SA_prom[15] == 0x57);
441         if (neX000) {
442                 name = "C-Bus-NE2K-compat";
443         }
444         else
445         {
446 #ifdef SUPPORT_NE_BAD_CLONES
447                 /* Ack!  Well, there might be a *bad* NE*000 clone there.
448                    Check for total bogus addresses. */
449                 for (i = 0; bad_clone_list[i].name8; i++)
450                 {
451                         if (SA_prom[0] == bad_clone_list[i].SAprefix[0] &&
452                                 SA_prom[1] == bad_clone_list[i].SAprefix[1] &&
453                                 SA_prom[2] == bad_clone_list[i].SAprefix[2])
454                         {
455                                 if (wordlength == 2)
456                                 {
457                                         name = bad_clone_list[i].name16;
458                                 } else {
459                                         name = bad_clone_list[i].name8;
460                                 }
461                                 break;
462                         }
463                 }
464                 if (bad_clone_list[i].name8 == NULL)
465                 {
466                         printk(" not found (invalid signature %2.2x %2.2x).\n",
467                                 SA_prom[14], SA_prom[15]);
468                         ret = -ENXIO;
469                         goto err_out;
470                 }
471 #else
472                 printk(" not found.\n");
473                 ret = -ENXIO;
474                 goto err_out;
475 #endif
476         }
477
478         if (dev->irq < 2)
479         {
480                 unsigned long cookie = probe_irq_on();
481                 outb_p(0x50, ioaddr + EN0_IMR); /* Enable one interrupt. */
482                 outb_p(0x00, ioaddr + EN0_RCNTLO);
483                 outb_p(0x00, ioaddr + EN0_RCNTHI);
484                 outb_p(E8390_RREAD+E8390_START, ioaddr); /* Trigger it... */
485                 mdelay(10);             /* wait 10ms for interrupt to propagate */
486                 outb_p(0x00, ioaddr + EN0_IMR);                 /* Mask it again. */
487                 dev->irq = probe_irq_off(cookie);
488                 if (ei_debug > 2)
489                         printk(" autoirq is %d\n", dev->irq);
490         } else if (dev->irq == 7)
491                 /* Fixup for users that don't know that IRQ 7 is really IRQ 11,
492                    or don't know which one to set. */
493                 dev->irq = 11;
494
495         if (! dev->irq) {
496                 printk(" failed to detect IRQ line.\n");
497                 ret = -EAGAIN;
498                 goto err_out;
499         }
500
501         /* Snarf the interrupt now.  There's no point in waiting since we cannot
502            share and the board will usually be enabled. */
503         ret = request_irq(dev->irq, ei_interrupt, 0, name, dev);
504         if (ret) {
505                 printk (" unable to get IRQ %d (errno=%d).\n", dev->irq, ret);
506                 goto err_out_kfree;
507         }
508
509         dev->base_addr = ioaddr;
510
511         for(i = 0; i < ETHER_ADDR_LEN; i++) {
512                 printk(" %2.2x", SA_prom[i]);
513                 dev->dev_addr[i] = SA_prom[i];
514         }
515
516         printk("\n%s: %s found at %#x, hardware type %d(%s), using IRQ %d.\n",
517                    dev->name, name, ioaddr, hw->hwtype, hw->hwident, dev->irq);
518
519         ei_status.name = name;
520         ei_status.tx_start_page = start_page;
521         ei_status.stop_page = stop_page;
522         ei_status.word16 = (wordlength == 2);
523
524         ei_status.rx_start_page = start_page + TX_PAGES;
525 #ifdef PACKETBUF_MEMSIZE
526          /* Allow the packet buffer size to be overridden by know-it-alls. */
527         ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
528 #endif
529
530         ei_status.reset_8390 = &ne_reset_8390;
531         ei_status.block_input = &ne_block_input;
532         ei_status.block_output = &ne_block_output;
533         ei_status.get_8390_hdr = &ne_get_8390_hdr;
534         ei_status.priv = 0;
535         dev->open = &ne_open;
536         dev->stop = &ne_close;
537 #ifdef CONFIG_NET_POLL_CONTROLLER
538         dev->poll_controller = ei_poll;
539 #endif
540         NS8390_init(dev, 0);
541         return 0;
542
543 err_out_kfree:
544         ne2k_cbus_destroy(dev);
545 err_out:
546         while (rlist > hw->regionlist) {
547                 rlist --;
548                 release_region(ioaddr + rlist->start, rlist->range);
549         }
550         return ret;
551 }
552
553 static int ne_open(struct net_device *dev)
554 {
555         ei_open(dev);
556         return 0;
557 }
558
559 static int ne_close(struct net_device *dev)
560 {
561         if (ei_debug > 1)
562                 printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
563         ei_close(dev);
564         return 0;
565 }
566
567 /* Hard reset the card.  This used to pause for the same period that a
568    8390 reset command required, but that shouldn't be necessary. */
569
570 static void ne_reset_8390(struct net_device *dev)
571 {
572         unsigned long reset_start_time = jiffies;
573         struct ei_device *ei_local = (struct ei_device *)(dev->priv);
574
575         if (ei_debug > 1)
576                 printk(KERN_DEBUG "resetting the 8390 t=%ld...", jiffies);
577
578         /* derived from CNET98EL-patch for bad clones... */
579         outb_p(E8390_NODMA | E8390_STOP, NE_BASE + E8390_CMD);  /* 0x20 | 0x1 */
580
581         /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
582         outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
583
584         ei_status.txing = 0;
585         ei_status.dmaing = 0;
586
587         /* This check _should_not_ be necessary, omit eventually. */
588         while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
589                 if (jiffies - reset_start_time > 2*HZ/100) {
590                         printk(KERN_WARNING "%s: ne_reset_8390() did not complete.\n", dev->name);
591                         break;
592                 }
593         outb_p(ENISR_RESET, NE_BASE + EN0_ISR); /* Ack intr. */
594 }
595
596 /* Grab the 8390 specific header. Similar to the block_input routine, but
597    we don't need to be concerned with ring wrap as the header will be at
598    the start of a page, so we optimize accordingly. */
599
600 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
601 {
602         int nic_base = dev->base_addr;
603         struct ei_device *ei_local = (struct ei_device *)(dev->priv);
604
605         /* This *shouldn't* happen. If it does, it's the last thing you'll see */
606
607         if (ei_status.dmaing)
608         {
609                 printk(KERN_EMERG "%s: DMAing conflict in ne_get_8390_hdr "
610                         "[DMAstat:%d][irqlock:%d].\n",
611                         dev->name, ei_status.dmaing, ei_status.irqlock);
612                 return;
613         }
614
615         ei_status.dmaing |= 0x01;
616         outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
617         outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
618         outb_p(0, nic_base + EN0_RCNTHI);
619         outb_p(0, nic_base + EN0_RSARLO);               /* On page boundary */
620         outb_p(ring_page, nic_base + EN0_RSARHI);
621         outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
622
623         if (ei_status.word16)
624                 insw(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
625         else
626                 insb(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
627
628         outb_p(ENISR_RDC, nic_base + EN0_ISR);  /* Ack intr. */
629         ei_status.dmaing &= ~0x01;
630
631         le16_to_cpus(&hdr->count);
632 }
633
634 /* Block input and output, similar to the Crynwr packet driver.  If you
635    are porting to a new ethercard, look at the packet driver source for hints.
636    The NEx000 doesn't share the on-board packet memory -- you have to put
637    the packet out through the "remote DMA" dataport using outb. */
638
639 static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
640 {
641 #ifdef NE_SANITY_CHECK
642         int xfer_count = count;
643 #endif
644         int nic_base = dev->base_addr;
645         char *buf = skb->data;
646         struct ei_device *ei_local = (struct ei_device *)(dev->priv);
647
648         /* This *shouldn't* happen. If it does, it's the last thing you'll see */
649         if (ei_status.dmaing)
650         {
651                 printk(KERN_EMERG "%s: DMAing conflict in ne_block_input "
652                         "[DMAstat:%d][irqlock:%d].\n",
653                         dev->name, ei_status.dmaing, ei_status.irqlock);
654                 return;
655         }
656         ei_status.dmaing |= 0x01;
657
658         /* round up count to a word (derived from ICM-patch) */
659         count = (count + 1) & ~1;
660
661         outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
662         outb_p(count & 0xff, nic_base + EN0_RCNTLO);
663         outb_p(count >> 8, nic_base + EN0_RCNTHI);
664         outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
665         outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
666         outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
667         if (ei_status.word16)
668         {
669                 insw(NE_BASE + NE_DATAPORT,buf,count>>1);
670                 if (count & 0x01)
671                 {
672                         buf[count-1] = inb(NE_BASE + NE_DATAPORT);
673 #ifdef NE_SANITY_CHECK
674                         xfer_count++;
675 #endif
676                 }
677         } else {
678                 insb(NE_BASE + NE_DATAPORT, buf, count);
679         }
680
681 #ifdef NE_SANITY_CHECK
682         /* This was for the ALPHA version only, but enough people have
683            been encountering problems so it is still here.  If you see
684            this message you either 1) have a slightly incompatible clone
685            or 2) have noise/speed problems with your bus. */
686
687         if (ei_debug > 1)
688         {
689                 /* DMA termination address check... */
690                 int addr, tries = 20;
691                 do {
692                         /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
693                            -- it's broken for Rx on some cards! */
694                         int high = inb_p(nic_base + EN0_RSARHI);
695                         int low = inb_p(nic_base + EN0_RSARLO);
696                         addr = (high << 8) + low;
697                         if (((ring_offset + xfer_count) & 0xff) == low)
698                                 break;
699                 } while (--tries > 0);
700                 if (tries <= 0)
701                         printk(KERN_WARNING "%s: RX transfer address mismatch,"
702                                 "%#4.4x (expected) vs. %#4.4x (actual).\n",
703                                 dev->name, ring_offset + xfer_count, addr);
704         }
705 #endif
706         outb_p(ENISR_RDC, nic_base + EN0_ISR);  /* Ack intr. */
707         ei_status.dmaing &= ~0x01;
708 }
709
710 static void ne_block_output(struct net_device *dev, int count,
711                 const unsigned char *buf, const int start_page)
712 {
713         int nic_base = NE_BASE;
714         unsigned long dma_start;
715 #ifdef NE_SANITY_CHECK
716         int retries = 0;
717 #endif
718         struct ei_device *ei_local = (struct ei_device *)(dev->priv);
719
720         /* Round the count up for word writes.  Do we need to do this?
721            What effect will an odd byte count have on the 8390?
722            I should check someday. */
723
724         if (ei_status.word16 && (count & 0x01))
725                 count++;
726
727         /* This *shouldn't* happen. If it does, it's the last thing you'll see */
728         if (ei_status.dmaing)
729         {
730                 printk(KERN_EMERG "%s: DMAing conflict in ne_block_output."
731                         "[DMAstat:%d][irqlock:%d]\n",
732                         dev->name, ei_status.dmaing, ei_status.irqlock);
733                 return;
734         }
735         ei_status.dmaing |= 0x01;
736         /* We should already be in page 0, but to be safe... */
737         outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
738
739 #ifdef NE_SANITY_CHECK
740 retry:
741 #endif
742
743 #ifdef NE8390_RW_BUGFIX
744         /* Handle the read-before-write bug the same way as the
745            Crynwr packet driver -- the NatSemi method doesn't work.
746            Actually this doesn't always work either, but if you have
747            problems with your NEx000 this is better than nothing! */
748
749         outb_p(0x42, nic_base + EN0_RCNTLO);
750         outb_p(0x00,   nic_base + EN0_RCNTHI);
751         outb_p(0x42, nic_base + EN0_RSARLO);
752         outb_p(0x00, nic_base + EN0_RSARHI);
753         outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
754         /* Make certain that the dummy read has occurred. */
755         udelay(6);
756 #endif
757
758         outb_p(ENISR_RDC, nic_base + EN0_ISR);
759
760         /* Now the normal output. */
761         outb_p(count & 0xff, nic_base + EN0_RCNTLO);
762         outb_p(count >> 8,   nic_base + EN0_RCNTHI);
763         outb_p(0x00, nic_base + EN0_RSARLO);
764         outb_p(start_page, nic_base + EN0_RSARHI);
765
766         outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
767         if (ei_status.word16) {
768                 outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
769         } else {
770                 outsb(NE_BASE + NE_DATAPORT, buf, count);
771         }
772
773         dma_start = jiffies;
774
775 #ifdef NE_SANITY_CHECK
776         /* This was for the ALPHA version only, but enough people have
777            been encountering problems so it is still here. */
778
779         if (ei_debug > 1)
780         {
781                 /* DMA termination address check... */
782                 int addr, tries = 20;
783                 do {
784                         int high = inb_p(nic_base + EN0_RSARHI);
785                         int low = inb_p(nic_base + EN0_RSARLO);
786                         addr = (high << 8) + low;
787                         if ((start_page << 8) + count == addr)
788                                 break;
789                 } while (--tries > 0);
790
791                 if (tries <= 0)
792                 {
793                         printk(KERN_WARNING "%s: Tx packet transfer address mismatch,"
794                                 "%#4.4x (expected) vs. %#4.4x (actual).\n",
795                                 dev->name, (start_page << 8) + count, addr);
796                         if (retries++ == 0)
797                                 goto retry;
798                 }
799         }
800 #endif
801
802         while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
803                 if (jiffies - dma_start > 2*HZ/100) {           /* 20ms */
804                         printk(KERN_WARNING "%s: timeout waiting for Tx RDC.\n", dev->name);
805                         ne_reset_8390(dev);
806                         NS8390_init(dev,1);
807                         break;
808                 }
809
810         outb_p(ENISR_RDC, nic_base + EN0_ISR);  /* Ack intr. */
811         ei_status.dmaing &= ~0x01;
812         return;
813 }
814
815 \f
816 #ifdef MODULE
817 #define MAX_NE_CARDS    4       /* Max number of NE cards per module */
818 static struct net_device *dev_ne[MAX_NE_CARDS];
819 static int io[MAX_NE_CARDS];
820 static int irq[MAX_NE_CARDS];
821 static int bad[MAX_NE_CARDS];   /* 0xbad = bad sig or no reset ack */
822 static int hwtype[MAX_NE_CARDS] = { 0, }; /* board type */
823
824 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
825 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
826 MODULE_PARM(bad, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
827 MODULE_PARM(hwtype, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
828 MODULE_PARM_DESC(io, "I/O base address(es),required");
829 MODULE_PARM_DESC(irq, "IRQ number(s)");
830 MODULE_PARM_DESC(bad, "Accept card(s) with bad signatures");
831 MODULE_PARM_DESC(hwtype, "Board type of PC-9800 C-Bus NIC");
832 MODULE_DESCRIPTION("NE1000/NE2000 PC-9800 C-bus Ethernet driver");
833 MODULE_LICENSE("GPL");
834
835 /* This is set up so that no ISA autoprobe takes place. We can't guarantee
836 that the ne2k probe is the last 8390 based probe to take place (as it
837 is at boot) and so the probe will get confused by any other 8390 cards.
838 ISA device autoprobes on a running machine are not recommended anyway. */
839
840 int init_module(void)
841 {
842         int this_dev, found = 0;
843
844         for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
845                 struct net_device *dev = alloc_ei_netdev();
846                 if (!dev)
847                         break;
848                 dev->irq = irq[this_dev];
849                 dev->mem_end = bad[this_dev];
850                 dev->base_addr = io[this_dev];
851                 dev->mem_start = hwtype[this_dev];
852                 if (do_ne_probe(dev) == 0) {
853                         if (register_netdev(dev) == 0) {
854                                 dev_ne[found++] = dev;
855                                 continue;
856                         }
857                         cleanup_card(dev);
858                 }
859                 free_netdev(dev);
860                 if (found)
861                         break;
862                 if (io[this_dev] != 0)
863                         printk(KERN_WARNING "ne2k_cbus: No NE*000 card found at i/o = %#x\n", io[this_dev]);
864                 else
865                         printk(KERN_NOTICE "ne.c: You must supply \"io=0xNNN\" value(s) for ISA cards.\n");
866                 return -ENXIO;
867         }
868         if (found)
869                 return 0;
870         return -ENODEV;
871 }
872
873 void cleanup_module(void)
874 {
875         int this_dev;
876
877         for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
878                 struct net_device *dev = dev_ne[this_dev];
879                 if (dev) {
880                         unregister_netdev(dev);
881                         cleanup_card(dev);
882                         free_netdev(dev);
883                 }
884         }
885 }
886 #endif /* MODULE */
887