[MTD] NAND: Fix nand_default_mark_blockbad() when flash-based BBT disabled
[linux-2.6/linux-loongson.git] / drivers / net / bmac.c
blob4528ce9c4e435f9312ff87f62b944bad45cc1073
1 /*
2 * Network device driver for the BMAC ethernet controller on
3 * Apple Powermacs. Assumes it's under a DBDMA controller.
5 * Copyright (C) 1998 Randy Gobbel.
7 * May 1999, Al Viro: proper release of /proc/net/bmac entry, switched to
8 * dynamic procfs inode.
9 */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/delay.h>
15 #include <linux/string.h>
16 #include <linux/timer.h>
17 #include <linux/proc_fs.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/crc32.h>
21 #include <asm/prom.h>
22 #include <asm/dbdma.h>
23 #include <asm/io.h>
24 #include <asm/page.h>
25 #include <asm/pgtable.h>
26 #include <asm/machdep.h>
27 #include <asm/pmac_feature.h>
28 #include <asm/macio.h>
29 #include <asm/irq.h>
31 #include "bmac.h"
33 #define trunc_page(x) ((void *)(((unsigned long)(x)) & ~((unsigned long)(PAGE_SIZE - 1))))
34 #define round_page(x) trunc_page(((unsigned long)(x)) + ((unsigned long)(PAGE_SIZE - 1)))
37 * CRC polynomial - used in working out multicast filter bits.
39 #define ENET_CRCPOLY 0x04c11db7
41 /* switch to use multicast code lifted from sunhme driver */
42 #define SUNHME_MULTICAST
44 #define N_RX_RING 64
45 #define N_TX_RING 32
46 #define MAX_TX_ACTIVE 1
47 #define ETHERCRC 4
48 #define ETHERMINPACKET 64
49 #define ETHERMTU 1500
50 #define RX_BUFLEN (ETHERMTU + 14 + ETHERCRC + 2)
51 #define TX_TIMEOUT HZ /* 1 second */
53 /* Bits in transmit DMA status */
54 #define TX_DMA_ERR 0x80
56 #define XXDEBUG(args)
58 struct bmac_data {
59 /* volatile struct bmac *bmac; */
60 struct sk_buff_head *queue;
61 volatile struct dbdma_regs __iomem *tx_dma;
62 int tx_dma_intr;
63 volatile struct dbdma_regs __iomem *rx_dma;
64 int rx_dma_intr;
65 volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
66 volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
67 struct macio_dev *mdev;
68 int is_bmac_plus;
69 struct sk_buff *rx_bufs[N_RX_RING];
70 int rx_fill;
71 int rx_empty;
72 struct sk_buff *tx_bufs[N_TX_RING];
73 int tx_fill;
74 int tx_empty;
75 unsigned char tx_fullup;
76 struct net_device_stats stats;
77 struct timer_list tx_timeout;
78 int timeout_active;
79 int sleeping;
80 int opened;
81 unsigned short hash_use_count[64];
82 unsigned short hash_table_mask[4];
83 spinlock_t lock;
86 #if 0 /* Move that to ethtool */
88 typedef struct bmac_reg_entry {
89 char *name;
90 unsigned short reg_offset;
91 } bmac_reg_entry_t;
93 #define N_REG_ENTRIES 31
95 static bmac_reg_entry_t reg_entries[N_REG_ENTRIES] = {
96 {"MEMADD", MEMADD},
97 {"MEMDATAHI", MEMDATAHI},
98 {"MEMDATALO", MEMDATALO},
99 {"TXPNTR", TXPNTR},
100 {"RXPNTR", RXPNTR},
101 {"IPG1", IPG1},
102 {"IPG2", IPG2},
103 {"ALIMIT", ALIMIT},
104 {"SLOT", SLOT},
105 {"PALEN", PALEN},
106 {"PAPAT", PAPAT},
107 {"TXSFD", TXSFD},
108 {"JAM", JAM},
109 {"TXCFG", TXCFG},
110 {"TXMAX", TXMAX},
111 {"TXMIN", TXMIN},
112 {"PAREG", PAREG},
113 {"DCNT", DCNT},
114 {"NCCNT", NCCNT},
115 {"NTCNT", NTCNT},
116 {"EXCNT", EXCNT},
117 {"LTCNT", LTCNT},
118 {"TXSM", TXSM},
119 {"RXCFG", RXCFG},
120 {"RXMAX", RXMAX},
121 {"RXMIN", RXMIN},
122 {"FRCNT", FRCNT},
123 {"AECNT", AECNT},
124 {"FECNT", FECNT},
125 {"RXSM", RXSM},
126 {"RXCV", RXCV}
129 #endif
131 static unsigned char *bmac_emergency_rxbuf;
134 * Number of bytes of private data per BMAC: allow enough for
135 * the rx and tx dma commands plus a branch dma command each,
136 * and another 16 bytes to allow us to align the dma command
137 * buffers on a 16 byte boundary.
139 #define PRIV_BYTES (sizeof(struct bmac_data) \
140 + (N_RX_RING + N_TX_RING + 4) * sizeof(struct dbdma_cmd) \
141 + sizeof(struct sk_buff_head))
143 static unsigned char bitrev(unsigned char b);
144 static int bmac_open(struct net_device *dev);
145 static int bmac_close(struct net_device *dev);
146 static int bmac_transmit_packet(struct sk_buff *skb, struct net_device *dev);
147 static struct net_device_stats *bmac_stats(struct net_device *dev);
148 static void bmac_set_multicast(struct net_device *dev);
149 static void bmac_reset_and_enable(struct net_device *dev);
150 static void bmac_start_chip(struct net_device *dev);
151 static void bmac_init_chip(struct net_device *dev);
152 static void bmac_init_registers(struct net_device *dev);
153 static void bmac_enable_and_reset_chip(struct net_device *dev);
154 static int bmac_set_address(struct net_device *dev, void *addr);
155 static irqreturn_t bmac_misc_intr(int irq, void *dev_id);
156 static irqreturn_t bmac_txdma_intr(int irq, void *dev_id);
157 static irqreturn_t bmac_rxdma_intr(int irq, void *dev_id);
158 static void bmac_set_timeout(struct net_device *dev);
159 static void bmac_tx_timeout(unsigned long data);
160 static int bmac_output(struct sk_buff *skb, struct net_device *dev);
161 static void bmac_start(struct net_device *dev);
163 #define DBDMA_SET(x) ( ((x) | (x) << 16) )
164 #define DBDMA_CLEAR(x) ( (x) << 16)
166 static inline void
167 dbdma_st32(volatile __u32 __iomem *a, unsigned long x)
169 __asm__ volatile( "stwbrx %0,0,%1" : : "r" (x), "r" (a) : "memory");
170 return;
173 static inline unsigned long
174 dbdma_ld32(volatile __u32 __iomem *a)
176 __u32 swap;
177 __asm__ volatile ("lwbrx %0,0,%1" : "=r" (swap) : "r" (a));
178 return swap;
181 static void
182 dbdma_continue(volatile struct dbdma_regs __iomem *dmap)
184 dbdma_st32(&dmap->control,
185 DBDMA_SET(RUN|WAKE) | DBDMA_CLEAR(PAUSE|DEAD));
186 eieio();
189 static void
190 dbdma_reset(volatile struct dbdma_regs __iomem *dmap)
192 dbdma_st32(&dmap->control,
193 DBDMA_CLEAR(ACTIVE|DEAD|WAKE|FLUSH|PAUSE|RUN));
194 eieio();
195 while (dbdma_ld32(&dmap->status) & RUN)
196 eieio();
199 static void
200 dbdma_setcmd(volatile struct dbdma_cmd *cp,
201 unsigned short cmd, unsigned count, unsigned long addr,
202 unsigned long cmd_dep)
204 out_le16(&cp->command, cmd);
205 out_le16(&cp->req_count, count);
206 out_le32(&cp->phy_addr, addr);
207 out_le32(&cp->cmd_dep, cmd_dep);
208 out_le16(&cp->xfer_status, 0);
209 out_le16(&cp->res_count, 0);
212 static inline
213 void bmwrite(struct net_device *dev, unsigned long reg_offset, unsigned data )
215 out_le16((void __iomem *)dev->base_addr + reg_offset, data);
219 static inline
220 unsigned short bmread(struct net_device *dev, unsigned long reg_offset )
222 return in_le16((void __iomem *)dev->base_addr + reg_offset);
225 static void
226 bmac_enable_and_reset_chip(struct net_device *dev)
228 struct bmac_data *bp = netdev_priv(dev);
229 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
230 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
232 if (rd)
233 dbdma_reset(rd);
234 if (td)
235 dbdma_reset(td);
237 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 1);
240 #define MIFDELAY udelay(10)
242 static unsigned int
243 bmac_mif_readbits(struct net_device *dev, int nb)
245 unsigned int val = 0;
247 while (--nb >= 0) {
248 bmwrite(dev, MIFCSR, 0);
249 MIFDELAY;
250 if (bmread(dev, MIFCSR) & 8)
251 val |= 1 << nb;
252 bmwrite(dev, MIFCSR, 1);
253 MIFDELAY;
255 bmwrite(dev, MIFCSR, 0);
256 MIFDELAY;
257 bmwrite(dev, MIFCSR, 1);
258 MIFDELAY;
259 return val;
262 static void
263 bmac_mif_writebits(struct net_device *dev, unsigned int val, int nb)
265 int b;
267 while (--nb >= 0) {
268 b = (val & (1 << nb))? 6: 4;
269 bmwrite(dev, MIFCSR, b);
270 MIFDELAY;
271 bmwrite(dev, MIFCSR, b|1);
272 MIFDELAY;
276 static unsigned int
277 bmac_mif_read(struct net_device *dev, unsigned int addr)
279 unsigned int val;
281 bmwrite(dev, MIFCSR, 4);
282 MIFDELAY;
283 bmac_mif_writebits(dev, ~0U, 32);
284 bmac_mif_writebits(dev, 6, 4);
285 bmac_mif_writebits(dev, addr, 10);
286 bmwrite(dev, MIFCSR, 2);
287 MIFDELAY;
288 bmwrite(dev, MIFCSR, 1);
289 MIFDELAY;
290 val = bmac_mif_readbits(dev, 17);
291 bmwrite(dev, MIFCSR, 4);
292 MIFDELAY;
293 return val;
296 static void
297 bmac_mif_write(struct net_device *dev, unsigned int addr, unsigned int val)
299 bmwrite(dev, MIFCSR, 4);
300 MIFDELAY;
301 bmac_mif_writebits(dev, ~0U, 32);
302 bmac_mif_writebits(dev, 5, 4);
303 bmac_mif_writebits(dev, addr, 10);
304 bmac_mif_writebits(dev, 2, 2);
305 bmac_mif_writebits(dev, val, 16);
306 bmac_mif_writebits(dev, 3, 2);
309 static void
310 bmac_init_registers(struct net_device *dev)
312 struct bmac_data *bp = netdev_priv(dev);
313 volatile unsigned short regValue;
314 unsigned short *pWord16;
315 int i;
317 /* XXDEBUG(("bmac: enter init_registers\n")); */
319 bmwrite(dev, RXRST, RxResetValue);
320 bmwrite(dev, TXRST, TxResetBit);
322 i = 100;
323 do {
324 --i;
325 udelay(10000);
326 regValue = bmread(dev, TXRST); /* wait for reset to clear..acknowledge */
327 } while ((regValue & TxResetBit) && i > 0);
329 if (!bp->is_bmac_plus) {
330 regValue = bmread(dev, XCVRIF);
331 regValue |= ClkBit | SerialMode | COLActiveLow;
332 bmwrite(dev, XCVRIF, regValue);
333 udelay(10000);
336 bmwrite(dev, RSEED, (unsigned short)0x1968);
338 regValue = bmread(dev, XIFC);
339 regValue |= TxOutputEnable;
340 bmwrite(dev, XIFC, regValue);
342 bmread(dev, PAREG);
344 /* set collision counters to 0 */
345 bmwrite(dev, NCCNT, 0);
346 bmwrite(dev, NTCNT, 0);
347 bmwrite(dev, EXCNT, 0);
348 bmwrite(dev, LTCNT, 0);
350 /* set rx counters to 0 */
351 bmwrite(dev, FRCNT, 0);
352 bmwrite(dev, LECNT, 0);
353 bmwrite(dev, AECNT, 0);
354 bmwrite(dev, FECNT, 0);
355 bmwrite(dev, RXCV, 0);
357 /* set tx fifo information */
358 bmwrite(dev, TXTH, 4); /* 4 octets before tx starts */
360 bmwrite(dev, TXFIFOCSR, 0); /* first disable txFIFO */
361 bmwrite(dev, TXFIFOCSR, TxFIFOEnable );
363 /* set rx fifo information */
364 bmwrite(dev, RXFIFOCSR, 0); /* first disable rxFIFO */
365 bmwrite(dev, RXFIFOCSR, RxFIFOEnable );
367 //bmwrite(dev, TXCFG, TxMACEnable); /* TxNeverGiveUp maybe later */
368 bmread(dev, STATUS); /* read it just to clear it */
370 /* zero out the chip Hash Filter registers */
371 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0;
372 bmwrite(dev, BHASH3, bp->hash_table_mask[0]); /* bits 15 - 0 */
373 bmwrite(dev, BHASH2, bp->hash_table_mask[1]); /* bits 31 - 16 */
374 bmwrite(dev, BHASH1, bp->hash_table_mask[2]); /* bits 47 - 32 */
375 bmwrite(dev, BHASH0, bp->hash_table_mask[3]); /* bits 63 - 48 */
377 pWord16 = (unsigned short *)dev->dev_addr;
378 bmwrite(dev, MADD0, *pWord16++);
379 bmwrite(dev, MADD1, *pWord16++);
380 bmwrite(dev, MADD2, *pWord16);
382 bmwrite(dev, RXCFG, RxCRCNoStrip | RxHashFilterEnable | RxRejectOwnPackets);
384 bmwrite(dev, INTDISABLE, EnableNormal);
386 return;
389 #if 0
390 static void
391 bmac_disable_interrupts(struct net_device *dev)
393 bmwrite(dev, INTDISABLE, DisableAll);
396 static void
397 bmac_enable_interrupts(struct net_device *dev)
399 bmwrite(dev, INTDISABLE, EnableNormal);
401 #endif
404 static void
405 bmac_start_chip(struct net_device *dev)
407 struct bmac_data *bp = netdev_priv(dev);
408 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
409 unsigned short oldConfig;
411 /* enable rx dma channel */
412 dbdma_continue(rd);
414 oldConfig = bmread(dev, TXCFG);
415 bmwrite(dev, TXCFG, oldConfig | TxMACEnable );
417 /* turn on rx plus any other bits already on (promiscuous possibly) */
418 oldConfig = bmread(dev, RXCFG);
419 bmwrite(dev, RXCFG, oldConfig | RxMACEnable );
420 udelay(20000);
423 static void
424 bmac_init_phy(struct net_device *dev)
426 unsigned int addr;
427 struct bmac_data *bp = netdev_priv(dev);
429 printk(KERN_DEBUG "phy registers:");
430 for (addr = 0; addr < 32; ++addr) {
431 if ((addr & 7) == 0)
432 printk("\n" KERN_DEBUG);
433 printk(" %.4x", bmac_mif_read(dev, addr));
435 printk("\n");
436 if (bp->is_bmac_plus) {
437 unsigned int capable, ctrl;
439 ctrl = bmac_mif_read(dev, 0);
440 capable = ((bmac_mif_read(dev, 1) & 0xf800) >> 6) | 1;
441 if (bmac_mif_read(dev, 4) != capable
442 || (ctrl & 0x1000) == 0) {
443 bmac_mif_write(dev, 4, capable);
444 bmac_mif_write(dev, 0, 0x1200);
445 } else
446 bmac_mif_write(dev, 0, 0x1000);
450 static void bmac_init_chip(struct net_device *dev)
452 bmac_init_phy(dev);
453 bmac_init_registers(dev);
456 #ifdef CONFIG_PM
457 static int bmac_suspend(struct macio_dev *mdev, pm_message_t state)
459 struct net_device* dev = macio_get_drvdata(mdev);
460 struct bmac_data *bp = netdev_priv(dev);
461 unsigned long flags;
462 unsigned short config;
463 int i;
465 netif_device_detach(dev);
466 /* prolly should wait for dma to finish & turn off the chip */
467 spin_lock_irqsave(&bp->lock, flags);
468 if (bp->timeout_active) {
469 del_timer(&bp->tx_timeout);
470 bp->timeout_active = 0;
472 disable_irq(dev->irq);
473 disable_irq(bp->tx_dma_intr);
474 disable_irq(bp->rx_dma_intr);
475 bp->sleeping = 1;
476 spin_unlock_irqrestore(&bp->lock, flags);
477 if (bp->opened) {
478 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
479 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
481 config = bmread(dev, RXCFG);
482 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
483 config = bmread(dev, TXCFG);
484 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
485 bmwrite(dev, INTDISABLE, DisableAll); /* disable all intrs */
486 /* disable rx and tx dma */
487 st_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
488 st_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
489 /* free some skb's */
490 for (i=0; i<N_RX_RING; i++) {
491 if (bp->rx_bufs[i] != NULL) {
492 dev_kfree_skb(bp->rx_bufs[i]);
493 bp->rx_bufs[i] = NULL;
496 for (i = 0; i<N_TX_RING; i++) {
497 if (bp->tx_bufs[i] != NULL) {
498 dev_kfree_skb(bp->tx_bufs[i]);
499 bp->tx_bufs[i] = NULL;
503 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
504 return 0;
507 static int bmac_resume(struct macio_dev *mdev)
509 struct net_device* dev = macio_get_drvdata(mdev);
510 struct bmac_data *bp = netdev_priv(dev);
512 /* see if this is enough */
513 if (bp->opened)
514 bmac_reset_and_enable(dev);
516 enable_irq(dev->irq);
517 enable_irq(bp->tx_dma_intr);
518 enable_irq(bp->rx_dma_intr);
519 netif_device_attach(dev);
521 return 0;
523 #endif /* CONFIG_PM */
525 static int bmac_set_address(struct net_device *dev, void *addr)
527 struct bmac_data *bp = netdev_priv(dev);
528 unsigned char *p = addr;
529 unsigned short *pWord16;
530 unsigned long flags;
531 int i;
533 XXDEBUG(("bmac: enter set_address\n"));
534 spin_lock_irqsave(&bp->lock, flags);
536 for (i = 0; i < 6; ++i) {
537 dev->dev_addr[i] = p[i];
539 /* load up the hardware address */
540 pWord16 = (unsigned short *)dev->dev_addr;
541 bmwrite(dev, MADD0, *pWord16++);
542 bmwrite(dev, MADD1, *pWord16++);
543 bmwrite(dev, MADD2, *pWord16);
545 spin_unlock_irqrestore(&bp->lock, flags);
546 XXDEBUG(("bmac: exit set_address\n"));
547 return 0;
550 static inline void bmac_set_timeout(struct net_device *dev)
552 struct bmac_data *bp = netdev_priv(dev);
553 unsigned long flags;
555 spin_lock_irqsave(&bp->lock, flags);
556 if (bp->timeout_active)
557 del_timer(&bp->tx_timeout);
558 bp->tx_timeout.expires = jiffies + TX_TIMEOUT;
559 bp->tx_timeout.function = bmac_tx_timeout;
560 bp->tx_timeout.data = (unsigned long) dev;
561 add_timer(&bp->tx_timeout);
562 bp->timeout_active = 1;
563 spin_unlock_irqrestore(&bp->lock, flags);
566 static void
567 bmac_construct_xmt(struct sk_buff *skb, volatile struct dbdma_cmd *cp)
569 void *vaddr;
570 unsigned long baddr;
571 unsigned long len;
573 len = skb->len;
574 vaddr = skb->data;
575 baddr = virt_to_bus(vaddr);
577 dbdma_setcmd(cp, (OUTPUT_LAST | INTR_ALWAYS | WAIT_IFCLR), len, baddr, 0);
580 static void
581 bmac_construct_rxbuff(struct sk_buff *skb, volatile struct dbdma_cmd *cp)
583 unsigned char *addr = skb? skb->data: bmac_emergency_rxbuf;
585 dbdma_setcmd(cp, (INPUT_LAST | INTR_ALWAYS), RX_BUFLEN,
586 virt_to_bus(addr), 0);
589 /* Bit-reverse one byte of an ethernet hardware address. */
590 static unsigned char
591 bitrev(unsigned char b)
593 int d = 0, i;
595 for (i = 0; i < 8; ++i, b >>= 1)
596 d = (d << 1) | (b & 1);
597 return d;
601 static void
602 bmac_init_tx_ring(struct bmac_data *bp)
604 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
606 memset((char *)bp->tx_cmds, 0, (N_TX_RING+1) * sizeof(struct dbdma_cmd));
608 bp->tx_empty = 0;
609 bp->tx_fill = 0;
610 bp->tx_fullup = 0;
612 /* put a branch at the end of the tx command list */
613 dbdma_setcmd(&bp->tx_cmds[N_TX_RING],
614 (DBDMA_NOP | BR_ALWAYS), 0, 0, virt_to_bus(bp->tx_cmds));
616 /* reset tx dma */
617 dbdma_reset(td);
618 out_le32(&td->wait_sel, 0x00200020);
619 out_le32(&td->cmdptr, virt_to_bus(bp->tx_cmds));
622 static int
623 bmac_init_rx_ring(struct bmac_data *bp)
625 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
626 int i;
627 struct sk_buff *skb;
629 /* initialize list of sk_buffs for receiving and set up recv dma */
630 memset((char *)bp->rx_cmds, 0,
631 (N_RX_RING + 1) * sizeof(struct dbdma_cmd));
632 for (i = 0; i < N_RX_RING; i++) {
633 if ((skb = bp->rx_bufs[i]) == NULL) {
634 bp->rx_bufs[i] = skb = dev_alloc_skb(RX_BUFLEN+2);
635 if (skb != NULL)
636 skb_reserve(skb, 2);
638 bmac_construct_rxbuff(skb, &bp->rx_cmds[i]);
641 bp->rx_empty = 0;
642 bp->rx_fill = i;
644 /* Put a branch back to the beginning of the receive command list */
645 dbdma_setcmd(&bp->rx_cmds[N_RX_RING],
646 (DBDMA_NOP | BR_ALWAYS), 0, 0, virt_to_bus(bp->rx_cmds));
648 /* start rx dma */
649 dbdma_reset(rd);
650 out_le32(&rd->cmdptr, virt_to_bus(bp->rx_cmds));
652 return 1;
656 static int bmac_transmit_packet(struct sk_buff *skb, struct net_device *dev)
658 struct bmac_data *bp = netdev_priv(dev);
659 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
660 int i;
662 /* see if there's a free slot in the tx ring */
663 /* XXDEBUG(("bmac_xmit_start: empty=%d fill=%d\n", */
664 /* bp->tx_empty, bp->tx_fill)); */
665 i = bp->tx_fill + 1;
666 if (i >= N_TX_RING)
667 i = 0;
668 if (i == bp->tx_empty) {
669 netif_stop_queue(dev);
670 bp->tx_fullup = 1;
671 XXDEBUG(("bmac_transmit_packet: tx ring full\n"));
672 return -1; /* can't take it at the moment */
675 dbdma_setcmd(&bp->tx_cmds[i], DBDMA_STOP, 0, 0, 0);
677 bmac_construct_xmt(skb, &bp->tx_cmds[bp->tx_fill]);
679 bp->tx_bufs[bp->tx_fill] = skb;
680 bp->tx_fill = i;
682 bp->stats.tx_bytes += skb->len;
684 dbdma_continue(td);
686 return 0;
689 static int rxintcount;
691 static irqreturn_t bmac_rxdma_intr(int irq, void *dev_id)
693 struct net_device *dev = (struct net_device *) dev_id;
694 struct bmac_data *bp = netdev_priv(dev);
695 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
696 volatile struct dbdma_cmd *cp;
697 int i, nb, stat;
698 struct sk_buff *skb;
699 unsigned int residual;
700 int last;
701 unsigned long flags;
703 spin_lock_irqsave(&bp->lock, flags);
705 if (++rxintcount < 10) {
706 XXDEBUG(("bmac_rxdma_intr\n"));
709 last = -1;
710 i = bp->rx_empty;
712 while (1) {
713 cp = &bp->rx_cmds[i];
714 stat = ld_le16(&cp->xfer_status);
715 residual = ld_le16(&cp->res_count);
716 if ((stat & ACTIVE) == 0)
717 break;
718 nb = RX_BUFLEN - residual - 2;
719 if (nb < (ETHERMINPACKET - ETHERCRC)) {
720 skb = NULL;
721 bp->stats.rx_length_errors++;
722 bp->stats.rx_errors++;
723 } else {
724 skb = bp->rx_bufs[i];
725 bp->rx_bufs[i] = NULL;
727 if (skb != NULL) {
728 nb -= ETHERCRC;
729 skb_put(skb, nb);
730 skb->dev = dev;
731 skb->protocol = eth_type_trans(skb, dev);
732 netif_rx(skb);
733 dev->last_rx = jiffies;
734 ++bp->stats.rx_packets;
735 bp->stats.rx_bytes += nb;
736 } else {
737 ++bp->stats.rx_dropped;
739 dev->last_rx = jiffies;
740 if ((skb = bp->rx_bufs[i]) == NULL) {
741 bp->rx_bufs[i] = skb = dev_alloc_skb(RX_BUFLEN+2);
742 if (skb != NULL)
743 skb_reserve(bp->rx_bufs[i], 2);
745 bmac_construct_rxbuff(skb, &bp->rx_cmds[i]);
746 st_le16(&cp->res_count, 0);
747 st_le16(&cp->xfer_status, 0);
748 last = i;
749 if (++i >= N_RX_RING) i = 0;
752 if (last != -1) {
753 bp->rx_fill = last;
754 bp->rx_empty = i;
757 dbdma_continue(rd);
758 spin_unlock_irqrestore(&bp->lock, flags);
760 if (rxintcount < 10) {
761 XXDEBUG(("bmac_rxdma_intr done\n"));
763 return IRQ_HANDLED;
766 static int txintcount;
768 static irqreturn_t bmac_txdma_intr(int irq, void *dev_id)
770 struct net_device *dev = (struct net_device *) dev_id;
771 struct bmac_data *bp = netdev_priv(dev);
772 volatile struct dbdma_cmd *cp;
773 int stat;
774 unsigned long flags;
776 spin_lock_irqsave(&bp->lock, flags);
778 if (txintcount++ < 10) {
779 XXDEBUG(("bmac_txdma_intr\n"));
782 /* del_timer(&bp->tx_timeout); */
783 /* bp->timeout_active = 0; */
785 while (1) {
786 cp = &bp->tx_cmds[bp->tx_empty];
787 stat = ld_le16(&cp->xfer_status);
788 if (txintcount < 10) {
789 XXDEBUG(("bmac_txdma_xfer_stat=%#0x\n", stat));
791 if (!(stat & ACTIVE)) {
793 * status field might not have been filled by DBDMA
795 if (cp == bus_to_virt(in_le32(&bp->tx_dma->cmdptr)))
796 break;
799 if (bp->tx_bufs[bp->tx_empty]) {
800 ++bp->stats.tx_packets;
801 dev_kfree_skb_irq(bp->tx_bufs[bp->tx_empty]);
803 bp->tx_bufs[bp->tx_empty] = NULL;
804 bp->tx_fullup = 0;
805 netif_wake_queue(dev);
806 if (++bp->tx_empty >= N_TX_RING)
807 bp->tx_empty = 0;
808 if (bp->tx_empty == bp->tx_fill)
809 break;
812 spin_unlock_irqrestore(&bp->lock, flags);
814 if (txintcount < 10) {
815 XXDEBUG(("bmac_txdma_intr done->bmac_start\n"));
818 bmac_start(dev);
819 return IRQ_HANDLED;
822 static struct net_device_stats *bmac_stats(struct net_device *dev)
824 struct bmac_data *p = netdev_priv(dev);
826 return &p->stats;
829 #ifndef SUNHME_MULTICAST
830 /* Real fast bit-reversal algorithm, 6-bit values */
831 static int reverse6[64] = {
832 0x0,0x20,0x10,0x30,0x8,0x28,0x18,0x38,
833 0x4,0x24,0x14,0x34,0xc,0x2c,0x1c,0x3c,
834 0x2,0x22,0x12,0x32,0xa,0x2a,0x1a,0x3a,
835 0x6,0x26,0x16,0x36,0xe,0x2e,0x1e,0x3e,
836 0x1,0x21,0x11,0x31,0x9,0x29,0x19,0x39,
837 0x5,0x25,0x15,0x35,0xd,0x2d,0x1d,0x3d,
838 0x3,0x23,0x13,0x33,0xb,0x2b,0x1b,0x3b,
839 0x7,0x27,0x17,0x37,0xf,0x2f,0x1f,0x3f
842 static unsigned int
843 crc416(unsigned int curval, unsigned short nxtval)
845 register unsigned int counter, cur = curval, next = nxtval;
846 register int high_crc_set, low_data_set;
848 /* Swap bytes */
849 next = ((next & 0x00FF) << 8) | (next >> 8);
851 /* Compute bit-by-bit */
852 for (counter = 0; counter < 16; ++counter) {
853 /* is high CRC bit set? */
854 if ((cur & 0x80000000) == 0) high_crc_set = 0;
855 else high_crc_set = 1;
857 cur = cur << 1;
859 if ((next & 0x0001) == 0) low_data_set = 0;
860 else low_data_set = 1;
862 next = next >> 1;
864 /* do the XOR */
865 if (high_crc_set ^ low_data_set) cur = cur ^ ENET_CRCPOLY;
867 return cur;
870 static unsigned int
871 bmac_crc(unsigned short *address)
873 unsigned int newcrc;
875 XXDEBUG(("bmac_crc: addr=%#04x, %#04x, %#04x\n", *address, address[1], address[2]));
876 newcrc = crc416(0xffffffff, *address); /* address bits 47 - 32 */
877 newcrc = crc416(newcrc, address[1]); /* address bits 31 - 16 */
878 newcrc = crc416(newcrc, address[2]); /* address bits 15 - 0 */
880 return(newcrc);
884 * Add requested mcast addr to BMac's hash table filter.
888 static void
889 bmac_addhash(struct bmac_data *bp, unsigned char *addr)
891 unsigned int crc;
892 unsigned short mask;
894 if (!(*addr)) return;
895 crc = bmac_crc((unsigned short *)addr) & 0x3f; /* Big-endian alert! */
896 crc = reverse6[crc]; /* Hyperfast bit-reversing algorithm */
897 if (bp->hash_use_count[crc]++) return; /* This bit is already set */
898 mask = crc % 16;
899 mask = (unsigned char)1 << mask;
900 bp->hash_use_count[crc/16] |= mask;
903 static void
904 bmac_removehash(struct bmac_data *bp, unsigned char *addr)
906 unsigned int crc;
907 unsigned char mask;
909 /* Now, delete the address from the filter copy, as indicated */
910 crc = bmac_crc((unsigned short *)addr) & 0x3f; /* Big-endian alert! */
911 crc = reverse6[crc]; /* Hyperfast bit-reversing algorithm */
912 if (bp->hash_use_count[crc] == 0) return; /* That bit wasn't in use! */
913 if (--bp->hash_use_count[crc]) return; /* That bit is still in use */
914 mask = crc % 16;
915 mask = ((unsigned char)1 << mask) ^ 0xffff; /* To turn off bit */
916 bp->hash_table_mask[crc/16] &= mask;
920 * Sync the adapter with the software copy of the multicast mask
921 * (logical address filter).
924 static void
925 bmac_rx_off(struct net_device *dev)
927 unsigned short rx_cfg;
929 rx_cfg = bmread(dev, RXCFG);
930 rx_cfg &= ~RxMACEnable;
931 bmwrite(dev, RXCFG, rx_cfg);
932 do {
933 rx_cfg = bmread(dev, RXCFG);
934 } while (rx_cfg & RxMACEnable);
937 unsigned short
938 bmac_rx_on(struct net_device *dev, int hash_enable, int promisc_enable)
940 unsigned short rx_cfg;
942 rx_cfg = bmread(dev, RXCFG);
943 rx_cfg |= RxMACEnable;
944 if (hash_enable) rx_cfg |= RxHashFilterEnable;
945 else rx_cfg &= ~RxHashFilterEnable;
946 if (promisc_enable) rx_cfg |= RxPromiscEnable;
947 else rx_cfg &= ~RxPromiscEnable;
948 bmwrite(dev, RXRST, RxResetValue);
949 bmwrite(dev, RXFIFOCSR, 0); /* first disable rxFIFO */
950 bmwrite(dev, RXFIFOCSR, RxFIFOEnable );
951 bmwrite(dev, RXCFG, rx_cfg );
952 return rx_cfg;
955 static void
956 bmac_update_hash_table_mask(struct net_device *dev, struct bmac_data *bp)
958 bmwrite(dev, BHASH3, bp->hash_table_mask[0]); /* bits 15 - 0 */
959 bmwrite(dev, BHASH2, bp->hash_table_mask[1]); /* bits 31 - 16 */
960 bmwrite(dev, BHASH1, bp->hash_table_mask[2]); /* bits 47 - 32 */
961 bmwrite(dev, BHASH0, bp->hash_table_mask[3]); /* bits 63 - 48 */
964 #if 0
965 static void
966 bmac_add_multi(struct net_device *dev,
967 struct bmac_data *bp, unsigned char *addr)
969 /* XXDEBUG(("bmac: enter bmac_add_multi\n")); */
970 bmac_addhash(bp, addr);
971 bmac_rx_off(dev);
972 bmac_update_hash_table_mask(dev, bp);
973 bmac_rx_on(dev, 1, (dev->flags & IFF_PROMISC)? 1 : 0);
974 /* XXDEBUG(("bmac: exit bmac_add_multi\n")); */
977 static void
978 bmac_remove_multi(struct net_device *dev,
979 struct bmac_data *bp, unsigned char *addr)
981 bmac_removehash(bp, addr);
982 bmac_rx_off(dev);
983 bmac_update_hash_table_mask(dev, bp);
984 bmac_rx_on(dev, 1, (dev->flags & IFF_PROMISC)? 1 : 0);
986 #endif
988 /* Set or clear the multicast filter for this adaptor.
989 num_addrs == -1 Promiscuous mode, receive all packets
990 num_addrs == 0 Normal mode, clear multicast list
991 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
992 best-effort filtering.
994 static void bmac_set_multicast(struct net_device *dev)
996 struct dev_mc_list *dmi;
997 struct bmac_data *bp = netdev_priv(dev);
998 int num_addrs = dev->mc_count;
999 unsigned short rx_cfg;
1000 int i;
1002 if (bp->sleeping)
1003 return;
1005 XXDEBUG(("bmac: enter bmac_set_multicast, n_addrs=%d\n", num_addrs));
1007 if((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
1008 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0xffff;
1009 bmac_update_hash_table_mask(dev, bp);
1010 rx_cfg = bmac_rx_on(dev, 1, 0);
1011 XXDEBUG(("bmac: all multi, rx_cfg=%#08x\n"));
1012 } else if ((dev->flags & IFF_PROMISC) || (num_addrs < 0)) {
1013 rx_cfg = bmread(dev, RXCFG);
1014 rx_cfg |= RxPromiscEnable;
1015 bmwrite(dev, RXCFG, rx_cfg);
1016 rx_cfg = bmac_rx_on(dev, 0, 1);
1017 XXDEBUG(("bmac: promisc mode enabled, rx_cfg=%#08x\n", rx_cfg));
1018 } else {
1019 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0;
1020 for (i=0; i<64; i++) bp->hash_use_count[i] = 0;
1021 if (num_addrs == 0) {
1022 rx_cfg = bmac_rx_on(dev, 0, 0);
1023 XXDEBUG(("bmac: multi disabled, rx_cfg=%#08x\n", rx_cfg));
1024 } else {
1025 for (dmi=dev->mc_list; dmi!=NULL; dmi=dmi->next)
1026 bmac_addhash(bp, dmi->dmi_addr);
1027 bmac_update_hash_table_mask(dev, bp);
1028 rx_cfg = bmac_rx_on(dev, 1, 0);
1029 XXDEBUG(("bmac: multi enabled, rx_cfg=%#08x\n", rx_cfg));
1032 /* XXDEBUG(("bmac: exit bmac_set_multicast\n")); */
1034 #else /* ifdef SUNHME_MULTICAST */
1036 /* The version of set_multicast below was lifted from sunhme.c */
1038 static void bmac_set_multicast(struct net_device *dev)
1040 struct dev_mc_list *dmi = dev->mc_list;
1041 char *addrs;
1042 int i;
1043 unsigned short rx_cfg;
1044 u32 crc;
1046 if((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
1047 bmwrite(dev, BHASH0, 0xffff);
1048 bmwrite(dev, BHASH1, 0xffff);
1049 bmwrite(dev, BHASH2, 0xffff);
1050 bmwrite(dev, BHASH3, 0xffff);
1051 } else if(dev->flags & IFF_PROMISC) {
1052 rx_cfg = bmread(dev, RXCFG);
1053 rx_cfg |= RxPromiscEnable;
1054 bmwrite(dev, RXCFG, rx_cfg);
1055 } else {
1056 u16 hash_table[4];
1058 rx_cfg = bmread(dev, RXCFG);
1059 rx_cfg &= ~RxPromiscEnable;
1060 bmwrite(dev, RXCFG, rx_cfg);
1062 for(i = 0; i < 4; i++) hash_table[i] = 0;
1064 for(i = 0; i < dev->mc_count; i++) {
1065 addrs = dmi->dmi_addr;
1066 dmi = dmi->next;
1068 if(!(*addrs & 1))
1069 continue;
1071 crc = ether_crc_le(6, addrs);
1072 crc >>= 26;
1073 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1075 bmwrite(dev, BHASH0, hash_table[0]);
1076 bmwrite(dev, BHASH1, hash_table[1]);
1077 bmwrite(dev, BHASH2, hash_table[2]);
1078 bmwrite(dev, BHASH3, hash_table[3]);
1081 #endif /* SUNHME_MULTICAST */
1083 static int miscintcount;
1085 static irqreturn_t bmac_misc_intr(int irq, void *dev_id)
1087 struct net_device *dev = (struct net_device *) dev_id;
1088 struct bmac_data *bp = netdev_priv(dev);
1089 unsigned int status = bmread(dev, STATUS);
1090 if (miscintcount++ < 10) {
1091 XXDEBUG(("bmac_misc_intr\n"));
1093 /* XXDEBUG(("bmac_misc_intr, status=%#08x\n", status)); */
1094 /* bmac_txdma_intr_inner(irq, dev_id); */
1095 /* if (status & FrameReceived) bp->stats.rx_dropped++; */
1096 if (status & RxErrorMask) bp->stats.rx_errors++;
1097 if (status & RxCRCCntExp) bp->stats.rx_crc_errors++;
1098 if (status & RxLenCntExp) bp->stats.rx_length_errors++;
1099 if (status & RxOverFlow) bp->stats.rx_over_errors++;
1100 if (status & RxAlignCntExp) bp->stats.rx_frame_errors++;
1102 /* if (status & FrameSent) bp->stats.tx_dropped++; */
1103 if (status & TxErrorMask) bp->stats.tx_errors++;
1104 if (status & TxUnderrun) bp->stats.tx_fifo_errors++;
1105 if (status & TxNormalCollExp) bp->stats.collisions++;
1106 return IRQ_HANDLED;
1110 * Procedure for reading EEPROM
1112 #define SROMAddressLength 5
1113 #define DataInOn 0x0008
1114 #define DataInOff 0x0000
1115 #define Clk 0x0002
1116 #define ChipSelect 0x0001
1117 #define SDIShiftCount 3
1118 #define SD0ShiftCount 2
1119 #define DelayValue 1000 /* number of microseconds */
1120 #define SROMStartOffset 10 /* this is in words */
1121 #define SROMReadCount 3 /* number of words to read from SROM */
1122 #define SROMAddressBits 6
1123 #define EnetAddressOffset 20
1125 static unsigned char
1126 bmac_clock_out_bit(struct net_device *dev)
1128 unsigned short data;
1129 unsigned short val;
1131 bmwrite(dev, SROMCSR, ChipSelect | Clk);
1132 udelay(DelayValue);
1134 data = bmread(dev, SROMCSR);
1135 udelay(DelayValue);
1136 val = (data >> SD0ShiftCount) & 1;
1138 bmwrite(dev, SROMCSR, ChipSelect);
1139 udelay(DelayValue);
1141 return val;
1144 static void
1145 bmac_clock_in_bit(struct net_device *dev, unsigned int val)
1147 unsigned short data;
1149 if (val != 0 && val != 1) return;
1151 data = (val << SDIShiftCount);
1152 bmwrite(dev, SROMCSR, data | ChipSelect );
1153 udelay(DelayValue);
1155 bmwrite(dev, SROMCSR, data | ChipSelect | Clk );
1156 udelay(DelayValue);
1158 bmwrite(dev, SROMCSR, data | ChipSelect);
1159 udelay(DelayValue);
1162 static void
1163 reset_and_select_srom(struct net_device *dev)
1165 /* first reset */
1166 bmwrite(dev, SROMCSR, 0);
1167 udelay(DelayValue);
1169 /* send it the read command (110) */
1170 bmac_clock_in_bit(dev, 1);
1171 bmac_clock_in_bit(dev, 1);
1172 bmac_clock_in_bit(dev, 0);
1175 static unsigned short
1176 read_srom(struct net_device *dev, unsigned int addr, unsigned int addr_len)
1178 unsigned short data, val;
1179 int i;
1181 /* send out the address we want to read from */
1182 for (i = 0; i < addr_len; i++) {
1183 val = addr >> (addr_len-i-1);
1184 bmac_clock_in_bit(dev, val & 1);
1187 /* Now read in the 16-bit data */
1188 data = 0;
1189 for (i = 0; i < 16; i++) {
1190 val = bmac_clock_out_bit(dev);
1191 data <<= 1;
1192 data |= val;
1194 bmwrite(dev, SROMCSR, 0);
1196 return data;
1200 * It looks like Cogent and SMC use different methods for calculating
1201 * checksums. What a pain..
1204 static int
1205 bmac_verify_checksum(struct net_device *dev)
1207 unsigned short data, storedCS;
1209 reset_and_select_srom(dev);
1210 data = read_srom(dev, 3, SROMAddressBits);
1211 storedCS = ((data >> 8) & 0x0ff) | ((data << 8) & 0xff00);
1213 return 0;
1217 static void
1218 bmac_get_station_address(struct net_device *dev, unsigned char *ea)
1220 int i;
1221 unsigned short data;
1223 for (i = 0; i < 6; i++)
1225 reset_and_select_srom(dev);
1226 data = read_srom(dev, i + EnetAddressOffset/2, SROMAddressBits);
1227 ea[2*i] = bitrev(data & 0x0ff);
1228 ea[2*i+1] = bitrev((data >> 8) & 0x0ff);
1232 static void bmac_reset_and_enable(struct net_device *dev)
1234 struct bmac_data *bp = netdev_priv(dev);
1235 unsigned long flags;
1236 struct sk_buff *skb;
1237 unsigned char *data;
1239 spin_lock_irqsave(&bp->lock, flags);
1240 bmac_enable_and_reset_chip(dev);
1241 bmac_init_tx_ring(bp);
1242 bmac_init_rx_ring(bp);
1243 bmac_init_chip(dev);
1244 bmac_start_chip(dev);
1245 bmwrite(dev, INTDISABLE, EnableNormal);
1246 bp->sleeping = 0;
1249 * It seems that the bmac can't receive until it's transmitted
1250 * a packet. So we give it a dummy packet to transmit.
1252 skb = dev_alloc_skb(ETHERMINPACKET);
1253 if (skb != NULL) {
1254 data = skb_put(skb, ETHERMINPACKET);
1255 memset(data, 0, ETHERMINPACKET);
1256 memcpy(data, dev->dev_addr, 6);
1257 memcpy(data+6, dev->dev_addr, 6);
1258 bmac_transmit_packet(skb, dev);
1260 spin_unlock_irqrestore(&bp->lock, flags);
1263 static int __devinit bmac_probe(struct macio_dev *mdev, const struct of_device_id *match)
1265 int j, rev, ret;
1266 struct bmac_data *bp;
1267 const unsigned char *prop_addr;
1268 unsigned char addr[6];
1269 struct net_device *dev;
1270 int is_bmac_plus = ((int)match->data) != 0;
1272 if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
1273 printk(KERN_ERR "BMAC: can't use, need 3 addrs and 3 intrs\n");
1274 return -ENODEV;
1276 prop_addr = get_property(macio_get_of_node(mdev), "mac-address", NULL);
1277 if (prop_addr == NULL) {
1278 prop_addr = get_property(macio_get_of_node(mdev),
1279 "local-mac-address", NULL);
1280 if (prop_addr == NULL) {
1281 printk(KERN_ERR "BMAC: Can't get mac-address\n");
1282 return -ENODEV;
1285 memcpy(addr, prop_addr, sizeof(addr));
1287 dev = alloc_etherdev(PRIV_BYTES);
1288 if (!dev) {
1289 printk(KERN_ERR "BMAC: alloc_etherdev failed, out of memory\n");
1290 return -ENOMEM;
1293 bp = netdev_priv(dev);
1294 SET_MODULE_OWNER(dev);
1295 SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
1296 macio_set_drvdata(mdev, dev);
1298 bp->mdev = mdev;
1299 spin_lock_init(&bp->lock);
1301 if (macio_request_resources(mdev, "bmac")) {
1302 printk(KERN_ERR "BMAC: can't request IO resource !\n");
1303 goto out_free;
1306 dev->base_addr = (unsigned long)
1307 ioremap(macio_resource_start(mdev, 0), macio_resource_len(mdev, 0));
1308 if (dev->base_addr == 0)
1309 goto out_release;
1311 dev->irq = macio_irq(mdev, 0);
1313 bmac_enable_and_reset_chip(dev);
1314 bmwrite(dev, INTDISABLE, DisableAll);
1316 rev = addr[0] == 0 && addr[1] == 0xA0;
1317 for (j = 0; j < 6; ++j)
1318 dev->dev_addr[j] = rev? bitrev(addr[j]): addr[j];
1320 /* Enable chip without interrupts for now */
1321 bmac_enable_and_reset_chip(dev);
1322 bmwrite(dev, INTDISABLE, DisableAll);
1324 dev->open = bmac_open;
1325 dev->stop = bmac_close;
1326 dev->hard_start_xmit = bmac_output;
1327 dev->get_stats = bmac_stats;
1328 dev->set_multicast_list = bmac_set_multicast;
1329 dev->set_mac_address = bmac_set_address;
1331 bmac_get_station_address(dev, addr);
1332 if (bmac_verify_checksum(dev) != 0)
1333 goto err_out_iounmap;
1335 bp->is_bmac_plus = is_bmac_plus;
1336 bp->tx_dma = ioremap(macio_resource_start(mdev, 1), macio_resource_len(mdev, 1));
1337 if (!bp->tx_dma)
1338 goto err_out_iounmap;
1339 bp->tx_dma_intr = macio_irq(mdev, 1);
1340 bp->rx_dma = ioremap(macio_resource_start(mdev, 2), macio_resource_len(mdev, 2));
1341 if (!bp->rx_dma)
1342 goto err_out_iounmap_tx;
1343 bp->rx_dma_intr = macio_irq(mdev, 2);
1345 bp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(bp + 1);
1346 bp->rx_cmds = bp->tx_cmds + N_TX_RING + 1;
1348 bp->queue = (struct sk_buff_head *)(bp->rx_cmds + N_RX_RING + 1);
1349 skb_queue_head_init(bp->queue);
1351 init_timer(&bp->tx_timeout);
1353 ret = request_irq(dev->irq, bmac_misc_intr, 0, "BMAC-misc", dev);
1354 if (ret) {
1355 printk(KERN_ERR "BMAC: can't get irq %d\n", dev->irq);
1356 goto err_out_iounmap_rx;
1358 ret = request_irq(bp->tx_dma_intr, bmac_txdma_intr, 0, "BMAC-txdma", dev);
1359 if (ret) {
1360 printk(KERN_ERR "BMAC: can't get irq %d\n", bp->tx_dma_intr);
1361 goto err_out_irq0;
1363 ret = request_irq(bp->rx_dma_intr, bmac_rxdma_intr, 0, "BMAC-rxdma", dev);
1364 if (ret) {
1365 printk(KERN_ERR "BMAC: can't get irq %d\n", bp->rx_dma_intr);
1366 goto err_out_irq1;
1369 /* Mask chip interrupts and disable chip, will be
1370 * re-enabled on open()
1372 disable_irq(dev->irq);
1373 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1375 if (register_netdev(dev) != 0) {
1376 printk(KERN_ERR "BMAC: Ethernet registration failed\n");
1377 goto err_out_irq2;
1380 printk(KERN_INFO "%s: BMAC%s at", dev->name, (is_bmac_plus? "+": ""));
1381 for (j = 0; j < 6; ++j)
1382 printk("%c%.2x", (j? ':': ' '), dev->dev_addr[j]);
1383 XXDEBUG((", base_addr=%#0lx", dev->base_addr));
1384 printk("\n");
1386 return 0;
1388 err_out_irq2:
1389 free_irq(bp->rx_dma_intr, dev);
1390 err_out_irq1:
1391 free_irq(bp->tx_dma_intr, dev);
1392 err_out_irq0:
1393 free_irq(dev->irq, dev);
1394 err_out_iounmap_rx:
1395 iounmap(bp->rx_dma);
1396 err_out_iounmap_tx:
1397 iounmap(bp->tx_dma);
1398 err_out_iounmap:
1399 iounmap((void __iomem *)dev->base_addr);
1400 out_release:
1401 macio_release_resources(mdev);
1402 out_free:
1403 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1404 free_netdev(dev);
1406 return -ENODEV;
1409 static int bmac_open(struct net_device *dev)
1411 struct bmac_data *bp = netdev_priv(dev);
1412 /* XXDEBUG(("bmac: enter open\n")); */
1413 /* reset the chip */
1414 bp->opened = 1;
1415 bmac_reset_and_enable(dev);
1416 enable_irq(dev->irq);
1417 return 0;
1420 static int bmac_close(struct net_device *dev)
1422 struct bmac_data *bp = netdev_priv(dev);
1423 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
1424 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
1425 unsigned short config;
1426 int i;
1428 bp->sleeping = 1;
1430 /* disable rx and tx */
1431 config = bmread(dev, RXCFG);
1432 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
1434 config = bmread(dev, TXCFG);
1435 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
1437 bmwrite(dev, INTDISABLE, DisableAll); /* disable all intrs */
1439 /* disable rx and tx dma */
1440 st_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
1441 st_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
1443 /* free some skb's */
1444 XXDEBUG(("bmac: free rx bufs\n"));
1445 for (i=0; i<N_RX_RING; i++) {
1446 if (bp->rx_bufs[i] != NULL) {
1447 dev_kfree_skb(bp->rx_bufs[i]);
1448 bp->rx_bufs[i] = NULL;
1451 XXDEBUG(("bmac: free tx bufs\n"));
1452 for (i = 0; i<N_TX_RING; i++) {
1453 if (bp->tx_bufs[i] != NULL) {
1454 dev_kfree_skb(bp->tx_bufs[i]);
1455 bp->tx_bufs[i] = NULL;
1458 XXDEBUG(("bmac: all bufs freed\n"));
1460 bp->opened = 0;
1461 disable_irq(dev->irq);
1462 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1464 return 0;
1467 static void
1468 bmac_start(struct net_device *dev)
1470 struct bmac_data *bp = netdev_priv(dev);
1471 int i;
1472 struct sk_buff *skb;
1473 unsigned long flags;
1475 if (bp->sleeping)
1476 return;
1478 spin_lock_irqsave(&bp->lock, flags);
1479 while (1) {
1480 i = bp->tx_fill + 1;
1481 if (i >= N_TX_RING)
1482 i = 0;
1483 if (i == bp->tx_empty)
1484 break;
1485 skb = skb_dequeue(bp->queue);
1486 if (skb == NULL)
1487 break;
1488 bmac_transmit_packet(skb, dev);
1490 spin_unlock_irqrestore(&bp->lock, flags);
1493 static int
1494 bmac_output(struct sk_buff *skb, struct net_device *dev)
1496 struct bmac_data *bp = netdev_priv(dev);
1497 skb_queue_tail(bp->queue, skb);
1498 bmac_start(dev);
1499 return 0;
1502 static void bmac_tx_timeout(unsigned long data)
1504 struct net_device *dev = (struct net_device *) data;
1505 struct bmac_data *bp = netdev_priv(dev);
1506 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
1507 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
1508 volatile struct dbdma_cmd *cp;
1509 unsigned long flags;
1510 unsigned short config, oldConfig;
1511 int i;
1513 XXDEBUG(("bmac: tx_timeout called\n"));
1514 spin_lock_irqsave(&bp->lock, flags);
1515 bp->timeout_active = 0;
1517 /* update various counters */
1518 /* bmac_handle_misc_intrs(bp, 0); */
1520 cp = &bp->tx_cmds[bp->tx_empty];
1521 /* XXDEBUG((KERN_DEBUG "bmac: tx dmastat=%x %x runt=%d pr=%x fs=%x fc=%x\n", */
1522 /* ld_le32(&td->status), ld_le16(&cp->xfer_status), bp->tx_bad_runt, */
1523 /* mb->pr, mb->xmtfs, mb->fifofc)); */
1525 /* turn off both tx and rx and reset the chip */
1526 config = bmread(dev, RXCFG);
1527 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
1528 config = bmread(dev, TXCFG);
1529 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
1530 out_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE|ACTIVE|DEAD));
1531 printk(KERN_ERR "bmac: transmit timeout - resetting\n");
1532 bmac_enable_and_reset_chip(dev);
1534 /* restart rx dma */
1535 cp = bus_to_virt(ld_le32(&rd->cmdptr));
1536 out_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE|ACTIVE|DEAD));
1537 out_le16(&cp->xfer_status, 0);
1538 out_le32(&rd->cmdptr, virt_to_bus(cp));
1539 out_le32(&rd->control, DBDMA_SET(RUN|WAKE));
1541 /* fix up the transmit side */
1542 XXDEBUG((KERN_DEBUG "bmac: tx empty=%d fill=%d fullup=%d\n",
1543 bp->tx_empty, bp->tx_fill, bp->tx_fullup));
1544 i = bp->tx_empty;
1545 ++bp->stats.tx_errors;
1546 if (i != bp->tx_fill) {
1547 dev_kfree_skb(bp->tx_bufs[i]);
1548 bp->tx_bufs[i] = NULL;
1549 if (++i >= N_TX_RING) i = 0;
1550 bp->tx_empty = i;
1552 bp->tx_fullup = 0;
1553 netif_wake_queue(dev);
1554 if (i != bp->tx_fill) {
1555 cp = &bp->tx_cmds[i];
1556 out_le16(&cp->xfer_status, 0);
1557 out_le16(&cp->command, OUTPUT_LAST);
1558 out_le32(&td->cmdptr, virt_to_bus(cp));
1559 out_le32(&td->control, DBDMA_SET(RUN));
1560 /* bmac_set_timeout(dev); */
1561 XXDEBUG((KERN_DEBUG "bmac: starting %d\n", i));
1564 /* turn it back on */
1565 oldConfig = bmread(dev, RXCFG);
1566 bmwrite(dev, RXCFG, oldConfig | RxMACEnable );
1567 oldConfig = bmread(dev, TXCFG);
1568 bmwrite(dev, TXCFG, oldConfig | TxMACEnable );
1570 spin_unlock_irqrestore(&bp->lock, flags);
1573 #if 0
1574 static void dump_dbdma(volatile struct dbdma_cmd *cp,int count)
1576 int i,*ip;
1578 for (i=0;i< count;i++) {
1579 ip = (int*)(cp+i);
1581 printk("dbdma req 0x%x addr 0x%x baddr 0x%x xfer/res 0x%x\n",
1582 ld_le32(ip+0),
1583 ld_le32(ip+1),
1584 ld_le32(ip+2),
1585 ld_le32(ip+3));
1589 #endif
1591 #if 0
1592 static int
1593 bmac_proc_info(char *buffer, char **start, off_t offset, int length)
1595 int len = 0;
1596 off_t pos = 0;
1597 off_t begin = 0;
1598 int i;
1600 if (bmac_devs == NULL)
1601 return (-ENOSYS);
1603 len += sprintf(buffer, "BMAC counters & registers\n");
1605 for (i = 0; i<N_REG_ENTRIES; i++) {
1606 len += sprintf(buffer + len, "%s: %#08x\n",
1607 reg_entries[i].name,
1608 bmread(bmac_devs, reg_entries[i].reg_offset));
1609 pos = begin + len;
1611 if (pos < offset) {
1612 len = 0;
1613 begin = pos;
1616 if (pos > offset+length) break;
1619 *start = buffer + (offset - begin);
1620 len -= (offset - begin);
1622 if (len > length) len = length;
1624 return len;
1626 #endif
1628 static int __devexit bmac_remove(struct macio_dev *mdev)
1630 struct net_device *dev = macio_get_drvdata(mdev);
1631 struct bmac_data *bp = netdev_priv(dev);
1633 unregister_netdev(dev);
1635 free_irq(dev->irq, dev);
1636 free_irq(bp->tx_dma_intr, dev);
1637 free_irq(bp->rx_dma_intr, dev);
1639 iounmap((void __iomem *)dev->base_addr);
1640 iounmap(bp->tx_dma);
1641 iounmap(bp->rx_dma);
1643 macio_release_resources(mdev);
1645 free_netdev(dev);
1647 return 0;
1650 static struct of_device_id bmac_match[] =
1653 .name = "bmac",
1654 .data = (void *)0,
1657 .type = "network",
1658 .compatible = "bmac+",
1659 .data = (void *)1,
1663 MODULE_DEVICE_TABLE (of, bmac_match);
1665 static struct macio_driver bmac_driver =
1667 .name = "bmac",
1668 .match_table = bmac_match,
1669 .probe = bmac_probe,
1670 .remove = bmac_remove,
1671 #ifdef CONFIG_PM
1672 .suspend = bmac_suspend,
1673 .resume = bmac_resume,
1674 #endif
1678 static int __init bmac_init(void)
1680 if (bmac_emergency_rxbuf == NULL) {
1681 bmac_emergency_rxbuf = kmalloc(RX_BUFLEN, GFP_KERNEL);
1682 if (bmac_emergency_rxbuf == NULL) {
1683 printk(KERN_ERR "BMAC: can't allocate emergency RX buffer\n");
1684 return -ENOMEM;
1688 return macio_register_driver(&bmac_driver);
1691 static void __exit bmac_exit(void)
1693 macio_unregister_driver(&bmac_driver);
1695 kfree(bmac_emergency_rxbuf);
1696 bmac_emergency_rxbuf = NULL;
1699 MODULE_AUTHOR("Randy Gobbel/Paul Mackerras");
1700 MODULE_DESCRIPTION("PowerMac BMAC ethernet driver.");
1701 MODULE_LICENSE("GPL");
1703 module_init(bmac_init);
1704 module_exit(bmac_exit);