blob: 22478d54f5bb7f362642886e2fed62289ebdb339 [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>
Rob Herring5c9f3032013-09-07 14:05:10 -050039#include <linux/of_irq.h>
Grant Likely92744982009-04-25 12:53:39 +000040#include <linux/of_mdio.h>
41#include <linux/of_platform.h>
Michal Simek9f1a1fc2010-09-01 08:55:23 -060042#include <linux/of_address.h>
Grant Likely92744982009-04-25 12:53:39 +000043#include <linux/skbuff.h>
44#include <linux/spinlock.h>
45#include <linux/tcp.h> /* needed for sizeof(tcphdr) */
46#include <linux/udp.h> /* needed for sizeof(udphdr) */
47#include <linux/phy.h>
48#include <linux/in.h>
49#include <linux/io.h>
50#include <linux/ip.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Stephen Rothwellffbc03b2011-06-08 15:49:33 +100052#include <linux/interrupt.h>
Stephen Rothwell84cac392011-06-29 02:55:59 -070053#include <linux/dma-mapping.h>
Grant Likely92744982009-04-25 12:53:39 +000054
55#include "ll_temac.h"
56
57#define TX_BD_NUM 64
58#define RX_BD_NUM 128
59
60/* ---------------------------------------------------------------------
61 * Low level register access functions
62 */
63
64u32 temac_ior(struct temac_local *lp, int offset)
65{
66 return in_be32((u32 *)(lp->regs + offset));
67}
68
69void temac_iow(struct temac_local *lp, int offset, u32 value)
70{
71 out_be32((u32 *) (lp->regs + offset), value);
72}
73
74int temac_indirect_busywait(struct temac_local *lp)
75{
76 long end = jiffies + 2;
77
78 while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) {
79 if (end - jiffies <= 0) {
80 WARN_ON(1);
81 return -ETIMEDOUT;
82 }
83 msleep(1);
84 }
85 return 0;
86}
87
88/**
89 * temac_indirect_in32
90 *
91 * lp->indirect_mutex must be held when calling this function
92 */
93u32 temac_indirect_in32(struct temac_local *lp, int reg)
94{
95 u32 val;
96
97 if (temac_indirect_busywait(lp))
98 return -ETIMEDOUT;
99 temac_iow(lp, XTE_CTL0_OFFSET, reg);
100 if (temac_indirect_busywait(lp))
101 return -ETIMEDOUT;
102 val = temac_ior(lp, XTE_LSW0_OFFSET);
103
104 return val;
105}
106
107/**
108 * temac_indirect_out32
109 *
110 * lp->indirect_mutex must be held when calling this function
111 */
112void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
113{
114 if (temac_indirect_busywait(lp))
115 return;
116 temac_iow(lp, XTE_LSW0_OFFSET, value);
117 temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
Ricardo Ribaldaf79d7e62011-11-07 23:39:57 +0000118 temac_indirect_busywait(lp);
Grant Likely92744982009-04-25 12:53:39 +0000119}
120
John Linne44171f2010-04-08 07:08:02 +0000121/**
122 * temac_dma_in32 - Memory mapped DMA read, this function expects a
123 * register input that is based on DCR word addresses which
124 * are then converted to memory mapped byte addresses
125 */
Grant Likely92744982009-04-25 12:53:39 +0000126static u32 temac_dma_in32(struct temac_local *lp, int reg)
127{
John Linne44171f2010-04-08 07:08:02 +0000128 return in_be32((u32 *)(lp->sdma_regs + (reg << 2)));
129}
130
131/**
132 * temac_dma_out32 - Memory mapped DMA read, this function expects a
133 * register input that is based on DCR word addresses which
134 * are then converted to memory mapped byte addresses
135 */
136static void temac_dma_out32(struct temac_local *lp, int reg, u32 value)
137{
138 out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value);
139}
140
141/* DMA register access functions can be DCR based or memory mapped.
142 * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
143 * memory mapped.
144 */
145#ifdef CONFIG_PPC_DCR
146
147/**
148 * temac_dma_dcr_in32 - DCR based DMA read
149 */
150static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
151{
Grant Likely92744982009-04-25 12:53:39 +0000152 return dcr_read(lp->sdma_dcrs, reg);
153}
154
John Linne44171f2010-04-08 07:08:02 +0000155/**
156 * temac_dma_dcr_out32 - DCR based DMA write
157 */
158static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
Grant Likely92744982009-04-25 12:53:39 +0000159{
160 dcr_write(lp->sdma_dcrs, reg, value);
161}
162
163/**
John Linne44171f2010-04-08 07:08:02 +0000164 * temac_dcr_setup - If the DMA is DCR based, then setup the address and
165 * I/O functions
166 */
Grant Likely2dc11582010-08-06 09:25:50 -0600167static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
John Linne44171f2010-04-08 07:08:02 +0000168 struct device_node *np)
169{
170 unsigned int dcrs;
171
172 /* setup the dcr address mapping if it's in the device tree */
173
174 dcrs = dcr_resource_start(np, 0);
175 if (dcrs != 0) {
176 lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
177 lp->dma_in = temac_dma_dcr_in;
178 lp->dma_out = temac_dma_dcr_out;
179 dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
180 return 0;
181 }
182 /* no DCR in the device tree, indicate a failure */
183 return -1;
184}
185
186#else
187
188/*
189 * temac_dcr_setup - This is a stub for when DCR is not supported,
190 * such as with MicroBlaze
191 */
Grant Likely2dc11582010-08-06 09:25:50 -0600192static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
John Linne44171f2010-04-08 07:08:02 +0000193 struct device_node *np)
194{
195 return -1;
196}
197
198#endif
199
200/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000201 * temac_dma_bd_release - Release buffer descriptor rings
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000202 */
203static void temac_dma_bd_release(struct net_device *ndev)
204{
205 struct temac_local *lp = netdev_priv(ndev);
206 int i;
207
Ricardo Ribalda50ec1532011-11-07 23:31:58 +0000208 /* Reset Local Link (DMA) */
209 lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
210
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000211 for (i = 0; i < RX_BD_NUM; i++) {
212 if (!lp->rx_skb[i])
213 break;
214 else {
215 dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
216 XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
217 dev_kfree_skb(lp->rx_skb[i]);
218 }
219 }
220 if (lp->rx_bd_v)
221 dma_free_coherent(ndev->dev.parent,
222 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
223 lp->rx_bd_v, lp->rx_bd_p);
224 if (lp->tx_bd_v)
225 dma_free_coherent(ndev->dev.parent,
226 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
227 lp->tx_bd_v, lp->tx_bd_p);
228 if (lp->rx_skb)
229 kfree(lp->rx_skb);
230}
231
232/**
Grant Likely92744982009-04-25 12:53:39 +0000233 * temac_dma_bd_init - Setup buffer descriptor rings
234 */
235static int temac_dma_bd_init(struct net_device *ndev)
236{
237 struct temac_local *lp = netdev_priv(ndev);
238 struct sk_buff *skb;
239 int i;
240
Thomas Meyerddf98e62011-12-02 12:35:43 +0000241 lp->rx_skb = kcalloc(RX_BD_NUM, sizeof(*lp->rx_skb), GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +0000242 if (!lp->rx_skb)
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000243 goto out;
Joe Perchesb2adaca2013-02-03 17:43:58 +0000244
Grant Likely92744982009-04-25 12:53:39 +0000245 /* allocate the tx and rx ring buffer descriptors. */
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400246 /* returns a virtual address and a physical address. */
Joe Perchesede23fa82013-08-26 22:45:23 -0700247 lp->tx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
248 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
249 &lp->tx_bd_p, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000250 if (!lp->tx_bd_v)
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000251 goto out;
Joe Perchesd0320f72013-03-14 13:07:21 +0000252
Joe Perchesede23fa82013-08-26 22:45:23 -0700253 lp->rx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
254 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
255 &lp->rx_bd_p, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000256 if (!lp->rx_bd_v)
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000257 goto out;
Grant Likely92744982009-04-25 12:53:39 +0000258
Grant Likely92744982009-04-25 12:53:39 +0000259 for (i = 0; i < TX_BD_NUM; i++) {
260 lp->tx_bd_v[i].next = lp->tx_bd_p +
261 sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
262 }
263
Grant Likely92744982009-04-25 12:53:39 +0000264 for (i = 0; i < RX_BD_NUM; i++) {
265 lp->rx_bd_v[i].next = lp->rx_bd_p +
266 sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
267
John Linne44171f2010-04-08 07:08:02 +0000268 skb = netdev_alloc_skb_ip_align(ndev,
269 XTE_MAX_JUMBO_FRAME_SIZE);
Joe Perches720a43e2013-03-08 15:03:25 +0000270 if (!skb)
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000271 goto out;
Joe Perches720a43e2013-03-08 15:03:25 +0000272
Grant Likely92744982009-04-25 12:53:39 +0000273 lp->rx_skb[i] = skb;
Grant Likely92744982009-04-25 12:53:39 +0000274 /* returns physical address of skb->data */
275 lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
276 skb->data,
277 XTE_MAX_JUMBO_FRAME_SIZE,
278 DMA_FROM_DEVICE);
279 lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
280 lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
281 }
282
John Linne44171f2010-04-08 07:08:02 +0000283 lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
Grant Likely92744982009-04-25 12:53:39 +0000284 CHNL_CTRL_IRQ_EN |
285 CHNL_CTRL_IRQ_DLY_EN |
286 CHNL_CTRL_IRQ_COAL_EN);
287 /* 0x10220483 */
288 /* 0x00100483 */
Brian Hill23ecc4b2010-05-26 20:44:30 -0700289 lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
Grant Likely92744982009-04-25 12:53:39 +0000290 CHNL_CTRL_IRQ_EN |
291 CHNL_CTRL_IRQ_DLY_EN |
292 CHNL_CTRL_IRQ_COAL_EN |
293 CHNL_CTRL_IRQ_IOE);
294 /* 0xff010283 */
295
John Linne44171f2010-04-08 07:08:02 +0000296 lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
297 lp->dma_out(lp, RX_TAILDESC_PTR,
Grant Likely92744982009-04-25 12:53:39 +0000298 lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
John Linne44171f2010-04-08 07:08:02 +0000299 lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
Grant Likely92744982009-04-25 12:53:39 +0000300
301 return 0;
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000302
303out:
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000304 temac_dma_bd_release(ndev);
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000305 return -ENOMEM;
Grant Likely92744982009-04-25 12:53:39 +0000306}
307
308/* ---------------------------------------------------------------------
309 * net_device_ops
310 */
311
Jiri Pirko04e406d2013-01-01 03:30:19 +0000312static void temac_do_set_mac_address(struct net_device *ndev)
Grant Likely92744982009-04-25 12:53:39 +0000313{
314 struct temac_local *lp = netdev_priv(ndev);
315
Grant Likely92744982009-04-25 12:53:39 +0000316 /* set up unicast MAC address filter set its mac address */
317 mutex_lock(&lp->indirect_mutex);
318 temac_indirect_out32(lp, XTE_UAW0_OFFSET,
319 (ndev->dev_addr[0]) |
320 (ndev->dev_addr[1] << 8) |
321 (ndev->dev_addr[2] << 16) |
322 (ndev->dev_addr[3] << 24));
323 /* There are reserved bits in EUAW1
324 * so don't affect them Set MAC bits [47:32] in EUAW1 */
325 temac_indirect_out32(lp, XTE_UAW1_OFFSET,
326 (ndev->dev_addr[4] & 0x000000ff) |
327 (ndev->dev_addr[5] << 8));
328 mutex_unlock(&lp->indirect_mutex);
Jiri Pirko04e406d2013-01-01 03:30:19 +0000329}
Grant Likely92744982009-04-25 12:53:39 +0000330
Jiri Pirko04e406d2013-01-01 03:30:19 +0000331static int temac_init_mac_address(struct net_device *ndev, void *address)
332{
333 memcpy(ndev->dev_addr, address, ETH_ALEN);
334 if (!is_valid_ether_addr(ndev->dev_addr))
335 eth_hw_addr_random(ndev);
336 temac_do_set_mac_address(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000337 return 0;
338}
339
Jiri Pirko04e406d2013-01-01 03:30:19 +0000340static int temac_set_mac_address(struct net_device *ndev, void *p)
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000341{
342 struct sockaddr *addr = p;
343
Jiri Pirko04e406d2013-01-01 03:30:19 +0000344 if (!is_valid_ether_addr(addr->sa_data))
345 return -EADDRNOTAVAIL;
346 memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
347 temac_do_set_mac_address(ndev);
348 return 0;
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000349}
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 Pirko22bedad32010-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 Pirko22bedad32010-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 Pirko22bedad32010-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 Pirko22bedad32010-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
Jiri Pirko04e406d2013-01-01 03:30:19 +0000575 temac_do_set_mac_address(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000576
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,
Ian Campbell3ed6f692011-10-10 01:11:40 +0000717 skb_frag_address(frag),
Stephen Rothwell2edcd4c2011-11-02 01:49:44 -0400718 skb_frag_size(frag), DMA_TO_DEVICE);
719 cur_p->len = skb_frag_size(frag);
Grant Likely92744982009-04-25 12:53:39 +0000720 cur_p->app0 = 0;
721 frag++;
722 }
723 cur_p->app0 |= STS_CTRL_APP0_EOP;
724
725 tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
726 lp->tx_bd_tail++;
727 if (lp->tx_bd_tail >= TX_BD_NUM)
728 lp->tx_bd_tail = 0;
729
Richard Cochran93e0ed12011-06-19 21:51:26 +0000730 skb_tx_timestamp(skb);
731
Grant Likely92744982009-04-25 12:53:39 +0000732 /* Kick off the transfer */
John Linne44171f2010-04-08 07:08:02 +0000733 lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
Grant Likely92744982009-04-25 12:53:39 +0000734
Patrick McHardy6ed10652009-06-23 06:03:08 +0000735 return NETDEV_TX_OK;
Grant Likely92744982009-04-25 12:53:39 +0000736}
737
738
739static void ll_temac_recv(struct net_device *ndev)
740{
741 struct temac_local *lp = netdev_priv(ndev);
742 struct sk_buff *skb, *new_skb;
743 unsigned int bdstat;
744 struct cdmac_bd *cur_p;
745 dma_addr_t tail_p;
746 int length;
Grant Likely92744982009-04-25 12:53:39 +0000747 unsigned long flags;
748
749 spin_lock_irqsave(&lp->rx_lock, flags);
750
751 tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
752 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
753
754 bdstat = cur_p->app0;
755 while ((bdstat & STS_CTRL_APP0_CMPLT)) {
756
757 skb = lp->rx_skb[lp->rx_bd_ci];
Steven J. Magnanic3b7c122010-02-17 07:14:20 +0000758 length = cur_p->app4 & 0x3FFF;
Grant Likely92744982009-04-25 12:53:39 +0000759
John Linn33646d72010-04-08 07:08:01 +0000760 dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
Grant Likely92744982009-04-25 12:53:39 +0000761 DMA_FROM_DEVICE);
762
763 skb_put(skb, length);
Grant Likely92744982009-04-25 12:53:39 +0000764 skb->protocol = eth_type_trans(skb, ndev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700765 skb_checksum_none_assert(skb);
Grant Likely92744982009-04-25 12:53:39 +0000766
Brian Hill23ecc4b2010-05-26 20:44:30 -0700767 /* if we're doing rx csum offload, set it up */
768 if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
769 (skb->protocol == __constant_htons(ETH_P_IP)) &&
770 (skb->len > 64)) {
771
772 skb->csum = cur_p->app3 & 0xFFFF;
773 skb->ip_summed = CHECKSUM_COMPLETE;
774 }
775
Richard Cochran93e0ed12011-06-19 21:51:26 +0000776 if (!skb_defer_rx_timestamp(skb))
777 netif_rx(skb);
Grant Likely92744982009-04-25 12:53:39 +0000778
779 ndev->stats.rx_packets++;
780 ndev->stats.rx_bytes += length;
781
John Linne44171f2010-04-08 07:08:02 +0000782 new_skb = netdev_alloc_skb_ip_align(ndev,
783 XTE_MAX_JUMBO_FRAME_SIZE);
Joe Perches720a43e2013-03-08 15:03:25 +0000784 if (!new_skb) {
Grant Likely92744982009-04-25 12:53:39 +0000785 spin_unlock_irqrestore(&lp->rx_lock, flags);
786 return;
787 }
788
Grant Likely92744982009-04-25 12:53:39 +0000789 cur_p->app0 = STS_CTRL_APP0_IRQONEND;
790 cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
791 XTE_MAX_JUMBO_FRAME_SIZE,
792 DMA_FROM_DEVICE);
793 cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
794 lp->rx_skb[lp->rx_bd_ci] = new_skb;
795
796 lp->rx_bd_ci++;
797 if (lp->rx_bd_ci >= RX_BD_NUM)
798 lp->rx_bd_ci = 0;
799
800 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
801 bdstat = cur_p->app0;
802 }
John Linne44171f2010-04-08 07:08:02 +0000803 lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
Grant Likely92744982009-04-25 12:53:39 +0000804
805 spin_unlock_irqrestore(&lp->rx_lock, flags);
806}
807
808static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
809{
810 struct net_device *ndev = _ndev;
811 struct temac_local *lp = netdev_priv(ndev);
812 unsigned int status;
813
John Linne44171f2010-04-08 07:08:02 +0000814 status = lp->dma_in(lp, TX_IRQ_REG);
815 lp->dma_out(lp, TX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000816
817 if (status & (IRQ_COAL | IRQ_DLY))
818 temac_start_xmit_done(lp->ndev);
819 if (status & 0x080)
820 dev_err(&ndev->dev, "DMA error 0x%x\n", status);
821
822 return IRQ_HANDLED;
823}
824
825static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
826{
827 struct net_device *ndev = _ndev;
828 struct temac_local *lp = netdev_priv(ndev);
829 unsigned int status;
830
831 /* Read and clear the status registers */
John Linne44171f2010-04-08 07:08:02 +0000832 status = lp->dma_in(lp, RX_IRQ_REG);
833 lp->dma_out(lp, RX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000834
835 if (status & (IRQ_COAL | IRQ_DLY))
836 ll_temac_recv(lp->ndev);
837
838 return IRQ_HANDLED;
839}
840
841static int temac_open(struct net_device *ndev)
842{
843 struct temac_local *lp = netdev_priv(ndev);
844 int rc;
845
846 dev_dbg(&ndev->dev, "temac_open()\n");
847
848 if (lp->phy_node) {
849 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
850 temac_adjust_link, 0, 0);
851 if (!lp->phy_dev) {
852 dev_err(lp->dev, "of_phy_connect() failed\n");
853 return -ENODEV;
854 }
855
856 phy_start(lp->phy_dev);
857 }
858
Ricardo Ribalda50ec1532011-11-07 23:31:58 +0000859 temac_device_reset(ndev);
860
Grant Likely92744982009-04-25 12:53:39 +0000861 rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
862 if (rc)
863 goto err_tx_irq;
864 rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
865 if (rc)
866 goto err_rx_irq;
867
Grant Likely92744982009-04-25 12:53:39 +0000868 return 0;
869
870 err_rx_irq:
871 free_irq(lp->tx_irq, ndev);
872 err_tx_irq:
873 if (lp->phy_dev)
874 phy_disconnect(lp->phy_dev);
875 lp->phy_dev = NULL;
876 dev_err(lp->dev, "request_irq() failed\n");
877 return rc;
878}
879
880static int temac_stop(struct net_device *ndev)
881{
882 struct temac_local *lp = netdev_priv(ndev);
883
884 dev_dbg(&ndev->dev, "temac_close()\n");
885
886 free_irq(lp->tx_irq, ndev);
887 free_irq(lp->rx_irq, ndev);
888
889 if (lp->phy_dev)
890 phy_disconnect(lp->phy_dev);
891 lp->phy_dev = NULL;
892
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000893 temac_dma_bd_release(ndev);
894
Grant Likely92744982009-04-25 12:53:39 +0000895 return 0;
896}
897
898#ifdef CONFIG_NET_POLL_CONTROLLER
899static void
900temac_poll_controller(struct net_device *ndev)
901{
902 struct temac_local *lp = netdev_priv(ndev);
903
904 disable_irq(lp->tx_irq);
905 disable_irq(lp->rx_irq);
906
Michal Simek85399922010-08-18 00:26:34 +0000907 ll_temac_rx_irq(lp->tx_irq, ndev);
908 ll_temac_tx_irq(lp->rx_irq, ndev);
Grant Likely92744982009-04-25 12:53:39 +0000909
910 enable_irq(lp->tx_irq);
911 enable_irq(lp->rx_irq);
912}
913#endif
914
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000915static int temac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
916{
917 struct temac_local *lp = netdev_priv(ndev);
918
919 if (!netif_running(ndev))
920 return -EINVAL;
921
922 if (!lp->phy_dev)
923 return -EINVAL;
924
925 return phy_mii_ioctl(lp->phy_dev, rq, cmd);
926}
927
Grant Likely92744982009-04-25 12:53:39 +0000928static const struct net_device_ops temac_netdev_ops = {
929 .ndo_open = temac_open,
930 .ndo_stop = temac_stop,
931 .ndo_start_xmit = temac_start_xmit,
Jiri Pirko04e406d2013-01-01 03:30:19 +0000932 .ndo_set_mac_address = temac_set_mac_address,
Denis Kirjanov60eb5fd2010-07-10 11:10:44 +0000933 .ndo_validate_addr = eth_validate_addr,
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000934 .ndo_do_ioctl = temac_ioctl,
Grant Likely92744982009-04-25 12:53:39 +0000935#ifdef CONFIG_NET_POLL_CONTROLLER
936 .ndo_poll_controller = temac_poll_controller,
937#endif
938};
939
940/* ---------------------------------------------------------------------
941 * SYSFS device attributes
942 */
943static ssize_t temac_show_llink_regs(struct device *dev,
944 struct device_attribute *attr, char *buf)
945{
946 struct net_device *ndev = dev_get_drvdata(dev);
947 struct temac_local *lp = netdev_priv(ndev);
948 int i, len = 0;
949
950 for (i = 0; i < 0x11; i++)
John Linne44171f2010-04-08 07:08:02 +0000951 len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
Grant Likely92744982009-04-25 12:53:39 +0000952 (i % 8) == 7 ? "\n" : " ");
953 len += sprintf(buf + len, "\n");
954
955 return len;
956}
957
958static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
959
960static struct attribute *temac_device_attrs[] = {
961 &dev_attr_llink_regs.attr,
962 NULL,
963};
964
965static const struct attribute_group temac_attr_group = {
966 .attrs = temac_device_attrs,
967};
968
Ricardo9eac2d42011-10-18 21:35:25 +0000969/* ethtool support */
970static int temac_get_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
971{
972 struct temac_local *lp = netdev_priv(ndev);
973 return phy_ethtool_gset(lp->phy_dev, cmd);
974}
975
976static int temac_set_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
977{
978 struct temac_local *lp = netdev_priv(ndev);
979 return phy_ethtool_sset(lp->phy_dev, cmd);
980}
981
982static int temac_nway_reset(struct net_device *ndev)
983{
984 struct temac_local *lp = netdev_priv(ndev);
985 return phy_start_aneg(lp->phy_dev);
986}
987
988static const struct ethtool_ops temac_ethtool_ops = {
989 .get_settings = temac_get_settings,
990 .set_settings = temac_set_settings,
991 .nway_reset = temac_nway_reset,
992 .get_link = ethtool_op_get_link,
Richard Cochranf85e5ea2012-04-03 22:59:30 +0000993 .get_ts_info = ethtool_op_get_ts_info,
Ricardo9eac2d42011-10-18 21:35:25 +0000994};
995
Bill Pemberton06b0e682012-12-03 09:24:08 -0500996static int temac_of_probe(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +0000997{
998 struct device_node *np;
999 struct temac_local *lp;
1000 struct net_device *ndev;
1001 const void *addr;
Brian Hill23ecc4b2010-05-26 20:44:30 -07001002 __be32 *p;
Grant Likely92744982009-04-25 12:53:39 +00001003 int size, rc = 0;
Grant Likely92744982009-04-25 12:53:39 +00001004
1005 /* Init network device structure */
1006 ndev = alloc_etherdev(sizeof(*lp));
Joe Perches41de8d42012-01-29 13:47:52 +00001007 if (!ndev)
Grant Likely92744982009-04-25 12:53:39 +00001008 return -ENOMEM;
Joe Perches41de8d42012-01-29 13:47:52 +00001009
Grant Likely92744982009-04-25 12:53:39 +00001010 ether_setup(ndev);
Jingoo Han8513fbd2013-05-23 00:52:31 +00001011 platform_set_drvdata(op, ndev);
Grant Likely92744982009-04-25 12:53:39 +00001012 SET_NETDEV_DEV(ndev, &op->dev);
1013 ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
1014 ndev->features = NETIF_F_SG | NETIF_F_FRAGLIST;
1015 ndev->netdev_ops = &temac_netdev_ops;
Ricardo9eac2d42011-10-18 21:35:25 +00001016 ndev->ethtool_ops = &temac_ethtool_ops;
Grant Likely92744982009-04-25 12:53:39 +00001017#if 0
1018 ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
1019 ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
1020 ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
1021 ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
Patrick McHardyf6469682013-04-19 02:04:27 +00001022 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; /* Transmit VLAN hw accel */
1023 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; /* Receive VLAN hw acceleration */
1024 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; /* Receive VLAN filtering */
Grant Likely92744982009-04-25 12:53:39 +00001025 ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
1026 ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
1027 ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
1028 ndev->features |= NETIF_F_LRO; /* large receive offload */
1029#endif
1030
1031 /* setup temac private info structure */
1032 lp = netdev_priv(ndev);
1033 lp->ndev = ndev;
1034 lp->dev = &op->dev;
1035 lp->options = XTE_OPTION_DEFAULTS;
1036 spin_lock_init(&lp->rx_lock);
1037 mutex_init(&lp->indirect_mutex);
1038
1039 /* map device registers */
Grant Likely61c7a082010-04-13 16:12:29 -07001040 lp->regs = of_iomap(op->dev.of_node, 0);
Grant Likely92744982009-04-25 12:53:39 +00001041 if (!lp->regs) {
1042 dev_err(&op->dev, "could not map temac regs.\n");
1043 goto nodev;
1044 }
1045
Brian Hill23ecc4b2010-05-26 20:44:30 -07001046 /* Setup checksum offload, but default to off if not specified */
1047 lp->temac_features = 0;
1048 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
1049 if (p && be32_to_cpu(*p)) {
1050 lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
1051 /* Can checksum TCP/UDP over IPv4. */
1052 ndev->features |= NETIF_F_IP_CSUM;
1053 }
1054 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
1055 if (p && be32_to_cpu(*p))
1056 lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
1057
Grant Likely92744982009-04-25 12:53:39 +00001058 /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
Grant Likely61c7a082010-04-13 16:12:29 -07001059 np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
Grant Likely92744982009-04-25 12:53:39 +00001060 if (!np) {
1061 dev_err(&op->dev, "could not find DMA node\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001062 goto err_iounmap;
Grant Likely92744982009-04-25 12:53:39 +00001063 }
1064
John Linne44171f2010-04-08 07:08:02 +00001065 /* Setup the DMA register accesses, could be DCR or memory mapped */
1066 if (temac_dcr_setup(lp, op, np)) {
1067
1068 /* no DCR in the device tree, try non-DCR */
1069 lp->sdma_regs = of_iomap(np, 0);
1070 if (lp->sdma_regs) {
1071 lp->dma_in = temac_dma_in32;
1072 lp->dma_out = temac_dma_out32;
1073 dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
1074 } else {
1075 dev_err(&op->dev, "unable to map DMA registers\n");
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001076 of_node_put(np);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001077 goto err_iounmap;
John Linne44171f2010-04-08 07:08:02 +00001078 }
Grant Likely92744982009-04-25 12:53:39 +00001079 }
Grant Likely92744982009-04-25 12:53:39 +00001080
1081 lp->rx_irq = irq_of_parse_and_map(np, 0);
1082 lp->tx_irq = irq_of_parse_and_map(np, 1);
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001083
1084 of_node_put(np); /* Finished with the DMA node; drop the reference */
1085
Michal Simek4e68ea22011-12-21 15:42:50 -05001086 if (!lp->rx_irq || !lp->tx_irq) {
Grant Likely92744982009-04-25 12:53:39 +00001087 dev_err(&op->dev, "could not determine irqs\n");
1088 rc = -ENOMEM;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001089 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001090 }
1091
Grant Likely92744982009-04-25 12:53:39 +00001092
1093 /* Retrieve the MAC address */
Grant Likely61c7a082010-04-13 16:12:29 -07001094 addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
Grant Likely92744982009-04-25 12:53:39 +00001095 if ((!addr) || (size != 6)) {
1096 dev_err(&op->dev, "could not find MAC address\n");
1097 rc = -ENODEV;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001098 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001099 }
Jiri Pirko04e406d2013-01-01 03:30:19 +00001100 temac_init_mac_address(ndev, (void *)addr);
Grant Likely92744982009-04-25 12:53:39 +00001101
Grant Likely61c7a082010-04-13 16:12:29 -07001102 rc = temac_mdio_setup(lp, op->dev.of_node);
Grant Likely92744982009-04-25 12:53:39 +00001103 if (rc)
1104 dev_warn(&op->dev, "error registering MDIO bus\n");
1105
Grant Likely61c7a082010-04-13 16:12:29 -07001106 lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
Grant Likely92744982009-04-25 12:53:39 +00001107 if (lp->phy_node)
1108 dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
1109
1110 /* Add the device attributes */
1111 rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
1112 if (rc) {
1113 dev_err(lp->dev, "Error creating sysfs files\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001114 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001115 }
1116
1117 rc = register_netdev(lp->ndev);
1118 if (rc) {
1119 dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
1120 goto err_register_ndev;
1121 }
1122
1123 return 0;
1124
1125 err_register_ndev:
1126 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001127 err_iounmap_2:
1128 if (lp->sdma_regs)
1129 iounmap(lp->sdma_regs);
1130 err_iounmap:
1131 iounmap(lp->regs);
Grant Likely92744982009-04-25 12:53:39 +00001132 nodev:
1133 free_netdev(ndev);
1134 ndev = NULL;
1135 return rc;
1136}
1137
Bill Pemberton06b0e682012-12-03 09:24:08 -05001138static int temac_of_remove(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +00001139{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001140 struct net_device *ndev = platform_get_drvdata(op);
Grant Likely92744982009-04-25 12:53:39 +00001141 struct temac_local *lp = netdev_priv(ndev);
1142
1143 temac_mdio_teardown(lp);
1144 unregister_netdev(ndev);
1145 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1146 if (lp->phy_node)
1147 of_node_put(lp->phy_node);
1148 lp->phy_node = NULL;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001149 iounmap(lp->regs);
1150 if (lp->sdma_regs)
1151 iounmap(lp->sdma_regs);
Grant Likely92744982009-04-25 12:53:39 +00001152 free_netdev(ndev);
1153 return 0;
1154}
1155
Bill Pemberton06b0e682012-12-03 09:24:08 -05001156static struct of_device_id temac_of_match[] = {
Grant Likely92744982009-04-25 12:53:39 +00001157 { .compatible = "xlnx,xps-ll-temac-1.01.b", },
Steven J. Magnanic3b7c122010-02-17 07:14:20 +00001158 { .compatible = "xlnx,xps-ll-temac-2.00.a", },
1159 { .compatible = "xlnx,xps-ll-temac-2.02.a", },
1160 { .compatible = "xlnx,xps-ll-temac-2.03.a", },
Grant Likely92744982009-04-25 12:53:39 +00001161 {},
1162};
1163MODULE_DEVICE_TABLE(of, temac_of_match);
1164
Grant Likely74888762011-02-22 21:05:51 -07001165static struct platform_driver temac_of_driver = {
Grant Likely92744982009-04-25 12:53:39 +00001166 .probe = temac_of_probe,
Bill Pemberton06b0e682012-12-03 09:24:08 -05001167 .remove = temac_of_remove,
Grant Likely92744982009-04-25 12:53:39 +00001168 .driver = {
1169 .owner = THIS_MODULE,
1170 .name = "xilinx_temac",
Grant Likely40182942010-04-13 16:13:02 -07001171 .of_match_table = temac_of_match,
Grant Likely92744982009-04-25 12:53:39 +00001172 },
1173};
1174
Axel Lindb62f682011-11-27 16:44:17 +00001175module_platform_driver(temac_of_driver);
Grant Likely92744982009-04-25 12:53:39 +00001176
1177MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1178MODULE_AUTHOR("Yoshio Kashiwagi");
1179MODULE_LICENSE("GPL");