blob: 8cfc3a3de7fbf25c9b6b646277c8a271fd8bbe21 [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 int
346fec_enet_txq_submit_frag_skb(struct sk_buff *skb, struct net_device *ndev)
347{
348 struct fec_enet_private *fep = netdev_priv(ndev);
349 const struct platform_device_id *id_entry =
350 platform_get_device_id(fep->pdev);
351 struct bufdesc *bdp = fep->cur_tx;
352 struct bufdesc_ex *ebdp;
353 int nr_frags = skb_shinfo(skb)->nr_frags;
354 int frag, frag_len;
355 unsigned short status;
356 unsigned int estatus = 0;
357 skb_frag_t *this_frag;
358 unsigned int index;
359 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100360 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800361 int i;
362
363 for (frag = 0; frag < nr_frags; frag++) {
364 this_frag = &skb_shinfo(skb)->frags[frag];
365 bdp = fec_enet_get_nextdesc(bdp, fep);
366 ebdp = (struct bufdesc_ex *)bdp;
367
368 status = bdp->cbd_sc;
369 status &= ~BD_ENET_TX_STATS;
370 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
371 frag_len = skb_shinfo(skb)->frags[frag].size;
372
373 /* Handle the last BD specially */
374 if (frag == nr_frags - 1) {
375 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
376 if (fep->bufdesc_ex) {
377 estatus |= BD_ENET_TX_INT;
378 if (unlikely(skb_shinfo(skb)->tx_flags &
379 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
380 estatus |= BD_ENET_TX_TS;
381 }
382 }
383
384 if (fep->bufdesc_ex) {
385 if (skb->ip_summed == CHECKSUM_PARTIAL)
386 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
387 ebdp->cbd_bdu = 0;
388 ebdp->cbd_esc = estatus;
389 }
390
391 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
392
393 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
394 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
395 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
396 memcpy(fep->tx_bounce[index], bufaddr, frag_len);
397 bufaddr = fep->tx_bounce[index];
398
399 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
400 swap_buffer(bufaddr, frag_len);
401 }
402
Russell Kingd6bf3142014-07-08 00:23:19 +0100403 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
404 DMA_TO_DEVICE);
405 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800406 dev_kfree_skb_any(skb);
407 if (net_ratelimit())
408 netdev_err(ndev, "Tx DMA memory map failed\n");
409 goto dma_mapping_error;
410 }
411
Russell Kingd6bf3142014-07-08 00:23:19 +0100412 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800413 bdp->cbd_datlen = frag_len;
414 bdp->cbd_sc = status;
415 }
416
417 fep->cur_tx = bdp;
418
419 return 0;
420
421dma_mapping_error:
422 bdp = fep->cur_tx;
423 for (i = 0; i < frag; i++) {
424 bdp = fec_enet_get_nextdesc(bdp, fep);
425 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
426 bdp->cbd_datlen, DMA_TO_DEVICE);
427 }
428 return NETDEV_TX_OK;
429}
430
431static int fec_enet_txq_submit_skb(struct sk_buff *skb, struct net_device *ndev)
432{
433 struct fec_enet_private *fep = netdev_priv(ndev);
434 const struct platform_device_id *id_entry =
435 platform_get_device_id(fep->pdev);
436 int nr_frags = skb_shinfo(skb)->nr_frags;
437 struct bufdesc *bdp, *last_bdp;
438 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100439 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800440 unsigned short status;
441 unsigned short buflen;
442 unsigned int estatus = 0;
443 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800444 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800445 int ret;
446
Nimrod Andy79f33912014-06-12 08:16:23 +0800447 entries_free = fec_enet_get_free_txdesc_num(fep);
448 if (entries_free < MAX_SKB_FRAGS + 1) {
449 dev_kfree_skb_any(skb);
450 if (net_ratelimit())
451 netdev_err(ndev, "NOT enough BD for SG!\n");
452 return NETDEV_TX_OK;
453 }
454
Nimrod Andy6e909282014-06-12 08:16:22 +0800455 /* Protocol checksum off-load for TCP and UDP. */
456 if (fec_enet_clear_csum(skb, ndev)) {
457 dev_kfree_skb_any(skb);
458 return NETDEV_TX_OK;
459 }
460
461 /* Fill in a Tx ring entry */
462 bdp = fep->cur_tx;
463 status = bdp->cbd_sc;
464 status &= ~BD_ENET_TX_STATS;
465
466 /* Set buffer length and buffer pointer */
467 bufaddr = skb->data;
468 buflen = skb_headlen(skb);
469
470 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
471 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
472 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
473 memcpy(fep->tx_bounce[index], skb->data, buflen);
474 bufaddr = fep->tx_bounce[index];
475
476 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
477 swap_buffer(bufaddr, buflen);
478 }
479
Russell Kingd6bf3142014-07-08 00:23:19 +0100480 /* Push the data cache so the CPM does not get stale memory data. */
481 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
482 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800483 dev_kfree_skb_any(skb);
484 if (net_ratelimit())
485 netdev_err(ndev, "Tx DMA memory map failed\n");
486 return NETDEV_TX_OK;
487 }
488
489 if (nr_frags) {
490 ret = fec_enet_txq_submit_frag_skb(skb, ndev);
491 if (ret)
492 return ret;
493 } else {
494 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
495 if (fep->bufdesc_ex) {
496 estatus = BD_ENET_TX_INT;
497 if (unlikely(skb_shinfo(skb)->tx_flags &
498 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
499 estatus |= BD_ENET_TX_TS;
500 }
501 }
502
503 if (fep->bufdesc_ex) {
504
505 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
506
507 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
508 fep->hwts_tx_en))
509 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
510
511 if (skb->ip_summed == CHECKSUM_PARTIAL)
512 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
513
514 ebdp->cbd_bdu = 0;
515 ebdp->cbd_esc = estatus;
516 }
517
518 last_bdp = fep->cur_tx;
519 index = fec_enet_get_bd_index(fep->tx_bd_base, last_bdp, fep);
520 /* Save skb pointer */
521 fep->tx_skbuff[index] = skb;
522
523 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100524 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800525
526 /* Send it on its way. Tell FEC it's ready, interrupt when done,
527 * it's the last BD of the frame, and to put the CRC on the end.
528 */
529 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
530 bdp->cbd_sc = status;
531
Sascha Hauer22f6b862009-04-15 01:32:18 +0000532 /* If this was the last BD in the ring, start at the beginning again. */
Nimrod Andy6e909282014-06-12 08:16:22 +0800533 bdp = fec_enet_get_nextdesc(last_bdp, fep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800535 skb_tx_timestamp(skb);
536
Sascha Hauer2e285322009-04-15 01:32:16 +0000537 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Frank Lide5fb0a2013-03-03 17:34:25 +0000539 /* Trigger transmission start */
540 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
541
Nimrod Andy6e909282014-06-12 08:16:22 +0800542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Nimrod Andy79f33912014-06-12 08:16:23 +0800545static int
546fec_enet_txq_put_data_tso(struct sk_buff *skb, struct net_device *ndev,
547 struct bufdesc *bdp, int index, char *data,
548 int size, bool last_tcp, bool is_last)
549{
550 struct fec_enet_private *fep = netdev_priv(ndev);
551 const struct platform_device_id *id_entry =
552 platform_get_device_id(fep->pdev);
553 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
554 unsigned short status;
555 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100556 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800557
558 status = bdp->cbd_sc;
559 status &= ~BD_ENET_TX_STATS;
560
561 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800562
563 if (((unsigned long) data) & FEC_ALIGNMENT ||
564 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
565 memcpy(fep->tx_bounce[index], data, size);
566 data = fep->tx_bounce[index];
567
568 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
569 swap_buffer(data, size);
570 }
571
Russell Kingd6bf3142014-07-08 00:23:19 +0100572 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
573 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800574 dev_kfree_skb_any(skb);
575 if (net_ratelimit())
576 netdev_err(ndev, "Tx DMA memory map failed\n");
577 return NETDEV_TX_BUSY;
578 }
579
Russell Kingd6bf3142014-07-08 00:23:19 +0100580 bdp->cbd_datlen = size;
581 bdp->cbd_bufaddr = addr;
582
Nimrod Andy79f33912014-06-12 08:16:23 +0800583 if (fep->bufdesc_ex) {
584 if (skb->ip_summed == CHECKSUM_PARTIAL)
585 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
586 ebdp->cbd_bdu = 0;
587 ebdp->cbd_esc = estatus;
588 }
589
590 /* Handle the last BD specially */
591 if (last_tcp)
592 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
593 if (is_last) {
594 status |= BD_ENET_TX_INTR;
595 if (fep->bufdesc_ex)
596 ebdp->cbd_esc |= BD_ENET_TX_INT;
597 }
598
599 bdp->cbd_sc = status;
600
601 return 0;
602}
603
604static int
605fec_enet_txq_put_hdr_tso(struct sk_buff *skb, struct net_device *ndev,
606 struct bufdesc *bdp, int index)
607{
608 struct fec_enet_private *fep = netdev_priv(ndev);
609 const struct platform_device_id *id_entry =
610 platform_get_device_id(fep->pdev);
611 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
612 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
613 void *bufaddr;
614 unsigned long dmabuf;
615 unsigned short status;
616 unsigned int estatus = 0;
617
618 status = bdp->cbd_sc;
619 status &= ~BD_ENET_TX_STATS;
620 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
621
622 bufaddr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
623 dmabuf = fep->tso_hdrs_dma + index * TSO_HEADER_SIZE;
624 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
625 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
626 memcpy(fep->tx_bounce[index], skb->data, hdr_len);
627 bufaddr = fep->tx_bounce[index];
628
629 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
630 swap_buffer(bufaddr, hdr_len);
631
632 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
633 hdr_len, DMA_TO_DEVICE);
634 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
635 dev_kfree_skb_any(skb);
636 if (net_ratelimit())
637 netdev_err(ndev, "Tx DMA memory map failed\n");
638 return NETDEV_TX_BUSY;
639 }
640 }
641
642 bdp->cbd_bufaddr = dmabuf;
643 bdp->cbd_datlen = hdr_len;
644
645 if (fep->bufdesc_ex) {
646 if (skb->ip_summed == CHECKSUM_PARTIAL)
647 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
648 ebdp->cbd_bdu = 0;
649 ebdp->cbd_esc = estatus;
650 }
651
652 bdp->cbd_sc = status;
653
654 return 0;
655}
656
657static int fec_enet_txq_submit_tso(struct sk_buff *skb, struct net_device *ndev)
658{
659 struct fec_enet_private *fep = netdev_priv(ndev);
660 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
661 int total_len, data_left;
662 struct bufdesc *bdp = fep->cur_tx;
663 struct tso_t tso;
664 unsigned int index = 0;
665 int ret;
666
667 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep)) {
668 dev_kfree_skb_any(skb);
669 if (net_ratelimit())
670 netdev_err(ndev, "NOT enough BD for TSO!\n");
671 return NETDEV_TX_OK;
672 }
673
674 /* Protocol checksum off-load for TCP and UDP. */
675 if (fec_enet_clear_csum(skb, ndev)) {
676 dev_kfree_skb_any(skb);
677 return NETDEV_TX_OK;
678 }
679
680 /* Initialize the TSO handler, and prepare the first payload */
681 tso_start(skb, &tso);
682
683 total_len = skb->len - hdr_len;
684 while (total_len > 0) {
685 char *hdr;
686
687 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
688 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
689 total_len -= data_left;
690
691 /* prepare packet headers: MAC + IP + TCP */
692 hdr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
693 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
694 ret = fec_enet_txq_put_hdr_tso(skb, ndev, bdp, index);
695 if (ret)
696 goto err_release;
697
698 while (data_left > 0) {
699 int size;
700
701 size = min_t(int, tso.size, data_left);
702 bdp = fec_enet_get_nextdesc(bdp, fep);
703 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
704 ret = fec_enet_txq_put_data_tso(skb, ndev, bdp, index, tso.data,
705 size, size == data_left,
706 total_len == 0);
707 if (ret)
708 goto err_release;
709
710 data_left -= size;
711 tso_build_data(skb, &tso, size);
712 }
713
714 bdp = fec_enet_get_nextdesc(bdp, fep);
715 }
716
717 /* Save skb pointer */
718 fep->tx_skbuff[index] = skb;
719
Nimrod Andy79f33912014-06-12 08:16:23 +0800720 skb_tx_timestamp(skb);
721 fep->cur_tx = bdp;
722
723 /* Trigger transmission start */
724 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
725
726 return 0;
727
728err_release:
729 /* TODO: Release all used data descriptors for TSO */
730 return ret;
731}
732
Nimrod Andy61a44272014-06-12 08:16:18 +0800733static netdev_tx_t
734fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
735{
736 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800737 int entries_free;
Nimrod Andy61a44272014-06-12 08:16:18 +0800738 int ret;
739
Nimrod Andy79f33912014-06-12 08:16:23 +0800740 if (skb_is_gso(skb))
741 ret = fec_enet_txq_submit_tso(skb, ndev);
742 else
743 ret = fec_enet_txq_submit_skb(skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800744 if (ret)
745 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800746
Nimrod Andy6e909282014-06-12 08:16:22 +0800747 entries_free = fec_enet_get_free_txdesc_num(fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800748 if (entries_free <= fep->tx_stop_threshold)
Nimrod Andy61a44272014-06-12 08:16:18 +0800749 netif_stop_queue(ndev);
750
751 return NETDEV_TX_OK;
752}
753
Frank Li14109a52013-03-26 16:12:03 +0000754/* Init RX & TX buffer descriptors
755 */
756static void fec_enet_bd_init(struct net_device *dev)
757{
758 struct fec_enet_private *fep = netdev_priv(dev);
759 struct bufdesc *bdp;
760 unsigned int i;
761
762 /* Initialize the receive buffer descriptors. */
763 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800764 for (i = 0; i < fep->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000765
766 /* Initialize the BD for every fragment in the page. */
767 if (bdp->cbd_bufaddr)
768 bdp->cbd_sc = BD_ENET_RX_EMPTY;
769 else
770 bdp->cbd_sc = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800771 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000772 }
773
774 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800775 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000776 bdp->cbd_sc |= BD_SC_WRAP;
777
778 fep->cur_rx = fep->rx_bd_base;
779
780 /* ...and the same for transmit */
781 bdp = fep->tx_bd_base;
782 fep->cur_tx = bdp;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800783 for (i = 0; i < fep->tx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000784
785 /* Initialize the BD for every fragment in the page. */
786 bdp->cbd_sc = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100787 if (fep->tx_skbuff[i]) {
Frank Li14109a52013-03-26 16:12:03 +0000788 dev_kfree_skb_any(fep->tx_skbuff[i]);
789 fep->tx_skbuff[i] = NULL;
790 }
791 bdp->cbd_bufaddr = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800792 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000793 }
794
795 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800796 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000797 bdp->cbd_sc |= BD_SC_WRAP;
798 fep->dirty_tx = bdp;
799}
800
Russell Kingdbc64a82014-07-08 12:40:12 +0100801/*
802 * This function is called to start or restart the FEC during a link
803 * change, transmit timeout, or to reconfigure the FEC. The network
804 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100805 */
806static void
Russell Kingef833372014-07-08 12:40:43 +0100807fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100808{
809 struct fec_enet_private *fep = netdev_priv(ndev);
810 const struct platform_device_id *id_entry =
811 platform_get_device_id(fep->pdev);
812 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000813 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100814 u32 temp_mac[2];
815 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000816 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100817
818 /* Whack a reset. We should wait for this. */
819 writel(1, fep->hwp + FEC_ECNTRL);
820 udelay(10);
821
822 /*
823 * enet-mac reset will reset mac address registers too,
824 * so need to reconfigure it.
825 */
826 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
827 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
828 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
829 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
830 }
831
832 /* Clear any outstanding interrupt. */
833 writel(0xffc00000, fep->hwp + FEC_IEVENT);
834
Uwe Kleine-König45993652011-01-19 20:47:04 +0100835 /* Set maximum receive buffer size. */
836 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
837
Frank Li14109a52013-03-26 16:12:03 +0000838 fec_enet_bd_init(ndev);
839
Uwe Kleine-König45993652011-01-19 20:47:04 +0100840 /* Set receive and transmit descriptor base. */
841 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000842 if (fep->bufdesc_ex)
843 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800844 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000845 else
846 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800847 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100848
Uwe Kleine-König45993652011-01-19 20:47:04 +0100849
Uwe Kleine-König45993652011-01-19 20:47:04 +0100850 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
851 if (fep->tx_skbuff[i]) {
852 dev_kfree_skb_any(fep->tx_skbuff[i]);
853 fep->tx_skbuff[i] = NULL;
854 }
855 }
856
857 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100858 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100859 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100860 writel(0x04, fep->hwp + FEC_X_CNTRL);
861 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100862 /* No Rcv on Xmit */
863 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100864 writel(0x0, fep->hwp + FEC_X_CNTRL);
865 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100866
Uwe Kleine-König45993652011-01-19 20:47:04 +0100867 /* Set MII speed */
868 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
869
Guenter Roeckd1391932013-06-18 10:04:59 -0700870#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000871 /* set RX checksum */
872 val = readl(fep->hwp + FEC_RACC);
873 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
874 val |= FEC_RACC_OPTIONS;
875 else
876 val &= ~FEC_RACC_OPTIONS;
877 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700878#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000879
Uwe Kleine-König45993652011-01-19 20:47:04 +0100880 /*
881 * The phy interface and speed need to get configured
882 * differently on enet-mac.
883 */
884 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100885 /* Enable flow control and length check */
886 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100887
Shawn Guo230dec62011-09-23 02:12:48 +0000888 /* RGMII, RMII or MII */
889 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
890 rcntl |= (1 << 6);
891 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100892 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100893 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100894 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100895
Shawn Guo230dec62011-09-23 02:12:48 +0000896 /* 1G, 100M or 10M */
897 if (fep->phy_dev) {
898 if (fep->phy_dev->speed == SPEED_1000)
899 ecntl |= (1 << 5);
900 else if (fep->phy_dev->speed == SPEED_100)
901 rcntl &= ~(1 << 9);
902 else
903 rcntl |= (1 << 9);
904 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100905 } else {
906#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800907 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000908 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100909 /* disable the gasket and wait */
910 writel(0, fep->hwp + FEC_MIIGSK_ENR);
911 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
912 udelay(1);
913
914 /*
915 * configure the gasket:
916 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800917 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100918 */
Eric Benard8d82f212012-01-12 06:10:28 +0000919 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
920 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
921 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
922 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
923 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100924
925 /* re-enable the gasket */
926 writel(2, fep->hwp + FEC_MIIGSK_ENR);
927 }
928#endif
929 }
Frank Libaa70a52013-01-16 16:55:58 +0000930
Guenter Roeckd1391932013-06-18 10:04:59 -0700931#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +0000932 /* enable pause frame*/
933 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
934 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
935 fep->phy_dev && fep->phy_dev->pause)) {
936 rcntl |= FEC_ENET_FCE;
937
Jim Baxter4c09eed2013-04-19 08:10:49 +0000938 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000939 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
940 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
941 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
942 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
943
944 /* OPD */
945 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
946 } else {
947 rcntl &= ~FEC_ENET_FCE;
948 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700949#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +0000950
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100951 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100952
Stefan Wahren84fe6182014-03-12 11:28:19 +0100953 /* Setup multicast filter. */
954 set_multicast_list(ndev);
955#ifndef CONFIG_M5272
956 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
957 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
958#endif
959
Shawn Guo230dec62011-09-23 02:12:48 +0000960 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
961 /* enable ENET endian swap */
962 ecntl |= (1 << 8);
963 /* enable ENET store and forward mode */
964 writel(1 << 8, fep->hwp + FEC_X_WMRK);
965 }
966
Frank Liff43da82013-01-03 16:04:23 +0000967 if (fep->bufdesc_ex)
968 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000969
Chris Healy38ae92d2013-06-25 23:18:52 -0700970#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +0100971 /* Enable the MIB statistic event counters */
972 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -0700973#endif
974
Uwe Kleine-König45993652011-01-19 20:47:04 +0100975 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000976 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100977 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
978
Frank Liff43da82013-01-03 16:04:23 +0000979 if (fep->bufdesc_ex)
980 fec_ptp_start_cyclecounter(ndev);
981
Uwe Kleine-König45993652011-01-19 20:47:04 +0100982 /* Enable interrupts we wish to service */
983 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
984}
985
986static void
987fec_stop(struct net_device *ndev)
988{
989 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +0000990 const struct platform_device_id *id_entry =
991 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +0000992 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100993
994 /* We cannot expect a graceful transmit stop without link !!! */
995 if (fep->link) {
996 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
997 udelay(10);
998 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +0000999 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001000 }
1001
1002 /* Whack a reset. We should wait for this. */
1003 writel(1, fep->hwp + FEC_ECNTRL);
1004 udelay(10);
1005 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1006 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001007
1008 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001009 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001010 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001011 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1012 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001013}
1014
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001017fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001019 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001021 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Frank Li54309fa2013-05-07 14:08:44 +00001023 fep->delay_work.timeout = true;
1024 schedule_delayed_work(&(fep->delay_work.delay_work), 0);
1025}
1026
1027static void fec_enet_work(struct work_struct *work)
1028{
1029 struct fec_enet_private *fep =
1030 container_of(work,
1031 struct fec_enet_private,
1032 delay_work.delay_work.work);
Russell King8ce56242014-07-08 12:40:07 +01001033 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001034
1035 if (fep->delay_work.timeout) {
1036 fep->delay_work.timeout = false;
Russell Kingda1774e2014-07-08 12:39:57 +01001037 rtnl_lock();
Russell King8ce56242014-07-08 12:40:07 +01001038 if (netif_device_present(ndev) || netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001039 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001040 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001041 fec_restart(ndev);
Russell King8ce56242014-07-08 12:40:07 +01001042 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001043 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001044 napi_enable(&fep->napi);
Russell King8ce56242014-07-08 12:40:07 +01001045 }
Russell Kingda1774e2014-07-08 12:39:57 +01001046 rtnl_unlock();
Frank Li54309fa2013-05-07 14:08:44 +00001047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001051fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001054 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001055 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +00001057 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001058 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001060 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 bdp = fep->dirty_tx;
1062
Frank Lide5fb0a2013-03-03 17:34:25 +00001063 /* get next bdp of dirty_tx */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001064 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001065
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001066 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001067
1068 /* current queue is empty */
1069 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001070 break;
1071
Nimrod Andy61a44272014-06-12 08:16:18 +08001072 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001073
Frank Lide5fb0a2013-03-03 17:34:25 +00001074 skb = fep->tx_skbuff[index];
Russell Kingd6bf3142014-07-08 00:23:19 +01001075 fep->tx_skbuff[index] = NULL;
Nimrod Andy79f33912014-06-12 08:16:23 +08001076 if (!IS_TSO_HEADER(fep, bdp->cbd_bufaddr))
1077 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1078 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001079 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001080 if (!skb) {
1081 bdp = fec_enet_get_nextdesc(bdp, fep);
1082 continue;
1083 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001086 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 BD_ENET_TX_RL | BD_ENET_TX_UN |
1088 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001089 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001090 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001091 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001092 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001093 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001094 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001095 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001096 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001097 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001098 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001099 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001101 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001102 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
Frank Liff43da82013-01-03 16:04:23 +00001105 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1106 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001107 struct skb_shared_hwtstamps shhwtstamps;
1108 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +00001109 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001110
1111 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1112 spin_lock_irqsave(&fep->tmreg_lock, flags);
1113 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +00001114 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +00001115 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1116 skb_tstamp_tx(skb, &shhwtstamps);
1117 }
Frank Liff43da82013-01-03 16:04:23 +00001118
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001119 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +00001120 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +00001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* Deferred means some collisions occurred during transmit,
1123 * but we eventually sent the packet OK.
1124 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001125 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001126 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001127
Sascha Hauer22f6b862009-04-15 01:32:18 +00001128 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001130
1131 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001132
Sascha Hauer22f6b862009-04-15 01:32:18 +00001133 /* Update pointer to next buffer descriptor to be transmitted */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001134 bdp = fec_enet_get_nextdesc(bdp, fep);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001135
Sascha Hauer22f6b862009-04-15 01:32:18 +00001136 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001138 if (netif_queue_stopped(ndev)) {
1139 entries_free = fec_enet_get_free_txdesc_num(fep);
1140 if (entries_free >= fep->tx_wake_threshold)
1141 netif_wake_queue(ndev);
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Russell Kingccea2962014-07-08 13:01:38 +01001144
1145 /* ERR006538: Keep the transmitter going */
1146 if (bdp != fep->cur_tx && readl(fep->hwp + FEC_X_DES_ACTIVE) == 0)
1147 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150/* During a receive, the cur_rx points to the current incoming buffer.
1151 * When we update through the ring, if the next incoming buffer has
1152 * not been given to the system, we just set the empty indicator,
1153 * effectively tossing the packet.
1154 */
Frank Lidc975382013-01-28 18:31:42 +00001155static int
1156fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001158 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001159 const struct platform_device_id *id_entry =
1160 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001161 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001162 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct sk_buff *skb;
1164 ushort pkt_len;
1165 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001166 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001167 struct bufdesc_ex *ebdp = NULL;
1168 bool vlan_packet_rcvd = false;
1169 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001170 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001171
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001172#ifdef CONFIG_M532x
1173 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* First, grab all of the stats for the incoming packet.
1177 * These get messed up if we get called due to a busy condition.
1178 */
1179 bdp = fep->cur_rx;
1180
Sascha Hauer22f6b862009-04-15 01:32:18 +00001181 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Frank Lidc975382013-01-28 18:31:42 +00001183 if (pkt_received >= budget)
1184 break;
1185 pkt_received++;
1186
Sascha Hauer22f6b862009-04-15 01:32:18 +00001187 /* Since we have allocated space to hold a complete frame,
1188 * the last indicator should be set.
1189 */
1190 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001191 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Sascha Hauer22f6b862009-04-15 01:32:18 +00001193 /* Check for errors. */
1194 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001196 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001197 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1198 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001199 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001200 }
1201 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001202 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001203 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001204 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001205 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001206 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001208
1209 /* Report late collisions as a frame error.
1210 * On this error, the BD is closed, but we don't know what we
1211 * have in the buffer. So, just drop this frame on the floor.
1212 */
1213 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001214 ndev->stats.rx_errors++;
1215 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001216 goto rx_processing_done;
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Sascha Hauer22f6b862009-04-15 01:32:18 +00001219 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001220 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001221 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001222 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Nimrod Andy61a44272014-06-12 08:16:18 +08001224 index = fec_enet_get_bd_index(fep->rx_bd_base, bdp, fep);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001225 data = fep->rx_skbuff[index]->data;
1226 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1227 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001228
Shawn Guob5680e02011-01-05 21:13:13 +00001229 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1230 swap_buffer(data, pkt_len);
1231
Jim Baxtercdffcf12013-07-02 22:52:56 +01001232 /* Extract the enhanced buffer descriptor */
1233 ebdp = NULL;
1234 if (fep->bufdesc_ex)
1235 ebdp = (struct bufdesc_ex *)bdp;
1236
1237 /* If this is a VLAN packet remove the VLAN Tag */
1238 vlan_packet_rcvd = false;
1239 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1240 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
1241 /* Push and remove the vlan tag */
1242 struct vlan_hdr *vlan_header =
1243 (struct vlan_hdr *) (data + ETH_HLEN);
1244 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1245 pkt_len -= VLAN_HLEN;
1246
1247 vlan_packet_rcvd = true;
1248 }
1249
Sascha Hauer22f6b862009-04-15 01:32:18 +00001250 /* This does 16 byte alignment, exactly what we need.
1251 * The packet length includes FCS, but we don't want to
1252 * include that when passing upstream as it messes up
1253 * bridging applications.
1254 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001255 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Sascha Hauer85498892009-04-15 01:32:21 +00001257 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001258 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001259 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001260 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001261 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001262 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001263
1264 /* Extract the frame data without the VLAN header. */
1265 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1266 if (vlan_packet_rcvd)
1267 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
1268 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
1269 data + payload_offset,
1270 pkt_len - 4 - (2 * ETH_ALEN));
1271
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001272 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001273
Frank Li6605b732012-10-30 18:25:31 +00001274 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +00001275 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001276 struct skb_shared_hwtstamps *shhwtstamps =
1277 skb_hwtstamps(skb);
1278 unsigned long flags;
1279
1280 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1281
1282 spin_lock_irqsave(&fep->tmreg_lock, flags);
1283 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +00001284 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +00001285 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1286 }
Frank Liff43da82013-01-03 16:04:23 +00001287
Jim Baxter4c09eed2013-04-19 08:10:49 +00001288 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001289 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001290 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1291 /* don't check it */
1292 skb->ip_summed = CHECKSUM_UNNECESSARY;
1293 } else {
1294 skb_checksum_none_assert(skb);
1295 }
1296 }
1297
Jim Baxtercdffcf12013-07-02 22:52:56 +01001298 /* Handle received VLAN packets */
1299 if (vlan_packet_rcvd)
1300 __vlan_hwaccel_put_tag(skb,
1301 htons(ETH_P_8021Q),
1302 vlan_tag);
1303
Richard Cochran0affdf32013-08-30 20:28:10 +02001304 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001305 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001306
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001307 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1308 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001309rx_processing_done:
1310 /* Clear the status flags for this buffer */
1311 status &= ~BD_ENET_RX_STATS;
1312
1313 /* Mark the buffer empty */
1314 status |= BD_ENET_RX_EMPTY;
1315 bdp->cbd_sc = status;
1316
Frank Liff43da82013-01-03 16:04:23 +00001317 if (fep->bufdesc_ex) {
1318 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1319
1320 ebdp->cbd_esc = BD_ENET_RX_INT;
1321 ebdp->cbd_prot = 0;
1322 ebdp->cbd_bdu = 0;
1323 }
Frank Li6605b732012-10-30 18:25:31 +00001324
Sascha Hauer22f6b862009-04-15 01:32:18 +00001325 /* Update BD pointer to next entry */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001326 bdp = fec_enet_get_nextdesc(bdp, fep);
1327
Sascha Hauer22f6b862009-04-15 01:32:18 +00001328 /* Doing this here will keep the FEC running while we process
1329 * incoming frames. On a heavily loaded network, we should be
1330 * able to keep up at the expense of system resources.
1331 */
1332 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Sascha Hauer2e285322009-04-15 01:32:16 +00001334 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Frank Lidc975382013-01-28 18:31:42 +00001336 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Uwe Kleine-König45993652011-01-19 20:47:04 +01001339static irqreturn_t
1340fec_enet_interrupt(int irq, void *dev_id)
1341{
1342 struct net_device *ndev = dev_id;
1343 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001344 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001345 uint int_events;
1346 irqreturn_t ret = IRQ_NONE;
1347
Russell King7a168072014-07-08 00:22:44 +01001348 int_events = readl(fep->hwp + FEC_IEVENT);
1349 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001350
Russell King7a168072014-07-08 00:22:44 +01001351 if (int_events & napi_mask) {
1352 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001353
Russell King7a168072014-07-08 00:22:44 +01001354 /* Disable the NAPI interrupts */
1355 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1356 napi_schedule(&fep->napi);
1357 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001358
Russell King7a168072014-07-08 00:22:44 +01001359 if (int_events & FEC_ENET_MII) {
1360 ret = IRQ_HANDLED;
1361 complete(&fep->mdio_done);
1362 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001363
1364 return ret;
1365}
1366
Frank Lidc975382013-01-28 18:31:42 +00001367static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1368{
1369 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001370 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001371 int pkts;
1372
1373 /*
1374 * Clear any pending transmit or receive interrupts before
1375 * processing the rings to avoid racing with the hardware.
1376 */
1377 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1378
1379 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001380
Frank Lide5fb0a2013-03-03 17:34:25 +00001381 fec_enet_tx(ndev);
1382
Frank Lidc975382013-01-28 18:31:42 +00001383 if (pkts < budget) {
1384 napi_complete(napi);
1385 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1386 }
1387 return pkts;
1388}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001391static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001393 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001394 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001395 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Shawn Guo49da97d2011-01-05 21:13:11 +00001397 /*
1398 * try to get mac address in following order:
1399 *
1400 * 1) module parameter via kernel command line in form
1401 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1402 */
1403 iap = macaddr;
1404
1405 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001406 * 2) from device tree data
1407 */
1408 if (!is_valid_ether_addr(iap)) {
1409 struct device_node *np = fep->pdev->dev.of_node;
1410 if (np) {
1411 const char *mac = of_get_mac_address(np);
1412 if (mac)
1413 iap = (unsigned char *) mac;
1414 }
1415 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001416
1417 /*
1418 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001419 */
1420 if (!is_valid_ether_addr(iap)) {
1421#ifdef CONFIG_M5272
1422 if (FEC_FLASHMAC)
1423 iap = (unsigned char *)FEC_FLASHMAC;
1424#else
1425 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001426 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001427#endif
1428 }
1429
1430 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001431 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001432 */
1433 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001434 *((__be32 *) &tmpaddr[0]) =
1435 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1436 *((__be16 *) &tmpaddr[4]) =
1437 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 iap = &tmpaddr[0];
1439 }
1440
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001441 /*
1442 * 5) random mac address
1443 */
1444 if (!is_valid_ether_addr(iap)) {
1445 /* Report it and use a random ethernet address instead */
1446 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1447 eth_hw_addr_random(ndev);
1448 netdev_info(ndev, "Using random MAC address: %pM\n",
1449 ndev->dev_addr);
1450 return;
1451 }
1452
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001453 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Shawn Guo49da97d2011-01-05 21:13:11 +00001455 /* Adjust MAC if using macaddr */
1456 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001457 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460/* ------------------------------------------------------------------------- */
1461
Bryan Wue6b043d2010-03-31 02:10:44 +00001462/*
1463 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001465static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001467 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001468 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001469 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Bryan Wue6b043d2010-03-31 02:10:44 +00001471 /* Prevent a state halted on mii error */
1472 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1473 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001476
Russell King8ce56242014-07-08 12:40:07 +01001477 /*
1478 * If the netdev is down, or is going down, we're not interested
1479 * in link state events, so just mark our idea of the link as down
1480 * and ignore the event.
1481 */
1482 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1483 fep->link = 0;
1484 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001485 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001486 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001487 status_change = 1;
1488 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001489
Russell Kingef833372014-07-08 12:40:43 +01001490 if (fep->full_duplex != phy_dev->duplex) {
1491 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001492 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001493 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001494
1495 if (phy_dev->speed != fep->speed) {
1496 fep->speed = phy_dev->speed;
1497 status_change = 1;
1498 }
1499
1500 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001501 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001502 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001503 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001504 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001505 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001506 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001507 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001508 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001509 } else {
1510 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001511 napi_disable(&fep->napi);
1512 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001513 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001514 netif_tx_unlock_bh(ndev);
1515 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001516 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001517 status_change = 1;
1518 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001519 }
1520
Bryan Wue6b043d2010-03-31 02:10:44 +00001521 if (status_change)
1522 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Bryan Wue6b043d2010-03-31 02:10:44 +00001525static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Bryan Wue6b043d2010-03-31 02:10:44 +00001527 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001528 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001529
1530 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001531 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001532
1533 /* start a read op */
1534 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1535 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1536 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1537
1538 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001539 time_left = wait_for_completion_timeout(&fep->mdio_done,
1540 usecs_to_jiffies(FEC_MII_TIMEOUT));
1541 if (time_left == 0) {
1542 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001543 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001544 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001545 }
1546
1547 /* return value */
1548 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1549}
1550
1551static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1552 u16 value)
1553{
1554 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001555 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001556
1557 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001558 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001559
Shawn Guo862f0982011-01-05 21:13:09 +00001560 /* start a write op */
1561 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001562 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1563 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1564 fep->hwp + FEC_MII_DATA);
1565
1566 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001567 time_left = wait_for_completion_timeout(&fep->mdio_done,
1568 usecs_to_jiffies(FEC_MII_TIMEOUT));
1569 if (time_left == 0) {
1570 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001571 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001572 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001573 }
1574
1575 return 0;
1576}
1577
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001578static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1579{
1580 struct fec_enet_private *fep = netdev_priv(ndev);
1581 int ret;
1582
1583 if (enable) {
1584 ret = clk_prepare_enable(fep->clk_ahb);
1585 if (ret)
1586 return ret;
1587 ret = clk_prepare_enable(fep->clk_ipg);
1588 if (ret)
1589 goto failed_clk_ipg;
1590 if (fep->clk_enet_out) {
1591 ret = clk_prepare_enable(fep->clk_enet_out);
1592 if (ret)
1593 goto failed_clk_enet_out;
1594 }
1595 if (fep->clk_ptp) {
1596 ret = clk_prepare_enable(fep->clk_ptp);
1597 if (ret)
1598 goto failed_clk_ptp;
1599 }
1600 } else {
1601 clk_disable_unprepare(fep->clk_ahb);
1602 clk_disable_unprepare(fep->clk_ipg);
1603 if (fep->clk_enet_out)
1604 clk_disable_unprepare(fep->clk_enet_out);
1605 if (fep->clk_ptp)
1606 clk_disable_unprepare(fep->clk_ptp);
1607 }
1608
1609 return 0;
1610failed_clk_ptp:
1611 if (fep->clk_enet_out)
1612 clk_disable_unprepare(fep->clk_enet_out);
1613failed_clk_enet_out:
1614 clk_disable_unprepare(fep->clk_ipg);
1615failed_clk_ipg:
1616 clk_disable_unprepare(fep->clk_ahb);
1617
1618 return ret;
1619}
1620
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001621static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001622{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001623 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001624 const struct platform_device_id *id_entry =
1625 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001626 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001627 char mdio_bus_id[MII_BUS_ID_SIZE];
1628 char phy_name[MII_BUS_ID_SIZE + 3];
1629 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001630 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001631
Bryan Wu418bd0d2010-05-28 03:40:39 -07001632 fep->phy_dev = NULL;
1633
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001634 /* check for attached phy */
1635 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1636 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1637 continue;
1638 if (fep->mii_bus->phy_map[phy_id] == NULL)
1639 continue;
1640 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1641 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001642 if (dev_id--)
1643 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001644 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1645 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001646 }
1647
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001648 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001649 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001650 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001651 phy_id = 0;
1652 }
1653
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001654 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001655 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001656 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001657 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001658 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001659 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001660 }
1661
1662 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001663 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001664 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001665 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001666#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001667 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001668#endif
Frank Libaa70a52013-01-16 16:55:58 +00001669 }
Shawn Guo230dec62011-09-23 02:12:48 +00001670 else
1671 phy_dev->supported &= PHY_BASIC_FEATURES;
1672
Bryan Wue6b043d2010-03-31 02:10:44 +00001673 phy_dev->advertising = phy_dev->supported;
1674
1675 fep->phy_dev = phy_dev;
1676 fep->link = 0;
1677 fep->full_duplex = 0;
1678
Joe Perches31b77202013-04-13 19:03:17 +00001679 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1680 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1681 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001682
Bryan Wue6b043d2010-03-31 02:10:44 +00001683 return 0;
1684}
1685
1686static int fec_enet_mii_init(struct platform_device *pdev)
1687{
Shawn Guob5680e02011-01-05 21:13:13 +00001688 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001689 struct net_device *ndev = platform_get_drvdata(pdev);
1690 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001691 const struct platform_device_id *id_entry =
1692 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001693 int err = -ENXIO, i;
1694
Shawn Guob5680e02011-01-05 21:13:13 +00001695 /*
1696 * The dual fec interfaces are not equivalent with enet-mac.
1697 * Here are the differences:
1698 *
1699 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1700 * - fec0 acts as the 1588 time master while fec1 is slave
1701 * - external phys can only be configured by fec0
1702 *
1703 * That is to say fec1 can not work independently. It only works
1704 * when fec0 is working. The reason behind this design is that the
1705 * second interface is added primarily for Switch mode.
1706 *
1707 * Because of the last point above, both phys are attached on fec0
1708 * mdio interface in board design, and need to be configured by
1709 * fec0 mii_bus.
1710 */
Shawn Guo43af9402011-12-05 05:01:15 +00001711 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001712 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001713 if (mii_cnt && fec0_mii_bus) {
1714 fep->mii_bus = fec0_mii_bus;
1715 mii_cnt++;
1716 return 0;
1717 }
1718 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001719 }
1720
Bryan Wue6b043d2010-03-31 02:10:44 +00001721 fep->mii_timeout = 0;
1722
1723 /*
1724 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001725 *
1726 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1727 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1728 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1729 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001730 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001731 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001732 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1733 fep->phy_speed--;
1734 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001735 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1736
1737 fep->mii_bus = mdiobus_alloc();
1738 if (fep->mii_bus == NULL) {
1739 err = -ENOMEM;
1740 goto err_out;
1741 }
1742
1743 fep->mii_bus->name = "fec_enet_mii_bus";
1744 fep->mii_bus->read = fec_enet_mdio_read;
1745 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001746 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1747 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001748 fep->mii_bus->priv = fep;
1749 fep->mii_bus->parent = &pdev->dev;
1750
1751 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1752 if (!fep->mii_bus->irq) {
1753 err = -ENOMEM;
1754 goto err_out_free_mdiobus;
1755 }
1756
1757 for (i = 0; i < PHY_MAX_ADDR; i++)
1758 fep->mii_bus->irq[i] = PHY_POLL;
1759
Bryan Wue6b043d2010-03-31 02:10:44 +00001760 if (mdiobus_register(fep->mii_bus))
1761 goto err_out_free_mdio_irq;
1762
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001763 mii_cnt++;
1764
Shawn Guob5680e02011-01-05 21:13:13 +00001765 /* save fec0 mii_bus */
1766 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1767 fec0_mii_bus = fep->mii_bus;
1768
Bryan Wue6b043d2010-03-31 02:10:44 +00001769 return 0;
1770
Bryan Wue6b043d2010-03-31 02:10:44 +00001771err_out_free_mdio_irq:
1772 kfree(fep->mii_bus->irq);
1773err_out_free_mdiobus:
1774 mdiobus_free(fep->mii_bus);
1775err_out:
1776 return err;
1777}
1778
1779static void fec_enet_mii_remove(struct fec_enet_private *fep)
1780{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001781 if (--mii_cnt == 0) {
1782 mdiobus_unregister(fep->mii_bus);
1783 kfree(fep->mii_bus->irq);
1784 mdiobus_free(fep->mii_bus);
1785 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001786}
1787
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001788static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001789 struct ethtool_cmd *cmd)
1790{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001791 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001792 struct phy_device *phydev = fep->phy_dev;
1793
1794 if (!phydev)
1795 return -ENODEV;
1796
1797 return phy_ethtool_gset(phydev, cmd);
1798}
1799
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001800static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001801 struct ethtool_cmd *cmd)
1802{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001803 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001804 struct phy_device *phydev = fep->phy_dev;
1805
1806 if (!phydev)
1807 return -ENODEV;
1808
1809 return phy_ethtool_sset(phydev, cmd);
1810}
1811
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001812static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001813 struct ethtool_drvinfo *info)
1814{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001815 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Jiri Pirko7826d432013-01-06 00:44:26 +00001817 strlcpy(info->driver, fep->pdev->dev.driver->name,
1818 sizeof(info->driver));
1819 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1820 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
Bryan Wue6b043d2010-03-31 02:10:44 +00001822
Frank Li5ebae4892013-01-06 16:25:07 +00001823static int fec_enet_get_ts_info(struct net_device *ndev,
1824 struct ethtool_ts_info *info)
1825{
1826 struct fec_enet_private *fep = netdev_priv(ndev);
1827
1828 if (fep->bufdesc_ex) {
1829
1830 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1831 SOF_TIMESTAMPING_RX_SOFTWARE |
1832 SOF_TIMESTAMPING_SOFTWARE |
1833 SOF_TIMESTAMPING_TX_HARDWARE |
1834 SOF_TIMESTAMPING_RX_HARDWARE |
1835 SOF_TIMESTAMPING_RAW_HARDWARE;
1836 if (fep->ptp_clock)
1837 info->phc_index = ptp_clock_index(fep->ptp_clock);
1838 else
1839 info->phc_index = -1;
1840
1841 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1842 (1 << HWTSTAMP_TX_ON);
1843
1844 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1845 (1 << HWTSTAMP_FILTER_ALL);
1846 return 0;
1847 } else {
1848 return ethtool_op_get_ts_info(ndev, info);
1849 }
1850}
1851
Guenter Roeckd1391932013-06-18 10:04:59 -07001852#if !defined(CONFIG_M5272)
1853
Frank Libaa70a52013-01-16 16:55:58 +00001854static void fec_enet_get_pauseparam(struct net_device *ndev,
1855 struct ethtool_pauseparam *pause)
1856{
1857 struct fec_enet_private *fep = netdev_priv(ndev);
1858
1859 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1860 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1861 pause->rx_pause = pause->tx_pause;
1862}
1863
1864static int fec_enet_set_pauseparam(struct net_device *ndev,
1865 struct ethtool_pauseparam *pause)
1866{
1867 struct fec_enet_private *fep = netdev_priv(ndev);
1868
Russell King0b146ca2014-07-08 00:22:59 +01001869 if (!fep->phy_dev)
1870 return -ENODEV;
1871
Frank Libaa70a52013-01-16 16:55:58 +00001872 if (pause->tx_pause != pause->rx_pause) {
1873 netdev_info(ndev,
1874 "hardware only support enable/disable both tx and rx");
1875 return -EINVAL;
1876 }
1877
1878 fep->pause_flag = 0;
1879
1880 /* tx pause must be same as rx pause */
1881 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1882 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1883
1884 if (pause->rx_pause || pause->autoneg) {
1885 fep->phy_dev->supported |= ADVERTISED_Pause;
1886 fep->phy_dev->advertising |= ADVERTISED_Pause;
1887 } else {
1888 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1889 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1890 }
1891
1892 if (pause->autoneg) {
1893 if (netif_running(ndev))
1894 fec_stop(ndev);
1895 phy_start_aneg(fep->phy_dev);
1896 }
Russell Kingdbc64a82014-07-08 12:40:12 +01001897 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001898 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001899 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001900 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001901 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001902 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001903 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001904 }
Frank Libaa70a52013-01-16 16:55:58 +00001905
1906 return 0;
1907}
1908
Chris Healy38ae92d2013-06-25 23:18:52 -07001909static const struct fec_stat {
1910 char name[ETH_GSTRING_LEN];
1911 u16 offset;
1912} fec_stats[] = {
1913 /* RMON TX */
1914 { "tx_dropped", RMON_T_DROP },
1915 { "tx_packets", RMON_T_PACKETS },
1916 { "tx_broadcast", RMON_T_BC_PKT },
1917 { "tx_multicast", RMON_T_MC_PKT },
1918 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1919 { "tx_undersize", RMON_T_UNDERSIZE },
1920 { "tx_oversize", RMON_T_OVERSIZE },
1921 { "tx_fragment", RMON_T_FRAG },
1922 { "tx_jabber", RMON_T_JAB },
1923 { "tx_collision", RMON_T_COL },
1924 { "tx_64byte", RMON_T_P64 },
1925 { "tx_65to127byte", RMON_T_P65TO127 },
1926 { "tx_128to255byte", RMON_T_P128TO255 },
1927 { "tx_256to511byte", RMON_T_P256TO511 },
1928 { "tx_512to1023byte", RMON_T_P512TO1023 },
1929 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
1930 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
1931 { "tx_octets", RMON_T_OCTETS },
1932
1933 /* IEEE TX */
1934 { "IEEE_tx_drop", IEEE_T_DROP },
1935 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
1936 { "IEEE_tx_1col", IEEE_T_1COL },
1937 { "IEEE_tx_mcol", IEEE_T_MCOL },
1938 { "IEEE_tx_def", IEEE_T_DEF },
1939 { "IEEE_tx_lcol", IEEE_T_LCOL },
1940 { "IEEE_tx_excol", IEEE_T_EXCOL },
1941 { "IEEE_tx_macerr", IEEE_T_MACERR },
1942 { "IEEE_tx_cserr", IEEE_T_CSERR },
1943 { "IEEE_tx_sqe", IEEE_T_SQE },
1944 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
1945 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
1946
1947 /* RMON RX */
1948 { "rx_packets", RMON_R_PACKETS },
1949 { "rx_broadcast", RMON_R_BC_PKT },
1950 { "rx_multicast", RMON_R_MC_PKT },
1951 { "rx_crc_errors", RMON_R_CRC_ALIGN },
1952 { "rx_undersize", RMON_R_UNDERSIZE },
1953 { "rx_oversize", RMON_R_OVERSIZE },
1954 { "rx_fragment", RMON_R_FRAG },
1955 { "rx_jabber", RMON_R_JAB },
1956 { "rx_64byte", RMON_R_P64 },
1957 { "rx_65to127byte", RMON_R_P65TO127 },
1958 { "rx_128to255byte", RMON_R_P128TO255 },
1959 { "rx_256to511byte", RMON_R_P256TO511 },
1960 { "rx_512to1023byte", RMON_R_P512TO1023 },
1961 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
1962 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
1963 { "rx_octets", RMON_R_OCTETS },
1964
1965 /* IEEE RX */
1966 { "IEEE_rx_drop", IEEE_R_DROP },
1967 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
1968 { "IEEE_rx_crc", IEEE_R_CRC },
1969 { "IEEE_rx_align", IEEE_R_ALIGN },
1970 { "IEEE_rx_macerr", IEEE_R_MACERR },
1971 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
1972 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
1973};
1974
1975static void fec_enet_get_ethtool_stats(struct net_device *dev,
1976 struct ethtool_stats *stats, u64 *data)
1977{
1978 struct fec_enet_private *fep = netdev_priv(dev);
1979 int i;
1980
1981 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1982 data[i] = readl(fep->hwp + fec_stats[i].offset);
1983}
1984
1985static void fec_enet_get_strings(struct net_device *netdev,
1986 u32 stringset, u8 *data)
1987{
1988 int i;
1989 switch (stringset) {
1990 case ETH_SS_STATS:
1991 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1992 memcpy(data + i * ETH_GSTRING_LEN,
1993 fec_stats[i].name, ETH_GSTRING_LEN);
1994 break;
1995 }
1996}
1997
1998static int fec_enet_get_sset_count(struct net_device *dev, int sset)
1999{
2000 switch (sset) {
2001 case ETH_SS_STATS:
2002 return ARRAY_SIZE(fec_stats);
2003 default:
2004 return -EOPNOTSUPP;
2005 }
2006}
Guenter Roeckd1391932013-06-18 10:04:59 -07002007#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002008
Chris Healy32bc9b42013-06-17 07:25:06 -07002009static int fec_enet_nway_reset(struct net_device *dev)
2010{
2011 struct fec_enet_private *fep = netdev_priv(dev);
2012 struct phy_device *phydev = fep->phy_dev;
2013
2014 if (!phydev)
2015 return -ENODEV;
2016
2017 return genphy_restart_aneg(phydev);
2018}
2019
stephen hemminger9b07be42012-01-04 12:59:49 +00002020static const struct ethtool_ops fec_enet_ethtool_ops = {
Guenter Roeckd1391932013-06-18 10:04:59 -07002021#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002022 .get_pauseparam = fec_enet_get_pauseparam,
2023 .set_pauseparam = fec_enet_set_pauseparam,
Guenter Roeckd1391932013-06-18 10:04:59 -07002024#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00002025 .get_settings = fec_enet_get_settings,
2026 .set_settings = fec_enet_set_settings,
2027 .get_drvinfo = fec_enet_get_drvinfo,
2028 .get_link = ethtool_op_get_link,
Frank Li5ebae4892013-01-06 16:25:07 +00002029 .get_ts_info = fec_enet_get_ts_info,
Chris Healy32bc9b42013-06-17 07:25:06 -07002030 .nway_reset = fec_enet_nway_reset,
Chris Healy38ae92d2013-06-25 23:18:52 -07002031#ifndef CONFIG_M5272
2032 .get_ethtool_stats = fec_enet_get_ethtool_stats,
2033 .get_strings = fec_enet_get_strings,
2034 .get_sset_count = fec_enet_get_sset_count,
2035#endif
Bryan Wue6b043d2010-03-31 02:10:44 +00002036};
2037
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002038static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002039{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002040 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002041 struct phy_device *phydev = fep->phy_dev;
2042
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002043 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002044 return -EINVAL;
2045
2046 if (!phydev)
2047 return -ENODEV;
2048
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002049 if (fep->bufdesc_ex) {
2050 if (cmd == SIOCSHWTSTAMP)
2051 return fec_ptp_set(ndev, rq);
2052 if (cmd == SIOCGHWTSTAMP)
2053 return fec_ptp_get(ndev, rq);
2054 }
Frank Liff43da82013-01-03 16:04:23 +00002055
Richard Cochran28b04112010-07-17 08:48:55 +00002056 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002059static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002060{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002061 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002062 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002063 struct sk_buff *skb;
2064 struct bufdesc *bdp;
2065
2066 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002067 for (i = 0; i < fep->rx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002068 skb = fep->rx_skbuff[i];
Russell King730ee362014-07-08 00:23:14 +01002069 fep->rx_skbuff[i] = NULL;
2070 if (skb) {
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01002071 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002072 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002073 dev_kfree_skb(skb);
Russell King730ee362014-07-08 00:23:14 +01002074 }
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002075 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002076 }
2077
2078 bdp = fep->tx_bd_base;
Russell King8b7c9ef2014-07-08 00:23:25 +01002079 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002080 kfree(fep->tx_bounce[i]);
Russell King8b7c9ef2014-07-08 00:23:25 +01002081 fep->tx_bounce[i] = NULL;
2082 skb = fep->tx_skbuff[i];
2083 fep->tx_skbuff[i] = NULL;
2084 dev_kfree_skb(skb);
2085 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002086}
2087
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002088static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002089{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002090 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002091 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002092 struct sk_buff *skb;
2093 struct bufdesc *bdp;
2094
2095 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002096 for (i = 0; i < fep->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002097 dma_addr_t addr;
2098
Fabio Estevamb72061a2012-02-07 08:27:31 +00002099 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002100 if (!skb)
2101 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002102
Russell King730ee362014-07-08 00:23:14 +01002103 addr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002104 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Russell King730ee362014-07-08 00:23:14 +01002105 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2106 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002107 if (net_ratelimit())
2108 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002109 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002110 }
Russell King730ee362014-07-08 00:23:14 +01002111
2112 fep->rx_skbuff[i] = skb;
2113 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002114 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002115
2116 if (fep->bufdesc_ex) {
2117 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2118 ebdp->cbd_esc = BD_ENET_RX_INT;
2119 }
2120
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002121 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002122 }
2123
2124 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002125 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002126 bdp->cbd_sc |= BD_SC_WRAP;
2127
2128 bdp = fep->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002129 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002130 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
Russell Kingffdce2c2014-07-08 00:23:30 +01002131 if (!fep->tx_bounce[i])
2132 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002133
2134 bdp->cbd_sc = 0;
2135 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002136
Frank Liff43da82013-01-03 16:04:23 +00002137 if (fep->bufdesc_ex) {
2138 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002139 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002140 }
2141
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002142 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002143 }
2144
2145 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002146 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002147 bdp->cbd_sc |= BD_SC_WRAP;
2148
2149 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002150
2151 err_alloc:
2152 fec_enet_free_buffers(ndev);
2153 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002157fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002159 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002160 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002162 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002163 ret = fec_enet_clk_enable(ndev, true);
2164 if (ret)
2165 return ret;
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 /* I should reset the ring buffers here, but I don't yet know
2168 * a simple way to do that.
2169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002171 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002172 if (ret)
2173 return ret;
2174
Bryan Wu418bd0d2010-05-28 03:40:39 -07002175 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002176 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002177 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002178 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002179 return ret;
2180 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002181
Russell Kingef833372014-07-08 12:40:43 +01002182 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002183 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002184 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002185 netif_start_queue(ndev);
Sascha Hauer22f6b862009-04-15 01:32:18 +00002186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
2189static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002190fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002192 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Russell Kingd76cfae2014-07-08 00:23:04 +01002194 phy_stop(fep->phy_dev);
2195
Russell King31a6de32014-07-08 12:40:23 +01002196 if (netif_device_present(ndev)) {
2197 napi_disable(&fep->napi);
2198 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002199 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Russell King635cf172014-07-08 00:22:54 +01002202 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002203 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002204
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002205 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002206 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002207 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return 0;
2210}
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212/* Set or clear the multicast filter for this adaptor.
2213 * Skeleton taken from sunlance driver.
2214 * The CPM Ethernet implementation allows Multicast as well as individual
2215 * MAC address filtering. Some of the drivers check to make sure it is
2216 * a group multicast address, and discard those that are not. I guess I
2217 * will do the same for now, but just remove the test if you want
2218 * individual filtering as well (do the upper net layers want or support
2219 * this kind of feature?).
2220 */
2221
2222#define HASH_BITS 6 /* #bits in hash */
2223#define CRC32_POLY 0xEDB88320
2224
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002225static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002227 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002228 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002229 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 unsigned char hash;
2231
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002232 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002233 tmp = readl(fep->hwp + FEC_R_CNTRL);
2234 tmp |= 0x8;
2235 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002236 return;
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Sascha Hauer4e831832009-04-15 01:32:19 +00002239 tmp = readl(fep->hwp + FEC_R_CNTRL);
2240 tmp &= ~0x8;
2241 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002242
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002243 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002244 /* Catch all multicast addresses, so set the
2245 * filter to all 1's
2246 */
2247 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2248 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Sascha Hauer4e831832009-04-15 01:32:19 +00002250 return;
2251 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002252
Sascha Hauer4e831832009-04-15 01:32:19 +00002253 /* Clear filter and add the addresses in hash register
2254 */
2255 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2256 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002258 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002259 /* calculate crc32 value of mac address */
2260 crc = 0xffffffff;
2261
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002262 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002263 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002264 for (bit = 0; bit < 8; bit++, data >>= 1) {
2265 crc = (crc >> 1) ^
2266 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002269
2270 /* only upper 6 bits (HASH_BITS) are used
2271 * which point to specific bit in he hash registers
2272 */
2273 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2274
2275 if (hash > 31) {
2276 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2277 tmp |= 1 << (hash - 32);
2278 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2279 } else {
2280 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2281 tmp |= 1 << hash;
2282 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285}
2286
Sascha Hauer22f6b862009-04-15 01:32:18 +00002287/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002288static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002289fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002291 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002292 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Lucas Stach44934fa2014-03-30 21:32:08 +02002294 if (addr) {
2295 if (!is_valid_ether_addr(addr->sa_data))
2296 return -EADDRNOTAVAIL;
2297 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2298 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002299
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002300 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2301 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002302 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002303 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002304 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002308#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002309/**
2310 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002311 * @dev: The FEC network adapter
2312 *
2313 * Polled functionality used by netconsole and others in non interrupt mode
2314 *
2315 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002316static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002317{
2318 int i;
2319 struct fec_enet_private *fep = netdev_priv(dev);
2320
2321 for (i = 0; i < FEC_IRQ_NUM; i++) {
2322 if (fep->irq[i] > 0) {
2323 disable_irq(fep->irq[i]);
2324 fec_enet_interrupt(fep->irq[i], dev);
2325 enable_irq(fep->irq[i]);
2326 }
2327 }
2328}
2329#endif
2330
Russell King8506fa12014-07-08 12:40:33 +01002331#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2332
Jim Baxter4c09eed2013-04-19 08:10:49 +00002333static int fec_set_features(struct net_device *netdev,
2334 netdev_features_t features)
2335{
2336 struct fec_enet_private *fep = netdev_priv(netdev);
2337 netdev_features_t changed = features ^ netdev->features;
2338
Russell King8506fa12014-07-08 12:40:33 +01002339 /* Quiesce the device if necessary */
2340 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2341 napi_disable(&fep->napi);
2342 netif_tx_lock_bh(netdev);
2343 fec_stop(netdev);
2344 }
2345
Jim Baxter4c09eed2013-04-19 08:10:49 +00002346 netdev->features = features;
2347
2348 /* Receive checksum has been changed */
2349 if (changed & NETIF_F_RXCSUM) {
2350 if (features & NETIF_F_RXCSUM)
2351 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2352 else
2353 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002354 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002355
Russell King8506fa12014-07-08 12:40:33 +01002356 /* Resume the device after updates */
2357 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002358 fec_restart(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002359 netif_wake_queue(netdev);
2360 netif_tx_unlock_bh(netdev);
2361 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002362 }
2363
2364 return 0;
2365}
2366
Sascha Hauer009fda82009-04-15 01:32:23 +00002367static const struct net_device_ops fec_netdev_ops = {
2368 .ndo_open = fec_enet_open,
2369 .ndo_stop = fec_enet_close,
2370 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002371 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002372 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002373 .ndo_validate_addr = eth_validate_addr,
2374 .ndo_tx_timeout = fec_timeout,
2375 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002376 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002377#ifdef CONFIG_NET_POLL_CONTROLLER
2378 .ndo_poll_controller = fec_poll_controller,
2379#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002380 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002381};
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /*
2384 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002385 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002387static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002389 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002390 const struct platform_device_id *id_entry =
2391 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002392 struct bufdesc *cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002393 int bd_size;
2394
2395 /* init the tx & rx ring size */
2396 fep->tx_ring_size = TX_RING_SIZE;
2397 fep->rx_ring_size = RX_RING_SIZE;
2398
Nimrod Andy79f33912014-06-12 08:16:23 +08002399 fep->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2400 fep->tx_wake_threshold = (fep->tx_ring_size - fep->tx_stop_threshold) / 2;
2401
Nimrod Andy55d02182014-06-12 08:16:21 +08002402 if (fep->bufdesc_ex)
2403 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2404 else
2405 fep->bufdesc_size = sizeof(struct bufdesc);
2406 bd_size = (fep->tx_ring_size + fep->rx_ring_size) *
2407 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002409 /* Allocate memory for buffer descriptors. */
Nimrod Andy55d02182014-06-12 08:16:21 +08002410 cbd_base = dma_alloc_coherent(NULL, bd_size, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002411 GFP_KERNEL);
2412 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10002413 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10002414
Nimrod Andy79f33912014-06-12 08:16:23 +08002415 fep->tso_hdrs = dma_alloc_coherent(NULL, fep->tx_ring_size * TSO_HEADER_SIZE,
2416 &fep->tso_hdrs_dma, GFP_KERNEL);
2417 if (!fep->tso_hdrs) {
2418 dma_free_coherent(NULL, bd_size, cbd_base, fep->bd_dma);
2419 return -ENOMEM;
2420 }
2421
Frank Li14109a52013-03-26 16:12:03 +00002422 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002423
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002424 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Shawn Guo49da97d2011-01-05 21:13:11 +00002426 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002427 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002428 /* make sure MAC we just acquired is programmed into the hw */
2429 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002431 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 fep->rx_bd_base = cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002433 if (fep->bufdesc_ex)
Frank Liff43da82013-01-03 16:04:23 +00002434 fep->tx_bd_base = (struct bufdesc *)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002435 (((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
Nimrod Andy55d02182014-06-12 08:16:21 +08002436 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002437 fep->tx_bd_base = cbd_base + fep->rx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Sascha Hauer22f6b862009-04-15 01:32:18 +00002439 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002440 ndev->watchdog_timeo = TX_TIMEOUT;
2441 ndev->netdev_ops = &fec_netdev_ops;
2442 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002443
Frank Lidc975382013-01-28 18:31:42 +00002444 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002445 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002446
Nimrod Andy09d1e542014-06-12 08:16:20 +08002447 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002448 /* enable hw VLAN support */
2449 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002450
Shawn Guo48496252013-05-08 21:08:22 +00002451 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002452 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2453
Shawn Guo48496252013-05-08 21:08:22 +00002454 /* enable hw accelerator */
2455 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002456 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002457 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2458 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002459
Nimrod Andy09d1e542014-06-12 08:16:20 +08002460 ndev->hw_features = ndev->features;
2461
Russell Kingef833372014-07-08 12:40:43 +01002462 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return 0;
2465}
2466
Shawn Guoca2cc332011-06-25 02:04:35 +08002467#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002468static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002469{
2470 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002471 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002472 struct device_node *np = pdev->dev.of_node;
2473
2474 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002475 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002476
Shawn Guoa3caad02012-06-27 03:45:24 +00002477 of_property_read_u32(np, "phy-reset-duration", &msec);
2478 /* A sane reset duration should not be longer than 1s */
2479 if (msec > 1000)
2480 msec = 1;
2481
Shawn Guoca2cc332011-06-25 02:04:35 +08002482 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002483 if (!gpio_is_valid(phy_reset))
2484 return;
2485
Shawn Guo119fc002012-06-27 03:45:22 +00002486 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2487 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002488 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002489 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002490 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002491 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002492 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08002493 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002494}
2495#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002496static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002497{
2498 /*
2499 * In case of platform probe, the reset has been done
2500 * by machine code.
2501 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002502}
2503#endif /* CONFIG_OF */
2504
Bill Pemberton33897cc2012-12-03 09:23:58 -05002505static int
Sascha Haueread73182009-01-28 23:03:11 +00002506fec_probe(struct platform_device *pdev)
2507{
2508 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002509 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002510 struct net_device *ndev;
2511 int i, irq, ret = 0;
2512 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002513 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002514 static int dev_id;
Shawn Guoca2cc332011-06-25 02:04:35 +08002515
2516 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2517 if (of_id)
2518 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002519
Sascha Haueread73182009-01-28 23:03:11 +00002520 /* Init network device */
2521 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00002522 if (!ndev)
2523 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002524
2525 SET_NETDEV_DEV(ndev, &pdev->dev);
2526
2527 /* setup board info structure */
2528 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002529
Guenter Roeckd1391932013-06-18 10:04:59 -07002530#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002531 /* default enable pause frame auto negotiation */
2532 if (pdev->id_entry &&
2533 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2534 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002535#endif
Frank Libaa70a52013-01-16 16:55:58 +00002536
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002537 /* Select default pin state */
2538 pinctrl_pm_select_default_state(&pdev->dev);
2539
Fabio Estevam399db752013-07-21 13:25:03 -03002540 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302541 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2542 if (IS_ERR(fep->hwp)) {
2543 ret = PTR_ERR(fep->hwp);
2544 goto failed_ioremap;
2545 }
2546
Bryan Wue6b043d2010-03-31 02:10:44 +00002547 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002548 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002549
Frank Liff43da82013-01-03 16:04:23 +00002550 fep->bufdesc_ex = 0;
2551
Sascha Haueread73182009-01-28 23:03:11 +00002552 platform_set_drvdata(pdev, ndev);
2553
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002554 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002555 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002556 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002557 if (pdata)
2558 fep->phy_interface = pdata->phy;
2559 else
2560 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2561 } else {
2562 fep->phy_interface = ret;
2563 }
2564
Fabio Estevame2f8d552013-02-22 06:40:45 +00002565 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2566 if (IS_ERR(fep->clk_ipg)) {
2567 ret = PTR_ERR(fep->clk_ipg);
2568 goto failed_clk;
2569 }
2570
2571 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2572 if (IS_ERR(fep->clk_ahb)) {
2573 ret = PTR_ERR(fep->clk_ahb);
2574 goto failed_clk;
2575 }
2576
Linus Torvalds38f56f32013-05-07 11:06:17 -07002577 /* enet_out is optional, depends on board */
2578 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2579 if (IS_ERR(fep->clk_enet_out))
2580 fep->clk_enet_out = NULL;
2581
Fabio Estevame2f8d552013-02-22 06:40:45 +00002582 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002583 fep->bufdesc_ex =
2584 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002585 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07002586 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002587 fep->bufdesc_ex = 0;
2588 }
2589
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002590 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002591 if (ret)
2592 goto failed_clk;
2593
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00002594 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2595 if (!IS_ERR(fep->reg_phy)) {
2596 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002597 if (ret) {
2598 dev_err(&pdev->dev,
2599 "Failed to enable phy regulator: %d\n", ret);
2600 goto failed_regulator;
2601 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002602 } else {
2603 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002604 }
2605
2606 fec_reset_phy(pdev);
2607
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002608 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00002609 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002610
2611 ret = fec_enet_init(ndev);
2612 if (ret)
2613 goto failed_init;
2614
Xiao Jiangc7c83d12011-09-29 02:15:56 +00002615 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00002616 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00002617 if (irq < 0) {
2618 if (i)
2619 break;
2620 ret = irq;
2621 goto failed_irq;
2622 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002623 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02002624 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002625 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00002626 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00002627 }
2628
Bryan Wue6b043d2010-03-31 02:10:44 +00002629 ret = fec_enet_mii_init(pdev);
2630 if (ret)
2631 goto failed_mii_init;
2632
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002633 /* Carrier starts down, phylib will bring it up */
2634 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002635 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002636 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002637
Sascha Haueread73182009-01-28 23:03:11 +00002638 ret = register_netdev(ndev);
2639 if (ret)
2640 goto failed_register;
2641
Fabio Estevameb1d0642013-04-13 07:25:36 +00002642 if (fep->bufdesc_ex && fep->ptp_clock)
2643 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2644
Frank Li54309fa2013-05-07 14:08:44 +00002645 INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
Sascha Haueread73182009-01-28 23:03:11 +00002646 return 0;
2647
2648failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00002649 fec_enet_mii_remove(fep);
2650failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002651failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002652failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002653 if (fep->reg_phy)
2654 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00002655failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002656 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00002657failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00002658failed_ioremap:
2659 free_netdev(ndev);
2660
2661 return ret;
2662}
2663
Bill Pemberton33897cc2012-12-03 09:23:58 -05002664static int
Sascha Haueread73182009-01-28 23:03:11 +00002665fec_drv_remove(struct platform_device *pdev)
2666{
2667 struct net_device *ndev = platform_get_drvdata(pdev);
2668 struct fec_enet_private *fep = netdev_priv(ndev);
2669
Frank Li54309fa2013-05-07 14:08:44 +00002670 cancel_delayed_work_sync(&(fep->delay_work.delay_work));
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002671 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002672 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00002673 del_timer_sync(&fep->time_keep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002674 if (fep->reg_phy)
2675 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00002676 if (fep->ptp_clock)
2677 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002678 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00002679 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002680
Sascha Haueread73182009-01-28 23:03:11 +00002681 return 0;
2682}
2683
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002684#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00002685static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002686fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002687{
Eric Benard87cad5c2010-06-18 04:19:54 +00002688 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002689 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002690
Russell Kingda1774e2014-07-08 12:39:57 +01002691 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002692 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01002693 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01002694 napi_disable(&fep->napi);
2695 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002696 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002697 netif_tx_unlock_bh(ndev);
2698 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002699 }
Russell Kingda1774e2014-07-08 12:39:57 +01002700 rtnl_unlock();
2701
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002702 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002703 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002704
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002705 if (fep->reg_phy)
2706 regulator_disable(fep->reg_phy);
2707
Sascha Haueread73182009-01-28 23:03:11 +00002708 return 0;
2709}
2710
2711static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002712fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002713{
Eric Benard87cad5c2010-06-18 04:19:54 +00002714 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002715 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002716 int ret;
2717
2718 if (fep->reg_phy) {
2719 ret = regulator_enable(fep->reg_phy);
2720 if (ret)
2721 return ret;
2722 }
Sascha Haueread73182009-01-28 23:03:11 +00002723
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002724 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002725 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002726 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002727 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002728
Russell Kingda1774e2014-07-08 12:39:57 +01002729 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002730 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01002731 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002732 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002733 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002734 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002735 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01002736 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00002737 }
Russell Kingda1774e2014-07-08 12:39:57 +01002738 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002739
Sascha Haueread73182009-01-28 23:03:11 +00002740 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002741
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002742failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03002743 if (fep->reg_phy)
2744 regulator_disable(fep->reg_phy);
2745 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00002746}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002747#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00002748
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002749static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002750
Sascha Haueread73182009-01-28 23:03:11 +00002751static struct platform_driver fec_driver = {
2752 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002753 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002754 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002755 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002756 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002757 },
Shawn Guob5680e02011-01-05 21:13:13 +00002758 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002759 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002760 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002761};
2762
Fabio Estevamaaca2372012-01-23 16:44:50 +00002763module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03002765MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766MODULE_LICENSE("GPL");