Staging: et131x: config is already zeroed
[GitHub/mt8127/android_kernel_alcatel_ttab.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 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 */
162 static 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 */
210 static 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 */
254 static 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 */
303 static 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 */
364 static 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 */
447 static 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 */
476 static 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 */
520 static 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 */
547 static 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
591 netif_rx(skb); /* Send the packet upstream */
592 }
593
594 /**
595 * xemaclite_interrupt - Interrupt handler for this driver
596 * @irq: Irq of the Emaclite device
597 * @dev_id: Void pointer to the network device instance used as callback
598 * reference
599 *
600 * This function handles the Tx and Rx interrupts of the EmacLite device.
601 */
602 static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
603 {
604 bool tx_complete = 0;
605 struct net_device *dev = dev_id;
606 struct net_local *lp = (struct net_local *) netdev_priv(dev);
607 void __iomem *base_addr = lp->base_addr;
608 u32 tx_status;
609
610 /* Check if there is Rx Data available */
611 if ((in_be32(base_addr + XEL_RSR_OFFSET) & XEL_RSR_RECV_DONE_MASK) ||
612 (in_be32(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
613 & XEL_RSR_RECV_DONE_MASK))
614
615 xemaclite_rx_handler(dev);
616
617 /* Check if the Transmission for the first buffer is completed */
618 tx_status = in_be32(base_addr + XEL_TSR_OFFSET);
619 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
620 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
621
622 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
623 out_be32(base_addr + XEL_TSR_OFFSET, tx_status);
624
625 tx_complete = 1;
626 }
627
628 /* Check if the Transmission for the second buffer is completed */
629 tx_status = in_be32(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
630 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
631 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
632
633 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
634 out_be32(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET,
635 tx_status);
636
637 tx_complete = 1;
638 }
639
640 /* If there was a Tx interrupt, call the Tx Handler */
641 if (tx_complete != 0)
642 xemaclite_tx_handler(dev);
643
644 return IRQ_HANDLED;
645 }
646
647 /**
648 * xemaclite_open - Open the network device
649 * @dev: Pointer to the network device
650 *
651 * This function sets the MAC address, requests an IRQ and enables interrupts
652 * for the Emaclite device and starts the Tx queue.
653 */
654 static int xemaclite_open(struct net_device *dev)
655 {
656 struct net_local *lp = (struct net_local *) netdev_priv(dev);
657 int retval;
658
659 /* Just to be safe, stop the device first */
660 xemaclite_disable_interrupts(lp);
661
662 /* Set the MAC address each time opened */
663 xemaclite_set_mac_address(lp, dev->dev_addr);
664
665 /* Grab the IRQ */
666 retval = request_irq(dev->irq, &xemaclite_interrupt, 0, dev->name, dev);
667 if (retval) {
668 dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
669 dev->irq);
670 return retval;
671 }
672
673 /* Enable Interrupts */
674 xemaclite_enable_interrupts(lp);
675
676 /* We're ready to go */
677 netif_start_queue(dev);
678
679 return 0;
680 }
681
682 /**
683 * xemaclite_close - Close the network device
684 * @dev: Pointer to the network device
685 *
686 * This function stops the Tx queue, disables interrupts and frees the IRQ for
687 * the Emaclite device.
688 */
689 static int xemaclite_close(struct net_device *dev)
690 {
691 struct net_local *lp = (struct net_local *) netdev_priv(dev);
692
693 netif_stop_queue(dev);
694 xemaclite_disable_interrupts(lp);
695 free_irq(dev->irq, dev);
696
697 return 0;
698 }
699
700 /**
701 * xemaclite_get_stats - Get the stats for the net_device
702 * @dev: Pointer to the network device
703 *
704 * This function returns the address of the 'net_device_stats' structure for the
705 * given network device. This structure holds usage statistics for the network
706 * device.
707 *
708 * Return: Pointer to the net_device_stats structure.
709 */
710 static struct net_device_stats *xemaclite_get_stats(struct net_device *dev)
711 {
712 return &dev->stats;
713 }
714
715 /**
716 * xemaclite_send - Transmit a frame
717 * @orig_skb: Pointer to the socket buffer to be transmitted
718 * @dev: Pointer to the network device
719 *
720 * This function checks if the Tx buffer of the Emaclite device is free to send
721 * data. If so, it fills the Tx buffer with data from socket buffer data,
722 * updates the stats and frees the socket buffer. The Tx completion is signaled
723 * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
724 * deferred and the Tx queue is stopped so that the deferred socket buffer can
725 * be transmitted when the Emaclite device is free to transmit data.
726 *
727 * Return: 0, always.
728 */
729 static int xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
730 {
731 struct net_local *lp = (struct net_local *) netdev_priv(dev);
732 struct sk_buff *new_skb;
733 unsigned int len;
734 unsigned long flags;
735
736 len = orig_skb->len;
737
738 new_skb = orig_skb;
739
740 spin_lock_irqsave(&lp->reset_lock, flags);
741 if (xemaclite_send_data(lp, (u8 *) new_skb->data, len) != 0) {
742 /* If the Emaclite Tx buffer is busy, stop the Tx queue and
743 * defer the skb for transmission at a later point when the
744 * current transmission is complete */
745 netif_stop_queue(dev);
746 lp->deferred_skb = new_skb;
747 spin_unlock_irqrestore(&lp->reset_lock, flags);
748 return 0;
749 }
750 spin_unlock_irqrestore(&lp->reset_lock, flags);
751
752 dev->stats.tx_bytes += len;
753 dev_kfree_skb(new_skb);
754 dev->trans_start = jiffies;
755
756 return 0;
757 }
758
759 /**
760 * xemaclite_ioctl - Perform IO Control operations on the network device
761 * @dev: Pointer to the network device
762 * @rq: Pointer to the interface request structure
763 * @cmd: IOCTL command
764 *
765 * The only IOCTL operation supported by this function is setting the MAC
766 * address. An error is reported if any other operations are requested.
767 *
768 * Return: 0 to indicate success, or a negative error for failure.
769 */
770 static int xemaclite_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
771 {
772 struct net_local *lp = (struct net_local *) netdev_priv(dev);
773 struct hw_addr_data *hw_addr = (struct hw_addr_data *) &rq->ifr_hwaddr;
774
775 switch (cmd) {
776 case SIOCETHTOOL:
777 return -EIO;
778
779 case SIOCSIFHWADDR:
780 dev_err(&lp->ndev->dev, "SIOCSIFHWADDR\n");
781
782 /* Copy MAC address in from user space */
783 copy_from_user((void __force *) dev->dev_addr,
784 (void __user __force *) hw_addr,
785 IFHWADDRLEN);
786 xemaclite_set_mac_address(lp, dev->dev_addr);
787 break;
788 default:
789 return -EOPNOTSUPP;
790 }
791
792 return 0;
793 }
794
795 /**
796 * xemaclite_remove_ndev - Free the network device
797 * @ndev: Pointer to the network device to be freed
798 *
799 * This function un maps the IO region of the Emaclite device and frees the net
800 * device.
801 */
802 static void xemaclite_remove_ndev(struct net_device *ndev)
803 {
804 if (ndev) {
805 struct net_local *lp = (struct net_local *) netdev_priv(ndev);
806
807 if (lp->base_addr)
808 iounmap((void __iomem __force *) (lp->base_addr));
809 free_netdev(ndev);
810 }
811 }
812
813 /**
814 * get_bool - Get a parameter from the OF device
815 * @ofdev: Pointer to OF device structure
816 * @s: Property to be retrieved
817 *
818 * This function looks for a property in the device node and returns the value
819 * of the property if its found or 0 if the property is not found.
820 *
821 * Return: Value of the parameter if the parameter is found, or 0 otherwise
822 */
823 static bool get_bool(struct of_device *ofdev, const char *s)
824 {
825 u32 *p = (u32 *)of_get_property(ofdev->node, s, NULL);
826
827 if (p) {
828 return (bool)*p;
829 } else {
830 dev_warn(&ofdev->dev, "Parameter %s not found,"
831 "defaulting to false\n", s);
832 return 0;
833 }
834 }
835
836 static struct net_device_ops xemaclite_netdev_ops;
837
838 /**
839 * xemaclite_of_probe - Probe method for the Emaclite device.
840 * @ofdev: Pointer to OF device structure
841 * @match: Pointer to the structure used for matching a device
842 *
843 * This function probes for the Emaclite device in the device tree.
844 * It initializes the driver data structure and the hardware, sets the MAC
845 * address and registers the network device.
846 *
847 * Return: 0, if the driver is bound to the Emaclite device, or
848 * a negative error if there is failure.
849 */
850 static int __devinit xemaclite_of_probe(struct of_device *ofdev,
851 const struct of_device_id *match)
852 {
853 struct resource r_irq; /* Interrupt resources */
854 struct resource r_mem; /* IO mem resources */
855 struct net_device *ndev = NULL;
856 struct net_local *lp = NULL;
857 struct device *dev = &ofdev->dev;
858 const void *mac_address;
859
860 int rc = 0;
861
862 dev_info(dev, "Device Tree Probing\n");
863
864 /* Get iospace for the device */
865 rc = of_address_to_resource(ofdev->node, 0, &r_mem);
866 if (rc) {
867 dev_err(dev, "invalid address\n");
868 return rc;
869 }
870
871 /* Get IRQ for the device */
872 rc = of_irq_to_resource(ofdev->node, 0, &r_irq);
873 if (rc == NO_IRQ) {
874 dev_err(dev, "no IRQ found\n");
875 return rc;
876 }
877
878 /* Create an ethernet device instance */
879 ndev = alloc_etherdev(sizeof(struct net_local));
880 if (!ndev) {
881 dev_err(dev, "Could not allocate network device\n");
882 return -ENOMEM;
883 }
884
885 dev_set_drvdata(dev, ndev);
886
887 ndev->irq = r_irq.start;
888 ndev->mem_start = r_mem.start;
889 ndev->mem_end = r_mem.end;
890
891 lp = netdev_priv(ndev);
892 lp->ndev = ndev;
893
894 if (!request_mem_region(ndev->mem_start,
895 ndev->mem_end - ndev->mem_start + 1,
896 DRIVER_NAME)) {
897 dev_err(dev, "Couldn't lock memory region at %p\n",
898 (void *)ndev->mem_start);
899 rc = -EBUSY;
900 goto error2;
901 }
902
903 /* Get the virtual base address for the device */
904 lp->base_addr = ioremap(r_mem.start, r_mem.end - r_mem.start + 1);
905 if (NULL == lp->base_addr) {
906 dev_err(dev, "EmacLite: Could not allocate iomem\n");
907 rc = -EIO;
908 goto error1;
909 }
910
911 spin_lock_init(&lp->reset_lock);
912 lp->next_tx_buf_to_use = 0x0;
913 lp->next_rx_buf_to_use = 0x0;
914 lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
915 lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
916 mac_address = of_get_mac_address(ofdev->node);
917
918 if (mac_address)
919 /* Set the MAC address. */
920 memcpy(ndev->dev_addr, mac_address, 6);
921 else
922 dev_warn(dev, "No MAC address found\n");
923
924 /* Clear the Tx CSR's in case this is a restart */
925 out_be32(lp->base_addr + XEL_TSR_OFFSET, 0);
926 out_be32(lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET, 0);
927
928 /* Set the MAC address in the EmacLite device */
929 xemaclite_set_mac_address(lp, ndev->dev_addr);
930
931 dev_info(dev,
932 "MAC address is now %2x:%2x:%2x:%2x:%2x:%2x\n",
933 ndev->dev_addr[0], ndev->dev_addr[1],
934 ndev->dev_addr[2], ndev->dev_addr[3],
935 ndev->dev_addr[4], ndev->dev_addr[5]);
936
937 ndev->netdev_ops = &xemaclite_netdev_ops;
938 ndev->flags &= ~IFF_MULTICAST;
939 ndev->watchdog_timeo = TX_TIMEOUT;
940
941 /* Finally, register the device */
942 rc = register_netdev(ndev);
943 if (rc) {
944 dev_err(dev,
945 "Cannot register network device, aborting\n");
946 goto error1;
947 }
948
949 dev_info(dev,
950 "Xilinx EmacLite at 0x%08X mapped to 0x%08X, irq=%d\n",
951 (unsigned int __force)ndev->mem_start,
952 (unsigned int __force)lp->base_addr, ndev->irq);
953 return 0;
954
955 error1:
956 release_mem_region(ndev->mem_start, r_mem.end - r_mem.start + 1);
957
958 error2:
959 xemaclite_remove_ndev(ndev);
960 return rc;
961 }
962
963 /**
964 * xemaclite_of_remove - Unbind the driver from the Emaclite device.
965 * @of_dev: Pointer to OF device structure
966 *
967 * This function is called if a device is physically removed from the system or
968 * if the driver module is being unloaded. It frees any resources allocated to
969 * the device.
970 *
971 * Return: 0, always.
972 */
973 static int __devexit xemaclite_of_remove(struct of_device *of_dev)
974 {
975 struct device *dev = &of_dev->dev;
976 struct net_device *ndev = dev_get_drvdata(dev);
977
978 unregister_netdev(ndev);
979
980 release_mem_region(ndev->mem_start, ndev->mem_end-ndev->mem_start + 1);
981
982 xemaclite_remove_ndev(ndev);
983
984 dev_set_drvdata(dev, NULL);
985
986 return 0;
987 }
988
989 static struct net_device_ops xemaclite_netdev_ops = {
990 .ndo_open = xemaclite_open,
991 .ndo_stop = xemaclite_close,
992 .ndo_start_xmit = xemaclite_send,
993 .ndo_do_ioctl = xemaclite_ioctl,
994 .ndo_tx_timeout = xemaclite_tx_timeout,
995 .ndo_get_stats = xemaclite_get_stats,
996 };
997
998 /* Match table for OF platform binding */
999 static struct of_device_id xemaclite_of_match[] __devinitdata = {
1000 { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
1001 { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
1002 { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
1003 { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
1004 { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
1005 { /* end of list */ },
1006 };
1007 MODULE_DEVICE_TABLE(of, xemaclite_of_match);
1008
1009 static struct of_platform_driver xemaclite_of_driver = {
1010 .name = DRIVER_NAME,
1011 .match_table = xemaclite_of_match,
1012 .probe = xemaclite_of_probe,
1013 .remove = __devexit_p(xemaclite_of_remove),
1014 };
1015
1016 /**
1017 * xgpiopss_init - Initial driver registration call
1018 *
1019 * Return: 0 upon success, or a negative error upon failure.
1020 */
1021 static int __init xemaclite_init(void)
1022 {
1023 /* No kernel boot options used, we just need to register the driver */
1024 return of_register_platform_driver(&xemaclite_of_driver);
1025 }
1026
1027 /**
1028 * xemaclite_cleanup - Driver un-registration call
1029 */
1030 static void __exit xemaclite_cleanup(void)
1031 {
1032 of_unregister_platform_driver(&xemaclite_of_driver);
1033 }
1034
1035 module_init(xemaclite_init);
1036 module_exit(xemaclite_cleanup);
1037
1038 MODULE_AUTHOR("Xilinx, Inc.");
1039 MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1040 MODULE_LICENSE("GPL");