ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / mace.c
1 /*
2  * Network device driver for the MACE ethernet controller on
3  * Apple Powermacs.  Assumes it's under a DBDMA controller.
4  *
5  * Copyright (C) 1996 Paul Mackerras.
6  */
7
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/delay.h>
14 #include <linux/string.h>
15 #include <linux/timer.h>
16 #include <linux/init.h>
17 #include <linux/crc32.h>
18 #include <linux/spinlock.h>
19 #include <asm/prom.h>
20 #include <asm/dbdma.h>
21 #include <asm/io.h>
22 #include <asm/pgtable.h>
23 #include <asm/macio.h>
24
25 #include "mace.h"
26
27 static int port_aaui = -1;
28
29 #define N_RX_RING       8
30 #define N_TX_RING       6
31 #define MAX_TX_ACTIVE   1
32 #define NCMDS_TX        1       /* dma commands per element in tx ring */
33 #define RX_BUFLEN       (ETH_FRAME_LEN + 8)
34 #define TX_TIMEOUT      HZ      /* 1 second */
35
36 /* Chip rev needs workaround on HW & multicast addr change */
37 #define BROKEN_ADDRCHG_REV      0x0941
38
39 /* Bits in transmit DMA status */
40 #define TX_DMA_ERR      0x80
41
42 struct mace_data {
43     volatile struct mace *mace;
44     volatile struct dbdma_regs *tx_dma;
45     int tx_dma_intr;
46     volatile struct dbdma_regs *rx_dma;
47     int rx_dma_intr;
48     volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
49     volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
50     struct sk_buff *rx_bufs[N_RX_RING];
51     int rx_fill;
52     int rx_empty;
53     struct sk_buff *tx_bufs[N_TX_RING];
54     int tx_fill;
55     int tx_empty;
56     unsigned char maccc;
57     unsigned char tx_fullup;
58     unsigned char tx_active;
59     unsigned char tx_bad_runt;
60     struct net_device_stats stats;
61     struct timer_list tx_timeout;
62     int timeout_active;
63     int port_aaui;
64     int chipid;
65     struct macio_dev *mdev;
66     spinlock_t lock;
67 };
68
69 /*
70  * Number of bytes of private data per MACE: allow enough for
71  * the rx and tx dma commands plus a branch dma command each,
72  * and another 16 bytes to allow us to align the dma command
73  * buffers on a 16 byte boundary.
74  */
75 #define PRIV_BYTES      (sizeof(struct mace_data) \
76         + (N_RX_RING + NCMDS_TX * N_TX_RING + 3) * sizeof(struct dbdma_cmd))
77
78 static int bitrev(int);
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 struct net_device_stats *mace_stats(struct net_device *dev);
83 static void mace_set_multicast(struct net_device *dev);
84 static void mace_reset(struct net_device *dev);
85 static int mace_set_address(struct net_device *dev, void *addr);
86 static irqreturn_t mace_interrupt(int irq, void *dev_id, struct pt_regs *regs);
87 static irqreturn_t mace_txdma_intr(int irq, void *dev_id, struct pt_regs *regs);
88 static irqreturn_t mace_rxdma_intr(int irq, void *dev_id, struct pt_regs *regs);
89 static void mace_set_timeout(struct net_device *dev);
90 static void mace_tx_timeout(unsigned long data);
91 static inline void dbdma_reset(volatile struct dbdma_regs *dma);
92 static inline void mace_clean_rings(struct mace_data *mp);
93 static void __mace_set_address(struct net_device *dev, void *addr);
94
95 /*
96  * If we can't get a skbuff when we need it, we use this area for DMA.
97  */
98 static unsigned char *dummy_buf;
99
100 /* Bit-reverse one byte of an ethernet hardware address. */
101 static inline int
102 bitrev(int b)
103 {
104     int d = 0, i;
105
106     for (i = 0; i < 8; ++i, b >>= 1)
107         d = (d << 1) | (b & 1);
108     return d;
109 }
110
111
112 static int __devinit mace_probe(struct macio_dev *mdev, const struct of_match *match)
113 {
114         struct device_node *mace = macio_get_of_node(mdev);
115         struct net_device *dev;
116         struct mace_data *mp;
117         unsigned char *addr;
118         int j, rev, rc = -EBUSY;
119
120         if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
121                 printk(KERN_ERR "can't use MACE %s: need 3 addrs and 3 irqs\n",
122                        mace->full_name);
123                 return -ENODEV;
124         }
125
126         addr = get_property(mace, "mac-address", NULL);
127         if (addr == NULL) {
128                 addr = get_property(mace, "local-mac-address", NULL);
129                 if (addr == NULL) {
130                         printk(KERN_ERR "Can't get mac-address for MACE %s\n",
131                                mace->full_name);
132                         return -ENODEV;
133                 }
134         }
135
136         /*
137          * lazy allocate the driver-wide dummy buffer. (Note that we
138          * never have more than one MACE in the system anyway)
139          */
140         if (dummy_buf == NULL) {
141                 dummy_buf = kmalloc(RX_BUFLEN+2, GFP_KERNEL);
142                 if (dummy_buf == NULL) {
143                         printk(KERN_ERR "MACE: couldn't allocate dummy buffer\n");
144                         return -ENOMEM;
145                 }
146         }
147
148         if (macio_request_resources(mdev, "mace")) {
149                 printk(KERN_ERR "MACE: can't request IO resources !\n");
150                 return -EBUSY;
151         }
152
153         dev = alloc_etherdev(PRIV_BYTES);
154         if (!dev) {
155                 printk(KERN_ERR "MACE: can't allocate ethernet device !\n");
156                 rc = -ENOMEM;
157                 goto err_release;
158         }
159         SET_MODULE_OWNER(dev);
160         SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
161
162         mp = dev->priv;
163         mp->mdev = mdev;
164         macio_set_drvdata(mdev, dev);
165
166         dev->base_addr = macio_resource_start(mdev, 0);
167         mp->mace = (volatile struct mace *)ioremap(dev->base_addr, 0x1000);
168         if (mp->mace == NULL) {
169                 printk(KERN_ERR "MACE: can't map IO resources !\n");
170                 rc = -ENOMEM;
171                 goto err_free;
172         }
173         dev->irq = macio_irq(mdev, 0);
174
175         rev = addr[0] == 0 && addr[1] == 0xA0;
176         for (j = 0; j < 6; ++j) {
177                 dev->dev_addr[j] = rev? bitrev(addr[j]): addr[j];
178         }
179         mp->chipid = (in_8(&mp->mace->chipid_hi) << 8) |
180                         in_8(&mp->mace->chipid_lo);
181                 
182
183         mp = (struct mace_data *) dev->priv;
184         mp->maccc = ENXMT | ENRCV;
185
186         mp->tx_dma = (volatile struct dbdma_regs *)
187                 ioremap(macio_resource_start(mdev, 1), 0x1000);
188         if (mp->tx_dma == NULL) {
189                 printk(KERN_ERR "MACE: can't map TX DMA resources !\n");
190                 rc = -ENOMEM;
191                 goto err_unmap_io;
192         }
193         mp->tx_dma_intr = macio_irq(mdev, 1);
194
195         mp->rx_dma = (volatile struct dbdma_regs *)
196                 ioremap(macio_resource_start(mdev, 2), 0x1000);
197         if (mp->rx_dma == NULL) {
198                 printk(KERN_ERR "MACE: can't map RX DMA resources !\n");
199                 rc = -ENOMEM;
200                 goto err_unmap_tx_dma;
201         }
202         mp->rx_dma_intr = macio_irq(mdev, 2);
203
204         mp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(mp + 1);
205         mp->rx_cmds = mp->tx_cmds + NCMDS_TX * N_TX_RING + 1;
206
207         memset(&mp->stats, 0, sizeof(mp->stats));
208         memset((char *) mp->tx_cmds, 0,
209                (NCMDS_TX*N_TX_RING + N_RX_RING + 2) * sizeof(struct dbdma_cmd));
210         init_timer(&mp->tx_timeout);
211         spin_lock_init(&mp->lock);
212         mp->timeout_active = 0;
213
214         if (port_aaui >= 0)
215                 mp->port_aaui = port_aaui;
216         else {
217                 /* Apple Network Server uses the AAUI port */
218                 if (machine_is_compatible("AAPL,ShinerESB"))
219                         mp->port_aaui = 1;
220                 else {
221 #ifdef CONFIG_MACE_AAUI_PORT
222                         mp->port_aaui = 1;
223 #else
224                         mp->port_aaui = 0;
225 #endif                  
226                 }
227         }
228
229         dev->open = mace_open;
230         dev->stop = mace_close;
231         dev->hard_start_xmit = mace_xmit_start;
232         dev->get_stats = mace_stats;
233         dev->set_multicast_list = mace_set_multicast;
234         dev->set_mac_address = mace_set_address;
235
236         /*
237          * Most of what is below could be moved to mace_open()
238          */
239         mace_reset(dev);
240
241         rc = request_irq(dev->irq, mace_interrupt, 0, "MACE", dev);
242         if (rc) {
243                 printk(KERN_ERR "MACE: can't get irq %d\n", dev->irq);
244                 goto err_unmap_rx_dma;
245         }
246         rc = request_irq(mp->tx_dma_intr, mace_txdma_intr, 0, "MACE-txdma", dev);
247         if (rc) {
248                 printk(KERN_ERR "MACE: can't get irq %d\n", mace->intrs[1].line);
249                 goto err_free_irq;
250         }
251         rc = request_irq(mp->rx_dma_intr, mace_rxdma_intr, 0, "MACE-rxdma", dev);
252         if (rc) {
253                 printk(KERN_ERR "MACE: can't get irq %d\n", mace->intrs[2].line);
254                 goto err_free_tx_irq;
255         }
256
257         rc = register_netdev(dev);
258         if (rc) {
259                 printk(KERN_ERR "MACE: Cannot register net device, aborting.\n");
260                 goto err_free_rx_irq;
261         }
262
263         printk(KERN_INFO "%s: MACE at", dev->name);
264         for (j = 0; j < 6; ++j) {
265                 printk("%c%.2x", (j? ':': ' '), dev->dev_addr[j]);
266         }
267         printk(", chip revision %d.%d\n", mp->chipid >> 8, mp->chipid & 0xff);
268
269         return 0;
270  
271  err_free_rx_irq:
272         free_irq(macio_irq(mdev, 2), dev);
273  err_free_tx_irq:
274         free_irq(macio_irq(mdev, 1), dev);
275  err_free_irq:
276         free_irq(macio_irq(mdev, 0), dev);
277  err_unmap_rx_dma:
278         iounmap((void*)mp->rx_dma);
279  err_unmap_tx_dma:
280         iounmap((void*)mp->tx_dma);
281  err_unmap_io:
282         iounmap((void*)mp->mace);
283  err_free:
284         free_netdev(dev);
285  err_release:
286         macio_release_resources(mdev);
287
288         return rc;
289 }
290
291 static int __devexit mace_remove(struct macio_dev *mdev)
292 {
293         struct net_device *dev = macio_get_drvdata(mdev);
294         struct mace_data *mp;
295
296         BUG_ON(dev == NULL);
297
298         macio_set_drvdata(mdev, NULL);
299
300         mp = dev->priv;
301
302         unregister_netdev(dev);
303
304         free_irq(dev->irq, dev);
305         free_irq(mp->tx_dma_intr, dev);
306         free_irq(mp->rx_dma_intr, dev);
307
308         iounmap((void*)mp->rx_dma);
309         iounmap((void*)mp->tx_dma);
310         iounmap((void*)mp->mace);
311
312         free_netdev(dev);
313
314         macio_release_resources(mdev);
315
316         return 0;
317 }
318
319 static void dbdma_reset(volatile struct dbdma_regs *dma)
320 {
321     int i;
322
323     out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
324
325     /*
326      * Yes this looks peculiar, but apparently it needs to be this
327      * way on some machines.
328      */
329     for (i = 200; i > 0; --i)
330         if (ld_le32(&dma->control) & RUN)
331             udelay(1);
332 }
333
334 static void mace_reset(struct net_device *dev)
335 {
336     struct mace_data *mp = (struct mace_data *) dev->priv;
337     volatile struct mace *mb = mp->mace;
338     int i;
339
340     /* soft-reset the chip */
341     i = 200;
342     while (--i) {
343         out_8(&mb->biucc, SWRST);
344         if (in_8(&mb->biucc) & SWRST) {
345             udelay(10);
346             continue;
347         }
348         break;
349     }
350     if (!i) {
351         printk(KERN_ERR "mace: cannot reset chip!\n");
352         return;
353     }
354
355     out_8(&mb->imr, 0xff);      /* disable all intrs for now */
356     i = in_8(&mb->ir);
357     out_8(&mb->maccc, 0);       /* turn off tx, rx */
358
359     out_8(&mb->biucc, XMTSP_64);
360     out_8(&mb->utr, RTRD);
361     out_8(&mb->fifocc, RCVFW_32 | XMTFW_16 | XMTFWU | RCVFWU | XMTBRST);
362     out_8(&mb->xmtfc, AUTO_PAD_XMIT); /* auto-pad short frames */
363     out_8(&mb->rcvfc, 0);
364
365     /* load up the hardware address */
366     __mace_set_address(dev, dev->dev_addr);
367
368     /* clear the multicast filter */
369     if (mp->chipid == BROKEN_ADDRCHG_REV)
370         out_8(&mb->iac, LOGADDR);
371     else {
372         out_8(&mb->iac, ADDRCHG | LOGADDR);
373         while ((in_8(&mb->iac) & ADDRCHG) != 0)
374                 ;
375     }
376     for (i = 0; i < 8; ++i)
377         out_8(&mb->ladrf, 0);
378
379     /* done changing address */
380     if (mp->chipid != BROKEN_ADDRCHG_REV)
381         out_8(&mb->iac, 0);
382
383     if (mp->port_aaui)
384         out_8(&mb->plscc, PORTSEL_AUI + ENPLSIO);
385     else
386         out_8(&mb->plscc, PORTSEL_GPSI + ENPLSIO);
387 }
388
389 static void __mace_set_address(struct net_device *dev, void *addr)
390 {
391     struct mace_data *mp = (struct mace_data *) dev->priv;
392     volatile struct mace *mb = mp->mace;
393     unsigned char *p = addr;
394     int i;
395
396     /* load up the hardware address */
397     if (mp->chipid == BROKEN_ADDRCHG_REV)
398         out_8(&mb->iac, PHYADDR);
399     else {
400         out_8(&mb->iac, ADDRCHG | PHYADDR);
401         while ((in_8(&mb->iac) & ADDRCHG) != 0)
402             ;
403     }
404     for (i = 0; i < 6; ++i)
405         out_8(&mb->padr, dev->dev_addr[i] = p[i]);
406     if (mp->chipid != BROKEN_ADDRCHG_REV)
407         out_8(&mb->iac, 0);
408 }
409
410 static int mace_set_address(struct net_device *dev, void *addr)
411 {
412     struct mace_data *mp = (struct mace_data *) dev->priv;
413     volatile struct mace *mb = mp->mace;
414     unsigned long flags;
415
416     spin_lock_irqsave(&mp->lock, flags);
417
418     __mace_set_address(dev, addr);
419
420     /* note: setting ADDRCHG clears ENRCV */
421     out_8(&mb->maccc, mp->maccc);
422
423     spin_unlock_irqrestore(&mp->lock, flags);
424     return 0;
425 }
426
427 static inline void mace_clean_rings(struct mace_data *mp)
428 {
429     int i;
430
431     /* free some skb's */
432     for (i = 0; i < N_RX_RING; ++i) {
433         if (mp->rx_bufs[i] != 0) {
434             dev_kfree_skb(mp->rx_bufs[i]);
435             mp->rx_bufs[i] = 0;
436         }
437     }
438     for (i = mp->tx_empty; i != mp->tx_fill; ) {
439         dev_kfree_skb(mp->tx_bufs[i]);
440         if (++i >= N_TX_RING)
441             i = 0;
442     }
443 }
444
445 static int mace_open(struct net_device *dev)
446 {
447     struct mace_data *mp = (struct mace_data *) dev->priv;
448     volatile struct mace *mb = mp->mace;
449     volatile struct dbdma_regs *rd = mp->rx_dma;
450     volatile struct dbdma_regs *td = mp->tx_dma;
451     volatile struct dbdma_cmd *cp;
452     int i;
453     struct sk_buff *skb;
454     unsigned char *data;
455
456     /* reset the chip */
457     mace_reset(dev);
458
459     /* initialize list of sk_buffs for receiving and set up recv dma */
460     mace_clean_rings(mp);
461     memset((char *)mp->rx_cmds, 0, N_RX_RING * sizeof(struct dbdma_cmd));
462     cp = mp->rx_cmds;
463     for (i = 0; i < N_RX_RING - 1; ++i) {
464         skb = dev_alloc_skb(RX_BUFLEN + 2);
465         if (skb == 0) {
466             data = dummy_buf;
467         } else {
468             skb_reserve(skb, 2);        /* so IP header lands on 4-byte bdry */
469             data = skb->data;
470         }
471         mp->rx_bufs[i] = skb;
472         st_le16(&cp->req_count, RX_BUFLEN);
473         st_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
474         st_le32(&cp->phy_addr, virt_to_bus(data));
475         cp->xfer_status = 0;
476         ++cp;
477     }
478     mp->rx_bufs[i] = 0;
479     st_le16(&cp->command, DBDMA_STOP);
480     mp->rx_fill = i;
481     mp->rx_empty = 0;
482
483     /* Put a branch back to the beginning of the receive command list */
484     ++cp;
485     st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
486     st_le32(&cp->cmd_dep, virt_to_bus(mp->rx_cmds));
487
488     /* start rx dma */
489     out_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
490     out_le32(&rd->cmdptr, virt_to_bus(mp->rx_cmds));
491     out_le32(&rd->control, (RUN << 16) | RUN);
492
493     /* put a branch at the end of the tx command list */
494     cp = mp->tx_cmds + NCMDS_TX * N_TX_RING;
495     st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
496     st_le32(&cp->cmd_dep, virt_to_bus(mp->tx_cmds));
497
498     /* reset tx dma */
499     out_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
500     out_le32(&td->cmdptr, virt_to_bus(mp->tx_cmds));
501     mp->tx_fill = 0;
502     mp->tx_empty = 0;
503     mp->tx_fullup = 0;
504     mp->tx_active = 0;
505     mp->tx_bad_runt = 0;
506
507     /* turn it on! */
508     out_8(&mb->maccc, mp->maccc);
509     /* enable all interrupts except receive interrupts */
510     out_8(&mb->imr, RCVINT);
511
512     return 0;
513 }
514
515 static int mace_close(struct net_device *dev)
516 {
517     struct mace_data *mp = (struct mace_data *) dev->priv;
518     volatile struct mace *mb = mp->mace;
519     volatile struct dbdma_regs *rd = mp->rx_dma;
520     volatile struct dbdma_regs *td = mp->tx_dma;
521
522     /* disable rx and tx */
523     out_8(&mb->maccc, 0);
524     out_8(&mb->imr, 0xff);              /* disable all intrs */
525
526     /* disable rx and tx dma */
527     st_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
528     st_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
529
530     mace_clean_rings(mp);
531
532     return 0;
533 }
534
535 static inline void mace_set_timeout(struct net_device *dev)
536 {
537     struct mace_data *mp = (struct mace_data *) dev->priv;
538
539     if (mp->timeout_active)
540         del_timer(&mp->tx_timeout);
541     mp->tx_timeout.expires = jiffies + TX_TIMEOUT;
542     mp->tx_timeout.function = mace_tx_timeout;
543     mp->tx_timeout.data = (unsigned long) dev;
544     add_timer(&mp->tx_timeout);
545     mp->timeout_active = 1;
546 }
547
548 static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev)
549 {
550     struct mace_data *mp = (struct mace_data *) dev->priv;
551     volatile struct dbdma_regs *td = mp->tx_dma;
552     volatile struct dbdma_cmd *cp, *np;
553     unsigned long flags;
554     int fill, next, len;
555
556     /* see if there's a free slot in the tx ring */
557     spin_lock_irqsave(&mp->lock, flags);
558     fill = mp->tx_fill;
559     next = fill + 1;
560     if (next >= N_TX_RING)
561         next = 0;
562     if (next == mp->tx_empty) {
563         netif_stop_queue(dev);
564         mp->tx_fullup = 1;
565         spin_unlock_irqrestore(&mp->lock, flags);
566         return 1;               /* can't take it at the moment */
567     }
568     spin_unlock_irqrestore(&mp->lock, flags);
569
570     /* partially fill in the dma command block */
571     len = skb->len;
572     if (len > ETH_FRAME_LEN) {
573         printk(KERN_DEBUG "mace: xmit frame too long (%d)\n", len);
574         len = ETH_FRAME_LEN;
575     }
576     mp->tx_bufs[fill] = skb;
577     cp = mp->tx_cmds + NCMDS_TX * fill;
578     st_le16(&cp->req_count, len);
579     st_le32(&cp->phy_addr, virt_to_bus(skb->data));
580
581     np = mp->tx_cmds + NCMDS_TX * next;
582     out_le16(&np->command, DBDMA_STOP);
583
584     /* poke the tx dma channel */
585     spin_lock_irqsave(&mp->lock, flags);
586     mp->tx_fill = next;
587     if (!mp->tx_bad_runt && mp->tx_active < MAX_TX_ACTIVE) {
588         out_le16(&cp->xfer_status, 0);
589         out_le16(&cp->command, OUTPUT_LAST);
590         out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
591         ++mp->tx_active;
592         mace_set_timeout(dev);
593     }
594     if (++next >= N_TX_RING)
595         next = 0;
596     if (next == mp->tx_empty)
597         netif_stop_queue(dev);
598     spin_unlock_irqrestore(&mp->lock, flags);
599
600     return 0;
601 }
602
603 static struct net_device_stats *mace_stats(struct net_device *dev)
604 {
605     struct mace_data *p = (struct mace_data *) dev->priv;
606
607     return &p->stats;
608 }
609
610 static void mace_set_multicast(struct net_device *dev)
611 {
612     struct mace_data *mp = (struct mace_data *) dev->priv;
613     volatile struct mace *mb = mp->mace;
614     int i, j;
615     u32 crc;
616     unsigned long flags;
617
618     spin_lock_irqsave(&mp->lock, flags);
619     mp->maccc &= ~PROM;
620     if (dev->flags & IFF_PROMISC) {
621         mp->maccc |= PROM;
622     } else {
623         unsigned char multicast_filter[8];
624         struct dev_mc_list *dmi = dev->mc_list;
625
626         if (dev->flags & IFF_ALLMULTI) {
627             for (i = 0; i < 8; i++)
628                 multicast_filter[i] = 0xff;
629         } else {
630             for (i = 0; i < 8; i++)
631                 multicast_filter[i] = 0;
632             for (i = 0; i < dev->mc_count; i++) {
633                 crc = ether_crc_le(6, dmi->dmi_addr);
634                 j = crc >> 26;  /* bit number in multicast_filter */
635                 multicast_filter[j >> 3] |= 1 << (j & 7);
636                 dmi = dmi->next;
637             }
638         }
639 #if 0
640         printk("Multicast filter :");
641         for (i = 0; i < 8; i++)
642             printk("%02x ", multicast_filter[i]);
643         printk("\n");
644 #endif
645
646         if (mp->chipid == BROKEN_ADDRCHG_REV)
647             out_8(&mb->iac, LOGADDR);
648         else {
649             out_8(&mb->iac, ADDRCHG | LOGADDR);
650             while ((in_8(&mb->iac) & ADDRCHG) != 0)
651                 ;
652         }
653         for (i = 0; i < 8; ++i)
654             out_8(&mb->ladrf, multicast_filter[i]);
655         if (mp->chipid != BROKEN_ADDRCHG_REV)
656             out_8(&mb->iac, 0);
657     }
658     /* reset maccc */
659     out_8(&mb->maccc, mp->maccc);
660     spin_unlock_irqrestore(&mp->lock, flags);
661 }
662
663 static void mace_handle_misc_intrs(struct mace_data *mp, int intr)
664 {
665     volatile struct mace *mb = mp->mace;
666     static int mace_babbles, mace_jabbers;
667
668     if (intr & MPCO)
669         mp->stats.rx_missed_errors += 256;
670     mp->stats.rx_missed_errors += in_8(&mb->mpc);   /* reading clears it */
671     if (intr & RNTPCO)
672         mp->stats.rx_length_errors += 256;
673     mp->stats.rx_length_errors += in_8(&mb->rntpc); /* reading clears it */
674     if (intr & CERR)
675         ++mp->stats.tx_heartbeat_errors;
676     if (intr & BABBLE)
677         if (mace_babbles++ < 4)
678             printk(KERN_DEBUG "mace: babbling transmitter\n");
679     if (intr & JABBER)
680         if (mace_jabbers++ < 4)
681             printk(KERN_DEBUG "mace: jabbering transceiver\n");
682 }
683
684 static irqreturn_t mace_interrupt(int irq, void *dev_id, struct pt_regs *regs)
685 {
686     struct net_device *dev = (struct net_device *) dev_id;
687     struct mace_data *mp = (struct mace_data *) dev->priv;
688     volatile struct mace *mb = mp->mace;
689     volatile struct dbdma_regs *td = mp->tx_dma;
690     volatile struct dbdma_cmd *cp;
691     int intr, fs, i, stat, x;
692     int xcount, dstat;
693     unsigned long flags;
694     /* static int mace_last_fs, mace_last_xcount; */
695
696     spin_lock_irqsave(&mp->lock, flags);
697     intr = in_8(&mb->ir);               /* read interrupt register */
698     in_8(&mb->xmtrc);                   /* get retries */
699     mace_handle_misc_intrs(mp, intr);
700
701     i = mp->tx_empty;
702     while (in_8(&mb->pr) & XMTSV) {
703         del_timer(&mp->tx_timeout);
704         mp->timeout_active = 0;
705         /*
706          * Clear any interrupt indication associated with this status
707          * word.  This appears to unlatch any error indication from
708          * the DMA controller.
709          */
710         intr = in_8(&mb->ir);
711         if (intr != 0)
712             mace_handle_misc_intrs(mp, intr);
713         if (mp->tx_bad_runt) {
714             fs = in_8(&mb->xmtfs);
715             mp->tx_bad_runt = 0;
716             out_8(&mb->xmtfc, AUTO_PAD_XMIT);
717             continue;
718         }
719         dstat = ld_le32(&td->status);
720         /* stop DMA controller */
721         out_le32(&td->control, RUN << 16);
722         /*
723          * xcount is the number of complete frames which have been
724          * written to the fifo but for which status has not been read.
725          */
726         xcount = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
727         if (xcount == 0 || (dstat & DEAD)) {
728             /*
729              * If a packet was aborted before the DMA controller has
730              * finished transferring it, it seems that there are 2 bytes
731              * which are stuck in some buffer somewhere.  These will get
732              * transmitted as soon as we read the frame status (which
733              * reenables the transmit data transfer request).  Turning
734              * off the DMA controller and/or resetting the MACE doesn't
735              * help.  So we disable auto-padding and FCS transmission
736              * so the two bytes will only be a runt packet which should
737              * be ignored by other stations.
738              */
739             out_8(&mb->xmtfc, DXMTFCS);
740         }
741         fs = in_8(&mb->xmtfs);
742         if ((fs & XMTSV) == 0) {
743             printk(KERN_ERR "mace: xmtfs not valid! (fs=%x xc=%d ds=%x)\n",
744                    fs, xcount, dstat);
745             mace_reset(dev);
746                 /*
747                  * XXX mace likes to hang the machine after a xmtfs error.
748                  * This is hard to reproduce, reseting *may* help
749                  */
750         }
751         cp = mp->tx_cmds + NCMDS_TX * i;
752         stat = ld_le16(&cp->xfer_status);
753         if ((fs & (UFLO|LCOL|LCAR|RTRY)) || (dstat & DEAD) || xcount == 0) {
754             /*
755              * Check whether there were in fact 2 bytes written to
756              * the transmit FIFO.
757              */
758             udelay(1);
759             x = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
760             if (x != 0) {
761                 /* there were two bytes with an end-of-packet indication */
762                 mp->tx_bad_runt = 1;
763                 mace_set_timeout(dev);
764             } else {
765                 /*
766                  * Either there weren't the two bytes buffered up, or they
767                  * didn't have an end-of-packet indication.
768                  * We flush the transmit FIFO just in case (by setting the
769                  * XMTFWU bit with the transmitter disabled).
770                  */
771                 out_8(&mb->maccc, in_8(&mb->maccc) & ~ENXMT);
772                 out_8(&mb->fifocc, in_8(&mb->fifocc) | XMTFWU);
773                 udelay(1);
774                 out_8(&mb->maccc, in_8(&mb->maccc) | ENXMT);
775                 out_8(&mb->xmtfc, AUTO_PAD_XMIT);
776             }
777         }
778         /* dma should have finished */
779         if (i == mp->tx_fill) {
780             printk(KERN_DEBUG "mace: tx ring ran out? (fs=%x xc=%d ds=%x)\n",
781                    fs, xcount, dstat);
782             continue;
783         }
784         /* Update stats */
785         if (fs & (UFLO|LCOL|LCAR|RTRY)) {
786             ++mp->stats.tx_errors;
787             if (fs & LCAR)
788                 ++mp->stats.tx_carrier_errors;
789             if (fs & (UFLO|LCOL|RTRY))
790                 ++mp->stats.tx_aborted_errors;
791         } else {
792             mp->stats.tx_bytes += mp->tx_bufs[i]->len;
793             ++mp->stats.tx_packets;
794         }
795         dev_kfree_skb_irq(mp->tx_bufs[i]);
796         --mp->tx_active;
797         if (++i >= N_TX_RING)
798             i = 0;
799 #if 0
800         mace_last_fs = fs;
801         mace_last_xcount = xcount;
802 #endif
803     }
804
805     if (i != mp->tx_empty) {
806         mp->tx_fullup = 0;
807         netif_wake_queue(dev);
808     }
809     mp->tx_empty = i;
810     i += mp->tx_active;
811     if (i >= N_TX_RING)
812         i -= N_TX_RING;
813     if (!mp->tx_bad_runt && i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE) {
814         do {
815             /* set up the next one */
816             cp = mp->tx_cmds + NCMDS_TX * i;
817             out_le16(&cp->xfer_status, 0);
818             out_le16(&cp->command, OUTPUT_LAST);
819             ++mp->tx_active;
820             if (++i >= N_TX_RING)
821                 i = 0;
822         } while (i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE);
823         out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
824         mace_set_timeout(dev);
825     }
826     spin_unlock_irqrestore(&mp->lock, flags);
827     return IRQ_HANDLED;
828 }
829
830 static void mace_tx_timeout(unsigned long data)
831 {
832     struct net_device *dev = (struct net_device *) data;
833     struct mace_data *mp = (struct mace_data *) dev->priv;
834     volatile struct mace *mb = mp->mace;
835     volatile struct dbdma_regs *td = mp->tx_dma;
836     volatile struct dbdma_regs *rd = mp->rx_dma;
837     volatile struct dbdma_cmd *cp;
838     unsigned long flags;
839     int i;
840
841     spin_lock_irqsave(&mp->lock, flags);
842     mp->timeout_active = 0;
843     if (mp->tx_active == 0 && !mp->tx_bad_runt)
844         goto out;
845
846     /* update various counters */
847     mace_handle_misc_intrs(mp, in_8(&mb->ir));
848
849     cp = mp->tx_cmds + NCMDS_TX * mp->tx_empty;
850
851     /* turn off both tx and rx and reset the chip */
852     out_8(&mb->maccc, 0);
853     printk(KERN_ERR "mace: transmit timeout - resetting\n");
854     dbdma_reset(td);
855     mace_reset(dev);
856
857     /* restart rx dma */
858     cp = bus_to_virt(ld_le32(&rd->cmdptr));
859     dbdma_reset(rd);
860     out_le16(&cp->xfer_status, 0);
861     out_le32(&rd->cmdptr, virt_to_bus(cp));
862     out_le32(&rd->control, (RUN << 16) | RUN);
863
864     /* fix up the transmit side */
865     i = mp->tx_empty;
866     mp->tx_active = 0;
867     ++mp->stats.tx_errors;
868     if (mp->tx_bad_runt) {
869         mp->tx_bad_runt = 0;
870     } else if (i != mp->tx_fill) {
871         dev_kfree_skb(mp->tx_bufs[i]);
872         if (++i >= N_TX_RING)
873             i = 0;
874         mp->tx_empty = i;
875     }
876     mp->tx_fullup = 0;
877     netif_wake_queue(dev);
878     if (i != mp->tx_fill) {
879         cp = mp->tx_cmds + NCMDS_TX * i;
880         out_le16(&cp->xfer_status, 0);
881         out_le16(&cp->command, OUTPUT_LAST);
882         out_le32(&td->cmdptr, virt_to_bus(cp));
883         out_le32(&td->control, (RUN << 16) | RUN);
884         ++mp->tx_active;
885         mace_set_timeout(dev);
886     }
887
888     /* turn it back on */
889     out_8(&mb->imr, RCVINT);
890     out_8(&mb->maccc, mp->maccc);
891
892 out:
893     spin_unlock_irqrestore(&mp->lock, flags);
894 }
895
896 static irqreturn_t mace_txdma_intr(int irq, void *dev_id, struct pt_regs *regs)
897 {
898         return IRQ_HANDLED;
899 }
900
901 static irqreturn_t mace_rxdma_intr(int irq, void *dev_id, struct pt_regs *regs)
902 {
903     struct net_device *dev = (struct net_device *) dev_id;
904     struct mace_data *mp = (struct mace_data *) dev->priv;
905     volatile struct dbdma_regs *rd = mp->rx_dma;
906     volatile struct dbdma_cmd *cp, *np;
907     int i, nb, stat, next;
908     struct sk_buff *skb;
909     unsigned frame_status;
910     static int mace_lost_status;
911     unsigned char *data;
912     unsigned long flags;
913
914     spin_lock_irqsave(&mp->lock, flags);
915     for (i = mp->rx_empty; i != mp->rx_fill; ) {
916         cp = mp->rx_cmds + i;
917         stat = ld_le16(&cp->xfer_status);
918         if ((stat & ACTIVE) == 0) {
919             next = i + 1;
920             if (next >= N_RX_RING)
921                 next = 0;
922             np = mp->rx_cmds + next;
923             if (next != mp->rx_fill
924                 && (ld_le16(&np->xfer_status) & ACTIVE) != 0) {
925                 printk(KERN_DEBUG "mace: lost a status word\n");
926                 ++mace_lost_status;
927             } else
928                 break;
929         }
930         nb = ld_le16(&cp->req_count) - ld_le16(&cp->res_count);
931         out_le16(&cp->command, DBDMA_STOP);
932         /* got a packet, have a look at it */
933         skb = mp->rx_bufs[i];
934         if (skb == 0) {
935             ++mp->stats.rx_dropped;
936         } else if (nb > 8) {
937             data = skb->data;
938             frame_status = (data[nb-3] << 8) + data[nb-4];
939             if (frame_status & (RS_OFLO|RS_CLSN|RS_FRAMERR|RS_FCSERR)) {
940                 ++mp->stats.rx_errors;
941                 if (frame_status & RS_OFLO)
942                     ++mp->stats.rx_over_errors;
943                 if (frame_status & RS_FRAMERR)
944                     ++mp->stats.rx_frame_errors;
945                 if (frame_status & RS_FCSERR)
946                     ++mp->stats.rx_crc_errors;
947             } else {
948                 /* Mace feature AUTO_STRIP_RCV is on by default, dropping the
949                  * FCS on frames with 802.3 headers. This means that Ethernet
950                  * frames have 8 extra octets at the end, while 802.3 frames
951                  * have only 4. We need to correctly account for this. */
952                 if (*(unsigned short *)(data+12) < 1536) /* 802.3 header */
953                     nb -= 4;
954                 else    /* Ethernet header; mace includes FCS */
955                     nb -= 8;
956                 skb_put(skb, nb);
957                 skb->dev = dev;
958                 skb->protocol = eth_type_trans(skb, dev);
959                 mp->stats.rx_bytes += skb->len;
960                 netif_rx(skb);
961                 dev->last_rx = jiffies;
962                 mp->rx_bufs[i] = 0;
963                 ++mp->stats.rx_packets;
964             }
965         } else {
966             ++mp->stats.rx_errors;
967             ++mp->stats.rx_length_errors;
968         }
969
970         /* advance to next */
971         if (++i >= N_RX_RING)
972             i = 0;
973     }
974     mp->rx_empty = i;
975
976     i = mp->rx_fill;
977     for (;;) {
978         next = i + 1;
979         if (next >= N_RX_RING)
980             next = 0;
981         if (next == mp->rx_empty)
982             break;
983         cp = mp->rx_cmds + i;
984         skb = mp->rx_bufs[i];
985         if (skb == 0) {
986             skb = dev_alloc_skb(RX_BUFLEN + 2);
987             if (skb != 0) {
988                 skb_reserve(skb, 2);
989                 mp->rx_bufs[i] = skb;
990             }
991         }
992         st_le16(&cp->req_count, RX_BUFLEN);
993         data = skb? skb->data: dummy_buf;
994         st_le32(&cp->phy_addr, virt_to_bus(data));
995         out_le16(&cp->xfer_status, 0);
996         out_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
997 #if 0
998         if ((ld_le32(&rd->status) & ACTIVE) != 0) {
999             out_le32(&rd->control, (PAUSE << 16) | PAUSE);
1000             while ((in_le32(&rd->status) & ACTIVE) != 0)
1001                 ;
1002         }
1003 #endif
1004         i = next;
1005     }
1006     if (i != mp->rx_fill) {
1007         out_le32(&rd->control, ((RUN|WAKE) << 16) | (RUN|WAKE));
1008         mp->rx_fill = i;
1009     }
1010     spin_unlock_irqrestore(&mp->lock, flags);
1011     return IRQ_HANDLED;
1012 }
1013
1014 static struct of_match mace_match[] = 
1015 {
1016         {
1017         .name           = "mace",
1018         .type           = OF_ANY_MATCH,
1019         .compatible     = OF_ANY_MATCH
1020         },
1021         {},
1022 };
1023
1024 static struct macio_driver mace_driver = 
1025 {
1026         .name           = "mace",
1027         .match_table    = mace_match,
1028         .probe          = mace_probe,
1029         .remove         = mace_remove,
1030 };
1031
1032
1033 static int __init mace_init(void)
1034 {
1035         return macio_register_driver(&mace_driver);
1036 }
1037
1038 static void __exit mace_cleanup(void)
1039 {
1040         macio_unregister_driver(&mace_driver);
1041
1042         if (dummy_buf) {
1043                 kfree(dummy_buf);
1044                 dummy_buf = NULL;
1045         }
1046 }
1047
1048 MODULE_AUTHOR("Paul Mackerras");
1049 MODULE_DESCRIPTION("PowerMac MACE driver.");
1050 MODULE_PARM(port_aaui, "i");
1051 MODULE_PARM_DESC(port_aaui, "MACE uses AAUI port (0-1)");
1052 MODULE_LICENSE("GPL");
1053
1054 module_init(mace_init);
1055 module_exit(mace_cleanup);