[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / drivers / net / oaknet.c
blob62167a29debe304a4913e3027fbdf5127c6ac8d4
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 = -ENOMEM;
98 struct net_device *dev;
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 dev = alloc_ei_netdev();
110 if (!dev)
111 goto out_unmap;
113 ret = -EBUSY;
114 if (!request_region(OAKNET_IO_BASE, OAKNET_IO_SIZE, name))
115 goto out_dev;
117 /* Quick register check to see if the device is really there. */
119 ret = -ENODEV;
120 if ((reg0 = ei_ibp(ioaddr)) == 0xFF)
121 goto out_region;
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 ret = -ENODEV;
138 if (ei_ibp(ioaddr + EN0_COUNTER0) != 0) {
139 ei_obp(reg0, ioaddr);
140 ei_obp(regd, ioaddr + 0x0D);
141 goto out_region;
144 SET_MODULE_OWNER(dev);
147 * This controller is on an embedded board, so the base address
148 * and interrupt assignments are pre-assigned and unchageable.
151 dev->base_addr = ioaddr;
152 dev->irq = OAKNET_INT;
155 * Disable all chip interrupts for now and ACK all pending
156 * interrupts.
159 ei_obp(0x0, ioaddr + EN0_IMR);
160 ei_obp(0xFF, ioaddr + EN0_ISR);
162 /* Attempt to get the interrupt line */
164 ret = -EAGAIN;
165 if (request_irq(dev->irq, ei_interrupt, 0, name, dev)) {
166 printk("%s: unable to request interrupt %d.\n",
167 name, dev->irq);
168 goto out_region;
171 /* Tell the world about what and where we've found. */
173 printk("%s: %s at", dev->name, name);
174 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
175 dev->dev_addr[i] = bip->bi_enetaddr[i];
176 printk("%c%.2x", (i ? ':' : ' '), dev->dev_addr[i]);
178 printk(", found at %#lx, using IRQ %d.\n", dev->base_addr, dev->irq);
180 /* Set up some required driver fields and then we're done. */
182 ei_status.name = name;
183 ei_status.word16 = FALSE;
184 ei_status.tx_start_page = OAKNET_START_PG;
185 ei_status.rx_start_page = OAKNET_START_PG + TX_PAGES;
186 ei_status.stop_page = OAKNET_STOP_PG;
188 ei_status.reset_8390 = &oaknet_reset_8390;
189 ei_status.block_input = &oaknet_block_input;
190 ei_status.block_output = &oaknet_block_output;
191 ei_status.get_8390_hdr = &oaknet_get_8390_hdr;
193 dev->open = oaknet_open;
194 dev->stop = oaknet_close;
195 #ifdef CONFIG_NET_POLL_CONTROLLER
196 dev->poll_controller = ei_poll;
197 #endif
199 NS8390_init(dev, FALSE);
200 ret = register_netdev(dev);
201 if (ret)
202 goto out_irq;
204 oaknet_devs = dev;
205 return 0;
207 out_irq;
208 free_irq(dev->irq, dev);
209 out_region:
210 release_region(OAKNET_IO_BASE, OAKNET_IO_SIZE);
211 out_dev:
212 free_netdev(dev);
213 out_unmap:
214 iounmap(ioaddr);
215 return ret;
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 return (status);
245 * static int oaknet_close()
247 * Description:
248 * This routine is a modest wrapper around ei_close, the 8390-generic,
249 * driver close routine. This just decrements the module usage count
250 * and passes along the status from ei_close.
252 * Input(s):
253 * *dev - Pointer to the device structure for this driver.
255 * Output(s):
256 * *dev - Pointer to the device structure for this driver, potentially
257 * modified by ei_close.
259 * Returns:
260 * 0 if OK, otherwise < 0 on error.
263 static int
264 oaknet_close(struct net_device *dev)
266 int status = ei_close(dev);
267 return (status);
271 * static void oaknet_reset_8390()
273 * Description:
274 * This routine resets the DP83902 chip.
276 * Input(s):
277 * *dev - Pointer to the device structure for this driver.
279 * Output(s):
280 * N/A
282 * Returns:
283 * N/A
286 static void
287 oaknet_reset_8390(struct net_device *dev)
289 int base = E8390_BASE;
292 * We have no provision of reseting the controller as is done
293 * in other drivers, such as "ne.c". However, the following
294 * seems to work well enough in the TiVo driver.
297 printk("Resetting %s...\n", dev->name);
298 ei_obp(E8390_STOP | E8390_NODMA | E8390_PAGE0, base + E8390_CMD);
299 ei_status.txing = 0;
300 ei_status.dmaing = 0;
304 * static void oaknet_get_8390_hdr()
306 * Description:
307 * This routine grabs the 8390-specific header. It's similar to the
308 * block input routine, but we don't need to be concerned with ring wrap
309 * as the header will be at the start of a page, so we optimize accordingly.
311 * Input(s):
312 * *dev - Pointer to the device structure for this driver.
313 * *hdr - Pointer to storage for the 8390-specific packet header.
314 * ring_page - ?
316 * Output(s):
317 * *hdr - Pointer to the 8390-specific packet header for the just-
318 * received frame.
320 * Returns:
321 * N/A
324 static void
325 oaknet_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
326 int ring_page)
328 int base = dev->base_addr;
331 * This should NOT happen. If it does, it is the LAST thing you'll
332 * see.
335 if (ei_status.dmaing) {
336 oaknet_dma_error(dev, "oaknet_get_8390_hdr");
337 return;
340 ei_status.dmaing |= 0x01;
341 outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, base + OAKNET_CMD);
342 outb_p(sizeof(struct e8390_pkt_hdr), base + EN0_RCNTLO);
343 outb_p(0, base + EN0_RCNTHI);
344 outb_p(0, base + EN0_RSARLO); /* On page boundary */
345 outb_p(ring_page, base + EN0_RSARHI);
346 outb_p(E8390_RREAD + E8390_START, base + OAKNET_CMD);
348 if (ei_status.word16)
349 insw(base + OAKNET_DATA, hdr,
350 sizeof(struct e8390_pkt_hdr) >> 1);
351 else
352 insb(base + OAKNET_DATA, hdr,
353 sizeof(struct e8390_pkt_hdr));
355 /* Byte-swap the packet byte count */
357 hdr->count = le16_to_cpu(hdr->count);
359 outb_p(ENISR_RDC, base + EN0_ISR); /* ACK Remote DMA interrupt */
360 ei_status.dmaing &= ~0x01;
364 * XXX - Document me.
366 static void
367 oaknet_block_input(struct net_device *dev, int count, struct sk_buff *skb,
368 int ring_offset)
370 int base = OAKNET_BASE;
371 char *buf = skb->data;
374 * This should NOT happen. If it does, it is the LAST thing you'll
375 * see.
378 if (ei_status.dmaing) {
379 oaknet_dma_error(dev, "oaknet_block_input");
380 return;
383 #ifdef OAKNET_DISINT
384 save_flags(flags);
385 cli();
386 #endif
388 ei_status.dmaing |= 0x01;
389 ei_obp(E8390_NODMA + E8390_PAGE0 + E8390_START, base + E8390_CMD);
390 ei_obp(count & 0xff, base + EN0_RCNTLO);
391 ei_obp(count >> 8, base + EN0_RCNTHI);
392 ei_obp(ring_offset & 0xff, base + EN0_RSARLO);
393 ei_obp(ring_offset >> 8, base + EN0_RSARHI);
394 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
395 if (ei_status.word16) {
396 ei_isw(base + E8390_DATA, buf, count >> 1);
397 if (count & 0x01) {
398 buf[count - 1] = ei_ib(base + E8390_DATA);
399 #ifdef OAKNET_HEADCHECK
400 bytes++;
401 #endif
403 } else {
404 ei_isb(base + E8390_DATA, buf, count);
406 #ifdef OAKNET_HEADCHECK
408 * This was for the ALPHA version only, but enough people have
409 * been encountering problems so it is still here. If you see
410 * this message you either 1) have a slightly incompatible clone
411 * or 2) have noise/speed problems with your bus.
414 /* DMA termination address check... */
416 int addr, tries = 20;
417 do {
418 /* DON'T check for 'ei_ibp(EN0_ISR) & ENISR_RDC' here
419 -- it's broken for Rx on some cards! */
420 int high = ei_ibp(base + EN0_RSARHI);
421 int low = ei_ibp(base + EN0_RSARLO);
422 addr = (high << 8) + low;
423 if (((ring_offset + bytes) & 0xff) == low)
424 break;
425 } while (--tries > 0);
426 if (tries <= 0)
427 printk("%s: RX transfer address mismatch,"
428 "%#4.4x (expected) vs. %#4.4x (actual).\n",
429 dev->name, ring_offset + bytes, addr);
431 #endif
432 ei_obp(ENISR_RDC, base + EN0_ISR); /* ACK Remote DMA interrupt */
433 ei_status.dmaing &= ~0x01;
435 #ifdef OAKNET_DISINT
436 restore_flags(flags);
437 #endif
441 * static void oaknet_block_output()
443 * Description:
444 * This routine...
446 * Input(s):
447 * *dev - Pointer to the device structure for this driver.
448 * count - Number of bytes to be transferred.
449 * *buf -
450 * start_page -
452 * Output(s):
453 * N/A
455 * Returns:
456 * N/A
459 static void
460 oaknet_block_output(struct net_device *dev, int count,
461 const unsigned char *buf, int start_page)
463 int base = E8390_BASE;
464 #if 0
465 int bug;
466 #endif
467 unsigned long start;
468 #ifdef OAKNET_DISINT
469 unsigned long flags;
470 #endif
471 #ifdef OAKNET_HEADCHECK
472 int retries = 0;
473 #endif
475 /* Round the count up for word writes. */
477 if (ei_status.word16 && (count & 0x1))
478 count++;
481 * This should NOT happen. If it does, it is the LAST thing you'll
482 * see.
485 if (ei_status.dmaing) {
486 oaknet_dma_error(dev, "oaknet_block_output");
487 return;
490 #ifdef OAKNET_DISINT
491 save_flags(flags);
492 cli();
493 #endif
495 ei_status.dmaing |= 0x01;
497 /* Make sure we are in page 0. */
499 ei_obp(E8390_PAGE0 + E8390_START + E8390_NODMA, base + E8390_CMD);
501 #ifdef OAKNET_HEADCHECK
502 retry:
503 #endif
505 #if 0
507 * The 83902 documentation states that the processor needs to
508 * do a "dummy read" before doing the remote write to work
509 * around a chip bug they don't feel like fixing.
512 bug = 0;
513 while (1) {
514 unsigned int rdhi;
515 unsigned int rdlo;
517 /* Now the normal output. */
518 ei_obp(ENISR_RDC, base + EN0_ISR);
519 ei_obp(count & 0xff, base + EN0_RCNTLO);
520 ei_obp(count >> 8, base + EN0_RCNTHI);
521 ei_obp(0x00, base + EN0_RSARLO);
522 ei_obp(start_page, base + EN0_RSARHI);
524 if (bug++)
525 break;
527 /* Perform the dummy read */
528 rdhi = ei_ibp(base + EN0_CRDAHI);
529 rdlo = ei_ibp(base + EN0_CRDALO);
530 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
532 while (1) {
533 unsigned int nrdhi;
534 unsigned int nrdlo;
535 nrdhi = ei_ibp(base + EN0_CRDAHI);
536 nrdlo = ei_ibp(base + EN0_CRDALO);
537 if ((rdhi != nrdhi) || (rdlo != nrdlo))
538 break;
541 #else
542 #ifdef OAKNET_RWFIX
544 * Handle the read-before-write bug the same way as the
545 * Crynwr packet driver -- the Nat'l Semi. method doesn't work.
546 * Actually this doesn't always work either, but if you have
547 * problems with your 83902 this is better than nothing!
550 ei_obp(0x42, base + EN0_RCNTLO);
551 ei_obp(0x00, base + EN0_RCNTHI);
552 ei_obp(0x42, base + EN0_RSARLO);
553 ei_obp(0x00, base + EN0_RSARHI);
554 ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
555 /* Make certain that the dummy read has occurred. */
556 udelay(6);
557 #endif
559 ei_obp(ENISR_RDC, base + EN0_ISR);
561 /* Now the normal output. */
562 ei_obp(count & 0xff, base + EN0_RCNTLO);
563 ei_obp(count >> 8, base + EN0_RCNTHI);
564 ei_obp(0x00, base + EN0_RSARLO);
565 ei_obp(start_page, base + EN0_RSARHI);
566 #endif /* 0/1 */
568 ei_obp(E8390_RWRITE + E8390_START, base + E8390_CMD);
569 if (ei_status.word16) {
570 ei_osw(E8390_BASE + E8390_DATA, buf, count >> 1);
571 } else {
572 ei_osb(E8390_BASE + E8390_DATA, buf, count);
575 #ifdef OAKNET_DISINT
576 restore_flags(flags);
577 #endif
579 start = jiffies;
581 #ifdef OAKNET_HEADCHECK
583 * This was for the ALPHA version only, but enough people have
584 * been encountering problems so it is still here.
588 /* DMA termination address check... */
589 int addr, tries = 20;
590 do {
591 int high = ei_ibp(base + EN0_RSARHI);
592 int low = ei_ibp(base + EN0_RSARLO);
593 addr = (high << 8) + low;
594 if ((start_page << 8) + count == addr)
595 break;
596 } while (--tries > 0);
598 if (tries <= 0) {
599 printk("%s: Tx packet transfer address mismatch,"
600 "%#4.4x (expected) vs. %#4.4x (actual).\n",
601 dev->name, (start_page << 8) + count, addr);
602 if (retries++ == 0)
603 goto retry;
606 #endif
608 while ((ei_ibp(base + EN0_ISR) & ENISR_RDC) == 0) {
609 if (jiffies - start > OAKNET_WAIT) {
610 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
611 oaknet_reset_8390(dev);
612 NS8390_init(dev, TRUE);
613 break;
617 ei_obp(ENISR_RDC, base + EN0_ISR); /* Ack intr. */
618 ei_status.dmaing &= ~0x01;
622 * static void oaknet_dma_error()
624 * Description:
625 * This routine prints out a last-ditch informative message to the console
626 * indicating that a DMA error occurred. If you see this, it's the last
627 * thing you'll see.
629 * Input(s):
630 * *dev - Pointer to the device structure for this driver.
631 * *name - Informative text (e.g. function name) indicating where the
632 * DMA error occurred.
634 * Output(s):
635 * N/A
637 * Returns:
638 * N/A
641 static void
642 oaknet_dma_error(struct net_device *dev, const char *name)
644 printk(KERN_EMERG "%s: DMAing conflict in %s."
645 "[DMAstat:%d][irqlock:%d][intr:%ld]\n",
646 dev->name, name, ei_status.dmaing, ei_status.irqlock,
647 dev->interrupt);
651 * Oak Ethernet module unload interface.
653 static void __exit oaknet_cleanup_module (void)
655 /* Convert to loop once driver supports multiple devices. */
656 unregister_netdev(oaknet_dev);
657 free_irq(oaknet_devs->irq, oaknet_devs);
658 release_region(oaknet_devs->base_addr, OAKNET_IO_SIZE);
659 iounmap(ioaddr);
660 free_netdev(oaknet_devs);
663 module_init(oaknet_init);
664 module_exit(oaknet_cleanup_module);
665 MODULE_LICENSE("GPL");