1 /* W5500 Stand-alone Ethernet Controller with SPI
2  *
3  * Copyright (c) 2020 Linumiz
4  * Author: Parthiban Nallathambi <parthiban@linumiz.com>
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/drivers/spi.h>
11 
12 #ifndef _W5500_
13 #define _W5500_
14 
15 /*
16  * W5500 common registers
17  */
18 #define W5500_COMMON_REGS	0x0000
19 #define W5500_MR		0x0000 /* Mode Register */
20 #define W5500_GW		0x0001
21 #define MR_RST			0x80 /* S/W reset */
22 #define MR_PB			0x10 /* Ping block */
23 #define MR_AI			0x02 /* Address Auto-Increment */
24 #define MR_IND			0x01 /* Indirect mode */
25 #define W5500_SHAR		0x0009 /* Source MAC address */
26 #define W5500_IR		0x0015 /* Interrupt Register */
27 #define W5500_COMMON_REGS_LEN	0x0040
28 
29 #define W5500_Sn_MR		0x0000 /* Sn Mode Register */
30 #define W5500_Sn_CR		0x0001 /* Sn Command Register */
31 #define W5500_Sn_IR		0x0002 /* Sn Interrupt Register */
32 #define W5500_Sn_SR		0x0003 /* Sn Status Register */
33 #define W5500_Sn_TX_FSR		0x0020 /* Sn Transmit free memory size */
34 #define W5500_Sn_TX_RD		0x0022 /* Sn Transmit memory read pointer */
35 #define W5500_Sn_TX_WR		0x0024 /* Sn Transmit memory write pointer */
36 #define W5500_Sn_RX_RSR		0x0026 /* Sn Receive free memory size */
37 #define W5500_Sn_RX_RD		0x0028 /* Sn Receive memory read pointer */
38 
39 #define W5500_S0_REGS		0x10000
40 
41 #define W5500_S0_MR		(W5500_S0_REGS + W5500_Sn_MR)
42 #define S0_MR_MACRAW		0x04 /* MAC RAW mode */
43 #define S0_MR_MF		0x40 /* MAC Filter for W5500 */
44 #define W5500_S0_CR		(W5500_S0_REGS + W5500_Sn_CR)
45 #define S0_CR_OPEN		0x01 /* OPEN command */
46 #define S0_CR_CLOSE		0x10 /* CLOSE command */
47 #define S0_CR_SEND		0x20 /* SEND command */
48 #define S0_CR_RECV		0x40 /* RECV command */
49 #define W5500_S0_IR		(W5500_S0_REGS + W5500_Sn_IR)
50 #define S0_IR_SENDOK		0x10 /* complete sending */
51 #define S0_IR_RECV		0x04 /* receiving data */
52 #define W5500_S0_SR		(W5500_S0_REGS + W5500_Sn_SR)
53 #define S0_SR_MACRAW		0x42 /* mac raw mode */
54 #define W5500_S0_TX_FSR		(W5500_S0_REGS + W5500_Sn_TX_FSR)
55 #define W5500_S0_TX_RD		(W5500_S0_REGS + W5500_Sn_TX_RD)
56 #define W5500_S0_TX_WR		(W5500_S0_REGS + W5500_Sn_TX_WR)
57 #define W5500_S0_RX_RSR		(W5500_S0_REGS + W5500_Sn_RX_RSR)
58 #define W5500_S0_RX_RD		(W5500_S0_REGS + W5500_Sn_RX_RD)
59 #define W5500_S0_IMR		(W5500_S0_REGS + W5500_Sn_IMR)
60 
61 #define W5500_S0_MR_MF		7 /* MAC Filter for W5500 */
62 #define W5500_Sn_REGS_LEN	0x0040
63 #define W5500_SIMR		0x0018 /* Socket Interrupt Mask Register */
64 #define IR_S0			0x01
65 #define RTR_DEFAULT		2000
66 #define W5500_RTR		0x0019 /* Retry Time-value Register */
67 
68 
69 #define W5500_Sn_RXMEM_SIZE(n)	\
70 		(0x1001e + (n) * 0x40000) /* Sn RX Memory Size */
71 #define W5500_Sn_TXMEM_SIZE(n)	\
72 		(0x1001f + (n) * 0x40000) /* Sn TX Memory Size */
73 
74 #define W5500_Sn_TX_MEM_START	0x20000
75 #define W5500_TX_MEM_SIZE	0x04000
76 #define W5500_Sn_RX_MEM_START	0x30000
77 #define W5500_RX_MEM_SIZE	0x04000
78 
79 /* Delay for PHY write/read operations (25.6 us) */
80 #define W5500_PHY_ACCESS_DELAY		26U
81 struct w5500_config {
82 	struct spi_dt_spec spi;
83 	struct gpio_dt_spec interrupt;
84 	struct gpio_dt_spec reset;
85 	void (*config_func)(void);
86 	uint8_t full_duplex;
87 	int32_t timeout;
88 };
89 
90 struct w5500_runtime {
91 	struct net_if *iface;
92 
93 	K_KERNEL_STACK_MEMBER(thread_stack,
94 			      CONFIG_ETH_W5500_RX_THREAD_STACK_SIZE);
95 	struct k_thread thread;
96 	uint8_t mac_addr[6];
97 	struct gpio_callback gpio_cb;
98 	struct k_sem tx_sem;
99 	struct k_sem int_sem;
100 	void (*generate_mac)(uint8_t *mac);
101 	uint8_t buf[NET_ETH_MAX_FRAME_SIZE];
102 };
103 
104 #endif /*_W5500_*/
105