10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/netdevice.h>
17 #include <linux/ethtool.h>
19 #include <linux/types.h>
20 #include <linux/errno.h>
22 #include <linux/slab.h>
30 #define DRV_NAME "w5100"
31 #define DRV_VERSION "2012-04-04"
41 #define W5100_COMMON_REGS 0x0000
42 #define W5100_MR 0x0000
47 #define W5100_SHAR 0x0009
48 #define W5100_IR 0x0015
49 #define W5100_IMR 0x0016
51 #define W5100_RTR 0x0017
52 #define RTR_DEFAULT 2000
53 #define W5100_RMSR 0x001a
54 #define W5100_TMSR 0x001b
55 #define W5100_COMMON_REGS_LEN 0x0040
57 #define W5100_S0_REGS 0x0400
58 #define W5100_S0_MR 0x0400
59 #define S0_MR_MACRAW 0x04
60 #define S0_MR_MACRAW_MF 0x44
61 #define W5100_S0_CR 0x0401
62 #define S0_CR_OPEN 0x01
63 #define S0_CR_CLOSE 0x10
64 #define S0_CR_SEND 0x20
65 #define S0_CR_RECV 0x40
66 #define W5100_S0_IR 0x0402
67 #define S0_IR_SENDOK 0x10
68 #define S0_IR_RECV 0x04
69 #define W5100_S0_SR 0x0403
70 #define S0_SR_MACRAW 0x42
71 #define W5100_S0_TX_FSR 0x0420
72 #define W5100_S0_TX_RD 0x0422
73 #define W5100_S0_TX_WR 0x0424
74 #define W5100_S0_RX_RSR 0x0426
75 #define W5100_S0_RX_RD 0x0428
76 #define W5100_S0_REGS_LEN 0x0040
78 #define W5100_TX_MEM_START 0x4000
79 #define W5100_TX_MEM_END 0x5fff
80 #define W5100_TX_MEM_MASK 0x1fff
81 #define W5100_RX_MEM_START 0x6000
82 #define W5100_RX_MEM_END 0x7fff
83 #define W5100_RX_MEM_MASK 0x1fff
134 data = w5100_read_direct(priv, addr) << 8;
135 data |= w5100_read_direct(priv, addr + 1);
141 w5100_write_direct(priv, addr, data >> 8);
142 w5100_write_direct(priv, addr + 1, data);
151 for (i = 0; i < len; i++, addr++) {
154 *buf++ = w5100_read_direct(priv, addr);
158 static void w5100_writebuf_direct(
struct w5100_priv *priv,
159 u16 offset,
u8 *buf,
int len)
164 for (i = 0; i < len; i++, addr++) {
167 w5100_write_direct(priv, addr, *buf++);
179 #define W5100_IDM_AR 0x01
180 #define W5100_IDM_DR 0x03
191 spin_unlock_irqrestore(&priv->
reg_lock, flags);
205 spin_unlock_irqrestore(&priv->
reg_lock, flags);
218 spin_unlock_irqrestore(&priv->
reg_lock, flags);
233 spin_unlock_irqrestore(&priv->
reg_lock, flags);
236 static void w5100_readbuf_indirect(
struct w5100_priv *priv,
237 u16 offset,
u8 *buf,
int len)
247 for (i = 0; i < len; i++, addr++) {
256 spin_unlock_irqrestore(&priv->
reg_lock, flags);
259 static void w5100_writebuf_indirect(
struct w5100_priv *priv,
260 u16 offset,
u8 *buf,
int len)
270 for (i = 0; i < len; i++, addr++) {
279 spin_unlock_irqrestore(&priv->
reg_lock, flags);
282 #if defined(CONFIG_WIZNET_BUS_DIRECT)
283 #define w5100_read w5100_read_direct
284 #define w5100_write w5100_write_direct
285 #define w5100_read16 w5100_read16_direct
286 #define w5100_write16 w5100_write16_direct
287 #define w5100_readbuf w5100_readbuf_direct
288 #define w5100_writebuf w5100_writebuf_direct
290 #elif defined(CONFIG_WIZNET_BUS_INDIRECT)
291 #define w5100_read w5100_read_indirect
292 #define w5100_write w5100_write_indirect
293 #define w5100_read16 w5100_read16_indirect
294 #define w5100_write16 w5100_write16_indirect
295 #define w5100_readbuf w5100_readbuf_indirect
296 #define w5100_writebuf w5100_writebuf_indirect
299 #define w5100_read priv->read
300 #define w5100_write priv->write
301 #define w5100_read16 priv->read16
302 #define w5100_write16 priv->write16
303 #define w5100_readbuf priv->readbuf
304 #define w5100_writebuf priv->writebuf
323 static void w5100_write_macaddr(
struct w5100_priv *priv)
333 static void w5100_hw_reset(
struct w5100_priv *priv)
343 w5100_write_macaddr(priv);
353 static void w5100_hw_start(
struct w5100_priv *priv)
363 static void w5100_hw_close(
struct w5100_priv *priv)
376 static void w5100_get_drvinfo(
struct net_device *ndev,
409 static int w5100_get_regs_len(
struct net_device *ndev)
414 static void w5100_get_regs(
struct net_device *ndev,
428 static void w5100_tx_timeout(
struct net_device *ndev)
432 netif_stop_queue(ndev);
433 w5100_hw_reset(priv);
434 w5100_hw_start(priv);
435 ndev->
stats.tx_errors++;
437 netif_wake_queue(ndev);
445 netif_stop_queue(ndev);
452 ndev->
stats.tx_packets++;
470 for (rx_count = 0; rx_count < budget; rx_count++) {
477 rx_len = get_unaligned_be16(header) - 2;
479 skb = netdev_alloc_skb_ip_align(ndev, rx_len);
482 offset + rx_buf_len);
484 ndev->
stats.rx_dropped++;
496 ndev->
stats.rx_packets++;
497 ndev->
stats.rx_bytes += rx_len;
500 if (rx_count < budget) {
509 static irqreturn_t w5100_interrupt(
int irq,
void *ndev_instance)
522 netif_wake_queue(ndev);
526 if (napi_schedule_prep(&priv->
napi)) {
536 static irqreturn_t w5100_detect_link(
int irq,
void *ndev_instance)
541 if (netif_running(ndev)) {
554 static void w5100_set_rx_mode(
struct net_device *ndev)
559 if (priv->
promisc != set_promisc) {
561 w5100_hw_start(priv);
565 static int w5100_set_macaddr(
struct net_device *ndev,
void *addr)
570 if (!is_valid_ether_addr(sock_addr->
sa_data))
574 w5100_write_macaddr(priv);
578 static int w5100_open(
struct net_device *ndev)
583 if (!is_valid_ether_addr(ndev->
dev_addr))
585 w5100_hw_start(priv);
586 napi_enable(&priv->
napi);
587 netif_start_queue(ndev);
594 static int w5100_stop(
struct net_device *ndev)
598 netif_info(priv, ifdown, ndev,
"shutting down\n");
599 w5100_hw_close(priv);
601 netif_stop_queue(ndev);
602 napi_disable(&priv->
napi);
606 static const struct ethtool_ops w5100_ethtool_ops = {
607 .get_drvinfo = w5100_get_drvinfo,
608 .get_msglevel = w5100_get_msglevel,
609 .set_msglevel = w5100_set_msglevel,
610 .get_link = w5100_get_link,
611 .get_regs_len = w5100_get_regs_len,
612 .get_regs = w5100_get_regs,
616 .ndo_open = w5100_open,
617 .ndo_stop = w5100_stop,
618 .ndo_start_xmit = w5100_start_tx,
619 .ndo_tx_timeout = w5100_tx_timeout,
620 .ndo_set_rx_mode = w5100_set_rx_mode,
621 .ndo_set_mac_address = w5100_set_macaddr,
629 struct net_device *ndev = platform_get_drvdata(pdev);
631 const char *
name = netdev_name(ndev);
637 if (data && is_valid_ether_addr(data->
mac_addr)) {
640 eth_hw_addr_random(ndev);
646 mem_size = resource_size(mem);
656 priv->
read = w5100_read_indirect;
657 priv->
write = w5100_write_indirect;
658 priv->
read16 = w5100_read16_indirect;
659 priv->
write16 = w5100_write16_indirect;
660 priv->
readbuf = w5100_readbuf_indirect;
661 priv->
writebuf = w5100_writebuf_indirect;
663 priv->
read = w5100_read_direct;
664 priv->
write = w5100_write_direct;
665 priv->
read16 = w5100_read16_direct;
666 priv->
write16 = w5100_write16_direct;
667 priv->
readbuf = w5100_readbuf_direct;
668 priv->
writebuf = w5100_writebuf_direct;
671 w5100_hw_reset(priv);
689 snprintf(link_name, 16,
"%s-link", name);
693 link_name, priv->
ndev) < 0)
697 netdev_info(ndev,
"at 0x%llx irq %d\n", (
u64)mem->
start, irq);
707 ndev = alloc_etherdev(
sizeof(*priv));
711 platform_set_drvdata(pdev, ndev);
712 priv = netdev_priv(ndev);
730 err = w5100_hw_probe(pdev);
740 platform_set_drvdata(pdev,
NULL);
746 struct net_device *ndev = platform_get_drvdata(pdev);
749 w5100_hw_reset(priv);
756 platform_set_drvdata(pdev,
NULL);
761 static int w5100_suspend(
struct device *
dev)
764 struct net_device *ndev = platform_get_drvdata(pdev);
767 if (netif_running(ndev)) {
771 w5100_hw_close(priv);
776 static int w5100_resume(
struct device *dev)
779 struct net_device *ndev = platform_get_drvdata(pdev);
782 if (netif_running(ndev)) {
783 w5100_hw_reset(priv);
784 w5100_hw_start(priv);
803 .probe = w5100_probe,