blob: e3f19255af284016b2ac3f72743d9a646ee7ac3a [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>
Grant Likely92744982009-04-25 12:53:39 +000052
53#include "ll_temac.h"
54
55#define TX_BD_NUM 64
56#define RX_BD_NUM 128
57
58/* ---------------------------------------------------------------------
59 * Low level register access functions
60 */
61
62u32 temac_ior(struct temac_local *lp, int offset)
63{
64 return in_be32((u32 *)(lp->regs + offset));
65}
66
67void temac_iow(struct temac_local *lp, int offset, u32 value)
68{
69 out_be32((u32 *) (lp->regs + offset), value);
70}
71
72int temac_indirect_busywait(struct temac_local *lp)
73{
74 long end = jiffies + 2;
75
76 while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) {
77 if (end - jiffies <= 0) {
78 WARN_ON(1);
79 return -ETIMEDOUT;
80 }
81 msleep(1);
82 }
83 return 0;
84}
85
86/**
87 * temac_indirect_in32
88 *
89 * lp->indirect_mutex must be held when calling this function
90 */
91u32 temac_indirect_in32(struct temac_local *lp, int reg)
92{
93 u32 val;
94
95 if (temac_indirect_busywait(lp))
96 return -ETIMEDOUT;
97 temac_iow(lp, XTE_CTL0_OFFSET, reg);
98 if (temac_indirect_busywait(lp))
99 return -ETIMEDOUT;
100 val = temac_ior(lp, XTE_LSW0_OFFSET);
101
102 return val;
103}
104
105/**
106 * temac_indirect_out32
107 *
108 * lp->indirect_mutex must be held when calling this function
109 */
110void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
111{
112 if (temac_indirect_busywait(lp))
113 return;
114 temac_iow(lp, XTE_LSW0_OFFSET, value);
115 temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
116}
117
John Linne44171f2010-04-08 07:08:02 +0000118/**
119 * temac_dma_in32 - Memory mapped DMA read, this function expects a
120 * register input that is based on DCR word addresses which
121 * are then converted to memory mapped byte addresses
122 */
Grant Likely92744982009-04-25 12:53:39 +0000123static u32 temac_dma_in32(struct temac_local *lp, int reg)
124{
John Linne44171f2010-04-08 07:08:02 +0000125 return in_be32((u32 *)(lp->sdma_regs + (reg << 2)));
126}
127
128/**
129 * temac_dma_out32 - Memory mapped DMA read, this function expects a
130 * register input that is based on DCR word addresses which
131 * are then converted to memory mapped byte addresses
132 */
133static void temac_dma_out32(struct temac_local *lp, int reg, u32 value)
134{
135 out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value);
136}
137
138/* DMA register access functions can be DCR based or memory mapped.
139 * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
140 * memory mapped.
141 */
142#ifdef CONFIG_PPC_DCR
143
144/**
145 * temac_dma_dcr_in32 - DCR based DMA read
146 */
147static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
148{
Grant Likely92744982009-04-25 12:53:39 +0000149 return dcr_read(lp->sdma_dcrs, reg);
150}
151
John Linne44171f2010-04-08 07:08:02 +0000152/**
153 * temac_dma_dcr_out32 - DCR based DMA write
154 */
155static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
Grant Likely92744982009-04-25 12:53:39 +0000156{
157 dcr_write(lp->sdma_dcrs, reg, value);
158}
159
160/**
John Linne44171f2010-04-08 07:08:02 +0000161 * temac_dcr_setup - If the DMA is DCR based, then setup the address and
162 * I/O functions
163 */
Grant Likely2dc11582010-08-06 09:25:50 -0600164static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
John Linne44171f2010-04-08 07:08:02 +0000165 struct device_node *np)
166{
167 unsigned int dcrs;
168
169 /* setup the dcr address mapping if it's in the device tree */
170
171 dcrs = dcr_resource_start(np, 0);
172 if (dcrs != 0) {
173 lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
174 lp->dma_in = temac_dma_dcr_in;
175 lp->dma_out = temac_dma_dcr_out;
176 dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
177 return 0;
178 }
179 /* no DCR in the device tree, indicate a failure */
180 return -1;
181}
182
183#else
184
185/*
186 * temac_dcr_setup - This is a stub for when DCR is not supported,
187 * such as with MicroBlaze
188 */
Grant Likely2dc11582010-08-06 09:25:50 -0600189static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
John Linne44171f2010-04-08 07:08:02 +0000190 struct device_node *np)
191{
192 return -1;
193}
194
195#endif
196
197/**
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000198 * * temac_dma_bd_release - Release buffer descriptor rings
199 */
200static void temac_dma_bd_release(struct net_device *ndev)
201{
202 struct temac_local *lp = netdev_priv(ndev);
203 int i;
204
205 for (i = 0; i < RX_BD_NUM; i++) {
206 if (!lp->rx_skb[i])
207 break;
208 else {
209 dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
210 XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
211 dev_kfree_skb(lp->rx_skb[i]);
212 }
213 }
214 if (lp->rx_bd_v)
215 dma_free_coherent(ndev->dev.parent,
216 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
217 lp->rx_bd_v, lp->rx_bd_p);
218 if (lp->tx_bd_v)
219 dma_free_coherent(ndev->dev.parent,
220 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
221 lp->tx_bd_v, lp->tx_bd_p);
222 if (lp->rx_skb)
223 kfree(lp->rx_skb);
224}
225
226/**
Grant Likely92744982009-04-25 12:53:39 +0000227 * temac_dma_bd_init - Setup buffer descriptor rings
228 */
229static int temac_dma_bd_init(struct net_device *ndev)
230{
231 struct temac_local *lp = netdev_priv(ndev);
232 struct sk_buff *skb;
233 int i;
234
Julia Lawall5d66fe92009-12-29 09:15:42 +0000235 lp->rx_skb = kzalloc(sizeof(*lp->rx_skb) * RX_BD_NUM, GFP_KERNEL);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000236 if (!lp->rx_skb) {
237 dev_err(&ndev->dev,
238 "can't allocate memory for DMA RX buffer\n");
239 goto out;
240 }
Grant Likely92744982009-04-25 12:53:39 +0000241 /* allocate the tx and rx ring buffer descriptors. */
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400242 /* returns a virtual address and a physical address. */
Grant Likely92744982009-04-25 12:53:39 +0000243 lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
244 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
245 &lp->tx_bd_p, GFP_KERNEL);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000246 if (!lp->tx_bd_v) {
247 dev_err(&ndev->dev,
248 "unable to allocate DMA TX buffer descriptors");
249 goto out;
250 }
Grant Likely92744982009-04-25 12:53:39 +0000251 lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
252 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
253 &lp->rx_bd_p, GFP_KERNEL);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000254 if (!lp->rx_bd_v) {
255 dev_err(&ndev->dev,
256 "unable to allocate DMA RX buffer descriptors");
257 goto out;
258 }
Grant Likely92744982009-04-25 12:53:39 +0000259
260 memset(lp->tx_bd_v, 0, sizeof(*lp->tx_bd_v) * TX_BD_NUM);
261 for (i = 0; i < TX_BD_NUM; i++) {
262 lp->tx_bd_v[i].next = lp->tx_bd_p +
263 sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
264 }
265
266 memset(lp->rx_bd_v, 0, sizeof(*lp->rx_bd_v) * RX_BD_NUM);
267 for (i = 0; i < RX_BD_NUM; i++) {
268 lp->rx_bd_v[i].next = lp->rx_bd_p +
269 sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
270
John Linne44171f2010-04-08 07:08:02 +0000271 skb = netdev_alloc_skb_ip_align(ndev,
272 XTE_MAX_JUMBO_FRAME_SIZE);
273
Grant Likely92744982009-04-25 12:53:39 +0000274 if (skb == 0) {
275 dev_err(&ndev->dev, "alloc_skb error %d\n", i);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000276 goto out;
Grant Likely92744982009-04-25 12:53:39 +0000277 }
278 lp->rx_skb[i] = skb;
Grant Likely92744982009-04-25 12:53:39 +0000279 /* returns physical address of skb->data */
280 lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
281 skb->data,
282 XTE_MAX_JUMBO_FRAME_SIZE,
283 DMA_FROM_DEVICE);
284 lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
285 lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
286 }
287
John Linne44171f2010-04-08 07:08:02 +0000288 lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
Grant Likely92744982009-04-25 12:53:39 +0000289 CHNL_CTRL_IRQ_EN |
290 CHNL_CTRL_IRQ_DLY_EN |
291 CHNL_CTRL_IRQ_COAL_EN);
292 /* 0x10220483 */
293 /* 0x00100483 */
Brian Hill23ecc4b2010-05-26 20:44:30 -0700294 lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
Grant Likely92744982009-04-25 12:53:39 +0000295 CHNL_CTRL_IRQ_EN |
296 CHNL_CTRL_IRQ_DLY_EN |
297 CHNL_CTRL_IRQ_COAL_EN |
298 CHNL_CTRL_IRQ_IOE);
299 /* 0xff010283 */
300
John Linne44171f2010-04-08 07:08:02 +0000301 lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
302 lp->dma_out(lp, RX_TAILDESC_PTR,
Grant Likely92744982009-04-25 12:53:39 +0000303 lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
John Linne44171f2010-04-08 07:08:02 +0000304 lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
Grant Likely92744982009-04-25 12:53:39 +0000305
306 return 0;
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000307
308out:
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000309 temac_dma_bd_release(ndev);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000310 return -ENOMEM;
Grant Likely92744982009-04-25 12:53:39 +0000311}
312
313/* ---------------------------------------------------------------------
314 * net_device_ops
315 */
316
317static int temac_set_mac_address(struct net_device *ndev, void *address)
318{
319 struct temac_local *lp = netdev_priv(ndev);
320
321 if (address)
322 memcpy(ndev->dev_addr, address, ETH_ALEN);
323
324 if (!is_valid_ether_addr(ndev->dev_addr))
325 random_ether_addr(ndev->dev_addr);
326
327 /* set up unicast MAC address filter set its mac address */
328 mutex_lock(&lp->indirect_mutex);
329 temac_indirect_out32(lp, XTE_UAW0_OFFSET,
330 (ndev->dev_addr[0]) |
331 (ndev->dev_addr[1] << 8) |
332 (ndev->dev_addr[2] << 16) |
333 (ndev->dev_addr[3] << 24));
334 /* There are reserved bits in EUAW1
335 * so don't affect them Set MAC bits [47:32] in EUAW1 */
336 temac_indirect_out32(lp, XTE_UAW1_OFFSET,
337 (ndev->dev_addr[4] & 0x000000ff) |
338 (ndev->dev_addr[5] << 8));
339 mutex_unlock(&lp->indirect_mutex);
340
341 return 0;
342}
343
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000344static int netdev_set_mac_address(struct net_device *ndev, void *p)
345{
346 struct sockaddr *addr = p;
347
348 return temac_set_mac_address(ndev, addr->sa_data);
349}
350
Grant Likely92744982009-04-25 12:53:39 +0000351static void temac_set_multicast_list(struct net_device *ndev)
352{
353 struct temac_local *lp = netdev_priv(ndev);
354 u32 multi_addr_msw, multi_addr_lsw, val;
355 int i;
356
357 mutex_lock(&lp->indirect_mutex);
Joe Perches8e95a202009-12-03 07:58:21 +0000358 if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000359 netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) {
Grant Likely92744982009-04-25 12:53:39 +0000360 /*
361 * We must make the kernel realise we had to move
362 * into promisc mode or we start all out war on
363 * the cable. If it was a promisc request the
364 * flag is already set. If not we assert it.
365 */
366 ndev->flags |= IFF_PROMISC;
367 temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
368 dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000369 } else if (!netdev_mc_empty(ndev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +0000370 struct netdev_hw_addr *ha;
Grant Likely92744982009-04-25 12:53:39 +0000371
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000372 i = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000373 netdev_for_each_mc_addr(ha, ndev) {
Grant Likely92744982009-04-25 12:53:39 +0000374 if (i >= MULTICAST_CAM_TABLE_NUM)
375 break;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000376 multi_addr_msw = ((ha->addr[3] << 24) |
377 (ha->addr[2] << 16) |
378 (ha->addr[1] << 8) |
379 (ha->addr[0]));
Grant Likely92744982009-04-25 12:53:39 +0000380 temac_indirect_out32(lp, XTE_MAW0_OFFSET,
381 multi_addr_msw);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000382 multi_addr_lsw = ((ha->addr[5] << 8) |
383 (ha->addr[4]) | (i << 16));
Grant Likely92744982009-04-25 12:53:39 +0000384 temac_indirect_out32(lp, XTE_MAW1_OFFSET,
385 multi_addr_lsw);
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000386 i++;
Grant Likely92744982009-04-25 12:53:39 +0000387 }
388 } else {
389 val = temac_indirect_in32(lp, XTE_AFM_OFFSET);
390 temac_indirect_out32(lp, XTE_AFM_OFFSET,
391 val & ~XTE_AFM_EPPRM_MASK);
392 temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0);
393 temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0);
394 dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
395 }
396 mutex_unlock(&lp->indirect_mutex);
397}
398
399struct temac_option {
400 int flg;
401 u32 opt;
402 u32 reg;
403 u32 m_or;
404 u32 m_and;
405} temac_options[] = {
406 /* Turn on jumbo packet support for both Rx and Tx */
407 {
408 .opt = XTE_OPTION_JUMBO,
409 .reg = XTE_TXC_OFFSET,
410 .m_or = XTE_TXC_TXJMBO_MASK,
411 },
412 {
413 .opt = XTE_OPTION_JUMBO,
414 .reg = XTE_RXC1_OFFSET,
415 .m_or =XTE_RXC1_RXJMBO_MASK,
416 },
417 /* Turn on VLAN packet support for both Rx and Tx */
418 {
419 .opt = XTE_OPTION_VLAN,
420 .reg = XTE_TXC_OFFSET,
421 .m_or =XTE_TXC_TXVLAN_MASK,
422 },
423 {
424 .opt = XTE_OPTION_VLAN,
425 .reg = XTE_RXC1_OFFSET,
426 .m_or =XTE_RXC1_RXVLAN_MASK,
427 },
428 /* Turn on FCS stripping on receive packets */
429 {
430 .opt = XTE_OPTION_FCS_STRIP,
431 .reg = XTE_RXC1_OFFSET,
432 .m_or =XTE_RXC1_RXFCS_MASK,
433 },
434 /* Turn on FCS insertion on transmit packets */
435 {
436 .opt = XTE_OPTION_FCS_INSERT,
437 .reg = XTE_TXC_OFFSET,
438 .m_or =XTE_TXC_TXFCS_MASK,
439 },
440 /* Turn on length/type field checking on receive packets */
441 {
442 .opt = XTE_OPTION_LENTYPE_ERR,
443 .reg = XTE_RXC1_OFFSET,
444 .m_or =XTE_RXC1_RXLT_MASK,
445 },
446 /* Turn on flow control */
447 {
448 .opt = XTE_OPTION_FLOW_CONTROL,
449 .reg = XTE_FCC_OFFSET,
450 .m_or =XTE_FCC_RXFLO_MASK,
451 },
452 /* Turn on flow control */
453 {
454 .opt = XTE_OPTION_FLOW_CONTROL,
455 .reg = XTE_FCC_OFFSET,
456 .m_or =XTE_FCC_TXFLO_MASK,
457 },
458 /* Turn on promiscuous frame filtering (all frames are received ) */
459 {
460 .opt = XTE_OPTION_PROMISC,
461 .reg = XTE_AFM_OFFSET,
462 .m_or =XTE_AFM_EPPRM_MASK,
463 },
464 /* Enable transmitter if not already enabled */
465 {
466 .opt = XTE_OPTION_TXEN,
467 .reg = XTE_TXC_OFFSET,
468 .m_or =XTE_TXC_TXEN_MASK,
469 },
470 /* Enable receiver? */
471 {
472 .opt = XTE_OPTION_RXEN,
473 .reg = XTE_RXC1_OFFSET,
474 .m_or =XTE_RXC1_RXEN_MASK,
475 },
476 {}
477};
478
479/**
480 * temac_setoptions
481 */
482static u32 temac_setoptions(struct net_device *ndev, u32 options)
483{
484 struct temac_local *lp = netdev_priv(ndev);
485 struct temac_option *tp = &temac_options[0];
486 int reg;
487
488 mutex_lock(&lp->indirect_mutex);
489 while (tp->opt) {
490 reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or;
491 if (options & tp->opt)
492 reg |= tp->m_or;
493 temac_indirect_out32(lp, tp->reg, reg);
494 tp++;
495 }
496 lp->options |= options;
497 mutex_unlock(&lp->indirect_mutex);
498
Eric Dumazet807540b2010-09-23 05:40:09 +0000499 return 0;
Grant Likely92744982009-04-25 12:53:39 +0000500}
501
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200502/* Initialize temac */
Grant Likely92744982009-04-25 12:53:39 +0000503static void temac_device_reset(struct net_device *ndev)
504{
505 struct temac_local *lp = netdev_priv(ndev);
506 u32 timeout;
507 u32 val;
508
509 /* Perform a software reset */
510
511 /* 0x300 host enable bit ? */
512 /* reset PHY through control register ?:1 */
513
514 dev_dbg(&ndev->dev, "%s()\n", __func__);
515
516 mutex_lock(&lp->indirect_mutex);
517 /* Reset the receiver and wait for it to finish reset */
518 temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
519 timeout = 1000;
520 while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
521 udelay(1);
522 if (--timeout == 0) {
523 dev_err(&ndev->dev,
524 "temac_device_reset RX reset timeout!!\n");
525 break;
526 }
527 }
528
529 /* Reset the transmitter and wait for it to finish reset */
530 temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
531 timeout = 1000;
532 while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
533 udelay(1);
534 if (--timeout == 0) {
535 dev_err(&ndev->dev,
536 "temac_device_reset TX reset timeout!!\n");
537 break;
538 }
539 }
540
541 /* Disable the receiver */
542 val = temac_indirect_in32(lp, XTE_RXC1_OFFSET);
543 temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
544
545 /* Reset Local Link (DMA) */
John Linne44171f2010-04-08 07:08:02 +0000546 lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
Grant Likely92744982009-04-25 12:53:39 +0000547 timeout = 1000;
John Linne44171f2010-04-08 07:08:02 +0000548 while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
Grant Likely92744982009-04-25 12:53:39 +0000549 udelay(1);
550 if (--timeout == 0) {
551 dev_err(&ndev->dev,
552 "temac_device_reset DMA reset timeout!!\n");
553 break;
554 }
555 }
John Linne44171f2010-04-08 07:08:02 +0000556 lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
Grant Likely92744982009-04-25 12:53:39 +0000557
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000558 if (temac_dma_bd_init(ndev)) {
559 dev_err(&ndev->dev,
560 "temac_device_reset descriptor allocation failed\n");
561 }
Grant Likely92744982009-04-25 12:53:39 +0000562
563 temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0);
564 temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0);
565 temac_indirect_out32(lp, XTE_TXC_OFFSET, 0);
566 temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
567
568 mutex_unlock(&lp->indirect_mutex);
569
570 /* Sync default options with HW
571 * but leave receiver and transmitter disabled. */
572 temac_setoptions(ndev,
573 lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
574
575 temac_set_mac_address(ndev, NULL);
576
577 /* Set address filter table */
578 temac_set_multicast_list(ndev);
579 if (temac_setoptions(ndev, lp->options))
580 dev_err(&ndev->dev, "Error setting TEMAC options\n");
581
582 /* Init Driver variable */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700583 ndev->trans_start = jiffies; /* prevent tx timeout */
Grant Likely92744982009-04-25 12:53:39 +0000584}
585
586void temac_adjust_link(struct net_device *ndev)
587{
588 struct temac_local *lp = netdev_priv(ndev);
589 struct phy_device *phy = lp->phy_dev;
590 u32 mii_speed;
591 int link_state;
592
593 /* hash together the state values to decide if something has changed */
594 link_state = phy->speed | (phy->duplex << 1) | phy->link;
595
596 mutex_lock(&lp->indirect_mutex);
597 if (lp->last_link != link_state) {
598 mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET);
599 mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
600
601 switch (phy->speed) {
602 case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
603 case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
604 case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
605 }
606
607 /* Write new speed setting out to TEMAC */
608 temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed);
609 lp->last_link = link_state;
610 phy_print_status(phy);
611 }
612 mutex_unlock(&lp->indirect_mutex);
613}
614
615static void temac_start_xmit_done(struct net_device *ndev)
616{
617 struct temac_local *lp = netdev_priv(ndev);
618 struct cdmac_bd *cur_p;
619 unsigned int stat = 0;
620
621 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
622 stat = cur_p->app0;
623
624 while (stat & STS_CTRL_APP0_CMPLT) {
625 dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len,
626 DMA_TO_DEVICE);
627 if (cur_p->app4)
628 dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
629 cur_p->app0 = 0;
Brian Hill23ecc4b2010-05-26 20:44:30 -0700630 cur_p->app1 = 0;
631 cur_p->app2 = 0;
632 cur_p->app3 = 0;
633 cur_p->app4 = 0;
Grant Likely92744982009-04-25 12:53:39 +0000634
635 ndev->stats.tx_packets++;
636 ndev->stats.tx_bytes += cur_p->len;
637
638 lp->tx_bd_ci++;
639 if (lp->tx_bd_ci >= TX_BD_NUM)
640 lp->tx_bd_ci = 0;
641
642 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
643 stat = cur_p->app0;
644 }
645
646 netif_wake_queue(ndev);
647}
648
Brian Hill23ecc4b2010-05-26 20:44:30 -0700649static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
650{
651 struct cdmac_bd *cur_p;
652 int tail;
653
654 tail = lp->tx_bd_tail;
655 cur_p = &lp->tx_bd_v[tail];
656
657 do {
658 if (cur_p->app0)
659 return NETDEV_TX_BUSY;
660
661 tail++;
662 if (tail >= TX_BD_NUM)
663 tail = 0;
664
665 cur_p = &lp->tx_bd_v[tail];
666 num_frag--;
667 } while (num_frag >= 0);
668
669 return 0;
670}
671
Grant Likely92744982009-04-25 12:53:39 +0000672static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
673{
674 struct temac_local *lp = netdev_priv(ndev);
675 struct cdmac_bd *cur_p;
676 dma_addr_t start_p, tail_p;
677 int ii;
678 unsigned long num_frag;
679 skb_frag_t *frag;
680
681 num_frag = skb_shinfo(skb)->nr_frags;
682 frag = &skb_shinfo(skb)->frags[0];
683 start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
684 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
685
Brian Hill23ecc4b2010-05-26 20:44:30 -0700686 if (temac_check_tx_bd_space(lp, num_frag)) {
Grant Likely92744982009-04-25 12:53:39 +0000687 if (!netif_queue_stopped(ndev)) {
688 netif_stop_queue(ndev);
689 return NETDEV_TX_BUSY;
690 }
691 return NETDEV_TX_BUSY;
692 }
693
694 cur_p->app0 = 0;
695 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000696 unsigned int csum_start_off = skb_checksum_start_offset(skb);
Brian Hill23ecc4b2010-05-26 20:44:30 -0700697 unsigned int csum_index_off = csum_start_off + skb->csum_offset;
Grant Likely92744982009-04-25 12:53:39 +0000698
Brian Hill23ecc4b2010-05-26 20:44:30 -0700699 cur_p->app0 |= 1; /* TX Checksum Enabled */
700 cur_p->app1 = (csum_start_off << 16) | csum_index_off;
701 cur_p->app2 = 0; /* initial checksum seed */
Grant Likely92744982009-04-25 12:53:39 +0000702 }
Brian Hill23ecc4b2010-05-26 20:44:30 -0700703
Grant Likely92744982009-04-25 12:53:39 +0000704 cur_p->app0 |= STS_CTRL_APP0_SOP;
705 cur_p->len = skb_headlen(skb);
706 cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len,
707 DMA_TO_DEVICE);
708 cur_p->app4 = (unsigned long)skb;
709
710 for (ii = 0; ii < num_frag; ii++) {
711 lp->tx_bd_tail++;
712 if (lp->tx_bd_tail >= TX_BD_NUM)
713 lp->tx_bd_tail = 0;
714
715 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
716 cur_p->phys = dma_map_single(ndev->dev.parent,
717 (void *)page_address(frag->page) +
718 frag->page_offset,
719 frag->size, DMA_TO_DEVICE);
720 cur_p->len = frag->size;
721 cur_p->app0 = 0;
722 frag++;
723 }
724 cur_p->app0 |= STS_CTRL_APP0_EOP;
725
726 tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
727 lp->tx_bd_tail++;
728 if (lp->tx_bd_tail >= TX_BD_NUM)
729 lp->tx_bd_tail = 0;
730
Richard Cochran93e0ed12011-06-19 21:51:26 +0000731 skb_tx_timestamp(skb);
732
Grant Likely92744982009-04-25 12:53:39 +0000733 /* Kick off the transfer */
John Linne44171f2010-04-08 07:08:02 +0000734 lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
Grant Likely92744982009-04-25 12:53:39 +0000735
Patrick McHardy6ed10652009-06-23 06:03:08 +0000736 return NETDEV_TX_OK;
Grant Likely92744982009-04-25 12:53:39 +0000737}
738
739
740static void ll_temac_recv(struct net_device *ndev)
741{
742 struct temac_local *lp = netdev_priv(ndev);
743 struct sk_buff *skb, *new_skb;
744 unsigned int bdstat;
745 struct cdmac_bd *cur_p;
746 dma_addr_t tail_p;
747 int length;
Grant Likely92744982009-04-25 12:53:39 +0000748 unsigned long flags;
749
750 spin_lock_irqsave(&lp->rx_lock, flags);
751
752 tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
753 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
754
755 bdstat = cur_p->app0;
756 while ((bdstat & STS_CTRL_APP0_CMPLT)) {
757
758 skb = lp->rx_skb[lp->rx_bd_ci];
Steven J. Magnanic3b7c122010-02-17 07:14:20 +0000759 length = cur_p->app4 & 0x3FFF;
Grant Likely92744982009-04-25 12:53:39 +0000760
John Linn33646d72010-04-08 07:08:01 +0000761 dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
Grant Likely92744982009-04-25 12:53:39 +0000762 DMA_FROM_DEVICE);
763
764 skb_put(skb, length);
765 skb->dev = ndev;
766 skb->protocol = eth_type_trans(skb, ndev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700767 skb_checksum_none_assert(skb);
Grant Likely92744982009-04-25 12:53:39 +0000768
Brian Hill23ecc4b2010-05-26 20:44:30 -0700769 /* if we're doing rx csum offload, set it up */
770 if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
771 (skb->protocol == __constant_htons(ETH_P_IP)) &&
772 (skb->len > 64)) {
773
774 skb->csum = cur_p->app3 & 0xFFFF;
775 skb->ip_summed = CHECKSUM_COMPLETE;
776 }
777
Richard Cochran93e0ed12011-06-19 21:51:26 +0000778 if (!skb_defer_rx_timestamp(skb))
779 netif_rx(skb);
Grant Likely92744982009-04-25 12:53:39 +0000780
781 ndev->stats.rx_packets++;
782 ndev->stats.rx_bytes += length;
783
John Linne44171f2010-04-08 07:08:02 +0000784 new_skb = netdev_alloc_skb_ip_align(ndev,
785 XTE_MAX_JUMBO_FRAME_SIZE);
786
Grant Likely92744982009-04-25 12:53:39 +0000787 if (new_skb == 0) {
788 dev_err(&ndev->dev, "no memory for new sk_buff\n");
789 spin_unlock_irqrestore(&lp->rx_lock, flags);
790 return;
791 }
792
Grant Likely92744982009-04-25 12:53:39 +0000793 cur_p->app0 = STS_CTRL_APP0_IRQONEND;
794 cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
795 XTE_MAX_JUMBO_FRAME_SIZE,
796 DMA_FROM_DEVICE);
797 cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
798 lp->rx_skb[lp->rx_bd_ci] = new_skb;
799
800 lp->rx_bd_ci++;
801 if (lp->rx_bd_ci >= RX_BD_NUM)
802 lp->rx_bd_ci = 0;
803
804 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
805 bdstat = cur_p->app0;
806 }
John Linne44171f2010-04-08 07:08:02 +0000807 lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
Grant Likely92744982009-04-25 12:53:39 +0000808
809 spin_unlock_irqrestore(&lp->rx_lock, flags);
810}
811
812static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
813{
814 struct net_device *ndev = _ndev;
815 struct temac_local *lp = netdev_priv(ndev);
816 unsigned int status;
817
John Linne44171f2010-04-08 07:08:02 +0000818 status = lp->dma_in(lp, TX_IRQ_REG);
819 lp->dma_out(lp, TX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000820
821 if (status & (IRQ_COAL | IRQ_DLY))
822 temac_start_xmit_done(lp->ndev);
823 if (status & 0x080)
824 dev_err(&ndev->dev, "DMA error 0x%x\n", status);
825
826 return IRQ_HANDLED;
827}
828
829static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
830{
831 struct net_device *ndev = _ndev;
832 struct temac_local *lp = netdev_priv(ndev);
833 unsigned int status;
834
835 /* Read and clear the status registers */
John Linne44171f2010-04-08 07:08:02 +0000836 status = lp->dma_in(lp, RX_IRQ_REG);
837 lp->dma_out(lp, RX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000838
839 if (status & (IRQ_COAL | IRQ_DLY))
840 ll_temac_recv(lp->ndev);
841
842 return IRQ_HANDLED;
843}
844
845static int temac_open(struct net_device *ndev)
846{
847 struct temac_local *lp = netdev_priv(ndev);
848 int rc;
849
850 dev_dbg(&ndev->dev, "temac_open()\n");
851
852 if (lp->phy_node) {
853 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
854 temac_adjust_link, 0, 0);
855 if (!lp->phy_dev) {
856 dev_err(lp->dev, "of_phy_connect() failed\n");
857 return -ENODEV;
858 }
859
860 phy_start(lp->phy_dev);
861 }
862
863 rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
864 if (rc)
865 goto err_tx_irq;
866 rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
867 if (rc)
868 goto err_rx_irq;
869
870 temac_device_reset(ndev);
871 return 0;
872
873 err_rx_irq:
874 free_irq(lp->tx_irq, ndev);
875 err_tx_irq:
876 if (lp->phy_dev)
877 phy_disconnect(lp->phy_dev);
878 lp->phy_dev = NULL;
879 dev_err(lp->dev, "request_irq() failed\n");
880 return rc;
881}
882
883static int temac_stop(struct net_device *ndev)
884{
885 struct temac_local *lp = netdev_priv(ndev);
886
887 dev_dbg(&ndev->dev, "temac_close()\n");
888
889 free_irq(lp->tx_irq, ndev);
890 free_irq(lp->rx_irq, ndev);
891
892 if (lp->phy_dev)
893 phy_disconnect(lp->phy_dev);
894 lp->phy_dev = NULL;
895
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000896 temac_dma_bd_release(ndev);
897
Grant Likely92744982009-04-25 12:53:39 +0000898 return 0;
899}
900
901#ifdef CONFIG_NET_POLL_CONTROLLER
902static void
903temac_poll_controller(struct net_device *ndev)
904{
905 struct temac_local *lp = netdev_priv(ndev);
906
907 disable_irq(lp->tx_irq);
908 disable_irq(lp->rx_irq);
909
Michal Simek85399922010-08-18 00:26:34 +0000910 ll_temac_rx_irq(lp->tx_irq, ndev);
911 ll_temac_tx_irq(lp->rx_irq, ndev);
Grant Likely92744982009-04-25 12:53:39 +0000912
913 enable_irq(lp->tx_irq);
914 enable_irq(lp->rx_irq);
915}
916#endif
917
918static const struct net_device_ops temac_netdev_ops = {
919 .ndo_open = temac_open,
920 .ndo_stop = temac_stop,
921 .ndo_start_xmit = temac_start_xmit,
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000922 .ndo_set_mac_address = netdev_set_mac_address,
Denis Kirjanov60eb5fd2010-07-10 11:10:44 +0000923 .ndo_validate_addr = eth_validate_addr,
Grant Likely92744982009-04-25 12:53:39 +0000924 //.ndo_set_multicast_list = temac_set_multicast_list,
925#ifdef CONFIG_NET_POLL_CONTROLLER
926 .ndo_poll_controller = temac_poll_controller,
927#endif
928};
929
930/* ---------------------------------------------------------------------
931 * SYSFS device attributes
932 */
933static ssize_t temac_show_llink_regs(struct device *dev,
934 struct device_attribute *attr, char *buf)
935{
936 struct net_device *ndev = dev_get_drvdata(dev);
937 struct temac_local *lp = netdev_priv(ndev);
938 int i, len = 0;
939
940 for (i = 0; i < 0x11; i++)
John Linne44171f2010-04-08 07:08:02 +0000941 len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
Grant Likely92744982009-04-25 12:53:39 +0000942 (i % 8) == 7 ? "\n" : " ");
943 len += sprintf(buf + len, "\n");
944
945 return len;
946}
947
948static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
949
950static struct attribute *temac_device_attrs[] = {
951 &dev_attr_llink_regs.attr,
952 NULL,
953};
954
955static const struct attribute_group temac_attr_group = {
956 .attrs = temac_device_attrs,
957};
958
Grant Likely74888762011-02-22 21:05:51 -0700959static int __devinit temac_of_probe(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +0000960{
961 struct device_node *np;
962 struct temac_local *lp;
963 struct net_device *ndev;
964 const void *addr;
Brian Hill23ecc4b2010-05-26 20:44:30 -0700965 __be32 *p;
Grant Likely92744982009-04-25 12:53:39 +0000966 int size, rc = 0;
Grant Likely92744982009-04-25 12:53:39 +0000967
968 /* Init network device structure */
969 ndev = alloc_etherdev(sizeof(*lp));
970 if (!ndev) {
971 dev_err(&op->dev, "could not allocate device.\n");
972 return -ENOMEM;
973 }
974 ether_setup(ndev);
975 dev_set_drvdata(&op->dev, ndev);
976 SET_NETDEV_DEV(ndev, &op->dev);
977 ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
978 ndev->features = NETIF_F_SG | NETIF_F_FRAGLIST;
979 ndev->netdev_ops = &temac_netdev_ops;
980#if 0
981 ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
982 ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
983 ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
984 ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
985 ndev->features |= NETIF_F_HW_VLAN_TX; /* Transmit VLAN hw accel */
986 ndev->features |= NETIF_F_HW_VLAN_RX; /* Receive VLAN hw acceleration */
987 ndev->features |= NETIF_F_HW_VLAN_FILTER; /* Receive VLAN filtering */
988 ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
989 ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
990 ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
991 ndev->features |= NETIF_F_LRO; /* large receive offload */
992#endif
993
994 /* setup temac private info structure */
995 lp = netdev_priv(ndev);
996 lp->ndev = ndev;
997 lp->dev = &op->dev;
998 lp->options = XTE_OPTION_DEFAULTS;
999 spin_lock_init(&lp->rx_lock);
1000 mutex_init(&lp->indirect_mutex);
1001
1002 /* map device registers */
Grant Likely61c7a082010-04-13 16:12:29 -07001003 lp->regs = of_iomap(op->dev.of_node, 0);
Grant Likely92744982009-04-25 12:53:39 +00001004 if (!lp->regs) {
1005 dev_err(&op->dev, "could not map temac regs.\n");
1006 goto nodev;
1007 }
1008
Brian Hill23ecc4b2010-05-26 20:44:30 -07001009 /* Setup checksum offload, but default to off if not specified */
1010 lp->temac_features = 0;
1011 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
1012 if (p && be32_to_cpu(*p)) {
1013 lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
1014 /* Can checksum TCP/UDP over IPv4. */
1015 ndev->features |= NETIF_F_IP_CSUM;
1016 }
1017 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
1018 if (p && be32_to_cpu(*p))
1019 lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
1020
Grant Likely92744982009-04-25 12:53:39 +00001021 /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
Grant Likely61c7a082010-04-13 16:12:29 -07001022 np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
Grant Likely92744982009-04-25 12:53:39 +00001023 if (!np) {
1024 dev_err(&op->dev, "could not find DMA node\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001025 goto err_iounmap;
Grant Likely92744982009-04-25 12:53:39 +00001026 }
1027
John Linne44171f2010-04-08 07:08:02 +00001028 /* Setup the DMA register accesses, could be DCR or memory mapped */
1029 if (temac_dcr_setup(lp, op, np)) {
1030
1031 /* no DCR in the device tree, try non-DCR */
1032 lp->sdma_regs = of_iomap(np, 0);
1033 if (lp->sdma_regs) {
1034 lp->dma_in = temac_dma_in32;
1035 lp->dma_out = temac_dma_out32;
1036 dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
1037 } else {
1038 dev_err(&op->dev, "unable to map DMA registers\n");
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001039 of_node_put(np);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001040 goto err_iounmap;
John Linne44171f2010-04-08 07:08:02 +00001041 }
Grant Likely92744982009-04-25 12:53:39 +00001042 }
Grant Likely92744982009-04-25 12:53:39 +00001043
1044 lp->rx_irq = irq_of_parse_and_map(np, 0);
1045 lp->tx_irq = irq_of_parse_and_map(np, 1);
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001046
1047 of_node_put(np); /* Finished with the DMA node; drop the reference */
1048
Brian Hill755fae02010-05-26 20:42:18 -07001049 if ((lp->rx_irq == NO_IRQ) || (lp->tx_irq == NO_IRQ)) {
Grant Likely92744982009-04-25 12:53:39 +00001050 dev_err(&op->dev, "could not determine irqs\n");
1051 rc = -ENOMEM;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001052 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001053 }
1054
Grant Likely92744982009-04-25 12:53:39 +00001055
1056 /* Retrieve the MAC address */
Grant Likely61c7a082010-04-13 16:12:29 -07001057 addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
Grant Likely92744982009-04-25 12:53:39 +00001058 if ((!addr) || (size != 6)) {
1059 dev_err(&op->dev, "could not find MAC address\n");
1060 rc = -ENODEV;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001061 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001062 }
1063 temac_set_mac_address(ndev, (void *)addr);
1064
Grant Likely61c7a082010-04-13 16:12:29 -07001065 rc = temac_mdio_setup(lp, op->dev.of_node);
Grant Likely92744982009-04-25 12:53:39 +00001066 if (rc)
1067 dev_warn(&op->dev, "error registering MDIO bus\n");
1068
Grant Likely61c7a082010-04-13 16:12:29 -07001069 lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
Grant Likely92744982009-04-25 12:53:39 +00001070 if (lp->phy_node)
1071 dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
1072
1073 /* Add the device attributes */
1074 rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
1075 if (rc) {
1076 dev_err(lp->dev, "Error creating sysfs files\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001077 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001078 }
1079
1080 rc = register_netdev(lp->ndev);
1081 if (rc) {
1082 dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
1083 goto err_register_ndev;
1084 }
1085
1086 return 0;
1087
1088 err_register_ndev:
1089 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001090 err_iounmap_2:
1091 if (lp->sdma_regs)
1092 iounmap(lp->sdma_regs);
1093 err_iounmap:
1094 iounmap(lp->regs);
Grant Likely92744982009-04-25 12:53:39 +00001095 nodev:
1096 free_netdev(ndev);
1097 ndev = NULL;
1098 return rc;
1099}
1100
Grant Likely2dc11582010-08-06 09:25:50 -06001101static int __devexit temac_of_remove(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +00001102{
1103 struct net_device *ndev = dev_get_drvdata(&op->dev);
1104 struct temac_local *lp = netdev_priv(ndev);
1105
1106 temac_mdio_teardown(lp);
1107 unregister_netdev(ndev);
1108 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1109 if (lp->phy_node)
1110 of_node_put(lp->phy_node);
1111 lp->phy_node = NULL;
1112 dev_set_drvdata(&op->dev, NULL);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001113 iounmap(lp->regs);
1114 if (lp->sdma_regs)
1115 iounmap(lp->sdma_regs);
Grant Likely92744982009-04-25 12:53:39 +00001116 free_netdev(ndev);
1117 return 0;
1118}
1119
1120static struct of_device_id temac_of_match[] __devinitdata = {
1121 { .compatible = "xlnx,xps-ll-temac-1.01.b", },
Steven J. Magnanic3b7c122010-02-17 07:14:20 +00001122 { .compatible = "xlnx,xps-ll-temac-2.00.a", },
1123 { .compatible = "xlnx,xps-ll-temac-2.02.a", },
1124 { .compatible = "xlnx,xps-ll-temac-2.03.a", },
Grant Likely92744982009-04-25 12:53:39 +00001125 {},
1126};
1127MODULE_DEVICE_TABLE(of, temac_of_match);
1128
Grant Likely74888762011-02-22 21:05:51 -07001129static struct platform_driver temac_of_driver = {
Grant Likely92744982009-04-25 12:53:39 +00001130 .probe = temac_of_probe,
1131 .remove = __devexit_p(temac_of_remove),
1132 .driver = {
1133 .owner = THIS_MODULE,
1134 .name = "xilinx_temac",
Grant Likely40182942010-04-13 16:13:02 -07001135 .of_match_table = temac_of_match,
Grant Likely92744982009-04-25 12:53:39 +00001136 },
1137};
1138
1139static int __init temac_init(void)
1140{
Grant Likely74888762011-02-22 21:05:51 -07001141 return platform_driver_register(&temac_of_driver);
Grant Likely92744982009-04-25 12:53:39 +00001142}
1143module_init(temac_init);
1144
1145static void __exit temac_exit(void)
1146{
Grant Likely74888762011-02-22 21:05:51 -07001147 platform_driver_unregister(&temac_of_driver);
Grant Likely92744982009-04-25 12:53:39 +00001148}
1149module_exit(temac_exit);
1150
1151MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1152MODULE_AUTHOR("Yoshio Kashiwagi");
1153MODULE_LICENSE("GPL");