1 /* ne-h8300.c: A NE2000 clone on H8/300 driver for linux. */
4 Written 1992-94 by Donald Becker.
6 Copyright 1993 United States Government as represented by the
7 Director, National Security Agency.
9 This software may be used and distributed according to the terms
10 of the GNU General Public License, incorporated herein by reference.
12 The author may be reached as becker@scyld.com, or C/O
13 Scyld Computing Corporation, 410 Severn Ave., Suite 210, Annapolis MD 21403
16 Yoshinori Sato <ysato@users.sourceforge.jp>
19 static const char version1[] =
20 "ne-h8300.c:v1.00 2004/04/11 ysato\n";
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
31 #include <asm/system.h>
37 /* Some defines that people can play with if so inclined. */
39 /* Do we perform extra sanity checks on stuff ? */
40 /* #define NE_SANITY_CHECK */
42 /* Do we implement the read before write bugfix ? */
43 /* #define NE_RW_BUGFIX */
45 /* Do we have a non std. amount of memory? (in units of 256 byte pages) */
46 /* #define PACKETBUF_MEMSIZE 0x40 */
48 /* A zero-terminated list of I/O addresses to be probed at boot. */
50 /* ---- No user-serviceable parts below ---- */
52 #define NE_BASE (dev->base_addr)
54 #define NE_DATAPORT (ei_status.word16?0x20:0x10) /* NatSemi-defined port window offset. */
55 #define NE_RESET (ei_status.word16?0x3f:0x1f) /* Issue a read to reset, a write to clear. */
56 #define NE_IO_EXTENT (ei_status.word16?0x40:0x20)
58 #define NESM_START_PG 0x40 /* First page of TX buffer */
59 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
61 static int ne_probe1(struct net_device *dev, int ioaddr);
63 static int ne_open(struct net_device *dev);
64 static int ne_close(struct net_device *dev);
66 static void ne_reset_8390(struct net_device *dev);
67 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
69 static void ne_block_input(struct net_device *dev, int count,
70 struct sk_buff *skb, int ring_offset);
71 static void ne_block_output(struct net_device *dev, const int count,
72 const unsigned char *buf, const int start_page);
75 static u32 reg_offset[16];
77 static int __init init_reg_offset(struct net_device *dev,unsigned long base_addr)
79 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
81 unsigned char bus_width;
83 bus_width = *(volatile unsigned char *)ABWCR;
84 bus_width &= 1 << ((base_addr >> 21) & 7);
86 for (i = 0; i < sizeof(reg_offset) / sizeof(u32); i++)
88 reg_offset[i] = i * 2 + 1;
92 ei_local->reg_offset = reg_offset;
96 static int __initdata h8300_ne_count = 0;
97 #ifdef CONFIG_H8300H_H8MAX
98 static unsigned long __initdata h8300_ne_base[] = { 0x800600 };
99 static int h8300_ne_irq[] = {EXT_IRQ4};
101 #ifdef CONFIG_H8300H_AKI3068NET
102 static unsigned long __initdata h8300_ne_base[] = { 0x200000 };
103 static int h8300_ne_irq[] = {EXT_IRQ5};
106 static inline int init_dev(struct net_device *dev)
108 if (h8300_ne_count < (sizeof(h8300_ne_base) / sizeof(unsigned long))) {
109 dev->base_addr = h8300_ne_base[h8300_ne_count];
110 dev->irq = h8300_ne_irq[h8300_ne_count];
117 /* Probe for various non-shared-memory ethercards.
119 NEx000-clone boards have a Station Address PROM (SAPROM) in the packet
120 buffer memory space. NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of
121 the SAPROM, while other supposed NE2000 clones must be detected by their
124 Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
125 mode results in doubled values, which can be detected and compensated for.
127 The probe is also responsible for initializing the card and filling
128 in the 'dev' and 'ei_status' structures.
130 We use the minimum memory size for some ethercard product lines, iff we can't
131 distinguish models. You can increase the packet buffer size by setting
132 PACKETBUF_MEMSIZE. Reported Cabletron packet buffer locations are:
133 E1010 starts at 0x100 and ends at 0x2000.
134 E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory")
135 E2010 starts at 0x100 and ends at 0x4000.
136 E2010-x starts at 0x100 and ends at 0xffff. */
138 static int __init do_ne_probe(struct net_device *dev)
140 unsigned int base_addr = dev->base_addr;
142 SET_MODULE_OWNER(dev);
144 /* First check any supplied i/o locations. User knows best. <cough> */
145 if (base_addr > 0x1ff) /* Check a single specified location. */
146 return ne_probe1(dev, base_addr);
147 else if (base_addr != 0) /* Don't probe at all. */
153 static void cleanup_card(struct net_device *dev)
155 free_irq(dev->irq, dev);
156 release_region(dev->base_addr, NE_IO_EXTENT);
159 struct net_device * __init ne_probe(int unit)
161 struct net_device *dev = alloc_ei_netdev();
165 return ERR_PTR(-ENOMEM);
168 return ERR_PTR(-ENODEV);
170 sprintf(dev->name, "eth%d", unit);
171 netdev_boot_setup_check(dev);
173 err = init_reg_offset(dev, dev->base_addr);
177 err = do_ne_probe(dev);
180 err = register_netdev(dev);
191 static int __init ne_probe1(struct net_device *dev, int ioaddr)
194 unsigned char SA_prom[16];
196 const char *name = NULL;
197 int start_page, stop_page;
199 static unsigned version_printed;
200 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
201 unsigned char bus_width;
203 if (!request_region(ioaddr, NE_IO_EXTENT, dev->name))
206 reg0 = inb_p(ioaddr);
212 /* Do a preliminary verification that we have a 8390. */
215 outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
216 regd = inb_p(ioaddr + EI_SHIFT(0x0d));
217 outb_p(0xff, ioaddr + EI_SHIFT(0x0d));
218 outb_p(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
219 inb_p(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
220 if (inb_p(ioaddr + EN0_COUNTER0) != 0) {
221 outb_p(reg0, ioaddr + EI_SHIFT(0));
222 outb_p(regd, ioaddr + EI_SHIFT(0x0d)); /* Restore the old values. */
228 if (ei_debug && version_printed++ == 0)
229 printk(KERN_INFO "%s", version1);
231 printk(KERN_INFO "NE*000 ethercard probe at %08x:", ioaddr);
233 /* Read the 16 bytes of station address PROM.
234 We must first initialize registers, similar to NS8390_init(eifdev, 0).
235 We can't reliably read the SAPROM address without this.
236 (I learned the hard way!). */
238 struct {unsigned char value, offset; } program_seq[] =
240 {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
241 {0x48, EN0_DCFG}, /* Set byte-wide (0x48) access. */
242 {0x00, EN0_RCNTLO}, /* Clear the count regs. */
244 {0x00, EN0_IMR}, /* Mask completion irq. */
246 {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */
247 {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
250 {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */
252 {E8390_RREAD+E8390_START, E8390_CMD},
255 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
256 outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
259 bus_width = *(volatile unsigned char *)ABWCR;
260 bus_width &= 1 << ((ioaddr >> 21) & 7);
261 ei_status.word16 = (bus_width == 0); /* temporary setting */
262 for(i = 0; i < 16 /*sizeof(SA_prom)*/; i++) {
263 SA_prom[i] = inb_p(ioaddr + NE_DATAPORT);
264 inb_p(ioaddr + NE_DATAPORT); /* dummy read */
267 start_page = NESM_START_PG;
268 stop_page = NESM_STOP_PG;
273 outb_p(0x49, ioaddr + EN0_DCFG);
275 /* Set up the rest of the parameters. */
276 name = (wordlength == 2) ? "NE2000" : "NE1000";
279 printk(" failed to detect IRQ line.\n");
284 /* Snarf the interrupt now. There's no point in waiting since we cannot
285 share and the board will usually be enabled. */
286 ret = request_irq(dev->irq, ei_interrupt, 0, name, dev);
288 printk (" unable to get IRQ %d (errno=%d).\n", dev->irq, ret);
292 dev->base_addr = ioaddr;
294 for(i = 0; i < ETHER_ADDR_LEN; i++) {
295 printk(" %2.2x", SA_prom[i]);
296 dev->dev_addr[i] = SA_prom[i];
299 printk("\n%s: %s found at %#x, using IRQ %d.\n",
300 dev->name, name, ioaddr, dev->irq);
302 ei_status.name = name;
303 ei_status.tx_start_page = start_page;
304 ei_status.stop_page = stop_page;
305 ei_status.word16 = (wordlength == 2);
307 ei_status.rx_start_page = start_page + TX_PAGES;
308 #ifdef PACKETBUF_MEMSIZE
309 /* Allow the packet buffer size to be overridden by know-it-alls. */
310 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
313 ei_status.reset_8390 = &ne_reset_8390;
314 ei_status.block_input = &ne_block_input;
315 ei_status.block_output = &ne_block_output;
316 ei_status.get_8390_hdr = &ne_get_8390_hdr;
318 dev->open = &ne_open;
319 dev->stop = &ne_close;
320 #ifdef CONFIG_NET_POLL_CONTROLLER
321 dev->poll_controller = ei_poll;
327 release_region(ioaddr, NE_IO_EXTENT);
331 static int ne_open(struct net_device *dev)
337 static int ne_close(struct net_device *dev)
340 printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
345 /* Hard reset the card. This used to pause for the same period that a
346 8390 reset command required, but that shouldn't be necessary. */
348 static void ne_reset_8390(struct net_device *dev)
350 unsigned long reset_start_time = jiffies;
351 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
354 printk(KERN_DEBUG "resetting the 8390 t=%ld...", jiffies);
356 /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
357 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
360 ei_status.dmaing = 0;
362 /* This check _should_not_ be necessary, omit eventually. */
363 while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
364 if (jiffies - reset_start_time > 2*HZ/100) {
365 printk(KERN_WARNING "%s: ne_reset_8390() did not complete.\n", dev->name);
368 outb_p(ENISR_RESET, NE_BASE + EN0_ISR); /* Ack intr. */
371 /* Grab the 8390 specific header. Similar to the block_input routine, but
372 we don't need to be concerned with ring wrap as the header will be at
373 the start of a page, so we optimize accordingly. */
375 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
377 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
378 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
380 if (ei_status.dmaing)
382 printk(KERN_EMERG "%s: DMAing conflict in ne_get_8390_hdr "
383 "[DMAstat:%d][irqlock:%d].\n",
384 dev->name, ei_status.dmaing, ei_status.irqlock);
388 ei_status.dmaing |= 0x01;
389 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, NE_BASE + NE_CMD);
390 outb_p(sizeof(struct e8390_pkt_hdr), NE_BASE + EN0_RCNTLO);
391 outb_p(0, NE_BASE + EN0_RCNTHI);
392 outb_p(0, NE_BASE + EN0_RSARLO); /* On page boundary */
393 outb_p(ring_page, NE_BASE + EN0_RSARHI);
394 outb_p(E8390_RREAD+E8390_START, NE_BASE + NE_CMD);
396 if (ei_status.word16) {
398 unsigned short *p = (unsigned short *)hdr;
399 for (len = sizeof(struct e8390_pkt_hdr)>>1; len > 0; len--)
400 *p++ = inw(NE_BASE + NE_DATAPORT);
402 insb(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
404 outb_p(ENISR_RDC, NE_BASE + EN0_ISR); /* Ack intr. */
405 ei_status.dmaing &= ~0x01;
407 le16_to_cpus(&hdr->count);
410 /* Block input and output, similar to the Crynwr packet driver. If you
411 are porting to a new ethercard, look at the packet driver source for hints.
412 The NEx000 doesn't share the on-board packet memory -- you have to put
413 the packet out through the "remote DMA" dataport using outb. */
415 static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
417 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
418 #ifdef NE_SANITY_CHECK
419 int xfer_count = count;
421 char *buf = skb->data;
423 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
424 if (ei_status.dmaing)
426 printk(KERN_EMERG "%s: DMAing conflict in ne_block_input "
427 "[DMAstat:%d][irqlock:%d].\n",
428 dev->name, ei_status.dmaing, ei_status.irqlock);
431 ei_status.dmaing |= 0x01;
432 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, NE_BASE + NE_CMD);
433 outb_p(count & 0xff, NE_BASE + EN0_RCNTLO);
434 outb_p(count >> 8, NE_BASE + EN0_RCNTHI);
435 outb_p(ring_offset & 0xff, NE_BASE + EN0_RSARLO);
436 outb_p(ring_offset >> 8, NE_BASE + EN0_RSARHI);
437 outb_p(E8390_RREAD+E8390_START, NE_BASE + NE_CMD);
438 if (ei_status.word16)
441 unsigned short *p = (unsigned short *)buf;
442 for (len = count>>1; len > 0; len--)
443 *p++ = inw(NE_BASE + NE_DATAPORT);
446 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
447 #ifdef NE_SANITY_CHECK
452 insb(NE_BASE + NE_DATAPORT, buf, count);
455 #ifdef NE_SANITY_CHECK
456 /* This was for the ALPHA version only, but enough people have
457 been encountering problems so it is still here. If you see
458 this message you either 1) have a slightly incompatible clone
459 or 2) have noise/speed problems with your bus. */
463 /* DMA termination address check... */
464 int addr, tries = 20;
466 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
467 -- it's broken for Rx on some cards! */
468 int high = inb_p(NE_BASE + EN0_RSARHI);
469 int low = inb_p(NE_BASE + EN0_RSARLO);
470 addr = (high << 8) + low;
471 if (((ring_offset + xfer_count) & 0xff) == low)
473 } while (--tries > 0);
475 printk(KERN_WARNING "%s: RX transfer address mismatch,"
476 "%#4.4x (expected) vs. %#4.4x (actual).\n",
477 dev->name, ring_offset + xfer_count, addr);
480 outb_p(ENISR_RDC, NE_BASE + EN0_ISR); /* Ack intr. */
481 ei_status.dmaing &= ~0x01;
484 static void ne_block_output(struct net_device *dev, int count,
485 const unsigned char *buf, const int start_page)
487 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
488 unsigned long dma_start;
489 #ifdef NE_SANITY_CHECK
493 /* Round the count up for word writes. Do we need to do this?
494 What effect will an odd byte count have on the 8390?
495 I should check someday. */
497 if (ei_status.word16 && (count & 0x01))
500 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
501 if (ei_status.dmaing)
503 printk(KERN_EMERG "%s: DMAing conflict in ne_block_output."
504 "[DMAstat:%d][irqlock:%d]\n",
505 dev->name, ei_status.dmaing, ei_status.irqlock);
508 ei_status.dmaing |= 0x01;
509 /* We should already be in page 0, but to be safe... */
510 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, NE_BASE + NE_CMD);
512 #ifdef NE_SANITY_CHECK
516 #ifdef NE8390_RW_BUGFIX
517 /* Handle the read-before-write bug the same way as the
518 Crynwr packet driver -- the NatSemi method doesn't work.
519 Actually this doesn't always work either, but if you have
520 problems with your NEx000 this is better than nothing! */
522 outb_p(0x42, NE_BASE + EN0_RCNTLO);
523 outb_p(0x00, NE_BASE + EN0_RCNTHI);
524 outb_p(0x42, NE_BASE + EN0_RSARLO);
525 outb_p(0x00, NE_BASE + EN0_RSARHI);
526 outb_p(E8390_RREAD+E8390_START, NE_BASE + NE_CMD);
527 /* Make certain that the dummy read has occurred. */
531 outb_p(ENISR_RDC, NE_BASE + EN0_ISR);
533 /* Now the normal output. */
534 outb_p(count & 0xff, NE_BASE + EN0_RCNTLO);
535 outb_p(count >> 8, NE_BASE + EN0_RCNTHI);
536 outb_p(0x00, NE_BASE + EN0_RSARLO);
537 outb_p(start_page, NE_BASE + EN0_RSARHI);
539 outb_p(E8390_RWRITE+E8390_START, NE_BASE + NE_CMD);
540 if (ei_status.word16) {
542 unsigned short *p = (unsigned short *)buf;
543 for (len = count>>1; len > 0; len--)
544 outw(*p++, NE_BASE + NE_DATAPORT);
546 outsb(NE_BASE + NE_DATAPORT, buf, count);
551 #ifdef NE_SANITY_CHECK
552 /* This was for the ALPHA version only, but enough people have
553 been encountering problems so it is still here. */
557 /* DMA termination address check... */
558 int addr, tries = 20;
560 int high = inb_p(NE_BASE + EN0_RSARHI);
561 int low = inb_p(NE_BASE + EN0_RSARLO);
562 addr = (high << 8) + low;
563 if ((start_page << 8) + count == addr)
565 } while (--tries > 0);
569 printk(KERN_WARNING "%s: Tx packet transfer address mismatch,"
570 "%#4.4x (expected) vs. %#4.4x (actual).\n",
571 dev->name, (start_page << 8) + count, addr);
578 while ((inb_p(NE_BASE + EN0_ISR) & ENISR_RDC) == 0)
579 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
580 printk(KERN_WARNING "%s: timeout waiting for Tx RDC.\n", dev->name);
586 outb_p(ENISR_RDC, NE_BASE + EN0_ISR); /* Ack intr. */
587 ei_status.dmaing &= ~0x01;
593 #define MAX_NE_CARDS 1 /* Max number of NE cards per module */
594 static struct net_device *dev_ne[MAX_NE_CARDS];
595 static int io[MAX_NE_CARDS];
596 static int irq[MAX_NE_CARDS];
597 static int bad[MAX_NE_CARDS]; /* 0xbad = bad sig or no reset ack */
599 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
600 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
601 MODULE_PARM(bad, "1-" __MODULE_STRING(MAX_NE_CARDS) "i");
602 MODULE_PARM_DESC(io, "I/O base address(es)");
603 MODULE_PARM_DESC(irq, "IRQ number(s)");
604 MODULE_DESCRIPTION("H8/300 NE2000 Ethernet driver");
605 MODULE_LICENSE("GPL");
607 /* This is set up so that no ISA autoprobe takes place. We can't guarantee
608 that the ne2k probe is the last 8390 based probe to take place (as it
609 is at boot) and so the probe will get confused by any other 8390 cards.
610 ISA device autoprobes on a running machine are not recommended anyway. */
612 int init_module(void)
614 int this_dev, found = 0;
617 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
618 struct net_device *dev = alloc_ei_netdev();
622 dev->irq = irq[this_dev];
623 dev->mem_end = bad[this_dev];
624 dev->base_addr = io[this_dev];
626 dev->base_addr = h8300_ne_base[this_dev];
627 dev->irq = h8300_ne_irq[this_dev];
629 err = init_reg_offset(dev, dev->base_addr);
631 if (do_ne_probe(dev) == 0) {
632 if (register_netdev(dev) == 0) {
633 dev_ne[found++] = dev;
642 if (io[this_dev] != 0)
643 printk(KERN_WARNING "ne.c: No NE*000 card found at i/o = %#x\n", dev->base_addr);
645 printk(KERN_NOTICE "ne.c: You must supply \"io=0xNNN\" value(s) for ISA cards.\n");
653 void cleanup_module(void)
657 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
658 struct net_device *dev = dev_ne[this_dev];
660 unregister_netdev(dev);