x86: Reduce verbosity of "TSC is reliable" message
[linux-2.6/kvm.git] / drivers / net / xilinx_emaclite.c
blobdc22782633a5b4ac58eb04c285055ccaa6f9c867
1 /*
2 * Xilinx EmacLite Linux driver for the Xilinx Ethernet MAC Lite device.
4 * This is a new flat driver which is based on the original emac_lite
5 * driver from John Williams <john.williams@petalogix.com>.
7 * 2007-2009 (c) Xilinx, Inc.
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.
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>
23 #include <linux/of_device.h>
24 #include <linux/of_platform.h>
26 #define DRIVER_NAME "xilinx_emaclite"
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 */
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 */
38 #define XEL_BUFFER_OFFSET 0x0800 /* Next Tx/Rx buffer's offset */
40 /* Global Interrupt Enable Register (GIER) Bit Masks */
41 #define XEL_GIER_GIE_MASK 0x80000000 /* Global Enable */
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 */
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)
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 */
58 /* Transmit Packet Length Register (TPLR) */
59 #define XEL_TPLR_LENGTH_MASK 0x0000FFFF /* Tx packet length */
61 /* Receive Packet Length Register (RPLR) */
62 #define XEL_RPLR_LENGTH_MASK 0x0000FFFF /* Rx packet length */
64 #define XEL_HEADER_OFFSET 12 /* Offset to length field */
65 #define XEL_HEADER_SHIFT 16 /* Shift value for length */
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 */
73 #define TX_TIMEOUT (60*HZ) /* Tx timeout is 60 seconds. */
74 #define ALIGNMENT 4
76 /* BUFFER_ALIGN(adr) calculates the number of bytes to the next alignment. */
77 #define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32) adr)) % ALIGNMENT)
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
91 struct net_local {
93 struct net_device *ndev;
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;
101 spinlock_t reset_lock;
102 struct sk_buff *deferred_skb;
106 /*************************/
107 /* EmacLite driver calls */
108 /*************************/
111 * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
112 * @drvdata: Pointer to the Emaclite device private data
114 * This function enables the Tx and Rx interrupts for the Emaclite device along
115 * with the Global Interrupt Enable.
117 static void xemaclite_enable_interrupts(struct net_local *drvdata)
119 u32 reg_data;
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);
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);
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);
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);
151 /* Enable the Global Interrupt Enable */
152 out_be32(drvdata->base_addr + XEL_GIER_OFFSET, XEL_GIER_GIE_MASK);
156 * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
157 * @drvdata: Pointer to the Emaclite device private data
159 * This function disables the Tx and Rx interrupts for the Emaclite device,
160 * along with the Global Interrupt Enable.
162 static void xemaclite_disable_interrupts(struct net_local *drvdata)
164 u32 reg_data;
166 /* Disable the Global Interrupt Enable */
167 out_be32(drvdata->base_addr + XEL_GIER_OFFSET, XEL_GIER_GIE_MASK);
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));
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));
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));
189 /* Disable the Rx interrupts for the second buffer
190 * if configured in HW */
191 if (drvdata->rx_ping_pong != 0) {
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));
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
207 * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
208 * address in the EmacLite device.
210 static void xemaclite_aligned_write(void *src_ptr, u32 *dest_ptr,
211 unsigned length)
213 u32 align_buffer;
214 u32 *to_u32_ptr;
215 u16 *from_u16_ptr, *to_u16_ptr;
217 to_u32_ptr = dest_ptr;
218 from_u16_ptr = (u16 *) src_ptr;
219 align_buffer = 0;
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++;
226 /* Output a word */
227 *to_u32_ptr++ = align_buffer;
229 if (length) {
230 u8 *from_u8_ptr, *to_u8_ptr;
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;
237 /* Output the remaining data */
238 for (; length > 0; length--)
239 *to_u8_ptr++ = *from_u8_ptr++;
241 *to_u32_ptr = align_buffer;
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
251 * This function reads data from a 32-bit aligned address in the EmacLite device
252 * to a 16-bit aligned buffer.
254 static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
255 unsigned length)
257 u16 *to_u16_ptr, *from_u16_ptr;
258 u32 *from_u32_ptr;
259 u32 align_buffer;
261 from_u32_ptr = src_ptr;
262 to_u16_ptr = (u16 *) dest_ptr;
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;
269 /* Read data from source */
270 *to_u16_ptr++ = *from_u16_ptr++;
271 *to_u16_ptr++ = *from_u16_ptr++;
274 if (length) {
275 u8 *to_u8_ptr, *from_u8_ptr;
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;
282 /* Read the remaining data */
283 for (; length > 0; length--)
284 *to_u8_ptr = *from_u8_ptr;
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
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.
298 * Return: 0 upon success or -1 if the buffer(s) are full.
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.
303 static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
304 unsigned int byte_count)
306 u32 reg_data;
307 void __iomem *addr;
309 /* Determine the expected Tx buffer address */
310 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
312 /* If the length is too large, truncate it */
313 if (byte_count > ETH_FRAME_LEN)
314 byte_count = ETH_FRAME_LEN;
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) {
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 */
328 addr = (void __iomem __force *)((u32 __force)addr ^
329 XEL_BUFFER_OFFSET);
330 reg_data = in_be32(addr + XEL_TSR_OFFSET);
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 */
338 /* Write the frame to the buffer */
339 xemaclite_aligned_write(data, (u32 __force *) addr, byte_count);
341 out_be32(addr + XEL_TPLR_OFFSET, (byte_count & XEL_TPLR_LENGTH_MASK));
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);
351 return 0;
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
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.
362 * Return: Total number of bytes received
364 static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data)
366 void __iomem *addr;
367 u16 length, proto_type;
368 u32 reg_data;
370 /* Determine the expected buffer address */
371 addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use);
373 /* Verify which buffer has valid data */
374 reg_data = in_be32(addr + XEL_RSR_OFFSET);
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 */
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 */
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);
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)) {
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;
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;
424 /* Read from the EmacLite device */
425 xemaclite_aligned_read((u32 __force *) (addr + XEL_RXBUFF_OFFSET),
426 data, length);
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);
433 return length;
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)
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).
447 static void xemaclite_set_mac_address(struct net_local *drvdata,
448 u8 *address_ptr)
450 void __iomem *addr;
451 u32 reg_data;
453 /* Determine the expected Tx buffer address */
454 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
456 xemaclite_aligned_write(address_ptr, (u32 __force *) addr, ETH_ALEN);
458 out_be32(addr + XEL_TPLR_OFFSET, ETH_ALEN);
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);
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)
471 * xemaclite_tx_timeout - Callback for Tx Timeout
472 * @dev: Pointer to the network device
474 * This function is called when Tx time out occurs for Emaclite device.
476 static void xemaclite_tx_timeout(struct net_device *dev)
478 struct net_local *lp = (struct net_local *) netdev_priv(dev);
479 unsigned long flags;
481 dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
482 TX_TIMEOUT * 1000UL / HZ);
484 dev->stats.tx_errors++;
486 /* Reset the device */
487 spin_lock_irqsave(&lp->reset_lock, flags);
489 /* Shouldn't really be necessary, but shouldn't hurt */
490 netif_stop_queue(dev);
492 xemaclite_disable_interrupts(lp);
493 xemaclite_enable_interrupts(lp);
495 if (lp->deferred_skb) {
496 dev_kfree_skb(lp->deferred_skb);
497 lp->deferred_skb = NULL;
498 dev->stats.tx_errors++;
501 /* To exclude tx timeout */
502 dev->trans_start = 0xffffffff - TX_TIMEOUT - TX_TIMEOUT;
504 /* We're all ready to go. Start the queue */
505 netif_wake_queue(dev);
506 spin_unlock_irqrestore(&lp->reset_lock, flags);
509 /**********************/
510 /* Interrupt Handlers */
511 /**********************/
514 * xemaclite_tx_handler - Interrupt handler for frames sent
515 * @dev: Pointer to the network device
517 * This function updates the number of packets transmitted and handles the
518 * deferred skb, if there is one.
520 static void xemaclite_tx_handler(struct net_device *dev)
522 struct net_local *lp = (struct net_local *) netdev_priv(dev);
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);
541 * xemaclite_rx_handler- Interrupt handler for frames received
542 * @dev: Pointer to the network device
544 * This function allocates memory for a socket buffer, fills it with data
545 * received and hands it over to the TCP/IP stack.
547 static void xemaclite_rx_handler(struct net_device *dev)
549 struct net_local *lp = (struct net_local *) netdev_priv(dev);
550 struct sk_buff *skb;
551 unsigned int align;
552 u32 len;
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;
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);
572 skb_reserve(skb, 2);
574 len = xemaclite_recv_data(lp, (u8 *) skb->data);
576 if (!len) {
577 dev->stats.rx_errors++;
578 dev_kfree_skb_irq(skb);
579 return;
582 skb_put(skb, len); /* Tell the skb how much data we got */
583 skb->dev = dev; /* Fill out required meta-data */
585 skb->protocol = eth_type_trans(skb, dev);
586 skb->ip_summed = CHECKSUM_NONE;
588 dev->stats.rx_packets++;
589 dev->stats.rx_bytes += len;
591 netif_rx(skb); /* Send the packet upstream */
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
600 * This function handles the Tx and Rx interrupts of the EmacLite device.
602 static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
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;
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))
615 xemaclite_rx_handler(dev);
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) {
622 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
623 out_be32(base_addr + XEL_TSR_OFFSET, tx_status);
625 tx_complete = 1;
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) {
633 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
634 out_be32(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET,
635 tx_status);
637 tx_complete = 1;
640 /* If there was a Tx interrupt, call the Tx Handler */
641 if (tx_complete != 0)
642 xemaclite_tx_handler(dev);
644 return IRQ_HANDLED;
648 * xemaclite_open - Open the network device
649 * @dev: Pointer to the network device
651 * This function sets the MAC address, requests an IRQ and enables interrupts
652 * for the Emaclite device and starts the Tx queue.
654 static int xemaclite_open(struct net_device *dev)
656 struct net_local *lp = (struct net_local *) netdev_priv(dev);
657 int retval;
659 /* Just to be safe, stop the device first */
660 xemaclite_disable_interrupts(lp);
662 /* Set the MAC address each time opened */
663 xemaclite_set_mac_address(lp, dev->dev_addr);
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;
673 /* Enable Interrupts */
674 xemaclite_enable_interrupts(lp);
676 /* We're ready to go */
677 netif_start_queue(dev);
679 return 0;
683 * xemaclite_close - Close the network device
684 * @dev: Pointer to the network device
686 * This function stops the Tx queue, disables interrupts and frees the IRQ for
687 * the Emaclite device.
689 static int xemaclite_close(struct net_device *dev)
691 struct net_local *lp = (struct net_local *) netdev_priv(dev);
693 netif_stop_queue(dev);
694 xemaclite_disable_interrupts(lp);
695 free_irq(dev->irq, dev);
697 return 0;
701 * xemaclite_get_stats - Get the stats for the net_device
702 * @dev: Pointer to the network device
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.
708 * Return: Pointer to the net_device_stats structure.
710 static struct net_device_stats *xemaclite_get_stats(struct net_device *dev)
712 return &dev->stats;
716 * xemaclite_send - Transmit a frame
717 * @orig_skb: Pointer to the socket buffer to be transmitted
718 * @dev: Pointer to the network device
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.
727 * Return: 0, always.
729 static int xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
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;
736 len = orig_skb->len;
738 new_skb = orig_skb;
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;
750 spin_unlock_irqrestore(&lp->reset_lock, flags);
752 dev->stats.tx_bytes += len;
753 dev_kfree_skb(new_skb);
754 dev->trans_start = jiffies;
756 return 0;
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
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.
768 * Return: 0 to indicate success, or a negative error for failure.
770 static int xemaclite_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
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;
775 switch (cmd) {
776 case SIOCETHTOOL:
777 return -EIO;
779 case SIOCSIFHWADDR:
780 dev_err(&lp->ndev->dev, "SIOCSIFHWADDR\n");
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;
792 return 0;
796 * xemaclite_remove_ndev - Free the network device
797 * @ndev: Pointer to the network device to be freed
799 * This function un maps the IO region of the Emaclite device and frees the net
800 * device.
802 static void xemaclite_remove_ndev(struct net_device *ndev)
804 if (ndev) {
805 struct net_local *lp = (struct net_local *) netdev_priv(ndev);
807 if (lp->base_addr)
808 iounmap((void __iomem __force *) (lp->base_addr));
809 free_netdev(ndev);
814 * get_bool - Get a parameter from the OF device
815 * @ofdev: Pointer to OF device structure
816 * @s: Property to be retrieved
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.
821 * Return: Value of the parameter if the parameter is found, or 0 otherwise
823 static bool get_bool(struct of_device *ofdev, const char *s)
825 u32 *p = (u32 *)of_get_property(ofdev->node, s, NULL);
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;
836 static struct net_device_ops xemaclite_netdev_ops;
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
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.
847 * Return: 0, if the driver is bound to the Emaclite device, or
848 * a negative error if there is failure.
850 static int __devinit xemaclite_of_probe(struct of_device *ofdev,
851 const struct of_device_id *match)
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;
860 int rc = 0;
862 dev_info(dev, "Device Tree Probing\n");
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;
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;
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;
885 dev_set_drvdata(dev, ndev);
887 ndev->irq = r_irq.start;
888 ndev->mem_start = r_mem.start;
889 ndev->mem_end = r_mem.end;
891 lp = netdev_priv(ndev);
892 lp->ndev = ndev;
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;
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;
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);
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");
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);
928 /* Set the MAC address in the EmacLite device */
929 xemaclite_set_mac_address(lp, ndev->dev_addr);
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]);
937 ndev->netdev_ops = &xemaclite_netdev_ops;
938 ndev->flags &= ~IFF_MULTICAST;
939 ndev->watchdog_timeo = TX_TIMEOUT;
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;
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;
955 error1:
956 release_mem_region(ndev->mem_start, r_mem.end - r_mem.start + 1);
958 error2:
959 xemaclite_remove_ndev(ndev);
960 return rc;
964 * xemaclite_of_remove - Unbind the driver from the Emaclite device.
965 * @of_dev: Pointer to OF device structure
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.
971 * Return: 0, always.
973 static int __devexit xemaclite_of_remove(struct of_device *of_dev)
975 struct device *dev = &of_dev->dev;
976 struct net_device *ndev = dev_get_drvdata(dev);
978 unregister_netdev(ndev);
980 release_mem_region(ndev->mem_start, ndev->mem_end-ndev->mem_start + 1);
982 xemaclite_remove_ndev(ndev);
984 dev_set_drvdata(dev, NULL);
986 return 0;
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,
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 */ },
1007 MODULE_DEVICE_TABLE(of, xemaclite_of_match);
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),
1017 * xgpiopss_init - Initial driver registration call
1019 * Return: 0 upon success, or a negative error upon failure.
1021 static int __init xemaclite_init(void)
1023 /* No kernel boot options used, we just need to register the driver */
1024 return of_register_platform_driver(&xemaclite_of_driver);
1028 * xemaclite_cleanup - Driver un-registration call
1030 static void __exit xemaclite_cleanup(void)
1032 of_unregister_platform_driver(&xemaclite_of_driver);
1035 module_init(xemaclite_init);
1036 module_exit(xemaclite_cleanup);
1038 MODULE_AUTHOR("Xilinx, Inc.");
1039 MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1040 MODULE_LICENSE("GPL");