11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/netdevice.h>
16 #include <linux/string.h>
23 #include <linux/ethtool.h>
24 #include <linux/slab.h>
29 #include <asm/pgtable.h>
30 #include <asm/machdep.h>
37 #define trunc_page(x) ((void *)(((unsigned long)(x)) & ~((unsigned long)(PAGE_SIZE - 1))))
38 #define round_page(x) trunc_page(((unsigned long)(x)) + ((unsigned long)(PAGE_SIZE - 1)))
43 #define ENET_CRCPOLY 0x04c11db7
46 #define SUNHME_MULTICAST
50 #define MAX_TX_ACTIVE 1
52 #define ETHERMINPACKET 64
54 #define RX_BUFLEN (ETHERMTU + 14 + ETHERCRC + 2)
58 #define TX_DMA_ERR 0x80
91 typedef struct bmac_reg_entry {
93 unsigned short reg_offset;
96 #define N_REG_ENTRIES 31
98 static bmac_reg_entry_t reg_entries[N_REG_ENTRIES] = {
134 static unsigned char *bmac_emergency_rxbuf;
142 #define PRIV_BYTES (sizeof(struct bmac_data) \
143 + (N_RX_RING + N_TX_RING + 4) * sizeof(struct dbdma_cmd) \
144 + sizeof(struct sk_buff_head))
154 static void bmac_enable_and_reset_chip(
struct net_device *
dev);
160 static void bmac_tx_timeout(
unsigned long data);
164 #define DBDMA_SET(x) ( ((x) | (x) << 16) )
165 #define DBDMA_CLEAR(x) ( (x) << 16)
170 __asm__ volatile(
"stwbrx %0,0,%1" : :
"r" (
x),
"r" (a) :
"memory");
173 static inline unsigned long
177 __asm__ volatile (
"lwbrx %0,0,%1" :
"=r" (
swap) :
"r" (a));
182 dbdma_continue(
volatile struct dbdma_regs
__iomem *
dmap)
184 dbdma_st32(&dmap->control,
190 dbdma_reset(
volatile struct dbdma_regs
__iomem *dmap)
192 dbdma_st32(&dmap->control,
195 while (dbdma_ld32(&dmap->status) &
RUN)
200 dbdma_setcmd(
volatile struct dbdma_cmd *
cp,
202 unsigned long cmd_dep)
220 unsigned short bmread(
struct net_device *dev,
unsigned long reg_offset )
226 bmac_enable_and_reset_chip(
struct net_device *dev)
237 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->
mdev), 0, 1);
240 #define MIFDELAY udelay(10)
243 bmac_mif_readbits(
struct net_device *dev,
int nb)
245 unsigned int val = 0;
250 if (bmread(dev,
MIFCSR) & 8)
263 bmac_mif_writebits(
struct net_device *dev,
unsigned int val,
int nb)
268 b = (val & (1 << nb))? 6: 4;
271 bmwrite(dev,
MIFCSR, b|1);
283 bmac_mif_writebits(dev, ~0
U, 32);
284 bmac_mif_writebits(dev, 6, 4);
285 bmac_mif_writebits(dev, addr, 10);
290 val = bmac_mif_readbits(dev, 17);
297 bmac_mif_write(
struct net_device *dev,
unsigned int addr,
unsigned int val)
301 bmac_mif_writebits(dev, ~0
U, 32);
302 bmac_mif_writebits(dev, 5, 4);
303 bmac_mif_writebits(dev, addr, 10);
304 bmac_mif_writebits(dev, 2, 2);
305 bmac_mif_writebits(dev, val, 16);
306 bmac_mif_writebits(dev, 3, 2);
313 volatile unsigned short regValue;
314 unsigned short *pWord16;
326 regValue = bmread(dev,
TXRST);
330 regValue = bmread(dev,
XCVRIF);
332 bmwrite(dev,
XCVRIF, regValue);
336 bmwrite(dev,
RSEED, (
unsigned short)0x1968);
338 regValue = bmread(dev,
XIFC);
340 bmwrite(dev,
XIFC, regValue);
345 bmwrite(dev,
NCCNT, 0);
346 bmwrite(dev,
NTCNT, 0);
347 bmwrite(dev,
EXCNT, 0);
348 bmwrite(dev,
LTCNT, 0);
351 bmwrite(dev,
FRCNT, 0);
352 bmwrite(dev,
LECNT, 0);
353 bmwrite(dev,
AECNT, 0);
354 bmwrite(dev,
FECNT, 0);
355 bmwrite(dev,
RXCV, 0);
358 bmwrite(dev,
TXTH, 4);
377 pWord16 = (
unsigned short *)dev->
dev_addr;
378 bmwrite(dev,
MADD0, *pWord16++);
379 bmwrite(dev,
MADD1, *pWord16++);
380 bmwrite(dev,
MADD2, *pWord16);
389 bmac_disable_interrupts(
struct net_device *dev)
395 bmac_enable_interrupts(
struct net_device *dev)
407 unsigned short oldConfig;
412 oldConfig = bmread(dev,
TXCFG);
416 oldConfig = bmread(dev,
RXCFG);
428 for (addr = 0; addr < 32; ++
addr) {
438 ctrl = bmac_mif_read(dev, 0);
439 capable = ((bmac_mif_read(dev, 1) & 0xf800) >> 6) | 1;
440 if (bmac_mif_read(dev, 4) != capable ||
441 (ctrl & 0x1000) == 0) {
442 bmac_mif_write(dev, 4, capable);
443 bmac_mif_write(dev, 0, 0x1200);
445 bmac_mif_write(dev, 0, 0x1000);
449 static void bmac_init_chip(
struct net_device *dev)
452 bmac_init_registers(dev);
458 struct net_device* dev = macio_get_drvdata(mdev);
475 spin_unlock_irqrestore(&bp->
lock, flags);
480 config = bmread(dev,
RXCFG);
482 config = bmread(dev,
TXCFG);
502 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->
mdev), 0, 0);
506 static int bmac_resume(
struct macio_dev *mdev)
508 struct net_device* dev = macio_get_drvdata(mdev);
513 bmac_reset_and_enable(dev);
524 static int bmac_set_address(
struct net_device *dev,
void *addr)
527 unsigned char *
p =
addr;
528 unsigned short *pWord16;
532 XXDEBUG((
"bmac: enter set_address\n"));
535 for (i = 0; i < 6; ++
i) {
539 pWord16 = (
unsigned short *)dev->
dev_addr;
540 bmwrite(dev,
MADD0, *pWord16++);
541 bmwrite(dev,
MADD1, *pWord16++);
542 bmwrite(dev,
MADD2, *pWord16);
544 spin_unlock_irqrestore(&bp->
lock, flags);
545 XXDEBUG((
"bmac: exit set_address\n"));
549 static inline void bmac_set_timeout(
struct net_device *dev)
562 spin_unlock_irqrestore(&bp->
lock, flags);
566 bmac_construct_xmt(
struct sk_buff *
skb,
volatile struct dbdma_cmd *
cp)
576 dbdma_setcmd(cp, (OUTPUT_LAST |
INTR_ALWAYS | WAIT_IFCLR), len, baddr, 0);
580 bmac_construct_rxbuff(
struct sk_buff *skb,
volatile struct dbdma_cmd *cp)
582 unsigned char *addr = skb? skb->
data: bmac_emergency_rxbuf;
593 memset((
char *)bp->
tx_cmds, 0, (N_TX_RING+1) *
sizeof(
struct dbdma_cmd));
600 dbdma_setcmd(&bp->
tx_cmds[N_TX_RING],
605 out_le32(&td->wait_sel, 0x00200020);
619 (N_RX_RING + 1) *
sizeof(
struct dbdma_cmd));
626 bmac_construct_rxbuff(skb, &bp->
rx_cmds[i]);
633 dbdma_setcmd(&bp->
rx_cmds[N_RX_RING],
657 netif_stop_queue(dev);
659 XXDEBUG((
"bmac_transmit_packet: tx ring full\n"));
663 dbdma_setcmd(&bp->
tx_cmds[i], DBDMA_STOP, 0, 0, 0);
677 static int rxintcount;
684 volatile struct dbdma_cmd *
cp;
693 if (++rxintcount < 10) {
694 XXDEBUG((
"bmac_rxdma_intr\n"));
702 stat = ld_le16(&cp->xfer_status);
703 residual = ld_le16(&cp->res_count);
709 dev->
stats.rx_length_errors++;
710 dev->
stats.rx_errors++;
720 ++dev->
stats.rx_packets;
721 dev->
stats.rx_bytes += nb;
723 ++dev->
stats.rx_dropped;
728 skb_reserve(bp->
rx_bufs[i], 2);
730 bmac_construct_rxbuff(skb, &bp->
rx_cmds[i]);
731 st_le16(&cp->res_count, 0);
732 st_le16(&cp->xfer_status, 0);
734 if (++i >= N_RX_RING) i = 0;
743 spin_unlock_irqrestore(&bp->
lock, flags);
745 if (rxintcount < 10) {
746 XXDEBUG((
"bmac_rxdma_intr done\n"));
751 static int txintcount;
753 static irqreturn_t bmac_txdma_intr(
int irq,
void *dev_id)
757 volatile struct dbdma_cmd *
cp;
763 if (txintcount++ < 10) {
764 XXDEBUG((
"bmac_txdma_intr\n"));
772 stat = ld_le16(&cp->xfer_status);
773 if (txintcount < 10) {
774 XXDEBUG((
"bmac_txdma_xfer_stat=%#0x\n", stat));
776 if (!(stat & ACTIVE)) {
785 ++dev->
stats.tx_packets;
790 netif_wake_queue(dev);
797 spin_unlock_irqrestore(&bp->
lock, flags);
799 if (txintcount < 10) {
800 XXDEBUG((
"bmac_txdma_intr done->bmac_start\n"));
807 #ifndef SUNHME_MULTICAST
809 static int reverse6[64] = {
810 0x0,0x20,0x10,0x30,0x8,0x28,0x18,0x38,
811 0x4,0x24,0x14,0x34,0xc,0x2c,0x1c,0x3c,
812 0x2,0x22,0x12,0x32,0xa,0x2a,0x1a,0x3a,
813 0x6,0x26,0x16,0x36,0xe,0x2e,0x1e,0x3e,
814 0x1,0x21,0x11,0x31,0x9,0x29,0x19,0x39,
815 0x5,0x25,0x15,0x35,0xd,0x2d,0x1d,0x3d,
816 0x3,0x23,0x13,0x33,0xb,0x2b,0x1b,0x3b,
817 0x7,0x27,0x17,0x37,0xf,0x2f,0x1f,0x3f
821 crc416(
unsigned int curval,
unsigned short nxtval)
824 register int high_crc_set, low_data_set;
827 next = ((next & 0x00FF) << 8) | (next >> 8);
830 for (counter = 0; counter < 16; ++
counter) {
832 if ((cur & 0x80000000) == 0) high_crc_set = 0;
833 else high_crc_set = 1;
837 if ((next & 0x0001) == 0) low_data_set = 0;
838 else low_data_set = 1;
843 if (high_crc_set ^ low_data_set) cur = cur ^
ENET_CRCPOLY;
849 bmac_crc(
unsigned short *
address)
853 XXDEBUG((
"bmac_crc: addr=%#04x, %#04x, %#04x\n", *address, address[1], address[2]));
854 newcrc = crc416(0xffffffff, *address);
855 newcrc = crc416(newcrc, address[1]);
856 newcrc = crc416(newcrc, address[2]);
867 bmac_addhash(
struct bmac_data *bp,
unsigned char *addr)
872 if (!(*addr))
return;
873 crc = bmac_crc((
unsigned short *)addr) & 0x3f;
877 mask = (
unsigned char)1 << mask;
882 bmac_removehash(
struct bmac_data *bp,
unsigned char *addr)
888 crc = bmac_crc((
unsigned short *)addr) & 0x3f;
893 mask = ((
unsigned char)1 << mask) ^ 0xffff;
905 unsigned short rx_cfg;
907 rx_cfg = bmread(dev,
RXCFG);
909 bmwrite(dev,
RXCFG, rx_cfg);
911 rx_cfg = bmread(dev,
RXCFG);
918 unsigned short rx_cfg;
920 rx_cfg = bmread(dev,
RXCFG);
929 bmwrite(dev,
RXCFG, rx_cfg );
945 struct bmac_data *bp,
unsigned char *addr)
948 bmac_addhash(bp, addr);
950 bmac_update_hash_table_mask(dev, bp);
957 struct bmac_data *bp,
unsigned char *addr)
959 bmac_removehash(bp, addr);
961 bmac_update_hash_table_mask(dev, bp);
972 static void bmac_set_multicast(
struct net_device *dev)
977 unsigned short rx_cfg;
983 XXDEBUG((
"bmac: enter bmac_set_multicast, n_addrs=%d\n", num_addrs));
987 bmac_update_hash_table_mask(dev, bp);
988 rx_cfg = bmac_rx_on(dev, 1, 0);
989 XXDEBUG((
"bmac: all multi, rx_cfg=%#08x\n"));
991 rx_cfg = bmread(dev,
RXCFG);
993 bmwrite(dev,
RXCFG, rx_cfg);
994 rx_cfg = bmac_rx_on(dev, 0, 1);
995 XXDEBUG((
"bmac: promisc mode enabled, rx_cfg=%#08x\n", rx_cfg));
999 if (num_addrs == 0) {
1000 rx_cfg = bmac_rx_on(dev, 0, 0);
1001 XXDEBUG((
"bmac: multi disabled, rx_cfg=%#08x\n", rx_cfg));
1004 bmac_addhash(bp, ha->addr);
1005 bmac_update_hash_table_mask(dev, bp);
1006 rx_cfg = bmac_rx_on(dev, 1, 0);
1016 static void bmac_set_multicast(
struct net_device *dev)
1020 unsigned short rx_cfg;
1024 bmwrite(dev,
BHASH0, 0xffff);
1025 bmwrite(dev,
BHASH1, 0xffff);
1026 bmwrite(dev,
BHASH2, 0xffff);
1027 bmwrite(dev,
BHASH3, 0xffff);
1029 rx_cfg = bmread(dev,
RXCFG);
1031 bmwrite(dev,
RXCFG, rx_cfg);
1035 rx_cfg = bmread(dev,
RXCFG);
1037 bmwrite(dev,
RXCFG, rx_cfg);
1039 for(i = 0; i < 4; i++) hash_table[i] = 0;
1044 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1046 bmwrite(dev,
BHASH0, hash_table[0]);
1047 bmwrite(dev,
BHASH1, hash_table[1]);
1048 bmwrite(dev,
BHASH2, hash_table[2]);
1049 bmwrite(dev,
BHASH3, hash_table[3]);
1054 static int miscintcount;
1056 static irqreturn_t bmac_misc_intr(
int irq,
void *dev_id)
1060 if (miscintcount++ < 10) {
1061 XXDEBUG((
"bmac_misc_intr\n"));
1082 #define SROMAddressLength 5
1083 #define DataInOn 0x0008
1084 #define DataInOff 0x0000
1086 #define ChipSelect 0x0001
1087 #define SDIShiftCount 3
1088 #define SD0ShiftCount 2
1089 #define DelayValue 1000
1090 #define SROMStartOffset 10
1091 #define SROMReadCount 3
1092 #define SROMAddressBits 6
1093 #define EnetAddressOffset 20
1095 static unsigned char
1098 unsigned short data;
1115 bmac_clock_in_bit(
struct net_device *dev,
unsigned int val)
1117 unsigned short data;
1119 if (val != 0 && val != 1)
return;
1133 reset_and_select_srom(
struct net_device *dev)
1140 bmac_clock_in_bit(dev, 1);
1141 bmac_clock_in_bit(dev, 1);
1142 bmac_clock_in_bit(dev, 0);
1145 static unsigned short
1153 val = addr >> (addr_len-i-1);
1154 bmac_clock_in_bit(dev, val & 1);
1159 for (i = 0; i < 16; i++) {
1160 val = bmac_clock_out_bit(dev);
1177 unsigned short data, storedCS;
1179 reset_and_select_srom(dev);
1181 storedCS = ((data >> 8) & 0x0ff) | ((data << 8) & 0xff00);
1188 bmac_get_station_address(
struct net_device *dev,
unsigned char *
ea)
1191 unsigned short data;
1193 for (i = 0; i < 6; i++)
1195 reset_and_select_srom(dev);
1197 ea[2*
i] = bitrev8(data & 0x0ff);
1198 ea[2*i+1] = bitrev8((data >> 8) & 0x0ff);
1202 static void bmac_reset_and_enable(
struct net_device *dev)
1204 struct bmac_data *bp = netdev_priv(dev);
1205 unsigned long flags;
1207 unsigned char *
data;
1210 bmac_enable_and_reset_chip(dev);
1211 bmac_init_tx_ring(bp);
1212 bmac_init_rx_ring(dev);
1213 bmac_init_chip(dev);
1214 bmac_start_chip(dev);
1228 bmac_transmit_packet(skb, dev);
1230 spin_unlock_irqrestore(&bp->
lock, flags);
1233 static const struct ethtool_ops bmac_ethtool_ops = {
1238 .ndo_open = bmac_open,
1239 .ndo_stop = bmac_close,
1240 .ndo_start_xmit = bmac_output,
1241 .ndo_set_rx_mode = bmac_set_multicast,
1242 .ndo_set_mac_address = bmac_set_address,
1251 const unsigned char *prop_addr;
1252 unsigned char addr[6];
1254 int is_bmac_plus = ((
int)match->
data) != 0;
1256 if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
1261 "mac-address",
NULL);
1262 if (prop_addr ==
NULL) {
1264 "local-mac-address",
NULL);
1265 if (prop_addr ==
NULL) {
1270 memcpy(addr, prop_addr,
sizeof(addr));
1276 bp = netdev_priv(dev);
1278 macio_set_drvdata(mdev, dev);
1289 ioremap(macio_resource_start(mdev, 0), macio_resource_len(mdev, 0));
1293 dev->
irq = macio_irq(mdev, 0);
1295 bmac_enable_and_reset_chip(dev);
1298 rev = addr[0] == 0 && addr[1] == 0xA0;
1299 for (j = 0; j < 6; ++
j)
1300 dev->
dev_addr[j] = rev ? bitrev8(addr[j]): addr[
j];
1303 bmac_enable_and_reset_chip(dev);
1309 bmac_get_station_address(dev, addr);
1310 if (bmac_verify_checksum(dev) != 0)
1311 goto err_out_iounmap;
1314 bp->
tx_dma =
ioremap(macio_resource_start(mdev, 1), macio_resource_len(mdev, 1));
1316 goto err_out_iounmap;
1318 bp->
rx_dma =
ioremap(macio_resource_start(mdev, 2), macio_resource_len(mdev, 2));
1320 goto err_out_iounmap_tx;
1323 bp->
tx_cmds = (
volatile struct dbdma_cmd *) DBDMA_ALIGN(bp + 1);
1327 skb_queue_head_init(bp->
queue);
1331 ret =
request_irq(dev->
irq, bmac_misc_intr, 0,
"BMAC-misc", dev);
1334 goto err_out_iounmap_rx;
1351 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->
mdev), 0, 0);
1380 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->
mdev), 0, 0);
1388 struct bmac_data *bp = netdev_priv(dev);
1392 bmac_reset_and_enable(dev);
1397 static int bmac_close(
struct net_device *dev)
1399 struct bmac_data *bp = netdev_priv(dev);
1408 config = bmread(dev,
RXCFG);
1411 config = bmread(dev,
TXCFG);
1421 XXDEBUG((
"bmac: free rx bufs\n"));
1424 dev_kfree_skb(bp->
rx_bufs[i]);
1428 XXDEBUG((
"bmac: free tx bufs\n"));
1431 dev_kfree_skb(bp->
tx_bufs[i]);
1435 XXDEBUG((
"bmac: all bufs freed\n"));
1439 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->
mdev), 0, 0);
1447 struct bmac_data *bp = netdev_priv(dev);
1450 unsigned long flags;
1465 bmac_transmit_packet(skb, dev);
1467 spin_unlock_irqrestore(&bp->
lock, flags);
1473 struct bmac_data *bp = netdev_priv(dev);
1479 static void bmac_tx_timeout(
unsigned long data)
1482 struct bmac_data *bp = netdev_priv(dev);
1485 volatile struct dbdma_cmd *
cp;
1486 unsigned long flags;
1487 unsigned short config, oldConfig;
1490 XXDEBUG((
"bmac: tx_timeout called\n"));
1503 config = bmread(dev,
RXCFG);
1505 config = bmread(dev,
TXCFG);
1509 bmac_enable_and_reset_chip(dev);
1522 ++dev->
stats.tx_errors;
1524 dev_kfree_skb(bp->
tx_bufs[i]);
1526 if (++i >= N_TX_RING) i = 0;
1530 netif_wake_queue(dev);
1534 out_le16(&cp->command, OUTPUT_LAST);
1542 oldConfig = bmread(dev,
RXCFG);
1544 oldConfig = bmread(dev,
TXCFG);
1547 spin_unlock_irqrestore(&bp->
lock, flags);
1551 static void dump_dbdma(
volatile struct dbdma_cmd *cp,
int count)
1555 for (i=0;i<
count;i++) {
1558 printk(
"dbdma req 0x%x addr 0x%x baddr 0x%x xfer/res 0x%x\n",
1577 if (bmac_devs ==
NULL)
1580 len +=
sprintf(buffer,
"BMAC counters & registers\n");
1582 for (i = 0; i<N_REG_ENTRIES; i++) {
1583 len +=
sprintf(buffer + len,
"%s: %#08x\n",
1584 reg_entries[i].
name,
1585 bmread(bmac_devs, reg_entries[i].reg_offset));
1593 if (pos > offset+length)
break;
1596 *start = buffer + (offset - begin);
1597 len -= (offset - begin);
1599 if (len > length) len =
length;
1605 static int __devexit bmac_remove(
struct macio_dev *mdev)
1607 struct net_device *dev = macio_get_drvdata(mdev);
1608 struct bmac_data *bp = netdev_priv(dev);
1635 .compatible =
"bmac+",
1642 static struct macio_driver bmac_driver =
1647 .of_match_table = bmac_match,
1649 .probe = bmac_probe,
1650 .remove = bmac_remove,
1652 .suspend = bmac_suspend,
1653 .resume = bmac_resume,
1658 static int __init bmac_init(
void)
1660 if (bmac_emergency_rxbuf ==
NULL) {
1662 if (bmac_emergency_rxbuf ==
NULL)
1669 static void __exit bmac_exit(
void)
1673 kfree(bmac_emergency_rxbuf);
1674 bmac_emergency_rxbuf =
NULL;