blob: 1eb95b6030043589baef2d097cb680756a2bee9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Network device driver for the BMAC ethernet controller on
3 * Apple Powermacs. Assumes it's under a DBDMA controller.
4 *
5 * Copyright (C) 1998 Randy Gobbel.
6 *
7 * May 1999, Al Viro: proper release of /proc/net/bmac entry, switched to
8 * dynamic procfs inode.
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Akinobu Mitabc63eb92006-12-19 13:09:08 -080021#include <linux/bitrev.h>
Olaf Heringced13332007-08-25 20:32:59 +020022#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/prom.h>
24#include <asm/dbdma.h>
25#include <asm/io.h>
26#include <asm/page.h>
27#include <asm/pgtable.h>
28#include <asm/machdep.h>
29#include <asm/pmac_feature.h>
30#include <asm/macio.h>
31#include <asm/irq.h>
32
33#include "bmac.h"
34
35#define trunc_page(x) ((void *)(((unsigned long)(x)) & ~((unsigned long)(PAGE_SIZE - 1))))
36#define round_page(x) trunc_page(((unsigned long)(x)) + ((unsigned long)(PAGE_SIZE - 1)))
37
38/*
39 * CRC polynomial - used in working out multicast filter bits.
40 */
41#define ENET_CRCPOLY 0x04c11db7
42
43/* switch to use multicast code lifted from sunhme driver */
44#define SUNHME_MULTICAST
45
46#define N_RX_RING 64
47#define N_TX_RING 32
48#define MAX_TX_ACTIVE 1
49#define ETHERCRC 4
50#define ETHERMINPACKET 64
51#define ETHERMTU 1500
52#define RX_BUFLEN (ETHERMTU + 14 + ETHERCRC + 2)
53#define TX_TIMEOUT HZ /* 1 second */
54
55/* Bits in transmit DMA status */
56#define TX_DMA_ERR 0x80
57
58#define XXDEBUG(args)
59
60struct bmac_data {
61 /* volatile struct bmac *bmac; */
62 struct sk_buff_head *queue;
63 volatile struct dbdma_regs __iomem *tx_dma;
64 int tx_dma_intr;
65 volatile struct dbdma_regs __iomem *rx_dma;
66 int rx_dma_intr;
67 volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
68 volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
69 struct macio_dev *mdev;
70 int is_bmac_plus;
71 struct sk_buff *rx_bufs[N_RX_RING];
72 int rx_fill;
73 int rx_empty;
74 struct sk_buff *tx_bufs[N_TX_RING];
75 int tx_fill;
76 int tx_empty;
77 unsigned char tx_fullup;
78 struct net_device_stats stats;
79 struct timer_list tx_timeout;
80 int timeout_active;
81 int sleeping;
82 int opened;
83 unsigned short hash_use_count[64];
84 unsigned short hash_table_mask[4];
85 spinlock_t lock;
86};
87
88#if 0 /* Move that to ethtool */
89
90typedef struct bmac_reg_entry {
91 char *name;
92 unsigned short reg_offset;
93} bmac_reg_entry_t;
94
95#define N_REG_ENTRIES 31
96
97static bmac_reg_entry_t reg_entries[N_REG_ENTRIES] = {
98 {"MEMADD", MEMADD},
99 {"MEMDATAHI", MEMDATAHI},
100 {"MEMDATALO", MEMDATALO},
101 {"TXPNTR", TXPNTR},
102 {"RXPNTR", RXPNTR},
103 {"IPG1", IPG1},
104 {"IPG2", IPG2},
105 {"ALIMIT", ALIMIT},
106 {"SLOT", SLOT},
107 {"PALEN", PALEN},
108 {"PAPAT", PAPAT},
109 {"TXSFD", TXSFD},
110 {"JAM", JAM},
111 {"TXCFG", TXCFG},
112 {"TXMAX", TXMAX},
113 {"TXMIN", TXMIN},
114 {"PAREG", PAREG},
115 {"DCNT", DCNT},
116 {"NCCNT", NCCNT},
117 {"NTCNT", NTCNT},
118 {"EXCNT", EXCNT},
119 {"LTCNT", LTCNT},
120 {"TXSM", TXSM},
121 {"RXCFG", RXCFG},
122 {"RXMAX", RXMAX},
123 {"RXMIN", RXMIN},
124 {"FRCNT", FRCNT},
125 {"AECNT", AECNT},
126 {"FECNT", FECNT},
127 {"RXSM", RXSM},
128 {"RXCV", RXCV}
129};
130
131#endif
132
133static unsigned char *bmac_emergency_rxbuf;
134
135/*
136 * Number of bytes of private data per BMAC: allow enough for
137 * the rx and tx dma commands plus a branch dma command each,
138 * and another 16 bytes to allow us to align the dma command
139 * buffers on a 16 byte boundary.
140 */
141#define PRIV_BYTES (sizeof(struct bmac_data) \
142 + (N_RX_RING + N_TX_RING + 4) * sizeof(struct dbdma_cmd) \
143 + sizeof(struct sk_buff_head))
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static int bmac_open(struct net_device *dev);
146static int bmac_close(struct net_device *dev);
147static int bmac_transmit_packet(struct sk_buff *skb, struct net_device *dev);
148static struct net_device_stats *bmac_stats(struct net_device *dev);
149static void bmac_set_multicast(struct net_device *dev);
150static void bmac_reset_and_enable(struct net_device *dev);
151static void bmac_start_chip(struct net_device *dev);
152static void bmac_init_chip(struct net_device *dev);
153static void bmac_init_registers(struct net_device *dev);
154static void bmac_enable_and_reset_chip(struct net_device *dev);
155static int bmac_set_address(struct net_device *dev, void *addr);
David Howells7d12e782006-10-05 14:55:46 +0100156static irqreturn_t bmac_misc_intr(int irq, void *dev_id);
157static irqreturn_t bmac_txdma_intr(int irq, void *dev_id);
158static irqreturn_t bmac_rxdma_intr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void bmac_set_timeout(struct net_device *dev);
160static void bmac_tx_timeout(unsigned long data);
161static int bmac_output(struct sk_buff *skb, struct net_device *dev);
162static void bmac_start(struct net_device *dev);
163
164#define DBDMA_SET(x) ( ((x) | (x) << 16) )
165#define DBDMA_CLEAR(x) ( (x) << 16)
166
167static inline void
168dbdma_st32(volatile __u32 __iomem *a, unsigned long x)
169{
170 __asm__ volatile( "stwbrx %0,0,%1" : : "r" (x), "r" (a) : "memory");
171 return;
172}
173
174static inline unsigned long
175dbdma_ld32(volatile __u32 __iomem *a)
176{
177 __u32 swap;
178 __asm__ volatile ("lwbrx %0,0,%1" : "=r" (swap) : "r" (a));
179 return swap;
180}
181
182static void
183dbdma_continue(volatile struct dbdma_regs __iomem *dmap)
184{
185 dbdma_st32(&dmap->control,
186 DBDMA_SET(RUN|WAKE) | DBDMA_CLEAR(PAUSE|DEAD));
187 eieio();
188}
189
190static void
191dbdma_reset(volatile struct dbdma_regs __iomem *dmap)
192{
193 dbdma_st32(&dmap->control,
194 DBDMA_CLEAR(ACTIVE|DEAD|WAKE|FLUSH|PAUSE|RUN));
195 eieio();
196 while (dbdma_ld32(&dmap->status) & RUN)
197 eieio();
198}
199
200static void
201dbdma_setcmd(volatile struct dbdma_cmd *cp,
202 unsigned short cmd, unsigned count, unsigned long addr,
203 unsigned long cmd_dep)
204{
205 out_le16(&cp->command, cmd);
206 out_le16(&cp->req_count, count);
207 out_le32(&cp->phy_addr, addr);
208 out_le32(&cp->cmd_dep, cmd_dep);
209 out_le16(&cp->xfer_status, 0);
210 out_le16(&cp->res_count, 0);
211}
212
213static inline
214void bmwrite(struct net_device *dev, unsigned long reg_offset, unsigned data )
215{
216 out_le16((void __iomem *)dev->base_addr + reg_offset, data);
217}
218
219
220static inline
Al Viro66df3bb2005-09-30 04:19:43 +0100221unsigned short bmread(struct net_device *dev, unsigned long reg_offset )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 return in_le16((void __iomem *)dev->base_addr + reg_offset);
224}
225
226static void
227bmac_enable_and_reset_chip(struct net_device *dev)
228{
229 struct bmac_data *bp = netdev_priv(dev);
230 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
231 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
232
233 if (rd)
234 dbdma_reset(rd);
235 if (td)
236 dbdma_reset(td);
237
238 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 1);
239}
240
241#define MIFDELAY udelay(10)
242
243static unsigned int
244bmac_mif_readbits(struct net_device *dev, int nb)
245{
246 unsigned int val = 0;
247
248 while (--nb >= 0) {
249 bmwrite(dev, MIFCSR, 0);
250 MIFDELAY;
251 if (bmread(dev, MIFCSR) & 8)
252 val |= 1 << nb;
253 bmwrite(dev, MIFCSR, 1);
254 MIFDELAY;
255 }
256 bmwrite(dev, MIFCSR, 0);
257 MIFDELAY;
258 bmwrite(dev, MIFCSR, 1);
259 MIFDELAY;
260 return val;
261}
262
263static void
264bmac_mif_writebits(struct net_device *dev, unsigned int val, int nb)
265{
266 int b;
267
268 while (--nb >= 0) {
269 b = (val & (1 << nb))? 6: 4;
270 bmwrite(dev, MIFCSR, b);
271 MIFDELAY;
272 bmwrite(dev, MIFCSR, b|1);
273 MIFDELAY;
274 }
275}
276
277static unsigned int
278bmac_mif_read(struct net_device *dev, unsigned int addr)
279{
280 unsigned int val;
281
282 bmwrite(dev, MIFCSR, 4);
283 MIFDELAY;
284 bmac_mif_writebits(dev, ~0U, 32);
285 bmac_mif_writebits(dev, 6, 4);
286 bmac_mif_writebits(dev, addr, 10);
287 bmwrite(dev, MIFCSR, 2);
288 MIFDELAY;
289 bmwrite(dev, MIFCSR, 1);
290 MIFDELAY;
291 val = bmac_mif_readbits(dev, 17);
292 bmwrite(dev, MIFCSR, 4);
293 MIFDELAY;
294 return val;
295}
296
297static void
298bmac_mif_write(struct net_device *dev, unsigned int addr, unsigned int val)
299{
300 bmwrite(dev, MIFCSR, 4);
301 MIFDELAY;
302 bmac_mif_writebits(dev, ~0U, 32);
303 bmac_mif_writebits(dev, 5, 4);
304 bmac_mif_writebits(dev, addr, 10);
305 bmac_mif_writebits(dev, 2, 2);
306 bmac_mif_writebits(dev, val, 16);
307 bmac_mif_writebits(dev, 3, 2);
308}
309
310static void
311bmac_init_registers(struct net_device *dev)
312{
313 struct bmac_data *bp = netdev_priv(dev);
314 volatile unsigned short regValue;
315 unsigned short *pWord16;
316 int i;
317
318 /* XXDEBUG(("bmac: enter init_registers\n")); */
319
320 bmwrite(dev, RXRST, RxResetValue);
321 bmwrite(dev, TXRST, TxResetBit);
322
323 i = 100;
324 do {
325 --i;
326 udelay(10000);
327 regValue = bmread(dev, TXRST); /* wait for reset to clear..acknowledge */
328 } while ((regValue & TxResetBit) && i > 0);
329
330 if (!bp->is_bmac_plus) {
331 regValue = bmread(dev, XCVRIF);
332 regValue |= ClkBit | SerialMode | COLActiveLow;
333 bmwrite(dev, XCVRIF, regValue);
334 udelay(10000);
335 }
336
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400337 bmwrite(dev, RSEED, (unsigned short)0x1968);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 regValue = bmread(dev, XIFC);
340 regValue |= TxOutputEnable;
341 bmwrite(dev, XIFC, regValue);
342
343 bmread(dev, PAREG);
344
345 /* set collision counters to 0 */
346 bmwrite(dev, NCCNT, 0);
347 bmwrite(dev, NTCNT, 0);
348 bmwrite(dev, EXCNT, 0);
349 bmwrite(dev, LTCNT, 0);
350
351 /* set rx counters to 0 */
352 bmwrite(dev, FRCNT, 0);
353 bmwrite(dev, LECNT, 0);
354 bmwrite(dev, AECNT, 0);
355 bmwrite(dev, FECNT, 0);
356 bmwrite(dev, RXCV, 0);
357
358 /* set tx fifo information */
359 bmwrite(dev, TXTH, 4); /* 4 octets before tx starts */
360
361 bmwrite(dev, TXFIFOCSR, 0); /* first disable txFIFO */
362 bmwrite(dev, TXFIFOCSR, TxFIFOEnable );
363
364 /* set rx fifo information */
365 bmwrite(dev, RXFIFOCSR, 0); /* first disable rxFIFO */
366 bmwrite(dev, RXFIFOCSR, RxFIFOEnable );
367
368 //bmwrite(dev, TXCFG, TxMACEnable); /* TxNeverGiveUp maybe later */
369 bmread(dev, STATUS); /* read it just to clear it */
370
371 /* zero out the chip Hash Filter registers */
372 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0;
373 bmwrite(dev, BHASH3, bp->hash_table_mask[0]); /* bits 15 - 0 */
374 bmwrite(dev, BHASH2, bp->hash_table_mask[1]); /* bits 31 - 16 */
375 bmwrite(dev, BHASH1, bp->hash_table_mask[2]); /* bits 47 - 32 */
376 bmwrite(dev, BHASH0, bp->hash_table_mask[3]); /* bits 63 - 48 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 pWord16 = (unsigned short *)dev->dev_addr;
379 bmwrite(dev, MADD0, *pWord16++);
380 bmwrite(dev, MADD1, *pWord16++);
381 bmwrite(dev, MADD2, *pWord16);
382
383 bmwrite(dev, RXCFG, RxCRCNoStrip | RxHashFilterEnable | RxRejectOwnPackets);
384
385 bmwrite(dev, INTDISABLE, EnableNormal);
386
387 return;
388}
389
390#if 0
391static void
392bmac_disable_interrupts(struct net_device *dev)
393{
394 bmwrite(dev, INTDISABLE, DisableAll);
395}
396
397static void
398bmac_enable_interrupts(struct net_device *dev)
399{
400 bmwrite(dev, INTDISABLE, EnableNormal);
401}
402#endif
403
404
405static void
406bmac_start_chip(struct net_device *dev)
407{
408 struct bmac_data *bp = netdev_priv(dev);
409 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
410 unsigned short oldConfig;
411
412 /* enable rx dma channel */
413 dbdma_continue(rd);
414
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400415 oldConfig = bmread(dev, TXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 bmwrite(dev, TXCFG, oldConfig | TxMACEnable );
417
418 /* turn on rx plus any other bits already on (promiscuous possibly) */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400419 oldConfig = bmread(dev, RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 bmwrite(dev, RXCFG, oldConfig | RxMACEnable );
421 udelay(20000);
422}
423
424static void
425bmac_init_phy(struct net_device *dev)
426{
427 unsigned int addr;
428 struct bmac_data *bp = netdev_priv(dev);
429
430 printk(KERN_DEBUG "phy registers:");
431 for (addr = 0; addr < 32; ++addr) {
432 if ((addr & 7) == 0)
433 printk("\n" KERN_DEBUG);
434 printk(" %.4x", bmac_mif_read(dev, addr));
435 }
436 printk("\n");
437 if (bp->is_bmac_plus) {
438 unsigned int capable, ctrl;
439
440 ctrl = bmac_mif_read(dev, 0);
441 capable = ((bmac_mif_read(dev, 1) & 0xf800) >> 6) | 1;
442 if (bmac_mif_read(dev, 4) != capable
443 || (ctrl & 0x1000) == 0) {
444 bmac_mif_write(dev, 4, capable);
445 bmac_mif_write(dev, 0, 0x1200);
446 } else
447 bmac_mif_write(dev, 0, 0x1000);
448 }
449}
450
451static void bmac_init_chip(struct net_device *dev)
452{
453 bmac_init_phy(dev);
454 bmac_init_registers(dev);
455}
456
457#ifdef CONFIG_PM
Pavel Machek05adc3b2005-04-16 15:25:25 -0700458static int bmac_suspend(struct macio_dev *mdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400460 struct net_device* dev = macio_get_drvdata(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct bmac_data *bp = netdev_priv(dev);
462 unsigned long flags;
463 unsigned short config;
464 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 netif_device_detach(dev);
467 /* prolly should wait for dma to finish & turn off the chip */
468 spin_lock_irqsave(&bp->lock, flags);
469 if (bp->timeout_active) {
470 del_timer(&bp->tx_timeout);
471 bp->timeout_active = 0;
472 }
473 disable_irq(dev->irq);
474 disable_irq(bp->tx_dma_intr);
475 disable_irq(bp->rx_dma_intr);
476 bp->sleeping = 1;
477 spin_unlock_irqrestore(&bp->lock, flags);
478 if (bp->opened) {
479 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
480 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 config = bmread(dev, RXCFG);
483 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
484 config = bmread(dev, TXCFG);
485 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
486 bmwrite(dev, INTDISABLE, DisableAll); /* disable all intrs */
487 /* disable rx and tx dma */
488 st_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
489 st_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
490 /* free some skb's */
491 for (i=0; i<N_RX_RING; i++) {
492 if (bp->rx_bufs[i] != NULL) {
493 dev_kfree_skb(bp->rx_bufs[i]);
494 bp->rx_bufs[i] = NULL;
495 }
496 }
497 for (i = 0; i<N_TX_RING; i++) {
498 if (bp->tx_bufs[i] != NULL) {
499 dev_kfree_skb(bp->tx_bufs[i]);
500 bp->tx_bufs[i] = NULL;
501 }
502 }
503 }
504 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
505 return 0;
506}
507
508static int bmac_resume(struct macio_dev *mdev)
509{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400510 struct net_device* dev = macio_get_drvdata(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct bmac_data *bp = netdev_priv(dev);
512
513 /* see if this is enough */
514 if (bp->opened)
515 bmac_reset_and_enable(dev);
516
517 enable_irq(dev->irq);
518 enable_irq(bp->tx_dma_intr);
519 enable_irq(bp->rx_dma_intr);
520 netif_device_attach(dev);
521
522 return 0;
523}
524#endif /* CONFIG_PM */
525
526static int bmac_set_address(struct net_device *dev, void *addr)
527{
528 struct bmac_data *bp = netdev_priv(dev);
529 unsigned char *p = addr;
530 unsigned short *pWord16;
531 unsigned long flags;
532 int i;
533
534 XXDEBUG(("bmac: enter set_address\n"));
535 spin_lock_irqsave(&bp->lock, flags);
536
537 for (i = 0; i < 6; ++i) {
538 dev->dev_addr[i] = p[i];
539 }
540 /* load up the hardware address */
541 pWord16 = (unsigned short *)dev->dev_addr;
542 bmwrite(dev, MADD0, *pWord16++);
543 bmwrite(dev, MADD1, *pWord16++);
544 bmwrite(dev, MADD2, *pWord16);
545
546 spin_unlock_irqrestore(&bp->lock, flags);
547 XXDEBUG(("bmac: exit set_address\n"));
548 return 0;
549}
550
551static inline void bmac_set_timeout(struct net_device *dev)
552{
553 struct bmac_data *bp = netdev_priv(dev);
554 unsigned long flags;
555
556 spin_lock_irqsave(&bp->lock, flags);
557 if (bp->timeout_active)
558 del_timer(&bp->tx_timeout);
559 bp->tx_timeout.expires = jiffies + TX_TIMEOUT;
560 bp->tx_timeout.function = bmac_tx_timeout;
561 bp->tx_timeout.data = (unsigned long) dev;
562 add_timer(&bp->tx_timeout);
563 bp->timeout_active = 1;
564 spin_unlock_irqrestore(&bp->lock, flags);
565}
566
567static void
568bmac_construct_xmt(struct sk_buff *skb, volatile struct dbdma_cmd *cp)
569{
570 void *vaddr;
571 unsigned long baddr;
572 unsigned long len;
573
574 len = skb->len;
575 vaddr = skb->data;
576 baddr = virt_to_bus(vaddr);
577
578 dbdma_setcmd(cp, (OUTPUT_LAST | INTR_ALWAYS | WAIT_IFCLR), len, baddr, 0);
579}
580
581static void
582bmac_construct_rxbuff(struct sk_buff *skb, volatile struct dbdma_cmd *cp)
583{
584 unsigned char *addr = skb? skb->data: bmac_emergency_rxbuf;
585
586 dbdma_setcmd(cp, (INPUT_LAST | INTR_ALWAYS), RX_BUFLEN,
587 virt_to_bus(addr), 0);
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static void
591bmac_init_tx_ring(struct bmac_data *bp)
592{
593 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
594
595 memset((char *)bp->tx_cmds, 0, (N_TX_RING+1) * sizeof(struct dbdma_cmd));
596
597 bp->tx_empty = 0;
598 bp->tx_fill = 0;
599 bp->tx_fullup = 0;
600
601 /* put a branch at the end of the tx command list */
602 dbdma_setcmd(&bp->tx_cmds[N_TX_RING],
603 (DBDMA_NOP | BR_ALWAYS), 0, 0, virt_to_bus(bp->tx_cmds));
604
605 /* reset tx dma */
606 dbdma_reset(td);
607 out_le32(&td->wait_sel, 0x00200020);
608 out_le32(&td->cmdptr, virt_to_bus(bp->tx_cmds));
609}
610
611static int
612bmac_init_rx_ring(struct bmac_data *bp)
613{
614 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
615 int i;
616 struct sk_buff *skb;
617
618 /* initialize list of sk_buffs for receiving and set up recv dma */
619 memset((char *)bp->rx_cmds, 0,
620 (N_RX_RING + 1) * sizeof(struct dbdma_cmd));
621 for (i = 0; i < N_RX_RING; i++) {
622 if ((skb = bp->rx_bufs[i]) == NULL) {
623 bp->rx_bufs[i] = skb = dev_alloc_skb(RX_BUFLEN+2);
624 if (skb != NULL)
625 skb_reserve(skb, 2);
626 }
627 bmac_construct_rxbuff(skb, &bp->rx_cmds[i]);
628 }
629
630 bp->rx_empty = 0;
631 bp->rx_fill = i;
632
633 /* Put a branch back to the beginning of the receive command list */
634 dbdma_setcmd(&bp->rx_cmds[N_RX_RING],
635 (DBDMA_NOP | BR_ALWAYS), 0, 0, virt_to_bus(bp->rx_cmds));
636
637 /* start rx dma */
638 dbdma_reset(rd);
639 out_le32(&rd->cmdptr, virt_to_bus(bp->rx_cmds));
640
641 return 1;
642}
643
644
645static int bmac_transmit_packet(struct sk_buff *skb, struct net_device *dev)
646{
647 struct bmac_data *bp = netdev_priv(dev);
648 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
649 int i;
650
651 /* see if there's a free slot in the tx ring */
652 /* XXDEBUG(("bmac_xmit_start: empty=%d fill=%d\n", */
653 /* bp->tx_empty, bp->tx_fill)); */
654 i = bp->tx_fill + 1;
655 if (i >= N_TX_RING)
656 i = 0;
657 if (i == bp->tx_empty) {
658 netif_stop_queue(dev);
659 bp->tx_fullup = 1;
660 XXDEBUG(("bmac_transmit_packet: tx ring full\n"));
661 return -1; /* can't take it at the moment */
662 }
663
664 dbdma_setcmd(&bp->tx_cmds[i], DBDMA_STOP, 0, 0, 0);
665
666 bmac_construct_xmt(skb, &bp->tx_cmds[bp->tx_fill]);
667
668 bp->tx_bufs[bp->tx_fill] = skb;
669 bp->tx_fill = i;
670
671 bp->stats.tx_bytes += skb->len;
672
673 dbdma_continue(td);
674
675 return 0;
676}
677
678static int rxintcount;
679
David Howells7d12e782006-10-05 14:55:46 +0100680static irqreturn_t bmac_rxdma_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct net_device *dev = (struct net_device *) dev_id;
683 struct bmac_data *bp = netdev_priv(dev);
684 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
685 volatile struct dbdma_cmd *cp;
686 int i, nb, stat;
687 struct sk_buff *skb;
688 unsigned int residual;
689 int last;
690 unsigned long flags;
691
692 spin_lock_irqsave(&bp->lock, flags);
693
694 if (++rxintcount < 10) {
695 XXDEBUG(("bmac_rxdma_intr\n"));
696 }
697
698 last = -1;
699 i = bp->rx_empty;
700
701 while (1) {
702 cp = &bp->rx_cmds[i];
703 stat = ld_le16(&cp->xfer_status);
704 residual = ld_le16(&cp->res_count);
705 if ((stat & ACTIVE) == 0)
706 break;
707 nb = RX_BUFLEN - residual - 2;
708 if (nb < (ETHERMINPACKET - ETHERCRC)) {
709 skb = NULL;
710 bp->stats.rx_length_errors++;
711 bp->stats.rx_errors++;
712 } else {
713 skb = bp->rx_bufs[i];
714 bp->rx_bufs[i] = NULL;
715 }
716 if (skb != NULL) {
717 nb -= ETHERCRC;
718 skb_put(skb, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 skb->protocol = eth_type_trans(skb, dev);
720 netif_rx(skb);
721 dev->last_rx = jiffies;
722 ++bp->stats.rx_packets;
723 bp->stats.rx_bytes += nb;
724 } else {
725 ++bp->stats.rx_dropped;
726 }
727 dev->last_rx = jiffies;
728 if ((skb = bp->rx_bufs[i]) == NULL) {
729 bp->rx_bufs[i] = skb = dev_alloc_skb(RX_BUFLEN+2);
730 if (skb != NULL)
731 skb_reserve(bp->rx_bufs[i], 2);
732 }
733 bmac_construct_rxbuff(skb, &bp->rx_cmds[i]);
734 st_le16(&cp->res_count, 0);
735 st_le16(&cp->xfer_status, 0);
736 last = i;
737 if (++i >= N_RX_RING) i = 0;
738 }
739
740 if (last != -1) {
741 bp->rx_fill = last;
742 bp->rx_empty = i;
743 }
744
745 dbdma_continue(rd);
746 spin_unlock_irqrestore(&bp->lock, flags);
747
748 if (rxintcount < 10) {
749 XXDEBUG(("bmac_rxdma_intr done\n"));
750 }
751 return IRQ_HANDLED;
752}
753
754static int txintcount;
755
David Howells7d12e782006-10-05 14:55:46 +0100756static irqreturn_t bmac_txdma_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct net_device *dev = (struct net_device *) dev_id;
759 struct bmac_data *bp = netdev_priv(dev);
760 volatile struct dbdma_cmd *cp;
761 int stat;
762 unsigned long flags;
763
764 spin_lock_irqsave(&bp->lock, flags);
765
766 if (txintcount++ < 10) {
767 XXDEBUG(("bmac_txdma_intr\n"));
768 }
769
770 /* del_timer(&bp->tx_timeout); */
771 /* bp->timeout_active = 0; */
772
773 while (1) {
774 cp = &bp->tx_cmds[bp->tx_empty];
775 stat = ld_le16(&cp->xfer_status);
776 if (txintcount < 10) {
777 XXDEBUG(("bmac_txdma_xfer_stat=%#0x\n", stat));
778 }
779 if (!(stat & ACTIVE)) {
780 /*
781 * status field might not have been filled by DBDMA
782 */
783 if (cp == bus_to_virt(in_le32(&bp->tx_dma->cmdptr)))
784 break;
785 }
786
787 if (bp->tx_bufs[bp->tx_empty]) {
788 ++bp->stats.tx_packets;
789 dev_kfree_skb_irq(bp->tx_bufs[bp->tx_empty]);
790 }
791 bp->tx_bufs[bp->tx_empty] = NULL;
792 bp->tx_fullup = 0;
793 netif_wake_queue(dev);
794 if (++bp->tx_empty >= N_TX_RING)
795 bp->tx_empty = 0;
796 if (bp->tx_empty == bp->tx_fill)
797 break;
798 }
799
800 spin_unlock_irqrestore(&bp->lock, flags);
801
802 if (txintcount < 10) {
803 XXDEBUG(("bmac_txdma_intr done->bmac_start\n"));
804 }
805
806 bmac_start(dev);
807 return IRQ_HANDLED;
808}
809
810static struct net_device_stats *bmac_stats(struct net_device *dev)
811{
812 struct bmac_data *p = netdev_priv(dev);
813
814 return &p->stats;
815}
816
817#ifndef SUNHME_MULTICAST
818/* Real fast bit-reversal algorithm, 6-bit values */
819static int reverse6[64] = {
820 0x0,0x20,0x10,0x30,0x8,0x28,0x18,0x38,
821 0x4,0x24,0x14,0x34,0xc,0x2c,0x1c,0x3c,
822 0x2,0x22,0x12,0x32,0xa,0x2a,0x1a,0x3a,
823 0x6,0x26,0x16,0x36,0xe,0x2e,0x1e,0x3e,
824 0x1,0x21,0x11,0x31,0x9,0x29,0x19,0x39,
825 0x5,0x25,0x15,0x35,0xd,0x2d,0x1d,0x3d,
826 0x3,0x23,0x13,0x33,0xb,0x2b,0x1b,0x3b,
827 0x7,0x27,0x17,0x37,0xf,0x2f,0x1f,0x3f
828};
829
830static unsigned int
831crc416(unsigned int curval, unsigned short nxtval)
832{
833 register unsigned int counter, cur = curval, next = nxtval;
834 register int high_crc_set, low_data_set;
835
836 /* Swap bytes */
837 next = ((next & 0x00FF) << 8) | (next >> 8);
838
839 /* Compute bit-by-bit */
840 for (counter = 0; counter < 16; ++counter) {
841 /* is high CRC bit set? */
842 if ((cur & 0x80000000) == 0) high_crc_set = 0;
843 else high_crc_set = 1;
844
845 cur = cur << 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if ((next & 0x0001) == 0) low_data_set = 0;
848 else low_data_set = 1;
849
850 next = next >> 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* do the XOR */
853 if (high_crc_set ^ low_data_set) cur = cur ^ ENET_CRCPOLY;
854 }
855 return cur;
856}
857
858static unsigned int
859bmac_crc(unsigned short *address)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400860{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned int newcrc;
862
863 XXDEBUG(("bmac_crc: addr=%#04x, %#04x, %#04x\n", *address, address[1], address[2]));
864 newcrc = crc416(0xffffffff, *address); /* address bits 47 - 32 */
865 newcrc = crc416(newcrc, address[1]); /* address bits 31 - 16 */
866 newcrc = crc416(newcrc, address[2]); /* address bits 15 - 0 */
867
868 return(newcrc);
869}
870
871/*
872 * Add requested mcast addr to BMac's hash table filter.
873 *
874 */
875
876static void
877bmac_addhash(struct bmac_data *bp, unsigned char *addr)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400878{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 unsigned int crc;
880 unsigned short mask;
881
882 if (!(*addr)) return;
883 crc = bmac_crc((unsigned short *)addr) & 0x3f; /* Big-endian alert! */
884 crc = reverse6[crc]; /* Hyperfast bit-reversing algorithm */
885 if (bp->hash_use_count[crc]++) return; /* This bit is already set */
886 mask = crc % 16;
887 mask = (unsigned char)1 << mask;
888 bp->hash_use_count[crc/16] |= mask;
889}
890
891static void
892bmac_removehash(struct bmac_data *bp, unsigned char *addr)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400893{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 unsigned int crc;
895 unsigned char mask;
896
897 /* Now, delete the address from the filter copy, as indicated */
898 crc = bmac_crc((unsigned short *)addr) & 0x3f; /* Big-endian alert! */
899 crc = reverse6[crc]; /* Hyperfast bit-reversing algorithm */
900 if (bp->hash_use_count[crc] == 0) return; /* That bit wasn't in use! */
901 if (--bp->hash_use_count[crc]) return; /* That bit is still in use */
902 mask = crc % 16;
903 mask = ((unsigned char)1 << mask) ^ 0xffff; /* To turn off bit */
904 bp->hash_table_mask[crc/16] &= mask;
905}
906
907/*
908 * Sync the adapter with the software copy of the multicast mask
909 * (logical address filter).
910 */
911
912static void
913bmac_rx_off(struct net_device *dev)
914{
915 unsigned short rx_cfg;
916
917 rx_cfg = bmread(dev, RXCFG);
918 rx_cfg &= ~RxMACEnable;
919 bmwrite(dev, RXCFG, rx_cfg);
920 do {
921 rx_cfg = bmread(dev, RXCFG);
922 } while (rx_cfg & RxMACEnable);
923}
924
925unsigned short
926bmac_rx_on(struct net_device *dev, int hash_enable, int promisc_enable)
927{
928 unsigned short rx_cfg;
929
930 rx_cfg = bmread(dev, RXCFG);
931 rx_cfg |= RxMACEnable;
932 if (hash_enable) rx_cfg |= RxHashFilterEnable;
933 else rx_cfg &= ~RxHashFilterEnable;
934 if (promisc_enable) rx_cfg |= RxPromiscEnable;
935 else rx_cfg &= ~RxPromiscEnable;
936 bmwrite(dev, RXRST, RxResetValue);
937 bmwrite(dev, RXFIFOCSR, 0); /* first disable rxFIFO */
938 bmwrite(dev, RXFIFOCSR, RxFIFOEnable );
939 bmwrite(dev, RXCFG, rx_cfg );
940 return rx_cfg;
941}
942
943static void
944bmac_update_hash_table_mask(struct net_device *dev, struct bmac_data *bp)
945{
946 bmwrite(dev, BHASH3, bp->hash_table_mask[0]); /* bits 15 - 0 */
947 bmwrite(dev, BHASH2, bp->hash_table_mask[1]); /* bits 31 - 16 */
948 bmwrite(dev, BHASH1, bp->hash_table_mask[2]); /* bits 47 - 32 */
949 bmwrite(dev, BHASH0, bp->hash_table_mask[3]); /* bits 63 - 48 */
950}
951
952#if 0
953static void
954bmac_add_multi(struct net_device *dev,
955 struct bmac_data *bp, unsigned char *addr)
956{
957 /* XXDEBUG(("bmac: enter bmac_add_multi\n")); */
958 bmac_addhash(bp, addr);
959 bmac_rx_off(dev);
960 bmac_update_hash_table_mask(dev, bp);
961 bmac_rx_on(dev, 1, (dev->flags & IFF_PROMISC)? 1 : 0);
962 /* XXDEBUG(("bmac: exit bmac_add_multi\n")); */
963}
964
965static void
966bmac_remove_multi(struct net_device *dev,
967 struct bmac_data *bp, unsigned char *addr)
968{
969 bmac_removehash(bp, addr);
970 bmac_rx_off(dev);
971 bmac_update_hash_table_mask(dev, bp);
972 bmac_rx_on(dev, 1, (dev->flags & IFF_PROMISC)? 1 : 0);
973}
974#endif
975
976/* Set or clear the multicast filter for this adaptor.
977 num_addrs == -1 Promiscuous mode, receive all packets
978 num_addrs == 0 Normal mode, clear multicast list
979 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
980 best-effort filtering.
981 */
982static void bmac_set_multicast(struct net_device *dev)
983{
984 struct dev_mc_list *dmi;
985 struct bmac_data *bp = netdev_priv(dev);
986 int num_addrs = dev->mc_count;
987 unsigned short rx_cfg;
988 int i;
989
990 if (bp->sleeping)
991 return;
992
993 XXDEBUG(("bmac: enter bmac_set_multicast, n_addrs=%d\n", num_addrs));
994
995 if((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
996 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0xffff;
997 bmac_update_hash_table_mask(dev, bp);
998 rx_cfg = bmac_rx_on(dev, 1, 0);
999 XXDEBUG(("bmac: all multi, rx_cfg=%#08x\n"));
1000 } else if ((dev->flags & IFF_PROMISC) || (num_addrs < 0)) {
1001 rx_cfg = bmread(dev, RXCFG);
1002 rx_cfg |= RxPromiscEnable;
1003 bmwrite(dev, RXCFG, rx_cfg);
1004 rx_cfg = bmac_rx_on(dev, 0, 1);
1005 XXDEBUG(("bmac: promisc mode enabled, rx_cfg=%#08x\n", rx_cfg));
1006 } else {
1007 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0;
1008 for (i=0; i<64; i++) bp->hash_use_count[i] = 0;
1009 if (num_addrs == 0) {
1010 rx_cfg = bmac_rx_on(dev, 0, 0);
1011 XXDEBUG(("bmac: multi disabled, rx_cfg=%#08x\n", rx_cfg));
1012 } else {
1013 for (dmi=dev->mc_list; dmi!=NULL; dmi=dmi->next)
1014 bmac_addhash(bp, dmi->dmi_addr);
1015 bmac_update_hash_table_mask(dev, bp);
1016 rx_cfg = bmac_rx_on(dev, 1, 0);
1017 XXDEBUG(("bmac: multi enabled, rx_cfg=%#08x\n", rx_cfg));
1018 }
1019 }
1020 /* XXDEBUG(("bmac: exit bmac_set_multicast\n")); */
1021}
1022#else /* ifdef SUNHME_MULTICAST */
1023
1024/* The version of set_multicast below was lifted from sunhme.c */
1025
1026static void bmac_set_multicast(struct net_device *dev)
1027{
1028 struct dev_mc_list *dmi = dev->mc_list;
1029 char *addrs;
1030 int i;
1031 unsigned short rx_cfg;
1032 u32 crc;
1033
1034 if((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
1035 bmwrite(dev, BHASH0, 0xffff);
1036 bmwrite(dev, BHASH1, 0xffff);
1037 bmwrite(dev, BHASH2, 0xffff);
1038 bmwrite(dev, BHASH3, 0xffff);
1039 } else if(dev->flags & IFF_PROMISC) {
1040 rx_cfg = bmread(dev, RXCFG);
1041 rx_cfg |= RxPromiscEnable;
1042 bmwrite(dev, RXCFG, rx_cfg);
1043 } else {
1044 u16 hash_table[4];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 rx_cfg = bmread(dev, RXCFG);
1047 rx_cfg &= ~RxPromiscEnable;
1048 bmwrite(dev, RXCFG, rx_cfg);
1049
1050 for(i = 0; i < 4; i++) hash_table[i] = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 for(i = 0; i < dev->mc_count; i++) {
1053 addrs = dmi->dmi_addr;
1054 dmi = dmi->next;
1055
1056 if(!(*addrs & 1))
1057 continue;
1058
1059 crc = ether_crc_le(6, addrs);
1060 crc >>= 26;
1061 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1062 }
1063 bmwrite(dev, BHASH0, hash_table[0]);
1064 bmwrite(dev, BHASH1, hash_table[1]);
1065 bmwrite(dev, BHASH2, hash_table[2]);
1066 bmwrite(dev, BHASH3, hash_table[3]);
1067 }
1068}
1069#endif /* SUNHME_MULTICAST */
1070
1071static int miscintcount;
1072
David Howells7d12e782006-10-05 14:55:46 +01001073static irqreturn_t bmac_misc_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct net_device *dev = (struct net_device *) dev_id;
1076 struct bmac_data *bp = netdev_priv(dev);
1077 unsigned int status = bmread(dev, STATUS);
1078 if (miscintcount++ < 10) {
1079 XXDEBUG(("bmac_misc_intr\n"));
1080 }
1081 /* XXDEBUG(("bmac_misc_intr, status=%#08x\n", status)); */
David Howells7d12e782006-10-05 14:55:46 +01001082 /* bmac_txdma_intr_inner(irq, dev_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* if (status & FrameReceived) bp->stats.rx_dropped++; */
1084 if (status & RxErrorMask) bp->stats.rx_errors++;
1085 if (status & RxCRCCntExp) bp->stats.rx_crc_errors++;
1086 if (status & RxLenCntExp) bp->stats.rx_length_errors++;
1087 if (status & RxOverFlow) bp->stats.rx_over_errors++;
1088 if (status & RxAlignCntExp) bp->stats.rx_frame_errors++;
1089
1090 /* if (status & FrameSent) bp->stats.tx_dropped++; */
1091 if (status & TxErrorMask) bp->stats.tx_errors++;
1092 if (status & TxUnderrun) bp->stats.tx_fifo_errors++;
1093 if (status & TxNormalCollExp) bp->stats.collisions++;
1094 return IRQ_HANDLED;
1095}
1096
1097/*
1098 * Procedure for reading EEPROM
1099 */
1100#define SROMAddressLength 5
1101#define DataInOn 0x0008
1102#define DataInOff 0x0000
1103#define Clk 0x0002
1104#define ChipSelect 0x0001
1105#define SDIShiftCount 3
1106#define SD0ShiftCount 2
1107#define DelayValue 1000 /* number of microseconds */
1108#define SROMStartOffset 10 /* this is in words */
1109#define SROMReadCount 3 /* number of words to read from SROM */
1110#define SROMAddressBits 6
1111#define EnetAddressOffset 20
1112
1113static unsigned char
1114bmac_clock_out_bit(struct net_device *dev)
1115{
1116 unsigned short data;
1117 unsigned short val;
1118
1119 bmwrite(dev, SROMCSR, ChipSelect | Clk);
1120 udelay(DelayValue);
1121
1122 data = bmread(dev, SROMCSR);
1123 udelay(DelayValue);
1124 val = (data >> SD0ShiftCount) & 1;
1125
1126 bmwrite(dev, SROMCSR, ChipSelect);
1127 udelay(DelayValue);
1128
1129 return val;
1130}
1131
1132static void
1133bmac_clock_in_bit(struct net_device *dev, unsigned int val)
1134{
1135 unsigned short data;
1136
1137 if (val != 0 && val != 1) return;
1138
1139 data = (val << SDIShiftCount);
1140 bmwrite(dev, SROMCSR, data | ChipSelect );
1141 udelay(DelayValue);
1142
1143 bmwrite(dev, SROMCSR, data | ChipSelect | Clk );
1144 udelay(DelayValue);
1145
1146 bmwrite(dev, SROMCSR, data | ChipSelect);
1147 udelay(DelayValue);
1148}
1149
1150static void
1151reset_and_select_srom(struct net_device *dev)
1152{
1153 /* first reset */
1154 bmwrite(dev, SROMCSR, 0);
1155 udelay(DelayValue);
1156
1157 /* send it the read command (110) */
1158 bmac_clock_in_bit(dev, 1);
1159 bmac_clock_in_bit(dev, 1);
1160 bmac_clock_in_bit(dev, 0);
1161}
1162
1163static unsigned short
1164read_srom(struct net_device *dev, unsigned int addr, unsigned int addr_len)
1165{
1166 unsigned short data, val;
1167 int i;
1168
1169 /* send out the address we want to read from */
1170 for (i = 0; i < addr_len; i++) {
1171 val = addr >> (addr_len-i-1);
1172 bmac_clock_in_bit(dev, val & 1);
1173 }
1174
1175 /* Now read in the 16-bit data */
1176 data = 0;
1177 for (i = 0; i < 16; i++) {
1178 val = bmac_clock_out_bit(dev);
1179 data <<= 1;
1180 data |= val;
1181 }
1182 bmwrite(dev, SROMCSR, 0);
1183
1184 return data;
1185}
1186
1187/*
1188 * It looks like Cogent and SMC use different methods for calculating
1189 * checksums. What a pain..
1190 */
1191
1192static int
1193bmac_verify_checksum(struct net_device *dev)
1194{
1195 unsigned short data, storedCS;
1196
1197 reset_and_select_srom(dev);
1198 data = read_srom(dev, 3, SROMAddressBits);
1199 storedCS = ((data >> 8) & 0x0ff) | ((data << 8) & 0xff00);
1200
1201 return 0;
1202}
1203
1204
1205static void
1206bmac_get_station_address(struct net_device *dev, unsigned char *ea)
1207{
1208 int i;
1209 unsigned short data;
1210
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001211 for (i = 0; i < 6; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 {
1213 reset_and_select_srom(dev);
1214 data = read_srom(dev, i + EnetAddressOffset/2, SROMAddressBits);
Akinobu Mitabc63eb92006-12-19 13:09:08 -08001215 ea[2*i] = bitrev8(data & 0x0ff);
1216 ea[2*i+1] = bitrev8((data >> 8) & 0x0ff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218}
1219
1220static void bmac_reset_and_enable(struct net_device *dev)
1221{
1222 struct bmac_data *bp = netdev_priv(dev);
1223 unsigned long flags;
1224 struct sk_buff *skb;
1225 unsigned char *data;
1226
1227 spin_lock_irqsave(&bp->lock, flags);
1228 bmac_enable_and_reset_chip(dev);
1229 bmac_init_tx_ring(bp);
1230 bmac_init_rx_ring(bp);
1231 bmac_init_chip(dev);
1232 bmac_start_chip(dev);
1233 bmwrite(dev, INTDISABLE, EnableNormal);
1234 bp->sleeping = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
1237 * It seems that the bmac can't receive until it's transmitted
1238 * a packet. So we give it a dummy packet to transmit.
1239 */
1240 skb = dev_alloc_skb(ETHERMINPACKET);
1241 if (skb != NULL) {
1242 data = skb_put(skb, ETHERMINPACKET);
1243 memset(data, 0, ETHERMINPACKET);
1244 memcpy(data, dev->dev_addr, 6);
1245 memcpy(data+6, dev->dev_addr, 6);
1246 bmac_transmit_packet(skb, dev);
1247 }
1248 spin_unlock_irqrestore(&bp->lock, flags);
1249}
Olaf Heringced13332007-08-25 20:32:59 +02001250static void bmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1251{
1252 struct bmac_data *bp = netdev_priv(dev);
1253 strcpy(info->driver, "bmac");
1254 strcpy(info->bus_info, bp->mdev->ofdev.dev.bus_id);
1255}
1256
1257static const struct ethtool_ops bmac_ethtool_ops = {
1258 .get_drvinfo = bmac_get_drvinfo,
1259 .get_link = ethtool_op_get_link,
1260};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Jeff Mahoney5e655772005-07-06 15:44:41 -04001262static int __devinit bmac_probe(struct macio_dev *mdev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 int j, rev, ret;
1265 struct bmac_data *bp;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001266 const unsigned char *prop_addr;
1267 unsigned char addr[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct net_device *dev;
1269 int is_bmac_plus = ((int)match->data) != 0;
1270
1271 if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
1272 printk(KERN_ERR "BMAC: can't use, need 3 addrs and 3 intrs\n");
1273 return -ENODEV;
1274 }
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001275 prop_addr = of_get_property(macio_get_of_node(mdev),
1276 "mac-address", NULL);
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001277 if (prop_addr == NULL) {
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001278 prop_addr = of_get_property(macio_get_of_node(mdev),
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001279 "local-mac-address", NULL);
1280 if (prop_addr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 printk(KERN_ERR "BMAC: Can't get mac-address\n");
1282 return -ENODEV;
1283 }
1284 }
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001285 memcpy(addr, prop_addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 dev = alloc_etherdev(PRIV_BYTES);
1288 if (!dev) {
1289 printk(KERN_ERR "BMAC: alloc_etherdev failed, out of memory\n");
1290 return -ENOMEM;
1291 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 bp = netdev_priv(dev);
1294 SET_MODULE_OWNER(dev);
1295 SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
1296 macio_set_drvdata(mdev, dev);
1297
1298 bp->mdev = mdev;
1299 spin_lock_init(&bp->lock);
1300
1301 if (macio_request_resources(mdev, "bmac")) {
1302 printk(KERN_ERR "BMAC: can't request IO resource !\n");
1303 goto out_free;
1304 }
1305
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;
1310
1311 dev->irq = macio_irq(mdev, 0);
1312
1313 bmac_enable_and_reset_chip(dev);
1314 bmwrite(dev, INTDISABLE, DisableAll);
1315
1316 rev = addr[0] == 0 && addr[1] == 0xA0;
1317 for (j = 0; j < 6; ++j)
Akinobu Mitabc63eb92006-12-19 13:09:08 -08001318 dev->dev_addr[j] = rev ? bitrev8(addr[j]): addr[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /* Enable chip without interrupts for now */
1321 bmac_enable_and_reset_chip(dev);
1322 bmwrite(dev, INTDISABLE, DisableAll);
1323
1324 dev->open = bmac_open;
1325 dev->stop = bmac_close;
Olaf Heringced13332007-08-25 20:32:59 +02001326 dev->ethtool_ops = &bmac_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 dev->hard_start_xmit = bmac_output;
1328 dev->get_stats = bmac_stats;
1329 dev->set_multicast_list = bmac_set_multicast;
1330 dev->set_mac_address = bmac_set_address;
1331
1332 bmac_get_station_address(dev, addr);
1333 if (bmac_verify_checksum(dev) != 0)
1334 goto err_out_iounmap;
1335
1336 bp->is_bmac_plus = is_bmac_plus;
1337 bp->tx_dma = ioremap(macio_resource_start(mdev, 1), macio_resource_len(mdev, 1));
1338 if (!bp->tx_dma)
1339 goto err_out_iounmap;
1340 bp->tx_dma_intr = macio_irq(mdev, 1);
1341 bp->rx_dma = ioremap(macio_resource_start(mdev, 2), macio_resource_len(mdev, 2));
1342 if (!bp->rx_dma)
1343 goto err_out_iounmap_tx;
1344 bp->rx_dma_intr = macio_irq(mdev, 2);
1345
1346 bp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(bp + 1);
1347 bp->rx_cmds = bp->tx_cmds + N_TX_RING + 1;
1348
1349 bp->queue = (struct sk_buff_head *)(bp->rx_cmds + N_RX_RING + 1);
1350 skb_queue_head_init(bp->queue);
1351
1352 init_timer(&bp->tx_timeout);
1353
1354 ret = request_irq(dev->irq, bmac_misc_intr, 0, "BMAC-misc", dev);
1355 if (ret) {
1356 printk(KERN_ERR "BMAC: can't get irq %d\n", dev->irq);
1357 goto err_out_iounmap_rx;
1358 }
1359 ret = request_irq(bp->tx_dma_intr, bmac_txdma_intr, 0, "BMAC-txdma", dev);
1360 if (ret) {
1361 printk(KERN_ERR "BMAC: can't get irq %d\n", bp->tx_dma_intr);
1362 goto err_out_irq0;
1363 }
1364 ret = request_irq(bp->rx_dma_intr, bmac_rxdma_intr, 0, "BMAC-rxdma", dev);
1365 if (ret) {
1366 printk(KERN_ERR "BMAC: can't get irq %d\n", bp->rx_dma_intr);
1367 goto err_out_irq1;
1368 }
1369
1370 /* Mask chip interrupts and disable chip, will be
1371 * re-enabled on open()
1372 */
1373 disable_irq(dev->irq);
1374 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1375
1376 if (register_netdev(dev) != 0) {
1377 printk(KERN_ERR "BMAC: Ethernet registration failed\n");
1378 goto err_out_irq2;
1379 }
1380
1381 printk(KERN_INFO "%s: BMAC%s at", dev->name, (is_bmac_plus? "+": ""));
1382 for (j = 0; j < 6; ++j)
1383 printk("%c%.2x", (j? ':': ' '), dev->dev_addr[j]);
1384 XXDEBUG((", base_addr=%#0lx", dev->base_addr));
1385 printk("\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 0;
1388
1389err_out_irq2:
1390 free_irq(bp->rx_dma_intr, dev);
1391err_out_irq1:
1392 free_irq(bp->tx_dma_intr, dev);
1393err_out_irq0:
1394 free_irq(dev->irq, dev);
1395err_out_iounmap_rx:
1396 iounmap(bp->rx_dma);
1397err_out_iounmap_tx:
1398 iounmap(bp->tx_dma);
1399err_out_iounmap:
1400 iounmap((void __iomem *)dev->base_addr);
1401out_release:
1402 macio_release_resources(mdev);
1403out_free:
1404 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1405 free_netdev(dev);
1406
1407 return -ENODEV;
1408}
1409
1410static int bmac_open(struct net_device *dev)
1411{
1412 struct bmac_data *bp = netdev_priv(dev);
1413 /* XXDEBUG(("bmac: enter open\n")); */
1414 /* reset the chip */
1415 bp->opened = 1;
1416 bmac_reset_and_enable(dev);
1417 enable_irq(dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return 0;
1419}
1420
1421static int bmac_close(struct net_device *dev)
1422{
1423 struct bmac_data *bp = netdev_priv(dev);
1424 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
1425 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
1426 unsigned short config;
1427 int i;
1428
1429 bp->sleeping = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* disable rx and tx */
1432 config = bmread(dev, RXCFG);
1433 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
1434
1435 config = bmread(dev, TXCFG);
1436 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
1437
1438 bmwrite(dev, INTDISABLE, DisableAll); /* disable all intrs */
1439
1440 /* disable rx and tx dma */
1441 st_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
1442 st_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
1443
1444 /* free some skb's */
1445 XXDEBUG(("bmac: free rx bufs\n"));
1446 for (i=0; i<N_RX_RING; i++) {
1447 if (bp->rx_bufs[i] != NULL) {
1448 dev_kfree_skb(bp->rx_bufs[i]);
1449 bp->rx_bufs[i] = NULL;
1450 }
1451 }
1452 XXDEBUG(("bmac: free tx bufs\n"));
1453 for (i = 0; i<N_TX_RING; i++) {
1454 if (bp->tx_bufs[i] != NULL) {
1455 dev_kfree_skb(bp->tx_bufs[i]);
1456 bp->tx_bufs[i] = NULL;
1457 }
1458 }
1459 XXDEBUG(("bmac: all bufs freed\n"));
1460
1461 bp->opened = 0;
1462 disable_irq(dev->irq);
1463 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1464
1465 return 0;
1466}
1467
1468static void
1469bmac_start(struct net_device *dev)
1470{
1471 struct bmac_data *bp = netdev_priv(dev);
1472 int i;
1473 struct sk_buff *skb;
1474 unsigned long flags;
1475
1476 if (bp->sleeping)
1477 return;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 spin_lock_irqsave(&bp->lock, flags);
1480 while (1) {
1481 i = bp->tx_fill + 1;
1482 if (i >= N_TX_RING)
1483 i = 0;
1484 if (i == bp->tx_empty)
1485 break;
1486 skb = skb_dequeue(bp->queue);
1487 if (skb == NULL)
1488 break;
1489 bmac_transmit_packet(skb, dev);
1490 }
1491 spin_unlock_irqrestore(&bp->lock, flags);
1492}
1493
1494static int
1495bmac_output(struct sk_buff *skb, struct net_device *dev)
1496{
1497 struct bmac_data *bp = netdev_priv(dev);
1498 skb_queue_tail(bp->queue, skb);
1499 bmac_start(dev);
1500 return 0;
1501}
1502
1503static void bmac_tx_timeout(unsigned long data)
1504{
1505 struct net_device *dev = (struct net_device *) data;
1506 struct bmac_data *bp = netdev_priv(dev);
1507 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
1508 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
1509 volatile struct dbdma_cmd *cp;
1510 unsigned long flags;
1511 unsigned short config, oldConfig;
1512 int i;
1513
1514 XXDEBUG(("bmac: tx_timeout called\n"));
1515 spin_lock_irqsave(&bp->lock, flags);
1516 bp->timeout_active = 0;
1517
1518 /* update various counters */
1519/* bmac_handle_misc_intrs(bp, 0); */
1520
1521 cp = &bp->tx_cmds[bp->tx_empty];
1522/* XXDEBUG((KERN_DEBUG "bmac: tx dmastat=%x %x runt=%d pr=%x fs=%x fc=%x\n", */
1523/* ld_le32(&td->status), ld_le16(&cp->xfer_status), bp->tx_bad_runt, */
1524/* mb->pr, mb->xmtfs, mb->fifofc)); */
1525
1526 /* turn off both tx and rx and reset the chip */
1527 config = bmread(dev, RXCFG);
1528 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
1529 config = bmread(dev, TXCFG);
1530 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
1531 out_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE|ACTIVE|DEAD));
1532 printk(KERN_ERR "bmac: transmit timeout - resetting\n");
1533 bmac_enable_and_reset_chip(dev);
1534
1535 /* restart rx dma */
1536 cp = bus_to_virt(ld_le32(&rd->cmdptr));
1537 out_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE|ACTIVE|DEAD));
1538 out_le16(&cp->xfer_status, 0);
1539 out_le32(&rd->cmdptr, virt_to_bus(cp));
1540 out_le32(&rd->control, DBDMA_SET(RUN|WAKE));
1541
1542 /* fix up the transmit side */
1543 XXDEBUG((KERN_DEBUG "bmac: tx empty=%d fill=%d fullup=%d\n",
1544 bp->tx_empty, bp->tx_fill, bp->tx_fullup));
1545 i = bp->tx_empty;
1546 ++bp->stats.tx_errors;
1547 if (i != bp->tx_fill) {
1548 dev_kfree_skb(bp->tx_bufs[i]);
1549 bp->tx_bufs[i] = NULL;
1550 if (++i >= N_TX_RING) i = 0;
1551 bp->tx_empty = i;
1552 }
1553 bp->tx_fullup = 0;
1554 netif_wake_queue(dev);
1555 if (i != bp->tx_fill) {
1556 cp = &bp->tx_cmds[i];
1557 out_le16(&cp->xfer_status, 0);
1558 out_le16(&cp->command, OUTPUT_LAST);
1559 out_le32(&td->cmdptr, virt_to_bus(cp));
1560 out_le32(&td->control, DBDMA_SET(RUN));
1561 /* bmac_set_timeout(dev); */
1562 XXDEBUG((KERN_DEBUG "bmac: starting %d\n", i));
1563 }
1564
1565 /* turn it back on */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001566 oldConfig = bmread(dev, RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 bmwrite(dev, RXCFG, oldConfig | RxMACEnable );
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001568 oldConfig = bmread(dev, TXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 bmwrite(dev, TXCFG, oldConfig | TxMACEnable );
1570
1571 spin_unlock_irqrestore(&bp->lock, flags);
1572}
1573
1574#if 0
1575static void dump_dbdma(volatile struct dbdma_cmd *cp,int count)
1576{
1577 int i,*ip;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 for (i=0;i< count;i++) {
1580 ip = (int*)(cp+i);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 printk("dbdma req 0x%x addr 0x%x baddr 0x%x xfer/res 0x%x\n",
1583 ld_le32(ip+0),
1584 ld_le32(ip+1),
1585 ld_le32(ip+2),
1586 ld_le32(ip+3));
1587 }
1588
1589}
1590#endif
1591
1592#if 0
1593static int
1594bmac_proc_info(char *buffer, char **start, off_t offset, int length)
1595{
1596 int len = 0;
1597 off_t pos = 0;
1598 off_t begin = 0;
1599 int i;
1600
1601 if (bmac_devs == NULL)
1602 return (-ENOSYS);
1603
1604 len += sprintf(buffer, "BMAC counters & registers\n");
1605
1606 for (i = 0; i<N_REG_ENTRIES; i++) {
1607 len += sprintf(buffer + len, "%s: %#08x\n",
1608 reg_entries[i].name,
1609 bmread(bmac_devs, reg_entries[i].reg_offset));
1610 pos = begin + len;
1611
1612 if (pos < offset) {
1613 len = 0;
1614 begin = pos;
1615 }
1616
1617 if (pos > offset+length) break;
1618 }
1619
1620 *start = buffer + (offset - begin);
1621 len -= (offset - begin);
1622
1623 if (len > length) len = length;
1624
1625 return len;
1626}
1627#endif
1628
1629static int __devexit bmac_remove(struct macio_dev *mdev)
1630{
1631 struct net_device *dev = macio_get_drvdata(mdev);
1632 struct bmac_data *bp = netdev_priv(dev);
1633
1634 unregister_netdev(dev);
1635
1636 free_irq(dev->irq, dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001637 free_irq(bp->tx_dma_intr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 free_irq(bp->rx_dma_intr, dev);
1639
1640 iounmap((void __iomem *)dev->base_addr);
1641 iounmap(bp->tx_dma);
1642 iounmap(bp->rx_dma);
1643
1644 macio_release_resources(mdev);
1645
1646 free_netdev(dev);
1647
1648 return 0;
1649}
1650
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001651static struct of_device_id bmac_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 {
1654 .name = "bmac",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 .data = (void *)0,
1656 },
1657 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 .type = "network",
1659 .compatible = "bmac+",
1660 .data = (void *)1,
1661 },
1662 {},
1663};
Olaf Hering8c9795b2005-10-28 17:46:21 -07001664MODULE_DEVICE_TABLE (of, bmac_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001666static struct macio_driver bmac_driver =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 .name = "bmac",
1669 .match_table = bmac_match,
1670 .probe = bmac_probe,
1671 .remove = bmac_remove,
1672#ifdef CONFIG_PM
1673 .suspend = bmac_suspend,
1674 .resume = bmac_resume,
1675#endif
1676};
1677
1678
1679static int __init bmac_init(void)
1680{
1681 if (bmac_emergency_rxbuf == NULL) {
1682 bmac_emergency_rxbuf = kmalloc(RX_BUFLEN, GFP_KERNEL);
1683 if (bmac_emergency_rxbuf == NULL) {
1684 printk(KERN_ERR "BMAC: can't allocate emergency RX buffer\n");
1685 return -ENOMEM;
1686 }
1687 }
1688
1689 return macio_register_driver(&bmac_driver);
1690}
1691
1692static void __exit bmac_exit(void)
1693{
1694 macio_unregister_driver(&bmac_driver);
1695
Jesper Juhlb4558ea2005-10-28 16:53:13 -04001696 kfree(bmac_emergency_rxbuf);
1697 bmac_emergency_rxbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
1700MODULE_AUTHOR("Randy Gobbel/Paul Mackerras");
1701MODULE_DESCRIPTION("PowerMac BMAC ethernet driver.");
1702MODULE_LICENSE("GPL");
1703
1704module_init(bmac_init);
1705module_exit(bmac_exit);