16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/fcntl.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
24 #include <linux/netdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/tcp.h>
34 #define DRV_NAME "enc28j60"
35 #define DRV_VERSION "1.01"
39 #define ENC28J60_MSG_DEFAULT \
40 (NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN | NETIF_MSG_LINK)
44 #define SPI_TRANSFER_BUF_LEN (4 + MAX_FRAMELEN)
46 #define TX_TIMEOUT (4 * HZ)
49 #define MAX_TX_RETRYCOUNT 16
101 tx_buf[1] = tx_buf[2] = tx_buf[3] = 0;
103 spi_message_init(&
msg);
104 spi_message_add_tail(&t, &
msg);
120 static int spi_write_buf(
struct enc28j60_net *priv,
int len,
160 val = rx_buf[slen - 1];
182 static void enc28j60_soft_reset(
struct enc28j60_net *priv)
243 enc28j60_set_bank(priv, addr);
251 nolock_reg_bfset(priv, addr, mask);
261 enc28j60_set_bank(priv, addr);
269 nolock_reg_bfclr(priv, addr, mask);
279 enc28j60_set_bank(priv, address);
289 ret = nolock_regb_read(priv, address);
303 enc28j60_set_bank(priv, address);
307 return (rh << 8) | rl;
316 ret = nolock_regw_read(priv, address);
325 static void nolock_regb_write(
struct enc28j60_net *priv,
328 enc28j60_set_bank(priv, address);
332 static void locked_regb_write(
struct enc28j60_net *priv,
336 nolock_regb_write(priv, address, data);
343 static void nolock_regw_write(
struct enc28j60_net *priv,
344 u8 address,
u16 data)
346 enc28j60_set_bank(priv, address);
352 static void locked_regw_write(
struct enc28j60_net *priv,
353 u8 address,
u16 data)
356 nolock_regw_write(priv, address, data);
364 static void enc28j60_mem_read(
struct enc28j60_net *priv,
365 u16 addr,
int len,
u8 *data)
368 nolock_regw_write(priv,
ERDPTL, addr);
369 #ifdef CONFIG_ENC28J60_WRITEVERIFY
372 reg = nolock_regw_read(priv,
ERDPTL);
375 "(0x%04x - 0x%04x)\n", __func__, reg, addr);
378 spi_read_buf(priv, len, data);
386 enc28j60_packet_write(
struct enc28j60_net *priv,
int len,
const u8 *data)
391 #ifdef CONFIG_ENC28J60_WRITEVERIFY
394 reg = nolock_regw_read(priv,
EWRPTL);
397 ": %s() ERWPT:0x%04x != 0x%04x\n",
407 ": %s() after control byte ERWPT:0x%04x\n",
408 __func__, nolock_regw_read(priv,
EWRPTL));
410 spi_write_buf(priv, len, data);
413 ": %s() after write packet ERWPT:0x%04x, len=%d\n",
414 __func__, nolock_regw_read(priv,
EWRPTL), len);
418 static unsigned long msec20_to_jiffies;
425 while ((nolock_regb_read(priv, reg) & mask) != val) {
429 "reg %02x ready timeout!\n", reg);
455 nolock_regb_write(priv,
MIREGADR, address);
459 wait_phy_ready(priv);
461 nolock_regb_write(priv,
MICMD, 0x00);
463 ret = nolock_regw_read(priv,
MIRDL);
475 nolock_regb_write(priv,
MIREGADR, address);
477 nolock_regw_write(priv,
MIWRL, data);
479 ret = wait_phy_ready(priv);
497 ": %s: Setting MAC address to %pM\n",
510 ": %s() Hardware must be disabled to set "
511 "Mac address\n", __func__);
521 static int enc28j60_set_mac_address(
struct net_device *
dev,
void *addr)
525 if (netif_running(dev))
527 if (!is_valid_ether_addr(address->
sa_data))
532 return enc28j60_set_hw_macaddr(dev);
538 static void enc28j60_dump_regs(
struct enc28j60_net *priv,
const char *
msg)
543 "Cntrl: ECON1 ECON2 ESTAT EIR EIE\n"
544 " 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n"
545 "MAC : MACON1 MACON3 MACON4\n"
546 " 0x%02x 0x%02x 0x%02x\n"
547 "Rx : ERXST ERXND ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\n"
548 " 0x%04x 0x%04x 0x%04x 0x%04x "
549 "0x%02x 0x%02x 0x%04x\n"
550 "Tx : ETXST ETXND MACLCON1 MACLCON2 MAPHSUP\n"
551 " 0x%04x 0x%04x 0x%02x 0x%02x 0x%02x\n",
552 msg, nolock_regb_read(priv,
EREVID),
553 nolock_regb_read(priv,
ECON1), nolock_regb_read(priv,
ECON2),
554 nolock_regb_read(priv,
ESTAT), nolock_regb_read(priv,
EIR),
555 nolock_regb_read(priv,
EIE), nolock_regb_read(priv,
MACON1),
556 nolock_regb_read(priv,
MACON3), nolock_regb_read(priv,
MACON4),
557 nolock_regw_read(priv,
ERXSTL), nolock_regw_read(priv,
ERXNDL),
560 nolock_regb_read(priv,
ERXFCON),
561 nolock_regb_read(priv,
EPKTCNT),
562 nolock_regw_read(priv,
MAMXFLL), nolock_regw_read(priv,
ETXSTL),
563 nolock_regw_read(priv,
ETXNDL),
566 nolock_regb_read(priv,
MAPHSUP));
577 if ((next_packet_ptr - 1 < start) || (next_packet_ptr - 1 > end))
580 erxrdpt = next_packet_ptr - 1;
600 if (start > 0x1FFF || end > 0x1FFF || start > end) {
603 "bad parameters!\n", __func__, start, end);
608 nolock_regw_write(priv,
ERXSTL, start);
609 erxrdpt = erxrdpt_workaround(priv->
next_pk_ptr, start, end);
610 nolock_regw_write(priv,
ERXRDPTL, erxrdpt);
611 nolock_regw_write(priv,
ERXNDL, end);
616 if (start > 0x1FFF || end > 0x1FFF || start > end) {
619 "bad parameters!\n", __func__, start, end);
623 nolock_regw_write(priv,
ETXSTL, start);
624 nolock_regw_write(priv,
ETXNDL, end);
632 static void enc28j60_lowpower(
struct enc28j60_net *priv,
bool is_low)
636 is_low ?
"low" :
"high");
663 enc28j60_soft_reset(priv);
683 reg = locked_regb_read(priv,
EREVID);
686 if (reg == 0x00 || reg == 0xff) {
694 locked_regb_write(priv,
ERXFCON,
698 locked_regb_write(priv,
MACON1,
702 locked_regb_write(priv,
MACON3,
706 locked_regb_write(priv,
MAIPGL, 0x12);
708 locked_regb_write(priv,
MABBIPG, 0x15);
710 locked_regb_write(priv,
MACON3,
713 locked_regb_write(priv,
MACON4, 1 << 6);
715 locked_regw_write(priv,
MAIPGL, 0x0C12);
717 locked_regb_write(priv,
MABBIPG, 0x12);
733 if (!enc28j60_phy_write(priv,
PHCON2, 0x00))
736 if (!enc28j60_phy_write(priv,
PHCON1, 0x00))
742 enc28j60_dump_regs(priv,
"Hw initialized.");
747 static void enc28j60_hw_enable(
struct enc28j60_net *priv)
768 static void enc28j60_hw_disable(
struct enc28j60_net *priv)
772 nolock_regb_write(priv,
EIE, 0x00);
793 "unsupported link setting\n");
798 dev_warn(&ndev->
dev,
"Warning: hw must be disabled "
799 "to set link mode\n");
812 endptr = locked_regw_read(priv,
ETXNDL);
816 enc28j60_mem_read(priv, endptr + 1, TSV_SIZE, tsv);
819 static void enc28j60_dump_tsv(
struct enc28j60_net *priv,
const char *msg,
834 " TotByteOnWire: %d\n", tmp1, tsv[2] & 0x0f, tmp2);
841 "PacketDefer: %d, ExDefer: %d\n",
847 "Giant: %d, Underrun: %d\n",
852 "BackPressApp: %d, VLanTagFrame: %d\n",
862 static void enc28j60_dump_rsv(
struct enc28j60_net *priv,
const char *msg,
875 "LongDropEvent: %d, CarrierEvent: %d\n",
881 " UnknownOp: %d, VLanTagFrame: %d\n",
888 static void dump_packet(
const char *msg,
int len,
const char *data)
900 static void enc28j60_hw_rx(
struct net_device *ndev)
904 u16 erxrdpt, next_packet, rxstat;
915 "%s() Invalid packet address!! 0x%04x\n",
926 ndev->
stats.rx_errors++;
930 enc28j60_mem_read(priv, priv->
next_pk_ptr,
sizeof(rsv), rsv);
932 next_packet = rsv[1];
934 next_packet |= rsv[0];
945 enc28j60_dump_rsv(priv, __func__, next_packet, len, rxstat);
949 dev_err(&ndev->
dev,
"Rx Error (%04x)\n", rxstat);
950 ndev->
stats.rx_errors++;
952 ndev->
stats.rx_crc_errors++;
954 ndev->
stats.rx_frame_errors++;
956 ndev->
stats.rx_over_errors++;
962 "out of memory for Rx'd frame\n");
963 ndev->
stats.rx_dropped++;
967 enc28j60_mem_read(priv,
971 dump_packet(__func__, skb->
len, skb->
data);
974 ndev->
stats.rx_packets++;
990 nolock_regw_write(priv,
ERXRDPTL, erxrdpt);
991 #ifdef CONFIG_ENC28J60_WRITEVERIFY
994 reg = nolock_regw_read(priv,
ERXRDPTL);
997 "error (0x%04x - 0x%04x)\n", __func__,
1010 static int enc28j60_get_free_rxfifo(
struct enc28j60_net *priv)
1012 int epkcnt, erxst, erxnd, erxwr, erxrd;
1016 epkcnt = nolock_regb_read(priv,
EPKTCNT);
1020 erxst = nolock_regw_read(priv,
ERXSTL);
1021 erxnd = nolock_regw_read(priv,
ERXNDL);
1022 erxwr = nolock_regw_read(priv,
ERXWRPTL);
1023 erxrd = nolock_regw_read(priv,
ERXRDPTL);
1026 free_space = (erxnd - erxst) - (erxwr - erxrd);
1027 else if (erxwr == erxrd)
1028 free_space = (erxnd - erxst);
1030 free_space = erxrd - erxwr - 1;
1035 __func__, free_space);
1042 static void enc28j60_check_link_status(
struct net_device *ndev)
1048 reg = enc28j60_phy_read(priv,
PHSTAT2);
1051 "PHSTAT2: %04x\n", __func__,
1052 enc28j60_phy_read(priv,
PHSTAT1), reg);
1059 duplex ?
"Full duplex" :
"Half duplex");
1067 static void enc28j60_tx_clear(
struct net_device *ndev,
bool err)
1072 ndev->
stats.tx_errors++;
1074 ndev->
stats.tx_packets++;
1079 dev_kfree_skb(priv->
tx_skb);
1083 netif_wake_queue(ndev);
1094 static int enc28j60_rx_interrupt(
struct net_device *ndev)
1097 int pk_counter,
ret;
1099 pk_counter = locked_regb_read(priv,
EPKTCNT);
1110 while (pk_counter-- > 0)
1111 enc28j60_hw_rx(ndev);
1130 intflags = locked_regb_read(priv,
EIR);
1136 ": intDMA(%d)\n", loop);
1137 locked_reg_bfclr(priv,
EIR, EIR_DMAIF);
1144 ": intLINK(%d)\n", loop);
1145 enc28j60_check_link_status(ndev);
1147 enc28j60_phy_read(priv,
PHIR);
1155 ": intTX(%d)\n", loop);
1160 "Tx Error (aborted)\n");
1165 enc28j60_read_tsv(priv, tsv);
1166 enc28j60_dump_tsv(priv,
"Tx Done", tsv);
1168 enc28j60_tx_clear(ndev, err);
1169 locked_reg_bfclr(priv,
EIR, EIR_TXIF);
1178 ": intTXErr(%d)\n", loop);
1180 enc28j60_read_tsv(priv, tsv);
1182 enc28j60_dump_tsv(priv,
"Tx Error", tsv);
1193 ": LateCollision TXErr (%d)\n",
1196 locked_reg_bfset(priv,
ECON1,
1199 enc28j60_tx_clear(ndev,
true);
1201 enc28j60_tx_clear(ndev,
true);
1202 locked_reg_bfclr(priv,
EIR, EIR_TXERIF);
1209 ": intRXErr(%d)\n", loop);
1211 if (enc28j60_get_free_rxfifo(priv) <= 0) {
1215 ndev->
stats.rx_dropped++;
1217 locked_reg_bfclr(priv,
EIR, EIR_RXERIF);
1220 if (enc28j60_rx_interrupt(ndev))
1239 ": Tx Packet Len:%d\n", priv->
tx_skb->len);
1242 dump_packet(__func__,
1244 enc28j60_packet_write(priv, priv->
tx_skb->len, priv->
tx_skb->data);
1246 #ifdef CONFIG_ENC28J60_WRITEVERIFY
1253 test_len = priv->
tx_skb->len;
1254 if (test_len >
sizeof(test_buf))
1255 test_len =
sizeof(test_buf);
1258 enc28j60_mem_read(priv,
TXSTART_INIT + 1, test_len, test_buf);
1260 for (k = 0; k < test_len; k++) {
1261 if (priv->
tx_skb->data[k] != test_buf[k]) {
1263 ": Error, %d location differ: "
1264 "0x%02x-0x%02x\n", k,
1265 priv->
tx_skb->data[k], test_buf[k]);
1294 netif_stop_queue(dev);
1303 static void enc28j60_tx_work_handler(
struct work_struct *work)
1309 enc28j60_hw_tx(priv);
1328 static void enc28j60_tx_timeout(
struct net_device *ndev)
1333 dev_err(&ndev->
dev, DRV_NAME
" tx timeout\n");
1335 ndev->
stats.tx_errors++;
1348 static int enc28j60_net_open(
struct net_device *dev)
1355 if (!is_valid_ether_addr(dev->
dev_addr)) {
1357 dev_err(&dev->
dev,
"invalid MAC address %pM\n",
1362 enc28j60_lowpower(priv,
false);
1363 enc28j60_hw_disable(priv);
1364 if (!enc28j60_hw_init(priv)) {
1370 enc28j60_set_hw_macaddr(dev);
1372 enc28j60_hw_enable(priv);
1374 enc28j60_check_link_status(dev);
1378 netif_start_queue(dev);
1384 static int enc28j60_net_close(
struct net_device *dev)
1391 enc28j60_hw_disable(priv);
1392 enc28j60_lowpower(priv,
true);
1393 netif_stop_queue(dev);
1404 static void enc28j60_set_multicast_list(
struct net_device *dev)
1428 static void enc28j60_setrx_work_handler(
struct work_struct *work)
1436 locked_regb_write(priv,
ERXFCON, 0x00);
1440 locked_regb_write(priv,
ERXFCON,
1446 locked_regb_write(priv,
ERXFCON,
1452 static void enc28j60_restart_work_handler(
struct work_struct *work)
1460 if (netif_running(ndev)) {
1461 enc28j60_net_close(ndev);
1462 ret = enc28j60_net_open(ndev);
1464 dev_info(&ndev->
dev,
" could not restart %d\n", ret);
1479 dev_name(dev->
dev.parent),
sizeof(info->
bus_info));
1491 ethtool_cmd_speed_set(cmd,
SPEED_10);
1502 return enc28j60_setlink(dev, cmd->
autoneg,
1503 ethtool_cmd_speed(cmd), cmd->
duplex);
1512 static void enc28j60_set_msglevel(
struct net_device *dev,
u32 val)
1518 static const struct ethtool_ops enc28j60_ethtool_ops = {
1519 .get_settings = enc28j60_get_settings,
1520 .set_settings = enc28j60_set_settings,
1521 .get_drvinfo = enc28j60_get_drvinfo,
1522 .get_msglevel = enc28j60_get_msglevel,
1523 .set_msglevel = enc28j60_set_msglevel,
1526 static int enc28j60_chipset_init(
struct net_device *dev)
1530 return enc28j60_hw_init(priv);
1534 .ndo_open = enc28j60_net_open,
1535 .ndo_stop = enc28j60_net_close,
1536 .ndo_start_xmit = enc28j60_send_packet,
1537 .ndo_set_rx_mode = enc28j60_set_multicast_list,
1538 .ndo_set_mac_address = enc28j60_set_mac_address,
1539 .ndo_tx_timeout = enc28j60_tx_timeout,
1551 dev_info(&spi->
dev, DRV_NAME
" Ethernet driver %s loaded\n",
1559 priv = netdev_priv(dev);
1573 if (!enc28j60_chipset_init(dev)) {
1575 dev_info(&spi->
dev, DRV_NAME
" chip not found\n");
1579 eth_hw_addr_random(dev);
1580 enc28j60_set_hw_macaddr(dev);
1588 dev_err(&spi->
dev, DRV_NAME
": request irq %d failed "
1589 "(ret = %d)\n", spi->
irq, ret);
1599 enc28j60_lowpower(priv,
true);
1604 dev_err(&spi->
dev,
"register netdev " DRV_NAME
1605 " failed (ret = %d)\n", ret);
1606 goto error_register;
1608 dev_info(&dev->
dev, DRV_NAME
" driver registered\n");
1639 .probe = enc28j60_probe,
1643 static int __init enc28j60_init(
void)
1652 static void __exit enc28j60_exit(
void)
1654 spi_unregister_driver(&enc28j60_driver);