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>
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
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. */
76 /* BUFFER_ALIGN(adr) calculates the number of bytes to the next alignment. */
77 #define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32) adr)) % ALIGNMENT)
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
93 struct net_device
*ndev
;
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
)
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
+
133 reg_data
| XEL_TSR_XMIT_IE_MASK
);
136 /* Enable the Rx interrupts for the first buffer */
137 out_be32(drvdata
->base_addr
+ XEL_RSR_OFFSET
,
138 XEL_RSR_RECV_IE_MASK
);
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
+
145 XEL_RSR_RECV_IE_MASK
);
148 /* Enable the Global Interrupt Enable */
149 out_be32(drvdata
->base_addr
+ XEL_GIER_OFFSET
, XEL_GIER_GIE_MASK
);
153 * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
154 * @drvdata: Pointer to the Emaclite device private data
156 * This function disables the Tx and Rx interrupts for the Emaclite device,
157 * along with the Global Interrupt Enable.
159 static void xemaclite_disable_interrupts(struct net_local
*drvdata
)
163 /* Disable the Global Interrupt Enable */
164 out_be32(drvdata
->base_addr
+ XEL_GIER_OFFSET
, XEL_GIER_GIE_MASK
);
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
));
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
+
176 out_be32(drvdata
->base_addr
+ XEL_BUFFER_OFFSET
+
178 reg_data
& (~XEL_TSR_XMIT_IE_MASK
));
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
));
186 /* Disable the Rx interrupts for the second buffer
187 * if configured in HW */
188 if (drvdata
->rx_ping_pong
!= 0) {
190 reg_data
= in_be32(drvdata
->base_addr
+ XEL_BUFFER_OFFSET
+
192 out_be32(drvdata
->base_addr
+ XEL_BUFFER_OFFSET
+
194 reg_data
& (~XEL_RSR_RECV_IE_MASK
));
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
204 * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
205 * address in the EmacLite device.
207 static void xemaclite_aligned_write(void *src_ptr
, u32
*dest_ptr
,
212 u16
*from_u16_ptr
, *to_u16_ptr
;
214 to_u32_ptr
= dest_ptr
;
215 from_u16_ptr
= (u16
*) src_ptr
;
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
++;
224 *to_u32_ptr
++ = align_buffer
;
227 u8
*from_u8_ptr
, *to_u8_ptr
;
229 /* Set up to output the remaining data */
231 to_u8_ptr
= (u8
*) &align_buffer
;
232 from_u8_ptr
= (u8
*) from_u16_ptr
;
234 /* Output the remaining data */
235 for (; length
> 0; length
--)
236 *to_u8_ptr
++ = *from_u8_ptr
++;
238 *to_u32_ptr
= align_buffer
;
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
248 * This function reads data from a 32-bit aligned address in the EmacLite device
249 * to a 16-bit aligned buffer.
251 static void xemaclite_aligned_read(u32
*src_ptr
, u8
*dest_ptr
,
254 u16
*to_u16_ptr
, *from_u16_ptr
;
258 from_u32_ptr
= src_ptr
;
259 to_u16_ptr
= (u16
*) dest_ptr
;
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
;
266 /* Read data from source */
267 *to_u16_ptr
++ = *from_u16_ptr
++;
268 *to_u16_ptr
++ = *from_u16_ptr
++;
272 u8
*to_u8_ptr
, *from_u8_ptr
;
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
;
279 /* Read the remaining data */
280 for (; length
> 0; length
--)
281 *to_u8_ptr
= *from_u8_ptr
;
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
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
295 * Return: 0 upon success or -1 if the buffer(s) are full.
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.
300 static int xemaclite_send_data(struct net_local
*drvdata
, u8
*data
,
301 unsigned int byte_count
)
306 /* Determine the expected Tx buffer address */
307 addr
= drvdata
->base_addr
+ drvdata
->next_tx_buf_to_use
;
309 /* If the length is too large, truncate it */
310 if (byte_count
> ETH_FRAME_LEN
)
311 byte_count
= ETH_FRAME_LEN
;
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) {
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 */
325 addr
= (void __iomem __force
*)((u32 __force
)addr
^
327 reg_data
= in_be32(addr
+ XEL_TSR_OFFSET
);
329 if ((reg_data
& (XEL_TSR_XMIT_BUSY_MASK
|
330 XEL_TSR_XMIT_ACTIVE_MASK
)) != 0)
331 return -1; /* Buffers were full, return failure */
333 return -1; /* Buffer was full, return failure */
335 /* Write the frame to the buffer */
336 xemaclite_aligned_write(data
, (u32 __force
*) addr
, byte_count
);
338 out_be32(addr
+ XEL_TPLR_OFFSET
, (byte_count
& XEL_TPLR_LENGTH_MASK
));
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
);
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
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.
359 * Return: Total number of bytes received
361 static u16
xemaclite_recv_data(struct net_local
*drvdata
, u8
*data
)
364 u16 length
, proto_type
;
367 /* Determine the expected buffer address */
368 addr
= (drvdata
->base_addr
+ drvdata
->next_rx_buf_to_use
);
370 /* Verify which buffer has valid data */
371 reg_data
= in_be32(addr
+ XEL_RSR_OFFSET
);
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
;
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
^
385 return 0; /* No data was available */
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 */
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
);
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
)) {
403 if (proto_type
== ETH_P_IP
) {
404 length
= ((in_be32(addr
+
405 XEL_HEADER_IP_LENGTH_OFFSET
+
406 XEL_RXBUFF_OFFSET
) >>
408 XEL_RPLR_LENGTH_MASK
);
409 length
+= ETH_HLEN
+ ETH_FCS_LEN
;
411 } else if (proto_type
== ETH_P_ARP
)
412 length
= XEL_ARP_PACKET_SIZE
+ ETH_HLEN
+ ETH_FCS_LEN
;
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
;
418 /* Use the length in the frame, plus the header and trailer */
419 length
= proto_type
+ ETH_HLEN
+ ETH_FCS_LEN
;
421 /* Read from the EmacLite device */
422 xemaclite_aligned_read((u32 __force
*) (addr
+ XEL_RXBUFF_OFFSET
),
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
);
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)
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).
444 static void xemaclite_set_mac_address(struct net_local
*drvdata
,
450 /* Determine the expected Tx buffer address */
451 addr
= drvdata
->base_addr
+ drvdata
->next_tx_buf_to_use
;
453 xemaclite_aligned_write(address_ptr
, (u32 __force
*) addr
, ETH_ALEN
);
455 out_be32(addr
+ XEL_TPLR_OFFSET
, ETH_ALEN
);
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
);
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)
468 * xemaclite_tx_timeout - Callback for Tx Timeout
469 * @dev: Pointer to the network device
471 * This function is called when Tx time out occurs for Emaclite device.
473 static void xemaclite_tx_timeout(struct net_device
*dev
)
475 struct net_local
*lp
= (struct net_local
*) netdev_priv(dev
);
478 dev_err(&lp
->ndev
->dev
, "Exceeded transmit timeout of %lu ms\n",
479 TX_TIMEOUT
* 1000UL / HZ
);
481 dev
->stats
.tx_errors
++;
483 /* Reset the device */
484 spin_lock_irqsave(&lp
->reset_lock
, flags
);
486 /* Shouldn't really be necessary, but shouldn't hurt */
487 netif_stop_queue(dev
);
489 xemaclite_disable_interrupts(lp
);
490 xemaclite_enable_interrupts(lp
);
492 if (lp
->deferred_skb
) {
493 dev_kfree_skb(lp
->deferred_skb
);
494 lp
->deferred_skb
= NULL
;
495 dev
->stats
.tx_errors
++;
498 /* To exclude tx timeout */
499 dev
->trans_start
= 0xffffffff - TX_TIMEOUT
- TX_TIMEOUT
;
501 /* We're all ready to go. Start the queue */
502 netif_wake_queue(dev
);
503 spin_unlock_irqrestore(&lp
->reset_lock
, flags
);
506 /**********************/
507 /* Interrupt Handlers */
508 /**********************/
511 * xemaclite_tx_handler - Interrupt handler for frames sent
512 * @dev: Pointer to the network device
514 * This function updates the number of packets transmitted and handles the
515 * deferred skb, if there is one.
517 static void xemaclite_tx_handler(struct net_device
*dev
)
519 struct net_local
*lp
= (struct net_local
*) netdev_priv(dev
);
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)
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
);
538 * xemaclite_rx_handler- Interrupt handler for frames received
539 * @dev: Pointer to the network device
541 * This function allocates memory for a socket buffer, fills it with data
542 * received and hands it over to the TCP/IP stack.
544 static void xemaclite_rx_handler(struct net_device
*dev
)
546 struct net_local
*lp
= (struct net_local
*) netdev_priv(dev
);
551 len
= ETH_FRAME_LEN
+ ETH_FCS_LEN
;
552 skb
= dev_alloc_skb(len
+ ALIGNMENT
);
554 /* Couldn't get memory. */
555 dev
->stats
.rx_dropped
++;
556 dev_err(&lp
->ndev
->dev
, "Could not allocate receive buffer\n");
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
565 align
= BUFFER_ALIGN(skb
->data
);
567 skb_reserve(skb
, align
);
571 len
= xemaclite_recv_data(lp
, (u8
*) skb
->data
);
574 dev
->stats
.rx_errors
++;
575 dev_kfree_skb_irq(skb
);
579 skb_put(skb
, len
); /* Tell the skb how much data we got */
580 skb
->dev
= dev
; /* Fill out required meta-data */
582 skb
->protocol
= eth_type_trans(skb
, dev
);
583 skb
->ip_summed
= CHECKSUM_NONE
;
585 dev
->stats
.rx_packets
++;
586 dev
->stats
.rx_bytes
+= len
;
588 netif_rx(skb
); /* Send the packet upstream */
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
597 * This function handles the Tx and Rx interrupts of the EmacLite device.
599 static irqreturn_t
xemaclite_interrupt(int irq
, void *dev_id
)
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
;
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
))
612 xemaclite_rx_handler(dev
);
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) {
619 tx_status
&= ~XEL_TSR_XMIT_ACTIVE_MASK
;
620 out_be32(base_addr
+ XEL_TSR_OFFSET
, tx_status
);
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) {
630 tx_status
&= ~XEL_TSR_XMIT_ACTIVE_MASK
;
631 out_be32(base_addr
+ XEL_BUFFER_OFFSET
+ XEL_TSR_OFFSET
,
637 /* If there was a Tx interrupt, call the Tx Handler */
638 if (tx_complete
!= 0)
639 xemaclite_tx_handler(dev
);
645 * xemaclite_open - Open the network device
646 * @dev: Pointer to the network device
648 * This function sets the MAC address, requests an IRQ and enables interrupts
649 * for the Emaclite device and starts the Tx queue.
651 static int xemaclite_open(struct net_device
*dev
)
653 struct net_local
*lp
= (struct net_local
*) netdev_priv(dev
);
656 /* Just to be safe, stop the device first */
657 xemaclite_disable_interrupts(lp
);
659 /* Set the MAC address each time opened */
660 xemaclite_set_mac_address(lp
, dev
->dev_addr
);
663 retval
= request_irq(dev
->irq
, &xemaclite_interrupt
, 0, dev
->name
, dev
);
665 dev_err(&lp
->ndev
->dev
, "Could not allocate interrupt %d\n",
670 /* Enable Interrupts */
671 xemaclite_enable_interrupts(lp
);
673 /* We're ready to go */
674 netif_start_queue(dev
);
680 * xemaclite_close - Close the network device
681 * @dev: Pointer to the network device
683 * This function stops the Tx queue, disables interrupts and frees the IRQ for
684 * the Emaclite device.
686 static int xemaclite_close(struct net_device
*dev
)
688 struct net_local
*lp
= (struct net_local
*) netdev_priv(dev
);
690 netif_stop_queue(dev
);
691 xemaclite_disable_interrupts(lp
);
692 free_irq(dev
->irq
, dev
);
698 * xemaclite_get_stats - Get the stats for the net_device
699 * @dev: Pointer to the network device
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
705 * Return: Pointer to the net_device_stats structure.
707 static struct net_device_stats
*xemaclite_get_stats(struct net_device
*dev
)
713 * xemaclite_send - Transmit a frame
714 * @orig_skb: Pointer to the socket buffer to be transmitted
715 * @dev: Pointer to the network device
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.
726 static int xemaclite_send(struct sk_buff
*orig_skb
, struct net_device
*dev
)
728 struct net_local
*lp
= (struct net_local
*) netdev_priv(dev
);
729 struct sk_buff
*new_skb
;
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
);
747 spin_unlock_irqrestore(&lp
->reset_lock
, flags
);
749 dev
->stats
.tx_bytes
+= len
;
750 dev_kfree_skb(new_skb
);
751 dev
->trans_start
= jiffies
;
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
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.
765 * Return: 0 to indicate success, or a negative error for failure.
767 static int xemaclite_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
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
;
777 dev_err(&lp
->ndev
->dev
, "SIOCSIFHWADDR\n");
779 /* Copy MAC address in from user space */
780 copy_from_user((void __force
*) dev
->dev_addr
,
781 (void __user __force
*) hw_addr
,
783 xemaclite_set_mac_address(lp
, dev
->dev_addr
);
793 * xemaclite_remove_ndev - Free the network device
794 * @ndev: Pointer to the network device to be freed
796 * This function un maps the IO region of the Emaclite device and frees the net
799 static void xemaclite_remove_ndev(struct net_device
*ndev
)
802 struct net_local
*lp
= (struct net_local
*) netdev_priv(ndev
);
805 iounmap((void __iomem __force
*) (lp
->base_addr
));
811 * get_bool - Get a parameter from the OF device
812 * @ofdev: Pointer to OF device structure
813 * @s: Property to be retrieved
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.
818 * Return: Value of the parameter if the parameter is found, or 0 otherwise
820 static bool get_bool(struct of_device
*ofdev
, const char *s
)
822 u32
*p
= (u32
*)of_get_property(ofdev
->node
, s
, NULL
);
827 dev_warn(&ofdev
->dev
, "Parameter %s not found,"
828 "defaulting to false\n", s
);
833 static struct net_device_ops xemaclite_netdev_ops
;
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
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.
844 * Return: 0, if the driver is bound to the Emaclite device, or
845 * a negative error if there is failure.
847 static int __devinit
xemaclite_of_probe(struct of_device
*ofdev
,
848 const struct of_device_id
*match
)
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
;
859 dev_info(dev
, "Device Tree Probing\n");
861 /* Get iospace for the device */
862 rc
= of_address_to_resource(ofdev
->node
, 0, &r_mem
);
864 dev_err(dev
, "invalid address\n");
868 /* Get IRQ for the device */
869 rc
= of_irq_to_resource(ofdev
->node
, 0, &r_irq
);
871 dev_err(dev
, "no IRQ found\n");
875 /* Create an ethernet device instance */
876 ndev
= alloc_etherdev(sizeof(struct net_local
));
878 dev_err(dev
, "Could not allocate network device\n");
882 dev_set_drvdata(dev
, ndev
);
884 ndev
->irq
= r_irq
.start
;
885 ndev
->mem_start
= r_mem
.start
;
886 ndev
->mem_end
= r_mem
.end
;
888 lp
= netdev_priv(ndev
);
891 if (!request_mem_region(ndev
->mem_start
,
892 ndev
->mem_end
- ndev
->mem_start
+ 1,
894 dev_err(dev
, "Couldn't lock memory region at %p\n",
895 (void *)ndev
->mem_start
);
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");
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
);
916 /* Set the MAC address. */
917 memcpy(ndev
->dev_addr
, mac_address
, 6);
919 dev_warn(dev
, "No MAC address found\n");
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);
925 /* Set the MAC address in the EmacLite device */
926 xemaclite_set_mac_address(lp
, ndev
->dev_addr
);
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]);
934 ndev
->netdev_ops
= &xemaclite_netdev_ops
;
935 ndev
->flags
&= ~IFF_MULTICAST
;
936 ndev
->watchdog_timeo
= TX_TIMEOUT
;
938 /* Finally, register the device */
939 rc
= register_netdev(ndev
);
942 "Cannot register network device, aborting\n");
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
);
953 release_mem_region(ndev
->mem_start
, r_mem
.end
- r_mem
.start
+ 1);
956 xemaclite_remove_ndev(ndev
);
961 * xemaclite_of_remove - Unbind the driver from the Emaclite device.
962 * @of_dev: Pointer to OF device structure
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
970 static int __devexit
xemaclite_of_remove(struct of_device
*of_dev
)
972 struct device
*dev
= &of_dev
->dev
;
973 struct net_device
*ndev
= dev_get_drvdata(dev
);
975 unregister_netdev(ndev
);
977 release_mem_region(ndev
->mem_start
, ndev
->mem_end
-ndev
->mem_start
+ 1);
979 xemaclite_remove_ndev(ndev
);
981 dev_set_drvdata(dev
, NULL
);
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
,
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 */ },
1004 MODULE_DEVICE_TABLE(of
, xemaclite_of_match
);
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
),
1014 * xgpiopss_init - Initial driver registration call
1016 * Return: 0 upon success, or a negative error upon failure.
1018 static int __init
xemaclite_init(void)
1020 /* No kernel boot options used, we just need to register the driver */
1021 return of_register_platform_driver(&xemaclite_of_driver
);
1025 * xemaclite_cleanup - Driver un-registration call
1027 static void __exit
xemaclite_cleanup(void)
1029 of_unregister_platform_driver(&xemaclite_of_driver
);
1032 module_init(xemaclite_init
);
1033 module_exit(xemaclite_cleanup
);
1035 MODULE_AUTHOR("Xilinx, Inc.");
1036 MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1037 MODULE_LICENSE("GPL");