blob: ee9f04ff18cb9ddda428f7c4e982d2f213080d89 [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>
Uwe Kleine-König407066f2014-08-11 17:35:33 +020055#include <linux/of_mdio.h>
Shawn Guoca2cc332011-06-25 02:04:35 +080056#include <linux/of_net.h>
Shawn Guo5fa9c0f2012-06-27 03:45:21 +000057#include <linux/regulator/consumer.h>
Jim Baxtercdffcf12013-07-02 22:52:56 +010058#include <linux/if_vlan.h>
Fabio Estevama68ab982014-06-02 15:44:30 -030059#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Greg Ungerer080853a2007-07-30 16:28:46 +100061#include <asm/cacheflush.h>
Sascha Hauer196719e2009-01-28 23:03:10 +000062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "fec.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Christoph Müllner772e42b2013-06-27 21:18:23 +020065static void set_multicast_list(struct net_device *ndev);
66
Uwe Kleine-König085e79e2011-01-17 09:52:18 +010067#if defined(CONFIG_ARM)
Sascha Hauer196719e2009-01-28 23:03:10 +000068#define FEC_ALIGNMENT 0xf
69#else
70#define FEC_ALIGNMENT 0x3
71#endif
72
Shawn Guob5680e02011-01-05 21:13:13 +000073#define DRIVER_NAME "fec"
74
Frank Libaa70a52013-01-16 16:55:58 +000075/* Pause frame feild and FIFO threshold */
76#define FEC_ENET_FCE (1 << 5)
77#define FEC_ENET_RSEM_V 0x84
78#define FEC_ENET_RSFL_V 16
79#define FEC_ENET_RAEM_V 0x8
80#define FEC_ENET_RAFL_V 0x8
81#define FEC_ENET_OPD_V 0xFFF0
82
Shawn Guob5680e02011-01-05 21:13:13 +000083/* Controller is ENET-MAC */
84#define FEC_QUIRK_ENET_MAC (1 << 0)
85/* Controller needs driver to swap frame */
86#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080087/* Controller uses gasket */
88#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000089/* Controller has GBIT support */
90#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000091/* Controller has extend desc buffer */
92#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000093/* Controller has hardware checksum support */
94#define FEC_QUIRK_HAS_CSUM (1 << 5)
Jim Baxtercdffcf12013-07-02 22:52:56 +010095/* Controller has hardware vlan support */
96#define FEC_QUIRK_HAS_VLAN (1 << 6)
Frank Li03191652013-07-25 14:05:53 +080097/* ENET IP errata ERR006358
98 *
99 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
100 * detected as not set during a prior frame transmission, then the
101 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
102 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
Frank Li03191652013-07-25 14:05:53 +0800103 * frames not being transmitted until there is a 0-to-1 transition on
104 * ENET_TDAR[TDAR].
105 */
106#define FEC_QUIRK_ERR006358 (1 << 7)
Fugang Duan95a77472014-09-13 05:00:47 +0800107/* ENET IP hw AVB
108 *
109 * i.MX6SX ENET IP add Audio Video Bridging (AVB) feature support.
110 * - Two class indicators on receive with configurable priority
111 * - Two class indicators and line speed timer on transmit allowing
112 * implementation class credit based shapers externally
113 * - Additional DMA registers provisioned to allow managing up to 3
114 * independent rings
115 */
116#define FEC_QUIRK_HAS_AVB (1 << 8)
Shawn Guob5680e02011-01-05 21:13:13 +0000117
118static struct platform_device_id fec_devtype[] = {
119 {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800120 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +0000121 .name = DRIVER_NAME,
122 .driver_data = 0,
123 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800124 .name = "imx25-fec",
125 .driver_data = FEC_QUIRK_USE_GASKET,
126 }, {
127 .name = "imx27-fec",
128 .driver_data = 0,
129 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000130 .name = "imx28-fec",
131 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800132 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000133 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000134 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100135 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800136 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000137 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000138 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000139 .driver_data = FEC_QUIRK_ENET_MAC,
140 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800141 .name = "imx6sx-fec",
142 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
143 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
144 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
145 FEC_QUIRK_HAS_AVB,
146 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800147 /* sentinel */
148 }
Shawn Guob5680e02011-01-05 21:13:13 +0000149};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800150MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000151
Shawn Guoca2cc332011-06-25 02:04:35 +0800152enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000153 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800154 IMX27_FEC, /* runs on i.mx27/35/51 */
155 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000156 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000157 MVF600_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800158};
159
160static const struct of_device_id fec_dt_ids[] = {
161 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
162 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
163 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000164 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000165 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800166 { /* sentinel */ }
167};
168MODULE_DEVICE_TABLE(of, fec_dt_ids);
169
Shawn Guo49da97d2011-01-05 21:13:11 +0000170static unsigned char macaddr[ETH_ALEN];
171module_param_array(macaddr, byte, NULL, 0);
172MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
173
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000174#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
176 * Some hardware gets it MAC address out of local flash memory.
177 * if this is non-zero then assume it is the address to get MAC from.
178 */
179#if defined(CONFIG_NETtel)
180#define FEC_FLASHMAC 0xf0006006
181#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
182#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#elif defined(CONFIG_CANCam)
184#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000185#elif defined (CONFIG_M5272C3)
186#define FEC_FLASHMAC (0xffe04000 + 4)
187#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000188#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#else
190#define FEC_FLASHMAC 0
191#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800192#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000193
Sascha Hauer22f6b862009-04-15 01:32:18 +0000194/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
196#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
197#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
198#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
199#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
200#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
201#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
202#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
203#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
204#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
205
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000206#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000207#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000208
Jim Baxtercdffcf12013-07-02 22:52:56 +0100209/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100211#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100213#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jim Baxter4c09eed2013-04-19 08:10:49 +0000215/* FEC receive acceleration */
216#define FEC_RACC_IPDIS (1 << 1)
217#define FEC_RACC_PRODIS (1 << 2)
218#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
Matt Waddel6b265292006-06-27 13:10:56 +1000221 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * size bits. Other FEC hardware does not, so we need to take that into
223 * account when setting it.
224 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000225#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100226 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
228#else
229#define OPT_FRAME_SIZE 0
230#endif
231
Bryan Wue6b043d2010-03-31 02:10:44 +0000232/* FEC MII MMFR bits definition */
233#define FEC_MMFR_ST (1 << 30)
234#define FEC_MMFR_OP_READ (2 << 28)
235#define FEC_MMFR_OP_WRITE (1 << 28)
236#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
237#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
238#define FEC_MMFR_TA (2 << 16)
239#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500241#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Sascha Hauer22f6b862009-04-15 01:32:18 +0000243/* Transmitter timeout */
244#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Frank Libaa70a52013-01-16 16:55:58 +0000246#define FEC_PAUSE_FLAG_AUTONEG 0x1
247#define FEC_PAUSE_FLAG_ENABLE 0x2
248
Nimrod Andy79f33912014-06-12 08:16:23 +0800249#define TSO_HEADER_SIZE 128
250/* Max number of allowed TCP segments for software TSO */
251#define FEC_MAX_TSO_SEGS 100
252#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
253
254#define IS_TSO_HEADER(txq, addr) \
255 ((addr >= txq->tso_hdrs_dma) && \
256 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
257
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000258static int mii_cnt;
259
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800260static inline
261struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
Frank Liff43da82013-01-03 16:04:23 +0000262{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800263 struct bufdesc *new_bd = bdp + 1;
264 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
265 struct bufdesc_ex *ex_base;
266 struct bufdesc *base;
267 int ring_size;
268
269 if (bdp >= fep->tx_bd_base) {
270 base = fep->tx_bd_base;
271 ring_size = fep->tx_ring_size;
272 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
273 } else {
274 base = fep->rx_bd_base;
275 ring_size = fep->rx_ring_size;
276 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
277 }
278
279 if (fep->bufdesc_ex)
280 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
281 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000282 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800283 return (new_bd >= (base + ring_size)) ?
284 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000285}
286
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800287static inline
288struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
Frank Liff43da82013-01-03 16:04:23 +0000289{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800290 struct bufdesc *new_bd = bdp - 1;
291 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
292 struct bufdesc_ex *ex_base;
293 struct bufdesc *base;
294 int ring_size;
295
296 if (bdp >= fep->tx_bd_base) {
297 base = fep->tx_bd_base;
298 ring_size = fep->tx_ring_size;
299 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
300 } else {
301 base = fep->rx_bd_base;
302 ring_size = fep->rx_ring_size;
303 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
304 }
305
306 if (fep->bufdesc_ex)
307 return (struct bufdesc *)((ex_new_bd < ex_base) ?
308 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000309 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800310 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000311}
312
Nimrod Andy61a44272014-06-12 08:16:18 +0800313static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
314 struct fec_enet_private *fep)
315{
316 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
317}
318
Nimrod Andy6e909282014-06-12 08:16:22 +0800319static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep)
320{
321 int entries;
322
323 entries = ((const char *)fep->dirty_tx -
324 (const char *)fep->cur_tx) / fep->bufdesc_size - 1;
325
326 return entries > 0 ? entries : entries + fep->tx_ring_size;
327}
328
Shawn Guob5680e02011-01-05 21:13:13 +0000329static void *swap_buffer(void *bufaddr, int len)
330{
331 int i;
332 unsigned int *buf = bufaddr;
333
Fabio Estevamffed61e2013-05-21 05:44:26 +0000334 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000335 *buf = cpu_to_be32(*buf);
336
337 return bufaddr;
338}
339
Russell King344756f2014-07-08 13:01:59 +0100340static void fec_dump(struct net_device *ndev)
341{
342 struct fec_enet_private *fep = netdev_priv(ndev);
343 struct bufdesc *bdp = fep->tx_bd_base;
344 unsigned int index = 0;
345
346 netdev_info(ndev, "TX ring dump\n");
347 pr_info("Nr SC addr len SKB\n");
348
349 do {
350 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
351 index,
352 bdp == fep->cur_tx ? 'S' : ' ',
353 bdp == fep->dirty_tx ? 'H' : ' ',
354 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
355 fep->tx_skbuff[index]);
356 bdp = fec_enet_get_nextdesc(bdp, fep);
357 index++;
358 } while (bdp != fep->tx_bd_base);
359}
360
Fugang Duan62a02c92014-06-18 08:33:52 +0800361static inline bool is_ipv4_pkt(struct sk_buff *skb)
362{
363 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
364}
365
Jim Baxter4c09eed2013-04-19 08:10:49 +0000366static int
367fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
368{
369 /* Only run for packets requiring a checksum. */
370 if (skb->ip_summed != CHECKSUM_PARTIAL)
371 return 0;
372
373 if (unlikely(skb_cow_head(skb, 0)))
374 return -1;
375
Fugang Duan62a02c92014-06-18 08:33:52 +0800376 if (is_ipv4_pkt(skb))
377 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000378 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
379
380 return 0;
381}
382
Nimrod Andy6e909282014-06-12 08:16:22 +0800383static int
384fec_enet_txq_submit_frag_skb(struct sk_buff *skb, struct net_device *ndev)
385{
386 struct fec_enet_private *fep = netdev_priv(ndev);
387 const struct platform_device_id *id_entry =
388 platform_get_device_id(fep->pdev);
389 struct bufdesc *bdp = fep->cur_tx;
390 struct bufdesc_ex *ebdp;
391 int nr_frags = skb_shinfo(skb)->nr_frags;
392 int frag, frag_len;
393 unsigned short status;
394 unsigned int estatus = 0;
395 skb_frag_t *this_frag;
396 unsigned int index;
397 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100398 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800399 int i;
400
401 for (frag = 0; frag < nr_frags; frag++) {
402 this_frag = &skb_shinfo(skb)->frags[frag];
403 bdp = fec_enet_get_nextdesc(bdp, fep);
404 ebdp = (struct bufdesc_ex *)bdp;
405
406 status = bdp->cbd_sc;
407 status &= ~BD_ENET_TX_STATS;
408 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
409 frag_len = skb_shinfo(skb)->frags[frag].size;
410
411 /* Handle the last BD specially */
412 if (frag == nr_frags - 1) {
413 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
414 if (fep->bufdesc_ex) {
415 estatus |= BD_ENET_TX_INT;
416 if (unlikely(skb_shinfo(skb)->tx_flags &
417 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
418 estatus |= BD_ENET_TX_TS;
419 }
420 }
421
422 if (fep->bufdesc_ex) {
423 if (skb->ip_summed == CHECKSUM_PARTIAL)
424 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
425 ebdp->cbd_bdu = 0;
426 ebdp->cbd_esc = estatus;
427 }
428
429 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
430
431 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
432 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
433 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
434 memcpy(fep->tx_bounce[index], bufaddr, frag_len);
435 bufaddr = fep->tx_bounce[index];
436
437 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
438 swap_buffer(bufaddr, frag_len);
439 }
440
Russell Kingd6bf3142014-07-08 00:23:19 +0100441 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
442 DMA_TO_DEVICE);
443 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800444 dev_kfree_skb_any(skb);
445 if (net_ratelimit())
446 netdev_err(ndev, "Tx DMA memory map failed\n");
447 goto dma_mapping_error;
448 }
449
Russell Kingd6bf3142014-07-08 00:23:19 +0100450 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800451 bdp->cbd_datlen = frag_len;
452 bdp->cbd_sc = status;
453 }
454
455 fep->cur_tx = bdp;
456
457 return 0;
458
459dma_mapping_error:
460 bdp = fep->cur_tx;
461 for (i = 0; i < frag; i++) {
462 bdp = fec_enet_get_nextdesc(bdp, fep);
463 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
464 bdp->cbd_datlen, DMA_TO_DEVICE);
465 }
466 return NETDEV_TX_OK;
467}
468
469static int fec_enet_txq_submit_skb(struct sk_buff *skb, struct net_device *ndev)
470{
471 struct fec_enet_private *fep = netdev_priv(ndev);
472 const struct platform_device_id *id_entry =
473 platform_get_device_id(fep->pdev);
474 int nr_frags = skb_shinfo(skb)->nr_frags;
475 struct bufdesc *bdp, *last_bdp;
476 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100477 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800478 unsigned short status;
479 unsigned short buflen;
480 unsigned int estatus = 0;
481 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800482 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800483 int ret;
484
Nimrod Andy79f33912014-06-12 08:16:23 +0800485 entries_free = fec_enet_get_free_txdesc_num(fep);
486 if (entries_free < MAX_SKB_FRAGS + 1) {
487 dev_kfree_skb_any(skb);
488 if (net_ratelimit())
489 netdev_err(ndev, "NOT enough BD for SG!\n");
490 return NETDEV_TX_OK;
491 }
492
Nimrod Andy6e909282014-06-12 08:16:22 +0800493 /* Protocol checksum off-load for TCP and UDP. */
494 if (fec_enet_clear_csum(skb, ndev)) {
495 dev_kfree_skb_any(skb);
496 return NETDEV_TX_OK;
497 }
498
499 /* Fill in a Tx ring entry */
500 bdp = fep->cur_tx;
501 status = bdp->cbd_sc;
502 status &= ~BD_ENET_TX_STATS;
503
504 /* Set buffer length and buffer pointer */
505 bufaddr = skb->data;
506 buflen = skb_headlen(skb);
507
508 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
509 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
510 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
511 memcpy(fep->tx_bounce[index], skb->data, buflen);
512 bufaddr = fep->tx_bounce[index];
513
514 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
515 swap_buffer(bufaddr, buflen);
516 }
517
Russell Kingd6bf3142014-07-08 00:23:19 +0100518 /* Push the data cache so the CPM does not get stale memory data. */
519 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
520 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800521 dev_kfree_skb_any(skb);
522 if (net_ratelimit())
523 netdev_err(ndev, "Tx DMA memory map failed\n");
524 return NETDEV_TX_OK;
525 }
526
527 if (nr_frags) {
528 ret = fec_enet_txq_submit_frag_skb(skb, ndev);
529 if (ret)
530 return ret;
531 } else {
532 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
533 if (fep->bufdesc_ex) {
534 estatus = BD_ENET_TX_INT;
535 if (unlikely(skb_shinfo(skb)->tx_flags &
536 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
537 estatus |= BD_ENET_TX_TS;
538 }
539 }
540
541 if (fep->bufdesc_ex) {
542
543 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
544
545 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
546 fep->hwts_tx_en))
547 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
548
549 if (skb->ip_summed == CHECKSUM_PARTIAL)
550 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
551
552 ebdp->cbd_bdu = 0;
553 ebdp->cbd_esc = estatus;
554 }
555
556 last_bdp = fep->cur_tx;
557 index = fec_enet_get_bd_index(fep->tx_bd_base, last_bdp, fep);
558 /* Save skb pointer */
559 fep->tx_skbuff[index] = skb;
560
561 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100562 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800563
564 /* Send it on its way. Tell FEC it's ready, interrupt when done,
565 * it's the last BD of the frame, and to put the CRC on the end.
566 */
567 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
568 bdp->cbd_sc = status;
569
Sascha Hauer22f6b862009-04-15 01:32:18 +0000570 /* If this was the last BD in the ring, start at the beginning again. */
Nimrod Andy6e909282014-06-12 08:16:22 +0800571 bdp = fec_enet_get_nextdesc(last_bdp, fep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800573 skb_tx_timestamp(skb);
574
Sascha Hauer2e285322009-04-15 01:32:16 +0000575 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Frank Lide5fb0a2013-03-03 17:34:25 +0000577 /* Trigger transmission start */
578 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
579
Nimrod Andy6e909282014-06-12 08:16:22 +0800580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Nimrod Andy79f33912014-06-12 08:16:23 +0800583static int
584fec_enet_txq_put_data_tso(struct sk_buff *skb, struct net_device *ndev,
585 struct bufdesc *bdp, int index, char *data,
586 int size, bool last_tcp, bool is_last)
587{
588 struct fec_enet_private *fep = netdev_priv(ndev);
589 const struct platform_device_id *id_entry =
590 platform_get_device_id(fep->pdev);
591 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
592 unsigned short status;
593 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100594 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800595
596 status = bdp->cbd_sc;
597 status &= ~BD_ENET_TX_STATS;
598
599 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800600
601 if (((unsigned long) data) & FEC_ALIGNMENT ||
602 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
603 memcpy(fep->tx_bounce[index], data, size);
604 data = fep->tx_bounce[index];
605
606 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
607 swap_buffer(data, size);
608 }
609
Russell Kingd6bf3142014-07-08 00:23:19 +0100610 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
611 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800612 dev_kfree_skb_any(skb);
613 if (net_ratelimit())
614 netdev_err(ndev, "Tx DMA memory map failed\n");
615 return NETDEV_TX_BUSY;
616 }
617
Russell Kingd6bf3142014-07-08 00:23:19 +0100618 bdp->cbd_datlen = size;
619 bdp->cbd_bufaddr = addr;
620
Nimrod Andy79f33912014-06-12 08:16:23 +0800621 if (fep->bufdesc_ex) {
622 if (skb->ip_summed == CHECKSUM_PARTIAL)
623 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
624 ebdp->cbd_bdu = 0;
625 ebdp->cbd_esc = estatus;
626 }
627
628 /* Handle the last BD specially */
629 if (last_tcp)
630 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
631 if (is_last) {
632 status |= BD_ENET_TX_INTR;
633 if (fep->bufdesc_ex)
634 ebdp->cbd_esc |= BD_ENET_TX_INT;
635 }
636
637 bdp->cbd_sc = status;
638
639 return 0;
640}
641
642static int
643fec_enet_txq_put_hdr_tso(struct sk_buff *skb, struct net_device *ndev,
644 struct bufdesc *bdp, int index)
645{
646 struct fec_enet_private *fep = netdev_priv(ndev);
647 const struct platform_device_id *id_entry =
648 platform_get_device_id(fep->pdev);
649 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
650 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
651 void *bufaddr;
652 unsigned long dmabuf;
653 unsigned short status;
654 unsigned int estatus = 0;
655
656 status = bdp->cbd_sc;
657 status &= ~BD_ENET_TX_STATS;
658 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
659
660 bufaddr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
661 dmabuf = fep->tso_hdrs_dma + index * TSO_HEADER_SIZE;
662 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
663 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
664 memcpy(fep->tx_bounce[index], skb->data, hdr_len);
665 bufaddr = fep->tx_bounce[index];
666
667 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
668 swap_buffer(bufaddr, hdr_len);
669
670 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
671 hdr_len, DMA_TO_DEVICE);
672 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
673 dev_kfree_skb_any(skb);
674 if (net_ratelimit())
675 netdev_err(ndev, "Tx DMA memory map failed\n");
676 return NETDEV_TX_BUSY;
677 }
678 }
679
680 bdp->cbd_bufaddr = dmabuf;
681 bdp->cbd_datlen = hdr_len;
682
683 if (fep->bufdesc_ex) {
684 if (skb->ip_summed == CHECKSUM_PARTIAL)
685 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
686 ebdp->cbd_bdu = 0;
687 ebdp->cbd_esc = estatus;
688 }
689
690 bdp->cbd_sc = status;
691
692 return 0;
693}
694
695static int fec_enet_txq_submit_tso(struct sk_buff *skb, struct net_device *ndev)
696{
697 struct fec_enet_private *fep = netdev_priv(ndev);
698 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
699 int total_len, data_left;
700 struct bufdesc *bdp = fep->cur_tx;
701 struct tso_t tso;
702 unsigned int index = 0;
703 int ret;
704
705 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep)) {
706 dev_kfree_skb_any(skb);
707 if (net_ratelimit())
708 netdev_err(ndev, "NOT enough BD for TSO!\n");
709 return NETDEV_TX_OK;
710 }
711
712 /* Protocol checksum off-load for TCP and UDP. */
713 if (fec_enet_clear_csum(skb, ndev)) {
714 dev_kfree_skb_any(skb);
715 return NETDEV_TX_OK;
716 }
717
718 /* Initialize the TSO handler, and prepare the first payload */
719 tso_start(skb, &tso);
720
721 total_len = skb->len - hdr_len;
722 while (total_len > 0) {
723 char *hdr;
724
725 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
726 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
727 total_len -= data_left;
728
729 /* prepare packet headers: MAC + IP + TCP */
730 hdr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
731 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
732 ret = fec_enet_txq_put_hdr_tso(skb, ndev, bdp, index);
733 if (ret)
734 goto err_release;
735
736 while (data_left > 0) {
737 int size;
738
739 size = min_t(int, tso.size, data_left);
740 bdp = fec_enet_get_nextdesc(bdp, fep);
741 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
742 ret = fec_enet_txq_put_data_tso(skb, ndev, bdp, index, tso.data,
743 size, size == data_left,
744 total_len == 0);
745 if (ret)
746 goto err_release;
747
748 data_left -= size;
749 tso_build_data(skb, &tso, size);
750 }
751
752 bdp = fec_enet_get_nextdesc(bdp, fep);
753 }
754
755 /* Save skb pointer */
756 fep->tx_skbuff[index] = skb;
757
Nimrod Andy79f33912014-06-12 08:16:23 +0800758 skb_tx_timestamp(skb);
759 fep->cur_tx = bdp;
760
761 /* Trigger transmission start */
762 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
763
764 return 0;
765
766err_release:
767 /* TODO: Release all used data descriptors for TSO */
768 return ret;
769}
770
Nimrod Andy61a44272014-06-12 08:16:18 +0800771static netdev_tx_t
772fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
773{
774 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800775 int entries_free;
Nimrod Andy61a44272014-06-12 08:16:18 +0800776 int ret;
777
Nimrod Andy79f33912014-06-12 08:16:23 +0800778 if (skb_is_gso(skb))
779 ret = fec_enet_txq_submit_tso(skb, ndev);
780 else
781 ret = fec_enet_txq_submit_skb(skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800782 if (ret)
783 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800784
Nimrod Andy6e909282014-06-12 08:16:22 +0800785 entries_free = fec_enet_get_free_txdesc_num(fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800786 if (entries_free <= fep->tx_stop_threshold)
Nimrod Andy61a44272014-06-12 08:16:18 +0800787 netif_stop_queue(ndev);
788
789 return NETDEV_TX_OK;
790}
791
Frank Li14109a52013-03-26 16:12:03 +0000792/* Init RX & TX buffer descriptors
793 */
794static void fec_enet_bd_init(struct net_device *dev)
795{
796 struct fec_enet_private *fep = netdev_priv(dev);
797 struct bufdesc *bdp;
798 unsigned int i;
799
800 /* Initialize the receive buffer descriptors. */
801 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800802 for (i = 0; i < fep->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000803
804 /* Initialize the BD for every fragment in the page. */
805 if (bdp->cbd_bufaddr)
806 bdp->cbd_sc = BD_ENET_RX_EMPTY;
807 else
808 bdp->cbd_sc = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800809 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000810 }
811
812 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800813 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000814 bdp->cbd_sc |= BD_SC_WRAP;
815
816 fep->cur_rx = fep->rx_bd_base;
817
818 /* ...and the same for transmit */
819 bdp = fep->tx_bd_base;
820 fep->cur_tx = bdp;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800821 for (i = 0; i < fep->tx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000822
823 /* Initialize the BD for every fragment in the page. */
824 bdp->cbd_sc = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100825 if (fep->tx_skbuff[i]) {
Frank Li14109a52013-03-26 16:12:03 +0000826 dev_kfree_skb_any(fep->tx_skbuff[i]);
827 fep->tx_skbuff[i] = NULL;
828 }
829 bdp->cbd_bufaddr = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800830 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000831 }
832
833 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800834 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000835 bdp->cbd_sc |= BD_SC_WRAP;
836 fep->dirty_tx = bdp;
837}
838
Russell Kingdbc64a82014-07-08 12:40:12 +0100839/*
840 * This function is called to start or restart the FEC during a link
841 * change, transmit timeout, or to reconfigure the FEC. The network
842 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100843 */
844static void
Russell Kingef833372014-07-08 12:40:43 +0100845fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100846{
847 struct fec_enet_private *fep = netdev_priv(ndev);
848 const struct platform_device_id *id_entry =
849 platform_get_device_id(fep->pdev);
850 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000851 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100852 u32 temp_mac[2];
853 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000854 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100855
856 /* Whack a reset. We should wait for this. */
857 writel(1, fep->hwp + FEC_ECNTRL);
858 udelay(10);
859
860 /*
861 * enet-mac reset will reset mac address registers too,
862 * so need to reconfigure it.
863 */
864 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
865 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
866 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
867 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
868 }
869
870 /* Clear any outstanding interrupt. */
871 writel(0xffc00000, fep->hwp + FEC_IEVENT);
872
Uwe Kleine-König45993652011-01-19 20:47:04 +0100873 /* Set maximum receive buffer size. */
874 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
875
Frank Li14109a52013-03-26 16:12:03 +0000876 fec_enet_bd_init(ndev);
877
Uwe Kleine-König45993652011-01-19 20:47:04 +0100878 /* Set receive and transmit descriptor base. */
879 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000880 if (fep->bufdesc_ex)
881 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800882 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000883 else
884 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800885 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100886
Uwe Kleine-König45993652011-01-19 20:47:04 +0100887
Uwe Kleine-König45993652011-01-19 20:47:04 +0100888 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
889 if (fep->tx_skbuff[i]) {
890 dev_kfree_skb_any(fep->tx_skbuff[i]);
891 fep->tx_skbuff[i] = NULL;
892 }
893 }
894
895 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100896 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100897 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100898 writel(0x04, fep->hwp + FEC_X_CNTRL);
899 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100900 /* No Rcv on Xmit */
901 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100902 writel(0x0, fep->hwp + FEC_X_CNTRL);
903 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100904
Uwe Kleine-König45993652011-01-19 20:47:04 +0100905 /* Set MII speed */
906 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
907
Guenter Roeckd1391932013-06-18 10:04:59 -0700908#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000909 /* set RX checksum */
910 val = readl(fep->hwp + FEC_RACC);
911 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
912 val |= FEC_RACC_OPTIONS;
913 else
914 val &= ~FEC_RACC_OPTIONS;
915 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700916#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000917
Uwe Kleine-König45993652011-01-19 20:47:04 +0100918 /*
919 * The phy interface and speed need to get configured
920 * differently on enet-mac.
921 */
922 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100923 /* Enable flow control and length check */
924 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100925
Shawn Guo230dec62011-09-23 02:12:48 +0000926 /* RGMII, RMII or MII */
927 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
928 rcntl |= (1 << 6);
929 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100930 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100931 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100932 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100933
Shawn Guo230dec62011-09-23 02:12:48 +0000934 /* 1G, 100M or 10M */
935 if (fep->phy_dev) {
936 if (fep->phy_dev->speed == SPEED_1000)
937 ecntl |= (1 << 5);
938 else if (fep->phy_dev->speed == SPEED_100)
939 rcntl &= ~(1 << 9);
940 else
941 rcntl |= (1 << 9);
942 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100943 } else {
944#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800945 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000946 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100947 /* disable the gasket and wait */
948 writel(0, fep->hwp + FEC_MIIGSK_ENR);
949 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
950 udelay(1);
951
952 /*
953 * configure the gasket:
954 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800955 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100956 */
Eric Benard8d82f212012-01-12 06:10:28 +0000957 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
958 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
959 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
960 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
961 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100962
963 /* re-enable the gasket */
964 writel(2, fep->hwp + FEC_MIIGSK_ENR);
965 }
966#endif
967 }
Frank Libaa70a52013-01-16 16:55:58 +0000968
Guenter Roeckd1391932013-06-18 10:04:59 -0700969#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +0000970 /* enable pause frame*/
971 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
972 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
973 fep->phy_dev && fep->phy_dev->pause)) {
974 rcntl |= FEC_ENET_FCE;
975
Jim Baxter4c09eed2013-04-19 08:10:49 +0000976 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000977 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
978 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
979 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
980 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
981
982 /* OPD */
983 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
984 } else {
985 rcntl &= ~FEC_ENET_FCE;
986 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700987#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +0000988
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100989 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100990
Stefan Wahren84fe6182014-03-12 11:28:19 +0100991 /* Setup multicast filter. */
992 set_multicast_list(ndev);
993#ifndef CONFIG_M5272
994 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
995 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
996#endif
997
Shawn Guo230dec62011-09-23 02:12:48 +0000998 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
999 /* enable ENET endian swap */
1000 ecntl |= (1 << 8);
1001 /* enable ENET store and forward mode */
1002 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1003 }
1004
Frank Liff43da82013-01-03 16:04:23 +00001005 if (fep->bufdesc_ex)
1006 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001007
Chris Healy38ae92d2013-06-25 23:18:52 -07001008#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001009 /* Enable the MIB statistic event counters */
1010 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001011#endif
1012
Uwe Kleine-König45993652011-01-19 20:47:04 +01001013 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001014 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001015 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
1016
Frank Liff43da82013-01-03 16:04:23 +00001017 if (fep->bufdesc_ex)
1018 fec_ptp_start_cyclecounter(ndev);
1019
Uwe Kleine-König45993652011-01-19 20:47:04 +01001020 /* Enable interrupts we wish to service */
1021 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1022}
1023
1024static void
1025fec_stop(struct net_device *ndev)
1026{
1027 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001028 const struct platform_device_id *id_entry =
1029 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001030 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001031
1032 /* We cannot expect a graceful transmit stop without link !!! */
1033 if (fep->link) {
1034 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1035 udelay(10);
1036 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001037 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001038 }
1039
1040 /* Whack a reset. We should wait for this. */
1041 writel(1, fep->hwp + FEC_ECNTRL);
1042 udelay(10);
1043 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1044 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001045
1046 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001047 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001048 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001049 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1050 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001051}
1052
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001055fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001057 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Russell King344756f2014-07-08 13:01:59 +01001059 fec_dump(ndev);
1060
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001061 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Russell King36cdc742014-07-08 13:01:44 +01001063 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001064}
1065
Russell King36cdc742014-07-08 13:01:44 +01001066static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001067{
1068 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001069 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001070 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001071
Russell King36cdc742014-07-08 13:01:44 +01001072 rtnl_lock();
1073 if (netif_device_present(ndev) || netif_running(ndev)) {
1074 napi_disable(&fep->napi);
1075 netif_tx_lock_bh(ndev);
1076 fec_restart(ndev);
1077 netif_wake_queue(ndev);
1078 netif_tx_unlock_bh(ndev);
1079 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001080 }
Russell King36cdc742014-07-08 13:01:44 +01001081 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001085fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1086 struct skb_shared_hwtstamps *hwtstamps)
1087{
1088 unsigned long flags;
1089 u64 ns;
1090
1091 spin_lock_irqsave(&fep->tmreg_lock, flags);
1092 ns = timecounter_cyc2time(&fep->tc, ts);
1093 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1094
1095 memset(hwtstamps, 0, sizeof(*hwtstamps));
1096 hwtstamps->hwtstamp = ns_to_ktime(ns);
1097}
1098
1099static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001100fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001103 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001104 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +00001106 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001107 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001109 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 bdp = fep->dirty_tx;
1111
Frank Lide5fb0a2013-03-03 17:34:25 +00001112 /* get next bdp of dirty_tx */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001113 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001114
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001115 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001116
1117 /* current queue is empty */
1118 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001119 break;
1120
Nimrod Andy61a44272014-06-12 08:16:18 +08001121 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001122
Frank Lide5fb0a2013-03-03 17:34:25 +00001123 skb = fep->tx_skbuff[index];
Russell Kingd6bf3142014-07-08 00:23:19 +01001124 fep->tx_skbuff[index] = NULL;
Nimrod Andy79f33912014-06-12 08:16:23 +08001125 if (!IS_TSO_HEADER(fep, bdp->cbd_bufaddr))
1126 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1127 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001128 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001129 if (!skb) {
1130 bdp = fec_enet_get_nextdesc(bdp, fep);
1131 continue;
1132 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001135 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 BD_ENET_TX_RL | BD_ENET_TX_UN |
1137 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001138 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001139 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001140 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001141 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001142 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001143 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001144 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001145 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001146 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001147 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001148 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001150 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001151 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
Frank Liff43da82013-01-03 16:04:23 +00001154 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1155 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001156 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001157 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001158
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001159 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001160 skb_tstamp_tx(skb, &shhwtstamps);
1161 }
Frank Liff43da82013-01-03 16:04:23 +00001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /* Deferred means some collisions occurred during transmit,
1164 * but we eventually sent the packet OK.
1165 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001166 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001167 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001168
Sascha Hauer22f6b862009-04-15 01:32:18 +00001169 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001171
1172 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001173
Sascha Hauer22f6b862009-04-15 01:32:18 +00001174 /* Update pointer to next buffer descriptor to be transmitted */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001175 bdp = fec_enet_get_nextdesc(bdp, fep);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001176
Sascha Hauer22f6b862009-04-15 01:32:18 +00001177 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001179 if (netif_queue_stopped(ndev)) {
1180 entries_free = fec_enet_get_free_txdesc_num(fep);
1181 if (entries_free >= fep->tx_wake_threshold)
1182 netif_wake_queue(ndev);
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Russell Kingccea2962014-07-08 13:01:38 +01001185
1186 /* ERR006538: Keep the transmitter going */
1187 if (bdp != fep->cur_tx && readl(fep->hwp + FEC_X_DES_ACTIVE) == 0)
1188 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/* During a receive, the cur_rx points to the current incoming buffer.
1192 * When we update through the ring, if the next incoming buffer has
1193 * not been given to the system, we just set the empty indicator,
1194 * effectively tossing the packet.
1195 */
Frank Lidc975382013-01-28 18:31:42 +00001196static int
1197fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001199 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001200 const struct platform_device_id *id_entry =
1201 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001202 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001203 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct sk_buff *skb;
1205 ushort pkt_len;
1206 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001207 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001208 struct bufdesc_ex *ebdp = NULL;
1209 bool vlan_packet_rcvd = false;
1210 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001211 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001212
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001213#ifdef CONFIG_M532x
1214 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* First, grab all of the stats for the incoming packet.
1218 * These get messed up if we get called due to a busy condition.
1219 */
1220 bdp = fep->cur_rx;
1221
Sascha Hauer22f6b862009-04-15 01:32:18 +00001222 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Frank Lidc975382013-01-28 18:31:42 +00001224 if (pkt_received >= budget)
1225 break;
1226 pkt_received++;
1227
Sascha Hauer22f6b862009-04-15 01:32:18 +00001228 /* Since we have allocated space to hold a complete frame,
1229 * the last indicator should be set.
1230 */
1231 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001232 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Russell Kingdb3421c2014-07-08 13:01:49 +01001234 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
1235
Sascha Hauer22f6b862009-04-15 01:32:18 +00001236 /* Check for errors. */
1237 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001239 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001240 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1241 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001242 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001243 }
1244 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001245 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001246 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001247 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001248 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001249 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001251
1252 /* Report late collisions as a frame error.
1253 * On this error, the BD is closed, but we don't know what we
1254 * have in the buffer. So, just drop this frame on the floor.
1255 */
1256 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001257 ndev->stats.rx_errors++;
1258 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001259 goto rx_processing_done;
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Sascha Hauer22f6b862009-04-15 01:32:18 +00001262 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001263 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001264 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001265 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Nimrod Andy61a44272014-06-12 08:16:18 +08001267 index = fec_enet_get_bd_index(fep->rx_bd_base, bdp, fep);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001268 data = fep->rx_skbuff[index]->data;
1269 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1270 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001271
Shawn Guob5680e02011-01-05 21:13:13 +00001272 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1273 swap_buffer(data, pkt_len);
1274
Jim Baxtercdffcf12013-07-02 22:52:56 +01001275 /* Extract the enhanced buffer descriptor */
1276 ebdp = NULL;
1277 if (fep->bufdesc_ex)
1278 ebdp = (struct bufdesc_ex *)bdp;
1279
1280 /* If this is a VLAN packet remove the VLAN Tag */
1281 vlan_packet_rcvd = false;
1282 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1283 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
1284 /* Push and remove the vlan tag */
1285 struct vlan_hdr *vlan_header =
1286 (struct vlan_hdr *) (data + ETH_HLEN);
1287 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1288 pkt_len -= VLAN_HLEN;
1289
1290 vlan_packet_rcvd = true;
1291 }
1292
Sascha Hauer22f6b862009-04-15 01:32:18 +00001293 /* This does 16 byte alignment, exactly what we need.
1294 * The packet length includes FCS, but we don't want to
1295 * include that when passing upstream as it messes up
1296 * bridging applications.
1297 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001298 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Sascha Hauer85498892009-04-15 01:32:21 +00001300 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001301 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001302 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001303 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001304 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001305 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001306
1307 /* Extract the frame data without the VLAN header. */
1308 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1309 if (vlan_packet_rcvd)
1310 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
1311 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
1312 data + payload_offset,
1313 pkt_len - 4 - (2 * ETH_ALEN));
1314
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001315 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001316
Frank Li6605b732012-10-30 18:25:31 +00001317 /* Get receive timestamp from the skb */
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001318 if (fep->hwts_rx_en && fep->bufdesc_ex)
1319 fec_enet_hwtstamp(fep, ebdp->ts,
1320 skb_hwtstamps(skb));
Frank Liff43da82013-01-03 16:04:23 +00001321
Jim Baxter4c09eed2013-04-19 08:10:49 +00001322 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001323 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001324 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1325 /* don't check it */
1326 skb->ip_summed = CHECKSUM_UNNECESSARY;
1327 } else {
1328 skb_checksum_none_assert(skb);
1329 }
1330 }
1331
Jim Baxtercdffcf12013-07-02 22:52:56 +01001332 /* Handle received VLAN packets */
1333 if (vlan_packet_rcvd)
1334 __vlan_hwaccel_put_tag(skb,
1335 htons(ETH_P_8021Q),
1336 vlan_tag);
1337
Richard Cochran0affdf32013-08-30 20:28:10 +02001338 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001339 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001340
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001341 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1342 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001343rx_processing_done:
1344 /* Clear the status flags for this buffer */
1345 status &= ~BD_ENET_RX_STATS;
1346
1347 /* Mark the buffer empty */
1348 status |= BD_ENET_RX_EMPTY;
1349 bdp->cbd_sc = status;
1350
Frank Liff43da82013-01-03 16:04:23 +00001351 if (fep->bufdesc_ex) {
1352 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1353
1354 ebdp->cbd_esc = BD_ENET_RX_INT;
1355 ebdp->cbd_prot = 0;
1356 ebdp->cbd_bdu = 0;
1357 }
Frank Li6605b732012-10-30 18:25:31 +00001358
Sascha Hauer22f6b862009-04-15 01:32:18 +00001359 /* Update BD pointer to next entry */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001360 bdp = fec_enet_get_nextdesc(bdp, fep);
1361
Sascha Hauer22f6b862009-04-15 01:32:18 +00001362 /* Doing this here will keep the FEC running while we process
1363 * incoming frames. On a heavily loaded network, we should be
1364 * able to keep up at the expense of system resources.
1365 */
1366 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Sascha Hauer2e285322009-04-15 01:32:16 +00001368 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Frank Lidc975382013-01-28 18:31:42 +00001370 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Uwe Kleine-König45993652011-01-19 20:47:04 +01001373static irqreturn_t
1374fec_enet_interrupt(int irq, void *dev_id)
1375{
1376 struct net_device *ndev = dev_id;
1377 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001378 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001379 uint int_events;
1380 irqreturn_t ret = IRQ_NONE;
1381
Russell King7a168072014-07-08 00:22:44 +01001382 int_events = readl(fep->hwp + FEC_IEVENT);
1383 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001384
Russell King7a168072014-07-08 00:22:44 +01001385 if (int_events & napi_mask) {
1386 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001387
Russell King7a168072014-07-08 00:22:44 +01001388 /* Disable the NAPI interrupts */
1389 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1390 napi_schedule(&fep->napi);
1391 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001392
Russell King7a168072014-07-08 00:22:44 +01001393 if (int_events & FEC_ENET_MII) {
1394 ret = IRQ_HANDLED;
1395 complete(&fep->mdio_done);
1396 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001397
1398 return ret;
1399}
1400
Frank Lidc975382013-01-28 18:31:42 +00001401static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1402{
1403 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001404 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001405 int pkts;
1406
1407 /*
1408 * Clear any pending transmit or receive interrupts before
1409 * processing the rings to avoid racing with the hardware.
1410 */
1411 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1412
1413 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001414
Frank Lide5fb0a2013-03-03 17:34:25 +00001415 fec_enet_tx(ndev);
1416
Frank Lidc975382013-01-28 18:31:42 +00001417 if (pkts < budget) {
1418 napi_complete(napi);
1419 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1420 }
1421 return pkts;
1422}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001425static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001427 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001428 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001429 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Shawn Guo49da97d2011-01-05 21:13:11 +00001431 /*
1432 * try to get mac address in following order:
1433 *
1434 * 1) module parameter via kernel command line in form
1435 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1436 */
1437 iap = macaddr;
1438
1439 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001440 * 2) from device tree data
1441 */
1442 if (!is_valid_ether_addr(iap)) {
1443 struct device_node *np = fep->pdev->dev.of_node;
1444 if (np) {
1445 const char *mac = of_get_mac_address(np);
1446 if (mac)
1447 iap = (unsigned char *) mac;
1448 }
1449 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001450
1451 /*
1452 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001453 */
1454 if (!is_valid_ether_addr(iap)) {
1455#ifdef CONFIG_M5272
1456 if (FEC_FLASHMAC)
1457 iap = (unsigned char *)FEC_FLASHMAC;
1458#else
1459 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001460 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001461#endif
1462 }
1463
1464 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001465 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001466 */
1467 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001468 *((__be32 *) &tmpaddr[0]) =
1469 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1470 *((__be16 *) &tmpaddr[4]) =
1471 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 iap = &tmpaddr[0];
1473 }
1474
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001475 /*
1476 * 5) random mac address
1477 */
1478 if (!is_valid_ether_addr(iap)) {
1479 /* Report it and use a random ethernet address instead */
1480 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1481 eth_hw_addr_random(ndev);
1482 netdev_info(ndev, "Using random MAC address: %pM\n",
1483 ndev->dev_addr);
1484 return;
1485 }
1486
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001487 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Shawn Guo49da97d2011-01-05 21:13:11 +00001489 /* Adjust MAC if using macaddr */
1490 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001491 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494/* ------------------------------------------------------------------------- */
1495
Bryan Wue6b043d2010-03-31 02:10:44 +00001496/*
1497 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001499static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001501 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001502 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001503 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Bryan Wue6b043d2010-03-31 02:10:44 +00001505 /* Prevent a state halted on mii error */
1506 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1507 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001508 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001510
Russell King8ce56242014-07-08 12:40:07 +01001511 /*
1512 * If the netdev is down, or is going down, we're not interested
1513 * in link state events, so just mark our idea of the link as down
1514 * and ignore the event.
1515 */
1516 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1517 fep->link = 0;
1518 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001519 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001520 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001521 status_change = 1;
1522 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001523
Russell Kingef833372014-07-08 12:40:43 +01001524 if (fep->full_duplex != phy_dev->duplex) {
1525 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001526 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001527 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001528
1529 if (phy_dev->speed != fep->speed) {
1530 fep->speed = phy_dev->speed;
1531 status_change = 1;
1532 }
1533
1534 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001535 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001536 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001537 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001538 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001539 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001540 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001541 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001542 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001543 } else {
1544 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001545 napi_disable(&fep->napi);
1546 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001547 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001548 netif_tx_unlock_bh(ndev);
1549 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001550 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001551 status_change = 1;
1552 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001553 }
1554
Bryan Wue6b043d2010-03-31 02:10:44 +00001555 if (status_change)
1556 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Bryan Wue6b043d2010-03-31 02:10:44 +00001559static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Bryan Wue6b043d2010-03-31 02:10:44 +00001561 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001562 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001563
1564 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001565 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001566
1567 /* start a read op */
1568 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1569 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1570 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1571
1572 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001573 time_left = wait_for_completion_timeout(&fep->mdio_done,
1574 usecs_to_jiffies(FEC_MII_TIMEOUT));
1575 if (time_left == 0) {
1576 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001577 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001578 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001579 }
1580
1581 /* return value */
1582 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1583}
1584
1585static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1586 u16 value)
1587{
1588 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001589 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001590
1591 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001592 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001593
Shawn Guo862f0982011-01-05 21:13:09 +00001594 /* start a write op */
1595 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001596 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1597 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1598 fep->hwp + FEC_MII_DATA);
1599
1600 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001601 time_left = wait_for_completion_timeout(&fep->mdio_done,
1602 usecs_to_jiffies(FEC_MII_TIMEOUT));
1603 if (time_left == 0) {
1604 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001605 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001606 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001607 }
1608
1609 return 0;
1610}
1611
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001612static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1613{
1614 struct fec_enet_private *fep = netdev_priv(ndev);
1615 int ret;
1616
1617 if (enable) {
1618 ret = clk_prepare_enable(fep->clk_ahb);
1619 if (ret)
1620 return ret;
1621 ret = clk_prepare_enable(fep->clk_ipg);
1622 if (ret)
1623 goto failed_clk_ipg;
1624 if (fep->clk_enet_out) {
1625 ret = clk_prepare_enable(fep->clk_enet_out);
1626 if (ret)
1627 goto failed_clk_enet_out;
1628 }
1629 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001630 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001631 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001632 if (ret) {
1633 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001634 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001635 } else {
1636 fep->ptp_clk_on = true;
1637 }
1638 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001639 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001640 if (fep->clk_ref) {
1641 ret = clk_prepare_enable(fep->clk_ref);
1642 if (ret)
1643 goto failed_clk_ref;
1644 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001645 } else {
1646 clk_disable_unprepare(fep->clk_ahb);
1647 clk_disable_unprepare(fep->clk_ipg);
1648 if (fep->clk_enet_out)
1649 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001650 if (fep->clk_ptp) {
1651 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001652 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001653 fep->ptp_clk_on = false;
1654 mutex_unlock(&fep->ptp_clk_mutex);
1655 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001656 if (fep->clk_ref)
1657 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001658 }
1659
1660 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001661
1662failed_clk_ref:
1663 if (fep->clk_ref)
1664 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001665failed_clk_ptp:
1666 if (fep->clk_enet_out)
1667 clk_disable_unprepare(fep->clk_enet_out);
1668failed_clk_enet_out:
1669 clk_disable_unprepare(fep->clk_ipg);
1670failed_clk_ipg:
1671 clk_disable_unprepare(fep->clk_ahb);
1672
1673 return ret;
1674}
1675
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001676static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001677{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001678 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001679 const struct platform_device_id *id_entry =
1680 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001681 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001682 char mdio_bus_id[MII_BUS_ID_SIZE];
1683 char phy_name[MII_BUS_ID_SIZE + 3];
1684 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001685 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001686
Bryan Wu418bd0d2010-05-28 03:40:39 -07001687 fep->phy_dev = NULL;
1688
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001689 if (fep->phy_node) {
1690 phy_dev = of_phy_connect(ndev, fep->phy_node,
1691 &fec_enet_adjust_link, 0,
1692 fep->phy_interface);
1693 } else {
1694 /* check for attached phy */
1695 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1696 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1697 continue;
1698 if (fep->mii_bus->phy_map[phy_id] == NULL)
1699 continue;
1700 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1701 continue;
1702 if (dev_id--)
1703 continue;
1704 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1705 break;
1706 }
1707
1708 if (phy_id >= PHY_MAX_ADDR) {
1709 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1710 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1711 phy_id = 0;
1712 }
1713
1714 snprintf(phy_name, sizeof(phy_name),
1715 PHY_ID_FMT, mdio_bus_id, phy_id);
1716 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1717 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001718 }
1719
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001720 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001721 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001722 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001723 }
1724
1725 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001726 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001727 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001728 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001729#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001730 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001731#endif
Frank Libaa70a52013-01-16 16:55:58 +00001732 }
Shawn Guo230dec62011-09-23 02:12:48 +00001733 else
1734 phy_dev->supported &= PHY_BASIC_FEATURES;
1735
Bryan Wue6b043d2010-03-31 02:10:44 +00001736 phy_dev->advertising = phy_dev->supported;
1737
1738 fep->phy_dev = phy_dev;
1739 fep->link = 0;
1740 fep->full_duplex = 0;
1741
Joe Perches31b77202013-04-13 19:03:17 +00001742 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1743 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1744 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001745
Bryan Wue6b043d2010-03-31 02:10:44 +00001746 return 0;
1747}
1748
1749static int fec_enet_mii_init(struct platform_device *pdev)
1750{
Shawn Guob5680e02011-01-05 21:13:13 +00001751 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001752 struct net_device *ndev = platform_get_drvdata(pdev);
1753 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001754 const struct platform_device_id *id_entry =
1755 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001756 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001757 int err = -ENXIO, i;
1758
Shawn Guob5680e02011-01-05 21:13:13 +00001759 /*
1760 * The dual fec interfaces are not equivalent with enet-mac.
1761 * Here are the differences:
1762 *
1763 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1764 * - fec0 acts as the 1588 time master while fec1 is slave
1765 * - external phys can only be configured by fec0
1766 *
1767 * That is to say fec1 can not work independently. It only works
1768 * when fec0 is working. The reason behind this design is that the
1769 * second interface is added primarily for Switch mode.
1770 *
1771 * Because of the last point above, both phys are attached on fec0
1772 * mdio interface in board design, and need to be configured by
1773 * fec0 mii_bus.
1774 */
Shawn Guo43af9402011-12-05 05:01:15 +00001775 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001776 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001777 if (mii_cnt && fec0_mii_bus) {
1778 fep->mii_bus = fec0_mii_bus;
1779 mii_cnt++;
1780 return 0;
1781 }
1782 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001783 }
1784
Bryan Wue6b043d2010-03-31 02:10:44 +00001785 fep->mii_timeout = 0;
1786
1787 /*
1788 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001789 *
1790 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1791 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1792 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1793 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001794 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001795 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001796 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1797 fep->phy_speed--;
1798 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001799 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1800
1801 fep->mii_bus = mdiobus_alloc();
1802 if (fep->mii_bus == NULL) {
1803 err = -ENOMEM;
1804 goto err_out;
1805 }
1806
1807 fep->mii_bus->name = "fec_enet_mii_bus";
1808 fep->mii_bus->read = fec_enet_mdio_read;
1809 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001810 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1811 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001812 fep->mii_bus->priv = fep;
1813 fep->mii_bus->parent = &pdev->dev;
1814
1815 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1816 if (!fep->mii_bus->irq) {
1817 err = -ENOMEM;
1818 goto err_out_free_mdiobus;
1819 }
1820
1821 for (i = 0; i < PHY_MAX_ADDR; i++)
1822 fep->mii_bus->irq[i] = PHY_POLL;
1823
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001824 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
1825 if (node) {
1826 err = of_mdiobus_register(fep->mii_bus, node);
1827 of_node_put(node);
1828 } else {
1829 err = mdiobus_register(fep->mii_bus);
1830 }
1831
1832 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00001833 goto err_out_free_mdio_irq;
1834
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001835 mii_cnt++;
1836
Shawn Guob5680e02011-01-05 21:13:13 +00001837 /* save fec0 mii_bus */
1838 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1839 fec0_mii_bus = fep->mii_bus;
1840
Bryan Wue6b043d2010-03-31 02:10:44 +00001841 return 0;
1842
Bryan Wue6b043d2010-03-31 02:10:44 +00001843err_out_free_mdio_irq:
1844 kfree(fep->mii_bus->irq);
1845err_out_free_mdiobus:
1846 mdiobus_free(fep->mii_bus);
1847err_out:
1848 return err;
1849}
1850
1851static void fec_enet_mii_remove(struct fec_enet_private *fep)
1852{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001853 if (--mii_cnt == 0) {
1854 mdiobus_unregister(fep->mii_bus);
1855 kfree(fep->mii_bus->irq);
1856 mdiobus_free(fep->mii_bus);
1857 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001858}
1859
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001860static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001861 struct ethtool_cmd *cmd)
1862{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001863 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001864 struct phy_device *phydev = fep->phy_dev;
1865
1866 if (!phydev)
1867 return -ENODEV;
1868
1869 return phy_ethtool_gset(phydev, cmd);
1870}
1871
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001872static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001873 struct ethtool_cmd *cmd)
1874{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001875 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001876 struct phy_device *phydev = fep->phy_dev;
1877
1878 if (!phydev)
1879 return -ENODEV;
1880
1881 return phy_ethtool_sset(phydev, cmd);
1882}
1883
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001884static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001885 struct ethtool_drvinfo *info)
1886{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001887 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Jiri Pirko7826d432013-01-06 00:44:26 +00001889 strlcpy(info->driver, fep->pdev->dev.driver->name,
1890 sizeof(info->driver));
1891 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1892 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
Bryan Wue6b043d2010-03-31 02:10:44 +00001894
Frank Li5ebae4892013-01-06 16:25:07 +00001895static int fec_enet_get_ts_info(struct net_device *ndev,
1896 struct ethtool_ts_info *info)
1897{
1898 struct fec_enet_private *fep = netdev_priv(ndev);
1899
1900 if (fep->bufdesc_ex) {
1901
1902 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1903 SOF_TIMESTAMPING_RX_SOFTWARE |
1904 SOF_TIMESTAMPING_SOFTWARE |
1905 SOF_TIMESTAMPING_TX_HARDWARE |
1906 SOF_TIMESTAMPING_RX_HARDWARE |
1907 SOF_TIMESTAMPING_RAW_HARDWARE;
1908 if (fep->ptp_clock)
1909 info->phc_index = ptp_clock_index(fep->ptp_clock);
1910 else
1911 info->phc_index = -1;
1912
1913 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1914 (1 << HWTSTAMP_TX_ON);
1915
1916 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1917 (1 << HWTSTAMP_FILTER_ALL);
1918 return 0;
1919 } else {
1920 return ethtool_op_get_ts_info(ndev, info);
1921 }
1922}
1923
Guenter Roeckd1391932013-06-18 10:04:59 -07001924#if !defined(CONFIG_M5272)
1925
Frank Libaa70a52013-01-16 16:55:58 +00001926static void fec_enet_get_pauseparam(struct net_device *ndev,
1927 struct ethtool_pauseparam *pause)
1928{
1929 struct fec_enet_private *fep = netdev_priv(ndev);
1930
1931 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1932 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1933 pause->rx_pause = pause->tx_pause;
1934}
1935
1936static int fec_enet_set_pauseparam(struct net_device *ndev,
1937 struct ethtool_pauseparam *pause)
1938{
1939 struct fec_enet_private *fep = netdev_priv(ndev);
1940
Russell King0b146ca2014-07-08 00:22:59 +01001941 if (!fep->phy_dev)
1942 return -ENODEV;
1943
Frank Libaa70a52013-01-16 16:55:58 +00001944 if (pause->tx_pause != pause->rx_pause) {
1945 netdev_info(ndev,
1946 "hardware only support enable/disable both tx and rx");
1947 return -EINVAL;
1948 }
1949
1950 fep->pause_flag = 0;
1951
1952 /* tx pause must be same as rx pause */
1953 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1954 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1955
1956 if (pause->rx_pause || pause->autoneg) {
1957 fep->phy_dev->supported |= ADVERTISED_Pause;
1958 fep->phy_dev->advertising |= ADVERTISED_Pause;
1959 } else {
1960 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1961 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1962 }
1963
1964 if (pause->autoneg) {
1965 if (netif_running(ndev))
1966 fec_stop(ndev);
1967 phy_start_aneg(fep->phy_dev);
1968 }
Russell Kingdbc64a82014-07-08 12:40:12 +01001969 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001970 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001971 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001972 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001973 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001974 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001975 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001976 }
Frank Libaa70a52013-01-16 16:55:58 +00001977
1978 return 0;
1979}
1980
Chris Healy38ae92d2013-06-25 23:18:52 -07001981static const struct fec_stat {
1982 char name[ETH_GSTRING_LEN];
1983 u16 offset;
1984} fec_stats[] = {
1985 /* RMON TX */
1986 { "tx_dropped", RMON_T_DROP },
1987 { "tx_packets", RMON_T_PACKETS },
1988 { "tx_broadcast", RMON_T_BC_PKT },
1989 { "tx_multicast", RMON_T_MC_PKT },
1990 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1991 { "tx_undersize", RMON_T_UNDERSIZE },
1992 { "tx_oversize", RMON_T_OVERSIZE },
1993 { "tx_fragment", RMON_T_FRAG },
1994 { "tx_jabber", RMON_T_JAB },
1995 { "tx_collision", RMON_T_COL },
1996 { "tx_64byte", RMON_T_P64 },
1997 { "tx_65to127byte", RMON_T_P65TO127 },
1998 { "tx_128to255byte", RMON_T_P128TO255 },
1999 { "tx_256to511byte", RMON_T_P256TO511 },
2000 { "tx_512to1023byte", RMON_T_P512TO1023 },
2001 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2002 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2003 { "tx_octets", RMON_T_OCTETS },
2004
2005 /* IEEE TX */
2006 { "IEEE_tx_drop", IEEE_T_DROP },
2007 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2008 { "IEEE_tx_1col", IEEE_T_1COL },
2009 { "IEEE_tx_mcol", IEEE_T_MCOL },
2010 { "IEEE_tx_def", IEEE_T_DEF },
2011 { "IEEE_tx_lcol", IEEE_T_LCOL },
2012 { "IEEE_tx_excol", IEEE_T_EXCOL },
2013 { "IEEE_tx_macerr", IEEE_T_MACERR },
2014 { "IEEE_tx_cserr", IEEE_T_CSERR },
2015 { "IEEE_tx_sqe", IEEE_T_SQE },
2016 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2017 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2018
2019 /* RMON RX */
2020 { "rx_packets", RMON_R_PACKETS },
2021 { "rx_broadcast", RMON_R_BC_PKT },
2022 { "rx_multicast", RMON_R_MC_PKT },
2023 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2024 { "rx_undersize", RMON_R_UNDERSIZE },
2025 { "rx_oversize", RMON_R_OVERSIZE },
2026 { "rx_fragment", RMON_R_FRAG },
2027 { "rx_jabber", RMON_R_JAB },
2028 { "rx_64byte", RMON_R_P64 },
2029 { "rx_65to127byte", RMON_R_P65TO127 },
2030 { "rx_128to255byte", RMON_R_P128TO255 },
2031 { "rx_256to511byte", RMON_R_P256TO511 },
2032 { "rx_512to1023byte", RMON_R_P512TO1023 },
2033 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2034 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2035 { "rx_octets", RMON_R_OCTETS },
2036
2037 /* IEEE RX */
2038 { "IEEE_rx_drop", IEEE_R_DROP },
2039 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2040 { "IEEE_rx_crc", IEEE_R_CRC },
2041 { "IEEE_rx_align", IEEE_R_ALIGN },
2042 { "IEEE_rx_macerr", IEEE_R_MACERR },
2043 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2044 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2045};
2046
2047static void fec_enet_get_ethtool_stats(struct net_device *dev,
2048 struct ethtool_stats *stats, u64 *data)
2049{
2050 struct fec_enet_private *fep = netdev_priv(dev);
2051 int i;
2052
2053 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2054 data[i] = readl(fep->hwp + fec_stats[i].offset);
2055}
2056
2057static void fec_enet_get_strings(struct net_device *netdev,
2058 u32 stringset, u8 *data)
2059{
2060 int i;
2061 switch (stringset) {
2062 case ETH_SS_STATS:
2063 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2064 memcpy(data + i * ETH_GSTRING_LEN,
2065 fec_stats[i].name, ETH_GSTRING_LEN);
2066 break;
2067 }
2068}
2069
2070static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2071{
2072 switch (sset) {
2073 case ETH_SS_STATS:
2074 return ARRAY_SIZE(fec_stats);
2075 default:
2076 return -EOPNOTSUPP;
2077 }
2078}
Guenter Roeckd1391932013-06-18 10:04:59 -07002079#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002080
Chris Healy32bc9b42013-06-17 07:25:06 -07002081static int fec_enet_nway_reset(struct net_device *dev)
2082{
2083 struct fec_enet_private *fep = netdev_priv(dev);
2084 struct phy_device *phydev = fep->phy_dev;
2085
2086 if (!phydev)
2087 return -ENODEV;
2088
2089 return genphy_restart_aneg(phydev);
2090}
2091
stephen hemminger9b07be42012-01-04 12:59:49 +00002092static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002093 .get_settings = fec_enet_get_settings,
2094 .set_settings = fec_enet_set_settings,
2095 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002096 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002097 .get_link = ethtool_op_get_link,
Chris Healy38ae92d2013-06-25 23:18:52 -07002098#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002099 .get_pauseparam = fec_enet_get_pauseparam,
2100 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002101 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002102 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002103 .get_sset_count = fec_enet_get_sset_count,
2104#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002105 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00002106};
2107
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002108static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002109{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002110 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002111 struct phy_device *phydev = fep->phy_dev;
2112
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002113 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002114 return -EINVAL;
2115
2116 if (!phydev)
2117 return -ENODEV;
2118
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002119 if (fep->bufdesc_ex) {
2120 if (cmd == SIOCSHWTSTAMP)
2121 return fec_ptp_set(ndev, rq);
2122 if (cmd == SIOCGHWTSTAMP)
2123 return fec_ptp_get(ndev, rq);
2124 }
Frank Liff43da82013-01-03 16:04:23 +00002125
Richard Cochran28b04112010-07-17 08:48:55 +00002126 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002129static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002130{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002131 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002132 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002133 struct sk_buff *skb;
2134 struct bufdesc *bdp;
2135
2136 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002137 for (i = 0; i < fep->rx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002138 skb = fep->rx_skbuff[i];
Russell King730ee362014-07-08 00:23:14 +01002139 fep->rx_skbuff[i] = NULL;
2140 if (skb) {
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01002141 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002142 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002143 dev_kfree_skb(skb);
Russell King730ee362014-07-08 00:23:14 +01002144 }
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002145 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002146 }
2147
2148 bdp = fep->tx_bd_base;
Russell King8b7c9ef2014-07-08 00:23:25 +01002149 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002150 kfree(fep->tx_bounce[i]);
Russell King8b7c9ef2014-07-08 00:23:25 +01002151 fep->tx_bounce[i] = NULL;
2152 skb = fep->tx_skbuff[i];
2153 fep->tx_skbuff[i] = NULL;
2154 dev_kfree_skb(skb);
2155 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002156}
2157
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002158static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002159{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002160 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002161 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002162 struct sk_buff *skb;
2163 struct bufdesc *bdp;
2164
2165 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002166 for (i = 0; i < fep->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002167 dma_addr_t addr;
2168
Fabio Estevamb72061a2012-02-07 08:27:31 +00002169 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002170 if (!skb)
2171 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002172
Russell King730ee362014-07-08 00:23:14 +01002173 addr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002174 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Russell King730ee362014-07-08 00:23:14 +01002175 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2176 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002177 if (net_ratelimit())
2178 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002179 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002180 }
Russell King730ee362014-07-08 00:23:14 +01002181
2182 fep->rx_skbuff[i] = skb;
2183 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002184 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002185
2186 if (fep->bufdesc_ex) {
2187 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2188 ebdp->cbd_esc = BD_ENET_RX_INT;
2189 }
2190
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002191 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002192 }
2193
2194 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002195 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002196 bdp->cbd_sc |= BD_SC_WRAP;
2197
2198 bdp = fep->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002199 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002200 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
Russell Kingffdce2c2014-07-08 00:23:30 +01002201 if (!fep->tx_bounce[i])
2202 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002203
2204 bdp->cbd_sc = 0;
2205 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002206
Frank Liff43da82013-01-03 16:04:23 +00002207 if (fep->bufdesc_ex) {
2208 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002209 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002210 }
2211
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002212 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002213 }
2214
2215 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002216 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002217 bdp->cbd_sc |= BD_SC_WRAP;
2218
2219 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002220
2221 err_alloc:
2222 fec_enet_free_buffers(ndev);
2223 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002224}
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002227fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002229 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002230 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002232 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002233 ret = fec_enet_clk_enable(ndev, true);
2234 if (ret)
2235 return ret;
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* I should reset the ring buffers here, but I don't yet know
2238 * a simple way to do that.
2239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002241 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002242 if (ret)
2243 return ret;
2244
Bryan Wu418bd0d2010-05-28 03:40:39 -07002245 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002246 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002247 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002248 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002249 return ret;
2250 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002251
Russell Kingef833372014-07-08 12:40:43 +01002252 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002253 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002254 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002255 netif_start_queue(ndev);
Sascha Hauer22f6b862009-04-15 01:32:18 +00002256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
2259static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002260fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002262 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Russell Kingd76cfae2014-07-08 00:23:04 +01002264 phy_stop(fep->phy_dev);
2265
Russell King31a6de32014-07-08 12:40:23 +01002266 if (netif_device_present(ndev)) {
2267 napi_disable(&fep->napi);
2268 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002269 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Russell King635cf172014-07-08 00:22:54 +01002272 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002273 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002274
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002275 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002276 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002277 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return 0;
2280}
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282/* Set or clear the multicast filter for this adaptor.
2283 * Skeleton taken from sunlance driver.
2284 * The CPM Ethernet implementation allows Multicast as well as individual
2285 * MAC address filtering. Some of the drivers check to make sure it is
2286 * a group multicast address, and discard those that are not. I guess I
2287 * will do the same for now, but just remove the test if you want
2288 * individual filtering as well (do the upper net layers want or support
2289 * this kind of feature?).
2290 */
2291
2292#define HASH_BITS 6 /* #bits in hash */
2293#define CRC32_POLY 0xEDB88320
2294
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002295static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002297 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002298 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002299 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 unsigned char hash;
2301
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002302 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002303 tmp = readl(fep->hwp + FEC_R_CNTRL);
2304 tmp |= 0x8;
2305 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002306 return;
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Sascha Hauer4e831832009-04-15 01:32:19 +00002309 tmp = readl(fep->hwp + FEC_R_CNTRL);
2310 tmp &= ~0x8;
2311 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002312
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002313 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002314 /* Catch all multicast addresses, so set the
2315 * filter to all 1's
2316 */
2317 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2318 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Sascha Hauer4e831832009-04-15 01:32:19 +00002320 return;
2321 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002322
Sascha Hauer4e831832009-04-15 01:32:19 +00002323 /* Clear filter and add the addresses in hash register
2324 */
2325 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2326 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002328 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002329 /* calculate crc32 value of mac address */
2330 crc = 0xffffffff;
2331
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002332 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002333 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002334 for (bit = 0; bit < 8; bit++, data >>= 1) {
2335 crc = (crc >> 1) ^
2336 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002339
2340 /* only upper 6 bits (HASH_BITS) are used
2341 * which point to specific bit in he hash registers
2342 */
2343 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2344
2345 if (hash > 31) {
2346 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2347 tmp |= 1 << (hash - 32);
2348 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2349 } else {
2350 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2351 tmp |= 1 << hash;
2352 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355}
2356
Sascha Hauer22f6b862009-04-15 01:32:18 +00002357/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002358static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002359fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002361 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002362 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Lucas Stach44934fa2014-03-30 21:32:08 +02002364 if (addr) {
2365 if (!is_valid_ether_addr(addr->sa_data))
2366 return -EADDRNOTAVAIL;
2367 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2368 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002369
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002370 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2371 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002372 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002373 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002374 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002378#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002379/**
2380 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002381 * @dev: The FEC network adapter
2382 *
2383 * Polled functionality used by netconsole and others in non interrupt mode
2384 *
2385 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002386static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002387{
2388 int i;
2389 struct fec_enet_private *fep = netdev_priv(dev);
2390
2391 for (i = 0; i < FEC_IRQ_NUM; i++) {
2392 if (fep->irq[i] > 0) {
2393 disable_irq(fep->irq[i]);
2394 fec_enet_interrupt(fep->irq[i], dev);
2395 enable_irq(fep->irq[i]);
2396 }
2397 }
2398}
2399#endif
2400
Russell King8506fa12014-07-08 12:40:33 +01002401#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2402
Jim Baxter4c09eed2013-04-19 08:10:49 +00002403static int fec_set_features(struct net_device *netdev,
2404 netdev_features_t features)
2405{
2406 struct fec_enet_private *fep = netdev_priv(netdev);
2407 netdev_features_t changed = features ^ netdev->features;
2408
Russell King8506fa12014-07-08 12:40:33 +01002409 /* Quiesce the device if necessary */
2410 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2411 napi_disable(&fep->napi);
2412 netif_tx_lock_bh(netdev);
2413 fec_stop(netdev);
2414 }
2415
Jim Baxter4c09eed2013-04-19 08:10:49 +00002416 netdev->features = features;
2417
2418 /* Receive checksum has been changed */
2419 if (changed & NETIF_F_RXCSUM) {
2420 if (features & NETIF_F_RXCSUM)
2421 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2422 else
2423 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002424 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002425
Russell King8506fa12014-07-08 12:40:33 +01002426 /* Resume the device after updates */
2427 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002428 fec_restart(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002429 netif_wake_queue(netdev);
2430 netif_tx_unlock_bh(netdev);
2431 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002432 }
2433
2434 return 0;
2435}
2436
Sascha Hauer009fda82009-04-15 01:32:23 +00002437static const struct net_device_ops fec_netdev_ops = {
2438 .ndo_open = fec_enet_open,
2439 .ndo_stop = fec_enet_close,
2440 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002441 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002442 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002443 .ndo_validate_addr = eth_validate_addr,
2444 .ndo_tx_timeout = fec_timeout,
2445 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002446 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002447#ifdef CONFIG_NET_POLL_CONTROLLER
2448 .ndo_poll_controller = fec_poll_controller,
2449#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002450 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002451};
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /*
2454 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002455 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002457static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002459 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002460 const struct platform_device_id *id_entry =
2461 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002462 struct bufdesc *cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002463 int bd_size;
2464
2465 /* init the tx & rx ring size */
2466 fep->tx_ring_size = TX_RING_SIZE;
2467 fep->rx_ring_size = RX_RING_SIZE;
2468
Nimrod Andy79f33912014-06-12 08:16:23 +08002469 fep->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2470 fep->tx_wake_threshold = (fep->tx_ring_size - fep->tx_stop_threshold) / 2;
2471
Nimrod Andy55d02182014-06-12 08:16:21 +08002472 if (fep->bufdesc_ex)
2473 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2474 else
2475 fep->bufdesc_size = sizeof(struct bufdesc);
2476 bd_size = (fep->tx_ring_size + fep->rx_ring_size) *
2477 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002479 /* Allocate memory for buffer descriptors. */
Nimrod Andy55d02182014-06-12 08:16:21 +08002480 cbd_base = dma_alloc_coherent(NULL, bd_size, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002481 GFP_KERNEL);
2482 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10002483 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10002484
Nimrod Andy79f33912014-06-12 08:16:23 +08002485 fep->tso_hdrs = dma_alloc_coherent(NULL, fep->tx_ring_size * TSO_HEADER_SIZE,
2486 &fep->tso_hdrs_dma, GFP_KERNEL);
2487 if (!fep->tso_hdrs) {
2488 dma_free_coherent(NULL, bd_size, cbd_base, fep->bd_dma);
2489 return -ENOMEM;
2490 }
2491
Frank Li14109a52013-03-26 16:12:03 +00002492 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002493
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002494 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Shawn Guo49da97d2011-01-05 21:13:11 +00002496 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002497 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002498 /* make sure MAC we just acquired is programmed into the hw */
2499 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002501 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 fep->rx_bd_base = cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002503 if (fep->bufdesc_ex)
Frank Liff43da82013-01-03 16:04:23 +00002504 fep->tx_bd_base = (struct bufdesc *)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002505 (((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
Nimrod Andy55d02182014-06-12 08:16:21 +08002506 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002507 fep->tx_bd_base = cbd_base + fep->rx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Sascha Hauer22f6b862009-04-15 01:32:18 +00002509 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002510 ndev->watchdog_timeo = TX_TIMEOUT;
2511 ndev->netdev_ops = &fec_netdev_ops;
2512 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002513
Frank Lidc975382013-01-28 18:31:42 +00002514 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002515 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002516
Nimrod Andy09d1e542014-06-12 08:16:20 +08002517 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002518 /* enable hw VLAN support */
2519 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002520
Shawn Guo48496252013-05-08 21:08:22 +00002521 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002522 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2523
Shawn Guo48496252013-05-08 21:08:22 +00002524 /* enable hw accelerator */
2525 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002526 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002527 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2528 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002529
Nimrod Andy09d1e542014-06-12 08:16:20 +08002530 ndev->hw_features = ndev->features;
2531
Russell Kingef833372014-07-08 12:40:43 +01002532 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return 0;
2535}
2536
Shawn Guoca2cc332011-06-25 02:04:35 +08002537#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002538static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002539{
2540 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002541 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002542 struct device_node *np = pdev->dev.of_node;
2543
2544 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002545 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002546
Shawn Guoa3caad02012-06-27 03:45:24 +00002547 of_property_read_u32(np, "phy-reset-duration", &msec);
2548 /* A sane reset duration should not be longer than 1s */
2549 if (msec > 1000)
2550 msec = 1;
2551
Shawn Guoca2cc332011-06-25 02:04:35 +08002552 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002553 if (!gpio_is_valid(phy_reset))
2554 return;
2555
Shawn Guo119fc002012-06-27 03:45:22 +00002556 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2557 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002558 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002559 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002560 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002561 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002562 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08002563 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002564}
2565#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002566static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002567{
2568 /*
2569 * In case of platform probe, the reset has been done
2570 * by machine code.
2571 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002572}
2573#endif /* CONFIG_OF */
2574
Bill Pemberton33897cc2012-12-03 09:23:58 -05002575static int
Sascha Haueread73182009-01-28 23:03:11 +00002576fec_probe(struct platform_device *pdev)
2577{
2578 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002579 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002580 struct net_device *ndev;
2581 int i, irq, ret = 0;
2582 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002583 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002584 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002585 struct device_node *np = pdev->dev.of_node, *phy_node;
Shawn Guoca2cc332011-06-25 02:04:35 +08002586
2587 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2588 if (of_id)
2589 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002590
Sascha Haueread73182009-01-28 23:03:11 +00002591 /* Init network device */
2592 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00002593 if (!ndev)
2594 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002595
2596 SET_NETDEV_DEV(ndev, &pdev->dev);
2597
2598 /* setup board info structure */
2599 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002600
Guenter Roeckd1391932013-06-18 10:04:59 -07002601#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002602 /* default enable pause frame auto negotiation */
2603 if (pdev->id_entry &&
2604 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2605 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002606#endif
Frank Libaa70a52013-01-16 16:55:58 +00002607
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002608 /* Select default pin state */
2609 pinctrl_pm_select_default_state(&pdev->dev);
2610
Fabio Estevam399db752013-07-21 13:25:03 -03002611 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302612 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2613 if (IS_ERR(fep->hwp)) {
2614 ret = PTR_ERR(fep->hwp);
2615 goto failed_ioremap;
2616 }
2617
Bryan Wue6b043d2010-03-31 02:10:44 +00002618 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002619 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002620
Frank Liff43da82013-01-03 16:04:23 +00002621 fep->bufdesc_ex = 0;
2622
Sascha Haueread73182009-01-28 23:03:11 +00002623 platform_set_drvdata(pdev, ndev);
2624
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002625 phy_node = of_parse_phandle(np, "phy-handle", 0);
2626 if (!phy_node && of_phy_is_fixed_link(np)) {
2627 ret = of_phy_register_fixed_link(np);
2628 if (ret < 0) {
2629 dev_err(&pdev->dev,
2630 "broken fixed-link specification\n");
2631 goto failed_phy;
2632 }
2633 phy_node = of_node_get(np);
2634 }
2635 fep->phy_node = phy_node;
2636
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002637 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002638 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002639 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002640 if (pdata)
2641 fep->phy_interface = pdata->phy;
2642 else
2643 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2644 } else {
2645 fep->phy_interface = ret;
2646 }
2647
Fabio Estevame2f8d552013-02-22 06:40:45 +00002648 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2649 if (IS_ERR(fep->clk_ipg)) {
2650 ret = PTR_ERR(fep->clk_ipg);
2651 goto failed_clk;
2652 }
2653
2654 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2655 if (IS_ERR(fep->clk_ahb)) {
2656 ret = PTR_ERR(fep->clk_ahb);
2657 goto failed_clk;
2658 }
2659
Linus Torvalds38f56f32013-05-07 11:06:17 -07002660 /* enet_out is optional, depends on board */
2661 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2662 if (IS_ERR(fep->clk_enet_out))
2663 fep->clk_enet_out = NULL;
2664
Nimrod Andy91c0d982014-08-21 17:09:38 +08002665 fep->ptp_clk_on = false;
2666 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08002667
2668 /* clk_ref is optional, depends on board */
2669 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
2670 if (IS_ERR(fep->clk_ref))
2671 fep->clk_ref = NULL;
2672
Fabio Estevame2f8d552013-02-22 06:40:45 +00002673 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002674 fep->bufdesc_ex =
2675 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002676 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07002677 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002678 fep->bufdesc_ex = 0;
2679 }
2680
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002681 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002682 if (ret)
2683 goto failed_clk;
2684
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00002685 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2686 if (!IS_ERR(fep->reg_phy)) {
2687 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002688 if (ret) {
2689 dev_err(&pdev->dev,
2690 "Failed to enable phy regulator: %d\n", ret);
2691 goto failed_regulator;
2692 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002693 } else {
2694 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002695 }
2696
2697 fec_reset_phy(pdev);
2698
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002699 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00002700 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002701
2702 ret = fec_enet_init(ndev);
2703 if (ret)
2704 goto failed_init;
2705
Xiao Jiangc7c83d12011-09-29 02:15:56 +00002706 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00002707 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00002708 if (irq < 0) {
2709 if (i)
2710 break;
2711 ret = irq;
2712 goto failed_irq;
2713 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002714 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02002715 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002716 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00002717 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00002718 }
2719
Bryan Wue6b043d2010-03-31 02:10:44 +00002720 ret = fec_enet_mii_init(pdev);
2721 if (ret)
2722 goto failed_mii_init;
2723
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002724 /* Carrier starts down, phylib will bring it up */
2725 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002726 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002727 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002728
Sascha Haueread73182009-01-28 23:03:11 +00002729 ret = register_netdev(ndev);
2730 if (ret)
2731 goto failed_register;
2732
Fabio Estevameb1d0642013-04-13 07:25:36 +00002733 if (fep->bufdesc_ex && fep->ptp_clock)
2734 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2735
Russell King36cdc742014-07-08 13:01:44 +01002736 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00002737 return 0;
2738
2739failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00002740 fec_enet_mii_remove(fep);
2741failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002742failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002743failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002744 if (fep->reg_phy)
2745 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00002746failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002747 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00002748failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002749failed_phy:
2750 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00002751failed_ioremap:
2752 free_netdev(ndev);
2753
2754 return ret;
2755}
2756
Bill Pemberton33897cc2012-12-03 09:23:58 -05002757static int
Sascha Haueread73182009-01-28 23:03:11 +00002758fec_drv_remove(struct platform_device *pdev)
2759{
2760 struct net_device *ndev = platform_get_drvdata(pdev);
2761 struct fec_enet_private *fep = netdev_priv(ndev);
2762
Nimrod Andy91c0d982014-08-21 17:09:38 +08002763 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01002764 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002765 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002766 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002767 if (fep->reg_phy)
2768 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00002769 if (fep->ptp_clock)
2770 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002771 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002772 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00002773 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002774
Sascha Haueread73182009-01-28 23:03:11 +00002775 return 0;
2776}
2777
Fabio Estevamdd66d382014-07-24 18:24:01 -03002778static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002779{
Eric Benard87cad5c2010-06-18 04:19:54 +00002780 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002781 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002782
Russell Kingda1774e2014-07-08 12:39:57 +01002783 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002784 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01002785 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01002786 napi_disable(&fep->napi);
2787 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002788 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002789 netif_tx_unlock_bh(ndev);
2790 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002791 }
Russell Kingda1774e2014-07-08 12:39:57 +01002792 rtnl_unlock();
2793
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002794 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002795 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002796
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002797 if (fep->reg_phy)
2798 regulator_disable(fep->reg_phy);
2799
Sascha Haueread73182009-01-28 23:03:11 +00002800 return 0;
2801}
2802
Fabio Estevamdd66d382014-07-24 18:24:01 -03002803static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002804{
Eric Benard87cad5c2010-06-18 04:19:54 +00002805 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002806 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002807 int ret;
2808
2809 if (fep->reg_phy) {
2810 ret = regulator_enable(fep->reg_phy);
2811 if (ret)
2812 return ret;
2813 }
Sascha Haueread73182009-01-28 23:03:11 +00002814
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002815 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002816 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002817 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002818 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002819
Russell Kingda1774e2014-07-08 12:39:57 +01002820 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002821 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01002822 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002823 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002824 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002825 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002826 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01002827 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00002828 }
Russell Kingda1774e2014-07-08 12:39:57 +01002829 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002830
Sascha Haueread73182009-01-28 23:03:11 +00002831 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002832
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002833failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03002834 if (fep->reg_phy)
2835 regulator_disable(fep->reg_phy);
2836 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00002837}
2838
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002839static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002840
Sascha Haueread73182009-01-28 23:03:11 +00002841static struct platform_driver fec_driver = {
2842 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002843 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002844 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002845 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002846 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002847 },
Shawn Guob5680e02011-01-05 21:13:13 +00002848 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002849 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002850 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002851};
2852
Fabio Estevamaaca2372012-01-23 16:44:50 +00002853module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03002855MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856MODULE_LICENSE("GPL");