blob: f11824a078e9e9c7806db1e1d97ee74e5e5c5e79 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5 *
6 * Right now, I am very wasteful with the buffers. I allocate memory
7 * pages and then divide them into 2K frame buffers. This way I know I
8 * have buffers large enough to hold one frame within one buffer descriptor.
9 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
10 * will be much more memory efficient and will easily handle lots of
11 * small packets.
12 *
13 * Much better multiple PHY support by Magnus Damm.
14 * Copyright (c) 2000 Ericsson Radio Systems AB.
15 *
16 * Support for FEC controller of ColdFire processors.
17 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
18 *
19 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
20 * Copyright (c) 2004-2006 Macq Electronique SA.
21 *
22 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
23 */
24
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/string.h>
28#include <linux/pm_runtime.h>
29#include <linux/ptrace.h>
30#include <linux/errno.h>
31#include <linux/ioport.h>
32#include <linux/slab.h>
33#include <linux/interrupt.h>
34#include <linux/delay.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/skbuff.h>
38#include <linux/in.h>
39#include <linux/ip.h>
40#include <net/ip.h>
41#include <net/tso.h>
42#include <linux/tcp.h>
43#include <linux/udp.h>
44#include <linux/icmp.h>
45#include <linux/spinlock.h>
46#include <linux/workqueue.h>
47#include <linux/bitops.h>
48#include <linux/io.h>
49#include <linux/irq.h>
50#include <linux/clk.h>
51#include <linux/crc32.h>
52#include <linux/platform_device.h>
53#include <linux/mdio.h>
54#include <linux/phy.h>
55#include <linux/fec.h>
56#include <linux/of.h>
57#include <linux/of_device.h>
58#include <linux/of_gpio.h>
59#include <linux/of_mdio.h>
60#include <linux/of_net.h>
61#include <linux/regulator/consumer.h>
62#include <linux/if_vlan.h>
63#include <linux/pinctrl/consumer.h>
64#include <linux/prefetch.h>
65#include <linux/mfd/syscon.h>
66#include <linux/regmap.h>
67#include <soc/imx/cpuidle.h>
68
69#include <asm/cacheflush.h>
70
71#include "fec.h"
72
73static void set_multicast_list(struct net_device *ndev);
74static void fec_enet_itr_coal_init(struct net_device *ndev);
75
76#define DRIVER_NAME "fec"
77
78#define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
79
80/* Pause frame feild and FIFO threshold */
81#define FEC_ENET_FCE (1 << 5)
82#define FEC_ENET_RSEM_V 0x84
83#define FEC_ENET_RSFL_V 16
84#define FEC_ENET_RAEM_V 0x8
85#define FEC_ENET_RAFL_V 0x8
86#define FEC_ENET_OPD_V 0xFFF0
87#define FEC_MDIO_PM_TIMEOUT 100 /* ms */
88
89struct fec_devinfo {
90 u32 quirks;
91 u8 stop_gpr_reg;
92 u8 stop_gpr_bit;
93};
94
95static const struct fec_devinfo fec_imx25_info = {
96 .quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
97 FEC_QUIRK_HAS_FRREG,
98};
99
100static const struct fec_devinfo fec_imx27_info = {
101 .quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG,
102};
103
104static const struct fec_devinfo fec_imx28_info = {
105 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
106 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
107 FEC_QUIRK_HAS_FRREG,
108};
109
110static const struct fec_devinfo fec_imx6q_info = {
111 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
112 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
113 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
114 FEC_QUIRK_HAS_RACC,
115 .stop_gpr_reg = 0x34,
116 .stop_gpr_bit = 27,
117};
118
119static const struct fec_devinfo fec_mvf600_info = {
120 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
121};
122
123static const struct fec_devinfo fec_imx6x_info = {
124 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
125 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
126 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
127 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
128 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE,
129};
130
131static const struct fec_devinfo fec_imx6ul_info = {
132 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
133 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
134 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
135 FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
136 FEC_QUIRK_HAS_COALESCE,
137};
138
139static struct platform_device_id fec_devtype[] = {
140 {
141 /* keep it for coldfire */
142 .name = DRIVER_NAME,
143 .driver_data = 0,
144 }, {
145 .name = "imx25-fec",
146 .driver_data = (kernel_ulong_t)&fec_imx25_info,
147 }, {
148 .name = "imx27-fec",
149 .driver_data = (kernel_ulong_t)&fec_imx27_info,
150 }, {
151 .name = "imx28-fec",
152 .driver_data = (kernel_ulong_t)&fec_imx28_info,
153 }, {
154 .name = "imx6q-fec",
155 .driver_data = (kernel_ulong_t)&fec_imx6q_info,
156 }, {
157 .name = "mvf600-fec",
158 .driver_data = (kernel_ulong_t)&fec_mvf600_info,
159 }, {
160 .name = "imx6sx-fec",
161 .driver_data = (kernel_ulong_t)&fec_imx6x_info,
162 }, {
163 .name = "imx6ul-fec",
164 .driver_data = (kernel_ulong_t)&fec_imx6ul_info,
165 }, {
166 /* sentinel */
167 }
168};
169MODULE_DEVICE_TABLE(platform, fec_devtype);
170
171enum imx_fec_type {
172 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
173 IMX27_FEC, /* runs on i.mx27/35/51 */
174 IMX28_FEC,
175 IMX6Q_FEC,
176 MVF600_FEC,
177 IMX6SX_FEC,
178 IMX6UL_FEC,
179};
180
181static const struct of_device_id fec_dt_ids[] = {
182 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
183 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
184 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
185 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
186 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
187 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
188 { .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
189 { /* sentinel */ }
190};
191MODULE_DEVICE_TABLE(of, fec_dt_ids);
192
193static unsigned char macaddr[ETH_ALEN];
194module_param_array(macaddr, byte, NULL, 0);
195MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
196
197#if defined(CONFIG_M5272)
198/*
199 * Some hardware gets it MAC address out of local flash memory.
200 * if this is non-zero then assume it is the address to get MAC from.
201 */
202#if defined(CONFIG_NETtel)
203#define FEC_FLASHMAC 0xf0006006
204#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
205#define FEC_FLASHMAC 0xf0006000
206#elif defined(CONFIG_CANCam)
207#define FEC_FLASHMAC 0xf0020000
208#elif defined (CONFIG_M5272C3)
209#define FEC_FLASHMAC (0xffe04000 + 4)
210#elif defined(CONFIG_MOD5272)
211#define FEC_FLASHMAC 0xffc0406b
212#else
213#define FEC_FLASHMAC 0
214#endif
215#endif /* CONFIG_M5272 */
216
217/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
218 *
219 * 2048 byte skbufs are allocated. However, alignment requirements
220 * varies between FEC variants. Worst case is 64, so round down by 64.
221 */
222#define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64))
223#define PKT_MINBUF_SIZE 64
224
225/* FEC receive acceleration */
226#define FEC_RACC_IPDIS BIT(1)
227#define FEC_RACC_PRODIS BIT(2)
228#define FEC_RACC_SHIFT16 BIT(7)
229#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
230
231/* MIB Control Register */
232#define FEC_MIB_CTRLSTAT_DISABLE BIT(31)
233
234/*
235 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
236 * size bits. Other FEC hardware does not, so we need to take that into
237 * account when setting it.
238 */
239#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
240 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
241 defined(CONFIG_ARM64)
242#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
243#else
244#define OPT_FRAME_SIZE 0
245#endif
246
247/* FEC MII MMFR bits definition */
248#define FEC_MMFR_ST (1 << 30)
249#define FEC_MMFR_ST_C45 (0)
250#define FEC_MMFR_OP_READ (2 << 28)
251#define FEC_MMFR_OP_READ_C45 (3 << 28)
252#define FEC_MMFR_OP_WRITE (1 << 28)
253#define FEC_MMFR_OP_ADDR_WRITE (0)
254#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
255#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
256#define FEC_MMFR_TA (2 << 16)
257#define FEC_MMFR_DATA(v) (v & 0xffff)
258/* FEC ECR bits definition */
259#define FEC_ECR_RESET BIT(0)
260#define FEC_ECR_ETHEREN BIT(1)
261#define FEC_ECR_MAGICEN BIT(2)
262#define FEC_ECR_SLEEP BIT(3)
263#define FEC_ECR_EN1588 BIT(4)
264#define FEC_ECR_BYTESWP BIT(8)
265/* FEC RCR bits definition */
266#define FEC_RCR_LOOP BIT(0)
267#define FEC_RCR_HALFDPX BIT(1)
268#define FEC_RCR_MII BIT(2)
269#define FEC_RCR_PROMISC BIT(3)
270#define FEC_RCR_BC_REJ BIT(4)
271#define FEC_RCR_FLOWCTL BIT(5)
272#define FEC_RCR_RMII BIT(8)
273#define FEC_RCR_10BASET BIT(9)
274/* TX WMARK bits */
275#define FEC_TXWMRK_STRFWD BIT(8)
276
277#define FEC_MII_TIMEOUT 30000 /* us */
278
279/* Transmitter timeout */
280#define TX_TIMEOUT (2 * HZ)
281
282#define FEC_PAUSE_FLAG_AUTONEG 0x1
283#define FEC_PAUSE_FLAG_ENABLE 0x2
284#define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
285#define FEC_WOL_FLAG_ENABLE (0x1 << 1)
286#define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
287
288#define COPYBREAK_DEFAULT 256
289
290/* Max number of allowed TCP segments for software TSO */
291#define FEC_MAX_TSO_SEGS 100
292#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
293
294#define IS_TSO_HEADER(txq, addr) \
295 ((addr >= txq->tso_hdrs_dma) && \
296 (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
297
298static int mii_cnt;
299
300static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
301 struct bufdesc_prop *bd)
302{
303 return (bdp >= bd->last) ? bd->base
304 : (struct bufdesc *)(((void *)bdp) + bd->dsize);
305}
306
307static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
308 struct bufdesc_prop *bd)
309{
310 return (bdp <= bd->base) ? bd->last
311 : (struct bufdesc *)(((void *)bdp) - bd->dsize);
312}
313
314static int fec_enet_get_bd_index(struct bufdesc *bdp,
315 struct bufdesc_prop *bd)
316{
317 return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
318}
319
320static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
321{
322 int entries;
323
324 entries = (((const char *)txq->dirty_tx -
325 (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
326
327 return entries >= 0 ? entries : entries + txq->bd.ring_size;
328}
329
330static void swap_buffer(void *bufaddr, int len)
331{
332 int i;
333 unsigned int *buf = bufaddr;
334
335 for (i = 0; i < len; i += 4, buf++)
336 swab32s(buf);
337}
338
339static void swap_buffer2(void *dst_buf, void *src_buf, int len)
340{
341 int i;
342 unsigned int *src = src_buf;
343 unsigned int *dst = dst_buf;
344
345 for (i = 0; i < len; i += 4, src++, dst++)
346 *dst = swab32p(src);
347}
348
349static void fec_dump(struct net_device *ndev)
350{
351 struct fec_enet_private *fep = netdev_priv(ndev);
352 struct bufdesc *bdp;
353 struct fec_enet_priv_tx_q *txq;
354 int index = 0;
355
356 netdev_info(ndev, "TX ring dump\n");
357 pr_info("Nr SC addr len SKB\n");
358
359 txq = fep->tx_queue[0];
360 bdp = txq->bd.base;
361
362 do {
363 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
364 index,
365 bdp == txq->bd.cur ? 'S' : ' ',
366 bdp == txq->dirty_tx ? 'H' : ' ',
367 fec16_to_cpu(bdp->cbd_sc),
368 fec32_to_cpu(bdp->cbd_bufaddr),
369 fec16_to_cpu(bdp->cbd_datlen),
370 txq->tx_skbuff[index]);
371 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
372 index++;
373 } while (bdp != txq->bd.base);
374}
375
376static inline bool is_ipv4_pkt(struct sk_buff *skb)
377{
378 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
379}
380
381static int
382fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
383{
384 /* Only run for packets requiring a checksum. */
385 if (skb->ip_summed != CHECKSUM_PARTIAL)
386 return 0;
387
388 if (unlikely(skb_cow_head(skb, 0)))
389 return -1;
390
391 if (is_ipv4_pkt(skb))
392 ip_hdr(skb)->check = 0;
393 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
394
395 return 0;
396}
397
398static struct bufdesc *
399fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
400 struct sk_buff *skb,
401 struct net_device *ndev)
402{
403 struct fec_enet_private *fep = netdev_priv(ndev);
404 struct bufdesc *bdp = txq->bd.cur;
405 struct bufdesc_ex *ebdp;
406 int nr_frags = skb_shinfo(skb)->nr_frags;
407 int frag, frag_len;
408 unsigned short status;
409 unsigned int estatus = 0;
410 skb_frag_t *this_frag;
411 unsigned int index;
412 void *bufaddr;
413 dma_addr_t addr;
414 int i;
415
416 for (frag = 0; frag < nr_frags; frag++) {
417 this_frag = &skb_shinfo(skb)->frags[frag];
418 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
419 ebdp = (struct bufdesc_ex *)bdp;
420
421 status = fec16_to_cpu(bdp->cbd_sc);
422 status &= ~BD_ENET_TX_STATS;
423 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
424 frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
425
426 /* Handle the last BD specially */
427 if (frag == nr_frags - 1) {
428 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
429 if (fep->bufdesc_ex) {
430 estatus |= BD_ENET_TX_INT;
431 if (unlikely(skb_shinfo(skb)->tx_flags &
432 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
433 estatus |= BD_ENET_TX_TS;
434 }
435 }
436
437 if (fep->bufdesc_ex) {
438 if (fep->quirks & FEC_QUIRK_HAS_AVB)
439 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
440 if (skb->ip_summed == CHECKSUM_PARTIAL)
441 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
442 ebdp->cbd_bdu = 0;
443 ebdp->cbd_esc = cpu_to_fec32(estatus);
444 }
445
446 bufaddr = skb_frag_address(this_frag);
447
448 index = fec_enet_get_bd_index(bdp, &txq->bd);
449 if (((unsigned long) bufaddr) & fep->tx_align ||
450 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
451 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
452 bufaddr = txq->tx_bounce[index];
453
454 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
455 swap_buffer(bufaddr, frag_len);
456 }
457
458 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
459 DMA_TO_DEVICE);
460 if (dma_mapping_error(&fep->pdev->dev, addr)) {
461 if (net_ratelimit())
462 netdev_err(ndev, "Tx DMA memory map failed\n");
463 goto dma_mapping_error;
464 }
465
466 bdp->cbd_bufaddr = cpu_to_fec32(addr);
467 bdp->cbd_datlen = cpu_to_fec16(frag_len);
468 /* Make sure the updates to rest of the descriptor are
469 * performed before transferring ownership.
470 */
471 wmb();
472 bdp->cbd_sc = cpu_to_fec16(status);
473 }
474
475 return bdp;
476dma_mapping_error:
477 bdp = txq->bd.cur;
478 for (i = 0; i < frag; i++) {
479 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
480 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
481 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
482 }
483 return ERR_PTR(-ENOMEM);
484}
485
486static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
487 struct sk_buff *skb, struct net_device *ndev)
488{
489 struct fec_enet_private *fep = netdev_priv(ndev);
490 int nr_frags = skb_shinfo(skb)->nr_frags;
491 struct bufdesc *bdp, *last_bdp;
492 void *bufaddr;
493 dma_addr_t addr;
494 unsigned short status;
495 unsigned short buflen;
496 unsigned int estatus = 0;
497 unsigned int index;
498 int entries_free;
499
500 entries_free = fec_enet_get_free_txdesc_num(txq);
501 if (entries_free < MAX_SKB_FRAGS + 1) {
502 dev_kfree_skb_any(skb);
503 if (net_ratelimit())
504 netdev_err(ndev, "NOT enough BD for SG!\n");
505 return NETDEV_TX_OK;
506 }
507
508 /* Protocol checksum off-load for TCP and UDP. */
509 if (fec_enet_clear_csum(skb, ndev)) {
510 dev_kfree_skb_any(skb);
511 return NETDEV_TX_OK;
512 }
513
514 /* Fill in a Tx ring entry */
515 bdp = txq->bd.cur;
516 last_bdp = bdp;
517 status = fec16_to_cpu(bdp->cbd_sc);
518 status &= ~BD_ENET_TX_STATS;
519
520 /* Set buffer length and buffer pointer */
521 bufaddr = skb->data;
522 buflen = skb_headlen(skb);
523
524 index = fec_enet_get_bd_index(bdp, &txq->bd);
525 if (((unsigned long) bufaddr) & fep->tx_align ||
526 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
527 memcpy(txq->tx_bounce[index], skb->data, buflen);
528 bufaddr = txq->tx_bounce[index];
529
530 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
531 swap_buffer(bufaddr, buflen);
532 }
533
534 /* Push the data cache so the CPM does not get stale memory data. */
535 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
536 if (dma_mapping_error(&fep->pdev->dev, addr)) {
537 dev_kfree_skb_any(skb);
538 if (net_ratelimit())
539 netdev_err(ndev, "Tx DMA memory map failed\n");
540 return NETDEV_TX_OK;
541 }
542
543 if (nr_frags) {
544 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
545 if (IS_ERR(last_bdp)) {
546 dma_unmap_single(&fep->pdev->dev, addr,
547 buflen, DMA_TO_DEVICE);
548 dev_kfree_skb_any(skb);
549 return NETDEV_TX_OK;
550 }
551 } else {
552 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
553 if (fep->bufdesc_ex) {
554 estatus = BD_ENET_TX_INT;
555 if (unlikely(skb_shinfo(skb)->tx_flags &
556 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
557 estatus |= BD_ENET_TX_TS;
558 }
559 }
560 bdp->cbd_bufaddr = cpu_to_fec32(addr);
561 bdp->cbd_datlen = cpu_to_fec16(buflen);
562
563 if (fep->bufdesc_ex) {
564
565 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
566
567 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
568 fep->hwts_tx_en))
569 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
570
571 if (fep->quirks & FEC_QUIRK_HAS_AVB)
572 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
573
574 if (skb->ip_summed == CHECKSUM_PARTIAL)
575 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
576
577 ebdp->cbd_bdu = 0;
578 ebdp->cbd_esc = cpu_to_fec32(estatus);
579 }
580
581 index = fec_enet_get_bd_index(last_bdp, &txq->bd);
582 /* Save skb pointer */
583 txq->tx_skbuff[index] = skb;
584
585 /* Make sure the updates to rest of the descriptor are performed before
586 * transferring ownership.
587 */
588 wmb();
589
590 /* Send it on its way. Tell FEC it's ready, interrupt when done,
591 * it's the last BD of the frame, and to put the CRC on the end.
592 */
593 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
594 bdp->cbd_sc = cpu_to_fec16(status);
595
596 /* If this was the last BD in the ring, start at the beginning again. */
597 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
598
599 skb_tx_timestamp(skb);
600
601 /* Make sure the update to bdp and tx_skbuff are performed before
602 * txq->bd.cur.
603 */
604 wmb();
605 txq->bd.cur = bdp;
606
607 /* Trigger transmission start */
608 writel(0, txq->bd.reg_desc_active);
609
610 return 0;
611}
612
613static int
614fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
615 struct net_device *ndev,
616 struct bufdesc *bdp, int index, char *data,
617 int size, bool last_tcp, bool is_last)
618{
619 struct fec_enet_private *fep = netdev_priv(ndev);
620 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
621 unsigned short status;
622 unsigned int estatus = 0;
623 dma_addr_t addr;
624
625 status = fec16_to_cpu(bdp->cbd_sc);
626 status &= ~BD_ENET_TX_STATS;
627
628 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
629
630 if (((unsigned long) data) & fep->tx_align ||
631 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
632 memcpy(txq->tx_bounce[index], data, size);
633 data = txq->tx_bounce[index];
634
635 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
636 swap_buffer(data, size);
637 }
638
639 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
640 if (dma_mapping_error(&fep->pdev->dev, addr)) {
641 dev_kfree_skb_any(skb);
642 if (net_ratelimit())
643 netdev_err(ndev, "Tx DMA memory map failed\n");
644 return NETDEV_TX_OK;
645 }
646
647 bdp->cbd_datlen = cpu_to_fec16(size);
648 bdp->cbd_bufaddr = cpu_to_fec32(addr);
649
650 if (fep->bufdesc_ex) {
651 if (fep->quirks & FEC_QUIRK_HAS_AVB)
652 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
653 if (skb->ip_summed == CHECKSUM_PARTIAL)
654 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
655 ebdp->cbd_bdu = 0;
656 ebdp->cbd_esc = cpu_to_fec32(estatus);
657 }
658
659 /* Handle the last BD specially */
660 if (last_tcp)
661 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
662 if (is_last) {
663 status |= BD_ENET_TX_INTR;
664 if (fep->bufdesc_ex)
665 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
666 }
667
668 bdp->cbd_sc = cpu_to_fec16(status);
669
670 return 0;
671}
672
673static int
674fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
675 struct sk_buff *skb, struct net_device *ndev,
676 struct bufdesc *bdp, int index)
677{
678 struct fec_enet_private *fep = netdev_priv(ndev);
679 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
680 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
681 void *bufaddr;
682 unsigned long dmabuf;
683 unsigned short status;
684 unsigned int estatus = 0;
685
686 status = fec16_to_cpu(bdp->cbd_sc);
687 status &= ~BD_ENET_TX_STATS;
688 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
689
690 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
691 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
692 if (((unsigned long)bufaddr) & fep->tx_align ||
693 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
694 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
695 bufaddr = txq->tx_bounce[index];
696
697 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
698 swap_buffer(bufaddr, hdr_len);
699
700 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
701 hdr_len, DMA_TO_DEVICE);
702 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
703 dev_kfree_skb_any(skb);
704 if (net_ratelimit())
705 netdev_err(ndev, "Tx DMA memory map failed\n");
706 return NETDEV_TX_OK;
707 }
708 }
709
710 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
711 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
712
713 if (fep->bufdesc_ex) {
714 if (fep->quirks & FEC_QUIRK_HAS_AVB)
715 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
716 if (skb->ip_summed == CHECKSUM_PARTIAL)
717 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
718 ebdp->cbd_bdu = 0;
719 ebdp->cbd_esc = cpu_to_fec32(estatus);
720 }
721
722 bdp->cbd_sc = cpu_to_fec16(status);
723
724 return 0;
725}
726
727static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
728 struct sk_buff *skb,
729 struct net_device *ndev)
730{
731 struct fec_enet_private *fep = netdev_priv(ndev);
732 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
733 int total_len, data_left;
734 struct bufdesc *bdp = txq->bd.cur;
735 struct tso_t tso;
736 unsigned int index = 0;
737 int ret;
738
739 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
740 dev_kfree_skb_any(skb);
741 if (net_ratelimit())
742 netdev_err(ndev, "NOT enough BD for TSO!\n");
743 return NETDEV_TX_OK;
744 }
745
746 /* Protocol checksum off-load for TCP and UDP. */
747 if (fec_enet_clear_csum(skb, ndev)) {
748 dev_kfree_skb_any(skb);
749 return NETDEV_TX_OK;
750 }
751
752 /* Initialize the TSO handler, and prepare the first payload */
753 tso_start(skb, &tso);
754
755 total_len = skb->len - hdr_len;
756 while (total_len > 0) {
757 char *hdr;
758
759 index = fec_enet_get_bd_index(bdp, &txq->bd);
760 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
761 total_len -= data_left;
762
763 /* prepare packet headers: MAC + IP + TCP */
764 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
765 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
766 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
767 if (ret)
768 goto err_release;
769
770 while (data_left > 0) {
771 int size;
772
773 size = min_t(int, tso.size, data_left);
774 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
775 index = fec_enet_get_bd_index(bdp, &txq->bd);
776 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
777 bdp, index,
778 tso.data, size,
779 size == data_left,
780 total_len == 0);
781 if (ret)
782 goto err_release;
783
784 data_left -= size;
785 tso_build_data(skb, &tso, size);
786 }
787
788 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
789 }
790
791 /* Save skb pointer */
792 txq->tx_skbuff[index] = skb;
793
794 skb_tx_timestamp(skb);
795 txq->bd.cur = bdp;
796
797 /* Trigger transmission start */
798 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
799 !readl(txq->bd.reg_desc_active) ||
800 !readl(txq->bd.reg_desc_active) ||
801 !readl(txq->bd.reg_desc_active) ||
802 !readl(txq->bd.reg_desc_active))
803 writel(0, txq->bd.reg_desc_active);
804
805 return 0;
806
807err_release:
808 /* TODO: Release all used data descriptors for TSO */
809 return ret;
810}
811
812static netdev_tx_t
813fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
814{
815 struct fec_enet_private *fep = netdev_priv(ndev);
816 int entries_free;
817 unsigned short queue;
818 struct fec_enet_priv_tx_q *txq;
819 struct netdev_queue *nq;
820 int ret;
821
822 queue = skb_get_queue_mapping(skb);
823 txq = fep->tx_queue[queue];
824 nq = netdev_get_tx_queue(ndev, queue);
825
826 if (skb_is_gso(skb))
827 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
828 else
829 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
830 if (ret)
831 return ret;
832
833 entries_free = fec_enet_get_free_txdesc_num(txq);
834 if (entries_free <= txq->tx_stop_threshold)
835 netif_tx_stop_queue(nq);
836
837 return NETDEV_TX_OK;
838}
839
840/* Init RX & TX buffer descriptors
841 */
842static void fec_enet_bd_init(struct net_device *dev)
843{
844 struct fec_enet_private *fep = netdev_priv(dev);
845 struct fec_enet_priv_tx_q *txq;
846 struct fec_enet_priv_rx_q *rxq;
847 struct bufdesc *bdp;
848 unsigned int i;
849 unsigned int q;
850
851 for (q = 0; q < fep->num_rx_queues; q++) {
852 /* Initialize the receive buffer descriptors. */
853 rxq = fep->rx_queue[q];
854 bdp = rxq->bd.base;
855
856 for (i = 0; i < rxq->bd.ring_size; i++) {
857
858 /* Initialize the BD for every fragment in the page. */
859 if (bdp->cbd_bufaddr)
860 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
861 else
862 bdp->cbd_sc = cpu_to_fec16(0);
863 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
864 }
865
866 /* Set the last buffer to wrap */
867 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
868 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
869
870 rxq->bd.cur = rxq->bd.base;
871 }
872
873 for (q = 0; q < fep->num_tx_queues; q++) {
874 /* ...and the same for transmit */
875 txq = fep->tx_queue[q];
876 bdp = txq->bd.base;
877 txq->bd.cur = bdp;
878
879 for (i = 0; i < txq->bd.ring_size; i++) {
880 /* Initialize the BD for every fragment in the page. */
881 bdp->cbd_sc = cpu_to_fec16(0);
882 if (bdp->cbd_bufaddr &&
883 !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
884 dma_unmap_single(&fep->pdev->dev,
885 fec32_to_cpu(bdp->cbd_bufaddr),
886 fec16_to_cpu(bdp->cbd_datlen),
887 DMA_TO_DEVICE);
888 if (txq->tx_skbuff[i]) {
889 dev_kfree_skb_any(txq->tx_skbuff[i]);
890 txq->tx_skbuff[i] = NULL;
891 }
892 bdp->cbd_bufaddr = cpu_to_fec32(0);
893 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
894 }
895
896 /* Set the last buffer to wrap */
897 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
898 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
899 txq->dirty_tx = bdp;
900 }
901}
902
903static void fec_enet_active_rxring(struct net_device *ndev)
904{
905 struct fec_enet_private *fep = netdev_priv(ndev);
906 int i;
907
908 for (i = 0; i < fep->num_rx_queues; i++)
909 writel(0, fep->rx_queue[i]->bd.reg_desc_active);
910}
911
912static void fec_enet_enable_ring(struct net_device *ndev)
913{
914 struct fec_enet_private *fep = netdev_priv(ndev);
915 struct fec_enet_priv_tx_q *txq;
916 struct fec_enet_priv_rx_q *rxq;
917 int i;
918
919 for (i = 0; i < fep->num_rx_queues; i++) {
920 rxq = fep->rx_queue[i];
921 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
922 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
923
924 /* enable DMA1/2 */
925 if (i)
926 writel(RCMR_MATCHEN | RCMR_CMP(i),
927 fep->hwp + FEC_RCMR(i));
928 }
929
930 for (i = 0; i < fep->num_tx_queues; i++) {
931 txq = fep->tx_queue[i];
932 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
933
934 /* enable DMA1/2 */
935 if (i)
936 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
937 fep->hwp + FEC_DMA_CFG(i));
938 }
939}
940
941static void fec_enet_reset_skb(struct net_device *ndev)
942{
943 struct fec_enet_private *fep = netdev_priv(ndev);
944 struct fec_enet_priv_tx_q *txq;
945 int i, j;
946
947 for (i = 0; i < fep->num_tx_queues; i++) {
948 txq = fep->tx_queue[i];
949
950 for (j = 0; j < txq->bd.ring_size; j++) {
951 if (txq->tx_skbuff[j]) {
952 dev_kfree_skb_any(txq->tx_skbuff[j]);
953 txq->tx_skbuff[j] = NULL;
954 }
955 }
956 }
957}
958
959/*
960 * This function is called to start or restart the FEC during a link
961 * change, transmit timeout, or to reconfigure the FEC. The network
962 * packet processing for this device must be stopped before this call.
963 */
964static void
965fec_restart(struct net_device *ndev)
966{
967 struct fec_enet_private *fep = netdev_priv(ndev);
968 u32 val;
969 u32 temp_mac[2];
970 u32 rcntl = OPT_FRAME_SIZE | 0x04;
971 u32 ecntl = FEC_ECR_ETHEREN;
972
973 /* Whack a reset. We should wait for this.
974 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
975 * instead of reset MAC itself.
976 */
977 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
978 writel(0, fep->hwp + FEC_ECNTRL);
979 } else {
980 writel(1, fep->hwp + FEC_ECNTRL);
981 udelay(10);
982 }
983
984 /*
985 * enet-mac reset will reset mac address registers too,
986 * so need to reconfigure it.
987 */
988 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
989 writel((__force u32)cpu_to_be32(temp_mac[0]),
990 fep->hwp + FEC_ADDR_LOW);
991 writel((__force u32)cpu_to_be32(temp_mac[1]),
992 fep->hwp + FEC_ADDR_HIGH);
993
994 /* Clear any outstanding interrupt. */
995 writel(0xffffffff, fep->hwp + FEC_IEVENT);
996
997 fec_enet_bd_init(ndev);
998
999 fec_enet_enable_ring(ndev);
1000
1001 /* Reset tx SKB buffers. */
1002 fec_enet_reset_skb(ndev);
1003
1004 /* Enable MII mode */
1005 if (fep->full_duplex == DUPLEX_FULL) {
1006 /* FD enable */
1007 writel(0x04, fep->hwp + FEC_X_CNTRL);
1008 } else {
1009 /* No Rcv on Xmit */
1010 rcntl |= 0x02;
1011 writel(0x0, fep->hwp + FEC_X_CNTRL);
1012 }
1013
1014 /* Set MII speed */
1015 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1016
1017#if !defined(CONFIG_M5272)
1018 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
1019 val = readl(fep->hwp + FEC_RACC);
1020 /* align IP header */
1021 val |= FEC_RACC_SHIFT16;
1022 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
1023 /* set RX checksum */
1024 val |= FEC_RACC_OPTIONS;
1025 else
1026 val &= ~FEC_RACC_OPTIONS;
1027 writel(val, fep->hwp + FEC_RACC);
1028 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
1029 }
1030#endif
1031
1032 /*
1033 * The phy interface and speed need to get configured
1034 * differently on enet-mac.
1035 */
1036 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1037 /* Enable flow control and length check */
1038 rcntl |= 0x40000000 | 0x00000020;
1039
1040 /* RGMII, RMII or MII */
1041 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
1042 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1043 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1044 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1045 rcntl |= (1 << 6);
1046 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1047 rcntl |= FEC_RCR_RMII;
1048 else
1049 rcntl &= ~FEC_RCR_RMII;
1050
1051 /* 1G, 100M or 10M */
1052 if (ndev->phydev) {
1053 if (ndev->phydev->speed == SPEED_1000)
1054 ecntl |= (1 << 5);
1055 else if (ndev->phydev->speed == SPEED_100)
1056 rcntl &= ~FEC_RCR_10BASET;
1057 else
1058 rcntl |= FEC_RCR_10BASET;
1059 }
1060 } else {
1061#ifdef FEC_MIIGSK_ENR
1062 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1063 u32 cfgr;
1064 /* disable the gasket and wait */
1065 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1066 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1067 udelay(1);
1068
1069 /*
1070 * configure the gasket:
1071 * RMII, 50 MHz, no loopback, no echo
1072 * MII, 25 MHz, no loopback, no echo
1073 */
1074 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1075 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1076 if (ndev->phydev && ndev->phydev->speed == SPEED_10)
1077 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1078 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1079
1080 /* re-enable the gasket */
1081 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1082 }
1083#endif
1084 }
1085
1086#if !defined(CONFIG_M5272)
1087 /* enable pause frame*/
1088 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1089 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1090 ndev->phydev && ndev->phydev->pause)) {
1091 rcntl |= FEC_ENET_FCE;
1092
1093 /* set FIFO threshold parameter to reduce overrun */
1094 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1095 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1096 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1097 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1098
1099 /* OPD */
1100 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1101 } else {
1102 rcntl &= ~FEC_ENET_FCE;
1103 }
1104#endif /* !defined(CONFIG_M5272) */
1105
1106 writel(rcntl, fep->hwp + FEC_R_CNTRL);
1107
1108 /* Setup multicast filter. */
1109 set_multicast_list(ndev);
1110#ifndef CONFIG_M5272
1111 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1112 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1113#endif
1114
1115 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1116 /* enable ENET endian swap */
1117 ecntl |= FEC_ECR_BYTESWP;
1118 /* enable ENET store and forward mode */
1119 writel(FEC_TXWMRK_STRFWD, fep->hwp + FEC_X_WMRK);
1120 }
1121
1122 if (fep->bufdesc_ex)
1123 ecntl |= FEC_ECR_EN1588;
1124
1125#ifndef CONFIG_M5272
1126 /* Enable the MIB statistic event counters */
1127 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
1128#endif
1129
1130 /* And last, enable the transmit and receive processing */
1131 writel(ecntl, fep->hwp + FEC_ECNTRL);
1132 fec_enet_active_rxring(ndev);
1133
1134 if (fep->bufdesc_ex)
1135 fec_ptp_start_cyclecounter(ndev);
1136
1137 /* Enable interrupts we wish to service */
1138 if (fep->link)
1139 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1140 else
1141 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1142
1143 /* Init the interrupt coalescing */
1144 fec_enet_itr_coal_init(ndev);
1145
1146}
1147
1148static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
1149{
1150 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1151 struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
1152
1153 if (stop_gpr->gpr) {
1154 if (enabled)
1155 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1156 BIT(stop_gpr->bit),
1157 BIT(stop_gpr->bit));
1158 else
1159 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1160 BIT(stop_gpr->bit), 0);
1161 } else if (pdata && pdata->sleep_mode_enable) {
1162 pdata->sleep_mode_enable(enabled);
1163 }
1164}
1165
1166static void
1167fec_stop(struct net_device *ndev)
1168{
1169 struct fec_enet_private *fep = netdev_priv(ndev);
1170 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & FEC_RCR_RMII;
1171 u32 val;
1172
1173 /* We cannot expect a graceful transmit stop without link !!! */
1174 if (fep->link) {
1175 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1176 udelay(10);
1177 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
1178 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1179 }
1180
1181 /* Whack a reset. We should wait for this.
1182 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1183 * instead of reset MAC itself.
1184 */
1185 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1186 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1187 writel(0, fep->hwp + FEC_ECNTRL);
1188 } else {
1189 writel(FEC_ECR_RESET, fep->hwp + FEC_ECNTRL);
1190 udelay(10);
1191 }
1192 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1193 } else {
1194 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1195 val = readl(fep->hwp + FEC_ECNTRL);
1196 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1197 writel(val, fep->hwp + FEC_ECNTRL);
1198 fec_enet_stop_mode(fep, true);
1199 }
1200 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1201
1202 /* We have to keep ENET enabled to have MII interrupt stay working */
1203 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1204 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1205 writel(FEC_ECR_ETHEREN, fep->hwp + FEC_ECNTRL);
1206 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1207 }
1208
1209 if (fep->bufdesc_ex) {
1210 val = readl(fep->hwp + FEC_ECNTRL);
1211 val |= FEC_ECR_EN1588;
1212 writel(val, fep->hwp + FEC_ECNTRL);
1213 }
1214}
1215
1216static void
1217fec_timeout(struct net_device *ndev)
1218{
1219 struct fec_enet_private *fep = netdev_priv(ndev);
1220
1221 fec_dump(ndev);
1222
1223 ndev->stats.tx_errors++;
1224
1225 schedule_work(&fep->tx_timeout_work);
1226}
1227
1228static void fec_enet_timeout_work(struct work_struct *work)
1229{
1230 struct fec_enet_private *fep =
1231 container_of(work, struct fec_enet_private, tx_timeout_work);
1232 struct net_device *ndev = fep->netdev;
1233
1234 rtnl_lock();
1235 if (netif_device_present(ndev) || netif_running(ndev)) {
1236 napi_disable(&fep->napi);
1237 netif_tx_lock_bh(ndev);
1238 fec_restart(ndev);
1239 netif_tx_wake_all_queues(ndev);
1240 netif_tx_unlock_bh(ndev);
1241 napi_enable(&fep->napi);
1242 }
1243 rtnl_unlock();
1244}
1245
1246static void
1247fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1248 struct skb_shared_hwtstamps *hwtstamps)
1249{
1250 unsigned long flags;
1251 u64 ns;
1252
1253 spin_lock_irqsave(&fep->tmreg_lock, flags);
1254 ns = timecounter_cyc2time(&fep->tc, ts);
1255 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1256
1257 memset(hwtstamps, 0, sizeof(*hwtstamps));
1258 hwtstamps->hwtstamp = ns_to_ktime(ns);
1259}
1260
1261static void
1262fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1263{
1264 struct fec_enet_private *fep;
1265 struct bufdesc *bdp;
1266 unsigned short status;
1267 struct sk_buff *skb;
1268 struct fec_enet_priv_tx_q *txq;
1269 struct netdev_queue *nq;
1270 int index = 0;
1271 int entries_free;
1272
1273 fep = netdev_priv(ndev);
1274
1275 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1276
1277 txq = fep->tx_queue[queue_id];
1278 /* get next bdp of dirty_tx */
1279 nq = netdev_get_tx_queue(ndev, queue_id);
1280 bdp = txq->dirty_tx;
1281
1282 /* get next bdp of dirty_tx */
1283 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1284
1285 while (bdp != READ_ONCE(txq->bd.cur)) {
1286 /* Order the load of bd.cur and cbd_sc */
1287 rmb();
1288 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1289 if (status & BD_ENET_TX_READY)
1290 break;
1291
1292 index = fec_enet_get_bd_index(bdp, &txq->bd);
1293
1294 skb = txq->tx_skbuff[index];
1295 txq->tx_skbuff[index] = NULL;
1296 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1297 dma_unmap_single(&fep->pdev->dev,
1298 fec32_to_cpu(bdp->cbd_bufaddr),
1299 fec16_to_cpu(bdp->cbd_datlen),
1300 DMA_TO_DEVICE);
1301 bdp->cbd_bufaddr = cpu_to_fec32(0);
1302 if (!skb)
1303 goto skb_done;
1304
1305 /* Check for errors. */
1306 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1307 BD_ENET_TX_RL | BD_ENET_TX_UN |
1308 BD_ENET_TX_CSL)) {
1309 ndev->stats.tx_errors++;
1310 if (status & BD_ENET_TX_HB) /* No heartbeat */
1311 ndev->stats.tx_heartbeat_errors++;
1312 if (status & BD_ENET_TX_LC) /* Late collision */
1313 ndev->stats.tx_window_errors++;
1314 if (status & BD_ENET_TX_RL) /* Retrans limit */
1315 ndev->stats.tx_aborted_errors++;
1316 if (status & BD_ENET_TX_UN) /* Underrun */
1317 ndev->stats.tx_fifo_errors++;
1318 if (status & BD_ENET_TX_CSL) /* Carrier lost */
1319 ndev->stats.tx_carrier_errors++;
1320 } else {
1321 ndev->stats.tx_packets++;
1322 ndev->stats.tx_bytes += skb->len;
1323 }
1324
1325 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1326 fep->bufdesc_ex) {
1327 struct skb_shared_hwtstamps shhwtstamps;
1328 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1329
1330 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1331 skb_tstamp_tx(skb, &shhwtstamps);
1332 }
1333
1334 /* Deferred means some collisions occurred during transmit,
1335 * but we eventually sent the packet OK.
1336 */
1337 if (status & BD_ENET_TX_DEF)
1338 ndev->stats.collisions++;
1339
1340 /* Free the sk buffer associated with this last transmit */
1341 dev_kfree_skb_any(skb);
1342skb_done:
1343 /* Make sure the update to bdp and tx_skbuff are performed
1344 * before dirty_tx
1345 */
1346 wmb();
1347 txq->dirty_tx = bdp;
1348
1349 /* Update pointer to next buffer descriptor to be transmitted */
1350 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1351
1352 /* Since we have freed up a buffer, the ring is no longer full
1353 */
1354 if (netif_tx_queue_stopped(nq)) {
1355 entries_free = fec_enet_get_free_txdesc_num(txq);
1356 if (entries_free >= txq->tx_wake_threshold)
1357 netif_tx_wake_queue(nq);
1358 }
1359 }
1360
1361 /* ERR006358: Keep the transmitter going */
1362 if (bdp != txq->bd.cur &&
1363 readl(txq->bd.reg_desc_active) == 0)
1364 writel(0, txq->bd.reg_desc_active);
1365}
1366
1367static void
1368fec_enet_tx(struct net_device *ndev)
1369{
1370 struct fec_enet_private *fep = netdev_priv(ndev);
1371 u16 queue_id;
1372 /* First process class A queue, then Class B and Best Effort queue */
1373 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1374 clear_bit(queue_id, &fep->work_tx);
1375 fec_enet_tx_queue(ndev, queue_id);
1376 }
1377 return;
1378}
1379
1380static int
1381fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1382{
1383 struct fec_enet_private *fep = netdev_priv(ndev);
1384 int off;
1385
1386 off = ((unsigned long)skb->data) & fep->rx_align;
1387 if (off)
1388 skb_reserve(skb, fep->rx_align + 1 - off);
1389
1390 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
1391 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
1392 if (net_ratelimit())
1393 netdev_err(ndev, "Rx DMA memory map failed\n");
1394 return -ENOMEM;
1395 }
1396
1397 return 0;
1398}
1399
1400static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
1401 struct bufdesc *bdp, u32 length, bool swap)
1402{
1403 struct fec_enet_private *fep = netdev_priv(ndev);
1404 struct sk_buff *new_skb;
1405
1406 if (length > fep->rx_copybreak)
1407 return false;
1408
1409 new_skb = netdev_alloc_skb(ndev, length);
1410 if (!new_skb)
1411 return false;
1412
1413 dma_sync_single_for_cpu(&fep->pdev->dev,
1414 fec32_to_cpu(bdp->cbd_bufaddr),
1415 FEC_ENET_RX_FRSIZE - fep->rx_align,
1416 DMA_FROM_DEVICE);
1417 if (!swap)
1418 memcpy(new_skb->data, (*skb)->data, length);
1419 else
1420 swap_buffer2(new_skb->data, (*skb)->data, length);
1421 *skb = new_skb;
1422
1423 return true;
1424}
1425
1426/* During a receive, the bd_rx.cur points to the current incoming buffer.
1427 * When we update through the ring, if the next incoming buffer has
1428 * not been given to the system, we just set the empty indicator,
1429 * effectively tossing the packet.
1430 */
1431static int
1432fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1433{
1434 struct fec_enet_private *fep = netdev_priv(ndev);
1435 struct fec_enet_priv_rx_q *rxq;
1436 struct bufdesc *bdp;
1437 unsigned short status;
1438 struct sk_buff *skb_new = NULL;
1439 struct sk_buff *skb;
1440 ushort pkt_len;
1441 __u8 *data;
1442 int pkt_received = 0;
1443 struct bufdesc_ex *ebdp = NULL;
1444 bool vlan_packet_rcvd = false;
1445 u16 vlan_tag;
1446 int index = 0;
1447 bool is_copybreak;
1448 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1449
1450#ifdef CONFIG_M532x
1451 flush_cache_all();
1452#endif
1453 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1454 rxq = fep->rx_queue[queue_id];
1455
1456 /* First, grab all of the stats for the incoming packet.
1457 * These get messed up if we get called due to a busy condition.
1458 */
1459 bdp = rxq->bd.cur;
1460
1461 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1462
1463 if (pkt_received >= budget)
1464 break;
1465 pkt_received++;
1466
1467 writel(FEC_ENET_RXF_GET(queue_id), fep->hwp + FEC_IEVENT);
1468
1469 /* Check for errors. */
1470 status ^= BD_ENET_RX_LAST;
1471 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1472 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1473 BD_ENET_RX_CL)) {
1474 ndev->stats.rx_errors++;
1475 if (status & BD_ENET_RX_OV) {
1476 /* FIFO overrun */
1477 ndev->stats.rx_fifo_errors++;
1478 goto rx_processing_done;
1479 }
1480 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1481 | BD_ENET_RX_LAST)) {
1482 /* Frame too long or too short. */
1483 ndev->stats.rx_length_errors++;
1484 if (status & BD_ENET_RX_LAST)
1485 netdev_err(ndev, "rcv is not +last\n");
1486 }
1487 if (status & BD_ENET_RX_CR) /* CRC Error */
1488 ndev->stats.rx_crc_errors++;
1489 /* Report late collisions as a frame error. */
1490 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1491 ndev->stats.rx_frame_errors++;
1492 goto rx_processing_done;
1493 }
1494
1495 /* Process the incoming frame. */
1496 ndev->stats.rx_packets++;
1497 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1498 ndev->stats.rx_bytes += pkt_len;
1499
1500 index = fec_enet_get_bd_index(bdp, &rxq->bd);
1501 skb = rxq->rx_skbuff[index];
1502
1503 /* The packet length includes FCS, but we don't want to
1504 * include that when passing upstream as it messes up
1505 * bridging applications.
1506 */
1507 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1508 need_swap);
1509 if (!is_copybreak) {
1510 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1511 if (unlikely(!skb_new)) {
1512 ndev->stats.rx_dropped++;
1513 goto rx_processing_done;
1514 }
1515 dma_unmap_single(&fep->pdev->dev,
1516 fec32_to_cpu(bdp->cbd_bufaddr),
1517 FEC_ENET_RX_FRSIZE - fep->rx_align,
1518 DMA_FROM_DEVICE);
1519 }
1520
1521 prefetch(skb->data - NET_IP_ALIGN);
1522 skb_put(skb, pkt_len - 4);
1523 data = skb->data;
1524
1525 if (!is_copybreak && need_swap)
1526 swap_buffer(data, pkt_len);
1527
1528#if !defined(CONFIG_M5272)
1529 if (fep->quirks & FEC_QUIRK_HAS_RACC)
1530 data = skb_pull_inline(skb, 2);
1531#endif
1532
1533 /* Extract the enhanced buffer descriptor */
1534 ebdp = NULL;
1535 if (fep->bufdesc_ex)
1536 ebdp = (struct bufdesc_ex *)bdp;
1537
1538 /* If this is a VLAN packet remove the VLAN Tag */
1539 vlan_packet_rcvd = false;
1540 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1541 fep->bufdesc_ex &&
1542 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1543 /* Push and remove the vlan tag */
1544 struct vlan_hdr *vlan_header =
1545 (struct vlan_hdr *) (data + ETH_HLEN);
1546 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1547
1548 vlan_packet_rcvd = true;
1549
1550 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
1551 skb_pull(skb, VLAN_HLEN);
1552 }
1553
1554 skb->protocol = eth_type_trans(skb, ndev);
1555
1556 /* Get receive timestamp from the skb */
1557 if (fep->hwts_rx_en && fep->bufdesc_ex)
1558 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1559 skb_hwtstamps(skb));
1560
1561 if (fep->bufdesc_ex &&
1562 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1563 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
1564 /* don't check it */
1565 skb->ip_summed = CHECKSUM_UNNECESSARY;
1566 } else {
1567 skb_checksum_none_assert(skb);
1568 }
1569 }
1570
1571 /* Handle received VLAN packets */
1572 if (vlan_packet_rcvd)
1573 __vlan_hwaccel_put_tag(skb,
1574 htons(ETH_P_8021Q),
1575 vlan_tag);
1576
1577 napi_gro_receive(&fep->napi, skb);
1578
1579 if (is_copybreak) {
1580 dma_sync_single_for_device(&fep->pdev->dev,
1581 fec32_to_cpu(bdp->cbd_bufaddr),
1582 FEC_ENET_RX_FRSIZE - fep->rx_align,
1583 DMA_FROM_DEVICE);
1584 } else {
1585 rxq->rx_skbuff[index] = skb_new;
1586 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1587 }
1588
1589rx_processing_done:
1590 /* Clear the status flags for this buffer */
1591 status &= ~BD_ENET_RX_STATS;
1592
1593 /* Mark the buffer empty */
1594 status |= BD_ENET_RX_EMPTY;
1595
1596 if (fep->bufdesc_ex) {
1597 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1598
1599 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1600 ebdp->cbd_prot = 0;
1601 ebdp->cbd_bdu = 0;
1602 }
1603 /* Make sure the updates to rest of the descriptor are
1604 * performed before transferring ownership.
1605 */
1606 wmb();
1607 bdp->cbd_sc = cpu_to_fec16(status);
1608
1609 /* Update BD pointer to next entry */
1610 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
1611
1612 /* Doing this here will keep the FEC running while we process
1613 * incoming frames. On a heavily loaded network, we should be
1614 * able to keep up at the expense of system resources.
1615 */
1616 writel(0, rxq->bd.reg_desc_active);
1617 }
1618 rxq->bd.cur = bdp;
1619 return pkt_received;
1620}
1621
1622static int
1623fec_enet_rx(struct net_device *ndev, int budget)
1624{
1625 int pkt_received = 0;
1626 u16 queue_id;
1627 struct fec_enet_private *fep = netdev_priv(ndev);
1628
1629 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1630 int ret;
1631
1632 ret = fec_enet_rx_queue(ndev,
1633 budget - pkt_received, queue_id);
1634
1635 if (ret < budget - pkt_received)
1636 clear_bit(queue_id, &fep->work_rx);
1637
1638 pkt_received += ret;
1639 }
1640 return pkt_received;
1641}
1642
1643static bool
1644fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1645{
1646 if (int_events == 0)
1647 return false;
1648
1649 if (int_events & FEC_ENET_RXF_0)
1650 fep->work_rx |= (1 << 2);
1651 if (int_events & FEC_ENET_RXF_1)
1652 fep->work_rx |= (1 << 0);
1653 if (int_events & FEC_ENET_RXF_2)
1654 fep->work_rx |= (1 << 1);
1655
1656 if (int_events & FEC_ENET_TXF_0)
1657 fep->work_tx |= (1 << 2);
1658 if (int_events & FEC_ENET_TXF_1)
1659 fep->work_tx |= (1 << 0);
1660 if (int_events & FEC_ENET_TXF_2)
1661 fep->work_tx |= (1 << 1);
1662
1663 return true;
1664}
1665
1666static irqreturn_t
1667fec_enet_interrupt(int irq, void *dev_id)
1668{
1669 struct net_device *ndev = dev_id;
1670 struct fec_enet_private *fep = netdev_priv(ndev);
1671 uint int_events;
1672 irqreturn_t ret = IRQ_NONE;
1673
1674 int_events = readl(fep->hwp + FEC_IEVENT);
1675 writel(int_events, fep->hwp + FEC_IEVENT);
1676 fec_enet_collect_events(fep, int_events);
1677
1678 if ((fep->work_tx || fep->work_rx) && fep->link) {
1679 ret = IRQ_HANDLED;
1680
1681 if (napi_schedule_prep(&fep->napi)) {
1682 /* Disable the NAPI interrupts */
1683 writel(FEC_NAPI_IMASK, fep->hwp + FEC_IMASK);
1684 __napi_schedule(&fep->napi);
1685 }
1686 }
1687
1688 if (int_events & FEC_ENET_MII) {
1689 ret = IRQ_HANDLED;
1690 complete(&fep->mdio_done);
1691 }
1692 return ret;
1693}
1694
1695static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1696{
1697 struct net_device *ndev = napi->dev;
1698 struct fec_enet_private *fep = netdev_priv(ndev);
1699 int pkts;
1700
1701 pkts = fec_enet_rx(ndev, budget);
1702
1703 fec_enet_tx(ndev);
1704
1705 if (pkts < budget) {
1706 napi_complete_done(napi, pkts);
1707 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1708 }
1709 return pkts;
1710}
1711
1712/* ------------------------------------------------------------------------- */
1713static void fec_get_mac(struct net_device *ndev)
1714{
1715 struct fec_enet_private *fep = netdev_priv(ndev);
1716 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1717 unsigned char *iap, tmpaddr[ETH_ALEN];
1718
1719 /*
1720 * try to get mac address in following order:
1721 *
1722 * 1) module parameter via kernel command line in form
1723 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1724 */
1725 iap = macaddr;
1726
1727 /*
1728 * 2) from device tree data
1729 */
1730 if (!is_valid_ether_addr(iap)) {
1731 struct device_node *np = fep->pdev->dev.of_node;
1732 if (np) {
1733 const char *mac = of_get_mac_address(np);
1734 if (!IS_ERR(mac))
1735 iap = (unsigned char *) mac;
1736 }
1737 }
1738
1739 /*
1740 * 3) from flash or fuse (via platform data)
1741 */
1742 if (!is_valid_ether_addr(iap)) {
1743#ifdef CONFIG_M5272
1744 if (FEC_FLASHMAC)
1745 iap = (unsigned char *)FEC_FLASHMAC;
1746#else
1747 if (pdata)
1748 iap = (unsigned char *)&pdata->mac;
1749#endif
1750 }
1751
1752 /*
1753 * 4) FEC mac registers set by bootloader
1754 */
1755 if (!is_valid_ether_addr(iap)) {
1756 *((__be32 *) &tmpaddr[0]) =
1757 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1758 *((__be16 *) &tmpaddr[4]) =
1759 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1760 iap = &tmpaddr[0];
1761 }
1762
1763 /*
1764 * 5) random mac address
1765 */
1766 if (!is_valid_ether_addr(iap)) {
1767 /* Report it and use a random ethernet address instead */
1768 dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
1769 eth_hw_addr_random(ndev);
1770 dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
1771 ndev->dev_addr);
1772 return;
1773 }
1774
1775 memcpy(ndev->dev_addr, iap, ETH_ALEN);
1776
1777 /* Adjust MAC if using macaddr */
1778 if (iap == macaddr)
1779 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1780}
1781
1782/* ------------------------------------------------------------------------- */
1783
1784/*
1785 * Phy section
1786 */
1787static void fec_enet_adjust_link(struct net_device *ndev)
1788{
1789 struct fec_enet_private *fep = netdev_priv(ndev);
1790 struct phy_device *phy_dev = ndev->phydev;
1791 int status_change = 0;
1792
1793 /*
1794 * If the netdev is down, or is going down, we're not interested
1795 * in link state events, so just mark our idea of the link as down
1796 * and ignore the event.
1797 */
1798 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1799 fep->link = 0;
1800 } else if (phy_dev->link) {
1801 if (!fep->link) {
1802 fep->link = phy_dev->link;
1803 status_change = 1;
1804 }
1805
1806 if (fep->full_duplex != phy_dev->duplex) {
1807 fep->full_duplex = phy_dev->duplex;
1808 status_change = 1;
1809 }
1810
1811 if (phy_dev->speed != fep->speed) {
1812 fep->speed = phy_dev->speed;
1813 status_change = 1;
1814 }
1815
1816 /* if any of the above changed restart the FEC */
1817 if (status_change) {
1818 netif_stop_queue(ndev);
1819 napi_disable(&fep->napi);
1820 netif_tx_lock_bh(ndev);
1821 fec_restart(ndev);
1822 netif_tx_wake_all_queues(ndev);
1823 netif_tx_unlock_bh(ndev);
1824 napi_enable(&fep->napi);
1825 }
1826 } else {
1827 if (fep->link) {
1828 netif_stop_queue(ndev);
1829 napi_disable(&fep->napi);
1830 netif_tx_lock_bh(ndev);
1831 fec_stop(ndev);
1832 netif_tx_unlock_bh(ndev);
1833 napi_enable(&fep->napi);
1834 fep->link = phy_dev->link;
1835 status_change = 1;
1836 }
1837 }
1838
1839 if (status_change)
1840 phy_print_status(phy_dev);
1841}
1842
1843static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1844{
1845 struct fec_enet_private *fep = bus->priv;
1846 struct device *dev = &fep->pdev->dev;
1847 unsigned long time_left;
1848 int ret = 0, frame_start, frame_addr, frame_op;
1849 bool is_c45 = !!(regnum & MII_ADDR_C45);
1850
1851 ret = pm_runtime_get_sync(dev);
1852 if (ret < 0)
1853 return ret;
1854
1855 reinit_completion(&fep->mdio_done);
1856
1857 if (is_c45) {
1858 frame_start = FEC_MMFR_ST_C45;
1859
1860 /* write address */
1861 frame_addr = (regnum >> 16);
1862 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1863 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1864 FEC_MMFR_TA | (regnum & 0xFFFF),
1865 fep->hwp + FEC_MII_DATA);
1866
1867 /* wait for end of transfer */
1868 time_left = wait_for_completion_timeout(&fep->mdio_done,
1869 usecs_to_jiffies(FEC_MII_TIMEOUT));
1870 if (time_left == 0) {
1871 netdev_err(fep->netdev, "MDIO address write timeout\n");
1872 ret = -ETIMEDOUT;
1873 goto out;
1874 }
1875
1876 frame_op = FEC_MMFR_OP_READ_C45;
1877
1878 } else {
1879 /* C22 read */
1880 frame_op = FEC_MMFR_OP_READ;
1881 frame_start = FEC_MMFR_ST;
1882 frame_addr = regnum;
1883 }
1884
1885 /* start a read op */
1886 writel(frame_start | frame_op |
1887 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1888 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1889
1890 /* wait for end of transfer */
1891 time_left = wait_for_completion_timeout(&fep->mdio_done,
1892 usecs_to_jiffies(FEC_MII_TIMEOUT));
1893 if (time_left == 0) {
1894 netdev_err(fep->netdev, "MDIO read timeout\n");
1895 ret = -ETIMEDOUT;
1896 goto out;
1897 }
1898
1899 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1900
1901out:
1902 pm_runtime_mark_last_busy(dev);
1903 pm_runtime_put_autosuspend(dev);
1904
1905 return ret;
1906}
1907
1908static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1909 u16 value)
1910{
1911 struct fec_enet_private *fep = bus->priv;
1912 struct device *dev = &fep->pdev->dev;
1913 unsigned long time_left;
1914 int ret, frame_start, frame_addr;
1915 bool is_c45 = !!(regnum & MII_ADDR_C45);
1916
1917 ret = pm_runtime_get_sync(dev);
1918 if (ret < 0)
1919 return ret;
1920 else
1921 ret = 0;
1922
1923 reinit_completion(&fep->mdio_done);
1924
1925 if (is_c45) {
1926 frame_start = FEC_MMFR_ST_C45;
1927
1928 /* write address */
1929 frame_addr = (regnum >> 16);
1930 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1931 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1932 FEC_MMFR_TA | (regnum & 0xFFFF),
1933 fep->hwp + FEC_MII_DATA);
1934
1935 /* wait for end of transfer */
1936 time_left = wait_for_completion_timeout(&fep->mdio_done,
1937 usecs_to_jiffies(FEC_MII_TIMEOUT));
1938 if (time_left == 0) {
1939 netdev_err(fep->netdev, "MDIO address write timeout\n");
1940 ret = -ETIMEDOUT;
1941 goto out;
1942 }
1943 } else {
1944 /* C22 write */
1945 frame_start = FEC_MMFR_ST;
1946 frame_addr = regnum;
1947 }
1948
1949 /* start a write op */
1950 writel(frame_start | FEC_MMFR_OP_WRITE |
1951 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1952 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1953 fep->hwp + FEC_MII_DATA);
1954
1955 /* wait for end of transfer */
1956 time_left = wait_for_completion_timeout(&fep->mdio_done,
1957 usecs_to_jiffies(FEC_MII_TIMEOUT));
1958 if (time_left == 0) {
1959 netdev_err(fep->netdev, "MDIO write timeout\n");
1960 ret = -ETIMEDOUT;
1961 }
1962
1963out:
1964 pm_runtime_mark_last_busy(dev);
1965 pm_runtime_put_autosuspend(dev);
1966
1967 return ret;
1968}
1969
1970static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
1971{
1972 struct fec_enet_private *fep = netdev_priv(ndev);
1973 struct phy_device *phy_dev = ndev->phydev;
1974
1975 if (phy_dev) {
1976 phy_reset_after_clk_enable(phy_dev);
1977 } else if (fep->phy_node) {
1978 /*
1979 * If the PHY still is not bound to the MAC, but there is
1980 * OF PHY node and a matching PHY device instance already,
1981 * use the OF PHY node to obtain the PHY device instance,
1982 * and then use that PHY device instance when triggering
1983 * the PHY reset.
1984 */
1985 phy_dev = of_phy_find_device(fep->phy_node);
1986 phy_reset_after_clk_enable(phy_dev);
1987 put_device(&phy_dev->mdio.dev);
1988 }
1989}
1990
1991static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1992{
1993 struct fec_enet_private *fep = netdev_priv(ndev);
1994 int ret;
1995
1996 if (enable) {
1997 ret = clk_prepare_enable(fep->clk_enet_out);
1998 if (ret)
1999 return ret;
2000
2001 if (fep->clk_ptp) {
2002 mutex_lock(&fep->ptp_clk_mutex);
2003 ret = clk_prepare_enable(fep->clk_ptp);
2004 if (ret) {
2005 mutex_unlock(&fep->ptp_clk_mutex);
2006 goto failed_clk_ptp;
2007 } else {
2008 fep->ptp_clk_on = true;
2009 }
2010 mutex_unlock(&fep->ptp_clk_mutex);
2011 }
2012
2013 ret = clk_prepare_enable(fep->clk_ref);
2014 if (ret)
2015 goto failed_clk_ref;
2016
2017 fec_enet_phy_reset_after_clk_enable(ndev);
2018 } else {
2019 clk_disable_unprepare(fep->clk_enet_out);
2020 if (fep->clk_ptp) {
2021 mutex_lock(&fep->ptp_clk_mutex);
2022 clk_disable_unprepare(fep->clk_ptp);
2023 fep->ptp_clk_on = false;
2024 mutex_unlock(&fep->ptp_clk_mutex);
2025 }
2026 clk_disable_unprepare(fep->clk_ref);
2027 }
2028
2029 return 0;
2030
2031failed_clk_ref:
2032 if (fep->clk_ref)
2033 clk_disable_unprepare(fep->clk_ref);
2034failed_clk_ptp:
2035 if (fep->clk_enet_out)
2036 clk_disable_unprepare(fep->clk_enet_out);
2037
2038 return ret;
2039}
2040
2041static int fec_enet_mii_probe(struct net_device *ndev)
2042{
2043 struct fec_enet_private *fep = netdev_priv(ndev);
2044 struct phy_device *phy_dev = NULL;
2045 char mdio_bus_id[MII_BUS_ID_SIZE];
2046 char phy_name[MII_BUS_ID_SIZE + 3];
2047 int phy_id;
2048 int dev_id = fep->dev_id;
2049
2050 if (fep->phy_node) {
2051 phy_dev = of_phy_connect(ndev, fep->phy_node,
2052 &fec_enet_adjust_link, 0,
2053 fep->phy_interface);
2054 if (!phy_dev) {
2055 netdev_err(ndev, "Unable to connect to phy\n");
2056 return -ENODEV;
2057 }
2058 } else {
2059 /* check for attached phy */
2060 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
2061 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
2062 continue;
2063 if (dev_id--)
2064 continue;
2065 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
2066 break;
2067 }
2068
2069 if (phy_id >= PHY_MAX_ADDR) {
2070 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
2071 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
2072 phy_id = 0;
2073 }
2074
2075 snprintf(phy_name, sizeof(phy_name),
2076 PHY_ID_FMT, mdio_bus_id, phy_id);
2077 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
2078 fep->phy_interface);
2079 }
2080
2081 if (IS_ERR(phy_dev)) {
2082 netdev_err(ndev, "could not attach to PHY\n");
2083 return PTR_ERR(phy_dev);
2084 }
2085
2086 /* mask with MAC supported features */
2087 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
2088 phy_set_max_speed(phy_dev, 1000);
2089 phy_remove_link_mode(phy_dev,
2090 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
2091#if !defined(CONFIG_M5272)
2092 phy_support_sym_pause(phy_dev);
2093#endif
2094 }
2095 else
2096 phy_set_max_speed(phy_dev, 100);
2097
2098 fep->link = 0;
2099 fep->full_duplex = 0;
2100
2101 phy_attached_info(phy_dev);
2102
2103 return 0;
2104}
2105
2106static int fec_enet_mii_init(struct platform_device *pdev)
2107{
2108 static struct mii_bus *fec0_mii_bus;
2109 struct net_device *ndev = platform_get_drvdata(pdev);
2110 struct fec_enet_private *fep = netdev_priv(ndev);
2111 struct device_node *node;
2112 int err = -ENXIO;
2113 u32 mii_speed, holdtime;
2114
2115 /*
2116 * The i.MX28 dual fec interfaces are not equal.
2117 * Here are the differences:
2118 *
2119 * - fec0 supports MII & RMII modes while fec1 only supports RMII
2120 * - fec0 acts as the 1588 time master while fec1 is slave
2121 * - external phys can only be configured by fec0
2122 *
2123 * That is to say fec1 can not work independently. It only works
2124 * when fec0 is working. The reason behind this design is that the
2125 * second interface is added primarily for Switch mode.
2126 *
2127 * Because of the last point above, both phys are attached on fec0
2128 * mdio interface in board design, and need to be configured by
2129 * fec0 mii_bus.
2130 */
2131 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
2132 /* fec1 uses fec0 mii_bus */
2133 if (mii_cnt && fec0_mii_bus) {
2134 fep->mii_bus = fec0_mii_bus;
2135 mii_cnt++;
2136 return 0;
2137 }
2138 return -ENOENT;
2139 }
2140
2141 /*
2142 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
2143 *
2144 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2145 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2146 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2147 * document.
2148 */
2149 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
2150 if (fep->quirks & FEC_QUIRK_ENET_MAC)
2151 mii_speed--;
2152 if (mii_speed > 63) {
2153 dev_err(&pdev->dev,
2154 "fec clock (%lu) too fast to get right mii speed\n",
2155 clk_get_rate(fep->clk_ipg));
2156 err = -EINVAL;
2157 goto err_out;
2158 }
2159
2160 /*
2161 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2162 * MII_SPEED) register that defines the MDIO output hold time. Earlier
2163 * versions are RAZ there, so just ignore the difference and write the
2164 * register always.
2165 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2166 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2167 * output.
2168 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2169 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2170 * holdtime cannot result in a value greater than 3.
2171 */
2172 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2173
2174 fep->phy_speed = mii_speed << 1 | holdtime << 8;
2175
2176 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2177
2178 fep->mii_bus = mdiobus_alloc();
2179 if (fep->mii_bus == NULL) {
2180 err = -ENOMEM;
2181 goto err_out;
2182 }
2183
2184 fep->mii_bus->name = "fec_enet_mii_bus";
2185 fep->mii_bus->read = fec_enet_mdio_read;
2186 fep->mii_bus->write = fec_enet_mdio_write;
2187 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2188 pdev->name, fep->dev_id + 1);
2189 fep->mii_bus->priv = fep;
2190 fep->mii_bus->parent = &pdev->dev;
2191
2192 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2193 err = of_mdiobus_register(fep->mii_bus, node);
2194 of_node_put(node);
2195 if (err)
2196 goto err_out_free_mdiobus;
2197
2198 mii_cnt++;
2199
2200 /* save fec0 mii_bus */
2201 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2202 fec0_mii_bus = fep->mii_bus;
2203
2204 return 0;
2205
2206err_out_free_mdiobus:
2207 mdiobus_free(fep->mii_bus);
2208err_out:
2209 return err;
2210}
2211
2212static void fec_enet_mii_remove(struct fec_enet_private *fep)
2213{
2214 if (--mii_cnt == 0) {
2215 mdiobus_unregister(fep->mii_bus);
2216 mdiobus_free(fep->mii_bus);
2217 }
2218}
2219
2220static void fec_enet_get_drvinfo(struct net_device *ndev,
2221 struct ethtool_drvinfo *info)
2222{
2223 struct fec_enet_private *fep = netdev_priv(ndev);
2224
2225 strlcpy(info->driver, fep->pdev->dev.driver->name,
2226 sizeof(info->driver));
2227 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2228 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2229}
2230
2231static int fec_enet_get_regs_len(struct net_device *ndev)
2232{
2233 struct fec_enet_private *fep = netdev_priv(ndev);
2234 struct resource *r;
2235 int s = 0;
2236
2237 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2238 if (r)
2239 s = resource_size(r);
2240
2241 return s;
2242}
2243
2244/* List of registers that can be safety be read to dump them with ethtool */
2245#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2246 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
2247 defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
2248static __u32 fec_enet_register_version = 2;
2249static u32 fec_enet_register_offset[] = {
2250 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2251 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2252 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2253 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2254 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2255 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2256 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2257 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2258 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2259 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2260 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2261 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2262 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2263 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2264 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2265 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2266 RMON_T_P_GTE2048, RMON_T_OCTETS,
2267 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2268 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2269 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2270 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2271 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2272 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2273 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2274 RMON_R_P_GTE2048, RMON_R_OCTETS,
2275 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2276 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2277};
2278#else
2279static __u32 fec_enet_register_version = 1;
2280static u32 fec_enet_register_offset[] = {
2281 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2282 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2283 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2284 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2285 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2286 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2287 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2288 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2289 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2290};
2291#endif
2292
2293static void fec_enet_get_regs(struct net_device *ndev,
2294 struct ethtool_regs *regs, void *regbuf)
2295{
2296 struct fec_enet_private *fep = netdev_priv(ndev);
2297 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2298 struct device *dev = &fep->pdev->dev;
2299 u32 *buf = (u32 *)regbuf;
2300 u32 i, off;
2301 int ret;
2302
2303 ret = pm_runtime_get_sync(dev);
2304 if (ret < 0)
2305 return;
2306
2307 regs->version = fec_enet_register_version;
2308
2309 memset(buf, 0, regs->len);
2310
2311 for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2312 off = fec_enet_register_offset[i];
2313
2314 if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
2315 !(fep->quirks & FEC_QUIRK_HAS_FRREG))
2316 continue;
2317
2318 off >>= 2;
2319 buf[off] = readl(&theregs[off]);
2320 }
2321
2322 pm_runtime_mark_last_busy(dev);
2323 pm_runtime_put_autosuspend(dev);
2324}
2325
2326static int fec_enet_get_ts_info(struct net_device *ndev,
2327 struct ethtool_ts_info *info)
2328{
2329 struct fec_enet_private *fep = netdev_priv(ndev);
2330
2331 if (fep->bufdesc_ex) {
2332
2333 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2334 SOF_TIMESTAMPING_RX_SOFTWARE |
2335 SOF_TIMESTAMPING_SOFTWARE |
2336 SOF_TIMESTAMPING_TX_HARDWARE |
2337 SOF_TIMESTAMPING_RX_HARDWARE |
2338 SOF_TIMESTAMPING_RAW_HARDWARE;
2339 if (fep->ptp_clock)
2340 info->phc_index = ptp_clock_index(fep->ptp_clock);
2341 else
2342 info->phc_index = -1;
2343
2344 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2345 (1 << HWTSTAMP_TX_ON);
2346
2347 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2348 (1 << HWTSTAMP_FILTER_ALL);
2349 return 0;
2350 } else {
2351 return ethtool_op_get_ts_info(ndev, info);
2352 }
2353}
2354
2355#if !defined(CONFIG_M5272)
2356
2357static void fec_enet_get_pauseparam(struct net_device *ndev,
2358 struct ethtool_pauseparam *pause)
2359{
2360 struct fec_enet_private *fep = netdev_priv(ndev);
2361
2362 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2363 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2364 pause->rx_pause = pause->tx_pause;
2365}
2366
2367static int fec_enet_set_pauseparam(struct net_device *ndev,
2368 struct ethtool_pauseparam *pause)
2369{
2370 struct fec_enet_private *fep = netdev_priv(ndev);
2371
2372 if (!ndev->phydev)
2373 return -ENODEV;
2374
2375 if (pause->tx_pause != pause->rx_pause) {
2376 netdev_info(ndev,
2377 "hardware only support enable/disable both tx and rx");
2378 return -EINVAL;
2379 }
2380
2381 fep->pause_flag = 0;
2382
2383 /* tx pause must be same as rx pause */
2384 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2385 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2386
2387 phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
2388 pause->autoneg);
2389
2390 if (pause->autoneg) {
2391 if (netif_running(ndev))
2392 fec_stop(ndev);
2393 phy_start_aneg(ndev->phydev);
2394 }
2395 if (netif_running(ndev)) {
2396 napi_disable(&fep->napi);
2397 netif_tx_lock_bh(ndev);
2398 fec_restart(ndev);
2399 netif_tx_wake_all_queues(ndev);
2400 netif_tx_unlock_bh(ndev);
2401 napi_enable(&fep->napi);
2402 }
2403
2404 return 0;
2405}
2406
2407static const struct fec_stat {
2408 char name[ETH_GSTRING_LEN];
2409 u16 offset;
2410} fec_stats[] = {
2411 /* RMON TX */
2412 { "tx_dropped", RMON_T_DROP },
2413 { "tx_packets", RMON_T_PACKETS },
2414 { "tx_broadcast", RMON_T_BC_PKT },
2415 { "tx_multicast", RMON_T_MC_PKT },
2416 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2417 { "tx_undersize", RMON_T_UNDERSIZE },
2418 { "tx_oversize", RMON_T_OVERSIZE },
2419 { "tx_fragment", RMON_T_FRAG },
2420 { "tx_jabber", RMON_T_JAB },
2421 { "tx_collision", RMON_T_COL },
2422 { "tx_64byte", RMON_T_P64 },
2423 { "tx_65to127byte", RMON_T_P65TO127 },
2424 { "tx_128to255byte", RMON_T_P128TO255 },
2425 { "tx_256to511byte", RMON_T_P256TO511 },
2426 { "tx_512to1023byte", RMON_T_P512TO1023 },
2427 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2428 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2429 { "tx_octets", RMON_T_OCTETS },
2430
2431 /* IEEE TX */
2432 { "IEEE_tx_drop", IEEE_T_DROP },
2433 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2434 { "IEEE_tx_1col", IEEE_T_1COL },
2435 { "IEEE_tx_mcol", IEEE_T_MCOL },
2436 { "IEEE_tx_def", IEEE_T_DEF },
2437 { "IEEE_tx_lcol", IEEE_T_LCOL },
2438 { "IEEE_tx_excol", IEEE_T_EXCOL },
2439 { "IEEE_tx_macerr", IEEE_T_MACERR },
2440 { "IEEE_tx_cserr", IEEE_T_CSERR },
2441 { "IEEE_tx_sqe", IEEE_T_SQE },
2442 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2443 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2444
2445 /* RMON RX */
2446 { "rx_packets", RMON_R_PACKETS },
2447 { "rx_broadcast", RMON_R_BC_PKT },
2448 { "rx_multicast", RMON_R_MC_PKT },
2449 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2450 { "rx_undersize", RMON_R_UNDERSIZE },
2451 { "rx_oversize", RMON_R_OVERSIZE },
2452 { "rx_fragment", RMON_R_FRAG },
2453 { "rx_jabber", RMON_R_JAB },
2454 { "rx_64byte", RMON_R_P64 },
2455 { "rx_65to127byte", RMON_R_P65TO127 },
2456 { "rx_128to255byte", RMON_R_P128TO255 },
2457 { "rx_256to511byte", RMON_R_P256TO511 },
2458 { "rx_512to1023byte", RMON_R_P512TO1023 },
2459 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2460 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2461 { "rx_octets", RMON_R_OCTETS },
2462
2463 /* IEEE RX */
2464 { "IEEE_rx_drop", IEEE_R_DROP },
2465 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2466 { "IEEE_rx_crc", IEEE_R_CRC },
2467 { "IEEE_rx_align", IEEE_R_ALIGN },
2468 { "IEEE_rx_macerr", IEEE_R_MACERR },
2469 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2470 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2471};
2472
2473#define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64))
2474
2475static void fec_enet_update_ethtool_stats(struct net_device *dev)
2476{
2477 struct fec_enet_private *fep = netdev_priv(dev);
2478 int i;
2479
2480 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2481 fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
2482}
2483
2484static void fec_enet_get_ethtool_stats(struct net_device *dev,
2485 struct ethtool_stats *stats, u64 *data)
2486{
2487 struct fec_enet_private *fep = netdev_priv(dev);
2488
2489 if (netif_running(dev))
2490 fec_enet_update_ethtool_stats(dev);
2491
2492 memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
2493}
2494
2495static void fec_enet_get_strings(struct net_device *netdev,
2496 u32 stringset, u8 *data)
2497{
2498 int i;
2499 switch (stringset) {
2500 case ETH_SS_STATS:
2501 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2502 memcpy(data + i * ETH_GSTRING_LEN,
2503 fec_stats[i].name, ETH_GSTRING_LEN);
2504 break;
2505 }
2506}
2507
2508static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2509{
2510 switch (sset) {
2511 case ETH_SS_STATS:
2512 return ARRAY_SIZE(fec_stats);
2513 default:
2514 return -EOPNOTSUPP;
2515 }
2516}
2517
2518static void fec_enet_clear_ethtool_stats(struct net_device *dev)
2519{
2520 struct fec_enet_private *fep = netdev_priv(dev);
2521 int i;
2522
2523 /* Disable MIB statistics counters */
2524 writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
2525
2526 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2527 writel(0, fep->hwp + fec_stats[i].offset);
2528
2529 /* Don't disable MIB statistics counters */
2530 writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
2531}
2532
2533#else /* !defined(CONFIG_M5272) */
2534#define FEC_STATS_SIZE 0
2535static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2536{
2537}
2538
2539static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
2540{
2541}
2542#endif /* !defined(CONFIG_M5272) */
2543
2544/* ITR clock source is enet system clock (clk_ahb).
2545 * TCTT unit is cycle_ns * 64 cycle
2546 * So, the ICTT value = X us / (cycle_ns * 64)
2547 */
2548static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2549{
2550 struct fec_enet_private *fep = netdev_priv(ndev);
2551
2552 return us * (fep->itr_clk_rate / 64000) / 1000;
2553}
2554
2555/* Set threshold for interrupt coalescing */
2556static void fec_enet_itr_coal_set(struct net_device *ndev)
2557{
2558 struct fec_enet_private *fep = netdev_priv(ndev);
2559 int rx_itr, tx_itr;
2560
2561 /* Must be greater than zero to avoid unpredictable behavior */
2562 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2563 !fep->tx_time_itr || !fep->tx_pkts_itr)
2564 return;
2565
2566 /* Select enet system clock as Interrupt Coalescing
2567 * timer Clock Source
2568 */
2569 rx_itr = FEC_ITR_CLK_SEL;
2570 tx_itr = FEC_ITR_CLK_SEL;
2571
2572 /* set ICFT and ICTT */
2573 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2574 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2575 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2576 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2577
2578 rx_itr |= FEC_ITR_EN;
2579 tx_itr |= FEC_ITR_EN;
2580
2581 writel(tx_itr, fep->hwp + FEC_TXIC0);
2582 writel(rx_itr, fep->hwp + FEC_RXIC0);
2583 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
2584 writel(tx_itr, fep->hwp + FEC_TXIC1);
2585 writel(rx_itr, fep->hwp + FEC_RXIC1);
2586 writel(tx_itr, fep->hwp + FEC_TXIC2);
2587 writel(rx_itr, fep->hwp + FEC_RXIC2);
2588 }
2589}
2590
2591static int
2592fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2593{
2594 struct fec_enet_private *fep = netdev_priv(ndev);
2595
2596 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2597 return -EOPNOTSUPP;
2598
2599 ec->rx_coalesce_usecs = fep->rx_time_itr;
2600 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2601
2602 ec->tx_coalesce_usecs = fep->tx_time_itr;
2603 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2604
2605 return 0;
2606}
2607
2608static int
2609fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2610{
2611 struct fec_enet_private *fep = netdev_priv(ndev);
2612 struct device *dev = &fep->pdev->dev;
2613 unsigned int cycle;
2614
2615 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2616 return -EOPNOTSUPP;
2617
2618 if (ec->rx_max_coalesced_frames > 255) {
2619 dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
2620 return -EINVAL;
2621 }
2622
2623 if (ec->tx_max_coalesced_frames > 255) {
2624 dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
2625 return -EINVAL;
2626 }
2627
2628 cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
2629 if (cycle > 0xFFFF) {
2630 dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
2631 return -EINVAL;
2632 }
2633
2634 cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
2635 if (cycle > 0xFFFF) {
2636 dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
2637 return -EINVAL;
2638 }
2639
2640 fep->rx_time_itr = ec->rx_coalesce_usecs;
2641 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2642
2643 fep->tx_time_itr = ec->tx_coalesce_usecs;
2644 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2645
2646 fec_enet_itr_coal_set(ndev);
2647
2648 return 0;
2649}
2650
2651static void fec_enet_itr_coal_init(struct net_device *ndev)
2652{
2653 struct ethtool_coalesce ec;
2654
2655 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2656 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2657
2658 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2659 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2660
2661 fec_enet_set_coalesce(ndev, &ec);
2662}
2663
2664static int fec_enet_get_tunable(struct net_device *netdev,
2665 const struct ethtool_tunable *tuna,
2666 void *data)
2667{
2668 struct fec_enet_private *fep = netdev_priv(netdev);
2669 int ret = 0;
2670
2671 switch (tuna->id) {
2672 case ETHTOOL_RX_COPYBREAK:
2673 *(u32 *)data = fep->rx_copybreak;
2674 break;
2675 default:
2676 ret = -EINVAL;
2677 break;
2678 }
2679
2680 return ret;
2681}
2682
2683static int fec_enet_set_tunable(struct net_device *netdev,
2684 const struct ethtool_tunable *tuna,
2685 const void *data)
2686{
2687 struct fec_enet_private *fep = netdev_priv(netdev);
2688 int ret = 0;
2689
2690 switch (tuna->id) {
2691 case ETHTOOL_RX_COPYBREAK:
2692 fep->rx_copybreak = *(u32 *)data;
2693 break;
2694 default:
2695 ret = -EINVAL;
2696 break;
2697 }
2698
2699 return ret;
2700}
2701
2702static void
2703fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2704{
2705 struct fec_enet_private *fep = netdev_priv(ndev);
2706
2707 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2708 wol->supported = WAKE_MAGIC;
2709 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2710 } else {
2711 wol->supported = wol->wolopts = 0;
2712 }
2713}
2714
2715static int
2716fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2717{
2718 struct fec_enet_private *fep = netdev_priv(ndev);
2719
2720 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2721 return -EINVAL;
2722
2723 if (wol->wolopts & ~WAKE_MAGIC)
2724 return -EINVAL;
2725
2726 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2727 if (device_may_wakeup(&ndev->dev)) {
2728 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2729 if (fep->irq[0] > 0)
2730 enable_irq_wake(fep->irq[0]);
2731 } else {
2732 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2733 if (fep->irq[0] > 0)
2734 disable_irq_wake(fep->irq[0]);
2735 }
2736
2737 return 0;
2738}
2739
2740static const struct ethtool_ops fec_enet_ethtool_ops = {
2741 .get_drvinfo = fec_enet_get_drvinfo,
2742 .get_regs_len = fec_enet_get_regs_len,
2743 .get_regs = fec_enet_get_regs,
2744 .nway_reset = phy_ethtool_nway_reset,
2745 .get_link = ethtool_op_get_link,
2746 .get_coalesce = fec_enet_get_coalesce,
2747 .set_coalesce = fec_enet_set_coalesce,
2748#ifndef CONFIG_M5272
2749 .get_pauseparam = fec_enet_get_pauseparam,
2750 .set_pauseparam = fec_enet_set_pauseparam,
2751 .get_strings = fec_enet_get_strings,
2752 .get_ethtool_stats = fec_enet_get_ethtool_stats,
2753 .get_sset_count = fec_enet_get_sset_count,
2754#endif
2755 .get_ts_info = fec_enet_get_ts_info,
2756 .get_tunable = fec_enet_get_tunable,
2757 .set_tunable = fec_enet_set_tunable,
2758 .get_wol = fec_enet_get_wol,
2759 .set_wol = fec_enet_set_wol,
2760 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2761 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2762};
2763
2764static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2765{
2766 struct fec_enet_private *fep = netdev_priv(ndev);
2767 struct phy_device *phydev = ndev->phydev;
2768
2769 if (!netif_running(ndev))
2770 return -EINVAL;
2771
2772 if (!phydev)
2773 return -ENODEV;
2774
2775 if (fep->bufdesc_ex) {
2776 if (cmd == SIOCSHWTSTAMP)
2777 return fec_ptp_set(ndev, rq);
2778 if (cmd == SIOCGHWTSTAMP)
2779 return fec_ptp_get(ndev, rq);
2780 }
2781
2782 return phy_mii_ioctl(phydev, rq, cmd);
2783}
2784
2785static void fec_enet_free_buffers(struct net_device *ndev)
2786{
2787 struct fec_enet_private *fep = netdev_priv(ndev);
2788 unsigned int i;
2789 struct sk_buff *skb;
2790 struct bufdesc *bdp;
2791 struct fec_enet_priv_tx_q *txq;
2792 struct fec_enet_priv_rx_q *rxq;
2793 unsigned int q;
2794
2795 for (q = 0; q < fep->num_rx_queues; q++) {
2796 rxq = fep->rx_queue[q];
2797 bdp = rxq->bd.base;
2798 for (i = 0; i < rxq->bd.ring_size; i++) {
2799 skb = rxq->rx_skbuff[i];
2800 rxq->rx_skbuff[i] = NULL;
2801 if (skb) {
2802 dma_unmap_single(&fep->pdev->dev,
2803 fec32_to_cpu(bdp->cbd_bufaddr),
2804 FEC_ENET_RX_FRSIZE - fep->rx_align,
2805 DMA_FROM_DEVICE);
2806 dev_kfree_skb(skb);
2807 }
2808 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
2809 }
2810 }
2811
2812 for (q = 0; q < fep->num_tx_queues; q++) {
2813 txq = fep->tx_queue[q];
2814 bdp = txq->bd.base;
2815 for (i = 0; i < txq->bd.ring_size; i++) {
2816 kfree(txq->tx_bounce[i]);
2817 txq->tx_bounce[i] = NULL;
2818 skb = txq->tx_skbuff[i];
2819 txq->tx_skbuff[i] = NULL;
2820 dev_kfree_skb(skb);
2821 }
2822 }
2823}
2824
2825static void fec_enet_free_queue(struct net_device *ndev)
2826{
2827 struct fec_enet_private *fep = netdev_priv(ndev);
2828 int i;
2829 struct fec_enet_priv_tx_q *txq;
2830
2831 for (i = 0; i < fep->num_tx_queues; i++)
2832 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2833 txq = fep->tx_queue[i];
2834 dma_free_coherent(&fep->pdev->dev,
2835 txq->bd.ring_size * TSO_HEADER_SIZE,
2836 txq->tso_hdrs,
2837 txq->tso_hdrs_dma);
2838 }
2839
2840 for (i = 0; i < fep->num_rx_queues; i++)
2841 kfree(fep->rx_queue[i]);
2842 for (i = 0; i < fep->num_tx_queues; i++)
2843 kfree(fep->tx_queue[i]);
2844}
2845
2846static int fec_enet_alloc_queue(struct net_device *ndev)
2847{
2848 struct fec_enet_private *fep = netdev_priv(ndev);
2849 int i;
2850 int ret = 0;
2851 struct fec_enet_priv_tx_q *txq;
2852
2853 for (i = 0; i < fep->num_tx_queues; i++) {
2854 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2855 if (!txq) {
2856 ret = -ENOMEM;
2857 goto alloc_failed;
2858 }
2859
2860 fep->tx_queue[i] = txq;
2861 txq->bd.ring_size = TX_RING_SIZE;
2862 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
2863
2864 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2865 txq->tx_wake_threshold =
2866 (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
2867
2868 txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
2869 txq->bd.ring_size * TSO_HEADER_SIZE,
2870 &txq->tso_hdrs_dma,
2871 GFP_KERNEL);
2872 if (!txq->tso_hdrs) {
2873 ret = -ENOMEM;
2874 goto alloc_failed;
2875 }
2876 }
2877
2878 for (i = 0; i < fep->num_rx_queues; i++) {
2879 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2880 GFP_KERNEL);
2881 if (!fep->rx_queue[i]) {
2882 ret = -ENOMEM;
2883 goto alloc_failed;
2884 }
2885
2886 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
2887 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
2888 }
2889 return ret;
2890
2891alloc_failed:
2892 fec_enet_free_queue(ndev);
2893 return ret;
2894}
2895
2896static int
2897fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
2898{
2899 struct fec_enet_private *fep = netdev_priv(ndev);
2900 unsigned int i;
2901 struct sk_buff *skb;
2902 struct bufdesc *bdp;
2903 struct fec_enet_priv_rx_q *rxq;
2904
2905 rxq = fep->rx_queue[queue];
2906 bdp = rxq->bd.base;
2907 for (i = 0; i < rxq->bd.ring_size; i++) {
2908 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
2909 if (!skb)
2910 goto err_alloc;
2911
2912 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
2913 dev_kfree_skb(skb);
2914 goto err_alloc;
2915 }
2916
2917 rxq->rx_skbuff[i] = skb;
2918 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
2919
2920 if (fep->bufdesc_ex) {
2921 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2922 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
2923 }
2924
2925 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
2926 }
2927
2928 /* Set the last buffer to wrap. */
2929 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
2930 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
2931 return 0;
2932
2933 err_alloc:
2934 fec_enet_free_buffers(ndev);
2935 return -ENOMEM;
2936}
2937
2938static int
2939fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2940{
2941 struct fec_enet_private *fep = netdev_priv(ndev);
2942 unsigned int i;
2943 struct bufdesc *bdp;
2944 struct fec_enet_priv_tx_q *txq;
2945
2946 txq = fep->tx_queue[queue];
2947 bdp = txq->bd.base;
2948 for (i = 0; i < txq->bd.ring_size; i++) {
2949 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2950 if (!txq->tx_bounce[i])
2951 goto err_alloc;
2952
2953 bdp->cbd_sc = cpu_to_fec16(0);
2954 bdp->cbd_bufaddr = cpu_to_fec32(0);
2955
2956 if (fep->bufdesc_ex) {
2957 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2958 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
2959 }
2960
2961 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
2962 }
2963
2964 /* Set the last buffer to wrap. */
2965 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
2966 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
2967
2968 return 0;
2969
2970 err_alloc:
2971 fec_enet_free_buffers(ndev);
2972 return -ENOMEM;
2973}
2974
2975static int fec_enet_alloc_buffers(struct net_device *ndev)
2976{
2977 struct fec_enet_private *fep = netdev_priv(ndev);
2978 unsigned int i;
2979
2980 for (i = 0; i < fep->num_rx_queues; i++)
2981 if (fec_enet_alloc_rxq_buffers(ndev, i))
2982 return -ENOMEM;
2983
2984 for (i = 0; i < fep->num_tx_queues; i++)
2985 if (fec_enet_alloc_txq_buffers(ndev, i))
2986 return -ENOMEM;
2987 return 0;
2988}
2989
2990static int
2991fec_enet_open(struct net_device *ndev)
2992{
2993 struct fec_enet_private *fep = netdev_priv(ndev);
2994 int ret;
2995 bool reset_again;
2996
2997 ret = pm_runtime_get_sync(&fep->pdev->dev);
2998 if (ret < 0)
2999 return ret;
3000
3001 pinctrl_pm_select_default_state(&fep->pdev->dev);
3002 ret = fec_enet_clk_enable(ndev, true);
3003 if (ret)
3004 goto clk_enable;
3005
3006 /* During the first fec_enet_open call the PHY isn't probed at this
3007 * point. Therefore the phy_reset_after_clk_enable() call within
3008 * fec_enet_clk_enable() fails. As we need this reset in order to be
3009 * sure the PHY is working correctly we check if we need to reset again
3010 * later when the PHY is probed
3011 */
3012 if (ndev->phydev && ndev->phydev->drv)
3013 reset_again = false;
3014 else
3015 reset_again = true;
3016
3017 /* I should reset the ring buffers here, but I don't yet know
3018 * a simple way to do that.
3019 */
3020
3021 ret = fec_enet_alloc_buffers(ndev);
3022 if (ret)
3023 goto err_enet_alloc;
3024
3025 /* Init MAC prior to mii bus probe */
3026 fec_restart(ndev);
3027
3028 /* Call phy_reset_after_clk_enable() again if it failed during
3029 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
3030 */
3031 if (reset_again)
3032 fec_enet_phy_reset_after_clk_enable(ndev);
3033
3034 /* Probe and connect to PHY when open the interface */
3035 ret = fec_enet_mii_probe(ndev);
3036 if (ret)
3037 goto err_enet_mii_probe;
3038
3039 if (fep->quirks & FEC_QUIRK_ERR006687)
3040 imx6q_cpuidle_fec_irqs_used();
3041
3042 napi_enable(&fep->napi);
3043 phy_start(ndev->phydev);
3044 netif_tx_start_all_queues(ndev);
3045
3046 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
3047 FEC_WOL_FLAG_ENABLE);
3048
3049 return 0;
3050
3051err_enet_mii_probe:
3052 fec_enet_free_buffers(ndev);
3053err_enet_alloc:
3054 fec_enet_clk_enable(ndev, false);
3055clk_enable:
3056 pm_runtime_mark_last_busy(&fep->pdev->dev);
3057 pm_runtime_put_autosuspend(&fep->pdev->dev);
3058 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3059 return ret;
3060}
3061
3062static int
3063fec_enet_close(struct net_device *ndev)
3064{
3065 struct fec_enet_private *fep = netdev_priv(ndev);
3066
3067 phy_stop(ndev->phydev);
3068
3069 if (netif_device_present(ndev)) {
3070 napi_disable(&fep->napi);
3071 netif_tx_disable(ndev);
3072 fec_stop(ndev);
3073 }
3074
3075 phy_disconnect(ndev->phydev);
3076
3077 if (fep->quirks & FEC_QUIRK_ERR006687)
3078 imx6q_cpuidle_fec_irqs_unused();
3079
3080 fec_enet_update_ethtool_stats(ndev);
3081
3082 fec_enet_clk_enable(ndev, false);
3083 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3084 pm_runtime_mark_last_busy(&fep->pdev->dev);
3085 pm_runtime_put_autosuspend(&fep->pdev->dev);
3086
3087 fec_enet_free_buffers(ndev);
3088
3089 return 0;
3090}
3091
3092/* Set or clear the multicast filter for this adaptor.
3093 * Skeleton taken from sunlance driver.
3094 * The CPM Ethernet implementation allows Multicast as well as individual
3095 * MAC address filtering. Some of the drivers check to make sure it is
3096 * a group multicast address, and discard those that are not. I guess I
3097 * will do the same for now, but just remove the test if you want
3098 * individual filtering as well (do the upper net layers want or support
3099 * this kind of feature?).
3100 */
3101
3102#define FEC_HASH_BITS 6 /* #bits in hash */
3103
3104static void set_multicast_list(struct net_device *ndev)
3105{
3106 struct fec_enet_private *fep = netdev_priv(ndev);
3107 struct netdev_hw_addr *ha;
3108 unsigned int crc, tmp;
3109 unsigned char hash;
3110 unsigned int hash_high = 0, hash_low = 0;
3111
3112 if (ndev->flags & IFF_PROMISC) {
3113 tmp = readl(fep->hwp + FEC_R_CNTRL);
3114 tmp |= 0x8;
3115 writel(tmp, fep->hwp + FEC_R_CNTRL);
3116 return;
3117 }
3118
3119 tmp = readl(fep->hwp + FEC_R_CNTRL);
3120 tmp &= ~0x8;
3121 writel(tmp, fep->hwp + FEC_R_CNTRL);
3122
3123 if (ndev->flags & IFF_ALLMULTI) {
3124 /* Catch all multicast addresses, so set the
3125 * filter to all 1's
3126 */
3127 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3128 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3129
3130 return;
3131 }
3132
3133 /* Add the addresses in hash register */
3134 netdev_for_each_mc_addr(ha, ndev) {
3135 /* calculate crc32 value of mac address */
3136 crc = ether_crc_le(ndev->addr_len, ha->addr);
3137
3138 /* only upper 6 bits (FEC_HASH_BITS) are used
3139 * which point to specific bit in the hash registers
3140 */
3141 hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
3142
3143 if (hash > 31)
3144 hash_high |= 1 << (hash - 32);
3145 else
3146 hash_low |= 1 << hash;
3147 }
3148
3149 writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3150 writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3151}
3152
3153/* Set a MAC change in hardware. */
3154static int
3155fec_set_mac_address(struct net_device *ndev, void *p)
3156{
3157 struct fec_enet_private *fep = netdev_priv(ndev);
3158 struct sockaddr *addr = p;
3159
3160 if (addr) {
3161 if (!is_valid_ether_addr(addr->sa_data))
3162 return -EADDRNOTAVAIL;
3163 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3164 }
3165
3166 /* Add netif status check here to avoid system hang in below case:
3167 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
3168 * After ethx down, fec all clocks are gated off and then register
3169 * access causes system hang.
3170 */
3171 if (!netif_running(ndev))
3172 return 0;
3173
3174 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3175 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3176 fep->hwp + FEC_ADDR_LOW);
3177 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3178 fep->hwp + FEC_ADDR_HIGH);
3179 return 0;
3180}
3181
3182#ifdef CONFIG_NET_POLL_CONTROLLER
3183/**
3184 * fec_poll_controller - FEC Poll controller function
3185 * @dev: The FEC network adapter
3186 *
3187 * Polled functionality used by netconsole and others in non interrupt mode
3188 *
3189 */
3190static void fec_poll_controller(struct net_device *dev)
3191{
3192 int i;
3193 struct fec_enet_private *fep = netdev_priv(dev);
3194
3195 for (i = 0; i < FEC_IRQ_NUM; i++) {
3196 if (fep->irq[i] > 0) {
3197 disable_irq(fep->irq[i]);
3198 fec_enet_interrupt(fep->irq[i], dev);
3199 enable_irq(fep->irq[i]);
3200 }
3201 }
3202}
3203#endif
3204
3205static inline void fec_enet_set_netdev_features(struct net_device *netdev,
3206 netdev_features_t features)
3207{
3208 struct fec_enet_private *fep = netdev_priv(netdev);
3209 netdev_features_t changed = features ^ netdev->features;
3210
3211 netdev->features = features;
3212
3213 /* Receive checksum has been changed */
3214 if (changed & NETIF_F_RXCSUM) {
3215 if (features & NETIF_F_RXCSUM)
3216 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3217 else
3218 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
3219 }
3220}
3221
3222static int fec_set_features(struct net_device *netdev,
3223 netdev_features_t features)
3224{
3225 struct fec_enet_private *fep = netdev_priv(netdev);
3226 netdev_features_t changed = features ^ netdev->features;
3227
3228 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
3229 napi_disable(&fep->napi);
3230 netif_tx_lock_bh(netdev);
3231 fec_stop(netdev);
3232 fec_enet_set_netdev_features(netdev, features);
3233 fec_restart(netdev);
3234 netif_tx_wake_all_queues(netdev);
3235 netif_tx_unlock_bh(netdev);
3236 napi_enable(&fep->napi);
3237 } else {
3238 fec_enet_set_netdev_features(netdev, features);
3239 }
3240
3241 return 0;
3242}
3243
3244static const struct net_device_ops fec_netdev_ops = {
3245 .ndo_open = fec_enet_open,
3246 .ndo_stop = fec_enet_close,
3247 .ndo_start_xmit = fec_enet_start_xmit,
3248 .ndo_set_rx_mode = set_multicast_list,
3249 .ndo_validate_addr = eth_validate_addr,
3250 .ndo_tx_timeout = fec_timeout,
3251 .ndo_set_mac_address = fec_set_mac_address,
3252 .ndo_do_ioctl = fec_enet_ioctl,
3253#ifdef CONFIG_NET_POLL_CONTROLLER
3254 .ndo_poll_controller = fec_poll_controller,
3255#endif
3256 .ndo_set_features = fec_set_features,
3257};
3258
3259static const unsigned short offset_des_active_rxq[] = {
3260 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
3261};
3262
3263static const unsigned short offset_des_active_txq[] = {
3264 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
3265};
3266
3267 /*
3268 * XXX: We need to clean up on failure exits here.
3269 *
3270 */
3271static int fec_enet_init(struct net_device *ndev)
3272{
3273 struct fec_enet_private *fep = netdev_priv(ndev);
3274 struct bufdesc *cbd_base;
3275 dma_addr_t bd_dma;
3276 int bd_size;
3277 unsigned int i;
3278 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3279 sizeof(struct bufdesc);
3280 unsigned dsize_log2 = __fls(dsize);
3281 int ret;
3282
3283 WARN_ON(dsize != (1 << dsize_log2));
3284#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
3285 fep->rx_align = 0xf;
3286 fep->tx_align = 0xf;
3287#else
3288 fep->rx_align = 0x3;
3289 fep->tx_align = 0x3;
3290#endif
3291
3292 /* Check mask of the streaming and coherent API */
3293 ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
3294 if (ret < 0) {
3295 dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
3296 return ret;
3297 }
3298
3299 ret = fec_enet_alloc_queue(ndev);
3300 if (ret)
3301 return ret;
3302
3303 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3304
3305 /* Allocate memory for buffer descriptors. */
3306 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3307 GFP_KERNEL);
3308 if (!cbd_base) {
3309 ret = -ENOMEM;
3310 goto free_queue_mem;
3311 }
3312
3313 /* Get the Ethernet address */
3314 fec_get_mac(ndev);
3315 /* make sure MAC we just acquired is programmed into the hw */
3316 fec_set_mac_address(ndev, NULL);
3317
3318 /* Set receive and transmit descriptor base. */
3319 for (i = 0; i < fep->num_rx_queues; i++) {
3320 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3321 unsigned size = dsize * rxq->bd.ring_size;
3322
3323 rxq->bd.qid = i;
3324 rxq->bd.base = cbd_base;
3325 rxq->bd.cur = cbd_base;
3326 rxq->bd.dma = bd_dma;
3327 rxq->bd.dsize = dsize;
3328 rxq->bd.dsize_log2 = dsize_log2;
3329 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
3330 bd_dma += size;
3331 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3332 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3333 }
3334
3335 for (i = 0; i < fep->num_tx_queues; i++) {
3336 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3337 unsigned size = dsize * txq->bd.ring_size;
3338
3339 txq->bd.qid = i;
3340 txq->bd.base = cbd_base;
3341 txq->bd.cur = cbd_base;
3342 txq->bd.dma = bd_dma;
3343 txq->bd.dsize = dsize;
3344 txq->bd.dsize_log2 = dsize_log2;
3345 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
3346 bd_dma += size;
3347 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3348 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3349 }
3350
3351
3352 /* The FEC Ethernet specific entries in the device structure */
3353 ndev->watchdog_timeo = TX_TIMEOUT;
3354 ndev->netdev_ops = &fec_netdev_ops;
3355 ndev->ethtool_ops = &fec_enet_ethtool_ops;
3356
3357 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3358 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
3359
3360 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3361 /* enable hw VLAN support */
3362 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3363
3364 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
3365 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3366
3367 /* enable hw accelerator */
3368 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3369 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
3370 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3371 }
3372
3373 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
3374 fep->tx_align = 0;
3375 fep->rx_align = 0x3f;
3376 }
3377
3378 ndev->hw_features = ndev->features;
3379
3380 fec_restart(ndev);
3381
3382 if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
3383 fec_enet_clear_ethtool_stats(ndev);
3384 else
3385 fec_enet_update_ethtool_stats(ndev);
3386
3387 return 0;
3388
3389free_queue_mem:
3390 fec_enet_free_queue(ndev);
3391 return ret;
3392}
3393
3394static void fec_enet_deinit(struct net_device *ndev)
3395{
3396 struct fec_enet_private *fep = netdev_priv(ndev);
3397
3398 netif_napi_del(&fep->napi);
3399 fec_enet_free_queue(ndev);
3400}
3401
3402#ifdef CONFIG_OF
3403static int fec_reset_phy(struct platform_device *pdev)
3404{
3405 int err, phy_reset;
3406 bool active_high = false;
3407 int msec = 1, phy_post_delay = 0;
3408 struct device_node *np = pdev->dev.of_node;
3409
3410 if (!np)
3411 return 0;
3412
3413 err = of_property_read_u32(np, "phy-reset-duration", &msec);
3414 /* A sane reset duration should not be longer than 1s */
3415 if (!err && msec > 1000)
3416 msec = 1;
3417
3418 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
3419 if (phy_reset == -EPROBE_DEFER)
3420 return phy_reset;
3421 else if (!gpio_is_valid(phy_reset))
3422 return 0;
3423
3424 err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
3425 /* valid reset duration should be less than 1s */
3426 if (!err && phy_post_delay > 1000)
3427 return -EINVAL;
3428
3429 active_high = of_property_read_bool(np, "phy-reset-active-high");
3430
3431 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3432 active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
3433 "phy-reset");
3434 if (err) {
3435 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
3436 return err;
3437 }
3438
3439 if (msec > 20)
3440 msleep(msec);
3441 else
3442 usleep_range(msec * 1000, msec * 1000 + 1000);
3443
3444 gpio_set_value_cansleep(phy_reset, !active_high);
3445
3446 if (!phy_post_delay)
3447 return 0;
3448
3449 if (phy_post_delay > 20)
3450 msleep(phy_post_delay);
3451 else
3452 usleep_range(phy_post_delay * 1000,
3453 phy_post_delay * 1000 + 1000);
3454
3455 return 0;
3456}
3457#else /* CONFIG_OF */
3458static int fec_reset_phy(struct platform_device *pdev)
3459{
3460 /*
3461 * In case of platform probe, the reset has been done
3462 * by machine code.
3463 */
3464 return 0;
3465}
3466#endif /* CONFIG_OF */
3467
3468static void
3469fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3470{
3471 struct device_node *np = pdev->dev.of_node;
3472
3473 *num_tx = *num_rx = 1;
3474
3475 if (!np || !of_device_is_available(np))
3476 return;
3477
3478 /* parse the num of tx and rx queues */
3479 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3480
3481 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3482
3483 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3484 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3485 *num_tx);
3486 *num_tx = 1;
3487 return;
3488 }
3489
3490 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3491 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3492 *num_rx);
3493 *num_rx = 1;
3494 return;
3495 }
3496
3497}
3498
3499static int fec_enet_get_irq_cnt(struct platform_device *pdev)
3500{
3501 int irq_cnt = platform_irq_count(pdev);
3502
3503 if (irq_cnt > FEC_IRQ_NUM)
3504 irq_cnt = FEC_IRQ_NUM; /* last for pps */
3505 else if (irq_cnt == 2)
3506 irq_cnt = 1; /* last for pps */
3507 else if (irq_cnt <= 0)
3508 irq_cnt = 1; /* At least 1 irq is needed */
3509 return irq_cnt;
3510}
3511
3512static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
3513 struct fec_devinfo *dev_info,
3514 struct device_node *np)
3515{
3516 struct device_node *gpr_np;
3517 int ret = 0;
3518
3519 if (!dev_info)
3520 return 0;
3521
3522 gpr_np = of_parse_phandle(np, "gpr", 0);
3523 if (!gpr_np)
3524 return 0;
3525
3526 fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
3527 if (IS_ERR(fep->stop_gpr.gpr)) {
3528 dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
3529 ret = PTR_ERR(fep->stop_gpr.gpr);
3530 fep->stop_gpr.gpr = NULL;
3531 goto out;
3532 }
3533
3534 fep->stop_gpr.reg = dev_info->stop_gpr_reg;
3535 fep->stop_gpr.bit = dev_info->stop_gpr_bit;
3536
3537out:
3538 of_node_put(gpr_np);
3539
3540 return ret;
3541}
3542
3543static int
3544fec_probe(struct platform_device *pdev)
3545{
3546 struct fec_enet_private *fep;
3547 struct fec_platform_data *pdata;
3548 struct net_device *ndev;
3549 int i, irq, ret = 0;
3550 const struct of_device_id *of_id;
3551 static int dev_id;
3552 struct device_node *np = pdev->dev.of_node, *phy_node;
3553 int num_tx_qs;
3554 int num_rx_qs;
3555 char irq_name[8];
3556 int irq_cnt;
3557 struct fec_devinfo *dev_info;
3558
3559 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3560
3561 /* Init network device */
3562 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
3563 FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
3564 if (!ndev)
3565 return -ENOMEM;
3566
3567 SET_NETDEV_DEV(ndev, &pdev->dev);
3568
3569 /* setup board info structure */
3570 fep = netdev_priv(ndev);
3571
3572 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3573 if (of_id)
3574 pdev->id_entry = of_id->data;
3575 dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
3576 if (dev_info)
3577 fep->quirks = dev_info->quirks;
3578
3579 fep->netdev = ndev;
3580 fep->num_rx_queues = num_rx_qs;
3581 fep->num_tx_queues = num_tx_qs;
3582
3583#if !defined(CONFIG_M5272)
3584 /* default enable pause frame auto negotiation */
3585 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
3586 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3587#endif
3588
3589 /* Select default pin state */
3590 pinctrl_pm_select_default_state(&pdev->dev);
3591
3592 fep->hwp = devm_platform_ioremap_resource(pdev, 0);
3593 if (IS_ERR(fep->hwp)) {
3594 ret = PTR_ERR(fep->hwp);
3595 goto failed_ioremap;
3596 }
3597
3598 fep->pdev = pdev;
3599 fep->dev_id = dev_id++;
3600
3601 platform_set_drvdata(pdev, ndev);
3602
3603 if ((of_machine_is_compatible("fsl,imx6q") ||
3604 of_machine_is_compatible("fsl,imx6dl")) &&
3605 !of_property_read_bool(np, "fsl,err006687-workaround-present"))
3606 fep->quirks |= FEC_QUIRK_ERR006687;
3607
3608 if (of_get_property(np, "fsl,magic-packet", NULL))
3609 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3610
3611 ret = fec_enet_init_stop_mode(fep, dev_info, np);
3612 if (ret)
3613 goto failed_stop_mode;
3614
3615 phy_node = of_parse_phandle(np, "phy-handle", 0);
3616 if (!phy_node && of_phy_is_fixed_link(np)) {
3617 ret = of_phy_register_fixed_link(np);
3618 if (ret < 0) {
3619 dev_err(&pdev->dev,
3620 "broken fixed-link specification\n");
3621 goto failed_phy;
3622 }
3623 phy_node = of_node_get(np);
3624 }
3625 fep->phy_node = phy_node;
3626
3627 ret = of_get_phy_mode(pdev->dev.of_node);
3628 if (ret < 0) {
3629 pdata = dev_get_platdata(&pdev->dev);
3630 if (pdata)
3631 fep->phy_interface = pdata->phy;
3632 else
3633 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3634 } else {
3635 fep->phy_interface = ret;
3636 }
3637
3638 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3639 if (IS_ERR(fep->clk_ipg)) {
3640 ret = PTR_ERR(fep->clk_ipg);
3641 goto failed_clk;
3642 }
3643
3644 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3645 if (IS_ERR(fep->clk_ahb)) {
3646 ret = PTR_ERR(fep->clk_ahb);
3647 goto failed_clk;
3648 }
3649
3650 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3651
3652 /* enet_out is optional, depends on board */
3653 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3654 if (IS_ERR(fep->clk_enet_out))
3655 fep->clk_enet_out = NULL;
3656
3657 fep->ptp_clk_on = false;
3658 mutex_init(&fep->ptp_clk_mutex);
3659
3660 /* clk_ref is optional, depends on board */
3661 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3662 if (IS_ERR(fep->clk_ref))
3663 fep->clk_ref = NULL;
3664
3665 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
3666 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3667 if (IS_ERR(fep->clk_ptp)) {
3668 fep->clk_ptp = NULL;
3669 fep->bufdesc_ex = false;
3670 }
3671
3672 ret = fec_enet_clk_enable(ndev, true);
3673 if (ret)
3674 goto failed_clk;
3675
3676 ret = clk_prepare_enable(fep->clk_ipg);
3677 if (ret)
3678 goto failed_clk_ipg;
3679 ret = clk_prepare_enable(fep->clk_ahb);
3680 if (ret)
3681 goto failed_clk_ahb;
3682
3683 fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
3684 if (!IS_ERR(fep->reg_phy)) {
3685 ret = regulator_enable(fep->reg_phy);
3686 if (ret) {
3687 dev_err(&pdev->dev,
3688 "Failed to enable phy regulator: %d\n", ret);
3689 goto failed_regulator;
3690 }
3691 } else {
3692 if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
3693 ret = -EPROBE_DEFER;
3694 goto failed_regulator;
3695 }
3696 fep->reg_phy = NULL;
3697 }
3698
3699 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3700 pm_runtime_use_autosuspend(&pdev->dev);
3701 pm_runtime_get_noresume(&pdev->dev);
3702 pm_runtime_set_active(&pdev->dev);
3703 pm_runtime_enable(&pdev->dev);
3704
3705 ret = fec_reset_phy(pdev);
3706 if (ret)
3707 goto failed_reset;
3708
3709 irq_cnt = fec_enet_get_irq_cnt(pdev);
3710 if (fep->bufdesc_ex)
3711 fec_ptp_init(pdev, irq_cnt);
3712
3713 ret = fec_enet_init(ndev);
3714 if (ret)
3715 goto failed_init;
3716
3717 for (i = 0; i < irq_cnt; i++) {
3718 snprintf(irq_name, sizeof(irq_name), "int%d", i);
3719 irq = platform_get_irq_byname_optional(pdev, irq_name);
3720 if (irq < 0)
3721 irq = platform_get_irq(pdev, i);
3722 if (irq < 0) {
3723 ret = irq;
3724 goto failed_irq;
3725 }
3726 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
3727 0, pdev->name, ndev);
3728 if (ret)
3729 goto failed_irq;
3730
3731 fep->irq[i] = irq;
3732 }
3733
3734 init_completion(&fep->mdio_done);
3735 ret = fec_enet_mii_init(pdev);
3736 if (ret)
3737 goto failed_mii_init;
3738
3739 /* Carrier starts down, phylib will bring it up */
3740 netif_carrier_off(ndev);
3741 fec_enet_clk_enable(ndev, false);
3742 pinctrl_pm_select_sleep_state(&pdev->dev);
3743
3744 ret = register_netdev(ndev);
3745 if (ret)
3746 goto failed_register;
3747
3748 device_init_wakeup(&ndev->dev, fep->wol_flag &
3749 FEC_WOL_HAS_MAGIC_PACKET);
3750
3751 if (fep->bufdesc_ex && fep->ptp_clock)
3752 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3753
3754 fep->rx_copybreak = COPYBREAK_DEFAULT;
3755 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
3756
3757 pm_runtime_mark_last_busy(&pdev->dev);
3758 pm_runtime_put_autosuspend(&pdev->dev);
3759
3760 return 0;
3761
3762failed_register:
3763 fec_enet_mii_remove(fep);
3764failed_mii_init:
3765failed_irq:
3766 fec_enet_deinit(ndev);
3767failed_init:
3768 fec_ptp_stop(pdev);
3769failed_reset:
3770 pm_runtime_put_noidle(&pdev->dev);
3771 pm_runtime_disable(&pdev->dev);
3772 if (fep->reg_phy)
3773 regulator_disable(fep->reg_phy);
3774failed_regulator:
3775 clk_disable_unprepare(fep->clk_ahb);
3776failed_clk_ahb:
3777 clk_disable_unprepare(fep->clk_ipg);
3778failed_clk_ipg:
3779 fec_enet_clk_enable(ndev, false);
3780failed_clk:
3781 if (of_phy_is_fixed_link(np))
3782 of_phy_deregister_fixed_link(np);
3783 of_node_put(phy_node);
3784failed_stop_mode:
3785failed_phy:
3786 dev_id--;
3787failed_ioremap:
3788 free_netdev(ndev);
3789
3790 return ret;
3791}
3792
3793static int
3794fec_drv_remove(struct platform_device *pdev)
3795{
3796 struct net_device *ndev = platform_get_drvdata(pdev);
3797 struct fec_enet_private *fep = netdev_priv(ndev);
3798 struct device_node *np = pdev->dev.of_node;
3799 int ret;
3800
3801 ret = pm_runtime_get_sync(&pdev->dev);
3802 if (ret < 0)
3803 dev_err(&pdev->dev,
3804 "Failed to resume device in remove callback (%pe)\n",
3805 ERR_PTR(ret));
3806
3807 cancel_work_sync(&fep->tx_timeout_work);
3808 fec_ptp_stop(pdev);
3809 unregister_netdev(ndev);
3810 fec_enet_mii_remove(fep);
3811 if (fep->reg_phy)
3812 regulator_disable(fep->reg_phy);
3813
3814 if (of_phy_is_fixed_link(np))
3815 of_phy_deregister_fixed_link(np);
3816 of_node_put(fep->phy_node);
3817
3818 /* After pm_runtime_get_sync() failed, the clks are still off, so skip
3819 * disabling them again.
3820 */
3821 if (ret >= 0) {
3822 clk_disable_unprepare(fep->clk_ahb);
3823 clk_disable_unprepare(fep->clk_ipg);
3824 }
3825 pm_runtime_put_noidle(&pdev->dev);
3826 pm_runtime_disable(&pdev->dev);
3827
3828 fec_enet_deinit(ndev);
3829 free_netdev(ndev);
3830 return 0;
3831}
3832
3833static int __maybe_unused fec_suspend(struct device *dev)
3834{
3835 struct net_device *ndev = dev_get_drvdata(dev);
3836 struct fec_enet_private *fep = netdev_priv(ndev);
3837
3838 rtnl_lock();
3839 if (netif_running(ndev)) {
3840 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3841 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
3842 phy_stop(ndev->phydev);
3843 napi_disable(&fep->napi);
3844 netif_tx_lock_bh(ndev);
3845 netif_device_detach(ndev);
3846 netif_tx_unlock_bh(ndev);
3847 fec_stop(ndev);
3848 fec_enet_clk_enable(ndev, false);
3849 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3850 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3851 }
3852 rtnl_unlock();
3853
3854 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3855 regulator_disable(fep->reg_phy);
3856
3857 /* SOC supply clock to phy, when clock is disabled, phy link down
3858 * SOC control phy regulator, when regulator is disabled, phy link down
3859 */
3860 if (fep->clk_enet_out || fep->reg_phy)
3861 fep->link = 0;
3862
3863 return 0;
3864}
3865
3866static int __maybe_unused fec_resume(struct device *dev)
3867{
3868 struct net_device *ndev = dev_get_drvdata(dev);
3869 struct fec_enet_private *fep = netdev_priv(ndev);
3870 int ret;
3871 int val;
3872
3873 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
3874 ret = regulator_enable(fep->reg_phy);
3875 if (ret)
3876 return ret;
3877 }
3878
3879 rtnl_lock();
3880 if (netif_running(ndev)) {
3881 ret = fec_enet_clk_enable(ndev, true);
3882 if (ret) {
3883 rtnl_unlock();
3884 goto failed_clk;
3885 }
3886 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3887 fec_enet_stop_mode(fep, false);
3888
3889 val = readl(fep->hwp + FEC_ECNTRL);
3890 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3891 writel(val, fep->hwp + FEC_ECNTRL);
3892 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3893 } else {
3894 pinctrl_pm_select_default_state(&fep->pdev->dev);
3895 }
3896 fec_restart(ndev);
3897 netif_tx_lock_bh(ndev);
3898 netif_device_attach(ndev);
3899 netif_tx_unlock_bh(ndev);
3900 napi_enable(&fep->napi);
3901 phy_start(ndev->phydev);
3902 }
3903 rtnl_unlock();
3904
3905 return 0;
3906
3907failed_clk:
3908 if (fep->reg_phy)
3909 regulator_disable(fep->reg_phy);
3910 return ret;
3911}
3912
3913static int __maybe_unused fec_runtime_suspend(struct device *dev)
3914{
3915 struct net_device *ndev = dev_get_drvdata(dev);
3916 struct fec_enet_private *fep = netdev_priv(ndev);
3917
3918 clk_disable_unprepare(fep->clk_ahb);
3919 clk_disable_unprepare(fep->clk_ipg);
3920
3921 return 0;
3922}
3923
3924static int __maybe_unused fec_runtime_resume(struct device *dev)
3925{
3926 struct net_device *ndev = dev_get_drvdata(dev);
3927 struct fec_enet_private *fep = netdev_priv(ndev);
3928 int ret;
3929
3930 ret = clk_prepare_enable(fep->clk_ahb);
3931 if (ret)
3932 return ret;
3933 ret = clk_prepare_enable(fep->clk_ipg);
3934 if (ret)
3935 goto failed_clk_ipg;
3936
3937 return 0;
3938
3939failed_clk_ipg:
3940 clk_disable_unprepare(fep->clk_ahb);
3941 return ret;
3942}
3943
3944static const struct dev_pm_ops fec_pm_ops = {
3945 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3946 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3947};
3948
3949static struct platform_driver fec_driver = {
3950 .driver = {
3951 .name = DRIVER_NAME,
3952 .pm = &fec_pm_ops,
3953 .of_match_table = fec_dt_ids,
3954 },
3955 .id_table = fec_devtype,
3956 .probe = fec_probe,
3957 .remove = fec_drv_remove,
3958};
3959
3960module_platform_driver(fec_driver);
3961
3962MODULE_ALIAS("platform:"DRIVER_NAME);
3963MODULE_LICENSE("GPL");