1 /* $Id: sunbmac.c,v 1.30 2002/01/15 06:48:55 davem Exp $
2 * sunbmac.c: Driver for Sparc BigMAC 100baseT ethernet adapters.
4 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
7 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/fcntl.h>
12 #include <linux/interrupt.h>
13 #include <linux/ioport.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/crc32.h>
20 #include <linux/errno.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/skbuff.h>
25 #include <asm/auxio.h>
26 #include <asm/bitops.h>
27 #include <asm/byteorder.h>
29 #include <asm/idprom.h>
31 #include <asm/openprom.h>
32 #include <asm/oplib.h>
33 #include <asm/pgtable.h>
35 #include <asm/system.h>
39 static char version
[] __initdata
=
40 "sunbmac.c:v1.9 11/Sep/99 David S. Miller (davem@redhat.com)\n";
47 #define DP(x) printk x
53 #define DTX(x) printk x
59 #define DIRQ(x) printk x
64 static struct bigmac
*root_bigmac_dev
;
66 #define DEFAULT_JAMSIZE 4 /* Toe jam */
68 #define QEC_RESET_TRIES 200
70 static int qec_global_reset(unsigned long gregs
)
72 int tries
= QEC_RESET_TRIES
;
74 sbus_writel(GLOB_CTRL_RESET
, gregs
+ GLOB_CTRL
);
76 if (sbus_readl(gregs
+ GLOB_CTRL
) & GLOB_CTRL_RESET
) {
84 printk(KERN_ERR
"BigMAC: Cannot reset the QEC.\n");
88 static void qec_init(struct bigmac
*bp
)
90 unsigned long gregs
= bp
->gregs
;
91 struct sbus_dev
*qec_sdev
= bp
->qec_sdev
;
92 u8 bsizes
= bp
->bigmac_bursts
;
95 /* 64byte bursts do not work at the moment, do
96 * not even try to enable them. -DaveM
98 if (bsizes
& DMA_BURST32
)
99 regval
= GLOB_CTRL_B32
;
101 regval
= GLOB_CTRL_B16
;
102 sbus_writel(regval
| GLOB_CTRL_BMODE
, gregs
+ GLOB_CTRL
);
103 sbus_writel(GLOB_PSIZE_2048
, gregs
+ GLOB_PSIZE
);
105 /* All of memsize is given to bigmac. */
106 sbus_writel(qec_sdev
->reg_addrs
[1].reg_size
,
109 /* Half to the transmitter, half to the receiver. */
110 sbus_writel(qec_sdev
->reg_addrs
[1].reg_size
>> 1,
112 sbus_writel(qec_sdev
->reg_addrs
[1].reg_size
>> 1,
116 #define TX_RESET_TRIES 32
117 #define RX_RESET_TRIES 32
119 static void bigmac_tx_reset(unsigned long bregs
)
121 int tries
= TX_RESET_TRIES
;
123 sbus_writel(0, bregs
+ BMAC_TXCFG
);
125 /* The fifo threshold bit is read-only and does
128 while ((sbus_readl(bregs
+ BMAC_TXCFG
) & ~(BIGMAC_TXCFG_FIFO
)) != 0 &&
133 printk(KERN_ERR
"BIGMAC: Transmitter will not reset.\n");
134 printk(KERN_ERR
"BIGMAC: tx_cfg is %08x\n",
135 sbus_readl(bregs
+ BMAC_TXCFG
));
139 static void bigmac_rx_reset(unsigned long bregs
)
141 int tries
= RX_RESET_TRIES
;
143 sbus_writel(0, bregs
+ BMAC_RXCFG
);
144 while (sbus_readl(bregs
+ BMAC_RXCFG
) && --tries
)
148 printk(KERN_ERR
"BIGMAC: Receiver will not reset.\n");
149 printk(KERN_ERR
"BIGMAC: rx_cfg is %08x\n",
150 sbus_readl(bregs
+ BMAC_RXCFG
));
154 /* Reset the transmitter and receiver. */
155 static void bigmac_stop(struct bigmac
*bp
)
157 bigmac_tx_reset(bp
->bregs
);
158 bigmac_rx_reset(bp
->bregs
);
161 static void bigmac_get_counters(struct bigmac
*bp
, unsigned long bregs
)
163 struct net_device_stats
*stats
= &bp
->enet_stats
;
165 stats
->rx_crc_errors
+= sbus_readl(bregs
+ BMAC_RCRCECTR
);
166 sbus_writel(0, bregs
+ BMAC_RCRCECTR
);
168 stats
->rx_frame_errors
+= sbus_readl(bregs
+ BMAC_UNALECTR
);
169 sbus_writel(0, bregs
+ BMAC_UNALECTR
);
171 stats
->rx_length_errors
+= sbus_readl(bregs
+ BMAC_GLECTR
);
172 sbus_writel(0, bregs
+ BMAC_GLECTR
);
174 stats
->tx_aborted_errors
+= sbus_readl(bregs
+ BMAC_EXCTR
);
177 (sbus_readl(bregs
+ BMAC_EXCTR
) +
178 sbus_readl(bregs
+ BMAC_LTCTR
));
179 sbus_writel(0, bregs
+ BMAC_EXCTR
);
180 sbus_writel(0, bregs
+ BMAC_LTCTR
);
183 static void bigmac_clean_rings(struct bigmac
*bp
)
187 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
188 if (bp
->rx_skbs
[i
] != NULL
) {
189 dev_kfree_skb_any(bp
->rx_skbs
[i
]);
190 bp
->rx_skbs
[i
] = NULL
;
194 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
195 if (bp
->tx_skbs
[i
] != NULL
) {
196 dev_kfree_skb_any(bp
->tx_skbs
[i
]);
197 bp
->tx_skbs
[i
] = NULL
;
202 static void bigmac_init_rings(struct bigmac
*bp
, int from_irq
)
204 struct bmac_init_block
*bb
= bp
->bmac_block
;
205 struct net_device
*dev
= bp
->dev
;
206 int i
, gfp_flags
= GFP_KERNEL
;
208 if (from_irq
|| in_interrupt())
209 gfp_flags
= GFP_ATOMIC
;
211 bp
->rx_new
= bp
->rx_old
= bp
->tx_new
= bp
->tx_old
= 0;
213 /* Free any skippy bufs left around in the rings. */
214 bigmac_clean_rings(bp
);
216 /* Now get new skbufs for the receive ring. */
217 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
220 skb
= big_mac_alloc_skb(RX_BUF_ALLOC_SIZE
, gfp_flags
);
224 bp
->rx_skbs
[i
] = skb
;
227 /* Because we reserve afterwards. */
228 skb_put(skb
, ETH_FRAME_LEN
);
229 skb_reserve(skb
, 34);
231 bb
->be_rxd
[i
].rx_addr
=
232 sbus_map_single(bp
->bigmac_sdev
, skb
->data
,
233 RX_BUF_ALLOC_SIZE
- 34,
234 SBUS_DMA_FROMDEVICE
);
235 bb
->be_rxd
[i
].rx_flags
=
236 (RXD_OWN
| ((RX_BUF_ALLOC_SIZE
- 34) & RXD_LENGTH
));
239 for (i
= 0; i
< TX_RING_SIZE
; i
++)
240 bb
->be_txd
[i
].tx_flags
= bb
->be_txd
[i
].tx_addr
= 0;
243 #define MGMT_CLKON (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB|MGMT_PAL_DCLOCK)
244 #define MGMT_CLKOFF (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB)
246 static void idle_transceiver(unsigned long tregs
)
251 sbus_writel(MGMT_CLKOFF
, tregs
+ TCVR_MPAL
);
252 sbus_readl(tregs
+ TCVR_MPAL
);
253 sbus_writel(MGMT_CLKON
, tregs
+ TCVR_MPAL
);
254 sbus_readl(tregs
+ TCVR_MPAL
);
258 static void write_tcvr_bit(struct bigmac
*bp
, unsigned long tregs
, int bit
)
260 if (bp
->tcvr_type
== internal
) {
261 bit
= (bit
& 1) << 3;
262 sbus_writel(bit
| (MGMT_PAL_OENAB
| MGMT_PAL_EXT_MDIO
),
264 sbus_readl(tregs
+ TCVR_MPAL
);
265 sbus_writel(bit
| MGMT_PAL_OENAB
| MGMT_PAL_EXT_MDIO
| MGMT_PAL_DCLOCK
,
267 sbus_readl(tregs
+ TCVR_MPAL
);
268 } else if (bp
->tcvr_type
== external
) {
269 bit
= (bit
& 1) << 2;
270 sbus_writel(bit
| MGMT_PAL_INT_MDIO
| MGMT_PAL_OENAB
,
272 sbus_readl(tregs
+ TCVR_MPAL
);
273 sbus_writel(bit
| MGMT_PAL_INT_MDIO
| MGMT_PAL_OENAB
| MGMT_PAL_DCLOCK
,
275 sbus_readl(tregs
+ TCVR_MPAL
);
277 printk(KERN_ERR
"write_tcvr_bit: No transceiver type known!\n");
281 static int read_tcvr_bit(struct bigmac
*bp
, unsigned long tregs
)
285 if (bp
->tcvr_type
== internal
) {
286 sbus_writel(MGMT_PAL_EXT_MDIO
, tregs
+ TCVR_MPAL
);
287 sbus_readl(tregs
+ TCVR_MPAL
);
288 sbus_writel(MGMT_PAL_EXT_MDIO
| MGMT_PAL_DCLOCK
,
290 sbus_readl(tregs
+ TCVR_MPAL
);
291 retval
= (sbus_readl(tregs
+ TCVR_MPAL
) & MGMT_PAL_INT_MDIO
) >> 3;
292 } else if (bp
->tcvr_type
== external
) {
293 sbus_writel(MGMT_PAL_INT_MDIO
, tregs
+ TCVR_MPAL
);
294 sbus_readl(tregs
+ TCVR_MPAL
);
295 sbus_writel(MGMT_PAL_INT_MDIO
| MGMT_PAL_DCLOCK
, tregs
+ TCVR_MPAL
);
296 sbus_readl(tregs
+ TCVR_MPAL
);
297 retval
= (sbus_readl(tregs
+ TCVR_MPAL
) & MGMT_PAL_EXT_MDIO
) >> 2;
299 printk(KERN_ERR
"read_tcvr_bit: No transceiver type known!\n");
304 static int read_tcvr_bit2(struct bigmac
*bp
, unsigned long tregs
)
308 if (bp
->tcvr_type
== internal
) {
309 sbus_writel(MGMT_PAL_EXT_MDIO
, tregs
+ TCVR_MPAL
);
310 sbus_readl(tregs
+ TCVR_MPAL
);
311 retval
= (sbus_readl(tregs
+ TCVR_MPAL
) & MGMT_PAL_INT_MDIO
) >> 3;
312 sbus_writel(MGMT_PAL_EXT_MDIO
| MGMT_PAL_DCLOCK
, tregs
+ TCVR_MPAL
);
313 sbus_readl(tregs
+ TCVR_MPAL
);
314 } else if (bp
->tcvr_type
== external
) {
315 sbus_writel(MGMT_PAL_INT_MDIO
, tregs
+ TCVR_MPAL
);
316 sbus_readl(tregs
+ TCVR_MPAL
);
317 retval
= (sbus_readl(tregs
+ TCVR_MPAL
) & MGMT_PAL_EXT_MDIO
) >> 2;
318 sbus_writel(MGMT_PAL_INT_MDIO
| MGMT_PAL_DCLOCK
, tregs
+ TCVR_MPAL
);
319 sbus_readl(tregs
+ TCVR_MPAL
);
321 printk(KERN_ERR
"read_tcvr_bit2: No transceiver type known!\n");
326 static void put_tcvr_byte(struct bigmac
*bp
,
333 write_tcvr_bit(bp
, tregs
, ((byte
>> shift
) & 1));
335 } while (shift
>= 0);
338 static void bigmac_tcvr_write(struct bigmac
*bp
, unsigned long tregs
,
339 int reg
, unsigned short val
)
345 switch(bp
->tcvr_type
) {
351 printk(KERN_ERR
"bigmac_tcvr_read: Whoops, no known transceiver type.\n");
355 idle_transceiver(tregs
);
356 write_tcvr_bit(bp
, tregs
, 0);
357 write_tcvr_bit(bp
, tregs
, 1);
358 write_tcvr_bit(bp
, tregs
, 0);
359 write_tcvr_bit(bp
, tregs
, 1);
361 put_tcvr_byte(bp
, tregs
,
362 ((bp
->tcvr_type
== internal
) ?
363 BIGMAC_PHY_INTERNAL
: BIGMAC_PHY_EXTERNAL
));
365 put_tcvr_byte(bp
, tregs
, reg
);
367 write_tcvr_bit(bp
, tregs
, 1);
368 write_tcvr_bit(bp
, tregs
, 0);
372 write_tcvr_bit(bp
, tregs
, (val
>> shift
) & 1);
374 } while (shift
>= 0);
377 static unsigned short bigmac_tcvr_read(struct bigmac
*bp
,
381 unsigned short retval
= 0;
384 switch(bp
->tcvr_type
) {
390 printk(KERN_ERR
"bigmac_tcvr_read: Whoops, no known transceiver type.\n");
394 idle_transceiver(tregs
);
395 write_tcvr_bit(bp
, tregs
, 0);
396 write_tcvr_bit(bp
, tregs
, 1);
397 write_tcvr_bit(bp
, tregs
, 1);
398 write_tcvr_bit(bp
, tregs
, 0);
400 put_tcvr_byte(bp
, tregs
,
401 ((bp
->tcvr_type
== internal
) ?
402 BIGMAC_PHY_INTERNAL
: BIGMAC_PHY_EXTERNAL
));
404 put_tcvr_byte(bp
, tregs
, reg
);
406 if (bp
->tcvr_type
== external
) {
409 (void) read_tcvr_bit2(bp
, tregs
);
410 (void) read_tcvr_bit2(bp
, tregs
);
415 tmp
= read_tcvr_bit2(bp
, tregs
);
416 retval
|= ((tmp
& 1) << shift
);
418 } while (shift
>= 0);
420 (void) read_tcvr_bit2(bp
, tregs
);
421 (void) read_tcvr_bit2(bp
, tregs
);
422 (void) read_tcvr_bit2(bp
, tregs
);
426 (void) read_tcvr_bit(bp
, tregs
);
427 (void) read_tcvr_bit(bp
, tregs
);
432 tmp
= read_tcvr_bit(bp
, tregs
);
433 retval
|= ((tmp
& 1) << shift
);
435 } while (shift
>= 0);
437 (void) read_tcvr_bit(bp
, tregs
);
438 (void) read_tcvr_bit(bp
, tregs
);
439 (void) read_tcvr_bit(bp
, tregs
);
444 static void bigmac_tcvr_init(struct bigmac
*bp
)
446 unsigned long tregs
= bp
->tregs
;
449 idle_transceiver(tregs
);
450 sbus_writel(MGMT_PAL_INT_MDIO
| MGMT_PAL_EXT_MDIO
| MGMT_PAL_DCLOCK
,
452 sbus_readl(tregs
+ TCVR_MPAL
);
454 /* Only the bit for the present transceiver (internal or
455 * external) will stick, set them both and see what stays.
457 sbus_writel(MGMT_PAL_INT_MDIO
| MGMT_PAL_EXT_MDIO
, tregs
+ TCVR_MPAL
);
458 sbus_readl(tregs
+ TCVR_MPAL
);
461 mpal
= sbus_readl(tregs
+ TCVR_MPAL
);
462 if (mpal
& MGMT_PAL_EXT_MDIO
) {
463 bp
->tcvr_type
= external
;
464 sbus_writel(~(TCVR_PAL_EXTLBACK
| TCVR_PAL_MSENSE
| TCVR_PAL_LTENABLE
),
466 sbus_readl(tregs
+ TCVR_TPAL
);
467 } else if (mpal
& MGMT_PAL_INT_MDIO
) {
468 bp
->tcvr_type
= internal
;
469 sbus_writel(~(TCVR_PAL_SERIAL
| TCVR_PAL_EXTLBACK
|
470 TCVR_PAL_MSENSE
| TCVR_PAL_LTENABLE
),
472 sbus_readl(tregs
+ TCVR_TPAL
);
474 printk(KERN_ERR
"BIGMAC: AIEEE, neither internal nor "
475 "external MDIO available!\n");
476 printk(KERN_ERR
"BIGMAC: mgmt_pal[%08x] tcvr_pal[%08x]\n",
477 sbus_readl(tregs
+ TCVR_MPAL
),
478 sbus_readl(tregs
+ TCVR_TPAL
));
482 static int bigmac_init(struct bigmac
*, int);
484 static int try_next_permutation(struct bigmac
*bp
, unsigned long tregs
)
486 if (bp
->sw_bmcr
& BMCR_SPEED100
) {
490 bp
->sw_bmcr
= (BMCR_ISOLATE
| BMCR_PDOWN
| BMCR_LOOPBACK
);
491 bigmac_tcvr_write(bp
, tregs
, BIGMAC_BMCR
, bp
->sw_bmcr
);
492 bp
->sw_bmcr
= (BMCR_RESET
);
493 bigmac_tcvr_write(bp
, tregs
, BIGMAC_BMCR
, bp
->sw_bmcr
);
497 bp
->sw_bmcr
= bigmac_tcvr_read(bp
, tregs
, BIGMAC_BMCR
);
498 if ((bp
->sw_bmcr
& BMCR_RESET
) == 0)
503 printk(KERN_ERR
"%s: PHY reset failed.\n", bp
->dev
->name
);
505 bp
->sw_bmcr
= bigmac_tcvr_read(bp
, tregs
, BIGMAC_BMCR
);
507 /* Now we try 10baseT. */
508 bp
->sw_bmcr
&= ~(BMCR_SPEED100
);
509 bigmac_tcvr_write(bp
, tregs
, BIGMAC_BMCR
, bp
->sw_bmcr
);
513 /* We've tried them all. */
517 static void bigmac_timer(unsigned long data
)
519 struct bigmac
*bp
= (struct bigmac
*) data
;
520 unsigned long tregs
= bp
->tregs
;
521 int restart_timer
= 0;
524 if (bp
->timer_state
== ltrywait
) {
525 bp
->sw_bmsr
= bigmac_tcvr_read(bp
, tregs
, BIGMAC_BMSR
);
526 bp
->sw_bmcr
= bigmac_tcvr_read(bp
, tregs
, BIGMAC_BMCR
);
527 if (bp
->sw_bmsr
& BMSR_LSTATUS
) {
528 printk(KERN_INFO
"%s: Link is now up at %s.\n",
530 (bp
->sw_bmcr
& BMCR_SPEED100
) ?
531 "100baseT" : "10baseT");
532 bp
->timer_state
= asleep
;
535 if (bp
->timer_ticks
>= 4) {
538 ret
= try_next_permutation(bp
, tregs
);
540 printk(KERN_ERR
"%s: Link down, cable problem?\n",
542 ret
= bigmac_init(bp
, 0);
544 printk(KERN_ERR
"%s: Error, cannot re-init the "
545 "BigMAC.\n", bp
->dev
->name
);
556 /* Can't happens.... */
557 printk(KERN_ERR
"%s: Aieee, link timer is asleep but we got one anyways!\n",
561 bp
->timer_state
= asleep
; /* foo on you */
564 if (restart_timer
!= 0) {
565 bp
->bigmac_timer
.expires
= jiffies
+ ((12 * HZ
)/10); /* 1.2 sec. */
566 add_timer(&bp
->bigmac_timer
);
570 /* Well, really we just force the chip into 100baseT then
571 * 10baseT, each time checking for a link status.
573 static void bigmac_begin_auto_negotiation(struct bigmac
*bp
)
575 unsigned long tregs
= bp
->tregs
;
578 /* Grab new software copies of PHY registers. */
579 bp
->sw_bmsr
= bigmac_tcvr_read(bp
, tregs
, BIGMAC_BMSR
);
580 bp
->sw_bmcr
= bigmac_tcvr_read(bp
, tregs
, BIGMAC_BMCR
);
583 bp
->sw_bmcr
= (BMCR_ISOLATE
| BMCR_PDOWN
| BMCR_LOOPBACK
);
584 bigmac_tcvr_write(bp
, tregs
, BIGMAC_BMCR
, bp
->sw_bmcr
);
585 bp
->sw_bmcr
= (BMCR_RESET
);
586 bigmac_tcvr_write(bp
, tregs
, BIGMAC_BMCR
, bp
->sw_bmcr
);
590 bp
->sw_bmcr
= bigmac_tcvr_read(bp
, tregs
, BIGMAC_BMCR
);
591 if ((bp
->sw_bmcr
& BMCR_RESET
) == 0)
596 printk(KERN_ERR
"%s: PHY reset failed.\n", bp
->dev
->name
);
598 bp
->sw_bmcr
= bigmac_tcvr_read(bp
, tregs
, BIGMAC_BMCR
);
600 /* First we try 100baseT. */
601 bp
->sw_bmcr
|= BMCR_SPEED100
;
602 bigmac_tcvr_write(bp
, tregs
, BIGMAC_BMCR
, bp
->sw_bmcr
);
604 bp
->timer_state
= ltrywait
;
606 bp
->bigmac_timer
.expires
= jiffies
+ (12 * HZ
) / 10;
607 bp
->bigmac_timer
.data
= (unsigned long) bp
;
608 bp
->bigmac_timer
.function
= &bigmac_timer
;
609 add_timer(&bp
->bigmac_timer
);
612 static int bigmac_init(struct bigmac
*bp
, int from_irq
)
614 unsigned long gregs
= bp
->gregs
;
615 unsigned long cregs
= bp
->creg
;
616 unsigned long bregs
= bp
->bregs
;
617 unsigned char *e
= &bp
->dev
->dev_addr
[0];
619 /* Latch current counters into statistics. */
620 bigmac_get_counters(bp
, bregs
);
623 qec_global_reset(gregs
);
628 /* Alloc and reset the tx/rx descriptor chains. */
629 bigmac_init_rings(bp
, from_irq
);
631 /* Initialize the PHY. */
632 bigmac_tcvr_init(bp
);
634 /* Stop transmitter and receiver. */
637 /* Set hardware ethernet address. */
638 sbus_writel(((e
[4] << 8) | e
[5]), bregs
+ BMAC_MACADDR2
);
639 sbus_writel(((e
[2] << 8) | e
[3]), bregs
+ BMAC_MACADDR1
);
640 sbus_writel(((e
[0] << 8) | e
[1]), bregs
+ BMAC_MACADDR0
);
642 /* Clear the hash table until mc upload occurs. */
643 sbus_writel(0, bregs
+ BMAC_HTABLE3
);
644 sbus_writel(0, bregs
+ BMAC_HTABLE2
);
645 sbus_writel(0, bregs
+ BMAC_HTABLE1
);
646 sbus_writel(0, bregs
+ BMAC_HTABLE0
);
648 /* Enable Big Mac hash table filter. */
649 sbus_writel(BIGMAC_RXCFG_HENABLE
| BIGMAC_RXCFG_FIFO
,
653 /* Ok, configure the Big Mac transmitter. */
654 sbus_writel(BIGMAC_TXCFG_FIFO
, bregs
+ BMAC_TXCFG
);
656 /* The HME docs recommend to use the 10LSB of our MAC here. */
657 sbus_writel(((e
[5] | e
[4] << 8) & 0x3ff),
660 /* Enable the output drivers no matter what. */
661 sbus_writel(BIGMAC_XCFG_ODENABLE
| BIGMAC_XCFG_RESV
,
662 bregs
+ BMAC_XIFCFG
);
664 /* Tell the QEC where the ring descriptors are. */
665 sbus_writel(bp
->bblock_dvma
+ bib_offset(be_rxd
, 0),
667 sbus_writel(bp
->bblock_dvma
+ bib_offset(be_txd
, 0),
670 /* Setup the FIFO pointers into QEC local memory. */
671 sbus_writel(0, cregs
+ CREG_RXRBUFPTR
);
672 sbus_writel(0, cregs
+ CREG_RXWBUFPTR
);
673 sbus_writel(sbus_readl(gregs
+ GLOB_RSIZE
),
674 cregs
+ CREG_TXRBUFPTR
);
675 sbus_writel(sbus_readl(gregs
+ GLOB_RSIZE
),
676 cregs
+ CREG_TXWBUFPTR
);
678 /* Tell bigmac what interrupts we don't want to hear about. */
679 sbus_writel(BIGMAC_IMASK_GOTFRAME
| BIGMAC_IMASK_SENTFRAME
,
682 /* Enable the various other irq's. */
683 sbus_writel(0, cregs
+ CREG_RIMASK
);
684 sbus_writel(0, cregs
+ CREG_TIMASK
);
685 sbus_writel(0, cregs
+ CREG_QMASK
);
686 sbus_writel(0, cregs
+ CREG_BMASK
);
688 /* Set jam size to a reasonable default. */
689 sbus_writel(DEFAULT_JAMSIZE
, bregs
+ BMAC_JSIZE
);
691 /* Clear collision counter. */
692 sbus_writel(0, cregs
+ CREG_CCNT
);
694 /* Enable transmitter and receiver. */
695 sbus_writel(sbus_readl(bregs
+ BMAC_TXCFG
) | BIGMAC_TXCFG_ENABLE
,
697 sbus_writel(sbus_readl(bregs
+ BMAC_RXCFG
) | BIGMAC_RXCFG_ENABLE
,
700 /* Ok, start detecting link speed/duplex. */
701 bigmac_begin_auto_negotiation(bp
);
707 /* Error interrupts get sent here. */
708 static void bigmac_is_medium_rare(struct bigmac
*bp
, u32 qec_status
, u32 bmac_status
)
710 printk(KERN_ERR
"bigmac_is_medium_rare: ");
711 if (qec_status
& (GLOB_STAT_ER
| GLOB_STAT_BM
)) {
712 if (qec_status
& GLOB_STAT_ER
)
713 printk("QEC_ERROR, ");
714 if (qec_status
& GLOB_STAT_BM
)
715 printk("QEC_BMAC_ERROR, ");
717 if (bmac_status
& CREG_STAT_ERRORS
) {
718 if (bmac_status
& CREG_STAT_BERROR
)
719 printk("BMAC_ERROR, ");
720 if (bmac_status
& CREG_STAT_TXDERROR
)
721 printk("TXD_ERROR, ");
722 if (bmac_status
& CREG_STAT_TXLERR
)
723 printk("TX_LATE_ERROR, ");
724 if (bmac_status
& CREG_STAT_TXPERR
)
725 printk("TX_PARITY_ERROR, ");
726 if (bmac_status
& CREG_STAT_TXSERR
)
727 printk("TX_SBUS_ERROR, ");
729 if (bmac_status
& CREG_STAT_RXDROP
)
730 printk("RX_DROP_ERROR, ");
732 if (bmac_status
& CREG_STAT_RXSMALL
)
733 printk("RX_SMALL_ERROR, ");
734 if (bmac_status
& CREG_STAT_RXLERR
)
735 printk("RX_LATE_ERROR, ");
736 if (bmac_status
& CREG_STAT_RXPERR
)
737 printk("RX_PARITY_ERROR, ");
738 if (bmac_status
& CREG_STAT_RXSERR
)
739 printk("RX_SBUS_ERROR, ");
746 /* BigMAC transmit complete service routines. */
747 static void bigmac_tx(struct bigmac
*bp
)
749 struct be_txd
*txbase
= &bp
->bmac_block
->be_txd
[0];
750 struct net_device
*dev
= bp
->dev
;
753 spin_lock(&bp
->lock
);
756 DTX(("bigmac_tx: tx_old[%d] ", elem
));
757 while (elem
!= bp
->tx_new
) {
759 struct be_txd
*this = &txbase
[elem
];
761 DTX(("this(%p) [flags(%08x)addr(%08x)]",
762 this, this->tx_flags
, this->tx_addr
));
764 if (this->tx_flags
& TXD_OWN
)
766 skb
= bp
->tx_skbs
[elem
];
767 bp
->enet_stats
.tx_packets
++;
768 bp
->enet_stats
.tx_bytes
+= skb
->len
;
769 sbus_unmap_single(bp
->bigmac_sdev
,
770 this->tx_addr
, skb
->len
,
773 DTX(("skb(%p) ", skb
));
774 bp
->tx_skbs
[elem
] = NULL
;
775 dev_kfree_skb_irq(skb
);
777 elem
= NEXT_TX(elem
);
779 DTX((" DONE, tx_old=%d\n", elem
));
782 if (netif_queue_stopped(dev
) &&
783 TX_BUFFS_AVAIL(bp
) > 0)
784 netif_wake_queue(bp
->dev
);
786 spin_unlock(&bp
->lock
);
789 /* BigMAC receive complete service routines. */
790 static void bigmac_rx(struct bigmac
*bp
)
792 struct be_rxd
*rxbase
= &bp
->bmac_block
->be_rxd
[0];
794 int elem
= bp
->rx_new
, drops
= 0;
797 this = &rxbase
[elem
];
798 while (!((flags
= this->rx_flags
) & RXD_OWN
)) {
800 int len
= (flags
& RXD_LENGTH
); /* FCS not included */
802 /* Check for errors. */
803 if (len
< ETH_ZLEN
) {
804 bp
->enet_stats
.rx_errors
++;
805 bp
->enet_stats
.rx_length_errors
++;
808 /* Return it to the BigMAC. */
809 bp
->enet_stats
.rx_dropped
++;
811 (RXD_OWN
| ((RX_BUF_ALLOC_SIZE
- 34) & RXD_LENGTH
));
814 skb
= bp
->rx_skbs
[elem
];
815 if (len
> RX_COPY_THRESHOLD
) {
816 struct sk_buff
*new_skb
;
818 /* Now refill the entry, if we can. */
819 new_skb
= big_mac_alloc_skb(RX_BUF_ALLOC_SIZE
, GFP_ATOMIC
);
820 if (new_skb
== NULL
) {
824 sbus_unmap_single(bp
->bigmac_sdev
,
826 RX_BUF_ALLOC_SIZE
- 34,
827 SBUS_DMA_FROMDEVICE
);
828 bp
->rx_skbs
[elem
] = new_skb
;
829 new_skb
->dev
= bp
->dev
;
830 skb_put(new_skb
, ETH_FRAME_LEN
);
831 skb_reserve(new_skb
, 34);
832 this->rx_addr
= sbus_map_single(bp
->bigmac_sdev
,
834 RX_BUF_ALLOC_SIZE
- 34,
835 SBUS_DMA_FROMDEVICE
);
837 (RXD_OWN
| ((RX_BUF_ALLOC_SIZE
- 34) & RXD_LENGTH
));
839 /* Trim the original skb for the netif. */
842 struct sk_buff
*copy_skb
= dev_alloc_skb(len
+ 2);
844 if (copy_skb
== NULL
) {
848 copy_skb
->dev
= bp
->dev
;
849 skb_reserve(copy_skb
, 2);
850 skb_put(copy_skb
, len
);
851 sbus_dma_sync_single(bp
->bigmac_sdev
,
852 this->rx_addr
, len
, SBUS_DMA_FROMDEVICE
);
853 eth_copy_and_sum(copy_skb
, (unsigned char *)skb
->data
, len
, 0);
855 /* Reuse original ring buffer. */
857 (RXD_OWN
| ((RX_BUF_ALLOC_SIZE
- 34) & RXD_LENGTH
));
862 /* No checksums done by the BigMAC ;-( */
863 skb
->protocol
= eth_type_trans(skb
, bp
->dev
);
865 bp
->dev
->last_rx
= jiffies
;
866 bp
->enet_stats
.rx_packets
++;
867 bp
->enet_stats
.rx_bytes
+= len
;
869 elem
= NEXT_RX(elem
);
870 this = &rxbase
[elem
];
874 printk(KERN_NOTICE
"%s: Memory squeeze, deferring packet.\n", bp
->dev
->name
);
877 static irqreturn_t
bigmac_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
879 struct bigmac
*bp
= (struct bigmac
*) dev_id
;
880 u32 qec_status
, bmac_status
;
882 DIRQ(("bigmac_interrupt: "));
884 /* Latch status registers now. */
885 bmac_status
= sbus_readl(bp
->creg
+ CREG_STAT
);
886 qec_status
= sbus_readl(bp
->gregs
+ GLOB_STAT
);
888 DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status
, bmac_status
));
889 if ((qec_status
& (GLOB_STAT_ER
| GLOB_STAT_BM
)) ||
890 (bmac_status
& CREG_STAT_ERRORS
))
891 bigmac_is_medium_rare(bp
, qec_status
, bmac_status
);
893 if (bmac_status
& CREG_STAT_TXIRQ
)
896 if (bmac_status
& CREG_STAT_RXIRQ
)
902 static int bigmac_open(struct net_device
*dev
)
904 struct bigmac
*bp
= (struct bigmac
*) dev
->priv
;
907 ret
= request_irq(dev
->irq
, &bigmac_interrupt
, SA_SHIRQ
, dev
->name
, bp
);
909 printk(KERN_ERR
"BIGMAC: Can't order irq %d to go.\n", dev
->irq
);
912 init_timer(&bp
->bigmac_timer
);
913 ret
= bigmac_init(bp
, 0);
915 free_irq(dev
->irq
, bp
);
919 static int bigmac_close(struct net_device
*dev
)
921 struct bigmac
*bp
= (struct bigmac
*) dev
->priv
;
923 del_timer(&bp
->bigmac_timer
);
924 bp
->timer_state
= asleep
;
928 bigmac_clean_rings(bp
);
929 free_irq(dev
->irq
, bp
);
933 static void bigmac_tx_timeout(struct net_device
*dev
)
935 struct bigmac
*bp
= (struct bigmac
*) dev
->priv
;
938 netif_wake_queue(dev
);
941 /* Put a packet on the wire. */
942 static int bigmac_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
944 struct bigmac
*bp
= (struct bigmac
*) dev
->priv
;
949 mapping
= sbus_map_single(bp
->bigmac_sdev
, skb
->data
, len
, SBUS_DMA_TODEVICE
);
951 /* Avoid a race... */
952 spin_lock_irq(&bp
->lock
);
954 DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len
, entry
));
955 bp
->bmac_block
->be_txd
[entry
].tx_flags
= TXD_UPDATE
;
956 bp
->tx_skbs
[entry
] = skb
;
957 bp
->bmac_block
->be_txd
[entry
].tx_addr
= mapping
;
958 bp
->bmac_block
->be_txd
[entry
].tx_flags
=
959 (TXD_OWN
| TXD_SOP
| TXD_EOP
| (len
& TXD_LENGTH
));
960 bp
->tx_new
= NEXT_TX(entry
);
961 if (TX_BUFFS_AVAIL(bp
) <= 0)
962 netif_stop_queue(dev
);
963 spin_unlock_irq(&bp
->lock
);
966 sbus_writel(CREG_CTRL_TWAKEUP
, bp
->creg
+ CREG_CTRL
);
969 dev
->trans_start
= jiffies
;
974 static struct net_device_stats
*bigmac_get_stats(struct net_device
*dev
)
976 struct bigmac
*bp
= (struct bigmac
*) dev
->priv
;
978 bigmac_get_counters(bp
, bp
->bregs
);
979 return &bp
->enet_stats
;
982 static void bigmac_set_multicast(struct net_device
*dev
)
984 struct bigmac
*bp
= (struct bigmac
*) dev
->priv
;
985 unsigned long bregs
= bp
->bregs
;
986 struct dev_mc_list
*dmi
= dev
->mc_list
;
991 /* Disable the receiver. The bit self-clears when
992 * the operation is complete.
994 tmp
= sbus_readl(bregs
+ BMAC_RXCFG
);
995 tmp
&= ~(BIGMAC_RXCFG_ENABLE
);
996 sbus_writel(tmp
, bregs
+ BMAC_RXCFG
);
997 while ((sbus_readl(bregs
+ BMAC_RXCFG
) & BIGMAC_RXCFG_ENABLE
) != 0)
1000 if ((dev
->flags
& IFF_ALLMULTI
) || (dev
->mc_count
> 64)) {
1001 sbus_writel(0xffff, bregs
+ BMAC_HTABLE0
);
1002 sbus_writel(0xffff, bregs
+ BMAC_HTABLE1
);
1003 sbus_writel(0xffff, bregs
+ BMAC_HTABLE2
);
1004 sbus_writel(0xffff, bregs
+ BMAC_HTABLE3
);
1005 } else if (dev
->flags
& IFF_PROMISC
) {
1006 tmp
= sbus_readl(bregs
+ BMAC_RXCFG
);
1007 tmp
|= BIGMAC_RXCFG_PMISC
;
1008 sbus_writel(tmp
, bregs
+ BMAC_RXCFG
);
1012 for (i
= 0; i
< 4; i
++)
1015 for (i
= 0; i
< dev
->mc_count
; i
++) {
1016 addrs
= dmi
->dmi_addr
;
1022 crc
= ether_crc_le(6, addrs
);
1024 hash_table
[crc
>> 4] |= 1 << (crc
& 0xf);
1026 sbus_writel(hash_table
[0], bregs
+ BMAC_HTABLE0
);
1027 sbus_writel(hash_table
[1], bregs
+ BMAC_HTABLE1
);
1028 sbus_writel(hash_table
[2], bregs
+ BMAC_HTABLE2
);
1029 sbus_writel(hash_table
[3], bregs
+ BMAC_HTABLE3
);
1032 /* Re-enable the receiver. */
1033 tmp
= sbus_readl(bregs
+ BMAC_RXCFG
);
1034 tmp
|= BIGMAC_RXCFG_ENABLE
;
1035 sbus_writel(tmp
, bregs
+ BMAC_RXCFG
);
1038 static int __init
bigmac_ether_init(struct sbus_dev
*qec_sdev
)
1040 struct net_device
*dev
;
1041 static int version_printed
;
1043 u8 bsizes
, bsizes_more
;
1046 /* Get a new device struct for this interface. */
1047 dev
= init_etherdev(NULL
, sizeof(struct bigmac
));
1050 SET_MODULE_OWNER(dev
);
1052 if (version_printed
++ == 0)
1053 printk(KERN_INFO
"%s", version
);
1055 /* Report what we have found to the user. */
1056 printk(KERN_INFO
"%s: BigMAC 100baseT Ethernet ", dev
->name
);
1057 dev
->base_addr
= (long) qec_sdev
;
1058 for (i
= 0; i
< 6; i
++)
1059 printk("%2.2x%c", dev
->dev_addr
[i
] = idprom
->id_ethaddr
[i
],
1060 i
== 5 ? ' ' : ':');
1063 /* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
1065 bp
->qec_sdev
= qec_sdev
;
1066 bp
->bigmac_sdev
= qec_sdev
->child
;
1068 spin_lock_init(&bp
->lock
);
1070 /* Verify the registers we expect, are actually there. */
1071 if ((bp
->bigmac_sdev
->num_registers
!= 3) ||
1072 (bp
->qec_sdev
->num_registers
!= 2)) {
1073 printk(KERN_ERR
"BIGMAC: Device does not have 2 and 3 regs, it has %d and %d.\n",
1074 bp
->qec_sdev
->num_registers
,
1075 bp
->bigmac_sdev
->num_registers
);
1076 printk(KERN_ERR
"BIGMAC: Would you like that for here or to go?\n");
1077 goto fail_and_cleanup
;
1080 /* Map in QEC global control registers. */
1081 bp
->gregs
= sbus_ioremap(&bp
->qec_sdev
->resource
[0], 0,
1082 GLOB_REG_SIZE
, "BigMAC QEC GLobal Regs");
1084 printk(KERN_ERR
"BIGMAC: Cannot map QEC global registers.\n");
1085 goto fail_and_cleanup
;
1088 /* Make sure QEC is in BigMAC mode. */
1089 if ((sbus_readl(bp
->gregs
+ GLOB_CTRL
) & 0xf0000000) != GLOB_CTRL_BMODE
) {
1090 printk(KERN_ERR
"BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
1091 goto fail_and_cleanup
;
1094 /* Reset the QEC. */
1095 if (qec_global_reset(bp
->gregs
))
1096 goto fail_and_cleanup
;
1098 /* Get supported SBUS burst sizes. */
1099 bsizes
= prom_getintdefault(bp
->qec_sdev
->prom_node
,
1103 bsizes_more
= prom_getintdefault(bp
->qec_sdev
->bus
->prom_node
,
1108 if (bsizes_more
!= 0xff)
1109 bsizes
&= bsizes_more
;
1110 if (bsizes
== 0xff || (bsizes
& DMA_BURST16
) == 0 ||
1111 (bsizes
& DMA_BURST32
) == 0)
1112 bsizes
= (DMA_BURST32
- 1);
1113 bp
->bigmac_bursts
= bsizes
;
1115 /* Perform QEC initialization. */
1118 /* Map in the BigMAC channel registers. */
1119 bp
->creg
= sbus_ioremap(&bp
->bigmac_sdev
->resource
[0], 0,
1120 CREG_REG_SIZE
, "BigMAC QEC Channel Regs");
1122 printk(KERN_ERR
"BIGMAC: Cannot map QEC channel registers.\n");
1123 goto fail_and_cleanup
;
1126 /* Map in the BigMAC control registers. */
1127 bp
->bregs
= sbus_ioremap(&bp
->bigmac_sdev
->resource
[1], 0,
1128 BMAC_REG_SIZE
, "BigMAC Primary Regs");
1130 printk(KERN_ERR
"BIGMAC: Cannot map BigMAC primary registers.\n");
1131 goto fail_and_cleanup
;
1134 /* Map in the BigMAC transceiver registers, this is how you poke at
1137 bp
->tregs
= sbus_ioremap(&bp
->bigmac_sdev
->resource
[2], 0,
1138 TCVR_REG_SIZE
, "BigMAC Transceiver Regs");
1140 printk(KERN_ERR
"BIGMAC: Cannot map BigMAC transceiver registers.\n");
1141 goto fail_and_cleanup
;
1144 /* Stop the BigMAC. */
1147 /* Allocate transmit/receive descriptor DVMA block. */
1148 bp
->bmac_block
= sbus_alloc_consistent(bp
->bigmac_sdev
,
1151 if (bp
->bmac_block
== NULL
|| bp
->bblock_dvma
== 0) {
1152 printk(KERN_ERR
"BIGMAC: Cannot allocate consistent DMA.\n");
1153 goto fail_and_cleanup
;
1156 /* Get the board revision of this BigMAC. */
1157 bp
->board_rev
= prom_getintdefault(bp
->bigmac_sdev
->prom_node
,
1158 "board-version", 1);
1160 /* Init auto-negotiation timer state. */
1161 init_timer(&bp
->bigmac_timer
);
1162 bp
->timer_state
= asleep
;
1163 bp
->timer_ticks
= 0;
1165 /* Backlink to generic net device struct. */
1168 /* Set links to our BigMAC open and close routines. */
1169 dev
->open
= &bigmac_open
;
1170 dev
->stop
= &bigmac_close
;
1171 dev
->hard_start_xmit
= &bigmac_start_xmit
;
1173 /* Set links to BigMAC statistic and multi-cast loading code. */
1174 dev
->get_stats
= &bigmac_get_stats
;
1175 dev
->set_multicast_list
= &bigmac_set_multicast
;
1177 dev
->tx_timeout
= &bigmac_tx_timeout
;
1178 dev
->watchdog_timeo
= 5*HZ
;
1180 /* Finish net device registration. */
1181 dev
->irq
= bp
->bigmac_sdev
->irqs
[0];
1184 /* Put us into the list of instances attached for later driver
1187 bp
->next_module
= root_bigmac_dev
;
1188 root_bigmac_dev
= bp
;
1193 /* Something went wrong, undo whatever we did so far. */
1194 /* Free register mappings if any. */
1196 sbus_iounmap(bp
->gregs
, GLOB_REG_SIZE
);
1198 sbus_iounmap(bp
->creg
, CREG_REG_SIZE
);
1200 sbus_iounmap(bp
->bregs
, BMAC_REG_SIZE
);
1202 sbus_iounmap(bp
->tregs
, TCVR_REG_SIZE
);
1205 sbus_free_consistent(bp
->bigmac_sdev
,
1210 unregister_netdev(dev
);
1211 /* This also frees the co-located 'dev->priv' */
1216 /* QEC can be the parent of either QuadEthernet or
1217 * a BigMAC. We want the latter.
1219 static int __init
bigmac_match(struct sbus_dev
*sdev
)
1221 struct sbus_dev
*child
= sdev
->child
;
1223 if (strcmp(sdev
->prom_name
, "qec") != 0)
1229 if (strcmp(child
->prom_name
, "be") != 0)
1235 static int __init
bigmac_probe(void)
1237 struct sbus_bus
*sbus
;
1238 struct sbus_dev
*sdev
= 0;
1242 root_bigmac_dev
= NULL
;
1248 for_each_sbus(sbus
) {
1249 for_each_sbusdev(sdev
, sbus
) {
1250 if (bigmac_match(sdev
)) {
1252 if ((v
= bigmac_ether_init(sdev
)))
1262 static void __exit
bigmac_cleanup(void)
1264 while (root_bigmac_dev
) {
1265 struct bigmac
*bp
= root_bigmac_dev
;
1266 struct bigmac
*bp_nxt
= root_bigmac_dev
->next_module
;
1268 sbus_iounmap(bp
->gregs
, GLOB_REG_SIZE
);
1269 sbus_iounmap(bp
->creg
, CREG_REG_SIZE
);
1270 sbus_iounmap(bp
->bregs
, BMAC_REG_SIZE
);
1271 sbus_iounmap(bp
->tregs
, TCVR_REG_SIZE
);
1272 sbus_free_consistent(bp
->bigmac_sdev
,
1277 unregister_netdev(bp
->dev
);
1279 root_bigmac_dev
= bp_nxt
;
1283 module_init(bigmac_probe
);
1284 module_exit(bigmac_cleanup
);
1285 MODULE_LICENSE("GPL");