2 * Ethernet driver for the WIZnet W5100 chip.
4 * Copyright (C) 2006-2008 WIZnet Co.,Ltd.
5 * Copyright (C) 2012 Mike Sinkovsky <msink@permonline.ru>
7 * Licensed under the GPL-2 or later.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/platform_device.h>
15 #include <linux/platform_data/wiznet.h>
16 #include <linux/ethtool.h>
17 #include <linux/skbuff.h>
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
24 #include <linux/ioport.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/gpio.h>
31 #define DRV_NAME "w5100"
32 #define DRV_VERSION "2012-04-04"
34 MODULE_DESCRIPTION("WIZnet W5100 Ethernet driver v"DRV_VERSION
);
35 MODULE_AUTHOR("Mike Sinkovsky <msink@permonline.ru>");
36 MODULE_ALIAS("platform:"DRV_NAME
);
37 MODULE_LICENSE("GPL");
40 * W5100/W5200/W5500 common registers
42 #define W5100_COMMON_REGS 0x0000
43 #define W5100_MR 0x0000 /* Mode Register */
44 #define MR_RST 0x80 /* S/W reset */
45 #define MR_PB 0x10 /* Ping block */
46 #define MR_AI 0x02 /* Address Auto-Increment */
47 #define MR_IND 0x01 /* Indirect mode */
48 #define W5100_SHAR 0x0009 /* Source MAC address */
49 #define W5100_IR 0x0015 /* Interrupt Register */
50 #define W5100_COMMON_REGS_LEN 0x0040
52 #define W5100_Sn_MR 0x0000 /* Sn Mode Register */
53 #define W5100_Sn_CR 0x0001 /* Sn Command Register */
54 #define W5100_Sn_IR 0x0002 /* Sn Interrupt Register */
55 #define W5100_Sn_SR 0x0003 /* Sn Status Register */
56 #define W5100_Sn_TX_FSR 0x0020 /* Sn Transmit free memory size */
57 #define W5100_Sn_TX_RD 0x0022 /* Sn Transmit memory read pointer */
58 #define W5100_Sn_TX_WR 0x0024 /* Sn Transmit memory write pointer */
59 #define W5100_Sn_RX_RSR 0x0026 /* Sn Receive free memory size */
60 #define W5100_Sn_RX_RD 0x0028 /* Sn Receive memory read pointer */
62 #define S0_REGS(priv) ((priv)->s0_regs)
64 #define W5100_S0_MR(priv) (S0_REGS(priv) + W5100_Sn_MR)
65 #define S0_MR_MACRAW 0x04 /* MAC RAW mode */
66 #define S0_MR_MF 0x40 /* MAC Filter for W5100 and W5200 */
67 #define W5500_S0_MR_MF 0x80 /* MAC Filter for W5500 */
68 #define W5100_S0_CR(priv) (S0_REGS(priv) + W5100_Sn_CR)
69 #define S0_CR_OPEN 0x01 /* OPEN command */
70 #define S0_CR_CLOSE 0x10 /* CLOSE command */
71 #define S0_CR_SEND 0x20 /* SEND command */
72 #define S0_CR_RECV 0x40 /* RECV command */
73 #define W5100_S0_IR(priv) (S0_REGS(priv) + W5100_Sn_IR)
74 #define S0_IR_SENDOK 0x10 /* complete sending */
75 #define S0_IR_RECV 0x04 /* receiving data */
76 #define W5100_S0_SR(priv) (S0_REGS(priv) + W5100_Sn_SR)
77 #define S0_SR_MACRAW 0x42 /* mac raw mode */
78 #define W5100_S0_TX_FSR(priv) (S0_REGS(priv) + W5100_Sn_TX_FSR)
79 #define W5100_S0_TX_RD(priv) (S0_REGS(priv) + W5100_Sn_TX_RD)
80 #define W5100_S0_TX_WR(priv) (S0_REGS(priv) + W5100_Sn_TX_WR)
81 #define W5100_S0_RX_RSR(priv) (S0_REGS(priv) + W5100_Sn_RX_RSR)
82 #define W5100_S0_RX_RD(priv) (S0_REGS(priv) + W5100_Sn_RX_RD)
84 #define W5100_S0_REGS_LEN 0x0040
87 * W5100 and W5200 common registers
89 #define W5100_IMR 0x0016 /* Interrupt Mask Register */
90 #define IR_S0 0x01 /* S0 interrupt */
91 #define W5100_RTR 0x0017 /* Retry Time-value Register */
92 #define RTR_DEFAULT 2000 /* =0x07d0 (2000) */
95 * W5100 specific register and memory
97 #define W5100_RMSR 0x001a /* Receive Memory Size */
98 #define W5100_TMSR 0x001b /* Transmit Memory Size */
100 #define W5100_S0_REGS 0x0400
102 #define W5100_TX_MEM_START 0x4000
103 #define W5100_TX_MEM_SIZE 0x2000
104 #define W5100_RX_MEM_START 0x6000
105 #define W5100_RX_MEM_SIZE 0x2000
108 * W5200 specific register and memory
110 #define W5200_S0_REGS 0x4000
112 #define W5200_Sn_RXMEM_SIZE(n) (0x401e + (n) * 0x0100) /* Sn RX Memory Size */
113 #define W5200_Sn_TXMEM_SIZE(n) (0x401f + (n) * 0x0100) /* Sn TX Memory Size */
115 #define W5200_TX_MEM_START 0x8000
116 #define W5200_TX_MEM_SIZE 0x4000
117 #define W5200_RX_MEM_START 0xc000
118 #define W5200_RX_MEM_SIZE 0x4000
121 * W5500 specific register and memory
123 * W5500 register and memory are organized by multiple blocks. Each one is
124 * selected by 16bits offset address and 5bits block select bits. So we
125 * encode it into 32bits address. (lower 16bits is offset address and
126 * upper 16bits is block select bits)
128 #define W5500_SIMR 0x0018 /* Socket Interrupt Mask Register */
129 #define W5500_RTR 0x0019 /* Retry Time-value Register */
131 #define W5500_S0_REGS 0x10000
133 #define W5500_Sn_RXMEM_SIZE(n) \
134 (0x1001e + (n) * 0x40000) /* Sn RX Memory Size */
135 #define W5500_Sn_TXMEM_SIZE(n) \
136 (0x1001f + (n) * 0x40000) /* Sn TX Memory Size */
138 #define W5500_TX_MEM_START 0x20000
139 #define W5500_TX_MEM_SIZE 0x04000
140 #define W5500_RX_MEM_START 0x30000
141 #define W5500_RX_MEM_SIZE 0x04000
144 * Device driver private data structure
148 const struct w5100_ops
*ops
;
150 /* Socket 0 register offset address */
152 /* Socket 0 TX buffer offset address and size */
155 /* Socket 0 RX buffer offset address and size */
163 struct napi_struct napi
;
164 struct net_device
*ndev
;
168 struct workqueue_struct
*xfer_wq
;
169 struct work_struct rx_work
;
170 struct sk_buff
*tx_skb
;
171 struct work_struct tx_work
;
172 struct work_struct setrx_work
;
173 struct work_struct restart_work
;
176 /************************************************************************
178 * Lowlevel I/O functions
180 ***********************************************************************/
182 struct w5100_mmio_priv
{
184 /* Serialize access in indirect address mode */
188 static inline struct w5100_mmio_priv
*w5100_mmio_priv(struct net_device
*dev
)
190 return w5100_ops_priv(dev
);
193 static inline void __iomem
*w5100_mmio(struct net_device
*ndev
)
195 struct w5100_mmio_priv
*mmio_priv
= w5100_mmio_priv(ndev
);
197 return mmio_priv
->base
;
201 * In direct address mode host system can directly access W5100 registers
202 * after mapping to Memory-Mapped I/O space.
204 * 0x8000 bytes are required for memory space.
206 static inline int w5100_read_direct(struct net_device
*ndev
, u32 addr
)
208 return ioread8(w5100_mmio(ndev
) + (addr
<< CONFIG_WIZNET_BUS_SHIFT
));
211 static inline int __w5100_write_direct(struct net_device
*ndev
, u32 addr
,
214 iowrite8(data
, w5100_mmio(ndev
) + (addr
<< CONFIG_WIZNET_BUS_SHIFT
));
219 static inline int w5100_write_direct(struct net_device
*ndev
, u32 addr
, u8 data
)
221 __w5100_write_direct(ndev
, addr
, data
);
227 static int w5100_read16_direct(struct net_device
*ndev
, u32 addr
)
230 data
= w5100_read_direct(ndev
, addr
) << 8;
231 data
|= w5100_read_direct(ndev
, addr
+ 1);
235 static int w5100_write16_direct(struct net_device
*ndev
, u32 addr
, u16 data
)
237 __w5100_write_direct(ndev
, addr
, data
>> 8);
238 __w5100_write_direct(ndev
, addr
+ 1, data
);
244 static int w5100_readbulk_direct(struct net_device
*ndev
, u32 addr
, u8
*buf
,
249 for (i
= 0; i
< len
; i
++, addr
++)
250 *buf
++ = w5100_read_direct(ndev
, addr
);
255 static int w5100_writebulk_direct(struct net_device
*ndev
, u32 addr
,
256 const u8
*buf
, int len
)
260 for (i
= 0; i
< len
; i
++, addr
++)
261 __w5100_write_direct(ndev
, addr
, *buf
++);
268 static int w5100_mmio_init(struct net_device
*ndev
)
270 struct platform_device
*pdev
= to_platform_device(ndev
->dev
.parent
);
271 struct w5100_priv
*priv
= netdev_priv(ndev
);
272 struct w5100_mmio_priv
*mmio_priv
= w5100_mmio_priv(ndev
);
273 struct resource
*mem
;
275 spin_lock_init(&mmio_priv
->reg_lock
);
277 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
278 mmio_priv
->base
= devm_ioremap_resource(&pdev
->dev
, mem
);
279 if (IS_ERR(mmio_priv
->base
))
280 return PTR_ERR(mmio_priv
->base
);
282 netdev_info(ndev
, "at 0x%llx irq %d\n", (u64
)mem
->start
, priv
->irq
);
287 static const struct w5100_ops w5100_mmio_direct_ops
= {
289 .read
= w5100_read_direct
,
290 .write
= w5100_write_direct
,
291 .read16
= w5100_read16_direct
,
292 .write16
= w5100_write16_direct
,
293 .readbulk
= w5100_readbulk_direct
,
294 .writebulk
= w5100_writebulk_direct
,
295 .init
= w5100_mmio_init
,
299 * In indirect address mode host system indirectly accesses registers by
300 * using Indirect Mode Address Register (IDM_AR) and Indirect Mode Data
301 * Register (IDM_DR), which are directly mapped to Memory-Mapped I/O space.
302 * Mode Register (MR) is directly accessible.
304 * Only 0x04 bytes are required for memory space.
306 #define W5100_IDM_AR 0x01 /* Indirect Mode Address Register */
307 #define W5100_IDM_DR 0x03 /* Indirect Mode Data Register */
309 static int w5100_read_indirect(struct net_device
*ndev
, u32 addr
)
311 struct w5100_mmio_priv
*mmio_priv
= w5100_mmio_priv(ndev
);
315 spin_lock_irqsave(&mmio_priv
->reg_lock
, flags
);
316 w5100_write16_direct(ndev
, W5100_IDM_AR
, addr
);
317 data
= w5100_read_direct(ndev
, W5100_IDM_DR
);
318 spin_unlock_irqrestore(&mmio_priv
->reg_lock
, flags
);
323 static int w5100_write_indirect(struct net_device
*ndev
, u32 addr
, u8 data
)
325 struct w5100_mmio_priv
*mmio_priv
= w5100_mmio_priv(ndev
);
328 spin_lock_irqsave(&mmio_priv
->reg_lock
, flags
);
329 w5100_write16_direct(ndev
, W5100_IDM_AR
, addr
);
330 w5100_write_direct(ndev
, W5100_IDM_DR
, data
);
331 spin_unlock_irqrestore(&mmio_priv
->reg_lock
, flags
);
336 static int w5100_read16_indirect(struct net_device
*ndev
, u32 addr
)
338 struct w5100_mmio_priv
*mmio_priv
= w5100_mmio_priv(ndev
);
342 spin_lock_irqsave(&mmio_priv
->reg_lock
, flags
);
343 w5100_write16_direct(ndev
, W5100_IDM_AR
, addr
);
344 data
= w5100_read_direct(ndev
, W5100_IDM_DR
) << 8;
345 data
|= w5100_read_direct(ndev
, W5100_IDM_DR
);
346 spin_unlock_irqrestore(&mmio_priv
->reg_lock
, flags
);
351 static int w5100_write16_indirect(struct net_device
*ndev
, u32 addr
, u16 data
)
353 struct w5100_mmio_priv
*mmio_priv
= w5100_mmio_priv(ndev
);
356 spin_lock_irqsave(&mmio_priv
->reg_lock
, flags
);
357 w5100_write16_direct(ndev
, W5100_IDM_AR
, addr
);
358 __w5100_write_direct(ndev
, W5100_IDM_DR
, data
>> 8);
359 w5100_write_direct(ndev
, W5100_IDM_DR
, data
);
360 spin_unlock_irqrestore(&mmio_priv
->reg_lock
, flags
);
365 static int w5100_readbulk_indirect(struct net_device
*ndev
, u32 addr
, u8
*buf
,
368 struct w5100_mmio_priv
*mmio_priv
= w5100_mmio_priv(ndev
);
372 spin_lock_irqsave(&mmio_priv
->reg_lock
, flags
);
373 w5100_write16_direct(ndev
, W5100_IDM_AR
, addr
);
375 for (i
= 0; i
< len
; i
++)
376 *buf
++ = w5100_read_direct(ndev
, W5100_IDM_DR
);
379 spin_unlock_irqrestore(&mmio_priv
->reg_lock
, flags
);
384 static int w5100_writebulk_indirect(struct net_device
*ndev
, u32 addr
,
385 const u8
*buf
, int len
)
387 struct w5100_mmio_priv
*mmio_priv
= w5100_mmio_priv(ndev
);
391 spin_lock_irqsave(&mmio_priv
->reg_lock
, flags
);
392 w5100_write16_direct(ndev
, W5100_IDM_AR
, addr
);
394 for (i
= 0; i
< len
; i
++)
395 __w5100_write_direct(ndev
, W5100_IDM_DR
, *buf
++);
398 spin_unlock_irqrestore(&mmio_priv
->reg_lock
, flags
);
403 static int w5100_reset_indirect(struct net_device
*ndev
)
405 w5100_write_direct(ndev
, W5100_MR
, MR_RST
);
407 w5100_write_direct(ndev
, W5100_MR
, MR_PB
| MR_AI
| MR_IND
);
412 static const struct w5100_ops w5100_mmio_indirect_ops
= {
414 .read
= w5100_read_indirect
,
415 .write
= w5100_write_indirect
,
416 .read16
= w5100_read16_indirect
,
417 .write16
= w5100_write16_indirect
,
418 .readbulk
= w5100_readbulk_indirect
,
419 .writebulk
= w5100_writebulk_indirect
,
420 .init
= w5100_mmio_init
,
421 .reset
= w5100_reset_indirect
,
424 #if defined(CONFIG_WIZNET_BUS_DIRECT)
426 static int w5100_read(struct w5100_priv
*priv
, u32 addr
)
428 return w5100_read_direct(priv
->ndev
, addr
);
431 static int w5100_write(struct w5100_priv
*priv
, u32 addr
, u8 data
)
433 return w5100_write_direct(priv
->ndev
, addr
, data
);
436 static int w5100_read16(struct w5100_priv
*priv
, u32 addr
)
438 return w5100_read16_direct(priv
->ndev
, addr
);
441 static int w5100_write16(struct w5100_priv
*priv
, u32 addr
, u16 data
)
443 return w5100_write16_direct(priv
->ndev
, addr
, data
);
446 static int w5100_readbulk(struct w5100_priv
*priv
, u32 addr
, u8
*buf
, int len
)
448 return w5100_readbulk_direct(priv
->ndev
, addr
, buf
, len
);
451 static int w5100_writebulk(struct w5100_priv
*priv
, u32 addr
, const u8
*buf
,
454 return w5100_writebulk_direct(priv
->ndev
, addr
, buf
, len
);
457 #elif defined(CONFIG_WIZNET_BUS_INDIRECT)
459 static int w5100_read(struct w5100_priv
*priv
, u32 addr
)
461 return w5100_read_indirect(priv
->ndev
, addr
);
464 static int w5100_write(struct w5100_priv
*priv
, u32 addr
, u8 data
)
466 return w5100_write_indirect(priv
->ndev
, addr
, data
);
469 static int w5100_read16(struct w5100_priv
*priv
, u32 addr
)
471 return w5100_read16_indirect(priv
->ndev
, addr
);
474 static int w5100_write16(struct w5100_priv
*priv
, u32 addr
, u16 data
)
476 return w5100_write16_indirect(priv
->ndev
, addr
, data
);
479 static int w5100_readbulk(struct w5100_priv
*priv
, u32 addr
, u8
*buf
, int len
)
481 return w5100_readbulk_indirect(priv
->ndev
, addr
, buf
, len
);
484 static int w5100_writebulk(struct w5100_priv
*priv
, u32 addr
, const u8
*buf
,
487 return w5100_writebulk_indirect(priv
->ndev
, addr
, buf
, len
);
490 #else /* CONFIG_WIZNET_BUS_ANY */
492 static int w5100_read(struct w5100_priv
*priv
, u32 addr
)
494 return priv
->ops
->read(priv
->ndev
, addr
);
497 static int w5100_write(struct w5100_priv
*priv
, u32 addr
, u8 data
)
499 return priv
->ops
->write(priv
->ndev
, addr
, data
);
502 static int w5100_read16(struct w5100_priv
*priv
, u32 addr
)
504 return priv
->ops
->read16(priv
->ndev
, addr
);
507 static int w5100_write16(struct w5100_priv
*priv
, u32 addr
, u16 data
)
509 return priv
->ops
->write16(priv
->ndev
, addr
, data
);
512 static int w5100_readbulk(struct w5100_priv
*priv
, u32 addr
, u8
*buf
, int len
)
514 return priv
->ops
->readbulk(priv
->ndev
, addr
, buf
, len
);
517 static int w5100_writebulk(struct w5100_priv
*priv
, u32 addr
, const u8
*buf
,
520 return priv
->ops
->writebulk(priv
->ndev
, addr
, buf
, len
);
525 static int w5100_readbuf(struct w5100_priv
*priv
, u16 offset
, u8
*buf
, int len
)
530 const u32 mem_start
= priv
->s0_rx_buf
;
531 const u16 mem_size
= priv
->s0_rx_buf_size
;
534 addr
= mem_start
+ offset
;
536 if (offset
+ len
> mem_size
) {
537 remain
= (offset
+ len
) % mem_size
;
538 len
= mem_size
- offset
;
541 ret
= w5100_readbulk(priv
, addr
, buf
, len
);
545 return w5100_readbulk(priv
, mem_start
, buf
+ len
, remain
);
548 static int w5100_writebuf(struct w5100_priv
*priv
, u16 offset
, const u8
*buf
,
554 const u32 mem_start
= priv
->s0_tx_buf
;
555 const u16 mem_size
= priv
->s0_tx_buf_size
;
558 addr
= mem_start
+ offset
;
560 if (offset
+ len
> mem_size
) {
561 remain
= (offset
+ len
) % mem_size
;
562 len
= mem_size
- offset
;
565 ret
= w5100_writebulk(priv
, addr
, buf
, len
);
569 return w5100_writebulk(priv
, mem_start
, buf
+ len
, remain
);
572 static int w5100_reset(struct w5100_priv
*priv
)
574 if (priv
->ops
->reset
)
575 return priv
->ops
->reset(priv
->ndev
);
577 w5100_write(priv
, W5100_MR
, MR_RST
);
579 w5100_write(priv
, W5100_MR
, MR_PB
);
584 static int w5100_command(struct w5100_priv
*priv
, u16 cmd
)
586 unsigned long timeout
;
588 w5100_write(priv
, W5100_S0_CR(priv
), cmd
);
590 timeout
= jiffies
+ msecs_to_jiffies(100);
592 while (w5100_read(priv
, W5100_S0_CR(priv
)) != 0) {
593 if (time_after(jiffies
, timeout
))
601 static void w5100_write_macaddr(struct w5100_priv
*priv
)
603 struct net_device
*ndev
= priv
->ndev
;
605 w5100_writebulk(priv
, W5100_SHAR
, ndev
->dev_addr
, ETH_ALEN
);
608 static void w5100_socket_intr_mask(struct w5100_priv
*priv
, u8 mask
)
612 if (priv
->ops
->chip_id
== W5500
)
617 w5100_write(priv
, imr
, mask
);
620 static void w5100_enable_intr(struct w5100_priv
*priv
)
622 w5100_socket_intr_mask(priv
, IR_S0
);
625 static void w5100_disable_intr(struct w5100_priv
*priv
)
627 w5100_socket_intr_mask(priv
, 0);
630 static void w5100_memory_configure(struct w5100_priv
*priv
)
632 /* Configure 16K of internal memory
633 * as 8K RX buffer and 8K TX buffer
635 w5100_write(priv
, W5100_RMSR
, 0x03);
636 w5100_write(priv
, W5100_TMSR
, 0x03);
639 static void w5200_memory_configure(struct w5100_priv
*priv
)
643 /* Configure internal RX memory as 16K RX buffer and
644 * internal TX memory as 16K TX buffer
646 w5100_write(priv
, W5200_Sn_RXMEM_SIZE(0), 0x10);
647 w5100_write(priv
, W5200_Sn_TXMEM_SIZE(0), 0x10);
649 for (i
= 1; i
< 8; i
++) {
650 w5100_write(priv
, W5200_Sn_RXMEM_SIZE(i
), 0);
651 w5100_write(priv
, W5200_Sn_TXMEM_SIZE(i
), 0);
655 static void w5500_memory_configure(struct w5100_priv
*priv
)
659 /* Configure internal RX memory as 16K RX buffer and
660 * internal TX memory as 16K TX buffer
662 w5100_write(priv
, W5500_Sn_RXMEM_SIZE(0), 0x10);
663 w5100_write(priv
, W5500_Sn_TXMEM_SIZE(0), 0x10);
665 for (i
= 1; i
< 8; i
++) {
666 w5100_write(priv
, W5500_Sn_RXMEM_SIZE(i
), 0);
667 w5100_write(priv
, W5500_Sn_TXMEM_SIZE(i
), 0);
671 static int w5100_hw_reset(struct w5100_priv
*priv
)
677 w5100_disable_intr(priv
);
678 w5100_write_macaddr(priv
);
680 switch (priv
->ops
->chip_id
) {
682 w5100_memory_configure(priv
);
686 w5200_memory_configure(priv
);
690 w5500_memory_configure(priv
);
697 if (w5100_read16(priv
, rtr
) != RTR_DEFAULT
)
703 static void w5100_hw_start(struct w5100_priv
*priv
)
705 u8 mode
= S0_MR_MACRAW
;
707 if (!priv
->promisc
) {
708 if (priv
->ops
->chip_id
== W5500
)
709 mode
|= W5500_S0_MR_MF
;
714 w5100_write(priv
, W5100_S0_MR(priv
), mode
);
715 w5100_command(priv
, S0_CR_OPEN
);
716 w5100_enable_intr(priv
);
719 static void w5100_hw_close(struct w5100_priv
*priv
)
721 w5100_disable_intr(priv
);
722 w5100_command(priv
, S0_CR_CLOSE
);
725 /***********************************************************************
727 * Device driver functions / callbacks
729 ***********************************************************************/
731 static void w5100_get_drvinfo(struct net_device
*ndev
,
732 struct ethtool_drvinfo
*info
)
734 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
735 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
736 strlcpy(info
->bus_info
, dev_name(ndev
->dev
.parent
),
737 sizeof(info
->bus_info
));
740 static u32
w5100_get_link(struct net_device
*ndev
)
742 struct w5100_priv
*priv
= netdev_priv(ndev
);
744 if (gpio_is_valid(priv
->link_gpio
))
745 return !!gpio_get_value(priv
->link_gpio
);
750 static u32
w5100_get_msglevel(struct net_device
*ndev
)
752 struct w5100_priv
*priv
= netdev_priv(ndev
);
754 return priv
->msg_enable
;
757 static void w5100_set_msglevel(struct net_device
*ndev
, u32 value
)
759 struct w5100_priv
*priv
= netdev_priv(ndev
);
761 priv
->msg_enable
= value
;
764 static int w5100_get_regs_len(struct net_device
*ndev
)
766 return W5100_COMMON_REGS_LEN
+ W5100_S0_REGS_LEN
;
769 static void w5100_get_regs(struct net_device
*ndev
,
770 struct ethtool_regs
*regs
, void *buf
)
772 struct w5100_priv
*priv
= netdev_priv(ndev
);
775 w5100_readbulk(priv
, W5100_COMMON_REGS
, buf
, W5100_COMMON_REGS_LEN
);
776 buf
+= W5100_COMMON_REGS_LEN
;
777 w5100_readbulk(priv
, S0_REGS(priv
), buf
, W5100_S0_REGS_LEN
);
780 static void w5100_restart(struct net_device
*ndev
)
782 struct w5100_priv
*priv
= netdev_priv(ndev
);
784 netif_stop_queue(ndev
);
785 w5100_hw_reset(priv
);
786 w5100_hw_start(priv
);
787 ndev
->stats
.tx_errors
++;
788 netif_trans_update(ndev
);
789 netif_wake_queue(ndev
);
792 static void w5100_restart_work(struct work_struct
*work
)
794 struct w5100_priv
*priv
= container_of(work
, struct w5100_priv
,
797 w5100_restart(priv
->ndev
);
800 static void w5100_tx_timeout(struct net_device
*ndev
)
802 struct w5100_priv
*priv
= netdev_priv(ndev
);
804 if (priv
->ops
->may_sleep
)
805 schedule_work(&priv
->restart_work
);
810 static void w5100_tx_skb(struct net_device
*ndev
, struct sk_buff
*skb
)
812 struct w5100_priv
*priv
= netdev_priv(ndev
);
815 offset
= w5100_read16(priv
, W5100_S0_TX_WR(priv
));
816 w5100_writebuf(priv
, offset
, skb
->data
, skb
->len
);
817 w5100_write16(priv
, W5100_S0_TX_WR(priv
), offset
+ skb
->len
);
818 ndev
->stats
.tx_bytes
+= skb
->len
;
819 ndev
->stats
.tx_packets
++;
822 w5100_command(priv
, S0_CR_SEND
);
825 static void w5100_tx_work(struct work_struct
*work
)
827 struct w5100_priv
*priv
= container_of(work
, struct w5100_priv
,
829 struct sk_buff
*skb
= priv
->tx_skb
;
835 w5100_tx_skb(priv
->ndev
, skb
);
838 static int w5100_start_tx(struct sk_buff
*skb
, struct net_device
*ndev
)
840 struct w5100_priv
*priv
= netdev_priv(ndev
);
842 netif_stop_queue(ndev
);
844 if (priv
->ops
->may_sleep
) {
845 WARN_ON(priv
->tx_skb
);
847 queue_work(priv
->xfer_wq
, &priv
->tx_work
);
849 w5100_tx_skb(ndev
, skb
);
855 static struct sk_buff
*w5100_rx_skb(struct net_device
*ndev
)
857 struct w5100_priv
*priv
= netdev_priv(ndev
);
862 u16 rx_buf_len
= w5100_read16(priv
, W5100_S0_RX_RSR(priv
));
867 offset
= w5100_read16(priv
, W5100_S0_RX_RD(priv
));
868 w5100_readbuf(priv
, offset
, header
, 2);
869 rx_len
= get_unaligned_be16(header
) - 2;
871 skb
= netdev_alloc_skb_ip_align(ndev
, rx_len
);
872 if (unlikely(!skb
)) {
873 w5100_write16(priv
, W5100_S0_RX_RD(priv
), offset
+ rx_buf_len
);
874 w5100_command(priv
, S0_CR_RECV
);
875 ndev
->stats
.rx_dropped
++;
879 skb_put(skb
, rx_len
);
880 w5100_readbuf(priv
, offset
+ 2, skb
->data
, rx_len
);
881 w5100_write16(priv
, W5100_S0_RX_RD(priv
), offset
+ 2 + rx_len
);
882 w5100_command(priv
, S0_CR_RECV
);
883 skb
->protocol
= eth_type_trans(skb
, ndev
);
885 ndev
->stats
.rx_packets
++;
886 ndev
->stats
.rx_bytes
+= rx_len
;
891 static void w5100_rx_work(struct work_struct
*work
)
893 struct w5100_priv
*priv
= container_of(work
, struct w5100_priv
,
897 while ((skb
= w5100_rx_skb(priv
->ndev
)))
900 w5100_enable_intr(priv
);
903 static int w5100_napi_poll(struct napi_struct
*napi
, int budget
)
905 struct w5100_priv
*priv
= container_of(napi
, struct w5100_priv
, napi
);
908 for (rx_count
= 0; rx_count
< budget
; rx_count
++) {
909 struct sk_buff
*skb
= w5100_rx_skb(priv
->ndev
);
912 netif_receive_skb(skb
);
917 if (rx_count
< budget
) {
918 napi_complete_done(napi
, rx_count
);
919 w5100_enable_intr(priv
);
925 static irqreturn_t
w5100_interrupt(int irq
, void *ndev_instance
)
927 struct net_device
*ndev
= ndev_instance
;
928 struct w5100_priv
*priv
= netdev_priv(ndev
);
930 int ir
= w5100_read(priv
, W5100_S0_IR(priv
));
933 w5100_write(priv
, W5100_S0_IR(priv
), ir
);
935 if (ir
& S0_IR_SENDOK
) {
936 netif_dbg(priv
, tx_done
, ndev
, "tx done\n");
937 netif_wake_queue(ndev
);
940 if (ir
& S0_IR_RECV
) {
941 w5100_disable_intr(priv
);
943 if (priv
->ops
->may_sleep
)
944 queue_work(priv
->xfer_wq
, &priv
->rx_work
);
945 else if (napi_schedule_prep(&priv
->napi
))
946 __napi_schedule(&priv
->napi
);
952 static irqreturn_t
w5100_detect_link(int irq
, void *ndev_instance
)
954 struct net_device
*ndev
= ndev_instance
;
955 struct w5100_priv
*priv
= netdev_priv(ndev
);
957 if (netif_running(ndev
)) {
958 if (gpio_get_value(priv
->link_gpio
) != 0) {
959 netif_info(priv
, link
, ndev
, "link is up\n");
960 netif_carrier_on(ndev
);
962 netif_info(priv
, link
, ndev
, "link is down\n");
963 netif_carrier_off(ndev
);
970 static void w5100_setrx_work(struct work_struct
*work
)
972 struct w5100_priv
*priv
= container_of(work
, struct w5100_priv
,
975 w5100_hw_start(priv
);
978 static void w5100_set_rx_mode(struct net_device
*ndev
)
980 struct w5100_priv
*priv
= netdev_priv(ndev
);
981 bool set_promisc
= (ndev
->flags
& IFF_PROMISC
) != 0;
983 if (priv
->promisc
!= set_promisc
) {
984 priv
->promisc
= set_promisc
;
986 if (priv
->ops
->may_sleep
)
987 schedule_work(&priv
->setrx_work
);
989 w5100_hw_start(priv
);
993 static int w5100_set_macaddr(struct net_device
*ndev
, void *addr
)
995 struct w5100_priv
*priv
= netdev_priv(ndev
);
996 struct sockaddr
*sock_addr
= addr
;
998 if (!is_valid_ether_addr(sock_addr
->sa_data
))
999 return -EADDRNOTAVAIL
;
1000 memcpy(ndev
->dev_addr
, sock_addr
->sa_data
, ETH_ALEN
);
1001 w5100_write_macaddr(priv
);
1005 static int w5100_open(struct net_device
*ndev
)
1007 struct w5100_priv
*priv
= netdev_priv(ndev
);
1009 netif_info(priv
, ifup
, ndev
, "enabling\n");
1010 w5100_hw_start(priv
);
1011 napi_enable(&priv
->napi
);
1012 netif_start_queue(ndev
);
1013 if (!gpio_is_valid(priv
->link_gpio
) ||
1014 gpio_get_value(priv
->link_gpio
) != 0)
1015 netif_carrier_on(ndev
);
1019 static int w5100_stop(struct net_device
*ndev
)
1021 struct w5100_priv
*priv
= netdev_priv(ndev
);
1023 netif_info(priv
, ifdown
, ndev
, "shutting down\n");
1024 w5100_hw_close(priv
);
1025 netif_carrier_off(ndev
);
1026 netif_stop_queue(ndev
);
1027 napi_disable(&priv
->napi
);
1031 static const struct ethtool_ops w5100_ethtool_ops
= {
1032 .get_drvinfo
= w5100_get_drvinfo
,
1033 .get_msglevel
= w5100_get_msglevel
,
1034 .set_msglevel
= w5100_set_msglevel
,
1035 .get_link
= w5100_get_link
,
1036 .get_regs_len
= w5100_get_regs_len
,
1037 .get_regs
= w5100_get_regs
,
1040 static const struct net_device_ops w5100_netdev_ops
= {
1041 .ndo_open
= w5100_open
,
1042 .ndo_stop
= w5100_stop
,
1043 .ndo_start_xmit
= w5100_start_tx
,
1044 .ndo_tx_timeout
= w5100_tx_timeout
,
1045 .ndo_set_rx_mode
= w5100_set_rx_mode
,
1046 .ndo_set_mac_address
= w5100_set_macaddr
,
1047 .ndo_validate_addr
= eth_validate_addr
,
1050 static int w5100_mmio_probe(struct platform_device
*pdev
)
1052 struct wiznet_platform_data
*data
= dev_get_platdata(&pdev
->dev
);
1053 const void *mac_addr
= NULL
;
1054 struct resource
*mem
;
1055 const struct w5100_ops
*ops
;
1058 if (data
&& is_valid_ether_addr(data
->mac_addr
))
1059 mac_addr
= data
->mac_addr
;
1061 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1062 if (resource_size(mem
) < W5100_BUS_DIRECT_SIZE
)
1063 ops
= &w5100_mmio_indirect_ops
;
1065 ops
= &w5100_mmio_direct_ops
;
1067 irq
= platform_get_irq(pdev
, 0);
1071 return w5100_probe(&pdev
->dev
, ops
, sizeof(struct w5100_mmio_priv
),
1072 mac_addr
, irq
, data
? data
->link_gpio
: -EINVAL
);
1075 static int w5100_mmio_remove(struct platform_device
*pdev
)
1077 return w5100_remove(&pdev
->dev
);
1080 void *w5100_ops_priv(const struct net_device
*ndev
)
1082 return netdev_priv(ndev
) +
1083 ALIGN(sizeof(struct w5100_priv
), NETDEV_ALIGN
);
1085 EXPORT_SYMBOL_GPL(w5100_ops_priv
);
1087 int w5100_probe(struct device
*dev
, const struct w5100_ops
*ops
,
1088 int sizeof_ops_priv
, const void *mac_addr
, int irq
,
1091 struct w5100_priv
*priv
;
1092 struct net_device
*ndev
;
1096 alloc_size
= sizeof(*priv
);
1097 if (sizeof_ops_priv
) {
1098 alloc_size
= ALIGN(alloc_size
, NETDEV_ALIGN
);
1099 alloc_size
+= sizeof_ops_priv
;
1101 alloc_size
+= NETDEV_ALIGN
- 1;
1103 ndev
= alloc_etherdev(alloc_size
);
1106 SET_NETDEV_DEV(ndev
, dev
);
1107 dev_set_drvdata(dev
, ndev
);
1108 priv
= netdev_priv(ndev
);
1110 switch (ops
->chip_id
) {
1112 priv
->s0_regs
= W5100_S0_REGS
;
1113 priv
->s0_tx_buf
= W5100_TX_MEM_START
;
1114 priv
->s0_tx_buf_size
= W5100_TX_MEM_SIZE
;
1115 priv
->s0_rx_buf
= W5100_RX_MEM_START
;
1116 priv
->s0_rx_buf_size
= W5100_RX_MEM_SIZE
;
1119 priv
->s0_regs
= W5200_S0_REGS
;
1120 priv
->s0_tx_buf
= W5200_TX_MEM_START
;
1121 priv
->s0_tx_buf_size
= W5200_TX_MEM_SIZE
;
1122 priv
->s0_rx_buf
= W5200_RX_MEM_START
;
1123 priv
->s0_rx_buf_size
= W5200_RX_MEM_SIZE
;
1126 priv
->s0_regs
= W5500_S0_REGS
;
1127 priv
->s0_tx_buf
= W5500_TX_MEM_START
;
1128 priv
->s0_tx_buf_size
= W5500_TX_MEM_SIZE
;
1129 priv
->s0_rx_buf
= W5500_RX_MEM_START
;
1130 priv
->s0_rx_buf_size
= W5500_RX_MEM_SIZE
;
1140 priv
->link_gpio
= link_gpio
;
1142 ndev
->netdev_ops
= &w5100_netdev_ops
;
1143 ndev
->ethtool_ops
= &w5100_ethtool_ops
;
1144 netif_napi_add(ndev
, &priv
->napi
, w5100_napi_poll
, 16);
1146 /* This chip doesn't support VLAN packets with normal MTU,
1147 * so disable VLAN for this device.
1149 ndev
->features
|= NETIF_F_VLAN_CHALLENGED
;
1151 err
= register_netdev(ndev
);
1155 priv
->xfer_wq
= alloc_workqueue("%s", WQ_MEM_RECLAIM
, 0,
1157 if (!priv
->xfer_wq
) {
1162 INIT_WORK(&priv
->rx_work
, w5100_rx_work
);
1163 INIT_WORK(&priv
->tx_work
, w5100_tx_work
);
1164 INIT_WORK(&priv
->setrx_work
, w5100_setrx_work
);
1165 INIT_WORK(&priv
->restart_work
, w5100_restart_work
);
1168 memcpy(ndev
->dev_addr
, mac_addr
, ETH_ALEN
);
1170 eth_hw_addr_random(ndev
);
1172 if (priv
->ops
->init
) {
1173 err
= priv
->ops
->init(priv
->ndev
);
1178 err
= w5100_hw_reset(priv
);
1182 if (ops
->may_sleep
) {
1183 err
= request_threaded_irq(priv
->irq
, NULL
, w5100_interrupt
,
1184 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
1185 netdev_name(ndev
), ndev
);
1187 err
= request_irq(priv
->irq
, w5100_interrupt
,
1188 IRQF_TRIGGER_LOW
, netdev_name(ndev
), ndev
);
1193 if (gpio_is_valid(priv
->link_gpio
)) {
1194 char *link_name
= devm_kzalloc(dev
, 16, GFP_KERNEL
);
1200 snprintf(link_name
, 16, "%s-link", netdev_name(ndev
));
1201 priv
->link_irq
= gpio_to_irq(priv
->link_gpio
);
1202 if (request_any_context_irq(priv
->link_irq
, w5100_detect_link
,
1203 IRQF_TRIGGER_RISING
|
1204 IRQF_TRIGGER_FALLING
,
1205 link_name
, priv
->ndev
) < 0)
1206 priv
->link_gpio
= -EINVAL
;
1212 free_irq(priv
->irq
, ndev
);
1214 destroy_workqueue(priv
->xfer_wq
);
1216 unregister_netdev(ndev
);
1221 EXPORT_SYMBOL_GPL(w5100_probe
);
1223 int w5100_remove(struct device
*dev
)
1225 struct net_device
*ndev
= dev_get_drvdata(dev
);
1226 struct w5100_priv
*priv
= netdev_priv(ndev
);
1228 w5100_hw_reset(priv
);
1229 free_irq(priv
->irq
, ndev
);
1230 if (gpio_is_valid(priv
->link_gpio
))
1231 free_irq(priv
->link_irq
, ndev
);
1233 flush_work(&priv
->setrx_work
);
1234 flush_work(&priv
->restart_work
);
1235 destroy_workqueue(priv
->xfer_wq
);
1237 unregister_netdev(ndev
);
1241 EXPORT_SYMBOL_GPL(w5100_remove
);
1243 #ifdef CONFIG_PM_SLEEP
1244 static int w5100_suspend(struct device
*dev
)
1246 struct net_device
*ndev
= dev_get_drvdata(dev
);
1247 struct w5100_priv
*priv
= netdev_priv(ndev
);
1249 if (netif_running(ndev
)) {
1250 netif_carrier_off(ndev
);
1251 netif_device_detach(ndev
);
1253 w5100_hw_close(priv
);
1258 static int w5100_resume(struct device
*dev
)
1260 struct net_device
*ndev
= dev_get_drvdata(dev
);
1261 struct w5100_priv
*priv
= netdev_priv(ndev
);
1263 if (netif_running(ndev
)) {
1264 w5100_hw_reset(priv
);
1265 w5100_hw_start(priv
);
1267 netif_device_attach(ndev
);
1268 if (!gpio_is_valid(priv
->link_gpio
) ||
1269 gpio_get_value(priv
->link_gpio
) != 0)
1270 netif_carrier_on(ndev
);
1274 #endif /* CONFIG_PM_SLEEP */
1276 SIMPLE_DEV_PM_OPS(w5100_pm_ops
, w5100_suspend
, w5100_resume
);
1277 EXPORT_SYMBOL_GPL(w5100_pm_ops
);
1279 static struct platform_driver w5100_mmio_driver
= {
1282 .pm
= &w5100_pm_ops
,
1284 .probe
= w5100_mmio_probe
,
1285 .remove
= w5100_mmio_remove
,
1287 module_platform_driver(w5100_mmio_driver
);