blob: 658b0b344bd4913ebaa18c7ecfa40df778c4fcb5 [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
Jim Baxtercdffcf12013-07-02 22:52:56 +0100196/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Jim Baxtercdffcf12013-07-02 22:52:56 +0100198#define PKT_MAXBUF_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define PKT_MINBUF_SIZE 64
Jim Baxtercdffcf12013-07-02 22:52:56 +0100200#define PKT_MAXBLR_SIZE 1536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Jim Baxter4c09eed2013-04-19 08:10:49 +0000202/* FEC receive acceleration */
203#define FEC_RACC_IPDIS (1 << 1)
204#define FEC_RACC_PRODIS (1 << 2)
205#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
Matt Waddel6b265292006-06-27 13:10:56 +1000208 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * size bits. Other FEC hardware does not, so we need to take that into
210 * account when setting it.
211 */
Greg Ungerer562d2f82005-11-07 14:09:50 +1000212#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
Uwe Kleine-König085e79e2011-01-17 09:52:18 +0100213 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
215#else
216#define OPT_FRAME_SIZE 0
217#endif
218
Bryan Wue6b043d2010-03-31 02:10:44 +0000219/* FEC MII MMFR bits definition */
220#define FEC_MMFR_ST (1 << 30)
221#define FEC_MMFR_OP_READ (2 << 28)
222#define FEC_MMFR_OP_WRITE (1 << 28)
223#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
224#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
225#define FEC_MMFR_TA (2 << 16)
226#define FEC_MMFR_DATA(v) (v & 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Rogerio Pimentelc3b084c2011-12-27 14:07:37 -0500228#define FEC_MII_TIMEOUT 30000 /* us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Sascha Hauer22f6b862009-04-15 01:32:18 +0000230/* Transmitter timeout */
231#define TX_TIMEOUT (2 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Frank Libaa70a52013-01-16 16:55:58 +0000233#define FEC_PAUSE_FLAG_AUTONEG 0x1
234#define FEC_PAUSE_FLAG_ENABLE 0x2
235
Nimrod Andy79f33912014-06-12 08:16:23 +0800236#define TSO_HEADER_SIZE 128
237/* Max number of allowed TCP segments for software TSO */
238#define FEC_MAX_TSO_SEGS 100
239#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
240
241#define IS_TSO_HEADER(txq, addr) \
242 ((addr >= txq->tso_hdrs_dma) && \
243 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
244
Lothar Waßmanne163cc92011-12-07 21:59:31 +0000245static int mii_cnt;
246
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800247static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800248struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
249 struct fec_enet_private *fep,
250 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000251{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800252 struct bufdesc *new_bd = bdp + 1;
253 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800254 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
255 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800256 struct bufdesc_ex *ex_base;
257 struct bufdesc *base;
258 int ring_size;
259
Fugang Duan4d494cd2014-09-13 05:00:48 +0800260 if (bdp >= txq->tx_bd_base) {
261 base = txq->tx_bd_base;
262 ring_size = txq->tx_ring_size;
263 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800264 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800265 base = rxq->rx_bd_base;
266 ring_size = rxq->rx_ring_size;
267 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800268 }
269
270 if (fep->bufdesc_ex)
271 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
272 ex_base : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000273 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800274 return (new_bd >= (base + ring_size)) ?
275 base : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000276}
277
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800278static inline
Fugang Duan4d494cd2014-09-13 05:00:48 +0800279struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
280 struct fec_enet_private *fep,
281 int queue_id)
Frank Liff43da82013-01-03 16:04:23 +0000282{
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800283 struct bufdesc *new_bd = bdp - 1;
284 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800285 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
286 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800287 struct bufdesc_ex *ex_base;
288 struct bufdesc *base;
289 int ring_size;
290
Fugang Duan4d494cd2014-09-13 05:00:48 +0800291 if (bdp >= txq->tx_bd_base) {
292 base = txq->tx_bd_base;
293 ring_size = txq->tx_ring_size;
294 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800295 } else {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800296 base = rxq->rx_bd_base;
297 ring_size = rxq->rx_ring_size;
298 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800299 }
300
301 if (fep->bufdesc_ex)
302 return (struct bufdesc *)((ex_new_bd < ex_base) ?
303 (ex_new_bd + ring_size) : ex_new_bd);
Frank Liff43da82013-01-03 16:04:23 +0000304 else
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +0800305 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
Frank Liff43da82013-01-03 16:04:23 +0000306}
307
Nimrod Andy61a44272014-06-12 08:16:18 +0800308static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
309 struct fec_enet_private *fep)
310{
311 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
312}
313
Fugang Duan4d494cd2014-09-13 05:00:48 +0800314static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
315 struct fec_enet_priv_tx_q *txq)
Nimrod Andy6e909282014-06-12 08:16:22 +0800316{
317 int entries;
318
Fugang Duan4d494cd2014-09-13 05:00:48 +0800319 entries = ((const char *)txq->dirty_tx -
320 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
Nimrod Andy6e909282014-06-12 08:16:22 +0800321
Fugang Duan4d494cd2014-09-13 05:00:48 +0800322 return entries > 0 ? entries : entries + txq->tx_ring_size;
Nimrod Andy6e909282014-06-12 08:16:22 +0800323}
324
Shawn Guob5680e02011-01-05 21:13:13 +0000325static void *swap_buffer(void *bufaddr, int len)
326{
327 int i;
328 unsigned int *buf = bufaddr;
329
Fabio Estevamffed61e2013-05-21 05:44:26 +0000330 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
Shawn Guob5680e02011-01-05 21:13:13 +0000331 *buf = cpu_to_be32(*buf);
332
333 return bufaddr;
334}
335
Russell King344756f2014-07-08 13:01:59 +0100336static void fec_dump(struct net_device *ndev)
337{
338 struct fec_enet_private *fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800339 struct bufdesc *bdp;
340 struct fec_enet_priv_tx_q *txq;
341 int index = 0;
Russell King344756f2014-07-08 13:01:59 +0100342
343 netdev_info(ndev, "TX ring dump\n");
344 pr_info("Nr SC addr len SKB\n");
345
Fugang Duan4d494cd2014-09-13 05:00:48 +0800346 txq = fep->tx_queue[0];
347 bdp = txq->tx_bd_base;
348
Russell King344756f2014-07-08 13:01:59 +0100349 do {
350 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
351 index,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800352 bdp == txq->cur_tx ? 'S' : ' ',
353 bdp == txq->dirty_tx ? 'H' : ' ',
Russell King344756f2014-07-08 13:01:59 +0100354 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
Fugang Duan4d494cd2014-09-13 05:00:48 +0800355 txq->tx_skbuff[index]);
356 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
Russell King344756f2014-07-08 13:01:59 +0100357 index++;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800358 } while (bdp != txq->tx_bd_base);
Russell King344756f2014-07-08 13:01:59 +0100359}
360
Fugang Duan62a02c92014-06-18 08:33:52 +0800361static inline bool is_ipv4_pkt(struct sk_buff *skb)
362{
363 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
364}
365
Jim Baxter4c09eed2013-04-19 08:10:49 +0000366static int
367fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
368{
369 /* Only run for packets requiring a checksum. */
370 if (skb->ip_summed != CHECKSUM_PARTIAL)
371 return 0;
372
373 if (unlikely(skb_cow_head(skb, 0)))
374 return -1;
375
Fugang Duan62a02c92014-06-18 08:33:52 +0800376 if (is_ipv4_pkt(skb))
377 ip_hdr(skb)->check = 0;
Jim Baxter4c09eed2013-04-19 08:10:49 +0000378 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
379
380 return 0;
381}
382
Nimrod Andy6e909282014-06-12 08:16:22 +0800383static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800384fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
385 struct sk_buff *skb,
386 struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800387{
388 struct fec_enet_private *fep = netdev_priv(ndev);
389 const struct platform_device_id *id_entry =
390 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800391 struct bufdesc *bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800392 struct bufdesc_ex *ebdp;
393 int nr_frags = skb_shinfo(skb)->nr_frags;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800394 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy6e909282014-06-12 08:16:22 +0800395 int frag, frag_len;
396 unsigned short status;
397 unsigned int estatus = 0;
398 skb_frag_t *this_frag;
399 unsigned int index;
400 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100401 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800402 int i;
403
404 for (frag = 0; frag < nr_frags; frag++) {
405 this_frag = &skb_shinfo(skb)->frags[frag];
Fugang Duan4d494cd2014-09-13 05:00:48 +0800406 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800407 ebdp = (struct bufdesc_ex *)bdp;
408
409 status = bdp->cbd_sc;
410 status &= ~BD_ENET_TX_STATS;
411 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
412 frag_len = skb_shinfo(skb)->frags[frag].size;
413
414 /* Handle the last BD specially */
415 if (frag == nr_frags - 1) {
416 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
417 if (fep->bufdesc_ex) {
418 estatus |= BD_ENET_TX_INT;
419 if (unlikely(skb_shinfo(skb)->tx_flags &
420 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
421 estatus |= BD_ENET_TX_TS;
422 }
423 }
424
425 if (fep->bufdesc_ex) {
426 if (skb->ip_summed == CHECKSUM_PARTIAL)
427 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
428 ebdp->cbd_bdu = 0;
429 ebdp->cbd_esc = estatus;
430 }
431
432 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
433
Fugang Duan4d494cd2014-09-13 05:00:48 +0800434 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800435 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
436 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800437 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
438 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800439
440 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
441 swap_buffer(bufaddr, frag_len);
442 }
443
Russell Kingd6bf3142014-07-08 00:23:19 +0100444 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
445 DMA_TO_DEVICE);
446 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800447 dev_kfree_skb_any(skb);
448 if (net_ratelimit())
449 netdev_err(ndev, "Tx DMA memory map failed\n");
450 goto dma_mapping_error;
451 }
452
Russell Kingd6bf3142014-07-08 00:23:19 +0100453 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800454 bdp->cbd_datlen = frag_len;
455 bdp->cbd_sc = status;
456 }
457
Fugang Duan4d494cd2014-09-13 05:00:48 +0800458 txq->cur_tx = bdp;
Nimrod Andy6e909282014-06-12 08:16:22 +0800459
460 return 0;
461
462dma_mapping_error:
Fugang Duan4d494cd2014-09-13 05:00:48 +0800463 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800464 for (i = 0; i < frag; i++) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800465 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy6e909282014-06-12 08:16:22 +0800466 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
467 bdp->cbd_datlen, DMA_TO_DEVICE);
468 }
469 return NETDEV_TX_OK;
470}
471
Fugang Duan4d494cd2014-09-13 05:00:48 +0800472static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
473 struct sk_buff *skb, struct net_device *ndev)
Nimrod Andy6e909282014-06-12 08:16:22 +0800474{
475 struct fec_enet_private *fep = netdev_priv(ndev);
476 const struct platform_device_id *id_entry =
477 platform_get_device_id(fep->pdev);
478 int nr_frags = skb_shinfo(skb)->nr_frags;
479 struct bufdesc *bdp, *last_bdp;
480 void *bufaddr;
Russell Kingd6bf3142014-07-08 00:23:19 +0100481 dma_addr_t addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800482 unsigned short status;
483 unsigned short buflen;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800484 unsigned short queue;
Nimrod Andy6e909282014-06-12 08:16:22 +0800485 unsigned int estatus = 0;
486 unsigned int index;
Nimrod Andy79f33912014-06-12 08:16:23 +0800487 int entries_free;
Nimrod Andy6e909282014-06-12 08:16:22 +0800488 int ret;
489
Fugang Duan4d494cd2014-09-13 05:00:48 +0800490 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
Nimrod Andy79f33912014-06-12 08:16:23 +0800491 if (entries_free < MAX_SKB_FRAGS + 1) {
492 dev_kfree_skb_any(skb);
493 if (net_ratelimit())
494 netdev_err(ndev, "NOT enough BD for SG!\n");
495 return NETDEV_TX_OK;
496 }
497
Nimrod Andy6e909282014-06-12 08:16:22 +0800498 /* Protocol checksum off-load for TCP and UDP. */
499 if (fec_enet_clear_csum(skb, ndev)) {
500 dev_kfree_skb_any(skb);
501 return NETDEV_TX_OK;
502 }
503
504 /* Fill in a Tx ring entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800505 bdp = txq->cur_tx;
Nimrod Andy6e909282014-06-12 08:16:22 +0800506 status = bdp->cbd_sc;
507 status &= ~BD_ENET_TX_STATS;
508
509 /* Set buffer length and buffer pointer */
510 bufaddr = skb->data;
511 buflen = skb_headlen(skb);
512
Fugang Duan4d494cd2014-09-13 05:00:48 +0800513 queue = skb_get_queue_mapping(skb);
514 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800515 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
516 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800517 memcpy(txq->tx_bounce[index], skb->data, buflen);
518 bufaddr = txq->tx_bounce[index];
Nimrod Andy6e909282014-06-12 08:16:22 +0800519
520 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
521 swap_buffer(bufaddr, buflen);
522 }
523
Russell Kingd6bf3142014-07-08 00:23:19 +0100524 /* Push the data cache so the CPM does not get stale memory data. */
525 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
526 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy6e909282014-06-12 08:16:22 +0800527 dev_kfree_skb_any(skb);
528 if (net_ratelimit())
529 netdev_err(ndev, "Tx DMA memory map failed\n");
530 return NETDEV_TX_OK;
531 }
532
533 if (nr_frags) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800534 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800535 if (ret)
536 return ret;
537 } else {
538 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
539 if (fep->bufdesc_ex) {
540 estatus = BD_ENET_TX_INT;
541 if (unlikely(skb_shinfo(skb)->tx_flags &
542 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
543 estatus |= BD_ENET_TX_TS;
544 }
545 }
546
547 if (fep->bufdesc_ex) {
548
549 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
550
551 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
552 fep->hwts_tx_en))
553 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
554
555 if (skb->ip_summed == CHECKSUM_PARTIAL)
556 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
557
558 ebdp->cbd_bdu = 0;
559 ebdp->cbd_esc = estatus;
560 }
561
Fugang Duan4d494cd2014-09-13 05:00:48 +0800562 last_bdp = txq->cur_tx;
563 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
Nimrod Andy6e909282014-06-12 08:16:22 +0800564 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800565 txq->tx_skbuff[index] = skb;
Nimrod Andy6e909282014-06-12 08:16:22 +0800566
567 bdp->cbd_datlen = buflen;
Russell Kingd6bf3142014-07-08 00:23:19 +0100568 bdp->cbd_bufaddr = addr;
Nimrod Andy6e909282014-06-12 08:16:22 +0800569
570 /* Send it on its way. Tell FEC it's ready, interrupt when done,
571 * it's the last BD of the frame, and to put the CRC on the end.
572 */
573 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
574 bdp->cbd_sc = status;
575
Sascha Hauer22f6b862009-04-15 01:32:18 +0000576 /* If this was the last BD in the ring, start at the beginning again. */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800577 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Eric Dumazet7a2a8452013-12-19 10:53:02 -0800579 skb_tx_timestamp(skb);
580
Fugang Duan4d494cd2014-09-13 05:00:48 +0800581 txq->cur_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Frank Lide5fb0a2013-03-03 17:34:25 +0000583 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800584 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Frank Lide5fb0a2013-03-03 17:34:25 +0000585
Nimrod Andy6e909282014-06-12 08:16:22 +0800586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Nimrod Andy79f33912014-06-12 08:16:23 +0800589static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800590fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
591 struct net_device *ndev,
592 struct bufdesc *bdp, int index, char *data,
593 int size, bool last_tcp, bool is_last)
Nimrod Andy79f33912014-06-12 08:16:23 +0800594{
595 struct fec_enet_private *fep = netdev_priv(ndev);
596 const struct platform_device_id *id_entry =
597 platform_get_device_id(fep->pdev);
598 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
599 unsigned short status;
600 unsigned int estatus = 0;
Russell Kingd6bf3142014-07-08 00:23:19 +0100601 dma_addr_t addr;
Nimrod Andy79f33912014-06-12 08:16:23 +0800602
603 status = bdp->cbd_sc;
604 status &= ~BD_ENET_TX_STATS;
605
606 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
Nimrod Andy79f33912014-06-12 08:16:23 +0800607
608 if (((unsigned long) data) & FEC_ALIGNMENT ||
609 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800610 memcpy(txq->tx_bounce[index], data, size);
611 data = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800612
613 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
614 swap_buffer(data, size);
615 }
616
Russell Kingd6bf3142014-07-08 00:23:19 +0100617 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
618 if (dma_mapping_error(&fep->pdev->dev, addr)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800619 dev_kfree_skb_any(skb);
620 if (net_ratelimit())
621 netdev_err(ndev, "Tx DMA memory map failed\n");
622 return NETDEV_TX_BUSY;
623 }
624
Russell Kingd6bf3142014-07-08 00:23:19 +0100625 bdp->cbd_datlen = size;
626 bdp->cbd_bufaddr = addr;
627
Nimrod Andy79f33912014-06-12 08:16:23 +0800628 if (fep->bufdesc_ex) {
629 if (skb->ip_summed == CHECKSUM_PARTIAL)
630 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
631 ebdp->cbd_bdu = 0;
632 ebdp->cbd_esc = estatus;
633 }
634
635 /* Handle the last BD specially */
636 if (last_tcp)
637 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
638 if (is_last) {
639 status |= BD_ENET_TX_INTR;
640 if (fep->bufdesc_ex)
641 ebdp->cbd_esc |= BD_ENET_TX_INT;
642 }
643
644 bdp->cbd_sc = status;
645
646 return 0;
647}
648
649static int
Fugang Duan4d494cd2014-09-13 05:00:48 +0800650fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
651 struct sk_buff *skb, struct net_device *ndev,
652 struct bufdesc *bdp, int index)
Nimrod Andy79f33912014-06-12 08:16:23 +0800653{
654 struct fec_enet_private *fep = netdev_priv(ndev);
655 const struct platform_device_id *id_entry =
656 platform_get_device_id(fep->pdev);
657 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
658 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
659 void *bufaddr;
660 unsigned long dmabuf;
661 unsigned short status;
662 unsigned int estatus = 0;
663
664 status = bdp->cbd_sc;
665 status &= ~BD_ENET_TX_STATS;
666 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
667
Fugang Duan4d494cd2014-09-13 05:00:48 +0800668 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
669 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800670 if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
671 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
Fugang Duan4d494cd2014-09-13 05:00:48 +0800672 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
673 bufaddr = txq->tx_bounce[index];
Nimrod Andy79f33912014-06-12 08:16:23 +0800674
675 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
676 swap_buffer(bufaddr, hdr_len);
677
678 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
679 hdr_len, DMA_TO_DEVICE);
680 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
681 dev_kfree_skb_any(skb);
682 if (net_ratelimit())
683 netdev_err(ndev, "Tx DMA memory map failed\n");
684 return NETDEV_TX_BUSY;
685 }
686 }
687
688 bdp->cbd_bufaddr = dmabuf;
689 bdp->cbd_datlen = hdr_len;
690
691 if (fep->bufdesc_ex) {
692 if (skb->ip_summed == CHECKSUM_PARTIAL)
693 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
694 ebdp->cbd_bdu = 0;
695 ebdp->cbd_esc = estatus;
696 }
697
698 bdp->cbd_sc = status;
699
700 return 0;
701}
702
Fugang Duan4d494cd2014-09-13 05:00:48 +0800703static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
704 struct sk_buff *skb,
705 struct net_device *ndev)
Nimrod Andy79f33912014-06-12 08:16:23 +0800706{
707 struct fec_enet_private *fep = netdev_priv(ndev);
708 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
709 int total_len, data_left;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800710 struct bufdesc *bdp = txq->cur_tx;
711 unsigned short queue = skb_get_queue_mapping(skb);
Nimrod Andy79f33912014-06-12 08:16:23 +0800712 struct tso_t tso;
713 unsigned int index = 0;
714 int ret;
715
Fugang Duan4d494cd2014-09-13 05:00:48 +0800716 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
Nimrod Andy79f33912014-06-12 08:16:23 +0800717 dev_kfree_skb_any(skb);
718 if (net_ratelimit())
719 netdev_err(ndev, "NOT enough BD for TSO!\n");
720 return NETDEV_TX_OK;
721 }
722
723 /* Protocol checksum off-load for TCP and UDP. */
724 if (fec_enet_clear_csum(skb, ndev)) {
725 dev_kfree_skb_any(skb);
726 return NETDEV_TX_OK;
727 }
728
729 /* Initialize the TSO handler, and prepare the first payload */
730 tso_start(skb, &tso);
731
732 total_len = skb->len - hdr_len;
733 while (total_len > 0) {
734 char *hdr;
735
Fugang Duan4d494cd2014-09-13 05:00:48 +0800736 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Nimrod Andy79f33912014-06-12 08:16:23 +0800737 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
738 total_len -= data_left;
739
740 /* prepare packet headers: MAC + IP + TCP */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800741 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
Nimrod Andy79f33912014-06-12 08:16:23 +0800742 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800743 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
Nimrod Andy79f33912014-06-12 08:16:23 +0800744 if (ret)
745 goto err_release;
746
747 while (data_left > 0) {
748 int size;
749
750 size = min_t(int, tso.size, data_left);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800751 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
752 index = fec_enet_get_bd_index(txq->tx_bd_base,
753 bdp, fep);
754 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
755 bdp, index,
756 tso.data, size,
757 size == data_left,
Nimrod Andy79f33912014-06-12 08:16:23 +0800758 total_len == 0);
759 if (ret)
760 goto err_release;
761
762 data_left -= size;
763 tso_build_data(skb, &tso, size);
764 }
765
Fugang Duan4d494cd2014-09-13 05:00:48 +0800766 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
Nimrod Andy79f33912014-06-12 08:16:23 +0800767 }
768
769 /* Save skb pointer */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800770 txq->tx_skbuff[index] = skb;
Nimrod Andy79f33912014-06-12 08:16:23 +0800771
Nimrod Andy79f33912014-06-12 08:16:23 +0800772 skb_tx_timestamp(skb);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800773 txq->cur_tx = bdp;
Nimrod Andy79f33912014-06-12 08:16:23 +0800774
775 /* Trigger transmission start */
Fugang Duan4d494cd2014-09-13 05:00:48 +0800776 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
Nimrod Andy79f33912014-06-12 08:16:23 +0800777
778 return 0;
779
780err_release:
781 /* TODO: Release all used data descriptors for TSO */
782 return ret;
783}
784
Nimrod Andy61a44272014-06-12 08:16:18 +0800785static netdev_tx_t
786fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
787{
788 struct fec_enet_private *fep = netdev_priv(ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800789 int entries_free;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800790 unsigned short queue;
791 struct fec_enet_priv_tx_q *txq;
792 struct netdev_queue *nq;
Nimrod Andy61a44272014-06-12 08:16:18 +0800793 int ret;
794
Fugang Duan4d494cd2014-09-13 05:00:48 +0800795 queue = skb_get_queue_mapping(skb);
796 txq = fep->tx_queue[queue];
797 nq = netdev_get_tx_queue(ndev, queue);
798
Nimrod Andy79f33912014-06-12 08:16:23 +0800799 if (skb_is_gso(skb))
Fugang Duan4d494cd2014-09-13 05:00:48 +0800800 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
Nimrod Andy79f33912014-06-12 08:16:23 +0800801 else
Fugang Duan4d494cd2014-09-13 05:00:48 +0800802 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
Nimrod Andy6e909282014-06-12 08:16:22 +0800803 if (ret)
804 return ret;
Nimrod Andy61a44272014-06-12 08:16:18 +0800805
Fugang Duan4d494cd2014-09-13 05:00:48 +0800806 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
807 if (entries_free <= txq->tx_stop_threshold)
808 netif_tx_stop_queue(nq);
Nimrod Andy61a44272014-06-12 08:16:18 +0800809
810 return NETDEV_TX_OK;
811}
812
Frank Li14109a52013-03-26 16:12:03 +0000813/* Init RX & TX buffer descriptors
814 */
815static void fec_enet_bd_init(struct net_device *dev)
816{
817 struct fec_enet_private *fep = netdev_priv(dev);
Fugang Duan4d494cd2014-09-13 05:00:48 +0800818 struct fec_enet_priv_tx_q *txq;
819 struct fec_enet_priv_rx_q *rxq;
Frank Li14109a52013-03-26 16:12:03 +0000820 struct bufdesc *bdp;
821 unsigned int i;
Frank Li59d0f7462014-09-13 05:00:50 +0800822 unsigned int q;
Frank Li14109a52013-03-26 16:12:03 +0000823
Frank Li59d0f7462014-09-13 05:00:50 +0800824 for (q = 0; q < fep->num_rx_queues; q++) {
825 /* Initialize the receive buffer descriptors. */
826 rxq = fep->rx_queue[q];
827 bdp = rxq->rx_bd_base;
Fugang Duan4d494cd2014-09-13 05:00:48 +0800828
Frank Li59d0f7462014-09-13 05:00:50 +0800829 for (i = 0; i < rxq->rx_ring_size; i++) {
Frank Li14109a52013-03-26 16:12:03 +0000830
Frank Li59d0f7462014-09-13 05:00:50 +0800831 /* Initialize the BD for every fragment in the page. */
832 if (bdp->cbd_bufaddr)
833 bdp->cbd_sc = BD_ENET_RX_EMPTY;
834 else
835 bdp->cbd_sc = 0;
836 bdp = fec_enet_get_nextdesc(bdp, fep, q);
Frank Li14109a52013-03-26 16:12:03 +0000837 }
Frank Li59d0f7462014-09-13 05:00:50 +0800838
839 /* Set the last buffer to wrap */
840 bdp = fec_enet_get_prevdesc(bdp, fep, q);
841 bdp->cbd_sc |= BD_SC_WRAP;
842
843 rxq->cur_rx = rxq->rx_bd_base;
Frank Li14109a52013-03-26 16:12:03 +0000844 }
845
Frank Li59d0f7462014-09-13 05:00:50 +0800846 for (q = 0; q < fep->num_tx_queues; q++) {
847 /* ...and the same for transmit */
848 txq = fep->tx_queue[q];
849 bdp = txq->tx_bd_base;
850 txq->cur_tx = bdp;
851
852 for (i = 0; i < txq->tx_ring_size; i++) {
853 /* Initialize the BD for every fragment in the page. */
854 bdp->cbd_sc = 0;
855 if (txq->tx_skbuff[i]) {
856 dev_kfree_skb_any(txq->tx_skbuff[i]);
857 txq->tx_skbuff[i] = NULL;
858 }
859 bdp->cbd_bufaddr = 0;
860 bdp = fec_enet_get_nextdesc(bdp, fep, q);
861 }
862
863 /* Set the last buffer to wrap */
864 bdp = fec_enet_get_prevdesc(bdp, fep, q);
865 bdp->cbd_sc |= BD_SC_WRAP;
866 txq->dirty_tx = bdp;
867 }
868}
869
Frank Lice99d0d2014-09-13 05:00:52 +0800870static void fec_enet_active_rxring(struct net_device *ndev)
871{
872 struct fec_enet_private *fep = netdev_priv(ndev);
873 int i;
874
875 for (i = 0; i < fep->num_rx_queues; i++)
876 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
877}
878
Frank Li59d0f7462014-09-13 05:00:50 +0800879static void fec_enet_enable_ring(struct net_device *ndev)
880{
881 struct fec_enet_private *fep = netdev_priv(ndev);
882 struct fec_enet_priv_tx_q *txq;
883 struct fec_enet_priv_rx_q *rxq;
884 int i;
885
886 for (i = 0; i < fep->num_rx_queues; i++) {
887 rxq = fep->rx_queue[i];
888 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
889
890 /* enable DMA1/2 */
891 if (i)
892 writel(RCMR_MATCHEN | RCMR_CMP(i),
893 fep->hwp + FEC_RCMR(i));
894 }
895
896 for (i = 0; i < fep->num_tx_queues; i++) {
897 txq = fep->tx_queue[i];
898 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
899
900 /* enable DMA1/2 */
901 if (i)
902 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
903 fep->hwp + FEC_DMA_CFG(i));
904 }
905}
906
907static void fec_enet_reset_skb(struct net_device *ndev)
908{
909 struct fec_enet_private *fep = netdev_priv(ndev);
910 struct fec_enet_priv_tx_q *txq;
911 int i, j;
912
913 for (i = 0; i < fep->num_tx_queues; i++) {
914 txq = fep->tx_queue[i];
915
916 for (j = 0; j < txq->tx_ring_size; j++) {
917 if (txq->tx_skbuff[j]) {
918 dev_kfree_skb_any(txq->tx_skbuff[j]);
919 txq->tx_skbuff[j] = NULL;
920 }
921 }
922 }
Frank Li14109a52013-03-26 16:12:03 +0000923}
924
Russell Kingdbc64a82014-07-08 12:40:12 +0100925/*
926 * This function is called to start or restart the FEC during a link
927 * change, transmit timeout, or to reconfigure the FEC. The network
928 * packet processing for this device must be stopped before this call.
Uwe Kleine-König45993652011-01-19 20:47:04 +0100929 */
930static void
Russell Kingef833372014-07-08 12:40:43 +0100931fec_restart(struct net_device *ndev)
Uwe Kleine-König45993652011-01-19 20:47:04 +0100932{
933 struct fec_enet_private *fep = netdev_priv(ndev);
934 const struct platform_device_id *id_entry =
935 platform_get_device_id(fep->pdev);
Jim Baxter4c09eed2013-04-19 08:10:49 +0000936 u32 val;
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100937 u32 temp_mac[2];
938 u32 rcntl = OPT_FRAME_SIZE | 0x04;
Shawn Guo230dec62011-09-23 02:12:48 +0000939 u32 ecntl = 0x2; /* ETHEREN */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100940
Fugang Duan106c3142014-09-13 05:00:51 +0800941 /* Whack a reset. We should wait for this.
942 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
943 * instead of reset MAC itself.
944 */
945 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
946 writel(0, fep->hwp + FEC_ECNTRL);
947 } else {
948 writel(1, fep->hwp + FEC_ECNTRL);
949 udelay(10);
950 }
Uwe Kleine-König45993652011-01-19 20:47:04 +0100951
952 /*
953 * enet-mac reset will reset mac address registers too,
954 * so need to reconfigure it.
955 */
956 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
957 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
958 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
959 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
960 }
961
962 /* Clear any outstanding interrupt. */
963 writel(0xffc00000, fep->hwp + FEC_IEVENT);
964
Uwe Kleine-König45993652011-01-19 20:47:04 +0100965 /* Set maximum receive buffer size. */
966 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
967
Frank Li14109a52013-03-26 16:12:03 +0000968 fec_enet_bd_init(ndev);
969
Frank Li59d0f7462014-09-13 05:00:50 +0800970 fec_enet_enable_ring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100971
Frank Li59d0f7462014-09-13 05:00:50 +0800972 /* Reset tx SKB buffers. */
973 fec_enet_reset_skb(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +0100974
975 /* Enable MII mode */
Russell Kingef833372014-07-08 12:40:43 +0100976 if (fep->full_duplex == DUPLEX_FULL) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100977 /* FD enable */
Uwe Kleine-König45993652011-01-19 20:47:04 +0100978 writel(0x04, fep->hwp + FEC_X_CNTRL);
979 } else {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100980 /* No Rcv on Xmit */
981 rcntl |= 0x02;
Uwe Kleine-König45993652011-01-19 20:47:04 +0100982 writel(0x0, fep->hwp + FEC_X_CNTRL);
983 }
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +0100984
Uwe Kleine-König45993652011-01-19 20:47:04 +0100985 /* Set MII speed */
986 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
987
Guenter Roeckd1391932013-06-18 10:04:59 -0700988#if !defined(CONFIG_M5272)
Jim Baxter4c09eed2013-04-19 08:10:49 +0000989 /* set RX checksum */
990 val = readl(fep->hwp + FEC_RACC);
991 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
992 val |= FEC_RACC_OPTIONS;
993 else
994 val &= ~FEC_RACC_OPTIONS;
995 writel(val, fep->hwp + FEC_RACC);
Guenter Roeckd1391932013-06-18 10:04:59 -0700996#endif
Jim Baxter4c09eed2013-04-19 08:10:49 +0000997
Uwe Kleine-König45993652011-01-19 20:47:04 +0100998 /*
999 * The phy interface and speed need to get configured
1000 * differently on enet-mac.
1001 */
1002 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001003 /* Enable flow control and length check */
1004 rcntl |= 0x40000000 | 0x00000020;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001005
Shawn Guo230dec62011-09-23 02:12:48 +00001006 /* RGMII, RMII or MII */
1007 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
1008 rcntl |= (1 << 6);
1009 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001010 rcntl |= (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001011 else
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001012 rcntl &= ~(1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001013
Shawn Guo230dec62011-09-23 02:12:48 +00001014 /* 1G, 100M or 10M */
1015 if (fep->phy_dev) {
1016 if (fep->phy_dev->speed == SPEED_1000)
1017 ecntl |= (1 << 5);
1018 else if (fep->phy_dev->speed == SPEED_100)
1019 rcntl &= ~(1 << 9);
1020 else
1021 rcntl |= (1 << 9);
1022 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001023 } else {
1024#ifdef FEC_MIIGSK_ENR
Shawn Guo0ca1e292011-07-01 18:11:22 +08001025 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
Eric Benard8d82f212012-01-12 06:10:28 +00001026 u32 cfgr;
Uwe Kleine-König45993652011-01-19 20:47:04 +01001027 /* disable the gasket and wait */
1028 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1029 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1030 udelay(1);
1031
1032 /*
1033 * configure the gasket:
1034 * RMII, 50 MHz, no loopback, no echo
Shawn Guo0ca1e292011-07-01 18:11:22 +08001035 * MII, 25 MHz, no loopback, no echo
Uwe Kleine-König45993652011-01-19 20:47:04 +01001036 */
Eric Benard8d82f212012-01-12 06:10:28 +00001037 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1038 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1039 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1040 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1041 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001042
1043 /* re-enable the gasket */
1044 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1045 }
1046#endif
1047 }
Frank Libaa70a52013-01-16 16:55:58 +00001048
Guenter Roeckd1391932013-06-18 10:04:59 -07001049#if !defined(CONFIG_M5272)
Frank Libaa70a52013-01-16 16:55:58 +00001050 /* enable pause frame*/
1051 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1052 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1053 fep->phy_dev && fep->phy_dev->pause)) {
1054 rcntl |= FEC_ENET_FCE;
1055
Jim Baxter4c09eed2013-04-19 08:10:49 +00001056 /* set FIFO threshold parameter to reduce overrun */
Frank Libaa70a52013-01-16 16:55:58 +00001057 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1058 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1059 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1060 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1061
1062 /* OPD */
1063 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1064 } else {
1065 rcntl &= ~FEC_ENET_FCE;
1066 }
Guenter Roeckd1391932013-06-18 10:04:59 -07001067#endif /* !defined(CONFIG_M5272) */
Frank Libaa70a52013-01-16 16:55:58 +00001068
Uwe Kleine-Königcd1f4022011-01-25 22:11:25 +01001069 writel(rcntl, fep->hwp + FEC_R_CNTRL);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001070
Stefan Wahren84fe6182014-03-12 11:28:19 +01001071 /* Setup multicast filter. */
1072 set_multicast_list(ndev);
1073#ifndef CONFIG_M5272
1074 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1075 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1076#endif
1077
Shawn Guo230dec62011-09-23 02:12:48 +00001078 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1079 /* enable ENET endian swap */
1080 ecntl |= (1 << 8);
1081 /* enable ENET store and forward mode */
1082 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1083 }
1084
Frank Liff43da82013-01-03 16:04:23 +00001085 if (fep->bufdesc_ex)
1086 ecntl |= (1 << 4);
Frank Li6605b732012-10-30 18:25:31 +00001087
Chris Healy38ae92d2013-06-25 23:18:52 -07001088#ifndef CONFIG_M5272
Jim Baxterb9eef552013-07-01 14:57:54 +01001089 /* Enable the MIB statistic event counters */
1090 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
Chris Healy38ae92d2013-06-25 23:18:52 -07001091#endif
1092
Uwe Kleine-König45993652011-01-19 20:47:04 +01001093 /* And last, enable the transmit and receive processing */
Shawn Guo230dec62011-09-23 02:12:48 +00001094 writel(ecntl, fep->hwp + FEC_ECNTRL);
Frank Lice99d0d2014-09-13 05:00:52 +08001095 fec_enet_active_rxring(ndev);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001096
Frank Liff43da82013-01-03 16:04:23 +00001097 if (fep->bufdesc_ex)
1098 fec_ptp_start_cyclecounter(ndev);
1099
Uwe Kleine-König45993652011-01-19 20:47:04 +01001100 /* Enable interrupts we wish to service */
1101 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1102}
1103
1104static void
1105fec_stop(struct net_device *ndev)
1106{
1107 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guo230dec62011-09-23 02:12:48 +00001108 const struct platform_device_id *id_entry =
1109 platform_get_device_id(fep->pdev);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001110 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
Uwe Kleine-König45993652011-01-19 20:47:04 +01001111
1112 /* We cannot expect a graceful transmit stop without link !!! */
1113 if (fep->link) {
1114 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1115 udelay(10);
1116 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
Joe Perches31b77202013-04-13 19:03:17 +00001117 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
Uwe Kleine-König45993652011-01-19 20:47:04 +01001118 }
1119
Fugang Duan106c3142014-09-13 05:00:51 +08001120 /* Whack a reset. We should wait for this.
1121 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1122 * instead of reset MAC itself.
1123 */
1124 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1125 writel(0, fep->hwp + FEC_ECNTRL);
1126 } else {
1127 writel(1, fep->hwp + FEC_ECNTRL);
1128 udelay(10);
1129 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001130 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1131 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Shawn Guo230dec62011-09-23 02:12:48 +00001132
1133 /* We have to keep ENET enabled to have MII interrupt stay working */
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001134 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
Shawn Guo230dec62011-09-23 02:12:48 +00001135 writel(2, fep->hwp + FEC_ECNTRL);
Lothar Waßmann42431dc2011-12-07 21:59:30 +00001136 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1137 }
Uwe Kleine-König45993652011-01-19 20:47:04 +01001138}
1139
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static void
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001142fec_timeout(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001144 struct fec_enet_private *fep = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Russell King344756f2014-07-08 13:01:59 +01001146 fec_dump(ndev);
1147
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001148 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Russell King36cdc742014-07-08 13:01:44 +01001150 schedule_work(&fep->tx_timeout_work);
Frank Li54309fa2013-05-07 14:08:44 +00001151}
1152
Russell King36cdc742014-07-08 13:01:44 +01001153static void fec_enet_timeout_work(struct work_struct *work)
Frank Li54309fa2013-05-07 14:08:44 +00001154{
1155 struct fec_enet_private *fep =
Russell King36cdc742014-07-08 13:01:44 +01001156 container_of(work, struct fec_enet_private, tx_timeout_work);
Russell King8ce56242014-07-08 12:40:07 +01001157 struct net_device *ndev = fep->netdev;
Frank Li54309fa2013-05-07 14:08:44 +00001158
Russell King36cdc742014-07-08 13:01:44 +01001159 rtnl_lock();
1160 if (netif_device_present(ndev) || netif_running(ndev)) {
1161 napi_disable(&fep->napi);
1162 netif_tx_lock_bh(ndev);
1163 fec_restart(ndev);
1164 netif_wake_queue(ndev);
1165 netif_tx_unlock_bh(ndev);
1166 napi_enable(&fep->napi);
Frank Li54309fa2013-05-07 14:08:44 +00001167 }
Russell King36cdc742014-07-08 13:01:44 +01001168 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171static void
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001172fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1173 struct skb_shared_hwtstamps *hwtstamps)
1174{
1175 unsigned long flags;
1176 u64 ns;
1177
1178 spin_lock_irqsave(&fep->tmreg_lock, flags);
1179 ns = timecounter_cyc2time(&fep->tc, ts);
1180 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1181
1182 memset(hwtstamps, 0, sizeof(*hwtstamps));
1183 hwtstamps->hwtstamp = ns_to_ktime(ns);
1184}
1185
1186static void
Fugang Duan4d494cd2014-09-13 05:00:48 +08001187fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 struct fec_enet_private *fep;
Sascha Hauer2e285322009-04-15 01:32:16 +00001190 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001191 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 struct sk_buff *skb;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001193 struct fec_enet_priv_tx_q *txq;
1194 struct netdev_queue *nq;
Frank Lide5fb0a2013-03-03 17:34:25 +00001195 int index = 0;
Nimrod Andy79f33912014-06-12 08:16:23 +08001196 int entries_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001198 fep = netdev_priv(ndev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001199
1200 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1201
1202 txq = fep->tx_queue[queue_id];
1203 /* get next bdp of dirty_tx */
1204 nq = netdev_get_tx_queue(ndev, queue_id);
1205 bdp = txq->dirty_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Frank Lide5fb0a2013-03-03 17:34:25 +00001207 /* get next bdp of dirty_tx */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001208 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Frank Lide5fb0a2013-03-03 17:34:25 +00001209
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001210 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
Frank Lide5fb0a2013-03-03 17:34:25 +00001211
1212 /* current queue is empty */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001213 if (bdp == txq->cur_tx)
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001214 break;
1215
Fugang Duan4d494cd2014-09-13 05:00:48 +08001216 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
Frank Lide5fb0a2013-03-03 17:34:25 +00001217
Fugang Duan4d494cd2014-09-13 05:00:48 +08001218 skb = txq->tx_skbuff[index];
1219 txq->tx_skbuff[index] = NULL;
1220 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
Nimrod Andy79f33912014-06-12 08:16:23 +08001221 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1222 bdp->cbd_datlen, DMA_TO_DEVICE);
Sebastian Siewior2488a542013-12-02 10:52:55 +01001223 bdp->cbd_bufaddr = 0;
Nimrod Andy6e909282014-06-12 08:16:22 +08001224 if (!skb) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001225 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Nimrod Andy6e909282014-06-12 08:16:22 +08001226 continue;
1227 }
Frank Lide5fb0a2013-03-03 17:34:25 +00001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* Check for errors. */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001230 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 BD_ENET_TX_RL | BD_ENET_TX_UN |
1232 BD_ENET_TX_CSL)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001233 ndev->stats.tx_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001234 if (status & BD_ENET_TX_HB) /* No heartbeat */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001235 ndev->stats.tx_heartbeat_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001236 if (status & BD_ENET_TX_LC) /* Late collision */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001237 ndev->stats.tx_window_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001238 if (status & BD_ENET_TX_RL) /* Retrans limit */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001239 ndev->stats.tx_aborted_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001240 if (status & BD_ENET_TX_UN) /* Underrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001241 ndev->stats.tx_fifo_errors++;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001242 if (status & BD_ENET_TX_CSL) /* Carrier lost */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001243 ndev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 } else {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001245 ndev->stats.tx_packets++;
Nimrod Andy6e909282014-06-12 08:16:22 +08001246 ndev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
Frank Liff43da82013-01-03 16:04:23 +00001249 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1250 fep->bufdesc_ex) {
Frank Li6605b732012-10-30 18:25:31 +00001251 struct skb_shared_hwtstamps shhwtstamps;
Frank Liff43da82013-01-03 16:04:23 +00001252 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
Frank Li6605b732012-10-30 18:25:31 +00001253
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001254 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
Frank Li6605b732012-10-30 18:25:31 +00001255 skb_tstamp_tx(skb, &shhwtstamps);
1256 }
Frank Liff43da82013-01-03 16:04:23 +00001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* Deferred means some collisions occurred during transmit,
1259 * but we eventually sent the packet OK.
1260 */
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001261 if (status & BD_ENET_TX_DEF)
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001262 ndev->stats.collisions++;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001263
Sascha Hauer22f6b862009-04-15 01:32:18 +00001264 /* Free the sk buffer associated with this last transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 dev_kfree_skb_any(skb);
Frank Lide5fb0a2013-03-03 17:34:25 +00001266
Fugang Duan4d494cd2014-09-13 05:00:48 +08001267 txq->dirty_tx = bdp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001268
Sascha Hauer22f6b862009-04-15 01:32:18 +00001269 /* Update pointer to next buffer descriptor to be transmitted */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001270 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001271
Sascha Hauer22f6b862009-04-15 01:32:18 +00001272 /* Since we have freed up a buffer, the ring is no longer full
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Nimrod Andy79f33912014-06-12 08:16:23 +08001274 if (netif_queue_stopped(ndev)) {
Fugang Duan4d494cd2014-09-13 05:00:48 +08001275 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1276 if (entries_free >= txq->tx_wake_threshold)
1277 netif_tx_wake_queue(nq);
Nimrod Andy79f33912014-06-12 08:16:23 +08001278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Russell Kingccea2962014-07-08 13:01:38 +01001280
1281 /* ERR006538: Keep the transmitter going */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001282 if (bdp != txq->cur_tx &&
1283 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1284 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1285}
1286
1287static void
1288fec_enet_tx(struct net_device *ndev)
1289{
1290 struct fec_enet_private *fep = netdev_priv(ndev);
1291 u16 queue_id;
1292 /* First process class A queue, then Class B and Best Effort queue */
1293 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1294 clear_bit(queue_id, &fep->work_tx);
1295 fec_enet_tx_queue(ndev, queue_id);
1296 }
1297 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300/* During a receive, the cur_rx points to the current incoming buffer.
1301 * When we update through the ring, if the next incoming buffer has
1302 * not been given to the system, we just set the empty indicator,
1303 * effectively tossing the packet.
1304 */
Frank Lidc975382013-01-28 18:31:42 +00001305static int
Fugang Duan4d494cd2014-09-13 05:00:48 +08001306fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001308 struct fec_enet_private *fep = netdev_priv(ndev);
Shawn Guob5680e02011-01-05 21:13:13 +00001309 const struct platform_device_id *id_entry =
1310 platform_get_device_id(fep->pdev);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001311 struct fec_enet_priv_rx_q *rxq;
Sascha Hauer2e285322009-04-15 01:32:16 +00001312 struct bufdesc *bdp;
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001313 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 struct sk_buff *skb;
1315 ushort pkt_len;
1316 __u8 *data;
Frank Lidc975382013-01-28 18:31:42 +00001317 int pkt_received = 0;
Jim Baxtercdffcf12013-07-02 22:52:56 +01001318 struct bufdesc_ex *ebdp = NULL;
1319 bool vlan_packet_rcvd = false;
1320 u16 vlan_tag;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001321 int index = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001322
Greg Ungerer0e702ab2006-06-27 13:19:33 +10001323#ifdef CONFIG_M532x
1324 flush_cache_all();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001325#endif
Fugang Duan4d494cd2014-09-13 05:00:48 +08001326 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1327 rxq = fep->rx_queue[queue_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /* First, grab all of the stats for the incoming packet.
1330 * These get messed up if we get called due to a busy condition.
1331 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001332 bdp = rxq->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Sascha Hauer22f6b862009-04-15 01:32:18 +00001334 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Frank Lidc975382013-01-28 18:31:42 +00001336 if (pkt_received >= budget)
1337 break;
1338 pkt_received++;
1339
Sascha Hauer22f6b862009-04-15 01:32:18 +00001340 /* Since we have allocated space to hold a complete frame,
1341 * the last indicator should be set.
1342 */
1343 if ((status & BD_ENET_RX_LAST) == 0)
Joe Perches31b77202013-04-13 19:03:17 +00001344 netdev_err(ndev, "rcv is not +last\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Russell Kingdb3421c2014-07-08 13:01:49 +01001346
Sascha Hauer22f6b862009-04-15 01:32:18 +00001347 /* Check for errors. */
1348 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001350 ndev->stats.rx_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001351 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1352 /* Frame too long or too short. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001353 ndev->stats.rx_length_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001354 }
1355 if (status & BD_ENET_RX_NO) /* Frame alignment */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001356 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001357 if (status & BD_ENET_RX_CR) /* CRC Error */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001358 ndev->stats.rx_crc_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001359 if (status & BD_ENET_RX_OV) /* FIFO overrun */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001360 ndev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Sascha Hauer22f6b862009-04-15 01:32:18 +00001362
1363 /* Report late collisions as a frame error.
1364 * On this error, the BD is closed, but we don't know what we
1365 * have in the buffer. So, just drop this frame on the floor.
1366 */
1367 if (status & BD_ENET_RX_CL) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001368 ndev->stats.rx_errors++;
1369 ndev->stats.rx_frame_errors++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001370 goto rx_processing_done;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Sascha Hauer22f6b862009-04-15 01:32:18 +00001373 /* Process the incoming frame. */
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001374 ndev->stats.rx_packets++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001375 pkt_len = bdp->cbd_datlen;
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001376 ndev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Fugang Duan4d494cd2014-09-13 05:00:48 +08001378 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
1379 data = rxq->rx_skbuff[index]->data;
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001380 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1381 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauerccdc4f12009-01-28 23:03:09 +00001382
Shawn Guob5680e02011-01-05 21:13:13 +00001383 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1384 swap_buffer(data, pkt_len);
1385
Jim Baxtercdffcf12013-07-02 22:52:56 +01001386 /* Extract the enhanced buffer descriptor */
1387 ebdp = NULL;
1388 if (fep->bufdesc_ex)
1389 ebdp = (struct bufdesc_ex *)bdp;
1390
1391 /* If this is a VLAN packet remove the VLAN Tag */
1392 vlan_packet_rcvd = false;
1393 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Fugang Duan4d494cd2014-09-13 05:00:48 +08001394 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001395 /* Push and remove the vlan tag */
1396 struct vlan_hdr *vlan_header =
1397 (struct vlan_hdr *) (data + ETH_HLEN);
1398 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1399 pkt_len -= VLAN_HLEN;
1400
1401 vlan_packet_rcvd = true;
1402 }
1403
Sascha Hauer22f6b862009-04-15 01:32:18 +00001404 /* This does 16 byte alignment, exactly what we need.
1405 * The packet length includes FCS, but we don't want to
1406 * include that when passing upstream as it messes up
1407 * bridging applications.
1408 */
Fabio Estevamb72061a2012-02-07 08:27:31 +00001409 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Sascha Hauer85498892009-04-15 01:32:21 +00001411 if (unlikely(!skb)) {
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001412 ndev->stats.rx_dropped++;
Sascha Hauer22f6b862009-04-15 01:32:18 +00001413 } else {
Jim Baxtercdffcf12013-07-02 22:52:56 +01001414 int payload_offset = (2 * ETH_ALEN);
Sascha Hauer85498892009-04-15 01:32:21 +00001415 skb_reserve(skb, NET_IP_ALIGN);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001416 skb_put(skb, pkt_len - 4); /* Make room */
Jim Baxtercdffcf12013-07-02 22:52:56 +01001417
1418 /* Extract the frame data without the VLAN header. */
1419 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1420 if (vlan_packet_rcvd)
1421 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
Fugang Duan4d494cd2014-09-13 05:00:48 +08001422 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
Jim Baxtercdffcf12013-07-02 22:52:56 +01001423 data + payload_offset,
1424 pkt_len - 4 - (2 * ETH_ALEN));
1425
Uwe Kleine-Königc5561672011-01-19 11:58:12 +01001426 skb->protocol = eth_type_trans(skb, ndev);
Frank Liff43da82013-01-03 16:04:23 +00001427
Frank Li6605b732012-10-30 18:25:31 +00001428 /* Get receive timestamp from the skb */
Russell Kingbfd4ecd2014-07-08 13:02:09 +01001429 if (fep->hwts_rx_en && fep->bufdesc_ex)
1430 fec_enet_hwtstamp(fep, ebdp->ts,
1431 skb_hwtstamps(skb));
Frank Liff43da82013-01-03 16:04:23 +00001432
Jim Baxter4c09eed2013-04-19 08:10:49 +00001433 if (fep->bufdesc_ex &&
Jim Baxtercdffcf12013-07-02 22:52:56 +01001434 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
Jim Baxter4c09eed2013-04-19 08:10:49 +00001435 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1436 /* don't check it */
1437 skb->ip_summed = CHECKSUM_UNNECESSARY;
1438 } else {
1439 skb_checksum_none_assert(skb);
1440 }
1441 }
1442
Jim Baxtercdffcf12013-07-02 22:52:56 +01001443 /* Handle received VLAN packets */
1444 if (vlan_packet_rcvd)
1445 __vlan_hwaccel_put_tag(skb,
1446 htons(ETH_P_8021Q),
1447 vlan_tag);
1448
Richard Cochran0affdf32013-08-30 20:28:10 +02001449 napi_gro_receive(&fep->napi, skb);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001450 }
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00001451
Duan Fugang-B38611d842a312013-11-14 09:57:10 +08001452 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1453 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
Sascha Hauer22f6b862009-04-15 01:32:18 +00001454rx_processing_done:
1455 /* Clear the status flags for this buffer */
1456 status &= ~BD_ENET_RX_STATS;
1457
1458 /* Mark the buffer empty */
1459 status |= BD_ENET_RX_EMPTY;
1460 bdp->cbd_sc = status;
1461
Frank Liff43da82013-01-03 16:04:23 +00001462 if (fep->bufdesc_ex) {
1463 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1464
1465 ebdp->cbd_esc = BD_ENET_RX_INT;
1466 ebdp->cbd_prot = 0;
1467 ebdp->cbd_bdu = 0;
1468 }
Frank Li6605b732012-10-30 18:25:31 +00001469
Sascha Hauer22f6b862009-04-15 01:32:18 +00001470 /* Update BD pointer to next entry */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001471 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
Duan Fugang-B3861136e24e22013-09-03 10:41:18 +08001472
Sascha Hauer22f6b862009-04-15 01:32:18 +00001473 /* Doing this here will keep the FEC running while we process
1474 * incoming frames. On a heavily loaded network, we should be
1475 * able to keep up at the expense of system resources.
1476 */
Fugang Duan4d494cd2014-09-13 05:00:48 +08001477 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Fugang Duan4d494cd2014-09-13 05:00:48 +08001479 rxq->cur_rx = bdp;
Frank Lidc975382013-01-28 18:31:42 +00001480 return pkt_received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Fugang Duan4d494cd2014-09-13 05:00:48 +08001483static int
1484fec_enet_rx(struct net_device *ndev, int budget)
1485{
1486 int pkt_received = 0;
1487 u16 queue_id;
1488 struct fec_enet_private *fep = netdev_priv(ndev);
1489
1490 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1491 clear_bit(queue_id, &fep->work_rx);
1492 pkt_received += fec_enet_rx_queue(ndev,
1493 budget - pkt_received, queue_id);
1494 }
1495 return pkt_received;
1496}
1497
1498static bool
1499fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1500{
1501 if (int_events == 0)
1502 return false;
1503
1504 if (int_events & FEC_ENET_RXF)
1505 fep->work_rx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001506 if (int_events & FEC_ENET_RXF_1)
1507 fep->work_rx |= (1 << 0);
1508 if (int_events & FEC_ENET_RXF_2)
1509 fep->work_rx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001510
1511 if (int_events & FEC_ENET_TXF)
1512 fep->work_tx |= (1 << 2);
Frank Lice99d0d2014-09-13 05:00:52 +08001513 if (int_events & FEC_ENET_TXF_1)
1514 fep->work_tx |= (1 << 0);
1515 if (int_events & FEC_ENET_TXF_2)
1516 fep->work_tx |= (1 << 1);
Fugang Duan4d494cd2014-09-13 05:00:48 +08001517
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 Li59d0f7462014-09-13 05:00:50 +08002286 unsigned int q;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002287
Frank Li59d0f7462014-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 Li59d0f7462014-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 Li59d0f7462014-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 Li59d0f7462014-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 Li59d0f7462014-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 Li59d0f7462014-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 Li59d0f7462014-09-13 05:00:50 +08002434 return 0;
Sascha Hauerf0b3fbe2009-04-15 01:32:24 +00002435
Frank Li59d0f7462014-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 Li59d0f7462014-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 Li59d0f7462014-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 Li59d0f7462014-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 Li59d0f7462014-09-13 05:00:50 +08002742 unsigned int i;
Nimrod Andy55d02182014-06-12 08:16:21 +08002743
Frank Li59d0f7462014-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 Li59d0f7462014-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");