11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/netdevice.h>
18 #include <linux/ethtool.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
23 #include <linux/slab.h>
31 #define DRV_NAME "w5300"
32 #define DRV_VERSION "2012-04-04"
42 #define W5300_MR 0x0000
43 #define MR_DBW (1 << 15)
44 #define MR_MPF (1 << 14)
45 #define MR_WDF(n) (((n)&7)<<11)
46 #define MR_RDH (1 << 10)
47 #define MR_FS (1 << 8)
48 #define MR_RST (1 << 7)
49 #define MR_PB (1 << 4)
50 #define MR_DBS (1 << 2)
51 #define MR_IND (1 << 0)
52 #define W5300_IR 0x0002
53 #define W5300_IMR 0x0004
55 #define W5300_SHARL 0x0008
56 #define W5300_SHARH 0x000c
57 #define W5300_TMSRL 0x0020
58 #define W5300_TMSRH 0x0024
59 #define W5300_RMSRL 0x0028
60 #define W5300_RMSRH 0x002c
61 #define W5300_MTYPE 0x0030
62 #define W5300_IDR 0x00fe
63 #define IDR_W5300 0x5300
64 #define W5300_S0_MR 0x0200
65 #define S0_MR_CLOSED 0x0000
66 #define S0_MR_MACRAW 0x0004
67 #define S0_MR_MACRAW_MF 0x0044
68 #define W5300_S0_CR 0x0202
69 #define S0_CR_OPEN 0x0001
70 #define S0_CR_CLOSE 0x0010
71 #define S0_CR_SEND 0x0020
72 #define S0_CR_RECV 0x0040
73 #define W5300_S0_IMR 0x0204
74 #define W5300_S0_IR 0x0206
75 #define S0_IR_RECV 0x0004
76 #define S0_IR_SENDOK 0x0010
77 #define W5300_S0_SSR 0x0208
78 #define W5300_S0_TX_WRSR 0x0220
79 #define W5300_S0_TX_FSR 0x0224
80 #define W5300_S0_RX_RSR 0x0228
81 #define W5300_S0_TX_FIFO 0x022e
82 #define W5300_S0_RX_FIFO 0x0230
83 #define W5300_REGS_LEN 0x0400
135 #define W5300_IDM_AR 0x0002
136 #define W5300_IDM_DR 0x0004
147 spin_unlock_irqrestore(&priv->
reg_lock, flags);
161 spin_unlock_irqrestore(&priv->
reg_lock, flags);
164 #if defined(CONFIG_WIZNET_BUS_DIRECT)
165 #define w5300_read w5300_read_direct
166 #define w5300_write w5300_write_direct
168 #elif defined(CONFIG_WIZNET_BUS_INDIRECT)
169 #define w5300_read w5300_read_indirect
170 #define w5300_write w5300_write_indirect
173 #define w5300_read priv->read
174 #define w5300_write priv->write
207 static void w5300_read_frame(
struct w5300_priv *priv,
u8 *
buf,
int len)
212 for (i = 0; i < len; i += 2) {
221 static void w5300_write_frame(
struct w5300_priv *priv,
u8 *buf,
int len)
226 for (i = 0; i < len; i += 2) {
234 static void w5300_write_macaddr(
struct w5300_priv *priv)
248 static void w5300_hw_reset(
struct w5300_priv *priv)
258 w5300_write_macaddr(priv);
271 static void w5300_hw_start(
struct w5300_priv *priv)
282 static void w5300_hw_close(
struct w5300_priv *priv)
328 static int w5300_get_regs_len(
struct net_device *ndev)
333 static void w5300_get_regs(
struct net_device *ndev,
343 switch (addr & 0x23f) {
357 static void w5300_tx_timeout(
struct net_device *ndev)
361 netif_stop_queue(ndev);
362 w5300_hw_reset(priv);
363 w5300_hw_start(priv);
364 ndev->
stats.tx_errors++;
366 netif_wake_queue(ndev);
373 netif_stop_queue(ndev);
375 w5300_write_frame(priv, skb->
data, skb->
len);
377 ndev->
stats.tx_packets++;
380 netif_dbg(priv, tx_queued, ndev,
"tx queued\n");
395 for (rx_count = 0; rx_count < budget; rx_count++) {
397 if (rx_fifo_len == 0)
402 skb = netdev_alloc_skb_ip_align(ndev,
roundup(rx_len, 2));
405 for (i = 0; i < rx_fifo_len; i += 2)
407 ndev->
stats.rx_dropped++;
412 w5300_read_frame(priv, skb->
data, rx_len);
416 ndev->
stats.rx_packets++;
417 ndev->
stats.rx_bytes += rx_len;
420 if (rx_count < budget) {
429 static irqreturn_t w5300_interrupt(
int irq,
void *ndev_instance)
442 netif_wake_queue(ndev);
446 if (napi_schedule_prep(&priv->
napi)) {
456 static irqreturn_t w5300_detect_link(
int irq,
void *ndev_instance)
461 if (netif_running(ndev)) {
474 static void w5300_set_rx_mode(
struct net_device *ndev)
479 if (priv->
promisc != set_promisc) {
481 w5300_hw_start(priv);
485 static int w5300_set_macaddr(
struct net_device *ndev,
void *addr)
490 if (!is_valid_ether_addr(sock_addr->
sa_data))
494 w5300_write_macaddr(priv);
498 static int w5300_open(
struct net_device *ndev)
503 if (!is_valid_ether_addr(ndev->
dev_addr))
505 w5300_hw_start(priv);
506 napi_enable(&priv->
napi);
507 netif_start_queue(ndev);
514 static int w5300_stop(
struct net_device *ndev)
518 netif_info(priv, ifdown, ndev,
"shutting down\n");
519 w5300_hw_close(priv);
521 netif_stop_queue(ndev);
522 napi_disable(&priv->
napi);
526 static const struct ethtool_ops w5300_ethtool_ops = {
527 .get_drvinfo = w5300_get_drvinfo,
528 .get_msglevel = w5300_get_msglevel,
529 .set_msglevel = w5300_set_msglevel,
530 .get_link = w5300_get_link,
531 .get_regs_len = w5300_get_regs_len,
532 .get_regs = w5300_get_regs,
536 .ndo_open = w5300_open,
537 .ndo_stop = w5300_stop,
538 .ndo_start_xmit = w5300_start_tx,
539 .ndo_tx_timeout = w5300_tx_timeout,
540 .ndo_set_rx_mode = w5300_set_rx_mode,
541 .ndo_set_mac_address = w5300_set_macaddr,
549 struct net_device *ndev = platform_get_drvdata(pdev);
551 const char *
name = netdev_name(ndev);
557 if (data && is_valid_ether_addr(data->
mac_addr)) {
560 eth_hw_addr_random(ndev);
566 mem_size = resource_size(mem);
576 priv->
read = w5300_read_indirect;
577 priv->
write = w5300_write_indirect;
579 priv->
read = w5300_read_direct;
580 priv->
write = w5300_write_direct;
583 w5300_hw_reset(priv);
601 snprintf(link_name, 16,
"%s-link", name);
605 link_name, priv->
ndev) < 0)
609 netdev_info(ndev,
"at 0x%llx irq %d\n", (
u64)mem->
start, irq);
619 ndev = alloc_etherdev(
sizeof(*priv));
623 platform_set_drvdata(pdev, ndev);
624 priv = netdev_priv(ndev);
642 err = w5300_hw_probe(pdev);
652 platform_set_drvdata(pdev,
NULL);
658 struct net_device *ndev = platform_get_drvdata(pdev);
661 w5300_hw_reset(priv);
668 platform_set_drvdata(pdev,
NULL);
673 static int w5300_suspend(
struct device *
dev)
676 struct net_device *ndev = platform_get_drvdata(pdev);
679 if (netif_running(ndev)) {
683 w5300_hw_close(priv);
688 static int w5300_resume(
struct device *dev)
691 struct net_device *ndev = platform_get_drvdata(pdev);
694 if (!netif_running(ndev)) {
695 w5300_hw_reset(priv);
696 w5300_hw_start(priv);
715 .probe = w5300_probe,