Embedded Xinu Operating System
An ongoing research project and educational operating system.
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
lan7800.h
Go to the documentation of this file.
1 
13 #ifndef _LAN7800_H_
14 #define _LAN7800_H_
15 
16 #include "usb_util.h"
17 #include <clock.h> /* for udelay() */
18 
19 #define LAN7800_VENDOR_ID 0x424
20 #define LAN7800_PRODUCT_ID 0x7800
21 
22 usb_status_t lan7800_write_reg(struct usb_device *udev, uint32_t index, uint32_t data);
23 usb_status_t lan7800_read_reg(struct usb_device *udev, uint32_t index, uint32_t *data);
24 usb_status_t lan7800_modify_reg(struct usb_device *udev, uint32_t index, uint32_t mask, uint32_t set);
25 usb_status_t lan7800_set_reg_bits(struct usb_device *udev, uint32_t index, uint32_t set);
26 usb_status_t lan7800_wait_device_attached(ushort minor);
27 usb_status_t lan7800_set_mac_address(struct usb_device *udev, const uint8_t *macaddr);
28 usb_status_t lan7800_get_mac_address(struct usb_device *udev, uint8_t *macaddr);
29 
30 struct usb_xfer_request;
31 
32 void lan7800_rx_complete(struct usb_xfer_request *req);
33 void lan7800_tx_complete(struct usb_xfer_request *req);
34 
35 static inline void
36 __lan7800_dump_reg(struct usb_device *udev, uint32_t index, const char *name)
37 {
38  uint32_t val = 0;
39  lan7800_read_reg(udev, index, &val);
40  kprintf("LAN7800: %s = 0x%08X\r\n", name, val);
41 }
42 
43 #define lan7800_dump_reg(udev, index) __lan7800_dump_reg(udev, index, #index)
44 
45 /* lan7800_mdio methods */
46 usb_status_t lan7800_mdio_read (struct usb_device *udev, int phy_id, int idx);
47 void lan7800_mdio_write(struct usb_device *udev, int phy_id, int idx, int regval);
48 
49 /* LAN7800 function declarations (defined in lan7800.c) */
50 usb_status_t lan7800_read_raw_otp(struct usb_device *udev, uint32_t offset,
51  uint32_t length, uint8_t *data);
52 int lan7800_read_otp_mac(unsigned char *enetaddr,
53  struct usb_device *udev);
54 usb_status_t lan7800_eeprom_confirm_not_busy(struct usb_device *udev);
55 usb_status_t lan7800_wait_eeprom(struct usb_device *udev);
56 usb_status_t lan7800_read_raw_eeprom(struct usb_device *dev, uint32_t offset,
57  uint32_t length, uint8_t *data);
58 usb_status_t lan7800_read_eeprom(struct usb_device *dev, uint32_t offset,
59  uint32_t length, uint8_t *data);
60 usb_status_t lan7800_set_rx_max_frame_length(struct usb_device *dev, int size);
61 
62 #define NETIF_F_RXCSUM 4
63 #define NETIF_F_HW_VLAN_CTAG_RX 2
64 #define NETIF_F_HW_VLAN_CTAG_FILTER 1
65 usb_status_t lan7800_set_features(struct usb_device *dev, uint32_t features);
66 usb_status_t lan7800_init(struct usb_device *dev, uint8_t* macaddress);
67 
68 usb_status_t lan7800_mdio_wait_for_bit(struct usb_device *udev,
69  const uint32_t reg,
70  const uint32_t mask,
71  const bool set);
72 
73 #define lan7800_wait_for_bit lan7800_mdio_wait_for_bit
74 
75 /***************************************************************************
76  * The following regsiter set is primarily based on that of Linux's open-source 78xx driver:
77  * https://github.com/torvalds/linux/blob/8fa3b6f9392bf6d90cb7b908e07bd90166639f0a/drivers/net/usb/lan78xx.h
78  * and U-Boot:
79  * https://github.com/trini/u-boot/blob/890e79f2b1c26c5ba1a86d179706348aec7feef7/drivers/usb/eth/lan7x.h
80  ***************************************************************************/
81 
82 /* TX command word A */
83 #define TX_CMD_A_IGE_ 0x20000000
84 #define TX_CMD_A_ICE_ 0x10000000
85 
86 /* ??? LSO = Last segment... */
87 #define TX_CMD_A_LSO 0x08000000
88 
89 #define TX_CMD_A_IPE_ 0x04000000
90 #define TX_CMD_A_TPE_ 0x02000000
91 #define TX_CMD_A_IVTG_ 0x01000000
92 #define TX_CMD_A_RVTG_ 0x00800000
93 
94 /* TX word A buffer size. */
95 #define LAN7800_TX_CMD_A_BUF_SIZE 0x000FFFFF
96 
97 /* TX command word B */
98 #define TX_CMD_B_MSS_SHIFT_ 16
99 #define TX_CMD_B_MSS_MASK_ 0x3FFF0000
100 #define TX_CMD_B_MSS_MIN_ (unsigned short)8
101 #define TX_CMD_B_VTAG_MASK_ 0x0000FFFF
102 #define TX_CMD_B_VTAG_PRI_MASK_ 0x0000E000
103 #define TX_CMD_B_VTAG_CFI_MASK_ 0x00001000
104 #define TX_CMD_B_VTAG_VID_MASK_ 0x00000FFF
105 
106 
107 /* TX/RX Overhead
108  * Used in sum to allocate for Tx transfer buffer in etherOpen() */
109 #define LAN7800_TX_OVERHEAD 8
110 #define LAN7800_RX_OVERHEAD 4
111 
112 /* According to U-Boot's LAN78xx driver. */
113 #define LAN7800_HS_USB_PKT_SIZE 512
114 
115 /* Read/write regsiters. */
116 #define LAN7800_VENDOR_REQUEST_WRITE 0xA0
117 #define LAN7800_VENDOR_REQUEST_READ 0xA1
118 #define LAN7800_VENDER_REQUEST_GET_STATS 0xA2
119 
120 /* MAC TX/RX */
121 #define LAN7800_MAC_RX 0x104
122 #define LAN7800_MAC_TX 0x108
123 
124 /* High and low RX register offsets */
125 #define LAN7800_ADDRH 0x118
126 #define LAN7800_ADDRL 0x11C
127 
128 /* Offset of Hardware Configuration Register. */
129 #define LAN7800_HW_CFG 0x010
130 
131 /* ??? */
132 #define LAN7800_HW_CFG_SRST 0x00000001
133 #define LAN7800_HW_CFG_LRST 0x00000002
134 
135 #define LAN7800_BURST_CAP 0x090
136 #define LAN7800_BURST_CAP_SIZE 0x000000FF
137 
138 /* Loopback mode. */
139 #define MAC_CR_LOOPBACK 0x00000400
140 
141 /* Received Ethernet Frame Length */
142 #define LAN7800_RX_STS_FL 0x00003FFF
143 
144 /* ??? Not entirely sure, appears to be Received Ethernet Error Summary. */
145 #define LAN7800_RX_CMD_A_RX_ERR 0xC03F0000
146 
147 /* The following register definitions were pulled from U-Boot's implementation:
148  * https://github.com/Screenly/u-boot/blob/master/drivers/usb/eth/lan78xx.c
149  */
150 
151 /* For initializing Tx. */
152 #define LAN7800_TX_FLOW 0x0D0
153 
154 /* For initializing Rx. */
155 #define LAN7800_RFE_CTL 0x0B0
156 
157 #define LAN7800_MAC_CR 0x100
158 
159 /* Adaptive & Dynamic Polling at MAC layer ??? */
160 #define LAN7800_MAC_CR_ADP (1 << 13)
161 
162 #define LAN7800_FCT_TX_CTL 0x0C4
163 
164 #define LAN7800_ETH_FRAME_LEN 1514
165 #define LAN7800_MAC_RX_MAX_SIZE(mtu) \
166  ((mtu) << 16)
167 #define LAN7800_MAC_RX_MAX_SIZE_DEFAULT LAN7800_MAC_RX_MAX_SIZE(LAN7800_ETH_FRAME_LEN + 4 /* VLAN */ + 4 /* CRC */)
168 
169 #define LAN7800_MAC_RX_FCS_STRIP (1 << 4)
170 
171 #define LAN7800_FCT_RX_CTL 0x0C0
172 
173 /* MAF is where U-Boot writes the hardware address. */
174 #define LAN78XX_MAF_BASE 0x400
175 #define LAN78XX_MAF_HIX 0x00
176 #define LAN78XX_MAF_LOX 0x04
177 #define LAN78XX_MAF_HI_BEGIN (LAN78XX_MAF_BASE + LAN78XX_MAF_HIX)
178 #define LAN78XX_MAF_LO_BEGIN (LAN78XX_MAF_BASE + LAN78XX_MAF_LOX)
179 #define LAN78XX_MAF_HI(index) (LAN78XX_MAF_BASE + (8 * (index)) + \
180  LAN78XX_MAF_HIX)
181 #define LAN78XX_MAF_LO(index) (LAN78XX_MAF_BASE + (8 * (index)) + \
182  LAN78XX_MAF_LOX)
183 #define LAN78XX_MAF_HI_VALID BIT(31)
184 
185 
186 /* MII_ACC */
187 #define MII_ACC 0x120
188 #define MII_ACC_MII_READ 0x0
189 #define MII_ACC_MII_WRITE 0x2
190 #define MII_ACC_MII_BUSY 1 << 0
191 
192 #define MII_DATA 0x124
193 
194 #define TX_OVERHEAD 8
195 #define RX_OVERHEAD 10
196 #define RX_CMD_A_LEN_MASK 0x00003FFF
197 #define RX_CMD_A_RED 0x00400000
198 #define TX_CMD_A_LEN_MASK 0x000FFFFF
199 #define TX_CMD_A_FCS 0x00400000
200 #define EINVAL 22
201 
202 /* OTP (One-Time Programmable) */
203 #define LAN7800_OTP_BASE 0x00001000
204 #define LAN7800_OTP_PWR_DN LAN7800_OTP_BASE + 4 * 0x00
205 #define LAN7800_OTP_PWR_DN_PWRDN_N 0x01
206 #define LAN7800_OTP_ADDR1 LAN7800_OTP_BASE + 4 * 0x01
207 #define LAN7800_OTP_ADDR1_15_11 0x1F
208 #define LAN7800_OTP_ADDR2 LAN7800_OTP_BASE + 4 * 0x02
209 #define LAN7800_OTP_ADDR2_10_3 0xFF
210 #define LAN7800_OTP_FUNC_CMD LAN7800_OTP_BASE + 4 * 0x08
211 #define LAN7800_OTP_FUNC_CMD_READ 0x01
212 #define LAN7800_OTP_CMD_GO LAN7800_OTP_BASE + 4 * 0x0A
213 #define LAN7800_OTP_CMD_GO_GO 0x01
214 #define LAN7800_OTP_STATUS LAN7800_OTP_BASE + 4 * 0x0C
215 #define LAN7800_OTP_STATUS_BUSY 0x01
216 #define LAN7800_OTP_RD_DATA 0x01
217 #define LAN7800_OTP_INDICATOR_1 0xF3
218 #define LAN7800_OTP_INDICATOR_2 0xF7
219 
220 #define EIO 5
221 
222 /* EEPROM */
223 #define LAN7800_EEPROM_INDICATOR 0xA5
224 #define LAN7800_E2P_CMD 0x040
225 #define LAN7800_E2P_CMD_EPC_BUSY 0x80000000
226 #define LAN7800_EEPROM_MAC_OFFSET 0x01
227 #define LAN7800_E2P_CMD_EPC_TIMEOUT 0x00000400
228 #define LAN7800_HW_CFG_LED3_EN 0x00800000
229 #define LAN7800_HW_CFG_LED2_EN 0x00400000
230 #define LAN7800_HW_CFG_LED1_EN 0x00200000
231 #define LAN7800_HW_CFG_LED0_EN 0x00100000
232 #define LAN7800_E2P_CMD_EPC_CMD_READ 0x00000000
233 #define LAN7800_E2P_CMD_EPC_ADDR_MASK 0x000001FF
234 #define LAN7800_E2P_DATA 0x44
235 #define LAN7800_USB_CFG1 0x084
236 #define LAN7800_USB_CFG1_LTM_ENABLE 0x00000100
237 
238 #define LAN7800_LTM_BELT_IDLE0 0x0E0
239 #define LAN7800_LTM_BELT_IDLE1 0x0E4
240 #define LAN7800_LTM_BELT_ACT0 0x0E8
241 #define LAN7800_LTM_BELT_ACT1 0x0EC
242 #define LAN7800_LTM_INACTIVE0 0x0F0
243 #define LAN7800_LTM_INACTIVE1 0x0F4
244 #define LAN7800_MAC_RX_MAX_SIZE_MASK 0x3FFF0000
245 #define LAN7800_MAC_RX_MAX_SIZE_SHIFT 16
246 
247 /* RFE */
248 #define RFE_CTL_TCPUDP_COE 0x00001000
249 #define RFE_CTL_IP_COE 0x00000800
250 #define RFE_CTL_ICMP_COE 0x00002000
251 #define RFE_CTL_IGMP_COE 0x00004000
252 #define RFE_CTL_TCPUDP_COE 0x00001000
253 #define NETIF_F_HW_VLAN_CTAG_RX 2
254 #define RFE_CTL_VLAN_STRIP 0x00000080
255 #define NETIF_F_HW_VLAN_CTAG_FILTER 1
256 #define RFE_CTL_VLAN_FILTER 0x00000020
257 #define NETIF_F_RXCSUM 4
258 
259 /* RESET CONTINUE */
260 #define LAN7800_USB_CFG0 0x080
261 #define LAN7800_USB_CFG_BIR 0x00000040
262 #define LAN7800_MAX_TX_FIFO_SIZE 12 * 1024
263 #define LAN7800_MAX_RX_FIFO_SIZE 12 * 1024
264 #define LAN7800_DEFAULT_BURST_CAP_SIZE LAN7800_MAX_TX_FIFO_SIZE
265 #define LAN7800_FS_USB_PKT_SIZE 64
266 #define LAN7800_HS_USB_PKT_SIZE 512
267 #define LAN7800_SS_USB_PKT_SIZE 1024
268 
269 #define LAN7800_BULK_IN_DLY 0x094
270 #define LAN7800_DEFAULT_BULK_IN_DELAY 0x0800
271 
272 #define LAN7800_HW_CFG_MEF 0x00000010
273 #define LAN7800_USB_CFG_BCE 0x00000020
274 
275 /* FIFO */
276 #define LAN7800_INT_STS 0x0C
277 #define LAN7800_INT_STS_CLEAR_ALL 0xFFFFFFFF
278 #define LAN7800_FLOW 0x10C
279 #define LAN7800_FCT_FLOW 0x0D0
280 #define LAN7800_FCT_TX_FIFO_END 0x0CC
281 #define LAN7800_FCT_RX_FIFO_END 0x0C8
282 
283 #define LAN7800_RFE_CTL_BCAST_EN 0x00000400
284 #define LAN7800_RFE_CTL_DA_PERFECT 0x00000002
285 #define LAN7800_RFE_CTL 0x0B0
286 #define LAN7800_RFE_CTL_UCAST_EN 0x00000100
287 #define LAN7800_RFE_CTL_MCAST_EN 0x00000200
288 #define LAN7800_RFE_CTL_MCAST_HASH 0x00000008
289 
290 /* PMT */
291 #define LAN7800_PMT_CTL 0x014
292 #define LAN7800_PMT_CTL_PHY_RST 0x00000010
293 
294 /* EEPROM, MAC */
295 #define LAN7800_EEPROM_INDICATOR 0xA5
296 #define LAN7800_MAC_CR_AUTO_DUPLEX 0x00001000
297 #define LAN7800_MAC_CR_AUTO_SPEED 0x00000800
298 #define LAN7800_FCT_TX_CTL 0x0C4
299 
300 /* DMA buffer size */
301 #define LAN7800_ETH_MTU 1500
302 
303 #define LAN7800_ETH_ALEN 6
304 
305 #define LAN7800_ETH_VLAN_LEN 4
306 #define LAN7800_MAC_RX_RXEN 0x00000001
307 #define LAN7800_MAC_TX_TXEN 0x00000001
308 
309 /* FCT */
310 #define LAN7800_FCT_RX_CTL 0x0C0
311 #define LAN7800_FCT_RX_CTL_EN 0x80000000
312 #define LAN7800_FCT_TX_CTL 0x0C4
313 #define LAN7800_FCT_TX_CTL_EN 0x80000000
314 
315 #endif /* _LAN7800_H_ */
usb_status_t lan7800_write_reg(struct usb_device *udev, uint32_t index, uint32_t data)
Definition: lan7800.c:36
usb_status_t lan7800_wait_device_attached(ushort minor)
Definition: etherInit.c:210
usb_status_t lan7800_read_raw_eeprom(struct usb_device *udev, uint32_t offset, uint32_t length, uint8_t *data)
Definition: lan7800.c:237
usb_status_t lan7800_set_mac_address(struct usb_device *udev, const uint8_t *macaddr)
Definition: lan7800.c:122
usb_status_t lan7800_get_mac_address(struct usb_device *udev, uint8_t *macaddr)
Definition: lan7800.c:148
usb_status_t lan7800_set_rx_max_frame_length(struct usb_device *udev, int size)
Definition: lan7800.c:294
usb_status_t lan7800_mdio_wait_for_bit(struct usb_device *udev, const uint32_t reg, const uint32_t mask, const bool set)
Definition: lan7800.c:184
usb_status_t lan7800_modify_reg(struct usb_device *udev, uint32_t index, uint32_t mask, uint32_t set)
Definition: lan7800.c:80
usb_status_t lan7800_init(struct usb_device *udev, uint8_t *macaddress)
Definition: lan7800.c:367
void lan7800_rx_complete(struct usb_xfer_request *req)
Definition: etherInterrupt.c:58
void lan7800_tx_complete(struct usb_xfer_request *req)
Definition: etherInterrupt.c:33
usb_status_t lan7800_set_reg_bits(struct usb_device *udev, uint32_t index, uint32_t set)
Definition: lan7800.c:106
usb_status_t lan7800_read_reg(struct usb_device *udev, uint32_t index, uint32_t *data)
Definition: lan7800.c:56
usb_status_t lan7800_set_features(struct usb_device *udev, uint32_t set)
Definition: lan7800.c:330
syscall kprintf(const char *format,...)
Definition: kprintf.c:26