Import 2.4.0-test4
[davej-history.git] / drivers / net / oaknet.c
blobe24e0e4f0498b8cde2fb0418433ed4be9c2e779f
1 /*
3 * Copyright (c) 1999-2000 Grant Erickson <grant@lcse.umn.edu>
5 * Module name: oaknet.c
7 * Description:
8 * Driver for the National Semiconductor DP83902AV Ethernet controller
9 * on-board the IBM PowerPC "Oak" evaluation board. Adapted from the
10 * various other 8390 drivers written by Donald Becker and Paul Gortmaker.
12 * Additional inspiration from the "tcd8390.c" driver from TiVo, Inc.
13 * and "enetLib.c" from IBM.
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/delay.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/init.h>
24 #include <asm/board.h>
25 #include <asm/io.h>
27 #include "8390.h"
30 /* Preprocessor Defines */
32 #if !defined(TRUE) || TRUE != 1
33 #define TRUE 1
34 #endif
36 #if !defined(FALSE) || FALSE != 0
37 #define FALSE 0
38 #endif
40 #define OAKNET_START_PG 0x20 /* First page of TX buffer */
41 #define OAKNET_STOP_PG 0x40 /* Last pagge +1 of RX ring */
43 #define OAKNET_WAIT (2 * HZ / 100) /* 20 ms */
45 /* Experimenting with some fixes for a broken driver... */
47 #define OAKNET_DISINT
48 #define OAKNET_HEADCHECK
49 #define OAKNET_RWFIX
52 /* Global Variables */
54 static const char *name = "National DP83902AV";
56 static struct net_device *oaknet_devs = NULL;
59 /* Function Prototypes */
61 static int oaknet_open(struct net_device *dev);
62 static int oaknet_close(struct net_device *dev);
64 static void oaknet_reset_8390(struct net_device *dev);
65 static void oaknet_get_8390_hdr(struct net_device *dev,
66 struct e8390_pkt_hdr *hdr, int ring_page);
67 static void oaknet_block_input(struct net_device *dev, int count,
68 struct sk_buff *skb, int ring_offset);
69 static void oaknet_block_output(struct net_device *dev, int count,
70 const unsigned char *buf, int start_page);
72 static void oaknet_dma_error(struct net_device *dev, const char *name);
76 * int oaknet_init()
78 * Description:
79 * This routine performs all the necessary platform-specific initiali-
80 * zation and set-up for the IBM "Oak" evaluation board's National
81 * Semiconductor DP83902AV "ST-NIC" Ethernet controller.
83 * Input(s):
84 * N/A
86 * Output(s):
87 * N/A
89 * Returns:
90 * 0 if OK, otherwise system error number on error.
93 static int __init oaknet_init(void)
95 register int i;
96 int reg0, regd;
97 struct net_device tmp, *dev = NULL;
98 #if 0
99 unsigned long ioaddr = OAKNET_IO_BASE;
100 #else
101 unsigned long ioaddr = ioremap(OAKNET_IO_BASE, OAKNET_IO_SIZE);
102 #endif
103 bd_t *bip = (bd_t *)__res;
106 * This MUST happen here because of the nic_* macros
107 * which have an implicit dependency on dev->base_addr.
110 tmp.base_addr = ioaddr;
111 dev = &tmp;
113 if (!request_region(OAKNET_IO_BASE, OAKNET_IO_SIZE, name))
114 return -EBUSY;
116 /* Quick register check to see if the device is really there. */
118 if ((reg0 = ei_ibp(ioaddr)) == 0xFF) {
119 release_region(OAKNET_IO_BASE, OAKNET_IO_SIZE);
120 return (ENODEV);
124 * That worked. Now a more thorough check, using the multicast
125 * address registers, that the device is definitely out there
126 * and semi-functional.
129 ei_obp(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
130 regd = ei_ibp(ioaddr + 0x0D);
131 ei_obp(0xFF, ioaddr + 0x0D);
132 ei_obp(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
133 ei_ibp(ioaddr + EN0_COUNTER0);
135 /* It's no good. Fix things back up and leave. */
137 if (ei_ibp(ioaddr + EN0_COUNTER0) != 0) {
138 ei_obp(reg0, ioaddr);
139 ei_obp(regd, ioaddr + 0x0D);
140 dev->base_addr = 0;
142 release_region(dev->base_addr, OAKNET_IO_SIZE);
143 return (ENODEV);
147 * We're not using the old-style probing API, so we have to allocate
148 * our own device structure.
151 dev = init_etherdev(0, 0);
152 oaknet_devs = dev;
155 * This controller is on an embedded board, so the base address
156 * and interrupt assignments are pre-assigned and unchageable.
159 dev->base_addr = ioaddr;
160 dev->irq = OAKNET_INT;
162 /* Allocate 8390-specific device-private area and fields. */
164 if (ethdev_init(dev)) {
165 printk(" unable to get memory for dev->priv.\n");
166 release_region(dev->base_addr, OAKNET_IO_SIZE);
167 return (-ENOMEM);
171 * Disable all chip interrupts for now and ACK all pending
172 * interrupts.
175 ei_obp(0x0, ioaddr + EN0_IMR);
176 ei_obp(0xFF, ioaddr + EN0_ISR);
178 /* Attempt to get the interrupt line */
180 if (request_irq(dev->irq, ei_interrupt, 0, name, dev)) {
181 printk("%s: unable to request interrupt %d.\n",
182 dev->name, dev->irq);
183 kfree(dev->priv);
184 release_region(dev->base_addr, OAKNET_IO_SIZE);
185 return (EAGAIN);
188 /* Tell the world about what and where we've found. */
190 printk("%s: %s at", dev->name, name);
191 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
192 dev->dev_addr[i] = bip->bi_enetaddr[i];
193 printk("%c%.2x", (i ? ':' : ' '), dev->dev_addr[i]);
195 printk(", found at %#lx, using IRQ %d.\n", dev->base_addr, dev->irq);
197 /* Set up some required driver fields and then we're done. */
199 ei_status.name = name;
200 ei_status.word16 = FALSE;
201 ei_status.tx_start_page = OAKNET_START_PG;
202 ei_status.rx_start_page = OAKNET_START_PG + TX_PAGES;
203 ei_status.stop_page = OAKNET_STOP_PG;
205 ei_status.reset_8390 = &oaknet_reset_8390;
206 ei_status.block_input = &oaknet_block_input;
207 ei_status.block_output = &oaknet_block_output;
208 ei_status.get_8390_hdr = &oaknet_get_8390_hdr;
210 dev->open = oaknet_open;
211 dev->stop = oaknet_close;
213 NS8390_init(dev, FALSE);
215 return (0);
219 * static int oaknet_open()
221 * Description:
222 * This routine is a modest wrapper around ei_open, the 8390-generic,
223 * driver open routine. This just increments the module usage count
224 * and passes along the status from ei_open.
226 * Input(s):
227 * *dev - Pointer to the device structure for this driver.
229 * Output(s):
230 * *dev - Pointer to the device structure for this driver, potentially
231 * modified by ei_open.
233 * Returns:
234 * 0 if OK, otherwise < 0 on error.
237 static int
238 oaknet_open(struct net_device *dev)
240 int status = ei_open(dev);
241 MOD_INC_USE_COUNT;
242 return (status);
246 * static int oaknet_close()
248 * Description:
249 * This routine is a modest wrapper around ei_close, the 8390-generic,
250 * driver close routine. This just decrements the module usage count
251 * and passes along the status from ei_close.
253 * Input(s):
254 * *dev - Pointer to the device structure for this driver.
256 * Output(s):
257 * *dev - Pointer to the device structure for this driver, potentially
258 * modified by ei_close.
260 * Returns:
261 * 0 if OK, otherwise < 0 on error.
264 static int
265 oaknet_close(struct net_device *dev)
267 int status = ei_close(dev);
268 MOD_DEC_USE_COUNT;
269 return (status);
273 * static void oaknet_reset_8390()
275 * Description:
276 * This routine resets the DP83902 chip.
278 * Input(s):
279 * *dev - Pointer to the device structure for this driver.
281 * Output(s):
282 * N/A
284 * Returns:
285 * N/A
288 static void
289 oaknet_reset_8390(struct net_device *dev)
291 int base = E8390_BASE;
294 * We have no provision of reseting the controller as is done
295 * in other drivers, such as "ne.c". However, the following
296 * seems to work well enough in the TiVo driver.
299 printk("Resetting %s...\n", dev->name);
300 ei_obp(E8390_STOP | E8390_NODMA | E8390_PAGE0, base + E8390_CMD);
301 ei_status.txing = 0;
302 ei_status.dmaing = 0;
304 return;
308 * static void oaknet_get_8390_hdr()
310 * Description:
311 * This routine grabs the 8390-specific header. It's similar to the
312 * block input routine, but we don't need to be concerned with ring wrap
313 * as the header will be at the start of a page, so we optimize accordingly.
315 * Input(s):
316 * *dev - Pointer to the device structure for this driver.
317 * *hdr - Pointer to storage for the 8390-specific packet header.
318 * ring_page - ?
320 * Output(s):
321 * *hdr - Pointer to the 8390-specific packet header for the just-
322 * received frame.
324 * Returns:
325 * N/A
328 static void
329 oaknet_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
330 int ring_page)
332 int base = dev->base_addr;
335 * This should NOT happen. If it does, it is the LAST thing you'll
336 * see.
339 if (ei_status.dmaing) {
340 oaknet_dma_error(dev, "oaknet_get_8390_hdr");
341 return;
344 ei_status.dmaing |= 0x01;
345 outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, base + OAKNET_CMD);
346 outb_p(sizeof(struct e8390_pkt_hdr), base + EN0_RCNTLO);
347 outb_p(0, base + EN0_RCNTHI);
348 outb_p(0, base + EN0_RSARLO); /* On page boundary */
349 outb_p(ring_page, base + EN0_RSARHI);
350 outb_p(E8390_RREAD + E8390_START, base + OAKNET_CMD);
352 if (ei_status.word16)
353 insw(base + OAKNET_DATA, hdr,
354 sizeof(struct e8390_pkt_hdr) >> 1);
355 else
356 insb(base + OAKNET_DATA, hdr,
357 sizeof(struct e8390_pkt_hdr));
359 /* Byte-swap the packet byte count */
361 hdr->count = le16_to_cpu(hdr->count);
363 outb_p(ENISR_RDC, base + EN0_ISR); /* ACK Remote DMA interrupt */
364 ei_status.dmaing &= ~0x01;
366 return;
370 * XXX - Document me.
372 static void
373 oaknet_block_input(struct net_device *dev, int count, struct sk_buff *skb,
374 int ring_offset)
376 int base = OAKNET_BASE;
377 char *buf = skb->data;
380 * This should NOT happen. If it does, it is the LAST thing you'll
381 * see.
384 if (ei_status.dmaing) {
385 oaknet_dma_error(dev, "oaknet_block_input");
386 return;
389 #ifdef OAKNET_DISINT
390 save_flags(flags);
391 cli();
392 #endif
394 ei_status.dmaing |= 0x01;
395 ei_obp(E8390_NODMA + E8390_PAGE0 + E8390_START, base + E8390_CMD);
396 ei_obp(count & 0xff, base + EN0_RCNTLO);
397 ei_obp(count >> 8, base + EN0_RCNTHI);
398 ei_obp(ring_offset & 0xff, base + EN0_RSARLO);
399 ei_obp(ring_offset >> 8, base + EN0_RSARHI);
400 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
401 if (ei_status.word16) {
402 ei_isw(base + E8390_DATA, buf, count >> 1);
403 if (count & 0x01) {
404 buf[count - 1] = ei_ib(base + E8390_DATA);
405 #ifdef OAKNET_HEADCHECK
406 bytes++;
407 #endif
409 } else {
410 ei_isb(base + E8390_DATA, buf, count);
412 #ifdef OAKNET_HEADCHECK
414 * This was for the ALPHA version only, but enough people have
415 * been encountering problems so it is still here. If you see
416 * this message you either 1) have a slightly incompatible clone
417 * or 2) have noise/speed problems with your bus.
420 /* DMA termination address check... */
422 int addr, tries = 20;
423 do {
424 /* DON'T check for 'ei_ibp(EN0_ISR) & ENISR_RDC' here
425 -- it's broken for Rx on some cards! */
426 int high = ei_ibp(base + EN0_RSARHI);
427 int low = ei_ibp(base + EN0_RSARLO);
428 addr = (high << 8) + low;
429 if (((ring_offset + bytes) & 0xff) == low)
430 break;
431 } while (--tries > 0);
432 if (tries <= 0)
433 printk("%s: RX transfer address mismatch,"
434 "%#4.4x (expected) vs. %#4.4x (actual).\n",
435 dev->name, ring_offset + bytes, addr);
437 #endif
438 ei_obp(ENISR_RDC, base + EN0_ISR); /* ACK Remote DMA interrupt */
439 ei_status.dmaing &= ~0x01;
441 #ifdef OAKNET_DISINT
442 restore_flags(flags);
443 #endif
445 return;
449 * static void oaknet_block_output()
451 * Description:
452 * This routine...
454 * Input(s):
455 * *dev - Pointer to the device structure for this driver.
456 * count - Number of bytes to be transferred.
457 * *buf -
458 * start_page -
460 * Output(s):
461 * N/A
463 * Returns:
464 * N/A
467 static void
468 oaknet_block_output(struct net_device *dev, int count,
469 const unsigned char *buf, int start_page)
471 int base = E8390_BASE;
472 #if 0
473 int bug;
474 #endif
475 unsigned long start;
476 #ifdef OAKNET_DISINT
477 unsigned long flags;
478 #endif
479 #ifdef OAKNET_HEADCHECK
480 int retries = 0;
481 #endif
483 /* Round the count up for word writes. */
485 if (ei_status.word16 && (count & 0x1))
486 count++;
489 * This should NOT happen. If it does, it is the LAST thing you'll
490 * see.
493 if (ei_status.dmaing) {
494 oaknet_dma_error(dev, "oaknet_block_output");
495 return;
498 #ifdef OAKNET_DISINT
499 save_flags(flags);
500 cli();
501 #endif
503 ei_status.dmaing |= 0x01;
505 /* Make sure we are in page 0. */
507 ei_obp(E8390_PAGE0 + E8390_START + E8390_NODMA, base + E8390_CMD);
509 #ifdef OAKNET_HEADCHECK
510 retry:
511 #endif
513 #if 0
515 * The 83902 documentation states that the processor needs to
516 * do a "dummy read" before doing the remote write to work
517 * around a chip bug they don't feel like fixing.
520 bug = 0;
521 while (1) {
522 unsigned int rdhi;
523 unsigned int rdlo;
525 /* Now the normal output. */
526 ei_obp(ENISR_RDC, base + EN0_ISR);
527 ei_obp(count & 0xff, base + EN0_RCNTLO);
528 ei_obp(count >> 8, base + EN0_RCNTHI);
529 ei_obp(0x00, base + EN0_RSARLO);
530 ei_obp(start_page, base + EN0_RSARHI);
532 if (bug++)
533 break;
535 /* Perform the dummy read */
536 rdhi = ei_ibp(base + EN0_CRDAHI);
537 rdlo = ei_ibp(base + EN0_CRDALO);
538 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
540 while (1) {
541 unsigned int nrdhi;
542 unsigned int nrdlo;
543 nrdhi = ei_ibp(base + EN0_CRDAHI);
544 nrdlo = ei_ibp(base + EN0_CRDALO);
545 if ((rdhi != nrdhi) || (rdlo != nrdlo))
546 break;
549 #else
550 #ifdef OAKNET_RWFIX
552 * Handle the read-before-write bug the same way as the
553 * Crynwr packet driver -- the Nat'l Semi. method doesn't work.
554 * Actually this doesn't always work either, but if you have
555 * problems with your 83902 this is better than nothing!
558 ei_obp(0x42, base + EN0_RCNTLO);
559 ei_obp(0x00, base + EN0_RCNTHI);
560 ei_obp(0x42, base + EN0_RSARLO);
561 ei_obp(0x00, base + EN0_RSARHI);
562 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
563 /* Make certain that the dummy read has occurred. */
564 udelay(6);
565 #endif
567 ei_obp(ENISR_RDC, base + EN0_ISR);
569 /* Now the normal output. */
570 ei_obp(count & 0xff, base + EN0_RCNTLO);
571 ei_obp(count >> 8, base + EN0_RCNTHI);
572 ei_obp(0x00, base + EN0_RSARLO);
573 ei_obp(start_page, base + EN0_RSARHI);
574 #endif /* 0/1 */
576 ei_obp(E8390_RWRITE + E8390_START, base + E8390_CMD);
577 if (ei_status.word16) {
578 ei_osw(E8390_BASE + E8390_DATA, buf, count >> 1);
579 } else {
580 ei_osb(E8390_BASE + E8390_DATA, buf, count);
583 #ifdef OAKNET_DISINT
584 restore_flags(flags);
585 #endif
587 start = jiffies;
589 #ifdef OAKNET_HEADCHECK
591 * This was for the ALPHA version only, but enough people have
592 * been encountering problems so it is still here.
596 /* DMA termination address check... */
597 int addr, tries = 20;
598 do {
599 int high = ei_ibp(base + EN0_RSARHI);
600 int low = ei_ibp(base + EN0_RSARLO);
601 addr = (high << 8) + low;
602 if ((start_page << 8) + count == addr)
603 break;
604 } while (--tries > 0);
606 if (tries <= 0) {
607 printk("%s: Tx packet transfer address mismatch,"
608 "%#4.4x (expected) vs. %#4.4x (actual).\n",
609 dev->name, (start_page << 8) + count, addr);
610 if (retries++ == 0)
611 goto retry;
614 #endif
616 while ((ei_ibp(base + EN0_ISR) & ENISR_RDC) == 0) {
617 if (jiffies - start > OAKNET_WAIT) {
618 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
619 oaknet_reset_8390(dev);
620 NS8390_init(dev, TRUE);
621 break;
625 ei_obp(ENISR_RDC, base + EN0_ISR); /* Ack intr. */
626 ei_status.dmaing &= ~0x01;
628 return;
632 * static void oaknet_dma_error()
634 * Description:
635 * This routine prints out a last-ditch informative message to the console
636 * indicating that a DMA error occured. If you see this, it's the last
637 * thing you'll see.
639 * Input(s):
640 * *dev - Pointer to the device structure for this driver.
641 * *name - Informative text (e.g. function name) indicating where the
642 * DMA error occurred.
644 * Output(s):
645 * N/A
647 * Returns:
648 * N/A
651 static void
652 oaknet_dma_error(struct net_device *dev, const char *name)
654 printk(KERN_EMERG "%s: DMAing conflict in %s."
655 "[DMAstat:%d][irqlock:%d][intr:%ld]\n",
656 dev->name, name, ei_status.dmaing, ei_status.irqlock,
657 dev->interrupt);
659 return;
663 * Oak Ethernet module load interface.
665 static int __init oaknet_init_module (void)
667 int status;
670 * We're dependent on the 8390 generic driver module, make
671 * sure its symbols are loaded.
674 if (load_8390_module("oaknet.c"))
675 return (-ENOSYS);
677 if (oaknet_devs != NULL)
678 return (-EBUSY);
680 status = oaknet_init()
682 if (status != 0)
683 unload_8390_module();
685 return (status);
689 * Oak Ethernet module unload interface.
691 static void __exit oaknet_cleanup_module (void)
693 if (oaknet_devs == NULL)
694 return;
696 if (oaknet_devs->priv != NULL) {
697 int ioaddr = oaknet_devs->base_addr;
698 void *priv = oaknet_devs->priv;
699 free_irq(oaknet_devs->irq, oaknet_devs);
700 release_region(ioaddr, OAKNET_IO_SIZE);
701 unregister_netdev(oaknet_dev);
702 kfree(priv);
705 oaknet_devs = NULL;
707 unload_8390_module();
710 module_init(oaknet_init_module);
711 module_exit(oaknet_cleanup_module);