1 // SPDX-License-Identifier: GPL-2.0
2 /* sunbmac.c: Driver for Sparc BigMAC 100baseT ethernet adapters.
4 * Copyright (C) 1997, 1998, 1999, 2003, 2008 David S. Miller (davem@davemloft.net)
7 #include <linux/module.h>
8 #include <linux/pgtable.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/fcntl.h>
13 #include <linux/interrupt.h>
14 #include <linux/ioport.h>
16 #include <linux/string.h>
17 #include <linux/delay.h>
18 #include <linux/crc32.h>
19 #include <linux/errno.h>
20 #include <linux/ethtool.h>
21 #include <linux/mii.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/bitops.h>
26 #include <linux/dma-mapping.h>
28 #include <linux/of_device.h>
29 #include <linux/gfp.h>
31 #include <asm/auxio.h>
32 #include <asm/byteorder.h>
34 #include <asm/idprom.h>
36 #include <asm/openprom.h>
37 #include <asm/oplib.h>
41 #define DRV_NAME "sunbmac"
42 #define DRV_VERSION "2.1"
43 #define DRV_RELDATE "August 26, 2008"
44 #define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
46 static char version[] =
47 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
49 MODULE_VERSION(DRV_VERSION);
50 MODULE_AUTHOR(DRV_AUTHOR);
51 MODULE_DESCRIPTION("Sun BigMAC 100baseT ethernet driver");
52 MODULE_LICENSE("GPL");
59 #define DP(x) printk x
65 #define DTX(x) printk x
71 #define DIRQ(x) printk x
76 #define DEFAULT_JAMSIZE 4 /* Toe jam */
78 #define QEC_RESET_TRIES 200
80 static int qec_global_reset(void __iomem *gregs)
82 int tries = QEC_RESET_TRIES;
84 sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
86 if (sbus_readl(gregs + GLOB_CTRL) & GLOB_CTRL_RESET) {
94 printk(KERN_ERR "BigMAC: Cannot reset the QEC.\n");
98 static void qec_init(struct bigmac *bp)
100 struct platform_device *qec_op = bp->qec_op;
101 void __iomem *gregs = bp->gregs;
102 u8 bsizes = bp->bigmac_bursts;
105 /* 64byte bursts do not work at the moment, do
106 * not even try to enable them. -DaveM
108 if (bsizes & DMA_BURST32)
109 regval = GLOB_CTRL_B32;
111 regval = GLOB_CTRL_B16;
112 sbus_writel(regval | GLOB_CTRL_BMODE, gregs + GLOB_CTRL);
113 sbus_writel(GLOB_PSIZE_2048, gregs + GLOB_PSIZE);
115 /* All of memsize is given to bigmac. */
116 sbus_writel(resource_size(&qec_op->resource[1]),
119 /* Half to the transmitter, half to the receiver. */
120 sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
122 sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
126 #define TX_RESET_TRIES 32
127 #define RX_RESET_TRIES 32
129 static void bigmac_tx_reset(void __iomem *bregs)
131 int tries = TX_RESET_TRIES;
133 sbus_writel(0, bregs + BMAC_TXCFG);
135 /* The fifo threshold bit is read-only and does
138 while ((sbus_readl(bregs + BMAC_TXCFG) & ~(BIGMAC_TXCFG_FIFO)) != 0 &&
143 printk(KERN_ERR "BIGMAC: Transmitter will not reset.\n");
144 printk(KERN_ERR "BIGMAC: tx_cfg is %08x\n",
145 sbus_readl(bregs + BMAC_TXCFG));
149 static void bigmac_rx_reset(void __iomem *bregs)
151 int tries = RX_RESET_TRIES;
153 sbus_writel(0, bregs + BMAC_RXCFG);
154 while (sbus_readl(bregs + BMAC_RXCFG) && --tries)
158 printk(KERN_ERR "BIGMAC: Receiver will not reset.\n");
159 printk(KERN_ERR "BIGMAC: rx_cfg is %08x\n",
160 sbus_readl(bregs + BMAC_RXCFG));
164 /* Reset the transmitter and receiver. */
165 static void bigmac_stop(struct bigmac *bp)
167 bigmac_tx_reset(bp->bregs);
168 bigmac_rx_reset(bp->bregs);
171 static void bigmac_get_counters(struct bigmac *bp, void __iomem *bregs)
173 struct net_device_stats *stats = &bp->dev->stats;
175 stats->rx_crc_errors += sbus_readl(bregs + BMAC_RCRCECTR);
176 sbus_writel(0, bregs + BMAC_RCRCECTR);
178 stats->rx_frame_errors += sbus_readl(bregs + BMAC_UNALECTR);
179 sbus_writel(0, bregs + BMAC_UNALECTR);
181 stats->rx_length_errors += sbus_readl(bregs + BMAC_GLECTR);
182 sbus_writel(0, bregs + BMAC_GLECTR);
184 stats->tx_aborted_errors += sbus_readl(bregs + BMAC_EXCTR);
187 (sbus_readl(bregs + BMAC_EXCTR) +
188 sbus_readl(bregs + BMAC_LTCTR));
189 sbus_writel(0, bregs + BMAC_EXCTR);
190 sbus_writel(0, bregs + BMAC_LTCTR);
193 static void bigmac_clean_rings(struct bigmac *bp)
197 for (i = 0; i < RX_RING_SIZE; i++) {
198 if (bp->rx_skbs[i] != NULL) {
199 dev_kfree_skb_any(bp->rx_skbs[i]);
200 bp->rx_skbs[i] = NULL;
204 for (i = 0; i < TX_RING_SIZE; i++) {
205 if (bp->tx_skbs[i] != NULL) {
206 dev_kfree_skb_any(bp->tx_skbs[i]);
207 bp->tx_skbs[i] = NULL;
212 static void bigmac_init_rings(struct bigmac *bp, bool non_blocking)
214 struct bmac_init_block *bb = bp->bmac_block;
216 gfp_t gfp_flags = GFP_KERNEL;
219 gfp_flags = GFP_ATOMIC;
221 bp->rx_new = bp->rx_old = bp->tx_new = bp->tx_old = 0;
223 /* Free any skippy bufs left around in the rings. */
224 bigmac_clean_rings(bp);
226 /* Now get new skbufs for the receive ring. */
227 for (i = 0; i < RX_RING_SIZE; i++) {
230 skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, gfp_flags);
234 bp->rx_skbs[i] = skb;
236 /* Because we reserve afterwards. */
237 skb_put(skb, ETH_FRAME_LEN);
238 skb_reserve(skb, 34);
240 bb->be_rxd[i].rx_addr =
241 dma_map_single(&bp->bigmac_op->dev,
243 RX_BUF_ALLOC_SIZE - 34,
245 bb->be_rxd[i].rx_flags =
246 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
249 for (i = 0; i < TX_RING_SIZE; i++)
250 bb->be_txd[i].tx_flags = bb->be_txd[i].tx_addr = 0;
253 #define MGMT_CLKON (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB|MGMT_PAL_DCLOCK)
254 #define MGMT_CLKOFF (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB)
256 static void idle_transceiver(void __iomem *tregs)
261 sbus_writel(MGMT_CLKOFF, tregs + TCVR_MPAL);
262 sbus_readl(tregs + TCVR_MPAL);
263 sbus_writel(MGMT_CLKON, tregs + TCVR_MPAL);
264 sbus_readl(tregs + TCVR_MPAL);
268 static void write_tcvr_bit(struct bigmac *bp, void __iomem *tregs, int bit)
270 if (bp->tcvr_type == internal) {
271 bit = (bit & 1) << 3;
272 sbus_writel(bit | (MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO),
274 sbus_readl(tregs + TCVR_MPAL);
275 sbus_writel(bit | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
277 sbus_readl(tregs + TCVR_MPAL);
278 } else if (bp->tcvr_type == external) {
279 bit = (bit & 1) << 2;
280 sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB,
282 sbus_readl(tregs + TCVR_MPAL);
283 sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB | MGMT_PAL_DCLOCK,
285 sbus_readl(tregs + TCVR_MPAL);
287 printk(KERN_ERR "write_tcvr_bit: No transceiver type known!\n");
291 static int read_tcvr_bit(struct bigmac *bp, void __iomem *tregs)
295 if (bp->tcvr_type == internal) {
296 sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
297 sbus_readl(tregs + TCVR_MPAL);
298 sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
300 sbus_readl(tregs + TCVR_MPAL);
301 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
302 } else if (bp->tcvr_type == external) {
303 sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
304 sbus_readl(tregs + TCVR_MPAL);
305 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
306 sbus_readl(tregs + TCVR_MPAL);
307 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
309 printk(KERN_ERR "read_tcvr_bit: No transceiver type known!\n");
314 static int read_tcvr_bit2(struct bigmac *bp, void __iomem *tregs)
318 if (bp->tcvr_type == internal) {
319 sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
320 sbus_readl(tregs + TCVR_MPAL);
321 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
322 sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
323 sbus_readl(tregs + TCVR_MPAL);
324 } else if (bp->tcvr_type == external) {
325 sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
326 sbus_readl(tregs + TCVR_MPAL);
327 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
328 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
329 sbus_readl(tregs + TCVR_MPAL);
331 printk(KERN_ERR "read_tcvr_bit2: No transceiver type known!\n");
336 static void put_tcvr_byte(struct bigmac *bp,
343 write_tcvr_bit(bp, tregs, ((byte >> shift) & 1));
345 } while (shift >= 0);
348 static void bigmac_tcvr_write(struct bigmac *bp, void __iomem *tregs,
349 int reg, unsigned short val)
355 switch(bp->tcvr_type) {
361 printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
365 idle_transceiver(tregs);
366 write_tcvr_bit(bp, tregs, 0);
367 write_tcvr_bit(bp, tregs, 1);
368 write_tcvr_bit(bp, tregs, 0);
369 write_tcvr_bit(bp, tregs, 1);
371 put_tcvr_byte(bp, tregs,
372 ((bp->tcvr_type == internal) ?
373 BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
375 put_tcvr_byte(bp, tregs, reg);
377 write_tcvr_bit(bp, tregs, 1);
378 write_tcvr_bit(bp, tregs, 0);
382 write_tcvr_bit(bp, tregs, (val >> shift) & 1);
384 } while (shift >= 0);
387 static unsigned short bigmac_tcvr_read(struct bigmac *bp,
391 unsigned short retval = 0;
394 switch(bp->tcvr_type) {
400 printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
404 idle_transceiver(tregs);
405 write_tcvr_bit(bp, tregs, 0);
406 write_tcvr_bit(bp, tregs, 1);
407 write_tcvr_bit(bp, tregs, 1);
408 write_tcvr_bit(bp, tregs, 0);
410 put_tcvr_byte(bp, tregs,
411 ((bp->tcvr_type == internal) ?
412 BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
414 put_tcvr_byte(bp, tregs, reg);
416 if (bp->tcvr_type == external) {
419 (void) read_tcvr_bit2(bp, tregs);
420 (void) read_tcvr_bit2(bp, tregs);
425 tmp = read_tcvr_bit2(bp, tregs);
426 retval |= ((tmp & 1) << shift);
428 } while (shift >= 0);
430 (void) read_tcvr_bit2(bp, tregs);
431 (void) read_tcvr_bit2(bp, tregs);
432 (void) read_tcvr_bit2(bp, tregs);
436 (void) read_tcvr_bit(bp, tregs);
437 (void) read_tcvr_bit(bp, tregs);
442 tmp = read_tcvr_bit(bp, tregs);
443 retval |= ((tmp & 1) << shift);
445 } while (shift >= 0);
447 (void) read_tcvr_bit(bp, tregs);
448 (void) read_tcvr_bit(bp, tregs);
449 (void) read_tcvr_bit(bp, tregs);
454 static void bigmac_tcvr_init(struct bigmac *bp)
456 void __iomem *tregs = bp->tregs;
459 idle_transceiver(tregs);
460 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
462 sbus_readl(tregs + TCVR_MPAL);
464 /* Only the bit for the present transceiver (internal or
465 * external) will stick, set them both and see what stays.
467 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
468 sbus_readl(tregs + TCVR_MPAL);
471 mpal = sbus_readl(tregs + TCVR_MPAL);
472 if (mpal & MGMT_PAL_EXT_MDIO) {
473 bp->tcvr_type = external;
474 sbus_writel(~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
476 sbus_readl(tregs + TCVR_TPAL);
477 } else if (mpal & MGMT_PAL_INT_MDIO) {
478 bp->tcvr_type = internal;
479 sbus_writel(~(TCVR_PAL_SERIAL | TCVR_PAL_EXTLBACK |
480 TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
482 sbus_readl(tregs + TCVR_TPAL);
484 printk(KERN_ERR "BIGMAC: AIEEE, neither internal nor "
485 "external MDIO available!\n");
486 printk(KERN_ERR "BIGMAC: mgmt_pal[%08x] tcvr_pal[%08x]\n",
487 sbus_readl(tregs + TCVR_MPAL),
488 sbus_readl(tregs + TCVR_TPAL));
492 static int bigmac_init_hw(struct bigmac *, bool);
494 static int try_next_permutation(struct bigmac *bp, void __iomem *tregs)
496 if (bp->sw_bmcr & BMCR_SPEED100) {
500 bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
501 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
502 bp->sw_bmcr = (BMCR_RESET);
503 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
507 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
508 if ((bp->sw_bmcr & BMCR_RESET) == 0)
513 printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
515 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
517 /* Now we try 10baseT. */
518 bp->sw_bmcr &= ~(BMCR_SPEED100);
519 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
523 /* We've tried them all. */
527 static void bigmac_timer(struct timer_list *t)
529 struct bigmac *bp = from_timer(bp, t, bigmac_timer);
530 void __iomem *tregs = bp->tregs;
531 int restart_timer = 0;
534 if (bp->timer_state == ltrywait) {
535 bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, MII_BMSR);
536 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
537 if (bp->sw_bmsr & BMSR_LSTATUS) {
538 printk(KERN_INFO "%s: Link is now up at %s.\n",
540 (bp->sw_bmcr & BMCR_SPEED100) ?
541 "100baseT" : "10baseT");
542 bp->timer_state = asleep;
545 if (bp->timer_ticks >= 4) {
548 ret = try_next_permutation(bp, tregs);
550 printk(KERN_ERR "%s: Link down, cable problem?\n",
552 ret = bigmac_init_hw(bp, true);
554 printk(KERN_ERR "%s: Error, cannot re-init the "
555 "BigMAC.\n", bp->dev->name);
566 /* Can't happens.... */
567 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
571 bp->timer_state = asleep; /* foo on you */
574 if (restart_timer != 0) {
575 bp->bigmac_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
576 add_timer(&bp->bigmac_timer);
580 /* Well, really we just force the chip into 100baseT then
581 * 10baseT, each time checking for a link status.
583 static void bigmac_begin_auto_negotiation(struct bigmac *bp)
585 void __iomem *tregs = bp->tregs;
588 /* Grab new software copies of PHY registers. */
589 bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, MII_BMSR);
590 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
593 bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
594 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
595 bp->sw_bmcr = (BMCR_RESET);
596 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
600 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
601 if ((bp->sw_bmcr & BMCR_RESET) == 0)
606 printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
608 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
610 /* First we try 100baseT. */
611 bp->sw_bmcr |= BMCR_SPEED100;
612 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
614 bp->timer_state = ltrywait;
616 bp->bigmac_timer.expires = jiffies + (12 * HZ) / 10;
617 add_timer(&bp->bigmac_timer);
620 static int bigmac_init_hw(struct bigmac *bp, bool non_blocking)
622 void __iomem *gregs = bp->gregs;
623 void __iomem *cregs = bp->creg;
624 void __iomem *bregs = bp->bregs;
625 __u32 bblk_dvma = (__u32)bp->bblock_dvma;
626 unsigned char *e = &bp->dev->dev_addr[0];
628 /* Latch current counters into statistics. */
629 bigmac_get_counters(bp, bregs);
632 qec_global_reset(gregs);
637 /* Alloc and reset the tx/rx descriptor chains. */
638 bigmac_init_rings(bp, non_blocking);
640 /* Initialize the PHY. */
641 bigmac_tcvr_init(bp);
643 /* Stop transmitter and receiver. */
646 /* Set hardware ethernet address. */
647 sbus_writel(((e[4] << 8) | e[5]), bregs + BMAC_MACADDR2);
648 sbus_writel(((e[2] << 8) | e[3]), bregs + BMAC_MACADDR1);
649 sbus_writel(((e[0] << 8) | e[1]), bregs + BMAC_MACADDR0);
651 /* Clear the hash table until mc upload occurs. */
652 sbus_writel(0, bregs + BMAC_HTABLE3);
653 sbus_writel(0, bregs + BMAC_HTABLE2);
654 sbus_writel(0, bregs + BMAC_HTABLE1);
655 sbus_writel(0, bregs + BMAC_HTABLE0);
657 /* Enable Big Mac hash table filter. */
658 sbus_writel(BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_FIFO,
662 /* Ok, configure the Big Mac transmitter. */
663 sbus_writel(BIGMAC_TXCFG_FIFO, bregs + BMAC_TXCFG);
665 /* The HME docs recommend to use the 10LSB of our MAC here. */
666 sbus_writel(((e[5] | e[4] << 8) & 0x3ff),
669 /* Enable the output drivers no matter what. */
670 sbus_writel(BIGMAC_XCFG_ODENABLE | BIGMAC_XCFG_RESV,
671 bregs + BMAC_XIFCFG);
673 /* Tell the QEC where the ring descriptors are. */
674 sbus_writel(bblk_dvma + bib_offset(be_rxd, 0),
676 sbus_writel(bblk_dvma + bib_offset(be_txd, 0),
679 /* Setup the FIFO pointers into QEC local memory. */
680 sbus_writel(0, cregs + CREG_RXRBUFPTR);
681 sbus_writel(0, cregs + CREG_RXWBUFPTR);
682 sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
683 cregs + CREG_TXRBUFPTR);
684 sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
685 cregs + CREG_TXWBUFPTR);
687 /* Tell bigmac what interrupts we don't want to hear about. */
688 sbus_writel(BIGMAC_IMASK_GOTFRAME | BIGMAC_IMASK_SENTFRAME,
691 /* Enable the various other irq's. */
692 sbus_writel(0, cregs + CREG_RIMASK);
693 sbus_writel(0, cregs + CREG_TIMASK);
694 sbus_writel(0, cregs + CREG_QMASK);
695 sbus_writel(0, cregs + CREG_BMASK);
697 /* Set jam size to a reasonable default. */
698 sbus_writel(DEFAULT_JAMSIZE, bregs + BMAC_JSIZE);
700 /* Clear collision counter. */
701 sbus_writel(0, cregs + CREG_CCNT);
703 /* Enable transmitter and receiver. */
704 sbus_writel(sbus_readl(bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE,
706 sbus_writel(sbus_readl(bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE,
709 /* Ok, start detecting link speed/duplex. */
710 bigmac_begin_auto_negotiation(bp);
716 /* Error interrupts get sent here. */
717 static void bigmac_is_medium_rare(struct bigmac *bp, u32 qec_status, u32 bmac_status)
719 printk(KERN_ERR "bigmac_is_medium_rare: ");
720 if (qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) {
721 if (qec_status & GLOB_STAT_ER)
722 printk("QEC_ERROR, ");
723 if (qec_status & GLOB_STAT_BM)
724 printk("QEC_BMAC_ERROR, ");
726 if (bmac_status & CREG_STAT_ERRORS) {
727 if (bmac_status & CREG_STAT_BERROR)
728 printk("BMAC_ERROR, ");
729 if (bmac_status & CREG_STAT_TXDERROR)
730 printk("TXD_ERROR, ");
731 if (bmac_status & CREG_STAT_TXLERR)
732 printk("TX_LATE_ERROR, ");
733 if (bmac_status & CREG_STAT_TXPERR)
734 printk("TX_PARITY_ERROR, ");
735 if (bmac_status & CREG_STAT_TXSERR)
736 printk("TX_SBUS_ERROR, ");
738 if (bmac_status & CREG_STAT_RXDROP)
739 printk("RX_DROP_ERROR, ");
741 if (bmac_status & CREG_STAT_RXSMALL)
742 printk("RX_SMALL_ERROR, ");
743 if (bmac_status & CREG_STAT_RXLERR)
744 printk("RX_LATE_ERROR, ");
745 if (bmac_status & CREG_STAT_RXPERR)
746 printk("RX_PARITY_ERROR, ");
747 if (bmac_status & CREG_STAT_RXSERR)
748 printk("RX_SBUS_ERROR, ");
752 bigmac_init_hw(bp, true);
755 /* BigMAC transmit complete service routines. */
756 static void bigmac_tx(struct bigmac *bp)
758 struct be_txd *txbase = &bp->bmac_block->be_txd[0];
759 struct net_device *dev = bp->dev;
762 spin_lock(&bp->lock);
765 DTX(("bigmac_tx: tx_old[%d] ", elem));
766 while (elem != bp->tx_new) {
768 struct be_txd *this = &txbase[elem];
770 DTX(("this(%p) [flags(%08x)addr(%08x)]",
771 this, this->tx_flags, this->tx_addr));
773 if (this->tx_flags & TXD_OWN)
775 skb = bp->tx_skbs[elem];
776 dev->stats.tx_packets++;
777 dev->stats.tx_bytes += skb->len;
778 dma_unmap_single(&bp->bigmac_op->dev,
779 this->tx_addr, skb->len,
782 DTX(("skb(%p) ", skb));
783 bp->tx_skbs[elem] = NULL;
784 dev_consume_skb_irq(skb);
786 elem = NEXT_TX(elem);
788 DTX((" DONE, tx_old=%d\n", elem));
791 if (netif_queue_stopped(dev) &&
792 TX_BUFFS_AVAIL(bp) > 0)
793 netif_wake_queue(bp->dev);
795 spin_unlock(&bp->lock);
798 /* BigMAC receive complete service routines. */
799 static void bigmac_rx(struct bigmac *bp)
801 struct be_rxd *rxbase = &bp->bmac_block->be_rxd[0];
803 int elem = bp->rx_new, drops = 0;
806 this = &rxbase[elem];
807 while (!((flags = this->rx_flags) & RXD_OWN)) {
809 int len = (flags & RXD_LENGTH); /* FCS not included */
811 /* Check for errors. */
812 if (len < ETH_ZLEN) {
813 bp->dev->stats.rx_errors++;
814 bp->dev->stats.rx_length_errors++;
817 /* Return it to the BigMAC. */
818 bp->dev->stats.rx_dropped++;
820 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
823 skb = bp->rx_skbs[elem];
824 if (len > RX_COPY_THRESHOLD) {
825 struct sk_buff *new_skb;
827 /* Now refill the entry, if we can. */
828 new_skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
829 if (new_skb == NULL) {
833 dma_unmap_single(&bp->bigmac_op->dev,
835 RX_BUF_ALLOC_SIZE - 34,
837 bp->rx_skbs[elem] = new_skb;
838 skb_put(new_skb, ETH_FRAME_LEN);
839 skb_reserve(new_skb, 34);
841 dma_map_single(&bp->bigmac_op->dev,
843 RX_BUF_ALLOC_SIZE - 34,
846 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
848 /* Trim the original skb for the netif. */
851 struct sk_buff *copy_skb = netdev_alloc_skb(bp->dev, len + 2);
853 if (copy_skb == NULL) {
857 skb_reserve(copy_skb, 2);
858 skb_put(copy_skb, len);
859 dma_sync_single_for_cpu(&bp->bigmac_op->dev,
862 skb_copy_to_linear_data(copy_skb, (unsigned char *)skb->data, len);
863 dma_sync_single_for_device(&bp->bigmac_op->dev,
867 /* Reuse original ring buffer. */
869 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
874 /* No checksums done by the BigMAC ;-( */
875 skb->protocol = eth_type_trans(skb, bp->dev);
877 bp->dev->stats.rx_packets++;
878 bp->dev->stats.rx_bytes += len;
880 elem = NEXT_RX(elem);
881 this = &rxbase[elem];
885 printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", bp->dev->name);
888 static irqreturn_t bigmac_interrupt(int irq, void *dev_id)
890 struct bigmac *bp = (struct bigmac *) dev_id;
891 u32 qec_status, bmac_status;
893 DIRQ(("bigmac_interrupt: "));
895 /* Latch status registers now. */
896 bmac_status = sbus_readl(bp->creg + CREG_STAT);
897 qec_status = sbus_readl(bp->gregs + GLOB_STAT);
899 DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
900 if ((qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) ||
901 (bmac_status & CREG_STAT_ERRORS))
902 bigmac_is_medium_rare(bp, qec_status, bmac_status);
904 if (bmac_status & CREG_STAT_TXIRQ)
907 if (bmac_status & CREG_STAT_RXIRQ)
913 static int bigmac_open(struct net_device *dev)
915 struct bigmac *bp = netdev_priv(dev);
918 ret = request_irq(dev->irq, bigmac_interrupt, IRQF_SHARED, dev->name, bp);
920 printk(KERN_ERR "BIGMAC: Can't order irq %d to go.\n", dev->irq);
923 timer_setup(&bp->bigmac_timer, bigmac_timer, 0);
924 ret = bigmac_init_hw(bp, false);
926 free_irq(dev->irq, bp);
930 static int bigmac_close(struct net_device *dev)
932 struct bigmac *bp = netdev_priv(dev);
934 del_timer(&bp->bigmac_timer);
935 bp->timer_state = asleep;
939 bigmac_clean_rings(bp);
940 free_irq(dev->irq, bp);
944 static void bigmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
946 struct bigmac *bp = netdev_priv(dev);
948 bigmac_init_hw(bp, true);
949 netif_wake_queue(dev);
952 /* Put a packet on the wire. */
954 bigmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
956 struct bigmac *bp = netdev_priv(dev);
961 mapping = dma_map_single(&bp->bigmac_op->dev, skb->data,
964 /* Avoid a race... */
965 spin_lock_irq(&bp->lock);
967 DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
968 bp->bmac_block->be_txd[entry].tx_flags = TXD_UPDATE;
969 bp->tx_skbs[entry] = skb;
970 bp->bmac_block->be_txd[entry].tx_addr = mapping;
971 bp->bmac_block->be_txd[entry].tx_flags =
972 (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
973 bp->tx_new = NEXT_TX(entry);
974 if (TX_BUFFS_AVAIL(bp) <= 0)
975 netif_stop_queue(dev);
976 spin_unlock_irq(&bp->lock);
979 sbus_writel(CREG_CTRL_TWAKEUP, bp->creg + CREG_CTRL);
985 static struct net_device_stats *bigmac_get_stats(struct net_device *dev)
987 struct bigmac *bp = netdev_priv(dev);
989 bigmac_get_counters(bp, bp->bregs);
993 static void bigmac_set_multicast(struct net_device *dev)
995 struct bigmac *bp = netdev_priv(dev);
996 void __iomem *bregs = bp->bregs;
997 struct netdev_hw_addr *ha;
1000 /* Disable the receiver. The bit self-clears when
1001 * the operation is complete.
1003 tmp = sbus_readl(bregs + BMAC_RXCFG);
1004 tmp &= ~(BIGMAC_RXCFG_ENABLE);
1005 sbus_writel(tmp, bregs + BMAC_RXCFG);
1006 while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
1009 if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
1010 sbus_writel(0xffff, bregs + BMAC_HTABLE0);
1011 sbus_writel(0xffff, bregs + BMAC_HTABLE1);
1012 sbus_writel(0xffff, bregs + BMAC_HTABLE2);
1013 sbus_writel(0xffff, bregs + BMAC_HTABLE3);
1014 } else if (dev->flags & IFF_PROMISC) {
1015 tmp = sbus_readl(bregs + BMAC_RXCFG);
1016 tmp |= BIGMAC_RXCFG_PMISC;
1017 sbus_writel(tmp, bregs + BMAC_RXCFG);
1019 u16 hash_table[4] = { 0 };
1021 netdev_for_each_mc_addr(ha, dev) {
1022 crc = ether_crc_le(6, ha->addr);
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 /* Ethtool support... */
1039 static void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1041 strlcpy(info->driver, "sunbmac", sizeof(info->driver));
1042 strlcpy(info->version, "2.0", sizeof(info->version));
1045 static u32 bigmac_get_link(struct net_device *dev)
1047 struct bigmac *bp = netdev_priv(dev);
1049 spin_lock_irq(&bp->lock);
1050 bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, MII_BMSR);
1051 spin_unlock_irq(&bp->lock);
1053 return (bp->sw_bmsr & BMSR_LSTATUS);
1056 static const struct ethtool_ops bigmac_ethtool_ops = {
1057 .get_drvinfo = bigmac_get_drvinfo,
1058 .get_link = bigmac_get_link,
1061 static const struct net_device_ops bigmac_ops = {
1062 .ndo_open = bigmac_open,
1063 .ndo_stop = bigmac_close,
1064 .ndo_start_xmit = bigmac_start_xmit,
1065 .ndo_get_stats = bigmac_get_stats,
1066 .ndo_set_rx_mode = bigmac_set_multicast,
1067 .ndo_tx_timeout = bigmac_tx_timeout,
1068 .ndo_set_mac_address = eth_mac_addr,
1069 .ndo_validate_addr = eth_validate_addr,
1072 static int bigmac_ether_init(struct platform_device *op,
1073 struct platform_device *qec_op)
1075 static int version_printed;
1076 struct net_device *dev;
1077 u8 bsizes, bsizes_more;
1081 /* Get a new device struct for this interface. */
1082 dev = alloc_etherdev(sizeof(struct bigmac));
1086 if (version_printed++ == 0)
1087 printk(KERN_INFO "%s", version);
1089 for (i = 0; i < 6; i++)
1090 dev->dev_addr[i] = idprom->id_ethaddr[i];
1092 /* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
1093 bp = netdev_priv(dev);
1094 bp->qec_op = qec_op;
1097 SET_NETDEV_DEV(dev, &op->dev);
1099 spin_lock_init(&bp->lock);
1101 /* Map in QEC global control registers. */
1102 bp->gregs = of_ioremap(&qec_op->resource[0], 0,
1103 GLOB_REG_SIZE, "BigMAC QEC GLobal Regs");
1105 printk(KERN_ERR "BIGMAC: Cannot map QEC global registers.\n");
1106 goto fail_and_cleanup;
1109 /* Make sure QEC is in BigMAC mode. */
1110 if ((sbus_readl(bp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_BMODE) {
1111 printk(KERN_ERR "BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
1112 goto fail_and_cleanup;
1115 /* Reset the QEC. */
1116 if (qec_global_reset(bp->gregs))
1117 goto fail_and_cleanup;
1119 /* Get supported SBUS burst sizes. */
1120 bsizes = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
1121 bsizes_more = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
1124 if (bsizes_more != 0xff)
1125 bsizes &= bsizes_more;
1126 if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
1127 (bsizes & DMA_BURST32) == 0)
1128 bsizes = (DMA_BURST32 - 1);
1129 bp->bigmac_bursts = bsizes;
1131 /* Perform QEC initialization. */
1134 /* Map in the BigMAC channel registers. */
1135 bp->creg = of_ioremap(&op->resource[0], 0,
1136 CREG_REG_SIZE, "BigMAC QEC Channel Regs");
1138 printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
1139 goto fail_and_cleanup;
1142 /* Map in the BigMAC control registers. */
1143 bp->bregs = of_ioremap(&op->resource[1], 0,
1144 BMAC_REG_SIZE, "BigMAC Primary Regs");
1146 printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
1147 goto fail_and_cleanup;
1150 /* Map in the BigMAC transceiver registers, this is how you poke at
1153 bp->tregs = of_ioremap(&op->resource[2], 0,
1154 TCVR_REG_SIZE, "BigMAC Transceiver Regs");
1156 printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
1157 goto fail_and_cleanup;
1160 /* Stop the BigMAC. */
1163 /* Allocate transmit/receive descriptor DVMA block. */
1164 bp->bmac_block = dma_alloc_coherent(&bp->bigmac_op->dev,
1166 &bp->bblock_dvma, GFP_ATOMIC);
1167 if (bp->bmac_block == NULL || bp->bblock_dvma == 0)
1168 goto fail_and_cleanup;
1170 /* Get the board revision of this BigMAC. */
1171 bp->board_rev = of_getintprop_default(bp->bigmac_op->dev.of_node,
1172 "board-version", 1);
1174 /* Init auto-negotiation timer state. */
1175 timer_setup(&bp->bigmac_timer, bigmac_timer, 0);
1176 bp->timer_state = asleep;
1177 bp->timer_ticks = 0;
1179 /* Backlink to generic net device struct. */
1182 /* Set links to our BigMAC open and close routines. */
1183 dev->ethtool_ops = &bigmac_ethtool_ops;
1184 dev->netdev_ops = &bigmac_ops;
1185 dev->watchdog_timeo = 5*HZ;
1187 /* Finish net device registration. */
1188 dev->irq = bp->bigmac_op->archdata.irqs[0];
1191 if (register_netdev(dev)) {
1192 printk(KERN_ERR "BIGMAC: Cannot register device.\n");
1193 goto fail_and_cleanup;
1196 dev_set_drvdata(&bp->bigmac_op->dev, bp);
1198 printk(KERN_INFO "%s: BigMAC 100baseT Ethernet %pM\n",
1199 dev->name, dev->dev_addr);
1204 /* Something went wrong, undo whatever we did so far. */
1205 /* Free register mappings if any. */
1207 of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
1209 of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
1211 of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
1213 of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
1216 dma_free_coherent(&bp->bigmac_op->dev,
1221 /* This also frees the co-located private data */
1226 /* QEC can be the parent of either QuadEthernet or a BigMAC. We want
1229 static int bigmac_sbus_probe(struct platform_device *op)
1231 struct device *parent = op->dev.parent;
1232 struct platform_device *qec_op;
1234 qec_op = to_platform_device(parent);
1236 return bigmac_ether_init(op, qec_op);
1239 static int bigmac_sbus_remove(struct platform_device *op)
1241 struct bigmac *bp = platform_get_drvdata(op);
1242 struct device *parent = op->dev.parent;
1243 struct net_device *net_dev = bp->dev;
1244 struct platform_device *qec_op;
1246 qec_op = to_platform_device(parent);
1248 unregister_netdev(net_dev);
1250 of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
1251 of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
1252 of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
1253 of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
1254 dma_free_coherent(&op->dev,
1259 free_netdev(net_dev);
1264 static const struct of_device_id bigmac_sbus_match[] = {
1271 MODULE_DEVICE_TABLE(of, bigmac_sbus_match);
1273 static struct platform_driver bigmac_sbus_driver = {
1276 .of_match_table = bigmac_sbus_match,
1278 .probe = bigmac_sbus_probe,
1279 .remove = bigmac_sbus_remove,
1282 module_platform_driver(bigmac_sbus_driver);