blob: fefb8cd5eb65e1cb462f2a277c0bda422a89cd60 [file] [log] [blame]
John Linnbb81b2d2009-08-20 02:52:16 -07001/*
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
Michal Simek9e7c4142013-05-30 00:28:08 +00005 * driver from John Williams <john.williams@xilinx.com>.
John Linnbb81b2d2009-08-20 02:52:16 -07006 *
Michal Simek9e7c4142013-05-30 00:28:08 +00007 * 2007 - 2013 (c) Xilinx, Inc.
John Linnbb81b2d2009-08-20 02:52:16 -07008 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Grant Likely22ae7822010-07-29 11:49:01 -060023#include <linux/of_address.h>
John Linnbb81b2d2009-08-20 02:52:16 -070024#include <linux/of_device.h>
25#include <linux/of_platform.h>
John Linn5cdaaa12010-02-15 21:51:00 -080026#include <linux/of_mdio.h>
David Daney4b6ba8a2010-10-26 15:07:13 -070027#include <linux/of_net.h>
John Linn5cdaaa12010-02-15 21:51:00 -080028#include <linux/phy.h>
Michal Simek075cd292011-06-09 01:29:13 -070029#include <linux/interrupt.h>
John Linnbb81b2d2009-08-20 02:52:16 -070030
31#define DRIVER_NAME "xilinx_emaclite"
32
33/* Register offsets for the EmacLite Core */
Michal Simekcd738c42013-09-12 09:05:11 +020034#define XEL_TXBUFF_OFFSET 0x0 /* Transmit Buffer */
John Linn5cdaaa12010-02-15 21:51:00 -080035#define XEL_MDIOADDR_OFFSET 0x07E4 /* MDIO Address Register */
36#define XEL_MDIOWR_OFFSET 0x07E8 /* MDIO Write Data Register */
37#define XEL_MDIORD_OFFSET 0x07EC /* MDIO Read Data Register */
38#define XEL_MDIOCTRL_OFFSET 0x07F0 /* MDIO Control Register */
John Linnbb81b2d2009-08-20 02:52:16 -070039#define XEL_GIER_OFFSET 0x07F8 /* GIE Register */
40#define XEL_TSR_OFFSET 0x07FC /* Tx status */
41#define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
42
43#define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
44#define XEL_RPLR_OFFSET 0x100C /* Rx packet length */
45#define XEL_RSR_OFFSET 0x17FC /* Rx status */
46
47#define XEL_BUFFER_OFFSET 0x0800 /* Next Tx/Rx buffer's offset */
48
John Linn5cdaaa12010-02-15 21:51:00 -080049/* MDIO Address Register Bit Masks */
50#define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */
51#define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */
52#define XEL_MDIOADDR_PHYADR_SHIFT 5
53#define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */
54
55/* MDIO Write Data Register Bit Masks */
56#define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */
57
58/* MDIO Read Data Register Bit Masks */
59#define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */
60
61/* MDIO Control Register Bit Masks */
62#define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */
63#define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */
64
John Linnbb81b2d2009-08-20 02:52:16 -070065/* Global Interrupt Enable Register (GIER) Bit Masks */
Michal Simekcd738c42013-09-12 09:05:11 +020066#define XEL_GIER_GIE_MASK 0x80000000 /* Global Enable */
John Linnbb81b2d2009-08-20 02:52:16 -070067
68/* Transmit Status Register (TSR) Bit Masks */
Michal Simekcd738c42013-09-12 09:05:11 +020069#define XEL_TSR_XMIT_BUSY_MASK 0x00000001 /* Tx complete */
70#define XEL_TSR_PROGRAM_MASK 0x00000002 /* Program the MAC address */
71#define XEL_TSR_XMIT_IE_MASK 0x00000008 /* Tx interrupt enable bit */
72#define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000 /* Buffer is active, SW bit
John Linnbb81b2d2009-08-20 02:52:16 -070073 * only. This is not documented
74 * in the HW spec */
75
76/* Define for programming the MAC address into the EmacLite */
77#define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
78
79/* Receive Status Register (RSR) */
Michal Simekcd738c42013-09-12 09:05:11 +020080#define XEL_RSR_RECV_DONE_MASK 0x00000001 /* Rx complete */
81#define XEL_RSR_RECV_IE_MASK 0x00000008 /* Rx interrupt enable bit */
John Linnbb81b2d2009-08-20 02:52:16 -070082
83/* Transmit Packet Length Register (TPLR) */
Michal Simekcd738c42013-09-12 09:05:11 +020084#define XEL_TPLR_LENGTH_MASK 0x0000FFFF /* Tx packet length */
John Linnbb81b2d2009-08-20 02:52:16 -070085
86/* Receive Packet Length Register (RPLR) */
Michal Simekcd738c42013-09-12 09:05:11 +020087#define XEL_RPLR_LENGTH_MASK 0x0000FFFF /* Rx packet length */
John Linnbb81b2d2009-08-20 02:52:16 -070088
Michal Simekcd738c42013-09-12 09:05:11 +020089#define XEL_HEADER_OFFSET 12 /* Offset to length field */
90#define XEL_HEADER_SHIFT 16 /* Shift value for length */
John Linnbb81b2d2009-08-20 02:52:16 -070091
92/* General Ethernet Definitions */
Michal Simekcd738c42013-09-12 09:05:11 +020093#define XEL_ARP_PACKET_SIZE 28 /* Max ARP packet size */
94#define XEL_HEADER_IP_LENGTH_OFFSET 16 /* IP Length Offset */
John Linnbb81b2d2009-08-20 02:52:16 -070095
96
97
98#define TX_TIMEOUT (60*HZ) /* Tx timeout is 60 seconds. */
99#define ALIGNMENT 4
100
101/* BUFFER_ALIGN(adr) calculates the number of bytes to the next alignment. */
102#define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32) adr)) % ALIGNMENT)
103
104/**
105 * struct net_local - Our private per device data
106 * @ndev: instance of the network device
107 * @tx_ping_pong: indicates whether Tx Pong buffer is configured in HW
108 * @rx_ping_pong: indicates whether Rx Pong buffer is configured in HW
109 * @next_tx_buf_to_use: next Tx buffer to write to
110 * @next_rx_buf_to_use: next Rx buffer to read from
111 * @base_addr: base address of the Emaclite device
112 * @reset_lock: lock used for synchronization
113 * @deferred_skb: holds an skb (for transmission at a later time) when the
114 * Tx buffer is not free
John Linn5cdaaa12010-02-15 21:51:00 -0800115 * @phy_dev: pointer to the PHY device
116 * @phy_node: pointer to the PHY device node
117 * @mii_bus: pointer to the MII bus
118 * @mdio_irqs: IRQs table for MDIO bus
119 * @last_link: last link status
120 * @has_mdio: indicates whether MDIO is included in the HW
John Linnbb81b2d2009-08-20 02:52:16 -0700121 */
122struct net_local {
123
124 struct net_device *ndev;
125
126 bool tx_ping_pong;
127 bool rx_ping_pong;
128 u32 next_tx_buf_to_use;
129 u32 next_rx_buf_to_use;
130 void __iomem *base_addr;
131
132 spinlock_t reset_lock;
133 struct sk_buff *deferred_skb;
John Linn5cdaaa12010-02-15 21:51:00 -0800134
135 struct phy_device *phy_dev;
136 struct device_node *phy_node;
137
138 struct mii_bus *mii_bus;
139 int mdio_irqs[PHY_MAX_ADDR];
140
141 int last_link;
142 bool has_mdio;
John Linnbb81b2d2009-08-20 02:52:16 -0700143};
144
145
146/*************************/
147/* EmacLite driver calls */
148/*************************/
149
150/**
151 * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
152 * @drvdata: Pointer to the Emaclite device private data
153 *
154 * This function enables the Tx and Rx interrupts for the Emaclite device along
155 * with the Global Interrupt Enable.
156 */
157static void xemaclite_enable_interrupts(struct net_local *drvdata)
158{
159 u32 reg_data;
160
161 /* Enable the Tx interrupts for the first Buffer */
Michal Simek123c1402013-05-30 00:28:06 +0000162 reg_data = __raw_readl(drvdata->base_addr + XEL_TSR_OFFSET);
163 __raw_writel(reg_data | XEL_TSR_XMIT_IE_MASK,
164 drvdata->base_addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700165
John Linnbb81b2d2009-08-20 02:52:16 -0700166 /* Enable the Rx interrupts for the first buffer */
Michal Simek123c1402013-05-30 00:28:06 +0000167 __raw_writel(XEL_RSR_RECV_IE_MASK, drvdata->base_addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700168
John Linnbb81b2d2009-08-20 02:52:16 -0700169 /* Enable the Global Interrupt Enable */
Michal Simek123c1402013-05-30 00:28:06 +0000170 __raw_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700171}
172
173/**
174 * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
175 * @drvdata: Pointer to the Emaclite device private data
176 *
177 * This function disables the Tx and Rx interrupts for the Emaclite device,
178 * along with the Global Interrupt Enable.
179 */
180static void xemaclite_disable_interrupts(struct net_local *drvdata)
181{
182 u32 reg_data;
183
184 /* Disable the Global Interrupt Enable */
Michal Simek123c1402013-05-30 00:28:06 +0000185 __raw_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700186
187 /* Disable the Tx interrupts for the first buffer */
Michal Simek123c1402013-05-30 00:28:06 +0000188 reg_data = __raw_readl(drvdata->base_addr + XEL_TSR_OFFSET);
189 __raw_writel(reg_data & (~XEL_TSR_XMIT_IE_MASK),
190 drvdata->base_addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700191
John Linnbb81b2d2009-08-20 02:52:16 -0700192 /* Disable the Rx interrupts for the first buffer */
Michal Simek123c1402013-05-30 00:28:06 +0000193 reg_data = __raw_readl(drvdata->base_addr + XEL_RSR_OFFSET);
194 __raw_writel(reg_data & (~XEL_RSR_RECV_IE_MASK),
195 drvdata->base_addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700196}
197
198/**
199 * xemaclite_aligned_write - Write from 16-bit aligned to 32-bit aligned address
200 * @src_ptr: Void pointer to the 16-bit aligned source address
201 * @dest_ptr: Pointer to the 32-bit aligned destination address
202 * @length: Number bytes to write from source to destination
203 *
204 * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
205 * address in the EmacLite device.
206 */
207static void xemaclite_aligned_write(void *src_ptr, u32 *dest_ptr,
208 unsigned length)
209{
210 u32 align_buffer;
211 u32 *to_u32_ptr;
212 u16 *from_u16_ptr, *to_u16_ptr;
213
214 to_u32_ptr = dest_ptr;
Joe Perches43d620c2011-06-16 19:08:06 +0000215 from_u16_ptr = src_ptr;
John Linnbb81b2d2009-08-20 02:52:16 -0700216 align_buffer = 0;
217
218 for (; length > 3; length -= 4) {
Joe Perches43d620c2011-06-16 19:08:06 +0000219 to_u16_ptr = (u16 *)&align_buffer;
John Linnbb81b2d2009-08-20 02:52:16 -0700220 *to_u16_ptr++ = *from_u16_ptr++;
221 *to_u16_ptr++ = *from_u16_ptr++;
222
Srikanth Thokalaec21b6b2013-12-07 13:40:49 +0530223 /* This barrier resolves occasional issues seen around
224 * cases where the data is not properly flushed out
225 * from the processor store buffers to the destination
226 * memory locations.
227 */
228 wmb();
229
John Linnbb81b2d2009-08-20 02:52:16 -0700230 /* Output a word */
231 *to_u32_ptr++ = align_buffer;
232 }
233 if (length) {
234 u8 *from_u8_ptr, *to_u8_ptr;
235
236 /* Set up to output the remaining data */
237 align_buffer = 0;
238 to_u8_ptr = (u8 *) &align_buffer;
239 from_u8_ptr = (u8 *) from_u16_ptr;
240
241 /* Output the remaining data */
242 for (; length > 0; length--)
243 *to_u8_ptr++ = *from_u8_ptr++;
244
Srikanth Thokalaec21b6b2013-12-07 13:40:49 +0530245 /* This barrier resolves occasional issues seen around
246 * cases where the data is not properly flushed out
247 * from the processor store buffers to the destination
248 * memory locations.
249 */
250 wmb();
John Linnbb81b2d2009-08-20 02:52:16 -0700251 *to_u32_ptr = align_buffer;
252 }
253}
254
255/**
256 * xemaclite_aligned_read - Read from 32-bit aligned to 16-bit aligned buffer
257 * @src_ptr: Pointer to the 32-bit aligned source address
258 * @dest_ptr: Pointer to the 16-bit aligned destination address
259 * @length: Number bytes to read from source to destination
260 *
261 * This function reads data from a 32-bit aligned address in the EmacLite device
262 * to a 16-bit aligned buffer.
263 */
264static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
265 unsigned length)
266{
267 u16 *to_u16_ptr, *from_u16_ptr;
268 u32 *from_u32_ptr;
269 u32 align_buffer;
270
271 from_u32_ptr = src_ptr;
272 to_u16_ptr = (u16 *) dest_ptr;
273
274 for (; length > 3; length -= 4) {
275 /* Copy each word into the temporary buffer */
276 align_buffer = *from_u32_ptr++;
277 from_u16_ptr = (u16 *)&align_buffer;
278
279 /* Read data from source */
280 *to_u16_ptr++ = *from_u16_ptr++;
281 *to_u16_ptr++ = *from_u16_ptr++;
282 }
283
284 if (length) {
285 u8 *to_u8_ptr, *from_u8_ptr;
286
287 /* Set up to read the remaining data */
288 to_u8_ptr = (u8 *) to_u16_ptr;
289 align_buffer = *from_u32_ptr++;
290 from_u8_ptr = (u8 *) &align_buffer;
291
292 /* Read the remaining data */
293 for (; length > 0; length--)
294 *to_u8_ptr = *from_u8_ptr;
295 }
296}
297
298/**
299 * xemaclite_send_data - Send an Ethernet frame
300 * @drvdata: Pointer to the Emaclite device private data
301 * @data: Pointer to the data to be sent
302 * @byte_count: Total frame size, including header
303 *
304 * This function checks if the Tx buffer of the Emaclite device is free to send
305 * data. If so, it fills the Tx buffer with data for transmission. Otherwise, it
306 * returns an error.
307 *
308 * Return: 0 upon success or -1 if the buffer(s) are full.
309 *
310 * Note: The maximum Tx packet size can not be more than Ethernet header
311 * (14 Bytes) + Maximum MTU (1500 bytes). This is excluding FCS.
312 */
313static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
314 unsigned int byte_count)
315{
316 u32 reg_data;
317 void __iomem *addr;
318
319 /* Determine the expected Tx buffer address */
320 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
321
322 /* If the length is too large, truncate it */
323 if (byte_count > ETH_FRAME_LEN)
324 byte_count = ETH_FRAME_LEN;
325
326 /* Check if the expected buffer is available */
Michal Simek123c1402013-05-30 00:28:06 +0000327 reg_data = __raw_readl(addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700328 if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
329 XEL_TSR_XMIT_ACTIVE_MASK)) == 0) {
330
331 /* Switch to next buffer if configured */
332 if (drvdata->tx_ping_pong != 0)
333 drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET;
334 } else if (drvdata->tx_ping_pong != 0) {
335 /* If the expected buffer is full, try the other buffer,
336 * if it is configured in HW */
337
338 addr = (void __iomem __force *)((u32 __force)addr ^
339 XEL_BUFFER_OFFSET);
Michal Simek123c1402013-05-30 00:28:06 +0000340 reg_data = __raw_readl(addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700341
342 if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
343 XEL_TSR_XMIT_ACTIVE_MASK)) != 0)
344 return -1; /* Buffers were full, return failure */
345 } else
346 return -1; /* Buffer was full, return failure */
347
348 /* Write the frame to the buffer */
349 xemaclite_aligned_write(data, (u32 __force *) addr, byte_count);
350
Michal Simek123c1402013-05-30 00:28:06 +0000351 __raw_writel((byte_count & XEL_TPLR_LENGTH_MASK),
352 addr + XEL_TPLR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700353
354 /* Update the Tx Status Register to indicate that there is a
355 * frame to send. Set the XEL_TSR_XMIT_ACTIVE_MASK flag which
356 * is used by the interrupt handler to check whether a frame
357 * has been transmitted */
Michal Simek123c1402013-05-30 00:28:06 +0000358 reg_data = __raw_readl(addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700359 reg_data |= (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_XMIT_ACTIVE_MASK);
Michal Simek123c1402013-05-30 00:28:06 +0000360 __raw_writel(reg_data, addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700361
362 return 0;
363}
364
365/**
366 * xemaclite_recv_data - Receive a frame
367 * @drvdata: Pointer to the Emaclite device private data
368 * @data: Address where the data is to be received
369 *
370 * This function is intended to be called from the interrupt context or
371 * with a wrapper which waits for the receive frame to be available.
372 *
373 * Return: Total number of bytes received
374 */
375static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data)
376{
377 void __iomem *addr;
378 u16 length, proto_type;
379 u32 reg_data;
380
381 /* Determine the expected buffer address */
382 addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use);
383
384 /* Verify which buffer has valid data */
Michal Simek123c1402013-05-30 00:28:06 +0000385 reg_data = __raw_readl(addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700386
387 if ((reg_data & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
388 if (drvdata->rx_ping_pong != 0)
389 drvdata->next_rx_buf_to_use ^= XEL_BUFFER_OFFSET;
390 } else {
391 /* The instance is out of sync, try other buffer if other
392 * buffer is configured, return 0 otherwise. If the instance is
393 * out of sync, do not update the 'next_rx_buf_to_use' since it
394 * will correct on subsequent calls */
395 if (drvdata->rx_ping_pong != 0)
396 addr = (void __iomem __force *)((u32 __force)addr ^
397 XEL_BUFFER_OFFSET);
398 else
399 return 0; /* No data was available */
400
401 /* Verify that buffer has valid data */
Michal Simek123c1402013-05-30 00:28:06 +0000402 reg_data = __raw_readl(addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700403 if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
404 XEL_RSR_RECV_DONE_MASK)
405 return 0; /* No data was available */
406 }
407
408 /* Get the protocol type of the ethernet frame that arrived */
Michal Simek123c1402013-05-30 00:28:06 +0000409 proto_type = ((ntohl(__raw_readl(addr + XEL_HEADER_OFFSET +
Michal Simek44180a52010-09-10 13:22:35 +0200410 XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
John Linnbb81b2d2009-08-20 02:52:16 -0700411 XEL_RPLR_LENGTH_MASK);
412
413 /* Check if received ethernet frame is a raw ethernet frame
414 * or an IP packet or an ARP packet */
415 if (proto_type > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
416
417 if (proto_type == ETH_P_IP) {
Michal Simek123c1402013-05-30 00:28:06 +0000418 length = ((ntohl(__raw_readl(addr +
John Linnbb81b2d2009-08-20 02:52:16 -0700419 XEL_HEADER_IP_LENGTH_OFFSET +
Michal Simek44180a52010-09-10 13:22:35 +0200420 XEL_RXBUFF_OFFSET)) >>
John Linnbb81b2d2009-08-20 02:52:16 -0700421 XEL_HEADER_SHIFT) &
422 XEL_RPLR_LENGTH_MASK);
423 length += ETH_HLEN + ETH_FCS_LEN;
424
425 } else if (proto_type == ETH_P_ARP)
426 length = XEL_ARP_PACKET_SIZE + ETH_HLEN + ETH_FCS_LEN;
427 else
428 /* Field contains type other than IP or ARP, use max
429 * frame size and let user parse it */
430 length = ETH_FRAME_LEN + ETH_FCS_LEN;
431 } else
432 /* Use the length in the frame, plus the header and trailer */
433 length = proto_type + ETH_HLEN + ETH_FCS_LEN;
434
435 /* Read from the EmacLite device */
436 xemaclite_aligned_read((u32 __force *) (addr + XEL_RXBUFF_OFFSET),
437 data, length);
438
439 /* Acknowledge the frame */
Michal Simek123c1402013-05-30 00:28:06 +0000440 reg_data = __raw_readl(addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700441 reg_data &= ~XEL_RSR_RECV_DONE_MASK;
Michal Simek123c1402013-05-30 00:28:06 +0000442 __raw_writel(reg_data, addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700443
444 return length;
445}
446
447/**
John Linn5cdaaa12010-02-15 21:51:00 -0800448 * xemaclite_update_address - Update the MAC address in the device
John Linnbb81b2d2009-08-20 02:52:16 -0700449 * @drvdata: Pointer to the Emaclite device private data
450 * @address_ptr:Pointer to the MAC address (MAC address is a 48-bit value)
451 *
452 * Tx must be idle and Rx should be idle for deterministic results.
453 * It is recommended that this function should be called after the
454 * initialization and before transmission of any packets from the device.
455 * The MAC address can be programmed using any of the two transmit
456 * buffers (if configured).
457 */
John Linn5cdaaa12010-02-15 21:51:00 -0800458static void xemaclite_update_address(struct net_local *drvdata,
459 u8 *address_ptr)
John Linnbb81b2d2009-08-20 02:52:16 -0700460{
461 void __iomem *addr;
462 u32 reg_data;
463
464 /* Determine the expected Tx buffer address */
465 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
466
467 xemaclite_aligned_write(address_ptr, (u32 __force *) addr, ETH_ALEN);
468
Michal Simek123c1402013-05-30 00:28:06 +0000469 __raw_writel(ETH_ALEN, addr + XEL_TPLR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700470
471 /* Update the MAC address in the EmacLite */
Michal Simek123c1402013-05-30 00:28:06 +0000472 reg_data = __raw_readl(addr + XEL_TSR_OFFSET);
473 __raw_writel(reg_data | XEL_TSR_PROG_MAC_ADDR, addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700474
475 /* Wait for EmacLite to finish with the MAC address update */
Michal Simek123c1402013-05-30 00:28:06 +0000476 while ((__raw_readl(addr + XEL_TSR_OFFSET) &
John Linnbb81b2d2009-08-20 02:52:16 -0700477 XEL_TSR_PROG_MAC_ADDR) != 0)
478 ;
479}
480
481/**
John Linn5cdaaa12010-02-15 21:51:00 -0800482 * xemaclite_set_mac_address - Set the MAC address for this device
483 * @dev: Pointer to the network device instance
484 * @addr: Void pointer to the sockaddr structure
485 *
486 * This function copies the HW address from the sockaddr strucutre to the
487 * net_device structure and updates the address in HW.
488 *
489 * Return: Error if the net device is busy or 0 if the addr is set
490 * successfully
491 */
492static int xemaclite_set_mac_address(struct net_device *dev, void *address)
493{
Joe Perchesece49152010-11-15 11:12:31 +0000494 struct net_local *lp = netdev_priv(dev);
John Linn5cdaaa12010-02-15 21:51:00 -0800495 struct sockaddr *addr = address;
496
497 if (netif_running(dev))
498 return -EBUSY;
499
500 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
501 xemaclite_update_address(lp, dev->dev_addr);
502 return 0;
503}
504
505/**
John Linnbb81b2d2009-08-20 02:52:16 -0700506 * xemaclite_tx_timeout - Callback for Tx Timeout
507 * @dev: Pointer to the network device
508 *
509 * This function is called when Tx time out occurs for Emaclite device.
510 */
511static void xemaclite_tx_timeout(struct net_device *dev)
512{
Joe Perchesece49152010-11-15 11:12:31 +0000513 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700514 unsigned long flags;
515
516 dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
517 TX_TIMEOUT * 1000UL / HZ);
518
519 dev->stats.tx_errors++;
520
521 /* Reset the device */
522 spin_lock_irqsave(&lp->reset_lock, flags);
523
524 /* Shouldn't really be necessary, but shouldn't hurt */
525 netif_stop_queue(dev);
526
527 xemaclite_disable_interrupts(lp);
528 xemaclite_enable_interrupts(lp);
529
530 if (lp->deferred_skb) {
531 dev_kfree_skb(lp->deferred_skb);
532 lp->deferred_skb = NULL;
533 dev->stats.tx_errors++;
534 }
535
536 /* To exclude tx timeout */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700537 dev->trans_start = jiffies; /* prevent tx timeout */
John Linnbb81b2d2009-08-20 02:52:16 -0700538
539 /* We're all ready to go. Start the queue */
540 netif_wake_queue(dev);
541 spin_unlock_irqrestore(&lp->reset_lock, flags);
542}
543
544/**********************/
545/* Interrupt Handlers */
546/**********************/
547
548/**
549 * xemaclite_tx_handler - Interrupt handler for frames sent
550 * @dev: Pointer to the network device
551 *
552 * This function updates the number of packets transmitted and handles the
553 * deferred skb, if there is one.
554 */
555static void xemaclite_tx_handler(struct net_device *dev)
556{
Joe Perchesece49152010-11-15 11:12:31 +0000557 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700558
559 dev->stats.tx_packets++;
560 if (lp->deferred_skb) {
561 if (xemaclite_send_data(lp,
562 (u8 *) lp->deferred_skb->data,
563 lp->deferred_skb->len) != 0)
564 return;
565 else {
566 dev->stats.tx_bytes += lp->deferred_skb->len;
567 dev_kfree_skb_irq(lp->deferred_skb);
568 lp->deferred_skb = NULL;
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700569 dev->trans_start = jiffies; /* prevent tx timeout */
John Linnbb81b2d2009-08-20 02:52:16 -0700570 netif_wake_queue(dev);
571 }
572 }
573}
574
575/**
576 * xemaclite_rx_handler- Interrupt handler for frames received
577 * @dev: Pointer to the network device
578 *
579 * This function allocates memory for a socket buffer, fills it with data
580 * received and hands it over to the TCP/IP stack.
581 */
582static void xemaclite_rx_handler(struct net_device *dev)
583{
Joe Perchesece49152010-11-15 11:12:31 +0000584 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700585 struct sk_buff *skb;
586 unsigned int align;
587 u32 len;
588
589 len = ETH_FRAME_LEN + ETH_FCS_LEN;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000590 skb = netdev_alloc_skb(dev, len + ALIGNMENT);
John Linnbb81b2d2009-08-20 02:52:16 -0700591 if (!skb) {
592 /* Couldn't get memory. */
593 dev->stats.rx_dropped++;
594 dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n");
595 return;
596 }
597
598 /*
599 * A new skb should have the data halfword aligned, but this code is
600 * here just in case that isn't true. Calculate how many
601 * bytes we should reserve to get the data to start on a word
602 * boundary */
603 align = BUFFER_ALIGN(skb->data);
604 if (align)
605 skb_reserve(skb, align);
606
607 skb_reserve(skb, 2);
608
609 len = xemaclite_recv_data(lp, (u8 *) skb->data);
610
611 if (!len) {
612 dev->stats.rx_errors++;
613 dev_kfree_skb_irq(skb);
614 return;
615 }
616
617 skb_put(skb, len); /* Tell the skb how much data we got */
John Linnbb81b2d2009-08-20 02:52:16 -0700618
619 skb->protocol = eth_type_trans(skb, dev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700620 skb_checksum_none_assert(skb);
John Linnbb81b2d2009-08-20 02:52:16 -0700621
622 dev->stats.rx_packets++;
623 dev->stats.rx_bytes += len;
John Linnbb81b2d2009-08-20 02:52:16 -0700624
Richard Cochran570773c2011-06-19 21:51:25 +0000625 if (!skb_defer_rx_timestamp(skb))
626 netif_rx(skb); /* Send the packet upstream */
John Linnbb81b2d2009-08-20 02:52:16 -0700627}
628
629/**
630 * xemaclite_interrupt - Interrupt handler for this driver
631 * @irq: Irq of the Emaclite device
632 * @dev_id: Void pointer to the network device instance used as callback
633 * reference
634 *
635 * This function handles the Tx and Rx interrupts of the EmacLite device.
636 */
637static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
638{
Rusty Russell3db1cd52011-12-19 13:56:45 +0000639 bool tx_complete = false;
John Linnbb81b2d2009-08-20 02:52:16 -0700640 struct net_device *dev = dev_id;
Joe Perchesece49152010-11-15 11:12:31 +0000641 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700642 void __iomem *base_addr = lp->base_addr;
643 u32 tx_status;
644
645 /* Check if there is Rx Data available */
Michal Simek123c1402013-05-30 00:28:06 +0000646 if ((__raw_readl(base_addr + XEL_RSR_OFFSET) &
647 XEL_RSR_RECV_DONE_MASK) ||
648 (__raw_readl(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
John Linnbb81b2d2009-08-20 02:52:16 -0700649 & XEL_RSR_RECV_DONE_MASK))
650
651 xemaclite_rx_handler(dev);
652
653 /* Check if the Transmission for the first buffer is completed */
Michal Simek123c1402013-05-30 00:28:06 +0000654 tx_status = __raw_readl(base_addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700655 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
656 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
657
658 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
Michal Simek123c1402013-05-30 00:28:06 +0000659 __raw_writel(tx_status, base_addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700660
Rusty Russell3db1cd52011-12-19 13:56:45 +0000661 tx_complete = true;
John Linnbb81b2d2009-08-20 02:52:16 -0700662 }
663
664 /* Check if the Transmission for the second buffer is completed */
Michal Simek123c1402013-05-30 00:28:06 +0000665 tx_status = __raw_readl(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700666 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
667 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
668
669 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
Michal Simek123c1402013-05-30 00:28:06 +0000670 __raw_writel(tx_status, base_addr + XEL_BUFFER_OFFSET +
671 XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700672
Rusty Russell3db1cd52011-12-19 13:56:45 +0000673 tx_complete = true;
John Linnbb81b2d2009-08-20 02:52:16 -0700674 }
675
676 /* If there was a Tx interrupt, call the Tx Handler */
677 if (tx_complete != 0)
678 xemaclite_tx_handler(dev);
679
680 return IRQ_HANDLED;
681}
682
John Linn5cdaaa12010-02-15 21:51:00 -0800683/**********************/
684/* MDIO Bus functions */
685/**********************/
686
687/**
688 * xemaclite_mdio_wait - Wait for the MDIO to be ready to use
689 * @lp: Pointer to the Emaclite device private data
690 *
691 * This function waits till the device is ready to accept a new MDIO
692 * request.
693 *
694 * Return: 0 for success or ETIMEDOUT for a timeout
695 */
696
697static int xemaclite_mdio_wait(struct net_local *lp)
698{
699 long end = jiffies + 2;
700
701 /* wait for the MDIO interface to not be busy or timeout
702 after some time.
703 */
Michal Simek123c1402013-05-30 00:28:06 +0000704 while (__raw_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET) &
John Linn5cdaaa12010-02-15 21:51:00 -0800705 XEL_MDIOCTRL_MDIOSTS_MASK) {
706 if (end - jiffies <= 0) {
707 WARN_ON(1);
708 return -ETIMEDOUT;
709 }
710 msleep(1);
711 }
712 return 0;
713}
714
715/**
716 * xemaclite_mdio_read - Read from a given MII management register
717 * @bus: the mii_bus struct
718 * @phy_id: the phy address
719 * @reg: register number to read from
720 *
721 * This function waits till the device is ready to accept a new MDIO
722 * request and then writes the phy address to the MDIO Address register
723 * and reads data from MDIO Read Data register, when its available.
724 *
725 * Return: Value read from the MII management register
726 */
727static int xemaclite_mdio_read(struct mii_bus *bus, int phy_id, int reg)
728{
729 struct net_local *lp = bus->priv;
730 u32 ctrl_reg;
731 u32 rc;
732
733 if (xemaclite_mdio_wait(lp))
734 return -ETIMEDOUT;
735
736 /* Write the PHY address, register number and set the OP bit in the
737 * MDIO Address register. Set the Status bit in the MDIO Control
738 * register to start a MDIO read transaction.
739 */
Michal Simek123c1402013-05-30 00:28:06 +0000740 ctrl_reg = __raw_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
741 __raw_writel(XEL_MDIOADDR_OP_MASK |
742 ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
743 lp->base_addr + XEL_MDIOADDR_OFFSET);
744 __raw_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
745 lp->base_addr + XEL_MDIOCTRL_OFFSET);
John Linn5cdaaa12010-02-15 21:51:00 -0800746
747 if (xemaclite_mdio_wait(lp))
748 return -ETIMEDOUT;
749
Michal Simek123c1402013-05-30 00:28:06 +0000750 rc = __raw_readl(lp->base_addr + XEL_MDIORD_OFFSET);
John Linn5cdaaa12010-02-15 21:51:00 -0800751
752 dev_dbg(&lp->ndev->dev,
753 "xemaclite_mdio_read(phy_id=%i, reg=%x) == %x\n",
754 phy_id, reg, rc);
755
756 return rc;
757}
758
759/**
760 * xemaclite_mdio_write - Write to a given MII management register
761 * @bus: the mii_bus struct
762 * @phy_id: the phy address
763 * @reg: register number to write to
764 * @val: value to write to the register number specified by reg
765 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300766 * This function waits till the device is ready to accept a new MDIO
John Linn5cdaaa12010-02-15 21:51:00 -0800767 * request and then writes the val to the MDIO Write Data register.
768 */
769static int xemaclite_mdio_write(struct mii_bus *bus, int phy_id, int reg,
770 u16 val)
771{
772 struct net_local *lp = bus->priv;
773 u32 ctrl_reg;
774
775 dev_dbg(&lp->ndev->dev,
776 "xemaclite_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
777 phy_id, reg, val);
778
779 if (xemaclite_mdio_wait(lp))
780 return -ETIMEDOUT;
781
782 /* Write the PHY address, register number and clear the OP bit in the
783 * MDIO Address register and then write the value into the MDIO Write
784 * Data register. Finally, set the Status bit in the MDIO Control
785 * register to start a MDIO write transaction.
786 */
Michal Simek123c1402013-05-30 00:28:06 +0000787 ctrl_reg = __raw_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
788 __raw_writel(~XEL_MDIOADDR_OP_MASK &
789 ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
790 lp->base_addr + XEL_MDIOADDR_OFFSET);
791 __raw_writel(val, lp->base_addr + XEL_MDIOWR_OFFSET);
792 __raw_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
793 lp->base_addr + XEL_MDIOCTRL_OFFSET);
John Linn5cdaaa12010-02-15 21:51:00 -0800794
795 return 0;
796}
797
798/**
799 * xemaclite_mdio_reset - Reset the mdio bus.
800 * @bus: Pointer to the MII bus
801 *
802 * This function is required(?) as per Documentation/networking/phy.txt.
803 * There is no reset in this device; this function always returns 0.
804 */
805static int xemaclite_mdio_reset(struct mii_bus *bus)
806{
807 return 0;
808}
809
810/**
811 * xemaclite_mdio_setup - Register mii_bus for the Emaclite device
812 * @lp: Pointer to the Emaclite device private data
813 * @ofdev: Pointer to OF device structure
814 *
815 * This function enables MDIO bus in the Emaclite device and registers a
816 * mii_bus.
817 *
818 * Return: 0 upon success or a negative error upon failure
819 */
820static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
821{
822 struct mii_bus *bus;
823 int rc;
824 struct resource res;
825 struct device_node *np = of_get_parent(lp->phy_node);
Michal Simeke0a3bc62013-05-30 00:28:04 +0000826 struct device_node *npp;
John Linn5cdaaa12010-02-15 21:51:00 -0800827
828 /* Don't register the MDIO bus if the phy_node or its parent node
829 * can't be found.
830 */
Michal Simekccfecdf2013-05-30 00:28:03 +0000831 if (!np) {
832 dev_err(dev, "Failed to register mdio bus.\n");
John Linn5cdaaa12010-02-15 21:51:00 -0800833 return -ENODEV;
Michal Simekccfecdf2013-05-30 00:28:03 +0000834 }
Michal Simeke0a3bc62013-05-30 00:28:04 +0000835 npp = of_get_parent(np);
836
837 of_address_to_resource(npp, 0, &res);
838 if (lp->ndev->mem_start != res.start) {
839 struct phy_device *phydev;
840 phydev = of_phy_find_device(lp->phy_node);
841 if (!phydev)
842 dev_info(dev,
843 "MDIO of the phy is not registered yet\n");
844 return 0;
845 }
John Linn5cdaaa12010-02-15 21:51:00 -0800846
847 /* Enable the MDIO bus by asserting the enable bit in MDIO Control
848 * register.
849 */
Michal Simek123c1402013-05-30 00:28:06 +0000850 __raw_writel(XEL_MDIOCTRL_MDIOEN_MASK,
851 lp->base_addr + XEL_MDIOCTRL_OFFSET);
John Linn5cdaaa12010-02-15 21:51:00 -0800852
853 bus = mdiobus_alloc();
Michal Simekccfecdf2013-05-30 00:28:03 +0000854 if (!bus) {
Jens Renner \(EFE\)f1362e32013-06-02 05:19:06 +0000855 dev_err(dev, "Failed to allocate mdiobus\n");
John Linn5cdaaa12010-02-15 21:51:00 -0800856 return -ENOMEM;
Michal Simekccfecdf2013-05-30 00:28:03 +0000857 }
John Linn5cdaaa12010-02-15 21:51:00 -0800858
John Linn5cdaaa12010-02-15 21:51:00 -0800859 snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
860 (unsigned long long)res.start);
861 bus->priv = lp;
862 bus->name = "Xilinx Emaclite MDIO";
863 bus->read = xemaclite_mdio_read;
864 bus->write = xemaclite_mdio_write;
865 bus->reset = xemaclite_mdio_reset;
866 bus->parent = dev;
867 bus->irq = lp->mdio_irqs; /* preallocated IRQ table */
868
869 lp->mii_bus = bus;
870
871 rc = of_mdiobus_register(bus, np);
Michal Simekccfecdf2013-05-30 00:28:03 +0000872 if (rc) {
873 dev_err(dev, "Failed to register mdio bus.\n");
John Linn5cdaaa12010-02-15 21:51:00 -0800874 goto err_register;
Michal Simekccfecdf2013-05-30 00:28:03 +0000875 }
John Linn5cdaaa12010-02-15 21:51:00 -0800876
877 return 0;
878
879err_register:
880 mdiobus_free(bus);
881 return rc;
882}
883
884/**
885 * xemaclite_adjust_link - Link state callback for the Emaclite device
886 * @ndev: pointer to net_device struct
887 *
888 * There's nothing in the Emaclite device to be configured when the link
889 * state changes. We just print the status.
890 */
Michal Simek3fb99fa2013-05-30 00:28:05 +0000891static void xemaclite_adjust_link(struct net_device *ndev)
John Linn5cdaaa12010-02-15 21:51:00 -0800892{
893 struct net_local *lp = netdev_priv(ndev);
894 struct phy_device *phy = lp->phy_dev;
895 int link_state;
896
897 /* hash together the state values to decide if something has changed */
898 link_state = phy->speed | (phy->duplex << 1) | phy->link;
899
900 if (lp->last_link != link_state) {
901 lp->last_link = link_state;
902 phy_print_status(phy);
903 }
904}
905
John Linnbb81b2d2009-08-20 02:52:16 -0700906/**
907 * xemaclite_open - Open the network device
908 * @dev: Pointer to the network device
909 *
910 * This function sets the MAC address, requests an IRQ and enables interrupts
911 * for the Emaclite device and starts the Tx queue.
John Linn5cdaaa12010-02-15 21:51:00 -0800912 * It also connects to the phy device, if MDIO is included in Emaclite device.
John Linnbb81b2d2009-08-20 02:52:16 -0700913 */
914static int xemaclite_open(struct net_device *dev)
915{
Joe Perchesece49152010-11-15 11:12:31 +0000916 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700917 int retval;
918
919 /* Just to be safe, stop the device first */
920 xemaclite_disable_interrupts(lp);
921
John Linn5cdaaa12010-02-15 21:51:00 -0800922 if (lp->phy_node) {
923 u32 bmcr;
924
925 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
926 xemaclite_adjust_link, 0,
927 PHY_INTERFACE_MODE_MII);
928 if (!lp->phy_dev) {
929 dev_err(&lp->ndev->dev, "of_phy_connect() failed\n");
930 return -ENODEV;
931 }
932
933 /* EmacLite doesn't support giga-bit speeds */
934 lp->phy_dev->supported &= (PHY_BASIC_FEATURES);
935 lp->phy_dev->advertising = lp->phy_dev->supported;
936
937 /* Don't advertise 1000BASE-T Full/Half duplex speeds */
938 phy_write(lp->phy_dev, MII_CTRL1000, 0);
939
940 /* Advertise only 10 and 100mbps full/half duplex speeds */
Jens Renner \(EFE\)3a5395b2013-06-03 04:32:52 +0000941 phy_write(lp->phy_dev, MII_ADVERTISE, ADVERTISE_ALL |
942 ADVERTISE_CSMA);
John Linn5cdaaa12010-02-15 21:51:00 -0800943
944 /* Restart auto negotiation */
945 bmcr = phy_read(lp->phy_dev, MII_BMCR);
946 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
947 phy_write(lp->phy_dev, MII_BMCR, bmcr);
948
949 phy_start(lp->phy_dev);
950 }
951
John Linnbb81b2d2009-08-20 02:52:16 -0700952 /* Set the MAC address each time opened */
John Linn5cdaaa12010-02-15 21:51:00 -0800953 xemaclite_update_address(lp, dev->dev_addr);
John Linnbb81b2d2009-08-20 02:52:16 -0700954
955 /* Grab the IRQ */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800956 retval = request_irq(dev->irq, xemaclite_interrupt, 0, dev->name, dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700957 if (retval) {
958 dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
959 dev->irq);
John Linn5cdaaa12010-02-15 21:51:00 -0800960 if (lp->phy_dev)
961 phy_disconnect(lp->phy_dev);
962 lp->phy_dev = NULL;
963
John Linnbb81b2d2009-08-20 02:52:16 -0700964 return retval;
965 }
966
967 /* Enable Interrupts */
968 xemaclite_enable_interrupts(lp);
969
970 /* We're ready to go */
971 netif_start_queue(dev);
972
973 return 0;
974}
975
976/**
977 * xemaclite_close - Close the network device
978 * @dev: Pointer to the network device
979 *
980 * This function stops the Tx queue, disables interrupts and frees the IRQ for
981 * the Emaclite device.
John Linn5cdaaa12010-02-15 21:51:00 -0800982 * It also disconnects the phy device associated with the Emaclite device.
John Linnbb81b2d2009-08-20 02:52:16 -0700983 */
984static int xemaclite_close(struct net_device *dev)
985{
Joe Perchesece49152010-11-15 11:12:31 +0000986 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700987
988 netif_stop_queue(dev);
989 xemaclite_disable_interrupts(lp);
990 free_irq(dev->irq, dev);
991
John Linn5cdaaa12010-02-15 21:51:00 -0800992 if (lp->phy_dev)
993 phy_disconnect(lp->phy_dev);
994 lp->phy_dev = NULL;
995
John Linnbb81b2d2009-08-20 02:52:16 -0700996 return 0;
997}
998
999/**
John Linnbb81b2d2009-08-20 02:52:16 -07001000 * xemaclite_send - Transmit a frame
1001 * @orig_skb: Pointer to the socket buffer to be transmitted
1002 * @dev: Pointer to the network device
1003 *
1004 * This function checks if the Tx buffer of the Emaclite device is free to send
1005 * data. If so, it fills the Tx buffer with data from socket buffer data,
1006 * updates the stats and frees the socket buffer. The Tx completion is signaled
1007 * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
1008 * deferred and the Tx queue is stopped so that the deferred socket buffer can
1009 * be transmitted when the Emaclite device is free to transmit data.
1010 *
1011 * Return: 0, always.
1012 */
1013static int xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
1014{
Joe Perchesece49152010-11-15 11:12:31 +00001015 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -07001016 struct sk_buff *new_skb;
1017 unsigned int len;
1018 unsigned long flags;
1019
1020 len = orig_skb->len;
1021
1022 new_skb = orig_skb;
1023
1024 spin_lock_irqsave(&lp->reset_lock, flags);
1025 if (xemaclite_send_data(lp, (u8 *) new_skb->data, len) != 0) {
1026 /* If the Emaclite Tx buffer is busy, stop the Tx queue and
Richard Cochran570773c2011-06-19 21:51:25 +00001027 * defer the skb for transmission during the ISR, after the
John Linnbb81b2d2009-08-20 02:52:16 -07001028 * current transmission is complete */
1029 netif_stop_queue(dev);
1030 lp->deferred_skb = new_skb;
Richard Cochran570773c2011-06-19 21:51:25 +00001031 /* Take the time stamp now, since we can't do this in an ISR. */
1032 skb_tx_timestamp(new_skb);
John Linnbb81b2d2009-08-20 02:52:16 -07001033 spin_unlock_irqrestore(&lp->reset_lock, flags);
1034 return 0;
1035 }
1036 spin_unlock_irqrestore(&lp->reset_lock, flags);
1037
Richard Cochran570773c2011-06-19 21:51:25 +00001038 skb_tx_timestamp(new_skb);
1039
John Linnbb81b2d2009-08-20 02:52:16 -07001040 dev->stats.tx_bytes += len;
1041 dev_kfree_skb(new_skb);
John Linnbb81b2d2009-08-20 02:52:16 -07001042
1043 return 0;
1044}
1045
1046/**
John Linnbb81b2d2009-08-20 02:52:16 -07001047 * xemaclite_remove_ndev - Free the network device
1048 * @ndev: Pointer to the network device to be freed
1049 *
1050 * This function un maps the IO region of the Emaclite device and frees the net
1051 * device.
1052 */
Michal Simek37c67c62013-09-12 09:05:10 +02001053static void xemaclite_remove_ndev(struct net_device *ndev)
John Linnbb81b2d2009-08-20 02:52:16 -07001054{
1055 if (ndev) {
John Linnbb81b2d2009-08-20 02:52:16 -07001056 free_netdev(ndev);
1057 }
1058}
1059
1060/**
1061 * get_bool - Get a parameter from the OF device
1062 * @ofdev: Pointer to OF device structure
1063 * @s: Property to be retrieved
1064 *
1065 * This function looks for a property in the device node and returns the value
1066 * of the property if its found or 0 if the property is not found.
1067 *
1068 * Return: Value of the parameter if the parameter is found, or 0 otherwise
1069 */
Grant Likely2dc11582010-08-06 09:25:50 -06001070static bool get_bool(struct platform_device *ofdev, const char *s)
John Linnbb81b2d2009-08-20 02:52:16 -07001071{
Grant Likely61c7a082010-04-13 16:12:29 -07001072 u32 *p = (u32 *)of_get_property(ofdev->dev.of_node, s, NULL);
John Linnbb81b2d2009-08-20 02:52:16 -07001073
1074 if (p) {
1075 return (bool)*p;
1076 } else {
1077 dev_warn(&ofdev->dev, "Parameter %s not found,"
1078 "defaulting to false\n", s);
1079 return 0;
1080 }
1081}
1082
1083static struct net_device_ops xemaclite_netdev_ops;
1084
1085/**
1086 * xemaclite_of_probe - Probe method for the Emaclite device.
1087 * @ofdev: Pointer to OF device structure
1088 * @match: Pointer to the structure used for matching a device
1089 *
1090 * This function probes for the Emaclite device in the device tree.
1091 * It initializes the driver data structure and the hardware, sets the MAC
1092 * address and registers the network device.
John Linn5cdaaa12010-02-15 21:51:00 -08001093 * It also registers a mii_bus for the Emaclite device, if MDIO is included
1094 * in the device.
John Linnbb81b2d2009-08-20 02:52:16 -07001095 *
1096 * Return: 0, if the driver is bound to the Emaclite device, or
1097 * a negative error if there is failure.
1098 */
Bill Pemberton06b0e682012-12-03 09:24:08 -05001099static int xemaclite_of_probe(struct platform_device *ofdev)
John Linnbb81b2d2009-08-20 02:52:16 -07001100{
Michal Simek7a3e2582013-06-04 00:03:27 +00001101 struct resource *res;
John Linnbb81b2d2009-08-20 02:52:16 -07001102 struct net_device *ndev = NULL;
1103 struct net_local *lp = NULL;
1104 struct device *dev = &ofdev->dev;
1105 const void *mac_address;
1106
1107 int rc = 0;
1108
1109 dev_info(dev, "Device Tree Probing\n");
1110
John Linnbb81b2d2009-08-20 02:52:16 -07001111 /* Create an ethernet device instance */
1112 ndev = alloc_etherdev(sizeof(struct net_local));
Joe Perches41de8d42012-01-29 13:47:52 +00001113 if (!ndev)
John Linnbb81b2d2009-08-20 02:52:16 -07001114 return -ENOMEM;
John Linnbb81b2d2009-08-20 02:52:16 -07001115
1116 dev_set_drvdata(dev, ndev);
John Linn5cdaaa12010-02-15 21:51:00 -08001117 SET_NETDEV_DEV(ndev, &ofdev->dev);
John Linnbb81b2d2009-08-20 02:52:16 -07001118
John Linnbb81b2d2009-08-20 02:52:16 -07001119 lp = netdev_priv(ndev);
1120 lp->ndev = ndev;
1121
Michal Simek7a3e2582013-06-04 00:03:27 +00001122 /* Get IRQ for the device */
1123 res = platform_get_resource(ofdev, IORESOURCE_IRQ, 0);
1124 if (!res) {
1125 dev_err(dev, "no IRQ found\n");
1126 goto error;
John Linnbb81b2d2009-08-20 02:52:16 -07001127 }
1128
Michal Simek7a3e2582013-06-04 00:03:27 +00001129 ndev->irq = res->start;
1130
1131 res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
Tushar Beheraeed5d292013-06-10 17:05:06 +05301132 lp->base_addr = devm_ioremap_resource(&ofdev->dev, res);
1133 if (IS_ERR(lp->base_addr)) {
1134 rc = PTR_ERR(lp->base_addr);
Michal Simek7a3e2582013-06-04 00:03:27 +00001135 goto error;
Tushar Beheraeed5d292013-06-10 17:05:06 +05301136 }
Michal Simek7a3e2582013-06-04 00:03:27 +00001137
1138 ndev->mem_start = res->start;
1139 ndev->mem_end = res->end;
John Linnbb81b2d2009-08-20 02:52:16 -07001140
1141 spin_lock_init(&lp->reset_lock);
1142 lp->next_tx_buf_to_use = 0x0;
1143 lp->next_rx_buf_to_use = 0x0;
1144 lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
1145 lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
Grant Likely61c7a082010-04-13 16:12:29 -07001146 mac_address = of_get_mac_address(ofdev->dev.of_node);
John Linnbb81b2d2009-08-20 02:52:16 -07001147
1148 if (mac_address)
1149 /* Set the MAC address. */
Joe Perchesd458cdf2013-10-01 19:04:40 -07001150 memcpy(ndev->dev_addr, mac_address, ETH_ALEN);
John Linnbb81b2d2009-08-20 02:52:16 -07001151 else
1152 dev_warn(dev, "No MAC address found\n");
1153
1154 /* Clear the Tx CSR's in case this is a restart */
Michal Simek123c1402013-05-30 00:28:06 +00001155 __raw_writel(0, lp->base_addr + XEL_TSR_OFFSET);
1156 __raw_writel(0, lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -07001157
1158 /* Set the MAC address in the EmacLite device */
John Linn5cdaaa12010-02-15 21:51:00 -08001159 xemaclite_update_address(lp, ndev->dev_addr);
1160
Grant Likely61c7a082010-04-13 16:12:29 -07001161 lp->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
John Linn5cdaaa12010-02-15 21:51:00 -08001162 rc = xemaclite_mdio_setup(lp, &ofdev->dev);
1163 if (rc)
1164 dev_warn(&ofdev->dev, "error registering MDIO bus\n");
John Linnbb81b2d2009-08-20 02:52:16 -07001165
H Hartley Sweeten5491f3a2009-12-29 20:04:53 -08001166 dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
John Linnbb81b2d2009-08-20 02:52:16 -07001167
1168 ndev->netdev_ops = &xemaclite_netdev_ops;
1169 ndev->flags &= ~IFF_MULTICAST;
1170 ndev->watchdog_timeo = TX_TIMEOUT;
1171
1172 /* Finally, register the device */
1173 rc = register_netdev(ndev);
1174 if (rc) {
1175 dev_err(dev,
1176 "Cannot register network device, aborting\n");
Michal Simek7a3e2582013-06-04 00:03:27 +00001177 goto error;
John Linnbb81b2d2009-08-20 02:52:16 -07001178 }
1179
1180 dev_info(dev,
1181 "Xilinx EmacLite at 0x%08X mapped to 0x%08X, irq=%d\n",
1182 (unsigned int __force)ndev->mem_start,
1183 (unsigned int __force)lp->base_addr, ndev->irq);
1184 return 0;
1185
Michal Simek7a3e2582013-06-04 00:03:27 +00001186error:
Michal Simek37c67c62013-09-12 09:05:10 +02001187 xemaclite_remove_ndev(ndev);
John Linnbb81b2d2009-08-20 02:52:16 -07001188 return rc;
1189}
1190
1191/**
1192 * xemaclite_of_remove - Unbind the driver from the Emaclite device.
1193 * @of_dev: Pointer to OF device structure
1194 *
1195 * This function is called if a device is physically removed from the system or
1196 * if the driver module is being unloaded. It frees any resources allocated to
1197 * the device.
1198 *
1199 * Return: 0, always.
1200 */
Bill Pemberton06b0e682012-12-03 09:24:08 -05001201static int xemaclite_of_remove(struct platform_device *of_dev)
John Linnbb81b2d2009-08-20 02:52:16 -07001202{
Libo Chen34e01842013-08-19 20:00:25 +08001203 struct net_device *ndev = platform_get_drvdata(of_dev);
John Linnbb81b2d2009-08-20 02:52:16 -07001204
Joe Perchesece49152010-11-15 11:12:31 +00001205 struct net_local *lp = netdev_priv(ndev);
John Linn5cdaaa12010-02-15 21:51:00 -08001206
1207 /* Un-register the mii_bus, if configured */
1208 if (lp->has_mdio) {
1209 mdiobus_unregister(lp->mii_bus);
1210 kfree(lp->mii_bus->irq);
1211 mdiobus_free(lp->mii_bus);
1212 lp->mii_bus = NULL;
1213 }
1214
John Linnbb81b2d2009-08-20 02:52:16 -07001215 unregister_netdev(ndev);
1216
John Linn5cdaaa12010-02-15 21:51:00 -08001217 if (lp->phy_node)
1218 of_node_put(lp->phy_node);
1219 lp->phy_node = NULL;
1220
Michal Simek37c67c62013-09-12 09:05:10 +02001221 xemaclite_remove_ndev(ndev);
John Linnbb81b2d2009-08-20 02:52:16 -07001222
1223 return 0;
1224}
1225
Michal Simek357e8b5f2010-08-18 01:22:49 +00001226#ifdef CONFIG_NET_POLL_CONTROLLER
1227static void
1228xemaclite_poll_controller(struct net_device *ndev)
1229{
1230 disable_irq(ndev->irq);
1231 xemaclite_interrupt(ndev->irq, ndev);
1232 enable_irq(ndev->irq);
1233}
1234#endif
1235
John Linnbb81b2d2009-08-20 02:52:16 -07001236static struct net_device_ops xemaclite_netdev_ops = {
1237 .ndo_open = xemaclite_open,
1238 .ndo_stop = xemaclite_close,
1239 .ndo_start_xmit = xemaclite_send,
John Linn5cdaaa12010-02-15 21:51:00 -08001240 .ndo_set_mac_address = xemaclite_set_mac_address,
John Linnbb81b2d2009-08-20 02:52:16 -07001241 .ndo_tx_timeout = xemaclite_tx_timeout,
Michal Simek357e8b5f2010-08-18 01:22:49 +00001242#ifdef CONFIG_NET_POLL_CONTROLLER
1243 .ndo_poll_controller = xemaclite_poll_controller,
1244#endif
John Linnbb81b2d2009-08-20 02:52:16 -07001245};
1246
1247/* Match table for OF platform binding */
Bill Pemberton06b0e682012-12-03 09:24:08 -05001248static struct of_device_id xemaclite_of_match[] = {
John Linnbb81b2d2009-08-20 02:52:16 -07001249 { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
1250 { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
1251 { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
1252 { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
1253 { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
John Linn5cdaaa12010-02-15 21:51:00 -08001254 { .compatible = "xlnx,xps-ethernetlite-3.00.a", },
John Linnbb81b2d2009-08-20 02:52:16 -07001255 { /* end of list */ },
1256};
1257MODULE_DEVICE_TABLE(of, xemaclite_of_match);
1258
Grant Likely74888762011-02-22 21:05:51 -07001259static struct platform_driver xemaclite_of_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001260 .driver = {
1261 .name = DRIVER_NAME,
1262 .owner = THIS_MODULE,
1263 .of_match_table = xemaclite_of_match,
1264 },
John Linnbb81b2d2009-08-20 02:52:16 -07001265 .probe = xemaclite_of_probe,
Bill Pemberton06b0e682012-12-03 09:24:08 -05001266 .remove = xemaclite_of_remove,
John Linnbb81b2d2009-08-20 02:52:16 -07001267};
1268
Axel Lindb62f682011-11-27 16:44:17 +00001269module_platform_driver(xemaclite_of_driver);
John Linnbb81b2d2009-08-20 02:52:16 -07001270
1271MODULE_AUTHOR("Xilinx, Inc.");
1272MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1273MODULE_LICENSE("GPL");