aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorJohn Linn <john.linn@xilinx.com>2009-08-20 05:52:16 -0400
committerDavid S. Miller <davem@davemloft.net>2009-08-20 05:52:16 -0400
commitbb81b2ddfa194b6d12761a350b5b5985cecae0a9 (patch)
tree3955dda3791dd39a3b0c08ddbd658fc0b340e5ca /drivers
parent886f9fe68310168358f55028a03553ae154dfeb6 (diff)
net: add Xilinx emac lite device driver
This patch adds support for the Xilinx Ethernet Lite device. The soft logic core from Xilinx is typically used on Virtex and Spartan designs attached to either a PowerPC or a Microblaze processor. Signed-off-by: Sadanand M <sadanan@xilinx.com> Signed-off-by: John Linn <john.linn@xilinx.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/net/Kconfig6
-rw-r--r--drivers/net/Makefile1
-rwxr-xr-xdrivers/net/xilinx_emaclite.c1041
3 files changed, 1048 insertions, 0 deletions
diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 29935a9fb1a2..bbe77a065845 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -1926,6 +1926,12 @@ config ATL2
1926 To compile this driver as a module, choose M here. The module 1926 To compile this driver as a module, choose M here. The module
1927 will be called atl2. 1927 will be called atl2.
1928 1928
1929config XILINX_EMACLITE
1930 tristate "Xilinx 10/100 Ethernet Lite support"
1931 depends on PPC32 || MICROBLAZE
1932 help
1933 This driver supports the 10/100 Ethernet Lite from Xilinx.
1934
1929source "drivers/net/fs_enet/Kconfig" 1935source "drivers/net/fs_enet/Kconfig"
1930 1936
1931endif # NET_ETHERNET 1937endif # NET_ETHERNET
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index ead8cab3cfe1..99ae6d7fe6a9 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -142,6 +142,7 @@ obj-$(CONFIG_TSI108_ETH) += tsi108_eth.o
142obj-$(CONFIG_MV643XX_ETH) += mv643xx_eth.o 142obj-$(CONFIG_MV643XX_ETH) += mv643xx_eth.o
143ll_temac-objs := ll_temac_main.o ll_temac_mdio.o 143ll_temac-objs := ll_temac_main.o ll_temac_mdio.o
144obj-$(CONFIG_XILINX_LL_TEMAC) += ll_temac.o 144obj-$(CONFIG_XILINX_LL_TEMAC) += ll_temac.o
145obj-$(CONFIG_XILINX_EMACLITE) += xilinx_emaclite.o
145obj-$(CONFIG_QLA3XXX) += qla3xxx.o 146obj-$(CONFIG_QLA3XXX) += qla3xxx.o
146obj-$(CONFIG_QLGE) += qlge/ 147obj-$(CONFIG_QLGE) += qlge/
147 148
diff --git a/drivers/net/xilinx_emaclite.c b/drivers/net/xilinx_emaclite.c
new file mode 100755
index 000000000000..7e05b40ae36b
--- /dev/null
+++ b/drivers/net/xilinx_emaclite.c
@@ -0,0 +1,1041 @@
1/*
2 * Xilinx EmacLite Linux driver for the Xilinx Ethernet MAC Lite device.
3 *
4 * This is a new flat driver which is based on the original emac_lite
5 * driver from John Williams <john.williams@petalogix.com>.
6 *
7 * 2007-2009 (c) Xilinx, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 */
14
15#include <linux/module.h>
16#include <linux/uaccess.h>
17#include <linux/init.h>
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
20#include <linux/skbuff.h>
21#include <linux/io.h>
22
23#include <linux/of_device.h>
24#include <linux/of_platform.h>
25
26#define DRIVER_NAME "xilinx_emaclite"
27
28/* Register offsets for the EmacLite Core */
29#define XEL_TXBUFF_OFFSET 0x0 /* Transmit Buffer */
30#define XEL_GIER_OFFSET 0x07F8 /* GIE Register */
31#define XEL_TSR_OFFSET 0x07FC /* Tx status */
32#define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
33
34#define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
35#define XEL_RPLR_OFFSET 0x100C /* Rx packet length */
36#define XEL_RSR_OFFSET 0x17FC /* Rx status */
37
38#define XEL_BUFFER_OFFSET 0x0800 /* Next Tx/Rx buffer's offset */
39
40/* Global Interrupt Enable Register (GIER) Bit Masks */
41#define XEL_GIER_GIE_MASK 0x80000000 /* Global Enable */
42
43/* Transmit Status Register (TSR) Bit Masks */
44#define XEL_TSR_XMIT_BUSY_MASK 0x00000001 /* Tx complete */
45#define XEL_TSR_PROGRAM_MASK 0x00000002 /* Program the MAC address */
46#define XEL_TSR_XMIT_IE_MASK 0x00000008 /* Tx interrupt enable bit */
47#define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000 /* Buffer is active, SW bit
48 * only. This is not documented
49 * in the HW spec */
50
51/* Define for programming the MAC address into the EmacLite */
52#define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
53
54/* Receive Status Register (RSR) */
55#define XEL_RSR_RECV_DONE_MASK 0x00000001 /* Rx complete */
56#define XEL_RSR_RECV_IE_MASK 0x00000008 /* Rx interrupt enable bit */
57
58/* Transmit Packet Length Register (TPLR) */
59#define XEL_TPLR_LENGTH_MASK 0x0000FFFF /* Tx packet length */
60
61/* Receive Packet Length Register (RPLR) */
62#define XEL_RPLR_LENGTH_MASK 0x0000FFFF /* Rx packet length */
63
64#define XEL_HEADER_OFFSET 12 /* Offset to length field */
65#define XEL_HEADER_SHIFT 16 /* Shift value for length */
66
67/* General Ethernet Definitions */
68#define XEL_ARP_PACKET_SIZE 28 /* Max ARP packet size */
69#define XEL_HEADER_IP_LENGTH_OFFSET 16 /* IP Length Offset */
70
71
72
73#define TX_TIMEOUT (60*HZ) /* Tx timeout is 60 seconds. */
74#define ALIGNMENT 4
75
76/* BUFFER_ALIGN(adr) calculates the number of bytes to the next alignment. */
77#define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32) adr)) % ALIGNMENT)
78
79/**
80 * struct net_local - Our private per device data
81 * @ndev: instance of the network device
82 * @tx_ping_pong: indicates whether Tx Pong buffer is configured in HW
83 * @rx_ping_pong: indicates whether Rx Pong buffer is configured in HW
84 * @next_tx_buf_to_use: next Tx buffer to write to
85 * @next_rx_buf_to_use: next Rx buffer to read from
86 * @base_addr: base address of the Emaclite device
87 * @reset_lock: lock used for synchronization
88 * @deferred_skb: holds an skb (for transmission at a later time) when the
89 * Tx buffer is not free
90 */
91struct net_local {
92
93 struct net_device *ndev;
94
95 bool tx_ping_pong;
96 bool rx_ping_pong;
97 u32 next_tx_buf_to_use;
98 u32 next_rx_buf_to_use;
99 void __iomem *base_addr;
100
101 spinlock_t reset_lock;
102 struct sk_buff *deferred_skb;
103};
104
105
106/*************************/
107/* EmacLite driver calls */
108/*************************/
109
110/**
111 * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
112 * @drvdata: Pointer to the Emaclite device private data
113 *
114 * This function enables the Tx and Rx interrupts for the Emaclite device along
115 * with the Global Interrupt Enable.
116 */
117static void xemaclite_enable_interrupts(struct net_local *drvdata)
118{
119 u32 reg_data;
120
121 /* Enable the Tx interrupts for the first Buffer */
122 reg_data = in_be32(drvdata->base_addr + XEL_TSR_OFFSET);
123 out_be32(drvdata->base_addr + XEL_TSR_OFFSET,
124 reg_data | XEL_TSR_XMIT_IE_MASK);
125
126 /* Enable the Tx interrupts for the second Buffer if
127 * configured in HW */
128 if (drvdata->tx_ping_pong != 0) {
129 reg_data = in_be32(drvdata->base_addr +
130 XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
131 out_be32(drvdata->base_addr + XEL_BUFFER_OFFSET +
132 XEL_TSR_OFFSET,
133 reg_data | XEL_TSR_XMIT_IE_MASK);
134 }
135
136 /* Enable the Rx interrupts for the first buffer */
137 reg_data = in_be32(drvdata->base_addr + XEL_RSR_OFFSET);
138 out_be32(drvdata->base_addr + XEL_RSR_OFFSET,
139 reg_data | XEL_RSR_RECV_IE_MASK);
140
141 /* Enable the Rx interrupts for the second Buffer if
142 * configured in HW */
143 if (drvdata->rx_ping_pong != 0) {
144 reg_data = in_be32(drvdata->base_addr + XEL_BUFFER_OFFSET +
145 XEL_RSR_OFFSET);
146 out_be32(drvdata->base_addr + XEL_BUFFER_OFFSET +
147 XEL_RSR_OFFSET,
148 reg_data | XEL_RSR_RECV_IE_MASK);
149 }
150
151 /* Enable the Global Interrupt Enable */
152 out_be32(drvdata->base_addr + XEL_GIER_OFFSET, XEL_GIER_GIE_MASK);
153}
154
155/**
156 * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
157 * @drvdata: Pointer to the Emaclite device private data
158 *
159 * This function disables the Tx and Rx interrupts for the Emaclite device,
160 * along with the Global Interrupt Enable.
161 */
162static void xemaclite_disable_interrupts(struct net_local *drvdata)
163{
164 u32 reg_data;
165
166 /* Disable the Global Interrupt Enable */
167 out_be32(drvdata->base_addr + XEL_GIER_OFFSET, XEL_GIER_GIE_MASK);
168
169 /* Disable the Tx interrupts for the first buffer */
170 reg_data = in_be32(drvdata->base_addr + XEL_TSR_OFFSET);
171 out_be32(drvdata->base_addr + XEL_TSR_OFFSET,
172 reg_data & (~XEL_TSR_XMIT_IE_MASK));
173
174 /* Disable the Tx interrupts for the second Buffer
175 * if configured in HW */
176 if (drvdata->tx_ping_pong != 0) {
177 reg_data = in_be32(drvdata->base_addr + XEL_BUFFER_OFFSET +
178 XEL_TSR_OFFSET);
179 out_be32(drvdata->base_addr + XEL_BUFFER_OFFSET +
180 XEL_TSR_OFFSET,
181 reg_data & (~XEL_TSR_XMIT_IE_MASK));
182 }
183
184 /* Disable the Rx interrupts for the first buffer */
185 reg_data = in_be32(drvdata->base_addr + XEL_RSR_OFFSET);
186 out_be32(drvdata->base_addr + XEL_RSR_OFFSET,
187 reg_data & (~XEL_RSR_RECV_IE_MASK));
188
189 /* Disable the Rx interrupts for the second buffer
190 * if configured in HW */
191 if (drvdata->rx_ping_pong != 0) {
192
193 reg_data = in_be32(drvdata->base_addr + XEL_BUFFER_OFFSET +
194 XEL_RSR_OFFSET);
195 out_be32(drvdata->base_addr + XEL_BUFFER_OFFSET +
196 XEL_RSR_OFFSET,
197 reg_data & (~XEL_RSR_RECV_IE_MASK));
198 }
199}
200
201/**
202 * xemaclite_aligned_write - Write from 16-bit aligned to 32-bit aligned address
203 * @src_ptr: Void pointer to the 16-bit aligned source address
204 * @dest_ptr: Pointer to the 32-bit aligned destination address
205 * @length: Number bytes to write from source to destination
206 *
207 * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
208 * address in the EmacLite device.
209 */
210static void xemaclite_aligned_write(void *src_ptr, u32 *dest_ptr,
211 unsigned length)
212{
213 u32 align_buffer;
214 u32 *to_u32_ptr;
215 u16 *from_u16_ptr, *to_u16_ptr;
216
217 to_u32_ptr = dest_ptr;
218 from_u16_ptr = (u16 *) src_ptr;
219 align_buffer = 0;
220
221 for (; length > 3; length -= 4) {
222 to_u16_ptr = (u16 *) ((void *) &align_buffer);
223 *to_u16_ptr++ = *from_u16_ptr++;
224 *to_u16_ptr++ = *from_u16_ptr++;
225
226 /* Output a word */
227 *to_u32_ptr++ = align_buffer;
228 }
229 if (length) {
230 u8 *from_u8_ptr, *to_u8_ptr;
231
232 /* Set up to output the remaining data */
233 align_buffer = 0;
234 to_u8_ptr = (u8 *) &align_buffer;
235 from_u8_ptr = (u8 *) from_u16_ptr;
236
237 /* Output the remaining data */
238 for (; length > 0; length--)
239 *to_u8_ptr++ = *from_u8_ptr++;
240
241 *to_u32_ptr = align_buffer;
242 }
243}
244
245/**
246 * xemaclite_aligned_read - Read from 32-bit aligned to 16-bit aligned buffer
247 * @src_ptr: Pointer to the 32-bit aligned source address
248 * @dest_ptr: Pointer to the 16-bit aligned destination address
249 * @length: Number bytes to read from source to destination
250 *
251 * This function reads data from a 32-bit aligned address in the EmacLite device
252 * to a 16-bit aligned buffer.
253 */
254static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
255 unsigned length)
256{
257 u16 *to_u16_ptr, *from_u16_ptr;
258 u32 *from_u32_ptr;
259 u32 align_buffer;
260
261 from_u32_ptr = src_ptr;
262 to_u16_ptr = (u16 *) dest_ptr;
263
264 for (; length > 3; length -= 4) {
265 /* Copy each word into the temporary buffer */
266 align_buffer = *from_u32_ptr++;
267 from_u16_ptr = (u16 *)&align_buffer;
268
269 /* Read data from source */
270 *to_u16_ptr++ = *from_u16_ptr++;
271 *to_u16_ptr++ = *from_u16_ptr++;
272 }
273
274 if (length) {
275 u8 *to_u8_ptr, *from_u8_ptr;
276
277 /* Set up to read the remaining data */
278 to_u8_ptr = (u8 *) to_u16_ptr;
279 align_buffer = *from_u32_ptr++;
280 from_u8_ptr = (u8 *) &align_buffer;
281
282 /* Read the remaining data */
283 for (; length > 0; length--)
284 *to_u8_ptr = *from_u8_ptr;
285 }
286}
287
288/**
289 * xemaclite_send_data - Send an Ethernet frame
290 * @drvdata: Pointer to the Emaclite device private data
291 * @data: Pointer to the data to be sent
292 * @byte_count: Total frame size, including header
293 *
294 * This function checks if the Tx buffer of the Emaclite device is free to send
295 * data. If so, it fills the Tx buffer with data for transmission. Otherwise, it
296 * returns an error.
297 *
298 * Return: 0 upon success or -1 if the buffer(s) are full.
299 *
300 * Note: The maximum Tx packet size can not be more than Ethernet header
301 * (14 Bytes) + Maximum MTU (1500 bytes). This is excluding FCS.
302 */
303static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
304 unsigned int byte_count)
305{
306 u32 reg_data;
307 void __iomem *addr;
308
309 /* Determine the expected Tx buffer address */
310 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
311
312 /* If the length is too large, truncate it */
313 if (byte_count > ETH_FRAME_LEN)
314 byte_count = ETH_FRAME_LEN;
315
316 /* Check if the expected buffer is available */
317 reg_data = in_be32(addr + XEL_TSR_OFFSET);
318 if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
319 XEL_TSR_XMIT_ACTIVE_MASK)) == 0) {
320
321 /* Switch to next buffer if configured */
322 if (drvdata->tx_ping_pong != 0)
323 drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET;
324 } else if (drvdata->tx_ping_pong != 0) {
325 /* If the expected buffer is full, try the other buffer,
326 * if it is configured in HW */
327
328 addr = (void __iomem __force *)((u32 __force)addr ^
329 XEL_BUFFER_OFFSET);
330 reg_data = in_be32(addr + XEL_TSR_OFFSET);
331
332 if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
333 XEL_TSR_XMIT_ACTIVE_MASK)) != 0)
334 return -1; /* Buffers were full, return failure */
335 } else
336 return -1; /* Buffer was full, return failure */
337
338 /* Write the frame to the buffer */
339 xemaclite_aligned_write(data, (u32 __force *) addr, byte_count);
340
341 out_be32(addr + XEL_TPLR_OFFSET, (byte_count & XEL_TPLR_LENGTH_MASK));
342
343 /* Update the Tx Status Register to indicate that there is a
344 * frame to send. Set the XEL_TSR_XMIT_ACTIVE_MASK flag which
345 * is used by the interrupt handler to check whether a frame
346 * has been transmitted */
347 reg_data = in_be32(addr + XEL_TSR_OFFSET);
348 reg_data |= (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_XMIT_ACTIVE_MASK);
349 out_be32(addr + XEL_TSR_OFFSET, reg_data);
350
351 return 0;
352}
353
354/**
355 * xemaclite_recv_data - Receive a frame
356 * @drvdata: Pointer to the Emaclite device private data
357 * @data: Address where the data is to be received
358 *
359 * This function is intended to be called from the interrupt context or
360 * with a wrapper which waits for the receive frame to be available.
361 *
362 * Return: Total number of bytes received
363 */
364static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data)
365{
366 void __iomem *addr;
367 u16 length, proto_type;
368 u32 reg_data;
369
370 /* Determine the expected buffer address */
371 addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use);
372
373 /* Verify which buffer has valid data */
374 reg_data = in_be32(addr + XEL_RSR_OFFSET);
375
376 if ((reg_data & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
377 if (drvdata->rx_ping_pong != 0)
378 drvdata->next_rx_buf_to_use ^= XEL_BUFFER_OFFSET;
379 } else {
380 /* The instance is out of sync, try other buffer if other
381 * buffer is configured, return 0 otherwise. If the instance is
382 * out of sync, do not update the 'next_rx_buf_to_use' since it
383 * will correct on subsequent calls */
384 if (drvdata->rx_ping_pong != 0)
385 addr = (void __iomem __force *)((u32 __force)addr ^
386 XEL_BUFFER_OFFSET);
387 else
388 return 0; /* No data was available */
389
390 /* Verify that buffer has valid data */
391 reg_data = in_be32(addr + XEL_RSR_OFFSET);
392 if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
393 XEL_RSR_RECV_DONE_MASK)
394 return 0; /* No data was available */
395 }
396
397 /* Get the protocol type of the ethernet frame that arrived */
398 proto_type = ((in_be32(addr + XEL_HEADER_OFFSET +
399 XEL_RXBUFF_OFFSET) >> XEL_HEADER_SHIFT) &
400 XEL_RPLR_LENGTH_MASK);
401
402 /* Check if received ethernet frame is a raw ethernet frame
403 * or an IP packet or an ARP packet */
404 if (proto_type > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
405
406 if (proto_type == ETH_P_IP) {
407 length = ((in_be32(addr +
408 XEL_HEADER_IP_LENGTH_OFFSET +
409 XEL_RXBUFF_OFFSET) >>
410 XEL_HEADER_SHIFT) &
411 XEL_RPLR_LENGTH_MASK);
412 length += ETH_HLEN + ETH_FCS_LEN;
413
414 } else if (proto_type == ETH_P_ARP)
415 length = XEL_ARP_PACKET_SIZE + ETH_HLEN + ETH_FCS_LEN;
416 else
417 /* Field contains type other than IP or ARP, use max
418 * frame size and let user parse it */
419 length = ETH_FRAME_LEN + ETH_FCS_LEN;
420 } else
421 /* Use the length in the frame, plus the header and trailer */
422 length = proto_type + ETH_HLEN + ETH_FCS_LEN;
423
424 /* Read from the EmacLite device */
425 xemaclite_aligned_read((u32 __force *) (addr + XEL_RXBUFF_OFFSET),
426 data, length);
427
428 /* Acknowledge the frame */
429 reg_data = in_be32(addr + XEL_RSR_OFFSET);
430 reg_data &= ~XEL_RSR_RECV_DONE_MASK;
431 out_be32(addr + XEL_RSR_OFFSET, reg_data);
432
433 return length;
434}
435
436/**
437 * xemaclite_set_mac_address - Set the MAC address for this device
438 * @drvdata: Pointer to the Emaclite device private data
439 * @address_ptr:Pointer to the MAC address (MAC address is a 48-bit value)
440 *
441 * Tx must be idle and Rx should be idle for deterministic results.
442 * It is recommended that this function should be called after the
443 * initialization and before transmission of any packets from the device.
444 * The MAC address can be programmed using any of the two transmit
445 * buffers (if configured).
446 */
447static void xemaclite_set_mac_address(struct net_local *drvdata,
448 u8 *address_ptr)
449{
450 void __iomem *addr;
451 u32 reg_data;
452
453 /* Determine the expected Tx buffer address */
454 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
455
456 xemaclite_aligned_write(address_ptr, (u32 __force *) addr, ETH_ALEN);
457
458 out_be32(addr + XEL_TPLR_OFFSET, ETH_ALEN);
459
460 /* Update the MAC address in the EmacLite */
461 reg_data = in_be32(addr + XEL_TSR_OFFSET);
462 out_be32(addr + XEL_TSR_OFFSET, reg_data | XEL_TSR_PROG_MAC_ADDR);
463
464 /* Wait for EmacLite to finish with the MAC address update */
465 while ((in_be32(addr + XEL_TSR_OFFSET) &
466 XEL_TSR_PROG_MAC_ADDR) != 0)
467 ;
468}
469
470/**
471 * xemaclite_tx_timeout - Callback for Tx Timeout
472 * @dev: Pointer to the network device
473 *
474 * This function is called when Tx time out occurs for Emaclite device.
475 */
476static void xemaclite_tx_timeout(struct net_device *dev)
477{
478 struct net_local *lp = (struct net_local *) netdev_priv(dev);
479 unsigned long flags;
480
481 dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
482 TX_TIMEOUT * 1000UL / HZ);
483
484 dev->stats.tx_errors++;
485
486 /* Reset the device */
487 spin_lock_irqsave(&lp->reset_lock, flags);
488
489 /* Shouldn't really be necessary, but shouldn't hurt */
490 netif_stop_queue(dev);
491
492 xemaclite_disable_interrupts(lp);
493 xemaclite_enable_interrupts(lp);
494
495 if (lp->deferred_skb) {
496 dev_kfree_skb(lp->deferred_skb);
497 lp->deferred_skb = NULL;
498 dev->stats.tx_errors++;
499 }
500
501 /* To exclude tx timeout */
502 dev->trans_start = 0xffffffff - TX_TIMEOUT - TX_TIMEOUT;
503
504 /* We're all ready to go. Start the queue */
505 netif_wake_queue(dev);
506 spin_unlock_irqrestore(&lp->reset_lock, flags);
507}
508
509/**********************/
510/* Interrupt Handlers */
511/**********************/
512
513/**
514 * xemaclite_tx_handler - Interrupt handler for frames sent
515 * @dev: Pointer to the network device
516 *
517 * This function updates the number of packets transmitted and handles the
518 * deferred skb, if there is one.
519 */
520static void xemaclite_tx_handler(struct net_device *dev)
521{
522 struct net_local *lp = (struct net_local *) netdev_priv(dev);
523
524 dev->stats.tx_packets++;
525 if (lp->deferred_skb) {
526 if (xemaclite_send_data(lp,
527 (u8 *) lp->deferred_skb->data,
528 lp->deferred_skb->len) != 0)
529 return;
530 else {
531 dev->stats.tx_bytes += lp->deferred_skb->len;
532 dev_kfree_skb_irq(lp->deferred_skb);
533 lp->deferred_skb = NULL;
534 dev->trans_start = jiffies;
535 netif_wake_queue(dev);
536 }
537 }
538}
539
540/**
541 * xemaclite_rx_handler- Interrupt handler for frames received
542 * @dev: Pointer to the network device
543 *
544 * This function allocates memory for a socket buffer, fills it with data
545 * received and hands it over to the TCP/IP stack.
546 */
547static void xemaclite_rx_handler(struct net_device *dev)
548{
549 struct net_local *lp = (struct net_local *) netdev_priv(dev);
550 struct sk_buff *skb;
551 unsigned int align;
552 u32 len;
553
554 len = ETH_FRAME_LEN + ETH_FCS_LEN;
555 skb = dev_alloc_skb(len + ALIGNMENT);
556 if (!skb) {
557 /* Couldn't get memory. */
558 dev->stats.rx_dropped++;
559 dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n");
560 return;
561 }
562
563 /*
564 * A new skb should have the data halfword aligned, but this code is
565 * here just in case that isn't true. Calculate how many
566 * bytes we should reserve to get the data to start on a word
567 * boundary */
568 align = BUFFER_ALIGN(skb->data);
569 if (align)
570 skb_reserve(skb, align);
571
572 skb_reserve(skb, 2);
573
574 len = xemaclite_recv_data(lp, (u8 *) skb->data);
575
576 if (!len) {
577 dev->stats.rx_errors++;
578 dev_kfree_skb_irq(skb);
579 return;
580 }
581
582 skb_put(skb, len); /* Tell the skb how much data we got */
583 skb->dev = dev; /* Fill out required meta-data */
584
585 skb->protocol = eth_type_trans(skb, dev);
586 skb->ip_summed = CHECKSUM_NONE;
587
588 dev->stats.rx_packets++;
589 dev->stats.rx_bytes += len;
590 dev->last_rx = jiffies;
591
592 netif_rx(skb); /* Send the packet upstream */
593}
594
595/**
596 * xemaclite_interrupt - Interrupt handler for this driver
597 * @irq: Irq of the Emaclite device
598 * @dev_id: Void pointer to the network device instance used as callback
599 * reference
600 *
601 * This function handles the Tx and Rx interrupts of the EmacLite device.
602 */
603static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
604{
605 bool tx_complete = 0;
606 struct net_device *dev = dev_id;
607 struct net_local *lp = (struct net_local *) netdev_priv(dev);
608 void __iomem *base_addr = lp->base_addr;
609 u32 tx_status;
610
611 /* Check if there is Rx Data available */
612 if ((in_be32(base_addr + XEL_RSR_OFFSET) & XEL_RSR_RECV_DONE_MASK) ||
613 (in_be32(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
614 & XEL_RSR_RECV_DONE_MASK))
615
616 xemaclite_rx_handler(dev);
617
618 /* Check if the Transmission for the first buffer is completed */
619 tx_status = in_be32(base_addr + XEL_TSR_OFFSET);
620 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
621 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
622
623 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
624 out_be32(base_addr + XEL_TSR_OFFSET, tx_status);
625
626 tx_complete = 1;
627 }
628
629 /* Check if the Transmission for the second buffer is completed */
630 tx_status = in_be32(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
631 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
632 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
633
634 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
635 out_be32(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET,
636 tx_status);
637
638 tx_complete = 1;
639 }
640
641 /* If there was a Tx interrupt, call the Tx Handler */
642 if (tx_complete != 0)
643 xemaclite_tx_handler(dev);
644
645 return IRQ_HANDLED;
646}
647
648/**
649 * xemaclite_open - Open the network device
650 * @dev: Pointer to the network device
651 *
652 * This function sets the MAC address, requests an IRQ and enables interrupts
653 * for the Emaclite device and starts the Tx queue.
654 */
655static int xemaclite_open(struct net_device *dev)
656{
657 struct net_local *lp = (struct net_local *) netdev_priv(dev);
658 int retval;
659
660 /* Just to be safe, stop the device first */
661 xemaclite_disable_interrupts(lp);
662
663 /* Set the MAC address each time opened */
664 xemaclite_set_mac_address(lp, dev->dev_addr);
665
666 /* Grab the IRQ */
667 retval = request_irq(dev->irq, &xemaclite_interrupt, 0, dev->name, dev);
668 if (retval) {
669 dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
670 dev->irq);
671 return retval;
672 }
673
674 /* Enable Interrupts */
675 xemaclite_enable_interrupts(lp);
676
677 /* We're ready to go */
678 netif_start_queue(dev);
679
680 return 0;
681}
682
683/**
684 * xemaclite_close - Close the network device
685 * @dev: Pointer to the network device
686 *
687 * This function stops the Tx queue, disables interrupts and frees the IRQ for
688 * the Emaclite device.
689 */
690static int xemaclite_close(struct net_device *dev)
691{
692 struct net_local *lp = (struct net_local *) netdev_priv(dev);
693
694 netif_stop_queue(dev);
695 xemaclite_disable_interrupts(lp);
696 free_irq(dev->irq, dev);
697
698 return 0;
699}
700
701/**
702 * xemaclite_get_stats - Get the stats for the net_device
703 * @dev: Pointer to the network device
704 *
705 * This function returns the address of the 'net_device_stats' structure for the
706 * given network device. This structure holds usage statistics for the network
707 * device.
708 *
709 * Return: Pointer to the net_device_stats structure.
710 */
711static struct net_device_stats *xemaclite_get_stats(struct net_device *dev)
712{
713 return &dev->stats;
714}
715
716/**
717 * xemaclite_send - Transmit a frame
718 * @orig_skb: Pointer to the socket buffer to be transmitted
719 * @dev: Pointer to the network device
720 *
721 * This function checks if the Tx buffer of the Emaclite device is free to send
722 * data. If so, it fills the Tx buffer with data from socket buffer data,
723 * updates the stats and frees the socket buffer. The Tx completion is signaled
724 * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
725 * deferred and the Tx queue is stopped so that the deferred socket buffer can
726 * be transmitted when the Emaclite device is free to transmit data.
727 *
728 * Return: 0, always.
729 */
730static int xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
731{
732 struct net_local *lp = (struct net_local *) netdev_priv(dev);
733 struct sk_buff *new_skb;
734 unsigned int len;
735 unsigned long flags;
736
737 len = orig_skb->len;
738
739 new_skb = orig_skb;
740
741 spin_lock_irqsave(&lp->reset_lock, flags);
742 if (xemaclite_send_data(lp, (u8 *) new_skb->data, len) != 0) {
743 /* If the Emaclite Tx buffer is busy, stop the Tx queue and
744 * defer the skb for transmission at a later point when the
745 * current transmission is complete */
746 netif_stop_queue(dev);
747 lp->deferred_skb = new_skb;
748 spin_unlock_irqrestore(&lp->reset_lock, flags);
749 return 0;
750 }
751 spin_unlock_irqrestore(&lp->reset_lock, flags);
752
753 dev->stats.tx_bytes += len;
754 dev_kfree_skb(new_skb);
755 dev->trans_start = jiffies;
756
757 return 0;
758}
759
760/**
761 * xemaclite_ioctl - Perform IO Control operations on the network device
762 * @dev: Pointer to the network device
763 * @rq: Pointer to the interface request structure
764 * @cmd: IOCTL command
765 *
766 * The only IOCTL operation supported by this function is setting the MAC
767 * address. An error is reported if any other operations are requested.
768 *
769 * Return: 0 to indicate success, or a negative error for failure.
770 */
771static int xemaclite_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
772{
773 struct net_local *lp = (struct net_local *) netdev_priv(dev);
774 struct hw_addr_data *hw_addr = (struct hw_addr_data *) &rq->ifr_hwaddr;
775
776 switch (cmd) {
777 case SIOCETHTOOL:
778 return -EIO;
779
780 case SIOCSIFHWADDR:
781 dev_err(&lp->ndev->dev, "SIOCSIFHWADDR\n");
782
783 /* Copy MAC address in from user space */
784 copy_from_user((void __force *) dev->dev_addr,
785 (void __user __force *) hw_addr,
786 IFHWADDRLEN);
787 xemaclite_set_mac_address(lp, dev->dev_addr);
788 break;
789 default:
790 return -EOPNOTSUPP;
791 }
792
793 return 0;
794}
795
796/**
797 * xemaclite_remove_ndev - Free the network device
798 * @ndev: Pointer to the network device to be freed
799 *
800 * This function un maps the IO region of the Emaclite device and frees the net
801 * device.
802 */
803static void xemaclite_remove_ndev(struct net_device *ndev)
804{
805 if (ndev) {
806 struct net_local *lp = (struct net_local *) netdev_priv(ndev);
807
808 if (lp->base_addr)
809 iounmap((void __iomem __force *) (lp->base_addr));
810 free_netdev(ndev);
811 }
812}
813
814/**
815 * get_bool - Get a parameter from the OF device
816 * @ofdev: Pointer to OF device structure
817 * @s: Property to be retrieved
818 *
819 * This function looks for a property in the device node and returns the value
820 * of the property if its found or 0 if the property is not found.
821 *
822 * Return: Value of the parameter if the parameter is found, or 0 otherwise
823 */
824static bool get_bool(struct of_device *ofdev, const char *s)
825{
826 u32 *p = (u32 *)of_get_property(ofdev->node, s, NULL);
827
828 if (p) {
829 return (bool)*p;
830 } else {
831 dev_warn(&ofdev->dev, "Parameter %s not found,"
832 "defaulting to false\n", s);
833 return 0;
834 }
835}
836
837static struct net_device_ops xemaclite_netdev_ops;
838
839/**
840 * xemaclite_of_probe - Probe method for the Emaclite device.
841 * @ofdev: Pointer to OF device structure
842 * @match: Pointer to the structure used for matching a device
843 *
844 * This function probes for the Emaclite device in the device tree.
845 * It initializes the driver data structure and the hardware, sets the MAC
846 * address and registers the network device.
847 *
848 * Return: 0, if the driver is bound to the Emaclite device, or
849 * a negative error if there is failure.
850 */
851static int __devinit xemaclite_of_probe(struct of_device *ofdev,
852 const struct of_device_id *match)
853{
854 struct resource r_irq; /* Interrupt resources */
855 struct resource r_mem; /* IO mem resources */
856 struct net_device *ndev = NULL;
857 struct net_local *lp = NULL;
858 struct device *dev = &ofdev->dev;
859 const void *mac_address;
860
861 int rc = 0;
862
863 dev_info(dev, "Device Tree Probing\n");
864
865 /* Get iospace for the device */
866 rc = of_address_to_resource(ofdev->node, 0, &r_mem);
867 if (rc) {
868 dev_err(dev, "invalid address\n");
869 return rc;
870 }
871
872 /* Get IRQ for the device */
873 rc = of_irq_to_resource(ofdev->node, 0, &r_irq);
874 if (rc == NO_IRQ) {
875 dev_err(dev, "no IRQ found\n");
876 return rc;
877 }
878
879 /* Create an ethernet device instance */
880 ndev = alloc_etherdev(sizeof(struct net_local));
881 if (!ndev) {
882 dev_err(dev, "Could not allocate network device\n");
883 return -ENOMEM;
884 }
885
886 dev_set_drvdata(dev, ndev);
887
888 ndev->irq = r_irq.start;
889 ndev->mem_start = r_mem.start;
890 ndev->mem_end = r_mem.end;
891
892 lp = netdev_priv(ndev);
893 lp->ndev = ndev;
894
895 if (!request_mem_region(ndev->mem_start,
896 ndev->mem_end - ndev->mem_start + 1,
897 DRIVER_NAME)) {
898 dev_err(dev, "Couldn't lock memory region at %p\n",
899 (void *)ndev->mem_start);
900 rc = -EBUSY;
901 goto error2;
902 }
903
904 /* Get the virtual base address for the device */
905 lp->base_addr = ioremap(r_mem.start, r_mem.end - r_mem.start + 1);
906 if (NULL == lp->base_addr) {
907 dev_err(dev, "EmacLite: Could not allocate iomem\n");
908 rc = -EIO;
909 goto error1;
910 }
911
912 spin_lock_init(&lp->reset_lock);
913 lp->next_tx_buf_to_use = 0x0;
914 lp->next_rx_buf_to_use = 0x0;
915 lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
916 lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
917 mac_address = of_get_mac_address(ofdev->node);
918
919 if (mac_address)
920 /* Set the MAC address. */
921 memcpy(ndev->dev_addr, mac_address, 6);
922 else
923 dev_warn(dev, "No MAC address found\n");
924
925 /* Clear the Tx CSR's in case this is a restart */
926 out_be32(lp->base_addr + XEL_TSR_OFFSET, 0);
927 out_be32(lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET, 0);
928
929 /* Set the MAC address in the EmacLite device */
930 xemaclite_set_mac_address(lp, ndev->dev_addr);
931
932 dev_info(dev,
933 "MAC address is now %2x:%2x:%2x:%2x:%2x:%2x\n",
934 ndev->dev_addr[0], ndev->dev_addr[1],
935 ndev->dev_addr[2], ndev->dev_addr[3],
936 ndev->dev_addr[4], ndev->dev_addr[5]);
937
938 ndev->netdev_ops = &xemaclite_netdev_ops;
939 ndev->flags &= ~IFF_MULTICAST;
940 ndev->watchdog_timeo = TX_TIMEOUT;
941
942 /* Finally, register the device */
943 rc = register_netdev(ndev);
944 if (rc) {
945 dev_err(dev,
946 "Cannot register network device, aborting\n");
947 goto error1;
948 }
949
950 dev_info(dev,
951 "Xilinx EmacLite at 0x%08X mapped to 0x%08X, irq=%d\n",
952 (unsigned int __force)ndev->mem_start,
953 (unsigned int __force)lp->base_addr, ndev->irq);
954 return 0;
955
956error1:
957 release_mem_region(ndev->mem_start, r_mem.end - r_mem.start + 1);
958
959error2:
960 xemaclite_remove_ndev(ndev);
961 return rc;
962}
963
964/**
965 * xemaclite_of_remove - Unbind the driver from the Emaclite device.
966 * @of_dev: Pointer to OF device structure
967 *
968 * This function is called if a device is physically removed from the system or
969 * if the driver module is being unloaded. It frees any resources allocated to
970 * the device.
971 *
972 * Return: 0, always.
973 */
974static int __devexit xemaclite_of_remove(struct of_device *of_dev)
975{
976 struct device *dev = &of_dev->dev;
977 struct net_device *ndev = dev_get_drvdata(dev);
978
979 unregister_netdev(ndev);
980
981 release_mem_region(ndev->mem_start, ndev->mem_end-ndev->mem_start + 1);
982
983 xemaclite_remove_ndev(ndev);
984
985 dev_set_drvdata(dev, NULL);
986
987 return 0;
988}
989
990static struct net_device_ops xemaclite_netdev_ops = {
991 .ndo_open = xemaclite_open,
992 .ndo_stop = xemaclite_close,
993 .ndo_start_xmit = xemaclite_send,
994 .ndo_do_ioctl = xemaclite_ioctl,
995 .ndo_tx_timeout = xemaclite_tx_timeout,
996 .ndo_get_stats = xemaclite_get_stats,
997};
998
999/* Match table for OF platform binding */
1000static struct of_device_id xemaclite_of_match[] __devinitdata = {
1001 { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
1002 { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
1003 { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
1004 { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
1005 { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
1006 { /* end of list */ },
1007};
1008MODULE_DEVICE_TABLE(of, xemaclite_of_match);
1009
1010static struct of_platform_driver xemaclite_of_driver = {
1011 .name = DRIVER_NAME,
1012 .match_table = xemaclite_of_match,
1013 .probe = xemaclite_of_probe,
1014 .remove = __devexit_p(xemaclite_of_remove),
1015};
1016
1017/**
1018 * xgpiopss_init - Initial driver registration call
1019 *
1020 * Return: 0 upon success, or a negative error upon failure.
1021 */
1022static int __init xemaclite_init(void)
1023{
1024 /* No kernel boot options used, we just need to register the driver */
1025 return of_register_platform_driver(&xemaclite_of_driver);
1026}
1027
1028/**
1029 * xemaclite_cleanup - Driver un-registration call
1030 */
1031static void __exit xemaclite_cleanup(void)
1032{
1033 of_unregister_platform_driver(&xemaclite_of_driver);
1034}
1035
1036module_init(xemaclite_init);
1037module_exit(xemaclite_cleanup);
1038
1039MODULE_AUTHOR("Xilinx, Inc.");
1040MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1041MODULE_LICENSE("GPL");