blob: c21ecff176089dd4c23dbdea0df528e90ee60e00 [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)
Shawn Guob5680e02011-01-05 21:13:13 +0000107
108static struct platform_device_id fec_devtype[] = {
109 {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800110 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +0000111 .name = DRIVER_NAME,
112 .driver_data = 0,
113 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800114 .name = "imx25-fec",
115 .driver_data = FEC_QUIRK_USE_GASKET,
116 }, {
117 .name = "imx27-fec",
118 .driver_data = 0,
119 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000120 .name = "imx28-fec",
121 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800122 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000123 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000124 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100125 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800126 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000127 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000128 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000129 .driver_data = FEC_QUIRK_ENET_MAC,
130 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800131 /* sentinel */
132 }
Shawn Guob5680e02011-01-05 21:13:13 +0000133};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800134MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000135
Shawn Guoca2cc332011-06-25 02:04:35 +0800136enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000137 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800138 IMX27_FEC, /* runs on i.mx27/35/51 */
139 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000140 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000141 MVF600_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800142};
143
144static const struct of_device_id fec_dt_ids[] = {
145 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
146 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
147 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000148 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000149 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800150 { /* sentinel */ }
151};
152MODULE_DEVICE_TABLE(of, fec_dt_ids);
153
Shawn Guo49da97d2011-01-05 21:13:11 +0000154static unsigned char macaddr[ETH_ALEN];
155module_param_array(macaddr, byte, NULL, 0);
156MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
157
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000158#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * Some hardware gets it MAC address out of local flash memory.
161 * if this is non-zero then assume it is the address to get MAC from.
162 */
163#if defined(CONFIG_NETtel)
164#define FEC_FLASHMAC 0xf0006006
165#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
166#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#elif defined(CONFIG_CANCam)
168#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000169#elif defined (CONFIG_M5272C3)
170#define FEC_FLASHMAC (0xffe04000 + 4)
171#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000172#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#else
174#define FEC_FLASHMAC 0
175#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800176#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000177
Sascha Hauer22f6b862009-04-15 01:32:18 +0000178/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
180#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
181#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
182#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
183#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
184#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
185#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
186#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
187#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
188#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
189
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000190#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000191#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000192
Jim Baxtercdffcf12013-07-02 22:52:56 +0100193/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100195#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100197#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jim Baxter4c09eed2013-04-19 08:10:49 +0000199/* FEC receive acceleration */
200#define FEC_RACC_IPDIS (1 << 1)
201#define FEC_RACC_PRODIS (1 << 2)
202#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
Matt Waddel6b265292006-06-27 13:10:56 +1000205 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * size bits. Other FEC hardware does not, so we need to take that into
207 * account when setting it.
208 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000209#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100210 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
212#else
213#define OPT_FRAME_SIZE 0
214#endif
215
Bryan Wue6b043d2010-03-31 02:10:44 +0000216/* FEC MII MMFR bits definition */
217#define FEC_MMFR_ST (1 << 30)
218#define FEC_MMFR_OP_READ (2 << 28)
219#define FEC_MMFR_OP_WRITE (1 << 28)
220#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
221#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
222#define FEC_MMFR_TA (2 << 16)
223#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500225#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Sascha Hauer22f6b862009-04-15 01:32:18 +0000227/* Transmitter timeout */
228#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Frank Libaa70a52013-01-16 16:55:58 +0000230#define FEC_PAUSE_FLAG_AUTONEG 0x1
231#define FEC_PAUSE_FLAG_ENABLE 0x2
232
Nimrod Andy79f33912014-06-12 08:16:23 +0800233#define TSO_HEADER_SIZE 128
234/* Max number of allowed TCP segments for software TSO */
235#define FEC_MAX_TSO_SEGS 100
236#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
237
238#define IS_TSO_HEADER(txq, addr) \
239 ((addr >= txq->tso_hdrs_dma) && \
240 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
241
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000242static int mii_cnt;
243
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800244static inline
245struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
Frank Liff43da82013-01-03 16:04:23 +0000246{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800247 struct bufdesc *new_bd = bdp + 1;
248 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
249 struct bufdesc_ex *ex_base;
250 struct bufdesc *base;
251 int ring_size;
252
253 if (bdp >= fep->tx_bd_base) {
254 base = fep->tx_bd_base;
255 ring_size = fep->tx_ring_size;
256 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
257 } else {
258 base = fep->rx_bd_base;
259 ring_size = fep->rx_ring_size;
260 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
261 }
262
263 if (fep->bufdesc_ex)
264 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
265 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000266 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800267 return (new_bd >= (base + ring_size)) ?
268 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000269}
270
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800271static inline
272struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
Frank Liff43da82013-01-03 16:04:23 +0000273{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800274 struct bufdesc *new_bd = bdp - 1;
275 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
276 struct bufdesc_ex *ex_base;
277 struct bufdesc *base;
278 int ring_size;
279
280 if (bdp >= fep->tx_bd_base) {
281 base = fep->tx_bd_base;
282 ring_size = fep->tx_ring_size;
283 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
284 } else {
285 base = fep->rx_bd_base;
286 ring_size = fep->rx_ring_size;
287 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
288 }
289
290 if (fep->bufdesc_ex)
291 return (struct bufdesc *)((ex_new_bd < ex_base) ?
292 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000293 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800294 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000295}
296
Nimrod Andy61a44272014-06-12 08:16:18 +0800297static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
298 struct fec_enet_private *fep)
299{
300 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
301}
302
Nimrod Andy6e909282014-06-12 08:16:22 +0800303static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep)
304{
305 int entries;
306
307 entries = ((const char *)fep->dirty_tx -
308 (const char *)fep->cur_tx) / fep->bufdesc_size - 1;
309
310 return entries > 0 ? entries : entries + fep->tx_ring_size;
311}
312
Shawn Guob5680e02011-01-05 21:13:13 +0000313static void *swap_buffer(void *bufaddr, int len)
314{
315 int i;
316 unsigned int *buf = bufaddr;
317
Fabio Estevamffed61e2013-05-21 05:44:26 +0000318 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000319 *buf = cpu_to_be32(*buf);
320
321 return bufaddr;
322}
323
Russell King344756f2014-07-08 13:01:59 +0100324static void fec_dump(struct net_device *ndev)
325{
326 struct fec_enet_private *fep = netdev_priv(ndev);
327 struct bufdesc *bdp = fep->tx_bd_base;
328 unsigned int index = 0;
329
330 netdev_info(ndev, "TX ring dump\n");
331 pr_info("Nr SC addr len SKB\n");
332
333 do {
334 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
335 index,
336 bdp == fep->cur_tx ? 'S' : ' ',
337 bdp == fep->dirty_tx ? 'H' : ' ',
338 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
339 fep->tx_skbuff[index]);
340 bdp = fec_enet_get_nextdesc(bdp, fep);
341 index++;
342 } while (bdp != fep->tx_bd_base);
343}
344
Fugang Duan62a02c92014-06-18 08:33:52 +0800345static inline bool is_ipv4_pkt(struct sk_buff *skb)
346{
347 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
348}
349
Jim Baxter4c09eed2013-04-19 08:10:49 +0000350static int
351fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
352{
353 /* Only run for packets requiring a checksum. */
354 if (skb->ip_summed != CHECKSUM_PARTIAL)
355 return 0;
356
357 if (unlikely(skb_cow_head(skb, 0)))
358 return -1;
359
Fugang Duan62a02c92014-06-18 08:33:52 +0800360 if (is_ipv4_pkt(skb))
361 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000362 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
363
364 return 0;
365}
366
Nimrod Andy6e909282014-06-12 08:16:22 +0800367static int
368fec_enet_txq_submit_frag_skb(struct sk_buff *skb, struct net_device *ndev)
369{
370 struct fec_enet_private *fep = netdev_priv(ndev);
371 const struct platform_device_id *id_entry =
372 platform_get_device_id(fep->pdev);
373 struct bufdesc *bdp = fep->cur_tx;
374 struct bufdesc_ex *ebdp;
375 int nr_frags = skb_shinfo(skb)->nr_frags;
376 int frag, frag_len;
377 unsigned short status;
378 unsigned int estatus = 0;
379 skb_frag_t *this_frag;
380 unsigned int index;
381 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100382 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800383 int i;
384
385 for (frag = 0; frag < nr_frags; frag++) {
386 this_frag = &skb_shinfo(skb)->frags[frag];
387 bdp = fec_enet_get_nextdesc(bdp, fep);
388 ebdp = (struct bufdesc_ex *)bdp;
389
390 status = bdp->cbd_sc;
391 status &= ~BD_ENET_TX_STATS;
392 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
393 frag_len = skb_shinfo(skb)->frags[frag].size;
394
395 /* Handle the last BD specially */
396 if (frag == nr_frags - 1) {
397 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
398 if (fep->bufdesc_ex) {
399 estatus |= BD_ENET_TX_INT;
400 if (unlikely(skb_shinfo(skb)->tx_flags &
401 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
402 estatus |= BD_ENET_TX_TS;
403 }
404 }
405
406 if (fep->bufdesc_ex) {
407 if (skb->ip_summed == CHECKSUM_PARTIAL)
408 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
409 ebdp->cbd_bdu = 0;
410 ebdp->cbd_esc = estatus;
411 }
412
413 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
414
415 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
416 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
417 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
418 memcpy(fep->tx_bounce[index], bufaddr, frag_len);
419 bufaddr = fep->tx_bounce[index];
420
421 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
422 swap_buffer(bufaddr, frag_len);
423 }
424
Russell Kingd6bf3142014-07-08 00:23:19 +0100425 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
426 DMA_TO_DEVICE);
427 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800428 dev_kfree_skb_any(skb);
429 if (net_ratelimit())
430 netdev_err(ndev, "Tx DMA memory map failed\n");
431 goto dma_mapping_error;
432 }
433
Russell Kingd6bf3142014-07-08 00:23:19 +0100434 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800435 bdp->cbd_datlen = frag_len;
436 bdp->cbd_sc = status;
437 }
438
439 fep->cur_tx = bdp;
440
441 return 0;
442
443dma_mapping_error:
444 bdp = fep->cur_tx;
445 for (i = 0; i < frag; i++) {
446 bdp = fec_enet_get_nextdesc(bdp, fep);
447 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
448 bdp->cbd_datlen, DMA_TO_DEVICE);
449 }
450 return NETDEV_TX_OK;
451}
452
453static int fec_enet_txq_submit_skb(struct sk_buff *skb, struct net_device *ndev)
454{
455 struct fec_enet_private *fep = netdev_priv(ndev);
456 const struct platform_device_id *id_entry =
457 platform_get_device_id(fep->pdev);
458 int nr_frags = skb_shinfo(skb)->nr_frags;
459 struct bufdesc *bdp, *last_bdp;
460 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100461 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800462 unsigned short status;
463 unsigned short buflen;
464 unsigned int estatus = 0;
465 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800466 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800467 int ret;
468
Nimrod Andy79f33912014-06-12 08:16:23 +0800469 entries_free = fec_enet_get_free_txdesc_num(fep);
470 if (entries_free < MAX_SKB_FRAGS + 1) {
471 dev_kfree_skb_any(skb);
472 if (net_ratelimit())
473 netdev_err(ndev, "NOT enough BD for SG!\n");
474 return NETDEV_TX_OK;
475 }
476
Nimrod Andy6e909282014-06-12 08:16:22 +0800477 /* Protocol checksum off-load for TCP and UDP. */
478 if (fec_enet_clear_csum(skb, ndev)) {
479 dev_kfree_skb_any(skb);
480 return NETDEV_TX_OK;
481 }
482
483 /* Fill in a Tx ring entry */
484 bdp = fep->cur_tx;
485 status = bdp->cbd_sc;
486 status &= ~BD_ENET_TX_STATS;
487
488 /* Set buffer length and buffer pointer */
489 bufaddr = skb->data;
490 buflen = skb_headlen(skb);
491
492 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
493 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
494 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
495 memcpy(fep->tx_bounce[index], skb->data, buflen);
496 bufaddr = fep->tx_bounce[index];
497
498 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
499 swap_buffer(bufaddr, buflen);
500 }
501
Russell Kingd6bf3142014-07-08 00:23:19 +0100502 /* Push the data cache so the CPM does not get stale memory data. */
503 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
504 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800505 dev_kfree_skb_any(skb);
506 if (net_ratelimit())
507 netdev_err(ndev, "Tx DMA memory map failed\n");
508 return NETDEV_TX_OK;
509 }
510
511 if (nr_frags) {
512 ret = fec_enet_txq_submit_frag_skb(skb, ndev);
513 if (ret)
514 return ret;
515 } else {
516 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
517 if (fep->bufdesc_ex) {
518 estatus = BD_ENET_TX_INT;
519 if (unlikely(skb_shinfo(skb)->tx_flags &
520 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
521 estatus |= BD_ENET_TX_TS;
522 }
523 }
524
525 if (fep->bufdesc_ex) {
526
527 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
528
529 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
530 fep->hwts_tx_en))
531 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
532
533 if (skb->ip_summed == CHECKSUM_PARTIAL)
534 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
535
536 ebdp->cbd_bdu = 0;
537 ebdp->cbd_esc = estatus;
538 }
539
540 last_bdp = fep->cur_tx;
541 index = fec_enet_get_bd_index(fep->tx_bd_base, last_bdp, fep);
542 /* Save skb pointer */
543 fep->tx_skbuff[index] = skb;
544
545 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100546 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800547
548 /* Send it on its way. Tell FEC it's ready, interrupt when done,
549 * it's the last BD of the frame, and to put the CRC on the end.
550 */
551 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
552 bdp->cbd_sc = status;
553
Sascha Hauer22f6b862009-04-15 01:32:18 +0000554 /* If this was the last BD in the ring, start at the beginning again. */
Nimrod Andy6e909282014-06-12 08:16:22 +0800555 bdp = fec_enet_get_nextdesc(last_bdp, fep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800557 skb_tx_timestamp(skb);
558
Sascha Hauer2e285322009-04-15 01:32:16 +0000559 fep->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Frank Lide5fb0a2013-03-03 17:34:25 +0000561 /* Trigger transmission start */
562 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
563
Nimrod Andy6e909282014-06-12 08:16:22 +0800564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Nimrod Andy79f33912014-06-12 08:16:23 +0800567static int
568fec_enet_txq_put_data_tso(struct sk_buff *skb, struct net_device *ndev,
569 struct bufdesc *bdp, int index, char *data,
570 int size, bool last_tcp, bool is_last)
571{
572 struct fec_enet_private *fep = netdev_priv(ndev);
573 const struct platform_device_id *id_entry =
574 platform_get_device_id(fep->pdev);
575 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
576 unsigned short status;
577 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100578 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800579
580 status = bdp->cbd_sc;
581 status &= ~BD_ENET_TX_STATS;
582
583 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800584
585 if (((unsigned long) data) & FEC_ALIGNMENT ||
586 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
587 memcpy(fep->tx_bounce[index], data, size);
588 data = fep->tx_bounce[index];
589
590 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
591 swap_buffer(data, size);
592 }
593
Russell Kingd6bf3142014-07-08 00:23:19 +0100594 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
595 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800596 dev_kfree_skb_any(skb);
597 if (net_ratelimit())
598 netdev_err(ndev, "Tx DMA memory map failed\n");
599 return NETDEV_TX_BUSY;
600 }
601
Russell Kingd6bf3142014-07-08 00:23:19 +0100602 bdp->cbd_datlen = size;
603 bdp->cbd_bufaddr = addr;
604
Nimrod Andy79f33912014-06-12 08:16:23 +0800605 if (fep->bufdesc_ex) {
606 if (skb->ip_summed == CHECKSUM_PARTIAL)
607 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
608 ebdp->cbd_bdu = 0;
609 ebdp->cbd_esc = estatus;
610 }
611
612 /* Handle the last BD specially */
613 if (last_tcp)
614 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
615 if (is_last) {
616 status |= BD_ENET_TX_INTR;
617 if (fep->bufdesc_ex)
618 ebdp->cbd_esc |= BD_ENET_TX_INT;
619 }
620
621 bdp->cbd_sc = status;
622
623 return 0;
624}
625
626static int
627fec_enet_txq_put_hdr_tso(struct sk_buff *skb, struct net_device *ndev,
628 struct bufdesc *bdp, int index)
629{
630 struct fec_enet_private *fep = netdev_priv(ndev);
631 const struct platform_device_id *id_entry =
632 platform_get_device_id(fep->pdev);
633 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
634 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
635 void *bufaddr;
636 unsigned long dmabuf;
637 unsigned short status;
638 unsigned int estatus = 0;
639
640 status = bdp->cbd_sc;
641 status &= ~BD_ENET_TX_STATS;
642 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
643
644 bufaddr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
645 dmabuf = fep->tso_hdrs_dma + index * TSO_HEADER_SIZE;
646 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
647 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
648 memcpy(fep->tx_bounce[index], skb->data, hdr_len);
649 bufaddr = fep->tx_bounce[index];
650
651 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
652 swap_buffer(bufaddr, hdr_len);
653
654 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
655 hdr_len, DMA_TO_DEVICE);
656 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
657 dev_kfree_skb_any(skb);
658 if (net_ratelimit())
659 netdev_err(ndev, "Tx DMA memory map failed\n");
660 return NETDEV_TX_BUSY;
661 }
662 }
663
664 bdp->cbd_bufaddr = dmabuf;
665 bdp->cbd_datlen = hdr_len;
666
667 if (fep->bufdesc_ex) {
668 if (skb->ip_summed == CHECKSUM_PARTIAL)
669 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
670 ebdp->cbd_bdu = 0;
671 ebdp->cbd_esc = estatus;
672 }
673
674 bdp->cbd_sc = status;
675
676 return 0;
677}
678
679static int fec_enet_txq_submit_tso(struct sk_buff *skb, struct net_device *ndev)
680{
681 struct fec_enet_private *fep = netdev_priv(ndev);
682 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
683 int total_len, data_left;
684 struct bufdesc *bdp = fep->cur_tx;
685 struct tso_t tso;
686 unsigned int index = 0;
687 int ret;
688
689 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep)) {
690 dev_kfree_skb_any(skb);
691 if (net_ratelimit())
692 netdev_err(ndev, "NOT enough BD for TSO!\n");
693 return NETDEV_TX_OK;
694 }
695
696 /* Protocol checksum off-load for TCP and UDP. */
697 if (fec_enet_clear_csum(skb, ndev)) {
698 dev_kfree_skb_any(skb);
699 return NETDEV_TX_OK;
700 }
701
702 /* Initialize the TSO handler, and prepare the first payload */
703 tso_start(skb, &tso);
704
705 total_len = skb->len - hdr_len;
706 while (total_len > 0) {
707 char *hdr;
708
709 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
710 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
711 total_len -= data_left;
712
713 /* prepare packet headers: MAC + IP + TCP */
714 hdr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
715 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
716 ret = fec_enet_txq_put_hdr_tso(skb, ndev, bdp, index);
717 if (ret)
718 goto err_release;
719
720 while (data_left > 0) {
721 int size;
722
723 size = min_t(int, tso.size, data_left);
724 bdp = fec_enet_get_nextdesc(bdp, fep);
725 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
726 ret = fec_enet_txq_put_data_tso(skb, ndev, bdp, index, tso.data,
727 size, size == data_left,
728 total_len == 0);
729 if (ret)
730 goto err_release;
731
732 data_left -= size;
733 tso_build_data(skb, &tso, size);
734 }
735
736 bdp = fec_enet_get_nextdesc(bdp, fep);
737 }
738
739 /* Save skb pointer */
740 fep->tx_skbuff[index] = skb;
741
Nimrod Andy79f33912014-06-12 08:16:23 +0800742 skb_tx_timestamp(skb);
743 fep->cur_tx = bdp;
744
745 /* Trigger transmission start */
746 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
747
748 return 0;
749
750err_release:
751 /* TODO: Release all used data descriptors for TSO */
752 return ret;
753}
754
Nimrod Andy61a44272014-06-12 08:16:18 +0800755static netdev_tx_t
756fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
757{
758 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800759 int entries_free;
Nimrod Andy61a44272014-06-12 08:16:18 +0800760 int ret;
761
Nimrod Andy79f33912014-06-12 08:16:23 +0800762 if (skb_is_gso(skb))
763 ret = fec_enet_txq_submit_tso(skb, ndev);
764 else
765 ret = fec_enet_txq_submit_skb(skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800766 if (ret)
767 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800768
Nimrod Andy6e909282014-06-12 08:16:22 +0800769 entries_free = fec_enet_get_free_txdesc_num(fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800770 if (entries_free <= fep->tx_stop_threshold)
Nimrod Andy61a44272014-06-12 08:16:18 +0800771 netif_stop_queue(ndev);
772
773 return NETDEV_TX_OK;
774}
775
Frank Li14109a52013-03-26 16:12:03 +0000776/* Init RX & TX buffer descriptors
777 */
778static void fec_enet_bd_init(struct net_device *dev)
779{
780 struct fec_enet_private *fep = netdev_priv(dev);
781 struct bufdesc *bdp;
782 unsigned int i;
783
784 /* Initialize the receive buffer descriptors. */
785 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800786 for (i = 0; i < fep->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000787
788 /* Initialize the BD for every fragment in the page. */
789 if (bdp->cbd_bufaddr)
790 bdp->cbd_sc = BD_ENET_RX_EMPTY;
791 else
792 bdp->cbd_sc = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800793 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000794 }
795
796 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800797 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000798 bdp->cbd_sc |= BD_SC_WRAP;
799
800 fep->cur_rx = fep->rx_bd_base;
801
802 /* ...and the same for transmit */
803 bdp = fep->tx_bd_base;
804 fep->cur_tx = bdp;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800805 for (i = 0; i < fep->tx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000806
807 /* Initialize the BD for every fragment in the page. */
808 bdp->cbd_sc = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100809 if (fep->tx_skbuff[i]) {
Frank Li14109a52013-03-26 16:12:03 +0000810 dev_kfree_skb_any(fep->tx_skbuff[i]);
811 fep->tx_skbuff[i] = NULL;
812 }
813 bdp->cbd_bufaddr = 0;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800814 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000815 }
816
817 /* Set the last buffer to wrap */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800818 bdp = fec_enet_get_prevdesc(bdp, fep);
Frank Li14109a52013-03-26 16:12:03 +0000819 bdp->cbd_sc |= BD_SC_WRAP;
820 fep->dirty_tx = bdp;
821}
822
Russell Kingdbc64a82014-07-08 12:40:12 +0100823/*
824 * This function is called to start or restart the FEC during a link
825 * change, transmit timeout, or to reconfigure the FEC. The network
826 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100827 */
828static void
Russell Kingef833372014-07-08 12:40:43 +0100829fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100830{
831 struct fec_enet_private *fep = netdev_priv(ndev);
832 const struct platform_device_id *id_entry =
833 platform_get_device_id(fep->pdev);
834 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000835 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100836 u32 temp_mac[2];
837 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000838 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100839
840 /* Whack a reset. We should wait for this. */
841 writel(1, fep->hwp + FEC_ECNTRL);
842 udelay(10);
843
844 /*
845 * enet-mac reset will reset mac address registers too,
846 * so need to reconfigure it.
847 */
848 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
849 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
850 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
851 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
852 }
853
854 /* Clear any outstanding interrupt. */
855 writel(0xffc00000, fep->hwp + FEC_IEVENT);
856
Uwe Kleine-König45993652011-01-19 20:47:04 +0100857 /* Set maximum receive buffer size. */
858 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
859
Frank Li14109a52013-03-26 16:12:03 +0000860 fec_enet_bd_init(ndev);
861
Uwe Kleine-König45993652011-01-19 20:47:04 +0100862 /* Set receive and transmit descriptor base. */
863 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000864 if (fep->bufdesc_ex)
865 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800866 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Frank Liff43da82013-01-03 16:04:23 +0000867 else
868 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800869 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100870
Uwe Kleine-König45993652011-01-19 20:47:04 +0100871
Uwe Kleine-König45993652011-01-19 20:47:04 +0100872 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
873 if (fep->tx_skbuff[i]) {
874 dev_kfree_skb_any(fep->tx_skbuff[i]);
875 fep->tx_skbuff[i] = NULL;
876 }
877 }
878
879 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100880 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100881 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100882 writel(0x04, fep->hwp + FEC_X_CNTRL);
883 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100884 /* No Rcv on Xmit */
885 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100886 writel(0x0, fep->hwp + FEC_X_CNTRL);
887 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100888
Uwe Kleine-König45993652011-01-19 20:47:04 +0100889 /* Set MII speed */
890 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
891
Guenter Roeckd1391932013-06-18 10:04:59 -0700892#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000893 /* set RX checksum */
894 val = readl(fep->hwp + FEC_RACC);
895 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
896 val |= FEC_RACC_OPTIONS;
897 else
898 val &= ~FEC_RACC_OPTIONS;
899 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700900#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000901
Uwe Kleine-König45993652011-01-19 20:47:04 +0100902 /*
903 * The phy interface and speed need to get configured
904 * differently on enet-mac.
905 */
906 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100907 /* Enable flow control and length check */
908 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100909
Shawn Guo230dec62011-09-23 02:12:48 +0000910 /* RGMII, RMII or MII */
911 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
912 rcntl |= (1 << 6);
913 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100914 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100915 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100916 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100917
Shawn Guo230dec62011-09-23 02:12:48 +0000918 /* 1G, 100M or 10M */
919 if (fep->phy_dev) {
920 if (fep->phy_dev->speed == SPEED_1000)
921 ecntl |= (1 << 5);
922 else if (fep->phy_dev->speed == SPEED_100)
923 rcntl &= ~(1 << 9);
924 else
925 rcntl |= (1 << 9);
926 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100927 } else {
928#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +0800929 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +0000930 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100931 /* disable the gasket and wait */
932 writel(0, fep->hwp + FEC_MIIGSK_ENR);
933 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
934 udelay(1);
935
936 /*
937 * configure the gasket:
938 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +0800939 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +0100940 */
Eric Benard8d82f212012-01-12 06:10:28 +0000941 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
942 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
943 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
944 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
945 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100946
947 /* re-enable the gasket */
948 writel(2, fep->hwp + FEC_MIIGSK_ENR);
949 }
950#endif
951 }
Frank Libaa70a52013-01-16 16:55:58 +0000952
Guenter Roeckd1391932013-06-18 10:04:59 -0700953#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +0000954 /* enable pause frame*/
955 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
956 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
957 fep->phy_dev && fep->phy_dev->pause)) {
958 rcntl |= FEC_ENET_FCE;
959
Jim Baxter4c09eed2013-04-19 08:10:49 +0000960 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +0000961 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
962 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
963 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
964 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
965
966 /* OPD */
967 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
968 } else {
969 rcntl &= ~FEC_ENET_FCE;
970 }
Guenter Roeckd1391932013-06-18 10:04:59 -0700971#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +0000972
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100973 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100974
Stefan Wahren84fe6182014-03-12 11:28:19 +0100975 /* Setup multicast filter. */
976 set_multicast_list(ndev);
977#ifndef CONFIG_M5272
978 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
979 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
980#endif
981
Shawn Guo230dec62011-09-23 02:12:48 +0000982 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
983 /* enable ENET endian swap */
984 ecntl |= (1 << 8);
985 /* enable ENET store and forward mode */
986 writel(1 << 8, fep->hwp + FEC_X_WMRK);
987 }
988
Frank Liff43da82013-01-03 16:04:23 +0000989 if (fep->bufdesc_ex)
990 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +0000991
Chris Healy38ae92d2013-06-25 23:18:52 -0700992#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +0100993 /* Enable the MIB statistic event counters */
994 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -0700995#endif
996
Uwe Kleine-König45993652011-01-19 20:47:04 +0100997 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +0000998 writel(ecntl, fep->hwp + FEC_ECNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100999 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
1000
Frank Liff43da82013-01-03 16:04:23 +00001001 if (fep->bufdesc_ex)
1002 fec_ptp_start_cyclecounter(ndev);
1003
Uwe Kleine-König45993652011-01-19 20:47:04 +01001004 /* Enable interrupts we wish to service */
1005 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1006}
1007
1008static void
1009fec_stop(struct net_device *ndev)
1010{
1011 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001012 const struct platform_device_id *id_entry =
1013 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001014 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001015
1016 /* We cannot expect a graceful transmit stop without link !!! */
1017 if (fep->link) {
1018 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1019 udelay(10);
1020 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001021 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001022 }
1023
1024 /* Whack a reset. We should wait for this. */
1025 writel(1, fep->hwp + FEC_ECNTRL);
1026 udelay(10);
1027 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1028 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001029
1030 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001031 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001032 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001033 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1034 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001035}
1036
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001039fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001041 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Russell King344756f2014-07-08 13:01:59 +01001043 fec_dump(ndev);
1044
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001045 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Russell King36cdc742014-07-08 13:01:44 +01001047 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001048}
1049
Russell King36cdc742014-07-08 13:01:44 +01001050static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001051{
1052 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001053 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001054 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001055
Russell King36cdc742014-07-08 13:01:44 +01001056 rtnl_lock();
1057 if (netif_device_present(ndev) || netif_running(ndev)) {
1058 napi_disable(&fep->napi);
1059 netif_tx_lock_bh(ndev);
1060 fec_restart(ndev);
1061 netif_wake_queue(ndev);
1062 netif_tx_unlock_bh(ndev);
1063 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001064 }
Russell King36cdc742014-07-08 13:01:44 +01001065 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001069fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1070 struct skb_shared_hwtstamps *hwtstamps)
1071{
1072 unsigned long flags;
1073 u64 ns;
1074
1075 spin_lock_irqsave(&fep->tmreg_lock, flags);
1076 ns = timecounter_cyc2time(&fep->tc, ts);
1077 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1078
1079 memset(hwtstamps, 0, sizeof(*hwtstamps));
1080 hwtstamps->hwtstamp = ns_to_ktime(ns);
1081}
1082
1083static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001084fec_enet_tx(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001087 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001088 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct sk_buff *skb;
Frank Lide5fb0a2013-03-03 17:34:25 +00001090 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001091 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001093 fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 bdp = fep->dirty_tx;
1095
Frank Lide5fb0a2013-03-03 17:34:25 +00001096 /* get next bdp of dirty_tx */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001097 bdp = fec_enet_get_nextdesc(bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001098
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001099 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001100
1101 /* current queue is empty */
1102 if (bdp == fep->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001103 break;
1104
Nimrod Andy61a44272014-06-12 08:16:18 +08001105 index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001106
Frank Lide5fb0a2013-03-03 17:34:25 +00001107 skb = fep->tx_skbuff[index];
Russell Kingd6bf3142014-07-08 00:23:19 +01001108 fep->tx_skbuff[index] = NULL;
Nimrod Andy79f33912014-06-12 08:16:23 +08001109 if (!IS_TSO_HEADER(fep, bdp->cbd_bufaddr))
1110 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1111 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001112 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001113 if (!skb) {
1114 bdp = fec_enet_get_nextdesc(bdp, fep);
1115 continue;
1116 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001119 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 BD_ENET_TX_RL | BD_ENET_TX_UN |
1121 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001122 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001123 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001124 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001125 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001126 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001127 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001128 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001129 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001130 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001131 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001132 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001134 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001135 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Frank Liff43da82013-01-03 16:04:23 +00001138 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1139 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001140 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001141 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001142
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001143 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001144 skb_tstamp_tx(skb, &shhwtstamps);
1145 }
Frank Liff43da82013-01-03 16:04:23 +00001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* Deferred means some collisions occurred during transmit,
1148 * but we eventually sent the packet OK.
1149 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001150 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001151 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001152
Sascha Hauer22f6b862009-04-15 01:32:18 +00001153 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001155
1156 fep->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001157
Sascha Hauer22f6b862009-04-15 01:32:18 +00001158 /* Update pointer to next buffer descriptor to be transmitted */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001159 bdp = fec_enet_get_nextdesc(bdp, fep);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001160
Sascha Hauer22f6b862009-04-15 01:32:18 +00001161 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001163 if (netif_queue_stopped(ndev)) {
1164 entries_free = fec_enet_get_free_txdesc_num(fep);
1165 if (entries_free >= fep->tx_wake_threshold)
1166 netif_wake_queue(ndev);
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
Russell Kingccea2962014-07-08 13:01:38 +01001169
1170 /* ERR006538: Keep the transmitter going */
1171 if (bdp != fep->cur_tx && readl(fep->hwp + FEC_X_DES_ACTIVE) == 0)
1172 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175/* During a receive, the cur_rx points to the current incoming buffer.
1176 * When we update through the ring, if the next incoming buffer has
1177 * not been given to the system, we just set the empty indicator,
1178 * effectively tossing the packet.
1179 */
Frank Lidc975382013-01-28 18:31:42 +00001180static int
1181fec_enet_rx(struct net_device *ndev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001183 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001184 const struct platform_device_id *id_entry =
1185 platform_get_device_id(fep->pdev);
Sascha Hauer2e285322009-04-15 01:32:16 +00001186 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001187 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 struct sk_buff *skb;
1189 ushort pkt_len;
1190 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001191 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001192 struct bufdesc_ex *ebdp = NULL;
1193 bool vlan_packet_rcvd = false;
1194 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001195 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001196
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001197#ifdef CONFIG_M532x
1198 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* First, grab all of the stats for the incoming packet.
1202 * These get messed up if we get called due to a busy condition.
1203 */
1204 bdp = fep->cur_rx;
1205
Sascha Hauer22f6b862009-04-15 01:32:18 +00001206 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Frank Lidc975382013-01-28 18:31:42 +00001208 if (pkt_received >= budget)
1209 break;
1210 pkt_received++;
1211
Sascha Hauer22f6b862009-04-15 01:32:18 +00001212 /* Since we have allocated space to hold a complete frame,
1213 * the last indicator should be set.
1214 */
1215 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001216 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Russell Kingdb3421c2014-07-08 13:01:49 +01001218 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
1219
Sascha Hauer22f6b862009-04-15 01:32:18 +00001220 /* Check for errors. */
1221 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001223 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001224 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1225 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001226 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001227 }
1228 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001229 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001230 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001231 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001232 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001233 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001235
1236 /* Report late collisions as a frame error.
1237 * On this error, the BD is closed, but we don't know what we
1238 * have in the buffer. So, just drop this frame on the floor.
1239 */
1240 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001241 ndev->stats.rx_errors++;
1242 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001243 goto rx_processing_done;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Sascha Hauer22f6b862009-04-15 01:32:18 +00001246 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001247 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001248 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001249 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Nimrod Andy61a44272014-06-12 08:16:18 +08001251 index = fec_enet_get_bd_index(fep->rx_bd_base, bdp, fep);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001252 data = fep->rx_skbuff[index]->data;
1253 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1254 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001255
Shawn Guob5680e02011-01-05 21:13:13 +00001256 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1257 swap_buffer(data, pkt_len);
1258
Jim Baxtercdffcf12013-07-02 22:52:56 +01001259 /* Extract the enhanced buffer descriptor */
1260 ebdp = NULL;
1261 if (fep->bufdesc_ex)
1262 ebdp = (struct bufdesc_ex *)bdp;
1263
1264 /* If this is a VLAN packet remove the VLAN Tag */
1265 vlan_packet_rcvd = false;
1266 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1267 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
1268 /* Push and remove the vlan tag */
1269 struct vlan_hdr *vlan_header =
1270 (struct vlan_hdr *) (data + ETH_HLEN);
1271 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1272 pkt_len -= VLAN_HLEN;
1273
1274 vlan_packet_rcvd = true;
1275 }
1276
Sascha Hauer22f6b862009-04-15 01:32:18 +00001277 /* This does 16 byte alignment, exactly what we need.
1278 * The packet length includes FCS, but we don't want to
1279 * include that when passing upstream as it messes up
1280 * bridging applications.
1281 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001282 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Sascha Hauer85498892009-04-15 01:32:21 +00001284 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001285 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001286 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001287 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001288 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001289 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001290
1291 /* Extract the frame data without the VLAN header. */
1292 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1293 if (vlan_packet_rcvd)
1294 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
1295 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
1296 data + payload_offset,
1297 pkt_len - 4 - (2 * ETH_ALEN));
1298
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001299 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001300
Frank Li6605b732012-10-30 18:25:31 +00001301 /* Get receive timestamp from the skb */
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001302 if (fep->hwts_rx_en && fep->bufdesc_ex)
1303 fec_enet_hwtstamp(fep, ebdp->ts,
1304 skb_hwtstamps(skb));
Frank Liff43da82013-01-03 16:04:23 +00001305
Jim Baxter4c09eed2013-04-19 08:10:49 +00001306 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001307 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001308 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1309 /* don't check it */
1310 skb->ip_summed = CHECKSUM_UNNECESSARY;
1311 } else {
1312 skb_checksum_none_assert(skb);
1313 }
1314 }
1315
Jim Baxtercdffcf12013-07-02 22:52:56 +01001316 /* Handle received VLAN packets */
1317 if (vlan_packet_rcvd)
1318 __vlan_hwaccel_put_tag(skb,
1319 htons(ETH_P_8021Q),
1320 vlan_tag);
1321
Richard Cochran0affdf32013-08-30 20:28:10 +02001322 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001323 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001324
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001325 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1326 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001327rx_processing_done:
1328 /* Clear the status flags for this buffer */
1329 status &= ~BD_ENET_RX_STATS;
1330
1331 /* Mark the buffer empty */
1332 status |= BD_ENET_RX_EMPTY;
1333 bdp->cbd_sc = status;
1334
Frank Liff43da82013-01-03 16:04:23 +00001335 if (fep->bufdesc_ex) {
1336 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1337
1338 ebdp->cbd_esc = BD_ENET_RX_INT;
1339 ebdp->cbd_prot = 0;
1340 ebdp->cbd_bdu = 0;
1341 }
Frank Li6605b732012-10-30 18:25:31 +00001342
Sascha Hauer22f6b862009-04-15 01:32:18 +00001343 /* Update BD pointer to next entry */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001344 bdp = fec_enet_get_nextdesc(bdp, fep);
1345
Sascha Hauer22f6b862009-04-15 01:32:18 +00001346 /* Doing this here will keep the FEC running while we process
1347 * incoming frames. On a heavily loaded network, we should be
1348 * able to keep up at the expense of system resources.
1349 */
1350 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Sascha Hauer2e285322009-04-15 01:32:16 +00001352 fep->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Frank Lidc975382013-01-28 18:31:42 +00001354 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Uwe Kleine-König45993652011-01-19 20:47:04 +01001357static irqreturn_t
1358fec_enet_interrupt(int irq, void *dev_id)
1359{
1360 struct net_device *ndev = dev_id;
1361 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001362 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001363 uint int_events;
1364 irqreturn_t ret = IRQ_NONE;
1365
Russell King7a168072014-07-08 00:22:44 +01001366 int_events = readl(fep->hwp + FEC_IEVENT);
1367 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001368
Russell King7a168072014-07-08 00:22:44 +01001369 if (int_events & napi_mask) {
1370 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001371
Russell King7a168072014-07-08 00:22:44 +01001372 /* Disable the NAPI interrupts */
1373 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1374 napi_schedule(&fep->napi);
1375 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001376
Russell King7a168072014-07-08 00:22:44 +01001377 if (int_events & FEC_ENET_MII) {
1378 ret = IRQ_HANDLED;
1379 complete(&fep->mdio_done);
1380 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001381
1382 return ret;
1383}
1384
Frank Lidc975382013-01-28 18:31:42 +00001385static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1386{
1387 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001388 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001389 int pkts;
1390
1391 /*
1392 * Clear any pending transmit or receive interrupts before
1393 * processing the rings to avoid racing with the hardware.
1394 */
1395 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1396
1397 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001398
Frank Lide5fb0a2013-03-03 17:34:25 +00001399 fec_enet_tx(ndev);
1400
Frank Lidc975382013-01-28 18:31:42 +00001401 if (pkts < budget) {
1402 napi_complete(napi);
1403 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1404 }
1405 return pkts;
1406}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001409static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001411 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001412 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001413 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Shawn Guo49da97d2011-01-05 21:13:11 +00001415 /*
1416 * try to get mac address in following order:
1417 *
1418 * 1) module parameter via kernel command line in form
1419 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1420 */
1421 iap = macaddr;
1422
1423 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001424 * 2) from device tree data
1425 */
1426 if (!is_valid_ether_addr(iap)) {
1427 struct device_node *np = fep->pdev->dev.of_node;
1428 if (np) {
1429 const char *mac = of_get_mac_address(np);
1430 if (mac)
1431 iap = (unsigned char *) mac;
1432 }
1433 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001434
1435 /*
1436 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001437 */
1438 if (!is_valid_ether_addr(iap)) {
1439#ifdef CONFIG_M5272
1440 if (FEC_FLASHMAC)
1441 iap = (unsigned char *)FEC_FLASHMAC;
1442#else
1443 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001444 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001445#endif
1446 }
1447
1448 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001449 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001450 */
1451 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001452 *((__be32 *) &tmpaddr[0]) =
1453 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1454 *((__be16 *) &tmpaddr[4]) =
1455 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 iap = &tmpaddr[0];
1457 }
1458
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001459 /*
1460 * 5) random mac address
1461 */
1462 if (!is_valid_ether_addr(iap)) {
1463 /* Report it and use a random ethernet address instead */
1464 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1465 eth_hw_addr_random(ndev);
1466 netdev_info(ndev, "Using random MAC address: %pM\n",
1467 ndev->dev_addr);
1468 return;
1469 }
1470
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001471 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Shawn Guo49da97d2011-01-05 21:13:11 +00001473 /* Adjust MAC if using macaddr */
1474 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001475 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478/* ------------------------------------------------------------------------- */
1479
Bryan Wue6b043d2010-03-31 02:10:44 +00001480/*
1481 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001483static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001485 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001486 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001487 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Bryan Wue6b043d2010-03-31 02:10:44 +00001489 /* Prevent a state halted on mii error */
1490 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1491 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001492 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001494
Russell King8ce56242014-07-08 12:40:07 +01001495 /*
1496 * If the netdev is down, or is going down, we're not interested
1497 * in link state events, so just mark our idea of the link as down
1498 * and ignore the event.
1499 */
1500 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1501 fep->link = 0;
1502 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001503 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001504 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001505 status_change = 1;
1506 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001507
Russell Kingef833372014-07-08 12:40:43 +01001508 if (fep->full_duplex != phy_dev->duplex) {
1509 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001510 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001511 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001512
1513 if (phy_dev->speed != fep->speed) {
1514 fep->speed = phy_dev->speed;
1515 status_change = 1;
1516 }
1517
1518 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001519 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001520 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001521 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001522 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001523 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001524 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001525 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001526 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001527 } else {
1528 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001529 napi_disable(&fep->napi);
1530 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001531 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001532 netif_tx_unlock_bh(ndev);
1533 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001534 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001535 status_change = 1;
1536 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001537 }
1538
Bryan Wue6b043d2010-03-31 02:10:44 +00001539 if (status_change)
1540 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Bryan Wue6b043d2010-03-31 02:10:44 +00001543static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Bryan Wue6b043d2010-03-31 02:10:44 +00001545 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001546 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001547
1548 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001549 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001550
1551 /* start a read op */
1552 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1553 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1554 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1555
1556 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001557 time_left = wait_for_completion_timeout(&fep->mdio_done,
1558 usecs_to_jiffies(FEC_MII_TIMEOUT));
1559 if (time_left == 0) {
1560 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001561 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001562 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001563 }
1564
1565 /* return value */
1566 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1567}
1568
1569static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1570 u16 value)
1571{
1572 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001573 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001574
1575 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001576 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001577
Shawn Guo862f0982011-01-05 21:13:09 +00001578 /* start a write op */
1579 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001580 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1581 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1582 fep->hwp + FEC_MII_DATA);
1583
1584 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001585 time_left = wait_for_completion_timeout(&fep->mdio_done,
1586 usecs_to_jiffies(FEC_MII_TIMEOUT));
1587 if (time_left == 0) {
1588 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001589 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001590 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001591 }
1592
1593 return 0;
1594}
1595
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001596static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1597{
1598 struct fec_enet_private *fep = netdev_priv(ndev);
1599 int ret;
1600
1601 if (enable) {
1602 ret = clk_prepare_enable(fep->clk_ahb);
1603 if (ret)
1604 return ret;
1605 ret = clk_prepare_enable(fep->clk_ipg);
1606 if (ret)
1607 goto failed_clk_ipg;
1608 if (fep->clk_enet_out) {
1609 ret = clk_prepare_enable(fep->clk_enet_out);
1610 if (ret)
1611 goto failed_clk_enet_out;
1612 }
1613 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001614 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001615 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001616 if (ret) {
1617 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001618 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001619 } else {
1620 fep->ptp_clk_on = true;
1621 }
1622 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001623 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001624 if (fep->clk_ref) {
1625 ret = clk_prepare_enable(fep->clk_ref);
1626 if (ret)
1627 goto failed_clk_ref;
1628 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001629 } else {
1630 clk_disable_unprepare(fep->clk_ahb);
1631 clk_disable_unprepare(fep->clk_ipg);
1632 if (fep->clk_enet_out)
1633 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001634 if (fep->clk_ptp) {
1635 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001636 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001637 fep->ptp_clk_on = false;
1638 mutex_unlock(&fep->ptp_clk_mutex);
1639 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001640 if (fep->clk_ref)
1641 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001642 }
1643
1644 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001645
1646failed_clk_ref:
1647 if (fep->clk_ref)
1648 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001649failed_clk_ptp:
1650 if (fep->clk_enet_out)
1651 clk_disable_unprepare(fep->clk_enet_out);
1652failed_clk_enet_out:
1653 clk_disable_unprepare(fep->clk_ipg);
1654failed_clk_ipg:
1655 clk_disable_unprepare(fep->clk_ahb);
1656
1657 return ret;
1658}
1659
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001660static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001661{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001662 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001663 const struct platform_device_id *id_entry =
1664 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001665 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001666 char mdio_bus_id[MII_BUS_ID_SIZE];
1667 char phy_name[MII_BUS_ID_SIZE + 3];
1668 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001669 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001670
Bryan Wu418bd0d2010-05-28 03:40:39 -07001671 fep->phy_dev = NULL;
1672
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001673 if (fep->phy_node) {
1674 phy_dev = of_phy_connect(ndev, fep->phy_node,
1675 &fec_enet_adjust_link, 0,
1676 fep->phy_interface);
1677 } else {
1678 /* check for attached phy */
1679 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1680 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1681 continue;
1682 if (fep->mii_bus->phy_map[phy_id] == NULL)
1683 continue;
1684 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1685 continue;
1686 if (dev_id--)
1687 continue;
1688 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1689 break;
1690 }
1691
1692 if (phy_id >= PHY_MAX_ADDR) {
1693 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1694 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1695 phy_id = 0;
1696 }
1697
1698 snprintf(phy_name, sizeof(phy_name),
1699 PHY_ID_FMT, mdio_bus_id, phy_id);
1700 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1701 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001702 }
1703
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001704 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001705 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001706 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001707 }
1708
1709 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001710 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001711 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001712 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001713#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001714 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001715#endif
Frank Libaa70a52013-01-16 16:55:58 +00001716 }
Shawn Guo230dec62011-09-23 02:12:48 +00001717 else
1718 phy_dev->supported &= PHY_BASIC_FEATURES;
1719
Bryan Wue6b043d2010-03-31 02:10:44 +00001720 phy_dev->advertising = phy_dev->supported;
1721
1722 fep->phy_dev = phy_dev;
1723 fep->link = 0;
1724 fep->full_duplex = 0;
1725
Joe Perches31b77202013-04-13 19:03:17 +00001726 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1727 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1728 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001729
Bryan Wue6b043d2010-03-31 02:10:44 +00001730 return 0;
1731}
1732
1733static int fec_enet_mii_init(struct platform_device *pdev)
1734{
Shawn Guob5680e02011-01-05 21:13:13 +00001735 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001736 struct net_device *ndev = platform_get_drvdata(pdev);
1737 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001738 const struct platform_device_id *id_entry =
1739 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001740 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001741 int err = -ENXIO, i;
1742
Shawn Guob5680e02011-01-05 21:13:13 +00001743 /*
1744 * The dual fec interfaces are not equivalent with enet-mac.
1745 * Here are the differences:
1746 *
1747 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1748 * - fec0 acts as the 1588 time master while fec1 is slave
1749 * - external phys can only be configured by fec0
1750 *
1751 * That is to say fec1 can not work independently. It only works
1752 * when fec0 is working. The reason behind this design is that the
1753 * second interface is added primarily for Switch mode.
1754 *
1755 * Because of the last point above, both phys are attached on fec0
1756 * mdio interface in board design, and need to be configured by
1757 * fec0 mii_bus.
1758 */
Shawn Guo43af9402011-12-05 05:01:15 +00001759 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001760 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001761 if (mii_cnt && fec0_mii_bus) {
1762 fep->mii_bus = fec0_mii_bus;
1763 mii_cnt++;
1764 return 0;
1765 }
1766 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001767 }
1768
Bryan Wue6b043d2010-03-31 02:10:44 +00001769 fep->mii_timeout = 0;
1770
1771 /*
1772 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001773 *
1774 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1775 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1776 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1777 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001778 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001779 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001780 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1781 fep->phy_speed--;
1782 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001783 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1784
1785 fep->mii_bus = mdiobus_alloc();
1786 if (fep->mii_bus == NULL) {
1787 err = -ENOMEM;
1788 goto err_out;
1789 }
1790
1791 fep->mii_bus->name = "fec_enet_mii_bus";
1792 fep->mii_bus->read = fec_enet_mdio_read;
1793 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001794 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1795 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001796 fep->mii_bus->priv = fep;
1797 fep->mii_bus->parent = &pdev->dev;
1798
1799 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1800 if (!fep->mii_bus->irq) {
1801 err = -ENOMEM;
1802 goto err_out_free_mdiobus;
1803 }
1804
1805 for (i = 0; i < PHY_MAX_ADDR; i++)
1806 fep->mii_bus->irq[i] = PHY_POLL;
1807
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001808 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
1809 if (node) {
1810 err = of_mdiobus_register(fep->mii_bus, node);
1811 of_node_put(node);
1812 } else {
1813 err = mdiobus_register(fep->mii_bus);
1814 }
1815
1816 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00001817 goto err_out_free_mdio_irq;
1818
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001819 mii_cnt++;
1820
Shawn Guob5680e02011-01-05 21:13:13 +00001821 /* save fec0 mii_bus */
1822 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1823 fec0_mii_bus = fep->mii_bus;
1824
Bryan Wue6b043d2010-03-31 02:10:44 +00001825 return 0;
1826
Bryan Wue6b043d2010-03-31 02:10:44 +00001827err_out_free_mdio_irq:
1828 kfree(fep->mii_bus->irq);
1829err_out_free_mdiobus:
1830 mdiobus_free(fep->mii_bus);
1831err_out:
1832 return err;
1833}
1834
1835static void fec_enet_mii_remove(struct fec_enet_private *fep)
1836{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001837 if (--mii_cnt == 0) {
1838 mdiobus_unregister(fep->mii_bus);
1839 kfree(fep->mii_bus->irq);
1840 mdiobus_free(fep->mii_bus);
1841 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001842}
1843
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001844static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001845 struct ethtool_cmd *cmd)
1846{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001847 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001848 struct phy_device *phydev = fep->phy_dev;
1849
1850 if (!phydev)
1851 return -ENODEV;
1852
1853 return phy_ethtool_gset(phydev, cmd);
1854}
1855
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001856static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001857 struct ethtool_cmd *cmd)
1858{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001859 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001860 struct phy_device *phydev = fep->phy_dev;
1861
1862 if (!phydev)
1863 return -ENODEV;
1864
1865 return phy_ethtool_sset(phydev, cmd);
1866}
1867
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001868static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00001869 struct ethtool_drvinfo *info)
1870{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001871 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Jiri Pirko7826d432013-01-06 00:44:26 +00001873 strlcpy(info->driver, fep->pdev->dev.driver->name,
1874 sizeof(info->driver));
1875 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1876 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
Bryan Wue6b043d2010-03-31 02:10:44 +00001878
Frank Li5ebae4892013-01-06 16:25:07 +00001879static int fec_enet_get_ts_info(struct net_device *ndev,
1880 struct ethtool_ts_info *info)
1881{
1882 struct fec_enet_private *fep = netdev_priv(ndev);
1883
1884 if (fep->bufdesc_ex) {
1885
1886 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1887 SOF_TIMESTAMPING_RX_SOFTWARE |
1888 SOF_TIMESTAMPING_SOFTWARE |
1889 SOF_TIMESTAMPING_TX_HARDWARE |
1890 SOF_TIMESTAMPING_RX_HARDWARE |
1891 SOF_TIMESTAMPING_RAW_HARDWARE;
1892 if (fep->ptp_clock)
1893 info->phc_index = ptp_clock_index(fep->ptp_clock);
1894 else
1895 info->phc_index = -1;
1896
1897 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1898 (1 << HWTSTAMP_TX_ON);
1899
1900 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1901 (1 << HWTSTAMP_FILTER_ALL);
1902 return 0;
1903 } else {
1904 return ethtool_op_get_ts_info(ndev, info);
1905 }
1906}
1907
Guenter Roeckd1391932013-06-18 10:04:59 -07001908#if !defined(CONFIG_M5272)
1909
Frank Libaa70a52013-01-16 16:55:58 +00001910static void fec_enet_get_pauseparam(struct net_device *ndev,
1911 struct ethtool_pauseparam *pause)
1912{
1913 struct fec_enet_private *fep = netdev_priv(ndev);
1914
1915 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1916 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1917 pause->rx_pause = pause->tx_pause;
1918}
1919
1920static int fec_enet_set_pauseparam(struct net_device *ndev,
1921 struct ethtool_pauseparam *pause)
1922{
1923 struct fec_enet_private *fep = netdev_priv(ndev);
1924
Russell King0b146ca2014-07-08 00:22:59 +01001925 if (!fep->phy_dev)
1926 return -ENODEV;
1927
Frank Libaa70a52013-01-16 16:55:58 +00001928 if (pause->tx_pause != pause->rx_pause) {
1929 netdev_info(ndev,
1930 "hardware only support enable/disable both tx and rx");
1931 return -EINVAL;
1932 }
1933
1934 fep->pause_flag = 0;
1935
1936 /* tx pause must be same as rx pause */
1937 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1938 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1939
1940 if (pause->rx_pause || pause->autoneg) {
1941 fep->phy_dev->supported |= ADVERTISED_Pause;
1942 fep->phy_dev->advertising |= ADVERTISED_Pause;
1943 } else {
1944 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1945 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1946 }
1947
1948 if (pause->autoneg) {
1949 if (netif_running(ndev))
1950 fec_stop(ndev);
1951 phy_start_aneg(fep->phy_dev);
1952 }
Russell Kingdbc64a82014-07-08 12:40:12 +01001953 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001954 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001955 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001956 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001957 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001958 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001959 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001960 }
Frank Libaa70a52013-01-16 16:55:58 +00001961
1962 return 0;
1963}
1964
Chris Healy38ae92d2013-06-25 23:18:52 -07001965static const struct fec_stat {
1966 char name[ETH_GSTRING_LEN];
1967 u16 offset;
1968} fec_stats[] = {
1969 /* RMON TX */
1970 { "tx_dropped", RMON_T_DROP },
1971 { "tx_packets", RMON_T_PACKETS },
1972 { "tx_broadcast", RMON_T_BC_PKT },
1973 { "tx_multicast", RMON_T_MC_PKT },
1974 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1975 { "tx_undersize", RMON_T_UNDERSIZE },
1976 { "tx_oversize", RMON_T_OVERSIZE },
1977 { "tx_fragment", RMON_T_FRAG },
1978 { "tx_jabber", RMON_T_JAB },
1979 { "tx_collision", RMON_T_COL },
1980 { "tx_64byte", RMON_T_P64 },
1981 { "tx_65to127byte", RMON_T_P65TO127 },
1982 { "tx_128to255byte", RMON_T_P128TO255 },
1983 { "tx_256to511byte", RMON_T_P256TO511 },
1984 { "tx_512to1023byte", RMON_T_P512TO1023 },
1985 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
1986 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
1987 { "tx_octets", RMON_T_OCTETS },
1988
1989 /* IEEE TX */
1990 { "IEEE_tx_drop", IEEE_T_DROP },
1991 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
1992 { "IEEE_tx_1col", IEEE_T_1COL },
1993 { "IEEE_tx_mcol", IEEE_T_MCOL },
1994 { "IEEE_tx_def", IEEE_T_DEF },
1995 { "IEEE_tx_lcol", IEEE_T_LCOL },
1996 { "IEEE_tx_excol", IEEE_T_EXCOL },
1997 { "IEEE_tx_macerr", IEEE_T_MACERR },
1998 { "IEEE_tx_cserr", IEEE_T_CSERR },
1999 { "IEEE_tx_sqe", IEEE_T_SQE },
2000 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2001 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2002
2003 /* RMON RX */
2004 { "rx_packets", RMON_R_PACKETS },
2005 { "rx_broadcast", RMON_R_BC_PKT },
2006 { "rx_multicast", RMON_R_MC_PKT },
2007 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2008 { "rx_undersize", RMON_R_UNDERSIZE },
2009 { "rx_oversize", RMON_R_OVERSIZE },
2010 { "rx_fragment", RMON_R_FRAG },
2011 { "rx_jabber", RMON_R_JAB },
2012 { "rx_64byte", RMON_R_P64 },
2013 { "rx_65to127byte", RMON_R_P65TO127 },
2014 { "rx_128to255byte", RMON_R_P128TO255 },
2015 { "rx_256to511byte", RMON_R_P256TO511 },
2016 { "rx_512to1023byte", RMON_R_P512TO1023 },
2017 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2018 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2019 { "rx_octets", RMON_R_OCTETS },
2020
2021 /* IEEE RX */
2022 { "IEEE_rx_drop", IEEE_R_DROP },
2023 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2024 { "IEEE_rx_crc", IEEE_R_CRC },
2025 { "IEEE_rx_align", IEEE_R_ALIGN },
2026 { "IEEE_rx_macerr", IEEE_R_MACERR },
2027 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2028 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2029};
2030
2031static void fec_enet_get_ethtool_stats(struct net_device *dev,
2032 struct ethtool_stats *stats, u64 *data)
2033{
2034 struct fec_enet_private *fep = netdev_priv(dev);
2035 int i;
2036
2037 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2038 data[i] = readl(fep->hwp + fec_stats[i].offset);
2039}
2040
2041static void fec_enet_get_strings(struct net_device *netdev,
2042 u32 stringset, u8 *data)
2043{
2044 int i;
2045 switch (stringset) {
2046 case ETH_SS_STATS:
2047 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2048 memcpy(data + i * ETH_GSTRING_LEN,
2049 fec_stats[i].name, ETH_GSTRING_LEN);
2050 break;
2051 }
2052}
2053
2054static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2055{
2056 switch (sset) {
2057 case ETH_SS_STATS:
2058 return ARRAY_SIZE(fec_stats);
2059 default:
2060 return -EOPNOTSUPP;
2061 }
2062}
Guenter Roeckd1391932013-06-18 10:04:59 -07002063#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002064
Chris Healy32bc9b42013-06-17 07:25:06 -07002065static int fec_enet_nway_reset(struct net_device *dev)
2066{
2067 struct fec_enet_private *fep = netdev_priv(dev);
2068 struct phy_device *phydev = fep->phy_dev;
2069
2070 if (!phydev)
2071 return -ENODEV;
2072
2073 return genphy_restart_aneg(phydev);
2074}
2075
stephen hemminger9b07be42012-01-04 12:59:49 +00002076static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002077 .get_settings = fec_enet_get_settings,
2078 .set_settings = fec_enet_set_settings,
2079 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002080 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002081 .get_link = ethtool_op_get_link,
Chris Healy38ae92d2013-06-25 23:18:52 -07002082#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002083 .get_pauseparam = fec_enet_get_pauseparam,
2084 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002085 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002086 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002087 .get_sset_count = fec_enet_get_sset_count,
2088#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002089 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00002090};
2091
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002092static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002093{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002094 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002095 struct phy_device *phydev = fep->phy_dev;
2096
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002097 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002098 return -EINVAL;
2099
2100 if (!phydev)
2101 return -ENODEV;
2102
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002103 if (fep->bufdesc_ex) {
2104 if (cmd == SIOCSHWTSTAMP)
2105 return fec_ptp_set(ndev, rq);
2106 if (cmd == SIOCGHWTSTAMP)
2107 return fec_ptp_get(ndev, rq);
2108 }
Frank Liff43da82013-01-03 16:04:23 +00002109
Richard Cochran28b04112010-07-17 08:48:55 +00002110 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002113static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002114{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002115 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002116 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002117 struct sk_buff *skb;
2118 struct bufdesc *bdp;
2119
2120 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002121 for (i = 0; i < fep->rx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002122 skb = fep->rx_skbuff[i];
Russell King730ee362014-07-08 00:23:14 +01002123 fep->rx_skbuff[i] = NULL;
2124 if (skb) {
Uwe Kleine-Königd1ab1f52011-01-20 09:26:38 +01002125 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002126 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002127 dev_kfree_skb(skb);
Russell King730ee362014-07-08 00:23:14 +01002128 }
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002129 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002130 }
2131
2132 bdp = fep->tx_bd_base;
Russell King8b7c9ef2014-07-08 00:23:25 +01002133 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002134 kfree(fep->tx_bounce[i]);
Russell King8b7c9ef2014-07-08 00:23:25 +01002135 fep->tx_bounce[i] = NULL;
2136 skb = fep->tx_skbuff[i];
2137 fep->tx_skbuff[i] = NULL;
2138 dev_kfree_skb(skb);
2139 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002140}
2141
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002142static int fec_enet_alloc_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002143{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002144 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002145 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002146 struct sk_buff *skb;
2147 struct bufdesc *bdp;
2148
2149 bdp = fep->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002150 for (i = 0; i < fep->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002151 dma_addr_t addr;
2152
Fabio Estevamb72061a2012-02-07 08:27:31 +00002153 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002154 if (!skb)
2155 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002156
Russell King730ee362014-07-08 00:23:14 +01002157 addr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002158 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Russell King730ee362014-07-08 00:23:14 +01002159 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2160 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002161 if (net_ratelimit())
2162 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002163 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002164 }
Russell King730ee362014-07-08 00:23:14 +01002165
2166 fep->rx_skbuff[i] = skb;
2167 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002168 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002169
2170 if (fep->bufdesc_ex) {
2171 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2172 ebdp->cbd_esc = BD_ENET_RX_INT;
2173 }
2174
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002175 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002176 }
2177
2178 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002179 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002180 bdp->cbd_sc |= BD_SC_WRAP;
2181
2182 bdp = fep->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002183 for (i = 0; i < fep->tx_ring_size; i++) {
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002184 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
Russell Kingffdce2c2014-07-08 00:23:30 +01002185 if (!fep->tx_bounce[i])
2186 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002187
2188 bdp->cbd_sc = 0;
2189 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002190
Frank Liff43da82013-01-03 16:04:23 +00002191 if (fep->bufdesc_ex) {
2192 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002193 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002194 }
2195
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002196 bdp = fec_enet_get_nextdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002197 }
2198
2199 /* Set the last buffer to wrap. */
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002200 bdp = fec_enet_get_prevdesc(bdp, fep);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002201 bdp->cbd_sc |= BD_SC_WRAP;
2202
2203 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002204
2205 err_alloc:
2206 fec_enet_free_buffers(ndev);
2207 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002208}
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002211fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002213 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002214 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002216 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002217 ret = fec_enet_clk_enable(ndev, true);
2218 if (ret)
2219 return ret;
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 /* I should reset the ring buffers here, but I don't yet know
2222 * a simple way to do that.
2223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002225 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002226 if (ret)
2227 return ret;
2228
Bryan Wu418bd0d2010-05-28 03:40:39 -07002229 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002230 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002231 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002232 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002233 return ret;
2234 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002235
Russell Kingef833372014-07-08 12:40:43 +01002236 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002237 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002238 phy_start(fep->phy_dev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002239 netif_start_queue(ndev);
Sascha Hauer22f6b862009-04-15 01:32:18 +00002240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
2243static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002244fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002246 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Russell Kingd76cfae2014-07-08 00:23:04 +01002248 phy_stop(fep->phy_dev);
2249
Russell King31a6de32014-07-08 12:40:23 +01002250 if (netif_device_present(ndev)) {
2251 napi_disable(&fep->napi);
2252 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002253 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Russell King635cf172014-07-08 00:22:54 +01002256 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002257 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002258
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002259 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002260 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002261 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return 0;
2264}
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266/* Set or clear the multicast filter for this adaptor.
2267 * Skeleton taken from sunlance driver.
2268 * The CPM Ethernet implementation allows Multicast as well as individual
2269 * MAC address filtering. Some of the drivers check to make sure it is
2270 * a group multicast address, and discard those that are not. I guess I
2271 * will do the same for now, but just remove the test if you want
2272 * individual filtering as well (do the upper net layers want or support
2273 * this kind of feature?).
2274 */
2275
2276#define HASH_BITS 6 /* #bits in hash */
2277#define CRC32_POLY 0xEDB88320
2278
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002279static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002281 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002282 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002283 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 unsigned char hash;
2285
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002286 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002287 tmp = readl(fep->hwp + FEC_R_CNTRL);
2288 tmp |= 0x8;
2289 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002290 return;
2291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Sascha Hauer4e831832009-04-15 01:32:19 +00002293 tmp = readl(fep->hwp + FEC_R_CNTRL);
2294 tmp &= ~0x8;
2295 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002296
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002297 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002298 /* Catch all multicast addresses, so set the
2299 * filter to all 1's
2300 */
2301 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2302 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Sascha Hauer4e831832009-04-15 01:32:19 +00002304 return;
2305 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002306
Sascha Hauer4e831832009-04-15 01:32:19 +00002307 /* Clear filter and add the addresses in hash register
2308 */
2309 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2310 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002312 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002313 /* calculate crc32 value of mac address */
2314 crc = 0xffffffff;
2315
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002316 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002317 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002318 for (bit = 0; bit < 8; bit++, data >>= 1) {
2319 crc = (crc >> 1) ^
2320 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002323
2324 /* only upper 6 bits (HASH_BITS) are used
2325 * which point to specific bit in he hash registers
2326 */
2327 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2328
2329 if (hash > 31) {
2330 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2331 tmp |= 1 << (hash - 32);
2332 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2333 } else {
2334 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2335 tmp |= 1 << hash;
2336 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339}
2340
Sascha Hauer22f6b862009-04-15 01:32:18 +00002341/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002342static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002343fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002345 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002346 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Lucas Stach44934fa2014-03-30 21:32:08 +02002348 if (addr) {
2349 if (!is_valid_ether_addr(addr->sa_data))
2350 return -EADDRNOTAVAIL;
2351 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2352 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002353
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002354 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2355 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002356 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002357 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002358 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002362#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002363/**
2364 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002365 * @dev: The FEC network adapter
2366 *
2367 * Polled functionality used by netconsole and others in non interrupt mode
2368 *
2369 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002370static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002371{
2372 int i;
2373 struct fec_enet_private *fep = netdev_priv(dev);
2374
2375 for (i = 0; i < FEC_IRQ_NUM; i++) {
2376 if (fep->irq[i] > 0) {
2377 disable_irq(fep->irq[i]);
2378 fec_enet_interrupt(fep->irq[i], dev);
2379 enable_irq(fep->irq[i]);
2380 }
2381 }
2382}
2383#endif
2384
Russell King8506fa12014-07-08 12:40:33 +01002385#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2386
Jim Baxter4c09eed2013-04-19 08:10:49 +00002387static int fec_set_features(struct net_device *netdev,
2388 netdev_features_t features)
2389{
2390 struct fec_enet_private *fep = netdev_priv(netdev);
2391 netdev_features_t changed = features ^ netdev->features;
2392
Russell King8506fa12014-07-08 12:40:33 +01002393 /* Quiesce the device if necessary */
2394 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2395 napi_disable(&fep->napi);
2396 netif_tx_lock_bh(netdev);
2397 fec_stop(netdev);
2398 }
2399
Jim Baxter4c09eed2013-04-19 08:10:49 +00002400 netdev->features = features;
2401
2402 /* Receive checksum has been changed */
2403 if (changed & NETIF_F_RXCSUM) {
2404 if (features & NETIF_F_RXCSUM)
2405 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2406 else
2407 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002408 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002409
Russell King8506fa12014-07-08 12:40:33 +01002410 /* Resume the device after updates */
2411 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002412 fec_restart(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002413 netif_wake_queue(netdev);
2414 netif_tx_unlock_bh(netdev);
2415 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002416 }
2417
2418 return 0;
2419}
2420
Sascha Hauer009fda82009-04-15 01:32:23 +00002421static const struct net_device_ops fec_netdev_ops = {
2422 .ndo_open = fec_enet_open,
2423 .ndo_stop = fec_enet_close,
2424 .ndo_start_xmit = fec_enet_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002425 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002426 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002427 .ndo_validate_addr = eth_validate_addr,
2428 .ndo_tx_timeout = fec_timeout,
2429 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002430 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002431#ifdef CONFIG_NET_POLL_CONTROLLER
2432 .ndo_poll_controller = fec_poll_controller,
2433#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002434 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002435};
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /*
2438 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002439 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002441static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002443 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002444 const struct platform_device_id *id_entry =
2445 platform_get_device_id(fep->pdev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002446 struct bufdesc *cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002447 int bd_size;
2448
2449 /* init the tx & rx ring size */
2450 fep->tx_ring_size = TX_RING_SIZE;
2451 fep->rx_ring_size = RX_RING_SIZE;
2452
Nimrod Andy79f33912014-06-12 08:16:23 +08002453 fep->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2454 fep->tx_wake_threshold = (fep->tx_ring_size - fep->tx_stop_threshold) / 2;
2455
Nimrod Andy55d02182014-06-12 08:16:21 +08002456 if (fep->bufdesc_ex)
2457 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2458 else
2459 fep->bufdesc_size = sizeof(struct bufdesc);
2460 bd_size = (fep->tx_ring_size + fep->rx_ring_size) *
2461 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002463 /* Allocate memory for buffer descriptors. */
Nimrod Andy55d02182014-06-12 08:16:21 +08002464 cbd_base = dma_alloc_coherent(NULL, bd_size, &fep->bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002465 GFP_KERNEL);
2466 if (!cbd_base)
Greg Ungerer562d2f82005-11-07 14:09:50 +10002467 return -ENOMEM;
Greg Ungerer562d2f82005-11-07 14:09:50 +10002468
Nimrod Andy79f33912014-06-12 08:16:23 +08002469 fep->tso_hdrs = dma_alloc_coherent(NULL, fep->tx_ring_size * TSO_HEADER_SIZE,
2470 &fep->tso_hdrs_dma, GFP_KERNEL);
2471 if (!fep->tso_hdrs) {
2472 dma_free_coherent(NULL, bd_size, cbd_base, fep->bd_dma);
2473 return -ENOMEM;
2474 }
2475
Frank Li14109a52013-03-26 16:12:03 +00002476 memset(cbd_base, 0, PAGE_SIZE);
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002477
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002478 fep->netdev = ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Shawn Guo49da97d2011-01-05 21:13:11 +00002480 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002481 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002482 /* make sure MAC we just acquired is programmed into the hw */
2483 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002485 /* Set receive and transmit descriptor base. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 fep->rx_bd_base = cbd_base;
Nimrod Andy55d02182014-06-12 08:16:21 +08002487 if (fep->bufdesc_ex)
Frank Liff43da82013-01-03 16:04:23 +00002488 fep->tx_bd_base = (struct bufdesc *)
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002489 (((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
Nimrod Andy55d02182014-06-12 08:16:21 +08002490 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08002491 fep->tx_bd_base = cbd_base + fep->rx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Sascha Hauer22f6b862009-04-15 01:32:18 +00002493 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002494 ndev->watchdog_timeo = TX_TIMEOUT;
2495 ndev->netdev_ops = &fec_netdev_ops;
2496 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002497
Frank Lidc975382013-01-28 18:31:42 +00002498 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002499 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002500
Nimrod Andy09d1e542014-06-12 08:16:20 +08002501 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002502 /* enable hw VLAN support */
2503 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002504
Shawn Guo48496252013-05-08 21:08:22 +00002505 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002506 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2507
Shawn Guo48496252013-05-08 21:08:22 +00002508 /* enable hw accelerator */
2509 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002510 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002511 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2512 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002513
Nimrod Andy09d1e542014-06-12 08:16:20 +08002514 ndev->hw_features = ndev->features;
2515
Russell Kingef833372014-07-08 12:40:43 +01002516 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return 0;
2519}
2520
Shawn Guoca2cc332011-06-25 02:04:35 +08002521#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002522static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002523{
2524 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002525 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002526 struct device_node *np = pdev->dev.of_node;
2527
2528 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002529 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002530
Shawn Guoa3caad02012-06-27 03:45:24 +00002531 of_property_read_u32(np, "phy-reset-duration", &msec);
2532 /* A sane reset duration should not be longer than 1s */
2533 if (msec > 1000)
2534 msec = 1;
2535
Shawn Guoca2cc332011-06-25 02:04:35 +08002536 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002537 if (!gpio_is_valid(phy_reset))
2538 return;
2539
Shawn Guo119fc002012-06-27 03:45:22 +00002540 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2541 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002542 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002543 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002544 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002545 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002546 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08002547 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002548}
2549#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002550static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002551{
2552 /*
2553 * In case of platform probe, the reset has been done
2554 * by machine code.
2555 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002556}
2557#endif /* CONFIG_OF */
2558
Bill Pemberton33897cc2012-12-03 09:23:58 -05002559static int
Sascha Haueread73182009-01-28 23:03:11 +00002560fec_probe(struct platform_device *pdev)
2561{
2562 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002563 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002564 struct net_device *ndev;
2565 int i, irq, ret = 0;
2566 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002567 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002568 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002569 struct device_node *np = pdev->dev.of_node, *phy_node;
Shawn Guoca2cc332011-06-25 02:04:35 +08002570
2571 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2572 if (of_id)
2573 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002574
Sascha Haueread73182009-01-28 23:03:11 +00002575 /* Init network device */
2576 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
Fabio Estevam83e519b2013-03-11 07:32:55 +00002577 if (!ndev)
2578 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002579
2580 SET_NETDEV_DEV(ndev, &pdev->dev);
2581
2582 /* setup board info structure */
2583 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002584
Guenter Roeckd1391932013-06-18 10:04:59 -07002585#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002586 /* default enable pause frame auto negotiation */
2587 if (pdev->id_entry &&
2588 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2589 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002590#endif
Frank Libaa70a52013-01-16 16:55:58 +00002591
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002592 /* Select default pin state */
2593 pinctrl_pm_select_default_state(&pdev->dev);
2594
Fabio Estevam399db752013-07-21 13:25:03 -03002595 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302596 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2597 if (IS_ERR(fep->hwp)) {
2598 ret = PTR_ERR(fep->hwp);
2599 goto failed_ioremap;
2600 }
2601
Bryan Wue6b043d2010-03-31 02:10:44 +00002602 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002603 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002604
Frank Liff43da82013-01-03 16:04:23 +00002605 fep->bufdesc_ex = 0;
2606
Sascha Haueread73182009-01-28 23:03:11 +00002607 platform_set_drvdata(pdev, ndev);
2608
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002609 phy_node = of_parse_phandle(np, "phy-handle", 0);
2610 if (!phy_node && of_phy_is_fixed_link(np)) {
2611 ret = of_phy_register_fixed_link(np);
2612 if (ret < 0) {
2613 dev_err(&pdev->dev,
2614 "broken fixed-link specification\n");
2615 goto failed_phy;
2616 }
2617 phy_node = of_node_get(np);
2618 }
2619 fep->phy_node = phy_node;
2620
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002621 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002622 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002623 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002624 if (pdata)
2625 fep->phy_interface = pdata->phy;
2626 else
2627 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2628 } else {
2629 fep->phy_interface = ret;
2630 }
2631
Fabio Estevame2f8d552013-02-22 06:40:45 +00002632 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2633 if (IS_ERR(fep->clk_ipg)) {
2634 ret = PTR_ERR(fep->clk_ipg);
2635 goto failed_clk;
2636 }
2637
2638 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2639 if (IS_ERR(fep->clk_ahb)) {
2640 ret = PTR_ERR(fep->clk_ahb);
2641 goto failed_clk;
2642 }
2643
Linus Torvalds38f56f32013-05-07 11:06:17 -07002644 /* enet_out is optional, depends on board */
2645 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2646 if (IS_ERR(fep->clk_enet_out))
2647 fep->clk_enet_out = NULL;
2648
Nimrod Andy91c0d982014-08-21 17:09:38 +08002649 fep->ptp_clk_on = false;
2650 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08002651
2652 /* clk_ref is optional, depends on board */
2653 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
2654 if (IS_ERR(fep->clk_ref))
2655 fep->clk_ref = NULL;
2656
Fabio Estevame2f8d552013-02-22 06:40:45 +00002657 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002658 fep->bufdesc_ex =
2659 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002660 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07002661 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002662 fep->bufdesc_ex = 0;
2663 }
2664
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002665 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002666 if (ret)
2667 goto failed_clk;
2668
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00002669 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2670 if (!IS_ERR(fep->reg_phy)) {
2671 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00002672 if (ret) {
2673 dev_err(&pdev->dev,
2674 "Failed to enable phy regulator: %d\n", ret);
2675 goto failed_regulator;
2676 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002677 } else {
2678 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00002679 }
2680
2681 fec_reset_phy(pdev);
2682
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002683 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00002684 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00002685
2686 ret = fec_enet_init(ndev);
2687 if (ret)
2688 goto failed_init;
2689
Xiao Jiangc7c83d12011-09-29 02:15:56 +00002690 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00002691 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00002692 if (irq < 0) {
2693 if (i)
2694 break;
2695 ret = irq;
2696 goto failed_irq;
2697 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002698 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02002699 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03002700 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00002701 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00002702 }
2703
Bryan Wue6b043d2010-03-31 02:10:44 +00002704 ret = fec_enet_mii_init(pdev);
2705 if (ret)
2706 goto failed_mii_init;
2707
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002708 /* Carrier starts down, phylib will bring it up */
2709 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002710 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002711 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00002712
Sascha Haueread73182009-01-28 23:03:11 +00002713 ret = register_netdev(ndev);
2714 if (ret)
2715 goto failed_register;
2716
Fabio Estevameb1d0642013-04-13 07:25:36 +00002717 if (fep->bufdesc_ex && fep->ptp_clock)
2718 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2719
Russell King36cdc742014-07-08 13:01:44 +01002720 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00002721 return 0;
2722
2723failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00002724 fec_enet_mii_remove(fep);
2725failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002726failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00002727failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002728 if (fep->reg_phy)
2729 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00002730failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002731 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00002732failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002733failed_phy:
2734 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00002735failed_ioremap:
2736 free_netdev(ndev);
2737
2738 return ret;
2739}
2740
Bill Pemberton33897cc2012-12-03 09:23:58 -05002741static int
Sascha Haueread73182009-01-28 23:03:11 +00002742fec_drv_remove(struct platform_device *pdev)
2743{
2744 struct net_device *ndev = platform_get_drvdata(pdev);
2745 struct fec_enet_private *fep = netdev_priv(ndev);
2746
Nimrod Andy91c0d982014-08-21 17:09:38 +08002747 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01002748 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002749 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002750 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00002751 if (fep->reg_phy)
2752 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00002753 if (fep->ptp_clock)
2754 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002755 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002756 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00002757 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01002758
Sascha Haueread73182009-01-28 23:03:11 +00002759 return 0;
2760}
2761
Fabio Estevamdd66d382014-07-24 18:24:01 -03002762static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002763{
Eric Benard87cad5c2010-06-18 04:19:54 +00002764 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002765 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002766
Russell Kingda1774e2014-07-08 12:39:57 +01002767 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002768 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01002769 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01002770 napi_disable(&fep->napi);
2771 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002772 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002773 netif_tx_unlock_bh(ndev);
2774 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002775 }
Russell Kingda1774e2014-07-08 12:39:57 +01002776 rtnl_unlock();
2777
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002778 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002779 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002780
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002781 if (fep->reg_phy)
2782 regulator_disable(fep->reg_phy);
2783
Sascha Haueread73182009-01-28 23:03:11 +00002784 return 0;
2785}
2786
Fabio Estevamdd66d382014-07-24 18:24:01 -03002787static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00002788{
Eric Benard87cad5c2010-06-18 04:19:54 +00002789 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002790 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00002791 int ret;
2792
2793 if (fep->reg_phy) {
2794 ret = regulator_enable(fep->reg_phy);
2795 if (ret)
2796 return ret;
2797 }
Sascha Haueread73182009-01-28 23:03:11 +00002798
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002799 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002800 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03002801 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002802 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002803
Russell Kingda1774e2014-07-08 12:39:57 +01002804 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002805 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01002806 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002807 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002808 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002809 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002810 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01002811 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00002812 }
Russell Kingda1774e2014-07-08 12:39:57 +01002813 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01002814
Sascha Haueread73182009-01-28 23:03:11 +00002815 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03002816
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002817failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03002818 if (fep->reg_phy)
2819 regulator_disable(fep->reg_phy);
2820 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00002821}
2822
Fabio Estevambf7bfd72013-04-16 08:17:46 +00002823static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00002824
Sascha Haueread73182009-01-28 23:03:11 +00002825static struct platform_driver fec_driver = {
2826 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00002827 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00002828 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00002829 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08002830 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00002831 },
Shawn Guob5680e02011-01-05 21:13:13 +00002832 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00002833 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05002834 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00002835};
2836
Fabio Estevamaaca2372012-01-23 16:44:50 +00002837module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03002839MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840MODULE_LICENSE("GPL");