2 * Network device driver for the MACE ethernet controller on
3 * Apple Powermacs. Assumes it's under a DBDMA controller.
5 * Copyright (C) 1996 Paul Mackerras.
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/delay.h>
13 #include <linux/string.h>
14 #include <linux/timer.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/crc32.h>
18 #include <linux/spinlock.h>
19 #include <linux/bitrev.h>
20 #include <linux/slab.h>
22 #include <asm/dbdma.h>
24 #include <asm/pgtable.h>
25 #include <asm/macio.h>
29 static int port_aaui
= -1;
33 #define MAX_TX_ACTIVE 1
34 #define NCMDS_TX 1 /* dma commands per element in tx ring */
35 #define RX_BUFLEN (ETH_FRAME_LEN + 8)
36 #define TX_TIMEOUT HZ /* 1 second */
38 /* Chip rev needs workaround on HW & multicast addr change */
39 #define BROKEN_ADDRCHG_REV 0x0941
41 /* Bits in transmit DMA status */
42 #define TX_DMA_ERR 0x80
45 volatile struct mace __iomem
*mace
;
46 volatile struct dbdma_regs __iomem
*tx_dma
;
48 volatile struct dbdma_regs __iomem
*rx_dma
;
50 volatile struct dbdma_cmd
*tx_cmds
; /* xmit dma command list */
51 volatile struct dbdma_cmd
*rx_cmds
; /* recv dma command list */
52 struct sk_buff
*rx_bufs
[N_RX_RING
];
55 struct sk_buff
*tx_bufs
[N_TX_RING
];
59 unsigned char tx_fullup
;
60 unsigned char tx_active
;
61 unsigned char tx_bad_runt
;
62 struct timer_list tx_timeout
;
66 struct macio_dev
*mdev
;
71 * Number of bytes of private data per MACE: allow enough for
72 * the rx and tx dma commands plus a branch dma command each,
73 * and another 16 bytes to allow us to align the dma command
74 * buffers on a 16 byte boundary.
76 #define PRIV_BYTES (sizeof(struct mace_data) \
77 + (N_RX_RING + NCMDS_TX * N_TX_RING + 3) * sizeof(struct dbdma_cmd))
79 static int mace_open(struct net_device
*dev
);
80 static int mace_close(struct net_device
*dev
);
81 static int mace_xmit_start(struct sk_buff
*skb
, struct net_device
*dev
);
82 static void mace_set_multicast(struct net_device
*dev
);
83 static void mace_reset(struct net_device
*dev
);
84 static int mace_set_address(struct net_device
*dev
, void *addr
);
85 static irqreturn_t
mace_interrupt(int irq
, void *dev_id
);
86 static irqreturn_t
mace_txdma_intr(int irq
, void *dev_id
);
87 static irqreturn_t
mace_rxdma_intr(int irq
, void *dev_id
);
88 static void mace_set_timeout(struct net_device
*dev
);
89 static void mace_tx_timeout(unsigned long data
);
90 static inline void dbdma_reset(volatile struct dbdma_regs __iomem
*dma
);
91 static inline void mace_clean_rings(struct mace_data
*mp
);
92 static void __mace_set_address(struct net_device
*dev
, void *addr
);
95 * If we can't get a skbuff when we need it, we use this area for DMA.
97 static unsigned char *dummy_buf
;
99 static const struct net_device_ops mace_netdev_ops
= {
100 .ndo_open
= mace_open
,
101 .ndo_stop
= mace_close
,
102 .ndo_start_xmit
= mace_xmit_start
,
103 .ndo_set_multicast_list
= mace_set_multicast
,
104 .ndo_set_mac_address
= mace_set_address
,
105 .ndo_change_mtu
= eth_change_mtu
,
106 .ndo_validate_addr
= eth_validate_addr
,
109 static int __devinit
mace_probe(struct macio_dev
*mdev
, const struct of_device_id
*match
)
111 struct device_node
*mace
= macio_get_of_node(mdev
);
112 struct net_device
*dev
;
113 struct mace_data
*mp
;
114 const unsigned char *addr
;
115 int j
, rev
, rc
= -EBUSY
;
117 if (macio_resource_count(mdev
) != 3 || macio_irq_count(mdev
) != 3) {
118 printk(KERN_ERR
"can't use MACE %s: need 3 addrs and 3 irqs\n",
123 addr
= of_get_property(mace
, "mac-address", NULL
);
125 addr
= of_get_property(mace
, "local-mac-address", NULL
);
127 printk(KERN_ERR
"Can't get mac-address for MACE %s\n",
134 * lazy allocate the driver-wide dummy buffer. (Note that we
135 * never have more than one MACE in the system anyway)
137 if (dummy_buf
== NULL
) {
138 dummy_buf
= kmalloc(RX_BUFLEN
+2, GFP_KERNEL
);
139 if (dummy_buf
== NULL
) {
140 printk(KERN_ERR
"MACE: couldn't allocate dummy buffer\n");
145 if (macio_request_resources(mdev
, "mace")) {
146 printk(KERN_ERR
"MACE: can't request IO resources !\n");
150 dev
= alloc_etherdev(PRIV_BYTES
);
152 printk(KERN_ERR
"MACE: can't allocate ethernet device !\n");
156 SET_NETDEV_DEV(dev
, &mdev
->ofdev
.dev
);
158 mp
= netdev_priv(dev
);
160 macio_set_drvdata(mdev
, dev
);
162 dev
->base_addr
= macio_resource_start(mdev
, 0);
163 mp
->mace
= ioremap(dev
->base_addr
, 0x1000);
164 if (mp
->mace
== NULL
) {
165 printk(KERN_ERR
"MACE: can't map IO resources !\n");
169 dev
->irq
= macio_irq(mdev
, 0);
171 rev
= addr
[0] == 0 && addr
[1] == 0xA0;
172 for (j
= 0; j
< 6; ++j
) {
173 dev
->dev_addr
[j
] = rev
? bitrev8(addr
[j
]): addr
[j
];
175 mp
->chipid
= (in_8(&mp
->mace
->chipid_hi
) << 8) |
176 in_8(&mp
->mace
->chipid_lo
);
179 mp
= netdev_priv(dev
);
180 mp
->maccc
= ENXMT
| ENRCV
;
182 mp
->tx_dma
= ioremap(macio_resource_start(mdev
, 1), 0x1000);
183 if (mp
->tx_dma
== NULL
) {
184 printk(KERN_ERR
"MACE: can't map TX DMA resources !\n");
188 mp
->tx_dma_intr
= macio_irq(mdev
, 1);
190 mp
->rx_dma
= ioremap(macio_resource_start(mdev
, 2), 0x1000);
191 if (mp
->rx_dma
== NULL
) {
192 printk(KERN_ERR
"MACE: can't map RX DMA resources !\n");
194 goto err_unmap_tx_dma
;
196 mp
->rx_dma_intr
= macio_irq(mdev
, 2);
198 mp
->tx_cmds
= (volatile struct dbdma_cmd
*) DBDMA_ALIGN(mp
+ 1);
199 mp
->rx_cmds
= mp
->tx_cmds
+ NCMDS_TX
* N_TX_RING
+ 1;
201 memset((char *) mp
->tx_cmds
, 0,
202 (NCMDS_TX
*N_TX_RING
+ N_RX_RING
+ 2) * sizeof(struct dbdma_cmd
));
203 init_timer(&mp
->tx_timeout
);
204 spin_lock_init(&mp
->lock
);
205 mp
->timeout_active
= 0;
208 mp
->port_aaui
= port_aaui
;
210 /* Apple Network Server uses the AAUI port */
211 if (of_machine_is_compatible("AAPL,ShinerESB"))
214 #ifdef CONFIG_MACE_AAUI_PORT
222 dev
->netdev_ops
= &mace_netdev_ops
;
225 * Most of what is below could be moved to mace_open()
229 rc
= request_irq(dev
->irq
, mace_interrupt
, 0, "MACE", dev
);
231 printk(KERN_ERR
"MACE: can't get irq %d\n", dev
->irq
);
232 goto err_unmap_rx_dma
;
234 rc
= request_irq(mp
->tx_dma_intr
, mace_txdma_intr
, 0, "MACE-txdma", dev
);
236 printk(KERN_ERR
"MACE: can't get irq %d\n", mp
->tx_dma_intr
);
239 rc
= request_irq(mp
->rx_dma_intr
, mace_rxdma_intr
, 0, "MACE-rxdma", dev
);
241 printk(KERN_ERR
"MACE: can't get irq %d\n", mp
->rx_dma_intr
);
242 goto err_free_tx_irq
;
245 rc
= register_netdev(dev
);
247 printk(KERN_ERR
"MACE: Cannot register net device, aborting.\n");
248 goto err_free_rx_irq
;
251 printk(KERN_INFO
"%s: MACE at %pM, chip revision %d.%d\n",
252 dev
->name
, dev
->dev_addr
,
253 mp
->chipid
>> 8, mp
->chipid
& 0xff);
258 free_irq(macio_irq(mdev
, 2), dev
);
260 free_irq(macio_irq(mdev
, 1), dev
);
262 free_irq(macio_irq(mdev
, 0), dev
);
272 macio_release_resources(mdev
);
277 static int __devexit
mace_remove(struct macio_dev
*mdev
)
279 struct net_device
*dev
= macio_get_drvdata(mdev
);
280 struct mace_data
*mp
;
284 macio_set_drvdata(mdev
, NULL
);
286 mp
= netdev_priv(dev
);
288 unregister_netdev(dev
);
290 free_irq(dev
->irq
, dev
);
291 free_irq(mp
->tx_dma_intr
, dev
);
292 free_irq(mp
->rx_dma_intr
, dev
);
300 macio_release_resources(mdev
);
305 static void dbdma_reset(volatile struct dbdma_regs __iomem
*dma
)
309 out_le32(&dma
->control
, (WAKE
|FLUSH
|PAUSE
|RUN
) << 16);
312 * Yes this looks peculiar, but apparently it needs to be this
313 * way on some machines.
315 for (i
= 200; i
> 0; --i
)
316 if (ld_le32(&dma
->control
) & RUN
)
320 static void mace_reset(struct net_device
*dev
)
322 struct mace_data
*mp
= netdev_priv(dev
);
323 volatile struct mace __iomem
*mb
= mp
->mace
;
326 /* soft-reset the chip */
329 out_8(&mb
->biucc
, SWRST
);
330 if (in_8(&mb
->biucc
) & SWRST
) {
337 printk(KERN_ERR
"mace: cannot reset chip!\n");
341 out_8(&mb
->imr
, 0xff); /* disable all intrs for now */
343 out_8(&mb
->maccc
, 0); /* turn off tx, rx */
345 out_8(&mb
->biucc
, XMTSP_64
);
346 out_8(&mb
->utr
, RTRD
);
347 out_8(&mb
->fifocc
, RCVFW_32
| XMTFW_16
| XMTFWU
| RCVFWU
| XMTBRST
);
348 out_8(&mb
->xmtfc
, AUTO_PAD_XMIT
); /* auto-pad short frames */
349 out_8(&mb
->rcvfc
, 0);
351 /* load up the hardware address */
352 __mace_set_address(dev
, dev
->dev_addr
);
354 /* clear the multicast filter */
355 if (mp
->chipid
== BROKEN_ADDRCHG_REV
)
356 out_8(&mb
->iac
, LOGADDR
);
358 out_8(&mb
->iac
, ADDRCHG
| LOGADDR
);
359 while ((in_8(&mb
->iac
) & ADDRCHG
) != 0)
362 for (i
= 0; i
< 8; ++i
)
363 out_8(&mb
->ladrf
, 0);
365 /* done changing address */
366 if (mp
->chipid
!= BROKEN_ADDRCHG_REV
)
370 out_8(&mb
->plscc
, PORTSEL_AUI
+ ENPLSIO
);
372 out_8(&mb
->plscc
, PORTSEL_GPSI
+ ENPLSIO
);
375 static void __mace_set_address(struct net_device
*dev
, void *addr
)
377 struct mace_data
*mp
= netdev_priv(dev
);
378 volatile struct mace __iomem
*mb
= mp
->mace
;
379 unsigned char *p
= addr
;
382 /* load up the hardware address */
383 if (mp
->chipid
== BROKEN_ADDRCHG_REV
)
384 out_8(&mb
->iac
, PHYADDR
);
386 out_8(&mb
->iac
, ADDRCHG
| PHYADDR
);
387 while ((in_8(&mb
->iac
) & ADDRCHG
) != 0)
390 for (i
= 0; i
< 6; ++i
)
391 out_8(&mb
->padr
, dev
->dev_addr
[i
] = p
[i
]);
392 if (mp
->chipid
!= BROKEN_ADDRCHG_REV
)
396 static int mace_set_address(struct net_device
*dev
, void *addr
)
398 struct mace_data
*mp
= netdev_priv(dev
);
399 volatile struct mace __iomem
*mb
= mp
->mace
;
402 spin_lock_irqsave(&mp
->lock
, flags
);
404 __mace_set_address(dev
, addr
);
406 /* note: setting ADDRCHG clears ENRCV */
407 out_8(&mb
->maccc
, mp
->maccc
);
409 spin_unlock_irqrestore(&mp
->lock
, flags
);
413 static inline void mace_clean_rings(struct mace_data
*mp
)
417 /* free some skb's */
418 for (i
= 0; i
< N_RX_RING
; ++i
) {
419 if (mp
->rx_bufs
[i
] != NULL
) {
420 dev_kfree_skb(mp
->rx_bufs
[i
]);
421 mp
->rx_bufs
[i
] = NULL
;
424 for (i
= mp
->tx_empty
; i
!= mp
->tx_fill
; ) {
425 dev_kfree_skb(mp
->tx_bufs
[i
]);
426 if (++i
>= N_TX_RING
)
431 static int mace_open(struct net_device
*dev
)
433 struct mace_data
*mp
= netdev_priv(dev
);
434 volatile struct mace __iomem
*mb
= mp
->mace
;
435 volatile struct dbdma_regs __iomem
*rd
= mp
->rx_dma
;
436 volatile struct dbdma_regs __iomem
*td
= mp
->tx_dma
;
437 volatile struct dbdma_cmd
*cp
;
445 /* initialize list of sk_buffs for receiving and set up recv dma */
446 mace_clean_rings(mp
);
447 memset((char *)mp
->rx_cmds
, 0, N_RX_RING
* sizeof(struct dbdma_cmd
));
449 for (i
= 0; i
< N_RX_RING
- 1; ++i
) {
450 skb
= dev_alloc_skb(RX_BUFLEN
+ 2);
454 skb_reserve(skb
, 2); /* so IP header lands on 4-byte bdry */
457 mp
->rx_bufs
[i
] = skb
;
458 st_le16(&cp
->req_count
, RX_BUFLEN
);
459 st_le16(&cp
->command
, INPUT_LAST
+ INTR_ALWAYS
);
460 st_le32(&cp
->phy_addr
, virt_to_bus(data
));
464 mp
->rx_bufs
[i
] = NULL
;
465 st_le16(&cp
->command
, DBDMA_STOP
);
469 /* Put a branch back to the beginning of the receive command list */
471 st_le16(&cp
->command
, DBDMA_NOP
+ BR_ALWAYS
);
472 st_le32(&cp
->cmd_dep
, virt_to_bus(mp
->rx_cmds
));
475 out_le32(&rd
->control
, (RUN
|PAUSE
|FLUSH
|WAKE
) << 16); /* clear run bit */
476 out_le32(&rd
->cmdptr
, virt_to_bus(mp
->rx_cmds
));
477 out_le32(&rd
->control
, (RUN
<< 16) | RUN
);
479 /* put a branch at the end of the tx command list */
480 cp
= mp
->tx_cmds
+ NCMDS_TX
* N_TX_RING
;
481 st_le16(&cp
->command
, DBDMA_NOP
+ BR_ALWAYS
);
482 st_le32(&cp
->cmd_dep
, virt_to_bus(mp
->tx_cmds
));
485 out_le32(&td
->control
, (RUN
|PAUSE
|FLUSH
|WAKE
) << 16);
486 out_le32(&td
->cmdptr
, virt_to_bus(mp
->tx_cmds
));
494 out_8(&mb
->maccc
, mp
->maccc
);
495 /* enable all interrupts except receive interrupts */
496 out_8(&mb
->imr
, RCVINT
);
501 static int mace_close(struct net_device
*dev
)
503 struct mace_data
*mp
= netdev_priv(dev
);
504 volatile struct mace __iomem
*mb
= mp
->mace
;
505 volatile struct dbdma_regs __iomem
*rd
= mp
->rx_dma
;
506 volatile struct dbdma_regs __iomem
*td
= mp
->tx_dma
;
508 /* disable rx and tx */
509 out_8(&mb
->maccc
, 0);
510 out_8(&mb
->imr
, 0xff); /* disable all intrs */
512 /* disable rx and tx dma */
513 st_le32(&rd
->control
, (RUN
|PAUSE
|FLUSH
|WAKE
) << 16); /* clear run bit */
514 st_le32(&td
->control
, (RUN
|PAUSE
|FLUSH
|WAKE
) << 16); /* clear run bit */
516 mace_clean_rings(mp
);
521 static inline void mace_set_timeout(struct net_device
*dev
)
523 struct mace_data
*mp
= netdev_priv(dev
);
525 if (mp
->timeout_active
)
526 del_timer(&mp
->tx_timeout
);
527 mp
->tx_timeout
.expires
= jiffies
+ TX_TIMEOUT
;
528 mp
->tx_timeout
.function
= mace_tx_timeout
;
529 mp
->tx_timeout
.data
= (unsigned long) dev
;
530 add_timer(&mp
->tx_timeout
);
531 mp
->timeout_active
= 1;
534 static int mace_xmit_start(struct sk_buff
*skb
, struct net_device
*dev
)
536 struct mace_data
*mp
= netdev_priv(dev
);
537 volatile struct dbdma_regs __iomem
*td
= mp
->tx_dma
;
538 volatile struct dbdma_cmd
*cp
, *np
;
542 /* see if there's a free slot in the tx ring */
543 spin_lock_irqsave(&mp
->lock
, flags
);
546 if (next
>= N_TX_RING
)
548 if (next
== mp
->tx_empty
) {
549 netif_stop_queue(dev
);
551 spin_unlock_irqrestore(&mp
->lock
, flags
);
552 return NETDEV_TX_BUSY
; /* can't take it at the moment */
554 spin_unlock_irqrestore(&mp
->lock
, flags
);
556 /* partially fill in the dma command block */
558 if (len
> ETH_FRAME_LEN
) {
559 printk(KERN_DEBUG
"mace: xmit frame too long (%d)\n", len
);
562 mp
->tx_bufs
[fill
] = skb
;
563 cp
= mp
->tx_cmds
+ NCMDS_TX
* fill
;
564 st_le16(&cp
->req_count
, len
);
565 st_le32(&cp
->phy_addr
, virt_to_bus(skb
->data
));
567 np
= mp
->tx_cmds
+ NCMDS_TX
* next
;
568 out_le16(&np
->command
, DBDMA_STOP
);
570 /* poke the tx dma channel */
571 spin_lock_irqsave(&mp
->lock
, flags
);
573 if (!mp
->tx_bad_runt
&& mp
->tx_active
< MAX_TX_ACTIVE
) {
574 out_le16(&cp
->xfer_status
, 0);
575 out_le16(&cp
->command
, OUTPUT_LAST
);
576 out_le32(&td
->control
, ((RUN
|WAKE
) << 16) + (RUN
|WAKE
));
578 mace_set_timeout(dev
);
580 if (++next
>= N_TX_RING
)
582 if (next
== mp
->tx_empty
)
583 netif_stop_queue(dev
);
584 spin_unlock_irqrestore(&mp
->lock
, flags
);
589 static void mace_set_multicast(struct net_device
*dev
)
591 struct mace_data
*mp
= netdev_priv(dev
);
592 volatile struct mace __iomem
*mb
= mp
->mace
;
597 spin_lock_irqsave(&mp
->lock
, flags
);
599 if (dev
->flags
& IFF_PROMISC
) {
602 unsigned char multicast_filter
[8];
603 struct netdev_hw_addr
*ha
;
605 if (dev
->flags
& IFF_ALLMULTI
) {
606 for (i
= 0; i
< 8; i
++)
607 multicast_filter
[i
] = 0xff;
609 for (i
= 0; i
< 8; i
++)
610 multicast_filter
[i
] = 0;
611 netdev_for_each_mc_addr(ha
, dev
) {
612 crc
= ether_crc_le(6, ha
->addr
);
613 i
= crc
>> 26; /* bit number in multicast_filter */
614 multicast_filter
[i
>> 3] |= 1 << (i
& 7);
618 printk("Multicast filter :");
619 for (i
= 0; i
< 8; i
++)
620 printk("%02x ", multicast_filter
[i
]);
624 if (mp
->chipid
== BROKEN_ADDRCHG_REV
)
625 out_8(&mb
->iac
, LOGADDR
);
627 out_8(&mb
->iac
, ADDRCHG
| LOGADDR
);
628 while ((in_8(&mb
->iac
) & ADDRCHG
) != 0)
631 for (i
= 0; i
< 8; ++i
)
632 out_8(&mb
->ladrf
, multicast_filter
[i
]);
633 if (mp
->chipid
!= BROKEN_ADDRCHG_REV
)
637 out_8(&mb
->maccc
, mp
->maccc
);
638 spin_unlock_irqrestore(&mp
->lock
, flags
);
641 static void mace_handle_misc_intrs(struct mace_data
*mp
, int intr
, struct net_device
*dev
)
643 volatile struct mace __iomem
*mb
= mp
->mace
;
644 static int mace_babbles
, mace_jabbers
;
647 dev
->stats
.rx_missed_errors
+= 256;
648 dev
->stats
.rx_missed_errors
+= in_8(&mb
->mpc
); /* reading clears it */
650 dev
->stats
.rx_length_errors
+= 256;
651 dev
->stats
.rx_length_errors
+= in_8(&mb
->rntpc
); /* reading clears it */
653 ++dev
->stats
.tx_heartbeat_errors
;
655 if (mace_babbles
++ < 4)
656 printk(KERN_DEBUG
"mace: babbling transmitter\n");
658 if (mace_jabbers
++ < 4)
659 printk(KERN_DEBUG
"mace: jabbering transceiver\n");
662 static irqreturn_t
mace_interrupt(int irq
, void *dev_id
)
664 struct net_device
*dev
= (struct net_device
*) dev_id
;
665 struct mace_data
*mp
= netdev_priv(dev
);
666 volatile struct mace __iomem
*mb
= mp
->mace
;
667 volatile struct dbdma_regs __iomem
*td
= mp
->tx_dma
;
668 volatile struct dbdma_cmd
*cp
;
669 int intr
, fs
, i
, stat
, x
;
672 /* static int mace_last_fs, mace_last_xcount; */
674 spin_lock_irqsave(&mp
->lock
, flags
);
675 intr
= in_8(&mb
->ir
); /* read interrupt register */
676 in_8(&mb
->xmtrc
); /* get retries */
677 mace_handle_misc_intrs(mp
, intr
, dev
);
680 while (in_8(&mb
->pr
) & XMTSV
) {
681 del_timer(&mp
->tx_timeout
);
682 mp
->timeout_active
= 0;
684 * Clear any interrupt indication associated with this status
685 * word. This appears to unlatch any error indication from
686 * the DMA controller.
688 intr
= in_8(&mb
->ir
);
690 mace_handle_misc_intrs(mp
, intr
, dev
);
691 if (mp
->tx_bad_runt
) {
692 fs
= in_8(&mb
->xmtfs
);
694 out_8(&mb
->xmtfc
, AUTO_PAD_XMIT
);
697 dstat
= ld_le32(&td
->status
);
698 /* stop DMA controller */
699 out_le32(&td
->control
, RUN
<< 16);
701 * xcount is the number of complete frames which have been
702 * written to the fifo but for which status has not been read.
704 xcount
= (in_8(&mb
->fifofc
) >> XMTFC_SH
) & XMTFC_MASK
;
705 if (xcount
== 0 || (dstat
& DEAD
)) {
707 * If a packet was aborted before the DMA controller has
708 * finished transferring it, it seems that there are 2 bytes
709 * which are stuck in some buffer somewhere. These will get
710 * transmitted as soon as we read the frame status (which
711 * reenables the transmit data transfer request). Turning
712 * off the DMA controller and/or resetting the MACE doesn't
713 * help. So we disable auto-padding and FCS transmission
714 * so the two bytes will only be a runt packet which should
715 * be ignored by other stations.
717 out_8(&mb
->xmtfc
, DXMTFCS
);
719 fs
= in_8(&mb
->xmtfs
);
720 if ((fs
& XMTSV
) == 0) {
721 printk(KERN_ERR
"mace: xmtfs not valid! (fs=%x xc=%d ds=%x)\n",
725 * XXX mace likes to hang the machine after a xmtfs error.
726 * This is hard to reproduce, reseting *may* help
729 cp
= mp
->tx_cmds
+ NCMDS_TX
* i
;
730 stat
= ld_le16(&cp
->xfer_status
);
731 if ((fs
& (UFLO
|LCOL
|LCAR
|RTRY
)) || (dstat
& DEAD
) || xcount
== 0) {
733 * Check whether there were in fact 2 bytes written to
737 x
= (in_8(&mb
->fifofc
) >> XMTFC_SH
) & XMTFC_MASK
;
739 /* there were two bytes with an end-of-packet indication */
741 mace_set_timeout(dev
);
744 * Either there weren't the two bytes buffered up, or they
745 * didn't have an end-of-packet indication.
746 * We flush the transmit FIFO just in case (by setting the
747 * XMTFWU bit with the transmitter disabled).
749 out_8(&mb
->maccc
, in_8(&mb
->maccc
) & ~ENXMT
);
750 out_8(&mb
->fifocc
, in_8(&mb
->fifocc
) | XMTFWU
);
752 out_8(&mb
->maccc
, in_8(&mb
->maccc
) | ENXMT
);
753 out_8(&mb
->xmtfc
, AUTO_PAD_XMIT
);
756 /* dma should have finished */
757 if (i
== mp
->tx_fill
) {
758 printk(KERN_DEBUG
"mace: tx ring ran out? (fs=%x xc=%d ds=%x)\n",
763 if (fs
& (UFLO
|LCOL
|LCAR
|RTRY
)) {
764 ++dev
->stats
.tx_errors
;
766 ++dev
->stats
.tx_carrier_errors
;
767 if (fs
& (UFLO
|LCOL
|RTRY
))
768 ++dev
->stats
.tx_aborted_errors
;
770 dev
->stats
.tx_bytes
+= mp
->tx_bufs
[i
]->len
;
771 ++dev
->stats
.tx_packets
;
773 dev_kfree_skb_irq(mp
->tx_bufs
[i
]);
775 if (++i
>= N_TX_RING
)
779 mace_last_xcount
= xcount
;
783 if (i
!= mp
->tx_empty
) {
785 netif_wake_queue(dev
);
791 if (!mp
->tx_bad_runt
&& i
!= mp
->tx_fill
&& mp
->tx_active
< MAX_TX_ACTIVE
) {
793 /* set up the next one */
794 cp
= mp
->tx_cmds
+ NCMDS_TX
* i
;
795 out_le16(&cp
->xfer_status
, 0);
796 out_le16(&cp
->command
, OUTPUT_LAST
);
798 if (++i
>= N_TX_RING
)
800 } while (i
!= mp
->tx_fill
&& mp
->tx_active
< MAX_TX_ACTIVE
);
801 out_le32(&td
->control
, ((RUN
|WAKE
) << 16) + (RUN
|WAKE
));
802 mace_set_timeout(dev
);
804 spin_unlock_irqrestore(&mp
->lock
, flags
);
808 static void mace_tx_timeout(unsigned long data
)
810 struct net_device
*dev
= (struct net_device
*) data
;
811 struct mace_data
*mp
= netdev_priv(dev
);
812 volatile struct mace __iomem
*mb
= mp
->mace
;
813 volatile struct dbdma_regs __iomem
*td
= mp
->tx_dma
;
814 volatile struct dbdma_regs __iomem
*rd
= mp
->rx_dma
;
815 volatile struct dbdma_cmd
*cp
;
819 spin_lock_irqsave(&mp
->lock
, flags
);
820 mp
->timeout_active
= 0;
821 if (mp
->tx_active
== 0 && !mp
->tx_bad_runt
)
824 /* update various counters */
825 mace_handle_misc_intrs(mp
, in_8(&mb
->ir
), dev
);
827 cp
= mp
->tx_cmds
+ NCMDS_TX
* mp
->tx_empty
;
829 /* turn off both tx and rx and reset the chip */
830 out_8(&mb
->maccc
, 0);
831 printk(KERN_ERR
"mace: transmit timeout - resetting\n");
836 cp
= bus_to_virt(ld_le32(&rd
->cmdptr
));
838 out_le16(&cp
->xfer_status
, 0);
839 out_le32(&rd
->cmdptr
, virt_to_bus(cp
));
840 out_le32(&rd
->control
, (RUN
<< 16) | RUN
);
842 /* fix up the transmit side */
845 ++dev
->stats
.tx_errors
;
846 if (mp
->tx_bad_runt
) {
848 } else if (i
!= mp
->tx_fill
) {
849 dev_kfree_skb(mp
->tx_bufs
[i
]);
850 if (++i
>= N_TX_RING
)
855 netif_wake_queue(dev
);
856 if (i
!= mp
->tx_fill
) {
857 cp
= mp
->tx_cmds
+ NCMDS_TX
* i
;
858 out_le16(&cp
->xfer_status
, 0);
859 out_le16(&cp
->command
, OUTPUT_LAST
);
860 out_le32(&td
->cmdptr
, virt_to_bus(cp
));
861 out_le32(&td
->control
, (RUN
<< 16) | RUN
);
863 mace_set_timeout(dev
);
866 /* turn it back on */
867 out_8(&mb
->imr
, RCVINT
);
868 out_8(&mb
->maccc
, mp
->maccc
);
871 spin_unlock_irqrestore(&mp
->lock
, flags
);
874 static irqreturn_t
mace_txdma_intr(int irq
, void *dev_id
)
879 static irqreturn_t
mace_rxdma_intr(int irq
, void *dev_id
)
881 struct net_device
*dev
= (struct net_device
*) dev_id
;
882 struct mace_data
*mp
= netdev_priv(dev
);
883 volatile struct dbdma_regs __iomem
*rd
= mp
->rx_dma
;
884 volatile struct dbdma_cmd
*cp
, *np
;
885 int i
, nb
, stat
, next
;
887 unsigned frame_status
;
888 static int mace_lost_status
;
892 spin_lock_irqsave(&mp
->lock
, flags
);
893 for (i
= mp
->rx_empty
; i
!= mp
->rx_fill
; ) {
894 cp
= mp
->rx_cmds
+ i
;
895 stat
= ld_le16(&cp
->xfer_status
);
896 if ((stat
& ACTIVE
) == 0) {
898 if (next
>= N_RX_RING
)
900 np
= mp
->rx_cmds
+ next
;
901 if (next
!= mp
->rx_fill
&&
902 (ld_le16(&np
->xfer_status
) & ACTIVE
) != 0) {
903 printk(KERN_DEBUG
"mace: lost a status word\n");
908 nb
= ld_le16(&cp
->req_count
) - ld_le16(&cp
->res_count
);
909 out_le16(&cp
->command
, DBDMA_STOP
);
910 /* got a packet, have a look at it */
911 skb
= mp
->rx_bufs
[i
];
913 ++dev
->stats
.rx_dropped
;
916 frame_status
= (data
[nb
-3] << 8) + data
[nb
-4];
917 if (frame_status
& (RS_OFLO
|RS_CLSN
|RS_FRAMERR
|RS_FCSERR
)) {
918 ++dev
->stats
.rx_errors
;
919 if (frame_status
& RS_OFLO
)
920 ++dev
->stats
.rx_over_errors
;
921 if (frame_status
& RS_FRAMERR
)
922 ++dev
->stats
.rx_frame_errors
;
923 if (frame_status
& RS_FCSERR
)
924 ++dev
->stats
.rx_crc_errors
;
926 /* Mace feature AUTO_STRIP_RCV is on by default, dropping the
927 * FCS on frames with 802.3 headers. This means that Ethernet
928 * frames have 8 extra octets at the end, while 802.3 frames
929 * have only 4. We need to correctly account for this. */
930 if (*(unsigned short *)(data
+12) < 1536) /* 802.3 header */
932 else /* Ethernet header; mace includes FCS */
935 skb
->protocol
= eth_type_trans(skb
, dev
);
936 dev
->stats
.rx_bytes
+= skb
->len
;
938 mp
->rx_bufs
[i
] = NULL
;
939 ++dev
->stats
.rx_packets
;
942 ++dev
->stats
.rx_errors
;
943 ++dev
->stats
.rx_length_errors
;
946 /* advance to next */
947 if (++i
>= N_RX_RING
)
955 if (next
>= N_RX_RING
)
957 if (next
== mp
->rx_empty
)
959 cp
= mp
->rx_cmds
+ i
;
960 skb
= mp
->rx_bufs
[i
];
962 skb
= dev_alloc_skb(RX_BUFLEN
+ 2);
965 mp
->rx_bufs
[i
] = skb
;
968 st_le16(&cp
->req_count
, RX_BUFLEN
);
969 data
= skb
? skb
->data
: dummy_buf
;
970 st_le32(&cp
->phy_addr
, virt_to_bus(data
));
971 out_le16(&cp
->xfer_status
, 0);
972 out_le16(&cp
->command
, INPUT_LAST
+ INTR_ALWAYS
);
974 if ((ld_le32(&rd
->status
) & ACTIVE
) != 0) {
975 out_le32(&rd
->control
, (PAUSE
<< 16) | PAUSE
);
976 while ((in_le32(&rd
->status
) & ACTIVE
) != 0)
982 if (i
!= mp
->rx_fill
) {
983 out_le32(&rd
->control
, ((RUN
|WAKE
) << 16) | (RUN
|WAKE
));
986 spin_unlock_irqrestore(&mp
->lock
, flags
);
990 static struct of_device_id mace_match
[] =
997 MODULE_DEVICE_TABLE (of
, mace_match
);
999 static struct macio_driver mace_driver
=
1003 .owner
= THIS_MODULE
,
1004 .of_match_table
= mace_match
,
1006 .probe
= mace_probe
,
1007 .remove
= mace_remove
,
1011 static int __init
mace_init(void)
1013 return macio_register_driver(&mace_driver
);
1016 static void __exit
mace_cleanup(void)
1018 macio_unregister_driver(&mace_driver
);
1024 MODULE_AUTHOR("Paul Mackerras");
1025 MODULE_DESCRIPTION("PowerMac MACE driver.");
1026 module_param(port_aaui
, int, 0);
1027 MODULE_PARM_DESC(port_aaui
, "MACE uses AAUI port (0-1)");
1028 MODULE_LICENSE("GPL");
1030 module_init(mace_init
);
1031 module_exit(mace_cleanup
);