- pre3:
[davej-history.git] / drivers / net / ncr885e.c
blobe9f724288ade693fcb23bfb9546249dc13c2400d
1 /*
2 * An Ethernet driver for the dual-function NCR 53C885 SCSI/Ethernet
3 * controller.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 static const char *version =
14 "ncr885e.c:v1.0 02/10/00 dan@synergymicro.com, cort@fsmlabs.com\n";
16 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/version.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/ptrace.h>
24 #include <linux/malloc.h>
25 #include <linux/netdevice.h>
26 #include <linux/pci.h>
27 #include <linux/delay.h>
28 #include <linux/ioport.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <asm/io.h>
32 #include <asm/dbdma.h>
33 #include <asm/uaccess.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
38 #include "ncr885e.h"
39 #include "ncr885_debug.h"
41 static const char *chipname = "ncr885e";
43 /* debugging flags */
44 #if 0
45 #define DEBUG_FUNC 0x0001
46 #define DEBUG_PACKET 0x0002
47 #define DEBUG_CMD 0x0004
48 #define DEBUG_CHANNEL 0x0008
49 #define DEBUG_INT 0x0010
50 #define DEBUG_RX 0x0020
51 #define DEBUG_TX 0x0040
52 #define DEBUG_DMA 0x0080
53 #define DEBUG_MAC 0x0100
54 #define DEBUG_DRIVER 0x0200
55 #define DEBUG_ALL 0x1fff
56 #endif
58 #ifdef DEBUG_NCR885E
59 #define NCR885E_DEBUG 0
60 #else
61 #define NCR885E_DEBUG 0
62 #endif
64 /* The 885's Ethernet PCI device id. */
65 #ifndef PCI_DEVICE_ID_NCR_53C885_ETHERNET
66 #define PCI_DEVICE_ID_NCR_53C885_ETHERNET 0x0701
67 #endif
69 #define NR_RX_RING 8
70 #define NR_TX_RING 8
71 #define MAX_TX_ACTIVE (NR_TX_RING-1)
72 #define NCMDS_TX NR_TX_RING
74 #define RX_BUFLEN (ETH_FRAME_LEN + 8)
75 #define TX_TIMEOUT 5*HZ
77 #define NCR885E_TOTAL_SIZE 0xe0
79 #define TXSR (1<<6) /* tx: xfer status written */
80 #define TXABORT (1<<7) /* tx: abort */
81 #define EOP (1<<7) /* rx: end of packet written to buffer */
83 int ncr885e_debug = NCR885E_DEBUG;
84 static int print_version = 0;
86 struct ncr885e_private {
88 /* preserve a 1-1 marking with buffs */
89 struct dbdma_cmd *head;
90 struct dbdma_cmd *tx_cmds;
91 struct dbdma_cmd *rx_cmds;
92 struct dbdma_cmd *stop_cmd;
94 struct sk_buff *tx_skbufs[NR_TX_RING];
95 struct sk_buff *rx_skbufs[NR_RX_RING];
97 int rx_current;
98 int rx_dirty;
100 int tx_dirty;
101 int tx_current;
103 unsigned short tx_status[NR_TX_RING];
105 unsigned char tx_fullup;
106 unsigned char tx_active;
108 struct net_device_stats stats;
110 struct net_device *dev;
112 struct timer_list tx_timeout;
113 int timeout_active;
115 spinlock_t lock;
118 static struct net_device *root_dev = NULL;
120 static int ncr885e_open( struct net_device *dev );
121 static int ncr885e_close( struct net_device *dev );
122 static void ncr885e_rx( struct net_device *dev );
123 static void ncr885e_tx( struct net_device *dev );
124 static int ncr885e_probe1( unsigned long ioaddr, unsigned char irq );
125 static int ncr885e_xmit_start( struct sk_buff *skb, struct net_device *dev );
126 static struct net_device_stats *ncr885e_stats( struct net_device *dev );
127 static void ncr885e_set_multicast( struct net_device *dev );
128 static void ncr885e_config( struct net_device *dev );
129 static int ncr885e_set_address( struct net_device *dev, void *addr );
130 static void ncr885e_interrupt( int irq, void *dev_id, struct pt_regs *regs );
131 static void show_dbdma_cmd( volatile struct dbdma_cmd *cmd );
132 #if 0
133 static int read_eeprom( unsigned int ioadddr, int location );
134 #endif
136 #ifdef NCR885E_DEBUG_MII
137 static void show_mii( unsigned long ioaddr );
138 static int read_mii( unsigned long ioaddr, int reg );
139 static void write_mii( unsigned long ioaddr, int reg, int data );
140 #endif /* NCR885E_DEBUG_MII */
142 #define TX_RESET_FLAGS (TX_CHANNEL_RUN|TX_CHANNEL_PAUSE|TX_CHANNEL_WAKE)
143 #define RX_RESET_FLAGS (RX_CHANNEL_RUN|RX_CHANNEL_PAUSE|RX_CHANNEL_WAKE)
146 #if 0
147 static int
148 debug_ioctl( struct net_device *dev, struct ifreq *req, int cmd )
150 unsigned long ioaddr = dev->base_addr;
151 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
152 struct ncr885e_private *data;
153 struct ncr885e_regs *regs;
154 unsigned long flags;
156 union {
157 struct ncr885e_regs dump;
158 struct ncr885e_private priv;
159 } temp;
161 switch( cmd ) {
163 /* dump the rx ring status */
164 case NCR885E_GET_PRIV:
166 data = (struct ncr885e_private *) &req->ifr_data;
168 if ( verify_area(VERIFY_WRITE, &req->ifr_data,
169 sizeof( struct ncr885e_private )))
170 return -EFAULT;
172 memcpy((char *) &temp.priv, sp, sizeof( struct ncr885e_private ));
173 copy_to_user( data, (char *) &temp.priv, sizeof( struct ncr885e_private));
174 break;
176 case NCR885E_GET_REGS:
178 regs = (struct ncr885e_regs *) &req->ifr_data;
180 if ( verify_area( VERIFY_WRITE, &req->ifr_data,
181 sizeof( struct ncr885e_regs )))
182 return -EFAULT;
184 spin_lock_irqsave( &sp->lock, flags );
186 temp.dump.tx_status = inl( ioaddr + TX_CHANNEL_STATUS );
187 temp.dump.rx_status = inl( ioaddr + RX_CHANNEL_STATUS );
188 temp.dump.mac_config = inl( ioaddr + MAC_CONFIG );
189 temp.dump.tx_control = inl( ioaddr + TX_CHANNEL_CONTROL );
190 temp.dump.rx_control = inl( ioaddr + RX_CHANNEL_CONTROL );
191 temp.dump.tx_cmd_ptr = inl( ioaddr + TX_CMD_PTR_LO );
192 temp.dump.rx_cmd_ptr = inl( ioaddr + RX_CMD_PTR_LO );
193 temp.dump.int_status = inl( ioaddr + INTERRUPT_STATUS_REG );
195 spin_unlock_irqrestore( &sp->lock, flags );
196 copy_to_user( regs, (char *) &temp.dump, sizeof( struct ncr885e_regs ));
198 break;
200 default:
201 return -EOPNOTSUPP;
203 return 0;
205 #endif
207 /* Enable interrupts on the 53C885 */
208 static inline void
209 ncr885e_enable( struct net_device *dev )
212 unsigned long ioaddr = dev->base_addr;
213 unsigned short reg;
215 reg = inw(ioaddr + INTERRUPT_ENABLE);
216 outw(reg | INTERRUPT_INTE, ioaddr + INTERRUPT_ENABLE);
219 /* Disable interrupts on the 53c885 */
220 static inline void
221 ncr885e_disable( struct net_device *dev )
224 unsigned long ioaddr = dev->base_addr;
225 unsigned short reg;
227 reg = inw( ioaddr + INTERRUPT_ENABLE );
228 outw( reg & ~INTERRUPT_INTE, ioaddr + INTERRUPT_ENABLE );
232 static inline void
233 ncr885e_reset( struct net_device *dev )
236 unsigned short reg;
237 unsigned long cntl;
238 int i;
239 unsigned long ioaddr = dev->base_addr;
241 if (ncr885e_debug > 1)
242 printk( KERN_INFO "%s: Resetting 53C885...\n", dev->name );
244 /* disable interrupts on the 53C885 */
245 ncr885e_disable( dev );
247 /* disable rx in the MAC */
248 reg = inw( ioaddr + MAC_CONFIG );
249 outw( reg & ~MAC_CONFIG_RXEN, ioaddr + MAC_CONFIG );
251 for( i=0; i < 100; i++ ) {
253 if ( !(inw( ioaddr + MAC_CONFIG ) & MAC_CONFIG_RXEN ))
254 break;
255 udelay( 10 );
258 reg = inw( ioaddr + MAC_CONFIG );
259 outw( reg | MAC_CONFIG_SRST, ioaddr + MAC_CONFIG );
260 outw( reg, ioaddr + MAC_CONFIG );
262 /* disable both rx and tx DBDMA channels */
263 outl( TX_DBDMA_ENABLE << 16, ioaddr + TX_CHANNEL_CONTROL );
264 outl( RX_DBDMA_ENABLE << 16, ioaddr + RX_CHANNEL_CONTROL );
266 for( i=0; i < 100; i++ ) {
268 if ( !(inw( ioaddr + TX_CHANNEL_STATUS ) & TX_DBDMA_ENABLE ) &&
269 !(inw( ioaddr + RX_CHANNEL_STATUS ) & RX_DBDMA_ENABLE ))
270 break;
271 udelay( 10 );
274 /* perform a "software reset" */
275 cntl = inl( ioaddr + DBDMA_CONTROL );
276 outl( cntl | DBDMA_SRST, ioaddr + DBDMA_CONTROL );
278 for( i=0; i < 100; i++ ) {
280 if ( !(inl( ioaddr + DBDMA_CONTROL ) & DBDMA_SRST ))
281 break;
282 udelay( 10 );
285 /* books says that a software reset should be done to the MAC, as
286 well. This true??? */
288 if (ncr885e_debug > 3)
289 printk( KERN_INFO "%s: reset complete\n", dev->name );
294 /* configure the 53C885 chip.
296 The DBDMA command descriptors on the 53C885 can be programmed to
297 branch, interrupt or pause conditionally or always by using the
298 interrupt, branch and wait select registers. */
300 static void
301 ncr885e_config( struct net_device *dev )
304 unsigned long ioaddr = dev->base_addr;
306 if (ncr885e_debug > 3)
307 printk( KERN_INFO "%s: Configuring 53C885.\n", dev->name );
309 ncr885e_reset( dev );
311 /* The 53C885 can be programmed to perform conditional DBDMA
312 branches, interrupts or waits.
314 Neither channel makes use of "wait", as it requires that the
315 DBDMA engine to be restarted. Don't go there. The rx channel
316 will branch upon the successful reception of a packet ('EOP' in
317 the xfer_status field). The branch address is to the STOP
318 DBDMA command descriptor, which shuts down the rx channel until
319 the interrupt is serviced. */
321 /* cause tx channel to stop after "status received" */
322 outl( 0, ioaddr + TX_INT_SELECT );
323 outl( (TX_WAIT_STAT_RECV << 16) | TX_WAIT_STAT_RECV,
324 ioaddr + TX_WAIT_SELECT );
325 outl( 0, ioaddr + TX_BRANCH_SELECT );
327 /* cause rx channel to branch to the STOP descriptor on "End-of-Packet" */
328 #if 0
329 outl( (RX_INT_SELECT_EOP << 16) | RX_INT_SELECT_EOP,
330 ioaddr + RX_INT_SELECT );
331 #else
332 outl( 0, ioaddr + RX_INT_SELECT );
333 #endif
334 #if 0
335 outl( 0, ioaddr + RX_WAIT_SELECT );
336 #else
337 outl( (RX_WAIT_SELECT_EOP << 16) | RX_WAIT_SELECT_EOP,
338 ioaddr + RX_WAIT_SELECT );
339 #endif
340 #if 1
341 outl( 0, ioaddr + RX_BRANCH_SELECT );
342 #else
343 outl( (RX_BRANCH_SELECT_EOP << 16) | RX_BRANCH_SELECT_EOP,
344 ioaddr + RX_BRANCH_SELECT );
345 #endif
347 /* configure DBDMA */
348 outl( (DBDMA_BE | DBDMA_DPMRLE | DBDMA_TDPCE |
349 DBDMA_DDPE | DBDMA_TDPE |
350 (DBDMA_BURST_4 << DBDMA_TX_BST_SHIFT) |
351 (DBDMA_BURST_4 << DBDMA_RX_BST_SHIFT) |
352 (DBDMA_TX_ARBITRATION_DEFAULT) |
353 (DBDMA_RX_ARBITRATION_DEFAULT)), ioaddr + DBDMA_CONTROL );
355 outl( 0, ioaddr + TX_THRESHOLD );
357 /* disable MAC loopback */
358 outl( (MAC_CONFIG_ITXA | MAC_CONFIG_RXEN | MAC_CONFIG_RETRYL |
359 MAC_CONFIG_PADEN | (0x18 << 16)),
360 ioaddr + MAC_CONFIG );
362 /* configure MAC */
363 outl( (MAC_CONFIG_ITXA | MAC_CONFIG_RXEN | MAC_CONFIG_RETRYL |
364 MAC_CONFIG_PADEN | ( 0x18 << 16)), ioaddr + MAC_CONFIG );
366 outw( (0x1018), ioaddr + NBTOB_INTP_GAP );
368 /* clear and enable interrupts */
369 inw( ioaddr + INTERRUPT_CLEAR );
370 ncr885e_enable( dev );
372 /* and enable them in the chip */
373 outl( (INTERRUPT_INTE|INTERRUPT_TX_MASK|INTERRUPT_RX_MASK)<<16,
374 ioaddr + INTERRUPT_ENABLE - 2);
376 if (ncr885e_debug > 3)
377 printk( KERN_INFO "%s: 53C885 config complete.\n", dev->name );
379 return;
385 transmit interrupt */
387 static void
388 ncr885e_tx( struct net_device *dev )
391 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
392 volatile struct dbdma_cmd *cp, *dp;
393 unsigned short txbits, xfer;
394 int i;
396 del_timer( &sp->tx_timeout );
398 if (ncr885e_debug > 3)
399 printk( KERN_INFO "%s: ncr885e_tx: active=%d, dirty=%d, current=%d\n",
400 dev->name, sp->tx_active, sp->tx_dirty, sp->tx_current );
402 sp->timeout_active = 0;
404 i = sp->tx_dirty;
405 cp = sp->tx_cmds + (i*3);
406 dp = cp+1;
407 sp->tx_active--;
409 xfer = inw( &dp->xfer_status );
410 txbits = inw( &sp->tx_status[i] );
412 if (ncr885e_debug > 4) {
413 show_dbdma_cmd( cp );
414 show_dbdma_cmd( dp );
417 /* get xmit result */
418 txbits = inw( &sp->tx_status[i] );
420 if (ncr885e_debug > 3)
421 printk( KERN_INFO "%s: tx xfer=%04x, txbits=%04x\n", dev->name,
422 xfer, txbits );
424 /* look for any channel status (?) */
425 if ( xfer ) {
427 dev_kfree_skb_irq( sp->tx_skbufs[i] );
429 if ( txbits & TX_STATUS_TXOK ) {
430 sp->stats.tx_packets++;
431 sp->stats.tx_bytes += inw( &cp->req_count );
434 /* dropped packets */
435 if ( txbits & (TX_STATUS_TDLC|TX_STATUS_TDEC) ) {
436 sp->stats.tx_dropped++;
439 /* add the collisions */
440 sp->stats.collisions += ( txbits & 0x04 );
444 netif_start_queue(dev);
446 return;
449 /* rx interrupt handling */
450 static void
451 ncr885e_rx( struct net_device *dev )
454 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
455 volatile struct dbdma_cmd *cp;
456 struct sk_buff *skb;
457 int i, nb;
458 unsigned short status;
459 unsigned char *data, *stats;
460 unsigned long rxbits, ioaddr = dev->base_addr;
462 i = sp->rx_current;
463 cp = sp->rx_cmds + (i*2);
465 if (ncr885e_debug > 3)
466 printk( KERN_INFO "%s: ncr885e_rx dirty=%d, current=%d (cp@%p)\n",
467 dev->name, sp->rx_dirty, sp->rx_current, cp );
469 nb = inw( &cp->req_count ) - inw( &cp->res_count );
470 status = inw( &cp->xfer_status );
472 if (ncr885e_debug > 3)
473 printk( KERN_INFO "%s: (rx %d) bytes=%d, xfer_status=%04x\n",
474 dev->name, i, nb, status );
476 if ( status ) {
478 skb = sp->rx_skbufs[i];
479 data = skb->data;
480 stats = data + nb - 3;
481 rxbits = (stats[0]|stats[1]<<8|stats[2]<<16);
483 if (ncr885e_debug > 3)
484 printk( KERN_INFO " rx_bits=%06lx\n", rxbits );
486 skb->dev = dev;
487 skb_put( skb, nb-3 );
488 skb->protocol = eth_type_trans( skb, dev );
489 netif_rx( skb );
490 sp->rx_skbufs[i] = 0;
492 if ( rxbits & RX_STATUS_RXOK ) {
493 sp->stats.rx_packets++;
494 sp->stats.rx_bytes += nb;
497 if ( rxbits & RX_STATUS_MCAST )
498 sp->stats.multicast++;
502 sp->rx_dirty = sp->rx_current;
504 if ( ++sp->rx_current >= NR_RX_RING )
505 sp->rx_current = 0;
507 /* fix up the one we just trashed */
508 cp = sp->rx_cmds + (sp->rx_dirty * 2);
510 skb = dev_alloc_skb( RX_BUFLEN + 2 );
511 if ( skb != 0 ) {
512 skb_reserve( skb, 2 );
513 sp->rx_skbufs[sp->rx_dirty] = skb;
516 if (ncr885e_debug > 2)
517 printk( KERN_INFO "%s: ncr885e_rx: using ring index %d, filling cp @ %p\n",
518 dev->name, sp->rx_current, cp );
520 outw( RX_BUFLEN, &cp->req_count );
521 outw( 0, &cp->res_count );
522 data = skb->data;
523 outl( virt_to_bus( data ), &cp->phy_addr );
524 outw( 0, &cp->xfer_status );
526 cp = sp->rx_cmds + (sp->rx_current * 2);
528 /* restart rx DMA */
529 outl( virt_to_bus( cp ), ioaddr + RX_CMD_PTR_LO );
530 outl( (RX_DBDMA_ENABLE << 16)|RX_CHANNEL_RUN,
531 ioaddr + RX_CHANNEL_CONTROL );
533 return;
536 static void
537 ncr885e_misc_ints( struct net_device *dev, unsigned short status )
540 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
541 struct dbdma_cmd *cp;
542 unsigned long ioaddr = dev->base_addr;
544 if (ncr885e_debug > 1)
545 printk( KERN_INFO "miscellaneous interrupt handled; status=%02x\n",
546 status );
548 /* various transmit errors */
549 if ( status &
550 (INTERRUPT_PPET | INTERRUPT_PBFT | INTERRUPT_IIDT) ) {
552 /* illegal instruction in tx dma */
553 if ( status & INTERRUPT_IIDT ) {
555 cp = (struct dbdma_cmd *) bus_to_virt( inl( ioaddr + TX_CMD_PTR_LO ));
556 printk( KERN_INFO "%s: tx illegal insn:\n", dev->name );
557 printk( KERN_INFO " tx DBDMA - cmd = %p, status = %04x\n",
558 cp, inw( ioaddr + TX_CHANNEL_STATUS ));
559 printk( KERN_INFO " command = %04x, phy_addr=%08x, req_count=%04x\n",
560 inw( &cp->command ), inw( &cp->phy_addr ), inw( &cp->req_count ));
563 if ( status & INTERRUPT_PPET )
564 printk( KERN_INFO "%s: tx PCI parity error\n", dev->name );
566 if ( status & INTERRUPT_PBFT )
567 printk( KERN_INFO "%s: tx PCI bus fault\n", dev->name );
570 /* look for rx errors */
571 if ( status &
572 (INTERRUPT_PPER | INTERRUPT_PBFR | INTERRUPT_IIDR)) {
574 /* illegal instruction in rx dma */
575 if ( status & INTERRUPT_IIDR ) {
576 #if 0
577 cmd = inl( ioaddr + RX_CMD_PTR_LO );
578 #endif
579 printk( KERN_ERR "%s: rx illegal DMA instruction:\n", dev->name );
580 printk( KERN_ERR " channel status=%04x,\n",
581 inl( ioaddr + RX_CHANNEL_STATUS ));
582 #if 0
583 show_dbdma_cmd( bus_to_virt( inl( ioaddr + RX_CMD_PTR_LO )));
584 printk( KERN_ERR " instr (%08x) %08x %08x %08x\n",
585 (int) cmd, cmd[0], cmd[1], cmd[2] );
586 #endif
589 /* PCI parity error */
590 if ( status & INTERRUPT_PPER )
591 printk( KERN_INFO "%s: rx PCI parity error\n", dev->name );
593 if ( status & INTERRUPT_PBFR )
594 printk( KERN_INFO "%s: rx PCI bus fault\n", dev->name );
596 sp->stats.rx_errors++;
599 if ( status & INTERRUPT_WI ) {
600 printk( KERN_INFO "%s: link pulse\n", dev->name );
603 /* bump any counters */
606 return;
609 static void
610 ncr885e_interrupt( int irq, void *dev_id, struct pt_regs *regs )
613 struct net_device *dev = (struct net_device *) dev_id;
614 struct ncr885e_private *sp;
615 unsigned short status;
616 int ioaddr;
618 if ( dev == NULL ) {
619 printk( KERN_ERR "symba: Interrupt IRQ %d for unknown device\n", irq );
620 return;
623 ioaddr = dev->base_addr;
624 sp = (struct ncr885e_private *) dev->priv;
625 spin_lock( &sp->lock );
627 status = inw( ioaddr + INTERRUPT_CLEAR );
629 if (ncr885e_debug > 2)
630 printk( KERN_INFO "%s: 53C885 interrupt 0x%02x\n", dev->name, status );
632 /* handle non-tx and rx interrupts first */
633 if ( status & ~(INTERRUPT_DIT|INTERRUPT_DIR))
634 ncr885e_misc_ints( dev, status );
636 /* look for tx interrupt: more to transmit, DBDMA stopped, or tx done */
637 if ( ( status & INTERRUPT_DIT ) ) {
639 if (ncr885e_debug > 2)
640 printk( KERN_INFO "%s: tx int; int=%02x, chan stat=%02x\n",
641 dev->name, status, inw( ioaddr + TX_CHANNEL_STATUS ));
643 /* turn off timer */
644 del_timer( &sp->tx_timeout );
645 sp->timeout_active = 0;
647 /* stop DMA */
648 outl( TX_DBDMA_ENABLE << 16, ioaddr + TX_CHANNEL_CONTROL );
650 ncr885e_tx( dev );
653 if ( status & INTERRUPT_DIR ) {
655 if ( ncr885e_debug > 2 )
656 printk( KERN_INFO "%s: rx interrupt; int=%02x, rx channel stat=%02x\n",
657 dev->name, status, inw( ioaddr + RX_CHANNEL_STATUS ));
659 /* stop DMA */
660 outl( RX_DBDMA_ENABLE << 16, ioaddr + RX_CHANNEL_CONTROL );
662 /* and handle the interrupt */
663 ncr885e_rx( dev );
666 spin_unlock( &sp->lock );
668 return;
672 /* doesn't set the address permanently, however... */
673 static int
674 ncr885e_set_address( struct net_device *dev, void *addr )
677 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
678 struct sockaddr *saddr = addr;
679 unsigned long flags;
680 unsigned short reg[3];
681 unsigned char *ioaddr, *p;
682 int i;
684 memcpy( dev->dev_addr, saddr->sa_data, dev->addr_len );
686 p = (unsigned char *) dev->dev_addr;
687 printk( KERN_INFO "%s: setting new MAC address - ", dev->name );
688 #if 0
689 for( p = (unsigned char *) dev->dev_addr, i=0; i < 6; i++, p++ )
690 printk("%c%2.2x", i ? ':' : ' ', *p );
691 #endif
694 p = (unsigned char *) &reg;
695 for( i=0; i < 6; i++ )
696 p[i] = dev->dev_addr[i];
698 #if 0
699 printk("%s: Setting new mac address - ", dev->name );
700 for( i=0; i < 6; i++ ) {
701 printk("%02x", i ? ':' : ' ', p[i] );
704 printk("\n");
705 #endif
707 /* stop rx for the change */
708 outl( RX_DBDMA_ENABLE << 16, ioaddr + RX_CHANNEL_CONTROL );
710 spin_lock_irqsave( &sp->lock, flags );
712 ioaddr = (unsigned char *) dev->base_addr;
714 for( i = 0; i < 3; i++ ) {
715 reg[i] = ((reg[i] & 0xff) << 8) | ((reg[i] >> 8) & 0xff);
716 printk("%04x ", reg[i] );
717 outw( reg[i], ioaddr + STATION_ADDRESS_0 + (i*2));
719 printk("\n");
721 spin_unlock_irqrestore( &sp->lock, flags );
723 /* restart rx */
724 outl((RX_DBDMA_ENABLE << 16)|RX_CHANNEL_RUN,
725 ioaddr + RX_CHANNEL_CONTROL );
727 return 0;
730 static void
731 ncr885e_tx_timeout( unsigned long data )
734 struct net_device *dev = (struct net_device *) data;
735 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
736 unsigned long flags, ioaddr;
737 int i;
739 save_flags( flags );
740 cli();
742 ioaddr = dev->base_addr;
743 sp->timeout_active = 0;
744 i = sp->tx_dirty;
746 /* if we weren't active, bail... */
747 if ( sp->tx_active == 0 ) {
748 printk( KERN_INFO "%s: ncr885e_timeout...tx not active!\n", dev->name );
749 goto out;
752 printk( KERN_ERR "%s: 53C885 timed out. Resetting...\n", dev->name );
754 /* disable rx and tx DMA */
755 outl( (TX_DBDMA_ENABLE << 16), ioaddr + TX_CHANNEL_CONTROL );
756 outl( (RX_DBDMA_ENABLE << 16), ioaddr + RX_CHANNEL_CONTROL );
758 /* reset the chip */
759 ncr885e_config( dev );
760 ncr885e_enable( dev );
762 /* clear the wedged skb in the tx ring */
763 sp->tx_active = 0;
764 ++sp->stats.tx_errors;
766 if ( sp->tx_skbufs[i] ) {
767 dev_kfree_skb( sp->tx_skbufs[i] );
768 sp->tx_skbufs[i] = 0;
771 /* start anew from the beginning of the ring buffer (why not?) */
772 sp->tx_current = 0;
773 netif_wake_queue(dev);
775 /* restart rx dma */
776 outl( (RX_DBDMA_ENABLE << 16) | RX_CHANNEL_RUN,
777 ioaddr + RX_CHANNEL_CONTROL );
778 out:
780 restore_flags( flags );
783 static inline void
784 ncr885e_set_timeout( struct net_device *dev )
787 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
788 unsigned long flags;
790 save_flags(flags);
791 cli();
793 if ( sp->timeout_active )
794 del_timer( &sp->tx_timeout );
796 sp->tx_timeout.expires = jiffies + TX_TIMEOUT;
797 sp->tx_timeout.function = ncr885e_tx_timeout;
798 sp->tx_timeout.data = (unsigned long) dev;
799 add_timer( &sp->tx_timeout );
800 sp->timeout_active = 1;
801 restore_flags( flags );
806 * The goal is to set up DBDMA such that the rx ring contains only
807 * one DMA descriptor per ring element and the tx ring has two (using
808 * the cool features of branch- and wait-select. However, I'm not sure
809 * if it's possible. For now, we plod through it with 3 descriptors
810 * for tx, and two for rx.
813 static int
814 ncr885e_open( struct net_device *dev )
817 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
818 unsigned long ioaddr = dev->base_addr;
819 struct sk_buff *skb;
820 int i, size;
821 char *data;
822 struct dbdma_cmd *cp;
823 unsigned long flags;
825 /* allocate enough space for the tx and rx rings and a STOP descriptor */
826 size = (sizeof( struct dbdma_cmd ) *
827 ((NR_TX_RING * 3) + (NR_RX_RING * 2) + 1));
829 cp = kmalloc( size, GFP_KERNEL );
831 if ( cp == 0 ) {
832 printk( KERN_ERR "Insufficient memory (%d bytes) for DBDMA\n", size );
833 return -ENOMEM;
836 spin_lock_init( &sp->lock );
837 spin_lock_irqsave( &sp->lock, flags );
839 memset((char *) cp, 0, size );
840 sp->head = cp;
842 sp->stop_cmd = cp;
843 outl( DBDMA_STOP, &cp->command );
845 sp->rx_cmds = ++cp;
847 for( i = 0; i < NR_RX_RING; i++ ) {
849 cp = sp->rx_cmds + (i*2);
850 skb = dev_alloc_skb( RX_BUFLEN + 2 );
852 /* if there is insufficient memory, make this last ring use a
853 static buffer and leave the loop with that skb as final one */
854 if ( skb == 0 ) {
855 printk( KERN_ERR "%s: insufficient memory for rx ring buffer\n",
856 dev->name );
857 break;
860 skb_reserve( skb, 2 );
861 sp->rx_skbufs[i] = skb;
862 data = skb->data;
864 /* The DMA commands here are done such that an EOP is the only
865 way that we should get an interrupt. This means that we could
866 fill more than one skbuff before getting the interrupt at EOP. */
868 /* Handle rx DMA such that it always interrupts.... */
869 outw( (INPUT_MORE|INTR_ALWAYS), &cp->command );
870 outw( RX_BUFLEN, &cp->req_count );
871 outw( 0, &cp->res_count );
872 outl( virt_to_bus( data ), &cp->phy_addr );
873 outl( virt_to_bus( sp->stop_cmd ), &cp->cmd_dep );
874 outw( 0, &cp->xfer_status );
875 #if 0
876 printk( KERN_INFO "rx at %p\n", cp );
877 show_dbdma_cmd( cp );
878 #endif
879 ++cp;
881 outw( DBDMA_STOP, &cp->command );
885 /* initialize to all rx buffers are available, fill limit is the end */
886 sp->rx_dirty = 0;
887 sp->rx_current = 0;
889 /* fill the tx ring */
890 sp->tx_cmds = cp+1;
892 for( i = 0; i < NR_TX_RING; i++ ) {
894 /* minimal setup for tx command */
895 cp = sp->tx_cmds + (i*3);
896 outw( OUTPUT_LAST, &cp->command );
897 if (ncr885e_debug > 3) {
898 printk( KERN_INFO "tx OUTPUT_LAST at %p\n", cp );
899 show_dbdma_cmd( cp );
902 /* full setup for the status cmd */
903 cp++;
904 outw( INPUT_LAST|INTR_ALWAYS|WAIT_IFCLR, &cp->command );
905 outl( virt_to_bus( &sp->tx_status[i] ), &cp->phy_addr );
906 outw( 2, &cp->req_count );
907 if ( ncr885e_debug > 3) {
908 printk( KERN_INFO "tx INPUT_LAST cmd at %p\n", cp );
909 show_dbdma_cmd( cp );
912 ++cp;
913 outw( DBDMA_STOP, &cp->command );
916 #if 0
917 /* chain the last tx DMA command to the STOP cmd */
918 outw((INPUT_LAST|INTR_ALWAYS|BR_ALWAYS), &cp->command );
919 outl( virt_to_bus( sp->stop_cmd ), &cp->cmd_dep );
920 #endif
921 sp->tx_active = 0;
922 sp->tx_current = 0;
923 sp->tx_dirty = 0;
925 spin_unlock_irqrestore( &sp->lock, flags );
927 /* the order seems important here for some reason. If the MPIC isn't
928 enabled before the ethernet chip is enabled, shrapnel from the
929 bootloader causes us to receive interrupts even though we've not
930 yet enabled the tx channel. Go figure. It'd be better to configure
931 the chip in the probe1() routine, but then we don't see interrupts
932 at all. Everything looks all right on the logic analyzer, but... */
934 ncr885e_config( dev );
936 /* enable ethernet interrupts */
937 if ( request_irq( dev->irq, &ncr885e_interrupt, SA_SHIRQ, chipname, dev )) {
938 printk( KERN_ERR "%s: can't get irq %d\n", dev->name, dev->irq );
939 return -EAGAIN;
942 (void) inw( ioaddr + INTERRUPT_CLEAR );
944 ncr885e_enable( dev );
946 /* start rx DBDMA */
947 outl( virt_to_bus( sp->rx_cmds ), ioaddr + RX_CMD_PTR_LO );
948 outl( (RX_DBDMA_ENABLE << 16)|RX_CHANNEL_RUN,
949 ioaddr + RX_CHANNEL_CONTROL );
951 netif_start_queue(dev);
953 MOD_INC_USE_COUNT;
955 return 0;
958 static int
959 ncr885e_xmit_start( struct sk_buff *skb, struct net_device *dev )
962 struct ncr885e_private *sp = (struct ncr885e_private *) dev->priv;
963 volatile struct dbdma_cmd *cp, *dp;
964 unsigned long flags, ioaddr = dev->base_addr;
965 int len, next, fill, entry;
967 if ( ncr885e_debug > 3)
968 printk( KERN_INFO "%s: xmit_start len=%d, dirty=%d, current=%d, active=%d\n",
969 dev->name, skb->len, sp->tx_dirty, sp->tx_current, sp->tx_active );
971 spin_lock_irqsave( &sp->lock, flags );
973 /* find the free slot in the ring buffer */
974 fill = sp->tx_current;
975 next = fill + 1;
977 if ( next >= NR_TX_RING )
978 next = 0;
980 /* mark ourselves as busy, even if we have too many packets waiting */
981 netif_stop_queue(dev);
983 /* see if it's necessary to defer this packet */
984 if ( sp->tx_active >= MAX_TX_ACTIVE ) {
985 spin_unlock_irqrestore( &sp->lock, flags );
986 return -1;
989 sp->tx_active++; /* bump "active tx" count */
990 sp->tx_current = next; /* and show that we've used this buffer */
991 sp->tx_dirty = fill; /* and mark this one to get picked up */
993 len = skb->len;
995 if ( len > ETH_FRAME_LEN ) {
996 printk( KERN_DEBUG "%s: xmit frame too long (%d)\n", dev->name, len );
997 len = ETH_FRAME_LEN;
1000 /* get index into the tx DBDMA chain */
1001 entry = fill * 3;
1002 sp->tx_skbufs[fill] = skb;
1003 cp = sp->tx_cmds + entry;
1004 dp = cp + 1;
1006 /* update the rest of the OUTPUT_MORE descriptor */
1007 outw( len, &cp->req_count );
1008 outl( virt_to_bus( skb->data ), &cp->phy_addr );
1009 outw( 0, &cp->xfer_status );
1010 outw( 0, &cp->res_count );
1012 /* and finish off the INPUT_MORE */
1013 outw( 0, &dp->xfer_status );
1014 outw( 0, &dp->res_count );
1015 sp->tx_status[fill] = 0;
1016 outl( virt_to_bus( &sp->tx_status[fill] ), &dp->phy_addr );
1018 if ( ncr885e_debug > 2 )
1019 printk(KERN_INFO "%s: xmit_start: active %d, tx_current %d, tx_dirty %d\n",
1020 dev->name, sp->tx_active, sp->tx_current, sp->tx_dirty );
1022 if ( ncr885e_debug > 4 ) {
1023 show_dbdma_cmd( cp );
1024 show_dbdma_cmd( dp );
1028 /* restart the tx DMA engine */
1029 outl( virt_to_bus( cp ), ioaddr + TX_CMD_PTR_LO );
1030 outl( (TX_DBDMA_ENABLE << 16)|TX_CHANNEL_RUN,
1031 ioaddr + TX_CHANNEL_CONTROL );
1033 ncr885e_set_timeout( dev );
1035 spin_unlock_irqrestore( &sp->lock, flags );
1036 dev->trans_start = jiffies;
1038 return 0;
1041 static int
1042 ncr885e_close(struct net_device *dev)
1045 int i;
1046 struct ncr885e_private *np = (struct ncr885e_private *) dev->priv;
1047 unsigned long ioaddr = dev->base_addr;
1049 netif_stop_queue(dev);
1051 spin_lock( &np->lock );
1053 printk(KERN_INFO "%s: NCR885E Ethernet closing...\n", dev->name );
1055 if (ncr885e_debug > 1)
1056 printk(KERN_DEBUG "%s: Shutting down Ethernet chip\n", dev->name);
1058 ncr885e_disable(dev);
1060 del_timer(&np->tx_timeout);
1062 /* flip off rx and tx */
1063 outl( (RX_DBDMA_ENABLE << 16), ioaddr + RX_CHANNEL_CONTROL );
1064 outl( (TX_DBDMA_ENABLE << 16), ioaddr + TX_CHANNEL_CONTROL );
1066 /* free up the IRQ */
1067 free_irq( dev->irq, dev );
1069 for( i = 0; i < NR_RX_RING; i++ ) {
1070 if (np->rx_skbufs[i])
1071 dev_kfree_skb( np->rx_skbufs[i] );
1072 np->rx_skbufs[i] = 0;
1074 #if 0
1075 for (i = 0; i < NR_TX_RING; i++) {
1076 if (np->tx_skbufs[i])
1077 dev_kfree_skb(np->tx_skbufs[i]);
1078 np->tx_skbufs[i] = 0;
1080 #endif
1081 spin_unlock( &np->lock );
1083 kfree( np->head );
1085 MOD_DEC_USE_COUNT;
1087 return 0;
1092 * multicast promiscuous mode isn't used here. Allow code in the
1093 * IP stack to determine which multicast packets are good or bad....
1094 * (this avoids having to use the hash table registers)
1096 static void
1097 ncr885e_set_multicast( struct net_device *dev )
1100 int ioaddr = dev->base_addr;
1102 if ( ncr885e_debug > 3 )
1103 printk("%s: set_multicast: dev->flags = %x, AF=%04x\n",
1104 dev->name, dev->flags, inw( ioaddr + ADDRESS_FILTER ));
1106 if ( dev->flags & IFF_PROMISC ) {
1107 printk( KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name );
1108 outw( ADDRESS_RPPRO, ioaddr + ADDRESS_FILTER );
1111 /* accept all multicast packets without checking the mc_list. */
1112 else if ( dev->flags & IFF_ALLMULTI ) {
1113 printk( KERN_INFO "%s: Enabling all multicast packets.\n",
1114 dev->name );
1115 outw( ADDRESS_RPPRM, ioaddr + ADDRESS_FILTER );
1118 /* enable broadcast rx */
1119 else {
1120 outw( ADDRESS_RPABC, ioaddr + ADDRESS_FILTER );
1124 static struct net_device_stats *
1125 ncr885e_stats( struct net_device *dev )
1128 struct ncr885e_private *np = (struct ncr885e_private *) dev->priv;
1130 return &np->stats;
1133 /* By this function, we're certain that we have a 885 Ethernet controller
1134 * so we finish setting it up and wrap up all the required Linux ethernet
1135 * configuration.
1138 static int __init ncr885e_probe1(unsigned long ioaddr, unsigned char irq )
1141 struct net_device *dev;
1142 struct ncr885e_private *sp;
1143 unsigned short station_addr[3], val;
1144 unsigned char *p;
1145 int i;
1147 dev = init_etherdev( NULL, sizeof( struct ncr885e_private ) );
1148 if (!dev)
1149 return -ENOMEM;
1151 sp = dev->priv;
1153 /* snag the station address and display it */
1154 for( i = 0; i < 3; i++ ) {
1155 val = inw( ioaddr + STATION_ADDRESS_0 + (i*2));
1156 station_addr[i] = ((val >> 8) & 0xff) | ((val << 8) & 0xff00);
1159 printk( KERN_INFO "%s: %s at %08lx,", dev->name, chipname, ioaddr );
1161 p = (unsigned char *) &station_addr;
1163 for( i=0; i < 6; i++ ) {
1164 dev->dev_addr[i] = *p;
1165 printk("%c%2.2x", i ? ':' : ' ', dev->dev_addr[i] );
1166 p++;
1169 printk(", IRQ %d.\n", irq );
1171 /* set up a timer */
1172 init_timer( &sp->tx_timeout );
1173 sp->timeout_active = 0;
1175 dev->base_addr = ioaddr;
1176 dev->irq = irq;
1178 ether_setup( dev );
1180 /* everything else */
1181 dev->open = ncr885e_open;
1182 dev->stop = ncr885e_close;
1183 dev->get_stats = ncr885e_stats;
1184 dev->hard_start_xmit = ncr885e_xmit_start;
1185 dev->set_multicast_list = ncr885e_set_multicast;
1186 dev->set_mac_address = ncr885e_set_address;
1188 root_dev = dev;
1190 return 0;
1193 /* Since the NCR 53C885 is a multi-function chip, I'm not worrying about
1194 * trying to get the the device(s) in slot order. For our (Synergy's)
1195 * purpose, there's just a single 53C885 on the board and we don't
1196 * worry about the rest.
1199 static int __init ncr885e_probe(void)
1201 struct pci_dev *pdev = NULL;
1202 unsigned int ioaddr, chips = 0;
1203 unsigned short cmd;
1204 unsigned char irq, latency;
1206 /* use 'if' not 'while' where because driver only supports one device */
1207 if (( pdev = pci_find_device( PCI_VENDOR_ID_NCR,
1208 PCI_DEVICE_ID_NCR_53C885_ETHERNET,
1209 pdev )) != NULL ) {
1211 if ( !print_version ) {
1212 print_version++;
1213 printk( KERN_INFO "%s", version );
1216 if (pci_enable_device(pdev))
1217 continue;
1219 /* Use I/O space */
1220 ioaddr = pci_resource_start (pdev, 0);
1221 irq = pdev->irq;
1223 /* Adjust around the Grackle... */
1224 #ifdef CONFIG_GEMINI
1225 ioaddr |= 0xfe000000;
1226 #endif
1228 if ( !request_region( ioaddr, NCR885E_TOTAL_SIZE, "ncr885e" ))
1229 continue;
1231 /* finish off the probe */
1232 if ( !(ncr885e_probe1(ioaddr, irq ))) {
1233 chips++;
1234 pci_set_master (pdev);
1235 } else
1236 release_region( ioaddr, NCR885E_TOTAL_SIZE );
1239 if ( !chips )
1240 return -ENODEV;
1241 else
1242 return 0;
1245 /* debugging to peek at dma descriptors */
1246 static void
1247 show_dbdma_cmd( volatile struct dbdma_cmd *cmd )
1250 printk( KERN_INFO " cmd %04x, physaddr %08x, req_count %04x\n",
1251 inw( &cmd->command ), inl( &cmd->phy_addr ), inw( &cmd->req_count ));
1252 printk( KERN_INFO " res_count %04x, xfer_status %04x, branch %08x\n",
1253 inw( &cmd->res_count ), inw( &cmd->xfer_status ),inl( &cmd->cmd_dep ));
1256 #if 0
1257 static int
1258 read_eeprom( unsigned int ioaddr, int location )
1261 int loop;
1262 unsigned char val;
1264 outb( (location & 0xff), ioaddr + EE_WORD_ADDR );
1266 /* take spillover from location in control reg */
1267 outb(EE_CONTROL_RND_READB | (location & (0x7<<8)), ioaddr + EE_CONTROL);
1269 loop = 1000;
1270 while( (inb( ioaddr + EE_STATUS) & EE_SEB) &&
1271 (loop > 0) ) {
1272 udelay( 10 );
1273 loop--;
1276 if ( inb( ioaddr + EE_STATUS ) & EE_SEE ) {
1277 printk("%s: Serial EEPROM read error\n", chipname);
1278 val = 0xff;
1281 else
1282 val = inb( ioaddr + EE_READ_DATA );
1284 return (int) val;
1286 #endif
1288 #ifdef NCR885E_DEBUG_MII
1289 static void
1290 show_mii( unsigned long ioaddr )
1293 int phyctrl, phystat, phyadvert, phypartner, phyexpan;
1295 phyctrl = read_mii( ioaddr, MII_AUTO_NEGOTIATION_CONTROL );
1296 phystat = read_mii( ioaddr, MII_AUTO_NEGOTIATION_STATUS );
1297 phyadvert = read_mii( ioaddr, MII_AUTO_NEGOTIATION_ADVERTISEMENT );
1298 phypartner = read_mii( ioaddr, MII_AUTO_NEGOTIATION_LINK_PARTNER );
1299 phyexpan = read_mii( ioaddr, MII_AUTO_NEGOTIATION_EXPANSION );
1301 printk( KERN_INFO "PHY: advert=%d %s, partner=%s %s, link=%d, %s%s\n",
1302 (phyadvert & MANATECH_100BASETX_FULL_DUPLEX ? 100 : 10),
1303 (phyctrl & MANC_AUTO_NEGOTIATION_ENABLE ? "auto" : "fixed"),
1304 (phypartner & MANLP_ACKNOWLEDGE ?
1305 (phypartner & MANATECH_100BASETX_FULL_DUPLEX ? "100" : "10") :
1306 "?"),
1307 (phyexpan & MANE_LINK_PARTNER_AUTO_ABLE ? "auto" : "fixed"),
1308 (phyctrl & MANC_PHY_SPEED_100 ? 100 : 10),
1309 (phystat & MANS_LINK_STATUS ? "up" : "down"),
1310 (phyexpan & MANE_PARALLEL_DETECTION_FAULT ? " PD-fault" : "" ));
1311 return;
1315 static int
1316 read_mii( unsigned long ioaddr, int reg )
1319 int timeout;
1322 timeout = 100000;
1324 while( inw( ioaddr + MII_INDICATOR ) & MII_BUSY ) {
1326 if ( timeout-- < 0 ) {
1327 printk( KERN_INFO "Timed out waiting for MII\n" );
1328 return -1;
1332 outw( (1<<8) + reg, ioaddr + MII_ADDRESS );
1333 outw( MIIM_RSTAT, ioaddr + MIIM_COMMAND );
1335 timeout = 100000;
1336 while( inw( ioaddr + MII_INDICATOR ) & MII_BUSY ) {
1337 if ( timeout-- < 0 ) {
1338 printk( KERN_INFO "Timed out waiting for MII\n" );
1339 return -1;
1343 return( inw( ioaddr + MII_READ_DATA ));
1346 static void
1347 write_mii( unsigned long ioaddr, int reg, int data )
1350 int timeout=100000;
1352 printk( KERN_INFO "MII indicator: %02x\n", inw( ioaddr + MII_INDICATOR ));
1354 while( inw( ioaddr + MII_INDICATOR ) & MII_BUSY ) {
1355 if ( timeout-- <= 0 ) {
1356 printk( KERN_INFO "Timeout waiting to write to MII\n" );
1357 return;
1359 udelay( 10 );
1362 outw( (1<<8) + reg, ioaddr + MII_ADDRESS );
1363 outw( data, ioaddr + MII_WRITE_DATA );
1365 return;
1368 #endif /* NCR885E_DEBUG_MII */
1370 static void __exit ncr885e_cleanup(void)
1372 if ( root_dev ) {
1373 unregister_netdev( root_dev );
1374 release_region( root_dev->base_addr, NCR885E_TOTAL_SIZE );
1375 kfree( root_dev );
1376 root_dev = NULL;
1380 module_init(ncr885e_probe);
1381 module_exit(ncr885e_cleanup);
1384 * Local variables:
1385 * compile-command: "gcc -DMODULE -DMODVERSIONS -D__KERNEL__ -I../../include -Wall -Wstrict-prototypes -O6 -c symba.c"
1386 * End: