blob: 09fcdc768931ea192637e0cee6771c9f463bd156 [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
Russell King344756f2014-07-08 13:01:59 +0100323static void fec_dump(struct net_device *ndev)
324{
325 struct fec_enet_private *fep = netdev_priv(ndev);
326 struct bufdesc *bdp = fep->tx_bd_base;
327 unsigned int index = 0;
328
329 netdev_info(ndev, "TX ring dump\n");
330 pr_info("Nr SC addr len SKB\n");
331
332 do {
333 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
334 index,
335 bdp == fep->cur_tx ? 'S' : ' ',
336 bdp == fep->dirty_tx ? 'H' : ' ',
337 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
338 fep->tx_skbuff[index]);
339 bdp = fec_enet_get_nextdesc(bdp, fep);
340 index++;
341 } while (bdp != fep->tx_bd_base);
342}
343
Fugang Duan62a02c92014-06-18 08:33:52 +0800344static inline bool is_ipv4_pkt(struct sk_buff *skb)
345{
346 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
347}
348
Jim Baxter4c09eed2013-04-19 08:10:49 +0000349static int
350fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
351{
352 /* Only run for packets requiring a checksum. */
353 if (skb->ip_summed != CHECKSUM_PARTIAL)
354 return 0;
355
356 if (unlikely(skb_cow_head(skb, 0)))
357 return -1;
358
Fugang Duan62a02c92014-06-18 08:33:52 +0800359 if (is_ipv4_pkt(skb))
360 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000361 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
362
363 return 0;
364}
365
Nimrod Andy6e909282014-06-12 08:16:22 +0800366static int
367fec_enet_txq_submit_frag_skb(struct sk_buff *skb, struct net_device *ndev)
368{
369 struct fec_enet_private *fep = netdev_priv(ndev);
370 const struct platform_device_id *id_entry =
371 platform_get_device_id(fep->pdev);
372 struct bufdesc *bdp = fep->cur_tx;
373 struct bufdesc_ex *ebdp;
374 int nr_frags = skb_shinfo(skb)->nr_frags;
375 int frag, frag_len;
376 unsigned short status;
377 unsigned int estatus = 0;
378 skb_frag_t *this_frag;
379 unsigned int index;
380 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100381 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800382 int i;
383
384 for (frag = 0; frag < nr_frags; frag++) {
385 this_frag = &skb_shinfo(skb)->frags[frag];
386 bdp = fec_enet_get_nextdesc(bdp, fep);
387 ebdp = (struct bufdesc_ex *)bdp;
388
389 status = bdp->cbd_sc;
390 status &= ~BD_ENET_TX_STATS;
391 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
392 frag_len = skb_shinfo(skb)->frags[frag].size;
393
394 /* Handle the last BD specially */
395 if (frag == nr_frags - 1) {
396 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
397 if (fep->bufdesc_ex) {
398 estatus |= BD_ENET_TX_INT;
399 if (unlikely(skb_shinfo(skb)->tx_flags &
400 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
401 estatus |= BD_ENET_TX_TS;
402 }
403 }
404
405 if (fep->bufdesc_ex) {
406 if (skb->ip_summed == CHECKSUM_PARTIAL)
407 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
408 ebdp->cbd_bdu = 0;
409 ebdp->cbd_esc = estatus;
410 }
411
412 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
413
414 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
415 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
416 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
417 memcpy(fep->tx_bounce[index], bufaddr, frag_len);
418 bufaddr = fep->tx_bounce[index];
419
420 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
421 swap_buffer(bufaddr, frag_len);
422 }
423
Russell Kingd6bf3142014-07-08 00:23:19 +0100424 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
425 DMA_TO_DEVICE);
426 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800427 dev_kfree_skb_any(skb);
428 if (net_ratelimit())
429 netdev_err(ndev, "Tx DMA memory map failed\n");
430 goto dma_mapping_error;
431 }
432
Russell Kingd6bf3142014-07-08 00:23:19 +0100433 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800434 bdp->cbd_datlen = frag_len;
435 bdp->cbd_sc = status;
436 }
437
438 fep->cur_tx = bdp;
439
440 return 0;
441
442dma_mapping_error:
443 bdp = fep->cur_tx;
444 for (i = 0; i < frag; i++) {
445 bdp = fec_enet_get_nextdesc(bdp, fep);
446 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
447 bdp->cbd_datlen, DMA_TO_DEVICE);
448 }
449 return NETDEV_TX_OK;
450}
451
452static int fec_enet_txq_submit_skb(struct sk_buff *skb, struct net_device *ndev)
453{
454 struct fec_enet_private *fep = netdev_priv(ndev);
455 const struct platform_device_id *id_entry =
456 platform_get_device_id(fep->pdev);
457 int nr_frags = skb_shinfo(skb)->nr_frags;
458 struct bufdesc *bdp, *last_bdp;
459 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100460 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800461 unsigned short status;
462 unsigned short buflen;
463 unsigned int estatus = 0;
464 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800465 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800466 int ret;
467
Nimrod Andy79f33912014-06-12 08:16:23 +0800468 entries_free = fec_enet_get_free_txdesc_num(fep);
469 if (entries_free < MAX_SKB_FRAGS + 1) {
470 dev_kfree_skb_any(skb);
471 if (net_ratelimit())
472 netdev_err(ndev, "NOT enough BD for SG!\n");
473 return NETDEV_TX_OK;
474 }
475
Nimrod Andy6e909282014-06-12 08:16:22 +0800476 /* Protocol checksum off-load for TCP and UDP. */
477 if (fec_enet_clear_csum(skb, ndev)) {
478 dev_kfree_skb_any(skb);
479 return NETDEV_TX_OK;
480 }
481
482 /* Fill in a Tx ring entry */
483 bdp = fep->cur_tx;
484 status = bdp->cbd_sc;
485 status &= ~BD_ENET_TX_STATS;
486
487 /* Set buffer length and buffer pointer */
488 bufaddr = skb->data;
489 buflen = skb_headlen(skb);
490
491 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
492 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
493 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
494 memcpy(fep->tx_bounce[index], skb->data, buflen);
495 bufaddr = fep->tx_bounce[index];
496
497 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
498 swap_buffer(bufaddr, buflen);
499 }
500
Russell Kingd6bf3142014-07-08 00:23:19 +0100501 /* Push the data cache so the CPM does not get stale memory data. */
502 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
503 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800504 dev_kfree_skb_any(skb);
505 if (net_ratelimit())
506 netdev_err(ndev, "Tx DMA memory map failed\n");
507 return NETDEV_TX_OK;
508 }
509
510 if (nr_frags) {
511 ret = fec_enet_txq_submit_frag_skb(skb, ndev);
512 if (ret)
513 return ret;
514 } else {
515 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
516 if (fep->bufdesc_ex) {
517 estatus = BD_ENET_TX_INT;
518 if (unlikely(skb_shinfo(skb)->tx_flags &
519 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
520 estatus |= BD_ENET_TX_TS;
521 }
522 }
523
524 if (fep->bufdesc_ex) {
525
526 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
527
528 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
529 fep->hwts_tx_en))
530 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
531
532 if (skb->ip_summed == CHECKSUM_PARTIAL)
533 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
534
535 ebdp->cbd_bdu = 0;
536 ebdp->cbd_esc = estatus;
537 }
538
539 last_bdp = fep->cur_tx;
540 index = fec_enet_get_bd_index(fep->tx_bd_base, last_bdp, fep);
541 /* Save skb pointer */
542 fep->tx_skbuff[index] = skb;
543
544 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100545 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800546
547 /* Send it on its way. Tell FEC it's ready, interrupt when done,
548 * it's the last BD of the frame, and to put the CRC on the end.
549 */
550 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
551 bdp->cbd_sc = status;
552
Sascha Hauer22f6b862009-04-15 01:32:18 +0000553 /* If this was the last BD in the ring, start at the beginning again. */
Nimrod Andy6e909282014-06-12 08:16:22 +0800554 bdp = fec_enet_get_nextdesc(last_bdp, fep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800556 skb_tx_timestamp(skb);
557
Sascha Hauer2e285322009-04-15 01:32:16 +0000558 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Frank Lide5fb0a2013-03-03 17:34:25 +0000560 /* Trigger transmission start */
561 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
562
Nimrod Andy6e909282014-06-12 08:16:22 +0800563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Nimrod Andy79f33912014-06-12 08:16:23 +0800566static int
567fec_enet_txq_put_data_tso(struct sk_buff *skb, struct net_device *ndev,
568 struct bufdesc *bdp, int index, char *data,
569 int size, bool last_tcp, bool is_last)
570{
571 struct fec_enet_private *fep = netdev_priv(ndev);
572 const struct platform_device_id *id_entry =
573 platform_get_device_id(fep->pdev);
574 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
575 unsigned short status;
576 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100577 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800578
579 status = bdp->cbd_sc;
580 status &= ~BD_ENET_TX_STATS;
581
582 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800583
584 if (((unsigned long) data) & FEC_ALIGNMENT ||
585 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
586 memcpy(fep->tx_bounce[index], data, size);
587 data = fep->tx_bounce[index];
588
589 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
590 swap_buffer(data, size);
591 }
592
Russell Kingd6bf3142014-07-08 00:23:19 +0100593 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
594 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800595 dev_kfree_skb_any(skb);
596 if (net_ratelimit())
597 netdev_err(ndev, "Tx DMA memory map failed\n");
598 return NETDEV_TX_BUSY;
599 }
600
Russell Kingd6bf3142014-07-08 00:23:19 +0100601 bdp->cbd_datlen = size;
602 bdp->cbd_bufaddr = addr;
603
Nimrod Andy79f33912014-06-12 08:16:23 +0800604 if (fep->bufdesc_ex) {
605 if (skb->ip_summed == CHECKSUM_PARTIAL)
606 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
607 ebdp->cbd_bdu = 0;
608 ebdp->cbd_esc = estatus;
609 }
610
611 /* Handle the last BD specially */
612 if (last_tcp)
613 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
614 if (is_last) {
615 status |= BD_ENET_TX_INTR;
616 if (fep->bufdesc_ex)
617 ebdp->cbd_esc |= BD_ENET_TX_INT;
618 }
619
620 bdp->cbd_sc = status;
621
622 return 0;
623}
624
625static int
626fec_enet_txq_put_hdr_tso(struct sk_buff *skb, struct net_device *ndev,
627 struct bufdesc *bdp, int index)
628{
629 struct fec_enet_private *fep = netdev_priv(ndev);
630 const struct platform_device_id *id_entry =
631 platform_get_device_id(fep->pdev);
632 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
633 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
634 void *bufaddr;
635 unsigned long dmabuf;
636 unsigned short status;
637 unsigned int estatus = 0;
638
639 status = bdp->cbd_sc;
640 status &= ~BD_ENET_TX_STATS;
641 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
642
643 bufaddr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
644 dmabuf = fep->tso_hdrs_dma + index * TSO_HEADER_SIZE;
645 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
646 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
647 memcpy(fep->tx_bounce[index], skb->data, hdr_len);
648 bufaddr = fep->tx_bounce[index];
649
650 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
651 swap_buffer(bufaddr, hdr_len);
652
653 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
654 hdr_len, DMA_TO_DEVICE);
655 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
656 dev_kfree_skb_any(skb);
657 if (net_ratelimit())
658 netdev_err(ndev, "Tx DMA memory map failed\n");
659 return NETDEV_TX_BUSY;
660 }
661 }
662
663 bdp->cbd_bufaddr = dmabuf;
664 bdp->cbd_datlen = hdr_len;
665
666 if (fep->bufdesc_ex) {
667 if (skb->ip_summed == CHECKSUM_PARTIAL)
668 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
669 ebdp->cbd_bdu = 0;
670 ebdp->cbd_esc = estatus;
671 }
672
673 bdp->cbd_sc = status;
674
675 return 0;
676}
677
678static int fec_enet_txq_submit_tso(struct sk_buff *skb, struct net_device *ndev)
679{
680 struct fec_enet_private *fep = netdev_priv(ndev);
681 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
682 int total_len, data_left;
683 struct bufdesc *bdp = fep->cur_tx;
684 struct tso_t tso;
685 unsigned int index = 0;
686 int ret;
687
688 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep)) {
689 dev_kfree_skb_any(skb);
690 if (net_ratelimit())
691 netdev_err(ndev, "NOT enough BD for TSO!\n");
692 return NETDEV_TX_OK;
693 }
694
695 /* Protocol checksum off-load for TCP and UDP. */
696 if (fec_enet_clear_csum(skb, ndev)) {
697 dev_kfree_skb_any(skb);
698 return NETDEV_TX_OK;
699 }
700
701 /* Initialize the TSO handler, and prepare the first payload */
702 tso_start(skb, &tso);
703
704 total_len = skb->len - hdr_len;
705 while (total_len > 0) {
706 char *hdr;
707
708 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
709 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
710 total_len -= data_left;
711
712 /* prepare packet headers: MAC + IP + TCP */
713 hdr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
714 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
715 ret = fec_enet_txq_put_hdr_tso(skb, ndev, bdp, index);
716 if (ret)
717 goto err_release;
718
719 while (data_left > 0) {
720 int size;
721
722 size = min_t(int, tso.size, data_left);
723 bdp = fec_enet_get_nextdesc(bdp, fep);
724 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
725 ret = fec_enet_txq_put_data_tso(skb, ndev, bdp, index, tso.data,
726 size, size == data_left,
727 total_len == 0);
728 if (ret)
729 goto err_release;
730
731 data_left -= size;
732 tso_build_data(skb, &tso, size);
733 }
734
735 bdp = fec_enet_get_nextdesc(bdp, fep);
736 }
737
738 /* Save skb pointer */
739 fep->tx_skbuff[index] = skb;
740
Nimrod Andy79f33912014-06-12 08:16:23 +0800741 skb_tx_timestamp(skb);
742 fep->cur_tx = bdp;
743
744 /* Trigger transmission start */
745 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
746
747 return 0;
748
749err_release:
750 /* TODO: Release all used data descriptors for TSO */
751 return ret;
752}
753
Nimrod Andy61a44272014-06-12 08:16:18 +0800754static netdev_tx_t
755fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
756{
757 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800758 int entries_free;
Nimrod Andy61a44272014-06-12 08:16:18 +0800759 int ret;
760
Nimrod Andy79f33912014-06-12 08:16:23 +0800761 if (skb_is_gso(skb))
762 ret = fec_enet_txq_submit_tso(skb, ndev);
763 else
764 ret = fec_enet_txq_submit_skb(skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800765 if (ret)
766 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800767
Nimrod Andy6e909282014-06-12 08:16:22 +0800768 entries_free = fec_enet_get_free_txdesc_num(fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800769 if (entries_free <= fep->tx_stop_threshold)
Nimrod Andy61a44272014-06-12 08:16:18 +0800770 netif_stop_queue(ndev);
771
772 return NETDEV_TX_OK;
773}
774
Frank Li14109a52013-03-26 16:12:03 +0000775/* Init RX & TX buffer descriptors
776 */
777static void fec_enet_bd_init(struct net_device *dev)
778{
779 struct fec_enet_private *fep = netdev_priv(dev);
780 struct bufdesc *bdp;
781 unsigned int i;
782
783 /* Initialize the receive buffer descriptors. */
784 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800785 for (i = 0; i < fep->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000786
787 /* Initialize the BD for every fragment in the page. */
788 if (bdp->cbd_bufaddr)
789 bdp->cbd_sc = BD_ENET_RX_EMPTY;
790 else
791 bdp->cbd_sc = 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
799 fep->cur_rx = fep->rx_bd_base;
800
801 /* ...and the same for transmit */
802 bdp = fep->tx_bd_base;
803 fep->cur_tx = bdp;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800804 for (i = 0; i < fep->tx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000805
806 /* Initialize the BD for every fragment in the page. */
807 bdp->cbd_sc = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100808 if (fep->tx_skbuff[i]) {
Frank Li14109a52013-03-26 16:12:03 +0000809 dev_kfree_skb_any(fep->tx_skbuff[i]);
810 fep->tx_skbuff[i] = NULL;
811 }
812 bdp->cbd_bufaddr = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800813 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000814 }
815
816 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800817 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000818 bdp->cbd_sc |= BD_SC_WRAP;
819 fep->dirty_tx = bdp;
820}
821
Russell Kingdbc64a82014-07-08 12:40:12 +0100822/*
823 * This function is called to start or restart the FEC during a link
824 * change, transmit timeout, or to reconfigure the FEC. The network
825 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100826 */
827static void
Russell Kingef833372014-07-08 12:40:43 +0100828fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100829{
830 struct fec_enet_private *fep = netdev_priv(ndev);
831 const struct platform_device_id *id_entry =
832 platform_get_device_id(fep->pdev);
833 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000834 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100835 u32 temp_mac[2];
836 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000837 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100838
839 /* Whack a reset. We should wait for this. */
840 writel(1, fep->hwp + FEC_ECNTRL);
841 udelay(10);
842
843 /*
844 * enet-mac reset will reset mac address registers too,
845 * so need to reconfigure it.
846 */
847 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
848 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
849 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
850 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
851 }
852
853 /* Clear any outstanding interrupt. */
854 writel(0xffc00000, fep->hwp + FEC_IEVENT);
855
Uwe Kleine-König45993652011-01-19 20:47:04 +0100856 /* Set maximum receive buffer size. */
857 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
858
Frank Li14109a52013-03-26 16:12:03 +0000859 fec_enet_bd_init(ndev);
860
Uwe Kleine-König45993652011-01-19 20:47:04 +0100861 /* Set receive and transmit descriptor base. */
862 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000863 if (fep->bufdesc_ex)
864 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800865 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000866 else
867 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800868 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100869
Uwe Kleine-König45993652011-01-19 20:47:04 +0100870
Uwe Kleine-König45993652011-01-19 20:47:04 +0100871 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
872 if (fep->tx_skbuff[i]) {
873 dev_kfree_skb_any(fep->tx_skbuff[i]);
874 fep->tx_skbuff[i] = NULL;
875 }
876 }
877
878 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100879 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100880 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100881 writel(0x04, fep->hwp + FEC_X_CNTRL);
882 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100883 /* No Rcv on Xmit */
884 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100885 writel(0x0, fep->hwp + FEC_X_CNTRL);
886 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100887
Uwe Kleine-König45993652011-01-19 20:47:04 +0100888 /* Set MII speed */
889 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
890
Guenter Roeckd1391932013-06-18 10:04:59 -0700891#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000892 /* set RX checksum */
893 val = readl(fep->hwp + FEC_RACC);
894 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
895 val |= FEC_RACC_OPTIONS;
896 else
897 val &= ~FEC_RACC_OPTIONS;
898 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700899#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000900
Uwe Kleine-König45993652011-01-19 20:47:04 +0100901 /*
902 * The phy interface and speed need to get configured
903 * differently on enet-mac.
904 */
905 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100906 /* Enable flow control and length check */
907 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100908
Shawn Guo230dec62011-09-23 02:12:48 +0000909 /* RGMII, RMII or MII */
910 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
911 rcntl |= (1 << 6);
912 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100913 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100914 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100915 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100916
Shawn Guo230dec62011-09-23 02:12:48 +0000917 /* 1G, 100M or 10M */
918 if (fep->phy_dev) {
919 if (fep->phy_dev->speed == SPEED_1000)
920 ecntl |= (1 << 5);
921 else if (fep->phy_dev->speed == SPEED_100)
922 rcntl &= ~(1 << 9);
923 else
924 rcntl |= (1 << 9);
925 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100926 } else {
927#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800928 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000929 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100930 /* disable the gasket and wait */
931 writel(0, fep->hwp + FEC_MIIGSK_ENR);
932 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
933 udelay(1);
934
935 /*
936 * configure the gasket:
937 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800938 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100939 */
Eric Benard8d82f212012-01-12 06:10:28 +0000940 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
941 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
942 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
943 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
944 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100945
946 /* re-enable the gasket */
947 writel(2, fep->hwp + FEC_MIIGSK_ENR);
948 }
949#endif
950 }
Frank Libaa70a52013-01-16 16:55:58 +0000951
Guenter Roeckd1391932013-06-18 10:04:59 -0700952#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +0000953 /* enable pause frame*/
954 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
955 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
956 fep->phy_dev && fep->phy_dev->pause)) {
957 rcntl |= FEC_ENET_FCE;
958
Jim Baxter4c09eed2013-04-19 08:10:49 +0000959 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000960 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
961 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
962 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
963 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
964
965 /* OPD */
966 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
967 } else {
968 rcntl &= ~FEC_ENET_FCE;
969 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700970#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +0000971
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100972 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100973
Stefan Wahren84fe6182014-03-12 11:28:19 +0100974 /* Setup multicast filter. */
975 set_multicast_list(ndev);
976#ifndef CONFIG_M5272
977 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
978 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
979#endif
980
Shawn Guo230dec62011-09-23 02:12:48 +0000981 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
982 /* enable ENET endian swap */
983 ecntl |= (1 << 8);
984 /* enable ENET store and forward mode */
985 writel(1 << 8, fep->hwp + FEC_X_WMRK);
986 }
987
Frank Liff43da82013-01-03 16:04:23 +0000988 if (fep->bufdesc_ex)
989 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000990
Chris Healy38ae92d2013-06-25 23:18:52 -0700991#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +0100992 /* Enable the MIB statistic event counters */
993 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -0700994#endif
995
Uwe Kleine-König45993652011-01-19 20:47:04 +0100996 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000997 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100998 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
999
Frank Liff43da82013-01-03 16:04:23 +00001000 if (fep->bufdesc_ex)
1001 fec_ptp_start_cyclecounter(ndev);
1002
Uwe Kleine-König45993652011-01-19 20:47:04 +01001003 /* Enable interrupts we wish to service */
1004 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1005}
1006
1007static void
1008fec_stop(struct net_device *ndev)
1009{
1010 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001011 const struct platform_device_id *id_entry =
1012 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001013 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001014
1015 /* We cannot expect a graceful transmit stop without link !!! */
1016 if (fep->link) {
1017 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1018 udelay(10);
1019 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001020 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001021 }
1022
1023 /* Whack a reset. We should wait for this. */
1024 writel(1, fep->hwp + FEC_ECNTRL);
1025 udelay(10);
1026 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1027 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001028
1029 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001030 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001031 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001032 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1033 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001034}
1035
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001038fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001040 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Russell King344756f2014-07-08 13:01:59 +01001042 fec_dump(ndev);
1043
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001044 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Russell King36cdc742014-07-08 13:01:44 +01001046 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001047}
1048
Russell King36cdc742014-07-08 13:01:44 +01001049static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001050{
1051 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001052 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001053 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001054
Russell King36cdc742014-07-08 13:01:44 +01001055 rtnl_lock();
1056 if (netif_device_present(ndev) || netif_running(ndev)) {
1057 napi_disable(&fep->napi);
1058 netif_tx_lock_bh(ndev);
1059 fec_restart(ndev);
1060 netif_wake_queue(ndev);
1061 netif_tx_unlock_bh(ndev);
1062 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001063 }
Russell King36cdc742014-07-08 13:01:44 +01001064 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001068fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001071 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001072 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +00001074 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001075 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001077 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 bdp = fep->dirty_tx;
1079
Frank Lide5fb0a2013-03-03 17:34:25 +00001080 /* get next bdp of dirty_tx */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001081 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001082
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001083 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001084
1085 /* current queue is empty */
1086 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001087 break;
1088
Nimrod Andy61a44272014-06-12 08:16:18 +08001089 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001090
Frank Lide5fb0a2013-03-03 17:34:25 +00001091 skb = fep->tx_skbuff[index];
Russell Kingd6bf3142014-07-08 00:23:19 +01001092 fep->tx_skbuff[index] = NULL;
Nimrod Andy79f33912014-06-12 08:16:23 +08001093 if (!IS_TSO_HEADER(fep, bdp->cbd_bufaddr))
1094 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1095 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001096 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001097 if (!skb) {
1098 bdp = fec_enet_get_nextdesc(bdp, fep);
1099 continue;
1100 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001103 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 BD_ENET_TX_RL | BD_ENET_TX_UN |
1105 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001106 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001107 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001108 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001109 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001110 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001111 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001112 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001113 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001114 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001115 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001116 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001118 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001119 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
Frank Liff43da82013-01-03 16:04:23 +00001122 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1123 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001124 struct skb_shared_hwtstamps shhwtstamps;
1125 unsigned long flags;
Frank Liff43da82013-01-03 16:04:23 +00001126 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001127
1128 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1129 spin_lock_irqsave(&fep->tmreg_lock, flags);
1130 shhwtstamps.hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +00001131 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +00001132 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1133 skb_tstamp_tx(skb, &shhwtstamps);
1134 }
Frank Liff43da82013-01-03 16:04:23 +00001135
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001136 if (status & BD_ENET_TX_READY)
Joe Perches31b77202013-04-13 19:03:17 +00001137 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
Sascha Hauer22f6b862009-04-15 01:32:18 +00001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /* Deferred means some collisions occurred during transmit,
1140 * but we eventually sent the packet OK.
1141 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001142 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001143 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001144
Sascha Hauer22f6b862009-04-15 01:32:18 +00001145 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001147
1148 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001149
Sascha Hauer22f6b862009-04-15 01:32:18 +00001150 /* Update pointer to next buffer descriptor to be transmitted */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001151 bdp = fec_enet_get_nextdesc(bdp, fep);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001152
Sascha Hauer22f6b862009-04-15 01:32:18 +00001153 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001155 if (netif_queue_stopped(ndev)) {
1156 entries_free = fec_enet_get_free_txdesc_num(fep);
1157 if (entries_free >= fep->tx_wake_threshold)
1158 netif_wake_queue(ndev);
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Russell Kingccea2962014-07-08 13:01:38 +01001161
1162 /* ERR006538: Keep the transmitter going */
1163 if (bdp != fep->cur_tx && readl(fep->hwp + FEC_X_DES_ACTIVE) == 0)
1164 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167/* During a receive, the cur_rx points to the current incoming buffer.
1168 * When we update through the ring, if the next incoming buffer has
1169 * not been given to the system, we just set the empty indicator,
1170 * effectively tossing the packet.
1171 */
Frank Lidc975382013-01-28 18:31:42 +00001172static int
1173fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001175 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001176 const struct platform_device_id *id_entry =
1177 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001178 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001179 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct sk_buff *skb;
1181 ushort pkt_len;
1182 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001183 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001184 struct bufdesc_ex *ebdp = NULL;
1185 bool vlan_packet_rcvd = false;
1186 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001187 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001188
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001189#ifdef CONFIG_M532x
1190 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* First, grab all of the stats for the incoming packet.
1194 * These get messed up if we get called due to a busy condition.
1195 */
1196 bdp = fep->cur_rx;
1197
Sascha Hauer22f6b862009-04-15 01:32:18 +00001198 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Frank Lidc975382013-01-28 18:31:42 +00001200 if (pkt_received >= budget)
1201 break;
1202 pkt_received++;
1203
Sascha Hauer22f6b862009-04-15 01:32:18 +00001204 /* Since we have allocated space to hold a complete frame,
1205 * the last indicator should be set.
1206 */
1207 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001208 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Russell Kingdb3421c2014-07-08 13:01:49 +01001210 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
1211
Sascha Hauer22f6b862009-04-15 01:32:18 +00001212 /* Check for errors. */
1213 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001215 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001216 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1217 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001218 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001219 }
1220 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001221 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001222 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001223 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001224 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001225 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001227
1228 /* Report late collisions as a frame error.
1229 * On this error, the BD is closed, but we don't know what we
1230 * have in the buffer. So, just drop this frame on the floor.
1231 */
1232 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001233 ndev->stats.rx_errors++;
1234 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001235 goto rx_processing_done;
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Sascha Hauer22f6b862009-04-15 01:32:18 +00001238 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001239 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001240 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001241 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Nimrod Andy61a44272014-06-12 08:16:18 +08001243 index = fec_enet_get_bd_index(fep->rx_bd_base, bdp, fep);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001244 data = fep->rx_skbuff[index]->data;
1245 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1246 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001247
Shawn Guob5680e02011-01-05 21:13:13 +00001248 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1249 swap_buffer(data, pkt_len);
1250
Jim Baxtercdffcf12013-07-02 22:52:56 +01001251 /* Extract the enhanced buffer descriptor */
1252 ebdp = NULL;
1253 if (fep->bufdesc_ex)
1254 ebdp = (struct bufdesc_ex *)bdp;
1255
1256 /* If this is a VLAN packet remove the VLAN Tag */
1257 vlan_packet_rcvd = false;
1258 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1259 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
1260 /* Push and remove the vlan tag */
1261 struct vlan_hdr *vlan_header =
1262 (struct vlan_hdr *) (data + ETH_HLEN);
1263 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1264 pkt_len -= VLAN_HLEN;
1265
1266 vlan_packet_rcvd = true;
1267 }
1268
Sascha Hauer22f6b862009-04-15 01:32:18 +00001269 /* This does 16 byte alignment, exactly what we need.
1270 * The packet length includes FCS, but we don't want to
1271 * include that when passing upstream as it messes up
1272 * bridging applications.
1273 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001274 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Sascha Hauer85498892009-04-15 01:32:21 +00001276 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001277 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001278 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001279 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001280 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001281 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001282
1283 /* Extract the frame data without the VLAN header. */
1284 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1285 if (vlan_packet_rcvd)
1286 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
1287 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
1288 data + payload_offset,
1289 pkt_len - 4 - (2 * ETH_ALEN));
1290
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001291 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001292
Frank Li6605b732012-10-30 18:25:31 +00001293 /* Get receive timestamp from the skb */
Frank Liff43da82013-01-03 16:04:23 +00001294 if (fep->hwts_rx_en && fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001295 struct skb_shared_hwtstamps *shhwtstamps =
1296 skb_hwtstamps(skb);
1297 unsigned long flags;
1298
1299 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1300
1301 spin_lock_irqsave(&fep->tmreg_lock, flags);
1302 shhwtstamps->hwtstamp = ns_to_ktime(
Frank Liff43da82013-01-03 16:04:23 +00001303 timecounter_cyc2time(&fep->tc, ebdp->ts));
Frank Li6605b732012-10-30 18:25:31 +00001304 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1305 }
Frank Liff43da82013-01-03 16:04:23 +00001306
Jim Baxter4c09eed2013-04-19 08:10:49 +00001307 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001308 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001309 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1310 /* don't check it */
1311 skb->ip_summed = CHECKSUM_UNNECESSARY;
1312 } else {
1313 skb_checksum_none_assert(skb);
1314 }
1315 }
1316
Jim Baxtercdffcf12013-07-02 22:52:56 +01001317 /* Handle received VLAN packets */
1318 if (vlan_packet_rcvd)
1319 __vlan_hwaccel_put_tag(skb,
1320 htons(ETH_P_8021Q),
1321 vlan_tag);
1322
Richard Cochran0affdf32013-08-30 20:28:10 +02001323 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001324 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001325
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001326 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1327 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001328rx_processing_done:
1329 /* Clear the status flags for this buffer */
1330 status &= ~BD_ENET_RX_STATS;
1331
1332 /* Mark the buffer empty */
1333 status |= BD_ENET_RX_EMPTY;
1334 bdp->cbd_sc = status;
1335
Frank Liff43da82013-01-03 16:04:23 +00001336 if (fep->bufdesc_ex) {
1337 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1338
1339 ebdp->cbd_esc = BD_ENET_RX_INT;
1340 ebdp->cbd_prot = 0;
1341 ebdp->cbd_bdu = 0;
1342 }
Frank Li6605b732012-10-30 18:25:31 +00001343
Sascha Hauer22f6b862009-04-15 01:32:18 +00001344 /* Update BD pointer to next entry */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001345 bdp = fec_enet_get_nextdesc(bdp, fep);
1346
Sascha Hauer22f6b862009-04-15 01:32:18 +00001347 /* Doing this here will keep the FEC running while we process
1348 * incoming frames. On a heavily loaded network, we should be
1349 * able to keep up at the expense of system resources.
1350 */
1351 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Sascha Hauer2e285322009-04-15 01:32:16 +00001353 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Frank Lidc975382013-01-28 18:31:42 +00001355 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Uwe Kleine-König45993652011-01-19 20:47:04 +01001358static irqreturn_t
1359fec_enet_interrupt(int irq, void *dev_id)
1360{
1361 struct net_device *ndev = dev_id;
1362 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001363 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001364 uint int_events;
1365 irqreturn_t ret = IRQ_NONE;
1366
Russell King7a168072014-07-08 00:22:44 +01001367 int_events = readl(fep->hwp + FEC_IEVENT);
1368 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001369
Russell King7a168072014-07-08 00:22:44 +01001370 if (int_events & napi_mask) {
1371 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001372
Russell King7a168072014-07-08 00:22:44 +01001373 /* Disable the NAPI interrupts */
1374 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1375 napi_schedule(&fep->napi);
1376 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001377
Russell King7a168072014-07-08 00:22:44 +01001378 if (int_events & FEC_ENET_MII) {
1379 ret = IRQ_HANDLED;
1380 complete(&fep->mdio_done);
1381 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001382
1383 return ret;
1384}
1385
Frank Lidc975382013-01-28 18:31:42 +00001386static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1387{
1388 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001389 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001390 int pkts;
1391
1392 /*
1393 * Clear any pending transmit or receive interrupts before
1394 * processing the rings to avoid racing with the hardware.
1395 */
1396 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1397
1398 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001399
Frank Lide5fb0a2013-03-03 17:34:25 +00001400 fec_enet_tx(ndev);
1401
Frank Lidc975382013-01-28 18:31:42 +00001402 if (pkts < budget) {
1403 napi_complete(napi);
1404 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1405 }
1406 return pkts;
1407}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001410static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001412 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001413 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001414 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Shawn Guo49da97d2011-01-05 21:13:11 +00001416 /*
1417 * try to get mac address in following order:
1418 *
1419 * 1) module parameter via kernel command line in form
1420 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1421 */
1422 iap = macaddr;
1423
1424 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001425 * 2) from device tree data
1426 */
1427 if (!is_valid_ether_addr(iap)) {
1428 struct device_node *np = fep->pdev->dev.of_node;
1429 if (np) {
1430 const char *mac = of_get_mac_address(np);
1431 if (mac)
1432 iap = (unsigned char *) mac;
1433 }
1434 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001435
1436 /*
1437 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001438 */
1439 if (!is_valid_ether_addr(iap)) {
1440#ifdef CONFIG_M5272
1441 if (FEC_FLASHMAC)
1442 iap = (unsigned char *)FEC_FLASHMAC;
1443#else
1444 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001445 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001446#endif
1447 }
1448
1449 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001450 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001451 */
1452 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001453 *((__be32 *) &tmpaddr[0]) =
1454 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1455 *((__be16 *) &tmpaddr[4]) =
1456 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 iap = &tmpaddr[0];
1458 }
1459
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001460 /*
1461 * 5) random mac address
1462 */
1463 if (!is_valid_ether_addr(iap)) {
1464 /* Report it and use a random ethernet address instead */
1465 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1466 eth_hw_addr_random(ndev);
1467 netdev_info(ndev, "Using random MAC address: %pM\n",
1468 ndev->dev_addr);
1469 return;
1470 }
1471
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001472 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Shawn Guo49da97d2011-01-05 21:13:11 +00001474 /* Adjust MAC if using macaddr */
1475 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001476 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479/* ------------------------------------------------------------------------- */
1480
Bryan Wue6b043d2010-03-31 02:10:44 +00001481/*
1482 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001484static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001486 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001487 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001488 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Bryan Wue6b043d2010-03-31 02:10:44 +00001490 /* Prevent a state halted on mii error */
1491 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1492 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001495
Russell King8ce56242014-07-08 12:40:07 +01001496 /*
1497 * If the netdev is down, or is going down, we're not interested
1498 * in link state events, so just mark our idea of the link as down
1499 * and ignore the event.
1500 */
1501 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1502 fep->link = 0;
1503 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001504 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001505 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001506 status_change = 1;
1507 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001508
Russell Kingef833372014-07-08 12:40:43 +01001509 if (fep->full_duplex != phy_dev->duplex) {
1510 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001511 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001512 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001513
1514 if (phy_dev->speed != fep->speed) {
1515 fep->speed = phy_dev->speed;
1516 status_change = 1;
1517 }
1518
1519 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001520 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001521 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001522 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001523 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001524 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001525 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001526 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001527 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001528 } else {
1529 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001530 napi_disable(&fep->napi);
1531 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001532 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001533 netif_tx_unlock_bh(ndev);
1534 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001535 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001536 status_change = 1;
1537 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001538 }
1539
Bryan Wue6b043d2010-03-31 02:10:44 +00001540 if (status_change)
1541 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Bryan Wue6b043d2010-03-31 02:10:44 +00001544static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Bryan Wue6b043d2010-03-31 02:10:44 +00001546 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001547 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001548
1549 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001550 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001551
1552 /* start a read op */
1553 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1554 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1555 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1556
1557 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001558 time_left = wait_for_completion_timeout(&fep->mdio_done,
1559 usecs_to_jiffies(FEC_MII_TIMEOUT));
1560 if (time_left == 0) {
1561 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001562 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001563 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001564 }
1565
1566 /* return value */
1567 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1568}
1569
1570static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1571 u16 value)
1572{
1573 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001574 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001575
1576 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001577 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001578
Shawn Guo862f0982011-01-05 21:13:09 +00001579 /* start a write op */
1580 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001581 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1582 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1583 fep->hwp + FEC_MII_DATA);
1584
1585 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001586 time_left = wait_for_completion_timeout(&fep->mdio_done,
1587 usecs_to_jiffies(FEC_MII_TIMEOUT));
1588 if (time_left == 0) {
1589 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001590 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001591 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001592 }
1593
1594 return 0;
1595}
1596
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001597static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1598{
1599 struct fec_enet_private *fep = netdev_priv(ndev);
1600 int ret;
1601
1602 if (enable) {
1603 ret = clk_prepare_enable(fep->clk_ahb);
1604 if (ret)
1605 return ret;
1606 ret = clk_prepare_enable(fep->clk_ipg);
1607 if (ret)
1608 goto failed_clk_ipg;
1609 if (fep->clk_enet_out) {
1610 ret = clk_prepare_enable(fep->clk_enet_out);
1611 if (ret)
1612 goto failed_clk_enet_out;
1613 }
1614 if (fep->clk_ptp) {
1615 ret = clk_prepare_enable(fep->clk_ptp);
1616 if (ret)
1617 goto failed_clk_ptp;
1618 }
1619 } else {
1620 clk_disable_unprepare(fep->clk_ahb);
1621 clk_disable_unprepare(fep->clk_ipg);
1622 if (fep->clk_enet_out)
1623 clk_disable_unprepare(fep->clk_enet_out);
1624 if (fep->clk_ptp)
1625 clk_disable_unprepare(fep->clk_ptp);
1626 }
1627
1628 return 0;
1629failed_clk_ptp:
1630 if (fep->clk_enet_out)
1631 clk_disable_unprepare(fep->clk_enet_out);
1632failed_clk_enet_out:
1633 clk_disable_unprepare(fep->clk_ipg);
1634failed_clk_ipg:
1635 clk_disable_unprepare(fep->clk_ahb);
1636
1637 return ret;
1638}
1639
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001640static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001641{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001642 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001643 const struct platform_device_id *id_entry =
1644 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001645 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001646 char mdio_bus_id[MII_BUS_ID_SIZE];
1647 char phy_name[MII_BUS_ID_SIZE + 3];
1648 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001649 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001650
Bryan Wu418bd0d2010-05-28 03:40:39 -07001651 fep->phy_dev = NULL;
1652
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001653 /* check for attached phy */
1654 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1655 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1656 continue;
1657 if (fep->mii_bus->phy_map[phy_id] == NULL)
1658 continue;
1659 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1660 continue;
Shawn Guob5680e02011-01-05 21:13:13 +00001661 if (dev_id--)
1662 continue;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001663 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1664 break;
Bryan Wue6b043d2010-03-31 02:10:44 +00001665 }
1666
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001667 if (phy_id >= PHY_MAX_ADDR) {
Joe Perches31b77202013-04-13 19:03:17 +00001668 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
Florian Fainelliea51ade92012-02-13 01:23:22 +00001669 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001670 phy_id = 0;
1671 }
1672
Richard Zhaoa7ed07d2012-01-29 22:08:12 +00001673 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainellif9a8f832013-01-14 00:52:52 +00001674 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
Shawn Guo230dec62011-09-23 02:12:48 +00001675 fep->phy_interface);
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001676 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001677 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001678 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001679 }
1680
1681 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001682 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001683 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001684 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001685#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001686 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001687#endif
Frank Libaa70a52013-01-16 16:55:58 +00001688 }
Shawn Guo230dec62011-09-23 02:12:48 +00001689 else
1690 phy_dev->supported &= PHY_BASIC_FEATURES;
1691
Bryan Wue6b043d2010-03-31 02:10:44 +00001692 phy_dev->advertising = phy_dev->supported;
1693
1694 fep->phy_dev = phy_dev;
1695 fep->link = 0;
1696 fep->full_duplex = 0;
1697
Joe Perches31b77202013-04-13 19:03:17 +00001698 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1699 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1700 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001701
Bryan Wue6b043d2010-03-31 02:10:44 +00001702 return 0;
1703}
1704
1705static int fec_enet_mii_init(struct platform_device *pdev)
1706{
Shawn Guob5680e02011-01-05 21:13:13 +00001707 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001708 struct net_device *ndev = platform_get_drvdata(pdev);
1709 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001710 const struct platform_device_id *id_entry =
1711 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001712 int err = -ENXIO, i;
1713
Shawn Guob5680e02011-01-05 21:13:13 +00001714 /*
1715 * The dual fec interfaces are not equivalent with enet-mac.
1716 * Here are the differences:
1717 *
1718 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1719 * - fec0 acts as the 1588 time master while fec1 is slave
1720 * - external phys can only be configured by fec0
1721 *
1722 * That is to say fec1 can not work independently. It only works
1723 * when fec0 is working. The reason behind this design is that the
1724 * second interface is added primarily for Switch mode.
1725 *
1726 * Because of the last point above, both phys are attached on fec0
1727 * mdio interface in board design, and need to be configured by
1728 * fec0 mii_bus.
1729 */
Shawn Guo43af9402011-12-05 05:01:15 +00001730 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001731 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001732 if (mii_cnt && fec0_mii_bus) {
1733 fep->mii_bus = fec0_mii_bus;
1734 mii_cnt++;
1735 return 0;
1736 }
1737 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001738 }
1739
Bryan Wue6b043d2010-03-31 02:10:44 +00001740 fep->mii_timeout = 0;
1741
1742 /*
1743 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001744 *
1745 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1746 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1747 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1748 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001749 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001750 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001751 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1752 fep->phy_speed--;
1753 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001754 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1755
1756 fep->mii_bus = mdiobus_alloc();
1757 if (fep->mii_bus == NULL) {
1758 err = -ENOMEM;
1759 goto err_out;
1760 }
1761
1762 fep->mii_bus->name = "fec_enet_mii_bus";
1763 fep->mii_bus->read = fec_enet_mdio_read;
1764 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001765 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1766 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001767 fep->mii_bus->priv = fep;
1768 fep->mii_bus->parent = &pdev->dev;
1769
1770 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1771 if (!fep->mii_bus->irq) {
1772 err = -ENOMEM;
1773 goto err_out_free_mdiobus;
1774 }
1775
1776 for (i = 0; i < PHY_MAX_ADDR; i++)
1777 fep->mii_bus->irq[i] = PHY_POLL;
1778
Bryan Wue6b043d2010-03-31 02:10:44 +00001779 if (mdiobus_register(fep->mii_bus))
1780 goto err_out_free_mdio_irq;
1781
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001782 mii_cnt++;
1783
Shawn Guob5680e02011-01-05 21:13:13 +00001784 /* save fec0 mii_bus */
1785 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1786 fec0_mii_bus = fep->mii_bus;
1787
Bryan Wue6b043d2010-03-31 02:10:44 +00001788 return 0;
1789
Bryan Wue6b043d2010-03-31 02:10:44 +00001790err_out_free_mdio_irq:
1791 kfree(fep->mii_bus->irq);
1792err_out_free_mdiobus:
1793 mdiobus_free(fep->mii_bus);
1794err_out:
1795 return err;
1796}
1797
1798static void fec_enet_mii_remove(struct fec_enet_private *fep)
1799{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001800 if (--mii_cnt == 0) {
1801 mdiobus_unregister(fep->mii_bus);
1802 kfree(fep->mii_bus->irq);
1803 mdiobus_free(fep->mii_bus);
1804 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001805}
1806
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001807static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001808 struct ethtool_cmd *cmd)
1809{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001810 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001811 struct phy_device *phydev = fep->phy_dev;
1812
1813 if (!phydev)
1814 return -ENODEV;
1815
1816 return phy_ethtool_gset(phydev, cmd);
1817}
1818
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001819static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001820 struct ethtool_cmd *cmd)
1821{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001822 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001823 struct phy_device *phydev = fep->phy_dev;
1824
1825 if (!phydev)
1826 return -ENODEV;
1827
1828 return phy_ethtool_sset(phydev, cmd);
1829}
1830
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001831static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001832 struct ethtool_drvinfo *info)
1833{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001834 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jiri Pirko7826d432013-01-06 00:44:26 +00001836 strlcpy(info->driver, fep->pdev->dev.driver->name,
1837 sizeof(info->driver));
1838 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1839 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
Bryan Wue6b043d2010-03-31 02:10:44 +00001841
Frank Li5ebae4892013-01-06 16:25:07 +00001842static int fec_enet_get_ts_info(struct net_device *ndev,
1843 struct ethtool_ts_info *info)
1844{
1845 struct fec_enet_private *fep = netdev_priv(ndev);
1846
1847 if (fep->bufdesc_ex) {
1848
1849 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1850 SOF_TIMESTAMPING_RX_SOFTWARE |
1851 SOF_TIMESTAMPING_SOFTWARE |
1852 SOF_TIMESTAMPING_TX_HARDWARE |
1853 SOF_TIMESTAMPING_RX_HARDWARE |
1854 SOF_TIMESTAMPING_RAW_HARDWARE;
1855 if (fep->ptp_clock)
1856 info->phc_index = ptp_clock_index(fep->ptp_clock);
1857 else
1858 info->phc_index = -1;
1859
1860 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1861 (1 << HWTSTAMP_TX_ON);
1862
1863 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1864 (1 << HWTSTAMP_FILTER_ALL);
1865 return 0;
1866 } else {
1867 return ethtool_op_get_ts_info(ndev, info);
1868 }
1869}
1870
Guenter Roeckd1391932013-06-18 10:04:59 -07001871#if !defined(CONFIG_M5272)
1872
Frank Libaa70a52013-01-16 16:55:58 +00001873static void fec_enet_get_pauseparam(struct net_device *ndev,
1874 struct ethtool_pauseparam *pause)
1875{
1876 struct fec_enet_private *fep = netdev_priv(ndev);
1877
1878 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1879 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1880 pause->rx_pause = pause->tx_pause;
1881}
1882
1883static int fec_enet_set_pauseparam(struct net_device *ndev,
1884 struct ethtool_pauseparam *pause)
1885{
1886 struct fec_enet_private *fep = netdev_priv(ndev);
1887
Russell King0b146ca2014-07-08 00:22:59 +01001888 if (!fep->phy_dev)
1889 return -ENODEV;
1890
Frank Libaa70a52013-01-16 16:55:58 +00001891 if (pause->tx_pause != pause->rx_pause) {
1892 netdev_info(ndev,
1893 "hardware only support enable/disable both tx and rx");
1894 return -EINVAL;
1895 }
1896
1897 fep->pause_flag = 0;
1898
1899 /* tx pause must be same as rx pause */
1900 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1901 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1902
1903 if (pause->rx_pause || pause->autoneg) {
1904 fep->phy_dev->supported |= ADVERTISED_Pause;
1905 fep->phy_dev->advertising |= ADVERTISED_Pause;
1906 } else {
1907 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1908 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1909 }
1910
1911 if (pause->autoneg) {
1912 if (netif_running(ndev))
1913 fec_stop(ndev);
1914 phy_start_aneg(fep->phy_dev);
1915 }
Russell Kingdbc64a82014-07-08 12:40:12 +01001916 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001917 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001918 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001919 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001920 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001921 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001922 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001923 }
Frank Libaa70a52013-01-16 16:55:58 +00001924
1925 return 0;
1926}
1927
Chris Healy38ae92d2013-06-25 23:18:52 -07001928static const struct fec_stat {
1929 char name[ETH_GSTRING_LEN];
1930 u16 offset;
1931} fec_stats[] = {
1932 /* RMON TX */
1933 { "tx_dropped", RMON_T_DROP },
1934 { "tx_packets", RMON_T_PACKETS },
1935 { "tx_broadcast", RMON_T_BC_PKT },
1936 { "tx_multicast", RMON_T_MC_PKT },
1937 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1938 { "tx_undersize", RMON_T_UNDERSIZE },
1939 { "tx_oversize", RMON_T_OVERSIZE },
1940 { "tx_fragment", RMON_T_FRAG },
1941 { "tx_jabber", RMON_T_JAB },
1942 { "tx_collision", RMON_T_COL },
1943 { "tx_64byte", RMON_T_P64 },
1944 { "tx_65to127byte", RMON_T_P65TO127 },
1945 { "tx_128to255byte", RMON_T_P128TO255 },
1946 { "tx_256to511byte", RMON_T_P256TO511 },
1947 { "tx_512to1023byte", RMON_T_P512TO1023 },
1948 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
1949 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
1950 { "tx_octets", RMON_T_OCTETS },
1951
1952 /* IEEE TX */
1953 { "IEEE_tx_drop", IEEE_T_DROP },
1954 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
1955 { "IEEE_tx_1col", IEEE_T_1COL },
1956 { "IEEE_tx_mcol", IEEE_T_MCOL },
1957 { "IEEE_tx_def", IEEE_T_DEF },
1958 { "IEEE_tx_lcol", IEEE_T_LCOL },
1959 { "IEEE_tx_excol", IEEE_T_EXCOL },
1960 { "IEEE_tx_macerr", IEEE_T_MACERR },
1961 { "IEEE_tx_cserr", IEEE_T_CSERR },
1962 { "IEEE_tx_sqe", IEEE_T_SQE },
1963 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
1964 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
1965
1966 /* RMON RX */
1967 { "rx_packets", RMON_R_PACKETS },
1968 { "rx_broadcast", RMON_R_BC_PKT },
1969 { "rx_multicast", RMON_R_MC_PKT },
1970 { "rx_crc_errors", RMON_R_CRC_ALIGN },
1971 { "rx_undersize", RMON_R_UNDERSIZE },
1972 { "rx_oversize", RMON_R_OVERSIZE },
1973 { "rx_fragment", RMON_R_FRAG },
1974 { "rx_jabber", RMON_R_JAB },
1975 { "rx_64byte", RMON_R_P64 },
1976 { "rx_65to127byte", RMON_R_P65TO127 },
1977 { "rx_128to255byte", RMON_R_P128TO255 },
1978 { "rx_256to511byte", RMON_R_P256TO511 },
1979 { "rx_512to1023byte", RMON_R_P512TO1023 },
1980 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
1981 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
1982 { "rx_octets", RMON_R_OCTETS },
1983
1984 /* IEEE RX */
1985 { "IEEE_rx_drop", IEEE_R_DROP },
1986 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
1987 { "IEEE_rx_crc", IEEE_R_CRC },
1988 { "IEEE_rx_align", IEEE_R_ALIGN },
1989 { "IEEE_rx_macerr", IEEE_R_MACERR },
1990 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
1991 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
1992};
1993
1994static void fec_enet_get_ethtool_stats(struct net_device *dev,
1995 struct ethtool_stats *stats, u64 *data)
1996{
1997 struct fec_enet_private *fep = netdev_priv(dev);
1998 int i;
1999
2000 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2001 data[i] = readl(fep->hwp + fec_stats[i].offset);
2002}
2003
2004static void fec_enet_get_strings(struct net_device *netdev,
2005 u32 stringset, u8 *data)
2006{
2007 int i;
2008 switch (stringset) {
2009 case ETH_SS_STATS:
2010 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2011 memcpy(data + i * ETH_GSTRING_LEN,
2012 fec_stats[i].name, ETH_GSTRING_LEN);
2013 break;
2014 }
2015}
2016
2017static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2018{
2019 switch (sset) {
2020 case ETH_SS_STATS:
2021 return ARRAY_SIZE(fec_stats);
2022 default:
2023 return -EOPNOTSUPP;
2024 }
2025}
Guenter Roeckd1391932013-06-18 10:04:59 -07002026#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002027
Chris Healy32bc9b42013-06-17 07:25:06 -07002028static int fec_enet_nway_reset(struct net_device *dev)
2029{
2030 struct fec_enet_private *fep = netdev_priv(dev);
2031 struct phy_device *phydev = fep->phy_dev;
2032
2033 if (!phydev)
2034 return -ENODEV;
2035
2036 return genphy_restart_aneg(phydev);
2037}
2038
stephen hemminger9b07be42012-01-04 12:59:49 +00002039static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002040 .get_settings = fec_enet_get_settings,
2041 .set_settings = fec_enet_set_settings,
2042 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002043 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002044 .get_link = ethtool_op_get_link,
Chris Healy38ae92d2013-06-25 23:18:52 -07002045#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002046 .get_pauseparam = fec_enet_get_pauseparam,
2047 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002048 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002049 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002050 .get_sset_count = fec_enet_get_sset_count,
2051#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002052 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00002053};
2054
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002055static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002056{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002057 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002058 struct phy_device *phydev = fep->phy_dev;
2059
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002060 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002061 return -EINVAL;
2062
2063 if (!phydev)
2064 return -ENODEV;
2065
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002066 if (fep->bufdesc_ex) {
2067 if (cmd == SIOCSHWTSTAMP)
2068 return fec_ptp_set(ndev, rq);
2069 if (cmd == SIOCGHWTSTAMP)
2070 return fec_ptp_get(ndev, rq);
2071 }
Frank Liff43da82013-01-03 16:04:23 +00002072
Richard Cochran28b04112010-07-17 08:48:55 +00002073 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002076static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002077{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002078 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002079 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002080 struct sk_buff *skb;
2081 struct bufdesc *bdp;
2082
2083 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002084 for (i = 0; i < fep->rx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002085 skb = fep->rx_skbuff[i];
Russell King730ee362014-07-08 00:23:14 +01002086 fep->rx_skbuff[i] = NULL;
2087 if (skb) {
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01002088 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002089 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002090 dev_kfree_skb(skb);
Russell King730ee362014-07-08 00:23:14 +01002091 }
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002092 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002093 }
2094
2095 bdp = fep->tx_bd_base;
Russell King8b7c9ef2014-07-08 00:23:25 +01002096 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002097 kfree(fep->tx_bounce[i]);
Russell King8b7c9ef2014-07-08 00:23:25 +01002098 fep->tx_bounce[i] = NULL;
2099 skb = fep->tx_skbuff[i];
2100 fep->tx_skbuff[i] = NULL;
2101 dev_kfree_skb(skb);
2102 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002103}
2104
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002105static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002106{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002107 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002108 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002109 struct sk_buff *skb;
2110 struct bufdesc *bdp;
2111
2112 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002113 for (i = 0; i < fep->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002114 dma_addr_t addr;
2115
Fabio Estevamb72061a2012-02-07 08:27:31 +00002116 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002117 if (!skb)
2118 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002119
Russell King730ee362014-07-08 00:23:14 +01002120 addr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002121 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Russell King730ee362014-07-08 00:23:14 +01002122 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2123 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002124 if (net_ratelimit())
2125 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002126 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002127 }
Russell King730ee362014-07-08 00:23:14 +01002128
2129 fep->rx_skbuff[i] = skb;
2130 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002131 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002132
2133 if (fep->bufdesc_ex) {
2134 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2135 ebdp->cbd_esc = BD_ENET_RX_INT;
2136 }
2137
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002138 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002139 }
2140
2141 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002142 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002143 bdp->cbd_sc |= BD_SC_WRAP;
2144
2145 bdp = fep->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002146 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002147 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
Russell Kingffdce2c2014-07-08 00:23:30 +01002148 if (!fep->tx_bounce[i])
2149 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002150
2151 bdp->cbd_sc = 0;
2152 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002153
Frank Liff43da82013-01-03 16:04:23 +00002154 if (fep->bufdesc_ex) {
2155 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002156 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002157 }
2158
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002159 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002160 }
2161
2162 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002163 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002164 bdp->cbd_sc |= BD_SC_WRAP;
2165
2166 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002167
2168 err_alloc:
2169 fec_enet_free_buffers(ndev);
2170 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002171}
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002174fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002176 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002177 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002179 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002180 ret = fec_enet_clk_enable(ndev, true);
2181 if (ret)
2182 return ret;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* I should reset the ring buffers here, but I don't yet know
2185 * a simple way to do that.
2186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002188 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002189 if (ret)
2190 return ret;
2191
Bryan Wu418bd0d2010-05-28 03:40:39 -07002192 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002193 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002194 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002195 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002196 return ret;
2197 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002198
Russell Kingef833372014-07-08 12:40:43 +01002199 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002200 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002201 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002202 netif_start_queue(ndev);
Sascha Hauer22f6b862009-04-15 01:32:18 +00002203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
2206static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002207fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002209 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Russell Kingd76cfae2014-07-08 00:23:04 +01002211 phy_stop(fep->phy_dev);
2212
Russell King31a6de32014-07-08 12:40:23 +01002213 if (netif_device_present(ndev)) {
2214 napi_disable(&fep->napi);
2215 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002216 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Russell King635cf172014-07-08 00:22:54 +01002219 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002220 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002221
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002222 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002223 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002224 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return 0;
2227}
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229/* Set or clear the multicast filter for this adaptor.
2230 * Skeleton taken from sunlance driver.
2231 * The CPM Ethernet implementation allows Multicast as well as individual
2232 * MAC address filtering. Some of the drivers check to make sure it is
2233 * a group multicast address, and discard those that are not. I guess I
2234 * will do the same for now, but just remove the test if you want
2235 * individual filtering as well (do the upper net layers want or support
2236 * this kind of feature?).
2237 */
2238
2239#define HASH_BITS 6 /* #bits in hash */
2240#define CRC32_POLY 0xEDB88320
2241
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002242static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002244 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002245 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002246 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 unsigned char hash;
2248
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002249 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002250 tmp = readl(fep->hwp + FEC_R_CNTRL);
2251 tmp |= 0x8;
2252 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002253 return;
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Sascha Hauer4e831832009-04-15 01:32:19 +00002256 tmp = readl(fep->hwp + FEC_R_CNTRL);
2257 tmp &= ~0x8;
2258 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002259
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002260 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002261 /* Catch all multicast addresses, so set the
2262 * filter to all 1's
2263 */
2264 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2265 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Sascha Hauer4e831832009-04-15 01:32:19 +00002267 return;
2268 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002269
Sascha Hauer4e831832009-04-15 01:32:19 +00002270 /* Clear filter and add the addresses in hash register
2271 */
2272 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2273 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002275 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002276 /* calculate crc32 value of mac address */
2277 crc = 0xffffffff;
2278
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002279 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002280 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002281 for (bit = 0; bit < 8; bit++, data >>= 1) {
2282 crc = (crc >> 1) ^
2283 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002286
2287 /* only upper 6 bits (HASH_BITS) are used
2288 * which point to specific bit in he hash registers
2289 */
2290 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2291
2292 if (hash > 31) {
2293 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2294 tmp |= 1 << (hash - 32);
2295 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2296 } else {
2297 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2298 tmp |= 1 << hash;
2299 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302}
2303
Sascha Hauer22f6b862009-04-15 01:32:18 +00002304/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002305static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002306fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002308 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002309 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Lucas Stach44934fa2014-03-30 21:32:08 +02002311 if (addr) {
2312 if (!is_valid_ether_addr(addr->sa_data))
2313 return -EADDRNOTAVAIL;
2314 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2315 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002316
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002317 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2318 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002319 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002320 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002321 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002325#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002326/**
2327 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002328 * @dev: The FEC network adapter
2329 *
2330 * Polled functionality used by netconsole and others in non interrupt mode
2331 *
2332 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002333static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002334{
2335 int i;
2336 struct fec_enet_private *fep = netdev_priv(dev);
2337
2338 for (i = 0; i < FEC_IRQ_NUM; i++) {
2339 if (fep->irq[i] > 0) {
2340 disable_irq(fep->irq[i]);
2341 fec_enet_interrupt(fep->irq[i], dev);
2342 enable_irq(fep->irq[i]);
2343 }
2344 }
2345}
2346#endif
2347
Russell King8506fa12014-07-08 12:40:33 +01002348#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2349
Jim Baxter4c09eed2013-04-19 08:10:49 +00002350static int fec_set_features(struct net_device *netdev,
2351 netdev_features_t features)
2352{
2353 struct fec_enet_private *fep = netdev_priv(netdev);
2354 netdev_features_t changed = features ^ netdev->features;
2355
Russell King8506fa12014-07-08 12:40:33 +01002356 /* Quiesce the device if necessary */
2357 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2358 napi_disable(&fep->napi);
2359 netif_tx_lock_bh(netdev);
2360 fec_stop(netdev);
2361 }
2362
Jim Baxter4c09eed2013-04-19 08:10:49 +00002363 netdev->features = features;
2364
2365 /* Receive checksum has been changed */
2366 if (changed & NETIF_F_RXCSUM) {
2367 if (features & NETIF_F_RXCSUM)
2368 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2369 else
2370 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002371 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002372
Russell King8506fa12014-07-08 12:40:33 +01002373 /* Resume the device after updates */
2374 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002375 fec_restart(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002376 netif_wake_queue(netdev);
2377 netif_tx_unlock_bh(netdev);
2378 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002379 }
2380
2381 return 0;
2382}
2383
Sascha Hauer009fda82009-04-15 01:32:23 +00002384static const struct net_device_ops fec_netdev_ops = {
2385 .ndo_open = fec_enet_open,
2386 .ndo_stop = fec_enet_close,
2387 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002388 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002389 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002390 .ndo_validate_addr = eth_validate_addr,
2391 .ndo_tx_timeout = fec_timeout,
2392 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002393 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002394#ifdef CONFIG_NET_POLL_CONTROLLER
2395 .ndo_poll_controller = fec_poll_controller,
2396#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002397 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002398};
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /*
2401 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002402 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002404static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002406 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002407 const struct platform_device_id *id_entry =
2408 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002409 struct bufdesc *cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002410 int bd_size;
2411
2412 /* init the tx & rx ring size */
2413 fep->tx_ring_size = TX_RING_SIZE;
2414 fep->rx_ring_size = RX_RING_SIZE;
2415
Nimrod Andy79f33912014-06-12 08:16:23 +08002416 fep->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2417 fep->tx_wake_threshold = (fep->tx_ring_size - fep->tx_stop_threshold) / 2;
2418
Nimrod Andy55d02182014-06-12 08:16:21 +08002419 if (fep->bufdesc_ex)
2420 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2421 else
2422 fep->bufdesc_size = sizeof(struct bufdesc);
2423 bd_size = (fep->tx_ring_size + fep->rx_ring_size) *
2424 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002426 /* Allocate memory for buffer descriptors. */
Nimrod Andy55d02182014-06-12 08:16:21 +08002427 cbd_base = dma_alloc_coherent(NULL, bd_size, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002428 GFP_KERNEL);
2429 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10002430 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10002431
Nimrod Andy79f33912014-06-12 08:16:23 +08002432 fep->tso_hdrs = dma_alloc_coherent(NULL, fep->tx_ring_size * TSO_HEADER_SIZE,
2433 &fep->tso_hdrs_dma, GFP_KERNEL);
2434 if (!fep->tso_hdrs) {
2435 dma_free_coherent(NULL, bd_size, cbd_base, fep->bd_dma);
2436 return -ENOMEM;
2437 }
2438
Frank Li14109a52013-03-26 16:12:03 +00002439 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002440
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002441 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Shawn Guo49da97d2011-01-05 21:13:11 +00002443 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002444 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002445 /* make sure MAC we just acquired is programmed into the hw */
2446 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002448 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 fep->rx_bd_base = cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002450 if (fep->bufdesc_ex)
Frank Liff43da82013-01-03 16:04:23 +00002451 fep->tx_bd_base = (struct bufdesc *)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002452 (((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
Nimrod Andy55d02182014-06-12 08:16:21 +08002453 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002454 fep->tx_bd_base = cbd_base + fep->rx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Sascha Hauer22f6b862009-04-15 01:32:18 +00002456 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002457 ndev->watchdog_timeo = TX_TIMEOUT;
2458 ndev->netdev_ops = &fec_netdev_ops;
2459 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002460
Frank Lidc975382013-01-28 18:31:42 +00002461 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002462 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002463
Nimrod Andy09d1e542014-06-12 08:16:20 +08002464 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002465 /* enable hw VLAN support */
2466 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002467
Shawn Guo48496252013-05-08 21:08:22 +00002468 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002469 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2470
Shawn Guo48496252013-05-08 21:08:22 +00002471 /* enable hw accelerator */
2472 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002473 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002474 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2475 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002476
Nimrod Andy09d1e542014-06-12 08:16:20 +08002477 ndev->hw_features = ndev->features;
2478
Russell Kingef833372014-07-08 12:40:43 +01002479 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return 0;
2482}
2483
Shawn Guoca2cc332011-06-25 02:04:35 +08002484#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002485static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002486{
2487 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002488 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002489 struct device_node *np = pdev->dev.of_node;
2490
2491 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002492 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002493
Shawn Guoa3caad02012-06-27 03:45:24 +00002494 of_property_read_u32(np, "phy-reset-duration", &msec);
2495 /* A sane reset duration should not be longer than 1s */
2496 if (msec > 1000)
2497 msec = 1;
2498
Shawn Guoca2cc332011-06-25 02:04:35 +08002499 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002500 if (!gpio_is_valid(phy_reset))
2501 return;
2502
Shawn Guo119fc002012-06-27 03:45:22 +00002503 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2504 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002505 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002506 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002507 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002508 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002509 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08002510 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002511}
2512#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002513static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002514{
2515 /*
2516 * In case of platform probe, the reset has been done
2517 * by machine code.
2518 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002519}
2520#endif /* CONFIG_OF */
2521
Bill Pemberton33897cc2012-12-03 09:23:58 -05002522static int
Sascha Haueread73182009-01-28 23:03:11 +00002523fec_probe(struct platform_device *pdev)
2524{
2525 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002526 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002527 struct net_device *ndev;
2528 int i, irq, ret = 0;
2529 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002530 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002531 static int dev_id;
Shawn Guoca2cc332011-06-25 02:04:35 +08002532
2533 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2534 if (of_id)
2535 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002536
Sascha Haueread73182009-01-28 23:03:11 +00002537 /* Init network device */
2538 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00002539 if (!ndev)
2540 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002541
2542 SET_NETDEV_DEV(ndev, &pdev->dev);
2543
2544 /* setup board info structure */
2545 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002546
Guenter Roeckd1391932013-06-18 10:04:59 -07002547#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002548 /* default enable pause frame auto negotiation */
2549 if (pdev->id_entry &&
2550 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2551 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002552#endif
Frank Libaa70a52013-01-16 16:55:58 +00002553
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002554 /* Select default pin state */
2555 pinctrl_pm_select_default_state(&pdev->dev);
2556
Fabio Estevam399db752013-07-21 13:25:03 -03002557 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302558 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2559 if (IS_ERR(fep->hwp)) {
2560 ret = PTR_ERR(fep->hwp);
2561 goto failed_ioremap;
2562 }
2563
Bryan Wue6b043d2010-03-31 02:10:44 +00002564 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002565 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002566
Frank Liff43da82013-01-03 16:04:23 +00002567 fep->bufdesc_ex = 0;
2568
Sascha Haueread73182009-01-28 23:03:11 +00002569 platform_set_drvdata(pdev, ndev);
2570
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002571 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002572 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002573 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002574 if (pdata)
2575 fep->phy_interface = pdata->phy;
2576 else
2577 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2578 } else {
2579 fep->phy_interface = ret;
2580 }
2581
Fabio Estevame2f8d552013-02-22 06:40:45 +00002582 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2583 if (IS_ERR(fep->clk_ipg)) {
2584 ret = PTR_ERR(fep->clk_ipg);
2585 goto failed_clk;
2586 }
2587
2588 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2589 if (IS_ERR(fep->clk_ahb)) {
2590 ret = PTR_ERR(fep->clk_ahb);
2591 goto failed_clk;
2592 }
2593
Linus Torvalds38f56f32013-05-07 11:06:17 -07002594 /* enet_out is optional, depends on board */
2595 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2596 if (IS_ERR(fep->clk_enet_out))
2597 fep->clk_enet_out = NULL;
2598
Fabio Estevame2f8d552013-02-22 06:40:45 +00002599 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002600 fep->bufdesc_ex =
2601 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002602 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07002603 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002604 fep->bufdesc_ex = 0;
2605 }
2606
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002607 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002608 if (ret)
2609 goto failed_clk;
2610
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00002611 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2612 if (!IS_ERR(fep->reg_phy)) {
2613 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002614 if (ret) {
2615 dev_err(&pdev->dev,
2616 "Failed to enable phy regulator: %d\n", ret);
2617 goto failed_regulator;
2618 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002619 } else {
2620 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002621 }
2622
2623 fec_reset_phy(pdev);
2624
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002625 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00002626 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002627
2628 ret = fec_enet_init(ndev);
2629 if (ret)
2630 goto failed_init;
2631
Xiao Jiangc7c83d12011-09-29 02:15:56 +00002632 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00002633 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00002634 if (irq < 0) {
2635 if (i)
2636 break;
2637 ret = irq;
2638 goto failed_irq;
2639 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002640 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02002641 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002642 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00002643 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00002644 }
2645
Bryan Wue6b043d2010-03-31 02:10:44 +00002646 ret = fec_enet_mii_init(pdev);
2647 if (ret)
2648 goto failed_mii_init;
2649
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002650 /* Carrier starts down, phylib will bring it up */
2651 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002652 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002653 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002654
Sascha Haueread73182009-01-28 23:03:11 +00002655 ret = register_netdev(ndev);
2656 if (ret)
2657 goto failed_register;
2658
Fabio Estevameb1d0642013-04-13 07:25:36 +00002659 if (fep->bufdesc_ex && fep->ptp_clock)
2660 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2661
Russell King36cdc742014-07-08 13:01:44 +01002662 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00002663 return 0;
2664
2665failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00002666 fec_enet_mii_remove(fep);
2667failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002668failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002669failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002670 if (fep->reg_phy)
2671 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00002672failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002673 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00002674failed_clk:
Sascha Haueread73182009-01-28 23:03:11 +00002675failed_ioremap:
2676 free_netdev(ndev);
2677
2678 return ret;
2679}
2680
Bill Pemberton33897cc2012-12-03 09:23:58 -05002681static int
Sascha Haueread73182009-01-28 23:03:11 +00002682fec_drv_remove(struct platform_device *pdev)
2683{
2684 struct net_device *ndev = platform_get_drvdata(pdev);
2685 struct fec_enet_private *fep = netdev_priv(ndev);
2686
Russell King36cdc742014-07-08 13:01:44 +01002687 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002688 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002689 fec_enet_mii_remove(fep);
Frank Li6605b732012-10-30 18:25:31 +00002690 del_timer_sync(&fep->time_keep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002691 if (fep->reg_phy)
2692 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00002693 if (fep->ptp_clock)
2694 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002695 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00002696 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002697
Sascha Haueread73182009-01-28 23:03:11 +00002698 return 0;
2699}
2700
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002701#ifdef CONFIG_PM_SLEEP
Sascha Haueread73182009-01-28 23:03:11 +00002702static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002703fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002704{
Eric Benard87cad5c2010-06-18 04:19:54 +00002705 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002706 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002707
Russell Kingda1774e2014-07-08 12:39:57 +01002708 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002709 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01002710 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01002711 napi_disable(&fep->napi);
2712 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002713 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002714 netif_tx_unlock_bh(ndev);
2715 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002716 }
Russell Kingda1774e2014-07-08 12:39:57 +01002717 rtnl_unlock();
2718
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002719 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002720 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002721
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002722 if (fep->reg_phy)
2723 regulator_disable(fep->reg_phy);
2724
Sascha Haueread73182009-01-28 23:03:11 +00002725 return 0;
2726}
2727
2728static int
Eric Benard87cad5c2010-06-18 04:19:54 +00002729fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002730{
Eric Benard87cad5c2010-06-18 04:19:54 +00002731 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002732 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002733 int ret;
2734
2735 if (fep->reg_phy) {
2736 ret = regulator_enable(fep->reg_phy);
2737 if (ret)
2738 return ret;
2739 }
Sascha Haueread73182009-01-28 23:03:11 +00002740
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002741 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002742 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002743 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002744 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002745
Russell Kingda1774e2014-07-08 12:39:57 +01002746 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002747 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01002748 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002749 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002750 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002751 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002752 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01002753 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00002754 }
Russell Kingda1774e2014-07-08 12:39:57 +01002755 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002756
Sascha Haueread73182009-01-28 23:03:11 +00002757 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002758
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002759failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03002760 if (fep->reg_phy)
2761 regulator_disable(fep->reg_phy);
2762 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00002763}
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002764#endif /* CONFIG_PM_SLEEP */
Sascha Haueread73182009-01-28 23:03:11 +00002765
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002766static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002767
Sascha Haueread73182009-01-28 23:03:11 +00002768static struct platform_driver fec_driver = {
2769 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002770 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002771 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002772 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002773 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002774 },
Shawn Guob5680e02011-01-05 21:13:13 +00002775 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002776 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002777 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002778};
2779
Fabio Estevamaaca2372012-01-23 16:44:50 +00002780module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03002782MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783MODULE_LICENSE("GPL");