blob: 5f8e997defadd942321916a56bb62d9e46b80f94 [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
Fugang Duan4d494cd2014-09-13 05:00:48 +080075#define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
76
Frank Libaa70a52013-01-16 16:55:58 +000077/* Pause frame feild and FIFO threshold */
78#define FEC_ENET_FCE (1 << 5)
79#define FEC_ENET_RSEM_V 0x84
80#define FEC_ENET_RSFL_V 16
81#define FEC_ENET_RAEM_V 0x8
82#define FEC_ENET_RAFL_V 0x8
83#define FEC_ENET_OPD_V 0xFFF0
84
Shawn Guob5680e02011-01-05 21:13:13 +000085/* Controller is ENET-MAC */
86#define FEC_QUIRK_ENET_MAC (1 << 0)
87/* Controller needs driver to swap frame */
88#define FEC_QUIRK_SWAP_FRAME (1 << 1)
Shawn Guo0ca1e292011-07-01 18:11:22 +080089/* Controller uses gasket */
90#define FEC_QUIRK_USE_GASKET (1 << 2)
Shawn Guo230dec62011-09-23 02:12:48 +000091/* Controller has GBIT support */
92#define FEC_QUIRK_HAS_GBIT (1 << 3)
Frank Liff43da82013-01-03 16:04:23 +000093/* Controller has extend desc buffer */
94#define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
Shawn Guo48496252013-05-08 21:08:22 +000095/* Controller has hardware checksum support */
96#define FEC_QUIRK_HAS_CSUM (1 << 5)
Jim Baxtercdffcf12013-07-02 22:52:56 +010097/* Controller has hardware vlan support */
98#define FEC_QUIRK_HAS_VLAN (1 << 6)
Frank Li03191652013-07-25 14:05:53 +080099/* ENET IP errata ERR006358
100 *
101 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
102 * detected as not set during a prior frame transmission, then the
103 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
104 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
Frank Li03191652013-07-25 14:05:53 +0800105 * frames not being transmitted until there is a 0-to-1 transition on
106 * ENET_TDAR[TDAR].
107 */
108#define FEC_QUIRK_ERR006358 (1 << 7)
Fugang Duan95a77472014-09-13 05:00:47 +0800109/* ENET IP hw AVB
110 *
111 * i.MX6SX ENET IP add Audio Video Bridging (AVB) feature support.
112 * - Two class indicators on receive with configurable priority
113 * - Two class indicators and line speed timer on transmit allowing
114 * implementation class credit based shapers externally
115 * - Additional DMA registers provisioned to allow managing up to 3
116 * independent rings
117 */
118#define FEC_QUIRK_HAS_AVB (1 << 8)
Shawn Guob5680e02011-01-05 21:13:13 +0000119
120static struct platform_device_id fec_devtype[] = {
121 {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800122 /* keep it for coldfire */
Shawn Guob5680e02011-01-05 21:13:13 +0000123 .name = DRIVER_NAME,
124 .driver_data = 0,
125 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800126 .name = "imx25-fec",
127 .driver_data = FEC_QUIRK_USE_GASKET,
128 }, {
129 .name = "imx27-fec",
130 .driver_data = 0,
131 }, {
Shawn Guob5680e02011-01-05 21:13:13 +0000132 .name = "imx28-fec",
133 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
Shawn Guo0ca1e292011-07-01 18:11:22 +0800134 }, {
Shawn Guo230dec62011-09-23 02:12:48 +0000135 .name = "imx6q-fec",
Frank Liff43da82013-01-03 16:04:23 +0000136 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
Jim Baxtercdffcf12013-07-02 22:52:56 +0100137 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
Frank Li03191652013-07-25 14:05:53 +0800138 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
Shawn Guo230dec62011-09-23 02:12:48 +0000139 }, {
Shawn Guo36803542013-05-19 04:38:46 +0000140 .name = "mvf600-fec",
Jingchang Luca7c4a42013-04-11 21:12:45 +0000141 .driver_data = FEC_QUIRK_ENET_MAC,
142 }, {
Fugang Duan95a77472014-09-13 05:00:47 +0800143 .name = "imx6sx-fec",
144 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
145 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
146 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
147 FEC_QUIRK_HAS_AVB,
148 }, {
Shawn Guo0ca1e292011-07-01 18:11:22 +0800149 /* sentinel */
150 }
Shawn Guob5680e02011-01-05 21:13:13 +0000151};
Shawn Guo0ca1e292011-07-01 18:11:22 +0800152MODULE_DEVICE_TABLE(platform, fec_devtype);
Shawn Guob5680e02011-01-05 21:13:13 +0000153
Shawn Guoca2cc332011-06-25 02:04:35 +0800154enum imx_fec_type {
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000155 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
Shawn Guoca2cc332011-06-25 02:04:35 +0800156 IMX27_FEC, /* runs on i.mx27/35/51 */
157 IMX28_FEC,
Shawn Guo230dec62011-09-23 02:12:48 +0000158 IMX6Q_FEC,
Shawn Guo36803542013-05-19 04:38:46 +0000159 MVF600_FEC,
Shawn Guoca2cc332011-06-25 02:04:35 +0800160};
161
162static const struct of_device_id fec_dt_ids[] = {
163 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
164 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
165 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
Shawn Guo230dec62011-09-23 02:12:48 +0000166 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
Shawn Guo36803542013-05-19 04:38:46 +0000167 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
Shawn Guoca2cc332011-06-25 02:04:35 +0800168 { /* sentinel */ }
169};
170MODULE_DEVICE_TABLE(of, fec_dt_ids);
171
Shawn Guo49da97d2011-01-05 21:13:11 +0000172static unsigned char macaddr[ETH_ALEN];
173module_param_array(macaddr, byte, NULL, 0);
174MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
175
Greg Ungerer87f4abb2008-06-06 15:55:36 +1000176#if defined(CONFIG_M5272)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
178 * Some hardware gets it MAC address out of local flash memory.
179 * if this is non-zero then assume it is the address to get MAC from.
180 */
181#if defined(CONFIG_NETtel)
182#define FEC_FLASHMAC 0xf0006006
183#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
184#define FEC_FLASHMAC 0xf0006000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#elif defined(CONFIG_CANCam)
186#define FEC_FLASHMAC 0xf0020000
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +1000187#elif defined (CONFIG_M5272C3)
188#define FEC_FLASHMAC (0xffe04000 + 4)
189#elif defined(CONFIG_MOD5272)
Lothar Waßmanna7dd3212011-12-07 21:59:25 +0000190#define FEC_FLASHMAC 0xffc0406b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#else
192#define FEC_FLASHMAC 0
193#endif
Greg Ungerer43be6362009-02-26 22:42:51 -0800194#endif /* CONFIG_M5272 */
Sascha Haueread73182009-01-28 23:03:11 +0000195
Sascha Hauer22f6b862009-04-15 01:32:18 +0000196/* Interrupt events/masks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
198#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
199#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
200#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
201#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
202#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
203#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
204#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
205#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
206#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
207
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000208#define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
Frank Lidc975382013-01-28 18:31:42 +0000209#define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
Wolfram Sang4bee1f92010-07-21 02:51:13 +0000210
Jim Baxtercdffcf12013-07-02 22:52:56 +0100211/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100213#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100215#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jim Baxter4c09eed2013-04-19 08:10:49 +0000217/* FEC receive acceleration */
218#define FEC_RACC_IPDIS (1 << 1)
219#define FEC_RACC_PRODIS (1 << 2)
220#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
Matt Waddel6b265292006-06-27 13:10:56 +1000223 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * size bits. Other FEC hardware does not, so we need to take that into
225 * account when setting it.
226 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000227#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100228 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
230#else
231#define OPT_FRAME_SIZE 0
232#endif
233
Bryan Wue6b043d2010-03-31 02:10:44 +0000234/* FEC MII MMFR bits definition */
235#define FEC_MMFR_ST (1 << 30)
236#define FEC_MMFR_OP_READ (2 << 28)
237#define FEC_MMFR_OP_WRITE (1 << 28)
238#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
239#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
240#define FEC_MMFR_TA (2 << 16)
241#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500243#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Sascha Hauer22f6b862009-04-15 01:32:18 +0000245/* Transmitter timeout */
246#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Frank Libaa70a52013-01-16 16:55:58 +0000248#define FEC_PAUSE_FLAG_AUTONEG 0x1
249#define FEC_PAUSE_FLAG_ENABLE 0x2
250
Nimrod Andy79f33912014-06-12 08:16:23 +0800251#define TSO_HEADER_SIZE 128
252/* Max number of allowed TCP segments for software TSO */
253#define FEC_MAX_TSO_SEGS 100
254#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
255
256#define IS_TSO_HEADER(txq, addr) \
257 ((addr >= txq->tso_hdrs_dma) && \
258 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
259
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000260static int mii_cnt;
261
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800262static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800263struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
264 struct fec_enet_private *fep,
265 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000266{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800267 struct bufdesc *new_bd = bdp + 1;
268 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800269 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
270 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800271 struct bufdesc_ex *ex_base;
272 struct bufdesc *base;
273 int ring_size;
274
Fugang Duan4d494cd2014-09-13 05:00:48 +0800275 if (bdp >= txq->tx_bd_base) {
276 base = txq->tx_bd_base;
277 ring_size = txq->tx_ring_size;
278 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800279 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800280 base = rxq->rx_bd_base;
281 ring_size = rxq->rx_ring_size;
282 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800283 }
284
285 if (fep->bufdesc_ex)
286 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
287 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000288 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800289 return (new_bd >= (base + ring_size)) ?
290 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000291}
292
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800293static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800294struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
295 struct fec_enet_private *fep,
296 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000297{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800298 struct bufdesc *new_bd = bdp - 1;
299 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800300 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
301 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800302 struct bufdesc_ex *ex_base;
303 struct bufdesc *base;
304 int ring_size;
305
Fugang Duan4d494cd2014-09-13 05:00:48 +0800306 if (bdp >= txq->tx_bd_base) {
307 base = txq->tx_bd_base;
308 ring_size = txq->tx_ring_size;
309 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800310 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800311 base = rxq->rx_bd_base;
312 ring_size = rxq->rx_ring_size;
313 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800314 }
315
316 if (fep->bufdesc_ex)
317 return (struct bufdesc *)((ex_new_bd < ex_base) ?
318 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000319 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800320 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000321}
322
Nimrod Andy61a44272014-06-12 08:16:18 +0800323static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
324 struct fec_enet_private *fep)
325{
326 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
327}
328
Fugang Duan4d494cd2014-09-13 05:00:48 +0800329static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
330 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800331{
332 int entries;
333
Fugang Duan4d494cd2014-09-13 05:00:48 +0800334 entries = ((const char *)txq->dirty_tx -
335 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800336
Fugang Duan4d494cd2014-09-13 05:00:48 +0800337 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800338}
339
Shawn Guob5680e02011-01-05 21:13:13 +0000340static void *swap_buffer(void *bufaddr, int len)
341{
342 int i;
343 unsigned int *buf = bufaddr;
344
Fabio Estevamffed61e2013-05-21 05:44:26 +0000345 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000346 *buf = cpu_to_be32(*buf);
347
348 return bufaddr;
349}
350
Russell King344756f2014-07-08 13:01:59 +0100351static void fec_dump(struct net_device *ndev)
352{
353 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800354 struct bufdesc *bdp;
355 struct fec_enet_priv_tx_q *txq;
356 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100357
358 netdev_info(ndev, "TX ring dump\n");
359 pr_info("Nr SC addr len SKB\n");
360
Fugang Duan4d494cd2014-09-13 05:00:48 +0800361 txq = fep->tx_queue[0];
362 bdp = txq->tx_bd_base;
363
Russell King344756f2014-07-08 13:01:59 +0100364 do {
365 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
366 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800367 bdp == txq->cur_tx ? 'S' : ' ',
368 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100369 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800370 txq->tx_skbuff[index]);
371 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100372 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800373 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100374}
375
Fugang Duan62a02c92014-06-18 08:33:52 +0800376static inline bool is_ipv4_pkt(struct sk_buff *skb)
377{
378 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
379}
380
Jim Baxter4c09eed2013-04-19 08:10:49 +0000381static int
382fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
383{
384 /* Only run for packets requiring a checksum. */
385 if (skb->ip_summed != CHECKSUM_PARTIAL)
386 return 0;
387
388 if (unlikely(skb_cow_head(skb, 0)))
389 return -1;
390
Fugang Duan62a02c92014-06-18 08:33:52 +0800391 if (is_ipv4_pkt(skb))
392 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000393 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
394
395 return 0;
396}
397
Nimrod Andy6e909282014-06-12 08:16:22 +0800398static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800399fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
400 struct sk_buff *skb,
401 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800402{
403 struct fec_enet_private *fep = netdev_priv(ndev);
404 const struct platform_device_id *id_entry =
405 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800406 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800407 struct bufdesc_ex *ebdp;
408 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800409 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800410 int frag, frag_len;
411 unsigned short status;
412 unsigned int estatus = 0;
413 skb_frag_t *this_frag;
414 unsigned int index;
415 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100416 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800417 int i;
418
419 for (frag = 0; frag < nr_frags; frag++) {
420 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800421 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800422 ebdp = (struct bufdesc_ex *)bdp;
423
424 status = bdp->cbd_sc;
425 status &= ~BD_ENET_TX_STATS;
426 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
427 frag_len = skb_shinfo(skb)->frags[frag].size;
428
429 /* Handle the last BD specially */
430 if (frag == nr_frags - 1) {
431 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
432 if (fep->bufdesc_ex) {
433 estatus |= BD_ENET_TX_INT;
434 if (unlikely(skb_shinfo(skb)->tx_flags &
435 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
436 estatus |= BD_ENET_TX_TS;
437 }
438 }
439
440 if (fep->bufdesc_ex) {
441 if (skb->ip_summed == CHECKSUM_PARTIAL)
442 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
443 ebdp->cbd_bdu = 0;
444 ebdp->cbd_esc = estatus;
445 }
446
447 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
448
Fugang Duan4d494cd2014-09-13 05:00:48 +0800449 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800450 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
451 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800452 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
453 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800454
455 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
456 swap_buffer(bufaddr, frag_len);
457 }
458
Russell Kingd6bf3142014-07-08 00:23:19 +0100459 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
460 DMA_TO_DEVICE);
461 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800462 dev_kfree_skb_any(skb);
463 if (net_ratelimit())
464 netdev_err(ndev, "Tx DMA memory map failed\n");
465 goto dma_mapping_error;
466 }
467
Russell Kingd6bf3142014-07-08 00:23:19 +0100468 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800469 bdp->cbd_datlen = frag_len;
470 bdp->cbd_sc = status;
471 }
472
Fugang Duan4d494cd2014-09-13 05:00:48 +0800473 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800474
475 return 0;
476
477dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800478 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800479 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800480 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800481 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
482 bdp->cbd_datlen, DMA_TO_DEVICE);
483 }
484 return NETDEV_TX_OK;
485}
486
Fugang Duan4d494cd2014-09-13 05:00:48 +0800487static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
488 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800489{
490 struct fec_enet_private *fep = netdev_priv(ndev);
491 const struct platform_device_id *id_entry =
492 platform_get_device_id(fep->pdev);
493 int nr_frags = skb_shinfo(skb)->nr_frags;
494 struct bufdesc *bdp, *last_bdp;
495 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100496 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800497 unsigned short status;
498 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800499 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800500 unsigned int estatus = 0;
501 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800502 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800503 int ret;
504
Fugang Duan4d494cd2014-09-13 05:00:48 +0800505 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800506 if (entries_free < MAX_SKB_FRAGS + 1) {
507 dev_kfree_skb_any(skb);
508 if (net_ratelimit())
509 netdev_err(ndev, "NOT enough BD for SG!\n");
510 return NETDEV_TX_OK;
511 }
512
Nimrod Andy6e909282014-06-12 08:16:22 +0800513 /* Protocol checksum off-load for TCP and UDP. */
514 if (fec_enet_clear_csum(skb, ndev)) {
515 dev_kfree_skb_any(skb);
516 return NETDEV_TX_OK;
517 }
518
519 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800520 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800521 status = bdp->cbd_sc;
522 status &= ~BD_ENET_TX_STATS;
523
524 /* Set buffer length and buffer pointer */
525 bufaddr = skb->data;
526 buflen = skb_headlen(skb);
527
Fugang Duan4d494cd2014-09-13 05:00:48 +0800528 queue = skb_get_queue_mapping(skb);
529 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800530 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
531 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800532 memcpy(txq->tx_bounce[index], skb->data, buflen);
533 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800534
535 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
536 swap_buffer(bufaddr, buflen);
537 }
538
Russell Kingd6bf3142014-07-08 00:23:19 +0100539 /* Push the data cache so the CPM does not get stale memory data. */
540 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
541 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800542 dev_kfree_skb_any(skb);
543 if (net_ratelimit())
544 netdev_err(ndev, "Tx DMA memory map failed\n");
545 return NETDEV_TX_OK;
546 }
547
548 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800549 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800550 if (ret)
551 return ret;
552 } else {
553 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
554 if (fep->bufdesc_ex) {
555 estatus = BD_ENET_TX_INT;
556 if (unlikely(skb_shinfo(skb)->tx_flags &
557 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
558 estatus |= BD_ENET_TX_TS;
559 }
560 }
561
562 if (fep->bufdesc_ex) {
563
564 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
565
566 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
567 fep->hwts_tx_en))
568 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
569
570 if (skb->ip_summed == CHECKSUM_PARTIAL)
571 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
572
573 ebdp->cbd_bdu = 0;
574 ebdp->cbd_esc = estatus;
575 }
576
Fugang Duan4d494cd2014-09-13 05:00:48 +0800577 last_bdp = txq->cur_tx;
578 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800579 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800580 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800581
582 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100583 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800584
585 /* Send it on its way. Tell FEC it's ready, interrupt when done,
586 * it's the last BD of the frame, and to put the CRC on the end.
587 */
588 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
589 bdp->cbd_sc = status;
590
Sascha Hauer22f6b862009-04-15 01:32:18 +0000591 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800592 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800594 skb_tx_timestamp(skb);
595
Fugang Duan4d494cd2014-09-13 05:00:48 +0800596 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Frank Lide5fb0a2013-03-03 17:34:25 +0000598 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800599 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000600
Nimrod Andy6e909282014-06-12 08:16:22 +0800601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Nimrod Andy79f33912014-06-12 08:16:23 +0800604static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800605fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
606 struct net_device *ndev,
607 struct bufdesc *bdp, int index, char *data,
608 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800609{
610 struct fec_enet_private *fep = netdev_priv(ndev);
611 const struct platform_device_id *id_entry =
612 platform_get_device_id(fep->pdev);
613 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
614 unsigned short status;
615 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100616 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800617
618 status = bdp->cbd_sc;
619 status &= ~BD_ENET_TX_STATS;
620
621 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800622
623 if (((unsigned long) data) & FEC_ALIGNMENT ||
624 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800625 memcpy(txq->tx_bounce[index], data, size);
626 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800627
628 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
629 swap_buffer(data, size);
630 }
631
Russell Kingd6bf3142014-07-08 00:23:19 +0100632 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
633 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800634 dev_kfree_skb_any(skb);
635 if (net_ratelimit())
636 netdev_err(ndev, "Tx DMA memory map failed\n");
637 return NETDEV_TX_BUSY;
638 }
639
Russell Kingd6bf3142014-07-08 00:23:19 +0100640 bdp->cbd_datlen = size;
641 bdp->cbd_bufaddr = addr;
642
Nimrod Andy79f33912014-06-12 08:16:23 +0800643 if (fep->bufdesc_ex) {
644 if (skb->ip_summed == CHECKSUM_PARTIAL)
645 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
646 ebdp->cbd_bdu = 0;
647 ebdp->cbd_esc = estatus;
648 }
649
650 /* Handle the last BD specially */
651 if (last_tcp)
652 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
653 if (is_last) {
654 status |= BD_ENET_TX_INTR;
655 if (fep->bufdesc_ex)
656 ebdp->cbd_esc |= BD_ENET_TX_INT;
657 }
658
659 bdp->cbd_sc = status;
660
661 return 0;
662}
663
664static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800665fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
666 struct sk_buff *skb, struct net_device *ndev,
667 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800668{
669 struct fec_enet_private *fep = netdev_priv(ndev);
670 const struct platform_device_id *id_entry =
671 platform_get_device_id(fep->pdev);
672 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
673 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
674 void *bufaddr;
675 unsigned long dmabuf;
676 unsigned short status;
677 unsigned int estatus = 0;
678
679 status = bdp->cbd_sc;
680 status &= ~BD_ENET_TX_STATS;
681 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
682
Fugang Duan4d494cd2014-09-13 05:00:48 +0800683 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
684 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800685 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
686 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800687 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
688 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800689
690 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
691 swap_buffer(bufaddr, hdr_len);
692
693 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
694 hdr_len, DMA_TO_DEVICE);
695 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
696 dev_kfree_skb_any(skb);
697 if (net_ratelimit())
698 netdev_err(ndev, "Tx DMA memory map failed\n");
699 return NETDEV_TX_BUSY;
700 }
701 }
702
703 bdp->cbd_bufaddr = dmabuf;
704 bdp->cbd_datlen = hdr_len;
705
706 if (fep->bufdesc_ex) {
707 if (skb->ip_summed == CHECKSUM_PARTIAL)
708 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
709 ebdp->cbd_bdu = 0;
710 ebdp->cbd_esc = estatus;
711 }
712
713 bdp->cbd_sc = status;
714
715 return 0;
716}
717
Fugang Duan4d494cd2014-09-13 05:00:48 +0800718static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
719 struct sk_buff *skb,
720 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800721{
722 struct fec_enet_private *fep = netdev_priv(ndev);
723 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
724 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800725 struct bufdesc *bdp = txq->cur_tx;
726 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800727 struct tso_t tso;
728 unsigned int index = 0;
729 int ret;
730
Fugang Duan4d494cd2014-09-13 05:00:48 +0800731 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800732 dev_kfree_skb_any(skb);
733 if (net_ratelimit())
734 netdev_err(ndev, "NOT enough BD for TSO!\n");
735 return NETDEV_TX_OK;
736 }
737
738 /* Protocol checksum off-load for TCP and UDP. */
739 if (fec_enet_clear_csum(skb, ndev)) {
740 dev_kfree_skb_any(skb);
741 return NETDEV_TX_OK;
742 }
743
744 /* Initialize the TSO handler, and prepare the first payload */
745 tso_start(skb, &tso);
746
747 total_len = skb->len - hdr_len;
748 while (total_len > 0) {
749 char *hdr;
750
Fugang Duan4d494cd2014-09-13 05:00:48 +0800751 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800752 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
753 total_len -= data_left;
754
755 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800756 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800757 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800758 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800759 if (ret)
760 goto err_release;
761
762 while (data_left > 0) {
763 int size;
764
765 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800766 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
767 index = fec_enet_get_bd_index(txq->tx_bd_base,
768 bdp, fep);
769 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
770 bdp, index,
771 tso.data, size,
772 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800773 total_len == 0);
774 if (ret)
775 goto err_release;
776
777 data_left -= size;
778 tso_build_data(skb, &tso, size);
779 }
780
Fugang Duan4d494cd2014-09-13 05:00:48 +0800781 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800782 }
783
784 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800785 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800786
Nimrod Andy79f33912014-06-12 08:16:23 +0800787 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800788 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800789
790 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800791 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800792
793 return 0;
794
795err_release:
796 /* TODO: Release all used data descriptors for TSO */
797 return ret;
798}
799
Nimrod Andy61a44272014-06-12 08:16:18 +0800800static netdev_tx_t
801fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
802{
803 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800804 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800805 unsigned short queue;
806 struct fec_enet_priv_tx_q *txq;
807 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800808 int ret;
809
Fugang Duan4d494cd2014-09-13 05:00:48 +0800810 queue = skb_get_queue_mapping(skb);
811 txq = fep->tx_queue[queue];
812 nq = netdev_get_tx_queue(ndev, queue);
813
Nimrod Andy79f33912014-06-12 08:16:23 +0800814 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800815 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800816 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800817 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800818 if (ret)
819 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800820
Fugang Duan4d494cd2014-09-13 05:00:48 +0800821 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
822 if (entries_free <= txq->tx_stop_threshold)
823 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800824
825 return NETDEV_TX_OK;
826}
827
Frank Li14109a52013-03-26 16:12:03 +0000828/* Init RX & TX buffer descriptors
829 */
830static void fec_enet_bd_init(struct net_device *dev)
831{
832 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800833 struct fec_enet_priv_tx_q *txq;
834 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000835 struct bufdesc *bdp;
836 unsigned int i;
Frank Li59d0f742014-09-13 05:00:50 +0800837 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000838
Frank Li59d0f742014-09-13 05:00:50 +0800839 for (q = 0; q < fep->num_rx_queues; q++) {
840 /* Initialize the receive buffer descriptors. */
841 rxq = fep->rx_queue[q];
842 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800843
Frank Li59d0f742014-09-13 05:00:50 +0800844 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000845
Frank Li59d0f742014-09-13 05:00:50 +0800846 /* Initialize the BD for every fragment in the page. */
847 if (bdp->cbd_bufaddr)
848 bdp->cbd_sc = BD_ENET_RX_EMPTY;
849 else
850 bdp->cbd_sc = 0;
851 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000852 }
Frank Li59d0f742014-09-13 05:00:50 +0800853
854 /* Set the last buffer to wrap */
855 bdp = fec_enet_get_prevdesc(bdp, fep, q);
856 bdp->cbd_sc |= BD_SC_WRAP;
857
858 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000859 }
860
Frank Li59d0f742014-09-13 05:00:50 +0800861 for (q = 0; q < fep->num_tx_queues; q++) {
862 /* ...and the same for transmit */
863 txq = fep->tx_queue[q];
864 bdp = txq->tx_bd_base;
865 txq->cur_tx = bdp;
866
867 for (i = 0; i < txq->tx_ring_size; i++) {
868 /* Initialize the BD for every fragment in the page. */
869 bdp->cbd_sc = 0;
870 if (txq->tx_skbuff[i]) {
871 dev_kfree_skb_any(txq->tx_skbuff[i]);
872 txq->tx_skbuff[i] = NULL;
873 }
874 bdp->cbd_bufaddr = 0;
875 bdp = fec_enet_get_nextdesc(bdp, fep, q);
876 }
877
878 /* Set the last buffer to wrap */
879 bdp = fec_enet_get_prevdesc(bdp, fep, q);
880 bdp->cbd_sc |= BD_SC_WRAP;
881 txq->dirty_tx = bdp;
882 }
883}
884
885static void fec_enet_enable_ring(struct net_device *ndev)
886{
887 struct fec_enet_private *fep = netdev_priv(ndev);
888 struct fec_enet_priv_tx_q *txq;
889 struct fec_enet_priv_rx_q *rxq;
890 int i;
891
892 for (i = 0; i < fep->num_rx_queues; i++) {
893 rxq = fep->rx_queue[i];
894 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
895
896 /* enable DMA1/2 */
897 if (i)
898 writel(RCMR_MATCHEN | RCMR_CMP(i),
899 fep->hwp + FEC_RCMR(i));
900 }
901
902 for (i = 0; i < fep->num_tx_queues; i++) {
903 txq = fep->tx_queue[i];
904 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
905
906 /* enable DMA1/2 */
907 if (i)
908 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
909 fep->hwp + FEC_DMA_CFG(i));
910 }
911}
912
913static void fec_enet_reset_skb(struct net_device *ndev)
914{
915 struct fec_enet_private *fep = netdev_priv(ndev);
916 struct fec_enet_priv_tx_q *txq;
917 int i, j;
918
919 for (i = 0; i < fep->num_tx_queues; i++) {
920 txq = fep->tx_queue[i];
921
922 for (j = 0; j < txq->tx_ring_size; j++) {
923 if (txq->tx_skbuff[j]) {
924 dev_kfree_skb_any(txq->tx_skbuff[j]);
925 txq->tx_skbuff[j] = NULL;
926 }
927 }
928 }
Frank Li14109a52013-03-26 16:12:03 +0000929}
930
Russell Kingdbc64a82014-07-08 12:40:12 +0100931/*
932 * This function is called to start or restart the FEC during a link
933 * change, transmit timeout, or to reconfigure the FEC. The network
934 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100935 */
936static void
Russell Kingef833372014-07-08 12:40:43 +0100937fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100938{
939 struct fec_enet_private *fep = netdev_priv(ndev);
940 const struct platform_device_id *id_entry =
941 platform_get_device_id(fep->pdev);
942 int i;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000943 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100944 u32 temp_mac[2];
945 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000946 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100947
Fugang Duan106c3142014-09-13 05:00:51 +0800948 /* Whack a reset. We should wait for this.
949 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
950 * instead of reset MAC itself.
951 */
952 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
953 writel(0, fep->hwp + FEC_ECNTRL);
954 } else {
955 writel(1, fep->hwp + FEC_ECNTRL);
956 udelay(10);
957 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100958
959 /*
960 * enet-mac reset will reset mac address registers too,
961 * so need to reconfigure it.
962 */
963 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
964 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
965 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
966 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
967 }
968
969 /* Clear any outstanding interrupt. */
970 writel(0xffc00000, fep->hwp + FEC_IEVENT);
971
Uwe Kleine-König45993652011-01-19 20:47:04 +0100972 /* Set maximum receive buffer size. */
973 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
974
Frank Li14109a52013-03-26 16:12:03 +0000975 fec_enet_bd_init(ndev);
976
Frank Li59d0f742014-09-13 05:00:50 +0800977 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100978
Frank Li59d0f742014-09-13 05:00:50 +0800979 /* Reset tx SKB buffers. */
980 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100981
982 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100983 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100984 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100985 writel(0x04, fep->hwp + FEC_X_CNTRL);
986 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100987 /* No Rcv on Xmit */
988 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100989 writel(0x0, fep->hwp + FEC_X_CNTRL);
990 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100991
Uwe Kleine-König45993652011-01-19 20:47:04 +0100992 /* Set MII speed */
993 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
994
Guenter Roeckd1391932013-06-18 10:04:59 -0700995#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000996 /* set RX checksum */
997 val = readl(fep->hwp + FEC_RACC);
998 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
999 val |= FEC_RACC_OPTIONS;
1000 else
1001 val &= ~FEC_RACC_OPTIONS;
1002 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -07001003#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00001004
Uwe Kleine-König45993652011-01-19 20:47:04 +01001005 /*
1006 * The phy interface and speed need to get configured
1007 * differently on enet-mac.
1008 */
1009 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001010 /* Enable flow control and length check */
1011 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001012
Shawn Guo230dec62011-09-23 02:12:48 +00001013 /* RGMII, RMII or MII */
1014 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1015 rcntl |= (1 << 6);
1016 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001017 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001018 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001019 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001020
Shawn Guo230dec62011-09-23 02:12:48 +00001021 /* 1G, 100M or 10M */
1022 if (fep->phy_dev) {
1023 if (fep->phy_dev->speed == SPEED_1000)
1024 ecntl |= (1 << 5);
1025 else if (fep->phy_dev->speed == SPEED_100)
1026 rcntl &= ~(1 << 9);
1027 else
1028 rcntl |= (1 << 9);
1029 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001030 } else {
1031#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001032 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001033 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001034 /* disable the gasket and wait */
1035 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1036 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1037 udelay(1);
1038
1039 /*
1040 * configure the gasket:
1041 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001042 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001043 */
Eric Benard8d82f212012-01-12 06:10:28 +00001044 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1045 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1046 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1047 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1048 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001049
1050 /* re-enable the gasket */
1051 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1052 }
1053#endif
1054 }
Frank Libaa70a52013-01-16 16:55:58 +00001055
Guenter Roeckd1391932013-06-18 10:04:59 -07001056#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001057 /* enable pause frame*/
1058 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1059 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1060 fep->phy_dev && fep->phy_dev->pause)) {
1061 rcntl |= FEC_ENET_FCE;
1062
Jim Baxter4c09eed2013-04-19 08:10:49 +00001063 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001064 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1065 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1066 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1067 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1068
1069 /* OPD */
1070 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1071 } else {
1072 rcntl &= ~FEC_ENET_FCE;
1073 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001074#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001075
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001076 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001077
Stefan Wahren84fe6182014-03-12 11:28:19 +01001078 /* Setup multicast filter. */
1079 set_multicast_list(ndev);
1080#ifndef CONFIG_M5272
1081 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1082 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1083#endif
1084
Shawn Guo230dec62011-09-23 02:12:48 +00001085 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1086 /* enable ENET endian swap */
1087 ecntl |= (1 << 8);
1088 /* enable ENET store and forward mode */
1089 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1090 }
1091
Frank Liff43da82013-01-03 16:04:23 +00001092 if (fep->bufdesc_ex)
1093 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001094
Chris Healy38ae92d2013-06-25 23:18:52 -07001095#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001096 /* Enable the MIB statistic event counters */
1097 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001098#endif
1099
Uwe Kleine-König45993652011-01-19 20:47:04 +01001100 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001101 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Li59d0f742014-09-13 05:00:50 +08001102 for (i = 0; i < fep->num_rx_queues; i++)
1103 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
Uwe Kleine-König45993652011-01-19 20:47:04 +01001104
Frank Liff43da82013-01-03 16:04:23 +00001105 if (fep->bufdesc_ex)
1106 fec_ptp_start_cyclecounter(ndev);
1107
Uwe Kleine-König45993652011-01-19 20:47:04 +01001108 /* Enable interrupts we wish to service */
1109 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1110}
1111
1112static void
1113fec_stop(struct net_device *ndev)
1114{
1115 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001116 const struct platform_device_id *id_entry =
1117 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001118 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001119
1120 /* We cannot expect a graceful transmit stop without link !!! */
1121 if (fep->link) {
1122 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1123 udelay(10);
1124 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001125 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001126 }
1127
Fugang Duan106c3142014-09-13 05:00:51 +08001128 /* Whack a reset. We should wait for this.
1129 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1130 * instead of reset MAC itself.
1131 */
1132 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1133 writel(0, fep->hwp + FEC_ECNTRL);
1134 } else {
1135 writel(1, fep->hwp + FEC_ECNTRL);
1136 udelay(10);
1137 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001138 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1139 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001140
1141 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001142 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001143 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001144 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1145 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001146}
1147
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001150fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001152 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Russell King344756f2014-07-08 13:01:59 +01001154 fec_dump(ndev);
1155
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001156 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Russell King36cdc742014-07-08 13:01:44 +01001158 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001159}
1160
Russell King36cdc742014-07-08 13:01:44 +01001161static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001162{
1163 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001164 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001165 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001166
Russell King36cdc742014-07-08 13:01:44 +01001167 rtnl_lock();
1168 if (netif_device_present(ndev) || netif_running(ndev)) {
1169 napi_disable(&fep->napi);
1170 netif_tx_lock_bh(ndev);
1171 fec_restart(ndev);
1172 netif_wake_queue(ndev);
1173 netif_tx_unlock_bh(ndev);
1174 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001175 }
Russell King36cdc742014-07-08 13:01:44 +01001176 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001180fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1181 struct skb_shared_hwtstamps *hwtstamps)
1182{
1183 unsigned long flags;
1184 u64 ns;
1185
1186 spin_lock_irqsave(&fep->tmreg_lock, flags);
1187 ns = timecounter_cyc2time(&fep->tc, ts);
1188 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1189
1190 memset(hwtstamps, 0, sizeof(*hwtstamps));
1191 hwtstamps->hwtstamp = ns_to_ktime(ns);
1192}
1193
1194static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001195fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001198 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001199 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001201 struct fec_enet_priv_tx_q *txq;
1202 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001203 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001204 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001206 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001207
1208 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1209
1210 txq = fep->tx_queue[queue_id];
1211 /* get next bdp of dirty_tx */
1212 nq = netdev_get_tx_queue(ndev, queue_id);
1213 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Frank Lide5fb0a2013-03-03 17:34:25 +00001215 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001216 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001217
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001218 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001219
1220 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001221 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001222 break;
1223
Fugang Duan4d494cd2014-09-13 05:00:48 +08001224 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001225
Fugang Duan4d494cd2014-09-13 05:00:48 +08001226 skb = txq->tx_skbuff[index];
1227 txq->tx_skbuff[index] = NULL;
1228 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001229 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1230 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001231 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001232 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001233 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001234 continue;
1235 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001238 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 BD_ENET_TX_RL | BD_ENET_TX_UN |
1240 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001241 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001242 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001243 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001244 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001245 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001246 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001247 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001248 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001249 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001250 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001251 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001253 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001254 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
Frank Liff43da82013-01-03 16:04:23 +00001257 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1258 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001259 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001260 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001261
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001262 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001263 skb_tstamp_tx(skb, &shhwtstamps);
1264 }
Frank Liff43da82013-01-03 16:04:23 +00001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* Deferred means some collisions occurred during transmit,
1267 * but we eventually sent the packet OK.
1268 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001269 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001270 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001271
Sascha Hauer22f6b862009-04-15 01:32:18 +00001272 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001274
Fugang Duan4d494cd2014-09-13 05:00:48 +08001275 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001276
Sascha Hauer22f6b862009-04-15 01:32:18 +00001277 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001278 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001279
Sascha Hauer22f6b862009-04-15 01:32:18 +00001280 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001282 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001283 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1284 if (entries_free >= txq->tx_wake_threshold)
1285 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Russell Kingccea2962014-07-08 13:01:38 +01001288
1289 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001290 if (bdp != txq->cur_tx &&
1291 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1292 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1293}
1294
1295static void
1296fec_enet_tx(struct net_device *ndev)
1297{
1298 struct fec_enet_private *fep = netdev_priv(ndev);
1299 u16 queue_id;
1300 /* First process class A queue, then Class B and Best Effort queue */
1301 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1302 clear_bit(queue_id, &fep->work_tx);
1303 fec_enet_tx_queue(ndev, queue_id);
1304 }
1305 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308/* During a receive, the cur_rx points to the current incoming buffer.
1309 * When we update through the ring, if the next incoming buffer has
1310 * not been given to the system, we just set the empty indicator,
1311 * effectively tossing the packet.
1312 */
Frank Lidc975382013-01-28 18:31:42 +00001313static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001314fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001316 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001317 const struct platform_device_id *id_entry =
1318 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001319 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001320 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001321 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct sk_buff *skb;
1323 ushort pkt_len;
1324 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001325 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001326 struct bufdesc_ex *ebdp = NULL;
1327 bool vlan_packet_rcvd = false;
1328 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001329 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001330
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001331#ifdef CONFIG_M532x
1332 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001333#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001334 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1335 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* First, grab all of the stats for the incoming packet.
1338 * These get messed up if we get called due to a busy condition.
1339 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001340 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Sascha Hauer22f6b862009-04-15 01:32:18 +00001342 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Frank Lidc975382013-01-28 18:31:42 +00001344 if (pkt_received >= budget)
1345 break;
1346 pkt_received++;
1347
Sascha Hauer22f6b862009-04-15 01:32:18 +00001348 /* Since we have allocated space to hold a complete frame,
1349 * the last indicator should be set.
1350 */
1351 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001352 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Russell Kingdb3421c2014-07-08 13:01:49 +01001354
Sascha Hauer22f6b862009-04-15 01:32:18 +00001355 /* Check for errors. */
1356 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001358 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001359 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1360 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001361 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001362 }
1363 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001364 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001365 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001366 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001367 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001368 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001370
1371 /* Report late collisions as a frame error.
1372 * On this error, the BD is closed, but we don't know what we
1373 * have in the buffer. So, just drop this frame on the floor.
1374 */
1375 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001376 ndev->stats.rx_errors++;
1377 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001378 goto rx_processing_done;
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Sascha Hauer22f6b862009-04-15 01:32:18 +00001381 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001382 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001383 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001384 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Fugang Duan4d494cd2014-09-13 05:00:48 +08001386 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
1387 data = rxq->rx_skbuff[index]->data;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001388 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1389 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001390
Shawn Guob5680e02011-01-05 21:13:13 +00001391 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1392 swap_buffer(data, pkt_len);
1393
Jim Baxtercdffcf12013-07-02 22:52:56 +01001394 /* Extract the enhanced buffer descriptor */
1395 ebdp = NULL;
1396 if (fep->bufdesc_ex)
1397 ebdp = (struct bufdesc_ex *)bdp;
1398
1399 /* If this is a VLAN packet remove the VLAN Tag */
1400 vlan_packet_rcvd = false;
1401 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001402 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001403 /* Push and remove the vlan tag */
1404 struct vlan_hdr *vlan_header =
1405 (struct vlan_hdr *) (data + ETH_HLEN);
1406 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1407 pkt_len -= VLAN_HLEN;
1408
1409 vlan_packet_rcvd = true;
1410 }
1411
Sascha Hauer22f6b862009-04-15 01:32:18 +00001412 /* This does 16 byte alignment, exactly what we need.
1413 * The packet length includes FCS, but we don't want to
1414 * include that when passing upstream as it messes up
1415 * bridging applications.
1416 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001417 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Sascha Hauer85498892009-04-15 01:32:21 +00001419 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001420 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001421 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001422 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001423 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001424 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001425
1426 /* Extract the frame data without the VLAN header. */
1427 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1428 if (vlan_packet_rcvd)
1429 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001430 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
Jim Baxtercdffcf12013-07-02 22:52:56 +01001431 data + payload_offset,
1432 pkt_len - 4 - (2 * ETH_ALEN));
1433
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001434 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001435
Frank Li6605b732012-10-30 18:25:31 +00001436 /* Get receive timestamp from the skb */
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001437 if (fep->hwts_rx_en && fep->bufdesc_ex)
1438 fec_enet_hwtstamp(fep, ebdp->ts,
1439 skb_hwtstamps(skb));
Frank Liff43da82013-01-03 16:04:23 +00001440
Jim Baxter4c09eed2013-04-19 08:10:49 +00001441 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001442 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001443 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1444 /* don't check it */
1445 skb->ip_summed = CHECKSUM_UNNECESSARY;
1446 } else {
1447 skb_checksum_none_assert(skb);
1448 }
1449 }
1450
Jim Baxtercdffcf12013-07-02 22:52:56 +01001451 /* Handle received VLAN packets */
1452 if (vlan_packet_rcvd)
1453 __vlan_hwaccel_put_tag(skb,
1454 htons(ETH_P_8021Q),
1455 vlan_tag);
1456
Richard Cochran0affdf32013-08-30 20:28:10 +02001457 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001458 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001459
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001460 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1461 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001462rx_processing_done:
1463 /* Clear the status flags for this buffer */
1464 status &= ~BD_ENET_RX_STATS;
1465
1466 /* Mark the buffer empty */
1467 status |= BD_ENET_RX_EMPTY;
1468 bdp->cbd_sc = status;
1469
Frank Liff43da82013-01-03 16:04:23 +00001470 if (fep->bufdesc_ex) {
1471 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1472
1473 ebdp->cbd_esc = BD_ENET_RX_INT;
1474 ebdp->cbd_prot = 0;
1475 ebdp->cbd_bdu = 0;
1476 }
Frank Li6605b732012-10-30 18:25:31 +00001477
Sascha Hauer22f6b862009-04-15 01:32:18 +00001478 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001479 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001480
Sascha Hauer22f6b862009-04-15 01:32:18 +00001481 /* Doing this here will keep the FEC running while we process
1482 * incoming frames. On a heavily loaded network, we should be
1483 * able to keep up at the expense of system resources.
1484 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001485 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001487 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001488 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Fugang Duan4d494cd2014-09-13 05:00:48 +08001491static int
1492fec_enet_rx(struct net_device *ndev, int budget)
1493{
1494 int pkt_received = 0;
1495 u16 queue_id;
1496 struct fec_enet_private *fep = netdev_priv(ndev);
1497
1498 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1499 clear_bit(queue_id, &fep->work_rx);
1500 pkt_received += fec_enet_rx_queue(ndev,
1501 budget - pkt_received, queue_id);
1502 }
1503 return pkt_received;
1504}
1505
1506static bool
1507fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1508{
1509 if (int_events == 0)
1510 return false;
1511
1512 if (int_events & FEC_ENET_RXF)
1513 fep->work_rx |= (1 << 2);
1514
1515 if (int_events & FEC_ENET_TXF)
1516 fep->work_tx |= (1 << 2);
1517
1518 return true;
1519}
1520
Uwe Kleine-König45993652011-01-19 20:47:04 +01001521static irqreturn_t
1522fec_enet_interrupt(int irq, void *dev_id)
1523{
1524 struct net_device *ndev = dev_id;
1525 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001526 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001527 uint int_events;
1528 irqreturn_t ret = IRQ_NONE;
1529
Russell King7a168072014-07-08 00:22:44 +01001530 int_events = readl(fep->hwp + FEC_IEVENT);
1531 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001532 fec_enet_collect_events(fep, int_events);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001533
Russell King7a168072014-07-08 00:22:44 +01001534 if (int_events & napi_mask) {
1535 ret = IRQ_HANDLED;
Frank Lidc975382013-01-28 18:31:42 +00001536
Russell King7a168072014-07-08 00:22:44 +01001537 /* Disable the NAPI interrupts */
1538 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1539 napi_schedule(&fep->napi);
1540 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001541
Russell King7a168072014-07-08 00:22:44 +01001542 if (int_events & FEC_ENET_MII) {
1543 ret = IRQ_HANDLED;
1544 complete(&fep->mdio_done);
1545 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001546
1547 return ret;
1548}
1549
Frank Lidc975382013-01-28 18:31:42 +00001550static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1551{
1552 struct net_device *ndev = napi->dev;
Frank Lidc975382013-01-28 18:31:42 +00001553 struct fec_enet_private *fep = netdev_priv(ndev);
Russell King7a168072014-07-08 00:22:44 +01001554 int pkts;
1555
1556 /*
1557 * Clear any pending transmit or receive interrupts before
1558 * processing the rings to avoid racing with the hardware.
1559 */
1560 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1561
1562 pkts = fec_enet_rx(ndev, budget);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001563
Frank Lide5fb0a2013-03-03 17:34:25 +00001564 fec_enet_tx(ndev);
1565
Frank Lidc975382013-01-28 18:31:42 +00001566 if (pkts < budget) {
1567 napi_complete(napi);
1568 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1569 }
1570 return pkts;
1571}
Uwe Kleine-König45993652011-01-19 20:47:04 +01001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573/* ------------------------------------------------------------------------- */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00001574static void fec_get_mac(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001576 struct fec_enet_private *fep = netdev_priv(ndev);
Jingoo Han94660ba2013-08-30 13:56:26 +09001577 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
Greg Ungerer7dd6a2a2005-09-12 11:18:10 +10001578 unsigned char *iap, tmpaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Shawn Guo49da97d2011-01-05 21:13:11 +00001580 /*
1581 * try to get mac address in following order:
1582 *
1583 * 1) module parameter via kernel command line in form
1584 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1585 */
1586 iap = macaddr;
1587
1588 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001589 * 2) from device tree data
1590 */
1591 if (!is_valid_ether_addr(iap)) {
1592 struct device_node *np = fep->pdev->dev.of_node;
1593 if (np) {
1594 const char *mac = of_get_mac_address(np);
1595 if (mac)
1596 iap = (unsigned char *) mac;
1597 }
1598 }
Shawn Guoca2cc332011-06-25 02:04:35 +08001599
1600 /*
1601 * 3) from flash or fuse (via platform data)
Shawn Guo49da97d2011-01-05 21:13:11 +00001602 */
1603 if (!is_valid_ether_addr(iap)) {
1604#ifdef CONFIG_M5272
1605 if (FEC_FLASHMAC)
1606 iap = (unsigned char *)FEC_FLASHMAC;
1607#else
1608 if (pdata)
Lothar Waßmann589efdc2011-12-07 21:59:29 +00001609 iap = (unsigned char *)&pdata->mac;
Shawn Guo49da97d2011-01-05 21:13:11 +00001610#endif
1611 }
1612
1613 /*
Shawn Guoca2cc332011-06-25 02:04:35 +08001614 * 4) FEC mac registers set by bootloader
Shawn Guo49da97d2011-01-05 21:13:11 +00001615 */
1616 if (!is_valid_ether_addr(iap)) {
Dan Carpenter7d7628f2013-08-29 11:25:14 +03001617 *((__be32 *) &tmpaddr[0]) =
1618 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1619 *((__be16 *) &tmpaddr[4]) =
1620 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 iap = &tmpaddr[0];
1622 }
1623
Lucas Stachff5b2fa2013-06-03 00:38:39 +00001624 /*
1625 * 5) random mac address
1626 */
1627 if (!is_valid_ether_addr(iap)) {
1628 /* Report it and use a random ethernet address instead */
1629 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1630 eth_hw_addr_random(ndev);
1631 netdev_info(ndev, "Using random MAC address: %pM\n",
1632 ndev->dev_addr);
1633 return;
1634 }
1635
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001636 memcpy(ndev->dev_addr, iap, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Shawn Guo49da97d2011-01-05 21:13:11 +00001638 /* Adjust MAC if using macaddr */
1639 if (iap == macaddr)
Shawn Guo43af9402011-12-05 05:01:15 +00001640 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643/* ------------------------------------------------------------------------- */
1644
Bryan Wue6b043d2010-03-31 02:10:44 +00001645/*
1646 * Phy section
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001648static void fec_enet_adjust_link(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001650 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001651 struct phy_device *phy_dev = fep->phy_dev;
Bryan Wue6b043d2010-03-31 02:10:44 +00001652 int status_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Bryan Wue6b043d2010-03-31 02:10:44 +00001654 /* Prevent a state halted on mii error */
1655 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1656 phy_dev->state = PHY_RESUMING;
Frank Li54309fa2013-05-07 14:08:44 +00001657 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001659
Russell King8ce56242014-07-08 12:40:07 +01001660 /*
1661 * If the netdev is down, or is going down, we're not interested
1662 * in link state events, so just mark our idea of the link as down
1663 * and ignore the event.
1664 */
1665 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1666 fep->link = 0;
1667 } else if (phy_dev->link) {
Lucas Stachd97e7492013-03-14 05:12:01 +00001668 if (!fep->link) {
Lothar Waßmann6ea07222011-12-07 21:59:27 +00001669 fep->link = phy_dev->link;
Bryan Wue6b043d2010-03-31 02:10:44 +00001670 status_change = 1;
1671 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001672
Russell Kingef833372014-07-08 12:40:43 +01001673 if (fep->full_duplex != phy_dev->duplex) {
1674 fep->full_duplex = phy_dev->duplex;
Lucas Stachd97e7492013-03-14 05:12:01 +00001675 status_change = 1;
Russell Kingef833372014-07-08 12:40:43 +01001676 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001677
1678 if (phy_dev->speed != fep->speed) {
1679 fep->speed = phy_dev->speed;
1680 status_change = 1;
1681 }
1682
1683 /* if any of the above changed restart the FEC */
Russell Kingdbc64a82014-07-08 12:40:12 +01001684 if (status_change) {
Russell Kingdbc64a82014-07-08 12:40:12 +01001685 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001686 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01001687 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001688 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01001689 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01001690 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01001691 }
Lucas Stachd97e7492013-03-14 05:12:01 +00001692 } else {
1693 if (fep->link) {
Russell Kingf208ce12014-07-08 12:40:38 +01001694 napi_disable(&fep->napi);
1695 netif_tx_lock_bh(ndev);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001696 fec_stop(ndev);
Russell Kingf208ce12014-07-08 12:40:38 +01001697 netif_tx_unlock_bh(ndev);
1698 napi_enable(&fep->napi);
Lucas Stach8d7ed0f2013-04-16 00:42:42 +00001699 fep->link = phy_dev->link;
Lucas Stachd97e7492013-03-14 05:12:01 +00001700 status_change = 1;
1701 }
Bryan Wue6b043d2010-03-31 02:10:44 +00001702 }
1703
Bryan Wue6b043d2010-03-31 02:10:44 +00001704 if (status_change)
1705 phy_print_status(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Bryan Wue6b043d2010-03-31 02:10:44 +00001708static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Bryan Wue6b043d2010-03-31 02:10:44 +00001710 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001711 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001712
1713 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001714 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001715
1716 /* start a read op */
1717 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1718 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1719 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1720
1721 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001722 time_left = wait_for_completion_timeout(&fep->mdio_done,
1723 usecs_to_jiffies(FEC_MII_TIMEOUT));
1724 if (time_left == 0) {
1725 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001726 netdev_err(fep->netdev, "MDIO read timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001727 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001728 }
1729
1730 /* return value */
1731 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1732}
1733
1734static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1735 u16 value)
1736{
1737 struct fec_enet_private *fep = bus->priv;
Baruch Siach97b72e42010-07-11 21:12:51 +00001738 unsigned long time_left;
Bryan Wue6b043d2010-03-31 02:10:44 +00001739
1740 fep->mii_timeout = 0;
Baruch Siach97b72e42010-07-11 21:12:51 +00001741 init_completion(&fep->mdio_done);
Bryan Wue6b043d2010-03-31 02:10:44 +00001742
Shawn Guo862f0982011-01-05 21:13:09 +00001743 /* start a write op */
1744 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
Bryan Wue6b043d2010-03-31 02:10:44 +00001745 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1746 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1747 fep->hwp + FEC_MII_DATA);
1748
1749 /* wait for end of transfer */
Baruch Siach97b72e42010-07-11 21:12:51 +00001750 time_left = wait_for_completion_timeout(&fep->mdio_done,
1751 usecs_to_jiffies(FEC_MII_TIMEOUT));
1752 if (time_left == 0) {
1753 fep->mii_timeout = 1;
Joe Perches31b77202013-04-13 19:03:17 +00001754 netdev_err(fep->netdev, "MDIO write timeout\n");
Baruch Siach97b72e42010-07-11 21:12:51 +00001755 return -ETIMEDOUT;
Bryan Wue6b043d2010-03-31 02:10:44 +00001756 }
1757
1758 return 0;
1759}
1760
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001761static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1762{
1763 struct fec_enet_private *fep = netdev_priv(ndev);
1764 int ret;
1765
1766 if (enable) {
1767 ret = clk_prepare_enable(fep->clk_ahb);
1768 if (ret)
1769 return ret;
1770 ret = clk_prepare_enable(fep->clk_ipg);
1771 if (ret)
1772 goto failed_clk_ipg;
1773 if (fep->clk_enet_out) {
1774 ret = clk_prepare_enable(fep->clk_enet_out);
1775 if (ret)
1776 goto failed_clk_enet_out;
1777 }
1778 if (fep->clk_ptp) {
Nimrod Andy91c0d982014-08-21 17:09:38 +08001779 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001780 ret = clk_prepare_enable(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001781 if (ret) {
1782 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001783 goto failed_clk_ptp;
Nimrod Andy91c0d982014-08-21 17:09:38 +08001784 } else {
1785 fep->ptp_clk_on = true;
1786 }
1787 mutex_unlock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001788 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001789 if (fep->clk_ref) {
1790 ret = clk_prepare_enable(fep->clk_ref);
1791 if (ret)
1792 goto failed_clk_ref;
1793 }
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001794 } else {
1795 clk_disable_unprepare(fep->clk_ahb);
1796 clk_disable_unprepare(fep->clk_ipg);
1797 if (fep->clk_enet_out)
1798 clk_disable_unprepare(fep->clk_enet_out);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001799 if (fep->clk_ptp) {
1800 mutex_lock(&fep->ptp_clk_mutex);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001801 clk_disable_unprepare(fep->clk_ptp);
Nimrod Andy91c0d982014-08-21 17:09:38 +08001802 fep->ptp_clk_on = false;
1803 mutex_unlock(&fep->ptp_clk_mutex);
1804 }
Fugang Duan9b5330e2014-09-13 05:00:46 +08001805 if (fep->clk_ref)
1806 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001807 }
1808
1809 return 0;
Fugang Duan9b5330e2014-09-13 05:00:46 +08001810
1811failed_clk_ref:
1812 if (fep->clk_ref)
1813 clk_disable_unprepare(fep->clk_ref);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08001814failed_clk_ptp:
1815 if (fep->clk_enet_out)
1816 clk_disable_unprepare(fep->clk_enet_out);
1817failed_clk_enet_out:
1818 clk_disable_unprepare(fep->clk_ipg);
1819failed_clk_ipg:
1820 clk_disable_unprepare(fep->clk_ahb);
1821
1822 return ret;
1823}
1824
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001825static int fec_enet_mii_probe(struct net_device *ndev)
Bryan Wue6b043d2010-03-31 02:10:44 +00001826{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001827 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001828 const struct platform_device_id *id_entry =
1829 platform_get_device_id(fep->pdev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001830 struct phy_device *phy_dev = NULL;
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001831 char mdio_bus_id[MII_BUS_ID_SIZE];
1832 char phy_name[MII_BUS_ID_SIZE + 3];
1833 int phy_id;
Shawn Guo43af9402011-12-05 05:01:15 +00001834 int dev_id = fep->dev_id;
Bryan Wue6b043d2010-03-31 02:10:44 +00001835
Bryan Wu418bd0d2010-05-28 03:40:39 -07001836 fep->phy_dev = NULL;
1837
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001838 if (fep->phy_node) {
1839 phy_dev = of_phy_connect(ndev, fep->phy_node,
1840 &fec_enet_adjust_link, 0,
1841 fep->phy_interface);
1842 } else {
1843 /* check for attached phy */
1844 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1845 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1846 continue;
1847 if (fep->mii_bus->phy_map[phy_id] == NULL)
1848 continue;
1849 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1850 continue;
1851 if (dev_id--)
1852 continue;
1853 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1854 break;
1855 }
1856
1857 if (phy_id >= PHY_MAX_ADDR) {
1858 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1859 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1860 phy_id = 0;
1861 }
1862
1863 snprintf(phy_name, sizeof(phy_name),
1864 PHY_ID_FMT, mdio_bus_id, phy_id);
1865 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1866 fep->phy_interface);
Bryan Wue6b043d2010-03-31 02:10:44 +00001867 }
1868
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001869 if (IS_ERR(phy_dev)) {
Joe Perches31b77202013-04-13 19:03:17 +00001870 netdev_err(ndev, "could not attach to PHY\n");
Greg Ungerer6fcc0402010-10-11 21:03:05 +00001871 return PTR_ERR(phy_dev);
Bryan Wue6b043d2010-03-31 02:10:44 +00001872 }
1873
1874 /* mask with MAC supported features */
Frank Libaa70a52013-01-16 16:55:58 +00001875 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
Shawn Guo230dec62011-09-23 02:12:48 +00001876 phy_dev->supported &= PHY_GBIT_FEATURES;
Russell Kingb44592f2014-07-08 00:22:34 +01001877 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
Guenter Roeckd1391932013-06-18 10:04:59 -07001878#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001879 phy_dev->supported |= SUPPORTED_Pause;
Guenter Roeckd1391932013-06-18 10:04:59 -07001880#endif
Frank Libaa70a52013-01-16 16:55:58 +00001881 }
Shawn Guo230dec62011-09-23 02:12:48 +00001882 else
1883 phy_dev->supported &= PHY_BASIC_FEATURES;
1884
Bryan Wue6b043d2010-03-31 02:10:44 +00001885 phy_dev->advertising = phy_dev->supported;
1886
1887 fep->phy_dev = phy_dev;
1888 fep->link = 0;
1889 fep->full_duplex = 0;
1890
Joe Perches31b77202013-04-13 19:03:17 +00001891 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1892 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1893 fep->phy_dev->irq);
Bryan Wu418bd0d2010-05-28 03:40:39 -07001894
Bryan Wue6b043d2010-03-31 02:10:44 +00001895 return 0;
1896}
1897
1898static int fec_enet_mii_init(struct platform_device *pdev)
1899{
Shawn Guob5680e02011-01-05 21:13:13 +00001900 static struct mii_bus *fec0_mii_bus;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001901 struct net_device *ndev = platform_get_drvdata(pdev);
1902 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001903 const struct platform_device_id *id_entry =
1904 platform_get_device_id(fep->pdev);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001905 struct device_node *node;
Bryan Wue6b043d2010-03-31 02:10:44 +00001906 int err = -ENXIO, i;
1907
Shawn Guob5680e02011-01-05 21:13:13 +00001908 /*
1909 * The dual fec interfaces are not equivalent with enet-mac.
1910 * Here are the differences:
1911 *
1912 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1913 * - fec0 acts as the 1588 time master while fec1 is slave
1914 * - external phys can only be configured by fec0
1915 *
1916 * That is to say fec1 can not work independently. It only works
1917 * when fec0 is working. The reason behind this design is that the
1918 * second interface is added primarily for Switch mode.
1919 *
1920 * Because of the last point above, both phys are attached on fec0
1921 * mdio interface in board design, and need to be configured by
1922 * fec0 mii_bus.
1923 */
Shawn Guo43af9402011-12-05 05:01:15 +00001924 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
Shawn Guob5680e02011-01-05 21:13:13 +00001925 /* fec1 uses fec0 mii_bus */
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001926 if (mii_cnt && fec0_mii_bus) {
1927 fep->mii_bus = fec0_mii_bus;
1928 mii_cnt++;
1929 return 0;
1930 }
1931 return -ENOENT;
Shawn Guob5680e02011-01-05 21:13:13 +00001932 }
1933
Bryan Wue6b043d2010-03-31 02:10:44 +00001934 fep->mii_timeout = 0;
1935
1936 /*
1937 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
Shawn Guo230dec62011-09-23 02:12:48 +00001938 *
1939 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1940 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1941 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1942 * document.
Bryan Wue6b043d2010-03-31 02:10:44 +00001943 */
Nimrod Andy98a6eeb2014-05-20 13:23:09 +08001944 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
Shawn Guo230dec62011-09-23 02:12:48 +00001945 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1946 fep->phy_speed--;
1947 fep->phy_speed <<= 1;
Bryan Wue6b043d2010-03-31 02:10:44 +00001948 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1949
1950 fep->mii_bus = mdiobus_alloc();
1951 if (fep->mii_bus == NULL) {
1952 err = -ENOMEM;
1953 goto err_out;
1954 }
1955
1956 fep->mii_bus->name = "fec_enet_mii_bus";
1957 fep->mii_bus->read = fec_enet_mdio_read;
1958 fep->mii_bus->write = fec_enet_mdio_write;
Florian Fainelli391420f2012-01-09 23:59:13 +00001959 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1960 pdev->name, fep->dev_id + 1);
Bryan Wue6b043d2010-03-31 02:10:44 +00001961 fep->mii_bus->priv = fep;
1962 fep->mii_bus->parent = &pdev->dev;
1963
1964 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1965 if (!fep->mii_bus->irq) {
1966 err = -ENOMEM;
1967 goto err_out_free_mdiobus;
1968 }
1969
1970 for (i = 0; i < PHY_MAX_ADDR; i++)
1971 fep->mii_bus->irq[i] = PHY_POLL;
1972
Uwe Kleine-König407066f2014-08-11 17:35:33 +02001973 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
1974 if (node) {
1975 err = of_mdiobus_register(fep->mii_bus, node);
1976 of_node_put(node);
1977 } else {
1978 err = mdiobus_register(fep->mii_bus);
1979 }
1980
1981 if (err)
Bryan Wue6b043d2010-03-31 02:10:44 +00001982 goto err_out_free_mdio_irq;
1983
Lothar Waßmanne163cc92011-12-07 21:59:31 +00001984 mii_cnt++;
1985
Shawn Guob5680e02011-01-05 21:13:13 +00001986 /* save fec0 mii_bus */
1987 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1988 fec0_mii_bus = fep->mii_bus;
1989
Bryan Wue6b043d2010-03-31 02:10:44 +00001990 return 0;
1991
Bryan Wue6b043d2010-03-31 02:10:44 +00001992err_out_free_mdio_irq:
1993 kfree(fep->mii_bus->irq);
1994err_out_free_mdiobus:
1995 mdiobus_free(fep->mii_bus);
1996err_out:
1997 return err;
1998}
1999
2000static void fec_enet_mii_remove(struct fec_enet_private *fep)
2001{
Lothar Waßmanne163cc92011-12-07 21:59:31 +00002002 if (--mii_cnt == 0) {
2003 mdiobus_unregister(fep->mii_bus);
2004 kfree(fep->mii_bus->irq);
2005 mdiobus_free(fep->mii_bus);
2006 }
Bryan Wue6b043d2010-03-31 02:10:44 +00002007}
2008
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002009static int fec_enet_get_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002010 struct ethtool_cmd *cmd)
2011{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002012 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002013 struct phy_device *phydev = fep->phy_dev;
2014
2015 if (!phydev)
2016 return -ENODEV;
2017
2018 return phy_ethtool_gset(phydev, cmd);
2019}
2020
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002021static int fec_enet_set_settings(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002022 struct ethtool_cmd *cmd)
2023{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002024 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002025 struct phy_device *phydev = fep->phy_dev;
2026
2027 if (!phydev)
2028 return -ENODEV;
2029
2030 return phy_ethtool_sset(phydev, cmd);
2031}
2032
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002033static void fec_enet_get_drvinfo(struct net_device *ndev,
Bryan Wue6b043d2010-03-31 02:10:44 +00002034 struct ethtool_drvinfo *info)
2035{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002036 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Jiri Pirko7826d432013-01-06 00:44:26 +00002038 strlcpy(info->driver, fep->pdev->dev.driver->name,
2039 sizeof(info->driver));
2040 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2041 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
Bryan Wue6b043d2010-03-31 02:10:44 +00002043
Frank Li5ebae4892013-01-06 16:25:07 +00002044static int fec_enet_get_ts_info(struct net_device *ndev,
2045 struct ethtool_ts_info *info)
2046{
2047 struct fec_enet_private *fep = netdev_priv(ndev);
2048
2049 if (fep->bufdesc_ex) {
2050
2051 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2052 SOF_TIMESTAMPING_RX_SOFTWARE |
2053 SOF_TIMESTAMPING_SOFTWARE |
2054 SOF_TIMESTAMPING_TX_HARDWARE |
2055 SOF_TIMESTAMPING_RX_HARDWARE |
2056 SOF_TIMESTAMPING_RAW_HARDWARE;
2057 if (fep->ptp_clock)
2058 info->phc_index = ptp_clock_index(fep->ptp_clock);
2059 else
2060 info->phc_index = -1;
2061
2062 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2063 (1 << HWTSTAMP_TX_ON);
2064
2065 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2066 (1 << HWTSTAMP_FILTER_ALL);
2067 return 0;
2068 } else {
2069 return ethtool_op_get_ts_info(ndev, info);
2070 }
2071}
2072
Guenter Roeckd1391932013-06-18 10:04:59 -07002073#if !defined(CONFIG_M5272)
2074
Frank Libaa70a52013-01-16 16:55:58 +00002075static void fec_enet_get_pauseparam(struct net_device *ndev,
2076 struct ethtool_pauseparam *pause)
2077{
2078 struct fec_enet_private *fep = netdev_priv(ndev);
2079
2080 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2081 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2082 pause->rx_pause = pause->tx_pause;
2083}
2084
2085static int fec_enet_set_pauseparam(struct net_device *ndev,
2086 struct ethtool_pauseparam *pause)
2087{
2088 struct fec_enet_private *fep = netdev_priv(ndev);
2089
Russell King0b146ca2014-07-08 00:22:59 +01002090 if (!fep->phy_dev)
2091 return -ENODEV;
2092
Frank Libaa70a52013-01-16 16:55:58 +00002093 if (pause->tx_pause != pause->rx_pause) {
2094 netdev_info(ndev,
2095 "hardware only support enable/disable both tx and rx");
2096 return -EINVAL;
2097 }
2098
2099 fep->pause_flag = 0;
2100
2101 /* tx pause must be same as rx pause */
2102 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2103 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2104
2105 if (pause->rx_pause || pause->autoneg) {
2106 fep->phy_dev->supported |= ADVERTISED_Pause;
2107 fep->phy_dev->advertising |= ADVERTISED_Pause;
2108 } else {
2109 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2110 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2111 }
2112
2113 if (pause->autoneg) {
2114 if (netif_running(ndev))
2115 fec_stop(ndev);
2116 phy_start_aneg(fep->phy_dev);
2117 }
Russell Kingdbc64a82014-07-08 12:40:12 +01002118 if (netif_running(ndev)) {
Russell Kingdbc64a82014-07-08 12:40:12 +01002119 napi_disable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002120 netif_tx_lock_bh(ndev);
Russell Kingef833372014-07-08 12:40:43 +01002121 fec_restart(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002122 netif_wake_queue(ndev);
Russell King6af42d42014-07-08 12:40:18 +01002123 netif_tx_unlock_bh(ndev);
Russell Kingdbc64a82014-07-08 12:40:12 +01002124 napi_enable(&fep->napi);
Russell Kingdbc64a82014-07-08 12:40:12 +01002125 }
Frank Libaa70a52013-01-16 16:55:58 +00002126
2127 return 0;
2128}
2129
Chris Healy38ae92d2013-06-25 23:18:52 -07002130static const struct fec_stat {
2131 char name[ETH_GSTRING_LEN];
2132 u16 offset;
2133} fec_stats[] = {
2134 /* RMON TX */
2135 { "tx_dropped", RMON_T_DROP },
2136 { "tx_packets", RMON_T_PACKETS },
2137 { "tx_broadcast", RMON_T_BC_PKT },
2138 { "tx_multicast", RMON_T_MC_PKT },
2139 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2140 { "tx_undersize", RMON_T_UNDERSIZE },
2141 { "tx_oversize", RMON_T_OVERSIZE },
2142 { "tx_fragment", RMON_T_FRAG },
2143 { "tx_jabber", RMON_T_JAB },
2144 { "tx_collision", RMON_T_COL },
2145 { "tx_64byte", RMON_T_P64 },
2146 { "tx_65to127byte", RMON_T_P65TO127 },
2147 { "tx_128to255byte", RMON_T_P128TO255 },
2148 { "tx_256to511byte", RMON_T_P256TO511 },
2149 { "tx_512to1023byte", RMON_T_P512TO1023 },
2150 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2151 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2152 { "tx_octets", RMON_T_OCTETS },
2153
2154 /* IEEE TX */
2155 { "IEEE_tx_drop", IEEE_T_DROP },
2156 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2157 { "IEEE_tx_1col", IEEE_T_1COL },
2158 { "IEEE_tx_mcol", IEEE_T_MCOL },
2159 { "IEEE_tx_def", IEEE_T_DEF },
2160 { "IEEE_tx_lcol", IEEE_T_LCOL },
2161 { "IEEE_tx_excol", IEEE_T_EXCOL },
2162 { "IEEE_tx_macerr", IEEE_T_MACERR },
2163 { "IEEE_tx_cserr", IEEE_T_CSERR },
2164 { "IEEE_tx_sqe", IEEE_T_SQE },
2165 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2166 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2167
2168 /* RMON RX */
2169 { "rx_packets", RMON_R_PACKETS },
2170 { "rx_broadcast", RMON_R_BC_PKT },
2171 { "rx_multicast", RMON_R_MC_PKT },
2172 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2173 { "rx_undersize", RMON_R_UNDERSIZE },
2174 { "rx_oversize", RMON_R_OVERSIZE },
2175 { "rx_fragment", RMON_R_FRAG },
2176 { "rx_jabber", RMON_R_JAB },
2177 { "rx_64byte", RMON_R_P64 },
2178 { "rx_65to127byte", RMON_R_P65TO127 },
2179 { "rx_128to255byte", RMON_R_P128TO255 },
2180 { "rx_256to511byte", RMON_R_P256TO511 },
2181 { "rx_512to1023byte", RMON_R_P512TO1023 },
2182 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2183 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2184 { "rx_octets", RMON_R_OCTETS },
2185
2186 /* IEEE RX */
2187 { "IEEE_rx_drop", IEEE_R_DROP },
2188 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2189 { "IEEE_rx_crc", IEEE_R_CRC },
2190 { "IEEE_rx_align", IEEE_R_ALIGN },
2191 { "IEEE_rx_macerr", IEEE_R_MACERR },
2192 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2193 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2194};
2195
2196static void fec_enet_get_ethtool_stats(struct net_device *dev,
2197 struct ethtool_stats *stats, u64 *data)
2198{
2199 struct fec_enet_private *fep = netdev_priv(dev);
2200 int i;
2201
2202 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2203 data[i] = readl(fep->hwp + fec_stats[i].offset);
2204}
2205
2206static void fec_enet_get_strings(struct net_device *netdev,
2207 u32 stringset, u8 *data)
2208{
2209 int i;
2210 switch (stringset) {
2211 case ETH_SS_STATS:
2212 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2213 memcpy(data + i * ETH_GSTRING_LEN,
2214 fec_stats[i].name, ETH_GSTRING_LEN);
2215 break;
2216 }
2217}
2218
2219static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2220{
2221 switch (sset) {
2222 case ETH_SS_STATS:
2223 return ARRAY_SIZE(fec_stats);
2224 default:
2225 return -EOPNOTSUPP;
2226 }
2227}
Guenter Roeckd1391932013-06-18 10:04:59 -07002228#endif /* !defined(CONFIG_M5272) */
Chris Healy38ae92d2013-06-25 23:18:52 -07002229
Chris Healy32bc9b42013-06-17 07:25:06 -07002230static int fec_enet_nway_reset(struct net_device *dev)
2231{
2232 struct fec_enet_private *fep = netdev_priv(dev);
2233 struct phy_device *phydev = fep->phy_dev;
2234
2235 if (!phydev)
2236 return -ENODEV;
2237
2238 return genphy_restart_aneg(phydev);
2239}
2240
stephen hemminger9b07be42012-01-04 12:59:49 +00002241static const struct ethtool_ops fec_enet_ethtool_ops = {
Bryan Wue6b043d2010-03-31 02:10:44 +00002242 .get_settings = fec_enet_get_settings,
2243 .set_settings = fec_enet_set_settings,
2244 .get_drvinfo = fec_enet_get_drvinfo,
Chris Healy32bc9b42013-06-17 07:25:06 -07002245 .nway_reset = fec_enet_nway_reset,
Russell Kingc1d7c482014-07-08 13:01:54 +01002246 .get_link = ethtool_op_get_link,
Chris Healy38ae92d2013-06-25 23:18:52 -07002247#ifndef CONFIG_M5272
Russell Kingc1d7c482014-07-08 13:01:54 +01002248 .get_pauseparam = fec_enet_get_pauseparam,
2249 .set_pauseparam = fec_enet_set_pauseparam,
Chris Healy38ae92d2013-06-25 23:18:52 -07002250 .get_strings = fec_enet_get_strings,
Russell Kingc1d7c482014-07-08 13:01:54 +01002251 .get_ethtool_stats = fec_enet_get_ethtool_stats,
Chris Healy38ae92d2013-06-25 23:18:52 -07002252 .get_sset_count = fec_enet_get_sset_count,
2253#endif
Russell Kingc1d7c482014-07-08 13:01:54 +01002254 .get_ts_info = fec_enet_get_ts_info,
Bryan Wue6b043d2010-03-31 02:10:44 +00002255};
2256
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002257static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Bryan Wue6b043d2010-03-31 02:10:44 +00002258{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002259 struct fec_enet_private *fep = netdev_priv(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00002260 struct phy_device *phydev = fep->phy_dev;
2261
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002262 if (!netif_running(ndev))
Bryan Wue6b043d2010-03-31 02:10:44 +00002263 return -EINVAL;
2264
2265 if (!phydev)
2266 return -ENODEV;
2267
Ben Hutchings1d5244d2013-11-18 23:02:44 +00002268 if (fep->bufdesc_ex) {
2269 if (cmd == SIOCSHWTSTAMP)
2270 return fec_ptp_set(ndev, rq);
2271 if (cmd == SIOCGHWTSTAMP)
2272 return fec_ptp_get(ndev, rq);
2273 }
Frank Liff43da82013-01-03 16:04:23 +00002274
Richard Cochran28b04112010-07-17 08:48:55 +00002275 return phy_mii_ioctl(phydev, rq, cmd);
Bryan Wue6b043d2010-03-31 02:10:44 +00002276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002278static void fec_enet_free_buffers(struct net_device *ndev)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002279{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002280 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002281 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002282 struct sk_buff *skb;
2283 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002284 struct fec_enet_priv_tx_q *txq;
2285 struct fec_enet_priv_rx_q *rxq;
Frank Li59d0f742014-09-13 05:00:50 +08002286 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002287
Frank Li59d0f742014-09-13 05:00:50 +08002288 for (q = 0; q < fep->num_rx_queues; q++) {
2289 rxq = fep->rx_queue[q];
2290 bdp = rxq->rx_bd_base;
2291 for (i = 0; i < rxq->rx_ring_size; i++) {
2292 skb = rxq->rx_skbuff[i];
2293 rxq->rx_skbuff[i] = NULL;
2294 if (skb) {
2295 dma_unmap_single(&fep->pdev->dev,
2296 bdp->cbd_bufaddr,
2297 FEC_ENET_RX_FRSIZE,
2298 DMA_FROM_DEVICE);
2299 dev_kfree_skb(skb);
2300 }
2301 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Russell King730ee362014-07-08 00:23:14 +01002302 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002303 }
2304
Frank Li59d0f742014-09-13 05:00:50 +08002305 for (q = 0; q < fep->num_tx_queues; q++) {
2306 txq = fep->tx_queue[q];
2307 bdp = txq->tx_bd_base;
2308 for (i = 0; i < txq->tx_ring_size; i++) {
2309 kfree(txq->tx_bounce[i]);
2310 txq->tx_bounce[i] = NULL;
2311 skb = txq->tx_skbuff[i];
2312 txq->tx_skbuff[i] = NULL;
2313 dev_kfree_skb(skb);
2314 }
Russell King8b7c9ef2014-07-08 00:23:25 +01002315 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002316}
2317
Frank Li59d0f742014-09-13 05:00:50 +08002318static void fec_enet_free_queue(struct net_device *ndev)
2319{
2320 struct fec_enet_private *fep = netdev_priv(ndev);
2321 int i;
2322 struct fec_enet_priv_tx_q *txq;
2323
2324 for (i = 0; i < fep->num_tx_queues; i++)
2325 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2326 txq = fep->tx_queue[i];
2327 dma_free_coherent(NULL,
2328 txq->tx_ring_size * TSO_HEADER_SIZE,
2329 txq->tso_hdrs,
2330 txq->tso_hdrs_dma);
2331 }
2332
2333 for (i = 0; i < fep->num_rx_queues; i++)
2334 if (fep->rx_queue[i])
2335 kfree(fep->rx_queue[i]);
2336
2337 for (i = 0; i < fep->num_tx_queues; i++)
2338 if (fep->tx_queue[i])
2339 kfree(fep->tx_queue[i]);
2340}
2341
2342static int fec_enet_alloc_queue(struct net_device *ndev)
2343{
2344 struct fec_enet_private *fep = netdev_priv(ndev);
2345 int i;
2346 int ret = 0;
2347 struct fec_enet_priv_tx_q *txq;
2348
2349 for (i = 0; i < fep->num_tx_queues; i++) {
2350 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2351 if (!txq) {
2352 ret = -ENOMEM;
2353 goto alloc_failed;
2354 }
2355
2356 fep->tx_queue[i] = txq;
2357 txq->tx_ring_size = TX_RING_SIZE;
2358 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2359
2360 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2361 txq->tx_wake_threshold =
2362 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2363
2364 txq->tso_hdrs = dma_alloc_coherent(NULL,
2365 txq->tx_ring_size * TSO_HEADER_SIZE,
2366 &txq->tso_hdrs_dma,
2367 GFP_KERNEL);
2368 if (!txq->tso_hdrs) {
2369 ret = -ENOMEM;
2370 goto alloc_failed;
2371 }
2372 }
2373
2374 for (i = 0; i < fep->num_rx_queues; i++) {
2375 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2376 GFP_KERNEL);
2377 if (!fep->rx_queue[i]) {
2378 ret = -ENOMEM;
2379 goto alloc_failed;
2380 }
2381
2382 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2383 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2384 }
2385 return ret;
2386
2387alloc_failed:
2388 fec_enet_free_queue(ndev);
2389 return ret;
2390}
2391
2392static int
2393fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002394{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002395 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevamda2191e2013-03-20 12:31:07 -03002396 unsigned int i;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002397 struct sk_buff *skb;
2398 struct bufdesc *bdp;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002399 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002400
Frank Li59d0f742014-09-13 05:00:50 +08002401 rxq = fep->rx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002402 bdp = rxq->rx_bd_base;
2403 for (i = 0; i < rxq->rx_ring_size; i++) {
Russell King730ee362014-07-08 00:23:14 +01002404 dma_addr_t addr;
2405
Fabio Estevamb72061a2012-02-07 08:27:31 +00002406 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
Russell Kingffdce2c2014-07-08 00:23:30 +01002407 if (!skb)
2408 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002409
Russell King730ee362014-07-08 00:23:14 +01002410 addr = dma_map_single(&fep->pdev->dev, skb->data,
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002411 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Russell King730ee362014-07-08 00:23:14 +01002412 if (dma_mapping_error(&fep->pdev->dev, addr)) {
2413 dev_kfree_skb(skb);
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002414 if (net_ratelimit())
2415 netdev_err(ndev, "Rx DMA memory map failed\n");
Russell Kingffdce2c2014-07-08 00:23:30 +01002416 goto err_alloc;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08002417 }
Russell King730ee362014-07-08 00:23:14 +01002418
Fugang Duan4d494cd2014-09-13 05:00:48 +08002419 rxq->rx_skbuff[i] = skb;
Russell King730ee362014-07-08 00:23:14 +01002420 bdp->cbd_bufaddr = addr;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002421 bdp->cbd_sc = BD_ENET_RX_EMPTY;
Frank Liff43da82013-01-03 16:04:23 +00002422
2423 if (fep->bufdesc_ex) {
2424 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2425 ebdp->cbd_esc = BD_ENET_RX_INT;
2426 }
2427
Frank Li59d0f742014-09-13 05:00:50 +08002428 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002429 }
2430
2431 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002432 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002433 bdp->cbd_sc |= BD_SC_WRAP;
Frank Li59d0f742014-09-13 05:00:50 +08002434 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002435
Frank Li59d0f742014-09-13 05:00:50 +08002436 err_alloc:
2437 fec_enet_free_buffers(ndev);
2438 return -ENOMEM;
2439}
2440
2441static int
2442fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2443{
2444 struct fec_enet_private *fep = netdev_priv(ndev);
2445 unsigned int i;
2446 struct bufdesc *bdp;
2447 struct fec_enet_priv_tx_q *txq;
2448
2449 txq = fep->tx_queue[queue];
Fugang Duan4d494cd2014-09-13 05:00:48 +08002450 bdp = txq->tx_bd_base;
2451 for (i = 0; i < txq->tx_ring_size; i++) {
2452 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2453 if (!txq->tx_bounce[i])
Russell Kingffdce2c2014-07-08 00:23:30 +01002454 goto err_alloc;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002455
2456 bdp->cbd_sc = 0;
2457 bdp->cbd_bufaddr = 0;
Frank Li6605b732012-10-30 18:25:31 +00002458
Frank Liff43da82013-01-03 16:04:23 +00002459 if (fep->bufdesc_ex) {
2460 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Jim Baxter96d22222013-03-26 05:25:07 +00002461 ebdp->cbd_esc = BD_ENET_TX_INT;
Frank Liff43da82013-01-03 16:04:23 +00002462 }
2463
Frank Li59d0f742014-09-13 05:00:50 +08002464 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002465 }
2466
2467 /* Set the last buffer to wrap. */
Frank Li59d0f742014-09-13 05:00:50 +08002468 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002469 bdp->cbd_sc |= BD_SC_WRAP;
2470
2471 return 0;
Russell Kingffdce2c2014-07-08 00:23:30 +01002472
2473 err_alloc:
2474 fec_enet_free_buffers(ndev);
2475 return -ENOMEM;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002476}
2477
Frank Li59d0f742014-09-13 05:00:50 +08002478static int fec_enet_alloc_buffers(struct net_device *ndev)
2479{
2480 struct fec_enet_private *fep = netdev_priv(ndev);
2481 unsigned int i;
2482
2483 for (i = 0; i < fep->num_rx_queues; i++)
2484 if (fec_enet_alloc_rxq_buffers(ndev, i))
2485 return -ENOMEM;
2486
2487 for (i = 0; i < fep->num_tx_queues; i++)
2488 if (fec_enet_alloc_txq_buffers(ndev, i))
2489 return -ENOMEM;
2490 return 0;
2491}
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002494fec_enet_open(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002496 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002497 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002499 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002500 ret = fec_enet_clk_enable(ndev, true);
2501 if (ret)
2502 return ret;
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 /* I should reset the ring buffers here, but I don't yet know
2505 * a simple way to do that.
2506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002508 ret = fec_enet_alloc_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002509 if (ret)
2510 return ret;
2511
Bryan Wu418bd0d2010-05-28 03:40:39 -07002512 /* Probe and connect to PHY when open the interface */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002513 ret = fec_enet_mii_probe(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002514 if (ret) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002515 fec_enet_free_buffers(ndev);
Bryan Wu418bd0d2010-05-28 03:40:39 -07002516 return ret;
2517 }
Russell Kingce5eaf02014-02-18 12:55:42 +00002518
Russell Kingef833372014-07-08 12:40:43 +01002519 fec_restart(ndev);
Russell Kingce5eaf02014-02-18 12:55:42 +00002520 napi_enable(&fep->napi);
Bryan Wue6b043d2010-03-31 02:10:44 +00002521 phy_start(fep->phy_dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002522 netif_tx_start_all_queues(ndev);
2523
Sascha Hauer22f6b862009-04-15 01:32:18 +00002524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
2527static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002528fec_enet_close(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002530 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Russell Kingd76cfae2014-07-08 00:23:04 +01002532 phy_stop(fep->phy_dev);
2533
Russell King31a6de32014-07-08 12:40:23 +01002534 if (netif_device_present(ndev)) {
2535 napi_disable(&fep->napi);
2536 netif_tx_disable(ndev);
Russell King8bbbd3c2014-07-08 12:40:02 +01002537 fec_stop(ndev);
Russell King31a6de32014-07-08 12:40:23 +01002538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Russell King635cf172014-07-08 00:22:54 +01002540 phy_disconnect(fep->phy_dev);
Russell King0b146ca2014-07-08 00:22:59 +01002541 fep->phy_dev = NULL;
Bryan Wu418bd0d2010-05-28 03:40:39 -07002542
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08002543 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002544 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002545 fec_enet_free_buffers(ndev);
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return 0;
2548}
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550/* Set or clear the multicast filter for this adaptor.
2551 * Skeleton taken from sunlance driver.
2552 * The CPM Ethernet implementation allows Multicast as well as individual
2553 * MAC address filtering. Some of the drivers check to make sure it is
2554 * a group multicast address, and discard those that are not. I guess I
2555 * will do the same for now, but just remove the test if you want
2556 * individual filtering as well (do the upper net layers want or support
2557 * this kind of feature?).
2558 */
2559
2560#define HASH_BITS 6 /* #bits in hash */
2561#define CRC32_POLY 0xEDB88320
2562
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002563static void set_multicast_list(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002565 struct fec_enet_private *fep = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002566 struct netdev_hw_addr *ha;
Jiri Pirko48e2f182010-02-22 09:22:26 +00002567 unsigned int i, bit, data, crc, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 unsigned char hash;
2569
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002570 if (ndev->flags & IFF_PROMISC) {
Sascha Hauerf44d6302009-04-15 03:11:30 +00002571 tmp = readl(fep->hwp + FEC_R_CNTRL);
2572 tmp |= 0x8;
2573 writel(tmp, fep->hwp + FEC_R_CNTRL);
Sascha Hauer4e831832009-04-15 01:32:19 +00002574 return;
2575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Sascha Hauer4e831832009-04-15 01:32:19 +00002577 tmp = readl(fep->hwp + FEC_R_CNTRL);
2578 tmp &= ~0x8;
2579 writel(tmp, fep->hwp + FEC_R_CNTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002580
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002581 if (ndev->flags & IFF_ALLMULTI) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002582 /* Catch all multicast addresses, so set the
2583 * filter to all 1's
2584 */
2585 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2586 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Sascha Hauer4e831832009-04-15 01:32:19 +00002588 return;
2589 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002590
Sascha Hauer4e831832009-04-15 01:32:19 +00002591 /* Clear filter and add the addresses in hash register
2592 */
2593 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2594 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002596 netdev_for_each_mc_addr(ha, ndev) {
Sascha Hauer4e831832009-04-15 01:32:19 +00002597 /* calculate crc32 value of mac address */
2598 crc = 0xffffffff;
2599
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002600 for (i = 0; i < ndev->addr_len; i++) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002601 data = ha->addr[i];
Sascha Hauer4e831832009-04-15 01:32:19 +00002602 for (bit = 0; bit < 8; bit++, data >>= 1) {
2603 crc = (crc >> 1) ^
2604 (((crc ^ data) & 1) ? CRC32_POLY : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606 }
Sascha Hauer4e831832009-04-15 01:32:19 +00002607
2608 /* only upper 6 bits (HASH_BITS) are used
2609 * which point to specific bit in he hash registers
2610 */
2611 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2612
2613 if (hash > 31) {
2614 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2615 tmp |= 1 << (hash - 32);
2616 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2617 } else {
2618 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2619 tmp |= 1 << hash;
2620 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623}
2624
Sascha Hauer22f6b862009-04-15 01:32:18 +00002625/* Set a MAC change in hardware. */
Sascha Hauer009fda82009-04-15 01:32:23 +00002626static int
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002627fec_set_mac_address(struct net_device *ndev, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002629 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Hauer009fda82009-04-15 01:32:23 +00002630 struct sockaddr *addr = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Lucas Stach44934fa2014-03-30 21:32:08 +02002632 if (addr) {
2633 if (!is_valid_ether_addr(addr->sa_data))
2634 return -EADDRNOTAVAIL;
2635 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2636 }
Sascha Hauer009fda82009-04-15 01:32:23 +00002637
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002638 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2639 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
Sascha Hauerf44d6302009-04-15 03:11:30 +00002640 fep->hwp + FEC_ADDR_LOW);
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002641 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
Mattias Walström7cff0942010-05-05 00:55:48 -07002642 fep->hwp + FEC_ADDR_HIGH);
Sascha Hauer009fda82009-04-15 01:32:23 +00002643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002646#ifdef CONFIG_NET_POLL_CONTROLLER
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002647/**
2648 * fec_poll_controller - FEC Poll controller function
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002649 * @dev: The FEC network adapter
2650 *
2651 * Polled functionality used by netconsole and others in non interrupt mode
2652 *
2653 */
Wei Yongjun47a52472013-03-20 05:06:11 +00002654static void fec_poll_controller(struct net_device *dev)
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002655{
2656 int i;
2657 struct fec_enet_private *fep = netdev_priv(dev);
2658
2659 for (i = 0; i < FEC_IRQ_NUM; i++) {
2660 if (fep->irq[i] > 0) {
2661 disable_irq(fep->irq[i]);
2662 fec_enet_interrupt(fep->irq[i], dev);
2663 enable_irq(fep->irq[i]);
2664 }
2665 }
2666}
2667#endif
2668
Russell King8506fa12014-07-08 12:40:33 +01002669#define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2670
Jim Baxter4c09eed2013-04-19 08:10:49 +00002671static int fec_set_features(struct net_device *netdev,
2672 netdev_features_t features)
2673{
2674 struct fec_enet_private *fep = netdev_priv(netdev);
2675 netdev_features_t changed = features ^ netdev->features;
2676
Russell King8506fa12014-07-08 12:40:33 +01002677 /* Quiesce the device if necessary */
2678 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2679 napi_disable(&fep->napi);
2680 netif_tx_lock_bh(netdev);
2681 fec_stop(netdev);
2682 }
2683
Jim Baxter4c09eed2013-04-19 08:10:49 +00002684 netdev->features = features;
2685
2686 /* Receive checksum has been changed */
2687 if (changed & NETIF_F_RXCSUM) {
2688 if (features & NETIF_F_RXCSUM)
2689 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2690 else
2691 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
Russell King8506fa12014-07-08 12:40:33 +01002692 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002693
Russell King8506fa12014-07-08 12:40:33 +01002694 /* Resume the device after updates */
2695 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
Russell Kingef833372014-07-08 12:40:43 +01002696 fec_restart(netdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002697 netif_tx_wake_all_queues(netdev);
Russell King8506fa12014-07-08 12:40:33 +01002698 netif_tx_unlock_bh(netdev);
2699 napi_enable(&fep->napi);
Jim Baxter4c09eed2013-04-19 08:10:49 +00002700 }
2701
2702 return 0;
2703}
2704
Fugang Duan4d494cd2014-09-13 05:00:48 +08002705u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
2706 void *accel_priv, select_queue_fallback_t fallback)
2707{
2708 return skb_tx_hash(ndev, skb);
2709}
2710
Sascha Hauer009fda82009-04-15 01:32:23 +00002711static const struct net_device_ops fec_netdev_ops = {
2712 .ndo_open = fec_enet_open,
2713 .ndo_stop = fec_enet_close,
2714 .ndo_start_xmit = fec_enet_start_xmit,
Fugang Duan4d494cd2014-09-13 05:00:48 +08002715 .ndo_select_queue = fec_enet_select_queue,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002716 .ndo_set_rx_mode = set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002717 .ndo_change_mtu = eth_change_mtu,
Sascha Hauer009fda82009-04-15 01:32:23 +00002718 .ndo_validate_addr = eth_validate_addr,
2719 .ndo_tx_timeout = fec_timeout,
2720 .ndo_set_mac_address = fec_set_mac_address,
Uwe Kleine-Königdb8880b2011-01-19 20:26:39 +01002721 .ndo_do_ioctl = fec_enet_ioctl,
Xiao Jiang7f5c6ad2011-09-29 02:15:57 +00002722#ifdef CONFIG_NET_POLL_CONTROLLER
2723 .ndo_poll_controller = fec_poll_controller,
2724#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +00002725 .ndo_set_features = fec_set_features,
Sascha Hauer009fda82009-04-15 01:32:23 +00002726};
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /*
2729 * XXX: We need to clean up on failure exits here.
Sascha Haueread73182009-01-28 23:03:11 +00002730 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002732static int fec_enet_init(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002734 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo48496252013-05-08 21:08:22 +00002735 const struct platform_device_id *id_entry =
2736 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002737 struct fec_enet_priv_tx_q *txq;
2738 struct fec_enet_priv_rx_q *rxq;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002739 struct bufdesc *cbd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +08002740 dma_addr_t bd_dma;
Nimrod Andy55d02182014-06-12 08:16:21 +08002741 int bd_size;
Frank Li59d0f742014-09-13 05:00:50 +08002742 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002743
Frank Li59d0f742014-09-13 05:00:50 +08002744 fec_enet_alloc_queue(ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +08002745
Nimrod Andy55d02182014-06-12 08:16:21 +08002746 if (fep->bufdesc_ex)
2747 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2748 else
2749 fep->bufdesc_size = sizeof(struct bufdesc);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002750 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
Nimrod Andy55d02182014-06-12 08:16:21 +08002751 fep->bufdesc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002753 /* Allocate memory for buffer descriptors. */
Fugang Duan4d494cd2014-09-13 05:00:48 +08002754 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00002755 GFP_KERNEL);
Fugang Duan4d494cd2014-09-13 05:00:48 +08002756 if (!cbd_base) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08002757 return -ENOMEM;
2758 }
Sebastian Siewior3b2b74c2008-05-01 14:08:12 +10002759
Fugang Duan4d494cd2014-09-13 05:00:48 +08002760 memset(cbd_base, 0, bd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Shawn Guo49da97d2011-01-05 21:13:11 +00002762 /* Get the Ethernet address */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002763 fec_get_mac(ndev);
Lucas Stach44934fa2014-03-30 21:32:08 +02002764 /* make sure MAC we just acquired is programmed into the hw */
2765 fec_set_mac_address(ndev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Sascha Hauer8d4dd5c2009-04-15 01:32:17 +00002767 /* Set receive and transmit descriptor base. */
Frank Li59d0f742014-09-13 05:00:50 +08002768 for (i = 0; i < fep->num_rx_queues; i++) {
2769 rxq = fep->rx_queue[i];
2770 rxq->index = i;
2771 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2772 rxq->bd_dma = bd_dma;
2773 if (fep->bufdesc_ex) {
2774 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
2775 cbd_base = (struct bufdesc *)
2776 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
2777 } else {
2778 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
2779 cbd_base += rxq->rx_ring_size;
2780 }
2781 }
2782
2783 for (i = 0; i < fep->num_tx_queues; i++) {
2784 txq = fep->tx_queue[i];
2785 txq->index = i;
2786 txq->tx_bd_base = (struct bufdesc *)cbd_base;
2787 txq->bd_dma = bd_dma;
2788 if (fep->bufdesc_ex) {
2789 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
2790 cbd_base = (struct bufdesc *)
2791 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
2792 } else {
2793 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
2794 cbd_base += txq->tx_ring_size;
2795 }
2796 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Sascha Hauer22f6b862009-04-15 01:32:18 +00002799 /* The FEC Ethernet specific entries in the device structure */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01002800 ndev->watchdog_timeo = TX_TIMEOUT;
2801 ndev->netdev_ops = &fec_netdev_ops;
2802 ndev->ethtool_ops = &fec_enet_ethtool_ops;
Rob Herring633e7532010-02-05 08:56:20 +00002803
Frank Lidc975382013-01-28 18:31:42 +00002804 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
Fabio Estevam322555f2013-08-27 17:35:08 -03002805 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
Frank Lidc975382013-01-28 18:31:42 +00002806
Nimrod Andy09d1e542014-06-12 08:16:20 +08002807 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
Jim Baxtercdffcf12013-07-02 22:52:56 +01002808 /* enable hw VLAN support */
2809 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Jim Baxtercdffcf12013-07-02 22:52:56 +01002810
Shawn Guo48496252013-05-08 21:08:22 +00002811 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
Nimrod Andy79f33912014-06-12 08:16:23 +08002812 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
2813
Shawn Guo48496252013-05-08 21:08:22 +00002814 /* enable hw accelerator */
2815 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
Nimrod Andy79f33912014-06-12 08:16:23 +08002816 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
Shawn Guo48496252013-05-08 21:08:22 +00002817 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2818 }
Jim Baxter4c09eed2013-04-19 08:10:49 +00002819
Nimrod Andy09d1e542014-06-12 08:16:20 +08002820 ndev->hw_features = ndev->features;
2821
Russell Kingef833372014-07-08 12:40:43 +01002822 fec_restart(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return 0;
2825}
2826
Shawn Guoca2cc332011-06-25 02:04:35 +08002827#ifdef CONFIG_OF
Bill Pemberton33897cc2012-12-03 09:23:58 -05002828static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002829{
2830 int err, phy_reset;
Shawn Guoa3caad02012-06-27 03:45:24 +00002831 int msec = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002832 struct device_node *np = pdev->dev.of_node;
2833
2834 if (!np)
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002835 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002836
Shawn Guoa3caad02012-06-27 03:45:24 +00002837 of_property_read_u32(np, "phy-reset-duration", &msec);
2838 /* A sane reset duration should not be longer than 1s */
2839 if (msec > 1000)
2840 msec = 1;
2841
Shawn Guoca2cc332011-06-25 02:04:35 +08002842 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002843 if (!gpio_is_valid(phy_reset))
2844 return;
2845
Shawn Guo119fc002012-06-27 03:45:22 +00002846 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2847 GPIOF_OUT_INIT_LOW, "phy-reset");
Shawn Guoca2cc332011-06-25 02:04:35 +08002848 if (err) {
Fabio Estevam07dcf8e2013-02-18 10:20:31 +00002849 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
Shawn Guoa9b2c8e2011-09-23 02:12:46 +00002850 return;
Shawn Guoca2cc332011-06-25 02:04:35 +08002851 }
Shawn Guoa3caad02012-06-27 03:45:24 +00002852 msleep(msec);
Shawn Guoca2cc332011-06-25 02:04:35 +08002853 gpio_set_value(phy_reset, 1);
Shawn Guoca2cc332011-06-25 02:04:35 +08002854}
2855#else /* CONFIG_OF */
Fabio Estevam0c7768a2013-01-07 17:42:56 +00002856static void fec_reset_phy(struct platform_device *pdev)
Shawn Guoca2cc332011-06-25 02:04:35 +08002857{
2858 /*
2859 * In case of platform probe, the reset has been done
2860 * by machine code.
2861 */
Shawn Guoca2cc332011-06-25 02:04:35 +08002862}
2863#endif /* CONFIG_OF */
2864
Fugang Duan9fc095f2014-09-13 05:00:49 +08002865static void
2866fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
2867{
2868 struct device_node *np = pdev->dev.of_node;
2869 int err;
2870
2871 *num_tx = *num_rx = 1;
2872
2873 if (!np || !of_device_is_available(np))
2874 return;
2875
2876 /* parse the num of tx and rx queues */
2877 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
2878 err |= of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
2879 if (err) {
2880 *num_tx = 1;
2881 *num_rx = 1;
2882 return;
2883 }
2884
2885 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
2886 dev_err(&pdev->dev, "Invalidate num_tx(=%d), fail back to 1\n",
2887 *num_tx);
2888 *num_tx = 1;
2889 return;
2890 }
2891
2892 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
2893 dev_err(&pdev->dev, "Invalidate num_rx(=%d), fail back to 1\n",
2894 *num_rx);
2895 *num_rx = 1;
2896 return;
2897 }
2898
2899}
2900
Bill Pemberton33897cc2012-12-03 09:23:58 -05002901static int
Sascha Haueread73182009-01-28 23:03:11 +00002902fec_probe(struct platform_device *pdev)
2903{
2904 struct fec_enet_private *fep;
Baruch Siach5eb32bd2010-05-24 00:36:13 -07002905 struct fec_platform_data *pdata;
Sascha Haueread73182009-01-28 23:03:11 +00002906 struct net_device *ndev;
2907 int i, irq, ret = 0;
2908 struct resource *r;
Shawn Guoca2cc332011-06-25 02:04:35 +08002909 const struct of_device_id *of_id;
Shawn Guo43af9402011-12-05 05:01:15 +00002910 static int dev_id;
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002911 struct device_node *np = pdev->dev.of_node, *phy_node;
Fugang Duan9fc095f2014-09-13 05:00:49 +08002912 int num_tx_qs = 1;
2913 int num_rx_qs = 1;
Shawn Guoca2cc332011-06-25 02:04:35 +08002914
2915 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2916 if (of_id)
2917 pdev->id_entry = of_id->data;
Sascha Haueread73182009-01-28 23:03:11 +00002918
Fugang Duan9fc095f2014-09-13 05:00:49 +08002919 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
2920
Sascha Haueread73182009-01-28 23:03:11 +00002921 /* Init network device */
Fugang Duan9fc095f2014-09-13 05:00:49 +08002922 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
2923 num_tx_qs, num_rx_qs);
Fabio Estevam83e519b2013-03-11 07:32:55 +00002924 if (!ndev)
2925 return -ENOMEM;
Sascha Haueread73182009-01-28 23:03:11 +00002926
2927 SET_NETDEV_DEV(ndev, &pdev->dev);
2928
2929 /* setup board info structure */
2930 fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00002931
Fugang Duan9fc095f2014-09-13 05:00:49 +08002932 fep->num_rx_queues = num_rx_qs;
2933 fep->num_tx_queues = num_tx_qs;
2934
Guenter Roeckd1391932013-06-18 10:04:59 -07002935#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00002936 /* default enable pause frame auto negotiation */
2937 if (pdev->id_entry &&
2938 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2939 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
Guenter Roeckd1391932013-06-18 10:04:59 -07002940#endif
Frank Libaa70a52013-01-16 16:55:58 +00002941
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08002942 /* Select default pin state */
2943 pinctrl_pm_select_default_state(&pdev->dev);
2944
Fabio Estevam399db752013-07-21 13:25:03 -03002945 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tushar Behera941e1732013-06-10 17:05:05 +05302946 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2947 if (IS_ERR(fep->hwp)) {
2948 ret = PTR_ERR(fep->hwp);
2949 goto failed_ioremap;
2950 }
2951
Bryan Wue6b043d2010-03-31 02:10:44 +00002952 fep->pdev = pdev;
Shawn Guo43af9402011-12-05 05:01:15 +00002953 fep->dev_id = dev_id++;
Sascha Haueread73182009-01-28 23:03:11 +00002954
Frank Liff43da82013-01-03 16:04:23 +00002955 fep->bufdesc_ex = 0;
2956
Sascha Haueread73182009-01-28 23:03:11 +00002957 platform_set_drvdata(pdev, ndev);
2958
Uwe Kleine-König407066f2014-08-11 17:35:33 +02002959 phy_node = of_parse_phandle(np, "phy-handle", 0);
2960 if (!phy_node && of_phy_is_fixed_link(np)) {
2961 ret = of_phy_register_fixed_link(np);
2962 if (ret < 0) {
2963 dev_err(&pdev->dev,
2964 "broken fixed-link specification\n");
2965 goto failed_phy;
2966 }
2967 phy_node = of_node_get(np);
2968 }
2969 fep->phy_node = phy_node;
2970
Guenter Roeck6c5f7802013-04-02 09:35:10 +00002971 ret = of_get_phy_mode(pdev->dev.of_node);
Shawn Guoca2cc332011-06-25 02:04:35 +08002972 if (ret < 0) {
Jingoo Han94660ba2013-08-30 13:56:26 +09002973 pdata = dev_get_platdata(&pdev->dev);
Shawn Guoca2cc332011-06-25 02:04:35 +08002974 if (pdata)
2975 fep->phy_interface = pdata->phy;
2976 else
2977 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2978 } else {
2979 fep->phy_interface = ret;
2980 }
2981
Fabio Estevame2f8d552013-02-22 06:40:45 +00002982 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2983 if (IS_ERR(fep->clk_ipg)) {
2984 ret = PTR_ERR(fep->clk_ipg);
2985 goto failed_clk;
2986 }
2987
2988 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2989 if (IS_ERR(fep->clk_ahb)) {
2990 ret = PTR_ERR(fep->clk_ahb);
2991 goto failed_clk;
2992 }
2993
Linus Torvalds38f56f32013-05-07 11:06:17 -07002994 /* enet_out is optional, depends on board */
2995 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2996 if (IS_ERR(fep->clk_enet_out))
2997 fep->clk_enet_out = NULL;
2998
Nimrod Andy91c0d982014-08-21 17:09:38 +08002999 fep->ptp_clk_on = false;
3000 mutex_init(&fep->ptp_clk_mutex);
Fugang Duan9b5330e2014-09-13 05:00:46 +08003001
3002 /* clk_ref is optional, depends on board */
3003 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3004 if (IS_ERR(fep->clk_ref))
3005 fep->clk_ref = NULL;
3006
Fabio Estevame2f8d552013-02-22 06:40:45 +00003007 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003008 fep->bufdesc_ex =
3009 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003010 if (IS_ERR(fep->clk_ptp)) {
Linus Torvalds38f56f32013-05-07 11:06:17 -07003011 fep->clk_ptp = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003012 fep->bufdesc_ex = 0;
3013 }
3014
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003015 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003016 if (ret)
3017 goto failed_clk;
3018
Fabio Estevamf4e9f3d2013-05-27 03:48:29 +00003019 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3020 if (!IS_ERR(fep->reg_phy)) {
3021 ret = regulator_enable(fep->reg_phy);
Fabio Estevame2f8d552013-02-22 06:40:45 +00003022 if (ret) {
3023 dev_err(&pdev->dev,
3024 "Failed to enable phy regulator: %d\n", ret);
3025 goto failed_regulator;
3026 }
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003027 } else {
3028 fep->reg_phy = NULL;
Fabio Estevame2f8d552013-02-22 06:40:45 +00003029 }
3030
3031 fec_reset_phy(pdev);
3032
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003033 if (fep->bufdesc_ex)
Fabio Estevamca162a82013-06-07 10:48:00 +00003034 fec_ptp_init(pdev);
Fabio Estevam7f7d6c22013-02-21 09:21:50 +00003035
3036 ret = fec_enet_init(ndev);
3037 if (ret)
3038 goto failed_init;
3039
Xiao Jiangc7c83d12011-09-29 02:15:56 +00003040 for (i = 0; i < FEC_IRQ_NUM; i++) {
Sascha Haueread73182009-01-28 23:03:11 +00003041 irq = platform_get_irq(pdev, i);
Lothar Waßmann86f9f2c2011-12-07 21:59:28 +00003042 if (irq < 0) {
3043 if (i)
3044 break;
3045 ret = irq;
3046 goto failed_irq;
3047 }
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003048 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
Michael Opdenacker44a272d2013-09-13 05:44:38 +02003049 0, pdev->name, ndev);
Fabio Estevam0d9b2ab2013-07-21 13:25:04 -03003050 if (ret)
Sascha Haueread73182009-01-28 23:03:11 +00003051 goto failed_irq;
Sascha Haueread73182009-01-28 23:03:11 +00003052 }
3053
Bryan Wue6b043d2010-03-31 02:10:44 +00003054 ret = fec_enet_mii_init(pdev);
3055 if (ret)
3056 goto failed_mii_init;
3057
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003058 /* Carrier starts down, phylib will bring it up */
3059 netif_carrier_off(ndev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003060 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003061 pinctrl_pm_select_sleep_state(&pdev->dev);
Oskar Schirmer03c698c2010-10-07 02:30:30 +00003062
Sascha Haueread73182009-01-28 23:03:11 +00003063 ret = register_netdev(ndev);
3064 if (ret)
3065 goto failed_register;
3066
Fabio Estevameb1d0642013-04-13 07:25:36 +00003067 if (fep->bufdesc_ex && fep->ptp_clock)
3068 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3069
Russell King36cdc742014-07-08 13:01:44 +01003070 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
Sascha Haueread73182009-01-28 23:03:11 +00003071 return 0;
3072
3073failed_register:
Bryan Wue6b043d2010-03-31 02:10:44 +00003074 fec_enet_mii_remove(fep);
3075failed_mii_init:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003076failed_irq:
Fabio Estevam7a2bbd82013-05-27 03:48:30 +00003077failed_init:
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003078 if (fep->reg_phy)
3079 regulator_disable(fep->reg_phy);
Shawn Guo5fa9c0f2012-06-27 03:45:21 +00003080failed_regulator:
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003081 fec_enet_clk_enable(ndev, false);
Sascha Haueread73182009-01-28 23:03:11 +00003082failed_clk:
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003083failed_phy:
3084 of_node_put(phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003085failed_ioremap:
3086 free_netdev(ndev);
3087
3088 return ret;
3089}
3090
Bill Pemberton33897cc2012-12-03 09:23:58 -05003091static int
Sascha Haueread73182009-01-28 23:03:11 +00003092fec_drv_remove(struct platform_device *pdev)
3093{
3094 struct net_device *ndev = platform_get_drvdata(pdev);
3095 struct fec_enet_private *fep = netdev_priv(ndev);
3096
Nimrod Andy91c0d982014-08-21 17:09:38 +08003097 cancel_delayed_work_sync(&fep->time_keep);
Russell King36cdc742014-07-08 13:01:44 +01003098 cancel_work_sync(&fep->tx_timeout_work);
Lothar Waßmanne163cc92011-12-07 21:59:31 +00003099 unregister_netdev(ndev);
Bryan Wue6b043d2010-03-31 02:10:44 +00003100 fec_enet_mii_remove(fep);
Fabio Estevamf6a4d602013-05-27 03:48:31 +00003101 if (fep->reg_phy)
3102 regulator_disable(fep->reg_phy);
Frank Li6605b732012-10-30 18:25:31 +00003103 if (fep->ptp_clock)
3104 ptp_clock_unregister(fep->ptp_clock);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003105 fec_enet_clk_enable(ndev, false);
Uwe Kleine-König407066f2014-08-11 17:35:33 +02003106 of_node_put(fep->phy_node);
Sascha Haueread73182009-01-28 23:03:11 +00003107 free_netdev(ndev);
Uwe Kleine-König28e21882011-01-13 21:44:18 +01003108
Sascha Haueread73182009-01-28 23:03:11 +00003109 return 0;
3110}
3111
Fabio Estevamdd66d382014-07-24 18:24:01 -03003112static int __maybe_unused fec_suspend(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003113{
Eric Benard87cad5c2010-06-18 04:19:54 +00003114 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003115 struct fec_enet_private *fep = netdev_priv(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003116
Russell Kingda1774e2014-07-08 12:39:57 +01003117 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003118 if (netif_running(ndev)) {
Russell Kingd76cfae2014-07-08 00:23:04 +01003119 phy_stop(fep->phy_dev);
Russell King31a6de32014-07-08 12:40:23 +01003120 napi_disable(&fep->napi);
3121 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003122 netif_device_detach(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003123 netif_tx_unlock_bh(ndev);
3124 fec_stop(ndev);
Sascha Haueread73182009-01-28 23:03:11 +00003125 }
Russell Kingda1774e2014-07-08 12:39:57 +01003126 rtnl_unlock();
3127
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003128 fec_enet_clk_enable(ndev, false);
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003129 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003130
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003131 if (fep->reg_phy)
3132 regulator_disable(fep->reg_phy);
3133
Sascha Haueread73182009-01-28 23:03:11 +00003134 return 0;
3135}
3136
Fabio Estevamdd66d382014-07-24 18:24:01 -03003137static int __maybe_unused fec_resume(struct device *dev)
Sascha Haueread73182009-01-28 23:03:11 +00003138{
Eric Benard87cad5c2010-06-18 04:19:54 +00003139 struct net_device *ndev = dev_get_drvdata(dev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003140 struct fec_enet_private *fep = netdev_priv(ndev);
Fabio Estevam238f7bc2013-05-27 03:48:33 +00003141 int ret;
3142
3143 if (fep->reg_phy) {
3144 ret = regulator_enable(fep->reg_phy);
3145 if (ret)
3146 return ret;
3147 }
Sascha Haueread73182009-01-28 23:03:11 +00003148
Nimrod Andy5bbde4d2014-05-27 15:51:08 +08003149 pinctrl_pm_select_default_state(&fep->pdev->dev);
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003150 ret = fec_enet_clk_enable(ndev, true);
Fabio Estevam13a097b2013-07-21 13:25:02 -03003151 if (ret)
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003152 goto failed_clk;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003153
Russell Kingda1774e2014-07-08 12:39:57 +01003154 rtnl_lock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003155 if (netif_running(ndev)) {
Russell Kingef833372014-07-08 12:40:43 +01003156 fec_restart(ndev);
Russell King31a6de32014-07-08 12:40:23 +01003157 netif_tx_lock_bh(ndev);
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003158 netif_device_attach(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003159 netif_tx_unlock_bh(ndev);
Russell King6af42d42014-07-08 12:40:18 +01003160 napi_enable(&fep->napi);
Russell Kingd76cfae2014-07-08 00:23:04 +01003161 phy_start(fep->phy_dev);
Sascha Haueread73182009-01-28 23:03:11 +00003162 }
Russell Kingda1774e2014-07-08 12:39:57 +01003163 rtnl_unlock();
Uwe Kleine-König04e52162011-01-13 21:53:40 +01003164
Sascha Haueread73182009-01-28 23:03:11 +00003165 return 0;
Fabio Estevam13a097b2013-07-21 13:25:02 -03003166
Nimrod Andye8fcfcd2014-05-20 13:22:51 +08003167failed_clk:
Fabio Estevam13a097b2013-07-21 13:25:02 -03003168 if (fep->reg_phy)
3169 regulator_disable(fep->reg_phy);
3170 return ret;
Sascha Haueread73182009-01-28 23:03:11 +00003171}
3172
Fabio Estevambf7bfd72013-04-16 08:17:46 +00003173static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
Denis Kirjanov59d42892010-06-02 09:27:04 +00003174
Sascha Haueread73182009-01-28 23:03:11 +00003175static struct platform_driver fec_driver = {
3176 .driver = {
Shawn Guob5680e02011-01-05 21:13:13 +00003177 .name = DRIVER_NAME,
Eric Benard87cad5c2010-06-18 04:19:54 +00003178 .owner = THIS_MODULE,
Eric Benard87cad5c2010-06-18 04:19:54 +00003179 .pm = &fec_pm_ops,
Shawn Guoca2cc332011-06-25 02:04:35 +08003180 .of_match_table = fec_dt_ids,
Sascha Haueread73182009-01-28 23:03:11 +00003181 },
Shawn Guob5680e02011-01-05 21:13:13 +00003182 .id_table = fec_devtype,
Eric Benard87cad5c2010-06-18 04:19:54 +00003183 .probe = fec_probe,
Bill Pemberton33897cc2012-12-03 09:23:58 -05003184 .remove = fec_drv_remove,
Sascha Haueread73182009-01-28 23:03:11 +00003185};
3186
Fabio Estevamaaca2372012-01-23 16:44:50 +00003187module_platform_driver(fec_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Fabio Estevamf8c0aca2013-07-20 16:20:36 -03003189MODULE_ALIAS("platform:"DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190MODULE_LICENSE("GPL");