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
*dev
;
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 dev
= alloc_ei_netdev();
114 if (!request_region(OAKNET_IO_BASE
, OAKNET_IO_SIZE
, name
))
117 /* Quick register check to see if the device is really there. */
120 if ((reg0
= ei_ibp(ioaddr
)) == 0xFF)
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. */
138 if (ei_ibp(ioaddr
+ EN0_COUNTER0
) != 0) {
139 ei_obp(reg0
, ioaddr
);
140 ei_obp(regd
, ioaddr
+ 0x0D);
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
159 ei_obp(0x0, ioaddr
+ EN0_IMR
);
160 ei_obp(0xFF, ioaddr
+ EN0_ISR
);
162 /* Attempt to get the interrupt line */
165 if (request_irq(dev
->irq
, ei_interrupt
, 0, name
, dev
)) {
166 printk("%s: unable to request interrupt %d.\n",
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
;
199 NS8390_init(dev
, FALSE
);
200 ret
= register_netdev(dev
);
208 free_irq(dev
->irq
, dev
);
210 release_region(OAKNET_IO_BASE
, OAKNET_IO_SIZE
);
219 * static int oaknet_open()
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.
227 * *dev - Pointer to the device structure for this driver.
230 * *dev - Pointer to the device structure for this driver, potentially
231 * modified by ei_open.
234 * 0 if OK, otherwise < 0 on error.
238 oaknet_open(struct net_device
*dev
)
240 int status
= ei_open(dev
);
245 * static int oaknet_close()
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.
253 * *dev - Pointer to the device structure for this driver.
256 * *dev - Pointer to the device structure for this driver, potentially
257 * modified by ei_close.
260 * 0 if OK, otherwise < 0 on error.
264 oaknet_close(struct net_device
*dev
)
266 int status
= ei_close(dev
);
271 * static void oaknet_reset_8390()
274 * This routine resets the DP83902 chip.
277 * *dev - Pointer to the device structure for this driver.
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
);
300 ei_status
.dmaing
= 0;
304 * static void oaknet_get_8390_hdr()
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.
312 * *dev - Pointer to the device structure for this driver.
313 * *hdr - Pointer to storage for the 8390-specific packet header.
317 * *hdr - Pointer to the 8390-specific packet header for the just-
325 oaknet_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
,
328 int base
= dev
->base_addr
;
331 * This should NOT happen. If it does, it is the LAST thing you'll
335 if (ei_status
.dmaing
) {
336 oaknet_dma_error(dev
, "oaknet_get_8390_hdr");
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);
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;
367 oaknet_block_input(struct net_device
*dev
, int count
, struct sk_buff
*skb
,
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
378 if (ei_status
.dmaing
) {
379 oaknet_dma_error(dev
, "oaknet_block_input");
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);
398 buf
[count
- 1] = ei_ib(base
+ E8390_DATA
);
399 #ifdef OAKNET_HEADCHECK
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;
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
)
425 } while (--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
);
432 ei_obp(ENISR_RDC
, base
+ EN0_ISR
); /* ACK Remote DMA interrupt */
433 ei_status
.dmaing
&= ~0x01;
436 restore_flags(flags
);
441 * static void oaknet_block_output()
447 * *dev - Pointer to the device structure for this driver.
448 * count - Number of bytes to be transferred.
460 oaknet_block_output(struct net_device
*dev
, int count
,
461 const unsigned char *buf
, int start_page
)
463 int base
= E8390_BASE
;
471 #ifdef OAKNET_HEADCHECK
475 /* Round the count up for word writes. */
477 if (ei_status
.word16
&& (count
& 0x1))
481 * This should NOT happen. If it does, it is the LAST thing you'll
485 if (ei_status
.dmaing
) {
486 oaknet_dma_error(dev
, "oaknet_block_output");
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
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.
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
);
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
);
535 nrdhi
= ei_ibp(base
+ EN0_CRDAHI
);
536 nrdlo
= ei_ibp(base
+ EN0_CRDALO
);
537 if ((rdhi
!= nrdhi
) || (rdlo
!= nrdlo
))
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. */
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
);
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);
572 ei_osb(E8390_BASE
+ E8390_DATA
, buf
, count
);
576 restore_flags(flags
);
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;
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
)
596 } while (--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
);
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
);
617 ei_obp(ENISR_RDC
, base
+ EN0_ISR
); /* Ack intr. */
618 ei_status
.dmaing
&= ~0x01;
622 * static void oaknet_dma_error()
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
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.
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
,
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
);
660 free_netdev(oaknet_devs
);
663 module_init(oaknet_init
);
664 module_exit(oaknet_cleanup_module
);
665 MODULE_LICENSE("GPL");