blob: 9fc2ada4c3c2f22b001021264564ceca323f62ea [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);
Joe Perchesb2adaca2013-02-03 17:43:58 +0000241 if (!lp->rx_skb)
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000242 goto out;
Joe Perchesb2adaca2013-02-03 17:43:58 +0000243
Grant Likely92744982009-04-25 12:53:39 +0000244 /* allocate the tx and rx ring buffer descriptors. */
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400245 /* returns a virtual address and a physical address. */
Grant Likely92744982009-04-25 12:53:39 +0000246 lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
247 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
248 &lp->tx_bd_p, GFP_KERNEL);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000249 if (!lp->tx_bd_v) {
250 dev_err(&ndev->dev,
251 "unable to allocate DMA TX buffer descriptors");
252 goto out;
253 }
Grant Likely92744982009-04-25 12:53:39 +0000254 lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
255 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
256 &lp->rx_bd_p, GFP_KERNEL);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000257 if (!lp->rx_bd_v) {
258 dev_err(&ndev->dev,
259 "unable to allocate DMA RX buffer descriptors");
260 goto out;
261 }
Grant Likely92744982009-04-25 12:53:39 +0000262
263 memset(lp->tx_bd_v, 0, sizeof(*lp->tx_bd_v) * TX_BD_NUM);
264 for (i = 0; i < TX_BD_NUM; i++) {
265 lp->tx_bd_v[i].next = lp->tx_bd_p +
266 sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
267 }
268
269 memset(lp->rx_bd_v, 0, sizeof(*lp->rx_bd_v) * RX_BD_NUM);
270 for (i = 0; i < RX_BD_NUM; i++) {
271 lp->rx_bd_v[i].next = lp->rx_bd_p +
272 sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
273
John Linne44171f2010-04-08 07:08:02 +0000274 skb = netdev_alloc_skb_ip_align(ndev,
275 XTE_MAX_JUMBO_FRAME_SIZE);
276
Grant Likely92744982009-04-25 12:53:39 +0000277 if (skb == 0) {
278 dev_err(&ndev->dev, "alloc_skb error %d\n", i);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000279 goto out;
Grant Likely92744982009-04-25 12:53:39 +0000280 }
281 lp->rx_skb[i] = skb;
Grant Likely92744982009-04-25 12:53:39 +0000282 /* returns physical address of skb->data */
283 lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
284 skb->data,
285 XTE_MAX_JUMBO_FRAME_SIZE,
286 DMA_FROM_DEVICE);
287 lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
288 lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
289 }
290
John Linne44171f2010-04-08 07:08:02 +0000291 lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
Grant Likely92744982009-04-25 12:53:39 +0000292 CHNL_CTRL_IRQ_EN |
293 CHNL_CTRL_IRQ_DLY_EN |
294 CHNL_CTRL_IRQ_COAL_EN);
295 /* 0x10220483 */
296 /* 0x00100483 */
Brian Hill23ecc4b2010-05-26 20:44:30 -0700297 lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
Grant Likely92744982009-04-25 12:53:39 +0000298 CHNL_CTRL_IRQ_EN |
299 CHNL_CTRL_IRQ_DLY_EN |
300 CHNL_CTRL_IRQ_COAL_EN |
301 CHNL_CTRL_IRQ_IOE);
302 /* 0xff010283 */
303
John Linne44171f2010-04-08 07:08:02 +0000304 lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
305 lp->dma_out(lp, RX_TAILDESC_PTR,
Grant Likely92744982009-04-25 12:53:39 +0000306 lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
John Linne44171f2010-04-08 07:08:02 +0000307 lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
Grant Likely92744982009-04-25 12:53:39 +0000308
309 return 0;
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000310
311out:
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000312 temac_dma_bd_release(ndev);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000313 return -ENOMEM;
Grant Likely92744982009-04-25 12:53:39 +0000314}
315
316/* ---------------------------------------------------------------------
317 * net_device_ops
318 */
319
Jiri Pirko04e406d2013-01-01 03:30:19 +0000320static void temac_do_set_mac_address(struct net_device *ndev)
Grant Likely92744982009-04-25 12:53:39 +0000321{
322 struct temac_local *lp = netdev_priv(ndev);
323
Grant Likely92744982009-04-25 12:53:39 +0000324 /* set up unicast MAC address filter set its mac address */
325 mutex_lock(&lp->indirect_mutex);
326 temac_indirect_out32(lp, XTE_UAW0_OFFSET,
327 (ndev->dev_addr[0]) |
328 (ndev->dev_addr[1] << 8) |
329 (ndev->dev_addr[2] << 16) |
330 (ndev->dev_addr[3] << 24));
331 /* There are reserved bits in EUAW1
332 * so don't affect them Set MAC bits [47:32] in EUAW1 */
333 temac_indirect_out32(lp, XTE_UAW1_OFFSET,
334 (ndev->dev_addr[4] & 0x000000ff) |
335 (ndev->dev_addr[5] << 8));
336 mutex_unlock(&lp->indirect_mutex);
Jiri Pirko04e406d2013-01-01 03:30:19 +0000337}
Grant Likely92744982009-04-25 12:53:39 +0000338
Jiri Pirko04e406d2013-01-01 03:30:19 +0000339static int temac_init_mac_address(struct net_device *ndev, void *address)
340{
341 memcpy(ndev->dev_addr, address, ETH_ALEN);
342 if (!is_valid_ether_addr(ndev->dev_addr))
343 eth_hw_addr_random(ndev);
344 temac_do_set_mac_address(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000345 return 0;
346}
347
Jiri Pirko04e406d2013-01-01 03:30:19 +0000348static int temac_set_mac_address(struct net_device *ndev, void *p)
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000349{
350 struct sockaddr *addr = p;
351
Jiri Pirko04e406d2013-01-01 03:30:19 +0000352 if (!is_valid_ether_addr(addr->sa_data))
353 return -EADDRNOTAVAIL;
354 memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
355 temac_do_set_mac_address(ndev);
356 return 0;
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000357}
358
Grant Likely92744982009-04-25 12:53:39 +0000359static void temac_set_multicast_list(struct net_device *ndev)
360{
361 struct temac_local *lp = netdev_priv(ndev);
362 u32 multi_addr_msw, multi_addr_lsw, val;
363 int i;
364
365 mutex_lock(&lp->indirect_mutex);
Joe Perches8e95a202009-12-03 07:58:21 +0000366 if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000367 netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) {
Grant Likely92744982009-04-25 12:53:39 +0000368 /*
369 * We must make the kernel realise we had to move
370 * into promisc mode or we start all out war on
371 * the cable. If it was a promisc request the
372 * flag is already set. If not we assert it.
373 */
374 ndev->flags |= IFF_PROMISC;
375 temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
376 dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000377 } else if (!netdev_mc_empty(ndev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000378 struct netdev_hw_addr *ha;
Grant Likely92744982009-04-25 12:53:39 +0000379
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000380 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000381 netdev_for_each_mc_addr(ha, ndev) {
Grant Likely92744982009-04-25 12:53:39 +0000382 if (i >= MULTICAST_CAM_TABLE_NUM)
383 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000384 multi_addr_msw = ((ha->addr[3] << 24) |
385 (ha->addr[2] << 16) |
386 (ha->addr[1] << 8) |
387 (ha->addr[0]));
Grant Likely92744982009-04-25 12:53:39 +0000388 temac_indirect_out32(lp, XTE_MAW0_OFFSET,
389 multi_addr_msw);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000390 multi_addr_lsw = ((ha->addr[5] << 8) |
391 (ha->addr[4]) | (i << 16));
Grant Likely92744982009-04-25 12:53:39 +0000392 temac_indirect_out32(lp, XTE_MAW1_OFFSET,
393 multi_addr_lsw);
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000394 i++;
Grant Likely92744982009-04-25 12:53:39 +0000395 }
396 } else {
397 val = temac_indirect_in32(lp, XTE_AFM_OFFSET);
398 temac_indirect_out32(lp, XTE_AFM_OFFSET,
399 val & ~XTE_AFM_EPPRM_MASK);
400 temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0);
401 temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0);
402 dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
403 }
404 mutex_unlock(&lp->indirect_mutex);
405}
406
407struct temac_option {
408 int flg;
409 u32 opt;
410 u32 reg;
411 u32 m_or;
412 u32 m_and;
413} temac_options[] = {
414 /* Turn on jumbo packet support for both Rx and Tx */
415 {
416 .opt = XTE_OPTION_JUMBO,
417 .reg = XTE_TXC_OFFSET,
418 .m_or = XTE_TXC_TXJMBO_MASK,
419 },
420 {
421 .opt = XTE_OPTION_JUMBO,
422 .reg = XTE_RXC1_OFFSET,
423 .m_or =XTE_RXC1_RXJMBO_MASK,
424 },
425 /* Turn on VLAN packet support for both Rx and Tx */
426 {
427 .opt = XTE_OPTION_VLAN,
428 .reg = XTE_TXC_OFFSET,
429 .m_or =XTE_TXC_TXVLAN_MASK,
430 },
431 {
432 .opt = XTE_OPTION_VLAN,
433 .reg = XTE_RXC1_OFFSET,
434 .m_or =XTE_RXC1_RXVLAN_MASK,
435 },
436 /* Turn on FCS stripping on receive packets */
437 {
438 .opt = XTE_OPTION_FCS_STRIP,
439 .reg = XTE_RXC1_OFFSET,
440 .m_or =XTE_RXC1_RXFCS_MASK,
441 },
442 /* Turn on FCS insertion on transmit packets */
443 {
444 .opt = XTE_OPTION_FCS_INSERT,
445 .reg = XTE_TXC_OFFSET,
446 .m_or =XTE_TXC_TXFCS_MASK,
447 },
448 /* Turn on length/type field checking on receive packets */
449 {
450 .opt = XTE_OPTION_LENTYPE_ERR,
451 .reg = XTE_RXC1_OFFSET,
452 .m_or =XTE_RXC1_RXLT_MASK,
453 },
454 /* Turn on flow control */
455 {
456 .opt = XTE_OPTION_FLOW_CONTROL,
457 .reg = XTE_FCC_OFFSET,
458 .m_or =XTE_FCC_RXFLO_MASK,
459 },
460 /* Turn on flow control */
461 {
462 .opt = XTE_OPTION_FLOW_CONTROL,
463 .reg = XTE_FCC_OFFSET,
464 .m_or =XTE_FCC_TXFLO_MASK,
465 },
466 /* Turn on promiscuous frame filtering (all frames are received ) */
467 {
468 .opt = XTE_OPTION_PROMISC,
469 .reg = XTE_AFM_OFFSET,
470 .m_or =XTE_AFM_EPPRM_MASK,
471 },
472 /* Enable transmitter if not already enabled */
473 {
474 .opt = XTE_OPTION_TXEN,
475 .reg = XTE_TXC_OFFSET,
476 .m_or =XTE_TXC_TXEN_MASK,
477 },
478 /* Enable receiver? */
479 {
480 .opt = XTE_OPTION_RXEN,
481 .reg = XTE_RXC1_OFFSET,
482 .m_or =XTE_RXC1_RXEN_MASK,
483 },
484 {}
485};
486
487/**
488 * temac_setoptions
489 */
490static u32 temac_setoptions(struct net_device *ndev, u32 options)
491{
492 struct temac_local *lp = netdev_priv(ndev);
493 struct temac_option *tp = &temac_options[0];
494 int reg;
495
496 mutex_lock(&lp->indirect_mutex);
497 while (tp->opt) {
498 reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or;
499 if (options & tp->opt)
500 reg |= tp->m_or;
501 temac_indirect_out32(lp, tp->reg, reg);
502 tp++;
503 }
504 lp->options |= options;
505 mutex_unlock(&lp->indirect_mutex);
506
Eric Dumazet807540b2010-09-23 05:40:09 +0000507 return 0;
Grant Likely92744982009-04-25 12:53:39 +0000508}
509
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200510/* Initialize temac */
Grant Likely92744982009-04-25 12:53:39 +0000511static void temac_device_reset(struct net_device *ndev)
512{
513 struct temac_local *lp = netdev_priv(ndev);
514 u32 timeout;
515 u32 val;
516
517 /* Perform a software reset */
518
519 /* 0x300 host enable bit ? */
520 /* reset PHY through control register ?:1 */
521
522 dev_dbg(&ndev->dev, "%s()\n", __func__);
523
524 mutex_lock(&lp->indirect_mutex);
525 /* Reset the receiver and wait for it to finish reset */
526 temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
527 timeout = 1000;
528 while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
529 udelay(1);
530 if (--timeout == 0) {
531 dev_err(&ndev->dev,
532 "temac_device_reset RX reset timeout!!\n");
533 break;
534 }
535 }
536
537 /* Reset the transmitter and wait for it to finish reset */
538 temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
539 timeout = 1000;
540 while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
541 udelay(1);
542 if (--timeout == 0) {
543 dev_err(&ndev->dev,
544 "temac_device_reset TX reset timeout!!\n");
545 break;
546 }
547 }
548
549 /* Disable the receiver */
550 val = temac_indirect_in32(lp, XTE_RXC1_OFFSET);
551 temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
552
553 /* Reset Local Link (DMA) */
John Linne44171f2010-04-08 07:08:02 +0000554 lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
Grant Likely92744982009-04-25 12:53:39 +0000555 timeout = 1000;
John Linne44171f2010-04-08 07:08:02 +0000556 while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
Grant Likely92744982009-04-25 12:53:39 +0000557 udelay(1);
558 if (--timeout == 0) {
559 dev_err(&ndev->dev,
560 "temac_device_reset DMA reset timeout!!\n");
561 break;
562 }
563 }
John Linne44171f2010-04-08 07:08:02 +0000564 lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
Grant Likely92744982009-04-25 12:53:39 +0000565
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000566 if (temac_dma_bd_init(ndev)) {
567 dev_err(&ndev->dev,
568 "temac_device_reset descriptor allocation failed\n");
569 }
Grant Likely92744982009-04-25 12:53:39 +0000570
571 temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0);
572 temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0);
573 temac_indirect_out32(lp, XTE_TXC_OFFSET, 0);
574 temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
575
576 mutex_unlock(&lp->indirect_mutex);
577
578 /* Sync default options with HW
579 * but leave receiver and transmitter disabled. */
580 temac_setoptions(ndev,
581 lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
582
Jiri Pirko04e406d2013-01-01 03:30:19 +0000583 temac_do_set_mac_address(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000584
585 /* Set address filter table */
586 temac_set_multicast_list(ndev);
587 if (temac_setoptions(ndev, lp->options))
588 dev_err(&ndev->dev, "Error setting TEMAC options\n");
589
590 /* Init Driver variable */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700591 ndev->trans_start = jiffies; /* prevent tx timeout */
Grant Likely92744982009-04-25 12:53:39 +0000592}
593
594void temac_adjust_link(struct net_device *ndev)
595{
596 struct temac_local *lp = netdev_priv(ndev);
597 struct phy_device *phy = lp->phy_dev;
598 u32 mii_speed;
599 int link_state;
600
601 /* hash together the state values to decide if something has changed */
602 link_state = phy->speed | (phy->duplex << 1) | phy->link;
603
604 mutex_lock(&lp->indirect_mutex);
605 if (lp->last_link != link_state) {
606 mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET);
607 mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
608
609 switch (phy->speed) {
610 case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
611 case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
612 case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
613 }
614
615 /* Write new speed setting out to TEMAC */
616 temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed);
617 lp->last_link = link_state;
618 phy_print_status(phy);
619 }
620 mutex_unlock(&lp->indirect_mutex);
621}
622
623static void temac_start_xmit_done(struct net_device *ndev)
624{
625 struct temac_local *lp = netdev_priv(ndev);
626 struct cdmac_bd *cur_p;
627 unsigned int stat = 0;
628
629 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
630 stat = cur_p->app0;
631
632 while (stat & STS_CTRL_APP0_CMPLT) {
633 dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len,
634 DMA_TO_DEVICE);
635 if (cur_p->app4)
636 dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
637 cur_p->app0 = 0;
Brian Hill23ecc4b2010-05-26 20:44:30 -0700638 cur_p->app1 = 0;
639 cur_p->app2 = 0;
640 cur_p->app3 = 0;
641 cur_p->app4 = 0;
Grant Likely92744982009-04-25 12:53:39 +0000642
643 ndev->stats.tx_packets++;
644 ndev->stats.tx_bytes += cur_p->len;
645
646 lp->tx_bd_ci++;
647 if (lp->tx_bd_ci >= TX_BD_NUM)
648 lp->tx_bd_ci = 0;
649
650 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
651 stat = cur_p->app0;
652 }
653
654 netif_wake_queue(ndev);
655}
656
Brian Hill23ecc4b2010-05-26 20:44:30 -0700657static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
658{
659 struct cdmac_bd *cur_p;
660 int tail;
661
662 tail = lp->tx_bd_tail;
663 cur_p = &lp->tx_bd_v[tail];
664
665 do {
666 if (cur_p->app0)
667 return NETDEV_TX_BUSY;
668
669 tail++;
670 if (tail >= TX_BD_NUM)
671 tail = 0;
672
673 cur_p = &lp->tx_bd_v[tail];
674 num_frag--;
675 } while (num_frag >= 0);
676
677 return 0;
678}
679
Grant Likely92744982009-04-25 12:53:39 +0000680static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
681{
682 struct temac_local *lp = netdev_priv(ndev);
683 struct cdmac_bd *cur_p;
684 dma_addr_t start_p, tail_p;
685 int ii;
686 unsigned long num_frag;
687 skb_frag_t *frag;
688
689 num_frag = skb_shinfo(skb)->nr_frags;
690 frag = &skb_shinfo(skb)->frags[0];
691 start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
692 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
693
Brian Hill23ecc4b2010-05-26 20:44:30 -0700694 if (temac_check_tx_bd_space(lp, num_frag)) {
Grant Likely92744982009-04-25 12:53:39 +0000695 if (!netif_queue_stopped(ndev)) {
696 netif_stop_queue(ndev);
697 return NETDEV_TX_BUSY;
698 }
699 return NETDEV_TX_BUSY;
700 }
701
702 cur_p->app0 = 0;
703 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000704 unsigned int csum_start_off = skb_checksum_start_offset(skb);
Brian Hill23ecc4b2010-05-26 20:44:30 -0700705 unsigned int csum_index_off = csum_start_off + skb->csum_offset;
Grant Likely92744982009-04-25 12:53:39 +0000706
Brian Hill23ecc4b2010-05-26 20:44:30 -0700707 cur_p->app0 |= 1; /* TX Checksum Enabled */
708 cur_p->app1 = (csum_start_off << 16) | csum_index_off;
709 cur_p->app2 = 0; /* initial checksum seed */
Grant Likely92744982009-04-25 12:53:39 +0000710 }
Brian Hill23ecc4b2010-05-26 20:44:30 -0700711
Grant Likely92744982009-04-25 12:53:39 +0000712 cur_p->app0 |= STS_CTRL_APP0_SOP;
713 cur_p->len = skb_headlen(skb);
714 cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len,
715 DMA_TO_DEVICE);
716 cur_p->app4 = (unsigned long)skb;
717
718 for (ii = 0; ii < num_frag; ii++) {
719 lp->tx_bd_tail++;
720 if (lp->tx_bd_tail >= TX_BD_NUM)
721 lp->tx_bd_tail = 0;
722
723 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
724 cur_p->phys = dma_map_single(ndev->dev.parent,
Ian Campbell3ed6f692011-10-10 01:11:40 +0000725 skb_frag_address(frag),
Stephen Rothwell2edcd4c2011-11-02 01:49:44 -0400726 skb_frag_size(frag), DMA_TO_DEVICE);
727 cur_p->len = skb_frag_size(frag);
Grant Likely92744982009-04-25 12:53:39 +0000728 cur_p->app0 = 0;
729 frag++;
730 }
731 cur_p->app0 |= STS_CTRL_APP0_EOP;
732
733 tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
734 lp->tx_bd_tail++;
735 if (lp->tx_bd_tail >= TX_BD_NUM)
736 lp->tx_bd_tail = 0;
737
Richard Cochran93e0ed12011-06-19 21:51:26 +0000738 skb_tx_timestamp(skb);
739
Grant Likely92744982009-04-25 12:53:39 +0000740 /* Kick off the transfer */
John Linne44171f2010-04-08 07:08:02 +0000741 lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
Grant Likely92744982009-04-25 12:53:39 +0000742
Patrick McHardy6ed10652009-06-23 06:03:08 +0000743 return NETDEV_TX_OK;
Grant Likely92744982009-04-25 12:53:39 +0000744}
745
746
747static void ll_temac_recv(struct net_device *ndev)
748{
749 struct temac_local *lp = netdev_priv(ndev);
750 struct sk_buff *skb, *new_skb;
751 unsigned int bdstat;
752 struct cdmac_bd *cur_p;
753 dma_addr_t tail_p;
754 int length;
Grant Likely92744982009-04-25 12:53:39 +0000755 unsigned long flags;
756
757 spin_lock_irqsave(&lp->rx_lock, flags);
758
759 tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
760 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
761
762 bdstat = cur_p->app0;
763 while ((bdstat & STS_CTRL_APP0_CMPLT)) {
764
765 skb = lp->rx_skb[lp->rx_bd_ci];
Steven J. Magnanic3b7c122010-02-17 07:14:20 +0000766 length = cur_p->app4 & 0x3FFF;
Grant Likely92744982009-04-25 12:53:39 +0000767
John Linn33646d72010-04-08 07:08:01 +0000768 dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
Grant Likely92744982009-04-25 12:53:39 +0000769 DMA_FROM_DEVICE);
770
771 skb_put(skb, length);
Grant Likely92744982009-04-25 12:53:39 +0000772 skb->protocol = eth_type_trans(skb, ndev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700773 skb_checksum_none_assert(skb);
Grant Likely92744982009-04-25 12:53:39 +0000774
Brian Hill23ecc4b2010-05-26 20:44:30 -0700775 /* if we're doing rx csum offload, set it up */
776 if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
777 (skb->protocol == __constant_htons(ETH_P_IP)) &&
778 (skb->len > 64)) {
779
780 skb->csum = cur_p->app3 & 0xFFFF;
781 skb->ip_summed = CHECKSUM_COMPLETE;
782 }
783
Richard Cochran93e0ed12011-06-19 21:51:26 +0000784 if (!skb_defer_rx_timestamp(skb))
785 netif_rx(skb);
Grant Likely92744982009-04-25 12:53:39 +0000786
787 ndev->stats.rx_packets++;
788 ndev->stats.rx_bytes += length;
789
John Linne44171f2010-04-08 07:08:02 +0000790 new_skb = netdev_alloc_skb_ip_align(ndev,
791 XTE_MAX_JUMBO_FRAME_SIZE);
792
Grant Likely92744982009-04-25 12:53:39 +0000793 if (new_skb == 0) {
794 dev_err(&ndev->dev, "no memory for new sk_buff\n");
795 spin_unlock_irqrestore(&lp->rx_lock, flags);
796 return;
797 }
798
Grant Likely92744982009-04-25 12:53:39 +0000799 cur_p->app0 = STS_CTRL_APP0_IRQONEND;
800 cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
801 XTE_MAX_JUMBO_FRAME_SIZE,
802 DMA_FROM_DEVICE);
803 cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
804 lp->rx_skb[lp->rx_bd_ci] = new_skb;
805
806 lp->rx_bd_ci++;
807 if (lp->rx_bd_ci >= RX_BD_NUM)
808 lp->rx_bd_ci = 0;
809
810 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
811 bdstat = cur_p->app0;
812 }
John Linne44171f2010-04-08 07:08:02 +0000813 lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
Grant Likely92744982009-04-25 12:53:39 +0000814
815 spin_unlock_irqrestore(&lp->rx_lock, flags);
816}
817
818static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
819{
820 struct net_device *ndev = _ndev;
821 struct temac_local *lp = netdev_priv(ndev);
822 unsigned int status;
823
John Linne44171f2010-04-08 07:08:02 +0000824 status = lp->dma_in(lp, TX_IRQ_REG);
825 lp->dma_out(lp, TX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000826
827 if (status & (IRQ_COAL | IRQ_DLY))
828 temac_start_xmit_done(lp->ndev);
829 if (status & 0x080)
830 dev_err(&ndev->dev, "DMA error 0x%x\n", status);
831
832 return IRQ_HANDLED;
833}
834
835static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
836{
837 struct net_device *ndev = _ndev;
838 struct temac_local *lp = netdev_priv(ndev);
839 unsigned int status;
840
841 /* Read and clear the status registers */
John Linne44171f2010-04-08 07:08:02 +0000842 status = lp->dma_in(lp, RX_IRQ_REG);
843 lp->dma_out(lp, RX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000844
845 if (status & (IRQ_COAL | IRQ_DLY))
846 ll_temac_recv(lp->ndev);
847
848 return IRQ_HANDLED;
849}
850
851static int temac_open(struct net_device *ndev)
852{
853 struct temac_local *lp = netdev_priv(ndev);
854 int rc;
855
856 dev_dbg(&ndev->dev, "temac_open()\n");
857
858 if (lp->phy_node) {
859 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
860 temac_adjust_link, 0, 0);
861 if (!lp->phy_dev) {
862 dev_err(lp->dev, "of_phy_connect() failed\n");
863 return -ENODEV;
864 }
865
866 phy_start(lp->phy_dev);
867 }
868
Ricardo Ribalda50ec1532011-11-07 23:31:58 +0000869 temac_device_reset(ndev);
870
Grant Likely92744982009-04-25 12:53:39 +0000871 rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
872 if (rc)
873 goto err_tx_irq;
874 rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
875 if (rc)
876 goto err_rx_irq;
877
Grant Likely92744982009-04-25 12:53:39 +0000878 return 0;
879
880 err_rx_irq:
881 free_irq(lp->tx_irq, ndev);
882 err_tx_irq:
883 if (lp->phy_dev)
884 phy_disconnect(lp->phy_dev);
885 lp->phy_dev = NULL;
886 dev_err(lp->dev, "request_irq() failed\n");
887 return rc;
888}
889
890static int temac_stop(struct net_device *ndev)
891{
892 struct temac_local *lp = netdev_priv(ndev);
893
894 dev_dbg(&ndev->dev, "temac_close()\n");
895
896 free_irq(lp->tx_irq, ndev);
897 free_irq(lp->rx_irq, ndev);
898
899 if (lp->phy_dev)
900 phy_disconnect(lp->phy_dev);
901 lp->phy_dev = NULL;
902
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000903 temac_dma_bd_release(ndev);
904
Grant Likely92744982009-04-25 12:53:39 +0000905 return 0;
906}
907
908#ifdef CONFIG_NET_POLL_CONTROLLER
909static void
910temac_poll_controller(struct net_device *ndev)
911{
912 struct temac_local *lp = netdev_priv(ndev);
913
914 disable_irq(lp->tx_irq);
915 disable_irq(lp->rx_irq);
916
Michal Simek85399922010-08-18 00:26:34 +0000917 ll_temac_rx_irq(lp->tx_irq, ndev);
918 ll_temac_tx_irq(lp->rx_irq, ndev);
Grant Likely92744982009-04-25 12:53:39 +0000919
920 enable_irq(lp->tx_irq);
921 enable_irq(lp->rx_irq);
922}
923#endif
924
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000925static int temac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
926{
927 struct temac_local *lp = netdev_priv(ndev);
928
929 if (!netif_running(ndev))
930 return -EINVAL;
931
932 if (!lp->phy_dev)
933 return -EINVAL;
934
935 return phy_mii_ioctl(lp->phy_dev, rq, cmd);
936}
937
Grant Likely92744982009-04-25 12:53:39 +0000938static const struct net_device_ops temac_netdev_ops = {
939 .ndo_open = temac_open,
940 .ndo_stop = temac_stop,
941 .ndo_start_xmit = temac_start_xmit,
Jiri Pirko04e406d2013-01-01 03:30:19 +0000942 .ndo_set_mac_address = temac_set_mac_address,
Denis Kirjanov60eb5fd2010-07-10 11:10:44 +0000943 .ndo_validate_addr = eth_validate_addr,
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000944 .ndo_do_ioctl = temac_ioctl,
Grant Likely92744982009-04-25 12:53:39 +0000945#ifdef CONFIG_NET_POLL_CONTROLLER
946 .ndo_poll_controller = temac_poll_controller,
947#endif
948};
949
950/* ---------------------------------------------------------------------
951 * SYSFS device attributes
952 */
953static ssize_t temac_show_llink_regs(struct device *dev,
954 struct device_attribute *attr, char *buf)
955{
956 struct net_device *ndev = dev_get_drvdata(dev);
957 struct temac_local *lp = netdev_priv(ndev);
958 int i, len = 0;
959
960 for (i = 0; i < 0x11; i++)
John Linne44171f2010-04-08 07:08:02 +0000961 len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
Grant Likely92744982009-04-25 12:53:39 +0000962 (i % 8) == 7 ? "\n" : " ");
963 len += sprintf(buf + len, "\n");
964
965 return len;
966}
967
968static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
969
970static struct attribute *temac_device_attrs[] = {
971 &dev_attr_llink_regs.attr,
972 NULL,
973};
974
975static const struct attribute_group temac_attr_group = {
976 .attrs = temac_device_attrs,
977};
978
Ricardo9eac2d42011-10-18 21:35:25 +0000979/* ethtool support */
980static int temac_get_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
981{
982 struct temac_local *lp = netdev_priv(ndev);
983 return phy_ethtool_gset(lp->phy_dev, cmd);
984}
985
986static int temac_set_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
987{
988 struct temac_local *lp = netdev_priv(ndev);
989 return phy_ethtool_sset(lp->phy_dev, cmd);
990}
991
992static int temac_nway_reset(struct net_device *ndev)
993{
994 struct temac_local *lp = netdev_priv(ndev);
995 return phy_start_aneg(lp->phy_dev);
996}
997
998static const struct ethtool_ops temac_ethtool_ops = {
999 .get_settings = temac_get_settings,
1000 .set_settings = temac_set_settings,
1001 .nway_reset = temac_nway_reset,
1002 .get_link = ethtool_op_get_link,
Richard Cochranf85e5ea2012-04-03 22:59:30 +00001003 .get_ts_info = ethtool_op_get_ts_info,
Ricardo9eac2d42011-10-18 21:35:25 +00001004};
1005
Bill Pemberton06b0e682012-12-03 09:24:08 -05001006static int temac_of_probe(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +00001007{
1008 struct device_node *np;
1009 struct temac_local *lp;
1010 struct net_device *ndev;
1011 const void *addr;
Brian Hill23ecc4b2010-05-26 20:44:30 -07001012 __be32 *p;
Grant Likely92744982009-04-25 12:53:39 +00001013 int size, rc = 0;
Grant Likely92744982009-04-25 12:53:39 +00001014
1015 /* Init network device structure */
1016 ndev = alloc_etherdev(sizeof(*lp));
Joe Perches41de8d42012-01-29 13:47:52 +00001017 if (!ndev)
Grant Likely92744982009-04-25 12:53:39 +00001018 return -ENOMEM;
Joe Perches41de8d42012-01-29 13:47:52 +00001019
Grant Likely92744982009-04-25 12:53:39 +00001020 ether_setup(ndev);
1021 dev_set_drvdata(&op->dev, ndev);
1022 SET_NETDEV_DEV(ndev, &op->dev);
1023 ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
1024 ndev->features = NETIF_F_SG | NETIF_F_FRAGLIST;
1025 ndev->netdev_ops = &temac_netdev_ops;
Ricardo9eac2d42011-10-18 21:35:25 +00001026 ndev->ethtool_ops = &temac_ethtool_ops;
Grant Likely92744982009-04-25 12:53:39 +00001027#if 0
1028 ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
1029 ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
1030 ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
1031 ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
1032 ndev->features |= NETIF_F_HW_VLAN_TX; /* Transmit VLAN hw accel */
1033 ndev->features |= NETIF_F_HW_VLAN_RX; /* Receive VLAN hw acceleration */
1034 ndev->features |= NETIF_F_HW_VLAN_FILTER; /* Receive VLAN filtering */
1035 ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
1036 ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
1037 ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
1038 ndev->features |= NETIF_F_LRO; /* large receive offload */
1039#endif
1040
1041 /* setup temac private info structure */
1042 lp = netdev_priv(ndev);
1043 lp->ndev = ndev;
1044 lp->dev = &op->dev;
1045 lp->options = XTE_OPTION_DEFAULTS;
1046 spin_lock_init(&lp->rx_lock);
1047 mutex_init(&lp->indirect_mutex);
1048
1049 /* map device registers */
Grant Likely61c7a082010-04-13 16:12:29 -07001050 lp->regs = of_iomap(op->dev.of_node, 0);
Grant Likely92744982009-04-25 12:53:39 +00001051 if (!lp->regs) {
1052 dev_err(&op->dev, "could not map temac regs.\n");
1053 goto nodev;
1054 }
1055
Brian Hill23ecc4b2010-05-26 20:44:30 -07001056 /* Setup checksum offload, but default to off if not specified */
1057 lp->temac_features = 0;
1058 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
1059 if (p && be32_to_cpu(*p)) {
1060 lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
1061 /* Can checksum TCP/UDP over IPv4. */
1062 ndev->features |= NETIF_F_IP_CSUM;
1063 }
1064 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
1065 if (p && be32_to_cpu(*p))
1066 lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
1067
Grant Likely92744982009-04-25 12:53:39 +00001068 /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
Grant Likely61c7a082010-04-13 16:12:29 -07001069 np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
Grant Likely92744982009-04-25 12:53:39 +00001070 if (!np) {
1071 dev_err(&op->dev, "could not find DMA node\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001072 goto err_iounmap;
Grant Likely92744982009-04-25 12:53:39 +00001073 }
1074
John Linne44171f2010-04-08 07:08:02 +00001075 /* Setup the DMA register accesses, could be DCR or memory mapped */
1076 if (temac_dcr_setup(lp, op, np)) {
1077
1078 /* no DCR in the device tree, try non-DCR */
1079 lp->sdma_regs = of_iomap(np, 0);
1080 if (lp->sdma_regs) {
1081 lp->dma_in = temac_dma_in32;
1082 lp->dma_out = temac_dma_out32;
1083 dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
1084 } else {
1085 dev_err(&op->dev, "unable to map DMA registers\n");
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001086 of_node_put(np);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001087 goto err_iounmap;
John Linne44171f2010-04-08 07:08:02 +00001088 }
Grant Likely92744982009-04-25 12:53:39 +00001089 }
Grant Likely92744982009-04-25 12:53:39 +00001090
1091 lp->rx_irq = irq_of_parse_and_map(np, 0);
1092 lp->tx_irq = irq_of_parse_and_map(np, 1);
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001093
1094 of_node_put(np); /* Finished with the DMA node; drop the reference */
1095
Michal Simek4e68ea22011-12-21 15:42:50 -05001096 if (!lp->rx_irq || !lp->tx_irq) {
Grant Likely92744982009-04-25 12:53:39 +00001097 dev_err(&op->dev, "could not determine irqs\n");
1098 rc = -ENOMEM;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001099 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001100 }
1101
Grant Likely92744982009-04-25 12:53:39 +00001102
1103 /* Retrieve the MAC address */
Grant Likely61c7a082010-04-13 16:12:29 -07001104 addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
Grant Likely92744982009-04-25 12:53:39 +00001105 if ((!addr) || (size != 6)) {
1106 dev_err(&op->dev, "could not find MAC address\n");
1107 rc = -ENODEV;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001108 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001109 }
Jiri Pirko04e406d2013-01-01 03:30:19 +00001110 temac_init_mac_address(ndev, (void *)addr);
Grant Likely92744982009-04-25 12:53:39 +00001111
Grant Likely61c7a082010-04-13 16:12:29 -07001112 rc = temac_mdio_setup(lp, op->dev.of_node);
Grant Likely92744982009-04-25 12:53:39 +00001113 if (rc)
1114 dev_warn(&op->dev, "error registering MDIO bus\n");
1115
Grant Likely61c7a082010-04-13 16:12:29 -07001116 lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
Grant Likely92744982009-04-25 12:53:39 +00001117 if (lp->phy_node)
1118 dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
1119
1120 /* Add the device attributes */
1121 rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
1122 if (rc) {
1123 dev_err(lp->dev, "Error creating sysfs files\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001124 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001125 }
1126
1127 rc = register_netdev(lp->ndev);
1128 if (rc) {
1129 dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
1130 goto err_register_ndev;
1131 }
1132
1133 return 0;
1134
1135 err_register_ndev:
1136 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001137 err_iounmap_2:
1138 if (lp->sdma_regs)
1139 iounmap(lp->sdma_regs);
1140 err_iounmap:
1141 iounmap(lp->regs);
Grant Likely92744982009-04-25 12:53:39 +00001142 nodev:
1143 free_netdev(ndev);
1144 ndev = NULL;
1145 return rc;
1146}
1147
Bill Pemberton06b0e682012-12-03 09:24:08 -05001148static int temac_of_remove(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +00001149{
1150 struct net_device *ndev = dev_get_drvdata(&op->dev);
1151 struct temac_local *lp = netdev_priv(ndev);
1152
1153 temac_mdio_teardown(lp);
1154 unregister_netdev(ndev);
1155 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1156 if (lp->phy_node)
1157 of_node_put(lp->phy_node);
1158 lp->phy_node = NULL;
1159 dev_set_drvdata(&op->dev, NULL);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001160 iounmap(lp->regs);
1161 if (lp->sdma_regs)
1162 iounmap(lp->sdma_regs);
Grant Likely92744982009-04-25 12:53:39 +00001163 free_netdev(ndev);
1164 return 0;
1165}
1166
Bill Pemberton06b0e682012-12-03 09:24:08 -05001167static struct of_device_id temac_of_match[] = {
Grant Likely92744982009-04-25 12:53:39 +00001168 { .compatible = "xlnx,xps-ll-temac-1.01.b", },
Steven J. Magnanic3b7c122010-02-17 07:14:20 +00001169 { .compatible = "xlnx,xps-ll-temac-2.00.a", },
1170 { .compatible = "xlnx,xps-ll-temac-2.02.a", },
1171 { .compatible = "xlnx,xps-ll-temac-2.03.a", },
Grant Likely92744982009-04-25 12:53:39 +00001172 {},
1173};
1174MODULE_DEVICE_TABLE(of, temac_of_match);
1175
Grant Likely74888762011-02-22 21:05:51 -07001176static struct platform_driver temac_of_driver = {
Grant Likely92744982009-04-25 12:53:39 +00001177 .probe = temac_of_probe,
Bill Pemberton06b0e682012-12-03 09:24:08 -05001178 .remove = temac_of_remove,
Grant Likely92744982009-04-25 12:53:39 +00001179 .driver = {
1180 .owner = THIS_MODULE,
1181 .name = "xilinx_temac",
Grant Likely40182942010-04-13 16:13:02 -07001182 .of_match_table = temac_of_match,
Grant Likely92744982009-04-25 12:53:39 +00001183 },
1184};
1185
Axel Lindb62f682011-11-27 16:44:17 +00001186module_platform_driver(temac_of_driver);
Grant Likely92744982009-04-25 12:53:39 +00001187
1188MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1189MODULE_AUTHOR("Yoshio Kashiwagi");
1190MODULE_LICENSE("GPL");