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