blob: 2a0c06e0f730c35e5e7d40d9d9893bf36cf9c6fa [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>
John Linnbb81b2d2009-08-20 02:52:16 -070017#include <linux/netdevice.h>
18#include <linux/etherdevice.h>
19#include <linux/skbuff.h>
20#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Grant Likely22ae7822010-07-29 11:49:01 -060022#include <linux/of_address.h>
John Linnbb81b2d2009-08-20 02:52:16 -070023#include <linux/of_device.h>
24#include <linux/of_platform.h>
John Linn5cdaaa12010-02-15 21:51:00 -080025#include <linux/of_mdio.h>
David Daney4b6ba8a2010-10-26 15:07:13 -070026#include <linux/of_net.h>
John Linn5cdaaa12010-02-15 21:51:00 -080027#include <linux/phy.h>
Michal Simek075cd292011-06-09 01:29:13 -070028#include <linux/interrupt.h>
John Linnbb81b2d2009-08-20 02:52:16 -070029
30#define DRIVER_NAME "xilinx_emaclite"
31
32/* Register offsets for the EmacLite Core */
Michal Simekcd738c42013-09-12 09:05:11 +020033#define XEL_TXBUFF_OFFSET 0x0 /* Transmit Buffer */
John Linn5cdaaa12010-02-15 21:51:00 -080034#define XEL_MDIOADDR_OFFSET 0x07E4 /* MDIO Address Register */
35#define XEL_MDIOWR_OFFSET 0x07E8 /* MDIO Write Data Register */
36#define XEL_MDIORD_OFFSET 0x07EC /* MDIO Read Data Register */
37#define XEL_MDIOCTRL_OFFSET 0x07F0 /* MDIO Control Register */
John Linnbb81b2d2009-08-20 02:52:16 -070038#define XEL_GIER_OFFSET 0x07F8 /* GIE Register */
39#define XEL_TSR_OFFSET 0x07FC /* Tx status */
40#define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
41
42#define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
43#define XEL_RPLR_OFFSET 0x100C /* Rx packet length */
44#define XEL_RSR_OFFSET 0x17FC /* Rx status */
45
46#define XEL_BUFFER_OFFSET 0x0800 /* Next Tx/Rx buffer's offset */
47
John Linn5cdaaa12010-02-15 21:51:00 -080048/* MDIO Address Register Bit Masks */
49#define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */
50#define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */
51#define XEL_MDIOADDR_PHYADR_SHIFT 5
52#define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */
53
54/* MDIO Write Data Register Bit Masks */
55#define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */
56
57/* MDIO Read Data Register Bit Masks */
58#define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */
59
60/* MDIO Control Register Bit Masks */
61#define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */
62#define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */
63
John Linnbb81b2d2009-08-20 02:52:16 -070064/* Global Interrupt Enable Register (GIER) Bit Masks */
Michal Simekcd738c42013-09-12 09:05:11 +020065#define XEL_GIER_GIE_MASK 0x80000000 /* Global Enable */
John Linnbb81b2d2009-08-20 02:52:16 -070066
67/* Transmit Status Register (TSR) Bit Masks */
Michal Simekcd738c42013-09-12 09:05:11 +020068#define XEL_TSR_XMIT_BUSY_MASK 0x00000001 /* Tx complete */
69#define XEL_TSR_PROGRAM_MASK 0x00000002 /* Program the MAC address */
70#define XEL_TSR_XMIT_IE_MASK 0x00000008 /* Tx interrupt enable bit */
71#define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000 /* Buffer is active, SW bit
John Linnbb81b2d2009-08-20 02:52:16 -070072 * only. This is not documented
73 * in the HW spec */
74
75/* Define for programming the MAC address into the EmacLite */
76#define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
77
78/* Receive Status Register (RSR) */
Michal Simekcd738c42013-09-12 09:05:11 +020079#define XEL_RSR_RECV_DONE_MASK 0x00000001 /* Rx complete */
80#define XEL_RSR_RECV_IE_MASK 0x00000008 /* Rx interrupt enable bit */
John Linnbb81b2d2009-08-20 02:52:16 -070081
82/* Transmit Packet Length Register (TPLR) */
Michal Simekcd738c42013-09-12 09:05:11 +020083#define XEL_TPLR_LENGTH_MASK 0x0000FFFF /* Tx packet length */
John Linnbb81b2d2009-08-20 02:52:16 -070084
85/* Receive Packet Length Register (RPLR) */
Michal Simekcd738c42013-09-12 09:05:11 +020086#define XEL_RPLR_LENGTH_MASK 0x0000FFFF /* Rx packet length */
John Linnbb81b2d2009-08-20 02:52:16 -070087
Michal Simekcd738c42013-09-12 09:05:11 +020088#define XEL_HEADER_OFFSET 12 /* Offset to length field */
89#define XEL_HEADER_SHIFT 16 /* Shift value for length */
John Linnbb81b2d2009-08-20 02:52:16 -070090
91/* General Ethernet Definitions */
Michal Simekcd738c42013-09-12 09:05:11 +020092#define XEL_ARP_PACKET_SIZE 28 /* Max ARP packet size */
93#define XEL_HEADER_IP_LENGTH_OFFSET 16 /* IP Length Offset */
John Linnbb81b2d2009-08-20 02:52:16 -070094
95
96
97#define TX_TIMEOUT (60*HZ) /* Tx timeout is 60 seconds. */
98#define ALIGNMENT 4
99
100/* BUFFER_ALIGN(adr) calculates the number of bytes to the next alignment. */
101#define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32) adr)) % ALIGNMENT)
102
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200103#ifdef __BIG_ENDIAN
104#define xemaclite_readl ioread32be
105#define xemaclite_writel iowrite32be
106#else
107#define xemaclite_readl ioread32
108#define xemaclite_writel iowrite32
109#endif
110
John Linnbb81b2d2009-08-20 02:52:16 -0700111/**
112 * struct net_local - Our private per device data
113 * @ndev: instance of the network device
114 * @tx_ping_pong: indicates whether Tx Pong buffer is configured in HW
115 * @rx_ping_pong: indicates whether Rx Pong buffer is configured in HW
116 * @next_tx_buf_to_use: next Tx buffer to write to
117 * @next_rx_buf_to_use: next Rx buffer to read from
118 * @base_addr: base address of the Emaclite device
119 * @reset_lock: lock used for synchronization
120 * @deferred_skb: holds an skb (for transmission at a later time) when the
121 * Tx buffer is not free
John Linn5cdaaa12010-02-15 21:51:00 -0800122 * @phy_dev: pointer to the PHY device
123 * @phy_node: pointer to the PHY device node
124 * @mii_bus: pointer to the MII bus
John Linn5cdaaa12010-02-15 21:51:00 -0800125 * @last_link: last link status
John Linnbb81b2d2009-08-20 02:52:16 -0700126 */
127struct net_local {
128
129 struct net_device *ndev;
130
131 bool tx_ping_pong;
132 bool rx_ping_pong;
133 u32 next_tx_buf_to_use;
134 u32 next_rx_buf_to_use;
135 void __iomem *base_addr;
136
137 spinlock_t reset_lock;
138 struct sk_buff *deferred_skb;
John Linn5cdaaa12010-02-15 21:51:00 -0800139
140 struct phy_device *phy_dev;
141 struct device_node *phy_node;
142
143 struct mii_bus *mii_bus;
John Linn5cdaaa12010-02-15 21:51:00 -0800144
145 int last_link;
John Linnbb81b2d2009-08-20 02:52:16 -0700146};
147
148
149/*************************/
150/* EmacLite driver calls */
151/*************************/
152
153/**
154 * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
155 * @drvdata: Pointer to the Emaclite device private data
156 *
157 * This function enables the Tx and Rx interrupts for the Emaclite device along
158 * with the Global Interrupt Enable.
159 */
160static void xemaclite_enable_interrupts(struct net_local *drvdata)
161{
162 u32 reg_data;
163
164 /* Enable the Tx interrupts for the first Buffer */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200165 reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
166 xemaclite_writel(reg_data | XEL_TSR_XMIT_IE_MASK,
167 drvdata->base_addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700168
John Linnbb81b2d2009-08-20 02:52:16 -0700169 /* Enable the Rx interrupts for the first buffer */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200170 xemaclite_writel(XEL_RSR_RECV_IE_MASK, drvdata->base_addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700171
John Linnbb81b2d2009-08-20 02:52:16 -0700172 /* Enable the Global Interrupt Enable */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200173 xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700174}
175
176/**
177 * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
178 * @drvdata: Pointer to the Emaclite device private data
179 *
180 * This function disables the Tx and Rx interrupts for the Emaclite device,
181 * along with the Global Interrupt Enable.
182 */
183static void xemaclite_disable_interrupts(struct net_local *drvdata)
184{
185 u32 reg_data;
186
187 /* Disable the Global Interrupt Enable */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200188 xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700189
190 /* Disable the Tx interrupts for the first buffer */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200191 reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
192 xemaclite_writel(reg_data & (~XEL_TSR_XMIT_IE_MASK),
193 drvdata->base_addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700194
John Linnbb81b2d2009-08-20 02:52:16 -0700195 /* Disable the Rx interrupts for the first buffer */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200196 reg_data = xemaclite_readl(drvdata->base_addr + XEL_RSR_OFFSET);
197 xemaclite_writel(reg_data & (~XEL_RSR_RECV_IE_MASK),
198 drvdata->base_addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700199}
200
201/**
202 * xemaclite_aligned_write - Write from 16-bit aligned to 32-bit aligned address
203 * @src_ptr: Void pointer to the 16-bit aligned source address
204 * @dest_ptr: Pointer to the 32-bit aligned destination address
205 * @length: Number bytes to write from source to destination
206 *
207 * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
208 * address in the EmacLite device.
209 */
210static void xemaclite_aligned_write(void *src_ptr, u32 *dest_ptr,
211 unsigned length)
212{
213 u32 align_buffer;
214 u32 *to_u32_ptr;
215 u16 *from_u16_ptr, *to_u16_ptr;
216
217 to_u32_ptr = dest_ptr;
Joe Perches43d620c2011-06-16 19:08:06 +0000218 from_u16_ptr = src_ptr;
John Linnbb81b2d2009-08-20 02:52:16 -0700219 align_buffer = 0;
220
221 for (; length > 3; length -= 4) {
Joe Perches43d620c2011-06-16 19:08:06 +0000222 to_u16_ptr = (u16 *)&align_buffer;
John Linnbb81b2d2009-08-20 02:52:16 -0700223 *to_u16_ptr++ = *from_u16_ptr++;
224 *to_u16_ptr++ = *from_u16_ptr++;
225
Srikanth Thokalaec21b6b2013-12-07 13:40:49 +0530226 /* This barrier resolves occasional issues seen around
227 * cases where the data is not properly flushed out
228 * from the processor store buffers to the destination
229 * memory locations.
230 */
231 wmb();
232
John Linnbb81b2d2009-08-20 02:52:16 -0700233 /* Output a word */
234 *to_u32_ptr++ = align_buffer;
235 }
236 if (length) {
237 u8 *from_u8_ptr, *to_u8_ptr;
238
239 /* Set up to output the remaining data */
240 align_buffer = 0;
241 to_u8_ptr = (u8 *) &align_buffer;
242 from_u8_ptr = (u8 *) from_u16_ptr;
243
244 /* Output the remaining data */
245 for (; length > 0; length--)
246 *to_u8_ptr++ = *from_u8_ptr++;
247
Srikanth Thokalaec21b6b2013-12-07 13:40:49 +0530248 /* This barrier resolves occasional issues seen around
249 * cases where the data is not properly flushed out
250 * from the processor store buffers to the destination
251 * memory locations.
252 */
253 wmb();
John Linnbb81b2d2009-08-20 02:52:16 -0700254 *to_u32_ptr = align_buffer;
255 }
256}
257
258/**
259 * xemaclite_aligned_read - Read from 32-bit aligned to 16-bit aligned buffer
260 * @src_ptr: Pointer to the 32-bit aligned source address
261 * @dest_ptr: Pointer to the 16-bit aligned destination address
262 * @length: Number bytes to read from source to destination
263 *
264 * This function reads data from a 32-bit aligned address in the EmacLite device
265 * to a 16-bit aligned buffer.
266 */
267static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
268 unsigned length)
269{
270 u16 *to_u16_ptr, *from_u16_ptr;
271 u32 *from_u32_ptr;
272 u32 align_buffer;
273
274 from_u32_ptr = src_ptr;
275 to_u16_ptr = (u16 *) dest_ptr;
276
277 for (; length > 3; length -= 4) {
278 /* Copy each word into the temporary buffer */
279 align_buffer = *from_u32_ptr++;
280 from_u16_ptr = (u16 *)&align_buffer;
281
282 /* Read data from source */
283 *to_u16_ptr++ = *from_u16_ptr++;
284 *to_u16_ptr++ = *from_u16_ptr++;
285 }
286
287 if (length) {
288 u8 *to_u8_ptr, *from_u8_ptr;
289
290 /* Set up to read the remaining data */
291 to_u8_ptr = (u8 *) to_u16_ptr;
292 align_buffer = *from_u32_ptr++;
293 from_u8_ptr = (u8 *) &align_buffer;
294
295 /* Read the remaining data */
296 for (; length > 0; length--)
297 *to_u8_ptr = *from_u8_ptr;
298 }
299}
300
301/**
302 * xemaclite_send_data - Send an Ethernet frame
303 * @drvdata: Pointer to the Emaclite device private data
304 * @data: Pointer to the data to be sent
305 * @byte_count: Total frame size, including header
306 *
307 * This function checks if the Tx buffer of the Emaclite device is free to send
308 * data. If so, it fills the Tx buffer with data for transmission. Otherwise, it
309 * returns an error.
310 *
311 * Return: 0 upon success or -1 if the buffer(s) are full.
312 *
313 * Note: The maximum Tx packet size can not be more than Ethernet header
314 * (14 Bytes) + Maximum MTU (1500 bytes). This is excluding FCS.
315 */
316static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
317 unsigned int byte_count)
318{
319 u32 reg_data;
320 void __iomem *addr;
321
322 /* Determine the expected Tx buffer address */
323 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
324
325 /* If the length is too large, truncate it */
326 if (byte_count > ETH_FRAME_LEN)
327 byte_count = ETH_FRAME_LEN;
328
329 /* Check if the expected buffer is available */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200330 reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700331 if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
332 XEL_TSR_XMIT_ACTIVE_MASK)) == 0) {
333
334 /* Switch to next buffer if configured */
335 if (drvdata->tx_ping_pong != 0)
336 drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET;
337 } else if (drvdata->tx_ping_pong != 0) {
338 /* If the expected buffer is full, try the other buffer,
339 * if it is configured in HW */
340
341 addr = (void __iomem __force *)((u32 __force)addr ^
342 XEL_BUFFER_OFFSET);
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200343 reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700344
345 if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
346 XEL_TSR_XMIT_ACTIVE_MASK)) != 0)
347 return -1; /* Buffers were full, return failure */
348 } else
349 return -1; /* Buffer was full, return failure */
350
351 /* Write the frame to the buffer */
352 xemaclite_aligned_write(data, (u32 __force *) addr, byte_count);
353
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200354 xemaclite_writel((byte_count & XEL_TPLR_LENGTH_MASK),
355 addr + XEL_TPLR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700356
357 /* Update the Tx Status Register to indicate that there is a
358 * frame to send. Set the XEL_TSR_XMIT_ACTIVE_MASK flag which
359 * is used by the interrupt handler to check whether a frame
360 * has been transmitted */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200361 reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700362 reg_data |= (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_XMIT_ACTIVE_MASK);
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200363 xemaclite_writel(reg_data, addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700364
365 return 0;
366}
367
368/**
369 * xemaclite_recv_data - Receive a frame
370 * @drvdata: Pointer to the Emaclite device private data
371 * @data: Address where the data is to be received
372 *
373 * This function is intended to be called from the interrupt context or
374 * with a wrapper which waits for the receive frame to be available.
375 *
376 * Return: Total number of bytes received
377 */
Anssi Hannulacd224552017-02-14 19:11:44 +0200378static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)
John Linnbb81b2d2009-08-20 02:52:16 -0700379{
380 void __iomem *addr;
381 u16 length, proto_type;
382 u32 reg_data;
383
384 /* Determine the expected buffer address */
385 addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use);
386
387 /* Verify which buffer has valid data */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200388 reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700389
390 if ((reg_data & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
391 if (drvdata->rx_ping_pong != 0)
392 drvdata->next_rx_buf_to_use ^= XEL_BUFFER_OFFSET;
393 } else {
394 /* The instance is out of sync, try other buffer if other
395 * buffer is configured, return 0 otherwise. If the instance is
396 * out of sync, do not update the 'next_rx_buf_to_use' since it
397 * will correct on subsequent calls */
398 if (drvdata->rx_ping_pong != 0)
399 addr = (void __iomem __force *)((u32 __force)addr ^
400 XEL_BUFFER_OFFSET);
401 else
402 return 0; /* No data was available */
403
404 /* Verify that buffer has valid data */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200405 reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700406 if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
407 XEL_RSR_RECV_DONE_MASK)
408 return 0; /* No data was available */
409 }
410
411 /* Get the protocol type of the ethernet frame that arrived */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200412 proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
Michal Simek44180a52010-09-10 13:22:35 +0200413 XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
John Linnbb81b2d2009-08-20 02:52:16 -0700414 XEL_RPLR_LENGTH_MASK);
415
416 /* Check if received ethernet frame is a raw ethernet frame
417 * or an IP packet or an ARP packet */
Anssi Hannulacd224552017-02-14 19:11:44 +0200418 if (proto_type > ETH_DATA_LEN) {
John Linnbb81b2d2009-08-20 02:52:16 -0700419
420 if (proto_type == ETH_P_IP) {
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200421 length = ((ntohl(xemaclite_readl(addr +
John Linnbb81b2d2009-08-20 02:52:16 -0700422 XEL_HEADER_IP_LENGTH_OFFSET +
Michal Simek44180a52010-09-10 13:22:35 +0200423 XEL_RXBUFF_OFFSET)) >>
John Linnbb81b2d2009-08-20 02:52:16 -0700424 XEL_HEADER_SHIFT) &
425 XEL_RPLR_LENGTH_MASK);
Anssi Hannulacd224552017-02-14 19:11:44 +0200426 length = min_t(u16, length, ETH_DATA_LEN);
John Linnbb81b2d2009-08-20 02:52:16 -0700427 length += ETH_HLEN + ETH_FCS_LEN;
428
429 } else if (proto_type == ETH_P_ARP)
430 length = XEL_ARP_PACKET_SIZE + ETH_HLEN + ETH_FCS_LEN;
431 else
432 /* Field contains type other than IP or ARP, use max
433 * frame size and let user parse it */
434 length = ETH_FRAME_LEN + ETH_FCS_LEN;
435 } else
436 /* Use the length in the frame, plus the header and trailer */
437 length = proto_type + ETH_HLEN + ETH_FCS_LEN;
438
Anssi Hannulacd224552017-02-14 19:11:44 +0200439 if (WARN_ON(length > maxlen))
440 length = maxlen;
441
John Linnbb81b2d2009-08-20 02:52:16 -0700442 /* Read from the EmacLite device */
443 xemaclite_aligned_read((u32 __force *) (addr + XEL_RXBUFF_OFFSET),
444 data, length);
445
446 /* Acknowledge the frame */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200447 reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700448 reg_data &= ~XEL_RSR_RECV_DONE_MASK;
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200449 xemaclite_writel(reg_data, addr + XEL_RSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700450
451 return length;
452}
453
454/**
John Linn5cdaaa12010-02-15 21:51:00 -0800455 * xemaclite_update_address - Update the MAC address in the device
John Linnbb81b2d2009-08-20 02:52:16 -0700456 * @drvdata: Pointer to the Emaclite device private data
457 * @address_ptr:Pointer to the MAC address (MAC address is a 48-bit value)
458 *
459 * Tx must be idle and Rx should be idle for deterministic results.
460 * It is recommended that this function should be called after the
461 * initialization and before transmission of any packets from the device.
462 * The MAC address can be programmed using any of the two transmit
463 * buffers (if configured).
464 */
John Linn5cdaaa12010-02-15 21:51:00 -0800465static void xemaclite_update_address(struct net_local *drvdata,
466 u8 *address_ptr)
John Linnbb81b2d2009-08-20 02:52:16 -0700467{
468 void __iomem *addr;
469 u32 reg_data;
470
471 /* Determine the expected Tx buffer address */
472 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
473
474 xemaclite_aligned_write(address_ptr, (u32 __force *) addr, ETH_ALEN);
475
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200476 xemaclite_writel(ETH_ALEN, addr + XEL_TPLR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700477
478 /* Update the MAC address in the EmacLite */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200479 reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
480 xemaclite_writel(reg_data | XEL_TSR_PROG_MAC_ADDR, addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700481
482 /* Wait for EmacLite to finish with the MAC address update */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200483 while ((xemaclite_readl(addr + XEL_TSR_OFFSET) &
John Linnbb81b2d2009-08-20 02:52:16 -0700484 XEL_TSR_PROG_MAC_ADDR) != 0)
485 ;
486}
487
488/**
John Linn5cdaaa12010-02-15 21:51:00 -0800489 * xemaclite_set_mac_address - Set the MAC address for this device
490 * @dev: Pointer to the network device instance
491 * @addr: Void pointer to the sockaddr structure
492 *
493 * This function copies the HW address from the sockaddr strucutre to the
494 * net_device structure and updates the address in HW.
495 *
496 * Return: Error if the net device is busy or 0 if the addr is set
497 * successfully
498 */
499static int xemaclite_set_mac_address(struct net_device *dev, void *address)
500{
Joe Perchesece49152010-11-15 11:12:31 +0000501 struct net_local *lp = netdev_priv(dev);
John Linn5cdaaa12010-02-15 21:51:00 -0800502 struct sockaddr *addr = address;
503
504 if (netif_running(dev))
505 return -EBUSY;
506
507 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
508 xemaclite_update_address(lp, dev->dev_addr);
509 return 0;
510}
511
512/**
John Linnbb81b2d2009-08-20 02:52:16 -0700513 * xemaclite_tx_timeout - Callback for Tx Timeout
514 * @dev: Pointer to the network device
515 *
516 * This function is called when Tx time out occurs for Emaclite device.
517 */
518static void xemaclite_tx_timeout(struct net_device *dev)
519{
Joe Perchesece49152010-11-15 11:12:31 +0000520 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700521 unsigned long flags;
522
523 dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
524 TX_TIMEOUT * 1000UL / HZ);
525
526 dev->stats.tx_errors++;
527
528 /* Reset the device */
529 spin_lock_irqsave(&lp->reset_lock, flags);
530
531 /* Shouldn't really be necessary, but shouldn't hurt */
532 netif_stop_queue(dev);
533
534 xemaclite_disable_interrupts(lp);
535 xemaclite_enable_interrupts(lp);
536
537 if (lp->deferred_skb) {
538 dev_kfree_skb(lp->deferred_skb);
539 lp->deferred_skb = NULL;
540 dev->stats.tx_errors++;
541 }
542
543 /* To exclude tx timeout */
Florian Westphal860e9532016-05-03 16:33:13 +0200544 netif_trans_update(dev); /* prevent tx timeout */
John Linnbb81b2d2009-08-20 02:52:16 -0700545
546 /* We're all ready to go. Start the queue */
547 netif_wake_queue(dev);
548 spin_unlock_irqrestore(&lp->reset_lock, flags);
549}
550
551/**********************/
552/* Interrupt Handlers */
553/**********************/
554
555/**
556 * xemaclite_tx_handler - Interrupt handler for frames sent
557 * @dev: Pointer to the network device
558 *
559 * This function updates the number of packets transmitted and handles the
560 * deferred skb, if there is one.
561 */
562static void xemaclite_tx_handler(struct net_device *dev)
563{
Joe Perchesece49152010-11-15 11:12:31 +0000564 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700565
566 dev->stats.tx_packets++;
567 if (lp->deferred_skb) {
568 if (xemaclite_send_data(lp,
569 (u8 *) lp->deferred_skb->data,
570 lp->deferred_skb->len) != 0)
571 return;
572 else {
573 dev->stats.tx_bytes += lp->deferred_skb->len;
574 dev_kfree_skb_irq(lp->deferred_skb);
575 lp->deferred_skb = NULL;
Florian Westphal860e9532016-05-03 16:33:13 +0200576 netif_trans_update(dev); /* prevent tx timeout */
John Linnbb81b2d2009-08-20 02:52:16 -0700577 netif_wake_queue(dev);
578 }
579 }
580}
581
582/**
583 * xemaclite_rx_handler- Interrupt handler for frames received
584 * @dev: Pointer to the network device
585 *
586 * This function allocates memory for a socket buffer, fills it with data
587 * received and hands it over to the TCP/IP stack.
588 */
589static void xemaclite_rx_handler(struct net_device *dev)
590{
Joe Perchesece49152010-11-15 11:12:31 +0000591 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700592 struct sk_buff *skb;
593 unsigned int align;
594 u32 len;
595
596 len = ETH_FRAME_LEN + ETH_FCS_LEN;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000597 skb = netdev_alloc_skb(dev, len + ALIGNMENT);
John Linnbb81b2d2009-08-20 02:52:16 -0700598 if (!skb) {
599 /* Couldn't get memory. */
600 dev->stats.rx_dropped++;
601 dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n");
602 return;
603 }
604
605 /*
606 * A new skb should have the data halfword aligned, but this code is
607 * here just in case that isn't true. Calculate how many
608 * bytes we should reserve to get the data to start on a word
609 * boundary */
610 align = BUFFER_ALIGN(skb->data);
611 if (align)
612 skb_reserve(skb, align);
613
614 skb_reserve(skb, 2);
615
Anssi Hannulacd224552017-02-14 19:11:44 +0200616 len = xemaclite_recv_data(lp, (u8 *) skb->data, len);
John Linnbb81b2d2009-08-20 02:52:16 -0700617
618 if (!len) {
619 dev->stats.rx_errors++;
620 dev_kfree_skb_irq(skb);
621 return;
622 }
623
624 skb_put(skb, len); /* Tell the skb how much data we got */
John Linnbb81b2d2009-08-20 02:52:16 -0700625
626 skb->protocol = eth_type_trans(skb, dev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700627 skb_checksum_none_assert(skb);
John Linnbb81b2d2009-08-20 02:52:16 -0700628
629 dev->stats.rx_packets++;
630 dev->stats.rx_bytes += len;
John Linnbb81b2d2009-08-20 02:52:16 -0700631
Richard Cochran570773c2011-06-19 21:51:25 +0000632 if (!skb_defer_rx_timestamp(skb))
633 netif_rx(skb); /* Send the packet upstream */
John Linnbb81b2d2009-08-20 02:52:16 -0700634}
635
636/**
637 * xemaclite_interrupt - Interrupt handler for this driver
638 * @irq: Irq of the Emaclite device
639 * @dev_id: Void pointer to the network device instance used as callback
640 * reference
641 *
642 * This function handles the Tx and Rx interrupts of the EmacLite device.
643 */
644static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
645{
Rusty Russell3db1cd52011-12-19 13:56:45 +0000646 bool tx_complete = false;
John Linnbb81b2d2009-08-20 02:52:16 -0700647 struct net_device *dev = dev_id;
Joe Perchesece49152010-11-15 11:12:31 +0000648 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700649 void __iomem *base_addr = lp->base_addr;
650 u32 tx_status;
651
652 /* Check if there is Rx Data available */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200653 if ((xemaclite_readl(base_addr + XEL_RSR_OFFSET) &
Michal Simek123c1402013-05-30 00:28:06 +0000654 XEL_RSR_RECV_DONE_MASK) ||
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200655 (xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
John Linnbb81b2d2009-08-20 02:52:16 -0700656 & XEL_RSR_RECV_DONE_MASK))
657
658 xemaclite_rx_handler(dev);
659
660 /* Check if the Transmission for the first buffer is completed */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200661 tx_status = xemaclite_readl(base_addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700662 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
663 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
664
665 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200666 xemaclite_writel(tx_status, base_addr + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700667
Rusty Russell3db1cd52011-12-19 13:56:45 +0000668 tx_complete = true;
John Linnbb81b2d2009-08-20 02:52:16 -0700669 }
670
671 /* Check if the Transmission for the second buffer is completed */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200672 tx_status = xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700673 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
674 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
675
676 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200677 xemaclite_writel(tx_status, base_addr + XEL_BUFFER_OFFSET +
678 XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -0700679
Rusty Russell3db1cd52011-12-19 13:56:45 +0000680 tx_complete = true;
John Linnbb81b2d2009-08-20 02:52:16 -0700681 }
682
683 /* If there was a Tx interrupt, call the Tx Handler */
684 if (tx_complete != 0)
685 xemaclite_tx_handler(dev);
686
687 return IRQ_HANDLED;
688}
689
John Linn5cdaaa12010-02-15 21:51:00 -0800690/**********************/
691/* MDIO Bus functions */
692/**********************/
693
694/**
695 * xemaclite_mdio_wait - Wait for the MDIO to be ready to use
696 * @lp: Pointer to the Emaclite device private data
697 *
698 * This function waits till the device is ready to accept a new MDIO
699 * request.
700 *
701 * Return: 0 for success or ETIMEDOUT for a timeout
702 */
703
704static int xemaclite_mdio_wait(struct net_local *lp)
705{
Manuel Schölling9f8b93c2014-06-22 13:24:54 +0200706 unsigned long end = jiffies + 2;
John Linn5cdaaa12010-02-15 21:51:00 -0800707
708 /* wait for the MDIO interface to not be busy or timeout
709 after some time.
710 */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200711 while (xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET) &
John Linn5cdaaa12010-02-15 21:51:00 -0800712 XEL_MDIOCTRL_MDIOSTS_MASK) {
Manuel Schölling3aeea532014-05-22 21:10:28 +0200713 if (time_before_eq(end, jiffies)) {
John Linn5cdaaa12010-02-15 21:51:00 -0800714 WARN_ON(1);
715 return -ETIMEDOUT;
716 }
717 msleep(1);
718 }
719 return 0;
720}
721
722/**
723 * xemaclite_mdio_read - Read from a given MII management register
724 * @bus: the mii_bus struct
725 * @phy_id: the phy address
726 * @reg: register number to read from
727 *
728 * This function waits till the device is ready to accept a new MDIO
729 * request and then writes the phy address to the MDIO Address register
730 * and reads data from MDIO Read Data register, when its available.
731 *
732 * Return: Value read from the MII management register
733 */
734static int xemaclite_mdio_read(struct mii_bus *bus, int phy_id, int reg)
735{
736 struct net_local *lp = bus->priv;
737 u32 ctrl_reg;
738 u32 rc;
739
740 if (xemaclite_mdio_wait(lp))
741 return -ETIMEDOUT;
742
743 /* Write the PHY address, register number and set the OP bit in the
744 * MDIO Address register. Set the Status bit in the MDIO Control
745 * register to start a MDIO read transaction.
746 */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200747 ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
748 xemaclite_writel(XEL_MDIOADDR_OP_MASK |
749 ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
750 lp->base_addr + XEL_MDIOADDR_OFFSET);
751 xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
752 lp->base_addr + XEL_MDIOCTRL_OFFSET);
John Linn5cdaaa12010-02-15 21:51:00 -0800753
754 if (xemaclite_mdio_wait(lp))
755 return -ETIMEDOUT;
756
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200757 rc = xemaclite_readl(lp->base_addr + XEL_MDIORD_OFFSET);
John Linn5cdaaa12010-02-15 21:51:00 -0800758
759 dev_dbg(&lp->ndev->dev,
760 "xemaclite_mdio_read(phy_id=%i, reg=%x) == %x\n",
761 phy_id, reg, rc);
762
763 return rc;
764}
765
766/**
767 * xemaclite_mdio_write - Write to a given MII management register
768 * @bus: the mii_bus struct
769 * @phy_id: the phy address
770 * @reg: register number to write to
771 * @val: value to write to the register number specified by reg
772 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300773 * This function waits till the device is ready to accept a new MDIO
John Linn5cdaaa12010-02-15 21:51:00 -0800774 * request and then writes the val to the MDIO Write Data register.
775 */
776static int xemaclite_mdio_write(struct mii_bus *bus, int phy_id, int reg,
777 u16 val)
778{
779 struct net_local *lp = bus->priv;
780 u32 ctrl_reg;
781
782 dev_dbg(&lp->ndev->dev,
783 "xemaclite_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
784 phy_id, reg, val);
785
786 if (xemaclite_mdio_wait(lp))
787 return -ETIMEDOUT;
788
789 /* Write the PHY address, register number and clear the OP bit in the
790 * MDIO Address register and then write the value into the MDIO Write
791 * Data register. Finally, set the Status bit in the MDIO Control
792 * register to start a MDIO write transaction.
793 */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200794 ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
795 xemaclite_writel(~XEL_MDIOADDR_OP_MASK &
796 ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
797 lp->base_addr + XEL_MDIOADDR_OFFSET);
798 xemaclite_writel(val, lp->base_addr + XEL_MDIOWR_OFFSET);
799 xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
800 lp->base_addr + XEL_MDIOCTRL_OFFSET);
John Linn5cdaaa12010-02-15 21:51:00 -0800801
802 return 0;
803}
804
805/**
John Linn5cdaaa12010-02-15 21:51:00 -0800806 * xemaclite_mdio_setup - Register mii_bus for the Emaclite device
807 * @lp: Pointer to the Emaclite device private data
808 * @ofdev: Pointer to OF device structure
809 *
810 * This function enables MDIO bus in the Emaclite device and registers a
811 * mii_bus.
812 *
813 * Return: 0 upon success or a negative error upon failure
814 */
815static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
816{
817 struct mii_bus *bus;
818 int rc;
819 struct resource res;
820 struct device_node *np = of_get_parent(lp->phy_node);
Michal Simeke0a3bc652013-05-30 00:28:04 +0000821 struct device_node *npp;
John Linn5cdaaa12010-02-15 21:51:00 -0800822
823 /* Don't register the MDIO bus if the phy_node or its parent node
824 * can't be found.
825 */
Michal Simekccfecdf2013-05-30 00:28:03 +0000826 if (!np) {
827 dev_err(dev, "Failed to register mdio bus.\n");
John Linn5cdaaa12010-02-15 21:51:00 -0800828 return -ENODEV;
Michal Simekccfecdf2013-05-30 00:28:03 +0000829 }
Michal Simeke0a3bc652013-05-30 00:28:04 +0000830 npp = of_get_parent(np);
831
832 of_address_to_resource(npp, 0, &res);
833 if (lp->ndev->mem_start != res.start) {
834 struct phy_device *phydev;
835 phydev = of_phy_find_device(lp->phy_node);
836 if (!phydev)
837 dev_info(dev,
838 "MDIO of the phy is not registered yet\n");
Russell King04d53b22015-09-24 20:36:18 +0100839 else
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100840 put_device(&phydev->mdio.dev);
Michal Simeke0a3bc652013-05-30 00:28:04 +0000841 return 0;
842 }
John Linn5cdaaa12010-02-15 21:51:00 -0800843
844 /* Enable the MDIO bus by asserting the enable bit in MDIO Control
845 * register.
846 */
Anssi Hannulaacf138f2017-02-14 19:11:45 +0200847 xemaclite_writel(XEL_MDIOCTRL_MDIOEN_MASK,
848 lp->base_addr + XEL_MDIOCTRL_OFFSET);
John Linn5cdaaa12010-02-15 21:51:00 -0800849
850 bus = mdiobus_alloc();
Michal Simekccfecdf2013-05-30 00:28:03 +0000851 if (!bus) {
Jens Renner \(EFE\)f1362e32013-06-02 05:19:06 +0000852 dev_err(dev, "Failed to allocate mdiobus\n");
John Linn5cdaaa12010-02-15 21:51:00 -0800853 return -ENOMEM;
Michal Simekccfecdf2013-05-30 00:28:03 +0000854 }
John Linn5cdaaa12010-02-15 21:51:00 -0800855
John Linn5cdaaa12010-02-15 21:51:00 -0800856 snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
857 (unsigned long long)res.start);
858 bus->priv = lp;
859 bus->name = "Xilinx Emaclite MDIO";
860 bus->read = xemaclite_mdio_read;
861 bus->write = xemaclite_mdio_write;
John Linn5cdaaa12010-02-15 21:51:00 -0800862 bus->parent = dev;
John Linn5cdaaa12010-02-15 21:51:00 -0800863
John Linn5cdaaa12010-02-15 21:51:00 -0800864 rc = of_mdiobus_register(bus, np);
Michal Simekccfecdf2013-05-30 00:28:03 +0000865 if (rc) {
866 dev_err(dev, "Failed to register mdio bus.\n");
John Linn5cdaaa12010-02-15 21:51:00 -0800867 goto err_register;
Michal Simekccfecdf2013-05-30 00:28:03 +0000868 }
John Linn5cdaaa12010-02-15 21:51:00 -0800869
Radhey Shyam Pandey087fca52018-06-13 12:05:16 +0530870 lp->mii_bus = bus;
871
John Linn5cdaaa12010-02-15 21:51:00 -0800872 return 0;
873
874err_register:
875 mdiobus_free(bus);
876 return rc;
877}
878
879/**
880 * xemaclite_adjust_link - Link state callback for the Emaclite device
881 * @ndev: pointer to net_device struct
882 *
883 * There's nothing in the Emaclite device to be configured when the link
884 * state changes. We just print the status.
885 */
Michal Simek3fb99fa2013-05-30 00:28:05 +0000886static void xemaclite_adjust_link(struct net_device *ndev)
John Linn5cdaaa12010-02-15 21:51:00 -0800887{
888 struct net_local *lp = netdev_priv(ndev);
889 struct phy_device *phy = lp->phy_dev;
890 int link_state;
891
892 /* hash together the state values to decide if something has changed */
893 link_state = phy->speed | (phy->duplex << 1) | phy->link;
894
895 if (lp->last_link != link_state) {
896 lp->last_link = link_state;
897 phy_print_status(phy);
898 }
899}
900
John Linnbb81b2d2009-08-20 02:52:16 -0700901/**
902 * xemaclite_open - Open the network device
903 * @dev: Pointer to the network device
904 *
905 * This function sets the MAC address, requests an IRQ and enables interrupts
906 * for the Emaclite device and starts the Tx queue.
John Linn5cdaaa12010-02-15 21:51:00 -0800907 * It also connects to the phy device, if MDIO is included in Emaclite device.
John Linnbb81b2d2009-08-20 02:52:16 -0700908 */
909static int xemaclite_open(struct net_device *dev)
910{
Joe Perchesece49152010-11-15 11:12:31 +0000911 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700912 int retval;
913
914 /* Just to be safe, stop the device first */
915 xemaclite_disable_interrupts(lp);
916
John Linn5cdaaa12010-02-15 21:51:00 -0800917 if (lp->phy_node) {
918 u32 bmcr;
919
920 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
921 xemaclite_adjust_link, 0,
922 PHY_INTERFACE_MODE_MII);
923 if (!lp->phy_dev) {
924 dev_err(&lp->ndev->dev, "of_phy_connect() failed\n");
925 return -ENODEV;
926 }
927
928 /* EmacLite doesn't support giga-bit speeds */
929 lp->phy_dev->supported &= (PHY_BASIC_FEATURES);
930 lp->phy_dev->advertising = lp->phy_dev->supported;
931
932 /* Don't advertise 1000BASE-T Full/Half duplex speeds */
933 phy_write(lp->phy_dev, MII_CTRL1000, 0);
934
935 /* Advertise only 10 and 100mbps full/half duplex speeds */
Jens Renner \(EFE\)3a5395b2013-06-03 04:32:52 +0000936 phy_write(lp->phy_dev, MII_ADVERTISE, ADVERTISE_ALL |
937 ADVERTISE_CSMA);
John Linn5cdaaa12010-02-15 21:51:00 -0800938
939 /* Restart auto negotiation */
940 bmcr = phy_read(lp->phy_dev, MII_BMCR);
941 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
942 phy_write(lp->phy_dev, MII_BMCR, bmcr);
943
944 phy_start(lp->phy_dev);
945 }
946
John Linnbb81b2d2009-08-20 02:52:16 -0700947 /* Set the MAC address each time opened */
John Linn5cdaaa12010-02-15 21:51:00 -0800948 xemaclite_update_address(lp, dev->dev_addr);
John Linnbb81b2d2009-08-20 02:52:16 -0700949
950 /* Grab the IRQ */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800951 retval = request_irq(dev->irq, xemaclite_interrupt, 0, dev->name, dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700952 if (retval) {
953 dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
954 dev->irq);
John Linn5cdaaa12010-02-15 21:51:00 -0800955 if (lp->phy_dev)
956 phy_disconnect(lp->phy_dev);
957 lp->phy_dev = NULL;
958
John Linnbb81b2d2009-08-20 02:52:16 -0700959 return retval;
960 }
961
962 /* Enable Interrupts */
963 xemaclite_enable_interrupts(lp);
964
965 /* We're ready to go */
966 netif_start_queue(dev);
967
968 return 0;
969}
970
971/**
972 * xemaclite_close - Close the network device
973 * @dev: Pointer to the network device
974 *
975 * This function stops the Tx queue, disables interrupts and frees the IRQ for
976 * the Emaclite device.
John Linn5cdaaa12010-02-15 21:51:00 -0800977 * It also disconnects the phy device associated with the Emaclite device.
John Linnbb81b2d2009-08-20 02:52:16 -0700978 */
979static int xemaclite_close(struct net_device *dev)
980{
Joe Perchesece49152010-11-15 11:12:31 +0000981 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -0700982
983 netif_stop_queue(dev);
984 xemaclite_disable_interrupts(lp);
985 free_irq(dev->irq, dev);
986
John Linn5cdaaa12010-02-15 21:51:00 -0800987 if (lp->phy_dev)
988 phy_disconnect(lp->phy_dev);
989 lp->phy_dev = NULL;
990
John Linnbb81b2d2009-08-20 02:52:16 -0700991 return 0;
992}
993
994/**
John Linnbb81b2d2009-08-20 02:52:16 -0700995 * xemaclite_send - Transmit a frame
996 * @orig_skb: Pointer to the socket buffer to be transmitted
997 * @dev: Pointer to the network device
998 *
999 * This function checks if the Tx buffer of the Emaclite device is free to send
1000 * data. If so, it fills the Tx buffer with data from socket buffer data,
1001 * updates the stats and frees the socket buffer. The Tx completion is signaled
1002 * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
1003 * deferred and the Tx queue is stopped so that the deferred socket buffer can
1004 * be transmitted when the Emaclite device is free to transmit data.
1005 *
1006 * Return: 0, always.
1007 */
1008static int xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
1009{
Joe Perchesece49152010-11-15 11:12:31 +00001010 struct net_local *lp = netdev_priv(dev);
John Linnbb81b2d2009-08-20 02:52:16 -07001011 struct sk_buff *new_skb;
1012 unsigned int len;
1013 unsigned long flags;
1014
1015 len = orig_skb->len;
1016
1017 new_skb = orig_skb;
1018
1019 spin_lock_irqsave(&lp->reset_lock, flags);
1020 if (xemaclite_send_data(lp, (u8 *) new_skb->data, len) != 0) {
1021 /* If the Emaclite Tx buffer is busy, stop the Tx queue and
Richard Cochran570773c2011-06-19 21:51:25 +00001022 * defer the skb for transmission during the ISR, after the
John Linnbb81b2d2009-08-20 02:52:16 -07001023 * current transmission is complete */
1024 netif_stop_queue(dev);
1025 lp->deferred_skb = new_skb;
Richard Cochran570773c2011-06-19 21:51:25 +00001026 /* Take the time stamp now, since we can't do this in an ISR. */
1027 skb_tx_timestamp(new_skb);
John Linnbb81b2d2009-08-20 02:52:16 -07001028 spin_unlock_irqrestore(&lp->reset_lock, flags);
1029 return 0;
1030 }
1031 spin_unlock_irqrestore(&lp->reset_lock, flags);
1032
Richard Cochran570773c2011-06-19 21:51:25 +00001033 skb_tx_timestamp(new_skb);
1034
John Linnbb81b2d2009-08-20 02:52:16 -07001035 dev->stats.tx_bytes += len;
Eric W. Biederman69e73d22014-03-15 18:27:33 -07001036 dev_consume_skb_any(new_skb);
John Linnbb81b2d2009-08-20 02:52:16 -07001037
1038 return 0;
1039}
1040
1041/**
John Linnbb81b2d2009-08-20 02:52:16 -07001042 * get_bool - Get a parameter from the OF device
1043 * @ofdev: Pointer to OF device structure
1044 * @s: Property to be retrieved
1045 *
1046 * This function looks for a property in the device node and returns the value
1047 * of the property if its found or 0 if the property is not found.
1048 *
1049 * Return: Value of the parameter if the parameter is found, or 0 otherwise
1050 */
Grant Likely2dc11582010-08-06 09:25:50 -06001051static bool get_bool(struct platform_device *ofdev, const char *s)
John Linnbb81b2d2009-08-20 02:52:16 -07001052{
Grant Likely61c7a082010-04-13 16:12:29 -07001053 u32 *p = (u32 *)of_get_property(ofdev->dev.of_node, s, NULL);
John Linnbb81b2d2009-08-20 02:52:16 -07001054
1055 if (p) {
1056 return (bool)*p;
1057 } else {
1058 dev_warn(&ofdev->dev, "Parameter %s not found,"
1059 "defaulting to false\n", s);
Joe Perches4e833c52015-03-29 18:25:12 -07001060 return false;
John Linnbb81b2d2009-08-20 02:52:16 -07001061 }
1062}
1063
Bhumika Goyal10eeb5e2017-01-21 12:28:58 +05301064static const struct net_device_ops xemaclite_netdev_ops;
John Linnbb81b2d2009-08-20 02:52:16 -07001065
1066/**
1067 * xemaclite_of_probe - Probe method for the Emaclite device.
1068 * @ofdev: Pointer to OF device structure
1069 * @match: Pointer to the structure used for matching a device
1070 *
1071 * This function probes for the Emaclite device in the device tree.
1072 * It initializes the driver data structure and the hardware, sets the MAC
1073 * address and registers the network device.
John Linn5cdaaa12010-02-15 21:51:00 -08001074 * It also registers a mii_bus for the Emaclite device, if MDIO is included
1075 * in the device.
John Linnbb81b2d2009-08-20 02:52:16 -07001076 *
1077 * Return: 0, if the driver is bound to the Emaclite device, or
1078 * a negative error if there is failure.
1079 */
Bill Pemberton06b0e682012-12-03 09:24:08 -05001080static int xemaclite_of_probe(struct platform_device *ofdev)
John Linnbb81b2d2009-08-20 02:52:16 -07001081{
Michal Simek7a3e2582013-06-04 00:03:27 +00001082 struct resource *res;
John Linnbb81b2d2009-08-20 02:52:16 -07001083 struct net_device *ndev = NULL;
1084 struct net_local *lp = NULL;
1085 struct device *dev = &ofdev->dev;
1086 const void *mac_address;
1087
1088 int rc = 0;
1089
1090 dev_info(dev, "Device Tree Probing\n");
1091
John Linnbb81b2d2009-08-20 02:52:16 -07001092 /* Create an ethernet device instance */
1093 ndev = alloc_etherdev(sizeof(struct net_local));
Joe Perches41de8d42012-01-29 13:47:52 +00001094 if (!ndev)
John Linnbb81b2d2009-08-20 02:52:16 -07001095 return -ENOMEM;
John Linnbb81b2d2009-08-20 02:52:16 -07001096
1097 dev_set_drvdata(dev, ndev);
John Linn5cdaaa12010-02-15 21:51:00 -08001098 SET_NETDEV_DEV(ndev, &ofdev->dev);
John Linnbb81b2d2009-08-20 02:52:16 -07001099
John Linnbb81b2d2009-08-20 02:52:16 -07001100 lp = netdev_priv(ndev);
1101 lp->ndev = ndev;
1102
Michal Simek7a3e2582013-06-04 00:03:27 +00001103 /* Get IRQ for the device */
1104 res = platform_get_resource(ofdev, IORESOURCE_IRQ, 0);
1105 if (!res) {
1106 dev_err(dev, "no IRQ found\n");
Julia Lawall8a0a1f82014-12-29 18:04:36 +01001107 rc = -ENXIO;
Michal Simek7a3e2582013-06-04 00:03:27 +00001108 goto error;
John Linnbb81b2d2009-08-20 02:52:16 -07001109 }
1110
Michal Simek7a3e2582013-06-04 00:03:27 +00001111 ndev->irq = res->start;
1112
1113 res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
Tushar Beheraeed5d292013-06-10 17:05:06 +05301114 lp->base_addr = devm_ioremap_resource(&ofdev->dev, res);
1115 if (IS_ERR(lp->base_addr)) {
1116 rc = PTR_ERR(lp->base_addr);
Michal Simek7a3e2582013-06-04 00:03:27 +00001117 goto error;
Tushar Beheraeed5d292013-06-10 17:05:06 +05301118 }
Michal Simek7a3e2582013-06-04 00:03:27 +00001119
1120 ndev->mem_start = res->start;
1121 ndev->mem_end = res->end;
John Linnbb81b2d2009-08-20 02:52:16 -07001122
1123 spin_lock_init(&lp->reset_lock);
1124 lp->next_tx_buf_to_use = 0x0;
1125 lp->next_rx_buf_to_use = 0x0;
1126 lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
1127 lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
Grant Likely61c7a082010-04-13 16:12:29 -07001128 mac_address = of_get_mac_address(ofdev->dev.of_node);
John Linnbb81b2d2009-08-20 02:52:16 -07001129
Daniel Romell5575cf12016-08-19 14:12:01 +02001130 if (mac_address) {
John Linnbb81b2d2009-08-20 02:52:16 -07001131 /* Set the MAC address. */
Joe Perchesd458cdf2013-10-01 19:04:40 -07001132 memcpy(ndev->dev_addr, mac_address, ETH_ALEN);
Daniel Romell5575cf12016-08-19 14:12:01 +02001133 } else {
1134 dev_warn(dev, "No MAC address found, using random\n");
1135 eth_hw_addr_random(ndev);
1136 }
John Linnbb81b2d2009-08-20 02:52:16 -07001137
1138 /* Clear the Tx CSR's in case this is a restart */
Anssi Hannulaacf138f2017-02-14 19:11:45 +02001139 xemaclite_writel(0, lp->base_addr + XEL_TSR_OFFSET);
1140 xemaclite_writel(0, lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
John Linnbb81b2d2009-08-20 02:52:16 -07001141
1142 /* Set the MAC address in the EmacLite device */
John Linn5cdaaa12010-02-15 21:51:00 -08001143 xemaclite_update_address(lp, ndev->dev_addr);
1144
Grant Likely61c7a082010-04-13 16:12:29 -07001145 lp->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
Radhey Shyam Pandey560c5bd2018-06-13 12:05:19 +05301146 xemaclite_mdio_setup(lp, &ofdev->dev);
John Linnbb81b2d2009-08-20 02:52:16 -07001147
H Hartley Sweeten5491f3a2009-12-29 20:04:53 -08001148 dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
John Linnbb81b2d2009-08-20 02:52:16 -07001149
1150 ndev->netdev_ops = &xemaclite_netdev_ops;
1151 ndev->flags &= ~IFF_MULTICAST;
1152 ndev->watchdog_timeo = TX_TIMEOUT;
1153
1154 /* Finally, register the device */
1155 rc = register_netdev(ndev);
1156 if (rc) {
1157 dev_err(dev,
1158 "Cannot register network device, aborting\n");
Michal Simek7a3e2582013-06-04 00:03:27 +00001159 goto error;
John Linnbb81b2d2009-08-20 02:52:16 -07001160 }
1161
1162 dev_info(dev,
1163 "Xilinx EmacLite at 0x%08X mapped to 0x%08X, irq=%d\n",
1164 (unsigned int __force)ndev->mem_start,
1165 (unsigned int __force)lp->base_addr, ndev->irq);
1166 return 0;
1167
Michal Simek7a3e2582013-06-04 00:03:27 +00001168error:
Tobias Klauser60f9b5e2017-01-05 10:41:36 +01001169 free_netdev(ndev);
John Linnbb81b2d2009-08-20 02:52:16 -07001170 return rc;
1171}
1172
1173/**
1174 * xemaclite_of_remove - Unbind the driver from the Emaclite device.
1175 * @of_dev: Pointer to OF device structure
1176 *
1177 * This function is called if a device is physically removed from the system or
1178 * if the driver module is being unloaded. It frees any resources allocated to
1179 * the device.
1180 *
1181 * Return: 0, always.
1182 */
Bill Pemberton06b0e682012-12-03 09:24:08 -05001183static int xemaclite_of_remove(struct platform_device *of_dev)
John Linnbb81b2d2009-08-20 02:52:16 -07001184{
Libo Chen34e01842013-08-19 20:00:25 +08001185 struct net_device *ndev = platform_get_drvdata(of_dev);
John Linnbb81b2d2009-08-20 02:52:16 -07001186
Joe Perchesece49152010-11-15 11:12:31 +00001187 struct net_local *lp = netdev_priv(ndev);
John Linn5cdaaa12010-02-15 21:51:00 -08001188
1189 /* Un-register the mii_bus, if configured */
Radhey Shyam Pandey27cad002018-06-13 12:05:17 +05301190 if (lp->mii_bus) {
John Linn5cdaaa12010-02-15 21:51:00 -08001191 mdiobus_unregister(lp->mii_bus);
John Linn5cdaaa12010-02-15 21:51:00 -08001192 mdiobus_free(lp->mii_bus);
1193 lp->mii_bus = NULL;
1194 }
1195
John Linnbb81b2d2009-08-20 02:52:16 -07001196 unregister_netdev(ndev);
1197
Markus Elfring38a90e72014-11-20 14:47:12 +01001198 of_node_put(lp->phy_node);
John Linn5cdaaa12010-02-15 21:51:00 -08001199 lp->phy_node = NULL;
1200
Tobias Klauser60f9b5e2017-01-05 10:41:36 +01001201 free_netdev(ndev);
John Linnbb81b2d2009-08-20 02:52:16 -07001202
1203 return 0;
1204}
1205
Michal Simek357e8b5f2010-08-18 01:22:49 +00001206#ifdef CONFIG_NET_POLL_CONTROLLER
1207static void
1208xemaclite_poll_controller(struct net_device *ndev)
1209{
1210 disable_irq(ndev->irq);
1211 xemaclite_interrupt(ndev->irq, ndev);
1212 enable_irq(ndev->irq);
1213}
1214#endif
1215
Bhumika Goyal10eeb5e2017-01-21 12:28:58 +05301216static const struct net_device_ops xemaclite_netdev_ops = {
John Linnbb81b2d2009-08-20 02:52:16 -07001217 .ndo_open = xemaclite_open,
1218 .ndo_stop = xemaclite_close,
1219 .ndo_start_xmit = xemaclite_send,
John Linn5cdaaa12010-02-15 21:51:00 -08001220 .ndo_set_mac_address = xemaclite_set_mac_address,
John Linnbb81b2d2009-08-20 02:52:16 -07001221 .ndo_tx_timeout = xemaclite_tx_timeout,
Michal Simek357e8b5f2010-08-18 01:22:49 +00001222#ifdef CONFIG_NET_POLL_CONTROLLER
1223 .ndo_poll_controller = xemaclite_poll_controller,
1224#endif
John Linnbb81b2d2009-08-20 02:52:16 -07001225};
1226
1227/* Match table for OF platform binding */
Fabian Frederick74847f22015-03-17 19:37:40 +01001228static const struct of_device_id xemaclite_of_match[] = {
John Linnbb81b2d2009-08-20 02:52:16 -07001229 { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
1230 { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
1231 { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
1232 { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
1233 { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
John Linn5cdaaa12010-02-15 21:51:00 -08001234 { .compatible = "xlnx,xps-ethernetlite-3.00.a", },
John Linnbb81b2d2009-08-20 02:52:16 -07001235 { /* end of list */ },
1236};
1237MODULE_DEVICE_TABLE(of, xemaclite_of_match);
1238
Grant Likely74888762011-02-22 21:05:51 -07001239static struct platform_driver xemaclite_of_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001240 .driver = {
1241 .name = DRIVER_NAME,
Grant Likely40182942010-04-13 16:13:02 -07001242 .of_match_table = xemaclite_of_match,
1243 },
John Linnbb81b2d2009-08-20 02:52:16 -07001244 .probe = xemaclite_of_probe,
Bill Pemberton06b0e682012-12-03 09:24:08 -05001245 .remove = xemaclite_of_remove,
John Linnbb81b2d2009-08-20 02:52:16 -07001246};
1247
Axel Lindb62f682011-11-27 16:44:17 +00001248module_platform_driver(xemaclite_of_driver);
John Linnbb81b2d2009-08-20 02:52:16 -07001249
1250MODULE_AUTHOR("Xilinx, Inc.");
1251MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1252MODULE_LICENSE("GPL");