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/ptrace.h>
28 #include <linux/errno.h>
29 #include <linux/ioport.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
40 #include <linux/tcp.h>
41 #include <linux/udp.h>
42 #include <linux/icmp.h>
43 #include <linux/spinlock.h>
44 #include <linux/workqueue.h>
45 #include <linux/bitops.h>
47 #include <linux/irq.h>
48 #include <linux/clk.h>
49 #include <linux/platform_device.h>
50 #include <linux/phy.h>
51 #include <linux/fec.h>
53 #include <linux/of_device.h>
54 #include <linux/of_gpio.h>
55 #include <linux/of_net.h>
56 #include <linux/pinctrl/consumer.h>
57 #include <linux/regulator/consumer.h>
59 #include <asm/cacheflush.h>
63 #if defined(CONFIG_ARM)
64 #define FEC_ALIGNMENT 0xf
66 #define FEC_ALIGNMENT 0x3
69 #define DRIVER_NAME "fec"
70 #define FEC_NAPI_WEIGHT 64
72 /* Pause frame feild and FIFO threshold */
73 #define FEC_ENET_FCE (1 << 5)
74 #define FEC_ENET_RSEM_V 0x84
75 #define FEC_ENET_RSFL_V 16
76 #define FEC_ENET_RAEM_V 0x8
77 #define FEC_ENET_RAFL_V 0x8
78 #define FEC_ENET_OPD_V 0xFFF0
80 /* Controller is ENET-MAC */
81 #define FEC_QUIRK_ENET_MAC (1 << 0)
82 /* Controller needs driver to swap frame */
83 #define FEC_QUIRK_SWAP_FRAME (1 << 1)
84 /* Controller uses gasket */
85 #define FEC_QUIRK_USE_GASKET (1 << 2)
86 /* Controller has GBIT support */
87 #define FEC_QUIRK_HAS_GBIT (1 << 3)
88 /* Controller has extend desc buffer */
89 #define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
91 static struct platform_device_id fec_devtype
[] = {
93 /* keep it for coldfire */
98 .driver_data
= FEC_QUIRK_USE_GASKET
,
104 .driver_data
= FEC_QUIRK_ENET_MAC
| FEC_QUIRK_SWAP_FRAME
,
107 .driver_data
= FEC_QUIRK_ENET_MAC
| FEC_QUIRK_HAS_GBIT
|
108 FEC_QUIRK_HAS_BUFDESC_EX
,
111 .driver_data
= FEC_QUIRK_ENET_MAC
,
116 MODULE_DEVICE_TABLE(platform
, fec_devtype
);
119 IMX25_FEC
= 1, /* runs on i.mx25/50/53 */
120 IMX27_FEC
, /* runs on i.mx27/35/51 */
126 static const struct of_device_id fec_dt_ids
[] = {
127 { .compatible
= "fsl,imx25-fec", .data
= &fec_devtype
[IMX25_FEC
], },
128 { .compatible
= "fsl,imx27-fec", .data
= &fec_devtype
[IMX27_FEC
], },
129 { .compatible
= "fsl,imx28-fec", .data
= &fec_devtype
[IMX28_FEC
], },
130 { .compatible
= "fsl,imx6q-fec", .data
= &fec_devtype
[IMX6Q_FEC
], },
131 { .compatible
= "fsl,mvf-fec", .data
= &fec_devtype
[MVF_FEC
], },
134 MODULE_DEVICE_TABLE(of
, fec_dt_ids
);
136 static unsigned char macaddr
[ETH_ALEN
];
137 module_param_array(macaddr
, byte
, NULL
, 0);
138 MODULE_PARM_DESC(macaddr
, "FEC Ethernet MAC address");
140 #if defined(CONFIG_M5272)
142 * Some hardware gets it MAC address out of local flash memory.
143 * if this is non-zero then assume it is the address to get MAC from.
145 #if defined(CONFIG_NETtel)
146 #define FEC_FLASHMAC 0xf0006006
147 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
148 #define FEC_FLASHMAC 0xf0006000
149 #elif defined(CONFIG_CANCam)
150 #define FEC_FLASHMAC 0xf0020000
151 #elif defined (CONFIG_M5272C3)
152 #define FEC_FLASHMAC (0xffe04000 + 4)
153 #elif defined(CONFIG_MOD5272)
154 #define FEC_FLASHMAC 0xffc0406b
156 #define FEC_FLASHMAC 0
158 #endif /* CONFIG_M5272 */
160 #if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
161 #error "FEC: descriptor ring size constants too large"
164 /* Interrupt events/masks. */
165 #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
166 #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
167 #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
168 #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
169 #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
170 #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
171 #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
172 #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
173 #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
174 #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
176 #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
177 #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
179 /* The FEC stores dest/src/type, data, and checksum for receive packets.
181 #define PKT_MAXBUF_SIZE 1518
182 #define PKT_MINBUF_SIZE 64
183 #define PKT_MAXBLR_SIZE 1520
185 /* FEC receive acceleration */
186 #define FEC_RACC_IPDIS (1 << 1)
187 #define FEC_RACC_PRODIS (1 << 2)
188 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
191 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
192 * size bits. Other FEC hardware does not, so we need to take that into
193 * account when setting it.
195 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
196 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
197 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
199 #define OPT_FRAME_SIZE 0
202 /* FEC MII MMFR bits definition */
203 #define FEC_MMFR_ST (1 << 30)
204 #define FEC_MMFR_OP_READ (2 << 28)
205 #define FEC_MMFR_OP_WRITE (1 << 28)
206 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
207 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
208 #define FEC_MMFR_TA (2 << 16)
209 #define FEC_MMFR_DATA(v) (v & 0xffff)
211 #define FEC_MII_TIMEOUT 30000 /* us */
213 /* Transmitter timeout */
214 #define TX_TIMEOUT (2 * HZ)
216 #define FEC_PAUSE_FLAG_AUTONEG 0x1
217 #define FEC_PAUSE_FLAG_ENABLE 0x2
221 static struct bufdesc
*fec_enet_get_nextdesc(struct bufdesc
*bdp
, int is_ex
)
223 struct bufdesc_ex
*ex
= (struct bufdesc_ex
*)bdp
;
225 return (struct bufdesc
*)(ex
+ 1);
230 static struct bufdesc
*fec_enet_get_prevdesc(struct bufdesc
*bdp
, int is_ex
)
232 struct bufdesc_ex
*ex
= (struct bufdesc_ex
*)bdp
;
234 return (struct bufdesc
*)(ex
- 1);
239 static void *swap_buffer(void *bufaddr
, int len
)
242 unsigned int *buf
= bufaddr
;
244 for (i
= 0; i
< (len
+ 3) / 4; i
++, buf
++)
245 *buf
= cpu_to_be32(*buf
);
251 fec_enet_clear_csum(struct sk_buff
*skb
, struct net_device
*ndev
)
253 /* Only run for packets requiring a checksum. */
254 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
257 if (unlikely(skb_cow_head(skb
, 0)))
260 *(__sum16
*)(skb
->head
+ skb
->csum_start
+ skb
->csum_offset
) = 0;
266 fec_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
268 struct fec_enet_private
*fep
= netdev_priv(ndev
);
269 const struct platform_device_id
*id_entry
=
270 platform_get_device_id(fep
->pdev
);
273 unsigned short status
;
277 /* Link is down or auto-negotiation is in progress. */
278 return NETDEV_TX_BUSY
;
281 /* Fill in a Tx ring entry */
284 status
= bdp
->cbd_sc
;
286 if (status
& BD_ENET_TX_READY
) {
287 /* Ooops. All transmit buffers are full. Bail out.
288 * This should not happen, since ndev->tbusy should be set.
290 netdev_err(ndev
, "tx queue full!\n");
291 return NETDEV_TX_BUSY
;
294 /* Protocol checksum off-load for TCP and UDP. */
295 if (fec_enet_clear_csum(skb
, ndev
)) {
300 /* Clear all of the status flags */
301 status
&= ~BD_ENET_TX_STATS
;
303 /* Set buffer length and buffer pointer */
305 bdp
->cbd_datlen
= skb
->len
;
308 * On some FEC implementations data must be aligned on
309 * 4-byte boundaries. Use bounce buffers to copy data
310 * and get it aligned. Ugh.
313 index
= (struct bufdesc_ex
*)bdp
-
314 (struct bufdesc_ex
*)fep
->tx_bd_base
;
316 index
= bdp
- fep
->tx_bd_base
;
318 if (((unsigned long) bufaddr
) & FEC_ALIGNMENT
) {
319 memcpy(fep
->tx_bounce
[index
], skb
->data
, skb
->len
);
320 bufaddr
= fep
->tx_bounce
[index
];
324 * Some design made an incorrect assumption on endian mode of
325 * the system that it's running on. As the result, driver has to
326 * swap every frame going to and coming from the controller.
328 if (id_entry
->driver_data
& FEC_QUIRK_SWAP_FRAME
)
329 swap_buffer(bufaddr
, skb
->len
);
331 /* Save skb pointer */
332 fep
->tx_skbuff
[index
] = skb
;
334 /* Push the data cache so the CPM does not get stale memory
337 bdp
->cbd_bufaddr
= dma_map_single(&fep
->pdev
->dev
, bufaddr
,
338 FEC_ENET_TX_FRSIZE
, DMA_TO_DEVICE
);
340 /* Send it on its way. Tell FEC it's ready, interrupt when done,
341 * it's the last BD of the frame, and to put the CRC on the end.
343 status
|= (BD_ENET_TX_READY
| BD_ENET_TX_INTR
344 | BD_ENET_TX_LAST
| BD_ENET_TX_TC
);
345 bdp
->cbd_sc
= status
;
347 if (fep
->bufdesc_ex
) {
349 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
351 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
&&
353 ebdp
->cbd_esc
= (BD_ENET_TX_TS
| BD_ENET_TX_INT
);
354 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
356 ebdp
->cbd_esc
= BD_ENET_TX_INT
;
358 /* Enable protocol checksum flags
359 * We do not bother with the IP Checksum bits as they
360 * are done by the kernel
362 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
363 ebdp
->cbd_esc
|= BD_ENET_TX_PINS
;
366 /* If this was the last BD in the ring, start at the beginning again. */
367 if (status
& BD_ENET_TX_WRAP
)
368 bdp
= fep
->tx_bd_base
;
370 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
374 if (fep
->cur_tx
== fep
->dirty_tx
)
375 netif_stop_queue(ndev
);
377 /* Trigger transmission start */
378 writel(0, fep
->hwp
+ FEC_X_DES_ACTIVE
);
380 skb_tx_timestamp(skb
);
385 /* Init RX & TX buffer descriptors
387 static void fec_enet_bd_init(struct net_device
*dev
)
389 struct fec_enet_private
*fep
= netdev_priv(dev
);
393 /* Initialize the receive buffer descriptors. */
394 bdp
= fep
->rx_bd_base
;
395 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
397 /* Initialize the BD for every fragment in the page. */
398 if (bdp
->cbd_bufaddr
)
399 bdp
->cbd_sc
= BD_ENET_RX_EMPTY
;
402 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
405 /* Set the last buffer to wrap */
406 bdp
= fec_enet_get_prevdesc(bdp
, fep
->bufdesc_ex
);
407 bdp
->cbd_sc
|= BD_SC_WRAP
;
409 fep
->cur_rx
= fep
->rx_bd_base
;
411 /* ...and the same for transmit */
412 bdp
= fep
->tx_bd_base
;
414 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
416 /* Initialize the BD for every fragment in the page. */
418 if (bdp
->cbd_bufaddr
&& fep
->tx_skbuff
[i
]) {
419 dev_kfree_skb_any(fep
->tx_skbuff
[i
]);
420 fep
->tx_skbuff
[i
] = NULL
;
422 bdp
->cbd_bufaddr
= 0;
423 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
426 /* Set the last buffer to wrap */
427 bdp
= fec_enet_get_prevdesc(bdp
, fep
->bufdesc_ex
);
428 bdp
->cbd_sc
|= BD_SC_WRAP
;
432 /* This function is called to start or restart the FEC during a link
433 * change. This only happens when switching between half and full
437 fec_restart(struct net_device
*ndev
, int duplex
)
439 struct fec_enet_private
*fep
= netdev_priv(ndev
);
440 const struct platform_device_id
*id_entry
=
441 platform_get_device_id(fep
->pdev
);
445 u32 rcntl
= OPT_FRAME_SIZE
| 0x04;
446 u32 ecntl
= 0x2; /* ETHEREN */
448 if (netif_running(ndev
)) {
449 netif_device_detach(ndev
);
450 napi_disable(&fep
->napi
);
451 netif_stop_queue(ndev
);
455 /* Whack a reset. We should wait for this. */
456 writel(1, fep
->hwp
+ FEC_ECNTRL
);
460 * enet-mac reset will reset mac address registers too,
461 * so need to reconfigure it.
463 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) {
464 memcpy(&temp_mac
, ndev
->dev_addr
, ETH_ALEN
);
465 writel(cpu_to_be32(temp_mac
[0]), fep
->hwp
+ FEC_ADDR_LOW
);
466 writel(cpu_to_be32(temp_mac
[1]), fep
->hwp
+ FEC_ADDR_HIGH
);
469 /* Clear any outstanding interrupt. */
470 writel(0xffc00000, fep
->hwp
+ FEC_IEVENT
);
472 /* Reset all multicast. */
473 writel(0, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
474 writel(0, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
476 writel(0, fep
->hwp
+ FEC_HASH_TABLE_HIGH
);
477 writel(0, fep
->hwp
+ FEC_HASH_TABLE_LOW
);
480 /* Set maximum receive buffer size. */
481 writel(PKT_MAXBLR_SIZE
, fep
->hwp
+ FEC_R_BUFF_SIZE
);
483 fec_enet_bd_init(ndev
);
485 /* Set receive and transmit descriptor base. */
486 writel(fep
->bd_dma
, fep
->hwp
+ FEC_R_DES_START
);
488 writel((unsigned long)fep
->bd_dma
+ sizeof(struct bufdesc_ex
)
489 * RX_RING_SIZE
, fep
->hwp
+ FEC_X_DES_START
);
491 writel((unsigned long)fep
->bd_dma
+ sizeof(struct bufdesc
)
492 * RX_RING_SIZE
, fep
->hwp
+ FEC_X_DES_START
);
495 for (i
= 0; i
<= TX_RING_MOD_MASK
; i
++) {
496 if (fep
->tx_skbuff
[i
]) {
497 dev_kfree_skb_any(fep
->tx_skbuff
[i
]);
498 fep
->tx_skbuff
[i
] = NULL
;
502 /* Enable MII mode */
505 writel(0x04, fep
->hwp
+ FEC_X_CNTRL
);
509 writel(0x0, fep
->hwp
+ FEC_X_CNTRL
);
512 fep
->full_duplex
= duplex
;
515 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
517 /* set RX checksum */
518 val
= readl(fep
->hwp
+ FEC_RACC
);
519 if (fep
->csum_flags
& FLAG_RX_CSUM_ENABLED
)
520 val
|= FEC_RACC_OPTIONS
;
522 val
&= ~FEC_RACC_OPTIONS
;
523 writel(val
, fep
->hwp
+ FEC_RACC
);
526 * The phy interface and speed need to get configured
527 * differently on enet-mac.
529 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) {
530 /* Enable flow control and length check */
531 rcntl
|= 0x40000000 | 0x00000020;
533 /* RGMII, RMII or MII */
534 if (fep
->phy_interface
== PHY_INTERFACE_MODE_RGMII
)
536 else if (fep
->phy_interface
== PHY_INTERFACE_MODE_RMII
)
541 /* 1G, 100M or 10M */
543 if (fep
->phy_dev
->speed
== SPEED_1000
)
545 else if (fep
->phy_dev
->speed
== SPEED_100
)
551 #ifdef FEC_MIIGSK_ENR
552 if (id_entry
->driver_data
& FEC_QUIRK_USE_GASKET
) {
554 /* disable the gasket and wait */
555 writel(0, fep
->hwp
+ FEC_MIIGSK_ENR
);
556 while (readl(fep
->hwp
+ FEC_MIIGSK_ENR
) & 4)
560 * configure the gasket:
561 * RMII, 50 MHz, no loopback, no echo
562 * MII, 25 MHz, no loopback, no echo
564 cfgr
= (fep
->phy_interface
== PHY_INTERFACE_MODE_RMII
)
565 ? BM_MIIGSK_CFGR_RMII
: BM_MIIGSK_CFGR_MII
;
566 if (fep
->phy_dev
&& fep
->phy_dev
->speed
== SPEED_10
)
567 cfgr
|= BM_MIIGSK_CFGR_FRCONT_10M
;
568 writel(cfgr
, fep
->hwp
+ FEC_MIIGSK_CFGR
);
570 /* re-enable the gasket */
571 writel(2, fep
->hwp
+ FEC_MIIGSK_ENR
);
576 /* enable pause frame*/
577 if ((fep
->pause_flag
& FEC_PAUSE_FLAG_ENABLE
) ||
578 ((fep
->pause_flag
& FEC_PAUSE_FLAG_AUTONEG
) &&
579 fep
->phy_dev
&& fep
->phy_dev
->pause
)) {
580 rcntl
|= FEC_ENET_FCE
;
582 /* set FIFO threshold parameter to reduce overrun */
583 writel(FEC_ENET_RSEM_V
, fep
->hwp
+ FEC_R_FIFO_RSEM
);
584 writel(FEC_ENET_RSFL_V
, fep
->hwp
+ FEC_R_FIFO_RSFL
);
585 writel(FEC_ENET_RAEM_V
, fep
->hwp
+ FEC_R_FIFO_RAEM
);
586 writel(FEC_ENET_RAFL_V
, fep
->hwp
+ FEC_R_FIFO_RAFL
);
589 writel(FEC_ENET_OPD_V
, fep
->hwp
+ FEC_OPD
);
591 rcntl
&= ~FEC_ENET_FCE
;
594 writel(rcntl
, fep
->hwp
+ FEC_R_CNTRL
);
596 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) {
597 /* enable ENET endian swap */
599 /* enable ENET store and forward mode */
600 writel(1 << 8, fep
->hwp
+ FEC_X_WMRK
);
606 /* And last, enable the transmit and receive processing */
607 writel(ecntl
, fep
->hwp
+ FEC_ECNTRL
);
608 writel(0, fep
->hwp
+ FEC_R_DES_ACTIVE
);
611 fec_ptp_start_cyclecounter(ndev
);
613 /* Enable interrupts we wish to service */
614 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
616 if (netif_running(ndev
)) {
617 netif_device_attach(ndev
);
618 napi_enable(&fep
->napi
);
619 netif_wake_queue(ndev
);
620 netif_tx_unlock(ndev
);
625 fec_stop(struct net_device
*ndev
)
627 struct fec_enet_private
*fep
= netdev_priv(ndev
);
628 const struct platform_device_id
*id_entry
=
629 platform_get_device_id(fep
->pdev
);
630 u32 rmii_mode
= readl(fep
->hwp
+ FEC_R_CNTRL
) & (1 << 8);
632 /* We cannot expect a graceful transmit stop without link !!! */
634 writel(1, fep
->hwp
+ FEC_X_CNTRL
); /* Graceful transmit stop */
636 if (!(readl(fep
->hwp
+ FEC_IEVENT
) & FEC_ENET_GRA
))
637 netdev_err(ndev
, "Graceful transmit stop did not complete!\n");
640 /* Whack a reset. We should wait for this. */
641 writel(1, fep
->hwp
+ FEC_ECNTRL
);
643 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
644 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
646 /* We have to keep ENET enabled to have MII interrupt stay working */
647 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) {
648 writel(2, fep
->hwp
+ FEC_ECNTRL
);
649 writel(rmii_mode
, fep
->hwp
+ FEC_R_CNTRL
);
655 fec_timeout(struct net_device
*ndev
)
657 struct fec_enet_private
*fep
= netdev_priv(ndev
);
659 ndev
->stats
.tx_errors
++;
661 fep
->delay_work
.timeout
= true;
662 schedule_delayed_work(&(fep
->delay_work
.delay_work
), 0);
665 static void fec_enet_work(struct work_struct
*work
)
667 struct fec_enet_private
*fep
=
669 struct fec_enet_private
,
670 delay_work
.delay_work
.work
);
672 if (fep
->delay_work
.timeout
) {
673 fep
->delay_work
.timeout
= false;
674 fec_restart(fep
->netdev
, fep
->full_duplex
);
675 netif_wake_queue(fep
->netdev
);
680 fec_enet_tx(struct net_device
*ndev
)
682 struct fec_enet_private
*fep
;
684 unsigned short status
;
688 fep
= netdev_priv(ndev
);
691 /* get next bdp of dirty_tx */
692 if (bdp
->cbd_sc
& BD_ENET_TX_WRAP
)
693 bdp
= fep
->tx_bd_base
;
695 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
697 while (((status
= bdp
->cbd_sc
) & BD_ENET_TX_READY
) == 0) {
699 /* current queue is empty */
700 if (bdp
== fep
->cur_tx
)
704 index
= (struct bufdesc_ex
*)bdp
-
705 (struct bufdesc_ex
*)fep
->tx_bd_base
;
707 index
= bdp
- fep
->tx_bd_base
;
709 dma_unmap_single(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
710 FEC_ENET_TX_FRSIZE
, DMA_TO_DEVICE
);
711 bdp
->cbd_bufaddr
= 0;
713 skb
= fep
->tx_skbuff
[index
];
715 /* Check for errors. */
716 if (status
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
717 BD_ENET_TX_RL
| BD_ENET_TX_UN
|
719 ndev
->stats
.tx_errors
++;
720 if (status
& BD_ENET_TX_HB
) /* No heartbeat */
721 ndev
->stats
.tx_heartbeat_errors
++;
722 if (status
& BD_ENET_TX_LC
) /* Late collision */
723 ndev
->stats
.tx_window_errors
++;
724 if (status
& BD_ENET_TX_RL
) /* Retrans limit */
725 ndev
->stats
.tx_aborted_errors
++;
726 if (status
& BD_ENET_TX_UN
) /* Underrun */
727 ndev
->stats
.tx_fifo_errors
++;
728 if (status
& BD_ENET_TX_CSL
) /* Carrier lost */
729 ndev
->stats
.tx_carrier_errors
++;
731 ndev
->stats
.tx_packets
++;
734 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_IN_PROGRESS
) &&
736 struct skb_shared_hwtstamps shhwtstamps
;
738 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
740 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
741 spin_lock_irqsave(&fep
->tmreg_lock
, flags
);
742 shhwtstamps
.hwtstamp
= ns_to_ktime(
743 timecounter_cyc2time(&fep
->tc
, ebdp
->ts
));
744 spin_unlock_irqrestore(&fep
->tmreg_lock
, flags
);
745 skb_tstamp_tx(skb
, &shhwtstamps
);
748 if (status
& BD_ENET_TX_READY
)
749 netdev_err(ndev
, "HEY! Enet xmit interrupt and TX_READY\n");
751 /* Deferred means some collisions occurred during transmit,
752 * but we eventually sent the packet OK.
754 if (status
& BD_ENET_TX_DEF
)
755 ndev
->stats
.collisions
++;
757 /* Free the sk buffer associated with this last transmit */
758 dev_kfree_skb_any(skb
);
759 fep
->tx_skbuff
[index
] = NULL
;
763 /* Update pointer to next buffer descriptor to be transmitted */
764 if (status
& BD_ENET_TX_WRAP
)
765 bdp
= fep
->tx_bd_base
;
767 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
769 /* Since we have freed up a buffer, the ring is no longer full
771 if (fep
->dirty_tx
!= fep
->cur_tx
) {
772 if (netif_queue_stopped(ndev
))
773 netif_wake_queue(ndev
);
780 /* During a receive, the cur_rx points to the current incoming buffer.
781 * When we update through the ring, if the next incoming buffer has
782 * not been given to the system, we just set the empty indicator,
783 * effectively tossing the packet.
786 fec_enet_rx(struct net_device
*ndev
, int budget
)
788 struct fec_enet_private
*fep
= netdev_priv(ndev
);
789 const struct platform_device_id
*id_entry
=
790 platform_get_device_id(fep
->pdev
);
792 unsigned short status
;
796 int pkt_received
= 0;
802 /* First, grab all of the stats for the incoming packet.
803 * These get messed up if we get called due to a busy condition.
807 while (!((status
= bdp
->cbd_sc
) & BD_ENET_RX_EMPTY
)) {
809 if (pkt_received
>= budget
)
813 /* Since we have allocated space to hold a complete frame,
814 * the last indicator should be set.
816 if ((status
& BD_ENET_RX_LAST
) == 0)
817 netdev_err(ndev
, "rcv is not +last\n");
820 goto rx_processing_done
;
822 /* Check for errors. */
823 if (status
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_NO
|
824 BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
825 ndev
->stats
.rx_errors
++;
826 if (status
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
)) {
827 /* Frame too long or too short. */
828 ndev
->stats
.rx_length_errors
++;
830 if (status
& BD_ENET_RX_NO
) /* Frame alignment */
831 ndev
->stats
.rx_frame_errors
++;
832 if (status
& BD_ENET_RX_CR
) /* CRC Error */
833 ndev
->stats
.rx_crc_errors
++;
834 if (status
& BD_ENET_RX_OV
) /* FIFO overrun */
835 ndev
->stats
.rx_fifo_errors
++;
838 /* Report late collisions as a frame error.
839 * On this error, the BD is closed, but we don't know what we
840 * have in the buffer. So, just drop this frame on the floor.
842 if (status
& BD_ENET_RX_CL
) {
843 ndev
->stats
.rx_errors
++;
844 ndev
->stats
.rx_frame_errors
++;
845 goto rx_processing_done
;
848 /* Process the incoming frame. */
849 ndev
->stats
.rx_packets
++;
850 pkt_len
= bdp
->cbd_datlen
;
851 ndev
->stats
.rx_bytes
+= pkt_len
;
852 data
= (__u8
*)__va(bdp
->cbd_bufaddr
);
854 dma_unmap_single(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
855 FEC_ENET_TX_FRSIZE
, DMA_FROM_DEVICE
);
857 if (id_entry
->driver_data
& FEC_QUIRK_SWAP_FRAME
)
858 swap_buffer(data
, pkt_len
);
860 /* This does 16 byte alignment, exactly what we need.
861 * The packet length includes FCS, but we don't want to
862 * include that when passing upstream as it messes up
863 * bridging applications.
865 skb
= netdev_alloc_skb(ndev
, pkt_len
- 4 + NET_IP_ALIGN
);
867 if (unlikely(!skb
)) {
868 ndev
->stats
.rx_dropped
++;
870 skb_reserve(skb
, NET_IP_ALIGN
);
871 skb_put(skb
, pkt_len
- 4); /* Make room */
872 skb_copy_to_linear_data(skb
, data
, pkt_len
- 4);
873 skb
->protocol
= eth_type_trans(skb
, ndev
);
875 /* Get receive timestamp from the skb */
876 if (fep
->hwts_rx_en
&& fep
->bufdesc_ex
) {
877 struct skb_shared_hwtstamps
*shhwtstamps
=
880 struct bufdesc_ex
*ebdp
=
881 (struct bufdesc_ex
*)bdp
;
883 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
885 spin_lock_irqsave(&fep
->tmreg_lock
, flags
);
886 shhwtstamps
->hwtstamp
= ns_to_ktime(
887 timecounter_cyc2time(&fep
->tc
, ebdp
->ts
));
888 spin_unlock_irqrestore(&fep
->tmreg_lock
, flags
);
891 if (fep
->bufdesc_ex
&&
892 (fep
->csum_flags
& FLAG_RX_CSUM_ENABLED
)) {
893 struct bufdesc_ex
*ebdp
=
894 (struct bufdesc_ex
*)bdp
;
895 if (!(ebdp
->cbd_esc
& FLAG_RX_CSUM_ERROR
)) {
897 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
899 skb_checksum_none_assert(skb
);
903 if (!skb_defer_rx_timestamp(skb
))
904 napi_gro_receive(&fep
->napi
, skb
);
907 bdp
->cbd_bufaddr
= dma_map_single(&fep
->pdev
->dev
, data
,
908 FEC_ENET_TX_FRSIZE
, DMA_FROM_DEVICE
);
910 /* Clear the status flags for this buffer */
911 status
&= ~BD_ENET_RX_STATS
;
913 /* Mark the buffer empty */
914 status
|= BD_ENET_RX_EMPTY
;
915 bdp
->cbd_sc
= status
;
917 if (fep
->bufdesc_ex
) {
918 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
920 ebdp
->cbd_esc
= BD_ENET_RX_INT
;
925 /* Update BD pointer to next entry */
926 if (status
& BD_ENET_RX_WRAP
)
927 bdp
= fep
->rx_bd_base
;
929 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
930 /* Doing this here will keep the FEC running while we process
931 * incoming frames. On a heavily loaded network, we should be
932 * able to keep up at the expense of system resources.
934 writel(0, fep
->hwp
+ FEC_R_DES_ACTIVE
);
942 fec_enet_interrupt(int irq
, void *dev_id
)
944 struct net_device
*ndev
= dev_id
;
945 struct fec_enet_private
*fep
= netdev_priv(ndev
);
947 irqreturn_t ret
= IRQ_NONE
;
950 int_events
= readl(fep
->hwp
+ FEC_IEVENT
);
951 writel(int_events
, fep
->hwp
+ FEC_IEVENT
);
953 if (int_events
& (FEC_ENET_RXF
| FEC_ENET_TXF
)) {
956 /* Disable the RX interrupt */
957 if (napi_schedule_prep(&fep
->napi
)) {
958 writel(FEC_RX_DISABLED_IMASK
,
959 fep
->hwp
+ FEC_IMASK
);
960 __napi_schedule(&fep
->napi
);
964 if (int_events
& FEC_ENET_MII
) {
966 complete(&fep
->mdio_done
);
968 } while (int_events
);
973 static int fec_enet_rx_napi(struct napi_struct
*napi
, int budget
)
975 struct net_device
*ndev
= napi
->dev
;
976 int pkts
= fec_enet_rx(ndev
, budget
);
977 struct fec_enet_private
*fep
= netdev_priv(ndev
);
983 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
988 /* ------------------------------------------------------------------------- */
989 static void fec_get_mac(struct net_device
*ndev
)
991 struct fec_enet_private
*fep
= netdev_priv(ndev
);
992 struct fec_platform_data
*pdata
= fep
->pdev
->dev
.platform_data
;
993 unsigned char *iap
, tmpaddr
[ETH_ALEN
];
996 * try to get mac address in following order:
998 * 1) module parameter via kernel command line in form
999 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1004 * 2) from device tree data
1006 if (!is_valid_ether_addr(iap
)) {
1007 struct device_node
*np
= fep
->pdev
->dev
.of_node
;
1009 const char *mac
= of_get_mac_address(np
);
1011 iap
= (unsigned char *) mac
;
1016 * 3) from flash or fuse (via platform data)
1018 if (!is_valid_ether_addr(iap
)) {
1021 iap
= (unsigned char *)FEC_FLASHMAC
;
1024 iap
= (unsigned char *)&pdata
->mac
;
1029 * 4) FEC mac registers set by bootloader
1031 if (!is_valid_ether_addr(iap
)) {
1032 *((unsigned long *) &tmpaddr
[0]) =
1033 be32_to_cpu(readl(fep
->hwp
+ FEC_ADDR_LOW
));
1034 *((unsigned short *) &tmpaddr
[4]) =
1035 be16_to_cpu(readl(fep
->hwp
+ FEC_ADDR_HIGH
) >> 16);
1039 memcpy(ndev
->dev_addr
, iap
, ETH_ALEN
);
1041 /* Adjust MAC if using macaddr */
1043 ndev
->dev_addr
[ETH_ALEN
-1] = macaddr
[ETH_ALEN
-1] + fep
->dev_id
;
1046 /* ------------------------------------------------------------------------- */
1051 static void fec_enet_adjust_link(struct net_device
*ndev
)
1053 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1054 struct phy_device
*phy_dev
= fep
->phy_dev
;
1055 int status_change
= 0;
1057 /* Prevent a state halted on mii error */
1058 if (fep
->mii_timeout
&& phy_dev
->state
== PHY_HALTED
) {
1059 phy_dev
->state
= PHY_RESUMING
;
1063 if (phy_dev
->link
) {
1065 fep
->link
= phy_dev
->link
;
1069 if (fep
->full_duplex
!= phy_dev
->duplex
)
1072 if (phy_dev
->speed
!= fep
->speed
) {
1073 fep
->speed
= phy_dev
->speed
;
1077 /* if any of the above changed restart the FEC */
1079 fec_restart(ndev
, phy_dev
->duplex
);
1083 fep
->link
= phy_dev
->link
;
1089 phy_print_status(phy_dev
);
1092 static int fec_enet_mdio_read(struct mii_bus
*bus
, int mii_id
, int regnum
)
1094 struct fec_enet_private
*fep
= bus
->priv
;
1095 unsigned long time_left
;
1097 fep
->mii_timeout
= 0;
1098 init_completion(&fep
->mdio_done
);
1100 /* start a read op */
1101 writel(FEC_MMFR_ST
| FEC_MMFR_OP_READ
|
1102 FEC_MMFR_PA(mii_id
) | FEC_MMFR_RA(regnum
) |
1103 FEC_MMFR_TA
, fep
->hwp
+ FEC_MII_DATA
);
1105 /* wait for end of transfer */
1106 time_left
= wait_for_completion_timeout(&fep
->mdio_done
,
1107 usecs_to_jiffies(FEC_MII_TIMEOUT
));
1108 if (time_left
== 0) {
1109 fep
->mii_timeout
= 1;
1110 netdev_err(fep
->netdev
, "MDIO read timeout\n");
1115 return FEC_MMFR_DATA(readl(fep
->hwp
+ FEC_MII_DATA
));
1118 static int fec_enet_mdio_write(struct mii_bus
*bus
, int mii_id
, int regnum
,
1121 struct fec_enet_private
*fep
= bus
->priv
;
1122 unsigned long time_left
;
1124 fep
->mii_timeout
= 0;
1125 init_completion(&fep
->mdio_done
);
1127 /* start a write op */
1128 writel(FEC_MMFR_ST
| FEC_MMFR_OP_WRITE
|
1129 FEC_MMFR_PA(mii_id
) | FEC_MMFR_RA(regnum
) |
1130 FEC_MMFR_TA
| FEC_MMFR_DATA(value
),
1131 fep
->hwp
+ FEC_MII_DATA
);
1133 /* wait for end of transfer */
1134 time_left
= wait_for_completion_timeout(&fep
->mdio_done
,
1135 usecs_to_jiffies(FEC_MII_TIMEOUT
));
1136 if (time_left
== 0) {
1137 fep
->mii_timeout
= 1;
1138 netdev_err(fep
->netdev
, "MDIO write timeout\n");
1145 static int fec_enet_mdio_reset(struct mii_bus
*bus
)
1150 static int fec_enet_mii_probe(struct net_device
*ndev
)
1152 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1153 const struct platform_device_id
*id_entry
=
1154 platform_get_device_id(fep
->pdev
);
1155 struct phy_device
*phy_dev
= NULL
;
1156 char mdio_bus_id
[MII_BUS_ID_SIZE
];
1157 char phy_name
[MII_BUS_ID_SIZE
+ 3];
1159 int dev_id
= fep
->dev_id
;
1161 fep
->phy_dev
= NULL
;
1163 /* check for attached phy */
1164 for (phy_id
= 0; (phy_id
< PHY_MAX_ADDR
); phy_id
++) {
1165 if ((fep
->mii_bus
->phy_mask
& (1 << phy_id
)))
1167 if (fep
->mii_bus
->phy_map
[phy_id
] == NULL
)
1169 if (fep
->mii_bus
->phy_map
[phy_id
]->phy_id
== 0)
1173 strncpy(mdio_bus_id
, fep
->mii_bus
->id
, MII_BUS_ID_SIZE
);
1177 if (phy_id
>= PHY_MAX_ADDR
) {
1178 netdev_info(ndev
, "no PHY, assuming direct connection to switch\n");
1179 strncpy(mdio_bus_id
, "fixed-0", MII_BUS_ID_SIZE
);
1183 snprintf(phy_name
, sizeof(phy_name
), PHY_ID_FMT
, mdio_bus_id
, phy_id
);
1184 phy_dev
= phy_connect(ndev
, phy_name
, &fec_enet_adjust_link
,
1185 fep
->phy_interface
);
1186 if (IS_ERR(phy_dev
)) {
1187 netdev_err(ndev
, "could not attach to PHY\n");
1188 return PTR_ERR(phy_dev
);
1191 /* mask with MAC supported features */
1192 if (id_entry
->driver_data
& FEC_QUIRK_HAS_GBIT
) {
1193 phy_dev
->supported
&= PHY_GBIT_FEATURES
;
1194 phy_dev
->supported
|= SUPPORTED_Pause
;
1197 phy_dev
->supported
&= PHY_BASIC_FEATURES
;
1199 phy_dev
->advertising
= phy_dev
->supported
;
1201 fep
->phy_dev
= phy_dev
;
1203 fep
->full_duplex
= 0;
1205 netdev_info(ndev
, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1206 fep
->phy_dev
->drv
->name
, dev_name(&fep
->phy_dev
->dev
),
1212 static int fec_enet_mii_init(struct platform_device
*pdev
)
1214 static struct mii_bus
*fec0_mii_bus
;
1215 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1216 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1217 const struct platform_device_id
*id_entry
=
1218 platform_get_device_id(fep
->pdev
);
1219 int err
= -ENXIO
, i
;
1222 * The dual fec interfaces are not equivalent with enet-mac.
1223 * Here are the differences:
1225 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1226 * - fec0 acts as the 1588 time master while fec1 is slave
1227 * - external phys can only be configured by fec0
1229 * That is to say fec1 can not work independently. It only works
1230 * when fec0 is working. The reason behind this design is that the
1231 * second interface is added primarily for Switch mode.
1233 * Because of the last point above, both phys are attached on fec0
1234 * mdio interface in board design, and need to be configured by
1237 if ((id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) && fep
->dev_id
> 0) {
1238 /* fec1 uses fec0 mii_bus */
1239 if (mii_cnt
&& fec0_mii_bus
) {
1240 fep
->mii_bus
= fec0_mii_bus
;
1247 fep
->mii_timeout
= 0;
1250 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1252 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1253 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1254 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1257 fep
->phy_speed
= DIV_ROUND_UP(clk_get_rate(fep
->clk_ahb
), 5000000);
1258 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
)
1260 fep
->phy_speed
<<= 1;
1261 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
1263 fep
->mii_bus
= mdiobus_alloc();
1264 if (fep
->mii_bus
== NULL
) {
1269 fep
->mii_bus
->name
= "fec_enet_mii_bus";
1270 fep
->mii_bus
->read
= fec_enet_mdio_read
;
1271 fep
->mii_bus
->write
= fec_enet_mdio_write
;
1272 fep
->mii_bus
->reset
= fec_enet_mdio_reset
;
1273 snprintf(fep
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
1274 pdev
->name
, fep
->dev_id
+ 1);
1275 fep
->mii_bus
->priv
= fep
;
1276 fep
->mii_bus
->parent
= &pdev
->dev
;
1278 fep
->mii_bus
->irq
= kmalloc(sizeof(int) * PHY_MAX_ADDR
, GFP_KERNEL
);
1279 if (!fep
->mii_bus
->irq
) {
1281 goto err_out_free_mdiobus
;
1284 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
1285 fep
->mii_bus
->irq
[i
] = PHY_POLL
;
1287 if (mdiobus_register(fep
->mii_bus
))
1288 goto err_out_free_mdio_irq
;
1292 /* save fec0 mii_bus */
1293 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
)
1294 fec0_mii_bus
= fep
->mii_bus
;
1298 err_out_free_mdio_irq
:
1299 kfree(fep
->mii_bus
->irq
);
1300 err_out_free_mdiobus
:
1301 mdiobus_free(fep
->mii_bus
);
1306 static void fec_enet_mii_remove(struct fec_enet_private
*fep
)
1308 if (--mii_cnt
== 0) {
1309 mdiobus_unregister(fep
->mii_bus
);
1310 kfree(fep
->mii_bus
->irq
);
1311 mdiobus_free(fep
->mii_bus
);
1315 static int fec_enet_get_settings(struct net_device
*ndev
,
1316 struct ethtool_cmd
*cmd
)
1318 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1319 struct phy_device
*phydev
= fep
->phy_dev
;
1324 return phy_ethtool_gset(phydev
, cmd
);
1327 static int fec_enet_set_settings(struct net_device
*ndev
,
1328 struct ethtool_cmd
*cmd
)
1330 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1331 struct phy_device
*phydev
= fep
->phy_dev
;
1336 return phy_ethtool_sset(phydev
, cmd
);
1339 static void fec_enet_get_drvinfo(struct net_device
*ndev
,
1340 struct ethtool_drvinfo
*info
)
1342 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1344 strlcpy(info
->driver
, fep
->pdev
->dev
.driver
->name
,
1345 sizeof(info
->driver
));
1346 strlcpy(info
->version
, "Revision: 1.0", sizeof(info
->version
));
1347 strlcpy(info
->bus_info
, dev_name(&ndev
->dev
), sizeof(info
->bus_info
));
1350 static int fec_enet_get_ts_info(struct net_device
*ndev
,
1351 struct ethtool_ts_info
*info
)
1353 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1355 if (fep
->bufdesc_ex
) {
1357 info
->so_timestamping
= SOF_TIMESTAMPING_TX_SOFTWARE
|
1358 SOF_TIMESTAMPING_RX_SOFTWARE
|
1359 SOF_TIMESTAMPING_SOFTWARE
|
1360 SOF_TIMESTAMPING_TX_HARDWARE
|
1361 SOF_TIMESTAMPING_RX_HARDWARE
|
1362 SOF_TIMESTAMPING_RAW_HARDWARE
;
1364 info
->phc_index
= ptp_clock_index(fep
->ptp_clock
);
1366 info
->phc_index
= -1;
1368 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) |
1369 (1 << HWTSTAMP_TX_ON
);
1371 info
->rx_filters
= (1 << HWTSTAMP_FILTER_NONE
) |
1372 (1 << HWTSTAMP_FILTER_ALL
);
1375 return ethtool_op_get_ts_info(ndev
, info
);
1379 static void fec_enet_get_pauseparam(struct net_device
*ndev
,
1380 struct ethtool_pauseparam
*pause
)
1382 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1384 pause
->autoneg
= (fep
->pause_flag
& FEC_PAUSE_FLAG_AUTONEG
) != 0;
1385 pause
->tx_pause
= (fep
->pause_flag
& FEC_PAUSE_FLAG_ENABLE
) != 0;
1386 pause
->rx_pause
= pause
->tx_pause
;
1389 static int fec_enet_set_pauseparam(struct net_device
*ndev
,
1390 struct ethtool_pauseparam
*pause
)
1392 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1394 if (pause
->tx_pause
!= pause
->rx_pause
) {
1396 "hardware only support enable/disable both tx and rx");
1400 fep
->pause_flag
= 0;
1402 /* tx pause must be same as rx pause */
1403 fep
->pause_flag
|= pause
->rx_pause
? FEC_PAUSE_FLAG_ENABLE
: 0;
1404 fep
->pause_flag
|= pause
->autoneg
? FEC_PAUSE_FLAG_AUTONEG
: 0;
1406 if (pause
->rx_pause
|| pause
->autoneg
) {
1407 fep
->phy_dev
->supported
|= ADVERTISED_Pause
;
1408 fep
->phy_dev
->advertising
|= ADVERTISED_Pause
;
1410 fep
->phy_dev
->supported
&= ~ADVERTISED_Pause
;
1411 fep
->phy_dev
->advertising
&= ~ADVERTISED_Pause
;
1414 if (pause
->autoneg
) {
1415 if (netif_running(ndev
))
1417 phy_start_aneg(fep
->phy_dev
);
1419 if (netif_running(ndev
))
1420 fec_restart(ndev
, 0);
1425 static const struct ethtool_ops fec_enet_ethtool_ops
= {
1426 .get_pauseparam
= fec_enet_get_pauseparam
,
1427 .set_pauseparam
= fec_enet_set_pauseparam
,
1428 .get_settings
= fec_enet_get_settings
,
1429 .set_settings
= fec_enet_set_settings
,
1430 .get_drvinfo
= fec_enet_get_drvinfo
,
1431 .get_link
= ethtool_op_get_link
,
1432 .get_ts_info
= fec_enet_get_ts_info
,
1435 static int fec_enet_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
1437 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1438 struct phy_device
*phydev
= fep
->phy_dev
;
1440 if (!netif_running(ndev
))
1446 if (cmd
== SIOCSHWTSTAMP
&& fep
->bufdesc_ex
)
1447 return fec_ptp_ioctl(ndev
, rq
, cmd
);
1449 return phy_mii_ioctl(phydev
, rq
, cmd
);
1452 static void fec_enet_free_buffers(struct net_device
*ndev
)
1454 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1456 struct sk_buff
*skb
;
1457 struct bufdesc
*bdp
;
1459 bdp
= fep
->rx_bd_base
;
1460 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1461 skb
= fep
->rx_skbuff
[i
];
1463 if (bdp
->cbd_bufaddr
)
1464 dma_unmap_single(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
1465 FEC_ENET_RX_FRSIZE
, DMA_FROM_DEVICE
);
1468 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
1471 bdp
= fep
->tx_bd_base
;
1472 for (i
= 0; i
< TX_RING_SIZE
; i
++)
1473 kfree(fep
->tx_bounce
[i
]);
1476 static int fec_enet_alloc_buffers(struct net_device
*ndev
)
1478 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1480 struct sk_buff
*skb
;
1481 struct bufdesc
*bdp
;
1483 bdp
= fep
->rx_bd_base
;
1484 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1485 skb
= netdev_alloc_skb(ndev
, FEC_ENET_RX_FRSIZE
);
1487 fec_enet_free_buffers(ndev
);
1490 fep
->rx_skbuff
[i
] = skb
;
1492 bdp
->cbd_bufaddr
= dma_map_single(&fep
->pdev
->dev
, skb
->data
,
1493 FEC_ENET_RX_FRSIZE
, DMA_FROM_DEVICE
);
1494 bdp
->cbd_sc
= BD_ENET_RX_EMPTY
;
1496 if (fep
->bufdesc_ex
) {
1497 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
1498 ebdp
->cbd_esc
= BD_ENET_RX_INT
;
1501 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
1504 /* Set the last buffer to wrap. */
1505 bdp
= fec_enet_get_prevdesc(bdp
, fep
->bufdesc_ex
);
1506 bdp
->cbd_sc
|= BD_SC_WRAP
;
1508 bdp
= fep
->tx_bd_base
;
1509 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1510 fep
->tx_bounce
[i
] = kmalloc(FEC_ENET_TX_FRSIZE
, GFP_KERNEL
);
1513 bdp
->cbd_bufaddr
= 0;
1515 if (fep
->bufdesc_ex
) {
1516 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
1517 ebdp
->cbd_esc
= BD_ENET_TX_INT
;
1520 bdp
= fec_enet_get_nextdesc(bdp
, fep
->bufdesc_ex
);
1523 /* Set the last buffer to wrap. */
1524 bdp
= fec_enet_get_prevdesc(bdp
, fep
->bufdesc_ex
);
1525 bdp
->cbd_sc
|= BD_SC_WRAP
;
1531 fec_enet_open(struct net_device
*ndev
)
1533 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1536 napi_enable(&fep
->napi
);
1538 /* I should reset the ring buffers here, but I don't yet know
1539 * a simple way to do that.
1542 ret
= fec_enet_alloc_buffers(ndev
);
1546 /* Probe and connect to PHY when open the interface */
1547 ret
= fec_enet_mii_probe(ndev
);
1549 fec_enet_free_buffers(ndev
);
1552 phy_start(fep
->phy_dev
);
1553 netif_start_queue(ndev
);
1559 fec_enet_close(struct net_device
*ndev
)
1561 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1563 /* Don't know what to do yet. */
1564 napi_disable(&fep
->napi
);
1566 netif_stop_queue(ndev
);
1570 phy_stop(fep
->phy_dev
);
1571 phy_disconnect(fep
->phy_dev
);
1574 fec_enet_free_buffers(ndev
);
1579 /* Set or clear the multicast filter for this adaptor.
1580 * Skeleton taken from sunlance driver.
1581 * The CPM Ethernet implementation allows Multicast as well as individual
1582 * MAC address filtering. Some of the drivers check to make sure it is
1583 * a group multicast address, and discard those that are not. I guess I
1584 * will do the same for now, but just remove the test if you want
1585 * individual filtering as well (do the upper net layers want or support
1586 * this kind of feature?).
1589 #define HASH_BITS 6 /* #bits in hash */
1590 #define CRC32_POLY 0xEDB88320
1592 static void set_multicast_list(struct net_device
*ndev
)
1594 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1595 struct netdev_hw_addr
*ha
;
1596 unsigned int i
, bit
, data
, crc
, tmp
;
1599 if (ndev
->flags
& IFF_PROMISC
) {
1600 tmp
= readl(fep
->hwp
+ FEC_R_CNTRL
);
1602 writel(tmp
, fep
->hwp
+ FEC_R_CNTRL
);
1606 tmp
= readl(fep
->hwp
+ FEC_R_CNTRL
);
1608 writel(tmp
, fep
->hwp
+ FEC_R_CNTRL
);
1610 if (ndev
->flags
& IFF_ALLMULTI
) {
1611 /* Catch all multicast addresses, so set the
1614 writel(0xffffffff, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
1615 writel(0xffffffff, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
1620 /* Clear filter and add the addresses in hash register
1622 writel(0, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
1623 writel(0, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
1625 netdev_for_each_mc_addr(ha
, ndev
) {
1626 /* calculate crc32 value of mac address */
1629 for (i
= 0; i
< ndev
->addr_len
; i
++) {
1631 for (bit
= 0; bit
< 8; bit
++, data
>>= 1) {
1633 (((crc
^ data
) & 1) ? CRC32_POLY
: 0);
1637 /* only upper 6 bits (HASH_BITS) are used
1638 * which point to specific bit in he hash registers
1640 hash
= (crc
>> (32 - HASH_BITS
)) & 0x3f;
1643 tmp
= readl(fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
1644 tmp
|= 1 << (hash
- 32);
1645 writel(tmp
, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
1647 tmp
= readl(fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
1649 writel(tmp
, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
1654 /* Set a MAC change in hardware. */
1656 fec_set_mac_address(struct net_device
*ndev
, void *p
)
1658 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1659 struct sockaddr
*addr
= p
;
1661 if (!is_valid_ether_addr(addr
->sa_data
))
1662 return -EADDRNOTAVAIL
;
1664 memcpy(ndev
->dev_addr
, addr
->sa_data
, ndev
->addr_len
);
1666 writel(ndev
->dev_addr
[3] | (ndev
->dev_addr
[2] << 8) |
1667 (ndev
->dev_addr
[1] << 16) | (ndev
->dev_addr
[0] << 24),
1668 fep
->hwp
+ FEC_ADDR_LOW
);
1669 writel((ndev
->dev_addr
[5] << 16) | (ndev
->dev_addr
[4] << 24),
1670 fep
->hwp
+ FEC_ADDR_HIGH
);
1674 #ifdef CONFIG_NET_POLL_CONTROLLER
1676 * fec_poll_controller - FEC Poll controller function
1677 * @dev: The FEC network adapter
1679 * Polled functionality used by netconsole and others in non interrupt mode
1682 static void fec_poll_controller(struct net_device
*dev
)
1685 struct fec_enet_private
*fep
= netdev_priv(dev
);
1687 for (i
= 0; i
< FEC_IRQ_NUM
; i
++) {
1688 if (fep
->irq
[i
] > 0) {
1689 disable_irq(fep
->irq
[i
]);
1690 fec_enet_interrupt(fep
->irq
[i
], dev
);
1691 enable_irq(fep
->irq
[i
]);
1697 static int fec_set_features(struct net_device
*netdev
,
1698 netdev_features_t features
)
1700 struct fec_enet_private
*fep
= netdev_priv(netdev
);
1701 netdev_features_t changed
= features
^ netdev
->features
;
1703 netdev
->features
= features
;
1705 /* Receive checksum has been changed */
1706 if (changed
& NETIF_F_RXCSUM
) {
1707 if (features
& NETIF_F_RXCSUM
)
1708 fep
->csum_flags
|= FLAG_RX_CSUM_ENABLED
;
1710 fep
->csum_flags
&= ~FLAG_RX_CSUM_ENABLED
;
1712 if (netif_running(netdev
)) {
1714 fec_restart(netdev
, fep
->phy_dev
->duplex
);
1715 netif_wake_queue(netdev
);
1717 fec_restart(netdev
, fep
->phy_dev
->duplex
);
1724 static const struct net_device_ops fec_netdev_ops
= {
1725 .ndo_open
= fec_enet_open
,
1726 .ndo_stop
= fec_enet_close
,
1727 .ndo_start_xmit
= fec_enet_start_xmit
,
1728 .ndo_set_rx_mode
= set_multicast_list
,
1729 .ndo_change_mtu
= eth_change_mtu
,
1730 .ndo_validate_addr
= eth_validate_addr
,
1731 .ndo_tx_timeout
= fec_timeout
,
1732 .ndo_set_mac_address
= fec_set_mac_address
,
1733 .ndo_do_ioctl
= fec_enet_ioctl
,
1734 #ifdef CONFIG_NET_POLL_CONTROLLER
1735 .ndo_poll_controller
= fec_poll_controller
,
1737 .ndo_set_features
= fec_set_features
,
1741 * XXX: We need to clean up on failure exits here.
1744 static int fec_enet_init(struct net_device
*ndev
)
1746 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1747 struct bufdesc
*cbd_base
;
1749 /* Allocate memory for buffer descriptors. */
1750 cbd_base
= dma_alloc_coherent(NULL
, PAGE_SIZE
, &fep
->bd_dma
,
1755 memset(cbd_base
, 0, PAGE_SIZE
);
1759 /* Get the Ethernet address */
1762 /* Set receive and transmit descriptor base. */
1763 fep
->rx_bd_base
= cbd_base
;
1764 if (fep
->bufdesc_ex
)
1765 fep
->tx_bd_base
= (struct bufdesc
*)
1766 (((struct bufdesc_ex
*)cbd_base
) + RX_RING_SIZE
);
1768 fep
->tx_bd_base
= cbd_base
+ RX_RING_SIZE
;
1770 /* The FEC Ethernet specific entries in the device structure */
1771 ndev
->watchdog_timeo
= TX_TIMEOUT
;
1772 ndev
->netdev_ops
= &fec_netdev_ops
;
1773 ndev
->ethtool_ops
= &fec_enet_ethtool_ops
;
1775 writel(FEC_RX_DISABLED_IMASK
, fep
->hwp
+ FEC_IMASK
);
1776 netif_napi_add(ndev
, &fep
->napi
, fec_enet_rx_napi
, FEC_NAPI_WEIGHT
);
1778 /* enable hw accelerator */
1779 ndev
->features
|= (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
1781 ndev
->hw_features
|= (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
1783 fep
->csum_flags
|= FLAG_RX_CSUM_ENABLED
;
1785 fec_restart(ndev
, 0);
1791 static void fec_reset_phy(struct platform_device
*pdev
)
1795 struct device_node
*np
= pdev
->dev
.of_node
;
1800 of_property_read_u32(np
, "phy-reset-duration", &msec
);
1801 /* A sane reset duration should not be longer than 1s */
1805 phy_reset
= of_get_named_gpio(np
, "phy-reset-gpios", 0);
1806 if (!gpio_is_valid(phy_reset
))
1809 err
= devm_gpio_request_one(&pdev
->dev
, phy_reset
,
1810 GPIOF_OUT_INIT_LOW
, "phy-reset");
1812 dev_err(&pdev
->dev
, "failed to get phy-reset-gpios: %d\n", err
);
1816 gpio_set_value(phy_reset
, 1);
1818 #else /* CONFIG_OF */
1819 static void fec_reset_phy(struct platform_device
*pdev
)
1822 * In case of platform probe, the reset has been done
1826 #endif /* CONFIG_OF */
1829 fec_probe(struct platform_device
*pdev
)
1831 struct fec_enet_private
*fep
;
1832 struct fec_platform_data
*pdata
;
1833 struct net_device
*ndev
;
1834 int i
, irq
, ret
= 0;
1836 const struct of_device_id
*of_id
;
1838 struct pinctrl
*pinctrl
;
1839 struct regulator
*reg_phy
;
1841 of_id
= of_match_device(fec_dt_ids
, &pdev
->dev
);
1843 pdev
->id_entry
= of_id
->data
;
1845 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1849 /* Init network device */
1850 ndev
= alloc_etherdev(sizeof(struct fec_enet_private
));
1854 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1856 /* setup board info structure */
1857 fep
= netdev_priv(ndev
);
1859 /* default enable pause frame auto negotiation */
1860 if (pdev
->id_entry
&&
1861 (pdev
->id_entry
->driver_data
& FEC_QUIRK_HAS_GBIT
))
1862 fep
->pause_flag
|= FEC_PAUSE_FLAG_AUTONEG
;
1864 fep
->hwp
= devm_request_and_ioremap(&pdev
->dev
, r
);
1866 fep
->dev_id
= dev_id
++;
1868 fep
->bufdesc_ex
= 0;
1872 goto failed_ioremap
;
1875 platform_set_drvdata(pdev
, ndev
);
1877 ret
= of_get_phy_mode(pdev
->dev
.of_node
);
1879 pdata
= pdev
->dev
.platform_data
;
1881 fep
->phy_interface
= pdata
->phy
;
1883 fep
->phy_interface
= PHY_INTERFACE_MODE_MII
;
1885 fep
->phy_interface
= ret
;
1888 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
1889 if (IS_ERR(pinctrl
)) {
1890 ret
= PTR_ERR(pinctrl
);
1894 fep
->clk_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
1895 if (IS_ERR(fep
->clk_ipg
)) {
1896 ret
= PTR_ERR(fep
->clk_ipg
);
1900 fep
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
1901 if (IS_ERR(fep
->clk_ahb
)) {
1902 ret
= PTR_ERR(fep
->clk_ahb
);
1906 /* enet_out is optional, depends on board */
1907 fep
->clk_enet_out
= devm_clk_get(&pdev
->dev
, "enet_out");
1908 if (IS_ERR(fep
->clk_enet_out
))
1909 fep
->clk_enet_out
= NULL
;
1911 fep
->clk_ptp
= devm_clk_get(&pdev
->dev
, "ptp");
1913 pdev
->id_entry
->driver_data
& FEC_QUIRK_HAS_BUFDESC_EX
;
1914 if (IS_ERR(fep
->clk_ptp
)) {
1915 fep
->clk_ptp
= NULL
;
1916 fep
->bufdesc_ex
= 0;
1919 clk_prepare_enable(fep
->clk_ahb
);
1920 clk_prepare_enable(fep
->clk_ipg
);
1921 clk_prepare_enable(fep
->clk_enet_out
);
1922 clk_prepare_enable(fep
->clk_ptp
);
1924 reg_phy
= devm_regulator_get(&pdev
->dev
, "phy");
1925 if (!IS_ERR(reg_phy
)) {
1926 ret
= regulator_enable(reg_phy
);
1929 "Failed to enable phy regulator: %d\n", ret
);
1930 goto failed_regulator
;
1934 fec_reset_phy(pdev
);
1936 if (fep
->bufdesc_ex
)
1937 fec_ptp_init(ndev
, pdev
);
1939 ret
= fec_enet_init(ndev
);
1943 for (i
= 0; i
< FEC_IRQ_NUM
; i
++) {
1944 irq
= platform_get_irq(pdev
, i
);
1951 ret
= request_irq(irq
, fec_enet_interrupt
, IRQF_DISABLED
, pdev
->name
, ndev
);
1954 irq
= platform_get_irq(pdev
, i
);
1955 free_irq(irq
, ndev
);
1961 ret
= fec_enet_mii_init(pdev
);
1963 goto failed_mii_init
;
1965 /* Carrier starts down, phylib will bring it up */
1966 netif_carrier_off(ndev
);
1968 ret
= register_netdev(ndev
);
1970 goto failed_register
;
1972 if (fep
->bufdesc_ex
&& fep
->ptp_clock
)
1973 netdev_info(ndev
, "registered PHC device %d\n", fep
->dev_id
);
1975 INIT_DELAYED_WORK(&(fep
->delay_work
.delay_work
), fec_enet_work
);
1979 fec_enet_mii_remove(fep
);
1982 for (i
= 0; i
< FEC_IRQ_NUM
; i
++) {
1983 irq
= platform_get_irq(pdev
, i
);
1985 free_irq(irq
, ndev
);
1989 clk_disable_unprepare(fep
->clk_ahb
);
1990 clk_disable_unprepare(fep
->clk_ipg
);
1991 clk_disable_unprepare(fep
->clk_enet_out
);
1992 clk_disable_unprepare(fep
->clk_ptp
);
2002 fec_drv_remove(struct platform_device
*pdev
)
2004 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2005 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2008 cancel_delayed_work_sync(&(fep
->delay_work
.delay_work
));
2009 unregister_netdev(ndev
);
2010 fec_enet_mii_remove(fep
);
2011 del_timer_sync(&fep
->time_keep
);
2012 clk_disable_unprepare(fep
->clk_ptp
);
2014 ptp_clock_unregister(fep
->ptp_clock
);
2015 clk_disable_unprepare(fep
->clk_enet_out
);
2016 clk_disable_unprepare(fep
->clk_ahb
);
2017 clk_disable_unprepare(fep
->clk_ipg
);
2018 for (i
= 0; i
< FEC_IRQ_NUM
; i
++) {
2019 int irq
= platform_get_irq(pdev
, i
);
2021 free_irq(irq
, ndev
);
2025 platform_set_drvdata(pdev
, NULL
);
2030 #ifdef CONFIG_PM_SLEEP
2032 fec_suspend(struct device
*dev
)
2034 struct net_device
*ndev
= dev_get_drvdata(dev
);
2035 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2037 if (netif_running(ndev
)) {
2039 netif_device_detach(ndev
);
2041 clk_disable_unprepare(fep
->clk_enet_out
);
2042 clk_disable_unprepare(fep
->clk_ahb
);
2043 clk_disable_unprepare(fep
->clk_ipg
);
2049 fec_resume(struct device
*dev
)
2051 struct net_device
*ndev
= dev_get_drvdata(dev
);
2052 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2054 clk_prepare_enable(fep
->clk_enet_out
);
2055 clk_prepare_enable(fep
->clk_ahb
);
2056 clk_prepare_enable(fep
->clk_ipg
);
2057 if (netif_running(ndev
)) {
2058 fec_restart(ndev
, fep
->full_duplex
);
2059 netif_device_attach(ndev
);
2064 #endif /* CONFIG_PM_SLEEP */
2066 static SIMPLE_DEV_PM_OPS(fec_pm_ops
, fec_suspend
, fec_resume
);
2068 static struct platform_driver fec_driver
= {
2070 .name
= DRIVER_NAME
,
2071 .owner
= THIS_MODULE
,
2073 .of_match_table
= fec_dt_ids
,
2075 .id_table
= fec_devtype
,
2077 .remove
= fec_drv_remove
,
2080 module_platform_driver(fec_driver
);
2082 MODULE_LICENSE("GPL");