1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Ethernet driver for the WIZnet W5100/W5200/W5500 chip.
5 * Copyright (C) 2016 Akinobu Mita <akinobu.mita@gmail.com>
8 * http://www.wiznet.co.kr/wp-content/uploads/wiznethome/Chip/W5100/Document/W5100_Datasheet_v1.2.6.pdf
9 * http://wiznethome.cafe24.com/wp-content/uploads/wiznethome/Chip/W5200/Documents/W5200_DS_V140E.pdf
10 * http://wizwiki.net/wiki/lib/exe/fetch.php?media=products:w5500:w5500_ds_v106e_141230.pdf
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/netdevice.h>
17 #include <linux/of_net.h>
18 #include <linux/of_device.h>
19 #include <linux/spi/spi.h>
23 #define W5100_SPI_WRITE_OPCODE 0xf0
24 #define W5100_SPI_READ_OPCODE 0x0f
26 static int w5100_spi_read(struct net_device
*ndev
, u32 addr
)
28 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
29 u8 cmd
[3] = { W5100_SPI_READ_OPCODE
, addr
>> 8, addr
& 0xff };
33 ret
= spi_write_then_read(spi
, cmd
, sizeof(cmd
), &data
, 1);
35 return ret
? ret
: data
;
38 static int w5100_spi_write(struct net_device
*ndev
, u32 addr
, u8 data
)
40 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
41 u8 cmd
[4] = { W5100_SPI_WRITE_OPCODE
, addr
>> 8, addr
& 0xff, data
};
43 return spi_write_then_read(spi
, cmd
, sizeof(cmd
), NULL
, 0);
46 static int w5100_spi_read16(struct net_device
*ndev
, u32 addr
)
51 ret
= w5100_spi_read(ndev
, addr
);
55 ret
= w5100_spi_read(ndev
, addr
+ 1);
57 return ret
< 0 ? ret
: data
| ret
;
60 static int w5100_spi_write16(struct net_device
*ndev
, u32 addr
, u16 data
)
64 ret
= w5100_spi_write(ndev
, addr
, data
>> 8);
68 return w5100_spi_write(ndev
, addr
+ 1, data
& 0xff);
71 static int w5100_spi_readbulk(struct net_device
*ndev
, u32 addr
, u8
*buf
,
76 for (i
= 0; i
< len
; i
++) {
77 int ret
= w5100_spi_read(ndev
, addr
+ i
);
87 static int w5100_spi_writebulk(struct net_device
*ndev
, u32 addr
, const u8
*buf
,
92 for (i
= 0; i
< len
; i
++) {
93 int ret
= w5100_spi_write(ndev
, addr
+ i
, buf
[i
]);
102 static const struct w5100_ops w5100_spi_ops
= {
105 .read
= w5100_spi_read
,
106 .write
= w5100_spi_write
,
107 .read16
= w5100_spi_read16
,
108 .write16
= w5100_spi_write16
,
109 .readbulk
= w5100_spi_readbulk
,
110 .writebulk
= w5100_spi_writebulk
,
113 #define W5200_SPI_WRITE_OPCODE 0x80
115 struct w5200_spi_priv
{
116 /* Serialize access to cmd_buf */
117 struct mutex cmd_lock
;
119 /* DMA (thus cache coherency maintenance) requires the
120 * transfer buffers to live in their own cache lines.
122 u8 cmd_buf
[4] ____cacheline_aligned
;
125 static struct w5200_spi_priv
*w5200_spi_priv(struct net_device
*ndev
)
127 return w5100_ops_priv(ndev
);
130 static int w5200_spi_init(struct net_device
*ndev
)
132 struct w5200_spi_priv
*spi_priv
= w5200_spi_priv(ndev
);
134 mutex_init(&spi_priv
->cmd_lock
);
139 static int w5200_spi_read(struct net_device
*ndev
, u32 addr
)
141 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
142 u8 cmd
[4] = { addr
>> 8, addr
& 0xff, 0, 1 };
146 ret
= spi_write_then_read(spi
, cmd
, sizeof(cmd
), &data
, 1);
148 return ret
? ret
: data
;
151 static int w5200_spi_write(struct net_device
*ndev
, u32 addr
, u8 data
)
153 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
154 u8 cmd
[5] = { addr
>> 8, addr
& 0xff, W5200_SPI_WRITE_OPCODE
, 1, data
};
156 return spi_write_then_read(spi
, cmd
, sizeof(cmd
), NULL
, 0);
159 static int w5200_spi_read16(struct net_device
*ndev
, u32 addr
)
161 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
162 u8 cmd
[4] = { addr
>> 8, addr
& 0xff, 0, 2 };
166 ret
= spi_write_then_read(spi
, cmd
, sizeof(cmd
), &data
, sizeof(data
));
168 return ret
? ret
: be16_to_cpu(data
);
171 static int w5200_spi_write16(struct net_device
*ndev
, u32 addr
, u16 data
)
173 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
175 addr
>> 8, addr
& 0xff,
176 W5200_SPI_WRITE_OPCODE
, 2,
177 data
>> 8, data
& 0xff
180 return spi_write_then_read(spi
, cmd
, sizeof(cmd
), NULL
, 0);
183 static int w5200_spi_readbulk(struct net_device
*ndev
, u32 addr
, u8
*buf
,
186 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
187 struct w5200_spi_priv
*spi_priv
= w5200_spi_priv(ndev
);
188 struct spi_transfer xfer
[] = {
190 .tx_buf
= spi_priv
->cmd_buf
,
191 .len
= sizeof(spi_priv
->cmd_buf
),
200 mutex_lock(&spi_priv
->cmd_lock
);
202 spi_priv
->cmd_buf
[0] = addr
>> 8;
203 spi_priv
->cmd_buf
[1] = addr
;
204 spi_priv
->cmd_buf
[2] = len
>> 8;
205 spi_priv
->cmd_buf
[3] = len
;
206 ret
= spi_sync_transfer(spi
, xfer
, ARRAY_SIZE(xfer
));
208 mutex_unlock(&spi_priv
->cmd_lock
);
213 static int w5200_spi_writebulk(struct net_device
*ndev
, u32 addr
, const u8
*buf
,
216 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
217 struct w5200_spi_priv
*spi_priv
= w5200_spi_priv(ndev
);
218 struct spi_transfer xfer
[] = {
220 .tx_buf
= spi_priv
->cmd_buf
,
221 .len
= sizeof(spi_priv
->cmd_buf
),
230 mutex_lock(&spi_priv
->cmd_lock
);
232 spi_priv
->cmd_buf
[0] = addr
>> 8;
233 spi_priv
->cmd_buf
[1] = addr
;
234 spi_priv
->cmd_buf
[2] = W5200_SPI_WRITE_OPCODE
| (len
>> 8);
235 spi_priv
->cmd_buf
[3] = len
;
236 ret
= spi_sync_transfer(spi
, xfer
, ARRAY_SIZE(xfer
));
238 mutex_unlock(&spi_priv
->cmd_lock
);
243 static const struct w5100_ops w5200_ops
= {
246 .read
= w5200_spi_read
,
247 .write
= w5200_spi_write
,
248 .read16
= w5200_spi_read16
,
249 .write16
= w5200_spi_write16
,
250 .readbulk
= w5200_spi_readbulk
,
251 .writebulk
= w5200_spi_writebulk
,
252 .init
= w5200_spi_init
,
255 #define W5500_SPI_BLOCK_SELECT(addr) (((addr) >> 16) & 0x1f)
256 #define W5500_SPI_READ_CONTROL(addr) (W5500_SPI_BLOCK_SELECT(addr) << 3)
257 #define W5500_SPI_WRITE_CONTROL(addr) \
258 ((W5500_SPI_BLOCK_SELECT(addr) << 3) | BIT(2))
260 struct w5500_spi_priv
{
261 /* Serialize access to cmd_buf */
262 struct mutex cmd_lock
;
264 /* DMA (thus cache coherency maintenance) requires the
265 * transfer buffers to live in their own cache lines.
267 u8 cmd_buf
[3] ____cacheline_aligned
;
270 static struct w5500_spi_priv
*w5500_spi_priv(struct net_device
*ndev
)
272 return w5100_ops_priv(ndev
);
275 static int w5500_spi_init(struct net_device
*ndev
)
277 struct w5500_spi_priv
*spi_priv
= w5500_spi_priv(ndev
);
279 mutex_init(&spi_priv
->cmd_lock
);
284 static int w5500_spi_read(struct net_device
*ndev
, u32 addr
)
286 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
290 W5500_SPI_READ_CONTROL(addr
)
295 ret
= spi_write_then_read(spi
, cmd
, sizeof(cmd
), &data
, 1);
297 return ret
? ret
: data
;
300 static int w5500_spi_write(struct net_device
*ndev
, u32 addr
, u8 data
)
302 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
306 W5500_SPI_WRITE_CONTROL(addr
),
310 return spi_write_then_read(spi
, cmd
, sizeof(cmd
), NULL
, 0);
313 static int w5500_spi_read16(struct net_device
*ndev
, u32 addr
)
315 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
319 W5500_SPI_READ_CONTROL(addr
)
324 ret
= spi_write_then_read(spi
, cmd
, sizeof(cmd
), &data
, sizeof(data
));
326 return ret
? ret
: be16_to_cpu(data
);
329 static int w5500_spi_write16(struct net_device
*ndev
, u32 addr
, u16 data
)
331 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
335 W5500_SPI_WRITE_CONTROL(addr
),
340 return spi_write_then_read(spi
, cmd
, sizeof(cmd
), NULL
, 0);
343 static int w5500_spi_readbulk(struct net_device
*ndev
, u32 addr
, u8
*buf
,
346 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
347 struct w5500_spi_priv
*spi_priv
= w5500_spi_priv(ndev
);
348 struct spi_transfer xfer
[] = {
350 .tx_buf
= spi_priv
->cmd_buf
,
351 .len
= sizeof(spi_priv
->cmd_buf
),
360 mutex_lock(&spi_priv
->cmd_lock
);
362 spi_priv
->cmd_buf
[0] = addr
>> 8;
363 spi_priv
->cmd_buf
[1] = addr
;
364 spi_priv
->cmd_buf
[2] = W5500_SPI_READ_CONTROL(addr
);
365 ret
= spi_sync_transfer(spi
, xfer
, ARRAY_SIZE(xfer
));
367 mutex_unlock(&spi_priv
->cmd_lock
);
372 static int w5500_spi_writebulk(struct net_device
*ndev
, u32 addr
, const u8
*buf
,
375 struct spi_device
*spi
= to_spi_device(ndev
->dev
.parent
);
376 struct w5500_spi_priv
*spi_priv
= w5500_spi_priv(ndev
);
377 struct spi_transfer xfer
[] = {
379 .tx_buf
= spi_priv
->cmd_buf
,
380 .len
= sizeof(spi_priv
->cmd_buf
),
389 mutex_lock(&spi_priv
->cmd_lock
);
391 spi_priv
->cmd_buf
[0] = addr
>> 8;
392 spi_priv
->cmd_buf
[1] = addr
;
393 spi_priv
->cmd_buf
[2] = W5500_SPI_WRITE_CONTROL(addr
);
394 ret
= spi_sync_transfer(spi
, xfer
, ARRAY_SIZE(xfer
));
396 mutex_unlock(&spi_priv
->cmd_lock
);
401 static const struct w5100_ops w5500_ops
= {
404 .read
= w5500_spi_read
,
405 .write
= w5500_spi_write
,
406 .read16
= w5500_spi_read16
,
407 .write16
= w5500_spi_write16
,
408 .readbulk
= w5500_spi_readbulk
,
409 .writebulk
= w5500_spi_writebulk
,
410 .init
= w5500_spi_init
,
413 static const struct of_device_id w5100_of_match
[] = {
414 { .compatible
= "wiznet,w5100", .data
= (const void*)W5100
, },
415 { .compatible
= "wiznet,w5200", .data
= (const void*)W5200
, },
416 { .compatible
= "wiznet,w5500", .data
= (const void*)W5500
, },
419 MODULE_DEVICE_TABLE(of
, w5100_of_match
);
421 static int w5100_spi_probe(struct spi_device
*spi
)
423 const struct of_device_id
*of_id
;
424 const struct w5100_ops
*ops
;
425 kernel_ulong_t driver_data
;
427 const void *mac
= of_get_mac_address(spi
->dev
.of_node
);
429 if (spi
->dev
.of_node
) {
430 of_id
= of_match_device(w5100_of_match
, &spi
->dev
);
433 driver_data
= (kernel_ulong_t
)of_id
->data
;
435 driver_data
= spi_get_device_id(spi
)->driver_data
;
438 switch (driver_data
) {
440 ops
= &w5100_spi_ops
;
445 priv_size
= sizeof(struct w5200_spi_priv
);
449 priv_size
= sizeof(struct w5500_spi_priv
);
455 return w5100_probe(&spi
->dev
, ops
, priv_size
, mac
, spi
->irq
, -EINVAL
);
458 static int w5100_spi_remove(struct spi_device
*spi
)
460 return w5100_remove(&spi
->dev
);
463 static const struct spi_device_id w5100_spi_ids
[] = {
469 MODULE_DEVICE_TABLE(spi
, w5100_spi_ids
);
471 static struct spi_driver w5100_spi_driver
= {
475 .of_match_table
= w5100_of_match
,
477 .probe
= w5100_spi_probe
,
478 .remove
= w5100_spi_remove
,
479 .id_table
= w5100_spi_ids
,
481 module_spi_driver(w5100_spi_driver
);
483 MODULE_DESCRIPTION("WIZnet W5100/W5200/W5500 Ethernet driver for SPI mode");
484 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
485 MODULE_LICENSE("GPL");