blob: 60abc9250f56a7eac7025447626799e3b046c8f5 [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>
Grant Likely92744982009-04-25 12:53:39 +000032#include <linux/mii.h>
33#include <linux/module.h>
34#include <linux/mutex.h>
35#include <linux/netdevice.h>
36#include <linux/of.h>
37#include <linux/of_device.h>
Rob Herring5c9f3032013-09-07 14:05:10 -050038#include <linux/of_irq.h>
Grant Likely92744982009-04-25 12:53:39 +000039#include <linux/of_mdio.h>
Tobias Klauser06205472016-12-07 13:25:29 +010040#include <linux/of_net.h>
Grant Likely92744982009-04-25 12:53:39 +000041#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{
Michal Simek84ea0de2015-06-05 10:49:17 +020066 return in_be32(lp->regs + offset);
Grant Likely92744982009-04-25 12:53:39 +000067}
68
69void temac_iow(struct temac_local *lp, int offset, u32 value)
70{
Michal Simek84ea0de2015-06-05 10:49:17 +020071 out_be32(lp->regs + offset, value);
Grant Likely92744982009-04-25 12:53:39 +000072}
73
74int temac_indirect_busywait(struct temac_local *lp)
75{
Manuel Schölling9f8b93c2014-06-22 13:24:54 +020076 unsigned long end = jiffies + 2;
Grant Likely92744982009-04-25 12:53:39 +000077
78 while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) {
Manuel Schölling3aeea532014-05-22 21:10:28 +020079 if (time_before_eq(end, jiffies)) {
Grant Likely92744982009-04-25 12:53:39 +000080 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{
Michal Simek84ea0de2015-06-05 10:49:17 +0200128 return in_be32(lp->sdma_regs + (reg << 2));
John Linne44171f2010-04-08 07:08:02 +0000129}
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{
Michal Simek84ea0de2015-06-05 10:49:17 +0200138 out_be32(lp->sdma_regs + (reg << 2), value);
John Linne44171f2010-04-08 07:08:02 +0000139}
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);
Markus Elfring38a90e72014-11-20 14:47:12 +0100228 kfree(lp->rx_skb);
Denis Kirjanov301e9d92010-07-08 10:24:51 +0000229}
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. */
Joe Perchesede23fa82013-08-26 22:45:23 -0700246 lp->tx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
247 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
248 &lp->tx_bd_p, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000249 if (!lp->tx_bd_v)
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000250 goto out;
Joe Perchesd0320f72013-03-14 13:07:21 +0000251
Joe Perchesede23fa82013-08-26 22:45:23 -0700252 lp->rx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
253 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
254 &lp->rx_bd_p, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000255 if (!lp->rx_bd_v)
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000256 goto out;
Grant Likely92744982009-04-25 12:53:39 +0000257
Grant Likely92744982009-04-25 12:53:39 +0000258 for (i = 0; i < TX_BD_NUM; i++) {
259 lp->tx_bd_v[i].next = lp->tx_bd_p +
260 sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
261 }
262
Grant Likely92744982009-04-25 12:53:39 +0000263 for (i = 0; i < RX_BD_NUM; i++) {
264 lp->rx_bd_v[i].next = lp->rx_bd_p +
265 sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
266
John Linne44171f2010-04-08 07:08:02 +0000267 skb = netdev_alloc_skb_ip_align(ndev,
268 XTE_MAX_JUMBO_FRAME_SIZE);
Joe Perches720a43e2013-03-08 15:03:25 +0000269 if (!skb)
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000270 goto out;
Joe Perches720a43e2013-03-08 15:03:25 +0000271
Grant Likely92744982009-04-25 12:53:39 +0000272 lp->rx_skb[i] = skb;
Grant Likely92744982009-04-25 12:53:39 +0000273 /* returns physical address of skb->data */
274 lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
275 skb->data,
276 XTE_MAX_JUMBO_FRAME_SIZE,
277 DMA_FROM_DEVICE);
278 lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
279 lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
280 }
281
John Linne44171f2010-04-08 07:08:02 +0000282 lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
Grant Likely92744982009-04-25 12:53:39 +0000283 CHNL_CTRL_IRQ_EN |
284 CHNL_CTRL_IRQ_DLY_EN |
285 CHNL_CTRL_IRQ_COAL_EN);
286 /* 0x10220483 */
287 /* 0x00100483 */
Brian Hill23ecc4b2010-05-26 20:44:30 -0700288 lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
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 CHNL_CTRL_IRQ_IOE);
293 /* 0xff010283 */
294
John Linne44171f2010-04-08 07:08:02 +0000295 lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
296 lp->dma_out(lp, RX_TAILDESC_PTR,
Grant Likely92744982009-04-25 12:53:39 +0000297 lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
John Linne44171f2010-04-08 07:08:02 +0000298 lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
Grant Likely92744982009-04-25 12:53:39 +0000299
Ricardo Ribalda7167cf02013-10-01 08:17:10 +0200300 /* Init descriptor indexes */
301 lp->tx_bd_ci = 0;
302 lp->tx_bd_next = 0;
303 lp->tx_bd_tail = 0;
304 lp->rx_bd_ci = 0;
305
Grant Likely92744982009-04-25 12:53:39 +0000306 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
Jiri Pirko04e406d2013-01-01 03:30:19 +0000317static void temac_do_set_mac_address(struct net_device *ndev)
Grant Likely92744982009-04-25 12:53:39 +0000318{
319 struct temac_local *lp = netdev_priv(ndev);
320
Grant Likely92744982009-04-25 12:53:39 +0000321 /* set up unicast MAC address filter set its mac address */
322 mutex_lock(&lp->indirect_mutex);
323 temac_indirect_out32(lp, XTE_UAW0_OFFSET,
324 (ndev->dev_addr[0]) |
325 (ndev->dev_addr[1] << 8) |
326 (ndev->dev_addr[2] << 16) |
327 (ndev->dev_addr[3] << 24));
328 /* There are reserved bits in EUAW1
329 * so don't affect them Set MAC bits [47:32] in EUAW1 */
330 temac_indirect_out32(lp, XTE_UAW1_OFFSET,
331 (ndev->dev_addr[4] & 0x000000ff) |
332 (ndev->dev_addr[5] << 8));
333 mutex_unlock(&lp->indirect_mutex);
Jiri Pirko04e406d2013-01-01 03:30:19 +0000334}
Grant Likely92744982009-04-25 12:53:39 +0000335
Tobias Klauser06205472016-12-07 13:25:29 +0100336static int temac_init_mac_address(struct net_device *ndev, const void *address)
Jiri Pirko04e406d2013-01-01 03:30:19 +0000337{
338 memcpy(ndev->dev_addr, address, ETH_ALEN);
339 if (!is_valid_ether_addr(ndev->dev_addr))
340 eth_hw_addr_random(ndev);
341 temac_do_set_mac_address(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000342 return 0;
343}
344
Jiri Pirko04e406d2013-01-01 03:30:19 +0000345static int temac_set_mac_address(struct net_device *ndev, void *p)
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000346{
347 struct sockaddr *addr = p;
348
Jiri Pirko04e406d2013-01-01 03:30:19 +0000349 if (!is_valid_ether_addr(addr->sa_data))
350 return -EADDRNOTAVAIL;
351 memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
352 temac_do_set_mac_address(ndev);
353 return 0;
Steven J. Magnani8ea7a372010-02-17 07:55:07 +0000354}
355
Grant Likely92744982009-04-25 12:53:39 +0000356static void temac_set_multicast_list(struct net_device *ndev)
357{
358 struct temac_local *lp = netdev_priv(ndev);
359 u32 multi_addr_msw, multi_addr_lsw, val;
360 int i;
361
362 mutex_lock(&lp->indirect_mutex);
Joe Perches8e95a202009-12-03 07:58:21 +0000363 if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000364 netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) {
Grant Likely92744982009-04-25 12:53:39 +0000365 /*
366 * We must make the kernel realise we had to move
367 * into promisc mode or we start all out war on
368 * the cable. If it was a promisc request the
369 * flag is already set. If not we assert it.
370 */
371 ndev->flags |= IFF_PROMISC;
372 temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
373 dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000374 } else if (!netdev_mc_empty(ndev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000375 struct netdev_hw_addr *ha;
Grant Likely92744982009-04-25 12:53:39 +0000376
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000377 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000378 netdev_for_each_mc_addr(ha, ndev) {
Grant Likely92744982009-04-25 12:53:39 +0000379 if (i >= MULTICAST_CAM_TABLE_NUM)
380 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000381 multi_addr_msw = ((ha->addr[3] << 24) |
382 (ha->addr[2] << 16) |
383 (ha->addr[1] << 8) |
384 (ha->addr[0]));
Grant Likely92744982009-04-25 12:53:39 +0000385 temac_indirect_out32(lp, XTE_MAW0_OFFSET,
386 multi_addr_msw);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000387 multi_addr_lsw = ((ha->addr[5] << 8) |
388 (ha->addr[4]) | (i << 16));
Grant Likely92744982009-04-25 12:53:39 +0000389 temac_indirect_out32(lp, XTE_MAW1_OFFSET,
390 multi_addr_lsw);
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000391 i++;
Grant Likely92744982009-04-25 12:53:39 +0000392 }
393 } else {
394 val = temac_indirect_in32(lp, XTE_AFM_OFFSET);
395 temac_indirect_out32(lp, XTE_AFM_OFFSET,
396 val & ~XTE_AFM_EPPRM_MASK);
397 temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0);
398 temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0);
399 dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
400 }
401 mutex_unlock(&lp->indirect_mutex);
402}
403
Michal Simek84ea0de2015-06-05 10:49:17 +0200404static struct temac_option {
Grant Likely92744982009-04-25 12:53:39 +0000405 int flg;
406 u32 opt;
407 u32 reg;
408 u32 m_or;
409 u32 m_and;
410} temac_options[] = {
411 /* Turn on jumbo packet support for both Rx and Tx */
412 {
413 .opt = XTE_OPTION_JUMBO,
414 .reg = XTE_TXC_OFFSET,
415 .m_or = XTE_TXC_TXJMBO_MASK,
416 },
417 {
418 .opt = XTE_OPTION_JUMBO,
419 .reg = XTE_RXC1_OFFSET,
420 .m_or =XTE_RXC1_RXJMBO_MASK,
421 },
422 /* Turn on VLAN packet support for both Rx and Tx */
423 {
424 .opt = XTE_OPTION_VLAN,
425 .reg = XTE_TXC_OFFSET,
426 .m_or =XTE_TXC_TXVLAN_MASK,
427 },
428 {
429 .opt = XTE_OPTION_VLAN,
430 .reg = XTE_RXC1_OFFSET,
431 .m_or =XTE_RXC1_RXVLAN_MASK,
432 },
433 /* Turn on FCS stripping on receive packets */
434 {
435 .opt = XTE_OPTION_FCS_STRIP,
436 .reg = XTE_RXC1_OFFSET,
437 .m_or =XTE_RXC1_RXFCS_MASK,
438 },
439 /* Turn on FCS insertion on transmit packets */
440 {
441 .opt = XTE_OPTION_FCS_INSERT,
442 .reg = XTE_TXC_OFFSET,
443 .m_or =XTE_TXC_TXFCS_MASK,
444 },
445 /* Turn on length/type field checking on receive packets */
446 {
447 .opt = XTE_OPTION_LENTYPE_ERR,
448 .reg = XTE_RXC1_OFFSET,
449 .m_or =XTE_RXC1_RXLT_MASK,
450 },
451 /* Turn on flow control */
452 {
453 .opt = XTE_OPTION_FLOW_CONTROL,
454 .reg = XTE_FCC_OFFSET,
455 .m_or =XTE_FCC_RXFLO_MASK,
456 },
457 /* Turn on flow control */
458 {
459 .opt = XTE_OPTION_FLOW_CONTROL,
460 .reg = XTE_FCC_OFFSET,
461 .m_or =XTE_FCC_TXFLO_MASK,
462 },
463 /* Turn on promiscuous frame filtering (all frames are received ) */
464 {
465 .opt = XTE_OPTION_PROMISC,
466 .reg = XTE_AFM_OFFSET,
467 .m_or =XTE_AFM_EPPRM_MASK,
468 },
469 /* Enable transmitter if not already enabled */
470 {
471 .opt = XTE_OPTION_TXEN,
472 .reg = XTE_TXC_OFFSET,
473 .m_or =XTE_TXC_TXEN_MASK,
474 },
475 /* Enable receiver? */
476 {
477 .opt = XTE_OPTION_RXEN,
478 .reg = XTE_RXC1_OFFSET,
479 .m_or =XTE_RXC1_RXEN_MASK,
480 },
481 {}
482};
483
484/**
485 * temac_setoptions
486 */
487static u32 temac_setoptions(struct net_device *ndev, u32 options)
488{
489 struct temac_local *lp = netdev_priv(ndev);
490 struct temac_option *tp = &temac_options[0];
491 int reg;
492
493 mutex_lock(&lp->indirect_mutex);
494 while (tp->opt) {
495 reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or;
496 if (options & tp->opt)
497 reg |= tp->m_or;
498 temac_indirect_out32(lp, tp->reg, reg);
499 tp++;
500 }
501 lp->options |= options;
502 mutex_unlock(&lp->indirect_mutex);
503
Eric Dumazet807540b2010-09-23 05:40:09 +0000504 return 0;
Grant Likely92744982009-04-25 12:53:39 +0000505}
506
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200507/* Initialize temac */
Grant Likely92744982009-04-25 12:53:39 +0000508static void temac_device_reset(struct net_device *ndev)
509{
510 struct temac_local *lp = netdev_priv(ndev);
511 u32 timeout;
512 u32 val;
513
514 /* Perform a software reset */
515
516 /* 0x300 host enable bit ? */
517 /* reset PHY through control register ?:1 */
518
519 dev_dbg(&ndev->dev, "%s()\n", __func__);
520
521 mutex_lock(&lp->indirect_mutex);
522 /* Reset the receiver and wait for it to finish reset */
523 temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
524 timeout = 1000;
525 while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
526 udelay(1);
527 if (--timeout == 0) {
528 dev_err(&ndev->dev,
529 "temac_device_reset RX reset timeout!!\n");
530 break;
531 }
532 }
533
534 /* Reset the transmitter and wait for it to finish reset */
535 temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
536 timeout = 1000;
537 while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
538 udelay(1);
539 if (--timeout == 0) {
540 dev_err(&ndev->dev,
541 "temac_device_reset TX reset timeout!!\n");
542 break;
543 }
544 }
545
546 /* Disable the receiver */
547 val = temac_indirect_in32(lp, XTE_RXC1_OFFSET);
548 temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
549
550 /* Reset Local Link (DMA) */
John Linne44171f2010-04-08 07:08:02 +0000551 lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
Grant Likely92744982009-04-25 12:53:39 +0000552 timeout = 1000;
John Linne44171f2010-04-08 07:08:02 +0000553 while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
Grant Likely92744982009-04-25 12:53:39 +0000554 udelay(1);
555 if (--timeout == 0) {
556 dev_err(&ndev->dev,
557 "temac_device_reset DMA reset timeout!!\n");
558 break;
559 }
560 }
John Linne44171f2010-04-08 07:08:02 +0000561 lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
Grant Likely92744982009-04-25 12:53:39 +0000562
Denis Kirjanovfe62c292010-06-30 23:39:05 +0000563 if (temac_dma_bd_init(ndev)) {
564 dev_err(&ndev->dev,
565 "temac_device_reset descriptor allocation failed\n");
566 }
Grant Likely92744982009-04-25 12:53:39 +0000567
568 temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0);
569 temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0);
570 temac_indirect_out32(lp, XTE_TXC_OFFSET, 0);
571 temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
572
573 mutex_unlock(&lp->indirect_mutex);
574
575 /* Sync default options with HW
576 * but leave receiver and transmitter disabled. */
577 temac_setoptions(ndev,
578 lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
579
Jiri Pirko04e406d2013-01-01 03:30:19 +0000580 temac_do_set_mac_address(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000581
582 /* Set address filter table */
583 temac_set_multicast_list(ndev);
584 if (temac_setoptions(ndev, lp->options))
585 dev_err(&ndev->dev, "Error setting TEMAC options\n");
586
587 /* Init Driver variable */
Florian Westphal860e9532016-05-03 16:33:13 +0200588 netif_trans_update(ndev); /* prevent tx timeout */
Grant Likely92744982009-04-25 12:53:39 +0000589}
590
Michal Simek84ea0de2015-06-05 10:49:17 +0200591static void temac_adjust_link(struct net_device *ndev)
Grant Likely92744982009-04-25 12:53:39 +0000592{
593 struct temac_local *lp = netdev_priv(ndev);
Philippe Reynes31abbe342016-07-14 01:48:51 +0200594 struct phy_device *phy = ndev->phydev;
Grant Likely92744982009-04-25 12:53:39 +0000595 u32 mii_speed;
596 int link_state;
597
598 /* hash together the state values to decide if something has changed */
599 link_state = phy->speed | (phy->duplex << 1) | phy->link;
600
601 mutex_lock(&lp->indirect_mutex);
602 if (lp->last_link != link_state) {
603 mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET);
604 mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
605
606 switch (phy->speed) {
607 case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
608 case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
609 case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
610 }
611
612 /* Write new speed setting out to TEMAC */
613 temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed);
614 lp->last_link = link_state;
615 phy_print_status(phy);
616 }
617 mutex_unlock(&lp->indirect_mutex);
618}
619
620static void temac_start_xmit_done(struct net_device *ndev)
621{
622 struct temac_local *lp = netdev_priv(ndev);
623 struct cdmac_bd *cur_p;
624 unsigned int stat = 0;
625
626 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
627 stat = cur_p->app0;
628
629 while (stat & STS_CTRL_APP0_CMPLT) {
630 dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len,
631 DMA_TO_DEVICE);
632 if (cur_p->app4)
633 dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
634 cur_p->app0 = 0;
Brian Hill23ecc4b2010-05-26 20:44:30 -0700635 cur_p->app1 = 0;
636 cur_p->app2 = 0;
637 cur_p->app3 = 0;
638 cur_p->app4 = 0;
Grant Likely92744982009-04-25 12:53:39 +0000639
640 ndev->stats.tx_packets++;
641 ndev->stats.tx_bytes += cur_p->len;
642
643 lp->tx_bd_ci++;
644 if (lp->tx_bd_ci >= TX_BD_NUM)
645 lp->tx_bd_ci = 0;
646
647 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
648 stat = cur_p->app0;
649 }
650
651 netif_wake_queue(ndev);
652}
653
Brian Hill23ecc4b2010-05-26 20:44:30 -0700654static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
655{
656 struct cdmac_bd *cur_p;
657 int tail;
658
659 tail = lp->tx_bd_tail;
660 cur_p = &lp->tx_bd_v[tail];
661
662 do {
663 if (cur_p->app0)
664 return NETDEV_TX_BUSY;
665
666 tail++;
667 if (tail >= TX_BD_NUM)
668 tail = 0;
669
670 cur_p = &lp->tx_bd_v[tail];
671 num_frag--;
672 } while (num_frag >= 0);
673
674 return 0;
675}
676
Grant Likely92744982009-04-25 12:53:39 +0000677static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
678{
679 struct temac_local *lp = netdev_priv(ndev);
680 struct cdmac_bd *cur_p;
681 dma_addr_t start_p, tail_p;
682 int ii;
683 unsigned long num_frag;
684 skb_frag_t *frag;
685
686 num_frag = skb_shinfo(skb)->nr_frags;
687 frag = &skb_shinfo(skb)->frags[0];
688 start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
689 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
690
Brian Hill23ecc4b2010-05-26 20:44:30 -0700691 if (temac_check_tx_bd_space(lp, num_frag)) {
Michal Simek38242462015-05-11 16:05:02 +0200692 if (!netif_queue_stopped(ndev))
Grant Likely92744982009-04-25 12:53:39 +0000693 netif_stop_queue(ndev);
Grant Likely92744982009-04-25 12:53:39 +0000694 return NETDEV_TX_BUSY;
695 }
696
697 cur_p->app0 = 0;
698 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000699 unsigned int csum_start_off = skb_checksum_start_offset(skb);
Brian Hill23ecc4b2010-05-26 20:44:30 -0700700 unsigned int csum_index_off = csum_start_off + skb->csum_offset;
Grant Likely92744982009-04-25 12:53:39 +0000701
Brian Hill23ecc4b2010-05-26 20:44:30 -0700702 cur_p->app0 |= 1; /* TX Checksum Enabled */
703 cur_p->app1 = (csum_start_off << 16) | csum_index_off;
704 cur_p->app2 = 0; /* initial checksum seed */
Grant Likely92744982009-04-25 12:53:39 +0000705 }
Brian Hill23ecc4b2010-05-26 20:44:30 -0700706
Grant Likely92744982009-04-25 12:53:39 +0000707 cur_p->app0 |= STS_CTRL_APP0_SOP;
708 cur_p->len = skb_headlen(skb);
Michal Simek44d4f8d2015-05-12 08:06:15 +0200709 cur_p->phys = dma_map_single(ndev->dev.parent, skb->data,
710 skb_headlen(skb), DMA_TO_DEVICE);
Grant Likely92744982009-04-25 12:53:39 +0000711 cur_p->app4 = (unsigned long)skb;
712
713 for (ii = 0; ii < num_frag; ii++) {
714 lp->tx_bd_tail++;
715 if (lp->tx_bd_tail >= TX_BD_NUM)
716 lp->tx_bd_tail = 0;
717
718 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
719 cur_p->phys = dma_map_single(ndev->dev.parent,
Ian Campbell3ed6f692011-10-10 01:11:40 +0000720 skb_frag_address(frag),
Stephen Rothwell2edcd4c2011-11-02 01:49:44 -0400721 skb_frag_size(frag), DMA_TO_DEVICE);
722 cur_p->len = skb_frag_size(frag);
Grant Likely92744982009-04-25 12:53:39 +0000723 cur_p->app0 = 0;
724 frag++;
725 }
726 cur_p->app0 |= STS_CTRL_APP0_EOP;
727
728 tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
729 lp->tx_bd_tail++;
730 if (lp->tx_bd_tail >= TX_BD_NUM)
731 lp->tx_bd_tail = 0;
732
Richard Cochran93e0ed12011-06-19 21:51:26 +0000733 skb_tx_timestamp(skb);
734
Grant Likely92744982009-04-25 12:53:39 +0000735 /* Kick off the transfer */
John Linne44171f2010-04-08 07:08:02 +0000736 lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
Grant Likely92744982009-04-25 12:53:39 +0000737
Patrick McHardy6ed10652009-06-23 06:03:08 +0000738 return NETDEV_TX_OK;
Grant Likely92744982009-04-25 12:53:39 +0000739}
740
741
742static void ll_temac_recv(struct net_device *ndev)
743{
744 struct temac_local *lp = netdev_priv(ndev);
745 struct sk_buff *skb, *new_skb;
746 unsigned int bdstat;
747 struct cdmac_bd *cur_p;
748 dma_addr_t tail_p;
749 int length;
Grant Likely92744982009-04-25 12:53:39 +0000750 unsigned long flags;
751
752 spin_lock_irqsave(&lp->rx_lock, flags);
753
754 tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
755 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
756
757 bdstat = cur_p->app0;
758 while ((bdstat & STS_CTRL_APP0_CMPLT)) {
759
760 skb = lp->rx_skb[lp->rx_bd_ci];
Steven J. Magnanic3b7c122010-02-17 07:14:20 +0000761 length = cur_p->app4 & 0x3FFF;
Grant Likely92744982009-04-25 12:53:39 +0000762
John Linn33646d72010-04-08 07:08:01 +0000763 dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
Grant Likely92744982009-04-25 12:53:39 +0000764 DMA_FROM_DEVICE);
765
766 skb_put(skb, length);
Grant Likely92744982009-04-25 12:53:39 +0000767 skb->protocol = eth_type_trans(skb, ndev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700768 skb_checksum_none_assert(skb);
Grant Likely92744982009-04-25 12:53:39 +0000769
Brian Hill23ecc4b2010-05-26 20:44:30 -0700770 /* if we're doing rx csum offload, set it up */
771 if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
Joe Perchesceffc4a2014-03-12 10:22:36 -0700772 (skb->protocol == htons(ETH_P_IP)) &&
773 (skb->len > 64)) {
Brian Hill23ecc4b2010-05-26 20:44:30 -0700774
775 skb->csum = cur_p->app3 & 0xFFFF;
776 skb->ip_summed = CHECKSUM_COMPLETE;
777 }
778
Richard Cochran93e0ed12011-06-19 21:51:26 +0000779 if (!skb_defer_rx_timestamp(skb))
780 netif_rx(skb);
Grant Likely92744982009-04-25 12:53:39 +0000781
782 ndev->stats.rx_packets++;
783 ndev->stats.rx_bytes += length;
784
John Linne44171f2010-04-08 07:08:02 +0000785 new_skb = netdev_alloc_skb_ip_align(ndev,
786 XTE_MAX_JUMBO_FRAME_SIZE);
Joe Perches720a43e2013-03-08 15:03:25 +0000787 if (!new_skb) {
Grant Likely92744982009-04-25 12:53:39 +0000788 spin_unlock_irqrestore(&lp->rx_lock, flags);
789 return;
790 }
791
Grant Likely92744982009-04-25 12:53:39 +0000792 cur_p->app0 = STS_CTRL_APP0_IRQONEND;
793 cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
794 XTE_MAX_JUMBO_FRAME_SIZE,
795 DMA_FROM_DEVICE);
796 cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
797 lp->rx_skb[lp->rx_bd_ci] = new_skb;
798
799 lp->rx_bd_ci++;
800 if (lp->rx_bd_ci >= RX_BD_NUM)
801 lp->rx_bd_ci = 0;
802
803 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
804 bdstat = cur_p->app0;
805 }
John Linne44171f2010-04-08 07:08:02 +0000806 lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
Grant Likely92744982009-04-25 12:53:39 +0000807
808 spin_unlock_irqrestore(&lp->rx_lock, flags);
809}
810
811static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
812{
813 struct net_device *ndev = _ndev;
814 struct temac_local *lp = netdev_priv(ndev);
815 unsigned int status;
816
John Linne44171f2010-04-08 07:08:02 +0000817 status = lp->dma_in(lp, TX_IRQ_REG);
818 lp->dma_out(lp, TX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000819
820 if (status & (IRQ_COAL | IRQ_DLY))
821 temac_start_xmit_done(lp->ndev);
822 if (status & 0x080)
823 dev_err(&ndev->dev, "DMA error 0x%x\n", status);
824
825 return IRQ_HANDLED;
826}
827
828static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
829{
830 struct net_device *ndev = _ndev;
831 struct temac_local *lp = netdev_priv(ndev);
832 unsigned int status;
833
834 /* Read and clear the status registers */
John Linne44171f2010-04-08 07:08:02 +0000835 status = lp->dma_in(lp, RX_IRQ_REG);
836 lp->dma_out(lp, RX_IRQ_REG, status);
Grant Likely92744982009-04-25 12:53:39 +0000837
838 if (status & (IRQ_COAL | IRQ_DLY))
839 ll_temac_recv(lp->ndev);
840
841 return IRQ_HANDLED;
842}
843
844static int temac_open(struct net_device *ndev)
845{
846 struct temac_local *lp = netdev_priv(ndev);
Philippe Reynes31abbe342016-07-14 01:48:51 +0200847 struct phy_device *phydev = NULL;
Grant Likely92744982009-04-25 12:53:39 +0000848 int rc;
849
850 dev_dbg(&ndev->dev, "temac_open()\n");
851
852 if (lp->phy_node) {
Philippe Reynes31abbe342016-07-14 01:48:51 +0200853 phydev = of_phy_connect(lp->ndev, lp->phy_node,
854 temac_adjust_link, 0, 0);
855 if (!phydev) {
Grant Likely92744982009-04-25 12:53:39 +0000856 dev_err(lp->dev, "of_phy_connect() failed\n");
857 return -ENODEV;
858 }
859
Philippe Reynes31abbe342016-07-14 01:48:51 +0200860 phy_start(phydev);
Grant Likely92744982009-04-25 12:53:39 +0000861 }
862
Ricardo Ribalda50ec1532011-11-07 23:31:58 +0000863 temac_device_reset(ndev);
864
Grant Likely92744982009-04-25 12:53:39 +0000865 rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
866 if (rc)
867 goto err_tx_irq;
868 rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
869 if (rc)
870 goto err_rx_irq;
871
Grant Likely92744982009-04-25 12:53:39 +0000872 return 0;
873
874 err_rx_irq:
875 free_irq(lp->tx_irq, ndev);
876 err_tx_irq:
Philippe Reynes31abbe342016-07-14 01:48:51 +0200877 if (phydev)
878 phy_disconnect(phydev);
Grant Likely92744982009-04-25 12:53:39 +0000879 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);
Philippe Reynes31abbe342016-07-14 01:48:51 +0200886 struct phy_device *phydev = ndev->phydev;
Grant Likely92744982009-04-25 12:53:39 +0000887
888 dev_dbg(&ndev->dev, "temac_close()\n");
889
890 free_irq(lp->tx_irq, ndev);
891 free_irq(lp->rx_irq, ndev);
892
Philippe Reynes31abbe342016-07-14 01:48:51 +0200893 if (phydev)
894 phy_disconnect(phydev);
Grant Likely92744982009-04-25 12:53:39 +0000895
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
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000918static int temac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
919{
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000920 if (!netif_running(ndev))
921 return -EINVAL;
922
Philippe Reynes31abbe342016-07-14 01:48:51 +0200923 if (!ndev->phydev)
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000924 return -EINVAL;
925
Philippe Reynes31abbe342016-07-14 01:48:51 +0200926 return phy_mii_ioctl(ndev->phydev, rq, cmd);
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000927}
928
Grant Likely92744982009-04-25 12:53:39 +0000929static const struct net_device_ops temac_netdev_ops = {
930 .ndo_open = temac_open,
931 .ndo_stop = temac_stop,
932 .ndo_start_xmit = temac_start_xmit,
Jiri Pirko04e406d2013-01-01 03:30:19 +0000933 .ndo_set_mac_address = temac_set_mac_address,
Denis Kirjanov60eb5fd2010-07-10 11:10:44 +0000934 .ndo_validate_addr = eth_validate_addr,
Ricardo Ribalda8d8bdfe2011-11-07 23:47:45 +0000935 .ndo_do_ioctl = temac_ioctl,
Grant Likely92744982009-04-25 12:53:39 +0000936#ifdef CONFIG_NET_POLL_CONTROLLER
937 .ndo_poll_controller = temac_poll_controller,
938#endif
939};
940
941/* ---------------------------------------------------------------------
942 * SYSFS device attributes
943 */
944static ssize_t temac_show_llink_regs(struct device *dev,
945 struct device_attribute *attr, char *buf)
946{
947 struct net_device *ndev = dev_get_drvdata(dev);
948 struct temac_local *lp = netdev_priv(ndev);
949 int i, len = 0;
950
951 for (i = 0; i < 0x11; i++)
John Linne44171f2010-04-08 07:08:02 +0000952 len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
Grant Likely92744982009-04-25 12:53:39 +0000953 (i % 8) == 7 ? "\n" : " ");
954 len += sprintf(buf + len, "\n");
955
956 return len;
957}
958
959static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
960
961static struct attribute *temac_device_attrs[] = {
962 &dev_attr_llink_regs.attr,
963 NULL,
964};
965
966static const struct attribute_group temac_attr_group = {
967 .attrs = temac_device_attrs,
968};
969
Ricardo9eac2d42011-10-18 21:35:25 +0000970/* ethtool support */
Ricardo9eac2d42011-10-18 21:35:25 +0000971static const struct ethtool_ops temac_ethtool_ops = {
Florian Fainelli16250522016-11-15 10:06:36 -0800972 .nway_reset = phy_ethtool_nway_reset,
Ricardo9eac2d42011-10-18 21:35:25 +0000973 .get_link = ethtool_op_get_link,
Richard Cochranf85e5ea2012-04-03 22:59:30 +0000974 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynese6dab9022016-07-14 01:48:52 +0200975 .get_link_ksettings = phy_ethtool_get_link_ksettings,
976 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Ricardo9eac2d42011-10-18 21:35:25 +0000977};
978
Bill Pemberton06b0e682012-12-03 09:24:08 -0500979static int temac_of_probe(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +0000980{
981 struct device_node *np;
982 struct temac_local *lp;
983 struct net_device *ndev;
984 const void *addr;
Brian Hill23ecc4b2010-05-26 20:44:30 -0700985 __be32 *p;
Tobias Klauser06205472016-12-07 13:25:29 +0100986 int rc = 0;
Grant Likely92744982009-04-25 12:53:39 +0000987
988 /* Init network device structure */
989 ndev = alloc_etherdev(sizeof(*lp));
Joe Perches41de8d42012-01-29 13:47:52 +0000990 if (!ndev)
Grant Likely92744982009-04-25 12:53:39 +0000991 return -ENOMEM;
Joe Perches41de8d42012-01-29 13:47:52 +0000992
Jingoo Han8513fbd2013-05-23 00:52:31 +0000993 platform_set_drvdata(op, ndev);
Grant Likely92744982009-04-25 12:53:39 +0000994 SET_NETDEV_DEV(ndev, &op->dev);
995 ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
Eric Dumazet28e24c62013-12-02 08:51:13 -0800996 ndev->features = NETIF_F_SG;
Grant Likely92744982009-04-25 12:53:39 +0000997 ndev->netdev_ops = &temac_netdev_ops;
Ricardo9eac2d42011-10-18 21:35:25 +0000998 ndev->ethtool_ops = &temac_ethtool_ops;
Grant Likely92744982009-04-25 12:53:39 +0000999#if 0
1000 ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
1001 ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
1002 ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
1003 ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
Patrick McHardyf6469682013-04-19 02:04:27 +00001004 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; /* Transmit VLAN hw accel */
1005 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; /* Receive VLAN hw acceleration */
1006 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; /* Receive VLAN filtering */
Grant Likely92744982009-04-25 12:53:39 +00001007 ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
1008 ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
1009 ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
1010 ndev->features |= NETIF_F_LRO; /* large receive offload */
1011#endif
1012
1013 /* setup temac private info structure */
1014 lp = netdev_priv(ndev);
1015 lp->ndev = ndev;
1016 lp->dev = &op->dev;
1017 lp->options = XTE_OPTION_DEFAULTS;
1018 spin_lock_init(&lp->rx_lock);
1019 mutex_init(&lp->indirect_mutex);
1020
1021 /* map device registers */
Grant Likely61c7a082010-04-13 16:12:29 -07001022 lp->regs = of_iomap(op->dev.of_node, 0);
Grant Likely92744982009-04-25 12:53:39 +00001023 if (!lp->regs) {
1024 dev_err(&op->dev, "could not map temac regs.\n");
Julia Lawall8a0a1f82014-12-29 18:04:36 +01001025 rc = -ENOMEM;
Grant Likely92744982009-04-25 12:53:39 +00001026 goto nodev;
1027 }
1028
Brian Hill23ecc4b2010-05-26 20:44:30 -07001029 /* Setup checksum offload, but default to off if not specified */
1030 lp->temac_features = 0;
1031 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
1032 if (p && be32_to_cpu(*p)) {
1033 lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
1034 /* Can checksum TCP/UDP over IPv4. */
1035 ndev->features |= NETIF_F_IP_CSUM;
1036 }
1037 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
1038 if (p && be32_to_cpu(*p))
1039 lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
1040
Grant Likely92744982009-04-25 12:53:39 +00001041 /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
Grant Likely61c7a082010-04-13 16:12:29 -07001042 np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
Grant Likely92744982009-04-25 12:53:39 +00001043 if (!np) {
1044 dev_err(&op->dev, "could not find DMA node\n");
Julia Lawall8a0a1f82014-12-29 18:04:36 +01001045 rc = -ENODEV;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001046 goto err_iounmap;
Grant Likely92744982009-04-25 12:53:39 +00001047 }
1048
John Linne44171f2010-04-08 07:08:02 +00001049 /* Setup the DMA register accesses, could be DCR or memory mapped */
1050 if (temac_dcr_setup(lp, op, np)) {
1051
1052 /* no DCR in the device tree, try non-DCR */
1053 lp->sdma_regs = of_iomap(np, 0);
1054 if (lp->sdma_regs) {
1055 lp->dma_in = temac_dma_in32;
1056 lp->dma_out = temac_dma_out32;
1057 dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
1058 } else {
1059 dev_err(&op->dev, "unable to map DMA registers\n");
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001060 of_node_put(np);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001061 goto err_iounmap;
John Linne44171f2010-04-08 07:08:02 +00001062 }
Grant Likely92744982009-04-25 12:53:39 +00001063 }
Grant Likely92744982009-04-25 12:53:39 +00001064
1065 lp->rx_irq = irq_of_parse_and_map(np, 0);
1066 lp->tx_irq = irq_of_parse_and_map(np, 1);
Kulikov Vasiliy7cc36f62010-07-08 23:43:20 -07001067
1068 of_node_put(np); /* Finished with the DMA node; drop the reference */
1069
Michal Simek4e68ea22011-12-21 15:42:50 -05001070 if (!lp->rx_irq || !lp->tx_irq) {
Grant Likely92744982009-04-25 12:53:39 +00001071 dev_err(&op->dev, "could not determine irqs\n");
1072 rc = -ENOMEM;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001073 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001074 }
1075
Grant Likely92744982009-04-25 12:53:39 +00001076
1077 /* Retrieve the MAC address */
Tobias Klauser06205472016-12-07 13:25:29 +01001078 addr = of_get_mac_address(op->dev.of_node);
1079 if (!addr) {
Grant Likely92744982009-04-25 12:53:39 +00001080 dev_err(&op->dev, "could not find MAC address\n");
1081 rc = -ENODEV;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001082 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001083 }
Tobias Klauser06205472016-12-07 13:25:29 +01001084 temac_init_mac_address(ndev, addr);
Grant Likely92744982009-04-25 12:53:39 +00001085
Grant Likely61c7a082010-04-13 16:12:29 -07001086 rc = temac_mdio_setup(lp, op->dev.of_node);
Grant Likely92744982009-04-25 12:53:39 +00001087 if (rc)
1088 dev_warn(&op->dev, "error registering MDIO bus\n");
1089
Grant Likely61c7a082010-04-13 16:12:29 -07001090 lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
Grant Likely92744982009-04-25 12:53:39 +00001091 if (lp->phy_node)
Rob Herringf7ce9102017-07-18 16:43:19 -05001092 dev_dbg(lp->dev, "using PHY node %pOF (%p)\n", np, np);
Grant Likely92744982009-04-25 12:53:39 +00001093
1094 /* Add the device attributes */
1095 rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
1096 if (rc) {
1097 dev_err(lp->dev, "Error creating sysfs files\n");
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001098 goto err_iounmap_2;
Grant Likely92744982009-04-25 12:53:39 +00001099 }
1100
1101 rc = register_netdev(lp->ndev);
1102 if (rc) {
1103 dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
1104 goto err_register_ndev;
1105 }
1106
1107 return 0;
1108
1109 err_register_ndev:
1110 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001111 err_iounmap_2:
1112 if (lp->sdma_regs)
1113 iounmap(lp->sdma_regs);
1114 err_iounmap:
1115 iounmap(lp->regs);
Grant Likely92744982009-04-25 12:53:39 +00001116 nodev:
1117 free_netdev(ndev);
1118 ndev = NULL;
1119 return rc;
1120}
1121
Bill Pemberton06b0e682012-12-03 09:24:08 -05001122static int temac_of_remove(struct platform_device *op)
Grant Likely92744982009-04-25 12:53:39 +00001123{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001124 struct net_device *ndev = platform_get_drvdata(op);
Grant Likely92744982009-04-25 12:53:39 +00001125 struct temac_local *lp = netdev_priv(ndev);
1126
1127 temac_mdio_teardown(lp);
1128 unregister_netdev(ndev);
1129 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
Julia Lawall6a3e6aa2014-08-08 12:07:53 +02001130 of_node_put(lp->phy_node);
Grant Likely92744982009-04-25 12:53:39 +00001131 lp->phy_node = NULL;
Denis Kirjanovdfe1e8e2010-07-05 21:44:20 +00001132 iounmap(lp->regs);
1133 if (lp->sdma_regs)
1134 iounmap(lp->sdma_regs);
Grant Likely92744982009-04-25 12:53:39 +00001135 free_netdev(ndev);
1136 return 0;
1137}
1138
Fabian Frederick74847f22015-03-17 19:37:40 +01001139static const struct of_device_id temac_of_match[] = {
Grant Likely92744982009-04-25 12:53:39 +00001140 { .compatible = "xlnx,xps-ll-temac-1.01.b", },
Steven J. Magnanic3b7c122010-02-17 07:14:20 +00001141 { .compatible = "xlnx,xps-ll-temac-2.00.a", },
1142 { .compatible = "xlnx,xps-ll-temac-2.02.a", },
1143 { .compatible = "xlnx,xps-ll-temac-2.03.a", },
Grant Likely92744982009-04-25 12:53:39 +00001144 {},
1145};
1146MODULE_DEVICE_TABLE(of, temac_of_match);
1147
Grant Likely74888762011-02-22 21:05:51 -07001148static struct platform_driver temac_of_driver = {
Grant Likely92744982009-04-25 12:53:39 +00001149 .probe = temac_of_probe,
Bill Pemberton06b0e682012-12-03 09:24:08 -05001150 .remove = temac_of_remove,
Grant Likely92744982009-04-25 12:53:39 +00001151 .driver = {
Grant Likely92744982009-04-25 12:53:39 +00001152 .name = "xilinx_temac",
Grant Likely40182942010-04-13 16:13:02 -07001153 .of_match_table = temac_of_match,
Grant Likely92744982009-04-25 12:53:39 +00001154 },
1155};
1156
Axel Lindb62f682011-11-27 16:44:17 +00001157module_platform_driver(temac_of_driver);
Grant Likely92744982009-04-25 12:53:39 +00001158
1159MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1160MODULE_AUTHOR("Yoshio Kashiwagi");
1161MODULE_LICENSE("GPL");