blob: 5022093286f043a3e918e6dac105c9bb36d8e115 [file] [log] [blame]
Grant Likely92744982009-04-25 12:53:39 +00001/*
2 * Driver for Xilinx TEMAC Ethernet device
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.
7 *
8 * This is a driver for the Xilinx ll_temac ipcore which is often used
9 * in the Virtex and Spartan series of chips.
10 *
11 * Notes:
12 * - The ll_temac hardware uses indirect access for many of the TEMAC
13 * registers, include the MDIO bus. However, indirect access to MDIO
14 * registers take considerably more clock cycles than to TEMAC registers.
15 * MDIO accesses are long, so threads doing them should probably sleep
16 * rather than busywait. However, since only one indirect access can be
17 * in progress at any given time, that means that *all* indirect accesses
18 * could end up sleeping (to wait for an MDIO access to complete).
19 * Fortunately none of the indirect accesses are on the 'hot' path for tx
20 * or rx, so this should be okay.
21 *
22 * TODO:
Grant Likely92744982009-04-25 12:53:39 +000023 * - Factor out locallink DMA code into separate driver
24 * - Fix multicast assignment.
25 * - Fix support for hardware checksumming.
26 * - Testing. Lots and lots of testing.
27 *
28 */
29
30#include <linux/delay.h>
31#include <linux/etherdevice.h>
32#include <linux/init.h>
33#include <linux/mii.h>
34#include <linux/module.h>
35#include <linux/mutex.h>
36#include <linux/netdevice.h>
37#include <linux/of.h>
38#include <linux/of_device.h>
39#include <linux/of_mdio.h>
40#include <linux/of_platform.h>
Michal Simek9f1a1fc2010-09-01 08:55:23 -060041#include <linux/of_address.h>
Grant Likely92744982009-04-25 12:53:39 +000042#include <linux/skbuff.h>
43#include <linux/spinlock.h>
44#include <linux/tcp.h> /* needed for sizeof(tcphdr) */
45#include <linux/udp.h> /* needed for sizeof(udphdr) */
46#include <linux/phy.h>
47#include <linux/in.h>
48#include <linux/io.h>
49#include <linux/ip.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Stephen Rothwellffbc03b2011-06-08 15:49:33 +100051#include <linux/interrupt.h>
Stephen Rothwell84cac392011-06-29 02:55:59 -070052#include <linux/dma-mapping.h>
Grant Likely92744982009-04-25 12:53:39 +000053
54#include "ll_temac.h"
55
56#define TX_BD_NUM 64
57#define RX_BD_NUM 128
58
59/* ---------------------------------------------------------------------
60 * Low level register access functions
61 */
62
63u32 temac_ior(struct temac_local *lp, int offset)
64{
65 return in_be32((u32 *)(lp->regs + offset));
66}
67
68void temac_iow(struct temac_local *lp, int offset, u32 value)
69{
70 out_be32((u32 *) (lp->regs + offset), value);
71}
72
73int temac_indirect_busywait(struct temac_local *lp)
74{
75 long end = jiffies + 2;
76
77 while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) {
78 if (end - jiffies <= 0) {
79 WARN_ON(1);
80 return -ETIMEDOUT;
81 }
82 msleep(1);
83 }
84 return 0;
85}
86
87/**
88 * temac_indirect_in32
89 *
90 * lp->indirect_mutex must be held when calling this function
91 */
92u32 temac_indirect_in32(struct temac_local *lp, int reg)
93{
94 u32 val;
95
96 if (temac_indirect_busywait(lp))
97 return -ETIMEDOUT;
98 temac_iow(lp, XTE_CTL0_OFFSET, reg);
99 if (temac_indirect_busywait(lp))
100 return -ETIMEDOUT;
101 val = temac_ior(lp, XTE_LSW0_OFFSET);
102
103 return val;
104}
105
106/**
107 * temac_indirect_out32
108 *
109 * lp->indirect_mutex must be held when calling this function
110 */
111void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
112{
113 if (temac_indirect_busywait(lp))
114 return;
115 temac_iow(lp, XTE_LSW0_OFFSET, value);
116 temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
Ricardo Ribaldaf79d7e62011-11-07 23:39:57 +0000117 temac_indirect_busywait(lp);
Grant Likely92744982009-04-25 12:53:39 +0000118}
119
John Linne44171f2010-04-08 07:08:02 +0000120/**
121 * temac_dma_in32 - Memory mapped DMA read, this function expects a
122 * register input that is based on DCR word addresses which
123 * are then converted to memory mapped byte addresses
124 */
Grant Likely92744982009-04-25 12:53:39 +0000125static u32 temac_dma_in32(struct temac_local *lp, int reg)
126{
John Linne44171f2010-04-08 07:08:02 +0000127 return in_be32((u32 *)(lp->sdma_regs + (reg << 2)));
128}
129
130/**
131 * temac_dma_out32 - Memory mapped DMA read, this function expects a
132 * register input that is based on DCR word addresses which
133 * are then converted to memory mapped byte addresses
134 */
135static void temac_dma_out32(struct temac_local *lp, int reg, u32 value)
136{
137 out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value);
138}
139
140/* DMA register access functions can be DCR based or memory mapped.
141 * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
142 * memory mapped.
143 */
144#ifdef CONFIG_PPC_DCR
145
146/**
147 * temac_dma_dcr_in32 - DCR based DMA read
148 */
149static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
150{
Grant Likely92744982009-04-25 12:53:39 +0000151 return dcr_read(lp->sdma_dcrs, reg);
152}
153
John Linne44171f2010-04-08 07:08:02 +0000154/**
155 * temac_dma_dcr_out32 - DCR based DMA write
156 */
157static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
Grant Likely92744982009-04-25 12:53:39 +0000158{
159 dcr_write(lp->sdma_dcrs, reg, value);
160}
161
162/**
John Linne44171f2010-04-08 07:08:02 +0000163 * temac_dcr_setup - If the DMA is DCR based, then setup the address and
164 * I/O functions
165 */
Grant Likely2dc11582010-08-06 09:25:50 -0600166static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
John Linne44171f2010-04-08 07:08:02 +0000167 struct device_node *np)
168{
169 unsigned int dcrs;
170
171 /* setup the dcr address mapping if it's in the device tree */
172
173 dcrs = dcr_resource_start(np, 0);
174 if (dcrs != 0) {
175 lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
176 lp->dma_in = temac_dma_dcr_in;
177 lp->dma_out = temac_dma_dcr_out;
178 dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
179 return 0;
180 }
181 /* no DCR in the device tree, indicate a failure */
182 return -1;
183}
184
185#else
186
187/*
188 * temac_dcr_setup - This is a stub for when DCR is not supported,
189 * such as with MicroBlaze
190 */
Grant Likely2dc11582010-08-06 09:25:50 -0600191static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
John Linne44171f2010-04-08 07:08:02 +0000192 struct device_node *np)
193{
194 return -1;
195}
196
197#endif
198
199/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000200 * temac_dma_bd_release - Release buffer descriptor rings
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000201 */
202static void temac_dma_bd_release(struct net_device *ndev)
203{
204 struct temac_local *lp = netdev_priv(ndev);
205 int i;
206
Ricardo Ribalda50ec1532011-11-07 23:31:58 +0000207 /* Reset Local Link (DMA) */
208 lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
209
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000210 for (i = 0; i < RX_BD_NUM; i++) {
211 if (!lp->rx_skb[i])
212 break;
213 else {
214 dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
215 XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
216 dev_kfree_skb(lp->rx_skb[i]);
217 }
218 }
219 if (lp->rx_bd_v)
220 dma_free_coherent(ndev->dev.parent,
221 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
222 lp->rx_bd_v, lp->rx_bd_p);
223 if (lp->tx_bd_v)
224 dma_free_coherent(ndev->dev.parent,
225 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
226 lp->tx_bd_v, lp->tx_bd_p);
227 if (lp->rx_skb)
228 kfree(lp->rx_skb);
229}
230
231/**
Grant Likely92744982009-04-25 12:53:39 +0000232 * temac_dma_bd_init - Setup buffer descriptor rings
233 */
234static int temac_dma_bd_init(struct net_device *ndev)
235{
236 struct temac_local *lp = netdev_priv(ndev);
237 struct sk_buff *skb;
238 int i;
239
Thomas Meyerddf98e62011-12-02 12:35:43 +0000240 lp->rx_skb = kcalloc(RX_BD_NUM, sizeof(*lp->rx_skb), GFP_KERNEL);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000241 if (!lp->rx_skb) {
242 dev_err(&ndev->dev,
243 "can't allocate memory for DMA RX buffer\n");
244 goto out;
245 }
Grant Likely92744982009-04-25 12:53:39 +0000246 /* allocate the tx and rx ring buffer descriptors. */
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400247 /* returns a virtual address and a physical address. */
Grant Likely92744982009-04-25 12:53:39 +0000248 lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
249 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
250 &lp->tx_bd_p, GFP_KERNEL);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000251 if (!lp->tx_bd_v) {
252 dev_err(&ndev->dev,
253 "unable to allocate DMA TX buffer descriptors");
254 goto out;
255 }
Grant Likely92744982009-04-25 12:53:39 +0000256 lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
257 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
258 &lp->rx_bd_p, GFP_KERNEL);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000259 if (!lp->rx_bd_v) {
260 dev_err(&ndev->dev,
261 "unable to allocate DMA RX buffer descriptors");
262 goto out;
263 }
Grant Likely92744982009-04-25 12:53:39 +0000264
265 memset(lp->tx_bd_v, 0, sizeof(*lp->tx_bd_v) * TX_BD_NUM);
266 for (i = 0; i < TX_BD_NUM; i++) {
267 lp->tx_bd_v[i].next = lp->tx_bd_p +
268 sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
269 }
270
271 memset(lp->rx_bd_v, 0, sizeof(*lp->rx_bd_v) * RX_BD_NUM);
272 for (i = 0; i < RX_BD_NUM; i++) {
273 lp->rx_bd_v[i].next = lp->rx_bd_p +
274 sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
275
John Linne44171f2010-04-08 07:08:02 +0000276 skb = netdev_alloc_skb_ip_align(ndev,
277 XTE_MAX_JUMBO_FRAME_SIZE);
278
Grant Likely92744982009-04-25 12:53:39 +0000279 if (skb == 0) {
280 dev_err(&ndev->dev, "alloc_skb error %d\n", i);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000281 goto out;
Grant Likely92744982009-04-25 12:53:39 +0000282 }
283 lp->rx_skb[i] = skb;
Grant Likely92744982009-04-25 12:53:39 +0000284 /* returns physical address of skb->data */
285 lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
286 skb->data,
287 XTE_MAX_JUMBO_FRAME_SIZE,
288 DMA_FROM_DEVICE);
289 lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
290 lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
291 }
292
John Linne44171f2010-04-08 07:08:02 +0000293 lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
Grant Likely92744982009-04-25 12:53:39 +0000294 CHNL_CTRL_IRQ_EN |
295 CHNL_CTRL_IRQ_DLY_EN |
296 CHNL_CTRL_IRQ_COAL_EN);
297 /* 0x10220483 */
298 /* 0x00100483 */
Brian Hill23ecc4b2010-05-26 20:44:30 -0700299 lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
Grant Likely92744982009-04-25 12:53:39 +0000300 CHNL_CTRL_IRQ_EN |
301 CHNL_CTRL_IRQ_DLY_EN |
302 CHNL_CTRL_IRQ_COAL_EN |
303 CHNL_CTRL_IRQ_IOE);
304 /* 0xff010283 */
305
John Linne44171f2010-04-08 07:08:02 +0000306 lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
307 lp->dma_out(lp, RX_TAILDESC_PTR,
Grant Likely92744982009-04-25 12:53:39 +0000308 lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
John Linne44171f2010-04-08 07:08:02 +0000309 lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
Grant Likely92744982009-04-25 12:53:39 +0000310
311 return 0;
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000312
313out:
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000314 temac_dma_bd_release(ndev);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000315 return -ENOMEM;
Grant Likely92744982009-04-25 12:53:39 +0000316}
317
318/* ---------------------------------------------------------------------
319 * net_device_ops
320 */
321
Jiri Pirko04e406d2013-01-01 03:30:19 +0000322static void temac_do_set_mac_address(struct net_device *ndev)
Grant Likely92744982009-04-25 12:53:39 +0000323{
324 struct temac_local *lp = netdev_priv(ndev);
325
Grant Likely92744982009-04-25 12:53:39 +0000326 /* set up unicast MAC address filter set its mac address */
327 mutex_lock(&lp->indirect_mutex);
328 temac_indirect_out32(lp, XTE_UAW0_OFFSET,
329 (ndev->dev_addr[0]) |
330 (ndev->dev_addr[1] << 8) |
331 (ndev->dev_addr[2] << 16) |
332 (ndev->dev_addr[3] << 24));
333 /* There are reserved bits in EUAW1
334 * so don't affect them Set MAC bits [47:32] in EUAW1 */
335 temac_indirect_out32(lp, XTE_UAW1_OFFSET,
336 (ndev->dev_addr[4] & 0x000000ff) |
337 (ndev->dev_addr[5] << 8));
338 mutex_unlock(&lp->indirect_mutex);
Jiri Pirko04e406d2013-01-01 03:30:19 +0000339}
Grant Likely92744982009-04-25 12:53:39 +0000340
Jiri Pirko04e406d2013-01-01 03:30:19 +0000341static int temac_init_mac_address(struct net_device *ndev, void *address)
342{
343 memcpy(ndev->dev_addr, address, ETH_ALEN);
344 if (!is_valid_ether_addr(ndev->dev_addr))
345 eth_hw_addr_random(ndev);
346 temac_do_set_mac_address(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000347 return 0;
348}
349
Jiri Pirko04e406d2013-01-01 03:30:19 +0000350static int temac_set_mac_address(struct net_device *ndev, void *p)
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000351{
352 struct sockaddr *addr = p;
353
Jiri Pirko04e406d2013-01-01 03:30:19 +0000354 if (!is_valid_ether_addr(addr->sa_data))
355 return -EADDRNOTAVAIL;
356 memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
357 temac_do_set_mac_address(ndev);
358 return 0;
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000359}
360
Grant Likely92744982009-04-25 12:53:39 +0000361static void temac_set_multicast_list(struct net_device *ndev)
362{
363 struct temac_local *lp = netdev_priv(ndev);
364 u32 multi_addr_msw, multi_addr_lsw, val;
365 int i;
366
367 mutex_lock(&lp->indirect_mutex);
Joe Perches8e95a202009-12-03 07:58:21 +0000368 if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000369 netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) {
Grant Likely92744982009-04-25 12:53:39 +0000370 /*
371 * We must make the kernel realise we had to move
372 * into promisc mode or we start all out war on
373 * the cable. If it was a promisc request the
374 * flag is already set. If not we assert it.
375 */
376 ndev->flags |= IFF_PROMISC;
377 temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
378 dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000379 } else if (!netdev_mc_empty(ndev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000380 struct netdev_hw_addr *ha;
Grant Likely92744982009-04-25 12:53:39 +0000381
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000382 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000383 netdev_for_each_mc_addr(ha, ndev) {
Grant Likely92744982009-04-25 12:53:39 +0000384 if (i >= MULTICAST_CAM_TABLE_NUM)
385 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000386 multi_addr_msw = ((ha->addr[3] << 24) |
387 (ha->addr[2] << 16) |
388 (ha->addr[1] << 8) |
389 (ha->addr[0]));
Grant Likely92744982009-04-25 12:53:39 +0000390 temac_indirect_out32(lp, XTE_MAW0_OFFSET,
391 multi_addr_msw);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000392 multi_addr_lsw = ((ha->addr[5] << 8) |
393 (ha->addr[4]) | (i << 16));
Grant Likely92744982009-04-25 12:53:39 +0000394 temac_indirect_out32(lp, XTE_MAW1_OFFSET,
395 multi_addr_lsw);
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000396 i++;
Grant Likely92744982009-04-25 12:53:39 +0000397 }
398 } else {
399 val = temac_indirect_in32(lp, XTE_AFM_OFFSET);
400 temac_indirect_out32(lp, XTE_AFM_OFFSET,
401 val & ~XTE_AFM_EPPRM_MASK);
402 temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0);
403 temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0);
404 dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
405 }
406 mutex_unlock(&lp->indirect_mutex);
407}
408
409struct temac_option {
410 int flg;
411 u32 opt;
412 u32 reg;
413 u32 m_or;
414 u32 m_and;
415} temac_options[] = {
416 /* Turn on jumbo packet support for both Rx and Tx */
417 {
418 .opt = XTE_OPTION_JUMBO,
419 .reg = XTE_TXC_OFFSET,
420 .m_or = XTE_TXC_TXJMBO_MASK,
421 },
422 {
423 .opt = XTE_OPTION_JUMBO,
424 .reg = XTE_RXC1_OFFSET,
425 .m_or =XTE_RXC1_RXJMBO_MASK,
426 },
427 /* Turn on VLAN packet support for both Rx and Tx */
428 {
429 .opt = XTE_OPTION_VLAN,
430 .reg = XTE_TXC_OFFSET,
431 .m_or =XTE_TXC_TXVLAN_MASK,
432 },
433 {
434 .opt = XTE_OPTION_VLAN,
435 .reg = XTE_RXC1_OFFSET,
436 .m_or =XTE_RXC1_RXVLAN_MASK,
437 },
438 /* Turn on FCS stripping on receive packets */
439 {
440 .opt = XTE_OPTION_FCS_STRIP,
441 .reg = XTE_RXC1_OFFSET,
442 .m_or =XTE_RXC1_RXFCS_MASK,
443 },
444 /* Turn on FCS insertion on transmit packets */
445 {
446 .opt = XTE_OPTION_FCS_INSERT,
447 .reg = XTE_TXC_OFFSET,
448 .m_or =XTE_TXC_TXFCS_MASK,
449 },
450 /* Turn on length/type field checking on receive packets */
451 {
452 .opt = XTE_OPTION_LENTYPE_ERR,
453 .reg = XTE_RXC1_OFFSET,
454 .m_or =XTE_RXC1_RXLT_MASK,
455 },
456 /* Turn on flow control */
457 {
458 .opt = XTE_OPTION_FLOW_CONTROL,
459 .reg = XTE_FCC_OFFSET,
460 .m_or =XTE_FCC_RXFLO_MASK,
461 },
462 /* Turn on flow control */
463 {
464 .opt = XTE_OPTION_FLOW_CONTROL,
465 .reg = XTE_FCC_OFFSET,
466 .m_or =XTE_FCC_TXFLO_MASK,
467 },
468 /* Turn on promiscuous frame filtering (all frames are received ) */
469 {
470 .opt = XTE_OPTION_PROMISC,
471 .reg = XTE_AFM_OFFSET,
472 .m_or =XTE_AFM_EPPRM_MASK,
473 },
474 /* Enable transmitter if not already enabled */
475 {
476 .opt = XTE_OPTION_TXEN,
477 .reg = XTE_TXC_OFFSET,
478 .m_or =XTE_TXC_TXEN_MASK,
479 },
480 /* Enable receiver? */
481 {
482 .opt = XTE_OPTION_RXEN,
483 .reg = XTE_RXC1_OFFSET,
484 .m_or =XTE_RXC1_RXEN_MASK,
485 },
486 {}
487};
488
489/**
490 * temac_setoptions
491 */
492static u32 temac_setoptions(struct net_device *ndev, u32 options)
493{
494 struct temac_local *lp = netdev_priv(ndev);
495 struct temac_option *tp = &temac_options[0];
496 int reg;
497
498 mutex_lock(&lp->indirect_mutex);
499 while (tp->opt) {
500 reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or;
501 if (options & tp->opt)
502 reg |= tp->m_or;
503 temac_indirect_out32(lp, tp->reg, reg);
504 tp++;
505 }
506 lp->options |= options;
507 mutex_unlock(&lp->indirect_mutex);
508
Eric Dumazet807540b2010-09-23 05:40:09 +0000509 return 0;
Grant Likely92744982009-04-25 12:53:39 +0000510}
511
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200512/* Initialize temac */
Grant Likely92744982009-04-25 12:53:39 +0000513static void temac_device_reset(struct net_device *ndev)
514{
515 struct temac_local *lp = netdev_priv(ndev);
516 u32 timeout;
517 u32 val;
518
519 /* Perform a software reset */
520
521 /* 0x300 host enable bit ? */
522 /* reset PHY through control register ?:1 */
523
524 dev_dbg(&ndev->dev, "%s()\n", __func__);
525
526 mutex_lock(&lp->indirect_mutex);
527 /* Reset the receiver and wait for it to finish reset */
528 temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
529 timeout = 1000;
530 while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
531 udelay(1);
532 if (--timeout == 0) {
533 dev_err(&ndev->dev,
534 "temac_device_reset RX reset timeout!!\n");
535 break;
536 }
537 }
538
539 /* Reset the transmitter and wait for it to finish reset */
540 temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
541 timeout = 1000;
542 while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
543 udelay(1);
544 if (--timeout == 0) {
545 dev_err(&ndev->dev,
546 "temac_device_reset TX reset timeout!!\n");
547 break;
548 }
549 }
550
551 /* Disable the receiver */
552 val = temac_indirect_in32(lp, XTE_RXC1_OFFSET);
553 temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
554
555 /* Reset Local Link (DMA) */
John Linne44171f2010-04-08 07:08:02 +0000556 lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
Grant Likely92744982009-04-25 12:53:39 +0000557 timeout = 1000;
John Linne44171f2010-04-08 07:08:02 +0000558 while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
Grant Likely92744982009-04-25 12:53:39 +0000559 udelay(1);
560 if (--timeout == 0) {
561 dev_err(&ndev->dev,
562 "temac_device_reset DMA reset timeout!!\n");
563 break;
564 }
565 }
John Linne44171f2010-04-08 07:08:02 +0000566 lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
Grant Likely92744982009-04-25 12:53:39 +0000567
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000568 if (temac_dma_bd_init(ndev)) {
569 dev_err(&ndev->dev,
570 "temac_device_reset descriptor allocation failed\n");
571 }
Grant Likely92744982009-04-25 12:53:39 +0000572
573 temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0);
574 temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0);
575 temac_indirect_out32(lp, XTE_TXC_OFFSET, 0);
576 temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
577
578 mutex_unlock(&lp->indirect_mutex);
579
580 /* Sync default options with HW
581 * but leave receiver and transmitter disabled. */
582 temac_setoptions(ndev,
583 lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
584
Jiri Pirko04e406d2013-01-01 03:30:19 +0000585 temac_do_set_mac_address(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000586
587 /* Set address filter table */
588 temac_set_multicast_list(ndev);
589 if (temac_setoptions(ndev, lp->options))
590 dev_err(&ndev->dev, "Error setting TEMAC options\n");
591
592 /* Init Driver variable */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700593 ndev->trans_start = jiffies; /* prevent tx timeout */
Grant Likely92744982009-04-25 12:53:39 +0000594}
595
596void temac_adjust_link(struct net_device *ndev)
597{
598 struct temac_local *lp = netdev_priv(ndev);
599 struct phy_device *phy = lp->phy_dev;
600 u32 mii_speed;
601 int link_state;
602
603 /* hash together the state values to decide if something has changed */
604 link_state = phy->speed | (phy->duplex << 1) | phy->link;
605
606 mutex_lock(&lp->indirect_mutex);
607 if (lp->last_link != link_state) {
608 mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET);
609 mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
610
611 switch (phy->speed) {
612 case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
613 case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
614 case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
615 }
616
617 /* Write new speed setting out to TEMAC */
618 temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed);
619 lp->last_link = link_state;
620 phy_print_status(phy);
621 }
622 mutex_unlock(&lp->indirect_mutex);
623}
624
625static void temac_start_xmit_done(struct net_device *ndev)
626{
627 struct temac_local *lp = netdev_priv(ndev);
628 struct cdmac_bd *cur_p;
629 unsigned int stat = 0;
630
631 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
632 stat = cur_p->app0;
633
634 while (stat & STS_CTRL_APP0_CMPLT) {
635 dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len,
636 DMA_TO_DEVICE);
637 if (cur_p->app4)
638 dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
639 cur_p->app0 = 0;
Brian Hill23ecc4b2010-05-26 20:44:30 -0700640 cur_p->app1 = 0;
641 cur_p->app2 = 0;
642 cur_p->app3 = 0;
643 cur_p->app4 = 0;
Grant Likely92744982009-04-25 12:53:39 +0000644
645 ndev->stats.tx_packets++;
646 ndev->stats.tx_bytes += cur_p->len;
647
648 lp->tx_bd_ci++;
649 if (lp->tx_bd_ci >= TX_BD_NUM)
650 lp->tx_bd_ci = 0;
651
652 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
653 stat = cur_p->app0;
654 }
655
656 netif_wake_queue(ndev);
657}
658
Brian Hill23ecc4b2010-05-26 20:44:30 -0700659static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
660{
661 struct cdmac_bd *cur_p;
662 int tail;
663
664 tail = lp->tx_bd_tail;
665 cur_p = &lp->tx_bd_v[tail];
666
667 do {
668 if (cur_p->app0)
669 return NETDEV_TX_BUSY;
670
671 tail++;
672 if (tail >= TX_BD_NUM)
673 tail = 0;
674
675 cur_p = &lp->tx_bd_v[tail];
676 num_frag--;
677 } while (num_frag >= 0);
678
679 return 0;
680}
681
Grant Likely92744982009-04-25 12:53:39 +0000682static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
683{
684 struct temac_local *lp = netdev_priv(ndev);
685 struct cdmac_bd *cur_p;
686 dma_addr_t start_p, tail_p;
687 int ii;
688 unsigned long num_frag;
689 skb_frag_t *frag;
690
691 num_frag = skb_shinfo(skb)->nr_frags;
692 frag = &skb_shinfo(skb)->frags[0];
693 start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
694 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
695
Brian Hill23ecc4b2010-05-26 20:44:30 -0700696 if (temac_check_tx_bd_space(lp, num_frag)) {
Grant Likely92744982009-04-25 12:53:39 +0000697 if (!netif_queue_stopped(ndev)) {
698 netif_stop_queue(ndev);
699 return NETDEV_TX_BUSY;
700 }
701 return NETDEV_TX_BUSY;
702 }
703
704 cur_p->app0 = 0;
705 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000706 unsigned int csum_start_off = skb_checksum_start_offset(skb);
Brian Hill23ecc4b2010-05-26 20:44:30 -0700707 unsigned int csum_index_off = csum_start_off + skb->csum_offset;
Grant Likely92744982009-04-25 12:53:39 +0000708
Brian Hill23ecc4b2010-05-26 20:44:30 -0700709 cur_p->app0 |= 1; /* TX Checksum Enabled */
710 cur_p->app1 = (csum_start_off << 16) | csum_index_off;
711 cur_p->app2 = 0; /* initial checksum seed */
Grant Likely92744982009-04-25 12:53:39 +0000712 }
Brian Hill23ecc4b2010-05-26 20:44:30 -0700713
Grant Likely92744982009-04-25 12:53:39 +0000714 cur_p->app0 |= STS_CTRL_APP0_SOP;
715 cur_p->len = skb_headlen(skb);
716 cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len,
717 DMA_TO_DEVICE);
718 cur_p->app4 = (unsigned long)skb;
719
720 for (ii = 0; ii < num_frag; ii++) {
721 lp->tx_bd_tail++;
722 if (lp->tx_bd_tail >= TX_BD_NUM)
723 lp->tx_bd_tail = 0;
724
725 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
726 cur_p->phys = dma_map_single(ndev->dev.parent,
Ian Campbell3ed6f692011-10-10 01:11:40 +0000727 skb_frag_address(frag),
Stephen Rothwell2edcd4c2011-11-02 01:49:44 -0400728 skb_frag_size(frag), DMA_TO_DEVICE);
729 cur_p->len = skb_frag_size(frag);
Grant Likely92744982009-04-25 12:53:39 +0000730 cur_p->app0 = 0;
731 frag++;
732 }
733 cur_p->app0 |= STS_CTRL_APP0_EOP;
734
735 tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
736 lp->tx_bd_tail++;
737 if (lp->tx_bd_tail >= TX_BD_NUM)
738 lp->tx_bd_tail = 0;
739
Richard Cochran93e0ed12011-06-19 21:51:26 +0000740 skb_tx_timestamp(skb);
741
Grant Likely92744982009-04-25 12:53:39 +0000742 /* Kick off the transfer */
John Linne44171f2010-04-08 07:08:02 +0000743 lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
Grant Likely92744982009-04-25 12:53:39 +0000744
Patrick McHardy6ed10652009-06-23 06:03:08 +0000745 return NETDEV_TX_OK;
Grant Likely92744982009-04-25 12:53:39 +0000746}
747
748
749static void ll_temac_recv(struct net_device *ndev)
750{
751 struct temac_local *lp = netdev_priv(ndev);
752 struct sk_buff *skb, *new_skb;
753 unsigned int bdstat;
754 struct cdmac_bd *cur_p;
755 dma_addr_t tail_p;
756 int length;
Grant Likely92744982009-04-25 12:53:39 +0000757 unsigned long flags;
758
759 spin_lock_irqsave(&lp->rx_lock, flags);
760
761 tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
762 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
763
764 bdstat = cur_p->app0;
765 while ((bdstat & STS_CTRL_APP0_CMPLT)) {
766
767 skb = lp->rx_skb[lp->rx_bd_ci];
Steven J. Magnanic3b7c122010-02-17 07:14:20 +0000768 length = cur_p->app4 & 0x3FFF;
Grant Likely92744982009-04-25 12:53:39 +0000769
John Linn33646d72010-04-08 07:08:01 +0000770 dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
Grant Likely92744982009-04-25 12:53:39 +0000771 DMA_FROM_DEVICE);
772
773 skb_put(skb, length);
Grant Likely92744982009-04-25 12:53:39 +0000774 skb->protocol = eth_type_trans(skb, ndev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700775 skb_checksum_none_assert(skb);
Grant Likely92744982009-04-25 12:53:39 +0000776
Brian Hill23ecc4b2010-05-26 20:44:30 -0700777 /* if we're doing rx csum offload, set it up */
778 if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
779 (skb->protocol == __constant_htons(ETH_P_IP)) &&
780 (skb->len > 64)) {
781
782 skb->csum = cur_p->app3 & 0xFFFF;
783 skb->ip_summed = CHECKSUM_COMPLETE;
784 }
785
Richard Cochran93e0ed12011-06-19 21:51:26 +0000786 if (!skb_defer_rx_timestamp(skb))
787 netif_rx(skb);
Grant Likely92744982009-04-25 12:53:39 +0000788
789 ndev->stats.rx_packets++;
790 ndev->stats.rx_bytes += length;
791
John Linne44171f2010-04-08 07:08:02 +0000792 new_skb = netdev_alloc_skb_ip_align(ndev,
793 XTE_MAX_JUMBO_FRAME_SIZE);
794
Grant Likely92744982009-04-25 12:53:39 +0000795 if (new_skb == 0) {
796 dev_err(&ndev->dev, "no memory for new sk_buff\n");
797 spin_unlock_irqrestore(&lp->rx_lock, flags);
798 return;
799 }
800
Grant Likely92744982009-04-25 12:53:39 +0000801 cur_p->app0 = STS_CTRL_APP0_IRQONEND;
802 cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
803 XTE_MAX_JUMBO_FRAME_SIZE,
804 DMA_FROM_DEVICE);
805 cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
806 lp->rx_skb[lp->rx_bd_ci] = new_skb;
807
808 lp->rx_bd_ci++;
809 if (lp->rx_bd_ci >= RX_BD_NUM)
810 lp->rx_bd_ci = 0;
811
812 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
813 bdstat = cur_p->app0;
814 }
John Linne44171f2010-04-08 07:08:02 +0000815 lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
Grant Likely92744982009-04-25 12:53:39 +0000816
817 spin_unlock_irqrestore(&lp->rx_lock, flags);
818}
819
820static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
821{
822 struct net_device *ndev = _ndev;
823 struct temac_local *lp = netdev_priv(ndev);
824 unsigned int status;
825
John Linne44171f2010-04-08 07:08:02 +0000826 status = lp->dma_in(lp, TX_IRQ_REG);
827 lp->dma_out(lp, TX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000828
829 if (status & (IRQ_COAL | IRQ_DLY))
830 temac_start_xmit_done(lp->ndev);
831 if (status & 0x080)
832 dev_err(&ndev->dev, "DMA error 0x%x\n", status);
833
834 return IRQ_HANDLED;
835}
836
837static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
838{
839 struct net_device *ndev = _ndev;
840 struct temac_local *lp = netdev_priv(ndev);
841 unsigned int status;
842
843 /* Read and clear the status registers */
John Linne44171f2010-04-08 07:08:02 +0000844 status = lp->dma_in(lp, RX_IRQ_REG);
845 lp->dma_out(lp, RX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000846
847 if (status & (IRQ_COAL | IRQ_DLY))
848 ll_temac_recv(lp->ndev);
849
850 return IRQ_HANDLED;
851}
852
853static int temac_open(struct net_device *ndev)
854{
855 struct temac_local *lp = netdev_priv(ndev);
856 int rc;
857
858 dev_dbg(&ndev->dev, "temac_open()\n");
859
860 if (lp->phy_node) {
861 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
862 temac_adjust_link, 0, 0);
863 if (!lp->phy_dev) {
864 dev_err(lp->dev, "of_phy_connect() failed\n");
865 return -ENODEV;
866 }
867
868 phy_start(lp->phy_dev);
869 }
870
Ricardo Ribalda50ec1532011-11-07 23:31:58 +0000871 temac_device_reset(ndev);
872
Grant Likely92744982009-04-25 12:53:39 +0000873 rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
874 if (rc)
875 goto err_tx_irq;
876 rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
877 if (rc)
878 goto err_rx_irq;
879
Grant Likely92744982009-04-25 12:53:39 +0000880 return 0;
881
882 err_rx_irq:
883 free_irq(lp->tx_irq, ndev);
884 err_tx_irq:
885 if (lp->phy_dev)
886 phy_disconnect(lp->phy_dev);
887 lp->phy_dev = NULL;
888 dev_err(lp->dev, "request_irq() failed\n");
889 return rc;
890}
891
892static int temac_stop(struct net_device *ndev)
893{
894 struct temac_local *lp = netdev_priv(ndev);
895
896 dev_dbg(&ndev->dev, "temac_close()\n");
897
898 free_irq(lp->tx_irq, ndev);
899 free_irq(lp->rx_irq, ndev);
900
901 if (lp->phy_dev)
902 phy_disconnect(lp->phy_dev);
903 lp->phy_dev = NULL;
904
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000905 temac_dma_bd_release(ndev);
906
Grant Likely92744982009-04-25 12:53:39 +0000907 return 0;
908}
909
910#ifdef CONFIG_NET_POLL_CONTROLLER
911static void
912temac_poll_controller(struct net_device *ndev)
913{
914 struct temac_local *lp = netdev_priv(ndev);
915
916 disable_irq(lp->tx_irq);
917 disable_irq(lp->rx_irq);
918
Michal Simek85399922010-08-18 00:26:34 +0000919 ll_temac_rx_irq(lp->tx_irq, ndev);
920 ll_temac_tx_irq(lp->rx_irq, ndev);
Grant Likely92744982009-04-25 12:53:39 +0000921
922 enable_irq(lp->tx_irq);
923 enable_irq(lp->rx_irq);
924}
925#endif
926
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000927static int temac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
928{
929 struct temac_local *lp = netdev_priv(ndev);
930
931 if (!netif_running(ndev))
932 return -EINVAL;
933
934 if (!lp->phy_dev)
935 return -EINVAL;
936
937 return phy_mii_ioctl(lp->phy_dev, rq, cmd);
938}
939
Grant Likely92744982009-04-25 12:53:39 +0000940static const struct net_device_ops temac_netdev_ops = {
941 .ndo_open = temac_open,
942 .ndo_stop = temac_stop,
943 .ndo_start_xmit = temac_start_xmit,
Jiri Pirko04e406d2013-01-01 03:30:19 +0000944 .ndo_set_mac_address = temac_set_mac_address,
Denis Kirjanov60eb5fd2010-07-10 11:10:44 +0000945 .ndo_validate_addr = eth_validate_addr,
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000946 .ndo_do_ioctl = temac_ioctl,
Grant Likely92744982009-04-25 12:53:39 +0000947#ifdef CONFIG_NET_POLL_CONTROLLER
948 .ndo_poll_controller = temac_poll_controller,
949#endif
950};
951
952/* ---------------------------------------------------------------------
953 * SYSFS device attributes
954 */
955static ssize_t temac_show_llink_regs(struct device *dev,
956 struct device_attribute *attr, char *buf)
957{
958 struct net_device *ndev = dev_get_drvdata(dev);
959 struct temac_local *lp = netdev_priv(ndev);
960 int i, len = 0;
961
962 for (i = 0; i < 0x11; i++)
John Linne44171f2010-04-08 07:08:02 +0000963 len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
Grant Likely92744982009-04-25 12:53:39 +0000964 (i % 8) == 7 ? "\n" : " ");
965 len += sprintf(buf + len, "\n");
966
967 return len;
968}
969
970static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
971
972static struct attribute *temac_device_attrs[] = {
973 &dev_attr_llink_regs.attr,
974 NULL,
975};
976
977static const struct attribute_group temac_attr_group = {
978 .attrs = temac_device_attrs,
979};
980
Ricardo9eac2d42011-10-18 21:35:25 +0000981/* ethtool support */
982static int temac_get_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
983{
984 struct temac_local *lp = netdev_priv(ndev);
985 return phy_ethtool_gset(lp->phy_dev, cmd);
986}
987
988static int temac_set_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
989{
990 struct temac_local *lp = netdev_priv(ndev);
991 return phy_ethtool_sset(lp->phy_dev, cmd);
992}
993
994static int temac_nway_reset(struct net_device *ndev)
995{
996 struct temac_local *lp = netdev_priv(ndev);
997 return phy_start_aneg(lp->phy_dev);
998}
999
1000static const struct ethtool_ops temac_ethtool_ops = {
1001 .get_settings = temac_get_settings,
1002 .set_settings = temac_set_settings,
1003 .nway_reset = temac_nway_reset,
1004 .get_link = ethtool_op_get_link,
Richard Cochranf85e5ea2012-04-03 22:59:30 +00001005 .get_ts_info = ethtool_op_get_ts_info,
Ricardo9eac2d42011-10-18 21:35:25 +00001006};
1007
Bill Pemberton06b0e682012-12-03 09:24:08 -05001008static int temac_of_probe(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +00001009{
1010 struct device_node *np;
1011 struct temac_local *lp;
1012 struct net_device *ndev;
1013 const void *addr;
Brian Hill23ecc4b2010-05-26 20:44:30 -07001014 __be32 *p;
Grant Likely92744982009-04-25 12:53:39 +00001015 int size, rc = 0;
Grant Likely92744982009-04-25 12:53:39 +00001016
1017 /* Init network device structure */
1018 ndev = alloc_etherdev(sizeof(*lp));
Joe Perches41de8d42012-01-29 13:47:52 +00001019 if (!ndev)
Grant Likely92744982009-04-25 12:53:39 +00001020 return -ENOMEM;
Joe Perches41de8d42012-01-29 13:47:52 +00001021
Grant Likely92744982009-04-25 12:53:39 +00001022 ether_setup(ndev);
1023 dev_set_drvdata(&op->dev, ndev);
1024 SET_NETDEV_DEV(ndev, &op->dev);
1025 ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
1026 ndev->features = NETIF_F_SG | NETIF_F_FRAGLIST;
1027 ndev->netdev_ops = &temac_netdev_ops;
Ricardo9eac2d42011-10-18 21:35:25 +00001028 ndev->ethtool_ops = &temac_ethtool_ops;
Grant Likely92744982009-04-25 12:53:39 +00001029#if 0
1030 ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
1031 ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
1032 ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
1033 ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
1034 ndev->features |= NETIF_F_HW_VLAN_TX; /* Transmit VLAN hw accel */
1035 ndev->features |= NETIF_F_HW_VLAN_RX; /* Receive VLAN hw acceleration */
1036 ndev->features |= NETIF_F_HW_VLAN_FILTER; /* Receive VLAN filtering */
1037 ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
1038 ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
1039 ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
1040 ndev->features |= NETIF_F_LRO; /* large receive offload */
1041#endif
1042
1043 /* setup temac private info structure */
1044 lp = netdev_priv(ndev);
1045 lp->ndev = ndev;
1046 lp->dev = &op->dev;
1047 lp->options = XTE_OPTION_DEFAULTS;
1048 spin_lock_init(&lp->rx_lock);
1049 mutex_init(&lp->indirect_mutex);
1050
1051 /* map device registers */
Grant Likely61c7a082010-04-13 16:12:29 -07001052 lp->regs = of_iomap(op->dev.of_node, 0);
Grant Likely92744982009-04-25 12:53:39 +00001053 if (!lp->regs) {
1054 dev_err(&op->dev, "could not map temac regs.\n");
1055 goto nodev;
1056 }
1057
Brian Hill23ecc4b2010-05-26 20:44:30 -07001058 /* Setup checksum offload, but default to off if not specified */
1059 lp->temac_features = 0;
1060 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
1061 if (p && be32_to_cpu(*p)) {
1062 lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
1063 /* Can checksum TCP/UDP over IPv4. */
1064 ndev->features |= NETIF_F_IP_CSUM;
1065 }
1066 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
1067 if (p && be32_to_cpu(*p))
1068 lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
1069
Grant Likely92744982009-04-25 12:53:39 +00001070 /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
Grant Likely61c7a082010-04-13 16:12:29 -07001071 np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
Grant Likely92744982009-04-25 12:53:39 +00001072 if (!np) {
1073 dev_err(&op->dev, "could not find DMA node\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001074 goto err_iounmap;
Grant Likely92744982009-04-25 12:53:39 +00001075 }
1076
John Linne44171f2010-04-08 07:08:02 +00001077 /* Setup the DMA register accesses, could be DCR or memory mapped */
1078 if (temac_dcr_setup(lp, op, np)) {
1079
1080 /* no DCR in the device tree, try non-DCR */
1081 lp->sdma_regs = of_iomap(np, 0);
1082 if (lp->sdma_regs) {
1083 lp->dma_in = temac_dma_in32;
1084 lp->dma_out = temac_dma_out32;
1085 dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
1086 } else {
1087 dev_err(&op->dev, "unable to map DMA registers\n");
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001088 of_node_put(np);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001089 goto err_iounmap;
John Linne44171f2010-04-08 07:08:02 +00001090 }
Grant Likely92744982009-04-25 12:53:39 +00001091 }
Grant Likely92744982009-04-25 12:53:39 +00001092
1093 lp->rx_irq = irq_of_parse_and_map(np, 0);
1094 lp->tx_irq = irq_of_parse_and_map(np, 1);
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001095
1096 of_node_put(np); /* Finished with the DMA node; drop the reference */
1097
Michal Simek4e68ea22011-12-21 15:42:50 -05001098 if (!lp->rx_irq || !lp->tx_irq) {
Grant Likely92744982009-04-25 12:53:39 +00001099 dev_err(&op->dev, "could not determine irqs\n");
1100 rc = -ENOMEM;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001101 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001102 }
1103
Grant Likely92744982009-04-25 12:53:39 +00001104
1105 /* Retrieve the MAC address */
Grant Likely61c7a082010-04-13 16:12:29 -07001106 addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
Grant Likely92744982009-04-25 12:53:39 +00001107 if ((!addr) || (size != 6)) {
1108 dev_err(&op->dev, "could not find MAC address\n");
1109 rc = -ENODEV;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001110 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001111 }
Jiri Pirko04e406d2013-01-01 03:30:19 +00001112 temac_init_mac_address(ndev, (void *)addr);
Grant Likely92744982009-04-25 12:53:39 +00001113
Grant Likely61c7a082010-04-13 16:12:29 -07001114 rc = temac_mdio_setup(lp, op->dev.of_node);
Grant Likely92744982009-04-25 12:53:39 +00001115 if (rc)
1116 dev_warn(&op->dev, "error registering MDIO bus\n");
1117
Grant Likely61c7a082010-04-13 16:12:29 -07001118 lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
Grant Likely92744982009-04-25 12:53:39 +00001119 if (lp->phy_node)
1120 dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
1121
1122 /* Add the device attributes */
1123 rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
1124 if (rc) {
1125 dev_err(lp->dev, "Error creating sysfs files\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001126 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001127 }
1128
1129 rc = register_netdev(lp->ndev);
1130 if (rc) {
1131 dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
1132 goto err_register_ndev;
1133 }
1134
1135 return 0;
1136
1137 err_register_ndev:
1138 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001139 err_iounmap_2:
1140 if (lp->sdma_regs)
1141 iounmap(lp->sdma_regs);
1142 err_iounmap:
1143 iounmap(lp->regs);
Grant Likely92744982009-04-25 12:53:39 +00001144 nodev:
1145 free_netdev(ndev);
1146 ndev = NULL;
1147 return rc;
1148}
1149
Bill Pemberton06b0e682012-12-03 09:24:08 -05001150static int temac_of_remove(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +00001151{
1152 struct net_device *ndev = dev_get_drvdata(&op->dev);
1153 struct temac_local *lp = netdev_priv(ndev);
1154
1155 temac_mdio_teardown(lp);
1156 unregister_netdev(ndev);
1157 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1158 if (lp->phy_node)
1159 of_node_put(lp->phy_node);
1160 lp->phy_node = NULL;
1161 dev_set_drvdata(&op->dev, NULL);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001162 iounmap(lp->regs);
1163 if (lp->sdma_regs)
1164 iounmap(lp->sdma_regs);
Grant Likely92744982009-04-25 12:53:39 +00001165 free_netdev(ndev);
1166 return 0;
1167}
1168
Bill Pemberton06b0e682012-12-03 09:24:08 -05001169static struct of_device_id temac_of_match[] = {
Grant Likely92744982009-04-25 12:53:39 +00001170 { .compatible = "xlnx,xps-ll-temac-1.01.b", },
Steven J. Magnanic3b7c122010-02-17 07:14:20 +00001171 { .compatible = "xlnx,xps-ll-temac-2.00.a", },
1172 { .compatible = "xlnx,xps-ll-temac-2.02.a", },
1173 { .compatible = "xlnx,xps-ll-temac-2.03.a", },
Grant Likely92744982009-04-25 12:53:39 +00001174 {},
1175};
1176MODULE_DEVICE_TABLE(of, temac_of_match);
1177
Grant Likely74888762011-02-22 21:05:51 -07001178static struct platform_driver temac_of_driver = {
Grant Likely92744982009-04-25 12:53:39 +00001179 .probe = temac_of_probe,
Bill Pemberton06b0e682012-12-03 09:24:08 -05001180 .remove = temac_of_remove,
Grant Likely92744982009-04-25 12:53:39 +00001181 .driver = {
1182 .owner = THIS_MODULE,
1183 .name = "xilinx_temac",
Grant Likely40182942010-04-13 16:13:02 -07001184 .of_match_table = temac_of_match,
Grant Likely92744982009-04-25 12:53:39 +00001185 },
1186};
1187
Axel Lindb62f682011-11-27 16:44:17 +00001188module_platform_driver(temac_of_driver);
Grant Likely92744982009-04-25 12:53:39 +00001189
1190MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1191MODULE_AUTHOR("Yoshio Kashiwagi");
1192MODULE_LICENSE("GPL");