2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5 * Right now, I am very wasteful with the buffers. I allocate memory
6 * pages and then divide them into 2K frame buffers. This way I know I
7 * have buffers large enough to hold one frame within one buffer descriptor.
8 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9 * will be much more memory efficient and will easily handle lots of
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
15 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
19 * Copyright (c) 2004-2006 Macq Electronique SA.
21 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/ptrace.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/delay.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
41 #include <linux/tcp.h>
42 #include <linux/udp.h>
43 #include <linux/icmp.h>
44 #include <linux/spinlock.h>
45 #include <linux/workqueue.h>
46 #include <linux/bitops.h>
48 #include <linux/irq.h>
49 #include <linux/clk.h>
50 #include <linux/platform_device.h>
51 #include <linux/mdio.h>
52 #include <linux/phy.h>
53 #include <linux/fec.h>
55 #include <linux/of_device.h>
56 #include <linux/of_gpio.h>
57 #include <linux/of_mdio.h>
58 #include <linux/of_net.h>
59 #include <linux/regulator/consumer.h>
60 #include <linux/if_vlan.h>
61 #include <linux/pinctrl/consumer.h>
62 #include <linux/prefetch.h>
64 #include <asm/cacheflush.h>
68 static void set_multicast_list(struct net_device
*ndev
);
69 static void fec_enet_itr_coal_init(struct net_device
*ndev
);
71 #define DRIVER_NAME "fec"
73 #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
75 /* Pause frame feild and FIFO threshold */
76 #define FEC_ENET_FCE (1 << 5)
77 #define FEC_ENET_RSEM_V 0x84
78 #define FEC_ENET_RSFL_V 16
79 #define FEC_ENET_RAEM_V 0x8
80 #define FEC_ENET_RAFL_V 0x8
81 #define FEC_ENET_OPD_V 0xFFF0
82 #define FEC_MDIO_PM_TIMEOUT 100 /* ms */
84 static struct platform_device_id fec_devtype
[] = {
86 /* keep it for coldfire */
91 .driver_data
= FEC_QUIRK_USE_GASKET
| FEC_QUIRK_HAS_RACC
,
94 .driver_data
= FEC_QUIRK_HAS_RACC
,
97 .driver_data
= FEC_QUIRK_ENET_MAC
| FEC_QUIRK_SWAP_FRAME
|
98 FEC_QUIRK_SINGLE_MDIO
| FEC_QUIRK_HAS_RACC
,
101 .driver_data
= FEC_QUIRK_ENET_MAC
| FEC_QUIRK_HAS_GBIT
|
102 FEC_QUIRK_HAS_BUFDESC_EX
| FEC_QUIRK_HAS_CSUM
|
103 FEC_QUIRK_HAS_VLAN
| FEC_QUIRK_ERR006358
|
106 .name
= "mvf600-fec",
107 .driver_data
= FEC_QUIRK_ENET_MAC
| FEC_QUIRK_HAS_RACC
,
109 .name
= "imx6sx-fec",
110 .driver_data
= FEC_QUIRK_ENET_MAC
| FEC_QUIRK_HAS_GBIT
|
111 FEC_QUIRK_HAS_BUFDESC_EX
| FEC_QUIRK_HAS_CSUM
|
112 FEC_QUIRK_HAS_VLAN
| FEC_QUIRK_HAS_AVB
|
113 FEC_QUIRK_ERR007885
| FEC_QUIRK_BUG_CAPTURE
|
119 MODULE_DEVICE_TABLE(platform
, fec_devtype
);
122 IMX25_FEC
= 1, /* runs on i.mx25/50/53 */
123 IMX27_FEC
, /* runs on i.mx27/35/51 */
130 static const struct of_device_id fec_dt_ids
[] = {
131 { .compatible
= "fsl,imx25-fec", .data
= &fec_devtype
[IMX25_FEC
], },
132 { .compatible
= "fsl,imx27-fec", .data
= &fec_devtype
[IMX27_FEC
], },
133 { .compatible
= "fsl,imx28-fec", .data
= &fec_devtype
[IMX28_FEC
], },
134 { .compatible
= "fsl,imx6q-fec", .data
= &fec_devtype
[IMX6Q_FEC
], },
135 { .compatible
= "fsl,mvf600-fec", .data
= &fec_devtype
[MVF600_FEC
], },
136 { .compatible
= "fsl,imx6sx-fec", .data
= &fec_devtype
[IMX6SX_FEC
], },
139 MODULE_DEVICE_TABLE(of
, fec_dt_ids
);
141 static unsigned char macaddr
[ETH_ALEN
];
142 module_param_array(macaddr
, byte
, NULL
, 0);
143 MODULE_PARM_DESC(macaddr
, "FEC Ethernet MAC address");
145 #if defined(CONFIG_M5272)
147 * Some hardware gets it MAC address out of local flash memory.
148 * if this is non-zero then assume it is the address to get MAC from.
150 #if defined(CONFIG_NETtel)
151 #define FEC_FLASHMAC 0xf0006006
152 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
153 #define FEC_FLASHMAC 0xf0006000
154 #elif defined(CONFIG_CANCam)
155 #define FEC_FLASHMAC 0xf0020000
156 #elif defined (CONFIG_M5272C3)
157 #define FEC_FLASHMAC (0xffe04000 + 4)
158 #elif defined(CONFIG_MOD5272)
159 #define FEC_FLASHMAC 0xffc0406b
161 #define FEC_FLASHMAC 0
163 #endif /* CONFIG_M5272 */
165 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
167 #define PKT_MAXBUF_SIZE 1522
168 #define PKT_MINBUF_SIZE 64
169 #define PKT_MAXBLR_SIZE 1536
171 /* FEC receive acceleration */
172 #define FEC_RACC_IPDIS (1 << 1)
173 #define FEC_RACC_PRODIS (1 << 2)
174 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
177 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
178 * size bits. Other FEC hardware does not, so we need to take that into
179 * account when setting it.
181 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
182 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
183 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
185 #define OPT_FRAME_SIZE 0
188 /* FEC MII MMFR bits definition */
189 #define FEC_MMFR_ST (1 << 30)
190 #define FEC_MMFR_OP_READ (2 << 28)
191 #define FEC_MMFR_OP_WRITE (1 << 28)
192 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
193 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
194 #define FEC_MMFR_TA (2 << 16)
195 #define FEC_MMFR_DATA(v) (v & 0xffff)
196 /* FEC ECR bits definition */
197 #define FEC_ECR_MAGICEN (1 << 2)
198 #define FEC_ECR_SLEEP (1 << 3)
200 #define FEC_MII_TIMEOUT 30000 /* us */
202 /* Transmitter timeout */
203 #define TX_TIMEOUT (2 * HZ)
205 #define FEC_PAUSE_FLAG_AUTONEG 0x1
206 #define FEC_PAUSE_FLAG_ENABLE 0x2
207 #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
208 #define FEC_WOL_FLAG_ENABLE (0x1 << 1)
209 #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
211 #define COPYBREAK_DEFAULT 256
213 #define TSO_HEADER_SIZE 128
214 /* Max number of allowed TCP segments for software TSO */
215 #define FEC_MAX_TSO_SEGS 100
216 #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
218 #define IS_TSO_HEADER(txq, addr) \
219 ((addr >= txq->tso_hdrs_dma) && \
220 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
225 struct bufdesc
*fec_enet_get_nextdesc(struct bufdesc
*bdp
,
226 struct fec_enet_private
*fep
,
229 struct bufdesc
*new_bd
= bdp
+ 1;
230 struct bufdesc_ex
*ex_new_bd
= (struct bufdesc_ex
*)bdp
+ 1;
231 struct fec_enet_priv_tx_q
*txq
= fep
->tx_queue
[queue_id
];
232 struct fec_enet_priv_rx_q
*rxq
= fep
->rx_queue
[queue_id
];
233 struct bufdesc_ex
*ex_base
;
234 struct bufdesc
*base
;
237 if (bdp
>= txq
->tx_bd_base
) {
238 base
= txq
->tx_bd_base
;
239 ring_size
= txq
->tx_ring_size
;
240 ex_base
= (struct bufdesc_ex
*)txq
->tx_bd_base
;
242 base
= rxq
->rx_bd_base
;
243 ring_size
= rxq
->rx_ring_size
;
244 ex_base
= (struct bufdesc_ex
*)rxq
->rx_bd_base
;
248 return (struct bufdesc
*)((ex_new_bd
>= (ex_base
+ ring_size
)) ?
249 ex_base
: ex_new_bd
);
251 return (new_bd
>= (base
+ ring_size
)) ?
256 struct bufdesc
*fec_enet_get_prevdesc(struct bufdesc
*bdp
,
257 struct fec_enet_private
*fep
,
260 struct bufdesc
*new_bd
= bdp
- 1;
261 struct bufdesc_ex
*ex_new_bd
= (struct bufdesc_ex
*)bdp
- 1;
262 struct fec_enet_priv_tx_q
*txq
= fep
->tx_queue
[queue_id
];
263 struct fec_enet_priv_rx_q
*rxq
= fep
->rx_queue
[queue_id
];
264 struct bufdesc_ex
*ex_base
;
265 struct bufdesc
*base
;
268 if (bdp
>= txq
->tx_bd_base
) {
269 base
= txq
->tx_bd_base
;
270 ring_size
= txq
->tx_ring_size
;
271 ex_base
= (struct bufdesc_ex
*)txq
->tx_bd_base
;
273 base
= rxq
->rx_bd_base
;
274 ring_size
= rxq
->rx_ring_size
;
275 ex_base
= (struct bufdesc_ex
*)rxq
->rx_bd_base
;
279 return (struct bufdesc
*)((ex_new_bd
< ex_base
) ?
280 (ex_new_bd
+ ring_size
) : ex_new_bd
);
282 return (new_bd
< base
) ? (new_bd
+ ring_size
) : new_bd
;
285 static int fec_enet_get_bd_index(struct bufdesc
*base
, struct bufdesc
*bdp
,
286 struct fec_enet_private
*fep
)
288 return ((const char *)bdp
- (const char *)base
) / fep
->bufdesc_size
;
291 static int fec_enet_get_free_txdesc_num(struct fec_enet_private
*fep
,
292 struct fec_enet_priv_tx_q
*txq
)
296 entries
= ((const char *)txq
->dirty_tx
-
297 (const char *)txq
->cur_tx
) / fep
->bufdesc_size
- 1;
299 return entries
> 0 ? entries
: entries
+ txq
->tx_ring_size
;
302 static void swap_buffer(void *bufaddr
, int len
)
305 unsigned int *buf
= bufaddr
;
307 for (i
= 0; i
< len
; i
+= 4, buf
++)
311 static void swap_buffer2(void *dst_buf
, void *src_buf
, int len
)
314 unsigned int *src
= src_buf
;
315 unsigned int *dst
= dst_buf
;
317 for (i
= 0; i
< len
; i
+= 4, src
++, dst
++)
321 static void fec_dump(struct net_device
*ndev
)
323 struct fec_enet_private
*fep
= netdev_priv(ndev
);
325 struct fec_enet_priv_tx_q
*txq
;
328 netdev_info(ndev
, "TX ring dump\n");
329 pr_info("Nr SC addr len SKB\n");
331 txq
= fep
->tx_queue
[0];
332 bdp
= txq
->tx_bd_base
;
335 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
337 bdp
== txq
->cur_tx
? 'S' : ' ',
338 bdp
== txq
->dirty_tx
? 'H' : ' ',
339 bdp
->cbd_sc
, bdp
->cbd_bufaddr
, bdp
->cbd_datlen
,
340 txq
->tx_skbuff
[index
]);
341 bdp
= fec_enet_get_nextdesc(bdp
, fep
, 0);
343 } while (bdp
!= txq
->tx_bd_base
);
346 static inline bool is_ipv4_pkt(struct sk_buff
*skb
)
348 return skb
->protocol
== htons(ETH_P_IP
) && ip_hdr(skb
)->version
== 4;
352 fec_enet_clear_csum(struct sk_buff
*skb
, struct net_device
*ndev
)
354 /* Only run for packets requiring a checksum. */
355 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
358 if (unlikely(skb_cow_head(skb
, 0)))
361 if (is_ipv4_pkt(skb
))
362 ip_hdr(skb
)->check
= 0;
363 *(__sum16
*)(skb
->head
+ skb
->csum_start
+ skb
->csum_offset
) = 0;
368 static struct bufdesc
*
369 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q
*txq
,
371 struct net_device
*ndev
)
373 struct fec_enet_private
*fep
= netdev_priv(ndev
);
374 struct bufdesc
*bdp
= txq
->cur_tx
;
375 struct bufdesc_ex
*ebdp
;
376 int nr_frags
= skb_shinfo(skb
)->nr_frags
;
377 unsigned short queue
= skb_get_queue_mapping(skb
);
379 unsigned short status
;
380 unsigned int estatus
= 0;
381 skb_frag_t
*this_frag
;
387 for (frag
= 0; frag
< nr_frags
; frag
++) {
388 this_frag
= &skb_shinfo(skb
)->frags
[frag
];
389 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue
);
390 ebdp
= (struct bufdesc_ex
*)bdp
;
392 status
= bdp
->cbd_sc
;
393 status
&= ~BD_ENET_TX_STATS
;
394 status
|= (BD_ENET_TX_TC
| BD_ENET_TX_READY
);
395 frag_len
= skb_shinfo(skb
)->frags
[frag
].size
;
397 /* Handle the last BD specially */
398 if (frag
== nr_frags
- 1) {
399 status
|= (BD_ENET_TX_INTR
| BD_ENET_TX_LAST
);
400 if (fep
->bufdesc_ex
) {
401 estatus
|= BD_ENET_TX_INT
;
402 if (unlikely(skb_shinfo(skb
)->tx_flags
&
403 SKBTX_HW_TSTAMP
&& fep
->hwts_tx_en
))
404 estatus
|= BD_ENET_TX_TS
;
408 if (fep
->bufdesc_ex
) {
409 if (fep
->quirks
& FEC_QUIRK_HAS_AVB
)
410 estatus
|= FEC_TX_BD_FTYPE(queue
);
411 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
412 estatus
|= BD_ENET_TX_PINS
| BD_ENET_TX_IINS
;
414 ebdp
->cbd_esc
= estatus
;
417 bufaddr
= page_address(this_frag
->page
.p
) + this_frag
->page_offset
;
419 index
= fec_enet_get_bd_index(txq
->tx_bd_base
, bdp
, fep
);
420 if (((unsigned long) bufaddr
) & fep
->tx_align
||
421 fep
->quirks
& FEC_QUIRK_SWAP_FRAME
) {
422 memcpy(txq
->tx_bounce
[index
], bufaddr
, frag_len
);
423 bufaddr
= txq
->tx_bounce
[index
];
425 if (fep
->quirks
& FEC_QUIRK_SWAP_FRAME
)
426 swap_buffer(bufaddr
, frag_len
);
429 addr
= dma_map_single(&fep
->pdev
->dev
, bufaddr
, frag_len
,
431 if (dma_mapping_error(&fep
->pdev
->dev
, addr
)) {
432 dev_kfree_skb_any(skb
);
434 netdev_err(ndev
, "Tx DMA memory map failed\n");
435 goto dma_mapping_error
;
438 bdp
->cbd_bufaddr
= addr
;
439 bdp
->cbd_datlen
= frag_len
;
440 bdp
->cbd_sc
= status
;
446 for (i
= 0; i
< frag
; i
++) {
447 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue
);
448 dma_unmap_single(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
449 bdp
->cbd_datlen
, DMA_TO_DEVICE
);
451 return ERR_PTR(-ENOMEM
);
454 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q
*txq
,
455 struct sk_buff
*skb
, struct net_device
*ndev
)
457 struct fec_enet_private
*fep
= netdev_priv(ndev
);
458 int nr_frags
= skb_shinfo(skb
)->nr_frags
;
459 struct bufdesc
*bdp
, *last_bdp
;
462 unsigned short status
;
463 unsigned short buflen
;
464 unsigned short queue
;
465 unsigned int estatus
= 0;
469 entries_free
= fec_enet_get_free_txdesc_num(fep
, txq
);
470 if (entries_free
< MAX_SKB_FRAGS
+ 1) {
471 dev_kfree_skb_any(skb
);
473 netdev_err(ndev
, "NOT enough BD for SG!\n");
477 /* Protocol checksum off-load for TCP and UDP. */
478 if (fec_enet_clear_csum(skb
, ndev
)) {
479 dev_kfree_skb_any(skb
);
483 /* Fill in a Tx ring entry */
486 status
= bdp
->cbd_sc
;
487 status
&= ~BD_ENET_TX_STATS
;
489 /* Set buffer length and buffer pointer */
491 buflen
= skb_headlen(skb
);
493 queue
= skb_get_queue_mapping(skb
);
494 index
= fec_enet_get_bd_index(txq
->tx_bd_base
, bdp
, fep
);
495 if (((unsigned long) bufaddr
) & fep
->tx_align
||
496 fep
->quirks
& FEC_QUIRK_SWAP_FRAME
) {
497 memcpy(txq
->tx_bounce
[index
], skb
->data
, buflen
);
498 bufaddr
= txq
->tx_bounce
[index
];
500 if (fep
->quirks
& FEC_QUIRK_SWAP_FRAME
)
501 swap_buffer(bufaddr
, buflen
);
504 /* Push the data cache so the CPM does not get stale memory data. */
505 addr
= dma_map_single(&fep
->pdev
->dev
, bufaddr
, buflen
, DMA_TO_DEVICE
);
506 if (dma_mapping_error(&fep
->pdev
->dev
, addr
)) {
507 dev_kfree_skb_any(skb
);
509 netdev_err(ndev
, "Tx DMA memory map failed\n");
514 last_bdp
= fec_enet_txq_submit_frag_skb(txq
, skb
, ndev
);
515 if (IS_ERR(last_bdp
))
518 status
|= (BD_ENET_TX_INTR
| BD_ENET_TX_LAST
);
519 if (fep
->bufdesc_ex
) {
520 estatus
= BD_ENET_TX_INT
;
521 if (unlikely(skb_shinfo(skb
)->tx_flags
&
522 SKBTX_HW_TSTAMP
&& fep
->hwts_tx_en
))
523 estatus
|= BD_ENET_TX_TS
;
527 if (fep
->bufdesc_ex
) {
529 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
531 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
&&
533 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
535 if (fep
->quirks
& FEC_QUIRK_HAS_AVB
)
536 estatus
|= FEC_TX_BD_FTYPE(queue
);
538 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
539 estatus
|= BD_ENET_TX_PINS
| BD_ENET_TX_IINS
;
542 ebdp
->cbd_esc
= estatus
;
545 index
= fec_enet_get_bd_index(txq
->tx_bd_base
, last_bdp
, fep
);
546 /* Save skb pointer */
547 txq
->tx_skbuff
[index
] = skb
;
549 bdp
->cbd_datlen
= buflen
;
550 bdp
->cbd_bufaddr
= addr
;
552 /* Send it on its way. Tell FEC it's ready, interrupt when done,
553 * it's the last BD of the frame, and to put the CRC on the end.
555 status
|= (BD_ENET_TX_READY
| BD_ENET_TX_TC
);
556 bdp
->cbd_sc
= status
;
558 /* If this was the last BD in the ring, start at the beginning again. */
559 bdp
= fec_enet_get_nextdesc(last_bdp
, fep
, queue
);
561 skb_tx_timestamp(skb
);
563 /* Make sure the update to bdp and tx_skbuff are performed before
569 /* Trigger transmission start */
570 writel(0, fep
->hwp
+ FEC_X_DES_ACTIVE(queue
));
576 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q
*txq
, struct sk_buff
*skb
,
577 struct net_device
*ndev
,
578 struct bufdesc
*bdp
, int index
, char *data
,
579 int size
, bool last_tcp
, bool is_last
)
581 struct fec_enet_private
*fep
= netdev_priv(ndev
);
582 struct bufdesc_ex
*ebdp
= container_of(bdp
, struct bufdesc_ex
, desc
);
583 unsigned short queue
= skb_get_queue_mapping(skb
);
584 unsigned short status
;
585 unsigned int estatus
= 0;
588 status
= bdp
->cbd_sc
;
589 status
&= ~BD_ENET_TX_STATS
;
591 status
|= (BD_ENET_TX_TC
| BD_ENET_TX_READY
);
593 if (((unsigned long) data
) & fep
->tx_align
||
594 fep
->quirks
& FEC_QUIRK_SWAP_FRAME
) {
595 memcpy(txq
->tx_bounce
[index
], data
, size
);
596 data
= txq
->tx_bounce
[index
];
598 if (fep
->quirks
& FEC_QUIRK_SWAP_FRAME
)
599 swap_buffer(data
, size
);
602 addr
= dma_map_single(&fep
->pdev
->dev
, data
, size
, DMA_TO_DEVICE
);
603 if (dma_mapping_error(&fep
->pdev
->dev
, addr
)) {
604 dev_kfree_skb_any(skb
);
606 netdev_err(ndev
, "Tx DMA memory map failed\n");
607 return NETDEV_TX_BUSY
;
610 bdp
->cbd_datlen
= size
;
611 bdp
->cbd_bufaddr
= addr
;
613 if (fep
->bufdesc_ex
) {
614 if (fep
->quirks
& FEC_QUIRK_HAS_AVB
)
615 estatus
|= FEC_TX_BD_FTYPE(queue
);
616 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
617 estatus
|= BD_ENET_TX_PINS
| BD_ENET_TX_IINS
;
619 ebdp
->cbd_esc
= estatus
;
622 /* Handle the last BD specially */
624 status
|= (BD_ENET_TX_LAST
| BD_ENET_TX_TC
);
626 status
|= BD_ENET_TX_INTR
;
628 ebdp
->cbd_esc
|= BD_ENET_TX_INT
;
631 bdp
->cbd_sc
= status
;
637 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q
*txq
,
638 struct sk_buff
*skb
, struct net_device
*ndev
,
639 struct bufdesc
*bdp
, int index
)
641 struct fec_enet_private
*fep
= netdev_priv(ndev
);
642 int hdr_len
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
643 struct bufdesc_ex
*ebdp
= container_of(bdp
, struct bufdesc_ex
, desc
);
644 unsigned short queue
= skb_get_queue_mapping(skb
);
646 unsigned long dmabuf
;
647 unsigned short status
;
648 unsigned int estatus
= 0;
650 status
= bdp
->cbd_sc
;
651 status
&= ~BD_ENET_TX_STATS
;
652 status
|= (BD_ENET_TX_TC
| BD_ENET_TX_READY
);
654 bufaddr
= txq
->tso_hdrs
+ index
* TSO_HEADER_SIZE
;
655 dmabuf
= txq
->tso_hdrs_dma
+ index
* TSO_HEADER_SIZE
;
656 if (((unsigned long)bufaddr
) & fep
->tx_align
||
657 fep
->quirks
& FEC_QUIRK_SWAP_FRAME
) {
658 memcpy(txq
->tx_bounce
[index
], skb
->data
, hdr_len
);
659 bufaddr
= txq
->tx_bounce
[index
];
661 if (fep
->quirks
& FEC_QUIRK_SWAP_FRAME
)
662 swap_buffer(bufaddr
, hdr_len
);
664 dmabuf
= dma_map_single(&fep
->pdev
->dev
, bufaddr
,
665 hdr_len
, DMA_TO_DEVICE
);
666 if (dma_mapping_error(&fep
->pdev
->dev
, dmabuf
)) {
667 dev_kfree_skb_any(skb
);
669 netdev_err(ndev
, "Tx DMA memory map failed\n");
670 return NETDEV_TX_BUSY
;
674 bdp
->cbd_bufaddr
= dmabuf
;
675 bdp
->cbd_datlen
= hdr_len
;
677 if (fep
->bufdesc_ex
) {
678 if (fep
->quirks
& FEC_QUIRK_HAS_AVB
)
679 estatus
|= FEC_TX_BD_FTYPE(queue
);
680 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
681 estatus
|= BD_ENET_TX_PINS
| BD_ENET_TX_IINS
;
683 ebdp
->cbd_esc
= estatus
;
686 bdp
->cbd_sc
= status
;
691 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q
*txq
,
693 struct net_device
*ndev
)
695 struct fec_enet_private
*fep
= netdev_priv(ndev
);
696 int hdr_len
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
697 int total_len
, data_left
;
698 struct bufdesc
*bdp
= txq
->cur_tx
;
699 unsigned short queue
= skb_get_queue_mapping(skb
);
701 unsigned int index
= 0;
704 if (tso_count_descs(skb
) >= fec_enet_get_free_txdesc_num(fep
, txq
)) {
705 dev_kfree_skb_any(skb
);
707 netdev_err(ndev
, "NOT enough BD for TSO!\n");
711 /* Protocol checksum off-load for TCP and UDP. */
712 if (fec_enet_clear_csum(skb
, ndev
)) {
713 dev_kfree_skb_any(skb
);
717 /* Initialize the TSO handler, and prepare the first payload */
718 tso_start(skb
, &tso
);
720 total_len
= skb
->len
- hdr_len
;
721 while (total_len
> 0) {
724 index
= fec_enet_get_bd_index(txq
->tx_bd_base
, bdp
, fep
);
725 data_left
= min_t(int, skb_shinfo(skb
)->gso_size
, total_len
);
726 total_len
-= data_left
;
728 /* prepare packet headers: MAC + IP + TCP */
729 hdr
= txq
->tso_hdrs
+ index
* TSO_HEADER_SIZE
;
730 tso_build_hdr(skb
, hdr
, &tso
, data_left
, total_len
== 0);
731 ret
= fec_enet_txq_put_hdr_tso(txq
, skb
, ndev
, bdp
, index
);
735 while (data_left
> 0) {
738 size
= min_t(int, tso
.size
, data_left
);
739 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue
);
740 index
= fec_enet_get_bd_index(txq
->tx_bd_base
,
742 ret
= fec_enet_txq_put_data_tso(txq
, skb
, ndev
,
751 tso_build_data(skb
, &tso
, size
);
754 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue
);
757 /* Save skb pointer */
758 txq
->tx_skbuff
[index
] = skb
;
760 skb_tx_timestamp(skb
);
763 /* Trigger transmission start */
764 if (!(fep
->quirks
& FEC_QUIRK_ERR007885
) ||
765 !readl(fep
->hwp
+ FEC_X_DES_ACTIVE(queue
)) ||
766 !readl(fep
->hwp
+ FEC_X_DES_ACTIVE(queue
)) ||
767 !readl(fep
->hwp
+ FEC_X_DES_ACTIVE(queue
)) ||
768 !readl(fep
->hwp
+ FEC_X_DES_ACTIVE(queue
)))
769 writel(0, fep
->hwp
+ FEC_X_DES_ACTIVE(queue
));
774 /* TODO: Release all used data descriptors for TSO */
779 fec_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
781 struct fec_enet_private
*fep
= netdev_priv(ndev
);
783 unsigned short queue
;
784 struct fec_enet_priv_tx_q
*txq
;
785 struct netdev_queue
*nq
;
788 queue
= skb_get_queue_mapping(skb
);
789 txq
= fep
->tx_queue
[queue
];
790 nq
= netdev_get_tx_queue(ndev
, queue
);
793 ret
= fec_enet_txq_submit_tso(txq
, skb
, ndev
);
795 ret
= fec_enet_txq_submit_skb(txq
, skb
, ndev
);
799 entries_free
= fec_enet_get_free_txdesc_num(fep
, txq
);
800 if (entries_free
<= txq
->tx_stop_threshold
)
801 netif_tx_stop_queue(nq
);
806 /* Init RX & TX buffer descriptors
808 static void fec_enet_bd_init(struct net_device
*dev
)
810 struct fec_enet_private
*fep
= netdev_priv(dev
);
811 struct fec_enet_priv_tx_q
*txq
;
812 struct fec_enet_priv_rx_q
*rxq
;
817 for (q
= 0; q
< fep
->num_rx_queues
; q
++) {
818 /* Initialize the receive buffer descriptors. */
819 rxq
= fep
->rx_queue
[q
];
820 bdp
= rxq
->rx_bd_base
;
822 for (i
= 0; i
< rxq
->rx_ring_size
; i
++) {
824 /* Initialize the BD for every fragment in the page. */
825 if (bdp
->cbd_bufaddr
)
826 bdp
->cbd_sc
= BD_ENET_RX_EMPTY
;
829 bdp
= fec_enet_get_nextdesc(bdp
, fep
, q
);
832 /* Set the last buffer to wrap */
833 bdp
= fec_enet_get_prevdesc(bdp
, fep
, q
);
834 bdp
->cbd_sc
|= BD_SC_WRAP
;
836 rxq
->cur_rx
= rxq
->rx_bd_base
;
839 for (q
= 0; q
< fep
->num_tx_queues
; q
++) {
840 /* ...and the same for transmit */
841 txq
= fep
->tx_queue
[q
];
842 bdp
= txq
->tx_bd_base
;
845 for (i
= 0; i
< txq
->tx_ring_size
; i
++) {
846 /* Initialize the BD for every fragment in the page. */
848 if (txq
->tx_skbuff
[i
]) {
849 dev_kfree_skb_any(txq
->tx_skbuff
[i
]);
850 txq
->tx_skbuff
[i
] = NULL
;
852 bdp
->cbd_bufaddr
= 0;
853 bdp
= fec_enet_get_nextdesc(bdp
, fep
, q
);
856 /* Set the last buffer to wrap */
857 bdp
= fec_enet_get_prevdesc(bdp
, fep
, q
);
858 bdp
->cbd_sc
|= BD_SC_WRAP
;
863 static void fec_enet_active_rxring(struct net_device
*ndev
)
865 struct fec_enet_private
*fep
= netdev_priv(ndev
);
868 for (i
= 0; i
< fep
->num_rx_queues
; i
++)
869 writel(0, fep
->hwp
+ FEC_R_DES_ACTIVE(i
));
872 static void fec_enet_enable_ring(struct net_device
*ndev
)
874 struct fec_enet_private
*fep
= netdev_priv(ndev
);
875 struct fec_enet_priv_tx_q
*txq
;
876 struct fec_enet_priv_rx_q
*rxq
;
879 for (i
= 0; i
< fep
->num_rx_queues
; i
++) {
880 rxq
= fep
->rx_queue
[i
];
881 writel(rxq
->bd_dma
, fep
->hwp
+ FEC_R_DES_START(i
));
882 writel(PKT_MAXBLR_SIZE
, fep
->hwp
+ FEC_R_BUFF_SIZE(i
));
886 writel(RCMR_MATCHEN
| RCMR_CMP(i
),
887 fep
->hwp
+ FEC_RCMR(i
));
890 for (i
= 0; i
< fep
->num_tx_queues
; i
++) {
891 txq
= fep
->tx_queue
[i
];
892 writel(txq
->bd_dma
, fep
->hwp
+ FEC_X_DES_START(i
));
896 writel(DMA_CLASS_EN
| IDLE_SLOPE(i
),
897 fep
->hwp
+ FEC_DMA_CFG(i
));
901 static void fec_enet_reset_skb(struct net_device
*ndev
)
903 struct fec_enet_private
*fep
= netdev_priv(ndev
);
904 struct fec_enet_priv_tx_q
*txq
;
907 for (i
= 0; i
< fep
->num_tx_queues
; i
++) {
908 txq
= fep
->tx_queue
[i
];
910 for (j
= 0; j
< txq
->tx_ring_size
; j
++) {
911 if (txq
->tx_skbuff
[j
]) {
912 dev_kfree_skb_any(txq
->tx_skbuff
[j
]);
913 txq
->tx_skbuff
[j
] = NULL
;
920 * This function is called to start or restart the FEC during a link
921 * change, transmit timeout, or to reconfigure the FEC. The network
922 * packet processing for this device must be stopped before this call.
925 fec_restart(struct net_device
*ndev
)
927 struct fec_enet_private
*fep
= netdev_priv(ndev
);
930 u32 rcntl
= OPT_FRAME_SIZE
| 0x04;
931 u32 ecntl
= 0x2; /* ETHEREN */
933 /* Whack a reset. We should wait for this.
934 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
935 * instead of reset MAC itself.
937 if (fep
->quirks
& FEC_QUIRK_HAS_AVB
) {
938 writel(0, fep
->hwp
+ FEC_ECNTRL
);
940 writel(1, fep
->hwp
+ FEC_ECNTRL
);
945 * enet-mac reset will reset mac address registers too,
946 * so need to reconfigure it.
948 if (fep
->quirks
& FEC_QUIRK_ENET_MAC
) {
949 memcpy(&temp_mac
, ndev
->dev_addr
, ETH_ALEN
);
950 writel(cpu_to_be32(temp_mac
[0]), fep
->hwp
+ FEC_ADDR_LOW
);
951 writel(cpu_to_be32(temp_mac
[1]), fep
->hwp
+ FEC_ADDR_HIGH
);
954 /* Clear any outstanding interrupt. */
955 writel(0xffffffff, fep
->hwp
+ FEC_IEVENT
);
957 fec_enet_bd_init(ndev
);
959 fec_enet_enable_ring(ndev
);
961 /* Reset tx SKB buffers. */
962 fec_enet_reset_skb(ndev
);
964 /* Enable MII mode */
965 if (fep
->full_duplex
== DUPLEX_FULL
) {
967 writel(0x04, fep
->hwp
+ FEC_X_CNTRL
);
971 writel(0x0, fep
->hwp
+ FEC_X_CNTRL
);
975 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
977 #if !defined(CONFIG_M5272)
978 if (fep
->quirks
& FEC_QUIRK_HAS_RACC
) {
979 /* set RX checksum */
980 val
= readl(fep
->hwp
+ FEC_RACC
);
981 if (fep
->csum_flags
& FLAG_RX_CSUM_ENABLED
)
982 val
|= FEC_RACC_OPTIONS
;
984 val
&= ~FEC_RACC_OPTIONS
;
985 writel(val
, fep
->hwp
+ FEC_RACC
);
990 * The phy interface and speed need to get configured
991 * differently on enet-mac.
993 if (fep
->quirks
& FEC_QUIRK_ENET_MAC
) {
994 /* Enable flow control and length check */
995 rcntl
|= 0x40000000 | 0x00000020;
997 /* RGMII, RMII or MII */
998 if (fep
->phy_interface
== PHY_INTERFACE_MODE_RGMII
||
999 fep
->phy_interface
== PHY_INTERFACE_MODE_RGMII_ID
||
1000 fep
->phy_interface
== PHY_INTERFACE_MODE_RGMII_RXID
||
1001 fep
->phy_interface
== PHY_INTERFACE_MODE_RGMII_TXID
)
1003 else if (fep
->phy_interface
== PHY_INTERFACE_MODE_RMII
)
1008 /* 1G, 100M or 10M */
1010 if (fep
->phy_dev
->speed
== SPEED_1000
)
1012 else if (fep
->phy_dev
->speed
== SPEED_100
)
1018 #ifdef FEC_MIIGSK_ENR
1019 if (fep
->quirks
& FEC_QUIRK_USE_GASKET
) {
1021 /* disable the gasket and wait */
1022 writel(0, fep
->hwp
+ FEC_MIIGSK_ENR
);
1023 while (readl(fep
->hwp
+ FEC_MIIGSK_ENR
) & 4)
1027 * configure the gasket:
1028 * RMII, 50 MHz, no loopback, no echo
1029 * MII, 25 MHz, no loopback, no echo
1031 cfgr
= (fep
->phy_interface
== PHY_INTERFACE_MODE_RMII
)
1032 ? BM_MIIGSK_CFGR_RMII
: BM_MIIGSK_CFGR_MII
;
1033 if (fep
->phy_dev
&& fep
->phy_dev
->speed
== SPEED_10
)
1034 cfgr
|= BM_MIIGSK_CFGR_FRCONT_10M
;
1035 writel(cfgr
, fep
->hwp
+ FEC_MIIGSK_CFGR
);
1037 /* re-enable the gasket */
1038 writel(2, fep
->hwp
+ FEC_MIIGSK_ENR
);
1043 #if !defined(CONFIG_M5272)
1044 /* enable pause frame*/
1045 if ((fep
->pause_flag
& FEC_PAUSE_FLAG_ENABLE
) ||
1046 ((fep
->pause_flag
& FEC_PAUSE_FLAG_AUTONEG
) &&
1047 fep
->phy_dev
&& fep
->phy_dev
->pause
)) {
1048 rcntl
|= FEC_ENET_FCE
;
1050 /* set FIFO threshold parameter to reduce overrun */
1051 writel(FEC_ENET_RSEM_V
, fep
->hwp
+ FEC_R_FIFO_RSEM
);
1052 writel(FEC_ENET_RSFL_V
, fep
->hwp
+ FEC_R_FIFO_RSFL
);
1053 writel(FEC_ENET_RAEM_V
, fep
->hwp
+ FEC_R_FIFO_RAEM
);
1054 writel(FEC_ENET_RAFL_V
, fep
->hwp
+ FEC_R_FIFO_RAFL
);
1057 writel(FEC_ENET_OPD_V
, fep
->hwp
+ FEC_OPD
);
1059 rcntl
&= ~FEC_ENET_FCE
;
1061 #endif /* !defined(CONFIG_M5272) */
1063 writel(rcntl
, fep
->hwp
+ FEC_R_CNTRL
);
1065 /* Setup multicast filter. */
1066 set_multicast_list(ndev
);
1067 #ifndef CONFIG_M5272
1068 writel(0, fep
->hwp
+ FEC_HASH_TABLE_HIGH
);
1069 writel(0, fep
->hwp
+ FEC_HASH_TABLE_LOW
);
1072 if (fep
->quirks
& FEC_QUIRK_ENET_MAC
) {
1073 /* enable ENET endian swap */
1075 /* enable ENET store and forward mode */
1076 writel(1 << 8, fep
->hwp
+ FEC_X_WMRK
);
1079 if (fep
->bufdesc_ex
)
1082 #ifndef CONFIG_M5272
1083 /* Enable the MIB statistic event counters */
1084 writel(0 << 31, fep
->hwp
+ FEC_MIB_CTRLSTAT
);
1087 /* And last, enable the transmit and receive processing */
1088 writel(ecntl
, fep
->hwp
+ FEC_ECNTRL
);
1089 fec_enet_active_rxring(ndev
);
1091 if (fep
->bufdesc_ex
)
1092 fec_ptp_start_cyclecounter(ndev
);
1094 /* Enable interrupts we wish to service */
1096 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
1098 writel(FEC_ENET_MII
, fep
->hwp
+ FEC_IMASK
);
1100 /* Init the interrupt coalescing */
1101 fec_enet_itr_coal_init(ndev
);
1106 fec_stop(struct net_device
*ndev
)
1108 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1109 struct fec_platform_data
*pdata
= fep
->pdev
->dev
.platform_data
;
1110 u32 rmii_mode
= readl(fep
->hwp
+ FEC_R_CNTRL
) & (1 << 8);
1113 /* We cannot expect a graceful transmit stop without link !!! */
1115 writel(1, fep
->hwp
+ FEC_X_CNTRL
); /* Graceful transmit stop */
1117 if (!(readl(fep
->hwp
+ FEC_IEVENT
) & FEC_ENET_GRA
))
1118 netdev_err(ndev
, "Graceful transmit stop did not complete!\n");
1121 /* Whack a reset. We should wait for this.
1122 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1123 * instead of reset MAC itself.
1125 if (!(fep
->wol_flag
& FEC_WOL_FLAG_SLEEP_ON
)) {
1126 if (fep
->quirks
& FEC_QUIRK_HAS_AVB
) {
1127 writel(0, fep
->hwp
+ FEC_ECNTRL
);
1129 writel(1, fep
->hwp
+ FEC_ECNTRL
);
1132 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
1134 writel(FEC_DEFAULT_IMASK
| FEC_ENET_WAKEUP
, fep
->hwp
+ FEC_IMASK
);
1135 val
= readl(fep
->hwp
+ FEC_ECNTRL
);
1136 val
|= (FEC_ECR_MAGICEN
| FEC_ECR_SLEEP
);
1137 writel(val
, fep
->hwp
+ FEC_ECNTRL
);
1139 if (pdata
&& pdata
->sleep_mode_enable
)
1140 pdata
->sleep_mode_enable(true);
1142 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
1144 /* We have to keep ENET enabled to have MII interrupt stay working */
1145 if (fep
->quirks
& FEC_QUIRK_ENET_MAC
&&
1146 !(fep
->wol_flag
& FEC_WOL_FLAG_SLEEP_ON
)) {
1147 writel(2, fep
->hwp
+ FEC_ECNTRL
);
1148 writel(rmii_mode
, fep
->hwp
+ FEC_R_CNTRL
);
1154 fec_timeout(struct net_device
*ndev
)
1156 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1160 ndev
->stats
.tx_errors
++;
1162 schedule_work(&fep
->tx_timeout_work
);
1165 static void fec_enet_timeout_work(struct work_struct
*work
)
1167 struct fec_enet_private
*fep
=
1168 container_of(work
, struct fec_enet_private
, tx_timeout_work
);
1169 struct net_device
*ndev
= fep
->netdev
;
1172 if (netif_device_present(ndev
) || netif_running(ndev
)) {
1173 napi_disable(&fep
->napi
);
1174 netif_tx_lock_bh(ndev
);
1176 netif_wake_queue(ndev
);
1177 netif_tx_unlock_bh(ndev
);
1178 napi_enable(&fep
->napi
);
1184 fec_enet_hwtstamp(struct fec_enet_private
*fep
, unsigned ts
,
1185 struct skb_shared_hwtstamps
*hwtstamps
)
1187 unsigned long flags
;
1190 spin_lock_irqsave(&fep
->tmreg_lock
, flags
);
1191 ns
= timecounter_cyc2time(&fep
->tc
, ts
);
1192 spin_unlock_irqrestore(&fep
->tmreg_lock
, flags
);
1194 memset(hwtstamps
, 0, sizeof(*hwtstamps
));
1195 hwtstamps
->hwtstamp
= ns_to_ktime(ns
);
1199 fec_enet_tx_queue(struct net_device
*ndev
, u16 queue_id
)
1201 struct fec_enet_private
*fep
;
1202 struct bufdesc
*bdp
;
1203 unsigned short status
;
1204 struct sk_buff
*skb
;
1205 struct fec_enet_priv_tx_q
*txq
;
1206 struct netdev_queue
*nq
;
1210 fep
= netdev_priv(ndev
);
1212 queue_id
= FEC_ENET_GET_QUQUE(queue_id
);
1214 txq
= fep
->tx_queue
[queue_id
];
1215 /* get next bdp of dirty_tx */
1216 nq
= netdev_get_tx_queue(ndev
, queue_id
);
1217 bdp
= txq
->dirty_tx
;
1219 /* get next bdp of dirty_tx */
1220 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue_id
);
1222 while (bdp
!= READ_ONCE(txq
->cur_tx
)) {
1223 /* Order the load of cur_tx and cbd_sc */
1225 status
= READ_ONCE(bdp
->cbd_sc
);
1226 if (status
& BD_ENET_TX_READY
)
1229 index
= fec_enet_get_bd_index(txq
->tx_bd_base
, bdp
, fep
);
1231 skb
= txq
->tx_skbuff
[index
];
1232 txq
->tx_skbuff
[index
] = NULL
;
1233 if (!IS_TSO_HEADER(txq
, bdp
->cbd_bufaddr
))
1234 dma_unmap_single(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
1235 bdp
->cbd_datlen
, DMA_TO_DEVICE
);
1236 bdp
->cbd_bufaddr
= 0;
1238 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue_id
);
1242 /* Check for errors. */
1243 if (status
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
1244 BD_ENET_TX_RL
| BD_ENET_TX_UN
|
1246 ndev
->stats
.tx_errors
++;
1247 if (status
& BD_ENET_TX_HB
) /* No heartbeat */
1248 ndev
->stats
.tx_heartbeat_errors
++;
1249 if (status
& BD_ENET_TX_LC
) /* Late collision */
1250 ndev
->stats
.tx_window_errors
++;
1251 if (status
& BD_ENET_TX_RL
) /* Retrans limit */
1252 ndev
->stats
.tx_aborted_errors
++;
1253 if (status
& BD_ENET_TX_UN
) /* Underrun */
1254 ndev
->stats
.tx_fifo_errors
++;
1255 if (status
& BD_ENET_TX_CSL
) /* Carrier lost */
1256 ndev
->stats
.tx_carrier_errors
++;
1258 ndev
->stats
.tx_packets
++;
1259 ndev
->stats
.tx_bytes
+= skb
->len
;
1262 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_IN_PROGRESS
) &&
1264 struct skb_shared_hwtstamps shhwtstamps
;
1265 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
1267 fec_enet_hwtstamp(fep
, ebdp
->ts
, &shhwtstamps
);
1268 skb_tstamp_tx(skb
, &shhwtstamps
);
1271 /* Deferred means some collisions occurred during transmit,
1272 * but we eventually sent the packet OK.
1274 if (status
& BD_ENET_TX_DEF
)
1275 ndev
->stats
.collisions
++;
1277 /* Free the sk buffer associated with this last transmit */
1278 dev_kfree_skb_any(skb
);
1280 /* Make sure the update to bdp and tx_skbuff are performed
1284 txq
->dirty_tx
= bdp
;
1286 /* Update pointer to next buffer descriptor to be transmitted */
1287 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue_id
);
1289 /* Since we have freed up a buffer, the ring is no longer full
1291 if (netif_queue_stopped(ndev
)) {
1292 entries_free
= fec_enet_get_free_txdesc_num(fep
, txq
);
1293 if (entries_free
>= txq
->tx_wake_threshold
)
1294 netif_tx_wake_queue(nq
);
1298 /* ERR006538: Keep the transmitter going */
1299 if (bdp
!= txq
->cur_tx
&&
1300 readl(fep
->hwp
+ FEC_X_DES_ACTIVE(queue_id
)) == 0)
1301 writel(0, fep
->hwp
+ FEC_X_DES_ACTIVE(queue_id
));
1305 fec_enet_tx(struct net_device
*ndev
)
1307 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1309 /* First process class A queue, then Class B and Best Effort queue */
1310 for_each_set_bit(queue_id
, &fep
->work_tx
, FEC_ENET_MAX_TX_QS
) {
1311 clear_bit(queue_id
, &fep
->work_tx
);
1312 fec_enet_tx_queue(ndev
, queue_id
);
1318 fec_enet_new_rxbdp(struct net_device
*ndev
, struct bufdesc
*bdp
, struct sk_buff
*skb
)
1320 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1323 off
= ((unsigned long)skb
->data
) & fep
->rx_align
;
1325 skb_reserve(skb
, fep
->rx_align
+ 1 - off
);
1327 bdp
->cbd_bufaddr
= dma_map_single(&fep
->pdev
->dev
, skb
->data
,
1328 FEC_ENET_RX_FRSIZE
- fep
->rx_align
,
1330 if (dma_mapping_error(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
)) {
1331 if (net_ratelimit())
1332 netdev_err(ndev
, "Rx DMA memory map failed\n");
1339 static bool fec_enet_copybreak(struct net_device
*ndev
, struct sk_buff
**skb
,
1340 struct bufdesc
*bdp
, u32 length
, bool swap
)
1342 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1343 struct sk_buff
*new_skb
;
1345 if (length
> fep
->rx_copybreak
)
1348 new_skb
= netdev_alloc_skb(ndev
, length
);
1352 dma_sync_single_for_cpu(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
1353 FEC_ENET_RX_FRSIZE
- fep
->rx_align
,
1356 memcpy(new_skb
->data
, (*skb
)->data
, length
);
1358 swap_buffer2(new_skb
->data
, (*skb
)->data
, length
);
1364 /* During a receive, the cur_rx points to the current incoming buffer.
1365 * When we update through the ring, if the next incoming buffer has
1366 * not been given to the system, we just set the empty indicator,
1367 * effectively tossing the packet.
1370 fec_enet_rx_queue(struct net_device
*ndev
, int budget
, u16 queue_id
)
1372 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1373 struct fec_enet_priv_rx_q
*rxq
;
1374 struct bufdesc
*bdp
;
1375 unsigned short status
;
1376 struct sk_buff
*skb_new
= NULL
;
1377 struct sk_buff
*skb
;
1380 int pkt_received
= 0;
1381 struct bufdesc_ex
*ebdp
= NULL
;
1382 bool vlan_packet_rcvd
= false;
1386 bool need_swap
= fep
->quirks
& FEC_QUIRK_SWAP_FRAME
;
1391 queue_id
= FEC_ENET_GET_QUQUE(queue_id
);
1392 rxq
= fep
->rx_queue
[queue_id
];
1394 /* First, grab all of the stats for the incoming packet.
1395 * These get messed up if we get called due to a busy condition.
1399 while (!((status
= bdp
->cbd_sc
) & BD_ENET_RX_EMPTY
)) {
1401 if (pkt_received
>= budget
)
1405 /* Since we have allocated space to hold a complete frame,
1406 * the last indicator should be set.
1408 if ((status
& BD_ENET_RX_LAST
) == 0)
1409 netdev_err(ndev
, "rcv is not +last\n");
1411 writel(FEC_ENET_RXF
, fep
->hwp
+ FEC_IEVENT
);
1413 /* Check for errors. */
1414 if (status
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_NO
|
1415 BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
1416 ndev
->stats
.rx_errors
++;
1417 if (status
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
)) {
1418 /* Frame too long or too short. */
1419 ndev
->stats
.rx_length_errors
++;
1421 if (status
& BD_ENET_RX_NO
) /* Frame alignment */
1422 ndev
->stats
.rx_frame_errors
++;
1423 if (status
& BD_ENET_RX_CR
) /* CRC Error */
1424 ndev
->stats
.rx_crc_errors
++;
1425 if (status
& BD_ENET_RX_OV
) /* FIFO overrun */
1426 ndev
->stats
.rx_fifo_errors
++;
1429 /* Report late collisions as a frame error.
1430 * On this error, the BD is closed, but we don't know what we
1431 * have in the buffer. So, just drop this frame on the floor.
1433 if (status
& BD_ENET_RX_CL
) {
1434 ndev
->stats
.rx_errors
++;
1435 ndev
->stats
.rx_frame_errors
++;
1436 goto rx_processing_done
;
1439 /* Process the incoming frame. */
1440 ndev
->stats
.rx_packets
++;
1441 pkt_len
= bdp
->cbd_datlen
;
1442 ndev
->stats
.rx_bytes
+= pkt_len
;
1444 index
= fec_enet_get_bd_index(rxq
->rx_bd_base
, bdp
, fep
);
1445 skb
= rxq
->rx_skbuff
[index
];
1447 /* The packet length includes FCS, but we don't want to
1448 * include that when passing upstream as it messes up
1449 * bridging applications.
1451 is_copybreak
= fec_enet_copybreak(ndev
, &skb
, bdp
, pkt_len
- 4,
1453 if (!is_copybreak
) {
1454 skb_new
= netdev_alloc_skb(ndev
, FEC_ENET_RX_FRSIZE
);
1455 if (unlikely(!skb_new
)) {
1456 ndev
->stats
.rx_dropped
++;
1457 goto rx_processing_done
;
1459 dma_unmap_single(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
1460 FEC_ENET_RX_FRSIZE
- fep
->rx_align
,
1464 prefetch(skb
->data
- NET_IP_ALIGN
);
1465 skb_put(skb
, pkt_len
- 4);
1467 if (!is_copybreak
&& need_swap
)
1468 swap_buffer(data
, pkt_len
);
1470 /* Extract the enhanced buffer descriptor */
1472 if (fep
->bufdesc_ex
)
1473 ebdp
= (struct bufdesc_ex
*)bdp
;
1475 /* If this is a VLAN packet remove the VLAN Tag */
1476 vlan_packet_rcvd
= false;
1477 if ((ndev
->features
& NETIF_F_HW_VLAN_CTAG_RX
) &&
1478 fep
->bufdesc_ex
&& (ebdp
->cbd_esc
& BD_ENET_RX_VLAN
)) {
1479 /* Push and remove the vlan tag */
1480 struct vlan_hdr
*vlan_header
=
1481 (struct vlan_hdr
*) (data
+ ETH_HLEN
);
1482 vlan_tag
= ntohs(vlan_header
->h_vlan_TCI
);
1484 vlan_packet_rcvd
= true;
1486 memmove(skb
->data
+ VLAN_HLEN
, data
, ETH_ALEN
* 2);
1487 skb_pull(skb
, VLAN_HLEN
);
1490 skb
->protocol
= eth_type_trans(skb
, ndev
);
1492 /* Get receive timestamp from the skb */
1493 if (fep
->hwts_rx_en
&& fep
->bufdesc_ex
)
1494 fec_enet_hwtstamp(fep
, ebdp
->ts
,
1495 skb_hwtstamps(skb
));
1497 if (fep
->bufdesc_ex
&&
1498 (fep
->csum_flags
& FLAG_RX_CSUM_ENABLED
)) {
1499 if (!(ebdp
->cbd_esc
& FLAG_RX_CSUM_ERROR
)) {
1500 /* don't check it */
1501 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1503 skb_checksum_none_assert(skb
);
1507 /* Handle received VLAN packets */
1508 if (vlan_packet_rcvd
)
1509 __vlan_hwaccel_put_tag(skb
,
1513 napi_gro_receive(&fep
->napi
, skb
);
1516 dma_sync_single_for_device(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
1517 FEC_ENET_RX_FRSIZE
- fep
->rx_align
,
1520 rxq
->rx_skbuff
[index
] = skb_new
;
1521 fec_enet_new_rxbdp(ndev
, bdp
, skb_new
);
1525 /* Clear the status flags for this buffer */
1526 status
&= ~BD_ENET_RX_STATS
;
1528 /* Mark the buffer empty */
1529 status
|= BD_ENET_RX_EMPTY
;
1530 bdp
->cbd_sc
= status
;
1532 if (fep
->bufdesc_ex
) {
1533 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
1535 ebdp
->cbd_esc
= BD_ENET_RX_INT
;
1540 /* Update BD pointer to next entry */
1541 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue_id
);
1543 /* Doing this here will keep the FEC running while we process
1544 * incoming frames. On a heavily loaded network, we should be
1545 * able to keep up at the expense of system resources.
1547 writel(0, fep
->hwp
+ FEC_R_DES_ACTIVE(queue_id
));
1550 return pkt_received
;
1554 fec_enet_rx(struct net_device
*ndev
, int budget
)
1556 int pkt_received
= 0;
1558 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1560 for_each_set_bit(queue_id
, &fep
->work_rx
, FEC_ENET_MAX_RX_QS
) {
1561 clear_bit(queue_id
, &fep
->work_rx
);
1562 pkt_received
+= fec_enet_rx_queue(ndev
,
1563 budget
- pkt_received
, queue_id
);
1565 return pkt_received
;
1569 fec_enet_collect_events(struct fec_enet_private
*fep
, uint int_events
)
1571 if (int_events
== 0)
1574 if (int_events
& FEC_ENET_RXF
)
1575 fep
->work_rx
|= (1 << 2);
1576 if (int_events
& FEC_ENET_RXF_1
)
1577 fep
->work_rx
|= (1 << 0);
1578 if (int_events
& FEC_ENET_RXF_2
)
1579 fep
->work_rx
|= (1 << 1);
1581 if (int_events
& FEC_ENET_TXF
)
1582 fep
->work_tx
|= (1 << 2);
1583 if (int_events
& FEC_ENET_TXF_1
)
1584 fep
->work_tx
|= (1 << 0);
1585 if (int_events
& FEC_ENET_TXF_2
)
1586 fep
->work_tx
|= (1 << 1);
1592 fec_enet_interrupt(int irq
, void *dev_id
)
1594 struct net_device
*ndev
= dev_id
;
1595 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1597 irqreturn_t ret
= IRQ_NONE
;
1599 int_events
= readl(fep
->hwp
+ FEC_IEVENT
);
1600 writel(int_events
, fep
->hwp
+ FEC_IEVENT
);
1601 fec_enet_collect_events(fep
, int_events
);
1603 if ((fep
->work_tx
|| fep
->work_rx
) && fep
->link
) {
1606 if (napi_schedule_prep(&fep
->napi
)) {
1607 /* Disable the NAPI interrupts */
1608 writel(FEC_ENET_MII
, fep
->hwp
+ FEC_IMASK
);
1609 __napi_schedule(&fep
->napi
);
1613 if (int_events
& FEC_ENET_MII
) {
1615 complete(&fep
->mdio_done
);
1619 fec_ptp_check_pps_event(fep
);
1624 static int fec_enet_rx_napi(struct napi_struct
*napi
, int budget
)
1626 struct net_device
*ndev
= napi
->dev
;
1627 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1630 pkts
= fec_enet_rx(ndev
, budget
);
1634 if (pkts
< budget
) {
1635 napi_complete(napi
);
1636 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
1641 /* ------------------------------------------------------------------------- */
1642 static void fec_get_mac(struct net_device
*ndev
)
1644 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1645 struct fec_platform_data
*pdata
= dev_get_platdata(&fep
->pdev
->dev
);
1646 unsigned char *iap
, tmpaddr
[ETH_ALEN
];
1649 * try to get mac address in following order:
1651 * 1) module parameter via kernel command line in form
1652 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1657 * 2) from device tree data
1659 if (!is_valid_ether_addr(iap
)) {
1660 struct device_node
*np
= fep
->pdev
->dev
.of_node
;
1662 const char *mac
= of_get_mac_address(np
);
1664 iap
= (unsigned char *) mac
;
1669 * 3) from flash or fuse (via platform data)
1671 if (!is_valid_ether_addr(iap
)) {
1674 iap
= (unsigned char *)FEC_FLASHMAC
;
1677 iap
= (unsigned char *)&pdata
->mac
;
1682 * 4) FEC mac registers set by bootloader
1684 if (!is_valid_ether_addr(iap
)) {
1685 *((__be32
*) &tmpaddr
[0]) =
1686 cpu_to_be32(readl(fep
->hwp
+ FEC_ADDR_LOW
));
1687 *((__be16
*) &tmpaddr
[4]) =
1688 cpu_to_be16(readl(fep
->hwp
+ FEC_ADDR_HIGH
) >> 16);
1693 * 5) random mac address
1695 if (!is_valid_ether_addr(iap
)) {
1696 /* Report it and use a random ethernet address instead */
1697 netdev_err(ndev
, "Invalid MAC address: %pM\n", iap
);
1698 eth_hw_addr_random(ndev
);
1699 netdev_info(ndev
, "Using random MAC address: %pM\n",
1704 memcpy(ndev
->dev_addr
, iap
, ETH_ALEN
);
1706 /* Adjust MAC if using macaddr */
1708 ndev
->dev_addr
[ETH_ALEN
-1] = macaddr
[ETH_ALEN
-1] + fep
->dev_id
;
1711 /* ------------------------------------------------------------------------- */
1716 static void fec_enet_adjust_link(struct net_device
*ndev
)
1718 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1719 struct phy_device
*phy_dev
= fep
->phy_dev
;
1720 int status_change
= 0;
1722 /* Prevent a state halted on mii error */
1723 if (fep
->mii_timeout
&& phy_dev
->state
== PHY_HALTED
) {
1724 phy_dev
->state
= PHY_RESUMING
;
1729 * If the netdev is down, or is going down, we're not interested
1730 * in link state events, so just mark our idea of the link as down
1731 * and ignore the event.
1733 if (!netif_running(ndev
) || !netif_device_present(ndev
)) {
1735 } else if (phy_dev
->link
) {
1737 fep
->link
= phy_dev
->link
;
1741 if (fep
->full_duplex
!= phy_dev
->duplex
) {
1742 fep
->full_duplex
= phy_dev
->duplex
;
1746 if (phy_dev
->speed
!= fep
->speed
) {
1747 fep
->speed
= phy_dev
->speed
;
1751 /* if any of the above changed restart the FEC */
1752 if (status_change
) {
1753 napi_disable(&fep
->napi
);
1754 netif_tx_lock_bh(ndev
);
1756 netif_wake_queue(ndev
);
1757 netif_tx_unlock_bh(ndev
);
1758 napi_enable(&fep
->napi
);
1762 napi_disable(&fep
->napi
);
1763 netif_tx_lock_bh(ndev
);
1765 netif_tx_unlock_bh(ndev
);
1766 napi_enable(&fep
->napi
);
1767 fep
->link
= phy_dev
->link
;
1773 phy_print_status(phy_dev
);
1776 static int fec_enet_mdio_read(struct mii_bus
*bus
, int mii_id
, int regnum
)
1778 struct fec_enet_private
*fep
= bus
->priv
;
1779 struct device
*dev
= &fep
->pdev
->dev
;
1780 unsigned long time_left
;
1783 ret
= pm_runtime_get_sync(dev
);
1787 fep
->mii_timeout
= 0;
1788 reinit_completion(&fep
->mdio_done
);
1790 /* start a read op */
1791 writel(FEC_MMFR_ST
| FEC_MMFR_OP_READ
|
1792 FEC_MMFR_PA(mii_id
) | FEC_MMFR_RA(regnum
) |
1793 FEC_MMFR_TA
, fep
->hwp
+ FEC_MII_DATA
);
1795 /* wait for end of transfer */
1796 time_left
= wait_for_completion_timeout(&fep
->mdio_done
,
1797 usecs_to_jiffies(FEC_MII_TIMEOUT
));
1798 if (time_left
== 0) {
1799 fep
->mii_timeout
= 1;
1800 netdev_err(fep
->netdev
, "MDIO read timeout\n");
1805 ret
= FEC_MMFR_DATA(readl(fep
->hwp
+ FEC_MII_DATA
));
1808 pm_runtime_mark_last_busy(dev
);
1809 pm_runtime_put_autosuspend(dev
);
1814 static int fec_enet_mdio_write(struct mii_bus
*bus
, int mii_id
, int regnum
,
1817 struct fec_enet_private
*fep
= bus
->priv
;
1818 struct device
*dev
= &fep
->pdev
->dev
;
1819 unsigned long time_left
;
1822 ret
= pm_runtime_get_sync(dev
);
1828 fep
->mii_timeout
= 0;
1829 reinit_completion(&fep
->mdio_done
);
1831 /* start a write op */
1832 writel(FEC_MMFR_ST
| FEC_MMFR_OP_WRITE
|
1833 FEC_MMFR_PA(mii_id
) | FEC_MMFR_RA(regnum
) |
1834 FEC_MMFR_TA
| FEC_MMFR_DATA(value
),
1835 fep
->hwp
+ FEC_MII_DATA
);
1837 /* wait for end of transfer */
1838 time_left
= wait_for_completion_timeout(&fep
->mdio_done
,
1839 usecs_to_jiffies(FEC_MII_TIMEOUT
));
1840 if (time_left
== 0) {
1841 fep
->mii_timeout
= 1;
1842 netdev_err(fep
->netdev
, "MDIO write timeout\n");
1846 pm_runtime_mark_last_busy(dev
);
1847 pm_runtime_put_autosuspend(dev
);
1852 static int fec_enet_clk_enable(struct net_device
*ndev
, bool enable
)
1854 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1858 ret
= clk_prepare_enable(fep
->clk_ahb
);
1861 if (fep
->clk_enet_out
) {
1862 ret
= clk_prepare_enable(fep
->clk_enet_out
);
1864 goto failed_clk_enet_out
;
1867 mutex_lock(&fep
->ptp_clk_mutex
);
1868 ret
= clk_prepare_enable(fep
->clk_ptp
);
1870 mutex_unlock(&fep
->ptp_clk_mutex
);
1871 goto failed_clk_ptp
;
1873 fep
->ptp_clk_on
= true;
1875 mutex_unlock(&fep
->ptp_clk_mutex
);
1878 ret
= clk_prepare_enable(fep
->clk_ref
);
1880 goto failed_clk_ref
;
1883 clk_disable_unprepare(fep
->clk_ahb
);
1884 if (fep
->clk_enet_out
)
1885 clk_disable_unprepare(fep
->clk_enet_out
);
1887 mutex_lock(&fep
->ptp_clk_mutex
);
1888 clk_disable_unprepare(fep
->clk_ptp
);
1889 fep
->ptp_clk_on
= false;
1890 mutex_unlock(&fep
->ptp_clk_mutex
);
1893 clk_disable_unprepare(fep
->clk_ref
);
1900 clk_disable_unprepare(fep
->clk_ref
);
1902 if (fep
->clk_enet_out
)
1903 clk_disable_unprepare(fep
->clk_enet_out
);
1904 failed_clk_enet_out
:
1905 clk_disable_unprepare(fep
->clk_ahb
);
1910 static int fec_enet_mii_probe(struct net_device
*ndev
)
1912 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1913 struct phy_device
*phy_dev
= NULL
;
1914 char mdio_bus_id
[MII_BUS_ID_SIZE
];
1915 char phy_name
[MII_BUS_ID_SIZE
+ 3];
1917 int dev_id
= fep
->dev_id
;
1919 fep
->phy_dev
= NULL
;
1921 if (fep
->phy_node
) {
1922 phy_dev
= of_phy_connect(ndev
, fep
->phy_node
,
1923 &fec_enet_adjust_link
, 0,
1924 fep
->phy_interface
);
1928 /* check for attached phy */
1929 for (phy_id
= 0; (phy_id
< PHY_MAX_ADDR
); phy_id
++) {
1930 if (!mdiobus_is_registered_device(fep
->mii_bus
, phy_id
))
1934 strlcpy(mdio_bus_id
, fep
->mii_bus
->id
, MII_BUS_ID_SIZE
);
1938 if (phy_id
>= PHY_MAX_ADDR
) {
1939 netdev_info(ndev
, "no PHY, assuming direct connection to switch\n");
1940 strlcpy(mdio_bus_id
, "fixed-0", MII_BUS_ID_SIZE
);
1944 snprintf(phy_name
, sizeof(phy_name
),
1945 PHY_ID_FMT
, mdio_bus_id
, phy_id
);
1946 phy_dev
= phy_connect(ndev
, phy_name
, &fec_enet_adjust_link
,
1947 fep
->phy_interface
);
1950 if (IS_ERR(phy_dev
)) {
1951 netdev_err(ndev
, "could not attach to PHY\n");
1952 return PTR_ERR(phy_dev
);
1955 /* mask with MAC supported features */
1956 if (fep
->quirks
& FEC_QUIRK_HAS_GBIT
) {
1957 phy_dev
->supported
&= PHY_GBIT_FEATURES
;
1958 phy_dev
->supported
&= ~SUPPORTED_1000baseT_Half
;
1959 #if !defined(CONFIG_M5272)
1960 phy_dev
->supported
|= SUPPORTED_Pause
;
1964 phy_dev
->supported
&= PHY_BASIC_FEATURES
;
1966 phy_dev
->advertising
= phy_dev
->supported
;
1968 fep
->phy_dev
= phy_dev
;
1970 fep
->full_duplex
= 0;
1972 phy_attached_info(phy_dev
);
1977 static int fec_enet_mii_init(struct platform_device
*pdev
)
1979 static struct mii_bus
*fec0_mii_bus
;
1980 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1981 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1982 struct device_node
*node
;
1984 u32 mii_speed
, holdtime
;
1987 * The i.MX28 dual fec interfaces are not equal.
1988 * Here are the differences:
1990 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1991 * - fec0 acts as the 1588 time master while fec1 is slave
1992 * - external phys can only be configured by fec0
1994 * That is to say fec1 can not work independently. It only works
1995 * when fec0 is working. The reason behind this design is that the
1996 * second interface is added primarily for Switch mode.
1998 * Because of the last point above, both phys are attached on fec0
1999 * mdio interface in board design, and need to be configured by
2002 if ((fep
->quirks
& FEC_QUIRK_SINGLE_MDIO
) && fep
->dev_id
> 0) {
2003 /* fec1 uses fec0 mii_bus */
2004 if (mii_cnt
&& fec0_mii_bus
) {
2005 fep
->mii_bus
= fec0_mii_bus
;
2012 fep
->mii_timeout
= 0;
2015 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
2017 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2018 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2019 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2022 mii_speed
= DIV_ROUND_UP(clk_get_rate(fep
->clk_ipg
), 5000000);
2023 if (fep
->quirks
& FEC_QUIRK_ENET_MAC
)
2025 if (mii_speed
> 63) {
2027 "fec clock (%lu) to fast to get right mii speed\n",
2028 clk_get_rate(fep
->clk_ipg
));
2034 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2035 * MII_SPEED) register that defines the MDIO output hold time. Earlier
2036 * versions are RAZ there, so just ignore the difference and write the
2038 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2039 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2041 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2042 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2043 * holdtime cannot result in a value greater than 3.
2045 holdtime
= DIV_ROUND_UP(clk_get_rate(fep
->clk_ipg
), 100000000) - 1;
2047 fep
->phy_speed
= mii_speed
<< 1 | holdtime
<< 8;
2049 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
2051 fep
->mii_bus
= mdiobus_alloc();
2052 if (fep
->mii_bus
== NULL
) {
2057 fep
->mii_bus
->name
= "fec_enet_mii_bus";
2058 fep
->mii_bus
->read
= fec_enet_mdio_read
;
2059 fep
->mii_bus
->write
= fec_enet_mdio_write
;
2060 snprintf(fep
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
2061 pdev
->name
, fep
->dev_id
+ 1);
2062 fep
->mii_bus
->priv
= fep
;
2063 fep
->mii_bus
->parent
= &pdev
->dev
;
2065 node
= of_get_child_by_name(pdev
->dev
.of_node
, "mdio");
2067 err
= of_mdiobus_register(fep
->mii_bus
, node
);
2070 err
= mdiobus_register(fep
->mii_bus
);
2074 goto err_out_free_mdiobus
;
2078 /* save fec0 mii_bus */
2079 if (fep
->quirks
& FEC_QUIRK_SINGLE_MDIO
)
2080 fec0_mii_bus
= fep
->mii_bus
;
2084 err_out_free_mdiobus
:
2085 mdiobus_free(fep
->mii_bus
);
2090 static void fec_enet_mii_remove(struct fec_enet_private
*fep
)
2092 if (--mii_cnt
== 0) {
2093 mdiobus_unregister(fep
->mii_bus
);
2094 mdiobus_free(fep
->mii_bus
);
2098 static int fec_enet_get_settings(struct net_device
*ndev
,
2099 struct ethtool_cmd
*cmd
)
2101 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2102 struct phy_device
*phydev
= fep
->phy_dev
;
2107 return phy_ethtool_gset(phydev
, cmd
);
2110 static int fec_enet_set_settings(struct net_device
*ndev
,
2111 struct ethtool_cmd
*cmd
)
2113 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2114 struct phy_device
*phydev
= fep
->phy_dev
;
2119 return phy_ethtool_sset(phydev
, cmd
);
2122 static void fec_enet_get_drvinfo(struct net_device
*ndev
,
2123 struct ethtool_drvinfo
*info
)
2125 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2127 strlcpy(info
->driver
, fep
->pdev
->dev
.driver
->name
,
2128 sizeof(info
->driver
));
2129 strlcpy(info
->version
, "Revision: 1.0", sizeof(info
->version
));
2130 strlcpy(info
->bus_info
, dev_name(&ndev
->dev
), sizeof(info
->bus_info
));
2133 static int fec_enet_get_regs_len(struct net_device
*ndev
)
2135 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2139 r
= platform_get_resource(fep
->pdev
, IORESOURCE_MEM
, 0);
2141 s
= resource_size(r
);
2146 /* List of registers that can be safety be read to dump them with ethtool */
2147 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2148 defined(CONFIG_M520x) || defined(CONFIG_M532x) || \
2149 defined(CONFIG_ARCH_MXC) || defined(CONFIG_SOC_IMX28)
2150 static u32 fec_enet_register_offset
[] = {
2151 FEC_IEVENT
, FEC_IMASK
, FEC_R_DES_ACTIVE_0
, FEC_X_DES_ACTIVE_0
,
2152 FEC_ECNTRL
, FEC_MII_DATA
, FEC_MII_SPEED
, FEC_MIB_CTRLSTAT
, FEC_R_CNTRL
,
2153 FEC_X_CNTRL
, FEC_ADDR_LOW
, FEC_ADDR_HIGH
, FEC_OPD
, FEC_TXIC0
, FEC_TXIC1
,
2154 FEC_TXIC2
, FEC_RXIC0
, FEC_RXIC1
, FEC_RXIC2
, FEC_HASH_TABLE_HIGH
,
2155 FEC_HASH_TABLE_LOW
, FEC_GRP_HASH_TABLE_HIGH
, FEC_GRP_HASH_TABLE_LOW
,
2156 FEC_X_WMRK
, FEC_R_BOUND
, FEC_R_FSTART
, FEC_R_DES_START_1
,
2157 FEC_X_DES_START_1
, FEC_R_BUFF_SIZE_1
, FEC_R_DES_START_2
,
2158 FEC_X_DES_START_2
, FEC_R_BUFF_SIZE_2
, FEC_R_DES_START_0
,
2159 FEC_X_DES_START_0
, FEC_R_BUFF_SIZE_0
, FEC_R_FIFO_RSFL
, FEC_R_FIFO_RSEM
,
2160 FEC_R_FIFO_RAEM
, FEC_R_FIFO_RAFL
, FEC_RACC
, FEC_RCMR_1
, FEC_RCMR_2
,
2161 FEC_DMA_CFG_1
, FEC_DMA_CFG_2
, FEC_R_DES_ACTIVE_1
, FEC_X_DES_ACTIVE_1
,
2162 FEC_R_DES_ACTIVE_2
, FEC_X_DES_ACTIVE_2
, FEC_QOS_SCHEME
,
2163 RMON_T_DROP
, RMON_T_PACKETS
, RMON_T_BC_PKT
, RMON_T_MC_PKT
,
2164 RMON_T_CRC_ALIGN
, RMON_T_UNDERSIZE
, RMON_T_OVERSIZE
, RMON_T_FRAG
,
2165 RMON_T_JAB
, RMON_T_COL
, RMON_T_P64
, RMON_T_P65TO127
, RMON_T_P128TO255
,
2166 RMON_T_P256TO511
, RMON_T_P512TO1023
, RMON_T_P1024TO2047
,
2167 RMON_T_P_GTE2048
, RMON_T_OCTETS
,
2168 IEEE_T_DROP
, IEEE_T_FRAME_OK
, IEEE_T_1COL
, IEEE_T_MCOL
, IEEE_T_DEF
,
2169 IEEE_T_LCOL
, IEEE_T_EXCOL
, IEEE_T_MACERR
, IEEE_T_CSERR
, IEEE_T_SQE
,
2170 IEEE_T_FDXFC
, IEEE_T_OCTETS_OK
,
2171 RMON_R_PACKETS
, RMON_R_BC_PKT
, RMON_R_MC_PKT
, RMON_R_CRC_ALIGN
,
2172 RMON_R_UNDERSIZE
, RMON_R_OVERSIZE
, RMON_R_FRAG
, RMON_R_JAB
,
2173 RMON_R_RESVD_O
, RMON_R_P64
, RMON_R_P65TO127
, RMON_R_P128TO255
,
2174 RMON_R_P256TO511
, RMON_R_P512TO1023
, RMON_R_P1024TO2047
,
2175 RMON_R_P_GTE2048
, RMON_R_OCTETS
,
2176 IEEE_R_DROP
, IEEE_R_FRAME_OK
, IEEE_R_CRC
, IEEE_R_ALIGN
, IEEE_R_MACERR
,
2177 IEEE_R_FDXFC
, IEEE_R_OCTETS_OK
2180 static u32 fec_enet_register_offset
[] = {
2181 FEC_ECNTRL
, FEC_IEVENT
, FEC_IMASK
, FEC_IVEC
, FEC_R_DES_ACTIVE_0
,
2182 FEC_R_DES_ACTIVE_1
, FEC_R_DES_ACTIVE_2
, FEC_X_DES_ACTIVE_0
,
2183 FEC_X_DES_ACTIVE_1
, FEC_X_DES_ACTIVE_2
, FEC_MII_DATA
, FEC_MII_SPEED
,
2184 FEC_R_BOUND
, FEC_R_FSTART
, FEC_X_WMRK
, FEC_X_FSTART
, FEC_R_CNTRL
,
2185 FEC_MAX_FRM_LEN
, FEC_X_CNTRL
, FEC_ADDR_LOW
, FEC_ADDR_HIGH
,
2186 FEC_GRP_HASH_TABLE_HIGH
, FEC_GRP_HASH_TABLE_LOW
, FEC_R_DES_START_0
,
2187 FEC_R_DES_START_1
, FEC_R_DES_START_2
, FEC_X_DES_START_0
,
2188 FEC_X_DES_START_1
, FEC_X_DES_START_2
, FEC_R_BUFF_SIZE_0
,
2189 FEC_R_BUFF_SIZE_1
, FEC_R_BUFF_SIZE_2
2193 static void fec_enet_get_regs(struct net_device
*ndev
,
2194 struct ethtool_regs
*regs
, void *regbuf
)
2196 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2197 u32 __iomem
*theregs
= (u32 __iomem
*)fep
->hwp
;
2198 u32
*buf
= (u32
*)regbuf
;
2201 memset(buf
, 0, regs
->len
);
2203 for (i
= 0; i
< ARRAY_SIZE(fec_enet_register_offset
); i
++) {
2204 off
= fec_enet_register_offset
[i
] / 4;
2205 buf
[off
] = readl(&theregs
[off
]);
2209 static int fec_enet_get_ts_info(struct net_device
*ndev
,
2210 struct ethtool_ts_info
*info
)
2212 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2214 if (fep
->bufdesc_ex
) {
2216 info
->so_timestamping
= SOF_TIMESTAMPING_TX_SOFTWARE
|
2217 SOF_TIMESTAMPING_RX_SOFTWARE
|
2218 SOF_TIMESTAMPING_SOFTWARE
|
2219 SOF_TIMESTAMPING_TX_HARDWARE
|
2220 SOF_TIMESTAMPING_RX_HARDWARE
|
2221 SOF_TIMESTAMPING_RAW_HARDWARE
;
2223 info
->phc_index
= ptp_clock_index(fep
->ptp_clock
);
2225 info
->phc_index
= -1;
2227 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) |
2228 (1 << HWTSTAMP_TX_ON
);
2230 info
->rx_filters
= (1 << HWTSTAMP_FILTER_NONE
) |
2231 (1 << HWTSTAMP_FILTER_ALL
);
2234 return ethtool_op_get_ts_info(ndev
, info
);
2238 #if !defined(CONFIG_M5272)
2240 static void fec_enet_get_pauseparam(struct net_device
*ndev
,
2241 struct ethtool_pauseparam
*pause
)
2243 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2245 pause
->autoneg
= (fep
->pause_flag
& FEC_PAUSE_FLAG_AUTONEG
) != 0;
2246 pause
->tx_pause
= (fep
->pause_flag
& FEC_PAUSE_FLAG_ENABLE
) != 0;
2247 pause
->rx_pause
= pause
->tx_pause
;
2250 static int fec_enet_set_pauseparam(struct net_device
*ndev
,
2251 struct ethtool_pauseparam
*pause
)
2253 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2258 if (pause
->tx_pause
!= pause
->rx_pause
) {
2260 "hardware only support enable/disable both tx and rx");
2264 fep
->pause_flag
= 0;
2266 /* tx pause must be same as rx pause */
2267 fep
->pause_flag
|= pause
->rx_pause
? FEC_PAUSE_FLAG_ENABLE
: 0;
2268 fep
->pause_flag
|= pause
->autoneg
? FEC_PAUSE_FLAG_AUTONEG
: 0;
2270 if (pause
->rx_pause
|| pause
->autoneg
) {
2271 fep
->phy_dev
->supported
|= ADVERTISED_Pause
;
2272 fep
->phy_dev
->advertising
|= ADVERTISED_Pause
;
2274 fep
->phy_dev
->supported
&= ~ADVERTISED_Pause
;
2275 fep
->phy_dev
->advertising
&= ~ADVERTISED_Pause
;
2278 if (pause
->autoneg
) {
2279 if (netif_running(ndev
))
2281 phy_start_aneg(fep
->phy_dev
);
2283 if (netif_running(ndev
)) {
2284 napi_disable(&fep
->napi
);
2285 netif_tx_lock_bh(ndev
);
2287 netif_wake_queue(ndev
);
2288 netif_tx_unlock_bh(ndev
);
2289 napi_enable(&fep
->napi
);
2295 static const struct fec_stat
{
2296 char name
[ETH_GSTRING_LEN
];
2300 { "tx_dropped", RMON_T_DROP
},
2301 { "tx_packets", RMON_T_PACKETS
},
2302 { "tx_broadcast", RMON_T_BC_PKT
},
2303 { "tx_multicast", RMON_T_MC_PKT
},
2304 { "tx_crc_errors", RMON_T_CRC_ALIGN
},
2305 { "tx_undersize", RMON_T_UNDERSIZE
},
2306 { "tx_oversize", RMON_T_OVERSIZE
},
2307 { "tx_fragment", RMON_T_FRAG
},
2308 { "tx_jabber", RMON_T_JAB
},
2309 { "tx_collision", RMON_T_COL
},
2310 { "tx_64byte", RMON_T_P64
},
2311 { "tx_65to127byte", RMON_T_P65TO127
},
2312 { "tx_128to255byte", RMON_T_P128TO255
},
2313 { "tx_256to511byte", RMON_T_P256TO511
},
2314 { "tx_512to1023byte", RMON_T_P512TO1023
},
2315 { "tx_1024to2047byte", RMON_T_P1024TO2047
},
2316 { "tx_GTE2048byte", RMON_T_P_GTE2048
},
2317 { "tx_octets", RMON_T_OCTETS
},
2320 { "IEEE_tx_drop", IEEE_T_DROP
},
2321 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK
},
2322 { "IEEE_tx_1col", IEEE_T_1COL
},
2323 { "IEEE_tx_mcol", IEEE_T_MCOL
},
2324 { "IEEE_tx_def", IEEE_T_DEF
},
2325 { "IEEE_tx_lcol", IEEE_T_LCOL
},
2326 { "IEEE_tx_excol", IEEE_T_EXCOL
},
2327 { "IEEE_tx_macerr", IEEE_T_MACERR
},
2328 { "IEEE_tx_cserr", IEEE_T_CSERR
},
2329 { "IEEE_tx_sqe", IEEE_T_SQE
},
2330 { "IEEE_tx_fdxfc", IEEE_T_FDXFC
},
2331 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK
},
2334 { "rx_packets", RMON_R_PACKETS
},
2335 { "rx_broadcast", RMON_R_BC_PKT
},
2336 { "rx_multicast", RMON_R_MC_PKT
},
2337 { "rx_crc_errors", RMON_R_CRC_ALIGN
},
2338 { "rx_undersize", RMON_R_UNDERSIZE
},
2339 { "rx_oversize", RMON_R_OVERSIZE
},
2340 { "rx_fragment", RMON_R_FRAG
},
2341 { "rx_jabber", RMON_R_JAB
},
2342 { "rx_64byte", RMON_R_P64
},
2343 { "rx_65to127byte", RMON_R_P65TO127
},
2344 { "rx_128to255byte", RMON_R_P128TO255
},
2345 { "rx_256to511byte", RMON_R_P256TO511
},
2346 { "rx_512to1023byte", RMON_R_P512TO1023
},
2347 { "rx_1024to2047byte", RMON_R_P1024TO2047
},
2348 { "rx_GTE2048byte", RMON_R_P_GTE2048
},
2349 { "rx_octets", RMON_R_OCTETS
},
2352 { "IEEE_rx_drop", IEEE_R_DROP
},
2353 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK
},
2354 { "IEEE_rx_crc", IEEE_R_CRC
},
2355 { "IEEE_rx_align", IEEE_R_ALIGN
},
2356 { "IEEE_rx_macerr", IEEE_R_MACERR
},
2357 { "IEEE_rx_fdxfc", IEEE_R_FDXFC
},
2358 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK
},
2361 static void fec_enet_get_ethtool_stats(struct net_device
*dev
,
2362 struct ethtool_stats
*stats
, u64
*data
)
2364 struct fec_enet_private
*fep
= netdev_priv(dev
);
2367 for (i
= 0; i
< ARRAY_SIZE(fec_stats
); i
++)
2368 data
[i
] = readl(fep
->hwp
+ fec_stats
[i
].offset
);
2371 static void fec_enet_get_strings(struct net_device
*netdev
,
2372 u32 stringset
, u8
*data
)
2375 switch (stringset
) {
2377 for (i
= 0; i
< ARRAY_SIZE(fec_stats
); i
++)
2378 memcpy(data
+ i
* ETH_GSTRING_LEN
,
2379 fec_stats
[i
].name
, ETH_GSTRING_LEN
);
2384 static int fec_enet_get_sset_count(struct net_device
*dev
, int sset
)
2388 return ARRAY_SIZE(fec_stats
);
2393 #endif /* !defined(CONFIG_M5272) */
2395 static int fec_enet_nway_reset(struct net_device
*dev
)
2397 struct fec_enet_private
*fep
= netdev_priv(dev
);
2398 struct phy_device
*phydev
= fep
->phy_dev
;
2403 return genphy_restart_aneg(phydev
);
2406 /* ITR clock source is enet system clock (clk_ahb).
2407 * TCTT unit is cycle_ns * 64 cycle
2408 * So, the ICTT value = X us / (cycle_ns * 64)
2410 static int fec_enet_us_to_itr_clock(struct net_device
*ndev
, int us
)
2412 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2414 return us
* (fep
->itr_clk_rate
/ 64000) / 1000;
2417 /* Set threshold for interrupt coalescing */
2418 static void fec_enet_itr_coal_set(struct net_device
*ndev
)
2420 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2423 if (!(fep
->quirks
& FEC_QUIRK_HAS_AVB
))
2426 /* Must be greater than zero to avoid unpredictable behavior */
2427 if (!fep
->rx_time_itr
|| !fep
->rx_pkts_itr
||
2428 !fep
->tx_time_itr
|| !fep
->tx_pkts_itr
)
2431 /* Select enet system clock as Interrupt Coalescing
2432 * timer Clock Source
2434 rx_itr
= FEC_ITR_CLK_SEL
;
2435 tx_itr
= FEC_ITR_CLK_SEL
;
2437 /* set ICFT and ICTT */
2438 rx_itr
|= FEC_ITR_ICFT(fep
->rx_pkts_itr
);
2439 rx_itr
|= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev
, fep
->rx_time_itr
));
2440 tx_itr
|= FEC_ITR_ICFT(fep
->tx_pkts_itr
);
2441 tx_itr
|= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev
, fep
->tx_time_itr
));
2443 rx_itr
|= FEC_ITR_EN
;
2444 tx_itr
|= FEC_ITR_EN
;
2446 writel(tx_itr
, fep
->hwp
+ FEC_TXIC0
);
2447 writel(rx_itr
, fep
->hwp
+ FEC_RXIC0
);
2448 writel(tx_itr
, fep
->hwp
+ FEC_TXIC1
);
2449 writel(rx_itr
, fep
->hwp
+ FEC_RXIC1
);
2450 writel(tx_itr
, fep
->hwp
+ FEC_TXIC2
);
2451 writel(rx_itr
, fep
->hwp
+ FEC_RXIC2
);
2455 fec_enet_get_coalesce(struct net_device
*ndev
, struct ethtool_coalesce
*ec
)
2457 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2459 if (!(fep
->quirks
& FEC_QUIRK_HAS_AVB
))
2462 ec
->rx_coalesce_usecs
= fep
->rx_time_itr
;
2463 ec
->rx_max_coalesced_frames
= fep
->rx_pkts_itr
;
2465 ec
->tx_coalesce_usecs
= fep
->tx_time_itr
;
2466 ec
->tx_max_coalesced_frames
= fep
->tx_pkts_itr
;
2472 fec_enet_set_coalesce(struct net_device
*ndev
, struct ethtool_coalesce
*ec
)
2474 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2477 if (!(fep
->quirks
& FEC_QUIRK_HAS_AVB
))
2480 if (ec
->rx_max_coalesced_frames
> 255) {
2481 pr_err("Rx coalesced frames exceed hardware limiation");
2485 if (ec
->tx_max_coalesced_frames
> 255) {
2486 pr_err("Tx coalesced frame exceed hardware limiation");
2490 cycle
= fec_enet_us_to_itr_clock(ndev
, fep
->rx_time_itr
);
2491 if (cycle
> 0xFFFF) {
2492 pr_err("Rx coalesed usec exceeed hardware limiation");
2496 cycle
= fec_enet_us_to_itr_clock(ndev
, fep
->tx_time_itr
);
2497 if (cycle
> 0xFFFF) {
2498 pr_err("Rx coalesed usec exceeed hardware limiation");
2502 fep
->rx_time_itr
= ec
->rx_coalesce_usecs
;
2503 fep
->rx_pkts_itr
= ec
->rx_max_coalesced_frames
;
2505 fep
->tx_time_itr
= ec
->tx_coalesce_usecs
;
2506 fep
->tx_pkts_itr
= ec
->tx_max_coalesced_frames
;
2508 fec_enet_itr_coal_set(ndev
);
2513 static void fec_enet_itr_coal_init(struct net_device
*ndev
)
2515 struct ethtool_coalesce ec
;
2517 ec
.rx_coalesce_usecs
= FEC_ITR_ICTT_DEFAULT
;
2518 ec
.rx_max_coalesced_frames
= FEC_ITR_ICFT_DEFAULT
;
2520 ec
.tx_coalesce_usecs
= FEC_ITR_ICTT_DEFAULT
;
2521 ec
.tx_max_coalesced_frames
= FEC_ITR_ICFT_DEFAULT
;
2523 fec_enet_set_coalesce(ndev
, &ec
);
2526 static int fec_enet_get_tunable(struct net_device
*netdev
,
2527 const struct ethtool_tunable
*tuna
,
2530 struct fec_enet_private
*fep
= netdev_priv(netdev
);
2534 case ETHTOOL_RX_COPYBREAK
:
2535 *(u32
*)data
= fep
->rx_copybreak
;
2545 static int fec_enet_set_tunable(struct net_device
*netdev
,
2546 const struct ethtool_tunable
*tuna
,
2549 struct fec_enet_private
*fep
= netdev_priv(netdev
);
2553 case ETHTOOL_RX_COPYBREAK
:
2554 fep
->rx_copybreak
= *(u32
*)data
;
2565 fec_enet_get_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
2567 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2569 if (fep
->wol_flag
& FEC_WOL_HAS_MAGIC_PACKET
) {
2570 wol
->supported
= WAKE_MAGIC
;
2571 wol
->wolopts
= fep
->wol_flag
& FEC_WOL_FLAG_ENABLE
? WAKE_MAGIC
: 0;
2573 wol
->supported
= wol
->wolopts
= 0;
2578 fec_enet_set_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
2580 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2582 if (!(fep
->wol_flag
& FEC_WOL_HAS_MAGIC_PACKET
))
2585 if (wol
->wolopts
& ~WAKE_MAGIC
)
2588 device_set_wakeup_enable(&ndev
->dev
, wol
->wolopts
& WAKE_MAGIC
);
2589 if (device_may_wakeup(&ndev
->dev
)) {
2590 fep
->wol_flag
|= FEC_WOL_FLAG_ENABLE
;
2591 if (fep
->irq
[0] > 0)
2592 enable_irq_wake(fep
->irq
[0]);
2594 fep
->wol_flag
&= (~FEC_WOL_FLAG_ENABLE
);
2595 if (fep
->irq
[0] > 0)
2596 disable_irq_wake(fep
->irq
[0]);
2602 static const struct ethtool_ops fec_enet_ethtool_ops
= {
2603 .get_settings
= fec_enet_get_settings
,
2604 .set_settings
= fec_enet_set_settings
,
2605 .get_drvinfo
= fec_enet_get_drvinfo
,
2606 .get_regs_len
= fec_enet_get_regs_len
,
2607 .get_regs
= fec_enet_get_regs
,
2608 .nway_reset
= fec_enet_nway_reset
,
2609 .get_link
= ethtool_op_get_link
,
2610 .get_coalesce
= fec_enet_get_coalesce
,
2611 .set_coalesce
= fec_enet_set_coalesce
,
2612 #ifndef CONFIG_M5272
2613 .get_pauseparam
= fec_enet_get_pauseparam
,
2614 .set_pauseparam
= fec_enet_set_pauseparam
,
2615 .get_strings
= fec_enet_get_strings
,
2616 .get_ethtool_stats
= fec_enet_get_ethtool_stats
,
2617 .get_sset_count
= fec_enet_get_sset_count
,
2619 .get_ts_info
= fec_enet_get_ts_info
,
2620 .get_tunable
= fec_enet_get_tunable
,
2621 .set_tunable
= fec_enet_set_tunable
,
2622 .get_wol
= fec_enet_get_wol
,
2623 .set_wol
= fec_enet_set_wol
,
2626 static int fec_enet_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
2628 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2629 struct phy_device
*phydev
= fep
->phy_dev
;
2631 if (!netif_running(ndev
))
2637 if (fep
->bufdesc_ex
) {
2638 if (cmd
== SIOCSHWTSTAMP
)
2639 return fec_ptp_set(ndev
, rq
);
2640 if (cmd
== SIOCGHWTSTAMP
)
2641 return fec_ptp_get(ndev
, rq
);
2644 return phy_mii_ioctl(phydev
, rq
, cmd
);
2647 static void fec_enet_free_buffers(struct net_device
*ndev
)
2649 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2651 struct sk_buff
*skb
;
2652 struct bufdesc
*bdp
;
2653 struct fec_enet_priv_tx_q
*txq
;
2654 struct fec_enet_priv_rx_q
*rxq
;
2657 for (q
= 0; q
< fep
->num_rx_queues
; q
++) {
2658 rxq
= fep
->rx_queue
[q
];
2659 bdp
= rxq
->rx_bd_base
;
2660 for (i
= 0; i
< rxq
->rx_ring_size
; i
++) {
2661 skb
= rxq
->rx_skbuff
[i
];
2662 rxq
->rx_skbuff
[i
] = NULL
;
2664 dma_unmap_single(&fep
->pdev
->dev
,
2666 FEC_ENET_RX_FRSIZE
- fep
->rx_align
,
2670 bdp
= fec_enet_get_nextdesc(bdp
, fep
, q
);
2674 for (q
= 0; q
< fep
->num_tx_queues
; q
++) {
2675 txq
= fep
->tx_queue
[q
];
2676 bdp
= txq
->tx_bd_base
;
2677 for (i
= 0; i
< txq
->tx_ring_size
; i
++) {
2678 kfree(txq
->tx_bounce
[i
]);
2679 txq
->tx_bounce
[i
] = NULL
;
2680 skb
= txq
->tx_skbuff
[i
];
2681 txq
->tx_skbuff
[i
] = NULL
;
2687 static void fec_enet_free_queue(struct net_device
*ndev
)
2689 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2691 struct fec_enet_priv_tx_q
*txq
;
2693 for (i
= 0; i
< fep
->num_tx_queues
; i
++)
2694 if (fep
->tx_queue
[i
] && fep
->tx_queue
[i
]->tso_hdrs
) {
2695 txq
= fep
->tx_queue
[i
];
2696 dma_free_coherent(NULL
,
2697 txq
->tx_ring_size
* TSO_HEADER_SIZE
,
2702 for (i
= 0; i
< fep
->num_rx_queues
; i
++)
2703 kfree(fep
->rx_queue
[i
]);
2704 for (i
= 0; i
< fep
->num_tx_queues
; i
++)
2705 kfree(fep
->tx_queue
[i
]);
2708 static int fec_enet_alloc_queue(struct net_device
*ndev
)
2710 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2713 struct fec_enet_priv_tx_q
*txq
;
2715 for (i
= 0; i
< fep
->num_tx_queues
; i
++) {
2716 txq
= kzalloc(sizeof(*txq
), GFP_KERNEL
);
2722 fep
->tx_queue
[i
] = txq
;
2723 txq
->tx_ring_size
= TX_RING_SIZE
;
2724 fep
->total_tx_ring_size
+= fep
->tx_queue
[i
]->tx_ring_size
;
2726 txq
->tx_stop_threshold
= FEC_MAX_SKB_DESCS
;
2727 txq
->tx_wake_threshold
=
2728 (txq
->tx_ring_size
- txq
->tx_stop_threshold
) / 2;
2730 txq
->tso_hdrs
= dma_alloc_coherent(NULL
,
2731 txq
->tx_ring_size
* TSO_HEADER_SIZE
,
2734 if (!txq
->tso_hdrs
) {
2740 for (i
= 0; i
< fep
->num_rx_queues
; i
++) {
2741 fep
->rx_queue
[i
] = kzalloc(sizeof(*fep
->rx_queue
[i
]),
2743 if (!fep
->rx_queue
[i
]) {
2748 fep
->rx_queue
[i
]->rx_ring_size
= RX_RING_SIZE
;
2749 fep
->total_rx_ring_size
+= fep
->rx_queue
[i
]->rx_ring_size
;
2754 fec_enet_free_queue(ndev
);
2759 fec_enet_alloc_rxq_buffers(struct net_device
*ndev
, unsigned int queue
)
2761 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2763 struct sk_buff
*skb
;
2764 struct bufdesc
*bdp
;
2765 struct fec_enet_priv_rx_q
*rxq
;
2767 rxq
= fep
->rx_queue
[queue
];
2768 bdp
= rxq
->rx_bd_base
;
2769 for (i
= 0; i
< rxq
->rx_ring_size
; i
++) {
2770 skb
= netdev_alloc_skb(ndev
, FEC_ENET_RX_FRSIZE
);
2774 if (fec_enet_new_rxbdp(ndev
, bdp
, skb
)) {
2779 rxq
->rx_skbuff
[i
] = skb
;
2780 bdp
->cbd_sc
= BD_ENET_RX_EMPTY
;
2782 if (fep
->bufdesc_ex
) {
2783 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
2784 ebdp
->cbd_esc
= BD_ENET_RX_INT
;
2787 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue
);
2790 /* Set the last buffer to wrap. */
2791 bdp
= fec_enet_get_prevdesc(bdp
, fep
, queue
);
2792 bdp
->cbd_sc
|= BD_SC_WRAP
;
2796 fec_enet_free_buffers(ndev
);
2801 fec_enet_alloc_txq_buffers(struct net_device
*ndev
, unsigned int queue
)
2803 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2805 struct bufdesc
*bdp
;
2806 struct fec_enet_priv_tx_q
*txq
;
2808 txq
= fep
->tx_queue
[queue
];
2809 bdp
= txq
->tx_bd_base
;
2810 for (i
= 0; i
< txq
->tx_ring_size
; i
++) {
2811 txq
->tx_bounce
[i
] = kmalloc(FEC_ENET_TX_FRSIZE
, GFP_KERNEL
);
2812 if (!txq
->tx_bounce
[i
])
2816 bdp
->cbd_bufaddr
= 0;
2818 if (fep
->bufdesc_ex
) {
2819 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
2820 ebdp
->cbd_esc
= BD_ENET_TX_INT
;
2823 bdp
= fec_enet_get_nextdesc(bdp
, fep
, queue
);
2826 /* Set the last buffer to wrap. */
2827 bdp
= fec_enet_get_prevdesc(bdp
, fep
, queue
);
2828 bdp
->cbd_sc
|= BD_SC_WRAP
;
2833 fec_enet_free_buffers(ndev
);
2837 static int fec_enet_alloc_buffers(struct net_device
*ndev
)
2839 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2842 for (i
= 0; i
< fep
->num_rx_queues
; i
++)
2843 if (fec_enet_alloc_rxq_buffers(ndev
, i
))
2846 for (i
= 0; i
< fep
->num_tx_queues
; i
++)
2847 if (fec_enet_alloc_txq_buffers(ndev
, i
))
2853 fec_enet_open(struct net_device
*ndev
)
2855 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2858 ret
= pm_runtime_get_sync(&fep
->pdev
->dev
);
2862 pinctrl_pm_select_default_state(&fep
->pdev
->dev
);
2863 ret
= fec_enet_clk_enable(ndev
, true);
2867 /* I should reset the ring buffers here, but I don't yet know
2868 * a simple way to do that.
2871 ret
= fec_enet_alloc_buffers(ndev
);
2873 goto err_enet_alloc
;
2875 /* Init MAC prior to mii bus probe */
2878 /* Probe and connect to PHY when open the interface */
2879 ret
= fec_enet_mii_probe(ndev
);
2881 goto err_enet_mii_probe
;
2883 napi_enable(&fep
->napi
);
2884 phy_start(fep
->phy_dev
);
2885 netif_tx_start_all_queues(ndev
);
2887 device_set_wakeup_enable(&ndev
->dev
, fep
->wol_flag
&
2888 FEC_WOL_FLAG_ENABLE
);
2893 fec_enet_free_buffers(ndev
);
2895 fec_enet_clk_enable(ndev
, false);
2897 pm_runtime_mark_last_busy(&fep
->pdev
->dev
);
2898 pm_runtime_put_autosuspend(&fep
->pdev
->dev
);
2899 pinctrl_pm_select_sleep_state(&fep
->pdev
->dev
);
2904 fec_enet_close(struct net_device
*ndev
)
2906 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2908 phy_stop(fep
->phy_dev
);
2910 if (netif_device_present(ndev
)) {
2911 napi_disable(&fep
->napi
);
2912 netif_tx_disable(ndev
);
2916 phy_disconnect(fep
->phy_dev
);
2917 fep
->phy_dev
= NULL
;
2919 fec_enet_clk_enable(ndev
, false);
2920 pinctrl_pm_select_sleep_state(&fep
->pdev
->dev
);
2921 pm_runtime_mark_last_busy(&fep
->pdev
->dev
);
2922 pm_runtime_put_autosuspend(&fep
->pdev
->dev
);
2924 fec_enet_free_buffers(ndev
);
2929 /* Set or clear the multicast filter for this adaptor.
2930 * Skeleton taken from sunlance driver.
2931 * The CPM Ethernet implementation allows Multicast as well as individual
2932 * MAC address filtering. Some of the drivers check to make sure it is
2933 * a group multicast address, and discard those that are not. I guess I
2934 * will do the same for now, but just remove the test if you want
2935 * individual filtering as well (do the upper net layers want or support
2936 * this kind of feature?).
2939 #define HASH_BITS 6 /* #bits in hash */
2940 #define CRC32_POLY 0xEDB88320
2942 static void set_multicast_list(struct net_device
*ndev
)
2944 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2945 struct netdev_hw_addr
*ha
;
2946 unsigned int i
, bit
, data
, crc
, tmp
;
2949 if (ndev
->flags
& IFF_PROMISC
) {
2950 tmp
= readl(fep
->hwp
+ FEC_R_CNTRL
);
2952 writel(tmp
, fep
->hwp
+ FEC_R_CNTRL
);
2956 tmp
= readl(fep
->hwp
+ FEC_R_CNTRL
);
2958 writel(tmp
, fep
->hwp
+ FEC_R_CNTRL
);
2960 if (ndev
->flags
& IFF_ALLMULTI
) {
2961 /* Catch all multicast addresses, so set the
2964 writel(0xffffffff, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
2965 writel(0xffffffff, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
2970 /* Clear filter and add the addresses in hash register
2972 writel(0, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
2973 writel(0, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
2975 netdev_for_each_mc_addr(ha
, ndev
) {
2976 /* calculate crc32 value of mac address */
2979 for (i
= 0; i
< ndev
->addr_len
; i
++) {
2981 for (bit
= 0; bit
< 8; bit
++, data
>>= 1) {
2983 (((crc
^ data
) & 1) ? CRC32_POLY
: 0);
2987 /* only upper 6 bits (HASH_BITS) are used
2988 * which point to specific bit in he hash registers
2990 hash
= (crc
>> (32 - HASH_BITS
)) & 0x3f;
2993 tmp
= readl(fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
2994 tmp
|= 1 << (hash
- 32);
2995 writel(tmp
, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
2997 tmp
= readl(fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
2999 writel(tmp
, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
3004 /* Set a MAC change in hardware. */
3006 fec_set_mac_address(struct net_device
*ndev
, void *p
)
3008 struct fec_enet_private
*fep
= netdev_priv(ndev
);
3009 struct sockaddr
*addr
= p
;
3012 if (!is_valid_ether_addr(addr
->sa_data
))
3013 return -EADDRNOTAVAIL
;
3014 memcpy(ndev
->dev_addr
, addr
->sa_data
, ndev
->addr_len
);
3017 /* Add netif status check here to avoid system hang in below case:
3018 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
3019 * After ethx down, fec all clocks are gated off and then register
3020 * access causes system hang.
3022 if (!netif_running(ndev
))
3025 writel(ndev
->dev_addr
[3] | (ndev
->dev_addr
[2] << 8) |
3026 (ndev
->dev_addr
[1] << 16) | (ndev
->dev_addr
[0] << 24),
3027 fep
->hwp
+ FEC_ADDR_LOW
);
3028 writel((ndev
->dev_addr
[5] << 16) | (ndev
->dev_addr
[4] << 24),
3029 fep
->hwp
+ FEC_ADDR_HIGH
);
3033 #ifdef CONFIG_NET_POLL_CONTROLLER
3035 * fec_poll_controller - FEC Poll controller function
3036 * @dev: The FEC network adapter
3038 * Polled functionality used by netconsole and others in non interrupt mode
3041 static void fec_poll_controller(struct net_device
*dev
)
3044 struct fec_enet_private
*fep
= netdev_priv(dev
);
3046 for (i
= 0; i
< FEC_IRQ_NUM
; i
++) {
3047 if (fep
->irq
[i
] > 0) {
3048 disable_irq(fep
->irq
[i
]);
3049 fec_enet_interrupt(fep
->irq
[i
], dev
);
3050 enable_irq(fep
->irq
[i
]);
3056 static inline void fec_enet_set_netdev_features(struct net_device
*netdev
,
3057 netdev_features_t features
)
3059 struct fec_enet_private
*fep
= netdev_priv(netdev
);
3060 netdev_features_t changed
= features
^ netdev
->features
;
3062 netdev
->features
= features
;
3064 /* Receive checksum has been changed */
3065 if (changed
& NETIF_F_RXCSUM
) {
3066 if (features
& NETIF_F_RXCSUM
)
3067 fep
->csum_flags
|= FLAG_RX_CSUM_ENABLED
;
3069 fep
->csum_flags
&= ~FLAG_RX_CSUM_ENABLED
;
3073 static int fec_set_features(struct net_device
*netdev
,
3074 netdev_features_t features
)
3076 struct fec_enet_private
*fep
= netdev_priv(netdev
);
3077 netdev_features_t changed
= features
^ netdev
->features
;
3079 if (netif_running(netdev
) && changed
& NETIF_F_RXCSUM
) {
3080 napi_disable(&fep
->napi
);
3081 netif_tx_lock_bh(netdev
);
3083 fec_enet_set_netdev_features(netdev
, features
);
3084 fec_restart(netdev
);
3085 netif_tx_wake_all_queues(netdev
);
3086 netif_tx_unlock_bh(netdev
);
3087 napi_enable(&fep
->napi
);
3089 fec_enet_set_netdev_features(netdev
, features
);
3095 static const struct net_device_ops fec_netdev_ops
= {
3096 .ndo_open
= fec_enet_open
,
3097 .ndo_stop
= fec_enet_close
,
3098 .ndo_start_xmit
= fec_enet_start_xmit
,
3099 .ndo_set_rx_mode
= set_multicast_list
,
3100 .ndo_change_mtu
= eth_change_mtu
,
3101 .ndo_validate_addr
= eth_validate_addr
,
3102 .ndo_tx_timeout
= fec_timeout
,
3103 .ndo_set_mac_address
= fec_set_mac_address
,
3104 .ndo_do_ioctl
= fec_enet_ioctl
,
3105 #ifdef CONFIG_NET_POLL_CONTROLLER
3106 .ndo_poll_controller
= fec_poll_controller
,
3108 .ndo_set_features
= fec_set_features
,
3112 * XXX: We need to clean up on failure exits here.
3115 static int fec_enet_init(struct net_device
*ndev
)
3117 struct fec_enet_private
*fep
= netdev_priv(ndev
);
3118 struct fec_enet_priv_tx_q
*txq
;
3119 struct fec_enet_priv_rx_q
*rxq
;
3120 struct bufdesc
*cbd_base
;
3125 #if defined(CONFIG_ARM)
3126 fep
->rx_align
= 0xf;
3127 fep
->tx_align
= 0xf;
3129 fep
->rx_align
= 0x3;
3130 fep
->tx_align
= 0x3;
3133 fec_enet_alloc_queue(ndev
);
3135 if (fep
->bufdesc_ex
)
3136 fep
->bufdesc_size
= sizeof(struct bufdesc_ex
);
3138 fep
->bufdesc_size
= sizeof(struct bufdesc
);
3139 bd_size
= (fep
->total_tx_ring_size
+ fep
->total_rx_ring_size
) *
3142 /* Allocate memory for buffer descriptors. */
3143 cbd_base
= dmam_alloc_coherent(&fep
->pdev
->dev
, bd_size
, &bd_dma
,
3149 memset(cbd_base
, 0, bd_size
);
3151 /* Get the Ethernet address */
3153 /* make sure MAC we just acquired is programmed into the hw */
3154 fec_set_mac_address(ndev
, NULL
);
3156 /* Set receive and transmit descriptor base. */
3157 for (i
= 0; i
< fep
->num_rx_queues
; i
++) {
3158 rxq
= fep
->rx_queue
[i
];
3160 rxq
->rx_bd_base
= (struct bufdesc
*)cbd_base
;
3161 rxq
->bd_dma
= bd_dma
;
3162 if (fep
->bufdesc_ex
) {
3163 bd_dma
+= sizeof(struct bufdesc_ex
) * rxq
->rx_ring_size
;
3164 cbd_base
= (struct bufdesc
*)
3165 (((struct bufdesc_ex
*)cbd_base
) + rxq
->rx_ring_size
);
3167 bd_dma
+= sizeof(struct bufdesc
) * rxq
->rx_ring_size
;
3168 cbd_base
+= rxq
->rx_ring_size
;
3172 for (i
= 0; i
< fep
->num_tx_queues
; i
++) {
3173 txq
= fep
->tx_queue
[i
];
3175 txq
->tx_bd_base
= (struct bufdesc
*)cbd_base
;
3176 txq
->bd_dma
= bd_dma
;
3177 if (fep
->bufdesc_ex
) {
3178 bd_dma
+= sizeof(struct bufdesc_ex
) * txq
->tx_ring_size
;
3179 cbd_base
= (struct bufdesc
*)
3180 (((struct bufdesc_ex
*)cbd_base
) + txq
->tx_ring_size
);
3182 bd_dma
+= sizeof(struct bufdesc
) * txq
->tx_ring_size
;
3183 cbd_base
+= txq
->tx_ring_size
;
3188 /* The FEC Ethernet specific entries in the device structure */
3189 ndev
->watchdog_timeo
= TX_TIMEOUT
;
3190 ndev
->netdev_ops
= &fec_netdev_ops
;
3191 ndev
->ethtool_ops
= &fec_enet_ethtool_ops
;
3193 writel(FEC_RX_DISABLED_IMASK
, fep
->hwp
+ FEC_IMASK
);
3194 netif_napi_add(ndev
, &fep
->napi
, fec_enet_rx_napi
, NAPI_POLL_WEIGHT
);
3196 if (fep
->quirks
& FEC_QUIRK_HAS_VLAN
)
3197 /* enable hw VLAN support */
3198 ndev
->features
|= NETIF_F_HW_VLAN_CTAG_RX
;
3200 if (fep
->quirks
& FEC_QUIRK_HAS_CSUM
) {
3201 ndev
->gso_max_segs
= FEC_MAX_TSO_SEGS
;
3203 /* enable hw accelerator */
3204 ndev
->features
|= (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
3205 | NETIF_F_RXCSUM
| NETIF_F_SG
| NETIF_F_TSO
);
3206 fep
->csum_flags
|= FLAG_RX_CSUM_ENABLED
;
3209 if (fep
->quirks
& FEC_QUIRK_HAS_AVB
) {
3211 fep
->rx_align
= 0x3f;
3214 ndev
->hw_features
= ndev
->features
;
3222 static void fec_reset_phy(struct platform_device
*pdev
)
3226 struct device_node
*np
= pdev
->dev
.of_node
;
3231 of_property_read_u32(np
, "phy-reset-duration", &msec
);
3232 /* A sane reset duration should not be longer than 1s */
3236 phy_reset
= of_get_named_gpio(np
, "phy-reset-gpios", 0);
3237 if (!gpio_is_valid(phy_reset
))
3240 err
= devm_gpio_request_one(&pdev
->dev
, phy_reset
,
3241 GPIOF_OUT_INIT_LOW
, "phy-reset");
3243 dev_err(&pdev
->dev
, "failed to get phy-reset-gpios: %d\n", err
);
3247 gpio_set_value_cansleep(phy_reset
, 1);
3249 #else /* CONFIG_OF */
3250 static void fec_reset_phy(struct platform_device
*pdev
)
3253 * In case of platform probe, the reset has been done
3257 #endif /* CONFIG_OF */
3260 fec_enet_get_queue_num(struct platform_device
*pdev
, int *num_tx
, int *num_rx
)
3262 struct device_node
*np
= pdev
->dev
.of_node
;
3264 *num_tx
= *num_rx
= 1;
3266 if (!np
|| !of_device_is_available(np
))
3269 /* parse the num of tx and rx queues */
3270 of_property_read_u32(np
, "fsl,num-tx-queues", num_tx
);
3272 of_property_read_u32(np
, "fsl,num-rx-queues", num_rx
);
3274 if (*num_tx
< 1 || *num_tx
> FEC_ENET_MAX_TX_QS
) {
3275 dev_warn(&pdev
->dev
, "Invalid num_tx(=%d), fall back to 1\n",
3281 if (*num_rx
< 1 || *num_rx
> FEC_ENET_MAX_RX_QS
) {
3282 dev_warn(&pdev
->dev
, "Invalid num_rx(=%d), fall back to 1\n",
3291 fec_probe(struct platform_device
*pdev
)
3293 struct fec_enet_private
*fep
;
3294 struct fec_platform_data
*pdata
;
3295 struct net_device
*ndev
;
3296 int i
, irq
, ret
= 0;
3298 const struct of_device_id
*of_id
;
3300 struct device_node
*np
= pdev
->dev
.of_node
, *phy_node
;
3304 fec_enet_get_queue_num(pdev
, &num_tx_qs
, &num_rx_qs
);
3306 /* Init network device */
3307 ndev
= alloc_etherdev_mqs(sizeof(struct fec_enet_private
),
3308 num_tx_qs
, num_rx_qs
);
3312 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
3314 /* setup board info structure */
3315 fep
= netdev_priv(ndev
);
3317 of_id
= of_match_device(fec_dt_ids
, &pdev
->dev
);
3319 pdev
->id_entry
= of_id
->data
;
3320 fep
->quirks
= pdev
->id_entry
->driver_data
;
3323 fep
->num_rx_queues
= num_rx_qs
;
3324 fep
->num_tx_queues
= num_tx_qs
;
3326 #if !defined(CONFIG_M5272)
3327 /* default enable pause frame auto negotiation */
3328 if (fep
->quirks
& FEC_QUIRK_HAS_GBIT
)
3329 fep
->pause_flag
|= FEC_PAUSE_FLAG_AUTONEG
;
3332 /* Select default pin state */
3333 pinctrl_pm_select_default_state(&pdev
->dev
);
3335 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3336 fep
->hwp
= devm_ioremap_resource(&pdev
->dev
, r
);
3337 if (IS_ERR(fep
->hwp
)) {
3338 ret
= PTR_ERR(fep
->hwp
);
3339 goto failed_ioremap
;
3343 fep
->dev_id
= dev_id
++;
3345 platform_set_drvdata(pdev
, ndev
);
3347 if (of_get_property(np
, "fsl,magic-packet", NULL
))
3348 fep
->wol_flag
|= FEC_WOL_HAS_MAGIC_PACKET
;
3350 phy_node
= of_parse_phandle(np
, "phy-handle", 0);
3351 if (!phy_node
&& of_phy_is_fixed_link(np
)) {
3352 ret
= of_phy_register_fixed_link(np
);
3355 "broken fixed-link specification\n");
3358 phy_node
= of_node_get(np
);
3360 fep
->phy_node
= phy_node
;
3362 ret
= of_get_phy_mode(pdev
->dev
.of_node
);
3364 pdata
= dev_get_platdata(&pdev
->dev
);
3366 fep
->phy_interface
= pdata
->phy
;
3368 fep
->phy_interface
= PHY_INTERFACE_MODE_MII
;
3370 fep
->phy_interface
= ret
;
3373 fep
->clk_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
3374 if (IS_ERR(fep
->clk_ipg
)) {
3375 ret
= PTR_ERR(fep
->clk_ipg
);
3379 fep
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
3380 if (IS_ERR(fep
->clk_ahb
)) {
3381 ret
= PTR_ERR(fep
->clk_ahb
);
3385 fep
->itr_clk_rate
= clk_get_rate(fep
->clk_ahb
);
3387 /* enet_out is optional, depends on board */
3388 fep
->clk_enet_out
= devm_clk_get(&pdev
->dev
, "enet_out");
3389 if (IS_ERR(fep
->clk_enet_out
))
3390 fep
->clk_enet_out
= NULL
;
3392 fep
->ptp_clk_on
= false;
3393 mutex_init(&fep
->ptp_clk_mutex
);
3395 /* clk_ref is optional, depends on board */
3396 fep
->clk_ref
= devm_clk_get(&pdev
->dev
, "enet_clk_ref");
3397 if (IS_ERR(fep
->clk_ref
))
3398 fep
->clk_ref
= NULL
;
3400 fep
->bufdesc_ex
= fep
->quirks
& FEC_QUIRK_HAS_BUFDESC_EX
;
3401 fep
->clk_ptp
= devm_clk_get(&pdev
->dev
, "ptp");
3402 if (IS_ERR(fep
->clk_ptp
)) {
3403 fep
->clk_ptp
= NULL
;
3404 fep
->bufdesc_ex
= false;
3407 ret
= fec_enet_clk_enable(ndev
, true);
3411 ret
= clk_prepare_enable(fep
->clk_ipg
);
3413 goto failed_clk_ipg
;
3415 fep
->reg_phy
= devm_regulator_get(&pdev
->dev
, "phy");
3416 if (!IS_ERR(fep
->reg_phy
)) {
3417 ret
= regulator_enable(fep
->reg_phy
);
3420 "Failed to enable phy regulator: %d\n", ret
);
3421 goto failed_regulator
;
3424 fep
->reg_phy
= NULL
;
3427 pm_runtime_set_autosuspend_delay(&pdev
->dev
, FEC_MDIO_PM_TIMEOUT
);
3428 pm_runtime_use_autosuspend(&pdev
->dev
);
3429 pm_runtime_get_noresume(&pdev
->dev
);
3430 pm_runtime_set_active(&pdev
->dev
);
3431 pm_runtime_enable(&pdev
->dev
);
3433 fec_reset_phy(pdev
);
3435 if (fep
->bufdesc_ex
)
3438 ret
= fec_enet_init(ndev
);
3442 for (i
= 0; i
< FEC_IRQ_NUM
; i
++) {
3443 irq
= platform_get_irq(pdev
, i
);
3450 ret
= devm_request_irq(&pdev
->dev
, irq
, fec_enet_interrupt
,
3451 0, pdev
->name
, ndev
);
3458 init_completion(&fep
->mdio_done
);
3459 ret
= fec_enet_mii_init(pdev
);
3461 goto failed_mii_init
;
3463 /* Carrier starts down, phylib will bring it up */
3464 netif_carrier_off(ndev
);
3465 fec_enet_clk_enable(ndev
, false);
3466 pinctrl_pm_select_sleep_state(&pdev
->dev
);
3468 ret
= register_netdev(ndev
);
3470 goto failed_register
;
3472 device_init_wakeup(&ndev
->dev
, fep
->wol_flag
&
3473 FEC_WOL_HAS_MAGIC_PACKET
);
3475 if (fep
->bufdesc_ex
&& fep
->ptp_clock
)
3476 netdev_info(ndev
, "registered PHC device %d\n", fep
->dev_id
);
3478 fep
->rx_copybreak
= COPYBREAK_DEFAULT
;
3479 INIT_WORK(&fep
->tx_timeout_work
, fec_enet_timeout_work
);
3481 pm_runtime_mark_last_busy(&pdev
->dev
);
3482 pm_runtime_put_autosuspend(&pdev
->dev
);
3487 fec_enet_mii_remove(fep
);
3493 regulator_disable(fep
->reg_phy
);
3495 clk_disable_unprepare(fep
->clk_ipg
);
3497 fec_enet_clk_enable(ndev
, false);
3500 of_node_put(phy_node
);
3508 fec_drv_remove(struct platform_device
*pdev
)
3510 struct net_device
*ndev
= platform_get_drvdata(pdev
);
3511 struct fec_enet_private
*fep
= netdev_priv(ndev
);
3513 cancel_work_sync(&fep
->tx_timeout_work
);
3515 unregister_netdev(ndev
);
3516 fec_enet_mii_remove(fep
);
3518 regulator_disable(fep
->reg_phy
);
3519 of_node_put(fep
->phy_node
);
3525 static int __maybe_unused
fec_suspend(struct device
*dev
)
3527 struct net_device
*ndev
= dev_get_drvdata(dev
);
3528 struct fec_enet_private
*fep
= netdev_priv(ndev
);
3531 if (netif_running(ndev
)) {
3532 if (fep
->wol_flag
& FEC_WOL_FLAG_ENABLE
)
3533 fep
->wol_flag
|= FEC_WOL_FLAG_SLEEP_ON
;
3534 phy_stop(fep
->phy_dev
);
3535 napi_disable(&fep
->napi
);
3536 netif_tx_lock_bh(ndev
);
3537 netif_device_detach(ndev
);
3538 netif_tx_unlock_bh(ndev
);
3540 fec_enet_clk_enable(ndev
, false);
3541 if (!(fep
->wol_flag
& FEC_WOL_FLAG_ENABLE
))
3542 pinctrl_pm_select_sleep_state(&fep
->pdev
->dev
);
3546 if (fep
->reg_phy
&& !(fep
->wol_flag
& FEC_WOL_FLAG_ENABLE
))
3547 regulator_disable(fep
->reg_phy
);
3549 /* SOC supply clock to phy, when clock is disabled, phy link down
3550 * SOC control phy regulator, when regulator is disabled, phy link down
3552 if (fep
->clk_enet_out
|| fep
->reg_phy
)
3558 static int __maybe_unused
fec_resume(struct device
*dev
)
3560 struct net_device
*ndev
= dev_get_drvdata(dev
);
3561 struct fec_enet_private
*fep
= netdev_priv(ndev
);
3562 struct fec_platform_data
*pdata
= fep
->pdev
->dev
.platform_data
;
3566 if (fep
->reg_phy
&& !(fep
->wol_flag
& FEC_WOL_FLAG_ENABLE
)) {
3567 ret
= regulator_enable(fep
->reg_phy
);
3573 if (netif_running(ndev
)) {
3574 ret
= fec_enet_clk_enable(ndev
, true);
3579 if (fep
->wol_flag
& FEC_WOL_FLAG_ENABLE
) {
3580 if (pdata
&& pdata
->sleep_mode_enable
)
3581 pdata
->sleep_mode_enable(false);
3582 val
= readl(fep
->hwp
+ FEC_ECNTRL
);
3583 val
&= ~(FEC_ECR_MAGICEN
| FEC_ECR_SLEEP
);
3584 writel(val
, fep
->hwp
+ FEC_ECNTRL
);
3585 fep
->wol_flag
&= ~FEC_WOL_FLAG_SLEEP_ON
;
3587 pinctrl_pm_select_default_state(&fep
->pdev
->dev
);
3590 netif_tx_lock_bh(ndev
);
3591 netif_device_attach(ndev
);
3592 netif_tx_unlock_bh(ndev
);
3593 napi_enable(&fep
->napi
);
3594 phy_start(fep
->phy_dev
);
3602 regulator_disable(fep
->reg_phy
);
3606 static int __maybe_unused
fec_runtime_suspend(struct device
*dev
)
3608 struct net_device
*ndev
= dev_get_drvdata(dev
);
3609 struct fec_enet_private
*fep
= netdev_priv(ndev
);
3611 clk_disable_unprepare(fep
->clk_ipg
);
3616 static int __maybe_unused
fec_runtime_resume(struct device
*dev
)
3618 struct net_device
*ndev
= dev_get_drvdata(dev
);
3619 struct fec_enet_private
*fep
= netdev_priv(ndev
);
3621 return clk_prepare_enable(fep
->clk_ipg
);
3624 static const struct dev_pm_ops fec_pm_ops
= {
3625 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend
, fec_resume
)
3626 SET_RUNTIME_PM_OPS(fec_runtime_suspend
, fec_runtime_resume
, NULL
)
3629 static struct platform_driver fec_driver
= {
3631 .name
= DRIVER_NAME
,
3633 .of_match_table
= fec_dt_ids
,
3635 .id_table
= fec_devtype
,
3637 .remove
= fec_drv_remove
,
3640 module_platform_driver(fec_driver
);
3642 MODULE_ALIAS("platform:"DRIVER_NAME
);
3643 MODULE_LICENSE("GPL");