More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / oaknet.c
blob0029c3c54f3c82ac596c18db7deef9ccc0d10004
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;
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 int ret;
98 struct net_device tmp, *dev = NULL;
99 #if 0
100 unsigned long ioaddr = OAKNET_IO_BASE;
101 #else
102 unsigned long ioaddr = ioremap(OAKNET_IO_BASE, OAKNET_IO_SIZE);
103 #endif
104 bd_t *bip = (bd_t *)__res;
106 if (!ioaddr)
107 return -ENOMEM;
109 * This MUST happen here because of the nic_* macros
110 * which have an implicit dependency on dev->base_addr.
113 tmp.base_addr = ioaddr;
114 dev = &tmp;
116 ret = -EBUSY;
117 if (!request_region(OAKNET_IO_BASE, OAKNET_IO_SIZE, name))
118 goto out_unmap;
120 /* Quick register check to see if the device is really there. */
122 ret = -ENODEV;
123 if ((reg0 = ei_ibp(ioaddr)) == 0xFF)
124 goto out_region;
127 * That worked. Now a more thorough check, using the multicast
128 * address registers, that the device is definitely out there
129 * and semi-functional.
132 ei_obp(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
133 regd = ei_ibp(ioaddr + 0x0D);
134 ei_obp(0xFF, ioaddr + 0x0D);
135 ei_obp(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
136 ei_ibp(ioaddr + EN0_COUNTER0);
138 /* It's no good. Fix things back up and leave. */
140 ret = -ENODEV;
141 if (ei_ibp(ioaddr + EN0_COUNTER0) != 0) {
142 ei_obp(reg0, ioaddr);
143 ei_obp(regd, ioaddr + 0x0D);
144 goto out_region;
148 * We're not using the old-style probing API, so we have to allocate
149 * our own device structure.
152 dev = init_etherdev(NULL, 0);
153 ret = -ENOMEM;
154 if (!dev)
155 goto out_region;
156 SET_MODULE_OWNER(dev);
157 oaknet_devs = dev;
160 * This controller is on an embedded board, so the base address
161 * and interrupt assignments are pre-assigned and unchageable.
164 dev->base_addr = ioaddr;
165 dev->irq = OAKNET_INT;
167 /* Allocate 8390-specific device-private area and fields. */
169 ret = -ENOMEM;
170 if (ethdev_init(dev)) {
171 printk(" unable to get memory for dev->priv.\n");
172 goto out_dev;
176 * Disable all chip interrupts for now and ACK all pending
177 * interrupts.
180 ei_obp(0x0, ioaddr + EN0_IMR);
181 ei_obp(0xFF, ioaddr + EN0_ISR);
183 /* Attempt to get the interrupt line */
185 ret = -EAGAIN;
186 if (request_irq(dev->irq, ei_interrupt, 0, name, dev)) {
187 printk("%s: unable to request interrupt %d.\n",
188 dev->name, dev->irq);
189 goto out_priv;
192 /* Tell the world about what and where we've found. */
194 printk("%s: %s at", dev->name, name);
195 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
196 dev->dev_addr[i] = bip->bi_enetaddr[i];
197 printk("%c%.2x", (i ? ':' : ' '), dev->dev_addr[i]);
199 printk(", found at %#lx, using IRQ %d.\n", dev->base_addr, dev->irq);
201 /* Set up some required driver fields and then we're done. */
203 ei_status.name = name;
204 ei_status.word16 = FALSE;
205 ei_status.tx_start_page = OAKNET_START_PG;
206 ei_status.rx_start_page = OAKNET_START_PG + TX_PAGES;
207 ei_status.stop_page = OAKNET_STOP_PG;
209 ei_status.reset_8390 = &oaknet_reset_8390;
210 ei_status.block_input = &oaknet_block_input;
211 ei_status.block_output = &oaknet_block_output;
212 ei_status.get_8390_hdr = &oaknet_get_8390_hdr;
214 dev->open = oaknet_open;
215 dev->stop = oaknet_close;
217 NS8390_init(dev, FALSE);
219 return (0);
220 out_priv:
221 kfree(dev->priv);
222 out_dev:
223 unregister_netdev(dev);
224 kfree(dev);
225 out_region:
226 release_region(OAKNET_IO_BASE, OAKNET_IO_SIZE);
227 out_unmap:
228 iounmap(ioaddr);
229 return ret;
233 * static int oaknet_open()
235 * Description:
236 * This routine is a modest wrapper around ei_open, the 8390-generic,
237 * driver open routine. This just increments the module usage count
238 * and passes along the status from ei_open.
240 * Input(s):
241 * *dev - Pointer to the device structure for this driver.
243 * Output(s):
244 * *dev - Pointer to the device structure for this driver, potentially
245 * modified by ei_open.
247 * Returns:
248 * 0 if OK, otherwise < 0 on error.
251 static int
252 oaknet_open(struct net_device *dev)
254 int status = ei_open(dev);
255 return (status);
259 * static int oaknet_close()
261 * Description:
262 * This routine is a modest wrapper around ei_close, the 8390-generic,
263 * driver close routine. This just decrements the module usage count
264 * and passes along the status from ei_close.
266 * Input(s):
267 * *dev - Pointer to the device structure for this driver.
269 * Output(s):
270 * *dev - Pointer to the device structure for this driver, potentially
271 * modified by ei_close.
273 * Returns:
274 * 0 if OK, otherwise < 0 on error.
277 static int
278 oaknet_close(struct net_device *dev)
280 int status = ei_close(dev);
281 return (status);
285 * static void oaknet_reset_8390()
287 * Description:
288 * This routine resets the DP83902 chip.
290 * Input(s):
291 * *dev - Pointer to the device structure for this driver.
293 * Output(s):
294 * N/A
296 * Returns:
297 * N/A
300 static void
301 oaknet_reset_8390(struct net_device *dev)
303 int base = E8390_BASE;
306 * We have no provision of reseting the controller as is done
307 * in other drivers, such as "ne.c". However, the following
308 * seems to work well enough in the TiVo driver.
311 printk("Resetting %s...\n", dev->name);
312 ei_obp(E8390_STOP | E8390_NODMA | E8390_PAGE0, base + E8390_CMD);
313 ei_status.txing = 0;
314 ei_status.dmaing = 0;
318 * static void oaknet_get_8390_hdr()
320 * Description:
321 * This routine grabs the 8390-specific header. It's similar to the
322 * block input routine, but we don't need to be concerned with ring wrap
323 * as the header will be at the start of a page, so we optimize accordingly.
325 * Input(s):
326 * *dev - Pointer to the device structure for this driver.
327 * *hdr - Pointer to storage for the 8390-specific packet header.
328 * ring_page - ?
330 * Output(s):
331 * *hdr - Pointer to the 8390-specific packet header for the just-
332 * received frame.
334 * Returns:
335 * N/A
338 static void
339 oaknet_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
340 int ring_page)
342 int base = dev->base_addr;
345 * This should NOT happen. If it does, it is the LAST thing you'll
346 * see.
349 if (ei_status.dmaing) {
350 oaknet_dma_error(dev, "oaknet_get_8390_hdr");
351 return;
354 ei_status.dmaing |= 0x01;
355 outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, base + OAKNET_CMD);
356 outb_p(sizeof(struct e8390_pkt_hdr), base + EN0_RCNTLO);
357 outb_p(0, base + EN0_RCNTHI);
358 outb_p(0, base + EN0_RSARLO); /* On page boundary */
359 outb_p(ring_page, base + EN0_RSARHI);
360 outb_p(E8390_RREAD + E8390_START, base + OAKNET_CMD);
362 if (ei_status.word16)
363 insw(base + OAKNET_DATA, hdr,
364 sizeof(struct e8390_pkt_hdr) >> 1);
365 else
366 insb(base + OAKNET_DATA, hdr,
367 sizeof(struct e8390_pkt_hdr));
369 /* Byte-swap the packet byte count */
371 hdr->count = le16_to_cpu(hdr->count);
373 outb_p(ENISR_RDC, base + EN0_ISR); /* ACK Remote DMA interrupt */
374 ei_status.dmaing &= ~0x01;
378 * XXX - Document me.
380 static void
381 oaknet_block_input(struct net_device *dev, int count, struct sk_buff *skb,
382 int ring_offset)
384 int base = OAKNET_BASE;
385 char *buf = skb->data;
388 * This should NOT happen. If it does, it is the LAST thing you'll
389 * see.
392 if (ei_status.dmaing) {
393 oaknet_dma_error(dev, "oaknet_block_input");
394 return;
397 #ifdef OAKNET_DISINT
398 save_flags(flags);
399 cli();
400 #endif
402 ei_status.dmaing |= 0x01;
403 ei_obp(E8390_NODMA + E8390_PAGE0 + E8390_START, base + E8390_CMD);
404 ei_obp(count & 0xff, base + EN0_RCNTLO);
405 ei_obp(count >> 8, base + EN0_RCNTHI);
406 ei_obp(ring_offset & 0xff, base + EN0_RSARLO);
407 ei_obp(ring_offset >> 8, base + EN0_RSARHI);
408 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
409 if (ei_status.word16) {
410 ei_isw(base + E8390_DATA, buf, count >> 1);
411 if (count & 0x01) {
412 buf[count - 1] = ei_ib(base + E8390_DATA);
413 #ifdef OAKNET_HEADCHECK
414 bytes++;
415 #endif
417 } else {
418 ei_isb(base + E8390_DATA, buf, count);
420 #ifdef OAKNET_HEADCHECK
422 * This was for the ALPHA version only, but enough people have
423 * been encountering problems so it is still here. If you see
424 * this message you either 1) have a slightly incompatible clone
425 * or 2) have noise/speed problems with your bus.
428 /* DMA termination address check... */
430 int addr, tries = 20;
431 do {
432 /* DON'T check for 'ei_ibp(EN0_ISR) & ENISR_RDC' here
433 -- it's broken for Rx on some cards! */
434 int high = ei_ibp(base + EN0_RSARHI);
435 int low = ei_ibp(base + EN0_RSARLO);
436 addr = (high << 8) + low;
437 if (((ring_offset + bytes) & 0xff) == low)
438 break;
439 } while (--tries > 0);
440 if (tries <= 0)
441 printk("%s: RX transfer address mismatch,"
442 "%#4.4x (expected) vs. %#4.4x (actual).\n",
443 dev->name, ring_offset + bytes, addr);
445 #endif
446 ei_obp(ENISR_RDC, base + EN0_ISR); /* ACK Remote DMA interrupt */
447 ei_status.dmaing &= ~0x01;
449 #ifdef OAKNET_DISINT
450 restore_flags(flags);
451 #endif
455 * static void oaknet_block_output()
457 * Description:
458 * This routine...
460 * Input(s):
461 * *dev - Pointer to the device structure for this driver.
462 * count - Number of bytes to be transferred.
463 * *buf -
464 * start_page -
466 * Output(s):
467 * N/A
469 * Returns:
470 * N/A
473 static void
474 oaknet_block_output(struct net_device *dev, int count,
475 const unsigned char *buf, int start_page)
477 int base = E8390_BASE;
478 #if 0
479 int bug;
480 #endif
481 unsigned long start;
482 #ifdef OAKNET_DISINT
483 unsigned long flags;
484 #endif
485 #ifdef OAKNET_HEADCHECK
486 int retries = 0;
487 #endif
489 /* Round the count up for word writes. */
491 if (ei_status.word16 && (count & 0x1))
492 count++;
495 * This should NOT happen. If it does, it is the LAST thing you'll
496 * see.
499 if (ei_status.dmaing) {
500 oaknet_dma_error(dev, "oaknet_block_output");
501 return;
504 #ifdef OAKNET_DISINT
505 save_flags(flags);
506 cli();
507 #endif
509 ei_status.dmaing |= 0x01;
511 /* Make sure we are in page 0. */
513 ei_obp(E8390_PAGE0 + E8390_START + E8390_NODMA, base + E8390_CMD);
515 #ifdef OAKNET_HEADCHECK
516 retry:
517 #endif
519 #if 0
521 * The 83902 documentation states that the processor needs to
522 * do a "dummy read" before doing the remote write to work
523 * around a chip bug they don't feel like fixing.
526 bug = 0;
527 while (1) {
528 unsigned int rdhi;
529 unsigned int rdlo;
531 /* Now the normal output. */
532 ei_obp(ENISR_RDC, base + EN0_ISR);
533 ei_obp(count & 0xff, base + EN0_RCNTLO);
534 ei_obp(count >> 8, base + EN0_RCNTHI);
535 ei_obp(0x00, base + EN0_RSARLO);
536 ei_obp(start_page, base + EN0_RSARHI);
538 if (bug++)
539 break;
541 /* Perform the dummy read */
542 rdhi = ei_ibp(base + EN0_CRDAHI);
543 rdlo = ei_ibp(base + EN0_CRDALO);
544 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
546 while (1) {
547 unsigned int nrdhi;
548 unsigned int nrdlo;
549 nrdhi = ei_ibp(base + EN0_CRDAHI);
550 nrdlo = ei_ibp(base + EN0_CRDALO);
551 if ((rdhi != nrdhi) || (rdlo != nrdlo))
552 break;
555 #else
556 #ifdef OAKNET_RWFIX
558 * Handle the read-before-write bug the same way as the
559 * Crynwr packet driver -- the Nat'l Semi. method doesn't work.
560 * Actually this doesn't always work either, but if you have
561 * problems with your 83902 this is better than nothing!
564 ei_obp(0x42, base + EN0_RCNTLO);
565 ei_obp(0x00, base + EN0_RCNTHI);
566 ei_obp(0x42, base + EN0_RSARLO);
567 ei_obp(0x00, base + EN0_RSARHI);
568 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
569 /* Make certain that the dummy read has occurred. */
570 udelay(6);
571 #endif
573 ei_obp(ENISR_RDC, base + EN0_ISR);
575 /* Now the normal output. */
576 ei_obp(count & 0xff, base + EN0_RCNTLO);
577 ei_obp(count >> 8, base + EN0_RCNTHI);
578 ei_obp(0x00, base + EN0_RSARLO);
579 ei_obp(start_page, base + EN0_RSARHI);
580 #endif /* 0/1 */
582 ei_obp(E8390_RWRITE + E8390_START, base + E8390_CMD);
583 if (ei_status.word16) {
584 ei_osw(E8390_BASE + E8390_DATA, buf, count >> 1);
585 } else {
586 ei_osb(E8390_BASE + E8390_DATA, buf, count);
589 #ifdef OAKNET_DISINT
590 restore_flags(flags);
591 #endif
593 start = jiffies;
595 #ifdef OAKNET_HEADCHECK
597 * This was for the ALPHA version only, but enough people have
598 * been encountering problems so it is still here.
602 /* DMA termination address check... */
603 int addr, tries = 20;
604 do {
605 int high = ei_ibp(base + EN0_RSARHI);
606 int low = ei_ibp(base + EN0_RSARLO);
607 addr = (high << 8) + low;
608 if ((start_page << 8) + count == addr)
609 break;
610 } while (--tries > 0);
612 if (tries <= 0) {
613 printk("%s: Tx packet transfer address mismatch,"
614 "%#4.4x (expected) vs. %#4.4x (actual).\n",
615 dev->name, (start_page << 8) + count, addr);
616 if (retries++ == 0)
617 goto retry;
620 #endif
622 while ((ei_ibp(base + EN0_ISR) & ENISR_RDC) == 0) {
623 if (jiffies - start > OAKNET_WAIT) {
624 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
625 oaknet_reset_8390(dev);
626 NS8390_init(dev, TRUE);
627 break;
631 ei_obp(ENISR_RDC, base + EN0_ISR); /* Ack intr. */
632 ei_status.dmaing &= ~0x01;
636 * static void oaknet_dma_error()
638 * Description:
639 * This routine prints out a last-ditch informative message to the console
640 * indicating that a DMA error occurred. If you see this, it's the last
641 * thing you'll see.
643 * Input(s):
644 * *dev - Pointer to the device structure for this driver.
645 * *name - Informative text (e.g. function name) indicating where the
646 * DMA error occurred.
648 * Output(s):
649 * N/A
651 * Returns:
652 * N/A
655 static void
656 oaknet_dma_error(struct net_device *dev, const char *name)
658 printk(KERN_EMERG "%s: DMAing conflict in %s."
659 "[DMAstat:%d][irqlock:%d][intr:%ld]\n",
660 dev->name, name, ei_status.dmaing, ei_status.irqlock,
661 dev->interrupt);
665 * Oak Ethernet module load interface.
667 static int __init oaknet_init_module (void)
669 if (oaknet_devs != NULL)
670 return (-EBUSY);
672 return (oaknet_init());
676 * Oak Ethernet module unload interface.
678 static void __exit oaknet_cleanup_module (void)
680 if (oaknet_devs == NULL)
681 return;
683 if (oaknet_devs->priv != NULL) {
684 int ioaddr = oaknet_devs->base_addr;
685 void *priv = oaknet_devs->priv;
686 free_irq(oaknet_devs->irq, oaknet_devs);
687 release_region(ioaddr, OAKNET_IO_SIZE);
688 iounmap(ioaddr);
689 unregister_netdev(oaknet_dev);
690 kfree(priv);
693 /* Convert to loop once driver supports multiple devices. */
694 kfree(oaknet_devs);
697 module_init(oaknet_init_module);
698 module_exit(oaknet_cleanup_module);
699 MODULE_LICENSE("GPL");