blob: 25a9f7fb30dab0fdb3a938fef2568419b579114e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4 *
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10005 * Right now, I am very wasteful with the buffers. I allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
10 * small packets.
11 *
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
14 *
Greg Ungerer562d2f82005-11-07 14:09:50 +100015 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +100017 *
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
Philippe De Muyter677177c2006-06-27 13:05:33 +100019 * Copyright (c) 2004-2006 Macq Electronique SA.
Shawn Guob5680e02011-01-05 21:13:13 +000020 *
Shawn Guo230dec62011-09-23 02:12:48 +000021 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/skbuff.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000036#include <linux/in.h>
37#include <linux/ip.h>
38#include <net/ip.h>
Nimrod Andy79f33912014-06-12 08:16:23 +080039#include <net/tso.h>
Jim Baxter4c09eed2013-04-19 08:10:49 +000040#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/spinlock.h>
44#include <linux/workqueue.h>
45#include <linux/bitops.h>
Sascha Hauer6f501b12009-01-28 23:03:05 +000046#include <linux/io.h>
47#include <linux/irq.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000048#include <linux/clk.h>
Sascha Haueread73182009-01-28 23:03:11 +000049#include <linux/platform_device.h>
Bryan Wue6b043d2010-03-31 02:10:44 +000050#include <linux/phy.h>
Baruch Siach5eb32bd2010-05-24 00:36:13 -070051#include <linux/fec.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080052#include <linux/of.h>
53#include <linux/of_device.h>
54#include <linux/of_gpio.h>
55#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000056#include <linux/regulator/consumer.h>
Jim Baxtercdffcf12013-07-02 22:52:56 +010057#include <linux/if_vlan.h>
Fabio Estevama68ab982014-06-02 15:44:30 -030058#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Greg Ungerer080853a2007-07-30 16:28:46 +100060#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Christoph Müllner772e42b2013-06-27 21:18:23 +020064static void set_multicast_list(struct net_device *ndev);
65
Uwe Kleine-König085e79e2011-01-17 09:52:18 +010066#if defined(CONFIG_ARM)
Sascha Hauer196719e2009-01-28 23:03:10 +000067#define FEC_ALIGNMENT 0xf
68#else
69#define FEC_ALIGNMENT 0x3
70#endif
71
Shawn Guob5680e02011-01-05 21:13:13 +000072#define DRIVER_NAME "fec"
73
Frank Libaa70a52013-01-16 16:55:58 +000074/* Pause frame feild and FIFO threshold */
75#define FEC_ENET_FCE (1 << 5)
76#define FEC_ENET_RSEM_V 0x84
77#define FEC_ENET_RSFL_V 16
78#define FEC_ENET_RAEM_V 0x8
79#define FEC_ENET_RAFL_V 0x8
80#define FEC_ENET_OPD_V 0xFFF0
81
Shawn Guob5680e02011-01-05 21:13:13 +000082/* Controller is ENET-MAC */
83#define FEC_QUIRK_ENET_MAC (1 << 0)
84/* Controller needs driver to swap frame */
85#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080086/* Controller uses gasket */
87#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000088/* Controller has GBIT support */
89#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000090/* Controller has extend desc buffer */
91#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000092/* Controller has hardware checksum support */
93#define FEC_QUIRK_HAS_CSUM (1 << 5)
Jim Baxtercdffcf12013-07-02 22:52:56 +010094/* Controller has hardware vlan support */
95#define FEC_QUIRK_HAS_VLAN (1 << 6)
Frank Li03191652013-07-25 14:05:53 +080096/* ENET IP errata ERR006358
97 *
98 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
99 * detected as not set during a prior frame transmission, then the
100 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
101 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
Frank Li03191652013-07-25 14:05:53 +0800102 * frames not being transmitted until there is a 0-to-1 transition on
103 * ENET_TDAR[TDAR].
104 */
105#define FEC_QUIRK_ERR006358 (1 << 7)
Shawn Guob5680e02011-01-05 21:13:13 +0000106
107static struct platform_device_id fec_devtype[] = {
108 {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800109 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +0000110 .name = DRIVER_NAME,
111 .driver_data = 0,
112 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800113 .name = "imx25-fec",
114 .driver_data = FEC_QUIRK_USE_GASKET,
115 }, {
116 .name = "imx27-fec",
117 .driver_data = 0,
118 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000119 .name = "imx28-fec",
120 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800121 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000122 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000123 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100124 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800125 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000126 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000127 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000128 .driver_data = FEC_QUIRK_ENET_MAC,
129 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800130 /* sentinel */
131 }
Shawn Guob5680e02011-01-05 21:13:13 +0000132};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800133MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000134
Shawn Guoca2cc332011-06-25 02:04:35 +0800135enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000136 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800137 IMX27_FEC, /* runs on i.mx27/35/51 */
138 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000139 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000140 MVF600_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800141};
142
143static const struct of_device_id fec_dt_ids[] = {
144 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
145 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
146 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000147 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000148 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800149 { /* sentinel */ }
150};
151MODULE_DEVICE_TABLE(of, fec_dt_ids);
152
Shawn Guo49da97d2011-01-05 21:13:11 +0000153static unsigned char macaddr[ETH_ALEN];
154module_param_array(macaddr, byte, NULL, 0);
155MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
156
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000157#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 * Some hardware gets it MAC address out of local flash memory.
160 * if this is non-zero then assume it is the address to get MAC from.
161 */
162#if defined(CONFIG_NETtel)
163#define FEC_FLASHMAC 0xf0006006
164#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
165#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#elif defined(CONFIG_CANCam)
167#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000168#elif defined (CONFIG_M5272C3)
169#define FEC_FLASHMAC (0xffe04000 + 4)
170#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000171#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#else
173#define FEC_FLASHMAC 0
174#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800175#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000176
Sascha Hauer22f6b862009-04-15 01:32:18 +0000177/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
179#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
180#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
181#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
182#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
183#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
184#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
185#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
186#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
187#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
188
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000189#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000190#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000191
Jim Baxtercdffcf12013-07-02 22:52:56 +0100192/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100194#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100196#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jim Baxter4c09eed2013-04-19 08:10:49 +0000198/* FEC receive acceleration */
199#define FEC_RACC_IPDIS (1 << 1)
200#define FEC_RACC_PRODIS (1 << 2)
201#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Matt Waddel6b265292006-06-27 13:10:56 +1000204 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * size bits. Other FEC hardware does not, so we need to take that into
206 * account when setting it.
207 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000208#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100209 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
211#else
212#define OPT_FRAME_SIZE 0
213#endif
214
Bryan Wue6b043d2010-03-31 02:10:44 +0000215/* FEC MII MMFR bits definition */
216#define FEC_MMFR_ST (1 << 30)
217#define FEC_MMFR_OP_READ (2 << 28)
218#define FEC_MMFR_OP_WRITE (1 << 28)
219#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
220#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
221#define FEC_MMFR_TA (2 << 16)
222#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500224#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Sascha Hauer22f6b862009-04-15 01:32:18 +0000226/* Transmitter timeout */
227#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Frank Libaa70a52013-01-16 16:55:58 +0000229#define FEC_PAUSE_FLAG_AUTONEG 0x1
230#define FEC_PAUSE_FLAG_ENABLE 0x2
231
Nimrod Andy79f33912014-06-12 08:16:23 +0800232#define TSO_HEADER_SIZE 128
233/* Max number of allowed TCP segments for software TSO */
234#define FEC_MAX_TSO_SEGS 100
235#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
236
237#define IS_TSO_HEADER(txq, addr) \
238 ((addr >= txq->tso_hdrs_dma) && \
239 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
240
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000241static int mii_cnt;
242
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800243static inline
244struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
Frank Liff43da82013-01-03 16:04:23 +0000245{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800246 struct bufdesc *new_bd = bdp + 1;
247 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
248 struct bufdesc_ex *ex_base;
249 struct bufdesc *base;
250 int ring_size;
251
252 if (bdp >= fep->tx_bd_base) {
253 base = fep->tx_bd_base;
254 ring_size = fep->tx_ring_size;
255 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
256 } else {
257 base = fep->rx_bd_base;
258 ring_size = fep->rx_ring_size;
259 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
260 }
261
262 if (fep->bufdesc_ex)
263 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
264 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000265 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800266 return (new_bd >= (base + ring_size)) ?
267 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000268}
269
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800270static inline
271struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
Frank Liff43da82013-01-03 16:04:23 +0000272{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800273 struct bufdesc *new_bd = bdp - 1;
274 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
275 struct bufdesc_ex *ex_base;
276 struct bufdesc *base;
277 int ring_size;
278
279 if (bdp >= fep->tx_bd_base) {
280 base = fep->tx_bd_base;
281 ring_size = fep->tx_ring_size;
282 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
283 } else {
284 base = fep->rx_bd_base;
285 ring_size = fep->rx_ring_size;
286 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
287 }
288
289 if (fep->bufdesc_ex)
290 return (struct bufdesc *)((ex_new_bd < ex_base) ?
291 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000292 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800293 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000294}
295
Nimrod Andy61a44272014-06-12 08:16:18 +0800296static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
297 struct fec_enet_private *fep)
298{
299 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
300}
301
Nimrod Andy6e909282014-06-12 08:16:22 +0800302static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep)
303{
304 int entries;
305
306 entries = ((const char *)fep->dirty_tx -
307 (const char *)fep->cur_tx) / fep->bufdesc_size - 1;
308
309 return entries > 0 ? entries : entries + fep->tx_ring_size;
310}
311
Shawn Guob5680e02011-01-05 21:13:13 +0000312static void *swap_buffer(void *bufaddr, int len)
313{
314 int i;
315 unsigned int *buf = bufaddr;
316
Fabio Estevamffed61e2013-05-21 05:44:26 +0000317 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000318 *buf = cpu_to_be32(*buf);
319
320 return bufaddr;
321}
322
Fugang Duan62a02c92014-06-18 08:33:52 +0800323static inline bool is_ipv4_pkt(struct sk_buff *skb)
324{
325 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
326}
327
Jim Baxter4c09eed2013-04-19 08:10:49 +0000328static int
329fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
330{
331 /* Only run for packets requiring a checksum. */
332 if (skb->ip_summed != CHECKSUM_PARTIAL)
333 return 0;
334
335 if (unlikely(skb_cow_head(skb, 0)))
336 return -1;
337
Fugang Duan62a02c92014-06-18 08:33:52 +0800338 if (is_ipv4_pkt(skb))
339 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000340 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
341
342 return 0;
343}
344
Nimrod Andy6e909282014-06-12 08:16:22 +0800345static void
346fec_enet_submit_work(struct bufdesc *bdp, struct fec_enet_private *fep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Shawn Guob5680e02011-01-05 21:13:13 +0000348 const struct platform_device_id *id_entry =
349 platform_get_device_id(fep->pdev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800350 struct bufdesc *bdp_pre;
Duan Fugang-B38611fb8ef782014-02-20 18:14:39 +0800351
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800352 bdp_pre = fec_enet_get_prevdesc(bdp, fep);
Frank Li03191652013-07-25 14:05:53 +0800353 if ((id_entry->driver_data & FEC_QUIRK_ERR006358) &&
354 !(bdp_pre->cbd_sc & BD_ENET_TX_READY)) {
355 fep->delay_work.trig_tx = true;
356 schedule_delayed_work(&(fep->delay_work.delay_work),
357 msecs_to_jiffies(1));
358 }
Nimrod Andy6e909282014-06-12 08:16:22 +0800359}
360
361static int
362fec_enet_txq_submit_frag_skb(struct sk_buff *skb, struct net_device *ndev)
363{
364 struct fec_enet_private *fep = netdev_priv(ndev);
365 const struct platform_device_id *id_entry =
366 platform_get_device_id(fep->pdev);
367 struct bufdesc *bdp = fep->cur_tx;
368 struct bufdesc_ex *ebdp;
369 int nr_frags = skb_shinfo(skb)->nr_frags;
370 int frag, frag_len;
371 unsigned short status;
372 unsigned int estatus = 0;
373 skb_frag_t *this_frag;
374 unsigned int index;
375 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100376 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800377 int i;
378
379 for (frag = 0; frag < nr_frags; frag++) {
380 this_frag = &skb_shinfo(skb)->frags[frag];
381 bdp = fec_enet_get_nextdesc(bdp, fep);
382 ebdp = (struct bufdesc_ex *)bdp;
383
384 status = bdp->cbd_sc;
385 status &= ~BD_ENET_TX_STATS;
386 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
387 frag_len = skb_shinfo(skb)->frags[frag].size;
388
389 /* Handle the last BD specially */
390 if (frag == nr_frags - 1) {
391 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
392 if (fep->bufdesc_ex) {
393 estatus |= BD_ENET_TX_INT;
394 if (unlikely(skb_shinfo(skb)->tx_flags &
395 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
396 estatus |= BD_ENET_TX_TS;
397 }
398 }
399
400 if (fep->bufdesc_ex) {
401 if (skb->ip_summed == CHECKSUM_PARTIAL)
402 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
403 ebdp->cbd_bdu = 0;
404 ebdp->cbd_esc = estatus;
405 }
406
407 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
408
409 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
410 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
411 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
412 memcpy(fep->tx_bounce[index], bufaddr, frag_len);
413 bufaddr = fep->tx_bounce[index];
414
415 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
416 swap_buffer(bufaddr, frag_len);
417 }
418
Russell Kingd6bf3142014-07-08 00:23:19 +0100419 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
420 DMA_TO_DEVICE);
421 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800422 dev_kfree_skb_any(skb);
423 if (net_ratelimit())
424 netdev_err(ndev, "Tx DMA memory map failed\n");
425 goto dma_mapping_error;
426 }
427
Russell Kingd6bf3142014-07-08 00:23:19 +0100428 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800429 bdp->cbd_datlen = frag_len;
430 bdp->cbd_sc = status;
431 }
432
433 fep->cur_tx = bdp;
434
435 return 0;
436
437dma_mapping_error:
438 bdp = fep->cur_tx;
439 for (i = 0; i < frag; i++) {
440 bdp = fec_enet_get_nextdesc(bdp, fep);
441 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
442 bdp->cbd_datlen, DMA_TO_DEVICE);
443 }
444 return NETDEV_TX_OK;
445}
446
447static int fec_enet_txq_submit_skb(struct sk_buff *skb, struct net_device *ndev)
448{
449 struct fec_enet_private *fep = netdev_priv(ndev);
450 const struct platform_device_id *id_entry =
451 platform_get_device_id(fep->pdev);
452 int nr_frags = skb_shinfo(skb)->nr_frags;
453 struct bufdesc *bdp, *last_bdp;
454 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100455 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800456 unsigned short status;
457 unsigned short buflen;
458 unsigned int estatus = 0;
459 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800460 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800461 int ret;
462
Nimrod Andy79f33912014-06-12 08:16:23 +0800463 entries_free = fec_enet_get_free_txdesc_num(fep);
464 if (entries_free < MAX_SKB_FRAGS + 1) {
465 dev_kfree_skb_any(skb);
466 if (net_ratelimit())
467 netdev_err(ndev, "NOT enough BD for SG!\n");
468 return NETDEV_TX_OK;
469 }
470
Nimrod Andy6e909282014-06-12 08:16:22 +0800471 /* Protocol checksum off-load for TCP and UDP. */
472 if (fec_enet_clear_csum(skb, ndev)) {
473 dev_kfree_skb_any(skb);
474 return NETDEV_TX_OK;
475 }
476
477 /* Fill in a Tx ring entry */
478 bdp = fep->cur_tx;
479 status = bdp->cbd_sc;
480 status &= ~BD_ENET_TX_STATS;
481
482 /* Set buffer length and buffer pointer */
483 bufaddr = skb->data;
484 buflen = skb_headlen(skb);
485
486 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
487 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
488 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
489 memcpy(fep->tx_bounce[index], skb->data, buflen);
490 bufaddr = fep->tx_bounce[index];
491
492 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
493 swap_buffer(bufaddr, buflen);
494 }
495
Russell Kingd6bf3142014-07-08 00:23:19 +0100496 /* Push the data cache so the CPM does not get stale memory data. */
497 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
498 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800499 dev_kfree_skb_any(skb);
500 if (net_ratelimit())
501 netdev_err(ndev, "Tx DMA memory map failed\n");
502 return NETDEV_TX_OK;
503 }
504
505 if (nr_frags) {
506 ret = fec_enet_txq_submit_frag_skb(skb, ndev);
507 if (ret)
508 return ret;
509 } else {
510 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
511 if (fep->bufdesc_ex) {
512 estatus = BD_ENET_TX_INT;
513 if (unlikely(skb_shinfo(skb)->tx_flags &
514 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
515 estatus |= BD_ENET_TX_TS;
516 }
517 }
518
519 if (fep->bufdesc_ex) {
520
521 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
522
523 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
524 fep->hwts_tx_en))
525 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
526
527 if (skb->ip_summed == CHECKSUM_PARTIAL)
528 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
529
530 ebdp->cbd_bdu = 0;
531 ebdp->cbd_esc = estatus;
532 }
533
534 last_bdp = fep->cur_tx;
535 index = fec_enet_get_bd_index(fep->tx_bd_base, last_bdp, fep);
536 /* Save skb pointer */
537 fep->tx_skbuff[index] = skb;
538
539 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100540 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800541
542 /* Send it on its way. Tell FEC it's ready, interrupt when done,
543 * it's the last BD of the frame, and to put the CRC on the end.
544 */
545 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
546 bdp->cbd_sc = status;
547
548 fec_enet_submit_work(bdp, fep);
Frank Li03191652013-07-25 14:05:53 +0800549
Sascha Hauer22f6b862009-04-15 01:32:18 +0000550 /* If this was the last BD in the ring, start at the beginning again. */
Nimrod Andy6e909282014-06-12 08:16:22 +0800551 bdp = fec_enet_get_nextdesc(last_bdp, fep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800553 skb_tx_timestamp(skb);
554
Sascha Hauer2e285322009-04-15 01:32:16 +0000555 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Frank Lide5fb0a2013-03-03 17:34:25 +0000557 /* Trigger transmission start */
558 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
559
Nimrod Andy6e909282014-06-12 08:16:22 +0800560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Nimrod Andy79f33912014-06-12 08:16:23 +0800563static int
564fec_enet_txq_put_data_tso(struct sk_buff *skb, struct net_device *ndev,
565 struct bufdesc *bdp, int index, char *data,
566 int size, bool last_tcp, bool is_last)
567{
568 struct fec_enet_private *fep = netdev_priv(ndev);
569 const struct platform_device_id *id_entry =
570 platform_get_device_id(fep->pdev);
571 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
572 unsigned short status;
573 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100574 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800575
576 status = bdp->cbd_sc;
577 status &= ~BD_ENET_TX_STATS;
578
579 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800580
581 if (((unsigned long) data) & FEC_ALIGNMENT ||
582 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
583 memcpy(fep->tx_bounce[index], data, size);
584 data = fep->tx_bounce[index];
585
586 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
587 swap_buffer(data, size);
588 }
589
Russell Kingd6bf3142014-07-08 00:23:19 +0100590 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
591 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800592 dev_kfree_skb_any(skb);
593 if (net_ratelimit())
594 netdev_err(ndev, "Tx DMA memory map failed\n");
595 return NETDEV_TX_BUSY;
596 }
597
Russell Kingd6bf3142014-07-08 00:23:19 +0100598 bdp->cbd_datlen = size;
599 bdp->cbd_bufaddr = addr;
600
Nimrod Andy79f33912014-06-12 08:16:23 +0800601 if (fep->bufdesc_ex) {
602 if (skb->ip_summed == CHECKSUM_PARTIAL)
603 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
604 ebdp->cbd_bdu = 0;
605 ebdp->cbd_esc = estatus;
606 }
607
608 /* Handle the last BD specially */
609 if (last_tcp)
610 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
611 if (is_last) {
612 status |= BD_ENET_TX_INTR;
613 if (fep->bufdesc_ex)
614 ebdp->cbd_esc |= BD_ENET_TX_INT;
615 }
616
617 bdp->cbd_sc = status;
618
619 return 0;
620}
621
622static int
623fec_enet_txq_put_hdr_tso(struct sk_buff *skb, struct net_device *ndev,
624 struct bufdesc *bdp, int index)
625{
626 struct fec_enet_private *fep = netdev_priv(ndev);
627 const struct platform_device_id *id_entry =
628 platform_get_device_id(fep->pdev);
629 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
630 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
631 void *bufaddr;
632 unsigned long dmabuf;
633 unsigned short status;
634 unsigned int estatus = 0;
635
636 status = bdp->cbd_sc;
637 status &= ~BD_ENET_TX_STATS;
638 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
639
640 bufaddr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
641 dmabuf = fep->tso_hdrs_dma + index * TSO_HEADER_SIZE;
642 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
643 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
644 memcpy(fep->tx_bounce[index], skb->data, hdr_len);
645 bufaddr = fep->tx_bounce[index];
646
647 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
648 swap_buffer(bufaddr, hdr_len);
649
650 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
651 hdr_len, DMA_TO_DEVICE);
652 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
653 dev_kfree_skb_any(skb);
654 if (net_ratelimit())
655 netdev_err(ndev, "Tx DMA memory map failed\n");
656 return NETDEV_TX_BUSY;
657 }
658 }
659
660 bdp->cbd_bufaddr = dmabuf;
661 bdp->cbd_datlen = hdr_len;
662
663 if (fep->bufdesc_ex) {
664 if (skb->ip_summed == CHECKSUM_PARTIAL)
665 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
666 ebdp->cbd_bdu = 0;
667 ebdp->cbd_esc = estatus;
668 }
669
670 bdp->cbd_sc = status;
671
672 return 0;
673}
674
675static int fec_enet_txq_submit_tso(struct sk_buff *skb, struct net_device *ndev)
676{
677 struct fec_enet_private *fep = netdev_priv(ndev);
678 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
679 int total_len, data_left;
680 struct bufdesc *bdp = fep->cur_tx;
681 struct tso_t tso;
682 unsigned int index = 0;
683 int ret;
684
685 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep)) {
686 dev_kfree_skb_any(skb);
687 if (net_ratelimit())
688 netdev_err(ndev, "NOT enough BD for TSO!\n");
689 return NETDEV_TX_OK;
690 }
691
692 /* Protocol checksum off-load for TCP and UDP. */
693 if (fec_enet_clear_csum(skb, ndev)) {
694 dev_kfree_skb_any(skb);
695 return NETDEV_TX_OK;
696 }
697
698 /* Initialize the TSO handler, and prepare the first payload */
699 tso_start(skb, &tso);
700
701 total_len = skb->len - hdr_len;
702 while (total_len > 0) {
703 char *hdr;
704
705 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
706 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
707 total_len -= data_left;
708
709 /* prepare packet headers: MAC + IP + TCP */
710 hdr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
711 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
712 ret = fec_enet_txq_put_hdr_tso(skb, ndev, bdp, index);
713 if (ret)
714 goto err_release;
715
716 while (data_left > 0) {
717 int size;
718
719 size = min_t(int, tso.size, data_left);
720 bdp = fec_enet_get_nextdesc(bdp, fep);
721 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
722 ret = fec_enet_txq_put_data_tso(skb, ndev, bdp, index, tso.data,
723 size, size == data_left,
724 total_len == 0);
725 if (ret)
726 goto err_release;
727
728 data_left -= size;
729 tso_build_data(skb, &tso, size);
730 }
731
732 bdp = fec_enet_get_nextdesc(bdp, fep);
733 }
734
735 /* Save skb pointer */
736 fep->tx_skbuff[index] = skb;
737
738 fec_enet_submit_work(bdp, fep);
739
740 skb_tx_timestamp(skb);
741 fep->cur_tx = bdp;
742
743 /* Trigger transmission start */
744 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
745
746 return 0;
747
748err_release:
749 /* TODO: Release all used data descriptors for TSO */
750 return ret;
751}
752
Nimrod Andy61a44272014-06-12 08:16:18 +0800753static netdev_tx_t
754fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
755{
756 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800757 int entries_free;
Nimrod Andy61a44272014-06-12 08:16:18 +0800758 int ret;
759
Nimrod Andy79f33912014-06-12 08:16:23 +0800760 if (skb_is_gso(skb))
761 ret = fec_enet_txq_submit_tso(skb, ndev);
762 else
763 ret = fec_enet_txq_submit_skb(skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800764 if (ret)
765 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800766
Nimrod Andy6e909282014-06-12 08:16:22 +0800767 entries_free = fec_enet_get_free_txdesc_num(fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800768 if (entries_free <= fep->tx_stop_threshold)
Nimrod Andy61a44272014-06-12 08:16:18 +0800769 netif_stop_queue(ndev);
770
771 return NETDEV_TX_OK;
772}
773
Frank Li14109a52013-03-26 16:12:03 +0000774/* Init RX & TX buffer descriptors
775 */
776static void fec_enet_bd_init(struct net_device *dev)
777{
778 struct fec_enet_private *fep = netdev_priv(dev);
779 struct bufdesc *bdp;
780 unsigned int i;
781
782 /* Initialize the receive buffer descriptors. */
783 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800784 for (i = 0; i < fep->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000785
786 /* Initialize the BD for every fragment in the page. */
787 if (bdp->cbd_bufaddr)
788 bdp->cbd_sc = BD_ENET_RX_EMPTY;
789 else
790 bdp->cbd_sc = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800791 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000792 }
793
794 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800795 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000796 bdp->cbd_sc |= BD_SC_WRAP;
797
798 fep->cur_rx = fep->rx_bd_base;
799
800 /* ...and the same for transmit */
801 bdp = fep->tx_bd_base;
802 fep->cur_tx = bdp;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800803 for (i = 0; i < fep->tx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000804
805 /* Initialize the BD for every fragment in the page. */
806 bdp->cbd_sc = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100807 if (fep->tx_skbuff[i]) {
Frank Li14109a52013-03-26 16:12:03 +0000808 dev_kfree_skb_any(fep->tx_skbuff[i]);
809 fep->tx_skbuff[i] = NULL;
810 }
811 bdp->cbd_bufaddr = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800812 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000813 }
814
815 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800816 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000817 bdp->cbd_sc |= BD_SC_WRAP;
818 fep->dirty_tx = bdp;
819}
820
Russell Kingdbc64a82014-07-08 12:40:12 +0100821/*
822 * This function is called to start or restart the FEC during a link
823 * change, transmit timeout, or to reconfigure the FEC. The network
824 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100825 */
826static void
827fec_restart(struct net_device *ndev, int duplex)
828{
829 struct fec_enet_private *fep = netdev_priv(ndev);
830 const struct platform_device_id *id_entry =
831 platform_get_device_id(fep->pdev);
832 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000833 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100834 u32 temp_mac[2];
835 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000836 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100837
838 /* Whack a reset. We should wait for this. */
839 writel(1, fep->hwp + FEC_ECNTRL);
840 udelay(10);
841
842 /*
843 * enet-mac reset will reset mac address registers too,
844 * so need to reconfigure it.
845 */
846 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
847 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
848 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
849 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
850 }
851
852 /* Clear any outstanding interrupt. */
853 writel(0xffc00000, fep->hwp + FEC_IEVENT);
854
Uwe Kleine-König45993652011-01-19 20:47:04 +0100855 /* Set maximum receive buffer size. */
856 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
857
Frank Li14109a52013-03-26 16:12:03 +0000858 fec_enet_bd_init(ndev);
859
Uwe Kleine-König45993652011-01-19 20:47:04 +0100860 /* Set receive and transmit descriptor base. */
861 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000862 if (fep->bufdesc_ex)
863 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800864 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000865 else
866 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800867 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100868
Uwe Kleine-König45993652011-01-19 20:47:04 +0100869
Uwe Kleine-König45993652011-01-19 20:47:04 +0100870 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
871 if (fep->tx_skbuff[i]) {
872 dev_kfree_skb_any(fep->tx_skbuff[i]);
873 fep->tx_skbuff[i] = NULL;
874 }
875 }
876
877 /* Enable MII mode */
878 if (duplex) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100879 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100880 writel(0x04, fep->hwp + FEC_X_CNTRL);
881 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100882 /* No Rcv on Xmit */
883 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100884 writel(0x0, fep->hwp + FEC_X_CNTRL);
885 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100886
Uwe Kleine-König45993652011-01-19 20:47:04 +0100887 fep->full_duplex = duplex;
888
889 /* Set MII speed */
890 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
891
Guenter Roeckd1391932013-06-18 10:04:59 -0700892#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000893 /* set RX checksum */
894 val = readl(fep->hwp + FEC_RACC);
895 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
896 val |= FEC_RACC_OPTIONS;
897 else
898 val &= ~FEC_RACC_OPTIONS;
899 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700900#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000901
Uwe Kleine-König45993652011-01-19 20:47:04 +0100902 /*
903 * The phy interface and speed need to get configured
904 * differently on enet-mac.
905 */
906 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100907 /* Enable flow control and length check */
908 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100909
Shawn Guo230dec62011-09-23 02:12:48 +0000910 /* RGMII, RMII or MII */
911 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
912 rcntl |= (1 << 6);
913 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100914 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100915 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100916 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100917
Shawn Guo230dec62011-09-23 02:12:48 +0000918 /* 1G, 100M or 10M */
919 if (fep->phy_dev) {
920 if (fep->phy_dev->speed == SPEED_1000)
921 ecntl |= (1 << 5);
922 else if (fep->phy_dev->speed == SPEED_100)
923 rcntl &= ~(1 << 9);
924 else
925 rcntl |= (1 << 9);
926 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100927 } else {
928#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800929 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000930 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100931 /* disable the gasket and wait */
932 writel(0, fep->hwp + FEC_MIIGSK_ENR);
933 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
934 udelay(1);
935
936 /*
937 * configure the gasket:
938 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800939 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100940 */
Eric Benard8d82f212012-01-12 06:10:28 +0000941 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
942 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
943 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
944 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
945 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100946
947 /* re-enable the gasket */
948 writel(2, fep->hwp + FEC_MIIGSK_ENR);
949 }
950#endif
951 }
Frank Libaa70a52013-01-16 16:55:58 +0000952
Guenter Roeckd1391932013-06-18 10:04:59 -0700953#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +0000954 /* enable pause frame*/
955 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
956 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
957 fep->phy_dev && fep->phy_dev->pause)) {
958 rcntl |= FEC_ENET_FCE;
959
Jim Baxter4c09eed2013-04-19 08:10:49 +0000960 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000961 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
962 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
963 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
964 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
965
966 /* OPD */
967 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
968 } else {
969 rcntl &= ~FEC_ENET_FCE;
970 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700971#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +0000972
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100973 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100974
Stefan Wahren84fe6182014-03-12 11:28:19 +0100975 /* Setup multicast filter. */
976 set_multicast_list(ndev);
977#ifndef CONFIG_M5272
978 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
979 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
980#endif
981
Shawn Guo230dec62011-09-23 02:12:48 +0000982 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
983 /* enable ENET endian swap */
984 ecntl |= (1 << 8);
985 /* enable ENET store and forward mode */
986 writel(1 << 8, fep->hwp + FEC_X_WMRK);
987 }
988
Frank Liff43da82013-01-03 16:04:23 +0000989 if (fep->bufdesc_ex)
990 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000991
Chris Healy38ae92d2013-06-25 23:18:52 -0700992#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +0100993 /* Enable the MIB statistic event counters */
994 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -0700995#endif
996
Uwe Kleine-König45993652011-01-19 20:47:04 +0100997 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000998 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100999 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
1000
Frank Liff43da82013-01-03 16:04:23 +00001001 if (fep->bufdesc_ex)
1002 fec_ptp_start_cyclecounter(ndev);
1003
Uwe Kleine-König45993652011-01-19 20:47:04 +01001004 /* Enable interrupts we wish to service */
1005 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1006}
1007
1008static void
1009fec_stop(struct net_device *ndev)
1010{
1011 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001012 const struct platform_device_id *id_entry =
1013 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001014 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001015
1016 /* We cannot expect a graceful transmit stop without link !!! */
1017 if (fep->link) {
1018 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1019 udelay(10);
1020 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001021 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001022 }
1023
1024 /* Whack a reset. We should wait for this. */
1025 writel(1, fep->hwp + FEC_ECNTRL);
1026 udelay(10);
1027 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1028 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001029
1030 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001031 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001032 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001033 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1034 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001035}
1036
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001039fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001041 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001043 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Frank Li54309fa2013-05-07 14:08:44 +00001045 fep->delay_work.timeout = true;
1046 schedule_delayed_work(&(fep->delay_work.delay_work), 0);
1047}
1048
1049static void fec_enet_work(struct work_struct *work)
1050{
1051 struct fec_enet_private *fep =
1052 container_of(work,
1053 struct fec_enet_private,
1054 delay_work.delay_work.work);
Russell King8ce56242014-07-08 12:40:07 +01001055 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001056
1057 if (fep->delay_work.timeout) {
1058 fep->delay_work.timeout = false;
Russell Kingda1774e2014-07-08 12:39:57 +01001059 rtnl_lock();
Russell King8ce56242014-07-08 12:40:07 +01001060 if (netif_device_present(ndev) || netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001061 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001062 netif_tx_lock_bh(ndev);
Russell King8ce56242014-07-08 12:40:07 +01001063 fec_restart(ndev, fep->full_duplex);
1064 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001065 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001066 napi_enable(&fep->napi);
Russell King8ce56242014-07-08 12:40:07 +01001067 }
Russell Kingda1774e2014-07-08 12:39:57 +01001068 rtnl_unlock();
Frank Li54309fa2013-05-07 14:08:44 +00001069 }
Frank Li03191652013-07-25 14:05:53 +08001070
1071 if (fep->delay_work.trig_tx) {
1072 fep->delay_work.trig_tx = false;
1073 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001078fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001081 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001082 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +00001084 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001085 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001087 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 bdp = fep->dirty_tx;
1089
Frank Lide5fb0a2013-03-03 17:34:25 +00001090 /* get next bdp of dirty_tx */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001091 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001092
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001093 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001094
1095 /* current queue is empty */
1096 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001097 break;
1098
Nimrod Andy61a44272014-06-12 08:16:18 +08001099 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001100
Frank Lide5fb0a2013-03-03 17:34:25 +00001101 skb = fep->tx_skbuff[index];
Russell Kingd6bf3142014-07-08 00:23:19 +01001102 fep->tx_skbuff[index] = NULL;
Nimrod Andy79f33912014-06-12 08:16:23 +08001103 if (!IS_TSO_HEADER(fep, bdp->cbd_bufaddr))
1104 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1105 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001106 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001107 if (!skb) {
1108 bdp = fec_enet_get_nextdesc(bdp, fep);
1109 continue;
1110 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001113 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 BD_ENET_TX_RL | BD_ENET_TX_UN |
1115 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001116 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001117 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001118 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001119 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001120 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001121 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001122 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001123 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001124 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001125 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001126 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001128 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001129 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
Frank Liff43da82013-01-03 16:04:23 +00001132 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1133 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001134 struct skb_shared_hwtstamps shhwtstamps;
1135 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +00001136 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001137
1138 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1139 spin_lock_irqsave(&fep->tmreg_lock, flags);
1140 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +00001141 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +00001142 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1143 skb_tstamp_tx(skb, &shhwtstamps);
1144 }
Frank Liff43da82013-01-03 16:04:23 +00001145
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001146 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +00001147 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +00001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /* Deferred means some collisions occurred during transmit,
1150 * but we eventually sent the packet OK.
1151 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001152 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001153 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001154
Sascha Hauer22f6b862009-04-15 01:32:18 +00001155 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001157
1158 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001159
Sascha Hauer22f6b862009-04-15 01:32:18 +00001160 /* Update pointer to next buffer descriptor to be transmitted */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001161 bdp = fec_enet_get_nextdesc(bdp, fep);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001162
Sascha Hauer22f6b862009-04-15 01:32:18 +00001163 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001165 if (netif_queue_stopped(ndev)) {
1166 entries_free = fec_enet_get_free_txdesc_num(fep);
1167 if (entries_free >= fep->tx_wake_threshold)
1168 netif_wake_queue(ndev);
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001171 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174/* During a receive, the cur_rx points to the current incoming buffer.
1175 * When we update through the ring, if the next incoming buffer has
1176 * not been given to the system, we just set the empty indicator,
1177 * effectively tossing the packet.
1178 */
Frank Lidc975382013-01-28 18:31:42 +00001179static int
1180fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001182 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001183 const struct platform_device_id *id_entry =
1184 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001185 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001186 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct sk_buff *skb;
1188 ushort pkt_len;
1189 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001190 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001191 struct bufdesc_ex *ebdp = NULL;
1192 bool vlan_packet_rcvd = false;
1193 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001194 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001195
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001196#ifdef CONFIG_M532x
1197 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* First, grab all of the stats for the incoming packet.
1201 * These get messed up if we get called due to a busy condition.
1202 */
1203 bdp = fep->cur_rx;
1204
Sascha Hauer22f6b862009-04-15 01:32:18 +00001205 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Frank Lidc975382013-01-28 18:31:42 +00001207 if (pkt_received >= budget)
1208 break;
1209 pkt_received++;
1210
Sascha Hauer22f6b862009-04-15 01:32:18 +00001211 /* Since we have allocated space to hold a complete frame,
1212 * the last indicator should be set.
1213 */
1214 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001215 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Sascha Hauer22f6b862009-04-15 01:32:18 +00001217 /* Check for errors. */
1218 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001220 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001221 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1222 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001223 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001224 }
1225 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001226 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001227 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001228 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001229 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001230 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001232
1233 /* Report late collisions as a frame error.
1234 * On this error, the BD is closed, but we don't know what we
1235 * have in the buffer. So, just drop this frame on the floor.
1236 */
1237 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001238 ndev->stats.rx_errors++;
1239 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001240 goto rx_processing_done;
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Sascha Hauer22f6b862009-04-15 01:32:18 +00001243 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001244 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001245 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001246 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Nimrod Andy61a44272014-06-12 08:16:18 +08001248 index = fec_enet_get_bd_index(fep->rx_bd_base, bdp, fep);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001249 data = fep->rx_skbuff[index]->data;
1250 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1251 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001252
Shawn Guob5680e02011-01-05 21:13:13 +00001253 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1254 swap_buffer(data, pkt_len);
1255
Jim Baxtercdffcf12013-07-02 22:52:56 +01001256 /* Extract the enhanced buffer descriptor */
1257 ebdp = NULL;
1258 if (fep->bufdesc_ex)
1259 ebdp = (struct bufdesc_ex *)bdp;
1260
1261 /* If this is a VLAN packet remove the VLAN Tag */
1262 vlan_packet_rcvd = false;
1263 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1264 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
1265 /* Push and remove the vlan tag */
1266 struct vlan_hdr *vlan_header =
1267 (struct vlan_hdr *) (data + ETH_HLEN);
1268 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1269 pkt_len -= VLAN_HLEN;
1270
1271 vlan_packet_rcvd = true;
1272 }
1273
Sascha Hauer22f6b862009-04-15 01:32:18 +00001274 /* This does 16 byte alignment, exactly what we need.
1275 * The packet length includes FCS, but we don't want to
1276 * include that when passing upstream as it messes up
1277 * bridging applications.
1278 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001279 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Sascha Hauer85498892009-04-15 01:32:21 +00001281 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001282 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001283 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001284 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001285 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001286 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001287
1288 /* Extract the frame data without the VLAN header. */
1289 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1290 if (vlan_packet_rcvd)
1291 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
1292 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
1293 data + payload_offset,
1294 pkt_len - 4 - (2 * ETH_ALEN));
1295
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001296 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001297
Frank Li6605b732012-10-30 18:25:31 +00001298 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +00001299 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001300 struct skb_shared_hwtstamps *shhwtstamps =
1301 skb_hwtstamps(skb);
1302 unsigned long flags;
1303
1304 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1305
1306 spin_lock_irqsave(&fep->tmreg_lock, flags);
1307 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +00001308 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +00001309 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1310 }
Frank Liff43da82013-01-03 16:04:23 +00001311
Jim Baxter4c09eed2013-04-19 08:10:49 +00001312 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001313 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001314 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1315 /* don't check it */
1316 skb->ip_summed = CHECKSUM_UNNECESSARY;
1317 } else {
1318 skb_checksum_none_assert(skb);
1319 }
1320 }
1321
Jim Baxtercdffcf12013-07-02 22:52:56 +01001322 /* Handle received VLAN packets */
1323 if (vlan_packet_rcvd)
1324 __vlan_hwaccel_put_tag(skb,
1325 htons(ETH_P_8021Q),
1326 vlan_tag);
1327
Richard Cochran0affdf32013-08-30 20:28:10 +02001328 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001329 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001330
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001331 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1332 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001333rx_processing_done:
1334 /* Clear the status flags for this buffer */
1335 status &= ~BD_ENET_RX_STATS;
1336
1337 /* Mark the buffer empty */
1338 status |= BD_ENET_RX_EMPTY;
1339 bdp->cbd_sc = status;
1340
Frank Liff43da82013-01-03 16:04:23 +00001341 if (fep->bufdesc_ex) {
1342 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1343
1344 ebdp->cbd_esc = BD_ENET_RX_INT;
1345 ebdp->cbd_prot = 0;
1346 ebdp->cbd_bdu = 0;
1347 }
Frank Li6605b732012-10-30 18:25:31 +00001348
Sascha Hauer22f6b862009-04-15 01:32:18 +00001349 /* Update BD pointer to next entry */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001350 bdp = fec_enet_get_nextdesc(bdp, fep);
1351
Sascha Hauer22f6b862009-04-15 01:32:18 +00001352 /* Doing this here will keep the FEC running while we process
1353 * incoming frames. On a heavily loaded network, we should be
1354 * able to keep up at the expense of system resources.
1355 */
1356 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Sascha Hauer2e285322009-04-15 01:32:16 +00001358 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Frank Lidc975382013-01-28 18:31:42 +00001360 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Uwe Kleine-König45993652011-01-19 20:47:04 +01001363static irqreturn_t
1364fec_enet_interrupt(int irq, void *dev_id)
1365{
1366 struct net_device *ndev = dev_id;
1367 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001368 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001369 uint int_events;
1370 irqreturn_t ret = IRQ_NONE;
1371
Russell King7a168072014-07-08 00:22:44 +01001372 int_events = readl(fep->hwp + FEC_IEVENT);
1373 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001374
Russell King7a168072014-07-08 00:22:44 +01001375 if (int_events & napi_mask) {
1376 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001377
Russell King7a168072014-07-08 00:22:44 +01001378 /* Disable the NAPI interrupts */
1379 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1380 napi_schedule(&fep->napi);
1381 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001382
Russell King7a168072014-07-08 00:22:44 +01001383 if (int_events & FEC_ENET_MII) {
1384 ret = IRQ_HANDLED;
1385 complete(&fep->mdio_done);
1386 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001387
1388 return ret;
1389}
1390
Frank Lidc975382013-01-28 18:31:42 +00001391static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1392{
1393 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001394 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001395 int pkts;
1396
1397 /*
1398 * Clear any pending transmit or receive interrupts before
1399 * processing the rings to avoid racing with the hardware.
1400 */
1401 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1402
1403 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001404
Frank Lide5fb0a2013-03-03 17:34:25 +00001405 fec_enet_tx(ndev);
1406
Frank Lidc975382013-01-28 18:31:42 +00001407 if (pkts < budget) {
1408 napi_complete(napi);
1409 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1410 }
1411 return pkts;
1412}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001415static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001417 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001418 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001419 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Shawn Guo49da97d2011-01-05 21:13:11 +00001421 /*
1422 * try to get mac address in following order:
1423 *
1424 * 1) module parameter via kernel command line in form
1425 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1426 */
1427 iap = macaddr;
1428
1429 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001430 * 2) from device tree data
1431 */
1432 if (!is_valid_ether_addr(iap)) {
1433 struct device_node *np = fep->pdev->dev.of_node;
1434 if (np) {
1435 const char *mac = of_get_mac_address(np);
1436 if (mac)
1437 iap = (unsigned char *) mac;
1438 }
1439 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001440
1441 /*
1442 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001443 */
1444 if (!is_valid_ether_addr(iap)) {
1445#ifdef CONFIG_M5272
1446 if (FEC_FLASHMAC)
1447 iap = (unsigned char *)FEC_FLASHMAC;
1448#else
1449 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001450 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001451#endif
1452 }
1453
1454 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001455 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001456 */
1457 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001458 *((__be32 *) &tmpaddr[0]) =
1459 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1460 *((__be16 *) &tmpaddr[4]) =
1461 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 iap = &tmpaddr[0];
1463 }
1464
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001465 /*
1466 * 5) random mac address
1467 */
1468 if (!is_valid_ether_addr(iap)) {
1469 /* Report it and use a random ethernet address instead */
1470 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1471 eth_hw_addr_random(ndev);
1472 netdev_info(ndev, "Using random MAC address: %pM\n",
1473 ndev->dev_addr);
1474 return;
1475 }
1476
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001477 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Shawn Guo49da97d2011-01-05 21:13:11 +00001479 /* Adjust MAC if using macaddr */
1480 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001481 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484/* ------------------------------------------------------------------------- */
1485
Bryan Wue6b043d2010-03-31 02:10:44 +00001486/*
1487 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001489static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001491 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001492 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001493 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Bryan Wue6b043d2010-03-31 02:10:44 +00001495 /* Prevent a state halted on mii error */
1496 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1497 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001498 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001500
Russell King8ce56242014-07-08 12:40:07 +01001501 /*
1502 * If the netdev is down, or is going down, we're not interested
1503 * in link state events, so just mark our idea of the link as down
1504 * and ignore the event.
1505 */
1506 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1507 fep->link = 0;
1508 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001509 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001510 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001511 status_change = 1;
1512 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001513
Lucas Stachd97e7492013-03-14 05:12:01 +00001514 if (fep->full_duplex != phy_dev->duplex)
1515 status_change = 1;
1516
1517 if (phy_dev->speed != fep->speed) {
1518 fep->speed = phy_dev->speed;
1519 status_change = 1;
1520 }
1521
1522 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001523 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001524 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001525 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001526 fec_restart(ndev, phy_dev->duplex);
Russell Kingdbc64a82014-07-08 12:40:12 +01001527 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001528 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001529 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001530 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001531 } else {
1532 if (fep->link) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001533 fec_stop(ndev);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001534 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001535 status_change = 1;
1536 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001537 }
1538
Bryan Wue6b043d2010-03-31 02:10:44 +00001539 if (status_change)
1540 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Bryan Wue6b043d2010-03-31 02:10:44 +00001543static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Bryan Wue6b043d2010-03-31 02:10:44 +00001545 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001546 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001547
1548 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001549 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001550
1551 /* start a read op */
1552 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1553 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1554 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1555
1556 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001557 time_left = wait_for_completion_timeout(&fep->mdio_done,
1558 usecs_to_jiffies(FEC_MII_TIMEOUT));
1559 if (time_left == 0) {
1560 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001561 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001562 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001563 }
1564
1565 /* return value */
1566 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1567}
1568
1569static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1570 u16 value)
1571{
1572 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001573 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001574
1575 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001576 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001577
Shawn Guo862f0982011-01-05 21:13:09 +00001578 /* start a write op */
1579 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001580 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1581 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1582 fep->hwp + FEC_MII_DATA);
1583
1584 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001585 time_left = wait_for_completion_timeout(&fep->mdio_done,
1586 usecs_to_jiffies(FEC_MII_TIMEOUT));
1587 if (time_left == 0) {
1588 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001589 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001590 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001591 }
1592
1593 return 0;
1594}
1595
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001596static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1597{
1598 struct fec_enet_private *fep = netdev_priv(ndev);
1599 int ret;
1600
1601 if (enable) {
1602 ret = clk_prepare_enable(fep->clk_ahb);
1603 if (ret)
1604 return ret;
1605 ret = clk_prepare_enable(fep->clk_ipg);
1606 if (ret)
1607 goto failed_clk_ipg;
1608 if (fep->clk_enet_out) {
1609 ret = clk_prepare_enable(fep->clk_enet_out);
1610 if (ret)
1611 goto failed_clk_enet_out;
1612 }
1613 if (fep->clk_ptp) {
1614 ret = clk_prepare_enable(fep->clk_ptp);
1615 if (ret)
1616 goto failed_clk_ptp;
1617 }
1618 } else {
1619 clk_disable_unprepare(fep->clk_ahb);
1620 clk_disable_unprepare(fep->clk_ipg);
1621 if (fep->clk_enet_out)
1622 clk_disable_unprepare(fep->clk_enet_out);
1623 if (fep->clk_ptp)
1624 clk_disable_unprepare(fep->clk_ptp);
1625 }
1626
1627 return 0;
1628failed_clk_ptp:
1629 if (fep->clk_enet_out)
1630 clk_disable_unprepare(fep->clk_enet_out);
1631failed_clk_enet_out:
1632 clk_disable_unprepare(fep->clk_ipg);
1633failed_clk_ipg:
1634 clk_disable_unprepare(fep->clk_ahb);
1635
1636 return ret;
1637}
1638
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001639static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001640{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001641 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001642 const struct platform_device_id *id_entry =
1643 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001644 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001645 char mdio_bus_id[MII_BUS_ID_SIZE];
1646 char phy_name[MII_BUS_ID_SIZE + 3];
1647 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001648 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001649
Bryan Wu418bd0d2010-05-28 03:40:39 -07001650 fep->phy_dev = NULL;
1651
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001652 /* check for attached phy */
1653 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1654 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1655 continue;
1656 if (fep->mii_bus->phy_map[phy_id] == NULL)
1657 continue;
1658 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1659 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001660 if (dev_id--)
1661 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001662 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1663 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001664 }
1665
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001666 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001667 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001668 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001669 phy_id = 0;
1670 }
1671
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001672 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001673 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001674 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001675 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001676 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001677 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001678 }
1679
1680 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001681 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001682 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001683 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001684#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001685 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001686#endif
Frank Libaa70a52013-01-16 16:55:58 +00001687 }
Shawn Guo230dec62011-09-23 02:12:48 +00001688 else
1689 phy_dev->supported &= PHY_BASIC_FEATURES;
1690
Bryan Wue6b043d2010-03-31 02:10:44 +00001691 phy_dev->advertising = phy_dev->supported;
1692
1693 fep->phy_dev = phy_dev;
1694 fep->link = 0;
1695 fep->full_duplex = 0;
1696
Joe Perches31b77202013-04-13 19:03:17 +00001697 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1698 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1699 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001700
Bryan Wue6b043d2010-03-31 02:10:44 +00001701 return 0;
1702}
1703
1704static int fec_enet_mii_init(struct platform_device *pdev)
1705{
Shawn Guob5680e02011-01-05 21:13:13 +00001706 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001707 struct net_device *ndev = platform_get_drvdata(pdev);
1708 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001709 const struct platform_device_id *id_entry =
1710 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001711 int err = -ENXIO, i;
1712
Shawn Guob5680e02011-01-05 21:13:13 +00001713 /*
1714 * The dual fec interfaces are not equivalent with enet-mac.
1715 * Here are the differences:
1716 *
1717 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1718 * - fec0 acts as the 1588 time master while fec1 is slave
1719 * - external phys can only be configured by fec0
1720 *
1721 * That is to say fec1 can not work independently. It only works
1722 * when fec0 is working. The reason behind this design is that the
1723 * second interface is added primarily for Switch mode.
1724 *
1725 * Because of the last point above, both phys are attached on fec0
1726 * mdio interface in board design, and need to be configured by
1727 * fec0 mii_bus.
1728 */
Shawn Guo43af9402011-12-05 05:01:15 +00001729 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001730 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001731 if (mii_cnt && fec0_mii_bus) {
1732 fep->mii_bus = fec0_mii_bus;
1733 mii_cnt++;
1734 return 0;
1735 }
1736 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001737 }
1738
Bryan Wue6b043d2010-03-31 02:10:44 +00001739 fep->mii_timeout = 0;
1740
1741 /*
1742 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001743 *
1744 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1745 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1746 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1747 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001748 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001749 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001750 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1751 fep->phy_speed--;
1752 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001753 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1754
1755 fep->mii_bus = mdiobus_alloc();
1756 if (fep->mii_bus == NULL) {
1757 err = -ENOMEM;
1758 goto err_out;
1759 }
1760
1761 fep->mii_bus->name = "fec_enet_mii_bus";
1762 fep->mii_bus->read = fec_enet_mdio_read;
1763 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001764 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1765 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001766 fep->mii_bus->priv = fep;
1767 fep->mii_bus->parent = &pdev->dev;
1768
1769 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1770 if (!fep->mii_bus->irq) {
1771 err = -ENOMEM;
1772 goto err_out_free_mdiobus;
1773 }
1774
1775 for (i = 0; i < PHY_MAX_ADDR; i++)
1776 fep->mii_bus->irq[i] = PHY_POLL;
1777
Bryan Wue6b043d2010-03-31 02:10:44 +00001778 if (mdiobus_register(fep->mii_bus))
1779 goto err_out_free_mdio_irq;
1780
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001781 mii_cnt++;
1782
Shawn Guob5680e02011-01-05 21:13:13 +00001783 /* save fec0 mii_bus */
1784 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1785 fec0_mii_bus = fep->mii_bus;
1786
Bryan Wue6b043d2010-03-31 02:10:44 +00001787 return 0;
1788
Bryan Wue6b043d2010-03-31 02:10:44 +00001789err_out_free_mdio_irq:
1790 kfree(fep->mii_bus->irq);
1791err_out_free_mdiobus:
1792 mdiobus_free(fep->mii_bus);
1793err_out:
1794 return err;
1795}
1796
1797static void fec_enet_mii_remove(struct fec_enet_private *fep)
1798{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001799 if (--mii_cnt == 0) {
1800 mdiobus_unregister(fep->mii_bus);
1801 kfree(fep->mii_bus->irq);
1802 mdiobus_free(fep->mii_bus);
1803 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001804}
1805
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001806static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001807 struct ethtool_cmd *cmd)
1808{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001809 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001810 struct phy_device *phydev = fep->phy_dev;
1811
1812 if (!phydev)
1813 return -ENODEV;
1814
1815 return phy_ethtool_gset(phydev, cmd);
1816}
1817
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001818static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001819 struct ethtool_cmd *cmd)
1820{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001821 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001822 struct phy_device *phydev = fep->phy_dev;
1823
1824 if (!phydev)
1825 return -ENODEV;
1826
1827 return phy_ethtool_sset(phydev, cmd);
1828}
1829
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001830static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001831 struct ethtool_drvinfo *info)
1832{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001833 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Jiri Pirko7826d432013-01-06 00:44:26 +00001835 strlcpy(info->driver, fep->pdev->dev.driver->name,
1836 sizeof(info->driver));
1837 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1838 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
Bryan Wue6b043d2010-03-31 02:10:44 +00001840
Frank Li5ebae4892013-01-06 16:25:07 +00001841static int fec_enet_get_ts_info(struct net_device *ndev,
1842 struct ethtool_ts_info *info)
1843{
1844 struct fec_enet_private *fep = netdev_priv(ndev);
1845
1846 if (fep->bufdesc_ex) {
1847
1848 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1849 SOF_TIMESTAMPING_RX_SOFTWARE |
1850 SOF_TIMESTAMPING_SOFTWARE |
1851 SOF_TIMESTAMPING_TX_HARDWARE |
1852 SOF_TIMESTAMPING_RX_HARDWARE |
1853 SOF_TIMESTAMPING_RAW_HARDWARE;
1854 if (fep->ptp_clock)
1855 info->phc_index = ptp_clock_index(fep->ptp_clock);
1856 else
1857 info->phc_index = -1;
1858
1859 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1860 (1 << HWTSTAMP_TX_ON);
1861
1862 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1863 (1 << HWTSTAMP_FILTER_ALL);
1864 return 0;
1865 } else {
1866 return ethtool_op_get_ts_info(ndev, info);
1867 }
1868}
1869
Guenter Roeckd1391932013-06-18 10:04:59 -07001870#if !defined(CONFIG_M5272)
1871
Frank Libaa70a52013-01-16 16:55:58 +00001872static void fec_enet_get_pauseparam(struct net_device *ndev,
1873 struct ethtool_pauseparam *pause)
1874{
1875 struct fec_enet_private *fep = netdev_priv(ndev);
1876
1877 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1878 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1879 pause->rx_pause = pause->tx_pause;
1880}
1881
1882static int fec_enet_set_pauseparam(struct net_device *ndev,
1883 struct ethtool_pauseparam *pause)
1884{
1885 struct fec_enet_private *fep = netdev_priv(ndev);
1886
Russell King0b146ca2014-07-08 00:22:59 +01001887 if (!fep->phy_dev)
1888 return -ENODEV;
1889
Frank Libaa70a52013-01-16 16:55:58 +00001890 if (pause->tx_pause != pause->rx_pause) {
1891 netdev_info(ndev,
1892 "hardware only support enable/disable both tx and rx");
1893 return -EINVAL;
1894 }
1895
1896 fep->pause_flag = 0;
1897
1898 /* tx pause must be same as rx pause */
1899 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1900 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1901
1902 if (pause->rx_pause || pause->autoneg) {
1903 fep->phy_dev->supported |= ADVERTISED_Pause;
1904 fep->phy_dev->advertising |= ADVERTISED_Pause;
1905 } else {
1906 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1907 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1908 }
1909
1910 if (pause->autoneg) {
1911 if (netif_running(ndev))
1912 fec_stop(ndev);
1913 phy_start_aneg(fep->phy_dev);
1914 }
Russell Kingdbc64a82014-07-08 12:40:12 +01001915 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001916 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001917 netif_tx_lock_bh(ndev);
Russell King9671a422014-07-08 00:22:39 +01001918 fec_restart(ndev, fep->full_duplex);
Russell Kingdbc64a82014-07-08 12:40:12 +01001919 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001920 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001921 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001922 }
Frank Libaa70a52013-01-16 16:55:58 +00001923
1924 return 0;
1925}
1926
Chris Healy38ae92d2013-06-25 23:18:52 -07001927static const struct fec_stat {
1928 char name[ETH_GSTRING_LEN];
1929 u16 offset;
1930} fec_stats[] = {
1931 /* RMON TX */
1932 { "tx_dropped", RMON_T_DROP },
1933 { "tx_packets", RMON_T_PACKETS },
1934 { "tx_broadcast", RMON_T_BC_PKT },
1935 { "tx_multicast", RMON_T_MC_PKT },
1936 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1937 { "tx_undersize", RMON_T_UNDERSIZE },
1938 { "tx_oversize", RMON_T_OVERSIZE },
1939 { "tx_fragment", RMON_T_FRAG },
1940 { "tx_jabber", RMON_T_JAB },
1941 { "tx_collision", RMON_T_COL },
1942 { "tx_64byte", RMON_T_P64 },
1943 { "tx_65to127byte", RMON_T_P65TO127 },
1944 { "tx_128to255byte", RMON_T_P128TO255 },
1945 { "tx_256to511byte", RMON_T_P256TO511 },
1946 { "tx_512to1023byte", RMON_T_P512TO1023 },
1947 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
1948 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
1949 { "tx_octets", RMON_T_OCTETS },
1950
1951 /* IEEE TX */
1952 { "IEEE_tx_drop", IEEE_T_DROP },
1953 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
1954 { "IEEE_tx_1col", IEEE_T_1COL },
1955 { "IEEE_tx_mcol", IEEE_T_MCOL },
1956 { "IEEE_tx_def", IEEE_T_DEF },
1957 { "IEEE_tx_lcol", IEEE_T_LCOL },
1958 { "IEEE_tx_excol", IEEE_T_EXCOL },
1959 { "IEEE_tx_macerr", IEEE_T_MACERR },
1960 { "IEEE_tx_cserr", IEEE_T_CSERR },
1961 { "IEEE_tx_sqe", IEEE_T_SQE },
1962 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
1963 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
1964
1965 /* RMON RX */
1966 { "rx_packets", RMON_R_PACKETS },
1967 { "rx_broadcast", RMON_R_BC_PKT },
1968 { "rx_multicast", RMON_R_MC_PKT },
1969 { "rx_crc_errors", RMON_R_CRC_ALIGN },
1970 { "rx_undersize", RMON_R_UNDERSIZE },
1971 { "rx_oversize", RMON_R_OVERSIZE },
1972 { "rx_fragment", RMON_R_FRAG },
1973 { "rx_jabber", RMON_R_JAB },
1974 { "rx_64byte", RMON_R_P64 },
1975 { "rx_65to127byte", RMON_R_P65TO127 },
1976 { "rx_128to255byte", RMON_R_P128TO255 },
1977 { "rx_256to511byte", RMON_R_P256TO511 },
1978 { "rx_512to1023byte", RMON_R_P512TO1023 },
1979 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
1980 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
1981 { "rx_octets", RMON_R_OCTETS },
1982
1983 /* IEEE RX */
1984 { "IEEE_rx_drop", IEEE_R_DROP },
1985 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
1986 { "IEEE_rx_crc", IEEE_R_CRC },
1987 { "IEEE_rx_align", IEEE_R_ALIGN },
1988 { "IEEE_rx_macerr", IEEE_R_MACERR },
1989 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
1990 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
1991};
1992
1993static void fec_enet_get_ethtool_stats(struct net_device *dev,
1994 struct ethtool_stats *stats, u64 *data)
1995{
1996 struct fec_enet_private *fep = netdev_priv(dev);
1997 int i;
1998
1999 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2000 data[i] = readl(fep->hwp + fec_stats[i].offset);
2001}
2002
2003static void fec_enet_get_strings(struct net_device *netdev,
2004 u32 stringset, u8 *data)
2005{
2006 int i;
2007 switch (stringset) {
2008 case ETH_SS_STATS:
2009 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2010 memcpy(data + i * ETH_GSTRING_LEN,
2011 fec_stats[i].name, ETH_GSTRING_LEN);
2012 break;
2013 }
2014}
2015
2016static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2017{
2018 switch (sset) {
2019 case ETH_SS_STATS:
2020 return ARRAY_SIZE(fec_stats);
2021 default:
2022 return -EOPNOTSUPP;
2023 }
2024}
Guenter Roeckd1391932013-06-18 10:04:59 -07002025#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002026
Chris Healy32bc9b42013-06-17 07:25:06 -07002027static int fec_enet_nway_reset(struct net_device *dev)
2028{
2029 struct fec_enet_private *fep = netdev_priv(dev);
2030 struct phy_device *phydev = fep->phy_dev;
2031
2032 if (!phydev)
2033 return -ENODEV;
2034
2035 return genphy_restart_aneg(phydev);
2036}
2037
stephen hemminger9b07be42012-01-04 12:59:49 +00002038static const struct ethtool_ops fec_enet_ethtool_ops = {
Guenter Roeckd1391932013-06-18 10:04:59 -07002039#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002040 .get_pauseparam = fec_enet_get_pauseparam,
2041 .set_pauseparam = fec_enet_set_pauseparam,
Guenter Roeckd1391932013-06-18 10:04:59 -07002042#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00002043 .get_settings = fec_enet_get_settings,
2044 .set_settings = fec_enet_set_settings,
2045 .get_drvinfo = fec_enet_get_drvinfo,
2046 .get_link = ethtool_op_get_link,
Frank Li5ebae4892013-01-06 16:25:07 +00002047 .get_ts_info = fec_enet_get_ts_info,
Chris Healy32bc9b42013-06-17 07:25:06 -07002048 .nway_reset = fec_enet_nway_reset,
Chris Healy38ae92d2013-06-25 23:18:52 -07002049#ifndef CONFIG_M5272
2050 .get_ethtool_stats = fec_enet_get_ethtool_stats,
2051 .get_strings = fec_enet_get_strings,
2052 .get_sset_count = fec_enet_get_sset_count,
2053#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00002054};
2055
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002056static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002057{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002058 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002059 struct phy_device *phydev = fep->phy_dev;
2060
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002061 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002062 return -EINVAL;
2063
2064 if (!phydev)
2065 return -ENODEV;
2066
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002067 if (fep->bufdesc_ex) {
2068 if (cmd == SIOCSHWTSTAMP)
2069 return fec_ptp_set(ndev, rq);
2070 if (cmd == SIOCGHWTSTAMP)
2071 return fec_ptp_get(ndev, rq);
2072 }
Frank Liff43da82013-01-03 16:04:23 +00002073
Richard Cochran28b04112010-07-17 08:48:55 +00002074 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002077static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002078{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002079 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002080 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002081 struct sk_buff *skb;
2082 struct bufdesc *bdp;
2083
2084 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002085 for (i = 0; i < fep->rx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002086 skb = fep->rx_skbuff[i];
Russell King730ee362014-07-08 00:23:14 +01002087 fep->rx_skbuff[i] = NULL;
2088 if (skb) {
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01002089 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002090 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002091 dev_kfree_skb(skb);
Russell King730ee362014-07-08 00:23:14 +01002092 }
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002093 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002094 }
2095
2096 bdp = fep->tx_bd_base;
Russell King8b7c9ef2014-07-08 00:23:25 +01002097 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002098 kfree(fep->tx_bounce[i]);
Russell King8b7c9ef2014-07-08 00:23:25 +01002099 fep->tx_bounce[i] = NULL;
2100 skb = fep->tx_skbuff[i];
2101 fep->tx_skbuff[i] = NULL;
2102 dev_kfree_skb(skb);
2103 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002104}
2105
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002106static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002107{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002108 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002109 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002110 struct sk_buff *skb;
2111 struct bufdesc *bdp;
2112
2113 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002114 for (i = 0; i < fep->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002115 dma_addr_t addr;
2116
Fabio Estevamb72061a2012-02-07 08:27:31 +00002117 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002118 if (!skb)
2119 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002120
Russell King730ee362014-07-08 00:23:14 +01002121 addr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002122 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Russell King730ee362014-07-08 00:23:14 +01002123 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2124 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002125 if (net_ratelimit())
2126 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002127 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002128 }
Russell King730ee362014-07-08 00:23:14 +01002129
2130 fep->rx_skbuff[i] = skb;
2131 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002132 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002133
2134 if (fep->bufdesc_ex) {
2135 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2136 ebdp->cbd_esc = BD_ENET_RX_INT;
2137 }
2138
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002139 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002140 }
2141
2142 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002143 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002144 bdp->cbd_sc |= BD_SC_WRAP;
2145
2146 bdp = fep->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002147 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002148 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
Russell Kingffdce2c2014-07-08 00:23:30 +01002149 if (!fep->tx_bounce[i])
2150 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002151
2152 bdp->cbd_sc = 0;
2153 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002154
Frank Liff43da82013-01-03 16:04:23 +00002155 if (fep->bufdesc_ex) {
2156 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002157 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002158 }
2159
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002160 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002161 }
2162
2163 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002164 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002165 bdp->cbd_sc |= BD_SC_WRAP;
2166
2167 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002168
2169 err_alloc:
2170 fec_enet_free_buffers(ndev);
2171 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002175fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002177 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002178 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002180 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002181 ret = fec_enet_clk_enable(ndev, true);
2182 if (ret)
2183 return ret;
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /* I should reset the ring buffers here, but I don't yet know
2186 * a simple way to do that.
2187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002189 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002190 if (ret)
2191 return ret;
2192
Bryan Wu418bd0d2010-05-28 03:40:39 -07002193 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002194 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002195 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002196 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002197 return ret;
2198 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002199
Russell King8ce56242014-07-08 12:40:07 +01002200 fec_restart(ndev, fep->full_duplex);
Russell Kingce5eaf02014-02-18 12:55:42 +00002201 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002202 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002203 netif_start_queue(ndev);
Sascha Hauer22f6b862009-04-15 01:32:18 +00002204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
2207static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002208fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002210 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Russell Kingd76cfae2014-07-08 00:23:04 +01002212 phy_stop(fep->phy_dev);
2213
Georg Hofmann3f104c32013-03-14 06:54:09 +00002214 napi_disable(&fep->napi);
Russell Kingb49cd502014-07-08 00:22:49 +01002215 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002216 if (netif_device_present(ndev))
2217 fec_stop(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Russell King635cf172014-07-08 00:22:54 +01002219 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002220 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002221
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002222 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002223 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002224 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return 0;
2227}
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229/* Set or clear the multicast filter for this adaptor.
2230 * Skeleton taken from sunlance driver.
2231 * The CPM Ethernet implementation allows Multicast as well as individual
2232 * MAC address filtering. Some of the drivers check to make sure it is
2233 * a group multicast address, and discard those that are not. I guess I
2234 * will do the same for now, but just remove the test if you want
2235 * individual filtering as well (do the upper net layers want or support
2236 * this kind of feature?).
2237 */
2238
2239#define HASH_BITS 6 /* #bits in hash */
2240#define CRC32_POLY 0xEDB88320
2241
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002242static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002244 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002245 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002246 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 unsigned char hash;
2248
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002249 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002250 tmp = readl(fep->hwp + FEC_R_CNTRL);
2251 tmp |= 0x8;
2252 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002253 return;
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Sascha Hauer4e831832009-04-15 01:32:19 +00002256 tmp = readl(fep->hwp + FEC_R_CNTRL);
2257 tmp &= ~0x8;
2258 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002259
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002260 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002261 /* Catch all multicast addresses, so set the
2262 * filter to all 1's
2263 */
2264 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2265 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Sascha Hauer4e831832009-04-15 01:32:19 +00002267 return;
2268 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002269
Sascha Hauer4e831832009-04-15 01:32:19 +00002270 /* Clear filter and add the addresses in hash register
2271 */
2272 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2273 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002275 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002276 /* calculate crc32 value of mac address */
2277 crc = 0xffffffff;
2278
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002279 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002280 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002281 for (bit = 0; bit < 8; bit++, data >>= 1) {
2282 crc = (crc >> 1) ^
2283 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002286
2287 /* only upper 6 bits (HASH_BITS) are used
2288 * which point to specific bit in he hash registers
2289 */
2290 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2291
2292 if (hash > 31) {
2293 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2294 tmp |= 1 << (hash - 32);
2295 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2296 } else {
2297 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2298 tmp |= 1 << hash;
2299 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302}
2303
Sascha Hauer22f6b862009-04-15 01:32:18 +00002304/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002305static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002306fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002308 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002309 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Lucas Stach44934fa2014-03-30 21:32:08 +02002311 if (addr) {
2312 if (!is_valid_ether_addr(addr->sa_data))
2313 return -EADDRNOTAVAIL;
2314 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2315 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002316
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002317 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2318 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002319 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002320 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002321 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002325#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002326/**
2327 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002328 * @dev: The FEC network adapter
2329 *
2330 * Polled functionality used by netconsole and others in non interrupt mode
2331 *
2332 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002333static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002334{
2335 int i;
2336 struct fec_enet_private *fep = netdev_priv(dev);
2337
2338 for (i = 0; i < FEC_IRQ_NUM; i++) {
2339 if (fep->irq[i] > 0) {
2340 disable_irq(fep->irq[i]);
2341 fec_enet_interrupt(fep->irq[i], dev);
2342 enable_irq(fep->irq[i]);
2343 }
2344 }
2345}
2346#endif
2347
Jim Baxter4c09eed2013-04-19 08:10:49 +00002348static int fec_set_features(struct net_device *netdev,
2349 netdev_features_t features)
2350{
2351 struct fec_enet_private *fep = netdev_priv(netdev);
2352 netdev_features_t changed = features ^ netdev->features;
2353
2354 netdev->features = features;
2355
2356 /* Receive checksum has been changed */
2357 if (changed & NETIF_F_RXCSUM) {
2358 if (features & NETIF_F_RXCSUM)
2359 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2360 else
2361 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
2362
2363 if (netif_running(netdev)) {
2364 fec_stop(netdev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002365 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002366 netif_tx_lock_bh(netdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002367 fec_restart(netdev, fep->phy_dev->duplex);
2368 netif_wake_queue(netdev);
Russell King6af42d42014-07-08 12:40:18 +01002369 netif_tx_unlock_bh(netdev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002370 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002371 }
2372 }
2373
2374 return 0;
2375}
2376
Sascha Hauer009fda82009-04-15 01:32:23 +00002377static const struct net_device_ops fec_netdev_ops = {
2378 .ndo_open = fec_enet_open,
2379 .ndo_stop = fec_enet_close,
2380 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002381 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002382 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002383 .ndo_validate_addr = eth_validate_addr,
2384 .ndo_tx_timeout = fec_timeout,
2385 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002386 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002387#ifdef CONFIG_NET_POLL_CONTROLLER
2388 .ndo_poll_controller = fec_poll_controller,
2389#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002390 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002391};
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 /*
2394 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002395 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002397static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002399 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002400 const struct platform_device_id *id_entry =
2401 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002402 struct bufdesc *cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002403 int bd_size;
2404
2405 /* init the tx & rx ring size */
2406 fep->tx_ring_size = TX_RING_SIZE;
2407 fep->rx_ring_size = RX_RING_SIZE;
2408
Nimrod Andy79f33912014-06-12 08:16:23 +08002409 fep->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2410 fep->tx_wake_threshold = (fep->tx_ring_size - fep->tx_stop_threshold) / 2;
2411
Nimrod Andy55d02182014-06-12 08:16:21 +08002412 if (fep->bufdesc_ex)
2413 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2414 else
2415 fep->bufdesc_size = sizeof(struct bufdesc);
2416 bd_size = (fep->tx_ring_size + fep->rx_ring_size) *
2417 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002419 /* Allocate memory for buffer descriptors. */
Nimrod Andy55d02182014-06-12 08:16:21 +08002420 cbd_base = dma_alloc_coherent(NULL, bd_size, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002421 GFP_KERNEL);
2422 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10002423 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10002424
Nimrod Andy79f33912014-06-12 08:16:23 +08002425 fep->tso_hdrs = dma_alloc_coherent(NULL, fep->tx_ring_size * TSO_HEADER_SIZE,
2426 &fep->tso_hdrs_dma, GFP_KERNEL);
2427 if (!fep->tso_hdrs) {
2428 dma_free_coherent(NULL, bd_size, cbd_base, fep->bd_dma);
2429 return -ENOMEM;
2430 }
2431
Frank Li14109a52013-03-26 16:12:03 +00002432 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002433
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002434 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Shawn Guo49da97d2011-01-05 21:13:11 +00002436 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002437 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002438 /* make sure MAC we just acquired is programmed into the hw */
2439 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002441 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 fep->rx_bd_base = cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002443 if (fep->bufdesc_ex)
Frank Liff43da82013-01-03 16:04:23 +00002444 fep->tx_bd_base = (struct bufdesc *)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002445 (((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
Nimrod Andy55d02182014-06-12 08:16:21 +08002446 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002447 fep->tx_bd_base = cbd_base + fep->rx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Sascha Hauer22f6b862009-04-15 01:32:18 +00002449 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002450 ndev->watchdog_timeo = TX_TIMEOUT;
2451 ndev->netdev_ops = &fec_netdev_ops;
2452 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002453
Frank Lidc975382013-01-28 18:31:42 +00002454 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002455 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002456
Nimrod Andy09d1e542014-06-12 08:16:20 +08002457 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002458 /* enable hw VLAN support */
2459 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002460
Shawn Guo48496252013-05-08 21:08:22 +00002461 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002462 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2463
Shawn Guo48496252013-05-08 21:08:22 +00002464 /* enable hw accelerator */
2465 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002466 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002467 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2468 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002469
Nimrod Andy09d1e542014-06-12 08:16:20 +08002470 ndev->hw_features = ndev->features;
2471
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002472 fec_restart(ndev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return 0;
2475}
2476
Shawn Guoca2cc332011-06-25 02:04:35 +08002477#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002478static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002479{
2480 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002481 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002482 struct device_node *np = pdev->dev.of_node;
2483
2484 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002485 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002486
Shawn Guoa3caad02012-06-27 03:45:24 +00002487 of_property_read_u32(np, "phy-reset-duration", &msec);
2488 /* A sane reset duration should not be longer than 1s */
2489 if (msec > 1000)
2490 msec = 1;
2491
Shawn Guoca2cc332011-06-25 02:04:35 +08002492 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002493 if (!gpio_is_valid(phy_reset))
2494 return;
2495
Shawn Guo119fc002012-06-27 03:45:22 +00002496 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2497 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002498 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002499 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002500 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002501 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002502 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08002503 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002504}
2505#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002506static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002507{
2508 /*
2509 * In case of platform probe, the reset has been done
2510 * by machine code.
2511 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002512}
2513#endif /* CONFIG_OF */
2514
Bill Pemberton33897cc2012-12-03 09:23:58 -05002515static int
Sascha Haueread73182009-01-28 23:03:11 +00002516fec_probe(struct platform_device *pdev)
2517{
2518 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002519 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002520 struct net_device *ndev;
2521 int i, irq, ret = 0;
2522 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002523 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002524 static int dev_id;
Shawn Guoca2cc332011-06-25 02:04:35 +08002525
2526 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2527 if (of_id)
2528 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002529
Sascha Haueread73182009-01-28 23:03:11 +00002530 /* Init network device */
2531 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00002532 if (!ndev)
2533 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002534
2535 SET_NETDEV_DEV(ndev, &pdev->dev);
2536
2537 /* setup board info structure */
2538 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002539
Guenter Roeckd1391932013-06-18 10:04:59 -07002540#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002541 /* default enable pause frame auto negotiation */
2542 if (pdev->id_entry &&
2543 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2544 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002545#endif
Frank Libaa70a52013-01-16 16:55:58 +00002546
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002547 /* Select default pin state */
2548 pinctrl_pm_select_default_state(&pdev->dev);
2549
Fabio Estevam399db752013-07-21 13:25:03 -03002550 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302551 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2552 if (IS_ERR(fep->hwp)) {
2553 ret = PTR_ERR(fep->hwp);
2554 goto failed_ioremap;
2555 }
2556
Bryan Wue6b043d2010-03-31 02:10:44 +00002557 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002558 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002559
Frank Liff43da82013-01-03 16:04:23 +00002560 fep->bufdesc_ex = 0;
2561
Sascha Haueread73182009-01-28 23:03:11 +00002562 platform_set_drvdata(pdev, ndev);
2563
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002564 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002565 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002566 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002567 if (pdata)
2568 fep->phy_interface = pdata->phy;
2569 else
2570 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2571 } else {
2572 fep->phy_interface = ret;
2573 }
2574
Fabio Estevame2f8d552013-02-22 06:40:45 +00002575 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2576 if (IS_ERR(fep->clk_ipg)) {
2577 ret = PTR_ERR(fep->clk_ipg);
2578 goto failed_clk;
2579 }
2580
2581 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2582 if (IS_ERR(fep->clk_ahb)) {
2583 ret = PTR_ERR(fep->clk_ahb);
2584 goto failed_clk;
2585 }
2586
Linus Torvalds38f56f32013-05-07 11:06:17 -07002587 /* enet_out is optional, depends on board */
2588 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2589 if (IS_ERR(fep->clk_enet_out))
2590 fep->clk_enet_out = NULL;
2591
Fabio Estevame2f8d552013-02-22 06:40:45 +00002592 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002593 fep->bufdesc_ex =
2594 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002595 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07002596 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002597 fep->bufdesc_ex = 0;
2598 }
2599
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002600 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002601 if (ret)
2602 goto failed_clk;
2603
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00002604 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2605 if (!IS_ERR(fep->reg_phy)) {
2606 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002607 if (ret) {
2608 dev_err(&pdev->dev,
2609 "Failed to enable phy regulator: %d\n", ret);
2610 goto failed_regulator;
2611 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002612 } else {
2613 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002614 }
2615
2616 fec_reset_phy(pdev);
2617
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002618 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00002619 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002620
2621 ret = fec_enet_init(ndev);
2622 if (ret)
2623 goto failed_init;
2624
Xiao Jiangc7c83d12011-09-29 02:15:56 +00002625 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00002626 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00002627 if (irq < 0) {
2628 if (i)
2629 break;
2630 ret = irq;
2631 goto failed_irq;
2632 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002633 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02002634 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002635 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00002636 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00002637 }
2638
Bryan Wue6b043d2010-03-31 02:10:44 +00002639 ret = fec_enet_mii_init(pdev);
2640 if (ret)
2641 goto failed_mii_init;
2642
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002643 /* Carrier starts down, phylib will bring it up */
2644 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002645 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002646 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002647
Sascha Haueread73182009-01-28 23:03:11 +00002648 ret = register_netdev(ndev);
2649 if (ret)
2650 goto failed_register;
2651
Fabio Estevameb1d0642013-04-13 07:25:36 +00002652 if (fep->bufdesc_ex && fep->ptp_clock)
2653 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2654
Frank Li54309fa2013-05-07 14:08:44 +00002655 INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
Sascha Haueread73182009-01-28 23:03:11 +00002656 return 0;
2657
2658failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00002659 fec_enet_mii_remove(fep);
2660failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002661failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002662failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002663 if (fep->reg_phy)
2664 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00002665failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002666 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00002667failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00002668failed_ioremap:
2669 free_netdev(ndev);
2670
2671 return ret;
2672}
2673
Bill Pemberton33897cc2012-12-03 09:23:58 -05002674static int
Sascha Haueread73182009-01-28 23:03:11 +00002675fec_drv_remove(struct platform_device *pdev)
2676{
2677 struct net_device *ndev = platform_get_drvdata(pdev);
2678 struct fec_enet_private *fep = netdev_priv(ndev);
2679
Frank Li54309fa2013-05-07 14:08:44 +00002680 cancel_delayed_work_sync(&(fep->delay_work.delay_work));
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002681 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002682 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00002683 del_timer_sync(&fep->time_keep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002684 if (fep->reg_phy)
2685 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00002686 if (fep->ptp_clock)
2687 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002688 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00002689 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002690
Sascha Haueread73182009-01-28 23:03:11 +00002691 return 0;
2692}
2693
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002694#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00002695static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002696fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002697{
Eric Benard87cad5c2010-06-18 04:19:54 +00002698 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002699 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002700
Russell Kingda1774e2014-07-08 12:39:57 +01002701 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002702 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01002703 phy_stop(fep->phy_dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002704 fec_stop(ndev);
2705 netif_device_detach(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002706 }
Russell Kingda1774e2014-07-08 12:39:57 +01002707 rtnl_unlock();
2708
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002709 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002710 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002711
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002712 if (fep->reg_phy)
2713 regulator_disable(fep->reg_phy);
2714
Sascha Haueread73182009-01-28 23:03:11 +00002715 return 0;
2716}
2717
2718static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002719fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002720{
Eric Benard87cad5c2010-06-18 04:19:54 +00002721 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002722 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002723 int ret;
2724
2725 if (fep->reg_phy) {
2726 ret = regulator_enable(fep->reg_phy);
2727 if (ret)
2728 return ret;
2729 }
Sascha Haueread73182009-01-28 23:03:11 +00002730
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002731 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002732 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002733 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002734 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002735
Russell Kingda1774e2014-07-08 12:39:57 +01002736 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002737 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002738 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002739 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002740 fec_restart(ndev, fep->full_duplex);
2741 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002742 netif_tx_unlock_bh(ndev);
2743 netif_device_attach(ndev);
2744 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01002745 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00002746 }
Russell Kingda1774e2014-07-08 12:39:57 +01002747 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002748
Sascha Haueread73182009-01-28 23:03:11 +00002749 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002750
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002751failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03002752 if (fep->reg_phy)
2753 regulator_disable(fep->reg_phy);
2754 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00002755}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002756#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00002757
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002758static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002759
Sascha Haueread73182009-01-28 23:03:11 +00002760static struct platform_driver fec_driver = {
2761 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002762 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002763 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002764 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002765 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002766 },
Shawn Guob5680e02011-01-05 21:13:13 +00002767 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002768 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002769 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002770};
2771
Fabio Estevamaaca2372012-01-23 16:44:50 +00002772module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03002774MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775MODULE_LICENSE("GPL");