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
= 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
);
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)
97 struct net_device tmp
, *dev
= NULL
;
99 unsigned long ioaddr
= OAKNET_IO_BASE
;
101 unsigned long ioaddr
= ioremap(OAKNET_IO_BASE
, OAKNET_IO_SIZE
);
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
;
113 if (!request_region(OAKNET_IO_BASE
, OAKNET_IO_SIZE
, name
))
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
);
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);
142 release_region(dev
->base_addr
, OAKNET_IO_SIZE
);
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);
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
);
171 * Disable all chip interrupts for now and ACK all pending
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
);
184 release_region(dev
->base_addr
, OAKNET_IO_SIZE
);
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
);
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
);
246 * static int oaknet_close()
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.
254 * *dev - Pointer to the device structure for this driver.
257 * *dev - Pointer to the device structure for this driver, potentially
258 * modified by ei_close.
261 * 0 if OK, otherwise < 0 on error.
265 oaknet_close(struct net_device
*dev
)
267 int status
= ei_close(dev
);
273 * static void oaknet_reset_8390()
276 * This routine resets the DP83902 chip.
279 * *dev - Pointer to the device structure for this driver.
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
);
302 ei_status
.dmaing
= 0;
308 * static void oaknet_get_8390_hdr()
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.
316 * *dev - Pointer to the device structure for this driver.
317 * *hdr - Pointer to storage for the 8390-specific packet header.
321 * *hdr - Pointer to the 8390-specific packet header for the just-
329 oaknet_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
,
332 int base
= dev
->base_addr
;
335 * This should NOT happen. If it does, it is the LAST thing you'll
339 if (ei_status
.dmaing
) {
340 oaknet_dma_error(dev
, "oaknet_get_8390_hdr");
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);
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;
373 oaknet_block_input(struct net_device
*dev
, int count
, struct sk_buff
*skb
,
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
384 if (ei_status
.dmaing
) {
385 oaknet_dma_error(dev
, "oaknet_block_input");
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);
404 buf
[count
- 1] = ei_ib(base
+ E8390_DATA
);
405 #ifdef OAKNET_HEADCHECK
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;
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
)
431 } while (--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
);
438 ei_obp(ENISR_RDC
, base
+ EN0_ISR
); /* ACK Remote DMA interrupt */
439 ei_status
.dmaing
&= ~0x01;
442 restore_flags(flags
);
449 * static void oaknet_block_output()
455 * *dev - Pointer to the device structure for this driver.
456 * count - Number of bytes to be transferred.
468 oaknet_block_output(struct net_device
*dev
, int count
,
469 const unsigned char *buf
, int start_page
)
471 int base
= E8390_BASE
;
479 #ifdef OAKNET_HEADCHECK
483 /* Round the count up for word writes. */
485 if (ei_status
.word16
&& (count
& 0x1))
489 * This should NOT happen. If it does, it is the LAST thing you'll
493 if (ei_status
.dmaing
) {
494 oaknet_dma_error(dev
, "oaknet_block_output");
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
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.
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
);
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
);
543 nrdhi
= ei_ibp(base
+ EN0_CRDAHI
);
544 nrdlo
= ei_ibp(base
+ EN0_CRDALO
);
545 if ((rdhi
!= nrdhi
) || (rdlo
!= nrdlo
))
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. */
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
);
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);
580 ei_osb(E8390_BASE
+ E8390_DATA
, buf
, count
);
584 restore_flags(flags
);
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;
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
)
604 } while (--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
);
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
);
625 ei_obp(ENISR_RDC
, base
+ EN0_ISR
); /* Ack intr. */
626 ei_status
.dmaing
&= ~0x01;
632 * static void oaknet_dma_error()
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
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.
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
,
663 * Oak Ethernet module load interface.
665 static int __init
oaknet_init_module (void)
670 * We're dependent on the 8390 generic driver module, make
671 * sure its symbols are loaded.
674 if (load_8390_module("oaknet.c"))
677 if (oaknet_devs
!= NULL
)
680 status
= oaknet_init()
683 unload_8390_module();
689 * Oak Ethernet module unload interface.
691 static void __exit
oaknet_cleanup_module (void)
693 if (oaknet_devs
== NULL
)
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
);
707 unload_8390_module();
710 module_init(oaknet_init_module
);
711 module_exit(oaknet_cleanup_module
);