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/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
39 #include <linux/tcp.h>
40 #include <linux/udp.h>
41 #include <linux/icmp.h>
42 #include <linux/spinlock.h>
43 #include <linux/workqueue.h>
44 #include <linux/bitops.h>
46 #include <linux/irq.h>
47 #include <linux/clk.h>
48 #include <linux/platform_device.h>
49 #include <linux/phy.h>
50 #include <linux/fec.h>
52 #include <linux/of_device.h>
53 #include <linux/of_gpio.h>
54 #include <linux/of_net.h>
55 #include <linux/regulator/consumer.h>
56 #include <linux/if_vlan.h>
58 #include <asm/cacheflush.h>
62 static void set_multicast_list(struct net_device
*ndev
);
64 #if defined(CONFIG_ARM)
65 #define FEC_ALIGNMENT 0xf
67 #define FEC_ALIGNMENT 0x3
70 #define DRIVER_NAME "fec"
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)
90 /* Controller has hardware checksum support */
91 #define FEC_QUIRK_HAS_CSUM (1 << 5)
92 /* Controller has hardware vlan support */
93 #define FEC_QUIRK_HAS_VLAN (1 << 6)
94 /* ENET IP errata ERR006358
96 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
97 * detected as not set during a prior frame transmission, then the
98 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
99 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
100 * frames not being transmitted until there is a 0-to-1 transition on
103 #define FEC_QUIRK_ERR006358 (1 << 7)
105 static struct platform_device_id fec_devtype
[] = {
107 /* keep it for coldfire */
112 .driver_data
= FEC_QUIRK_USE_GASKET
,
118 .driver_data
= FEC_QUIRK_ENET_MAC
| FEC_QUIRK_SWAP_FRAME
,
121 .driver_data
= FEC_QUIRK_ENET_MAC
| FEC_QUIRK_HAS_GBIT
|
122 FEC_QUIRK_HAS_BUFDESC_EX
| FEC_QUIRK_HAS_CSUM
|
123 FEC_QUIRK_HAS_VLAN
| FEC_QUIRK_ERR006358
,
125 .name
= "mvf600-fec",
126 .driver_data
= FEC_QUIRK_ENET_MAC
,
131 MODULE_DEVICE_TABLE(platform
, fec_devtype
);
134 IMX25_FEC
= 1, /* runs on i.mx25/50/53 */
135 IMX27_FEC
, /* runs on i.mx27/35/51 */
141 static const struct of_device_id fec_dt_ids
[] = {
142 { .compatible
= "fsl,imx25-fec", .data
= &fec_devtype
[IMX25_FEC
], },
143 { .compatible
= "fsl,imx27-fec", .data
= &fec_devtype
[IMX27_FEC
], },
144 { .compatible
= "fsl,imx28-fec", .data
= &fec_devtype
[IMX28_FEC
], },
145 { .compatible
= "fsl,imx6q-fec", .data
= &fec_devtype
[IMX6Q_FEC
], },
146 { .compatible
= "fsl,mvf600-fec", .data
= &fec_devtype
[MVF600_FEC
], },
149 MODULE_DEVICE_TABLE(of
, fec_dt_ids
);
151 static unsigned char macaddr
[ETH_ALEN
];
152 module_param_array(macaddr
, byte
, NULL
, 0);
153 MODULE_PARM_DESC(macaddr
, "FEC Ethernet MAC address");
155 #if defined(CONFIG_M5272)
157 * Some hardware gets it MAC address out of local flash memory.
158 * if this is non-zero then assume it is the address to get MAC from.
160 #if defined(CONFIG_NETtel)
161 #define FEC_FLASHMAC 0xf0006006
162 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
163 #define FEC_FLASHMAC 0xf0006000
164 #elif defined(CONFIG_CANCam)
165 #define FEC_FLASHMAC 0xf0020000
166 #elif defined (CONFIG_M5272C3)
167 #define FEC_FLASHMAC (0xffe04000 + 4)
168 #elif defined(CONFIG_MOD5272)
169 #define FEC_FLASHMAC 0xffc0406b
171 #define FEC_FLASHMAC 0
173 #endif /* CONFIG_M5272 */
175 #if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
176 #error "FEC: descriptor ring size constants too large"
179 /* Interrupt events/masks. */
180 #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
181 #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
182 #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
183 #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
184 #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
185 #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
186 #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
187 #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
188 #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
189 #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
191 #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
192 #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
194 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
196 #define PKT_MAXBUF_SIZE 1522
197 #define PKT_MINBUF_SIZE 64
198 #define PKT_MAXBLR_SIZE 1536
200 /* FEC receive acceleration */
201 #define FEC_RACC_IPDIS (1 << 1)
202 #define FEC_RACC_PRODIS (1 << 2)
203 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
206 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
207 * size bits. Other FEC hardware does not, so we need to take that into
208 * account when setting it.
210 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
211 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
212 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
214 #define OPT_FRAME_SIZE 0
217 /* FEC MII MMFR bits definition */
218 #define FEC_MMFR_ST (1 << 30)
219 #define FEC_MMFR_OP_READ (2 << 28)
220 #define FEC_MMFR_OP_WRITE (1 << 28)
221 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
222 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
223 #define FEC_MMFR_TA (2 << 16)
224 #define FEC_MMFR_DATA(v) (v & 0xffff)
226 #define FEC_MII_TIMEOUT 30000 /* us */
228 /* Transmitter timeout */
229 #define TX_TIMEOUT (2 * HZ)
231 #define FEC_PAUSE_FLAG_AUTONEG 0x1
232 #define FEC_PAUSE_FLAG_ENABLE 0x2
237 struct bufdesc
*fec_enet_get_nextdesc(struct bufdesc
*bdp
, struct fec_enet_private
*fep
)
239 struct bufdesc
*new_bd
= bdp
+ 1;
240 struct bufdesc_ex
*ex_new_bd
= (struct bufdesc_ex
*)bdp
+ 1;
241 struct bufdesc_ex
*ex_base
;
242 struct bufdesc
*base
;
245 if (bdp
>= fep
->tx_bd_base
) {
246 base
= fep
->tx_bd_base
;
247 ring_size
= fep
->tx_ring_size
;
248 ex_base
= (struct bufdesc_ex
*)fep
->tx_bd_base
;
250 base
= fep
->rx_bd_base
;
251 ring_size
= fep
->rx_ring_size
;
252 ex_base
= (struct bufdesc_ex
*)fep
->rx_bd_base
;
256 return (struct bufdesc
*)((ex_new_bd
>= (ex_base
+ ring_size
)) ?
257 ex_base
: ex_new_bd
);
259 return (new_bd
>= (base
+ ring_size
)) ?
264 struct bufdesc
*fec_enet_get_prevdesc(struct bufdesc
*bdp
, struct fec_enet_private
*fep
)
266 struct bufdesc
*new_bd
= bdp
- 1;
267 struct bufdesc_ex
*ex_new_bd
= (struct bufdesc_ex
*)bdp
- 1;
268 struct bufdesc_ex
*ex_base
;
269 struct bufdesc
*base
;
272 if (bdp
>= fep
->tx_bd_base
) {
273 base
= fep
->tx_bd_base
;
274 ring_size
= fep
->tx_ring_size
;
275 ex_base
= (struct bufdesc_ex
*)fep
->tx_bd_base
;
277 base
= fep
->rx_bd_base
;
278 ring_size
= fep
->rx_ring_size
;
279 ex_base
= (struct bufdesc_ex
*)fep
->rx_bd_base
;
283 return (struct bufdesc
*)((ex_new_bd
< ex_base
) ?
284 (ex_new_bd
+ ring_size
) : ex_new_bd
);
286 return (new_bd
< base
) ? (new_bd
+ ring_size
) : new_bd
;
289 static void *swap_buffer(void *bufaddr
, int len
)
292 unsigned int *buf
= bufaddr
;
294 for (i
= 0; i
< DIV_ROUND_UP(len
, 4); i
++, buf
++)
295 *buf
= cpu_to_be32(*buf
);
301 fec_enet_clear_csum(struct sk_buff
*skb
, struct net_device
*ndev
)
303 /* Only run for packets requiring a checksum. */
304 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
307 if (unlikely(skb_cow_head(skb
, 0)))
310 *(__sum16
*)(skb
->head
+ skb
->csum_start
+ skb
->csum_offset
) = 0;
316 fec_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
318 struct fec_enet_private
*fep
= netdev_priv(ndev
);
319 const struct platform_device_id
*id_entry
=
320 platform_get_device_id(fep
->pdev
);
321 struct bufdesc
*bdp
, *bdp_pre
;
323 unsigned short status
;
326 /* Fill in a Tx ring entry */
329 status
= bdp
->cbd_sc
;
331 if (status
& BD_ENET_TX_READY
) {
332 /* Ooops. All transmit buffers are full. Bail out.
333 * This should not happen, since ndev->tbusy should be set.
335 netdev_err(ndev
, "tx queue full!\n");
336 return NETDEV_TX_BUSY
;
339 /* Protocol checksum off-load for TCP and UDP. */
340 if (fec_enet_clear_csum(skb
, ndev
)) {
345 /* Clear all of the status flags */
346 status
&= ~BD_ENET_TX_STATS
;
348 /* Set buffer length and buffer pointer */
350 bdp
->cbd_datlen
= skb
->len
;
353 * On some FEC implementations data must be aligned on
354 * 4-byte boundaries. Use bounce buffers to copy data
355 * and get it aligned. Ugh.
358 index
= (struct bufdesc_ex
*)bdp
-
359 (struct bufdesc_ex
*)fep
->tx_bd_base
;
361 index
= bdp
- fep
->tx_bd_base
;
363 if (((unsigned long) bufaddr
) & FEC_ALIGNMENT
) {
364 memcpy(fep
->tx_bounce
[index
], skb
->data
, skb
->len
);
365 bufaddr
= fep
->tx_bounce
[index
];
369 * Some design made an incorrect assumption on endian mode of
370 * the system that it's running on. As the result, driver has to
371 * swap every frame going to and coming from the controller.
373 if (id_entry
->driver_data
& FEC_QUIRK_SWAP_FRAME
)
374 swap_buffer(bufaddr
, skb
->len
);
376 /* Save skb pointer */
377 fep
->tx_skbuff
[index
] = skb
;
379 /* Push the data cache so the CPM does not get stale memory
382 bdp
->cbd_bufaddr
= dma_map_single(&fep
->pdev
->dev
, bufaddr
,
383 skb
->len
, DMA_TO_DEVICE
);
384 if (dma_mapping_error(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
)) {
385 bdp
->cbd_bufaddr
= 0;
386 fep
->tx_skbuff
[index
] = NULL
;
387 dev_kfree_skb_any(skb
);
389 netdev_err(ndev
, "Tx DMA memory map failed\n");
392 /* Send it on its way. Tell FEC it's ready, interrupt when done,
393 * it's the last BD of the frame, and to put the CRC on the end.
395 status
|= (BD_ENET_TX_READY
| BD_ENET_TX_INTR
396 | BD_ENET_TX_LAST
| BD_ENET_TX_TC
);
397 bdp
->cbd_sc
= status
;
399 if (fep
->bufdesc_ex
) {
401 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
403 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
&&
405 ebdp
->cbd_esc
= (BD_ENET_TX_TS
| BD_ENET_TX_INT
);
406 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
408 ebdp
->cbd_esc
= BD_ENET_TX_INT
;
410 /* Enable protocol checksum flags
411 * We do not bother with the IP Checksum bits as they
412 * are done by the kernel
414 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
415 ebdp
->cbd_esc
|= BD_ENET_TX_PINS
;
419 bdp_pre
= fec_enet_get_prevdesc(bdp
, fep
);
420 if ((id_entry
->driver_data
& FEC_QUIRK_ERR006358
) &&
421 !(bdp_pre
->cbd_sc
& BD_ENET_TX_READY
)) {
422 fep
->delay_work
.trig_tx
= true;
423 schedule_delayed_work(&(fep
->delay_work
.delay_work
),
424 msecs_to_jiffies(1));
427 /* If this was the last BD in the ring, start at the beginning again. */
428 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
430 skb_tx_timestamp(skb
);
434 if (fep
->cur_tx
== fep
->dirty_tx
)
435 netif_stop_queue(ndev
);
437 /* Trigger transmission start */
438 writel(0, fep
->hwp
+ FEC_X_DES_ACTIVE
);
443 /* Init RX & TX buffer descriptors
445 static void fec_enet_bd_init(struct net_device
*dev
)
447 struct fec_enet_private
*fep
= netdev_priv(dev
);
451 /* Initialize the receive buffer descriptors. */
452 bdp
= fep
->rx_bd_base
;
453 for (i
= 0; i
< fep
->rx_ring_size
; i
++) {
455 /* Initialize the BD for every fragment in the page. */
456 if (bdp
->cbd_bufaddr
)
457 bdp
->cbd_sc
= BD_ENET_RX_EMPTY
;
460 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
463 /* Set the last buffer to wrap */
464 bdp
= fec_enet_get_prevdesc(bdp
, fep
);
465 bdp
->cbd_sc
|= BD_SC_WRAP
;
467 fep
->cur_rx
= fep
->rx_bd_base
;
469 /* ...and the same for transmit */
470 bdp
= fep
->tx_bd_base
;
472 for (i
= 0; i
< fep
->tx_ring_size
; i
++) {
474 /* Initialize the BD for every fragment in the page. */
476 if (bdp
->cbd_bufaddr
&& fep
->tx_skbuff
[i
]) {
477 dev_kfree_skb_any(fep
->tx_skbuff
[i
]);
478 fep
->tx_skbuff
[i
] = NULL
;
480 bdp
->cbd_bufaddr
= 0;
481 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
484 /* Set the last buffer to wrap */
485 bdp
= fec_enet_get_prevdesc(bdp
, fep
);
486 bdp
->cbd_sc
|= BD_SC_WRAP
;
490 /* This function is called to start or restart the FEC during a link
491 * change. This only happens when switching between half and full
495 fec_restart(struct net_device
*ndev
, int duplex
)
497 struct fec_enet_private
*fep
= netdev_priv(ndev
);
498 const struct platform_device_id
*id_entry
=
499 platform_get_device_id(fep
->pdev
);
503 u32 rcntl
= OPT_FRAME_SIZE
| 0x04;
504 u32 ecntl
= 0x2; /* ETHEREN */
506 if (netif_running(ndev
)) {
507 netif_device_detach(ndev
);
508 napi_disable(&fep
->napi
);
509 netif_stop_queue(ndev
);
510 netif_tx_lock_bh(ndev
);
513 /* Whack a reset. We should wait for this. */
514 writel(1, fep
->hwp
+ FEC_ECNTRL
);
518 * enet-mac reset will reset mac address registers too,
519 * so need to reconfigure it.
521 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) {
522 memcpy(&temp_mac
, ndev
->dev_addr
, ETH_ALEN
);
523 writel(cpu_to_be32(temp_mac
[0]), fep
->hwp
+ FEC_ADDR_LOW
);
524 writel(cpu_to_be32(temp_mac
[1]), fep
->hwp
+ FEC_ADDR_HIGH
);
527 /* Clear any outstanding interrupt. */
528 writel(0xffc00000, fep
->hwp
+ FEC_IEVENT
);
530 /* Setup multicast filter. */
531 set_multicast_list(ndev
);
533 writel(0, fep
->hwp
+ FEC_HASH_TABLE_HIGH
);
534 writel(0, fep
->hwp
+ FEC_HASH_TABLE_LOW
);
537 /* Set maximum receive buffer size. */
538 writel(PKT_MAXBLR_SIZE
, fep
->hwp
+ FEC_R_BUFF_SIZE
);
540 fec_enet_bd_init(ndev
);
542 /* Set receive and transmit descriptor base. */
543 writel(fep
->bd_dma
, fep
->hwp
+ FEC_R_DES_START
);
545 writel((unsigned long)fep
->bd_dma
+ sizeof(struct bufdesc_ex
)
546 * fep
->rx_ring_size
, fep
->hwp
+ FEC_X_DES_START
);
548 writel((unsigned long)fep
->bd_dma
+ sizeof(struct bufdesc
)
549 * fep
->rx_ring_size
, fep
->hwp
+ FEC_X_DES_START
);
552 for (i
= 0; i
<= TX_RING_MOD_MASK
; i
++) {
553 if (fep
->tx_skbuff
[i
]) {
554 dev_kfree_skb_any(fep
->tx_skbuff
[i
]);
555 fep
->tx_skbuff
[i
] = NULL
;
559 /* Enable MII mode */
562 writel(0x04, fep
->hwp
+ FEC_X_CNTRL
);
566 writel(0x0, fep
->hwp
+ FEC_X_CNTRL
);
569 fep
->full_duplex
= duplex
;
572 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
574 #if !defined(CONFIG_M5272)
575 /* set RX checksum */
576 val
= readl(fep
->hwp
+ FEC_RACC
);
577 if (fep
->csum_flags
& FLAG_RX_CSUM_ENABLED
)
578 val
|= FEC_RACC_OPTIONS
;
580 val
&= ~FEC_RACC_OPTIONS
;
581 writel(val
, fep
->hwp
+ FEC_RACC
);
585 * The phy interface and speed need to get configured
586 * differently on enet-mac.
588 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) {
589 /* Enable flow control and length check */
590 rcntl
|= 0x40000000 | 0x00000020;
592 /* RGMII, RMII or MII */
593 if (fep
->phy_interface
== PHY_INTERFACE_MODE_RGMII
)
595 else if (fep
->phy_interface
== PHY_INTERFACE_MODE_RMII
)
600 /* 1G, 100M or 10M */
602 if (fep
->phy_dev
->speed
== SPEED_1000
)
604 else if (fep
->phy_dev
->speed
== SPEED_100
)
610 #ifdef FEC_MIIGSK_ENR
611 if (id_entry
->driver_data
& FEC_QUIRK_USE_GASKET
) {
613 /* disable the gasket and wait */
614 writel(0, fep
->hwp
+ FEC_MIIGSK_ENR
);
615 while (readl(fep
->hwp
+ FEC_MIIGSK_ENR
) & 4)
619 * configure the gasket:
620 * RMII, 50 MHz, no loopback, no echo
621 * MII, 25 MHz, no loopback, no echo
623 cfgr
= (fep
->phy_interface
== PHY_INTERFACE_MODE_RMII
)
624 ? BM_MIIGSK_CFGR_RMII
: BM_MIIGSK_CFGR_MII
;
625 if (fep
->phy_dev
&& fep
->phy_dev
->speed
== SPEED_10
)
626 cfgr
|= BM_MIIGSK_CFGR_FRCONT_10M
;
627 writel(cfgr
, fep
->hwp
+ FEC_MIIGSK_CFGR
);
629 /* re-enable the gasket */
630 writel(2, fep
->hwp
+ FEC_MIIGSK_ENR
);
635 #if !defined(CONFIG_M5272)
636 /* enable pause frame*/
637 if ((fep
->pause_flag
& FEC_PAUSE_FLAG_ENABLE
) ||
638 ((fep
->pause_flag
& FEC_PAUSE_FLAG_AUTONEG
) &&
639 fep
->phy_dev
&& fep
->phy_dev
->pause
)) {
640 rcntl
|= FEC_ENET_FCE
;
642 /* set FIFO threshold parameter to reduce overrun */
643 writel(FEC_ENET_RSEM_V
, fep
->hwp
+ FEC_R_FIFO_RSEM
);
644 writel(FEC_ENET_RSFL_V
, fep
->hwp
+ FEC_R_FIFO_RSFL
);
645 writel(FEC_ENET_RAEM_V
, fep
->hwp
+ FEC_R_FIFO_RAEM
);
646 writel(FEC_ENET_RAFL_V
, fep
->hwp
+ FEC_R_FIFO_RAFL
);
649 writel(FEC_ENET_OPD_V
, fep
->hwp
+ FEC_OPD
);
651 rcntl
&= ~FEC_ENET_FCE
;
653 #endif /* !defined(CONFIG_M5272) */
655 writel(rcntl
, fep
->hwp
+ FEC_R_CNTRL
);
657 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) {
658 /* enable ENET endian swap */
660 /* enable ENET store and forward mode */
661 writel(1 << 8, fep
->hwp
+ FEC_X_WMRK
);
668 /* Enable the MIB statistic event counters */
669 writel(0 << 31, fep
->hwp
+ FEC_MIB_CTRLSTAT
);
672 /* And last, enable the transmit and receive processing */
673 writel(ecntl
, fep
->hwp
+ FEC_ECNTRL
);
674 writel(0, fep
->hwp
+ FEC_R_DES_ACTIVE
);
677 fec_ptp_start_cyclecounter(ndev
);
679 /* Enable interrupts we wish to service */
680 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
682 if (netif_running(ndev
)) {
683 netif_tx_unlock_bh(ndev
);
684 netif_wake_queue(ndev
);
685 napi_enable(&fep
->napi
);
686 netif_device_attach(ndev
);
691 fec_stop(struct net_device
*ndev
)
693 struct fec_enet_private
*fep
= netdev_priv(ndev
);
694 const struct platform_device_id
*id_entry
=
695 platform_get_device_id(fep
->pdev
);
696 u32 rmii_mode
= readl(fep
->hwp
+ FEC_R_CNTRL
) & (1 << 8);
698 /* We cannot expect a graceful transmit stop without link !!! */
700 writel(1, fep
->hwp
+ FEC_X_CNTRL
); /* Graceful transmit stop */
702 if (!(readl(fep
->hwp
+ FEC_IEVENT
) & FEC_ENET_GRA
))
703 netdev_err(ndev
, "Graceful transmit stop did not complete!\n");
706 /* Whack a reset. We should wait for this. */
707 writel(1, fep
->hwp
+ FEC_ECNTRL
);
709 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
710 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
712 /* We have to keep ENET enabled to have MII interrupt stay working */
713 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) {
714 writel(2, fep
->hwp
+ FEC_ECNTRL
);
715 writel(rmii_mode
, fep
->hwp
+ FEC_R_CNTRL
);
721 fec_timeout(struct net_device
*ndev
)
723 struct fec_enet_private
*fep
= netdev_priv(ndev
);
725 ndev
->stats
.tx_errors
++;
727 fep
->delay_work
.timeout
= true;
728 schedule_delayed_work(&(fep
->delay_work
.delay_work
), 0);
731 static void fec_enet_work(struct work_struct
*work
)
733 struct fec_enet_private
*fep
=
735 struct fec_enet_private
,
736 delay_work
.delay_work
.work
);
738 if (fep
->delay_work
.timeout
) {
739 fep
->delay_work
.timeout
= false;
740 fec_restart(fep
->netdev
, fep
->full_duplex
);
741 netif_wake_queue(fep
->netdev
);
744 if (fep
->delay_work
.trig_tx
) {
745 fep
->delay_work
.trig_tx
= false;
746 writel(0, fep
->hwp
+ FEC_X_DES_ACTIVE
);
751 fec_enet_tx(struct net_device
*ndev
)
753 struct fec_enet_private
*fep
;
755 unsigned short status
;
759 fep
= netdev_priv(ndev
);
762 /* get next bdp of dirty_tx */
763 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
765 while (((status
= bdp
->cbd_sc
) & BD_ENET_TX_READY
) == 0) {
767 /* current queue is empty */
768 if (bdp
== fep
->cur_tx
)
772 index
= (struct bufdesc_ex
*)bdp
-
773 (struct bufdesc_ex
*)fep
->tx_bd_base
;
775 index
= bdp
- fep
->tx_bd_base
;
777 skb
= fep
->tx_skbuff
[index
];
778 dma_unmap_single(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
, skb
->len
,
780 bdp
->cbd_bufaddr
= 0;
782 /* Check for errors. */
783 if (status
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
784 BD_ENET_TX_RL
| BD_ENET_TX_UN
|
786 ndev
->stats
.tx_errors
++;
787 if (status
& BD_ENET_TX_HB
) /* No heartbeat */
788 ndev
->stats
.tx_heartbeat_errors
++;
789 if (status
& BD_ENET_TX_LC
) /* Late collision */
790 ndev
->stats
.tx_window_errors
++;
791 if (status
& BD_ENET_TX_RL
) /* Retrans limit */
792 ndev
->stats
.tx_aborted_errors
++;
793 if (status
& BD_ENET_TX_UN
) /* Underrun */
794 ndev
->stats
.tx_fifo_errors
++;
795 if (status
& BD_ENET_TX_CSL
) /* Carrier lost */
796 ndev
->stats
.tx_carrier_errors
++;
798 ndev
->stats
.tx_packets
++;
799 ndev
->stats
.tx_bytes
+= bdp
->cbd_datlen
;
802 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_IN_PROGRESS
) &&
804 struct skb_shared_hwtstamps shhwtstamps
;
806 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
808 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
809 spin_lock_irqsave(&fep
->tmreg_lock
, flags
);
810 shhwtstamps
.hwtstamp
= ns_to_ktime(
811 timecounter_cyc2time(&fep
->tc
, ebdp
->ts
));
812 spin_unlock_irqrestore(&fep
->tmreg_lock
, flags
);
813 skb_tstamp_tx(skb
, &shhwtstamps
);
816 if (status
& BD_ENET_TX_READY
)
817 netdev_err(ndev
, "HEY! Enet xmit interrupt and TX_READY\n");
819 /* Deferred means some collisions occurred during transmit,
820 * but we eventually sent the packet OK.
822 if (status
& BD_ENET_TX_DEF
)
823 ndev
->stats
.collisions
++;
825 /* Free the sk buffer associated with this last transmit */
826 dev_kfree_skb_any(skb
);
827 fep
->tx_skbuff
[index
] = NULL
;
831 /* Update pointer to next buffer descriptor to be transmitted */
832 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
834 /* Since we have freed up a buffer, the ring is no longer full
836 if (fep
->dirty_tx
!= fep
->cur_tx
) {
837 if (netif_queue_stopped(ndev
))
838 netif_wake_queue(ndev
);
845 /* During a receive, the cur_rx points to the current incoming buffer.
846 * When we update through the ring, if the next incoming buffer has
847 * not been given to the system, we just set the empty indicator,
848 * effectively tossing the packet.
851 fec_enet_rx(struct net_device
*ndev
, int budget
)
853 struct fec_enet_private
*fep
= netdev_priv(ndev
);
854 const struct platform_device_id
*id_entry
=
855 platform_get_device_id(fep
->pdev
);
857 unsigned short status
;
861 int pkt_received
= 0;
862 struct bufdesc_ex
*ebdp
= NULL
;
863 bool vlan_packet_rcvd
= false;
871 /* First, grab all of the stats for the incoming packet.
872 * These get messed up if we get called due to a busy condition.
876 while (!((status
= bdp
->cbd_sc
) & BD_ENET_RX_EMPTY
)) {
878 if (pkt_received
>= budget
)
882 /* Since we have allocated space to hold a complete frame,
883 * the last indicator should be set.
885 if ((status
& BD_ENET_RX_LAST
) == 0)
886 netdev_err(ndev
, "rcv is not +last\n");
889 goto rx_processing_done
;
891 /* Check for errors. */
892 if (status
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_NO
|
893 BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
894 ndev
->stats
.rx_errors
++;
895 if (status
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
)) {
896 /* Frame too long or too short. */
897 ndev
->stats
.rx_length_errors
++;
899 if (status
& BD_ENET_RX_NO
) /* Frame alignment */
900 ndev
->stats
.rx_frame_errors
++;
901 if (status
& BD_ENET_RX_CR
) /* CRC Error */
902 ndev
->stats
.rx_crc_errors
++;
903 if (status
& BD_ENET_RX_OV
) /* FIFO overrun */
904 ndev
->stats
.rx_fifo_errors
++;
907 /* Report late collisions as a frame error.
908 * On this error, the BD is closed, but we don't know what we
909 * have in the buffer. So, just drop this frame on the floor.
911 if (status
& BD_ENET_RX_CL
) {
912 ndev
->stats
.rx_errors
++;
913 ndev
->stats
.rx_frame_errors
++;
914 goto rx_processing_done
;
917 /* Process the incoming frame. */
918 ndev
->stats
.rx_packets
++;
919 pkt_len
= bdp
->cbd_datlen
;
920 ndev
->stats
.rx_bytes
+= pkt_len
;
923 index
= (struct bufdesc_ex
*)bdp
-
924 (struct bufdesc_ex
*)fep
->rx_bd_base
;
926 index
= bdp
- fep
->rx_bd_base
;
927 data
= fep
->rx_skbuff
[index
]->data
;
928 dma_sync_single_for_cpu(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
929 FEC_ENET_RX_FRSIZE
, DMA_FROM_DEVICE
);
931 if (id_entry
->driver_data
& FEC_QUIRK_SWAP_FRAME
)
932 swap_buffer(data
, pkt_len
);
934 /* Extract the enhanced buffer descriptor */
937 ebdp
= (struct bufdesc_ex
*)bdp
;
939 /* If this is a VLAN packet remove the VLAN Tag */
940 vlan_packet_rcvd
= false;
941 if ((ndev
->features
& NETIF_F_HW_VLAN_CTAG_RX
) &&
942 fep
->bufdesc_ex
&& (ebdp
->cbd_esc
& BD_ENET_RX_VLAN
)) {
943 /* Push and remove the vlan tag */
944 struct vlan_hdr
*vlan_header
=
945 (struct vlan_hdr
*) (data
+ ETH_HLEN
);
946 vlan_tag
= ntohs(vlan_header
->h_vlan_TCI
);
947 pkt_len
-= VLAN_HLEN
;
949 vlan_packet_rcvd
= true;
952 /* This does 16 byte alignment, exactly what we need.
953 * The packet length includes FCS, but we don't want to
954 * include that when passing upstream as it messes up
955 * bridging applications.
957 skb
= netdev_alloc_skb(ndev
, pkt_len
- 4 + NET_IP_ALIGN
);
959 if (unlikely(!skb
)) {
960 ndev
->stats
.rx_dropped
++;
962 int payload_offset
= (2 * ETH_ALEN
);
963 skb_reserve(skb
, NET_IP_ALIGN
);
964 skb_put(skb
, pkt_len
- 4); /* Make room */
966 /* Extract the frame data without the VLAN header. */
967 skb_copy_to_linear_data(skb
, data
, (2 * ETH_ALEN
));
968 if (vlan_packet_rcvd
)
969 payload_offset
= (2 * ETH_ALEN
) + VLAN_HLEN
;
970 skb_copy_to_linear_data_offset(skb
, (2 * ETH_ALEN
),
971 data
+ payload_offset
,
972 pkt_len
- 4 - (2 * ETH_ALEN
));
974 skb
->protocol
= eth_type_trans(skb
, ndev
);
976 /* Get receive timestamp from the skb */
977 if (fep
->hwts_rx_en
&& fep
->bufdesc_ex
) {
978 struct skb_shared_hwtstamps
*shhwtstamps
=
982 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
984 spin_lock_irqsave(&fep
->tmreg_lock
, flags
);
985 shhwtstamps
->hwtstamp
= ns_to_ktime(
986 timecounter_cyc2time(&fep
->tc
, ebdp
->ts
));
987 spin_unlock_irqrestore(&fep
->tmreg_lock
, flags
);
990 if (fep
->bufdesc_ex
&&
991 (fep
->csum_flags
& FLAG_RX_CSUM_ENABLED
)) {
992 if (!(ebdp
->cbd_esc
& FLAG_RX_CSUM_ERROR
)) {
994 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
996 skb_checksum_none_assert(skb
);
1000 /* Handle received VLAN packets */
1001 if (vlan_packet_rcvd
)
1002 __vlan_hwaccel_put_tag(skb
,
1006 napi_gro_receive(&fep
->napi
, skb
);
1009 dma_sync_single_for_device(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
1010 FEC_ENET_RX_FRSIZE
, DMA_FROM_DEVICE
);
1012 /* Clear the status flags for this buffer */
1013 status
&= ~BD_ENET_RX_STATS
;
1015 /* Mark the buffer empty */
1016 status
|= BD_ENET_RX_EMPTY
;
1017 bdp
->cbd_sc
= status
;
1019 if (fep
->bufdesc_ex
) {
1020 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
1022 ebdp
->cbd_esc
= BD_ENET_RX_INT
;
1027 /* Update BD pointer to next entry */
1028 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
1030 /* Doing this here will keep the FEC running while we process
1031 * incoming frames. On a heavily loaded network, we should be
1032 * able to keep up at the expense of system resources.
1034 writel(0, fep
->hwp
+ FEC_R_DES_ACTIVE
);
1038 return pkt_received
;
1042 fec_enet_interrupt(int irq
, void *dev_id
)
1044 struct net_device
*ndev
= dev_id
;
1045 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1047 irqreturn_t ret
= IRQ_NONE
;
1050 int_events
= readl(fep
->hwp
+ FEC_IEVENT
);
1051 writel(int_events
, fep
->hwp
+ FEC_IEVENT
);
1053 if (int_events
& (FEC_ENET_RXF
| FEC_ENET_TXF
)) {
1056 /* Disable the RX interrupt */
1057 if (napi_schedule_prep(&fep
->napi
)) {
1058 writel(FEC_RX_DISABLED_IMASK
,
1059 fep
->hwp
+ FEC_IMASK
);
1060 __napi_schedule(&fep
->napi
);
1064 if (int_events
& FEC_ENET_MII
) {
1066 complete(&fep
->mdio_done
);
1068 } while (int_events
);
1073 static int fec_enet_rx_napi(struct napi_struct
*napi
, int budget
)
1075 struct net_device
*ndev
= napi
->dev
;
1076 int pkts
= fec_enet_rx(ndev
, budget
);
1077 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1081 if (pkts
< budget
) {
1082 napi_complete(napi
);
1083 writel(FEC_DEFAULT_IMASK
, fep
->hwp
+ FEC_IMASK
);
1088 /* ------------------------------------------------------------------------- */
1089 static void fec_get_mac(struct net_device
*ndev
)
1091 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1092 struct fec_platform_data
*pdata
= dev_get_platdata(&fep
->pdev
->dev
);
1093 unsigned char *iap
, tmpaddr
[ETH_ALEN
];
1096 * try to get mac address in following order:
1098 * 1) module parameter via kernel command line in form
1099 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1104 * 2) from device tree data
1106 if (!is_valid_ether_addr(iap
)) {
1107 struct device_node
*np
= fep
->pdev
->dev
.of_node
;
1109 const char *mac
= of_get_mac_address(np
);
1111 iap
= (unsigned char *) mac
;
1116 * 3) from flash or fuse (via platform data)
1118 if (!is_valid_ether_addr(iap
)) {
1121 iap
= (unsigned char *)FEC_FLASHMAC
;
1124 iap
= (unsigned char *)&pdata
->mac
;
1129 * 4) FEC mac registers set by bootloader
1131 if (!is_valid_ether_addr(iap
)) {
1132 *((__be32
*) &tmpaddr
[0]) =
1133 cpu_to_be32(readl(fep
->hwp
+ FEC_ADDR_LOW
));
1134 *((__be16
*) &tmpaddr
[4]) =
1135 cpu_to_be16(readl(fep
->hwp
+ FEC_ADDR_HIGH
) >> 16);
1140 * 5) random mac address
1142 if (!is_valid_ether_addr(iap
)) {
1143 /* Report it and use a random ethernet address instead */
1144 netdev_err(ndev
, "Invalid MAC address: %pM\n", iap
);
1145 eth_hw_addr_random(ndev
);
1146 netdev_info(ndev
, "Using random MAC address: %pM\n",
1151 memcpy(ndev
->dev_addr
, iap
, ETH_ALEN
);
1153 /* Adjust MAC if using macaddr */
1155 ndev
->dev_addr
[ETH_ALEN
-1] = macaddr
[ETH_ALEN
-1] + fep
->dev_id
;
1158 /* ------------------------------------------------------------------------- */
1163 static void fec_enet_adjust_link(struct net_device
*ndev
)
1165 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1166 struct phy_device
*phy_dev
= fep
->phy_dev
;
1167 int status_change
= 0;
1169 /* Prevent a state halted on mii error */
1170 if (fep
->mii_timeout
&& phy_dev
->state
== PHY_HALTED
) {
1171 phy_dev
->state
= PHY_RESUMING
;
1175 if (phy_dev
->link
) {
1177 fep
->link
= phy_dev
->link
;
1181 if (fep
->full_duplex
!= phy_dev
->duplex
)
1184 if (phy_dev
->speed
!= fep
->speed
) {
1185 fep
->speed
= phy_dev
->speed
;
1189 /* if any of the above changed restart the FEC */
1191 fec_restart(ndev
, phy_dev
->duplex
);
1195 fep
->link
= phy_dev
->link
;
1201 phy_print_status(phy_dev
);
1204 static int fec_enet_mdio_read(struct mii_bus
*bus
, int mii_id
, int regnum
)
1206 struct fec_enet_private
*fep
= bus
->priv
;
1207 unsigned long time_left
;
1209 fep
->mii_timeout
= 0;
1210 init_completion(&fep
->mdio_done
);
1212 /* start a read op */
1213 writel(FEC_MMFR_ST
| FEC_MMFR_OP_READ
|
1214 FEC_MMFR_PA(mii_id
) | FEC_MMFR_RA(regnum
) |
1215 FEC_MMFR_TA
, fep
->hwp
+ FEC_MII_DATA
);
1217 /* wait for end of transfer */
1218 time_left
= wait_for_completion_timeout(&fep
->mdio_done
,
1219 usecs_to_jiffies(FEC_MII_TIMEOUT
));
1220 if (time_left
== 0) {
1221 fep
->mii_timeout
= 1;
1222 netdev_err(fep
->netdev
, "MDIO read timeout\n");
1227 return FEC_MMFR_DATA(readl(fep
->hwp
+ FEC_MII_DATA
));
1230 static int fec_enet_mdio_write(struct mii_bus
*bus
, int mii_id
, int regnum
,
1233 struct fec_enet_private
*fep
= bus
->priv
;
1234 unsigned long time_left
;
1236 fep
->mii_timeout
= 0;
1237 init_completion(&fep
->mdio_done
);
1239 /* start a write op */
1240 writel(FEC_MMFR_ST
| FEC_MMFR_OP_WRITE
|
1241 FEC_MMFR_PA(mii_id
) | FEC_MMFR_RA(regnum
) |
1242 FEC_MMFR_TA
| FEC_MMFR_DATA(value
),
1243 fep
->hwp
+ FEC_MII_DATA
);
1245 /* wait for end of transfer */
1246 time_left
= wait_for_completion_timeout(&fep
->mdio_done
,
1247 usecs_to_jiffies(FEC_MII_TIMEOUT
));
1248 if (time_left
== 0) {
1249 fep
->mii_timeout
= 1;
1250 netdev_err(fep
->netdev
, "MDIO write timeout\n");
1257 static int fec_enet_mdio_reset(struct mii_bus
*bus
)
1262 static int fec_enet_mii_probe(struct net_device
*ndev
)
1264 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1265 const struct platform_device_id
*id_entry
=
1266 platform_get_device_id(fep
->pdev
);
1267 struct phy_device
*phy_dev
= NULL
;
1268 char mdio_bus_id
[MII_BUS_ID_SIZE
];
1269 char phy_name
[MII_BUS_ID_SIZE
+ 3];
1271 int dev_id
= fep
->dev_id
;
1273 fep
->phy_dev
= NULL
;
1275 /* check for attached phy */
1276 for (phy_id
= 0; (phy_id
< PHY_MAX_ADDR
); phy_id
++) {
1277 if ((fep
->mii_bus
->phy_mask
& (1 << phy_id
)))
1279 if (fep
->mii_bus
->phy_map
[phy_id
] == NULL
)
1281 if (fep
->mii_bus
->phy_map
[phy_id
]->phy_id
== 0)
1285 strncpy(mdio_bus_id
, fep
->mii_bus
->id
, MII_BUS_ID_SIZE
);
1289 if (phy_id
>= PHY_MAX_ADDR
) {
1290 netdev_info(ndev
, "no PHY, assuming direct connection to switch\n");
1291 strncpy(mdio_bus_id
, "fixed-0", MII_BUS_ID_SIZE
);
1295 snprintf(phy_name
, sizeof(phy_name
), PHY_ID_FMT
, mdio_bus_id
, phy_id
);
1296 phy_dev
= phy_connect(ndev
, phy_name
, &fec_enet_adjust_link
,
1297 fep
->phy_interface
);
1298 if (IS_ERR(phy_dev
)) {
1299 netdev_err(ndev
, "could not attach to PHY\n");
1300 return PTR_ERR(phy_dev
);
1303 /* mask with MAC supported features */
1304 if (id_entry
->driver_data
& FEC_QUIRK_HAS_GBIT
) {
1305 phy_dev
->supported
&= PHY_GBIT_FEATURES
;
1306 #if !defined(CONFIG_M5272)
1307 phy_dev
->supported
|= SUPPORTED_Pause
;
1311 phy_dev
->supported
&= PHY_BASIC_FEATURES
;
1313 phy_dev
->advertising
= phy_dev
->supported
;
1315 fep
->phy_dev
= phy_dev
;
1317 fep
->full_duplex
= 0;
1319 netdev_info(ndev
, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1320 fep
->phy_dev
->drv
->name
, dev_name(&fep
->phy_dev
->dev
),
1326 static int fec_enet_mii_init(struct platform_device
*pdev
)
1328 static struct mii_bus
*fec0_mii_bus
;
1329 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1330 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1331 const struct platform_device_id
*id_entry
=
1332 platform_get_device_id(fep
->pdev
);
1333 int err
= -ENXIO
, i
;
1336 * The dual fec interfaces are not equivalent with enet-mac.
1337 * Here are the differences:
1339 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1340 * - fec0 acts as the 1588 time master while fec1 is slave
1341 * - external phys can only be configured by fec0
1343 * That is to say fec1 can not work independently. It only works
1344 * when fec0 is working. The reason behind this design is that the
1345 * second interface is added primarily for Switch mode.
1347 * Because of the last point above, both phys are attached on fec0
1348 * mdio interface in board design, and need to be configured by
1351 if ((id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
) && fep
->dev_id
> 0) {
1352 /* fec1 uses fec0 mii_bus */
1353 if (mii_cnt
&& fec0_mii_bus
) {
1354 fep
->mii_bus
= fec0_mii_bus
;
1361 fep
->mii_timeout
= 0;
1364 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1366 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1367 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1368 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1371 fep
->phy_speed
= DIV_ROUND_UP(clk_get_rate(fep
->clk_ahb
), 5000000);
1372 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
)
1374 fep
->phy_speed
<<= 1;
1375 writel(fep
->phy_speed
, fep
->hwp
+ FEC_MII_SPEED
);
1377 fep
->mii_bus
= mdiobus_alloc();
1378 if (fep
->mii_bus
== NULL
) {
1383 fep
->mii_bus
->name
= "fec_enet_mii_bus";
1384 fep
->mii_bus
->read
= fec_enet_mdio_read
;
1385 fep
->mii_bus
->write
= fec_enet_mdio_write
;
1386 fep
->mii_bus
->reset
= fec_enet_mdio_reset
;
1387 snprintf(fep
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
1388 pdev
->name
, fep
->dev_id
+ 1);
1389 fep
->mii_bus
->priv
= fep
;
1390 fep
->mii_bus
->parent
= &pdev
->dev
;
1392 fep
->mii_bus
->irq
= kmalloc(sizeof(int) * PHY_MAX_ADDR
, GFP_KERNEL
);
1393 if (!fep
->mii_bus
->irq
) {
1395 goto err_out_free_mdiobus
;
1398 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
1399 fep
->mii_bus
->irq
[i
] = PHY_POLL
;
1401 if (mdiobus_register(fep
->mii_bus
))
1402 goto err_out_free_mdio_irq
;
1406 /* save fec0 mii_bus */
1407 if (id_entry
->driver_data
& FEC_QUIRK_ENET_MAC
)
1408 fec0_mii_bus
= fep
->mii_bus
;
1412 err_out_free_mdio_irq
:
1413 kfree(fep
->mii_bus
->irq
);
1414 err_out_free_mdiobus
:
1415 mdiobus_free(fep
->mii_bus
);
1420 static void fec_enet_mii_remove(struct fec_enet_private
*fep
)
1422 if (--mii_cnt
== 0) {
1423 mdiobus_unregister(fep
->mii_bus
);
1424 kfree(fep
->mii_bus
->irq
);
1425 mdiobus_free(fep
->mii_bus
);
1429 static int fec_enet_get_settings(struct net_device
*ndev
,
1430 struct ethtool_cmd
*cmd
)
1432 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1433 struct phy_device
*phydev
= fep
->phy_dev
;
1438 return phy_ethtool_gset(phydev
, cmd
);
1441 static int fec_enet_set_settings(struct net_device
*ndev
,
1442 struct ethtool_cmd
*cmd
)
1444 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1445 struct phy_device
*phydev
= fep
->phy_dev
;
1450 return phy_ethtool_sset(phydev
, cmd
);
1453 static void fec_enet_get_drvinfo(struct net_device
*ndev
,
1454 struct ethtool_drvinfo
*info
)
1456 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1458 strlcpy(info
->driver
, fep
->pdev
->dev
.driver
->name
,
1459 sizeof(info
->driver
));
1460 strlcpy(info
->version
, "Revision: 1.0", sizeof(info
->version
));
1461 strlcpy(info
->bus_info
, dev_name(&ndev
->dev
), sizeof(info
->bus_info
));
1464 static int fec_enet_get_ts_info(struct net_device
*ndev
,
1465 struct ethtool_ts_info
*info
)
1467 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1469 if (fep
->bufdesc_ex
) {
1471 info
->so_timestamping
= SOF_TIMESTAMPING_TX_SOFTWARE
|
1472 SOF_TIMESTAMPING_RX_SOFTWARE
|
1473 SOF_TIMESTAMPING_SOFTWARE
|
1474 SOF_TIMESTAMPING_TX_HARDWARE
|
1475 SOF_TIMESTAMPING_RX_HARDWARE
|
1476 SOF_TIMESTAMPING_RAW_HARDWARE
;
1478 info
->phc_index
= ptp_clock_index(fep
->ptp_clock
);
1480 info
->phc_index
= -1;
1482 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) |
1483 (1 << HWTSTAMP_TX_ON
);
1485 info
->rx_filters
= (1 << HWTSTAMP_FILTER_NONE
) |
1486 (1 << HWTSTAMP_FILTER_ALL
);
1489 return ethtool_op_get_ts_info(ndev
, info
);
1493 #if !defined(CONFIG_M5272)
1495 static void fec_enet_get_pauseparam(struct net_device
*ndev
,
1496 struct ethtool_pauseparam
*pause
)
1498 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1500 pause
->autoneg
= (fep
->pause_flag
& FEC_PAUSE_FLAG_AUTONEG
) != 0;
1501 pause
->tx_pause
= (fep
->pause_flag
& FEC_PAUSE_FLAG_ENABLE
) != 0;
1502 pause
->rx_pause
= pause
->tx_pause
;
1505 static int fec_enet_set_pauseparam(struct net_device
*ndev
,
1506 struct ethtool_pauseparam
*pause
)
1508 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1510 if (pause
->tx_pause
!= pause
->rx_pause
) {
1512 "hardware only support enable/disable both tx and rx");
1516 fep
->pause_flag
= 0;
1518 /* tx pause must be same as rx pause */
1519 fep
->pause_flag
|= pause
->rx_pause
? FEC_PAUSE_FLAG_ENABLE
: 0;
1520 fep
->pause_flag
|= pause
->autoneg
? FEC_PAUSE_FLAG_AUTONEG
: 0;
1522 if (pause
->rx_pause
|| pause
->autoneg
) {
1523 fep
->phy_dev
->supported
|= ADVERTISED_Pause
;
1524 fep
->phy_dev
->advertising
|= ADVERTISED_Pause
;
1526 fep
->phy_dev
->supported
&= ~ADVERTISED_Pause
;
1527 fep
->phy_dev
->advertising
&= ~ADVERTISED_Pause
;
1530 if (pause
->autoneg
) {
1531 if (netif_running(ndev
))
1533 phy_start_aneg(fep
->phy_dev
);
1535 if (netif_running(ndev
))
1536 fec_restart(ndev
, 0);
1541 static const struct fec_stat
{
1542 char name
[ETH_GSTRING_LEN
];
1546 { "tx_dropped", RMON_T_DROP
},
1547 { "tx_packets", RMON_T_PACKETS
},
1548 { "tx_broadcast", RMON_T_BC_PKT
},
1549 { "tx_multicast", RMON_T_MC_PKT
},
1550 { "tx_crc_errors", RMON_T_CRC_ALIGN
},
1551 { "tx_undersize", RMON_T_UNDERSIZE
},
1552 { "tx_oversize", RMON_T_OVERSIZE
},
1553 { "tx_fragment", RMON_T_FRAG
},
1554 { "tx_jabber", RMON_T_JAB
},
1555 { "tx_collision", RMON_T_COL
},
1556 { "tx_64byte", RMON_T_P64
},
1557 { "tx_65to127byte", RMON_T_P65TO127
},
1558 { "tx_128to255byte", RMON_T_P128TO255
},
1559 { "tx_256to511byte", RMON_T_P256TO511
},
1560 { "tx_512to1023byte", RMON_T_P512TO1023
},
1561 { "tx_1024to2047byte", RMON_T_P1024TO2047
},
1562 { "tx_GTE2048byte", RMON_T_P_GTE2048
},
1563 { "tx_octets", RMON_T_OCTETS
},
1566 { "IEEE_tx_drop", IEEE_T_DROP
},
1567 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK
},
1568 { "IEEE_tx_1col", IEEE_T_1COL
},
1569 { "IEEE_tx_mcol", IEEE_T_MCOL
},
1570 { "IEEE_tx_def", IEEE_T_DEF
},
1571 { "IEEE_tx_lcol", IEEE_T_LCOL
},
1572 { "IEEE_tx_excol", IEEE_T_EXCOL
},
1573 { "IEEE_tx_macerr", IEEE_T_MACERR
},
1574 { "IEEE_tx_cserr", IEEE_T_CSERR
},
1575 { "IEEE_tx_sqe", IEEE_T_SQE
},
1576 { "IEEE_tx_fdxfc", IEEE_T_FDXFC
},
1577 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK
},
1580 { "rx_packets", RMON_R_PACKETS
},
1581 { "rx_broadcast", RMON_R_BC_PKT
},
1582 { "rx_multicast", RMON_R_MC_PKT
},
1583 { "rx_crc_errors", RMON_R_CRC_ALIGN
},
1584 { "rx_undersize", RMON_R_UNDERSIZE
},
1585 { "rx_oversize", RMON_R_OVERSIZE
},
1586 { "rx_fragment", RMON_R_FRAG
},
1587 { "rx_jabber", RMON_R_JAB
},
1588 { "rx_64byte", RMON_R_P64
},
1589 { "rx_65to127byte", RMON_R_P65TO127
},
1590 { "rx_128to255byte", RMON_R_P128TO255
},
1591 { "rx_256to511byte", RMON_R_P256TO511
},
1592 { "rx_512to1023byte", RMON_R_P512TO1023
},
1593 { "rx_1024to2047byte", RMON_R_P1024TO2047
},
1594 { "rx_GTE2048byte", RMON_R_P_GTE2048
},
1595 { "rx_octets", RMON_R_OCTETS
},
1598 { "IEEE_rx_drop", IEEE_R_DROP
},
1599 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK
},
1600 { "IEEE_rx_crc", IEEE_R_CRC
},
1601 { "IEEE_rx_align", IEEE_R_ALIGN
},
1602 { "IEEE_rx_macerr", IEEE_R_MACERR
},
1603 { "IEEE_rx_fdxfc", IEEE_R_FDXFC
},
1604 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK
},
1607 static void fec_enet_get_ethtool_stats(struct net_device
*dev
,
1608 struct ethtool_stats
*stats
, u64
*data
)
1610 struct fec_enet_private
*fep
= netdev_priv(dev
);
1613 for (i
= 0; i
< ARRAY_SIZE(fec_stats
); i
++)
1614 data
[i
] = readl(fep
->hwp
+ fec_stats
[i
].offset
);
1617 static void fec_enet_get_strings(struct net_device
*netdev
,
1618 u32 stringset
, u8
*data
)
1621 switch (stringset
) {
1623 for (i
= 0; i
< ARRAY_SIZE(fec_stats
); i
++)
1624 memcpy(data
+ i
* ETH_GSTRING_LEN
,
1625 fec_stats
[i
].name
, ETH_GSTRING_LEN
);
1630 static int fec_enet_get_sset_count(struct net_device
*dev
, int sset
)
1634 return ARRAY_SIZE(fec_stats
);
1639 #endif /* !defined(CONFIG_M5272) */
1641 static int fec_enet_nway_reset(struct net_device
*dev
)
1643 struct fec_enet_private
*fep
= netdev_priv(dev
);
1644 struct phy_device
*phydev
= fep
->phy_dev
;
1649 return genphy_restart_aneg(phydev
);
1652 static const struct ethtool_ops fec_enet_ethtool_ops
= {
1653 #if !defined(CONFIG_M5272)
1654 .get_pauseparam
= fec_enet_get_pauseparam
,
1655 .set_pauseparam
= fec_enet_set_pauseparam
,
1657 .get_settings
= fec_enet_get_settings
,
1658 .set_settings
= fec_enet_set_settings
,
1659 .get_drvinfo
= fec_enet_get_drvinfo
,
1660 .get_link
= ethtool_op_get_link
,
1661 .get_ts_info
= fec_enet_get_ts_info
,
1662 .nway_reset
= fec_enet_nway_reset
,
1663 #ifndef CONFIG_M5272
1664 .get_ethtool_stats
= fec_enet_get_ethtool_stats
,
1665 .get_strings
= fec_enet_get_strings
,
1666 .get_sset_count
= fec_enet_get_sset_count
,
1670 static int fec_enet_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
1672 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1673 struct phy_device
*phydev
= fep
->phy_dev
;
1675 if (!netif_running(ndev
))
1681 if (fep
->bufdesc_ex
) {
1682 if (cmd
== SIOCSHWTSTAMP
)
1683 return fec_ptp_set(ndev
, rq
);
1684 if (cmd
== SIOCGHWTSTAMP
)
1685 return fec_ptp_get(ndev
, rq
);
1688 return phy_mii_ioctl(phydev
, rq
, cmd
);
1691 static void fec_enet_free_buffers(struct net_device
*ndev
)
1693 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1695 struct sk_buff
*skb
;
1696 struct bufdesc
*bdp
;
1698 bdp
= fep
->rx_bd_base
;
1699 for (i
= 0; i
< fep
->rx_ring_size
; i
++) {
1700 skb
= fep
->rx_skbuff
[i
];
1702 if (bdp
->cbd_bufaddr
)
1703 dma_unmap_single(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
,
1704 FEC_ENET_RX_FRSIZE
, DMA_FROM_DEVICE
);
1707 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
1710 bdp
= fep
->tx_bd_base
;
1711 for (i
= 0; i
< fep
->tx_ring_size
; i
++)
1712 kfree(fep
->tx_bounce
[i
]);
1715 static int fec_enet_alloc_buffers(struct net_device
*ndev
)
1717 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1719 struct sk_buff
*skb
;
1720 struct bufdesc
*bdp
;
1722 bdp
= fep
->rx_bd_base
;
1723 for (i
= 0; i
< fep
->rx_ring_size
; i
++) {
1724 skb
= netdev_alloc_skb(ndev
, FEC_ENET_RX_FRSIZE
);
1726 fec_enet_free_buffers(ndev
);
1729 fep
->rx_skbuff
[i
] = skb
;
1731 bdp
->cbd_bufaddr
= dma_map_single(&fep
->pdev
->dev
, skb
->data
,
1732 FEC_ENET_RX_FRSIZE
, DMA_FROM_DEVICE
);
1733 if (dma_mapping_error(&fep
->pdev
->dev
, bdp
->cbd_bufaddr
)) {
1734 fec_enet_free_buffers(ndev
);
1735 if (net_ratelimit())
1736 netdev_err(ndev
, "Rx DMA memory map failed\n");
1739 bdp
->cbd_sc
= BD_ENET_RX_EMPTY
;
1741 if (fep
->bufdesc_ex
) {
1742 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
1743 ebdp
->cbd_esc
= BD_ENET_RX_INT
;
1746 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
1749 /* Set the last buffer to wrap. */
1750 bdp
= fec_enet_get_prevdesc(bdp
, fep
);
1751 bdp
->cbd_sc
|= BD_SC_WRAP
;
1753 bdp
= fep
->tx_bd_base
;
1754 for (i
= 0; i
< fep
->tx_ring_size
; i
++) {
1755 fep
->tx_bounce
[i
] = kmalloc(FEC_ENET_TX_FRSIZE
, GFP_KERNEL
);
1758 bdp
->cbd_bufaddr
= 0;
1760 if (fep
->bufdesc_ex
) {
1761 struct bufdesc_ex
*ebdp
= (struct bufdesc_ex
*)bdp
;
1762 ebdp
->cbd_esc
= BD_ENET_TX_INT
;
1765 bdp
= fec_enet_get_nextdesc(bdp
, fep
);
1768 /* Set the last buffer to wrap. */
1769 bdp
= fec_enet_get_prevdesc(bdp
, fep
);
1770 bdp
->cbd_sc
|= BD_SC_WRAP
;
1776 fec_enet_open(struct net_device
*ndev
)
1778 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1781 napi_enable(&fep
->napi
);
1783 /* I should reset the ring buffers here, but I don't yet know
1784 * a simple way to do that.
1787 ret
= fec_enet_alloc_buffers(ndev
);
1791 /* Probe and connect to PHY when open the interface */
1792 ret
= fec_enet_mii_probe(ndev
);
1794 fec_enet_free_buffers(ndev
);
1797 phy_start(fep
->phy_dev
);
1798 netif_start_queue(ndev
);
1804 fec_enet_close(struct net_device
*ndev
)
1806 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1808 /* Don't know what to do yet. */
1809 napi_disable(&fep
->napi
);
1811 netif_stop_queue(ndev
);
1815 phy_stop(fep
->phy_dev
);
1816 phy_disconnect(fep
->phy_dev
);
1819 fec_enet_free_buffers(ndev
);
1824 /* Set or clear the multicast filter for this adaptor.
1825 * Skeleton taken from sunlance driver.
1826 * The CPM Ethernet implementation allows Multicast as well as individual
1827 * MAC address filtering. Some of the drivers check to make sure it is
1828 * a group multicast address, and discard those that are not. I guess I
1829 * will do the same for now, but just remove the test if you want
1830 * individual filtering as well (do the upper net layers want or support
1831 * this kind of feature?).
1834 #define HASH_BITS 6 /* #bits in hash */
1835 #define CRC32_POLY 0xEDB88320
1837 static void set_multicast_list(struct net_device
*ndev
)
1839 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1840 struct netdev_hw_addr
*ha
;
1841 unsigned int i
, bit
, data
, crc
, tmp
;
1844 if (ndev
->flags
& IFF_PROMISC
) {
1845 tmp
= readl(fep
->hwp
+ FEC_R_CNTRL
);
1847 writel(tmp
, fep
->hwp
+ FEC_R_CNTRL
);
1851 tmp
= readl(fep
->hwp
+ FEC_R_CNTRL
);
1853 writel(tmp
, fep
->hwp
+ FEC_R_CNTRL
);
1855 if (ndev
->flags
& IFF_ALLMULTI
) {
1856 /* Catch all multicast addresses, so set the
1859 writel(0xffffffff, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
1860 writel(0xffffffff, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
1865 /* Clear filter and add the addresses in hash register
1867 writel(0, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
1868 writel(0, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
1870 netdev_for_each_mc_addr(ha
, ndev
) {
1871 /* calculate crc32 value of mac address */
1874 for (i
= 0; i
< ndev
->addr_len
; i
++) {
1876 for (bit
= 0; bit
< 8; bit
++, data
>>= 1) {
1878 (((crc
^ data
) & 1) ? CRC32_POLY
: 0);
1882 /* only upper 6 bits (HASH_BITS) are used
1883 * which point to specific bit in he hash registers
1885 hash
= (crc
>> (32 - HASH_BITS
)) & 0x3f;
1888 tmp
= readl(fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
1889 tmp
|= 1 << (hash
- 32);
1890 writel(tmp
, fep
->hwp
+ FEC_GRP_HASH_TABLE_HIGH
);
1892 tmp
= readl(fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
1894 writel(tmp
, fep
->hwp
+ FEC_GRP_HASH_TABLE_LOW
);
1899 /* Set a MAC change in hardware. */
1901 fec_set_mac_address(struct net_device
*ndev
, void *p
)
1903 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1904 struct sockaddr
*addr
= p
;
1906 if (!is_valid_ether_addr(addr
->sa_data
))
1907 return -EADDRNOTAVAIL
;
1909 memcpy(ndev
->dev_addr
, addr
->sa_data
, ndev
->addr_len
);
1911 writel(ndev
->dev_addr
[3] | (ndev
->dev_addr
[2] << 8) |
1912 (ndev
->dev_addr
[1] << 16) | (ndev
->dev_addr
[0] << 24),
1913 fep
->hwp
+ FEC_ADDR_LOW
);
1914 writel((ndev
->dev_addr
[5] << 16) | (ndev
->dev_addr
[4] << 24),
1915 fep
->hwp
+ FEC_ADDR_HIGH
);
1919 #ifdef CONFIG_NET_POLL_CONTROLLER
1921 * fec_poll_controller - FEC Poll controller function
1922 * @dev: The FEC network adapter
1924 * Polled functionality used by netconsole and others in non interrupt mode
1927 static void fec_poll_controller(struct net_device
*dev
)
1930 struct fec_enet_private
*fep
= netdev_priv(dev
);
1932 for (i
= 0; i
< FEC_IRQ_NUM
; i
++) {
1933 if (fep
->irq
[i
] > 0) {
1934 disable_irq(fep
->irq
[i
]);
1935 fec_enet_interrupt(fep
->irq
[i
], dev
);
1936 enable_irq(fep
->irq
[i
]);
1942 static int fec_set_features(struct net_device
*netdev
,
1943 netdev_features_t features
)
1945 struct fec_enet_private
*fep
= netdev_priv(netdev
);
1946 netdev_features_t changed
= features
^ netdev
->features
;
1948 netdev
->features
= features
;
1950 /* Receive checksum has been changed */
1951 if (changed
& NETIF_F_RXCSUM
) {
1952 if (features
& NETIF_F_RXCSUM
)
1953 fep
->csum_flags
|= FLAG_RX_CSUM_ENABLED
;
1955 fep
->csum_flags
&= ~FLAG_RX_CSUM_ENABLED
;
1957 if (netif_running(netdev
)) {
1959 fec_restart(netdev
, fep
->phy_dev
->duplex
);
1960 netif_wake_queue(netdev
);
1962 fec_restart(netdev
, fep
->phy_dev
->duplex
);
1969 static const struct net_device_ops fec_netdev_ops
= {
1970 .ndo_open
= fec_enet_open
,
1971 .ndo_stop
= fec_enet_close
,
1972 .ndo_start_xmit
= fec_enet_start_xmit
,
1973 .ndo_set_rx_mode
= set_multicast_list
,
1974 .ndo_change_mtu
= eth_change_mtu
,
1975 .ndo_validate_addr
= eth_validate_addr
,
1976 .ndo_tx_timeout
= fec_timeout
,
1977 .ndo_set_mac_address
= fec_set_mac_address
,
1978 .ndo_do_ioctl
= fec_enet_ioctl
,
1979 #ifdef CONFIG_NET_POLL_CONTROLLER
1980 .ndo_poll_controller
= fec_poll_controller
,
1982 .ndo_set_features
= fec_set_features
,
1986 * XXX: We need to clean up on failure exits here.
1989 static int fec_enet_init(struct net_device
*ndev
)
1991 struct fec_enet_private
*fep
= netdev_priv(ndev
);
1992 const struct platform_device_id
*id_entry
=
1993 platform_get_device_id(fep
->pdev
);
1994 struct bufdesc
*cbd_base
;
1996 /* Allocate memory for buffer descriptors. */
1997 cbd_base
= dma_alloc_coherent(NULL
, PAGE_SIZE
, &fep
->bd_dma
,
2002 memset(cbd_base
, 0, PAGE_SIZE
);
2006 /* Get the Ethernet address */
2009 /* init the tx & rx ring size */
2010 fep
->tx_ring_size
= TX_RING_SIZE
;
2011 fep
->rx_ring_size
= RX_RING_SIZE
;
2013 /* Set receive and transmit descriptor base. */
2014 fep
->rx_bd_base
= cbd_base
;
2015 if (fep
->bufdesc_ex
)
2016 fep
->tx_bd_base
= (struct bufdesc
*)
2017 (((struct bufdesc_ex
*)cbd_base
) + fep
->rx_ring_size
);
2019 fep
->tx_bd_base
= cbd_base
+ fep
->rx_ring_size
;
2021 /* The FEC Ethernet specific entries in the device structure */
2022 ndev
->watchdog_timeo
= TX_TIMEOUT
;
2023 ndev
->netdev_ops
= &fec_netdev_ops
;
2024 ndev
->ethtool_ops
= &fec_enet_ethtool_ops
;
2026 writel(FEC_RX_DISABLED_IMASK
, fep
->hwp
+ FEC_IMASK
);
2027 netif_napi_add(ndev
, &fep
->napi
, fec_enet_rx_napi
, NAPI_POLL_WEIGHT
);
2029 if (id_entry
->driver_data
& FEC_QUIRK_HAS_VLAN
) {
2030 /* enable hw VLAN support */
2031 ndev
->features
|= NETIF_F_HW_VLAN_CTAG_RX
;
2032 ndev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_RX
;
2035 if (id_entry
->driver_data
& FEC_QUIRK_HAS_CSUM
) {
2036 /* enable hw accelerator */
2037 ndev
->features
|= (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
2039 ndev
->hw_features
|= (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
2041 fep
->csum_flags
|= FLAG_RX_CSUM_ENABLED
;
2044 fec_restart(ndev
, 0);
2050 static void fec_reset_phy(struct platform_device
*pdev
)
2054 struct device_node
*np
= pdev
->dev
.of_node
;
2059 of_property_read_u32(np
, "phy-reset-duration", &msec
);
2060 /* A sane reset duration should not be longer than 1s */
2064 phy_reset
= of_get_named_gpio(np
, "phy-reset-gpios", 0);
2065 if (!gpio_is_valid(phy_reset
))
2068 err
= devm_gpio_request_one(&pdev
->dev
, phy_reset
,
2069 GPIOF_OUT_INIT_LOW
, "phy-reset");
2071 dev_err(&pdev
->dev
, "failed to get phy-reset-gpios: %d\n", err
);
2075 gpio_set_value(phy_reset
, 1);
2077 #else /* CONFIG_OF */
2078 static void fec_reset_phy(struct platform_device
*pdev
)
2081 * In case of platform probe, the reset has been done
2085 #endif /* CONFIG_OF */
2088 fec_probe(struct platform_device
*pdev
)
2090 struct fec_enet_private
*fep
;
2091 struct fec_platform_data
*pdata
;
2092 struct net_device
*ndev
;
2093 int i
, irq
, ret
= 0;
2095 const struct of_device_id
*of_id
;
2098 of_id
= of_match_device(fec_dt_ids
, &pdev
->dev
);
2100 pdev
->id_entry
= of_id
->data
;
2102 /* Init network device */
2103 ndev
= alloc_etherdev(sizeof(struct fec_enet_private
));
2107 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
2109 /* setup board info structure */
2110 fep
= netdev_priv(ndev
);
2112 #if !defined(CONFIG_M5272)
2113 /* default enable pause frame auto negotiation */
2114 if (pdev
->id_entry
&&
2115 (pdev
->id_entry
->driver_data
& FEC_QUIRK_HAS_GBIT
))
2116 fep
->pause_flag
|= FEC_PAUSE_FLAG_AUTONEG
;
2119 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2120 fep
->hwp
= devm_ioremap_resource(&pdev
->dev
, r
);
2121 if (IS_ERR(fep
->hwp
)) {
2122 ret
= PTR_ERR(fep
->hwp
);
2123 goto failed_ioremap
;
2127 fep
->dev_id
= dev_id
++;
2129 fep
->bufdesc_ex
= 0;
2131 platform_set_drvdata(pdev
, ndev
);
2133 ret
= of_get_phy_mode(pdev
->dev
.of_node
);
2135 pdata
= dev_get_platdata(&pdev
->dev
);
2137 fep
->phy_interface
= pdata
->phy
;
2139 fep
->phy_interface
= PHY_INTERFACE_MODE_MII
;
2141 fep
->phy_interface
= ret
;
2144 fep
->clk_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
2145 if (IS_ERR(fep
->clk_ipg
)) {
2146 ret
= PTR_ERR(fep
->clk_ipg
);
2150 fep
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
2151 if (IS_ERR(fep
->clk_ahb
)) {
2152 ret
= PTR_ERR(fep
->clk_ahb
);
2156 /* enet_out is optional, depends on board */
2157 fep
->clk_enet_out
= devm_clk_get(&pdev
->dev
, "enet_out");
2158 if (IS_ERR(fep
->clk_enet_out
))
2159 fep
->clk_enet_out
= NULL
;
2161 fep
->clk_ptp
= devm_clk_get(&pdev
->dev
, "ptp");
2163 pdev
->id_entry
->driver_data
& FEC_QUIRK_HAS_BUFDESC_EX
;
2164 if (IS_ERR(fep
->clk_ptp
)) {
2165 fep
->clk_ptp
= NULL
;
2166 fep
->bufdesc_ex
= 0;
2169 ret
= clk_prepare_enable(fep
->clk_ahb
);
2173 ret
= clk_prepare_enable(fep
->clk_ipg
);
2175 goto failed_clk_ipg
;
2177 if (fep
->clk_enet_out
) {
2178 ret
= clk_prepare_enable(fep
->clk_enet_out
);
2180 goto failed_clk_enet_out
;
2184 ret
= clk_prepare_enable(fep
->clk_ptp
);
2186 goto failed_clk_ptp
;
2189 fep
->reg_phy
= devm_regulator_get(&pdev
->dev
, "phy");
2190 if (!IS_ERR(fep
->reg_phy
)) {
2191 ret
= regulator_enable(fep
->reg_phy
);
2194 "Failed to enable phy regulator: %d\n", ret
);
2195 goto failed_regulator
;
2198 fep
->reg_phy
= NULL
;
2201 fec_reset_phy(pdev
);
2203 if (fep
->bufdesc_ex
)
2206 ret
= fec_enet_init(ndev
);
2210 for (i
= 0; i
< FEC_IRQ_NUM
; i
++) {
2211 irq
= platform_get_irq(pdev
, i
);
2218 ret
= devm_request_irq(&pdev
->dev
, irq
, fec_enet_interrupt
,
2219 0, pdev
->name
, ndev
);
2224 ret
= fec_enet_mii_init(pdev
);
2226 goto failed_mii_init
;
2228 /* Carrier starts down, phylib will bring it up */
2229 netif_carrier_off(ndev
);
2231 ret
= register_netdev(ndev
);
2233 goto failed_register
;
2235 if (fep
->bufdesc_ex
&& fep
->ptp_clock
)
2236 netdev_info(ndev
, "registered PHC device %d\n", fep
->dev_id
);
2238 INIT_DELAYED_WORK(&(fep
->delay_work
.delay_work
), fec_enet_work
);
2242 fec_enet_mii_remove(fep
);
2247 regulator_disable(fep
->reg_phy
);
2250 clk_disable_unprepare(fep
->clk_ptp
);
2252 if (fep
->clk_enet_out
)
2253 clk_disable_unprepare(fep
->clk_enet_out
);
2254 failed_clk_enet_out
:
2255 clk_disable_unprepare(fep
->clk_ipg
);
2257 clk_disable_unprepare(fep
->clk_ahb
);
2266 fec_drv_remove(struct platform_device
*pdev
)
2268 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2269 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2271 cancel_delayed_work_sync(&(fep
->delay_work
.delay_work
));
2272 unregister_netdev(ndev
);
2273 fec_enet_mii_remove(fep
);
2274 del_timer_sync(&fep
->time_keep
);
2276 regulator_disable(fep
->reg_phy
);
2278 clk_disable_unprepare(fep
->clk_ptp
);
2280 ptp_clock_unregister(fep
->ptp_clock
);
2281 if (fep
->clk_enet_out
)
2282 clk_disable_unprepare(fep
->clk_enet_out
);
2283 clk_disable_unprepare(fep
->clk_ipg
);
2284 clk_disable_unprepare(fep
->clk_ahb
);
2290 #ifdef CONFIG_PM_SLEEP
2292 fec_suspend(struct device
*dev
)
2294 struct net_device
*ndev
= dev_get_drvdata(dev
);
2295 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2297 if (netif_running(ndev
)) {
2299 netif_device_detach(ndev
);
2302 clk_disable_unprepare(fep
->clk_ptp
);
2303 if (fep
->clk_enet_out
)
2304 clk_disable_unprepare(fep
->clk_enet_out
);
2305 clk_disable_unprepare(fep
->clk_ipg
);
2306 clk_disable_unprepare(fep
->clk_ahb
);
2309 regulator_disable(fep
->reg_phy
);
2315 fec_resume(struct device
*dev
)
2317 struct net_device
*ndev
= dev_get_drvdata(dev
);
2318 struct fec_enet_private
*fep
= netdev_priv(ndev
);
2322 ret
= regulator_enable(fep
->reg_phy
);
2327 ret
= clk_prepare_enable(fep
->clk_ahb
);
2329 goto failed_clk_ahb
;
2331 ret
= clk_prepare_enable(fep
->clk_ipg
);
2333 goto failed_clk_ipg
;
2335 if (fep
->clk_enet_out
) {
2336 ret
= clk_prepare_enable(fep
->clk_enet_out
);
2338 goto failed_clk_enet_out
;
2342 ret
= clk_prepare_enable(fep
->clk_ptp
);
2344 goto failed_clk_ptp
;
2347 if (netif_running(ndev
)) {
2348 fec_restart(ndev
, fep
->full_duplex
);
2349 netif_device_attach(ndev
);
2355 if (fep
->clk_enet_out
)
2356 clk_disable_unprepare(fep
->clk_enet_out
);
2357 failed_clk_enet_out
:
2358 clk_disable_unprepare(fep
->clk_ipg
);
2360 clk_disable_unprepare(fep
->clk_ahb
);
2363 regulator_disable(fep
->reg_phy
);
2366 #endif /* CONFIG_PM_SLEEP */
2368 static SIMPLE_DEV_PM_OPS(fec_pm_ops
, fec_suspend
, fec_resume
);
2370 static struct platform_driver fec_driver
= {
2372 .name
= DRIVER_NAME
,
2373 .owner
= THIS_MODULE
,
2375 .of_match_table
= fec_dt_ids
,
2377 .id_table
= fec_devtype
,
2379 .remove
= fec_drv_remove
,
2382 module_platform_driver(fec_driver
);
2384 MODULE_ALIAS("platform:"DRIVER_NAME
);
2385 MODULE_LICENSE("GPL");