3 * Copyright (c) 1999-2000 Grant Erickson <grant@lcse.umn.edu>
5 * Module name: oaknet.c
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>
30 /* Preprocessor Defines */
32 #if !defined(TRUE) || TRUE != 1
36 #if !defined(FALSE) || FALSE != 0
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... */
48 #define OAKNET_HEADCHECK
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
);
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.
90 * 0 if OK, otherwise system error number on error.
93 static int __init
oaknet_init(void)
98 struct net_device tmp
, *dev
= NULL
;
100 unsigned long ioaddr
= OAKNET_IO_BASE
;
102 unsigned long ioaddr
= ioremap(OAKNET_IO_BASE
, OAKNET_IO_SIZE
);
104 bd_t
*bip
= (bd_t
*)__res
;
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
;
117 if (!request_region(OAKNET_IO_BASE
, OAKNET_IO_SIZE
, name
))
120 /* Quick register check to see if the device is really there. */
123 if ((reg0
= ei_ibp(ioaddr
)) == 0xFF)
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. */
141 if (ei_ibp(ioaddr
+ EN0_COUNTER0
) != 0) {
142 ei_obp(reg0
, ioaddr
);
143 ei_obp(regd
, ioaddr
+ 0x0D);
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);
156 SET_MODULE_OWNER(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. */
170 if (ethdev_init(dev
)) {
171 printk(" unable to get memory for dev->priv.\n");
176 * Disable all chip interrupts for now and ACK all pending
180 ei_obp(0x0, ioaddr
+ EN0_IMR
);
181 ei_obp(0xFF, ioaddr
+ EN0_ISR
);
183 /* Attempt to get the interrupt line */
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
);
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
);
223 unregister_netdev(dev
);
226 release_region(OAKNET_IO_BASE
, OAKNET_IO_SIZE
);
233 * static int oaknet_open()
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.
241 * *dev - Pointer to the device structure for this driver.
244 * *dev - Pointer to the device structure for this driver, potentially
245 * modified by ei_open.
248 * 0 if OK, otherwise < 0 on error.
252 oaknet_open(struct net_device
*dev
)
254 int status
= ei_open(dev
);
259 * static int oaknet_close()
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.
267 * *dev - Pointer to the device structure for this driver.
270 * *dev - Pointer to the device structure for this driver, potentially
271 * modified by ei_close.
274 * 0 if OK, otherwise < 0 on error.
278 oaknet_close(struct net_device
*dev
)
280 int status
= ei_close(dev
);
285 * static void oaknet_reset_8390()
288 * This routine resets the DP83902 chip.
291 * *dev - Pointer to the device structure for this driver.
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
);
314 ei_status
.dmaing
= 0;
318 * static void oaknet_get_8390_hdr()
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.
326 * *dev - Pointer to the device structure for this driver.
327 * *hdr - Pointer to storage for the 8390-specific packet header.
331 * *hdr - Pointer to the 8390-specific packet header for the just-
339 oaknet_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
,
342 int base
= dev
->base_addr
;
345 * This should NOT happen. If it does, it is the LAST thing you'll
349 if (ei_status
.dmaing
) {
350 oaknet_dma_error(dev
, "oaknet_get_8390_hdr");
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);
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;
381 oaknet_block_input(struct net_device
*dev
, int count
, struct sk_buff
*skb
,
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
392 if (ei_status
.dmaing
) {
393 oaknet_dma_error(dev
, "oaknet_block_input");
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);
412 buf
[count
- 1] = ei_ib(base
+ E8390_DATA
);
413 #ifdef OAKNET_HEADCHECK
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;
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
)
439 } while (--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
);
446 ei_obp(ENISR_RDC
, base
+ EN0_ISR
); /* ACK Remote DMA interrupt */
447 ei_status
.dmaing
&= ~0x01;
450 restore_flags(flags
);
455 * static void oaknet_block_output()
461 * *dev - Pointer to the device structure for this driver.
462 * count - Number of bytes to be transferred.
474 oaknet_block_output(struct net_device
*dev
, int count
,
475 const unsigned char *buf
, int start_page
)
477 int base
= E8390_BASE
;
485 #ifdef OAKNET_HEADCHECK
489 /* Round the count up for word writes. */
491 if (ei_status
.word16
&& (count
& 0x1))
495 * This should NOT happen. If it does, it is the LAST thing you'll
499 if (ei_status
.dmaing
) {
500 oaknet_dma_error(dev
, "oaknet_block_output");
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
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.
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
);
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
);
549 nrdhi
= ei_ibp(base
+ EN0_CRDAHI
);
550 nrdlo
= ei_ibp(base
+ EN0_CRDALO
);
551 if ((rdhi
!= nrdhi
) || (rdlo
!= nrdlo
))
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. */
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
);
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);
586 ei_osb(E8390_BASE
+ E8390_DATA
, buf
, count
);
590 restore_flags(flags
);
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;
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
)
610 } while (--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
);
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
);
631 ei_obp(ENISR_RDC
, base
+ EN0_ISR
); /* Ack intr. */
632 ei_status
.dmaing
&= ~0x01;
636 * static void oaknet_dma_error()
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
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.
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
,
665 * Oak Ethernet module load interface.
667 static int __init
oaknet_init_module (void)
669 if (oaknet_devs
!= NULL
)
672 return (oaknet_init());
676 * Oak Ethernet module unload interface.
678 static void __exit
oaknet_cleanup_module (void)
680 if (oaknet_devs
== NULL
)
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
);
689 unregister_netdev(oaknet_dev
);
693 /* Convert to loop once driver supports multiple devices. */
697 module_init(oaknet_init_module
);
698 module_exit(oaknet_cleanup_module
);
699 MODULE_LICENSE("GPL");