blob: ec1889ce38a384f1311286c5835f61bc1f26bf40 [file] [log] [blame]
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001/*
2 * Ethernet driver for the WIZnet W5100 chip.
3 *
4 * Copyright (C) 2006-2008 WIZnet Co.,Ltd.
5 * Copyright (C) 2012 Mike Sinkovsky <msink@permonline.ru>
6 *
7 * Licensed under the GPL-2 or later.
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/kconfig.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/platform_device.h>
16#include <linux/platform_data/wiznet.h>
17#include <linux/ethtool.h>
18#include <linux/skbuff.h>
19#include <linux/types.h>
20#include <linux/errno.h>
21#include <linux/delay.h>
22#include <linux/slab.h>
23#include <linux/spinlock.h>
24#include <linux/io.h>
25#include <linux/ioport.h>
26#include <linux/interrupt.h>
Geert Uytterhoeven64d176f2012-04-12 09:19:23 +000027#include <linux/irq.h>
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000028#include <linux/gpio.h>
29
Akinobu Mita850576c2016-04-15 00:11:30 +090030#include "w5100.h"
31
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000032#define DRV_NAME "w5100"
33#define DRV_VERSION "2012-04-04"
34
35MODULE_DESCRIPTION("WIZnet W5100 Ethernet driver v"DRV_VERSION);
36MODULE_AUTHOR("Mike Sinkovsky <msink@permonline.ru>");
37MODULE_ALIAS("platform:"DRV_NAME);
38MODULE_LICENSE("GPL");
39
40/*
Akinobu Mita35ef7d62016-04-27 05:43:48 +090041 * W5100/W5200/W5500 common registers
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000042 */
43#define W5100_COMMON_REGS 0x0000
44#define W5100_MR 0x0000 /* Mode Register */
45#define MR_RST 0x80 /* S/W reset */
46#define MR_PB 0x10 /* Ping block */
47#define MR_AI 0x02 /* Address Auto-Increment */
48#define MR_IND 0x01 /* Indirect mode */
49#define W5100_SHAR 0x0009 /* Source MAC address */
50#define W5100_IR 0x0015 /* Interrupt Register */
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000051#define W5100_COMMON_REGS_LEN 0x0040
52
Akinobu Mita0c165ff2016-04-15 00:11:33 +090053#define W5100_Sn_MR 0x0000 /* Sn Mode Register */
54#define W5100_Sn_CR 0x0001 /* Sn Command Register */
55#define W5100_Sn_IR 0x0002 /* Sn Interrupt Register */
56#define W5100_Sn_SR 0x0003 /* Sn Status Register */
57#define W5100_Sn_TX_FSR 0x0020 /* Sn Transmit free memory size */
58#define W5100_Sn_TX_RD 0x0022 /* Sn Transmit memory read pointer */
59#define W5100_Sn_TX_WR 0x0024 /* Sn Transmit memory write pointer */
60#define W5100_Sn_RX_RSR 0x0026 /* Sn Receive free memory size */
61#define W5100_Sn_RX_RD 0x0028 /* Sn Receive memory read pointer */
62
Akinobu Mita35ef7d62016-04-27 05:43:48 +090063#define S0_REGS(priv) ((priv)->s0_regs)
Akinobu Mita0c165ff2016-04-15 00:11:33 +090064
65#define W5100_S0_MR(priv) (S0_REGS(priv) + W5100_Sn_MR)
Joe Perchesdbedd442015-03-06 20:49:12 -080066#define S0_MR_MACRAW 0x04 /* MAC RAW mode (promiscuous) */
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000067#define S0_MR_MACRAW_MF 0x44 /* MAC RAW mode (filtered) */
Akinobu Mita0c165ff2016-04-15 00:11:33 +090068#define W5100_S0_CR(priv) (S0_REGS(priv) + W5100_Sn_CR)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000069#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 */
Akinobu Mita0c165ff2016-04-15 00:11:33 +090073#define W5100_S0_IR(priv) (S0_REGS(priv) + W5100_Sn_IR)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000074#define S0_IR_SENDOK 0x10 /* complete sending */
75#define S0_IR_RECV 0x04 /* receiving data */
Akinobu Mita0c165ff2016-04-15 00:11:33 +090076#define W5100_S0_SR(priv) (S0_REGS(priv) + W5100_Sn_SR)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000077#define S0_SR_MACRAW 0x42 /* mac raw mode */
Akinobu Mita0c165ff2016-04-15 00:11:33 +090078#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)
83
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +000084#define W5100_S0_REGS_LEN 0x0040
85
Akinobu Mita0c165ff2016-04-15 00:11:33 +090086/*
Akinobu Mita35ef7d62016-04-27 05:43:48 +090087 * W5100 and W5200 common registers
88 */
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) */
93
94/*
95 * W5100 specific register and memory
Akinobu Mita0c165ff2016-04-15 00:11:33 +090096 */
97#define W5100_RMSR 0x001a /* Receive Memory Size */
98#define W5100_TMSR 0x001b /* Transmit Memory Size */
99
100#define W5100_S0_REGS 0x0400
101
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000102#define W5100_TX_MEM_START 0x4000
Akinobu Mita850576c2016-04-15 00:11:30 +0900103#define W5100_TX_MEM_SIZE 0x2000
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000104#define W5100_RX_MEM_START 0x6000
Akinobu Mita850576c2016-04-15 00:11:30 +0900105#define W5100_RX_MEM_SIZE 0x2000
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000106
107/*
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900108 * W5200 specific register and memory
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900109 */
110#define W5200_S0_REGS 0x4000
111
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 */
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900114
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
119
120/*
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900121 * W5500 specific register and memory
122 *
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)
127 */
128#define W5500_SIMR 0x0018 /* Socket Interrupt Mask Register */
129#define W5500_RTR 0x0019 /* Retry Time-value Register */
130
131#define W5500_S0_REGS 0x10000
132
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 */
137
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
142
143/*
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000144 * Device driver private data structure
145 */
Akinobu Mita850576c2016-04-15 00:11:30 +0900146
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000147struct w5100_priv {
Akinobu Mita850576c2016-04-15 00:11:30 +0900148 const struct w5100_ops *ops;
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900149
150 /* Socket 0 register offset address */
151 u32 s0_regs;
152 /* Socket 0 TX buffer offset address and size */
153 u32 s0_tx_buf;
154 u16 s0_tx_buf_size;
155 /* Socket 0 RX buffer offset address and size */
156 u32 s0_rx_buf;
157 u16 s0_rx_buf_size;
158
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000159 int irq;
160 int link_irq;
161 int link_gpio;
162
163 struct napi_struct napi;
164 struct net_device *ndev;
165 bool promisc;
166 u32 msg_enable;
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900167
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;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000174};
175
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900176static inline bool is_w5200(struct w5100_priv *priv)
177{
178 return priv->ops->chip_id == W5200;
179}
180
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000181/************************************************************************
182 *
183 * Lowlevel I/O functions
184 *
185 ***********************************************************************/
186
Akinobu Mita850576c2016-04-15 00:11:30 +0900187struct w5100_mmio_priv {
188 void __iomem *base;
189 /* Serialize access in indirect address mode */
190 spinlock_t reg_lock;
191};
192
193static inline struct w5100_mmio_priv *w5100_mmio_priv(struct net_device *dev)
194{
195 return w5100_ops_priv(dev);
196}
197
198static inline void __iomem *w5100_mmio(struct net_device *ndev)
199{
200 struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
201
202 return mmio_priv->base;
203}
204
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000205/*
206 * In direct address mode host system can directly access W5100 registers
207 * after mapping to Memory-Mapped I/O space.
208 *
209 * 0x8000 bytes are required for memory space.
210 */
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900211static inline int w5100_read_direct(struct net_device *ndev, u32 addr)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000212{
Akinobu Mita850576c2016-04-15 00:11:30 +0900213 return ioread8(w5100_mmio(ndev) + (addr << CONFIG_WIZNET_BUS_SHIFT));
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000214}
215
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900216static inline int __w5100_write_direct(struct net_device *ndev, u32 addr,
Akinobu Mita850576c2016-04-15 00:11:30 +0900217 u8 data)
Akinobu Mitad6586d22016-04-15 00:11:29 +0900218{
Akinobu Mita850576c2016-04-15 00:11:30 +0900219 iowrite8(data, w5100_mmio(ndev) + (addr << CONFIG_WIZNET_BUS_SHIFT));
220
221 return 0;
Akinobu Mitad6586d22016-04-15 00:11:29 +0900222}
223
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900224static inline int w5100_write_direct(struct net_device *ndev, u32 addr, u8 data)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000225{
Akinobu Mita850576c2016-04-15 00:11:30 +0900226 __w5100_write_direct(ndev, addr, data);
Akinobu Mitad6586d22016-04-15 00:11:29 +0900227 mmiowb();
Akinobu Mita850576c2016-04-15 00:11:30 +0900228
229 return 0;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000230}
231
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900232static int w5100_read16_direct(struct net_device *ndev, u32 addr)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000233{
234 u16 data;
Akinobu Mita850576c2016-04-15 00:11:30 +0900235 data = w5100_read_direct(ndev, addr) << 8;
236 data |= w5100_read_direct(ndev, addr + 1);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000237 return data;
238}
239
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900240static int w5100_write16_direct(struct net_device *ndev, u32 addr, u16 data)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000241{
Akinobu Mita850576c2016-04-15 00:11:30 +0900242 __w5100_write_direct(ndev, addr, data >> 8);
243 __w5100_write_direct(ndev, addr + 1, data);
Akinobu Mitad6586d22016-04-15 00:11:29 +0900244 mmiowb();
Akinobu Mita850576c2016-04-15 00:11:30 +0900245
246 return 0;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000247}
248
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900249static int w5100_readbulk_direct(struct net_device *ndev, u32 addr, u8 *buf,
Akinobu Mita850576c2016-04-15 00:11:30 +0900250 int len)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000251{
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000252 int i;
253
Akinobu Mita850576c2016-04-15 00:11:30 +0900254 for (i = 0; i < len; i++, addr++)
255 *buf++ = w5100_read_direct(ndev, addr);
256
257 return 0;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000258}
259
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900260static int w5100_writebulk_direct(struct net_device *ndev, u32 addr,
Akinobu Mita850576c2016-04-15 00:11:30 +0900261 const u8 *buf, int len)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000262{
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000263 int i;
264
Akinobu Mita850576c2016-04-15 00:11:30 +0900265 for (i = 0; i < len; i++, addr++)
266 __w5100_write_direct(ndev, addr, *buf++);
267
Akinobu Mitad6586d22016-04-15 00:11:29 +0900268 mmiowb();
Akinobu Mita850576c2016-04-15 00:11:30 +0900269
270 return 0;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000271}
272
Akinobu Mita850576c2016-04-15 00:11:30 +0900273static int w5100_mmio_init(struct net_device *ndev)
274{
275 struct platform_device *pdev = to_platform_device(ndev->dev.parent);
276 struct w5100_priv *priv = netdev_priv(ndev);
277 struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
278 struct resource *mem;
279
280 spin_lock_init(&mmio_priv->reg_lock);
281
282 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
283 mmio_priv->base = devm_ioremap_resource(&pdev->dev, mem);
284 if (IS_ERR(mmio_priv->base))
285 return PTR_ERR(mmio_priv->base);
286
287 netdev_info(ndev, "at 0x%llx irq %d\n", (u64)mem->start, priv->irq);
288
289 return 0;
290}
291
292static const struct w5100_ops w5100_mmio_direct_ops = {
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900293 .chip_id = W5100,
Akinobu Mita850576c2016-04-15 00:11:30 +0900294 .read = w5100_read_direct,
295 .write = w5100_write_direct,
296 .read16 = w5100_read16_direct,
297 .write16 = w5100_write16_direct,
298 .readbulk = w5100_readbulk_direct,
299 .writebulk = w5100_writebulk_direct,
300 .init = w5100_mmio_init,
301};
302
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000303/*
304 * In indirect address mode host system indirectly accesses registers by
305 * using Indirect Mode Address Register (IDM_AR) and Indirect Mode Data
306 * Register (IDM_DR), which are directly mapped to Memory-Mapped I/O space.
307 * Mode Register (MR) is directly accessible.
308 *
309 * Only 0x04 bytes are required for memory space.
310 */
311#define W5100_IDM_AR 0x01 /* Indirect Mode Address Register */
312#define W5100_IDM_DR 0x03 /* Indirect Mode Data Register */
313
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900314static int w5100_read_indirect(struct net_device *ndev, u32 addr)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000315{
Akinobu Mita850576c2016-04-15 00:11:30 +0900316 struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000317 unsigned long flags;
318 u8 data;
319
Akinobu Mita850576c2016-04-15 00:11:30 +0900320 spin_lock_irqsave(&mmio_priv->reg_lock, flags);
321 w5100_write16_direct(ndev, W5100_IDM_AR, addr);
322 data = w5100_read_direct(ndev, W5100_IDM_DR);
323 spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000324
325 return data;
326}
327
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900328static int w5100_write_indirect(struct net_device *ndev, u32 addr, u8 data)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000329{
Akinobu Mita850576c2016-04-15 00:11:30 +0900330 struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000331 unsigned long flags;
332
Akinobu Mita850576c2016-04-15 00:11:30 +0900333 spin_lock_irqsave(&mmio_priv->reg_lock, flags);
334 w5100_write16_direct(ndev, W5100_IDM_AR, addr);
335 w5100_write_direct(ndev, W5100_IDM_DR, data);
336 spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
337
338 return 0;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000339}
340
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900341static int w5100_read16_indirect(struct net_device *ndev, u32 addr)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000342{
Akinobu Mita850576c2016-04-15 00:11:30 +0900343 struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000344 unsigned long flags;
345 u16 data;
346
Akinobu Mita850576c2016-04-15 00:11:30 +0900347 spin_lock_irqsave(&mmio_priv->reg_lock, flags);
348 w5100_write16_direct(ndev, W5100_IDM_AR, addr);
349 data = w5100_read_direct(ndev, W5100_IDM_DR) << 8;
350 data |= w5100_read_direct(ndev, W5100_IDM_DR);
351 spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000352
353 return data;
354}
355
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900356static int w5100_write16_indirect(struct net_device *ndev, u32 addr, u16 data)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000357{
Akinobu Mita850576c2016-04-15 00:11:30 +0900358 struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000359 unsigned long flags;
360
Akinobu Mita850576c2016-04-15 00:11:30 +0900361 spin_lock_irqsave(&mmio_priv->reg_lock, flags);
362 w5100_write16_direct(ndev, W5100_IDM_AR, addr);
363 __w5100_write_direct(ndev, W5100_IDM_DR, data >> 8);
364 w5100_write_direct(ndev, W5100_IDM_DR, data);
365 spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
366
367 return 0;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000368}
369
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900370static int w5100_readbulk_indirect(struct net_device *ndev, u32 addr, u8 *buf,
Akinobu Mita850576c2016-04-15 00:11:30 +0900371 int len)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000372{
Akinobu Mita850576c2016-04-15 00:11:30 +0900373 struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000374 unsigned long flags;
375 int i;
376
Akinobu Mita850576c2016-04-15 00:11:30 +0900377 spin_lock_irqsave(&mmio_priv->reg_lock, flags);
378 w5100_write16_direct(ndev, W5100_IDM_AR, addr);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000379
Akinobu Mita850576c2016-04-15 00:11:30 +0900380 for (i = 0; i < len; i++)
381 *buf++ = w5100_read_direct(ndev, W5100_IDM_DR);
382
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000383 mmiowb();
Akinobu Mita850576c2016-04-15 00:11:30 +0900384 spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
385
386 return 0;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000387}
388
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900389static int w5100_writebulk_indirect(struct net_device *ndev, u32 addr,
Akinobu Mita850576c2016-04-15 00:11:30 +0900390 const u8 *buf, int len)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000391{
Akinobu Mita850576c2016-04-15 00:11:30 +0900392 struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000393 unsigned long flags;
394 int i;
395
Akinobu Mita850576c2016-04-15 00:11:30 +0900396 spin_lock_irqsave(&mmio_priv->reg_lock, flags);
397 w5100_write16_direct(ndev, W5100_IDM_AR, addr);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000398
Akinobu Mita850576c2016-04-15 00:11:30 +0900399 for (i = 0; i < len; i++)
400 __w5100_write_direct(ndev, W5100_IDM_DR, *buf++);
401
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000402 mmiowb();
Akinobu Mita850576c2016-04-15 00:11:30 +0900403 spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
404
405 return 0;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000406}
407
Akinobu Mita850576c2016-04-15 00:11:30 +0900408static int w5100_reset_indirect(struct net_device *ndev)
409{
410 w5100_write_direct(ndev, W5100_MR, MR_RST);
411 mdelay(5);
412 w5100_write_direct(ndev, W5100_MR, MR_PB | MR_AI | MR_IND);
413
414 return 0;
415}
416
417static const struct w5100_ops w5100_mmio_indirect_ops = {
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900418 .chip_id = W5100,
Akinobu Mita850576c2016-04-15 00:11:30 +0900419 .read = w5100_read_indirect,
420 .write = w5100_write_indirect,
421 .read16 = w5100_read16_indirect,
422 .write16 = w5100_write16_indirect,
423 .readbulk = w5100_readbulk_indirect,
424 .writebulk = w5100_writebulk_indirect,
425 .init = w5100_mmio_init,
426 .reset = w5100_reset_indirect,
427};
428
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000429#if defined(CONFIG_WIZNET_BUS_DIRECT)
Akinobu Mita850576c2016-04-15 00:11:30 +0900430
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900431static int w5100_read(struct w5100_priv *priv, u32 addr)
Akinobu Mita850576c2016-04-15 00:11:30 +0900432{
433 return w5100_read_direct(priv->ndev, addr);
434}
435
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900436static int w5100_write(struct w5100_priv *priv, u32 addr, u8 data)
Akinobu Mita850576c2016-04-15 00:11:30 +0900437{
438 return w5100_write_direct(priv->ndev, addr, data);
439}
440
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900441static int w5100_read16(struct w5100_priv *priv, u32 addr)
Akinobu Mita850576c2016-04-15 00:11:30 +0900442{
443 return w5100_read16_direct(priv->ndev, addr);
444}
445
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900446static int w5100_write16(struct w5100_priv *priv, u32 addr, u16 data)
Akinobu Mita850576c2016-04-15 00:11:30 +0900447{
448 return w5100_write16_direct(priv->ndev, addr, data);
449}
450
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900451static int w5100_readbulk(struct w5100_priv *priv, u32 addr, u8 *buf, int len)
Akinobu Mita850576c2016-04-15 00:11:30 +0900452{
453 return w5100_readbulk_direct(priv->ndev, addr, buf, len);
454}
455
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900456static int w5100_writebulk(struct w5100_priv *priv, u32 addr, const u8 *buf,
Akinobu Mita850576c2016-04-15 00:11:30 +0900457 int len)
458{
459 return w5100_writebulk_direct(priv->ndev, addr, buf, len);
460}
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000461
462#elif defined(CONFIG_WIZNET_BUS_INDIRECT)
Akinobu Mita850576c2016-04-15 00:11:30 +0900463
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900464static int w5100_read(struct w5100_priv *priv, u32 addr)
Akinobu Mita850576c2016-04-15 00:11:30 +0900465{
466 return w5100_read_indirect(priv->ndev, addr);
467}
468
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900469static int w5100_write(struct w5100_priv *priv, u32 addr, u8 data)
Akinobu Mita850576c2016-04-15 00:11:30 +0900470{
471 return w5100_write_indirect(priv->ndev, addr, data);
472}
473
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900474static int w5100_read16(struct w5100_priv *priv, u32 addr)
Akinobu Mita850576c2016-04-15 00:11:30 +0900475{
476 return w5100_read16_indirect(priv->ndev, addr);
477}
478
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900479static int w5100_write16(struct w5100_priv *priv, u32 addr, u16 data)
Akinobu Mita850576c2016-04-15 00:11:30 +0900480{
481 return w5100_write16_indirect(priv->ndev, addr, data);
482}
483
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900484static int w5100_readbulk(struct w5100_priv *priv, u32 addr, u8 *buf, int len)
Akinobu Mita850576c2016-04-15 00:11:30 +0900485{
486 return w5100_readbulk_indirect(priv->ndev, addr, buf, len);
487}
488
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900489static int w5100_writebulk(struct w5100_priv *priv, u32 addr, const u8 *buf,
Akinobu Mita850576c2016-04-15 00:11:30 +0900490 int len)
491{
492 return w5100_writebulk_indirect(priv->ndev, addr, buf, len);
493}
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000494
495#else /* CONFIG_WIZNET_BUS_ANY */
Akinobu Mita850576c2016-04-15 00:11:30 +0900496
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900497static int w5100_read(struct w5100_priv *priv, u32 addr)
Akinobu Mita850576c2016-04-15 00:11:30 +0900498{
499 return priv->ops->read(priv->ndev, addr);
500}
501
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900502static int w5100_write(struct w5100_priv *priv, u32 addr, u8 data)
Akinobu Mita850576c2016-04-15 00:11:30 +0900503{
504 return priv->ops->write(priv->ndev, addr, data);
505}
506
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900507static int w5100_read16(struct w5100_priv *priv, u32 addr)
Akinobu Mita850576c2016-04-15 00:11:30 +0900508{
509 return priv->ops->read16(priv->ndev, addr);
510}
511
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900512static int w5100_write16(struct w5100_priv *priv, u32 addr, u16 data)
Akinobu Mita850576c2016-04-15 00:11:30 +0900513{
514 return priv->ops->write16(priv->ndev, addr, data);
515}
516
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900517static int w5100_readbulk(struct w5100_priv *priv, u32 addr, u8 *buf, int len)
Akinobu Mita850576c2016-04-15 00:11:30 +0900518{
519 return priv->ops->readbulk(priv->ndev, addr, buf, len);
520}
521
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900522static int w5100_writebulk(struct w5100_priv *priv, u32 addr, const u8 *buf,
Akinobu Mita850576c2016-04-15 00:11:30 +0900523 int len)
524{
525 return priv->ops->writebulk(priv->ndev, addr, buf, len);
526}
527
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000528#endif
529
Akinobu Mita850576c2016-04-15 00:11:30 +0900530static int w5100_readbuf(struct w5100_priv *priv, u16 offset, u8 *buf, int len)
531{
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900532 u32 addr;
Akinobu Mita850576c2016-04-15 00:11:30 +0900533 int remain = 0;
534 int ret;
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900535 const u32 mem_start = priv->s0_rx_buf;
536 const u16 mem_size = priv->s0_rx_buf_size;
Akinobu Mita850576c2016-04-15 00:11:30 +0900537
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900538 offset %= mem_size;
539 addr = mem_start + offset;
Akinobu Mita850576c2016-04-15 00:11:30 +0900540
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900541 if (offset + len > mem_size) {
542 remain = (offset + len) % mem_size;
543 len = mem_size - offset;
Akinobu Mita850576c2016-04-15 00:11:30 +0900544 }
545
546 ret = w5100_readbulk(priv, addr, buf, len);
547 if (ret || !remain)
548 return ret;
549
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900550 return w5100_readbulk(priv, mem_start, buf + len, remain);
Akinobu Mita850576c2016-04-15 00:11:30 +0900551}
552
553static int w5100_writebuf(struct w5100_priv *priv, u16 offset, const u8 *buf,
554 int len)
555{
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900556 u32 addr;
Akinobu Mita850576c2016-04-15 00:11:30 +0900557 int ret;
558 int remain = 0;
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900559 const u32 mem_start = priv->s0_tx_buf;
560 const u16 mem_size = priv->s0_tx_buf_size;
Akinobu Mita850576c2016-04-15 00:11:30 +0900561
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900562 offset %= mem_size;
563 addr = mem_start + offset;
Akinobu Mita850576c2016-04-15 00:11:30 +0900564
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900565 if (offset + len > mem_size) {
566 remain = (offset + len) % mem_size;
567 len = mem_size - offset;
Akinobu Mita850576c2016-04-15 00:11:30 +0900568 }
569
570 ret = w5100_writebulk(priv, addr, buf, len);
571 if (ret || !remain)
572 return ret;
573
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900574 return w5100_writebulk(priv, mem_start, buf + len, remain);
Akinobu Mita850576c2016-04-15 00:11:30 +0900575}
576
577static int w5100_reset(struct w5100_priv *priv)
578{
579 if (priv->ops->reset)
580 return priv->ops->reset(priv->ndev);
581
582 w5100_write(priv, W5100_MR, MR_RST);
583 mdelay(5);
584 w5100_write(priv, W5100_MR, MR_PB);
585
586 return 0;
587}
588
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000589static int w5100_command(struct w5100_priv *priv, u16 cmd)
590{
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900591 unsigned long timeout;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000592
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900593 w5100_write(priv, W5100_S0_CR(priv), cmd);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000594
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900595 timeout = jiffies + msecs_to_jiffies(100);
596
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900597 while (w5100_read(priv, W5100_S0_CR(priv)) != 0) {
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000598 if (time_after(jiffies, timeout))
599 return -EIO;
600 cpu_relax();
601 }
602
603 return 0;
604}
605
606static void w5100_write_macaddr(struct w5100_priv *priv)
607{
608 struct net_device *ndev = priv->ndev;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000609
Akinobu Mita850576c2016-04-15 00:11:30 +0900610 w5100_writebulk(priv, W5100_SHAR, ndev->dev_addr, ETH_ALEN);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000611}
612
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900613static void w5100_socket_intr_mask(struct w5100_priv *priv, u8 mask)
614{
615 u32 imr;
616
617 if (priv->ops->chip_id == W5500)
618 imr = W5500_SIMR;
619 else
620 imr = W5100_IMR;
621
622 w5100_write(priv, imr, mask);
623}
624
625static void w5100_enable_intr(struct w5100_priv *priv)
626{
627 w5100_socket_intr_mask(priv, IR_S0);
628}
629
630static void w5100_disable_intr(struct w5100_priv *priv)
631{
632 w5100_socket_intr_mask(priv, 0);
633}
634
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900635static void w5100_memory_configure(struct w5100_priv *priv)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000636{
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000637 /* Configure 16K of internal memory
638 * as 8K RX buffer and 8K TX buffer
639 */
640 w5100_write(priv, W5100_RMSR, 0x03);
641 w5100_write(priv, W5100_TMSR, 0x03);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000642}
643
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900644static void w5200_memory_configure(struct w5100_priv *priv)
645{
646 int i;
647
648 /* Configure internal RX memory as 16K RX buffer and
649 * internal TX memory as 16K TX buffer
650 */
651 w5100_write(priv, W5200_Sn_RXMEM_SIZE(0), 0x10);
652 w5100_write(priv, W5200_Sn_TXMEM_SIZE(0), 0x10);
653
654 for (i = 1; i < 8; i++) {
655 w5100_write(priv, W5200_Sn_RXMEM_SIZE(i), 0);
656 w5100_write(priv, W5200_Sn_TXMEM_SIZE(i), 0);
657 }
658}
659
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900660static void w5500_memory_configure(struct w5100_priv *priv)
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900661{
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900662 int i;
663
664 /* Configure internal RX memory as 16K RX buffer and
665 * internal TX memory as 16K TX buffer
666 */
667 w5100_write(priv, W5500_Sn_RXMEM_SIZE(0), 0x10);
668 w5100_write(priv, W5500_Sn_TXMEM_SIZE(0), 0x10);
669
670 for (i = 1; i < 8; i++) {
671 w5100_write(priv, W5500_Sn_RXMEM_SIZE(i), 0);
672 w5100_write(priv, W5500_Sn_TXMEM_SIZE(i), 0);
673 }
674}
675
676static int w5100_hw_reset(struct w5100_priv *priv)
677{
678 u32 rtr;
679
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900680 w5100_reset(priv);
681
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900682 w5100_disable_intr(priv);
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900683 w5100_write_macaddr(priv);
684
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900685 switch (priv->ops->chip_id) {
686 case W5100:
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900687 w5100_memory_configure(priv);
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900688 rtr = W5100_RTR;
689 break;
690 case W5200:
691 w5200_memory_configure(priv);
692 rtr = W5100_RTR;
693 break;
694 case W5500:
695 w5500_memory_configure(priv);
696 rtr = W5500_RTR;
697 break;
698 default:
699 return -EINVAL;
700 }
701
702 if (w5100_read16(priv, rtr) != RTR_DEFAULT)
703 return -ENODEV;
704
705 return 0;
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900706}
707
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000708static void w5100_hw_start(struct w5100_priv *priv)
709{
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900710 w5100_write(priv, W5100_S0_MR(priv), priv->promisc ?
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000711 S0_MR_MACRAW : S0_MR_MACRAW_MF);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000712 w5100_command(priv, S0_CR_OPEN);
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900713 w5100_enable_intr(priv);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000714}
715
716static void w5100_hw_close(struct w5100_priv *priv)
717{
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900718 w5100_disable_intr(priv);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000719 w5100_command(priv, S0_CR_CLOSE);
720}
721
722/***********************************************************************
723 *
724 * Device driver functions / callbacks
725 *
726 ***********************************************************************/
727
728static void w5100_get_drvinfo(struct net_device *ndev,
729 struct ethtool_drvinfo *info)
730{
731 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
732 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
733 strlcpy(info->bus_info, dev_name(ndev->dev.parent),
734 sizeof(info->bus_info));
735}
736
737static u32 w5100_get_link(struct net_device *ndev)
738{
739 struct w5100_priv *priv = netdev_priv(ndev);
740
741 if (gpio_is_valid(priv->link_gpio))
742 return !!gpio_get_value(priv->link_gpio);
743
744 return 1;
745}
746
747static u32 w5100_get_msglevel(struct net_device *ndev)
748{
749 struct w5100_priv *priv = netdev_priv(ndev);
750
751 return priv->msg_enable;
752}
753
754static void w5100_set_msglevel(struct net_device *ndev, u32 value)
755{
756 struct w5100_priv *priv = netdev_priv(ndev);
757
758 priv->msg_enable = value;
759}
760
761static int w5100_get_regs_len(struct net_device *ndev)
762{
763 return W5100_COMMON_REGS_LEN + W5100_S0_REGS_LEN;
764}
765
766static void w5100_get_regs(struct net_device *ndev,
Akinobu Mita850576c2016-04-15 00:11:30 +0900767 struct ethtool_regs *regs, void *buf)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000768{
769 struct w5100_priv *priv = netdev_priv(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000770
771 regs->version = 1;
Akinobu Mita850576c2016-04-15 00:11:30 +0900772 w5100_readbulk(priv, W5100_COMMON_REGS, buf, W5100_COMMON_REGS_LEN);
773 buf += W5100_COMMON_REGS_LEN;
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900774 w5100_readbulk(priv, S0_REGS(priv), buf, W5100_S0_REGS_LEN);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000775}
776
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900777static void w5100_restart(struct net_device *ndev)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000778{
779 struct w5100_priv *priv = netdev_priv(ndev);
780
781 netif_stop_queue(ndev);
782 w5100_hw_reset(priv);
783 w5100_hw_start(priv);
784 ndev->stats.tx_errors++;
Florian Westphal860e9532016-05-03 16:33:13 +0200785 netif_trans_update(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000786 netif_wake_queue(ndev);
787}
788
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900789static void w5100_restart_work(struct work_struct *work)
790{
791 struct w5100_priv *priv = container_of(work, struct w5100_priv,
792 restart_work);
793
794 w5100_restart(priv->ndev);
795}
796
797static void w5100_tx_timeout(struct net_device *ndev)
798{
799 struct w5100_priv *priv = netdev_priv(ndev);
800
801 if (priv->ops->may_sleep)
802 schedule_work(&priv->restart_work);
803 else
804 w5100_restart(ndev);
805}
806
807static void w5100_tx_skb(struct net_device *ndev, struct sk_buff *skb)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000808{
809 struct w5100_priv *priv = netdev_priv(ndev);
810 u16 offset;
811
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900812 offset = w5100_read16(priv, W5100_S0_TX_WR(priv));
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000813 w5100_writebuf(priv, offset, skb->data, skb->len);
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900814 w5100_write16(priv, W5100_S0_TX_WR(priv), offset + skb->len);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000815 ndev->stats.tx_bytes += skb->len;
816 ndev->stats.tx_packets++;
817 dev_kfree_skb(skb);
818
819 w5100_command(priv, S0_CR_SEND);
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900820}
821
822static void w5100_tx_work(struct work_struct *work)
823{
824 struct w5100_priv *priv = container_of(work, struct w5100_priv,
825 tx_work);
826 struct sk_buff *skb = priv->tx_skb;
827
828 priv->tx_skb = NULL;
829
830 if (WARN_ON(!skb))
831 return;
832 w5100_tx_skb(priv->ndev, skb);
833}
834
835static int w5100_start_tx(struct sk_buff *skb, struct net_device *ndev)
836{
837 struct w5100_priv *priv = netdev_priv(ndev);
838
839 netif_stop_queue(ndev);
840
841 if (priv->ops->may_sleep) {
842 WARN_ON(priv->tx_skb);
843 priv->tx_skb = skb;
844 queue_work(priv->xfer_wq, &priv->tx_work);
845 } else {
846 w5100_tx_skb(ndev, skb);
847 }
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000848
849 return NETDEV_TX_OK;
850}
851
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900852static struct sk_buff *w5100_rx_skb(struct net_device *ndev)
853{
854 struct w5100_priv *priv = netdev_priv(ndev);
855 struct sk_buff *skb;
856 u16 rx_len;
857 u16 offset;
858 u8 header[2];
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900859 u16 rx_buf_len = w5100_read16(priv, W5100_S0_RX_RSR(priv));
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900860
861 if (rx_buf_len == 0)
862 return NULL;
863
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900864 offset = w5100_read16(priv, W5100_S0_RX_RD(priv));
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900865 w5100_readbuf(priv, offset, header, 2);
866 rx_len = get_unaligned_be16(header) - 2;
867
868 skb = netdev_alloc_skb_ip_align(ndev, rx_len);
869 if (unlikely(!skb)) {
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900870 w5100_write16(priv, W5100_S0_RX_RD(priv), offset + rx_buf_len);
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900871 w5100_command(priv, S0_CR_RECV);
872 ndev->stats.rx_dropped++;
873 return NULL;
874 }
875
876 skb_put(skb, rx_len);
877 w5100_readbuf(priv, offset + 2, skb->data, rx_len);
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900878 w5100_write16(priv, W5100_S0_RX_RD(priv), offset + 2 + rx_len);
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900879 w5100_command(priv, S0_CR_RECV);
880 skb->protocol = eth_type_trans(skb, ndev);
881
882 ndev->stats.rx_packets++;
883 ndev->stats.rx_bytes += rx_len;
884
885 return skb;
886}
887
888static void w5100_rx_work(struct work_struct *work)
889{
890 struct w5100_priv *priv = container_of(work, struct w5100_priv,
891 rx_work);
892 struct sk_buff *skb;
893
894 while ((skb = w5100_rx_skb(priv->ndev)))
895 netif_rx_ni(skb);
896
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900897 w5100_enable_intr(priv);
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900898}
899
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000900static int w5100_napi_poll(struct napi_struct *napi, int budget)
901{
902 struct w5100_priv *priv = container_of(napi, struct w5100_priv, napi);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000903 int rx_count;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000904
905 for (rx_count = 0; rx_count < budget; rx_count++) {
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900906 struct sk_buff *skb = w5100_rx_skb(priv->ndev);
907
908 if (skb)
909 netif_receive_skb(skb);
910 else
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000911 break;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000912 }
913
914 if (rx_count < budget) {
Yongbae Park5a3dba72015-03-10 11:35:07 +0900915 napi_complete(napi);
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900916 w5100_enable_intr(priv);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000917 }
918
919 return rx_count;
920}
921
922static irqreturn_t w5100_interrupt(int irq, void *ndev_instance)
923{
924 struct net_device *ndev = ndev_instance;
925 struct w5100_priv *priv = netdev_priv(ndev);
926
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900927 int ir = w5100_read(priv, W5100_S0_IR(priv));
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000928 if (!ir)
929 return IRQ_NONE;
Akinobu Mita0c165ff2016-04-15 00:11:33 +0900930 w5100_write(priv, W5100_S0_IR(priv), ir);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000931
Mike Sinkovsky376b16f2012-04-11 20:14:48 +0000932 if (ir & S0_IR_SENDOK) {
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000933 netif_dbg(priv, tx_done, ndev, "tx done\n");
934 netif_wake_queue(ndev);
935 }
936
937 if (ir & S0_IR_RECV) {
Akinobu Mita35ef7d62016-04-27 05:43:48 +0900938 w5100_disable_intr(priv);
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900939
940 if (priv->ops->may_sleep)
941 queue_work(priv->xfer_wq, &priv->rx_work);
942 else if (napi_schedule_prep(&priv->napi))
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000943 __napi_schedule(&priv->napi);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000944 }
945
946 return IRQ_HANDLED;
947}
948
949static irqreturn_t w5100_detect_link(int irq, void *ndev_instance)
950{
951 struct net_device *ndev = ndev_instance;
952 struct w5100_priv *priv = netdev_priv(ndev);
953
954 if (netif_running(ndev)) {
955 if (gpio_get_value(priv->link_gpio) != 0) {
956 netif_info(priv, link, ndev, "link is up\n");
957 netif_carrier_on(ndev);
958 } else {
959 netif_info(priv, link, ndev, "link is down\n");
960 netif_carrier_off(ndev);
961 }
962 }
963
964 return IRQ_HANDLED;
965}
966
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900967static void w5100_setrx_work(struct work_struct *work)
968{
969 struct w5100_priv *priv = container_of(work, struct w5100_priv,
970 setrx_work);
971
972 w5100_hw_start(priv);
973}
974
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000975static void w5100_set_rx_mode(struct net_device *ndev)
976{
977 struct w5100_priv *priv = netdev_priv(ndev);
978 bool set_promisc = (ndev->flags & IFF_PROMISC) != 0;
979
980 if (priv->promisc != set_promisc) {
981 priv->promisc = set_promisc;
Akinobu Mitabf2c6b902016-04-15 00:11:31 +0900982
983 if (priv->ops->may_sleep)
984 schedule_work(&priv->setrx_work);
985 else
986 w5100_hw_start(priv);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000987 }
988}
989
990static int w5100_set_macaddr(struct net_device *ndev, void *addr)
991{
992 struct w5100_priv *priv = netdev_priv(ndev);
993 struct sockaddr *sock_addr = addr;
994
995 if (!is_valid_ether_addr(sock_addr->sa_data))
996 return -EADDRNOTAVAIL;
997 memcpy(ndev->dev_addr, sock_addr->sa_data, ETH_ALEN);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +0000998 w5100_write_macaddr(priv);
999 return 0;
1000}
1001
1002static int w5100_open(struct net_device *ndev)
1003{
1004 struct w5100_priv *priv = netdev_priv(ndev);
1005
1006 netif_info(priv, ifup, ndev, "enabling\n");
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001007 w5100_hw_start(priv);
1008 napi_enable(&priv->napi);
1009 netif_start_queue(ndev);
1010 if (!gpio_is_valid(priv->link_gpio) ||
1011 gpio_get_value(priv->link_gpio) != 0)
1012 netif_carrier_on(ndev);
1013 return 0;
1014}
1015
1016static int w5100_stop(struct net_device *ndev)
1017{
1018 struct w5100_priv *priv = netdev_priv(ndev);
1019
1020 netif_info(priv, ifdown, ndev, "shutting down\n");
1021 w5100_hw_close(priv);
1022 netif_carrier_off(ndev);
1023 netif_stop_queue(ndev);
1024 napi_disable(&priv->napi);
1025 return 0;
1026}
1027
1028static const struct ethtool_ops w5100_ethtool_ops = {
1029 .get_drvinfo = w5100_get_drvinfo,
1030 .get_msglevel = w5100_get_msglevel,
1031 .set_msglevel = w5100_set_msglevel,
1032 .get_link = w5100_get_link,
1033 .get_regs_len = w5100_get_regs_len,
1034 .get_regs = w5100_get_regs,
1035};
1036
1037static const struct net_device_ops w5100_netdev_ops = {
1038 .ndo_open = w5100_open,
1039 .ndo_stop = w5100_stop,
1040 .ndo_start_xmit = w5100_start_tx,
1041 .ndo_tx_timeout = w5100_tx_timeout,
1042 .ndo_set_rx_mode = w5100_set_rx_mode,
1043 .ndo_set_mac_address = w5100_set_macaddr,
1044 .ndo_validate_addr = eth_validate_addr,
1045 .ndo_change_mtu = eth_change_mtu,
1046};
1047
Akinobu Mita850576c2016-04-15 00:11:30 +09001048static int w5100_mmio_probe(struct platform_device *pdev)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001049{
Jingoo Han5988aa62013-08-30 14:07:38 +09001050 struct wiznet_platform_data *data = dev_get_platdata(&pdev->dev);
Akinobu Mita850576c2016-04-15 00:11:30 +09001051 u8 *mac_addr = NULL;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001052 struct resource *mem;
Akinobu Mita850576c2016-04-15 00:11:30 +09001053 const struct w5100_ops *ops;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001054 int irq;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001055
Akinobu Mita850576c2016-04-15 00:11:30 +09001056 if (data && is_valid_ether_addr(data->mac_addr))
1057 mac_addr = data->mac_addr;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001058
1059 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Akinobu Mita850576c2016-04-15 00:11:30 +09001060 if (resource_size(mem) < W5100_BUS_DIRECT_SIZE)
1061 ops = &w5100_mmio_indirect_ops;
1062 else
1063 ops = &w5100_mmio_direct_ops;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001064
1065 irq = platform_get_irq(pdev, 0);
1066 if (irq < 0)
1067 return irq;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001068
Akinobu Mita850576c2016-04-15 00:11:30 +09001069 return w5100_probe(&pdev->dev, ops, sizeof(struct w5100_mmio_priv),
1070 mac_addr, irq, data ? data->link_gpio : -EINVAL);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001071}
1072
Akinobu Mita850576c2016-04-15 00:11:30 +09001073static int w5100_mmio_remove(struct platform_device *pdev)
1074{
1075 return w5100_remove(&pdev->dev);
1076}
1077
1078void *w5100_ops_priv(const struct net_device *ndev)
1079{
1080 return netdev_priv(ndev) +
1081 ALIGN(sizeof(struct w5100_priv), NETDEV_ALIGN);
1082}
1083EXPORT_SYMBOL_GPL(w5100_ops_priv);
1084
1085int w5100_probe(struct device *dev, const struct w5100_ops *ops,
1086 int sizeof_ops_priv, u8 *mac_addr, int irq, int link_gpio)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001087{
1088 struct w5100_priv *priv;
1089 struct net_device *ndev;
1090 int err;
Akinobu Mita850576c2016-04-15 00:11:30 +09001091 size_t alloc_size;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001092
Akinobu Mita850576c2016-04-15 00:11:30 +09001093 alloc_size = sizeof(*priv);
1094 if (sizeof_ops_priv) {
1095 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
1096 alloc_size += sizeof_ops_priv;
1097 }
1098 alloc_size += NETDEV_ALIGN - 1;
1099
1100 ndev = alloc_etherdev(alloc_size);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001101 if (!ndev)
1102 return -ENOMEM;
Akinobu Mita850576c2016-04-15 00:11:30 +09001103 SET_NETDEV_DEV(ndev, dev);
1104 dev_set_drvdata(dev, ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001105 priv = netdev_priv(ndev);
Akinobu Mita35ef7d62016-04-27 05:43:48 +09001106
1107 switch (ops->chip_id) {
1108 case W5100:
1109 priv->s0_regs = W5100_S0_REGS;
1110 priv->s0_tx_buf = W5100_TX_MEM_START;
1111 priv->s0_tx_buf_size = W5100_TX_MEM_SIZE;
1112 priv->s0_rx_buf = W5100_RX_MEM_START;
1113 priv->s0_rx_buf_size = W5100_RX_MEM_SIZE;
1114 break;
1115 case W5200:
1116 priv->s0_regs = W5200_S0_REGS;
1117 priv->s0_tx_buf = W5200_TX_MEM_START;
1118 priv->s0_tx_buf_size = W5200_TX_MEM_SIZE;
1119 priv->s0_rx_buf = W5200_RX_MEM_START;
1120 priv->s0_rx_buf_size = W5200_RX_MEM_SIZE;
1121 break;
1122 case W5500:
1123 priv->s0_regs = W5500_S0_REGS;
1124 priv->s0_tx_buf = W5500_TX_MEM_START;
1125 priv->s0_tx_buf_size = W5500_TX_MEM_SIZE;
1126 priv->s0_rx_buf = W5500_RX_MEM_START;
1127 priv->s0_rx_buf_size = W5500_RX_MEM_SIZE;
1128 break;
1129 default:
1130 err = -EINVAL;
1131 goto err_register;
1132 }
1133
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001134 priv->ndev = ndev;
Akinobu Mita850576c2016-04-15 00:11:30 +09001135 priv->ops = ops;
1136 priv->irq = irq;
1137 priv->link_gpio = link_gpio;
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001138
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001139 ndev->netdev_ops = &w5100_netdev_ops;
1140 ndev->ethtool_ops = &w5100_ethtool_ops;
1141 ndev->watchdog_timeo = HZ;
1142 netif_napi_add(ndev, &priv->napi, w5100_napi_poll, 16);
1143
1144 /* This chip doesn't support VLAN packets with normal MTU,
1145 * so disable VLAN for this device.
1146 */
1147 ndev->features |= NETIF_F_VLAN_CHALLENGED;
1148
1149 err = register_netdev(ndev);
1150 if (err < 0)
1151 goto err_register;
1152
Akinobu Mitabf2c6b902016-04-15 00:11:31 +09001153 priv->xfer_wq = create_workqueue(netdev_name(ndev));
1154 if (!priv->xfer_wq) {
1155 err = -ENOMEM;
1156 goto err_wq;
1157 }
1158
1159 INIT_WORK(&priv->rx_work, w5100_rx_work);
1160 INIT_WORK(&priv->tx_work, w5100_tx_work);
1161 INIT_WORK(&priv->setrx_work, w5100_setrx_work);
1162 INIT_WORK(&priv->restart_work, w5100_restart_work);
1163
Akinobu Mita850576c2016-04-15 00:11:30 +09001164 if (mac_addr)
1165 memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
1166 else
1167 eth_hw_addr_random(ndev);
1168
1169 if (priv->ops->init) {
1170 err = priv->ops->init(priv->ndev);
1171 if (err)
1172 goto err_hw;
1173 }
1174
Akinobu Mita35ef7d62016-04-27 05:43:48 +09001175 err = w5100_hw_reset(priv);
1176 if (err)
Akinobu Mita850576c2016-04-15 00:11:30 +09001177 goto err_hw;
Akinobu Mita850576c2016-04-15 00:11:30 +09001178
Akinobu Mitabf2c6b902016-04-15 00:11:31 +09001179 if (ops->may_sleep) {
1180 err = request_threaded_irq(priv->irq, NULL, w5100_interrupt,
1181 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1182 netdev_name(ndev), ndev);
1183 } else {
1184 err = request_irq(priv->irq, w5100_interrupt,
1185 IRQF_TRIGGER_LOW, netdev_name(ndev), ndev);
1186 }
Akinobu Mita850576c2016-04-15 00:11:30 +09001187 if (err)
1188 goto err_hw;
1189
1190 if (gpio_is_valid(priv->link_gpio)) {
1191 char *link_name = devm_kzalloc(dev, 16, GFP_KERNEL);
1192
1193 if (!link_name) {
1194 err = -ENOMEM;
1195 goto err_gpio;
1196 }
1197 snprintf(link_name, 16, "%s-link", netdev_name(ndev));
1198 priv->link_irq = gpio_to_irq(priv->link_gpio);
1199 if (request_any_context_irq(priv->link_irq, w5100_detect_link,
1200 IRQF_TRIGGER_RISING |
1201 IRQF_TRIGGER_FALLING,
1202 link_name, priv->ndev) < 0)
1203 priv->link_gpio = -EINVAL;
1204 }
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001205
1206 return 0;
1207
Akinobu Mita850576c2016-04-15 00:11:30 +09001208err_gpio:
1209 free_irq(priv->irq, ndev);
1210err_hw:
Akinobu Mitabf2c6b902016-04-15 00:11:31 +09001211 destroy_workqueue(priv->xfer_wq);
1212err_wq:
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001213 unregister_netdev(ndev);
1214err_register:
1215 free_netdev(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001216 return err;
1217}
Akinobu Mita850576c2016-04-15 00:11:30 +09001218EXPORT_SYMBOL_GPL(w5100_probe);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001219
Akinobu Mita850576c2016-04-15 00:11:30 +09001220int w5100_remove(struct device *dev)
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001221{
Akinobu Mita850576c2016-04-15 00:11:30 +09001222 struct net_device *ndev = dev_get_drvdata(dev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001223 struct w5100_priv *priv = netdev_priv(ndev);
1224
1225 w5100_hw_reset(priv);
1226 free_irq(priv->irq, ndev);
1227 if (gpio_is_valid(priv->link_gpio))
1228 free_irq(priv->link_irq, ndev);
1229
Akinobu Mitabf2c6b902016-04-15 00:11:31 +09001230 flush_work(&priv->setrx_work);
1231 flush_work(&priv->restart_work);
1232 flush_workqueue(priv->xfer_wq);
1233 destroy_workqueue(priv->xfer_wq);
1234
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001235 unregister_netdev(ndev);
1236 free_netdev(ndev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001237 return 0;
1238}
Akinobu Mita850576c2016-04-15 00:11:30 +09001239EXPORT_SYMBOL_GPL(w5100_remove);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001240
Jingoo Han4294beb2013-03-25 21:02:55 +00001241#ifdef CONFIG_PM_SLEEP
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001242static int w5100_suspend(struct device *dev)
1243{
Akinobu Mita850576c2016-04-15 00:11:30 +09001244 struct net_device *ndev = dev_get_drvdata(dev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001245 struct w5100_priv *priv = netdev_priv(ndev);
1246
1247 if (netif_running(ndev)) {
1248 netif_carrier_off(ndev);
1249 netif_device_detach(ndev);
1250
1251 w5100_hw_close(priv);
1252 }
1253 return 0;
1254}
1255
1256static int w5100_resume(struct device *dev)
1257{
Akinobu Mita850576c2016-04-15 00:11:30 +09001258 struct net_device *ndev = dev_get_drvdata(dev);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001259 struct w5100_priv *priv = netdev_priv(ndev);
1260
1261 if (netif_running(ndev)) {
1262 w5100_hw_reset(priv);
1263 w5100_hw_start(priv);
1264
1265 netif_device_attach(ndev);
1266 if (!gpio_is_valid(priv->link_gpio) ||
1267 gpio_get_value(priv->link_gpio) != 0)
1268 netif_carrier_on(ndev);
1269 }
1270 return 0;
1271}
Jingoo Han4294beb2013-03-25 21:02:55 +00001272#endif /* CONFIG_PM_SLEEP */
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001273
Akinobu Mita850576c2016-04-15 00:11:30 +09001274SIMPLE_DEV_PM_OPS(w5100_pm_ops, w5100_suspend, w5100_resume);
1275EXPORT_SYMBOL_GPL(w5100_pm_ops);
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001276
Akinobu Mita850576c2016-04-15 00:11:30 +09001277static struct platform_driver w5100_mmio_driver = {
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001278 .driver = {
1279 .name = DRV_NAME,
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001280 .pm = &w5100_pm_ops,
1281 },
Akinobu Mita850576c2016-04-15 00:11:30 +09001282 .probe = w5100_mmio_probe,
1283 .remove = w5100_mmio_remove,
Mike Sinkovsky8b1467a2012-04-04 19:33:54 +00001284};
Akinobu Mita850576c2016-04-15 00:11:30 +09001285module_platform_driver(w5100_mmio_driver);