blob: a4840952d372d6c763610cbce559dbafed7f9573 [file] [log] [blame]
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001/*
2 * Xilinx Axi Ethernet device driver
3 *
4 * Copyright (c) 2008 Nissin Systems Co., Ltd., Yoshio Kashiwagi
5 * Copyright (c) 2005-2008 DLA Systems, David H. Lynch Jr. <dhlii@dlasys.net>
6 * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
Michal Simek59a54f32012-04-12 01:11:12 +00007 * Copyright (c) 2010 - 2011 Michal Simek <monstr@monstr.eu>
8 * Copyright (c) 2010 - 2011 PetaLogix
9 * Copyright (c) 2010 - 2012 Xilinx, Inc. All rights reserved.
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +000010 *
11 * This is a driver for the Xilinx Axi Ethernet which is used in the Virtex6
12 * and Spartan6.
13 *
14 * TODO:
15 * - Add Axi Fifo support.
16 * - Factor out Axi DMA code into separate driver.
17 * - Test and fix basic multicast filtering.
18 * - Add support for extended multicast filtering.
19 * - Test basic VLAN support.
20 * - Add support for extended VLAN support.
21 */
22
23#include <linux/delay.h>
24#include <linux/etherdevice.h>
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +000025#include <linux/module.h>
26#include <linux/netdevice.h>
27#include <linux/of_mdio.h>
28#include <linux/of_platform.h>
Michal Simek9d5e8ec2014-02-13 08:10:42 +010029#include <linux/of_irq.h>
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +000030#include <linux/of_address.h>
31#include <linux/skbuff.h>
32#include <linux/spinlock.h>
33#include <linux/phy.h>
34#include <linux/mii.h>
35#include <linux/ethtool.h>
36
37#include "xilinx_axienet.h"
38
39/* Descriptors defines for Tx and Rx DMA - 2^n for the best performance */
40#define TX_BD_NUM 64
41#define RX_BD_NUM 128
42
43/* Must be shorter than length of ethtool_drvinfo.driver field to fit */
44#define DRIVER_NAME "xaxienet"
45#define DRIVER_DESCRIPTION "Xilinx Axi Ethernet driver"
46#define DRIVER_VERSION "1.00a"
47
48#define AXIENET_REGS_N 32
49
50/* Match table for of_platform binding */
Fabian Frederick74847f22015-03-17 19:37:40 +010051static const struct of_device_id axienet_of_match[] = {
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +000052 { .compatible = "xlnx,axi-ethernet-1.00.a", },
53 { .compatible = "xlnx,axi-ethernet-1.01.a", },
54 { .compatible = "xlnx,axi-ethernet-2.01.a", },
55 {},
56};
57
58MODULE_DEVICE_TABLE(of, axienet_of_match);
59
60/* Option table for setting up Axi Ethernet hardware options */
61static struct axienet_option axienet_options[] = {
62 /* Turn on jumbo packet support for both Rx and Tx */
63 {
64 .opt = XAE_OPTION_JUMBO,
65 .reg = XAE_TC_OFFSET,
66 .m_or = XAE_TC_JUM_MASK,
67 }, {
68 .opt = XAE_OPTION_JUMBO,
69 .reg = XAE_RCW1_OFFSET,
70 .m_or = XAE_RCW1_JUM_MASK,
71 }, { /* Turn on VLAN packet support for both Rx and Tx */
72 .opt = XAE_OPTION_VLAN,
73 .reg = XAE_TC_OFFSET,
74 .m_or = XAE_TC_VLAN_MASK,
75 }, {
76 .opt = XAE_OPTION_VLAN,
77 .reg = XAE_RCW1_OFFSET,
78 .m_or = XAE_RCW1_VLAN_MASK,
79 }, { /* Turn on FCS stripping on receive packets */
80 .opt = XAE_OPTION_FCS_STRIP,
81 .reg = XAE_RCW1_OFFSET,
82 .m_or = XAE_RCW1_FCS_MASK,
83 }, { /* Turn on FCS insertion on transmit packets */
84 .opt = XAE_OPTION_FCS_INSERT,
85 .reg = XAE_TC_OFFSET,
86 .m_or = XAE_TC_FCS_MASK,
87 }, { /* Turn off length/type field checking on receive packets */
88 .opt = XAE_OPTION_LENTYPE_ERR,
89 .reg = XAE_RCW1_OFFSET,
90 .m_or = XAE_RCW1_LT_DIS_MASK,
91 }, { /* Turn on Rx flow control */
92 .opt = XAE_OPTION_FLOW_CONTROL,
93 .reg = XAE_FCC_OFFSET,
94 .m_or = XAE_FCC_FCRX_MASK,
95 }, { /* Turn on Tx flow control */
96 .opt = XAE_OPTION_FLOW_CONTROL,
97 .reg = XAE_FCC_OFFSET,
98 .m_or = XAE_FCC_FCTX_MASK,
99 }, { /* Turn on promiscuous frame filtering */
100 .opt = XAE_OPTION_PROMISC,
101 .reg = XAE_FMI_OFFSET,
102 .m_or = XAE_FMI_PM_MASK,
103 }, { /* Enable transmitter */
104 .opt = XAE_OPTION_TXEN,
105 .reg = XAE_TC_OFFSET,
106 .m_or = XAE_TC_TX_MASK,
107 }, { /* Enable receiver */
108 .opt = XAE_OPTION_RXEN,
109 .reg = XAE_RCW1_OFFSET,
110 .m_or = XAE_RCW1_RX_MASK,
111 },
112 {}
113};
114
115/**
116 * axienet_dma_in32 - Memory mapped Axi DMA register read
117 * @lp: Pointer to axienet local structure
118 * @reg: Address offset from the base address of the Axi DMA core
119 *
120 * returns: The contents of the Axi DMA register
121 *
122 * This function returns the contents of the corresponding Axi DMA register.
123 */
124static inline u32 axienet_dma_in32(struct axienet_local *lp, off_t reg)
125{
126 return in_be32(lp->dma_regs + reg);
127}
128
129/**
130 * axienet_dma_out32 - Memory mapped Axi DMA register write.
131 * @lp: Pointer to axienet local structure
132 * @reg: Address offset from the base address of the Axi DMA core
133 * @value: Value to be written into the Axi DMA register
134 *
135 * This function writes the desired value into the corresponding Axi DMA
136 * register.
137 */
138static inline void axienet_dma_out32(struct axienet_local *lp,
139 off_t reg, u32 value)
140{
141 out_be32((lp->dma_regs + reg), value);
142}
143
144/**
145 * axienet_dma_bd_release - Release buffer descriptor rings
146 * @ndev: Pointer to the net_device structure
147 *
148 * This function is used to release the descriptors allocated in
149 * axienet_dma_bd_init. axienet_dma_bd_release is called when Axi Ethernet
150 * driver stop api is called.
151 */
152static void axienet_dma_bd_release(struct net_device *ndev)
153{
154 int i;
155 struct axienet_local *lp = netdev_priv(ndev);
156
157 for (i = 0; i < RX_BD_NUM; i++) {
158 dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
159 lp->max_frm_size, DMA_FROM_DEVICE);
160 dev_kfree_skb((struct sk_buff *)
161 (lp->rx_bd_v[i].sw_id_offset));
162 }
163
164 if (lp->rx_bd_v) {
165 dma_free_coherent(ndev->dev.parent,
166 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
167 lp->rx_bd_v,
168 lp->rx_bd_p);
169 }
170 if (lp->tx_bd_v) {
171 dma_free_coherent(ndev->dev.parent,
172 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
173 lp->tx_bd_v,
174 lp->tx_bd_p);
175 }
176}
177
178/**
179 * axienet_dma_bd_init - Setup buffer descriptor rings for Axi DMA
180 * @ndev: Pointer to the net_device structure
181 *
182 * returns: 0, on success
183 * -ENOMEM, on failure
184 *
185 * This function is called to initialize the Rx and Tx DMA descriptor
186 * rings. This initializes the descriptors with required default values
187 * and is called when Axi Ethernet driver reset is called.
188 */
189static int axienet_dma_bd_init(struct net_device *ndev)
190{
191 u32 cr;
192 int i;
193 struct sk_buff *skb;
194 struct axienet_local *lp = netdev_priv(ndev);
195
196 /* Reset the indexes which are used for accessing the BDs */
197 lp->tx_bd_ci = 0;
198 lp->tx_bd_tail = 0;
199 lp->rx_bd_ci = 0;
200
201 /*
202 * Allocate the Tx and Rx buffer descriptors.
203 */
Joe Perchesede23fa82013-08-26 22:45:23 -0700204 lp->tx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
205 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
206 &lp->tx_bd_p, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000207 if (!lp->tx_bd_v)
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000208 goto out;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000209
Joe Perchesede23fa82013-08-26 22:45:23 -0700210 lp->rx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
211 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
212 &lp->rx_bd_p, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000213 if (!lp->rx_bd_v)
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000214 goto out;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000215
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000216 for (i = 0; i < TX_BD_NUM; i++) {
217 lp->tx_bd_v[i].next = lp->tx_bd_p +
218 sizeof(*lp->tx_bd_v) *
219 ((i + 1) % TX_BD_NUM);
220 }
221
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000222 for (i = 0; i < RX_BD_NUM; i++) {
223 lp->rx_bd_v[i].next = lp->rx_bd_p +
224 sizeof(*lp->rx_bd_v) *
225 ((i + 1) % RX_BD_NUM);
226
227 skb = netdev_alloc_skb_ip_align(ndev, lp->max_frm_size);
Joe Perches720a43e2013-03-08 15:03:25 +0000228 if (!skb)
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000229 goto out;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000230
231 lp->rx_bd_v[i].sw_id_offset = (u32) skb;
232 lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
233 skb->data,
234 lp->max_frm_size,
235 DMA_FROM_DEVICE);
236 lp->rx_bd_v[i].cntrl = lp->max_frm_size;
237 }
238
239 /* Start updating the Rx channel control register */
240 cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
241 /* Update the interrupt coalesce count */
242 cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
243 ((lp->coalesce_count_rx) << XAXIDMA_COALESCE_SHIFT));
244 /* Update the delay timer count */
245 cr = ((cr & ~XAXIDMA_DELAY_MASK) |
246 (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
247 /* Enable coalesce, delay timer and error interrupts */
248 cr |= XAXIDMA_IRQ_ALL_MASK;
249 /* Write to the Rx channel control register */
250 axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
251
252 /* Start updating the Tx channel control register */
253 cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
254 /* Update the interrupt coalesce count */
255 cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
256 ((lp->coalesce_count_tx) << XAXIDMA_COALESCE_SHIFT));
257 /* Update the delay timer count */
258 cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
259 (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
260 /* Enable coalesce, delay timer and error interrupts */
261 cr |= XAXIDMA_IRQ_ALL_MASK;
262 /* Write to the Tx channel control register */
263 axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
264
265 /* Populate the tail pointer and bring the Rx Axi DMA engine out of
266 * halted state. This will make the Rx side ready for reception.*/
267 axienet_dma_out32(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
268 cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
269 axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET,
270 cr | XAXIDMA_CR_RUNSTOP_MASK);
271 axienet_dma_out32(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
272 (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
273
274 /* Write to the RS (Run-stop) bit in the Tx channel control register.
275 * Tx channel is now ready to run. But only after we write to the
276 * tail pointer register that the Tx channel will start transmitting */
277 axienet_dma_out32(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
278 cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
279 axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET,
280 cr | XAXIDMA_CR_RUNSTOP_MASK);
281
282 return 0;
283out:
284 axienet_dma_bd_release(ndev);
285 return -ENOMEM;
286}
287
288/**
289 * axienet_set_mac_address - Write the MAC address
290 * @ndev: Pointer to the net_device structure
291 * @address: 6 byte Address to be written as MAC address
292 *
293 * This function is called to initialize the MAC address of the Axi Ethernet
294 * core. It writes to the UAW0 and UAW1 registers of the core.
295 */
296static void axienet_set_mac_address(struct net_device *ndev, void *address)
297{
298 struct axienet_local *lp = netdev_priv(ndev);
299
300 if (address)
301 memcpy(ndev->dev_addr, address, ETH_ALEN);
302 if (!is_valid_ether_addr(ndev->dev_addr))
Joe Perches7efd26d2012-07-12 19:33:06 +0000303 eth_random_addr(ndev->dev_addr);
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000304
305 /* Set up unicast MAC address filter set its mac address */
306 axienet_iow(lp, XAE_UAW0_OFFSET,
307 (ndev->dev_addr[0]) |
308 (ndev->dev_addr[1] << 8) |
309 (ndev->dev_addr[2] << 16) |
310 (ndev->dev_addr[3] << 24));
311 axienet_iow(lp, XAE_UAW1_OFFSET,
312 (((axienet_ior(lp, XAE_UAW1_OFFSET)) &
313 ~XAE_UAW1_UNICASTADDR_MASK) |
314 (ndev->dev_addr[4] |
315 (ndev->dev_addr[5] << 8))));
316}
317
318/**
319 * netdev_set_mac_address - Write the MAC address (from outside the driver)
320 * @ndev: Pointer to the net_device structure
321 * @p: 6 byte Address to be written as MAC address
322 *
323 * returns: 0 for all conditions. Presently, there is no failure case.
324 *
325 * This function is called to initialize the MAC address of the Axi Ethernet
326 * core. It calls the core specific axienet_set_mac_address. This is the
327 * function that goes into net_device_ops structure entry ndo_set_mac_address.
328 */
329static int netdev_set_mac_address(struct net_device *ndev, void *p)
330{
331 struct sockaddr *addr = p;
332 axienet_set_mac_address(ndev, addr->sa_data);
333 return 0;
334}
335
336/**
337 * axienet_set_multicast_list - Prepare the multicast table
338 * @ndev: Pointer to the net_device structure
339 *
340 * This function is called to initialize the multicast table during
341 * initialization. The Axi Ethernet basic multicast support has a four-entry
342 * multicast table which is initialized here. Additionally this function
343 * goes into the net_device_ops structure entry ndo_set_multicast_list. This
344 * means whenever the multicast table entries need to be updated this
345 * function gets called.
346 */
347static void axienet_set_multicast_list(struct net_device *ndev)
348{
349 int i;
350 u32 reg, af0reg, af1reg;
351 struct axienet_local *lp = netdev_priv(ndev);
352
353 if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
354 netdev_mc_count(ndev) > XAE_MULTICAST_CAM_TABLE_NUM) {
355 /* We must make the kernel realize we had to move into
356 * promiscuous mode. If it was a promiscuous mode request
357 * the flag is already set. If not we set it. */
358 ndev->flags |= IFF_PROMISC;
359 reg = axienet_ior(lp, XAE_FMI_OFFSET);
360 reg |= XAE_FMI_PM_MASK;
361 axienet_iow(lp, XAE_FMI_OFFSET, reg);
362 dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
363 } else if (!netdev_mc_empty(ndev)) {
364 struct netdev_hw_addr *ha;
365
366 i = 0;
367 netdev_for_each_mc_addr(ha, ndev) {
368 if (i >= XAE_MULTICAST_CAM_TABLE_NUM)
369 break;
370
371 af0reg = (ha->addr[0]);
372 af0reg |= (ha->addr[1] << 8);
373 af0reg |= (ha->addr[2] << 16);
374 af0reg |= (ha->addr[3] << 24);
375
376 af1reg = (ha->addr[4]);
377 af1reg |= (ha->addr[5] << 8);
378
379 reg = axienet_ior(lp, XAE_FMI_OFFSET) & 0xFFFFFF00;
380 reg |= i;
381
382 axienet_iow(lp, XAE_FMI_OFFSET, reg);
383 axienet_iow(lp, XAE_AF0_OFFSET, af0reg);
384 axienet_iow(lp, XAE_AF1_OFFSET, af1reg);
385 i++;
386 }
387 } else {
388 reg = axienet_ior(lp, XAE_FMI_OFFSET);
389 reg &= ~XAE_FMI_PM_MASK;
390
391 axienet_iow(lp, XAE_FMI_OFFSET, reg);
392
393 for (i = 0; i < XAE_MULTICAST_CAM_TABLE_NUM; i++) {
394 reg = axienet_ior(lp, XAE_FMI_OFFSET) & 0xFFFFFF00;
395 reg |= i;
396
397 axienet_iow(lp, XAE_FMI_OFFSET, reg);
398 axienet_iow(lp, XAE_AF0_OFFSET, 0);
399 axienet_iow(lp, XAE_AF1_OFFSET, 0);
400 }
401
402 dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
403 }
404}
405
406/**
407 * axienet_setoptions - Set an Axi Ethernet option
408 * @ndev: Pointer to the net_device structure
409 * @options: Option to be enabled/disabled
410 *
411 * The Axi Ethernet core has multiple features which can be selectively turned
412 * on or off. The typical options could be jumbo frame option, basic VLAN
413 * option, promiscuous mode option etc. This function is used to set or clear
414 * these options in the Axi Ethernet hardware. This is done through
415 * axienet_option structure .
416 */
417static void axienet_setoptions(struct net_device *ndev, u32 options)
418{
419 int reg;
420 struct axienet_local *lp = netdev_priv(ndev);
421 struct axienet_option *tp = &axienet_options[0];
422
423 while (tp->opt) {
424 reg = ((axienet_ior(lp, tp->reg)) & ~(tp->m_or));
425 if (options & tp->opt)
426 reg |= tp->m_or;
427 axienet_iow(lp, tp->reg, reg);
428 tp++;
429 }
430
431 lp->options |= options;
432}
433
434static void __axienet_device_reset(struct axienet_local *lp,
435 struct device *dev, off_t offset)
436{
437 u32 timeout;
438 /* Reset Axi DMA. This would reset Axi Ethernet core as well. The reset
439 * process of Axi DMA takes a while to complete as all pending
440 * commands/transfers will be flushed or completed during this
441 * reset process. */
442 axienet_dma_out32(lp, offset, XAXIDMA_CR_RESET_MASK);
443 timeout = DELAY_OF_ONE_MILLISEC;
444 while (axienet_dma_in32(lp, offset) & XAXIDMA_CR_RESET_MASK) {
445 udelay(1);
446 if (--timeout == 0) {
447 dev_err(dev, "axienet_device_reset DMA "
448 "reset timeout!\n");
449 break;
450 }
451 }
452}
453
454/**
455 * axienet_device_reset - Reset and initialize the Axi Ethernet hardware.
456 * @ndev: Pointer to the net_device structure
457 *
458 * This function is called to reset and initialize the Axi Ethernet core. This
459 * is typically called during initialization. It does a reset of the Axi DMA
460 * Rx/Tx channels and initializes the Axi DMA BDs. Since Axi DMA reset lines
461 * areconnected to Axi Ethernet reset lines, this in turn resets the Axi
462 * Ethernet core. No separate hardware reset is done for the Axi Ethernet
463 * core.
464 */
465static void axienet_device_reset(struct net_device *ndev)
466{
467 u32 axienet_status;
468 struct axienet_local *lp = netdev_priv(ndev);
469
470 __axienet_device_reset(lp, &ndev->dev, XAXIDMA_TX_CR_OFFSET);
471 __axienet_device_reset(lp, &ndev->dev, XAXIDMA_RX_CR_OFFSET);
472
473 lp->max_frm_size = XAE_MAX_VLAN_FRAME_SIZE;
474 lp->options &= (~XAE_OPTION_JUMBO);
475
476 if ((ndev->mtu > XAE_MTU) &&
477 (ndev->mtu <= XAE_JUMBO_MTU) &&
478 (lp->jumbo_support)) {
479 lp->max_frm_size = ndev->mtu + XAE_HDR_VLAN_SIZE +
480 XAE_TRL_SIZE;
481 lp->options |= XAE_OPTION_JUMBO;
482 }
483
484 if (axienet_dma_bd_init(ndev)) {
485 dev_err(&ndev->dev, "axienet_device_reset descriptor "
486 "allocation failed\n");
487 }
488
489 axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
490 axienet_status &= ~XAE_RCW1_RX_MASK;
491 axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
492
493 axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
494 if (axienet_status & XAE_INT_RXRJECT_MASK)
495 axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
496
497 axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
498
499 /* Sync default options with HW but leave receiver and
500 * transmitter disabled.*/
501 axienet_setoptions(ndev, lp->options &
502 ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
503 axienet_set_mac_address(ndev, NULL);
504 axienet_set_multicast_list(ndev);
505 axienet_setoptions(ndev, lp->options);
506
507 ndev->trans_start = jiffies;
508}
509
510/**
511 * axienet_adjust_link - Adjust the PHY link speed/duplex.
512 * @ndev: Pointer to the net_device structure
513 *
514 * This function is called to change the speed and duplex setting after
515 * auto negotiation is done by the PHY. This is the function that gets
516 * registered with the PHY interface through the "of_phy_connect" call.
517 */
518static void axienet_adjust_link(struct net_device *ndev)
519{
520 u32 emmc_reg;
521 u32 link_state;
522 u32 setspeed = 1;
523 struct axienet_local *lp = netdev_priv(ndev);
524 struct phy_device *phy = lp->phy_dev;
525
526 link_state = phy->speed | (phy->duplex << 1) | phy->link;
527 if (lp->last_link != link_state) {
528 if ((phy->speed == SPEED_10) || (phy->speed == SPEED_100)) {
529 if (lp->phy_type == XAE_PHY_TYPE_1000BASE_X)
530 setspeed = 0;
531 } else {
532 if ((phy->speed == SPEED_1000) &&
533 (lp->phy_type == XAE_PHY_TYPE_MII))
534 setspeed = 0;
535 }
536
537 if (setspeed == 1) {
538 emmc_reg = axienet_ior(lp, XAE_EMMC_OFFSET);
539 emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
540
541 switch (phy->speed) {
542 case SPEED_1000:
543 emmc_reg |= XAE_EMMC_LINKSPD_1000;
544 break;
545 case SPEED_100:
546 emmc_reg |= XAE_EMMC_LINKSPD_100;
547 break;
548 case SPEED_10:
549 emmc_reg |= XAE_EMMC_LINKSPD_10;
550 break;
551 default:
552 dev_err(&ndev->dev, "Speed other than 10, 100 "
553 "or 1Gbps is not supported\n");
554 break;
555 }
556
557 axienet_iow(lp, XAE_EMMC_OFFSET, emmc_reg);
558 lp->last_link = link_state;
559 phy_print_status(phy);
560 } else {
561 dev_err(&ndev->dev, "Error setting Axi Ethernet "
562 "mac speed\n");
563 }
564 }
565}
566
567/**
568 * axienet_start_xmit_done - Invoked once a transmit is completed by the
569 * Axi DMA Tx channel.
570 * @ndev: Pointer to the net_device structure
571 *
572 * This function is invoked from the Axi DMA Tx isr to notify the completion
573 * of transmit operation. It clears fields in the corresponding Tx BDs and
574 * unmaps the corresponding buffer so that CPU can regain ownership of the
575 * buffer. It finally invokes "netif_wake_queue" to restart transmission if
576 * required.
577 */
578static void axienet_start_xmit_done(struct net_device *ndev)
579{
580 u32 size = 0;
581 u32 packets = 0;
582 struct axienet_local *lp = netdev_priv(ndev);
583 struct axidma_bd *cur_p;
584 unsigned int status = 0;
585
586 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
587 status = cur_p->status;
588 while (status & XAXIDMA_BD_STS_COMPLETE_MASK) {
589 dma_unmap_single(ndev->dev.parent, cur_p->phys,
590 (cur_p->cntrl & XAXIDMA_BD_CTRL_LENGTH_MASK),
591 DMA_TO_DEVICE);
592 if (cur_p->app4)
593 dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
594 /*cur_p->phys = 0;*/
595 cur_p->app0 = 0;
596 cur_p->app1 = 0;
597 cur_p->app2 = 0;
598 cur_p->app4 = 0;
599 cur_p->status = 0;
600
601 size += status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
602 packets++;
603
Michal Simek91ff37f2014-02-13 08:10:43 +0100604 ++lp->tx_bd_ci;
605 lp->tx_bd_ci %= TX_BD_NUM;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000606 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
607 status = cur_p->status;
608 }
609
610 ndev->stats.tx_packets += packets;
611 ndev->stats.tx_bytes += size;
612 netif_wake_queue(ndev);
613}
614
615/**
616 * axienet_check_tx_bd_space - Checks if a BD/group of BDs are currently busy
617 * @lp: Pointer to the axienet_local structure
618 * @num_frag: The number of BDs to check for
619 *
620 * returns: 0, on success
621 * NETDEV_TX_BUSY, if any of the descriptors are not free
622 *
623 * This function is invoked before BDs are allocated and transmission starts.
624 * This function returns 0 if a BD or group of BDs can be allocated for
625 * transmission. If the BD or any of the BDs are not free the function
626 * returns a busy status. This is invoked from axienet_start_xmit.
627 */
628static inline int axienet_check_tx_bd_space(struct axienet_local *lp,
629 int num_frag)
630{
631 struct axidma_bd *cur_p;
632 cur_p = &lp->tx_bd_v[(lp->tx_bd_tail + num_frag) % TX_BD_NUM];
633 if (cur_p->status & XAXIDMA_BD_STS_ALL_MASK)
634 return NETDEV_TX_BUSY;
635 return 0;
636}
637
638/**
639 * axienet_start_xmit - Starts the transmission.
640 * @skb: sk_buff pointer that contains data to be Txed.
641 * @ndev: Pointer to net_device structure.
642 *
643 * returns: NETDEV_TX_OK, on success
644 * NETDEV_TX_BUSY, if any of the descriptors are not free
645 *
646 * This function is invoked from upper layers to initiate transmission. The
647 * function uses the next available free BDs and populates their fields to
648 * start the transmission. Additionally if checksum offloading is supported,
649 * it populates AXI Stream Control fields with appropriate values.
650 */
651static int axienet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
652{
653 u32 ii;
654 u32 num_frag;
655 u32 csum_start_off;
656 u32 csum_index_off;
657 skb_frag_t *frag;
658 dma_addr_t tail_p;
659 struct axienet_local *lp = netdev_priv(ndev);
660 struct axidma_bd *cur_p;
661
662 num_frag = skb_shinfo(skb)->nr_frags;
663 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
664
665 if (axienet_check_tx_bd_space(lp, num_frag)) {
666 if (!netif_queue_stopped(ndev))
667 netif_stop_queue(ndev);
668 return NETDEV_TX_BUSY;
669 }
670
671 if (skb->ip_summed == CHECKSUM_PARTIAL) {
672 if (lp->features & XAE_FEATURE_FULL_TX_CSUM) {
673 /* Tx Full Checksum Offload Enabled */
674 cur_p->app0 |= 2;
675 } else if (lp->features & XAE_FEATURE_PARTIAL_RX_CSUM) {
676 csum_start_off = skb_transport_offset(skb);
677 csum_index_off = csum_start_off + skb->csum_offset;
678 /* Tx Partial Checksum Offload Enabled */
679 cur_p->app0 |= 1;
680 cur_p->app1 = (csum_start_off << 16) | csum_index_off;
681 }
682 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
683 cur_p->app0 |= 2; /* Tx Full Checksum Offload Enabled */
684 }
685
686 cur_p->cntrl = skb_headlen(skb) | XAXIDMA_BD_CTRL_TXSOF_MASK;
687 cur_p->phys = dma_map_single(ndev->dev.parent, skb->data,
688 skb_headlen(skb), DMA_TO_DEVICE);
689
690 for (ii = 0; ii < num_frag; ii++) {
Michal Simek91ff37f2014-02-13 08:10:43 +0100691 ++lp->tx_bd_tail;
692 lp->tx_bd_tail %= TX_BD_NUM;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000693 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
694 frag = &skb_shinfo(skb)->frags[ii];
695 cur_p->phys = dma_map_single(ndev->dev.parent,
696 skb_frag_address(frag),
697 skb_frag_size(frag),
698 DMA_TO_DEVICE);
699 cur_p->cntrl = skb_frag_size(frag);
700 }
701
702 cur_p->cntrl |= XAXIDMA_BD_CTRL_TXEOF_MASK;
703 cur_p->app4 = (unsigned long)skb;
704
705 tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
706 /* Start the transfer */
707 axienet_dma_out32(lp, XAXIDMA_TX_TDESC_OFFSET, tail_p);
Michal Simek91ff37f2014-02-13 08:10:43 +0100708 ++lp->tx_bd_tail;
709 lp->tx_bd_tail %= TX_BD_NUM;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000710
711 return NETDEV_TX_OK;
712}
713
714/**
715 * axienet_recv - Is called from Axi DMA Rx Isr to complete the received
716 * BD processing.
717 * @ndev: Pointer to net_device structure.
718 *
719 * This function is invoked from the Axi DMA Rx isr to process the Rx BDs. It
720 * does minimal processing and invokes "netif_rx" to complete further
721 * processing.
722 */
723static void axienet_recv(struct net_device *ndev)
724{
725 u32 length;
726 u32 csumstatus;
727 u32 size = 0;
728 u32 packets = 0;
Peter Crosthwaite38e96b32015-05-05 11:25:55 +0200729 dma_addr_t tail_p = 0;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000730 struct axienet_local *lp = netdev_priv(ndev);
731 struct sk_buff *skb, *new_skb;
732 struct axidma_bd *cur_p;
733
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000734 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
735
736 while ((cur_p->status & XAXIDMA_BD_STS_COMPLETE_MASK)) {
Peter Crosthwaite38e96b32015-05-05 11:25:55 +0200737 tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000738 skb = (struct sk_buff *) (cur_p->sw_id_offset);
739 length = cur_p->app4 & 0x0000FFFF;
740
741 dma_unmap_single(ndev->dev.parent, cur_p->phys,
742 lp->max_frm_size,
743 DMA_FROM_DEVICE);
744
745 skb_put(skb, length);
746 skb->protocol = eth_type_trans(skb, ndev);
747 /*skb_checksum_none_assert(skb);*/
748 skb->ip_summed = CHECKSUM_NONE;
749
750 /* if we're doing Rx csum offload, set it up */
751 if (lp->features & XAE_FEATURE_FULL_RX_CSUM) {
752 csumstatus = (cur_p->app2 &
753 XAE_FULL_CSUM_STATUS_MASK) >> 3;
754 if ((csumstatus == XAE_IP_TCP_CSUM_VALIDATED) ||
755 (csumstatus == XAE_IP_UDP_CSUM_VALIDATED)) {
756 skb->ip_summed = CHECKSUM_UNNECESSARY;
757 }
758 } else if ((lp->features & XAE_FEATURE_PARTIAL_RX_CSUM) != 0 &&
Joe Perchesceffc4a2014-03-12 10:22:36 -0700759 skb->protocol == htons(ETH_P_IP) &&
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000760 skb->len > 64) {
761 skb->csum = be32_to_cpu(cur_p->app3 & 0xFFFF);
762 skb->ip_summed = CHECKSUM_COMPLETE;
763 }
764
765 netif_rx(skb);
766
767 size += length;
768 packets++;
769
770 new_skb = netdev_alloc_skb_ip_align(ndev, lp->max_frm_size);
Joe Perches720a43e2013-03-08 15:03:25 +0000771 if (!new_skb)
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000772 return;
Joe Perches720a43e2013-03-08 15:03:25 +0000773
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000774 cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
775 lp->max_frm_size,
776 DMA_FROM_DEVICE);
777 cur_p->cntrl = lp->max_frm_size;
778 cur_p->status = 0;
779 cur_p->sw_id_offset = (u32) new_skb;
780
Michal Simek91ff37f2014-02-13 08:10:43 +0100781 ++lp->rx_bd_ci;
782 lp->rx_bd_ci %= RX_BD_NUM;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000783 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
784 }
785
786 ndev->stats.rx_packets += packets;
787 ndev->stats.rx_bytes += size;
788
Peter Crosthwaite38e96b32015-05-05 11:25:55 +0200789 if (tail_p)
790 axienet_dma_out32(lp, XAXIDMA_RX_TDESC_OFFSET, tail_p);
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000791}
792
793/**
794 * axienet_tx_irq - Tx Done Isr.
795 * @irq: irq number
796 * @_ndev: net_device pointer
797 *
798 * returns: IRQ_HANDLED for all cases.
799 *
800 * This is the Axi DMA Tx done Isr. It invokes "axienet_start_xmit_done"
801 * to complete the BD processing.
802 */
803static irqreturn_t axienet_tx_irq(int irq, void *_ndev)
804{
805 u32 cr;
806 unsigned int status;
807 struct net_device *ndev = _ndev;
808 struct axienet_local *lp = netdev_priv(ndev);
809
810 status = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
811 if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
812 axienet_start_xmit_done(lp->ndev);
813 goto out;
814 }
815 if (!(status & XAXIDMA_IRQ_ALL_MASK))
816 dev_err(&ndev->dev, "No interrupts asserted in Tx path");
817 if (status & XAXIDMA_IRQ_ERROR_MASK) {
818 dev_err(&ndev->dev, "DMA Tx error 0x%x\n", status);
819 dev_err(&ndev->dev, "Current BD is at: 0x%x\n",
820 (lp->tx_bd_v[lp->tx_bd_ci]).phys);
821
822 cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
823 /* Disable coalesce, delay timer and error interrupts */
824 cr &= (~XAXIDMA_IRQ_ALL_MASK);
825 /* Write to the Tx channel control register */
826 axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
827
828 cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
829 /* Disable coalesce, delay timer and error interrupts */
830 cr &= (~XAXIDMA_IRQ_ALL_MASK);
831 /* Write to the Rx channel control register */
832 axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
833
834 tasklet_schedule(&lp->dma_err_tasklet);
835 }
836out:
837 axienet_dma_out32(lp, XAXIDMA_TX_SR_OFFSET, status);
838 return IRQ_HANDLED;
839}
840
841/**
842 * axienet_rx_irq - Rx Isr.
843 * @irq: irq number
844 * @_ndev: net_device pointer
845 *
846 * returns: IRQ_HANDLED for all cases.
847 *
848 * This is the Axi DMA Rx Isr. It invokes "axienet_recv" to complete the BD
849 * processing.
850 */
851static irqreturn_t axienet_rx_irq(int irq, void *_ndev)
852{
853 u32 cr;
854 unsigned int status;
855 struct net_device *ndev = _ndev;
856 struct axienet_local *lp = netdev_priv(ndev);
857
858 status = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
859 if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
860 axienet_recv(lp->ndev);
861 goto out;
862 }
863 if (!(status & XAXIDMA_IRQ_ALL_MASK))
864 dev_err(&ndev->dev, "No interrupts asserted in Rx path");
865 if (status & XAXIDMA_IRQ_ERROR_MASK) {
866 dev_err(&ndev->dev, "DMA Rx error 0x%x\n", status);
867 dev_err(&ndev->dev, "Current BD is at: 0x%x\n",
868 (lp->rx_bd_v[lp->rx_bd_ci]).phys);
869
870 cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
871 /* Disable coalesce, delay timer and error interrupts */
872 cr &= (~XAXIDMA_IRQ_ALL_MASK);
873 /* Finally write to the Tx channel control register */
874 axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
875
876 cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
877 /* Disable coalesce, delay timer and error interrupts */
878 cr &= (~XAXIDMA_IRQ_ALL_MASK);
879 /* write to the Rx channel control register */
880 axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
881
882 tasklet_schedule(&lp->dma_err_tasklet);
883 }
884out:
885 axienet_dma_out32(lp, XAXIDMA_RX_SR_OFFSET, status);
886 return IRQ_HANDLED;
887}
888
Jeff Mahoneyaecb55b2012-11-20 10:23:13 +0000889static void axienet_dma_err_handler(unsigned long data);
890
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000891/**
892 * axienet_open - Driver open routine.
893 * @ndev: Pointer to net_device structure
894 *
895 * returns: 0, on success.
896 * -ENODEV, if PHY cannot be connected to
897 * non-zero error value on failure
898 *
899 * This is the driver open routine. It calls phy_start to start the PHY device.
900 * It also allocates interrupt service routines, enables the interrupt lines
901 * and ISR handling. Axi Ethernet core is reset through Axi DMA core. Buffer
902 * descriptors are initialized.
903 */
904static int axienet_open(struct net_device *ndev)
905{
906 int ret, mdio_mcreg;
907 struct axienet_local *lp = netdev_priv(ndev);
908
909 dev_dbg(&ndev->dev, "axienet_open()\n");
910
911 mdio_mcreg = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
912 ret = axienet_mdio_wait_until_ready(lp);
913 if (ret < 0)
914 return ret;
915 /* Disable the MDIO interface till Axi Ethernet Reset is completed.
916 * When we do an Axi Ethernet reset, it resets the complete core
917 * including the MDIO. If MDIO is not disabled when the reset
918 * process is started, MDIO will be broken afterwards. */
919 axienet_iow(lp, XAE_MDIO_MC_OFFSET,
920 (mdio_mcreg & (~XAE_MDIO_MC_MDIOEN_MASK)));
921 axienet_device_reset(ndev);
922 /* Enable the MDIO */
923 axienet_iow(lp, XAE_MDIO_MC_OFFSET, mdio_mcreg);
924 ret = axienet_mdio_wait_until_ready(lp);
925 if (ret < 0)
926 return ret;
927
928 if (lp->phy_node) {
Srikanth Thokalad1d372e2015-05-05 11:25:54 +0200929 if (lp->phy_type == XAE_PHY_TYPE_GMII) {
930 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000931 axienet_adjust_link, 0,
932 PHY_INTERFACE_MODE_GMII);
Srikanth Thokalad1d372e2015-05-05 11:25:54 +0200933 } else if (lp->phy_type == XAE_PHY_TYPE_RGMII_2_0) {
934 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
935 axienet_adjust_link, 0,
936 PHY_INTERFACE_MODE_RGMII_ID);
937 }
938
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000939 if (!lp->phy_dev) {
940 dev_err(lp->dev, "of_phy_connect() failed\n");
941 return -ENODEV;
942 }
943 phy_start(lp->phy_dev);
944 }
945
Xiaotian Feng71c6c832012-11-13 19:47:36 +0000946 /* Enable tasklets for Axi DMA error handling */
947 tasklet_init(&lp->dma_err_tasklet, axienet_dma_err_handler,
948 (unsigned long) lp);
949
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000950 /* Enable interrupts for Axi DMA Tx */
951 ret = request_irq(lp->tx_irq, axienet_tx_irq, 0, ndev->name, ndev);
952 if (ret)
953 goto err_tx_irq;
954 /* Enable interrupts for Axi DMA Rx */
955 ret = request_irq(lp->rx_irq, axienet_rx_irq, 0, ndev->name, ndev);
956 if (ret)
957 goto err_rx_irq;
Xiaotian Feng71c6c832012-11-13 19:47:36 +0000958
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000959 return 0;
960
961err_rx_irq:
962 free_irq(lp->tx_irq, ndev);
963err_tx_irq:
964 if (lp->phy_dev)
965 phy_disconnect(lp->phy_dev);
966 lp->phy_dev = NULL;
Xiaotian Feng71c6c832012-11-13 19:47:36 +0000967 tasklet_kill(&lp->dma_err_tasklet);
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000968 dev_err(lp->dev, "request_irq() failed\n");
969 return ret;
970}
971
972/**
973 * axienet_stop - Driver stop routine.
974 * @ndev: Pointer to net_device structure
975 *
976 * returns: 0, on success.
977 *
978 * This is the driver stop routine. It calls phy_disconnect to stop the PHY
979 * device. It also removes the interrupt handlers and disables the interrupts.
980 * The Axi DMA Tx/Rx BDs are released.
981 */
982static int axienet_stop(struct net_device *ndev)
983{
984 u32 cr;
985 struct axienet_local *lp = netdev_priv(ndev);
986
987 dev_dbg(&ndev->dev, "axienet_close()\n");
988
989 cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
990 axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET,
991 cr & (~XAXIDMA_CR_RUNSTOP_MASK));
992 cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
993 axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET,
994 cr & (~XAXIDMA_CR_RUNSTOP_MASK));
995 axienet_setoptions(ndev, lp->options &
996 ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
997
Xiaotian Feng175c0df2012-10-31 00:29:57 +0000998 tasklet_kill(&lp->dma_err_tasklet);
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +0000999
1000 free_irq(lp->tx_irq, ndev);
1001 free_irq(lp->rx_irq, ndev);
1002
1003 if (lp->phy_dev)
1004 phy_disconnect(lp->phy_dev);
1005 lp->phy_dev = NULL;
1006
1007 axienet_dma_bd_release(ndev);
1008 return 0;
1009}
1010
1011/**
1012 * axienet_change_mtu - Driver change mtu routine.
1013 * @ndev: Pointer to net_device structure
1014 * @new_mtu: New mtu value to be applied
1015 *
1016 * returns: Always returns 0 (success).
1017 *
1018 * This is the change mtu driver routine. It checks if the Axi Ethernet
1019 * hardware supports jumbo frames before changing the mtu. This can be
1020 * called only when the device is not up.
1021 */
1022static int axienet_change_mtu(struct net_device *ndev, int new_mtu)
1023{
1024 struct axienet_local *lp = netdev_priv(ndev);
1025
1026 if (netif_running(ndev))
1027 return -EBUSY;
1028 if (lp->jumbo_support) {
1029 if ((new_mtu > XAE_JUMBO_MTU) || (new_mtu < 64))
1030 return -EINVAL;
1031 ndev->mtu = new_mtu;
1032 } else {
1033 if ((new_mtu > XAE_MTU) || (new_mtu < 64))
1034 return -EINVAL;
1035 ndev->mtu = new_mtu;
1036 }
1037
1038 return 0;
1039}
1040
1041#ifdef CONFIG_NET_POLL_CONTROLLER
1042/**
1043 * axienet_poll_controller - Axi Ethernet poll mechanism.
1044 * @ndev: Pointer to net_device structure
1045 *
1046 * This implements Rx/Tx ISR poll mechanisms. The interrupts are disabled prior
1047 * to polling the ISRs and are enabled back after the polling is done.
1048 */
1049static void axienet_poll_controller(struct net_device *ndev)
1050{
1051 struct axienet_local *lp = netdev_priv(ndev);
1052 disable_irq(lp->tx_irq);
1053 disable_irq(lp->rx_irq);
1054 axienet_rx_irq(lp->tx_irq, ndev);
1055 axienet_tx_irq(lp->rx_irq, ndev);
1056 enable_irq(lp->tx_irq);
1057 enable_irq(lp->rx_irq);
1058}
1059#endif
1060
1061static const struct net_device_ops axienet_netdev_ops = {
1062 .ndo_open = axienet_open,
1063 .ndo_stop = axienet_stop,
1064 .ndo_start_xmit = axienet_start_xmit,
1065 .ndo_change_mtu = axienet_change_mtu,
1066 .ndo_set_mac_address = netdev_set_mac_address,
1067 .ndo_validate_addr = eth_validate_addr,
1068 .ndo_set_rx_mode = axienet_set_multicast_list,
1069#ifdef CONFIG_NET_POLL_CONTROLLER
1070 .ndo_poll_controller = axienet_poll_controller,
1071#endif
1072};
1073
1074/**
1075 * axienet_ethtools_get_settings - Get Axi Ethernet settings related to PHY.
1076 * @ndev: Pointer to net_device structure
1077 * @ecmd: Pointer to ethtool_cmd structure
1078 *
1079 * This implements ethtool command for getting PHY settings. If PHY could
1080 * not be found, the function returns -ENODEV. This function calls the
1081 * relevant PHY ethtool API to get the PHY settings.
1082 * Issue "ethtool ethX" under linux prompt to execute this function.
1083 */
1084static int axienet_ethtools_get_settings(struct net_device *ndev,
1085 struct ethtool_cmd *ecmd)
1086{
1087 struct axienet_local *lp = netdev_priv(ndev);
1088 struct phy_device *phydev = lp->phy_dev;
1089 if (!phydev)
1090 return -ENODEV;
1091 return phy_ethtool_gset(phydev, ecmd);
1092}
1093
1094/**
1095 * axienet_ethtools_set_settings - Set PHY settings as passed in the argument.
1096 * @ndev: Pointer to net_device structure
1097 * @ecmd: Pointer to ethtool_cmd structure
1098 *
1099 * This implements ethtool command for setting various PHY settings. If PHY
1100 * could not be found, the function returns -ENODEV. This function calls the
1101 * relevant PHY ethtool API to set the PHY.
1102 * Issue e.g. "ethtool -s ethX speed 1000" under linux prompt to execute this
1103 * function.
1104 */
1105static int axienet_ethtools_set_settings(struct net_device *ndev,
1106 struct ethtool_cmd *ecmd)
1107{
1108 struct axienet_local *lp = netdev_priv(ndev);
1109 struct phy_device *phydev = lp->phy_dev;
1110 if (!phydev)
1111 return -ENODEV;
1112 return phy_ethtool_sset(phydev, ecmd);
1113}
1114
1115/**
1116 * axienet_ethtools_get_drvinfo - Get various Axi Ethernet driver information.
1117 * @ndev: Pointer to net_device structure
1118 * @ed: Pointer to ethtool_drvinfo structure
1119 *
1120 * This implements ethtool command for getting the driver information.
1121 * Issue "ethtool -i ethX" under linux prompt to execute this function.
1122 */
1123static void axienet_ethtools_get_drvinfo(struct net_device *ndev,
1124 struct ethtool_drvinfo *ed)
1125{
Jiri Pirko7826d432013-01-06 00:44:26 +00001126 strlcpy(ed->driver, DRIVER_NAME, sizeof(ed->driver));
1127 strlcpy(ed->version, DRIVER_VERSION, sizeof(ed->version));
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001128 ed->regdump_len = sizeof(u32) * AXIENET_REGS_N;
1129}
1130
1131/**
1132 * axienet_ethtools_get_regs_len - Get the total regs length present in the
1133 * AxiEthernet core.
1134 * @ndev: Pointer to net_device structure
1135 *
1136 * This implements ethtool command for getting the total register length
1137 * information.
1138 */
1139static int axienet_ethtools_get_regs_len(struct net_device *ndev)
1140{
1141 return sizeof(u32) * AXIENET_REGS_N;
1142}
1143
1144/**
1145 * axienet_ethtools_get_regs - Dump the contents of all registers present
1146 * in AxiEthernet core.
1147 * @ndev: Pointer to net_device structure
1148 * @regs: Pointer to ethtool_regs structure
1149 * @ret: Void pointer used to return the contents of the registers.
1150 *
1151 * This implements ethtool command for getting the Axi Ethernet register dump.
1152 * Issue "ethtool -d ethX" to execute this function.
1153 */
1154static void axienet_ethtools_get_regs(struct net_device *ndev,
1155 struct ethtool_regs *regs, void *ret)
1156{
1157 u32 *data = (u32 *) ret;
1158 size_t len = sizeof(u32) * AXIENET_REGS_N;
1159 struct axienet_local *lp = netdev_priv(ndev);
1160
1161 regs->version = 0;
1162 regs->len = len;
1163
1164 memset(data, 0, len);
1165 data[0] = axienet_ior(lp, XAE_RAF_OFFSET);
1166 data[1] = axienet_ior(lp, XAE_TPF_OFFSET);
1167 data[2] = axienet_ior(lp, XAE_IFGP_OFFSET);
1168 data[3] = axienet_ior(lp, XAE_IS_OFFSET);
1169 data[4] = axienet_ior(lp, XAE_IP_OFFSET);
1170 data[5] = axienet_ior(lp, XAE_IE_OFFSET);
1171 data[6] = axienet_ior(lp, XAE_TTAG_OFFSET);
1172 data[7] = axienet_ior(lp, XAE_RTAG_OFFSET);
1173 data[8] = axienet_ior(lp, XAE_UAWL_OFFSET);
1174 data[9] = axienet_ior(lp, XAE_UAWU_OFFSET);
1175 data[10] = axienet_ior(lp, XAE_TPID0_OFFSET);
1176 data[11] = axienet_ior(lp, XAE_TPID1_OFFSET);
1177 data[12] = axienet_ior(lp, XAE_PPST_OFFSET);
1178 data[13] = axienet_ior(lp, XAE_RCW0_OFFSET);
1179 data[14] = axienet_ior(lp, XAE_RCW1_OFFSET);
1180 data[15] = axienet_ior(lp, XAE_TC_OFFSET);
1181 data[16] = axienet_ior(lp, XAE_FCC_OFFSET);
1182 data[17] = axienet_ior(lp, XAE_EMMC_OFFSET);
1183 data[18] = axienet_ior(lp, XAE_PHYC_OFFSET);
1184 data[19] = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
1185 data[20] = axienet_ior(lp, XAE_MDIO_MCR_OFFSET);
1186 data[21] = axienet_ior(lp, XAE_MDIO_MWD_OFFSET);
1187 data[22] = axienet_ior(lp, XAE_MDIO_MRD_OFFSET);
1188 data[23] = axienet_ior(lp, XAE_MDIO_MIS_OFFSET);
1189 data[24] = axienet_ior(lp, XAE_MDIO_MIP_OFFSET);
1190 data[25] = axienet_ior(lp, XAE_MDIO_MIE_OFFSET);
1191 data[26] = axienet_ior(lp, XAE_MDIO_MIC_OFFSET);
1192 data[27] = axienet_ior(lp, XAE_UAW0_OFFSET);
1193 data[28] = axienet_ior(lp, XAE_UAW1_OFFSET);
1194 data[29] = axienet_ior(lp, XAE_FMI_OFFSET);
1195 data[30] = axienet_ior(lp, XAE_AF0_OFFSET);
1196 data[31] = axienet_ior(lp, XAE_AF1_OFFSET);
1197}
1198
1199/**
1200 * axienet_ethtools_get_pauseparam - Get the pause parameter setting for
1201 * Tx and Rx paths.
1202 * @ndev: Pointer to net_device structure
1203 * @epauseparm: Pointer to ethtool_pauseparam structure.
1204 *
1205 * This implements ethtool command for getting axi ethernet pause frame
1206 * setting. Issue "ethtool -a ethX" to execute this function.
1207 */
1208static void
1209axienet_ethtools_get_pauseparam(struct net_device *ndev,
1210 struct ethtool_pauseparam *epauseparm)
1211{
1212 u32 regval;
1213 struct axienet_local *lp = netdev_priv(ndev);
1214 epauseparm->autoneg = 0;
1215 regval = axienet_ior(lp, XAE_FCC_OFFSET);
1216 epauseparm->tx_pause = regval & XAE_FCC_FCTX_MASK;
1217 epauseparm->rx_pause = regval & XAE_FCC_FCRX_MASK;
1218}
1219
1220/**
1221 * axienet_ethtools_set_pauseparam - Set device pause parameter(flow control)
1222 * settings.
1223 * @ndev: Pointer to net_device structure
1224 * @epauseparam:Pointer to ethtool_pauseparam structure
1225 *
1226 * This implements ethtool command for enabling flow control on Rx and Tx
1227 * paths. Issue "ethtool -A ethX tx on|off" under linux prompt to execute this
1228 * function.
1229 */
1230static int
1231axienet_ethtools_set_pauseparam(struct net_device *ndev,
1232 struct ethtool_pauseparam *epauseparm)
1233{
1234 u32 regval = 0;
1235 struct axienet_local *lp = netdev_priv(ndev);
1236
1237 if (netif_running(ndev)) {
1238 printk(KERN_ERR "%s: Please stop netif before applying "
1239 "configruation\n", ndev->name);
1240 return -EFAULT;
1241 }
1242
1243 regval = axienet_ior(lp, XAE_FCC_OFFSET);
1244 if (epauseparm->tx_pause)
1245 regval |= XAE_FCC_FCTX_MASK;
1246 else
1247 regval &= ~XAE_FCC_FCTX_MASK;
1248 if (epauseparm->rx_pause)
1249 regval |= XAE_FCC_FCRX_MASK;
1250 else
1251 regval &= ~XAE_FCC_FCRX_MASK;
1252 axienet_iow(lp, XAE_FCC_OFFSET, regval);
1253
1254 return 0;
1255}
1256
1257/**
1258 * axienet_ethtools_get_coalesce - Get DMA interrupt coalescing count.
1259 * @ndev: Pointer to net_device structure
1260 * @ecoalesce: Pointer to ethtool_coalesce structure
1261 *
1262 * This implements ethtool command for getting the DMA interrupt coalescing
1263 * count on Tx and Rx paths. Issue "ethtool -c ethX" under linux prompt to
1264 * execute this function.
1265 */
1266static int axienet_ethtools_get_coalesce(struct net_device *ndev,
1267 struct ethtool_coalesce *ecoalesce)
1268{
1269 u32 regval = 0;
1270 struct axienet_local *lp = netdev_priv(ndev);
1271 regval = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
1272 ecoalesce->rx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
1273 >> XAXIDMA_COALESCE_SHIFT;
1274 regval = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
1275 ecoalesce->tx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
1276 >> XAXIDMA_COALESCE_SHIFT;
1277 return 0;
1278}
1279
1280/**
1281 * axienet_ethtools_set_coalesce - Set DMA interrupt coalescing count.
1282 * @ndev: Pointer to net_device structure
1283 * @ecoalesce: Pointer to ethtool_coalesce structure
1284 *
1285 * This implements ethtool command for setting the DMA interrupt coalescing
1286 * count on Tx and Rx paths. Issue "ethtool -C ethX rx-frames 5" under linux
1287 * prompt to execute this function.
1288 */
1289static int axienet_ethtools_set_coalesce(struct net_device *ndev,
1290 struct ethtool_coalesce *ecoalesce)
1291{
1292 struct axienet_local *lp = netdev_priv(ndev);
1293
1294 if (netif_running(ndev)) {
1295 printk(KERN_ERR "%s: Please stop netif before applying "
1296 "configruation\n", ndev->name);
1297 return -EFAULT;
1298 }
1299
1300 if ((ecoalesce->rx_coalesce_usecs) ||
1301 (ecoalesce->rx_coalesce_usecs_irq) ||
1302 (ecoalesce->rx_max_coalesced_frames_irq) ||
1303 (ecoalesce->tx_coalesce_usecs) ||
1304 (ecoalesce->tx_coalesce_usecs_irq) ||
1305 (ecoalesce->tx_max_coalesced_frames_irq) ||
1306 (ecoalesce->stats_block_coalesce_usecs) ||
1307 (ecoalesce->use_adaptive_rx_coalesce) ||
1308 (ecoalesce->use_adaptive_tx_coalesce) ||
1309 (ecoalesce->pkt_rate_low) ||
1310 (ecoalesce->rx_coalesce_usecs_low) ||
1311 (ecoalesce->rx_max_coalesced_frames_low) ||
1312 (ecoalesce->tx_coalesce_usecs_low) ||
1313 (ecoalesce->tx_max_coalesced_frames_low) ||
1314 (ecoalesce->pkt_rate_high) ||
1315 (ecoalesce->rx_coalesce_usecs_high) ||
1316 (ecoalesce->rx_max_coalesced_frames_high) ||
1317 (ecoalesce->tx_coalesce_usecs_high) ||
1318 (ecoalesce->tx_max_coalesced_frames_high) ||
1319 (ecoalesce->rate_sample_interval))
1320 return -EOPNOTSUPP;
1321 if (ecoalesce->rx_max_coalesced_frames)
1322 lp->coalesce_count_rx = ecoalesce->rx_max_coalesced_frames;
1323 if (ecoalesce->tx_max_coalesced_frames)
1324 lp->coalesce_count_tx = ecoalesce->tx_max_coalesced_frames;
1325
1326 return 0;
1327}
1328
1329static struct ethtool_ops axienet_ethtool_ops = {
1330 .get_settings = axienet_ethtools_get_settings,
1331 .set_settings = axienet_ethtools_set_settings,
1332 .get_drvinfo = axienet_ethtools_get_drvinfo,
1333 .get_regs_len = axienet_ethtools_get_regs_len,
1334 .get_regs = axienet_ethtools_get_regs,
1335 .get_link = ethtool_op_get_link,
1336 .get_pauseparam = axienet_ethtools_get_pauseparam,
1337 .set_pauseparam = axienet_ethtools_set_pauseparam,
1338 .get_coalesce = axienet_ethtools_get_coalesce,
1339 .set_coalesce = axienet_ethtools_set_coalesce,
1340};
1341
1342/**
1343 * axienet_dma_err_handler - Tasklet handler for Axi DMA Error
1344 * @data: Data passed
1345 *
1346 * Resets the Axi DMA and Axi Ethernet devices, and reconfigures the
1347 * Tx/Rx BDs.
1348 */
1349static void axienet_dma_err_handler(unsigned long data)
1350{
1351 u32 axienet_status;
1352 u32 cr, i;
1353 int mdio_mcreg;
1354 struct axienet_local *lp = (struct axienet_local *) data;
1355 struct net_device *ndev = lp->ndev;
1356 struct axidma_bd *cur_p;
1357
1358 axienet_setoptions(ndev, lp->options &
1359 ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
1360 mdio_mcreg = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
1361 axienet_mdio_wait_until_ready(lp);
1362 /* Disable the MDIO interface till Axi Ethernet Reset is completed.
1363 * When we do an Axi Ethernet reset, it resets the complete core
1364 * including the MDIO. So if MDIO is not disabled when the reset
1365 * process is started, MDIO will be broken afterwards. */
1366 axienet_iow(lp, XAE_MDIO_MC_OFFSET, (mdio_mcreg &
1367 ~XAE_MDIO_MC_MDIOEN_MASK));
1368
1369 __axienet_device_reset(lp, &ndev->dev, XAXIDMA_TX_CR_OFFSET);
1370 __axienet_device_reset(lp, &ndev->dev, XAXIDMA_RX_CR_OFFSET);
1371
1372 axienet_iow(lp, XAE_MDIO_MC_OFFSET, mdio_mcreg);
1373 axienet_mdio_wait_until_ready(lp);
1374
1375 for (i = 0; i < TX_BD_NUM; i++) {
1376 cur_p = &lp->tx_bd_v[i];
1377 if (cur_p->phys)
1378 dma_unmap_single(ndev->dev.parent, cur_p->phys,
1379 (cur_p->cntrl &
1380 XAXIDMA_BD_CTRL_LENGTH_MASK),
1381 DMA_TO_DEVICE);
1382 if (cur_p->app4)
1383 dev_kfree_skb_irq((struct sk_buff *) cur_p->app4);
1384 cur_p->phys = 0;
1385 cur_p->cntrl = 0;
1386 cur_p->status = 0;
1387 cur_p->app0 = 0;
1388 cur_p->app1 = 0;
1389 cur_p->app2 = 0;
1390 cur_p->app3 = 0;
1391 cur_p->app4 = 0;
1392 cur_p->sw_id_offset = 0;
1393 }
1394
1395 for (i = 0; i < RX_BD_NUM; i++) {
1396 cur_p = &lp->rx_bd_v[i];
1397 cur_p->status = 0;
1398 cur_p->app0 = 0;
1399 cur_p->app1 = 0;
1400 cur_p->app2 = 0;
1401 cur_p->app3 = 0;
1402 cur_p->app4 = 0;
1403 }
1404
1405 lp->tx_bd_ci = 0;
1406 lp->tx_bd_tail = 0;
1407 lp->rx_bd_ci = 0;
1408
1409 /* Start updating the Rx channel control register */
1410 cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
1411 /* Update the interrupt coalesce count */
1412 cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
1413 (XAXIDMA_DFT_RX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
1414 /* Update the delay timer count */
1415 cr = ((cr & ~XAXIDMA_DELAY_MASK) |
1416 (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
1417 /* Enable coalesce, delay timer and error interrupts */
1418 cr |= XAXIDMA_IRQ_ALL_MASK;
1419 /* Finally write to the Rx channel control register */
1420 axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
1421
1422 /* Start updating the Tx channel control register */
1423 cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
1424 /* Update the interrupt coalesce count */
1425 cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
1426 (XAXIDMA_DFT_TX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
1427 /* Update the delay timer count */
1428 cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
1429 (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
1430 /* Enable coalesce, delay timer and error interrupts */
1431 cr |= XAXIDMA_IRQ_ALL_MASK;
1432 /* Finally write to the Tx channel control register */
1433 axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
1434
1435 /* Populate the tail pointer and bring the Rx Axi DMA engine out of
1436 * halted state. This will make the Rx side ready for reception.*/
1437 axienet_dma_out32(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
1438 cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
1439 axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET,
1440 cr | XAXIDMA_CR_RUNSTOP_MASK);
1441 axienet_dma_out32(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
1442 (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
1443
1444 /* Write to the RS (Run-stop) bit in the Tx channel control register.
1445 * Tx channel is now ready to run. But only after we write to the
1446 * tail pointer register that the Tx channel will start transmitting */
1447 axienet_dma_out32(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
1448 cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
1449 axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET,
1450 cr | XAXIDMA_CR_RUNSTOP_MASK);
1451
1452 axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
1453 axienet_status &= ~XAE_RCW1_RX_MASK;
1454 axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
1455
1456 axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
1457 if (axienet_status & XAE_INT_RXRJECT_MASK)
1458 axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
1459 axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
1460
1461 /* Sync default options with HW but leave receiver and
1462 * transmitter disabled.*/
1463 axienet_setoptions(ndev, lp->options &
1464 ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
1465 axienet_set_mac_address(ndev, NULL);
1466 axienet_set_multicast_list(ndev);
1467 axienet_setoptions(ndev, lp->options);
1468}
1469
1470/**
1471 * axienet_of_probe - Axi Ethernet probe function.
1472 * @op: Pointer to platform device structure.
1473 * @match: Pointer to device id structure
1474 *
1475 * returns: 0, on success
1476 * Non-zero error value on failure.
1477 *
1478 * This is the probe routine for Axi Ethernet driver. This is called before
1479 * any other driver routines are invoked. It allocates and sets up the Ethernet
1480 * device. Parses through device tree and populates fields of
1481 * axienet_local. It registers the Ethernet device.
1482 */
Bill Pemberton48d16cb2012-12-03 09:23:50 -05001483static int axienet_of_probe(struct platform_device *op)
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001484{
1485 __be32 *p;
1486 int size, ret = 0;
1487 struct device_node *np;
1488 struct axienet_local *lp;
1489 struct net_device *ndev;
1490 const void *addr;
1491
1492 ndev = alloc_etherdev(sizeof(*lp));
Joe Perches41de8d42012-01-29 13:47:52 +00001493 if (!ndev)
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001494 return -ENOMEM;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001495
Jingoo Han8513fbd2013-05-23 00:52:31 +00001496 platform_set_drvdata(op, ndev);
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001497
1498 SET_NETDEV_DEV(ndev, &op->dev);
1499 ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
Eric Dumazet28e24c62013-12-02 08:51:13 -08001500 ndev->features = NETIF_F_SG;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001501 ndev->netdev_ops = &axienet_netdev_ops;
1502 ndev->ethtool_ops = &axienet_ethtool_ops;
1503
1504 lp = netdev_priv(ndev);
1505 lp->ndev = ndev;
1506 lp->dev = &op->dev;
1507 lp->options = XAE_OPTION_DEFAULTS;
1508 /* Map device registers */
1509 lp->regs = of_iomap(op->dev.of_node, 0);
1510 if (!lp->regs) {
1511 dev_err(&op->dev, "could not map Axi Ethernet regs.\n");
Julia Lawall0f113b82014-12-29 18:04:42 +01001512 ret = -ENOMEM;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001513 goto nodev;
1514 }
1515 /* Setup checksum offload, but default to off if not specified */
1516 lp->features = 0;
1517
1518 p = (__be32 *) of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
1519 if (p) {
1520 switch (be32_to_cpup(p)) {
1521 case 1:
1522 lp->csum_offload_on_tx_path =
1523 XAE_FEATURE_PARTIAL_TX_CSUM;
1524 lp->features |= XAE_FEATURE_PARTIAL_TX_CSUM;
1525 /* Can checksum TCP/UDP over IPv4. */
1526 ndev->features |= NETIF_F_IP_CSUM;
1527 break;
1528 case 2:
1529 lp->csum_offload_on_tx_path =
1530 XAE_FEATURE_FULL_TX_CSUM;
1531 lp->features |= XAE_FEATURE_FULL_TX_CSUM;
1532 /* Can checksum TCP/UDP over IPv4. */
1533 ndev->features |= NETIF_F_IP_CSUM;
1534 break;
1535 default:
1536 lp->csum_offload_on_tx_path = XAE_NO_CSUM_OFFLOAD;
1537 }
1538 }
1539 p = (__be32 *) of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
1540 if (p) {
1541 switch (be32_to_cpup(p)) {
1542 case 1:
1543 lp->csum_offload_on_rx_path =
1544 XAE_FEATURE_PARTIAL_RX_CSUM;
1545 lp->features |= XAE_FEATURE_PARTIAL_RX_CSUM;
1546 break;
1547 case 2:
1548 lp->csum_offload_on_rx_path =
1549 XAE_FEATURE_FULL_RX_CSUM;
1550 lp->features |= XAE_FEATURE_FULL_RX_CSUM;
1551 break;
1552 default:
1553 lp->csum_offload_on_rx_path = XAE_NO_CSUM_OFFLOAD;
1554 }
1555 }
1556 /* For supporting jumbo frames, the Axi Ethernet hardware must have
1557 * a larger Rx/Tx Memory. Typically, the size must be more than or
1558 * equal to 16384 bytes, so that we can enable jumbo option and start
1559 * supporting jumbo frames. Here we check for memory allocated for
1560 * Rx/Tx in the hardware from the device-tree and accordingly set
1561 * flags. */
1562 p = (__be32 *) of_get_property(op->dev.of_node, "xlnx,rxmem", NULL);
1563 if (p) {
1564 if ((be32_to_cpup(p)) >= 0x4000)
1565 lp->jumbo_support = 1;
1566 }
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001567 p = (__be32 *) of_get_property(op->dev.of_node, "xlnx,phy-type", NULL);
1568 if (p)
1569 lp->phy_type = be32_to_cpup(p);
1570
1571 /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
1572 np = of_parse_phandle(op->dev.of_node, "axistream-connected", 0);
1573 if (!np) {
1574 dev_err(&op->dev, "could not find DMA node\n");
Julia Lawall0f113b82014-12-29 18:04:42 +01001575 ret = -ENODEV;
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001576 goto err_iounmap;
1577 }
1578 lp->dma_regs = of_iomap(np, 0);
1579 if (lp->dma_regs) {
1580 dev_dbg(&op->dev, "MEM base: %p\n", lp->dma_regs);
1581 } else {
1582 dev_err(&op->dev, "unable to map DMA registers\n");
1583 of_node_put(np);
1584 }
1585 lp->rx_irq = irq_of_parse_and_map(np, 1);
1586 lp->tx_irq = irq_of_parse_and_map(np, 0);
1587 of_node_put(np);
Michal Simekcb59c872013-01-10 06:58:43 +00001588 if ((lp->rx_irq <= 0) || (lp->tx_irq <= 0)) {
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001589 dev_err(&op->dev, "could not determine irqs\n");
1590 ret = -ENOMEM;
1591 goto err_iounmap_2;
1592 }
1593
1594 /* Retrieve the MAC address */
1595 addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
1596 if ((!addr) || (size != 6)) {
1597 dev_err(&op->dev, "could not find MAC address\n");
1598 ret = -ENODEV;
1599 goto err_iounmap_2;
1600 }
1601 axienet_set_mac_address(ndev, (void *) addr);
1602
1603 lp->coalesce_count_rx = XAXIDMA_DFT_RX_THRESHOLD;
1604 lp->coalesce_count_tx = XAXIDMA_DFT_TX_THRESHOLD;
1605
1606 lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
1607 ret = axienet_mdio_setup(lp, op->dev.of_node);
1608 if (ret)
1609 dev_warn(&op->dev, "error registering MDIO bus\n");
1610
1611 ret = register_netdev(lp->ndev);
1612 if (ret) {
1613 dev_err(lp->dev, "register_netdev() error (%i)\n", ret);
1614 goto err_iounmap_2;
1615 }
1616
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001617 return 0;
1618
1619err_iounmap_2:
1620 if (lp->dma_regs)
1621 iounmap(lp->dma_regs);
1622err_iounmap:
1623 iounmap(lp->regs);
1624nodev:
1625 free_netdev(ndev);
1626 ndev = NULL;
1627 return ret;
1628}
1629
Bill Pemberton48d16cb2012-12-03 09:23:50 -05001630static int axienet_of_remove(struct platform_device *op)
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001631{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001632 struct net_device *ndev = platform_get_drvdata(op);
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001633 struct axienet_local *lp = netdev_priv(ndev);
1634
1635 axienet_mdio_teardown(lp);
1636 unregister_netdev(ndev);
1637
Julia Lawall6f3a59a2014-08-08 12:07:43 +02001638 of_node_put(lp->phy_node);
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001639 lp->phy_node = NULL;
1640
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001641 iounmap(lp->regs);
1642 if (lp->dma_regs)
1643 iounmap(lp->dma_regs);
1644 free_netdev(ndev);
1645
1646 return 0;
1647}
1648
1649static struct platform_driver axienet_of_driver = {
1650 .probe = axienet_of_probe,
Bill Pemberton48d16cb2012-12-03 09:23:50 -05001651 .remove = axienet_of_remove,
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001652 .driver = {
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001653 .name = "xilinx_axienet",
1654 .of_match_table = axienet_of_match,
1655 },
1656};
1657
Tobias Klauserb4a46672012-02-17 05:35:37 +00001658module_platform_driver(axienet_of_driver);
danborkmann@iogearbox.net8a3b7a22012-01-19 00:39:31 +00001659
1660MODULE_DESCRIPTION("Xilinx Axi Ethernet driver");
1661MODULE_AUTHOR("Xilinx");
1662MODULE_LICENSE("GPL");