6 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/fcntl.h>
14 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/ethtool.h>
20 #include <linux/mii.h>
21 #include <linux/netdevice.h>
24 #include <linux/bitops.h>
31 #include <asm/byteorder.h>
33 #include <asm/idprom.h>
35 #include <asm/openprom.h>
36 #include <asm/oplib.h>
37 #include <asm/pgtable.h>
41 #define DRV_NAME "sunbmac"
42 #define DRV_VERSION "2.1"
43 #define DRV_RELDATE "August 26, 2008"
59 #define DP(x) printk x
65 #define DTX(x) printk x
71 #define DIRQ(x) printk x
76 #define DEFAULT_JAMSIZE 4
78 #define QEC_RESET_TRIES 200
80 static int qec_global_reset(
void __iomem *gregs)
98 static void qec_init(
struct bigmac *bp)
126 #define TX_RESET_TRIES 32
127 #define RX_RESET_TRIES 32
129 static void bigmac_tx_reset(
void __iomem *bregs)
149 static void bigmac_rx_reset(
void __iomem *bregs)
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)
193 static void bigmac_clean_rings(
struct bigmac *bp)
212 static void bigmac_init_rings(
struct bigmac *bp,
int from_irq)
224 bigmac_clean_rings(bp);
238 skb_reserve(skb, 34);
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)
271 bit = (bit & 1) << 3;
279 bit = (bit & 1) << 2;
291 static int read_tcvr_bit(
struct bigmac *bp,
void __iomem *tregs)
314 static int read_tcvr_bit2(
struct bigmac *bp,
void __iomem *tregs)
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)
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,
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;
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,
414 put_tcvr_byte(bp, tregs, reg);
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)
459 idle_transceiver(tregs);
485 "external MDIO available!\n");
492 static int bigmac_init_hw(
struct bigmac *,
int);
494 static int try_next_permutation(
struct bigmac *bp,
void __iomem *tregs)
527 static void bigmac_timer(
unsigned long data)
531 int restart_timer = 0;
541 "100baseT" :
"10baseT");
548 ret = try_next_permutation(bp, tregs);
552 ret = bigmac_init_hw(bp, 0);
555 "BigMAC.\n", bp->
dev->name);
567 printk(
KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
574 if (restart_timer != 0) {
583 static void bigmac_begin_auto_negotiation(
struct bigmac *bp)
622 static int bigmac_init_hw(
struct bigmac *bp,
int from_irq)
627 unsigned char *
e = &bp->
dev->dev_addr[0];
630 bigmac_get_counters(bp, bregs);
633 qec_global_reset(gregs);
639 bigmac_init_rings(bp, from_irq);
642 bigmac_tcvr_init(bp);
711 bigmac_begin_auto_negotiation(bp);
718 static void bigmac_is_medium_rare(
struct bigmac *bp,
u32 qec_status,
u32 bmac_status)
725 printk(
"QEC_BMAC_ERROR, ");
733 printk(
"TX_LATE_ERROR, ");
735 printk(
"TX_PARITY_ERROR, ");
737 printk(
"TX_SBUS_ERROR, ");
740 printk(
"RX_DROP_ERROR, ");
743 printk(
"RX_SMALL_ERROR, ");
745 printk(
"RX_LATE_ERROR, ");
747 printk(
"RX_PARITY_ERROR, ");
749 printk(
"RX_SBUS_ERROR, ");
753 bigmac_init_hw(bp, 1);
757 static void bigmac_tx(
struct bigmac *bp)
763 spin_lock(&bp->
lock);
766 DTX((
"bigmac_tx: tx_old[%d] ", elem));
767 while (elem != bp->
tx_new) {
771 DTX((
"this(%p) [flags(%08x)addr(%08x)]",
780 this->tx_addr, skb->
len,
783 DTX((
"skb(%p) ", skb));
789 DTX((
" DONE, tx_old=%d\n", elem));
792 if (netif_queue_stopped(dev) &&
794 netif_wake_queue(bp->
dev);
796 spin_unlock(&bp->
lock);
800 static void bigmac_rx(
struct bigmac *bp)
804 int elem = bp->
rx_new, drops = 0;
807 this = &rxbase[
elem];
830 if (new_skb ==
NULL) {
840 skb_reserve(new_skb, 34);
852 struct sk_buff *copy_skb = netdev_alloc_skb(bp->
dev, len + 2);
854 if (copy_skb ==
NULL) {
858 skb_reserve(copy_skb, 2);
863 skb_copy_to_linear_data(copy_skb, (
unsigned char *)skb->
data, len);
882 this = &rxbase[
elem];
892 u32 qec_status, bmac_status;
894 DIRQ((
"bigmac_interrupt: "));
900 DIRQ((
"qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
902 (bmac_status & CREG_STAT_ERRORS))
903 bigmac_is_medium_rare(bp, qec_status, bmac_status);
914 static int bigmac_open(
struct net_device *dev)
916 struct bigmac *bp = netdev_priv(dev);
925 ret = bigmac_init_hw(bp, 0);
931 static int bigmac_close(
struct net_device *dev)
933 struct bigmac *bp = netdev_priv(dev);
940 bigmac_clean_rings(bp);
945 static void bigmac_tx_timeout(
struct net_device *dev)
947 struct bigmac *bp = netdev_priv(dev);
949 bigmac_init_hw(bp, 0);
950 netif_wake_queue(dev);
956 struct bigmac *bp = netdev_priv(dev);
965 spin_lock_irq(&bp->
lock);
967 DTX((
"bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
975 netif_stop_queue(dev);
976 spin_unlock_irq(&bp->
lock);
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);
1022 for (i = 0; i < 4; i++)
1028 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1051 struct bigmac *bp = netdev_priv(dev);
1053 spin_lock_irq(&bp->
lock);
1055 spin_unlock_irq(&bp->
lock);
1060 static const struct ethtool_ops bigmac_ethtool_ops = {
1061 .get_drvinfo = bigmac_get_drvinfo,
1062 .get_link = bigmac_get_link,
1066 .ndo_open = bigmac_open,
1067 .ndo_stop = bigmac_close,
1068 .ndo_start_xmit = bigmac_start_xmit,
1069 .ndo_get_stats = bigmac_get_stats,
1070 .ndo_set_rx_mode = bigmac_set_multicast,
1071 .ndo_tx_timeout = bigmac_tx_timeout,
1080 static int version_printed;
1082 u8 bsizes, bsizes_more;
1087 dev = alloc_etherdev(
sizeof(
struct bigmac));
1091 if (version_printed++ == 0)
1094 for (i = 0; i < 6; i++)
1098 bp = netdev_priv(dev);
1111 goto fail_and_cleanup;
1117 goto fail_and_cleanup;
1121 if (qec_global_reset(bp->
gregs))
1122 goto fail_and_cleanup;
1129 if (bsizes_more != 0xff)
1130 bsizes &= bsizes_more;
1131 if (bsizes == 0xff || (bsizes &
DMA_BURST16) == 0 ||
1132 (bsizes & DMA_BURST32) == 0)
1133 bsizes = (DMA_BURST32 - 1);
1144 goto fail_and_cleanup;
1151 printk(
KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
1152 goto fail_and_cleanup;
1161 printk(
KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
1162 goto fail_and_cleanup;
1174 goto fail_and_cleanup;
1179 "board-version", 1);
1200 goto fail_and_cleanup;
1243 return bigmac_ether_init(op, qec_op);
1273 static const struct of_device_id bigmac_sbus_match[] = {
1286 .of_match_table = bigmac_sbus_match,
1288 .probe = bigmac_sbus_probe,